blob: 2f4850f25e82f0f009b8f614b593fc74eb5247aa [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
Marc Zyngier12de0a32018-05-23 18:41:37 +0100212static void xhci_zero_64b_regs(struct xhci_hcd *xhci)
213{
214 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
215 int err, i;
216 u64 val;
217
218 /*
219 * Some Renesas controllers get into a weird state if they are
220 * reset while programmed with 64bit addresses (they will preserve
221 * the top half of the address in internal, non visible
222 * registers). You end up with half the address coming from the
223 * kernel, and the other half coming from the firmware. Also,
224 * changing the programming leads to extra accesses even if the
225 * controller is supposed to be halted. The controller ends up with
226 * a fatal fault, and is then ripe for being properly reset.
227 *
228 * Special care is taken to only apply this if the device is behind
229 * an iommu. Doing anything when there is no iommu is definitely
230 * unsafe...
231 */
232 if (!(xhci->quirks & XHCI_ZERO_64B_REGS) || !dev->iommu_group)
233 return;
234
235 xhci_info(xhci, "Zeroing 64bit base registers, expecting fault\n");
236
237 /* Clear HSEIE so that faults do not get signaled */
238 val = readl(&xhci->op_regs->command);
239 val &= ~CMD_HSEIE;
240 writel(val, &xhci->op_regs->command);
241
242 /* Clear HSE (aka FATAL) */
243 val = readl(&xhci->op_regs->status);
244 val |= STS_FATAL;
245 writel(val, &xhci->op_regs->status);
246
247 /* Now zero the registers, and brace for impact */
248 val = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
249 if (upper_32_bits(val))
250 xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
251 val = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
252 if (upper_32_bits(val))
253 xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring);
254
255 for (i = 0; i < HCS_MAX_INTRS(xhci->hcs_params1); i++) {
256 struct xhci_intr_reg __iomem *ir;
257
258 ir = &xhci->run_regs->ir_set[i];
259 val = xhci_read_64(xhci, &ir->erst_base);
260 if (upper_32_bits(val))
261 xhci_write_64(xhci, 0, &ir->erst_base);
262 val= xhci_read_64(xhci, &ir->erst_dequeue);
263 if (upper_32_bits(val))
264 xhci_write_64(xhci, 0, &ir->erst_dequeue);
265 }
266
267 /* Wait for the fault to appear. It will be cleared on reset */
268 err = xhci_handshake(&xhci->op_regs->status,
269 STS_FATAL, STS_FATAL,
270 XHCI_MAX_HALT_USEC);
271 if (!err)
272 xhci_info(xhci, "Fault detected\n");
273}
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300274
yuan linyu2c93e792017-02-25 19:20:55 +0800275#ifdef CONFIG_USB_PCI
Dong Nguyen43b86af2010-07-21 16:56:08 -0700276/*
277 * Set up MSI
278 */
279static int xhci_setup_msi(struct xhci_hcd *xhci)
280{
281 int ret;
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800282 /*
283 * TODO:Check with MSI Soc for sysdev
284 */
Dong Nguyen43b86af2010-07-21 16:56:08 -0700285 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
286
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300287 ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
288 if (ret < 0) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300289 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
290 "failed to allocate MSI entry");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700291 return ret;
292 }
293
Alex Shi851ec162013-05-24 10:54:19 +0800294 ret = request_irq(pdev->irq, xhci_msi_irq,
Dong Nguyen43b86af2010-07-21 16:56:08 -0700295 0, "xhci_hcd", xhci_to_hcd(xhci));
296 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300297 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
298 "disable MSI interrupt");
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300299 pci_free_irq_vectors(pdev);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700300 }
301
302 return ret;
303}
304
305/*
306 * Set up MSI-X
307 */
308static int xhci_setup_msix(struct xhci_hcd *xhci)
309{
310 int i, ret = 0;
Andiry Xu00292272010-12-27 17:39:02 +0800311 struct usb_hcd *hcd = xhci_to_hcd(xhci);
312 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700313
314 /*
315 * calculate number of msi-x vectors supported.
316 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
317 * with max number of interrupters based on the xhci HCSPARAMS1.
318 * - num_online_cpus: maximum msi-x vectors per CPUs core.
319 * Add additional 1 vector to ensure always available interrupt.
320 */
321 xhci->msix_count = min(num_online_cpus() + 1,
322 HCS_MAX_INTRS(xhci->hcs_params1));
323
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300324 ret = pci_alloc_irq_vectors(pdev, xhci->msix_count, xhci->msix_count,
325 PCI_IRQ_MSIX);
326 if (ret < 0) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300327 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
328 "Failed to enable MSI-X");
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300329 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700330 }
331
Dong Nguyen43b86af2010-07-21 16:56:08 -0700332 for (i = 0; i < xhci->msix_count; i++) {
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300333 ret = request_irq(pci_irq_vector(pdev, i), xhci_msi_irq, 0,
334 "xhci_hcd", xhci_to_hcd(xhci));
Dong Nguyen43b86af2010-07-21 16:56:08 -0700335 if (ret)
336 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700337 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700338
Andiry Xu00292272010-12-27 17:39:02 +0800339 hcd->msix_enabled = 1;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700340 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700341
342disable_msix:
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300343 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt");
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300344 while (--i >= 0)
345 free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci));
346 pci_free_irq_vectors(pdev);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700347 return ret;
348}
349
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700350/* Free any IRQs and disable MSI-X */
351static void xhci_cleanup_msix(struct xhci_hcd *xhci)
352{
Andiry Xu00292272010-12-27 17:39:02 +0800353 struct usb_hcd *hcd = xhci_to_hcd(xhci);
354 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700355
Jack Pham90053552013-11-15 14:53:14 -0800356 if (xhci->quirks & XHCI_PLAT)
357 return;
358
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300359 /* return if using legacy interrupt */
360 if (hcd->irq > 0)
361 return;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700362
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300363 if (hcd->msix_enabled) {
364 int i;
365
366 for (i = 0; i < xhci->msix_count; i++)
367 free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci));
Dong Nguyen43b86af2010-07-21 16:56:08 -0700368 } else {
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300369 free_irq(pci_irq_vector(pdev, 0), xhci_to_hcd(xhci));
Dong Nguyen43b86af2010-07-21 16:56:08 -0700370 }
371
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300372 pci_free_irq_vectors(pdev);
Andiry Xu00292272010-12-27 17:39:02 +0800373 hcd->msix_enabled = 0;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700374}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700375
Olof Johanssond5c82fe2013-07-23 11:58:20 -0700376static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700377{
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300378 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700379
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300380 if (hcd->msix_enabled) {
381 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
382 int i;
383
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700384 for (i = 0; i < xhci->msix_count; i++)
Christoph Hellwig77d45b42017-04-19 16:55:49 +0300385 synchronize_irq(pci_irq_vector(pdev, i));
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700386 }
387}
388
389static int xhci_try_enable_msi(struct usb_hcd *hcd)
390{
391 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp52fb6122013-08-08 10:08:34 -0700392 struct pci_dev *pdev;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700393 int ret;
394
Sarah Sharp52fb6122013-08-08 10:08:34 -0700395 /* The xhci platform device has set up IRQs through usb_add_hcd. */
396 if (xhci->quirks & XHCI_PLAT)
397 return 0;
398
399 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700400 /*
401 * Some Fresco Logic host controllers advertise MSI, but fail to
402 * generate interrupts. Don't even try to enable MSI.
403 */
404 if (xhci->quirks & XHCI_BROKEN_MSI)
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100405 goto legacy_irq;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700406
407 /* unregister the legacy interrupt */
408 if (hcd->irq)
409 free_irq(hcd->irq, hcd);
Felipe Balbicd704692012-02-29 16:46:23 +0200410 hcd->irq = 0;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700411
412 ret = xhci_setup_msix(xhci);
413 if (ret)
414 /* fall back to msi*/
415 ret = xhci_setup_msi(xhci);
416
Peter Chen6a29bee2017-05-17 18:32:02 +0300417 if (!ret) {
418 hcd->msi_enabled = 1;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700419 return 0;
Peter Chen6a29bee2017-05-17 18:32:02 +0300420 }
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700421
Sarah Sharp68d07f62012-02-13 16:25:57 -0800422 if (!pdev->irq) {
423 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
424 return -EINVAL;
425 }
426
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100427 legacy_irq:
Adrian Huang79699432014-02-27 11:26:03 +0000428 if (!strlen(hcd->irq_descr))
429 snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
430 hcd->driver->description, hcd->self.busnum);
431
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700432 /* fall back to legacy interrupt*/
433 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
434 hcd->irq_descr, hcd);
435 if (ret) {
436 xhci_err(xhci, "request interrupt %d failed\n",
437 pdev->irq);
438 return ret;
439 }
440 hcd->irq = pdev->irq;
441 return 0;
442}
443
444#else
445
David Cohen01bb59e2014-04-25 19:20:16 +0300446static inline int xhci_try_enable_msi(struct usb_hcd *hcd)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700447{
448 return 0;
449}
450
David Cohen01bb59e2014-04-25 19:20:16 +0300451static inline void xhci_cleanup_msix(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700452{
453}
454
David Cohen01bb59e2014-04-25 19:20:16 +0300455static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700456{
457}
458
459#endif
460
Kees Cooke99e88a2017-10-16 14:43:17 -0700461static void compliance_mode_recovery(struct timer_list *t)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500462{
463 struct xhci_hcd *xhci;
464 struct usb_hcd *hcd;
Mathias Nyman38986ff2018-05-21 16:40:01 +0300465 struct xhci_hub *rhub;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500466 u32 temp;
467 int i;
468
Kees Cooke99e88a2017-10-16 14:43:17 -0700469 xhci = from_timer(xhci, t, comp_mode_recovery_timer);
Mathias Nyman38986ff2018-05-21 16:40:01 +0300470 rhub = &xhci->usb3_rhub;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500471
Mathias Nyman38986ff2018-05-21 16:40:01 +0300472 for (i = 0; i < rhub->num_ports; i++) {
473 temp = readl(rhub->ports[i]->addr);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500474 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
475 /*
476 * Compliance Mode Detected. Letting USB Core
477 * handle the Warm Reset
478 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300479 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
480 "Compliance mode detected->port %d",
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500481 i + 1);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300482 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
483 "Attempting compliance mode recovery");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500484 hcd = xhci->shared_hcd;
485
486 if (hcd->state == HC_STATE_SUSPENDED)
487 usb_hcd_resume_root_hub(hcd);
488
489 usb_hcd_poll_rh_status(hcd);
490 }
491 }
492
Mathias Nyman38986ff2018-05-21 16:40:01 +0300493 if (xhci->port_status_u0 != ((1 << rhub->num_ports) - 1))
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500494 mod_timer(&xhci->comp_mode_recovery_timer,
495 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
496}
497
498/*
499 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
500 * that causes ports behind that hardware to enter compliance mode sometimes.
501 * The quirk creates a timer that polls every 2 seconds the link state of
502 * each host controller's port and recovers it by issuing a Warm reset
503 * if Compliance mode is detected, otherwise the port will become "dead" (no
504 * device connections or disconnections will be detected anymore). Becasue no
505 * status event is generated when entering compliance mode (per xhci spec),
506 * this quirk is needed on systems that have the failing hardware installed.
507 */
508static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
509{
510 xhci->port_status_u0 = 0;
Kees Cooke99e88a2017-10-16 14:43:17 -0700511 timer_setup(&xhci->comp_mode_recovery_timer, compliance_mode_recovery,
512 0);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500513 xhci->comp_mode_recovery_timer.expires = jiffies +
514 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
515
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500516 add_timer(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300517 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
518 "Compliance mode recovery timer initialized");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500519}
520
521/*
522 * This function identifies the systems that have installed the SN65LVPE502CP
523 * USB3.0 re-driver and that need the Compliance Mode Quirk.
524 * Systems:
525 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
526 */
Andrew Brestickere1cd9722014-10-03 11:35:27 +0300527static bool xhci_compliance_mode_recovery_timer_quirk_check(void)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500528{
529 const char *dmi_product_name, *dmi_sys_vendor;
530
531 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
532 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
Vivek Gautam457a73d2012-09-22 18:11:19 +0530533 if (!dmi_product_name || !dmi_sys_vendor)
534 return false;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500535
536 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
537 return false;
538
539 if (strstr(dmi_product_name, "Z420") ||
540 strstr(dmi_product_name, "Z620") ||
Alexis R. Cortes47080972012-10-17 14:09:12 -0500541 strstr(dmi_product_name, "Z820") ||
Alexis R. Cortesb0e4e602012-11-08 16:59:27 -0600542 strstr(dmi_product_name, "Z1 Workstation"))
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500543 return true;
544
545 return false;
546}
547
548static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
549{
Mathias Nyman38986ff2018-05-21 16:40:01 +0300550 return (xhci->port_status_u0 == ((1 << xhci->usb3_rhub.num_ports) - 1));
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500551}
552
553
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700554/*
555 * Initialize memory for HCD and xHC (one-time init).
556 *
557 * Program the PAGESIZE register, initialize the device context array, create
558 * device contexts (?), set up a command ring segment (or two?), create event
559 * ring (one for now).
560 */
Lu Baolu39693842017-04-07 17:57:04 +0300561static int xhci_init(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700562{
563 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
564 int retval = 0;
565
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300566 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700567 spin_lock_init(&xhci->lock);
Sebastian Andrzej Siewiord7826592011-09-13 16:41:10 -0700568 if (xhci->hci_version == 0x95 && link_quirk) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300569 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
570 "QUIRK: Not clearing Link TRB chain bits.");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700571 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
572 } else {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300573 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
574 "xHCI doesn't need link TRB QUIRK");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700575 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700576 retval = xhci_mem_init(xhci, GFP_KERNEL);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300577 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700578
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500579 /* Initializing Compliance Mode Recovery Data If Needed */
Sarah Sharpc3897aa2013-04-18 10:02:03 -0700580 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500581 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
582 compliance_mode_recovery_timer_init(xhci);
583 }
584
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700585 return retval;
586}
587
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700588/*-------------------------------------------------------------------------*/
589
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700590
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800591static int xhci_run_finished(struct xhci_hcd *xhci)
592{
593 if (xhci_start(xhci)) {
594 xhci_halt(xhci);
595 return -ENODEV;
596 }
597 xhci->shared_hcd->state = HC_STATE_RUNNING;
Elric Fuc181bc52012-06-27 16:30:57 +0800598 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800599
600 if (xhci->quirks & XHCI_NEC_HOST)
601 xhci_ring_cmd_db(xhci);
602
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300603 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
604 "Finished xhci_run for USB3 roothub");
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800605 return 0;
606}
607
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700608/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700609 * Start the HC after it was halted.
610 *
611 * This function is called by the USB core when the HC driver is added.
612 * Its opposite is xhci_stop().
613 *
614 * xhci_init() must be called once before this function can be called.
615 * Reset the HC, enable device slot contexts, program DCBAAP, and
616 * set command ring pointer and event ring pointer.
617 *
618 * Setup MSI-X vectors and enable interrupts.
619 */
620int xhci_run(struct usb_hcd *hcd)
621{
622 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700623 u64 temp_64;
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700624 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700625 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700626
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800627 /* Start the xHCI host controller running only after the USB 2.0 roothub
628 * is setup.
629 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700630
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700631 hcd->uses_new_polling = 1;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800632 if (!usb_hcd_is_primary_hcd(hcd))
633 return xhci_run_finished(xhci);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700634
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300635 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700636
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700637 ret = xhci_try_enable_msi(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700638 if (ret)
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700639 return ret;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700640
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800641 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700642 temp_64 &= ~ERST_PTR_MASK;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300643 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
644 "ERST deq = 64'h%0lx", (long unsigned int) temp_64);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700645
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300646 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
647 "// Set the interrupt modulation register");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200648 temp = readl(&xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700649 temp &= ~ER_IRQ_INTERVAL_MASK;
Adam Wallisab725cb2017-12-08 17:59:13 +0200650 temp |= (xhci->imod_interval / 250) & ER_IRQ_INTERVAL_MASK;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200651 writel(temp, &xhci->ir_set->irq_control);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700652
653 /* Set the HCD state before we enable the irqs */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200654 temp = readl(&xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700655 temp |= (CMD_EIE);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300656 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
657 "// Enable interrupts, cmd = 0x%x.", temp);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200658 writel(temp, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700659
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200660 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300661 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
662 "// Enabling event ring interrupter %p by writing 0x%x to irq_pending",
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700663 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200664 writel(ER_IRQ_ENABLE(temp), &xhci->ir_set->irq_pending);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700665
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300666 if (xhci->quirks & XHCI_NEC_HOST) {
667 struct xhci_command *command;
Lu Baolu74e0b562017-04-07 17:57:05 +0300668
Mathias Nyman103afda2017-12-08 17:59:08 +0200669 command = xhci_alloc_command(xhci, false, GFP_KERNEL);
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300670 if (!command)
671 return -ENOMEM;
Lu Baolu74e0b562017-04-07 17:57:05 +0300672
Shu Wangd6f5f072017-07-20 14:48:31 +0300673 ret = xhci_queue_vendor_command(xhci, command, 0, 0, 0,
Sarah Sharp02386342010-05-24 13:25:28 -0700674 TRB_TYPE(TRB_NEC_GET_FW));
Shu Wangd6f5f072017-07-20 14:48:31 +0300675 if (ret)
676 xhci_free_command(xhci, command);
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300677 }
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300678 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
679 "Finished xhci_run for USB2 roothub");
Lu Baolu02b6fdc2017-10-05 11:21:39 +0300680
Lu Baoludfba2172017-12-08 17:59:10 +0200681 xhci_dbc_init(xhci);
682
Lu Baolu02b6fdc2017-10-05 11:21:39 +0300683 xhci_debugfs_init(xhci);
684
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700685 return 0;
686}
Andrew Bresticker436e8c72014-10-03 11:35:28 +0300687EXPORT_SYMBOL_GPL(xhci_run);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700688
689/*
690 * Stop xHCI driver.
691 *
692 * This function is called by the USB core when the HC driver is removed.
693 * Its opposite is xhci_run().
694 *
695 * Disable device contexts, disable IRQs, and quiesce the HC.
696 * Reset the HC, finish any completed transactions, and cleanup memory.
697 */
Lu Baolu39693842017-04-07 17:57:04 +0300698static void xhci_stop(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700699{
700 u32 temp;
701 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
702
Roger Quadros8c24d6d2015-09-21 17:46:14 +0300703 mutex_lock(&xhci->mutex);
Roger Quadros8c24d6d2015-09-21 17:46:14 +0300704
Joel Stanleyfe190ed2017-04-07 17:57:00 +0300705 /* Only halt host and free memory after both hcds are removed */
Gabriel Krisman Bertazi27a41a82016-06-01 18:09:07 +0300706 if (!usb_hcd_is_primary_hcd(hcd)) {
Joel Stanleyfe190ed2017-04-07 17:57:00 +0300707 /* usb core will free this hcd shortly, unset pointer */
708 xhci->shared_hcd = NULL;
Gabriel Krisman Bertazi27a41a82016-06-01 18:09:07 +0300709 mutex_unlock(&xhci->mutex);
710 return;
711 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700712
Lu Baoludfba2172017-12-08 17:59:10 +0200713 xhci_dbc_exit(xhci);
714
Joel Stanleyfe190ed2017-04-07 17:57:00 +0300715 spin_lock_irq(&xhci->lock);
716 xhci->xhc_state |= XHCI_STATE_HALTED;
717 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
718 xhci_halt(xhci);
719 xhci_reset(xhci);
720 spin_unlock_irq(&xhci->lock);
721
Zhang Rui40a9fb12010-12-17 13:17:04 -0800722 xhci_cleanup_msix(xhci);
723
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500724 /* Deleting Compliance Mode Recovery Timer */
725 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
Tony Camuso58b1d792013-04-05 14:27:07 -0400726 (!(xhci_all_ports_seen_u0(xhci)))) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500727 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300728 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
729 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -0400730 __func__);
731 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500732
Andiry Xuc41136b2011-03-22 17:08:14 +0800733 if (xhci->quirks & XHCI_AMD_PLL_FIX)
734 usb_amd_dev_put();
735
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300736 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
737 "// Disabling event ring interrupts");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200738 temp = readl(&xhci->op_regs->status);
Lu Baolud1001ab2017-04-07 17:56:50 +0300739 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200740 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200741 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700742
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300743 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700744 xhci_mem_cleanup(xhci);
Zhengjun Xing11cd7642018-02-12 14:24:51 +0200745 xhci_debugfs_exit(xhci);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300746 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
747 "xhci_stop completed - status = %x",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200748 readl(&xhci->op_regs->status));
Roger Quadros85ac90f2015-09-21 17:46:12 +0300749 mutex_unlock(&xhci->mutex);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700750}
751
752/*
753 * Shutdown HC (not bus-specific)
754 *
755 * This is called when the machine is rebooting or halting. We assume that the
756 * machine will be powered off, and the HC's internal state will be reset.
757 * Don't bother to free memory.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800758 *
759 * This will only ever be called with the main usb_hcd (the USB3 roothub).
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700760 */
Lu Baolu39693842017-04-07 17:57:04 +0300761static void xhci_shutdown(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700762{
763 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
764
Dan Carpenter052c7f92012-08-13 19:57:03 +0300765 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800766 usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev));
Sarah Sharpe95829f2012-07-23 18:59:30 +0300767
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700768 spin_lock_irq(&xhci->lock);
769 xhci_halt(xhci);
Takashi Iwai638298d2013-09-12 08:11:06 +0200770 /* Workaround for spurious wakeups at shutdown with HSW */
771 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
772 xhci_reset(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700773 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700774
Zhang Rui40a9fb12010-12-17 13:17:04 -0800775 xhci_cleanup_msix(xhci);
776
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300777 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
778 "xhci_shutdown completed - status = %x",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200779 readl(&xhci->op_regs->status));
Takashi Iwai638298d2013-09-12 08:11:06 +0200780
781 /* Yet another workaround for spurious wakeups at shutdown with HSW */
782 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +0800783 pci_set_power_state(to_pci_dev(hcd->self.sysdev), PCI_D3hot);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700784}
785
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700786#ifdef CONFIG_PM
Andiry Xu5535b1d52010-10-14 07:23:06 -0700787static void xhci_save_registers(struct xhci_hcd *xhci)
788{
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200789 xhci->s3.command = readl(&xhci->op_regs->command);
790 xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800791 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200792 xhci->s3.config_reg = readl(&xhci->op_regs->config_reg);
793 xhci->s3.erst_size = readl(&xhci->ir_set->erst_size);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800794 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
795 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200796 xhci->s3.irq_pending = readl(&xhci->ir_set->irq_pending);
797 xhci->s3.irq_control = readl(&xhci->ir_set->irq_control);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700798}
799
800static void xhci_restore_registers(struct xhci_hcd *xhci)
801{
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200802 writel(xhci->s3.command, &xhci->op_regs->command);
803 writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
Sarah Sharp477632d2014-01-29 14:02:00 -0800804 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200805 writel(xhci->s3.config_reg, &xhci->op_regs->config_reg);
806 writel(xhci->s3.erst_size, &xhci->ir_set->erst_size);
Sarah Sharp477632d2014-01-29 14:02:00 -0800807 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
808 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200809 writel(xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
810 writel(xhci->s3.irq_control, &xhci->ir_set->irq_control);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700811}
812
Sarah Sharp89821322010-11-12 11:59:31 -0800813static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
814{
815 u64 val_64;
816
817 /* step 2: initialize command ring buffer */
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800818 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
Sarah Sharp89821322010-11-12 11:59:31 -0800819 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
820 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
821 xhci->cmd_ring->dequeue) &
822 (u64) ~CMD_RING_RSVD_BITS) |
823 xhci->cmd_ring->cycle_state;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300824 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
825 "// Setting command ring address to 0x%llx",
Sarah Sharp89821322010-11-12 11:59:31 -0800826 (long unsigned long) val_64);
Sarah Sharp477632d2014-01-29 14:02:00 -0800827 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
Sarah Sharp89821322010-11-12 11:59:31 -0800828}
829
830/*
831 * The whole command ring must be cleared to zero when we suspend the host.
832 *
833 * The host doesn't save the command ring pointer in the suspend well, so we
834 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
835 * aligned, because of the reserved bits in the command ring dequeue pointer
836 * register. Therefore, we can't just set the dequeue pointer back in the
837 * middle of the ring (TRBs are 16-byte aligned).
838 */
839static void xhci_clear_command_ring(struct xhci_hcd *xhci)
840{
841 struct xhci_ring *ring;
842 struct xhci_segment *seg;
843
844 ring = xhci->cmd_ring;
845 seg = ring->deq_seg;
846 do {
Andiry Xu158886c2011-11-30 16:37:41 +0800847 memset(seg->trbs, 0,
848 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
849 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
850 cpu_to_le32(~TRB_CYCLE);
Sarah Sharp89821322010-11-12 11:59:31 -0800851 seg = seg->next;
852 } while (seg != ring->deq_seg);
853
854 /* Reset the software enqueue and dequeue pointers */
855 ring->deq_seg = ring->first_seg;
856 ring->dequeue = ring->first_seg->trbs;
857 ring->enq_seg = ring->deq_seg;
858 ring->enqueue = ring->dequeue;
859
Andiry Xub008df62012-03-05 17:49:34 +0800860 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
Sarah Sharp89821322010-11-12 11:59:31 -0800861 /*
862 * Ring is now zeroed, so the HW should look for change of ownership
863 * when the cycle bit is set to 1.
864 */
865 ring->cycle_state = 1;
866
867 /*
868 * Reset the hardware dequeue pointer.
869 * Yes, this will need to be re-written after resume, but we're paranoid
870 * and want to make sure the hardware doesn't access bogus memory
871 * because, say, the BIOS or an SMI started the host without changing
872 * the command ring pointers.
873 */
874 xhci_set_cmd_ring_deq(xhci);
875}
876
Lu Baolua1377e52014-11-18 11:27:14 +0200877static void xhci_disable_port_wake_on_bits(struct xhci_hcd *xhci)
878{
Mathias Nyman38986ff2018-05-21 16:40:01 +0300879 struct xhci_port **ports;
Lu Baolua1377e52014-11-18 11:27:14 +0200880 int port_index;
Lu Baolua1377e52014-11-18 11:27:14 +0200881 unsigned long flags;
882 u32 t1, t2;
883
884 spin_lock_irqsave(&xhci->lock, flags);
885
Masahiro Yamada8a1115f2017-03-09 16:16:31 -0800886 /* disable usb3 ports Wake bits */
Mathias Nyman38986ff2018-05-21 16:40:01 +0300887 port_index = xhci->usb3_rhub.num_ports;
888 ports = xhci->usb3_rhub.ports;
Lu Baolua1377e52014-11-18 11:27:14 +0200889 while (port_index--) {
Mathias Nyman38986ff2018-05-21 16:40:01 +0300890 t1 = readl(ports[port_index]->addr);
Lu Baolua1377e52014-11-18 11:27:14 +0200891 t1 = xhci_port_state_to_neutral(t1);
892 t2 = t1 & ~PORT_WAKE_BITS;
893 if (t1 != t2)
Mathias Nyman38986ff2018-05-21 16:40:01 +0300894 writel(t2, ports[port_index]->addr);
Lu Baolua1377e52014-11-18 11:27:14 +0200895 }
896
Masahiro Yamada8a1115f2017-03-09 16:16:31 -0800897 /* disable usb2 ports Wake bits */
Mathias Nyman38986ff2018-05-21 16:40:01 +0300898 port_index = xhci->usb2_rhub.num_ports;
899 ports = xhci->usb2_rhub.ports;
Lu Baolua1377e52014-11-18 11:27:14 +0200900 while (port_index--) {
Mathias Nyman38986ff2018-05-21 16:40:01 +0300901 t1 = readl(ports[port_index]->addr);
Lu Baolua1377e52014-11-18 11:27:14 +0200902 t1 = xhci_port_state_to_neutral(t1);
903 t2 = t1 & ~PORT_WAKE_BITS;
904 if (t1 != t2)
Mathias Nyman38986ff2018-05-21 16:40:01 +0300905 writel(t2, ports[port_index]->addr);
Lu Baolua1377e52014-11-18 11:27:14 +0200906 }
907
908 spin_unlock_irqrestore(&xhci->lock, flags);
909}
910
Mathias Nyman229bc192018-06-21 16:19:41 +0300911static bool xhci_pending_portevent(struct xhci_hcd *xhci)
912{
913 struct xhci_port **ports;
914 int port_index;
915 u32 status;
916 u32 portsc;
917
918 status = readl(&xhci->op_regs->status);
919 if (status & STS_EINT)
920 return true;
921 /*
922 * Checking STS_EINT is not enough as there is a lag between a change
923 * bit being set and the Port Status Change Event that it generated
924 * being written to the Event Ring. See note in xhci 1.1 section 4.19.2.
925 */
926
927 port_index = xhci->usb2_rhub.num_ports;
928 ports = xhci->usb2_rhub.ports;
929 while (port_index--) {
930 portsc = readl(ports[port_index]->addr);
931 if (portsc & PORT_CHANGE_MASK ||
932 (portsc & PORT_PLS_MASK) == XDEV_RESUME)
933 return true;
934 }
935 port_index = xhci->usb3_rhub.num_ports;
936 ports = xhci->usb3_rhub.ports;
937 while (port_index--) {
938 portsc = readl(ports[port_index]->addr);
939 if (portsc & PORT_CHANGE_MASK ||
940 (portsc & PORT_PLS_MASK) == XDEV_RESUME)
941 return true;
942 }
943 return false;
944}
945
Andiry Xu5535b1d52010-10-14 07:23:06 -0700946/*
947 * Stop HC (not bus-specific)
948 *
949 * This is called when the machine transition into S3/S4 mode.
950 *
951 */
Lu Baolua1377e52014-11-18 11:27:14 +0200952int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup)
Andiry Xu5535b1d52010-10-14 07:23:06 -0700953{
954 int rc = 0;
Oliver Neukum455f5892013-09-30 15:50:54 +0200955 unsigned int delay = XHCI_MAX_HALT_USEC;
Andiry Xu5535b1d52010-10-14 07:23:06 -0700956 struct usb_hcd *hcd = xhci_to_hcd(xhci);
957 u32 command;
958
Roger Quadros9fa733f2015-05-29 17:01:50 +0300959 if (!hcd->state)
960 return 0;
961
Felipe Balbi77b84762012-10-19 10:55:16 +0300962 if (hcd->state != HC_STATE_SUSPENDED ||
963 xhci->shared_hcd->state != HC_STATE_SUSPENDED)
964 return -EINVAL;
965
Lu Baoludfba2172017-12-08 17:59:10 +0200966 xhci_dbc_suspend(xhci);
967
Lu Baolua1377e52014-11-18 11:27:14 +0200968 /* Clear root port wake on bits if wakeup not allowed. */
969 if (!do_wakeup)
970 xhci_disable_port_wake_on_bits(xhci);
971
Sarah Sharpc52804a2012-11-27 12:30:23 -0800972 /* Don't poll the roothubs on bus suspend. */
973 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
974 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
975 del_timer_sync(&hcd->rh_timer);
Al Cooper14e61a12014-08-20 16:41:57 +0300976 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
977 del_timer_sync(&xhci->shared_hcd->rh_timer);
Sarah Sharpc52804a2012-11-27 12:30:23 -0800978
Kai-Heng Feng191edc52018-03-08 17:17:17 +0200979 if (xhci->quirks & XHCI_SUSPEND_DELAY)
980 usleep_range(1000, 1500);
981
Andiry Xu5535b1d52010-10-14 07:23:06 -0700982 spin_lock_irq(&xhci->lock);
983 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sarah Sharpb32093792011-03-07 11:24:07 -0800984 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700985 /* step 1: stop endpoint */
986 /* skipped assuming that port suspend has done */
987
988 /* step 2: clear Run/Stop bit */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200989 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700990 command &= ~CMD_RUN;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200991 writel(command, &xhci->op_regs->command);
Oliver Neukum455f5892013-09-30 15:50:54 +0200992
993 /* Some chips from Fresco Logic need an extraordinary delay */
994 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
995
Lin Wangdc0b1772015-01-09 16:06:28 +0200996 if (xhci_handshake(&xhci->op_regs->status,
Oliver Neukum455f5892013-09-30 15:50:54 +0200997 STS_HALT, STS_HALT, delay)) {
Andiry Xu5535b1d52010-10-14 07:23:06 -0700998 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
999 spin_unlock_irq(&xhci->lock);
1000 return -ETIMEDOUT;
1001 }
Sarah Sharp89821322010-11-12 11:59:31 -08001002 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001003
1004 /* step 3: save registers */
1005 xhci_save_registers(xhci);
1006
1007 /* step 4: set CSS flag */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001008 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001009 command |= CMD_CSS;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001010 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +02001011 if (xhci_handshake(&xhci->op_regs->status,
Sarah Sharp2611bd182012-10-25 13:27:51 -07001012 STS_SAVE, 0, 10 * 1000)) {
Andiry Xu622eb782012-06-13 10:51:57 +08001013 xhci_warn(xhci, "WARN: xHC save state timeout\n");
Andiry Xu5535b1d52010-10-14 07:23:06 -07001014 spin_unlock_irq(&xhci->lock);
1015 return -ETIMEDOUT;
1016 }
Andiry Xu5535b1d52010-10-14 07:23:06 -07001017 spin_unlock_irq(&xhci->lock);
1018
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001019 /*
1020 * Deleting Compliance Mode Recovery Timer because the xHCI Host
1021 * is about to be suspended.
1022 */
1023 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1024 (!(xhci_all_ports_seen_u0(xhci)))) {
1025 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001026 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1027 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -04001028 __func__);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001029 }
1030
Andiry Xu00292272010-12-27 17:39:02 +08001031 /* step 5: remove core well power */
1032 /* synchronize irq when using MSI-X */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -07001033 xhci_msix_sync_irqs(xhci);
Andiry Xu00292272010-12-27 17:39:02 +08001034
Andiry Xu5535b1d52010-10-14 07:23:06 -07001035 return rc;
1036}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03001037EXPORT_SYMBOL_GPL(xhci_suspend);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001038
1039/*
1040 * start xHC (not bus-specific)
1041 *
1042 * This is called when the machine transition from S3/S4 mode.
1043 *
1044 */
1045int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
1046{
Mathias Nyman229bc192018-06-21 16:19:41 +03001047 u32 command, temp = 0;
Andiry Xu5535b1d52010-10-14 07:23:06 -07001048 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sarah Sharp65b22f92010-12-17 12:35:05 -08001049 struct usb_hcd *secondary_hcd;
Alan Sternf69e31202011-11-03 11:37:10 -04001050 int retval = 0;
Tony Camuso77df9e02013-02-21 16:11:27 -05001051 bool comp_timer_running = false;
Andiry Xu5535b1d52010-10-14 07:23:06 -07001052
Roger Quadros9fa733f2015-05-29 17:01:50 +03001053 if (!hcd->state)
1054 return 0;
1055
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001056 /* Wait a bit if either of the roothubs need to settle from the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001057 * transition into bus suspend.
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001058 */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001059 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
1060 time_before(jiffies,
1061 xhci->bus_state[1].next_statechange))
Andiry Xu5535b1d52010-10-14 07:23:06 -07001062 msleep(100);
1063
Alan Sternf69e31202011-11-03 11:37:10 -04001064 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1065 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
1066
Andiry Xu5535b1d52010-10-14 07:23:06 -07001067 spin_lock_irq(&xhci->lock);
Maarten Lankhorstc877b3b2011-06-15 23:47:21 +02001068 if (xhci->quirks & XHCI_RESET_ON_RESUME)
1069 hibernated = true;
Andiry Xu5535b1d52010-10-14 07:23:06 -07001070
1071 if (!hibernated) {
1072 /* step 1: restore register */
1073 xhci_restore_registers(xhci);
1074 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -08001075 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001076 /* step 3: restore state and start state*/
1077 /* step 3: set CRS flag */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001078 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001079 command |= CMD_CRS;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001080 writel(command, &xhci->op_regs->command);
Ajay Gupta305886c2018-06-21 16:19:45 +03001081 /*
1082 * Some controllers take up to 55+ ms to complete the controller
1083 * restore so setting the timeout to 100ms. Xhci specification
1084 * doesn't mention any timeout value.
1085 */
Lin Wangdc0b1772015-01-09 16:06:28 +02001086 if (xhci_handshake(&xhci->op_regs->status,
Ajay Gupta305886c2018-06-21 16:19:45 +03001087 STS_RESTORE, 0, 100 * 1000)) {
Andiry Xu622eb782012-06-13 10:51:57 +08001088 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
Andiry Xu5535b1d52010-10-14 07:23:06 -07001089 spin_unlock_irq(&xhci->lock);
1090 return -ETIMEDOUT;
1091 }
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001092 temp = readl(&xhci->op_regs->status);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001093 }
1094
1095 /* If restore operation fails, re-initialize the HC during resume */
1096 if ((temp & STS_SRE) || hibernated) {
Tony Camuso77df9e02013-02-21 16:11:27 -05001097
1098 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1099 !(xhci_all_ports_seen_u0(xhci))) {
1100 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001101 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1102 "Compliance Mode Recovery Timer deleted!");
Tony Camuso77df9e02013-02-21 16:11:27 -05001103 }
1104
Sarah Sharpfedd3832011-04-12 17:43:19 -07001105 /* Let the USB core know _both_ roothubs lost power. */
1106 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
1107 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001108
1109 xhci_dbg(xhci, "Stop HCD\n");
1110 xhci_halt(xhci);
Marc Zyngier12de0a32018-05-23 18:41:37 +01001111 xhci_zero_64b_regs(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001112 xhci_reset(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001113 spin_unlock_irq(&xhci->lock);
Andiry Xu00292272010-12-27 17:39:02 +08001114 xhci_cleanup_msix(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001115
Andiry Xu5535b1d52010-10-14 07:23:06 -07001116 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001117 temp = readl(&xhci->op_regs->status);
Lu Baolud1001ab2017-04-07 17:56:50 +03001118 writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001119 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001120 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001121
1122 xhci_dbg(xhci, "cleaning up memory\n");
1123 xhci_mem_cleanup(xhci);
Zhengjun Xingd91676712018-02-12 14:24:49 +02001124 xhci_debugfs_exit(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001125 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001126 readl(&xhci->op_regs->status));
Andiry Xu5535b1d52010-10-14 07:23:06 -07001127
Sarah Sharp65b22f92010-12-17 12:35:05 -08001128 /* USB core calls the PCI reinit and start functions twice:
1129 * first with the primary HCD, and then with the secondary HCD.
1130 * If we don't do the same, the host will never be started.
1131 */
1132 if (!usb_hcd_is_primary_hcd(hcd))
1133 secondary_hcd = hcd;
1134 else
1135 secondary_hcd = xhci->shared_hcd;
1136
1137 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1138 retval = xhci_init(hcd->primary_hcd);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001139 if (retval)
1140 return retval;
Tony Camuso77df9e02013-02-21 16:11:27 -05001141 comp_timer_running = true;
1142
Sarah Sharp65b22f92010-12-17 12:35:05 -08001143 xhci_dbg(xhci, "Start the primary HCD\n");
1144 retval = xhci_run(hcd->primary_hcd);
Sarah Sharpb32093792011-03-07 11:24:07 -08001145 if (!retval) {
Alan Sternf69e31202011-11-03 11:37:10 -04001146 xhci_dbg(xhci, "Start the secondary HCD\n");
1147 retval = xhci_run(secondary_hcd);
Sarah Sharpb32093792011-03-07 11:24:07 -08001148 }
Andiry Xu5535b1d52010-10-14 07:23:06 -07001149 hcd->state = HC_STATE_SUSPENDED;
Sarah Sharpb32093792011-03-07 11:24:07 -08001150 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
Alan Sternf69e31202011-11-03 11:37:10 -04001151 goto done;
Andiry Xu5535b1d52010-10-14 07:23:06 -07001152 }
1153
Andiry Xu5535b1d52010-10-14 07:23:06 -07001154 /* step 4: set Run/Stop bit */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001155 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d52010-10-14 07:23:06 -07001156 command |= CMD_RUN;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001157 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +02001158 xhci_handshake(&xhci->op_regs->status, STS_HALT,
Andiry Xu5535b1d52010-10-14 07:23:06 -07001159 0, 250 * 1000);
1160
1161 /* step 5: walk topology and initialize portsc,
1162 * portpmsc and portli
1163 */
1164 /* this is done in bus_resume */
1165
1166 /* step 6: restart each of the previously
1167 * Running endpoints by ringing their doorbells
1168 */
1169
Andiry Xu5535b1d52010-10-14 07:23:06 -07001170 spin_unlock_irq(&xhci->lock);
Alan Sternf69e31202011-11-03 11:37:10 -04001171
Lu Baoludfba2172017-12-08 17:59:10 +02001172 xhci_dbc_resume(xhci);
1173
Alan Sternf69e31202011-11-03 11:37:10 -04001174 done:
1175 if (retval == 0) {
Wang, Yud6236f62014-06-24 17:14:44 +03001176 /* Resume root hubs only when have pending events. */
Mathias Nyman229bc192018-06-21 16:19:41 +03001177 if (xhci_pending_portevent(xhci)) {
Wang, Yud6236f62014-06-24 17:14:44 +03001178 usb_hcd_resume_root_hub(xhci->shared_hcd);
Mathias Nyman671ffdf2016-04-08 16:25:06 +03001179 usb_hcd_resume_root_hub(hcd);
Wang, Yud6236f62014-06-24 17:14:44 +03001180 }
Alan Sternf69e31202011-11-03 11:37:10 -04001181 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001182
1183 /*
1184 * If system is subject to the Quirk, Compliance Mode Timer needs to
1185 * be re-initialized Always after a system resume. Ports are subject
1186 * to suffer the Compliance Mode issue again. It doesn't matter if
1187 * ports have entered previously to U0 before system's suspension.
1188 */
Tony Camuso77df9e02013-02-21 16:11:27 -05001189 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001190 compliance_mode_recovery_timer_init(xhci);
1191
Jiahau Chang9da5a102017-07-20 14:48:27 +03001192 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
1193 usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller));
1194
Sarah Sharpc52804a2012-11-27 12:30:23 -08001195 /* Re-enable port polling. */
1196 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
Al Cooper14e61a12014-08-20 16:41:57 +03001197 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
1198 usb_hcd_poll_rh_status(xhci->shared_hcd);
Mathias Nyman671ffdf2016-04-08 16:25:06 +03001199 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1200 usb_hcd_poll_rh_status(hcd);
Sarah Sharpc52804a2012-11-27 12:30:23 -08001201
Alan Sternf69e31202011-11-03 11:37:10 -04001202 return retval;
Andiry Xu5535b1d52010-10-14 07:23:06 -07001203}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03001204EXPORT_SYMBOL_GPL(xhci_resume);
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -07001205#endif /* CONFIG_PM */
1206
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001207/*-------------------------------------------------------------------------*/
1208
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001209/**
1210 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1211 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1212 * value to right shift 1 for the bitmask.
1213 *
1214 * Index = (epnum * 2) + direction - 1,
1215 * where direction = 0 for OUT, 1 for IN.
1216 * For control endpoints, the IN index is used (OUT index is unused), so
1217 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1218 */
1219unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1220{
1221 unsigned int index;
1222 if (usb_endpoint_xfer_control(desc))
1223 index = (unsigned int) (usb_endpoint_num(desc)*2);
1224 else
1225 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1226 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1227 return index;
1228}
1229
Julius Werner01c5f442013-04-15 15:55:04 -07001230/* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1231 * address from the XHCI endpoint index.
1232 */
1233unsigned int xhci_get_endpoint_address(unsigned int ep_index)
1234{
1235 unsigned int number = DIV_ROUND_UP(ep_index, 2);
1236 unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
1237 return direction | number;
1238}
1239
Sarah Sharpf94e01862009-04-27 19:58:38 -07001240/* Find the flag for this endpoint (for use in the control context). Use the
1241 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1242 * bit 1, etc.
1243 */
Lu Baolu39693842017-04-07 17:57:04 +03001244static unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001245{
1246 return 1 << (xhci_get_endpoint_index(desc) + 1);
1247}
1248
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001249/* Find the flag for this endpoint (for use in the control context). Use the
1250 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1251 * bit 1, etc.
1252 */
Lu Baolu39693842017-04-07 17:57:04 +03001253static unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001254{
1255 return 1 << (ep_index + 1);
1256}
1257
Sarah Sharpf94e01862009-04-27 19:58:38 -07001258/* Compute the last valid endpoint context index. Basically, this is the
1259 * endpoint index plus one. For slot contexts with more than valid endpoint,
1260 * we find the most significant bit set in the added contexts flags.
1261 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1262 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1263 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001264unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001265{
1266 return fls(added_ctxs) - 1;
1267}
1268
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001269/* Returns 1 if the arguments are OK;
1270 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1271 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -08001272static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -07001273 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1274 const char *func) {
1275 struct xhci_hcd *xhci;
1276 struct xhci_virt_device *virt_dev;
1277
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001278 if (!hcd || (check_ep && !ep) || !udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001279 pr_debug("xHCI %s called with invalid args\n", func);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001280 return -EINVAL;
1281 }
1282 if (!udev->parent) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001283 pr_debug("xHCI %s called for root hub\n", func);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001284 return 0;
1285 }
Andiry Xu64927732010-10-14 07:22:45 -07001286
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001287 xhci = hcd_to_xhci(hcd);
Andiry Xu64927732010-10-14 07:22:45 -07001288 if (check_virt_dev) {
sifram.rajas@gmail.com73ddc242011-09-02 11:06:00 -07001289 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001290 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
1291 func);
Andiry Xu64927732010-10-14 07:22:45 -07001292 return -EINVAL;
1293 }
1294
1295 virt_dev = xhci->devs[udev->slot_id];
1296 if (virt_dev->udev != udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001297 xhci_dbg(xhci, "xHCI %s called with udev and "
Andiry Xu64927732010-10-14 07:22:45 -07001298 "virt_dev does not match\n", func);
1299 return -EINVAL;
1300 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001301 }
Andiry Xu64927732010-10-14 07:22:45 -07001302
Sarah Sharp203a8662013-07-24 10:27:13 -07001303 if (xhci->xhc_state & XHCI_STATE_HALTED)
1304 return -ENODEV;
1305
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001306 return 1;
1307}
1308
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001309static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001310 struct usb_device *udev, struct xhci_command *command,
1311 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001312
1313/*
1314 * Full speed devices may have a max packet size greater than 8 bytes, but the
1315 * USB core doesn't know that until it reads the first 8 bytes of the
1316 * descriptor. If the usb_device's max packet size changes after that point,
1317 * we need to issue an evaluate context command and wait on it.
1318 */
1319static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1320 unsigned int ep_index, struct urb *urb)
1321{
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001322 struct xhci_container_ctx *out_ctx;
1323 struct xhci_input_control_ctx *ctrl_ctx;
1324 struct xhci_ep_ctx *ep_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001325 struct xhci_command *command;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001326 int max_packet_size;
1327 int hw_max_packet_size;
1328 int ret = 0;
1329
1330 out_ctx = xhci->devs[slot_id]->out_ctx;
1331 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001332 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001333 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001334 if (hw_max_packet_size != max_packet_size) {
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001335 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1336 "Max Packet Size for ep 0 changed.");
1337 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1338 "Max packet size in usb_device = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001339 max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001340 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1341 "Max packet size in xHCI HW = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001342 hw_max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001343 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1344 "Issuing evaluate context command.");
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001345
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001346 /* Set up the input context flags for the command */
1347 /* FIXME: This won't work if a non-default control endpoint
1348 * changes max packet sizes.
1349 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001350
Mathias Nyman103afda2017-12-08 17:59:08 +02001351 command = xhci_alloc_command(xhci, true, GFP_KERNEL);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001352 if (!command)
1353 return -ENOMEM;
1354
1355 command->in_ctx = xhci->devs[slot_id]->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001356 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001357 if (!ctrl_ctx) {
1358 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1359 __func__);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001360 ret = -ENOMEM;
1361 goto command_cleanup;
Sarah Sharp92f8e762013-04-23 17:11:14 -07001362 }
1363 /* Set up the modified control endpoint 0 */
1364 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1365 xhci->devs[slot_id]->out_ctx, ep_index);
1366
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001367 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001368 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1369 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
1370
Matt Evans28ccd292011-03-29 13:40:46 +11001371 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001372 ctrl_ctx->drop_flags = 0;
1373
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001374 ret = xhci_configure_endpoint(xhci, urb->dev, command,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001375 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001376
1377 /* Clean up the input context for later use by bandwidth
1378 * functions.
1379 */
Matt Evans28ccd292011-03-29 13:40:46 +11001380 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001381command_cleanup:
1382 kfree(command->completion);
1383 kfree(command);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001384 }
1385 return ret;
1386}
1387
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001388/*
1389 * non-error returns are a promise to giveback() the urb later
1390 * we drop ownership so next owner (or urb unlink) can get it
1391 */
Lu Baolu39693842017-04-07 17:57:04 +03001392static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001393{
1394 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1395 unsigned long flags;
1396 int ret = 0;
Mathias Nyman15febf52018-03-16 16:33:03 +02001397 unsigned int slot_id, ep_index;
1398 unsigned int *ep_state;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001399 struct urb_priv *urb_priv;
Mathias Nyman7e64b032017-01-23 14:20:26 +02001400 int num_tds;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001401
Andiry Xu64927732010-10-14 07:22:45 -07001402 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1403 true, true, __func__) <= 0)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001404 return -EINVAL;
1405
1406 slot_id = urb->dev->slot_id;
1407 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Mathias Nyman15febf52018-03-16 16:33:03 +02001408 ep_state = &xhci->devs[slot_id]->eps[ep_index].ep_state;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001409
Alan Stern541c7d42010-06-22 16:39:10 -04001410 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001411 if (!in_interrupt())
1412 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
Mathias Nyman69694082017-01-23 14:20:27 +02001413 return -ESHUTDOWN;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001414 }
Andiry Xu8e51adc2010-07-22 15:23:31 -07001415
1416 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
Mathias Nymane6f7caa2017-01-23 14:20:24 +02001417 num_tds = urb->number_of_packets;
Reyad Attiyat4758dcd2015-08-06 19:23:58 +03001418 else if (usb_endpoint_is_bulk_out(&urb->ep->desc) &&
1419 urb->transfer_buffer_length > 0 &&
1420 urb->transfer_flags & URB_ZERO_PACKET &&
1421 !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc)))
Mathias Nymane6f7caa2017-01-23 14:20:24 +02001422 num_tds = 2;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001423 else
Mathias Nymane6f7caa2017-01-23 14:20:24 +02001424 num_tds = 1;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001425
1426 urb_priv = kzalloc(sizeof(struct urb_priv) +
Mathias Nyman7e64b032017-01-23 14:20:26 +02001427 num_tds * sizeof(struct xhci_td), mem_flags);
Andiry Xu8e51adc2010-07-22 15:23:31 -07001428 if (!urb_priv)
1429 return -ENOMEM;
1430
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02001431 urb_priv->num_tds = num_tds;
1432 urb_priv->num_tds_done = 0;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001433 urb->hcpriv = urb_priv;
1434
Felipe Balbi5abdc2e2017-01-23 14:20:20 +02001435 trace_xhci_urb_enqueue(urb);
1436
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001437 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1438 /* Check to see if the max packet size for the default control
1439 * endpoint changed during FS device enumeration
1440 */
1441 if (urb->dev->speed == USB_SPEED_FULL) {
1442 ret = xhci_check_maxpacket(xhci, slot_id,
1443 ep_index, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001444 if (ret < 0) {
Lin Wang4daf9df2015-01-09 16:06:31 +02001445 xhci_urb_free_priv(urb_priv);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001446 urb->hcpriv = NULL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001447 return ret;
Sarah Sharpd13565c2011-07-22 14:34:34 -07001448 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001449 }
Mathias Nyman69694082017-01-23 14:20:27 +02001450 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001451
Mathias Nyman69694082017-01-23 14:20:27 +02001452 spin_lock_irqsave(&xhci->lock, flags);
1453
1454 if (xhci->xhc_state & XHCI_STATE_DYING) {
1455 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for non-responsive xHCI host.\n",
1456 urb->ep->desc.bEndpointAddress, urb);
1457 ret = -ESHUTDOWN;
1458 goto free_priv;
1459 }
Mathias Nyman15febf52018-03-16 16:33:03 +02001460 if (*ep_state & (EP_GETTING_STREAMS | EP_GETTING_NO_STREAMS)) {
1461 xhci_warn(xhci, "WARN: Can't enqueue URB, ep in streams transition state %x\n",
1462 *ep_state);
1463 ret = -EINVAL;
1464 goto free_priv;
1465 }
Mathias Nymanf5249462018-03-16 16:33:04 +02001466 if (*ep_state & EP_SOFT_CLEAR_TOGGLE) {
1467 xhci_warn(xhci, "Can't enqueue URB while manually clearing toggle\n");
1468 ret = -EINVAL;
1469 goto free_priv;
1470 }
Mathias Nyman69694082017-01-23 14:20:27 +02001471
1472 switch (usb_endpoint_type(&urb->ep->desc)) {
1473
1474 case USB_ENDPOINT_XFER_CONTROL:
Sarah Sharpb11069f2009-07-27 12:03:23 -07001475 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Mathias Nyman69694082017-01-23 14:20:27 +02001476 slot_id, ep_index);
1477 break;
1478 case USB_ENDPOINT_XFER_BULK:
Mathias Nyman69694082017-01-23 14:20:27 +02001479 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1480 slot_id, ep_index);
1481 break;
Mathias Nyman69694082017-01-23 14:20:27 +02001482 case USB_ENDPOINT_XFER_INT:
Sarah Sharp624defa2009-09-02 12:14:28 -07001483 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1484 slot_id, ep_index);
Mathias Nyman69694082017-01-23 14:20:27 +02001485 break;
Mathias Nyman69694082017-01-23 14:20:27 +02001486 case USB_ENDPOINT_XFER_ISOC:
Andiry Xu787f4e52010-07-22 15:23:52 -07001487 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1488 slot_id, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001489 }
Mathias Nyman69694082017-01-23 14:20:27 +02001490
1491 if (ret) {
Sarah Sharpd13565c2011-07-22 14:34:34 -07001492free_priv:
Mathias Nyman69694082017-01-23 14:20:27 +02001493 xhci_urb_free_priv(urb_priv);
1494 urb->hcpriv = NULL;
1495 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001496 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001497 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001498}
1499
Sarah Sharpae636742009-04-29 19:02:31 -07001500/*
1501 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1502 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1503 * should pick up where it left off in the TD, unless a Set Transfer Ring
1504 * Dequeue Pointer is issued.
1505 *
1506 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1507 * the ring. Since the ring is a contiguous structure, they can't be physically
1508 * removed. Instead, there are two options:
1509 *
1510 * 1) If the HC is in the middle of processing the URB to be canceled, we
1511 * simply move the ring's dequeue pointer past those TRBs using the Set
1512 * Transfer Ring Dequeue Pointer command. This will be the common case,
1513 * when drivers timeout on the last submitted URB and attempt to cancel.
1514 *
1515 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1516 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1517 * HC will need to invalidate the any TRBs it has cached after the stop
1518 * endpoint command, as noted in the xHCI 0.95 errata.
1519 *
1520 * 3) The TD may have completed by the time the Stop Endpoint Command
1521 * completes, so software needs to handle that case too.
1522 *
1523 * This function should protect against the TD enqueueing code ringing the
1524 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1525 * It also needs to account for multiple cancellations on happening at the same
1526 * time for the same endpoint.
1527 *
1528 * Note that this function can be called in any context, or so says
1529 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001530 */
Lu Baolu39693842017-04-07 17:57:04 +03001531static int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001532{
Sarah Sharpae636742009-04-29 19:02:31 -07001533 unsigned long flags;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001534 int ret, i;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001535 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -07001536 struct xhci_hcd *xhci;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001537 struct urb_priv *urb_priv;
Sarah Sharpae636742009-04-29 19:02:31 -07001538 struct xhci_td *td;
1539 unsigned int ep_index;
1540 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001541 struct xhci_virt_ep *ep;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001542 struct xhci_command *command;
Mathias Nymand3519b92017-03-28 15:55:30 +03001543 struct xhci_virt_device *vdev;
Sarah Sharpae636742009-04-29 19:02:31 -07001544
1545 xhci = hcd_to_xhci(hcd);
1546 spin_lock_irqsave(&xhci->lock, flags);
Felipe Balbi5abdc2e2017-01-23 14:20:20 +02001547
1548 trace_xhci_urb_dequeue(urb);
1549
Sarah Sharpae636742009-04-29 19:02:31 -07001550 /* Make sure the URB hasn't completed or been unlinked already */
1551 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
Mathias Nymand3519b92017-03-28 15:55:30 +03001552 if (ret)
Sarah Sharpae636742009-04-29 19:02:31 -07001553 goto done;
Mathias Nymand3519b92017-03-28 15:55:30 +03001554
1555 /* give back URB now if we can't queue it for cancel */
1556 vdev = xhci->devs[urb->dev->slot_id];
1557 urb_priv = urb->hcpriv;
1558 if (!vdev || !urb_priv)
1559 goto err_giveback;
1560
1561 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1562 ep = &vdev->eps[ep_index];
1563 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1564 if (!ep || !ep_ring)
1565 goto err_giveback;
1566
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001567 /* If xHC is dead take it down and return ALL URBs in xhci_hc_died() */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001568 temp = readl(&xhci->op_regs->status);
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001569 if (temp == ~(u32)0 || xhci->xhc_state & XHCI_STATE_DYING) {
1570 xhci_hc_died(xhci);
1571 goto done;
1572 }
1573
1574 if (xhci->xhc_state & XHCI_STATE_HALTED) {
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001575 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
Mathias Nymand9f11ba2017-04-07 17:57:01 +03001576 "HC halted, freeing TD manually.");
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02001577 for (i = urb_priv->num_tds_done;
Mathias Nymand3519b92017-03-28 15:55:30 +03001578 i < urb_priv->num_tds;
Mathias Nyman5c821712016-01-26 17:50:12 +02001579 i++) {
Mathias Nyman7e64b032017-01-23 14:20:26 +02001580 td = &urb_priv->td[i];
Sarah Sharp585df1d2011-08-02 15:43:40 -07001581 if (!list_empty(&td->td_list))
1582 list_del_init(&td->td_list);
1583 if (!list_empty(&td->cancelled_td_list))
1584 list_del_init(&td->cancelled_td_list);
1585 }
Mathias Nymand3519b92017-03-28 15:55:30 +03001586 goto err_giveback;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001587 }
Sarah Sharpae636742009-04-29 19:02:31 -07001588
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02001589 i = urb_priv->num_tds_done;
1590 if (i < urb_priv->num_tds)
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001591 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1592 "Cancel URB %p, dev %s, ep 0x%x, "
1593 "starting at offset 0x%llx",
Sarah Sharp79688ac2011-12-19 16:56:04 -08001594 urb, urb->dev->devpath,
1595 urb->ep->desc.bEndpointAddress,
1596 (unsigned long long) xhci_trb_virt_to_dma(
Mathias Nyman7e64b032017-01-23 14:20:26 +02001597 urb_priv->td[i].start_seg,
1598 urb_priv->td[i].first_trb));
Andiry Xu8e51adc2010-07-22 15:23:31 -07001599
Mathias Nyman9ef7fbb2017-01-23 14:20:25 +02001600 for (; i < urb_priv->num_tds; i++) {
Mathias Nyman7e64b032017-01-23 14:20:26 +02001601 td = &urb_priv->td[i];
Andiry Xu8e51adc2010-07-22 15:23:31 -07001602 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1603 }
1604
Sarah Sharpae636742009-04-29 19:02:31 -07001605 /* Queue a stop endpoint command, but only if this is
1606 * the first cancellation to be handled.
1607 */
Mathias Nyman9983a5f2017-01-23 14:19:52 +02001608 if (!(ep->ep_state & EP_STOP_CMD_PENDING)) {
Mathias Nyman103afda2017-12-08 17:59:08 +02001609 command = xhci_alloc_command(xhci, false, GFP_ATOMIC);
Hans de Goedea0ee6192014-07-25 22:01:21 +02001610 if (!command) {
1611 ret = -ENOMEM;
1612 goto done;
1613 }
Mathias Nyman9983a5f2017-01-23 14:19:52 +02001614 ep->ep_state |= EP_STOP_CMD_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001615 ep->stop_cmd_timer.expires = jiffies +
1616 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1617 add_timer(&ep->stop_cmd_timer);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001618 xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id,
1619 ep_index, 0);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001620 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -07001621 }
1622done:
1623 spin_unlock_irqrestore(&xhci->lock, flags);
1624 return ret;
Mathias Nymand3519b92017-03-28 15:55:30 +03001625
1626err_giveback:
1627 if (urb_priv)
1628 xhci_urb_free_priv(urb_priv);
1629 usb_hcd_unlink_urb_from_ep(hcd, urb);
1630 spin_unlock_irqrestore(&xhci->lock, flags);
1631 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
1632 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001633}
1634
Sarah Sharpf94e01862009-04-27 19:58:38 -07001635/* Drop an endpoint from a new bandwidth configuration for this device.
1636 * Only one call to this function is allowed per endpoint before
1637 * check_bandwidth() or reset_bandwidth() must be called.
1638 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1639 * add the endpoint to the schedule with possibly new parameters denoted by a
1640 * different endpoint descriptor in usb_host_endpoint.
1641 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1642 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001643 *
1644 * The USB core will not allow URBs to be queued to an endpoint that is being
1645 * disabled, so there's no need for mutual exclusion to protect
1646 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001647 */
Lu Baolu39693842017-04-07 17:57:04 +03001648static int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
Sarah Sharpf94e01862009-04-27 19:58:38 -07001649 struct usb_host_endpoint *ep)
1650{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001651 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001652 struct xhci_container_ctx *in_ctx, *out_ctx;
1653 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001654 unsigned int ep_index;
1655 struct xhci_ep_ctx *ep_ctx;
1656 u32 drop_flag;
Julius Wernerd6759132014-06-24 17:14:42 +03001657 u32 new_add_flags, new_drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001658 int ret;
1659
Andiry Xu64927732010-10-14 07:22:45 -07001660 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001661 if (ret <= 0)
1662 return ret;
1663 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001664 if (xhci->xhc_state & XHCI_STATE_DYING)
1665 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001666
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001667 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001668 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1669 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1670 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1671 __func__, drop_flag);
1672 return 0;
1673 }
1674
Sarah Sharpf94e01862009-04-27 19:58:38 -07001675 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001676 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001677 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001678 if (!ctrl_ctx) {
1679 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1680 __func__);
1681 return 0;
1682 }
1683
Sarah Sharpf94e01862009-04-27 19:58:38 -07001684 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001685 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001686 /* If the HC already knows the endpoint is disabled,
1687 * or the HCD has noted it is disabled, ignore this request
1688 */
Mathias Nyman5071e6b2016-11-11 15:13:28 +02001689 if ((GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) ||
Matt Evans28ccd292011-03-29 13:40:46 +11001690 le32_to_cpu(ctrl_ctx->drop_flags) &
1691 xhci_get_endpoint_flag(&ep->desc)) {
Hans de Goedea6134132015-01-16 17:54:02 +02001692 /* Do not warn when called after a usb_device_reset */
1693 if (xhci->devs[udev->slot_id]->eps[ep_index].ring != NULL)
1694 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1695 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001696 return 0;
1697 }
1698
Matt Evans28ccd292011-03-29 13:40:46 +11001699 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1700 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001701
Matt Evans28ccd292011-03-29 13:40:46 +11001702 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1703 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001704
Lu Baolu02b6fdc2017-10-05 11:21:39 +03001705 xhci_debugfs_remove_endpoint(xhci, xhci->devs[udev->slot_id], ep_index);
1706
Sarah Sharpf94e01862009-04-27 19:58:38 -07001707 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1708
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001709 if (xhci->quirks & XHCI_MTK_HOST)
1710 xhci_mtk_drop_ep_quirk(hcd, udev, ep);
1711
Julius Wernerd6759132014-06-24 17:14:42 +03001712 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 -07001713 (unsigned int) ep->desc.bEndpointAddress,
1714 udev->slot_id,
1715 (unsigned int) new_drop_flags,
Julius Wernerd6759132014-06-24 17:14:42 +03001716 (unsigned int) new_add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001717 return 0;
1718}
1719
1720/* Add an endpoint to a new possible bandwidth configuration for this device.
1721 * Only one call to this function is allowed per endpoint before
1722 * check_bandwidth() or reset_bandwidth() must be called.
1723 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1724 * add the endpoint to the schedule with possibly new parameters denoted by a
1725 * different endpoint descriptor in usb_host_endpoint.
1726 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1727 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001728 *
1729 * The USB core will not allow URBs to be queued to an endpoint until the
1730 * configuration or alt setting is installed in the device, so there's no need
1731 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001732 */
Lu Baolu39693842017-04-07 17:57:04 +03001733static int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
Sarah Sharpf94e01862009-04-27 19:58:38 -07001734 struct usb_host_endpoint *ep)
1735{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001736 struct xhci_hcd *xhci;
Lin Wang92c96912015-01-09 16:06:27 +02001737 struct xhci_container_ctx *in_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001738 unsigned int ep_index;
John Yound115b042009-07-27 12:05:15 -07001739 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001740 u32 added_ctxs;
Julius Wernerd6759132014-06-24 17:14:42 +03001741 u32 new_add_flags, new_drop_flags;
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001742 struct xhci_virt_device *virt_dev;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001743 int ret = 0;
1744
Andiry Xu64927732010-10-14 07:22:45 -07001745 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001746 if (ret <= 0) {
1747 /* So we won't queue a reset ep command for a root hub */
1748 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001749 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001750 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001751 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001752 if (xhci->xhc_state & XHCI_STATE_DYING)
1753 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001754
1755 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001756 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1757 /* FIXME when we have to issue an evaluate endpoint command to
1758 * deal with ep0 max packet size changing once we get the
1759 * descriptors
1760 */
1761 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1762 __func__, added_ctxs);
1763 return 0;
1764 }
1765
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001766 virt_dev = xhci->devs[udev->slot_id];
1767 in_ctx = virt_dev->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001768 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001769 if (!ctrl_ctx) {
1770 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1771 __func__);
1772 return 0;
1773 }
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001774
Sarah Sharp92f8e762013-04-23 17:11:14 -07001775 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001776 /* If this endpoint is already in use, and the upper layers are trying
1777 * to add it again without dropping it, reject the addition.
1778 */
1779 if (virt_dev->eps[ep_index].ring &&
Lin Wang92c96912015-01-09 16:06:27 +02001780 !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) {
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001781 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1782 "without dropping it.\n",
1783 (unsigned int) ep->desc.bEndpointAddress);
1784 return -EINVAL;
1785 }
1786
Sarah Sharpf94e01862009-04-27 19:58:38 -07001787 /* If the HCD has already noted the endpoint is enabled,
1788 * ignore this request.
1789 */
Lin Wang92c96912015-01-09 16:06:27 +02001790 if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001791 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1792 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001793 return 0;
1794 }
1795
Sarah Sharpf88ba782009-05-14 11:44:22 -07001796 /*
1797 * Configuration and alternate setting changes must be done in
1798 * process context, not interrupt context (or so documenation
1799 * for usb_set_interface() and usb_set_configuration() claim).
1800 */
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001801 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001802 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1803 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001804 return -ENOMEM;
1805 }
1806
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001807 if (xhci->quirks & XHCI_MTK_HOST) {
1808 ret = xhci_mtk_add_ep_quirk(hcd, udev, ep);
1809 if (ret < 0) {
Lu Baolu98217862017-09-18 17:39:12 +03001810 xhci_ring_free(xhci, virt_dev->eps[ep_index].new_ring);
1811 virt_dev->eps[ep_index].new_ring = NULL;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001812 return ret;
1813 }
1814 }
1815
Matt Evans28ccd292011-03-29 13:40:46 +11001816 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1817 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001818
1819 /* If xhci_endpoint_disable() was called for this endpoint, but the
1820 * xHC hasn't been notified yet through the check_bandwidth() call,
1821 * this re-adds a new state for the endpoint from the new endpoint
1822 * descriptors. We must drop and re-add this endpoint, so we leave the
1823 * drop flags alone.
1824 */
Matt Evans28ccd292011-03-29 13:40:46 +11001825 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001826
Sarah Sharpa1587d92009-07-27 12:03:15 -07001827 /* Store the usb_device pointer for later use */
1828 ep->hcpriv = udev;
1829
Lu Baolu02b6fdc2017-10-05 11:21:39 +03001830 xhci_debugfs_create_endpoint(xhci, virt_dev, ep_index);
1831
Julius Wernerd6759132014-06-24 17:14:42 +03001832 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 -07001833 (unsigned int) ep->desc.bEndpointAddress,
1834 udev->slot_id,
1835 (unsigned int) new_drop_flags,
Julius Wernerd6759132014-06-24 17:14:42 +03001836 (unsigned int) new_add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001837 return 0;
1838}
1839
John Yound115b042009-07-27 12:05:15 -07001840static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001841{
John Yound115b042009-07-27 12:05:15 -07001842 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001843 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001844 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001845 int i;
1846
Lin Wang4daf9df2015-01-09 16:06:31 +02001847 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001848 if (!ctrl_ctx) {
1849 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1850 __func__);
1851 return;
1852 }
1853
Sarah Sharpf94e01862009-04-27 19:58:38 -07001854 /* When a device's add flag and drop flag are zero, any subsequent
1855 * configure endpoint command will leave that endpoint's state
1856 * untouched. Make sure we don't leave any old state in the input
1857 * endpoint contexts.
1858 */
John Yound115b042009-07-27 12:05:15 -07001859 ctrl_ctx->drop_flags = 0;
1860 ctrl_ctx->add_flags = 0;
1861 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001862 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001863 /* Endpoint 0 is always valid */
Matt Evans28ccd292011-03-29 13:40:46 +11001864 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
Felipe Balbi98871e92017-01-23 14:20:04 +02001865 for (i = 1; i < 31; i++) {
John Yound115b042009-07-27 12:05:15 -07001866 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001867 ep_ctx->ep_info = 0;
1868 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001869 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001870 ep_ctx->tx_info = 0;
1871 }
1872}
1873
Sarah Sharpf2217e82009-08-07 14:04:43 -07001874static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001875 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001876{
1877 int ret;
1878
Sarah Sharp913a8a32009-09-04 10:53:13 -07001879 switch (*cmd_status) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001880 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03001881 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03001882 xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
1883 ret = -ETIME;
1884 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001885 case COMP_RESOURCE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001886 dev_warn(&udev->dev,
1887 "Not enough host controller resources for new device state.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001888 ret = -ENOMEM;
1889 /* FIXME: can we allocate more resources for the HC? */
1890 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001891 case COMP_BANDWIDTH_ERROR:
1892 case COMP_SECONDARY_BANDWIDTH_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001893 dev_warn(&udev->dev,
1894 "Not enough bandwidth for new device state.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001895 ret = -ENOSPC;
1896 /* FIXME: can we go back to the old state? */
1897 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001898 case COMP_TRB_ERROR:
Sarah Sharpf2217e82009-08-07 14:04:43 -07001899 /* the HCD set up something wrong */
1900 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1901 "add flag = 1, "
1902 "and endpoint is not disabled.\n");
1903 ret = -EINVAL;
1904 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001905 case COMP_INCOMPATIBLE_DEVICE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001906 dev_warn(&udev->dev,
1907 "ERROR: Incompatible device for endpoint configure command.\n");
Alex Hef6ba6fe2011-06-08 18:34:06 +08001908 ret = -ENODEV;
1909 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001910 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001911 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1912 "Successful Endpoint Configure command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001913 ret = 0;
1914 break;
1915 default:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001916 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
1917 *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001918 ret = -EINVAL;
1919 break;
1920 }
1921 return ret;
1922}
1923
1924static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001925 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001926{
1927 int ret;
1928
Sarah Sharp913a8a32009-09-04 10:53:13 -07001929 switch (*cmd_status) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001930 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03001931 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03001932 xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
1933 ret = -ETIME;
1934 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001935 case COMP_PARAMETER_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001936 dev_warn(&udev->dev,
1937 "WARN: xHCI driver setup invalid evaluate context command.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001938 ret = -EINVAL;
1939 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001940 case COMP_SLOT_NOT_ENABLED_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001941 dev_warn(&udev->dev,
1942 "WARN: slot not enabled for evaluate context command.\n");
Sarah Sharpb8031342012-10-16 13:26:22 -07001943 ret = -EINVAL;
1944 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001945 case COMP_CONTEXT_STATE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001946 dev_warn(&udev->dev,
1947 "WARN: invalid context state for evaluate context command.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001948 ret = -EINVAL;
1949 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001950 case COMP_INCOMPATIBLE_DEVICE_ERROR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001951 dev_warn(&udev->dev,
1952 "ERROR: Incompatible device for evaluate context command.\n");
Alex Hef6ba6fe2011-06-08 18:34:06 +08001953 ret = -ENODEV;
1954 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02001955 case COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR:
Alex He1bb73a82011-05-05 18:14:12 +08001956 /* Max Exit Latency too large error */
1957 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1958 ret = -EINVAL;
1959 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001960 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001961 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1962 "Successful evaluate context command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001963 ret = 0;
1964 break;
1965 default:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001966 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
1967 *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001968 ret = -EINVAL;
1969 break;
1970 }
1971 return ret;
1972}
1973
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001974static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001975 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001976{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001977 u32 valid_add_flags;
1978 u32 valid_drop_flags;
1979
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001980 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1981 * (bit 1). The default control endpoint is added during the Address
1982 * Device command and is never removed until the slot is disabled.
1983 */
Xenia Ragiadakouef734002013-09-09 21:03:06 +03001984 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
1985 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001986
1987 /* Use hweight32 to count the number of ones in the add flags, or
1988 * number of endpoints added. Don't count endpoints that are changed
1989 * (both added and dropped).
1990 */
1991 return hweight32(valid_add_flags) -
1992 hweight32(valid_add_flags & valid_drop_flags);
1993}
1994
1995static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001996 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001997{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001998 u32 valid_add_flags;
1999 u32 valid_drop_flags;
2000
Xenia Ragiadakou78d1ff02013-09-09 21:03:07 +03002001 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
2002 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002003
2004 return hweight32(valid_drop_flags) -
2005 hweight32(valid_add_flags & valid_drop_flags);
2006}
2007
2008/*
2009 * We need to reserve the new number of endpoints before the configure endpoint
2010 * command completes. We can't subtract the dropped endpoints from the number
2011 * of active endpoints until the command completes because we can oversubscribe
2012 * the host in this case:
2013 *
2014 * - the first configure endpoint command drops more endpoints than it adds
2015 * - a second configure endpoint command that adds more endpoints is queued
2016 * - the first configure endpoint command fails, so the config is unchanged
2017 * - the second command may succeed, even though there isn't enough resources
2018 *
2019 * Must be called with xhci->lock held.
2020 */
2021static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002022 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002023{
2024 u32 added_eps;
2025
Sarah Sharp92f8e762013-04-23 17:11:14 -07002026 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002027 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002028 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2029 "Not enough ep ctxs: "
2030 "%u active, need to add %u, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002031 xhci->num_active_eps, added_eps,
2032 xhci->limit_active_eps);
2033 return -ENOMEM;
2034 }
2035 xhci->num_active_eps += added_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002036 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2037 "Adding %u ep ctxs, %u now active.", added_eps,
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002038 xhci->num_active_eps);
2039 return 0;
2040}
2041
2042/*
2043 * The configure endpoint was failed by the xHC for some other reason, so we
2044 * need to revert the resources that failed configuration would have used.
2045 *
2046 * Must be called with xhci->lock held.
2047 */
2048static void xhci_free_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002049 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002050{
2051 u32 num_failed_eps;
2052
Sarah Sharp92f8e762013-04-23 17:11:14 -07002053 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002054 xhci->num_active_eps -= num_failed_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002055 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2056 "Removing %u failed ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002057 num_failed_eps,
2058 xhci->num_active_eps);
2059}
2060
2061/*
2062 * Now that the command has completed, clean up the active endpoint count by
2063 * subtracting out the endpoints that were dropped (but not changed).
2064 *
2065 * Must be called with xhci->lock held.
2066 */
2067static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002068 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002069{
2070 u32 num_dropped_eps;
2071
Sarah Sharp92f8e762013-04-23 17:11:14 -07002072 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002073 xhci->num_active_eps -= num_dropped_eps;
2074 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002075 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2076 "Removing %u dropped ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002077 num_dropped_eps,
2078 xhci->num_active_eps);
2079}
2080
Felipe Balbied384bd2012-08-07 14:10:03 +03002081static unsigned int xhci_get_block_size(struct usb_device *udev)
Sarah Sharpc29eea62011-09-02 11:05:52 -07002082{
2083 switch (udev->speed) {
2084 case USB_SPEED_LOW:
2085 case USB_SPEED_FULL:
2086 return FS_BLOCK;
2087 case USB_SPEED_HIGH:
2088 return HS_BLOCK;
2089 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002090 case USB_SPEED_SUPER_PLUS:
Sarah Sharpc29eea62011-09-02 11:05:52 -07002091 return SS_BLOCK;
2092 case USB_SPEED_UNKNOWN:
2093 case USB_SPEED_WIRELESS:
2094 default:
2095 /* Should never happen */
2096 return 1;
2097 }
2098}
2099
Felipe Balbied384bd2012-08-07 14:10:03 +03002100static unsigned int
2101xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
Sarah Sharpc29eea62011-09-02 11:05:52 -07002102{
2103 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
2104 return LS_OVERHEAD;
2105 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2106 return FS_OVERHEAD;
2107 return HS_OVERHEAD;
2108}
2109
2110/* If we are changing a LS/FS device under a HS hub,
2111 * make sure (if we are activating a new TT) that the HS bus has enough
2112 * bandwidth for this new TT.
2113 */
2114static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2115 struct xhci_virt_device *virt_dev,
2116 int old_active_eps)
2117{
2118 struct xhci_interval_bw_table *bw_table;
2119 struct xhci_tt_bw_info *tt_info;
2120
2121 /* Find the bandwidth table for the root port this TT is attached to. */
2122 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2123 tt_info = virt_dev->tt_info;
2124 /* If this TT already had active endpoints, the bandwidth for this TT
2125 * has already been added. Removing all periodic endpoints (and thus
2126 * making the TT enactive) will only decrease the bandwidth used.
2127 */
2128 if (old_active_eps)
2129 return 0;
2130 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2131 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2132 return -ENOMEM;
2133 return 0;
2134 }
2135 /* Not sure why we would have no new active endpoints...
2136 *
2137 * Maybe because of an Evaluate Context change for a hub update or a
2138 * control endpoint 0 max packet size change?
2139 * FIXME: skip the bandwidth calculation in that case.
2140 */
2141 return 0;
2142}
2143
Sarah Sharp2b698992011-09-13 16:41:13 -07002144static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2145 struct xhci_virt_device *virt_dev)
2146{
2147 unsigned int bw_reserved;
2148
2149 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2150 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2151 return -ENOMEM;
2152
2153 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2154 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2155 return -ENOMEM;
2156
2157 return 0;
2158}
2159
Sarah Sharpc29eea62011-09-02 11:05:52 -07002160/*
2161 * This algorithm is a very conservative estimate of the worst-case scheduling
2162 * scenario for any one interval. The hardware dynamically schedules the
2163 * packets, so we can't tell which microframe could be the limiting factor in
2164 * the bandwidth scheduling. This only takes into account periodic endpoints.
2165 *
2166 * Obviously, we can't solve an NP complete problem to find the minimum worst
2167 * case scenario. Instead, we come up with an estimate that is no less than
2168 * the worst case bandwidth used for any one microframe, but may be an
2169 * over-estimate.
2170 *
2171 * We walk the requirements for each endpoint by interval, starting with the
2172 * smallest interval, and place packets in the schedule where there is only one
2173 * possible way to schedule packets for that interval. In order to simplify
2174 * this algorithm, we record the largest max packet size for each interval, and
2175 * assume all packets will be that size.
2176 *
2177 * For interval 0, we obviously must schedule all packets for each interval.
2178 * The bandwidth for interval 0 is just the amount of data to be transmitted
2179 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2180 * the number of packets).
2181 *
2182 * For interval 1, we have two possible microframes to schedule those packets
2183 * in. For this algorithm, if we can schedule the same number of packets for
2184 * each possible scheduling opportunity (each microframe), we will do so. The
2185 * remaining number of packets will be saved to be transmitted in the gaps in
2186 * the next interval's scheduling sequence.
2187 *
2188 * As we move those remaining packets to be scheduled with interval 2 packets,
2189 * we have to double the number of remaining packets to transmit. This is
2190 * because the intervals are actually powers of 2, and we would be transmitting
2191 * the previous interval's packets twice in this interval. We also have to be
2192 * sure that when we look at the largest max packet size for this interval, we
2193 * also look at the largest max packet size for the remaining packets and take
2194 * the greater of the two.
2195 *
2196 * The algorithm continues to evenly distribute packets in each scheduling
2197 * opportunity, and push the remaining packets out, until we get to the last
2198 * interval. Then those packets and their associated overhead are just added
2199 * to the bandwidth used.
Sarah Sharp2e279802011-09-02 11:05:50 -07002200 */
2201static int xhci_check_bw_table(struct xhci_hcd *xhci,
2202 struct xhci_virt_device *virt_dev,
2203 int old_active_eps)
2204{
Sarah Sharpc29eea62011-09-02 11:05:52 -07002205 unsigned int bw_reserved;
2206 unsigned int max_bandwidth;
2207 unsigned int bw_used;
2208 unsigned int block_size;
2209 struct xhci_interval_bw_table *bw_table;
2210 unsigned int packet_size = 0;
2211 unsigned int overhead = 0;
2212 unsigned int packets_transmitted = 0;
2213 unsigned int packets_remaining = 0;
2214 unsigned int i;
2215
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002216 if (virt_dev->udev->speed >= USB_SPEED_SUPER)
Sarah Sharp2b698992011-09-13 16:41:13 -07002217 return xhci_check_ss_bw(xhci, virt_dev);
2218
Sarah Sharpc29eea62011-09-02 11:05:52 -07002219 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2220 max_bandwidth = HS_BW_LIMIT;
2221 /* Convert percent of bus BW reserved to blocks reserved */
2222 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2223 } else {
2224 max_bandwidth = FS_BW_LIMIT;
2225 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2226 }
2227
2228 bw_table = virt_dev->bw_table;
2229 /* We need to translate the max packet size and max ESIT payloads into
2230 * the units the hardware uses.
2231 */
2232 block_size = xhci_get_block_size(virt_dev->udev);
2233
2234 /* If we are manipulating a LS/FS device under a HS hub, double check
2235 * that the HS bus has enough bandwidth if we are activing a new TT.
2236 */
2237 if (virt_dev->tt_info) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002238 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2239 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002240 virt_dev->real_port);
2241 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2242 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2243 "newly activated TT.\n");
2244 return -ENOMEM;
2245 }
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002246 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2247 "Recalculating BW for TT slot %u port %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002248 virt_dev->tt_info->slot_id,
2249 virt_dev->tt_info->ttport);
2250 } else {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002251 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2252 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002253 virt_dev->real_port);
2254 }
2255
2256 /* Add in how much bandwidth will be used for interval zero, or the
2257 * rounded max ESIT payload + number of packets * largest overhead.
2258 */
2259 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2260 bw_table->interval_bw[0].num_packets *
2261 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2262
2263 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2264 unsigned int bw_added;
2265 unsigned int largest_mps;
2266 unsigned int interval_overhead;
2267
2268 /*
2269 * How many packets could we transmit in this interval?
2270 * If packets didn't fit in the previous interval, we will need
2271 * to transmit that many packets twice within this interval.
2272 */
2273 packets_remaining = 2 * packets_remaining +
2274 bw_table->interval_bw[i].num_packets;
2275
2276 /* Find the largest max packet size of this or the previous
2277 * interval.
2278 */
2279 if (list_empty(&bw_table->interval_bw[i].endpoints))
2280 largest_mps = 0;
2281 else {
2282 struct xhci_virt_ep *virt_ep;
2283 struct list_head *ep_entry;
2284
2285 ep_entry = bw_table->interval_bw[i].endpoints.next;
2286 virt_ep = list_entry(ep_entry,
2287 struct xhci_virt_ep, bw_endpoint_list);
2288 /* Convert to blocks, rounding up */
2289 largest_mps = DIV_ROUND_UP(
2290 virt_ep->bw_info.max_packet_size,
2291 block_size);
2292 }
2293 if (largest_mps > packet_size)
2294 packet_size = largest_mps;
2295
2296 /* Use the larger overhead of this or the previous interval. */
2297 interval_overhead = xhci_get_largest_overhead(
2298 &bw_table->interval_bw[i]);
2299 if (interval_overhead > overhead)
2300 overhead = interval_overhead;
2301
2302 /* How many packets can we evenly distribute across
2303 * (1 << (i + 1)) possible scheduling opportunities?
2304 */
2305 packets_transmitted = packets_remaining >> (i + 1);
2306
2307 /* Add in the bandwidth used for those scheduled packets */
2308 bw_added = packets_transmitted * (overhead + packet_size);
2309
2310 /* How many packets do we have remaining to transmit? */
2311 packets_remaining = packets_remaining % (1 << (i + 1));
2312
2313 /* What largest max packet size should those packets have? */
2314 /* If we've transmitted all packets, don't carry over the
2315 * largest packet size.
2316 */
2317 if (packets_remaining == 0) {
2318 packet_size = 0;
2319 overhead = 0;
2320 } else if (packets_transmitted > 0) {
2321 /* Otherwise if we do have remaining packets, and we've
2322 * scheduled some packets in this interval, take the
2323 * largest max packet size from endpoints with this
2324 * interval.
2325 */
2326 packet_size = largest_mps;
2327 overhead = interval_overhead;
2328 }
2329 /* Otherwise carry over packet_size and overhead from the last
2330 * time we had a remainder.
2331 */
2332 bw_used += bw_added;
2333 if (bw_used > max_bandwidth) {
2334 xhci_warn(xhci, "Not enough bandwidth. "
2335 "Proposed: %u, Max: %u\n",
2336 bw_used, max_bandwidth);
2337 return -ENOMEM;
2338 }
2339 }
2340 /*
2341 * Ok, we know we have some packets left over after even-handedly
2342 * scheduling interval 15. We don't know which microframes they will
2343 * fit into, so we over-schedule and say they will be scheduled every
2344 * microframe.
2345 */
2346 if (packets_remaining > 0)
2347 bw_used += overhead + packet_size;
2348
2349 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2350 unsigned int port_index = virt_dev->real_port - 1;
2351
2352 /* OK, we're manipulating a HS device attached to a
2353 * root port bandwidth domain. Include the number of active TTs
2354 * in the bandwidth used.
2355 */
2356 bw_used += TT_HS_OVERHEAD *
2357 xhci->rh_bw[port_index].num_active_tts;
2358 }
2359
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002360 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2361 "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2362 "Available: %u " "percent",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002363 bw_used, max_bandwidth, bw_reserved,
2364 (max_bandwidth - bw_used - bw_reserved) * 100 /
2365 max_bandwidth);
2366
2367 bw_used += bw_reserved;
2368 if (bw_used > max_bandwidth) {
2369 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2370 bw_used, max_bandwidth);
2371 return -ENOMEM;
2372 }
2373
2374 bw_table->bw_used = bw_used;
Sarah Sharp2e279802011-09-02 11:05:50 -07002375 return 0;
2376}
2377
2378static bool xhci_is_async_ep(unsigned int ep_type)
2379{
2380 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2381 ep_type != ISOC_IN_EP &&
2382 ep_type != INT_IN_EP);
2383}
2384
Sarah Sharp2b698992011-09-13 16:41:13 -07002385static bool xhci_is_sync_in_ep(unsigned int ep_type)
2386{
Sarah Sharp392a07a2012-10-25 13:44:12 -07002387 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
Sarah Sharp2b698992011-09-13 16:41:13 -07002388}
2389
2390static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2391{
2392 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2393
2394 if (ep_bw->ep_interval == 0)
2395 return SS_OVERHEAD_BURST +
2396 (ep_bw->mult * ep_bw->num_packets *
2397 (SS_OVERHEAD + mps));
2398 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2399 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2400 1 << ep_bw->ep_interval);
2401
2402}
2403
Lu Baolu39693842017-04-07 17:57:04 +03002404static void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
Sarah Sharp2e279802011-09-02 11:05:50 -07002405 struct xhci_bw_info *ep_bw,
2406 struct xhci_interval_bw_table *bw_table,
2407 struct usb_device *udev,
2408 struct xhci_virt_ep *virt_ep,
2409 struct xhci_tt_bw_info *tt_info)
2410{
2411 struct xhci_interval_bw *interval_bw;
2412 int normalized_interval;
2413
Sarah Sharp2b698992011-09-13 16:41:13 -07002414 if (xhci_is_async_ep(ep_bw->type))
Sarah Sharp2e279802011-09-02 11:05:50 -07002415 return;
2416
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002417 if (udev->speed >= USB_SPEED_SUPER) {
Sarah Sharp2b698992011-09-13 16:41:13 -07002418 if (xhci_is_sync_in_ep(ep_bw->type))
2419 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2420 xhci_get_ss_bw_consumed(ep_bw);
2421 else
2422 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2423 xhci_get_ss_bw_consumed(ep_bw);
2424 return;
2425 }
2426
2427 /* SuperSpeed endpoints never get added to intervals in the table, so
2428 * this check is only valid for HS/FS/LS devices.
2429 */
2430 if (list_empty(&virt_ep->bw_endpoint_list))
2431 return;
Sarah Sharp2e279802011-09-02 11:05:50 -07002432 /* For LS/FS devices, we need to translate the interval expressed in
2433 * microframes to frames.
2434 */
2435 if (udev->speed == USB_SPEED_HIGH)
2436 normalized_interval = ep_bw->ep_interval;
2437 else
2438 normalized_interval = ep_bw->ep_interval - 3;
2439
2440 if (normalized_interval == 0)
2441 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2442 interval_bw = &bw_table->interval_bw[normalized_interval];
2443 interval_bw->num_packets -= ep_bw->num_packets;
2444 switch (udev->speed) {
2445 case USB_SPEED_LOW:
2446 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2447 break;
2448 case USB_SPEED_FULL:
2449 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2450 break;
2451 case USB_SPEED_HIGH:
2452 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2453 break;
2454 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002455 case USB_SPEED_SUPER_PLUS:
Sarah Sharp2e279802011-09-02 11:05:50 -07002456 case USB_SPEED_UNKNOWN:
2457 case USB_SPEED_WIRELESS:
2458 /* Should never happen because only LS/FS/HS endpoints will get
2459 * added to the endpoint list.
2460 */
2461 return;
2462 }
2463 if (tt_info)
2464 tt_info->active_eps -= 1;
2465 list_del_init(&virt_ep->bw_endpoint_list);
2466}
2467
2468static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2469 struct xhci_bw_info *ep_bw,
2470 struct xhci_interval_bw_table *bw_table,
2471 struct usb_device *udev,
2472 struct xhci_virt_ep *virt_ep,
2473 struct xhci_tt_bw_info *tt_info)
2474{
2475 struct xhci_interval_bw *interval_bw;
2476 struct xhci_virt_ep *smaller_ep;
2477 int normalized_interval;
2478
2479 if (xhci_is_async_ep(ep_bw->type))
2480 return;
2481
Sarah Sharp2b698992011-09-13 16:41:13 -07002482 if (udev->speed == USB_SPEED_SUPER) {
2483 if (xhci_is_sync_in_ep(ep_bw->type))
2484 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2485 xhci_get_ss_bw_consumed(ep_bw);
2486 else
2487 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2488 xhci_get_ss_bw_consumed(ep_bw);
2489 return;
2490 }
2491
Sarah Sharp2e279802011-09-02 11:05:50 -07002492 /* For LS/FS devices, we need to translate the interval expressed in
2493 * microframes to frames.
2494 */
2495 if (udev->speed == USB_SPEED_HIGH)
2496 normalized_interval = ep_bw->ep_interval;
2497 else
2498 normalized_interval = ep_bw->ep_interval - 3;
2499
2500 if (normalized_interval == 0)
2501 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2502 interval_bw = &bw_table->interval_bw[normalized_interval];
2503 interval_bw->num_packets += ep_bw->num_packets;
2504 switch (udev->speed) {
2505 case USB_SPEED_LOW:
2506 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2507 break;
2508 case USB_SPEED_FULL:
2509 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2510 break;
2511 case USB_SPEED_HIGH:
2512 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2513 break;
2514 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002515 case USB_SPEED_SUPER_PLUS:
Sarah Sharp2e279802011-09-02 11:05:50 -07002516 case USB_SPEED_UNKNOWN:
2517 case USB_SPEED_WIRELESS:
2518 /* Should never happen because only LS/FS/HS endpoints will get
2519 * added to the endpoint list.
2520 */
2521 return;
2522 }
2523
2524 if (tt_info)
2525 tt_info->active_eps += 1;
2526 /* Insert the endpoint into the list, largest max packet size first. */
2527 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2528 bw_endpoint_list) {
2529 if (ep_bw->max_packet_size >=
2530 smaller_ep->bw_info.max_packet_size) {
2531 /* Add the new ep before the smaller endpoint */
2532 list_add_tail(&virt_ep->bw_endpoint_list,
2533 &smaller_ep->bw_endpoint_list);
2534 return;
2535 }
2536 }
2537 /* Add the new endpoint at the end of the list. */
2538 list_add_tail(&virt_ep->bw_endpoint_list,
2539 &interval_bw->endpoints);
2540}
2541
2542void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2543 struct xhci_virt_device *virt_dev,
2544 int old_active_eps)
2545{
2546 struct xhci_root_port_bw_info *rh_bw_info;
2547 if (!virt_dev->tt_info)
2548 return;
2549
2550 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2551 if (old_active_eps == 0 &&
2552 virt_dev->tt_info->active_eps != 0) {
2553 rh_bw_info->num_active_tts += 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002554 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002555 } else if (old_active_eps != 0 &&
2556 virt_dev->tt_info->active_eps == 0) {
2557 rh_bw_info->num_active_tts -= 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002558 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002559 }
2560}
2561
2562static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2563 struct xhci_virt_device *virt_dev,
2564 struct xhci_container_ctx *in_ctx)
2565{
2566 struct xhci_bw_info ep_bw_info[31];
2567 int i;
2568 struct xhci_input_control_ctx *ctrl_ctx;
2569 int old_active_eps = 0;
2570
Sarah Sharp2e279802011-09-02 11:05:50 -07002571 if (virt_dev->tt_info)
2572 old_active_eps = virt_dev->tt_info->active_eps;
2573
Lin Wang4daf9df2015-01-09 16:06:31 +02002574 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002575 if (!ctrl_ctx) {
2576 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2577 __func__);
2578 return -ENOMEM;
2579 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002580
2581 for (i = 0; i < 31; i++) {
2582 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2583 continue;
2584
2585 /* Make a copy of the BW info in case we need to revert this */
2586 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2587 sizeof(ep_bw_info[i]));
2588 /* Drop the endpoint from the interval table if the endpoint is
2589 * being dropped or changed.
2590 */
2591 if (EP_IS_DROPPED(ctrl_ctx, i))
2592 xhci_drop_ep_from_interval_table(xhci,
2593 &virt_dev->eps[i].bw_info,
2594 virt_dev->bw_table,
2595 virt_dev->udev,
2596 &virt_dev->eps[i],
2597 virt_dev->tt_info);
2598 }
2599 /* Overwrite the information stored in the endpoints' bw_info */
2600 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2601 for (i = 0; i < 31; i++) {
2602 /* Add any changed or added endpoints to the interval table */
2603 if (EP_IS_ADDED(ctrl_ctx, i))
2604 xhci_add_ep_to_interval_table(xhci,
2605 &virt_dev->eps[i].bw_info,
2606 virt_dev->bw_table,
2607 virt_dev->udev,
2608 &virt_dev->eps[i],
2609 virt_dev->tt_info);
2610 }
2611
2612 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2613 /* Ok, this fits in the bandwidth we have.
2614 * Update the number of active TTs.
2615 */
2616 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2617 return 0;
2618 }
2619
2620 /* We don't have enough bandwidth for this, revert the stored info. */
2621 for (i = 0; i < 31; i++) {
2622 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2623 continue;
2624
2625 /* Drop the new copies of any added or changed endpoints from
2626 * the interval table.
2627 */
2628 if (EP_IS_ADDED(ctrl_ctx, i)) {
2629 xhci_drop_ep_from_interval_table(xhci,
2630 &virt_dev->eps[i].bw_info,
2631 virt_dev->bw_table,
2632 virt_dev->udev,
2633 &virt_dev->eps[i],
2634 virt_dev->tt_info);
2635 }
2636 /* Revert the endpoint back to its old information */
2637 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2638 sizeof(ep_bw_info[i]));
2639 /* Add any changed or dropped endpoints back into the table */
2640 if (EP_IS_DROPPED(ctrl_ctx, i))
2641 xhci_add_ep_to_interval_table(xhci,
2642 &virt_dev->eps[i].bw_info,
2643 virt_dev->bw_table,
2644 virt_dev->udev,
2645 &virt_dev->eps[i],
2646 virt_dev->tt_info);
2647 }
2648 return -ENOMEM;
2649}
2650
2651
Sarah Sharpf2217e82009-08-07 14:04:43 -07002652/* Issue a configure endpoint command or evaluate context command
2653 * and wait for it to finish.
2654 */
2655static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002656 struct usb_device *udev,
2657 struct xhci_command *command,
2658 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07002659{
2660 int ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002661 unsigned long flags;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002662 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002663 struct xhci_virt_device *virt_dev;
Mathias Nymane3a78ff2017-10-05 11:21:48 +03002664 struct xhci_slot_ctx *slot_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002665
2666 if (!command)
2667 return -EINVAL;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002668
2669 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymand9f11ba2017-04-07 17:57:01 +03002670
2671 if (xhci->xhc_state & XHCI_STATE_DYING) {
2672 spin_unlock_irqrestore(&xhci->lock, flags);
2673 return -ESHUTDOWN;
2674 }
2675
Sarah Sharp913a8a32009-09-04 10:53:13 -07002676 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002677
Lin Wang4daf9df2015-01-09 16:06:31 +02002678 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002679 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07002680 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002681 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2682 __func__);
2683 return -ENOMEM;
2684 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002685
2686 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
Sarah Sharp92f8e762013-04-23 17:11:14 -07002687 xhci_reserve_host_resources(xhci, ctrl_ctx)) {
Sarah Sharp750645f2011-09-02 11:05:43 -07002688 spin_unlock_irqrestore(&xhci->lock, flags);
2689 xhci_warn(xhci, "Not enough host resources, "
2690 "active endpoint contexts = %u\n",
2691 xhci->num_active_eps);
2692 return -ENOMEM;
2693 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002694 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002695 xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) {
Sarah Sharp2e279802011-09-02 11:05:50 -07002696 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002697 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2e279802011-09-02 11:05:50 -07002698 spin_unlock_irqrestore(&xhci->lock, flags);
2699 xhci_warn(xhci, "Not enough bandwidth\n");
2700 return -ENOMEM;
2701 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002702
Mathias Nymane3a78ff2017-10-05 11:21:48 +03002703 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
2704 trace_xhci_configure_endpoint(slot_ctx);
2705
Sarah Sharpf2217e82009-08-07 14:04:43 -07002706 if (!ctx_change)
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002707 ret = xhci_queue_configure_endpoint(xhci, command,
2708 command->in_ctx->dma,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002709 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002710 else
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002711 ret = xhci_queue_evaluate_context(xhci, command,
2712 command->in_ctx->dma,
Sarah Sharp4b266542012-05-07 15:34:26 -07002713 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002714 if (ret < 0) {
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002715 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002716 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002717 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03002718 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
2719 "FIXME allocate a new ring segment");
Sarah Sharpf2217e82009-08-07 14:04:43 -07002720 return -ENOMEM;
2721 }
2722 xhci_ring_cmd_db(xhci);
2723 spin_unlock_irqrestore(&xhci->lock, flags);
2724
2725 /* Wait for the configure endpoint command to complete */
Mathias Nymanc311e392014-05-08 19:26:03 +03002726 wait_for_completion(command->completion);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002727
2728 if (!ctx_change)
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002729 ret = xhci_configure_endpoint_result(xhci, udev,
2730 &command->status);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002731 else
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002732 ret = xhci_evaluate_context_result(xhci, udev,
2733 &command->status);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002734
2735 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2736 spin_lock_irqsave(&xhci->lock, flags);
2737 /* If the command failed, remove the reserved resources.
2738 * Otherwise, clean up the estimate to include dropped eps.
2739 */
2740 if (ret)
Sarah Sharp92f8e762013-04-23 17:11:14 -07002741 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002742 else
Sarah Sharp92f8e762013-04-23 17:11:14 -07002743 xhci_finish_resource_reservation(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002744 spin_unlock_irqrestore(&xhci->lock, flags);
2745 }
2746 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002747}
2748
Hans de Goededf613832013-10-04 00:29:45 +02002749static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci,
2750 struct xhci_virt_device *vdev, int i)
2751{
2752 struct xhci_virt_ep *ep = &vdev->eps[i];
2753
2754 if (ep->ep_state & EP_HAS_STREAMS) {
2755 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n",
2756 xhci_get_endpoint_address(i));
2757 xhci_free_stream_info(xhci, ep->stream_info);
2758 ep->stream_info = NULL;
2759 ep->ep_state &= ~EP_HAS_STREAMS;
2760 }
2761}
2762
Sarah Sharpf88ba782009-05-14 11:44:22 -07002763/* Called after one or more calls to xhci_add_endpoint() or
2764 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2765 * to call xhci_reset_bandwidth().
2766 *
2767 * Since we are in the middle of changing either configuration or
2768 * installing a new alt setting, the USB core won't allow URBs to be
2769 * enqueued for any endpoint on the old config or interface. Nothing
2770 * else should be touching the xhci->devs[slot_id] structure, so we
2771 * don't need to take the xhci->lock for manipulating that.
2772 */
Lu Baolu39693842017-04-07 17:57:04 +03002773static int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07002774{
2775 int i;
2776 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002777 struct xhci_hcd *xhci;
2778 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07002779 struct xhci_input_control_ctx *ctrl_ctx;
2780 struct xhci_slot_ctx *slot_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002781 struct xhci_command *command;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002782
Andiry Xu64927732010-10-14 07:22:45 -07002783 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002784 if (ret <= 0)
2785 return ret;
2786 xhci = hcd_to_xhci(hcd);
Mathias Nyman98d74f92016-04-08 16:25:10 +03002787 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
2788 (xhci->xhc_state & XHCI_STATE_REMOVING))
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07002789 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002790
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002791 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002792 virt_dev = xhci->devs[udev->slot_id];
2793
Mathias Nyman103afda2017-12-08 17:59:08 +02002794 command = xhci_alloc_command(xhci, true, GFP_KERNEL);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002795 if (!command)
2796 return -ENOMEM;
2797
2798 command->in_ctx = virt_dev->in_ctx;
2799
Sarah Sharpf94e01862009-04-27 19:58:38 -07002800 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
Lin Wang4daf9df2015-01-09 16:06:31 +02002801 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002802 if (!ctrl_ctx) {
2803 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2804 __func__);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002805 ret = -ENOMEM;
2806 goto command_cleanup;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002807 }
Matt Evans28ccd292011-03-29 13:40:46 +11002808 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2809 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2810 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
Sarah Sharp2dc37532011-09-02 11:05:40 -07002811
2812 /* Don't issue the command if there's no endpoints to update. */
2813 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002814 ctrl_ctx->drop_flags == 0) {
2815 ret = 0;
2816 goto command_cleanup;
2817 }
Julius Wernerd6759132014-06-24 17:14:42 +03002818 /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
John Yound115b042009-07-27 12:05:15 -07002819 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Julius Wernerd6759132014-06-24 17:14:42 +03002820 for (i = 31; i >= 1; i--) {
2821 __le32 le32 = cpu_to_le32(BIT(i));
2822
2823 if ((virt_dev->eps[i-1].ring && !(ctrl_ctx->drop_flags & le32))
2824 || (ctrl_ctx->add_flags & le32) || i == 1) {
2825 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
2826 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
2827 break;
2828 }
2829 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07002830
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002831 ret = xhci_configure_endpoint(xhci, udev, command,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002832 false, false);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002833 if (ret)
Sarah Sharpf94e01862009-04-27 19:58:38 -07002834 /* Callee should call reset_bandwidth() */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002835 goto command_cleanup;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002836
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002837 /* Free any rings that were dropped, but not changed. */
Felipe Balbi98871e92017-01-23 14:20:04 +02002838 for (i = 1; i < 31; i++) {
Matt Evans4819fef2011-06-01 13:01:07 +10002839 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
Hans de Goededf613832013-10-04 00:29:45 +02002840 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) {
Mathias Nymanc5628a22017-06-15 11:55:42 +03002841 xhci_free_endpoint_ring(xhci, virt_dev, i);
Hans de Goededf613832013-10-04 00:29:45 +02002842 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
2843 }
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002844 }
John Yound115b042009-07-27 12:05:15 -07002845 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002846 /*
2847 * Install any rings for completely new endpoints or changed endpoints,
Mathias Nymanc5628a22017-06-15 11:55:42 +03002848 * and free any old rings from changed endpoints.
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002849 */
Felipe Balbi98871e92017-01-23 14:20:04 +02002850 for (i = 1; i < 31; i++) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002851 if (!virt_dev->eps[i].new_ring)
2852 continue;
Mathias Nymanc5628a22017-06-15 11:55:42 +03002853 /* Only free the old ring if it exists.
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002854 * It may not if this is the first add of an endpoint.
2855 */
2856 if (virt_dev->eps[i].ring) {
Mathias Nymanc5628a22017-06-15 11:55:42 +03002857 xhci_free_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002858 }
Hans de Goededf613832013-10-04 00:29:45 +02002859 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002860 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2861 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002862 }
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002863command_cleanup:
2864 kfree(command->completion);
2865 kfree(command);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002866
Sarah Sharpf94e01862009-04-27 19:58:38 -07002867 return ret;
2868}
2869
Lu Baolu39693842017-04-07 17:57:04 +03002870static void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07002871{
Sarah Sharpf94e01862009-04-27 19:58:38 -07002872 struct xhci_hcd *xhci;
2873 struct xhci_virt_device *virt_dev;
2874 int i, ret;
2875
Andiry Xu64927732010-10-14 07:22:45 -07002876 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002877 if (ret <= 0)
2878 return;
2879 xhci = hcd_to_xhci(hcd);
2880
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002881 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002882 virt_dev = xhci->devs[udev->slot_id];
2883 /* Free any rings allocated for added endpoints */
Felipe Balbi98871e92017-01-23 14:20:04 +02002884 for (i = 0; i < 31; i++) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002885 if (virt_dev->eps[i].new_ring) {
Lu Baolu02b6fdc2017-10-05 11:21:39 +03002886 xhci_debugfs_remove_endpoint(xhci, virt_dev, i);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002887 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2888 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002889 }
2890 }
John Yound115b042009-07-27 12:05:15 -07002891 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002892}
2893
Sarah Sharp5270b952009-09-04 10:53:11 -07002894static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002895 struct xhci_container_ctx *in_ctx,
2896 struct xhci_container_ctx *out_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002897 struct xhci_input_control_ctx *ctrl_ctx,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002898 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07002899{
Matt Evans28ccd292011-03-29 13:40:46 +11002900 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2901 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002902 xhci_slot_copy(xhci, in_ctx, out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002903 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharp5270b952009-09-04 10:53:11 -07002904}
2905
Dmitry Torokhov8212a492011-02-08 13:55:59 -08002906static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002907 unsigned int slot_id, unsigned int ep_index,
2908 struct xhci_dequeue_state *deq_state)
2909{
Sarah Sharp92f8e762013-04-23 17:11:14 -07002910 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002911 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002912 struct xhci_ep_ctx *ep_ctx;
2913 u32 added_ctxs;
2914 dma_addr_t addr;
2915
Sarah Sharp92f8e762013-04-23 17:11:14 -07002916 in_ctx = xhci->devs[slot_id]->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02002917 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002918 if (!ctrl_ctx) {
2919 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2920 __func__);
2921 return;
2922 }
2923
Sarah Sharp913a8a32009-09-04 10:53:13 -07002924 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2925 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002926 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2927 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2928 deq_state->new_deq_ptr);
2929 if (addr == 0) {
2930 xhci_warn(xhci, "WARN Cannot submit config ep after "
2931 "reset ep command\n");
2932 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2933 deq_state->new_deq_seg,
2934 deq_state->new_deq_ptr);
2935 return;
2936 }
Matt Evans28ccd292011-03-29 13:40:46 +11002937 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002938
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002939 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002940 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002941 xhci->devs[slot_id]->out_ctx, ctrl_ctx,
2942 added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002943}
2944
Mathias Nymand36374f2017-06-15 11:55:47 +03002945void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci, unsigned int ep_index,
2946 unsigned int stream_id, struct xhci_td *td)
Sarah Sharp82d10092009-08-07 14:04:52 -07002947{
2948 struct xhci_dequeue_state deq_state;
Mathias Nymand97b4f82014-11-27 18:19:16 +02002949 struct usb_device *udev = td->urb->dev;
Sarah Sharp82d10092009-08-07 14:04:52 -07002950
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002951 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2952 "Cleaning up stalled endpoint ring");
Sarah Sharp82d10092009-08-07 14:04:52 -07002953 /* We need to move the HW's dequeue pointer past this TD,
2954 * or it will attempt to resend it on the next doorbell ring.
2955 */
2956 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Mathias Nymand36374f2017-06-15 11:55:47 +03002957 ep_index, stream_id, td, &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07002958
Mathias Nyman365038d2014-08-19 15:17:58 +03002959 if (!deq_state.new_deq_ptr || !deq_state.new_deq_seg)
2960 return;
2961
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002962 /* HW with the reset endpoint quirk will use the saved dequeue state to
2963 * issue a configure endpoint command later.
2964 */
2965 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002966 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2967 "Queueing new dequeue state");
Hans de Goede1e3452e2014-08-20 16:41:52 +03002968 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Mathias Nyman87907362017-06-02 16:36:23 +03002969 ep_index, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002970 } else {
2971 /* Better hope no one uses the input context between now and the
2972 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002973 * XXX: No idea how this hardware will react when stream rings
2974 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002975 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002976 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2977 "Setting up input context for "
2978 "configure endpoint command");
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002979 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2980 ep_index, &deq_state);
2981 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002982}
2983
Mathias Nymanf5249462018-03-16 16:33:04 +02002984/*
2985 * Called after usb core issues a clear halt control message.
2986 * The host side of the halt should already be cleared by a reset endpoint
2987 * command issued when the STALL event was received.
Mathias Nymand0167ad2015-03-10 19:49:00 +02002988 *
Mathias Nymanf5249462018-03-16 16:33:04 +02002989 * The reset endpoint command may only be issued to endpoints in the halted
2990 * state. For software that wishes to reset the data toggle or sequence number
2991 * of an endpoint that isn't in the halted state this function will issue a
2992 * configure endpoint command with the Drop and Add bits set for the target
2993 * endpoint. Refer to the additional note in xhci spcification section 4.6.8.
Sarah Sharpa1587d92009-07-27 12:03:15 -07002994 */
Mathias Nyman8e71a322014-11-18 11:27:12 +02002995
Lu Baolu39693842017-04-07 17:57:04 +03002996static void xhci_endpoint_reset(struct usb_hcd *hcd,
Mathias Nymanf5249462018-03-16 16:33:04 +02002997 struct usb_host_endpoint *host_ep)
Sarah Sharpa1587d92009-07-27 12:03:15 -07002998{
2999 struct xhci_hcd *xhci;
Mathias Nymanf5249462018-03-16 16:33:04 +02003000 struct usb_device *udev;
3001 struct xhci_virt_device *vdev;
3002 struct xhci_virt_ep *ep;
3003 struct xhci_input_control_ctx *ctrl_ctx;
3004 struct xhci_command *stop_cmd, *cfg_cmd;
3005 unsigned int ep_index;
3006 unsigned long flags;
3007 u32 ep_flag;
Sarah Sharpa1587d92009-07-27 12:03:15 -07003008
3009 xhci = hcd_to_xhci(hcd);
Mathias Nymanf5249462018-03-16 16:33:04 +02003010 if (!host_ep->hcpriv)
3011 return;
3012 udev = (struct usb_device *) host_ep->hcpriv;
3013 vdev = xhci->devs[udev->slot_id];
3014 ep_index = xhci_get_endpoint_index(&host_ep->desc);
3015 ep = &vdev->eps[ep_index];
3016
3017 /* Bail out if toggle is already being cleared by a endpoint reset */
3018 if (ep->ep_state & EP_HARD_CLEAR_TOGGLE) {
3019 ep->ep_state &= ~EP_HARD_CLEAR_TOGGLE;
3020 return;
3021 }
3022 /* Only interrupt and bulk ep's use data toggle, USB2 spec 5.5.4-> */
3023 if (usb_endpoint_xfer_control(&host_ep->desc) ||
3024 usb_endpoint_xfer_isoc(&host_ep->desc))
3025 return;
3026
3027 ep_flag = xhci_get_endpoint_flag(&host_ep->desc);
3028
3029 if (ep_flag == SLOT_FLAG || ep_flag == EP0_FLAG)
3030 return;
3031
3032 stop_cmd = xhci_alloc_command(xhci, true, GFP_NOWAIT);
3033 if (!stop_cmd)
3034 return;
3035
3036 cfg_cmd = xhci_alloc_command_with_ctx(xhci, true, GFP_NOWAIT);
3037 if (!cfg_cmd)
3038 goto cleanup;
3039
3040 spin_lock_irqsave(&xhci->lock, flags);
3041
3042 /* block queuing new trbs and ringing ep doorbell */
3043 ep->ep_state |= EP_SOFT_CLEAR_TOGGLE;
Sarah Sharpa1587d92009-07-27 12:03:15 -07003044
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003045 /*
Mathias Nymanf5249462018-03-16 16:33:04 +02003046 * Make sure endpoint ring is empty before resetting the toggle/seq.
3047 * Driver is required to synchronously cancel all transfer request.
3048 * Stop the endpoint to force xHC to update the output context
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07003049 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07003050
Mathias Nymanf5249462018-03-16 16:33:04 +02003051 if (!list_empty(&ep->ring->td_list)) {
3052 dev_err(&udev->dev, "EP not empty, refuse reset\n");
3053 spin_unlock_irqrestore(&xhci->lock, flags);
3054 goto cleanup;
3055 }
3056 xhci_queue_stop_endpoint(xhci, stop_cmd, udev->slot_id, ep_index, 0);
3057 xhci_ring_cmd_db(xhci);
3058 spin_unlock_irqrestore(&xhci->lock, flags);
3059
3060 wait_for_completion(stop_cmd->completion);
3061
3062 spin_lock_irqsave(&xhci->lock, flags);
3063
3064 /* config ep command clears toggle if add and drop ep flags are set */
3065 ctrl_ctx = xhci_get_input_control_ctx(cfg_cmd->in_ctx);
3066 xhci_setup_input_ctx_for_config_ep(xhci, cfg_cmd->in_ctx, vdev->out_ctx,
3067 ctrl_ctx, ep_flag, ep_flag);
3068 xhci_endpoint_copy(xhci, cfg_cmd->in_ctx, vdev->out_ctx, ep_index);
3069
3070 xhci_queue_configure_endpoint(xhci, cfg_cmd, cfg_cmd->in_ctx->dma,
3071 udev->slot_id, false);
3072 xhci_ring_cmd_db(xhci);
3073 spin_unlock_irqrestore(&xhci->lock, flags);
3074
3075 wait_for_completion(cfg_cmd->completion);
3076
3077 ep->ep_state &= ~EP_SOFT_CLEAR_TOGGLE;
3078 xhci_free_command(xhci, cfg_cmd);
3079cleanup:
3080 xhci_free_command(xhci, stop_cmd);
Sarah Sharpa1587d92009-07-27 12:03:15 -07003081}
3082
Sarah Sharp8df75f42010-04-02 15:34:16 -07003083static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
3084 struct usb_device *udev, struct usb_host_endpoint *ep,
3085 unsigned int slot_id)
3086{
3087 int ret;
3088 unsigned int ep_index;
3089 unsigned int ep_state;
3090
3091 if (!ep)
3092 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07003093 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003094 if (ret <= 0)
3095 return -EINVAL;
Hans de Goedea3901532013-10-04 17:05:55 +02003096 if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07003097 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
3098 " descriptor for ep 0x%x does not support streams\n",
3099 ep->desc.bEndpointAddress);
3100 return -EINVAL;
3101 }
3102
3103 ep_index = xhci_get_endpoint_index(&ep->desc);
3104 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3105 if (ep_state & EP_HAS_STREAMS ||
3106 ep_state & EP_GETTING_STREAMS) {
3107 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
3108 "already has streams set up.\n",
3109 ep->desc.bEndpointAddress);
3110 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
3111 "dynamic stream context array reallocation.\n");
3112 return -EINVAL;
3113 }
3114 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
3115 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
3116 "endpoint 0x%x; URBs are pending.\n",
3117 ep->desc.bEndpointAddress);
3118 return -EINVAL;
3119 }
3120 return 0;
3121}
3122
3123static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
3124 unsigned int *num_streams, unsigned int *num_stream_ctxs)
3125{
3126 unsigned int max_streams;
3127
3128 /* The stream context array size must be a power of two */
3129 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
3130 /*
3131 * Find out how many primary stream array entries the host controller
3132 * supports. Later we may use secondary stream arrays (similar to 2nd
3133 * level page entries), but that's an optional feature for xHCI host
3134 * controllers. xHCs must support at least 4 stream IDs.
3135 */
3136 max_streams = HCC_MAX_PSA(xhci->hcc_params);
3137 if (*num_stream_ctxs > max_streams) {
3138 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
3139 max_streams);
3140 *num_stream_ctxs = max_streams;
3141 *num_streams = max_streams;
3142 }
3143}
3144
3145/* Returns an error code if one of the endpoint already has streams.
3146 * This does not change any data structures, it only checks and gathers
3147 * information.
3148 */
3149static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
3150 struct usb_device *udev,
3151 struct usb_host_endpoint **eps, unsigned int num_eps,
3152 unsigned int *num_streams, u32 *changed_ep_bitmask)
3153{
Sarah Sharp8df75f42010-04-02 15:34:16 -07003154 unsigned int max_streams;
3155 unsigned int endpoint_flag;
3156 int i;
3157 int ret;
3158
3159 for (i = 0; i < num_eps; i++) {
3160 ret = xhci_check_streams_endpoint(xhci, udev,
3161 eps[i], udev->slot_id);
3162 if (ret < 0)
3163 return ret;
3164
Felipe Balbi18b7ede2012-01-02 13:35:41 +02003165 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003166 if (max_streams < (*num_streams - 1)) {
3167 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
3168 eps[i]->desc.bEndpointAddress,
3169 max_streams);
3170 *num_streams = max_streams+1;
3171 }
3172
3173 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
3174 if (*changed_ep_bitmask & endpoint_flag)
3175 return -EINVAL;
3176 *changed_ep_bitmask |= endpoint_flag;
3177 }
3178 return 0;
3179}
3180
3181static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3182 struct usb_device *udev,
3183 struct usb_host_endpoint **eps, unsigned int num_eps)
3184{
3185 u32 changed_ep_bitmask = 0;
3186 unsigned int slot_id;
3187 unsigned int ep_index;
3188 unsigned int ep_state;
3189 int i;
3190
3191 slot_id = udev->slot_id;
3192 if (!xhci->devs[slot_id])
3193 return 0;
3194
3195 for (i = 0; i < num_eps; i++) {
3196 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3197 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3198 /* Are streams already being freed for the endpoint? */
3199 if (ep_state & EP_GETTING_NO_STREAMS) {
3200 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003201 "endpoint 0x%x, "
3202 "streams are being disabled already\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003203 eps[i]->desc.bEndpointAddress);
3204 return 0;
3205 }
3206 /* Are there actually any streams to free? */
3207 if (!(ep_state & EP_HAS_STREAMS) &&
3208 !(ep_state & EP_GETTING_STREAMS)) {
3209 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003210 "endpoint 0x%x, "
3211 "streams are already disabled!\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003212 eps[i]->desc.bEndpointAddress);
3213 xhci_warn(xhci, "WARN xhci_free_streams() called "
3214 "with non-streams endpoint\n");
3215 return 0;
3216 }
3217 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3218 }
3219 return changed_ep_bitmask;
3220}
3221
3222/*
Luis de Bethencourtc2a298d2015-06-30 16:48:54 +02003223 * The USB device drivers use this function (through the HCD interface in USB
Sarah Sharp8df75f42010-04-02 15:34:16 -07003224 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3225 * coordinate mass storage command queueing across multiple endpoints (basically
3226 * a stream ID == a task ID).
3227 *
3228 * Setting up streams involves allocating the same size stream context array
3229 * for each endpoint and issuing a configure endpoint command for all endpoints.
3230 *
3231 * Don't allow the call to succeed if one endpoint only supports one stream
3232 * (which means it doesn't support streams at all).
3233 *
3234 * Drivers may get less stream IDs than they asked for, if the host controller
3235 * hardware or endpoints claim they can't support the number of requested
3236 * stream IDs.
3237 */
Lu Baolu39693842017-04-07 17:57:04 +03003238static int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
Sarah Sharp8df75f42010-04-02 15:34:16 -07003239 struct usb_host_endpoint **eps, unsigned int num_eps,
3240 unsigned int num_streams, gfp_t mem_flags)
3241{
3242 int i, ret;
3243 struct xhci_hcd *xhci;
3244 struct xhci_virt_device *vdev;
3245 struct xhci_command *config_cmd;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003246 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003247 unsigned int ep_index;
3248 unsigned int num_stream_ctxs;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003249 unsigned int max_packet;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003250 unsigned long flags;
3251 u32 changed_ep_bitmask = 0;
3252
3253 if (!eps)
3254 return -EINVAL;
3255
3256 /* Add one to the number of streams requested to account for
3257 * stream 0 that is reserved for xHCI usage.
3258 */
3259 num_streams += 1;
3260 xhci = hcd_to_xhci(hcd);
3261 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3262 num_streams);
3263
Hans de Goedef7920882013-11-15 12:14:38 +01003264 /* MaxPSASize value 0 (2 streams) means streams are not supported */
Hans de Goede8f873c12014-07-25 22:01:18 +02003265 if ((xhci->quirks & XHCI_BROKEN_STREAMS) ||
3266 HCC_MAX_PSA(xhci->hcc_params) < 4) {
Hans de Goedef7920882013-11-15 12:14:38 +01003267 xhci_dbg(xhci, "xHCI controller does not support streams.\n");
3268 return -ENOSYS;
3269 }
3270
Mathias Nyman14d49b72017-12-08 17:59:07 +02003271 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags);
Lu Baolu74e0b562017-04-07 17:57:05 +03003272 if (!config_cmd)
Sarah Sharp8df75f42010-04-02 15:34:16 -07003273 return -ENOMEM;
Lu Baolu74e0b562017-04-07 17:57:05 +03003274
Lin Wang4daf9df2015-01-09 16:06:31 +02003275 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003276 if (!ctrl_ctx) {
3277 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3278 __func__);
3279 xhci_free_command(xhci, config_cmd);
3280 return -ENOMEM;
3281 }
Sarah Sharp8df75f42010-04-02 15:34:16 -07003282
3283 /* Check to make sure all endpoints are not already configured for
3284 * streams. While we're at it, find the maximum number of streams that
3285 * all the endpoints will support and check for duplicate endpoints.
3286 */
3287 spin_lock_irqsave(&xhci->lock, flags);
3288 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3289 num_eps, &num_streams, &changed_ep_bitmask);
3290 if (ret < 0) {
3291 xhci_free_command(xhci, config_cmd);
3292 spin_unlock_irqrestore(&xhci->lock, flags);
3293 return ret;
3294 }
3295 if (num_streams <= 1) {
3296 xhci_warn(xhci, "WARN: endpoints can't handle "
3297 "more than one stream.\n");
3298 xhci_free_command(xhci, config_cmd);
3299 spin_unlock_irqrestore(&xhci->lock, flags);
3300 return -EINVAL;
3301 }
3302 vdev = xhci->devs[udev->slot_id];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003303 /* Mark each endpoint as being in transition, so
Sarah Sharp8df75f42010-04-02 15:34:16 -07003304 * xhci_urb_enqueue() will reject all URBs.
3305 */
3306 for (i = 0; i < num_eps; i++) {
3307 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3308 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3309 }
3310 spin_unlock_irqrestore(&xhci->lock, flags);
3311
3312 /* Setup internal data structures and allocate HW data structures for
3313 * streams (but don't install the HW structures in the input context
3314 * until we're sure all memory allocation succeeded).
3315 */
3316 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3317 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3318 num_stream_ctxs, num_streams);
3319
3320 for (i = 0; i < num_eps; i++) {
3321 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
Felipe Balbi734d3dd2016-09-28 13:46:37 +03003322 max_packet = usb_endpoint_maxp(&eps[i]->desc);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003323 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3324 num_stream_ctxs,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003325 num_streams,
3326 max_packet, mem_flags);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003327 if (!vdev->eps[ep_index].stream_info)
3328 goto cleanup;
3329 /* Set maxPstreams in endpoint context and update deq ptr to
3330 * point to stream context array. FIXME
3331 */
3332 }
3333
3334 /* Set up the input context for a configure endpoint command. */
3335 for (i = 0; i < num_eps; i++) {
3336 struct xhci_ep_ctx *ep_ctx;
3337
3338 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3339 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3340
3341 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3342 vdev->out_ctx, ep_index);
3343 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3344 vdev->eps[ep_index].stream_info);
3345 }
3346 /* Tell the HW to drop its old copy of the endpoint context info
3347 * and add the updated copy from the input context.
3348 */
3349 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003350 vdev->out_ctx, ctrl_ctx,
3351 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003352
3353 /* Issue and wait for the configure endpoint command */
3354 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3355 false, false);
3356
3357 /* xHC rejected the configure endpoint command for some reason, so we
3358 * leave the old ring intact and free our internal streams data
3359 * structure.
3360 */
3361 if (ret < 0)
3362 goto cleanup;
3363
3364 spin_lock_irqsave(&xhci->lock, flags);
3365 for (i = 0; i < num_eps; i++) {
3366 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3367 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3368 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3369 udev->slot_id, ep_index);
3370 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3371 }
3372 xhci_free_command(xhci, config_cmd);
3373 spin_unlock_irqrestore(&xhci->lock, flags);
3374
3375 /* Subtract 1 for stream 0, which drivers can't use */
3376 return num_streams - 1;
3377
3378cleanup:
3379 /* If it didn't work, free the streams! */
3380 for (i = 0; i < num_eps; i++) {
3381 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3382 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003383 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003384 /* FIXME Unset maxPstreams in endpoint context and
3385 * update deq ptr to point to normal string ring.
3386 */
3387 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3388 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3389 xhci_endpoint_zero(xhci, vdev, eps[i]);
3390 }
3391 xhci_free_command(xhci, config_cmd);
3392 return -ENOMEM;
3393}
3394
3395/* Transition the endpoint from using streams to being a "normal" endpoint
3396 * without streams.
3397 *
3398 * Modify the endpoint context state, submit a configure endpoint command,
3399 * and free all endpoint rings for streams if that completes successfully.
3400 */
Lu Baolu39693842017-04-07 17:57:04 +03003401static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
Sarah Sharp8df75f42010-04-02 15:34:16 -07003402 struct usb_host_endpoint **eps, unsigned int num_eps,
3403 gfp_t mem_flags)
3404{
3405 int i, ret;
3406 struct xhci_hcd *xhci;
3407 struct xhci_virt_device *vdev;
3408 struct xhci_command *command;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003409 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003410 unsigned int ep_index;
3411 unsigned long flags;
3412 u32 changed_ep_bitmask;
3413
3414 xhci = hcd_to_xhci(hcd);
3415 vdev = xhci->devs[udev->slot_id];
3416
3417 /* Set up a configure endpoint command to remove the streams rings */
3418 spin_lock_irqsave(&xhci->lock, flags);
3419 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3420 udev, eps, num_eps);
3421 if (changed_ep_bitmask == 0) {
3422 spin_unlock_irqrestore(&xhci->lock, flags);
3423 return -EINVAL;
3424 }
3425
3426 /* Use the xhci_command structure from the first endpoint. We may have
3427 * allocated too many, but the driver may call xhci_free_streams() for
3428 * each endpoint it grouped into one call to xhci_alloc_streams().
3429 */
3430 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3431 command = vdev->eps[ep_index].stream_info->free_streams_command;
Lin Wang4daf9df2015-01-09 16:06:31 +02003432 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003433 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07003434 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003435 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3436 __func__);
3437 return -EINVAL;
3438 }
3439
Sarah Sharp8df75f42010-04-02 15:34:16 -07003440 for (i = 0; i < num_eps; i++) {
3441 struct xhci_ep_ctx *ep_ctx;
3442
3443 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3444 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3445 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3446 EP_GETTING_NO_STREAMS;
3447
3448 xhci_endpoint_copy(xhci, command->in_ctx,
3449 vdev->out_ctx, ep_index);
Lin Wang4daf9df2015-01-09 16:06:31 +02003450 xhci_setup_no_streams_ep_input_ctx(ep_ctx,
Sarah Sharp8df75f42010-04-02 15:34:16 -07003451 &vdev->eps[ep_index]);
3452 }
3453 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003454 vdev->out_ctx, ctrl_ctx,
3455 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003456 spin_unlock_irqrestore(&xhci->lock, flags);
3457
3458 /* Issue and wait for the configure endpoint command,
3459 * which must succeed.
3460 */
3461 ret = xhci_configure_endpoint(xhci, udev, command,
3462 false, true);
3463
3464 /* xHC rejected the configure endpoint command for some reason, so we
3465 * leave the streams rings intact.
3466 */
3467 if (ret < 0)
3468 return ret;
3469
3470 spin_lock_irqsave(&xhci->lock, flags);
3471 for (i = 0; i < num_eps; i++) {
3472 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3473 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003474 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003475 /* FIXME Unset maxPstreams in endpoint context and
3476 * update deq ptr to point to normal string ring.
3477 */
3478 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3479 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3480 }
3481 spin_unlock_irqrestore(&xhci->lock, flags);
3482
3483 return 0;
3484}
3485
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003486/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003487 * Deletes endpoint resources for endpoints that were active before a Reset
3488 * Device command, or a Disable Slot command. The Reset Device command leaves
3489 * the control endpoint intact, whereas the Disable Slot command deletes it.
3490 *
3491 * Must be called with xhci->lock held.
3492 */
3493void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3494 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3495{
3496 int i;
3497 unsigned int num_dropped_eps = 0;
3498 unsigned int drop_flags = 0;
3499
3500 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3501 if (virt_dev->eps[i].ring) {
3502 drop_flags |= 1 << i;
3503 num_dropped_eps++;
3504 }
3505 }
3506 xhci->num_active_eps -= num_dropped_eps;
3507 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003508 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3509 "Dropped %u ep ctxs, flags = 0x%x, "
3510 "%u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003511 num_dropped_eps, drop_flags,
3512 xhci->num_active_eps);
3513}
3514
3515/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003516 * This submits a Reset Device Command, which will set the device state to 0,
3517 * set the device address to 0, and disable all the endpoints except the default
3518 * control endpoint. The USB core should come back and call
3519 * xhci_address_device(), and then re-set up the configuration. If this is
3520 * called because of a usb_reset_and_verify_device(), then the old alternate
3521 * settings will be re-installed through the normal bandwidth allocation
3522 * functions.
3523 *
3524 * Wait for the Reset Device command to finish. Remove all structures
3525 * associated with the endpoints that were disabled. Clear the input device
Mathias Nymanc5628a22017-06-15 11:55:42 +03003526 * structure? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07003527 *
3528 * If the virt_dev to be reset does not exist or does not match the udev,
3529 * it means the device is lost, possibly due to the xHC restore error and
3530 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3531 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003532 */
Lu Baolu39693842017-04-07 17:57:04 +03003533static int xhci_discover_or_reset_device(struct usb_hcd *hcd,
3534 struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003535{
3536 int ret, i;
3537 unsigned long flags;
3538 struct xhci_hcd *xhci;
3539 unsigned int slot_id;
3540 struct xhci_virt_device *virt_dev;
3541 struct xhci_command *reset_device_cmd;
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003542 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp2e279802011-09-02 11:05:50 -07003543 int old_active_eps = 0;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003544
Andiry Xuf0615c42010-10-14 07:22:48 -07003545 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003546 if (ret <= 0)
3547 return ret;
3548 xhci = hcd_to_xhci(hcd);
3549 slot_id = udev->slot_id;
3550 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07003551 if (!virt_dev) {
3552 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3553 "not exist. Re-allocate the device\n", slot_id);
3554 ret = xhci_alloc_dev(hcd, udev);
3555 if (ret == 1)
3556 return 0;
3557 else
3558 return -EINVAL;
3559 }
3560
Brian Campbell326124a2015-07-21 17:20:28 +03003561 if (virt_dev->tt_info)
3562 old_active_eps = virt_dev->tt_info->active_eps;
3563
Andiry Xuf0615c42010-10-14 07:22:48 -07003564 if (virt_dev->udev != udev) {
3565 /* If the virt_dev and the udev does not match, this virt_dev
3566 * may belong to another udev.
3567 * Re-allocate the device.
3568 */
3569 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3570 "not match the udev. Re-allocate the device\n",
3571 slot_id);
3572 ret = xhci_alloc_dev(hcd, udev);
3573 if (ret == 1)
3574 return 0;
3575 else
3576 return -EINVAL;
3577 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003578
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003579 /* If device is not setup, there is no point in resetting it */
3580 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3581 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3582 SLOT_STATE_DISABLED)
3583 return 0;
3584
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003585 trace_xhci_discover_or_reset_device(slot_ctx);
3586
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003587 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3588 /* Allocate the command structure that holds the struct completion.
3589 * Assume we're in process context, since the normal device reset
3590 * process has to wait for the device anyway. Storage devices are
3591 * reset as part of error handling, so use GFP_NOIO instead of
3592 * GFP_KERNEL.
3593 */
Mathias Nyman103afda2017-12-08 17:59:08 +02003594 reset_device_cmd = xhci_alloc_command(xhci, true, GFP_NOIO);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003595 if (!reset_device_cmd) {
3596 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3597 return -ENOMEM;
3598 }
3599
3600 /* Attempt to submit the Reset Device command to the command ring */
3601 spin_lock_irqsave(&xhci->lock, flags);
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08003602
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003603 ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003604 if (ret) {
3605 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003606 spin_unlock_irqrestore(&xhci->lock, flags);
3607 goto command_cleanup;
3608 }
3609 xhci_ring_cmd_db(xhci);
3610 spin_unlock_irqrestore(&xhci->lock, flags);
3611
3612 /* Wait for the Reset Device command to finish */
Mathias Nymanc311e392014-05-08 19:26:03 +03003613 wait_for_completion(reset_device_cmd->completion);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003614
3615 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3616 * unless we tried to reset a slot ID that wasn't enabled,
3617 * or the device wasn't in the addressed or configured state.
3618 */
3619 ret = reset_device_cmd->status;
3620 switch (ret) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003621 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03003622 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03003623 xhci_warn(xhci, "Timeout waiting for reset device command\n");
3624 ret = -ETIME;
3625 goto command_cleanup;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003626 case COMP_SLOT_NOT_ENABLED_ERROR: /* 0.95 completion for bad slot ID */
3627 case COMP_CONTEXT_STATE_ERROR: /* 0.96 completion code for same thing */
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003628 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n",
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003629 slot_id,
3630 xhci_get_slot_state(xhci, virt_dev->out_ctx));
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003631 xhci_dbg(xhci, "Not freeing device rings.\n");
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003632 /* Don't treat this as an error. May change my mind later. */
3633 ret = 0;
3634 goto command_cleanup;
3635 case COMP_SUCCESS:
3636 xhci_dbg(xhci, "Successful reset device command.\n");
3637 break;
3638 default:
3639 if (xhci_is_vendor_info_code(xhci, ret))
3640 break;
3641 xhci_warn(xhci, "Unknown completion code %u for "
3642 "reset device command.\n", ret);
3643 ret = -EINVAL;
3644 goto command_cleanup;
3645 }
3646
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003647 /* Free up host controller endpoint resources */
3648 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3649 spin_lock_irqsave(&xhci->lock, flags);
3650 /* Don't delete the default control endpoint resources */
3651 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3652 spin_unlock_irqrestore(&xhci->lock, flags);
3653 }
3654
Mathias Nymanc5628a22017-06-15 11:55:42 +03003655 /* Everything but endpoint 0 is disabled, so free the rings. */
Felipe Balbi98871e92017-01-23 14:20:04 +02003656 for (i = 1; i < 31; i++) {
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003657 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3658
3659 if (ep->ep_state & EP_HAS_STREAMS) {
Hans de Goededf613832013-10-04 00:29:45 +02003660 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n",
3661 xhci_get_endpoint_address(i));
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003662 xhci_free_stream_info(xhci, ep->stream_info);
3663 ep->stream_info = NULL;
3664 ep->ep_state &= ~EP_HAS_STREAMS;
3665 }
3666
3667 if (ep->ring) {
Lu Baolu02b6fdc2017-10-05 11:21:39 +03003668 xhci_debugfs_remove_endpoint(xhci, virt_dev, i);
Mathias Nymanc5628a22017-06-15 11:55:42 +03003669 xhci_free_endpoint_ring(xhci, virt_dev, i);
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003670 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003671 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3672 xhci_drop_ep_from_interval_table(xhci,
3673 &virt_dev->eps[i].bw_info,
3674 virt_dev->bw_table,
3675 udev,
3676 &virt_dev->eps[i],
3677 virt_dev->tt_info);
Sarah Sharp9af5d712011-09-02 11:05:48 -07003678 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003679 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003680 /* If necessary, update the number of active TTs on this root port */
3681 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003682 ret = 0;
3683
3684command_cleanup:
3685 xhci_free_command(xhci, reset_device_cmd);
3686 return ret;
3687}
3688
3689/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003690 * At this point, the struct usb_device is about to go away, the device has
3691 * disconnected, and all traffic has been stopped and the endpoints have been
3692 * disabled. Free any HC data structures associated with that device.
3693 */
Lu Baolu39693842017-04-07 17:57:04 +03003694static void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003695{
3696 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003697 struct xhci_virt_device *virt_dev;
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003698 struct xhci_slot_ctx *slot_ctx;
Andiry Xu64927732010-10-14 07:22:45 -07003699 int i, ret;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003700
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003701#ifndef CONFIG_USB_DEFAULT_PERSIST
3702 /*
3703 * We called pm_runtime_get_noresume when the device was attached.
3704 * Decrement the counter here to allow controller to runtime suspend
3705 * if no devices remain.
3706 */
3707 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003708 pm_runtime_put_noidle(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003709#endif
3710
Andiry Xu64927732010-10-14 07:22:45 -07003711 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003712 /* If the host is halted due to driver unload, we still need to free the
3713 * device.
3714 */
Lu Baolucd3f1792017-10-05 11:21:41 +03003715 if (ret <= 0 && ret != -ENODEV)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003716 return;
Andiry Xu64927732010-10-14 07:22:45 -07003717
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003718 virt_dev = xhci->devs[udev->slot_id];
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003719 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3720 trace_xhci_free_dev(slot_ctx);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003721
3722 /* Stop any wayward timer functions (which may grab the lock) */
Felipe Balbi98871e92017-01-23 14:20:04 +02003723 for (i = 0; i < 31; i++) {
Mathias Nyman9983a5f2017-01-23 14:19:52 +02003724 virt_dev->eps[i].ep_state &= ~EP_STOP_CMD_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003725 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3726 }
Zhengjun Xing8c5a93e2018-02-12 14:24:50 +02003727 xhci_debugfs_remove_slot(xhci, udev->slot_id);
Mathias Nyman44a182b2018-05-03 17:30:07 +03003728 virt_dev->udev = NULL;
Lu Baolu11ec7582017-10-05 11:21:42 +03003729 ret = xhci_disable_slot(xhci, udev->slot_id);
Zhengjun Xing8c5a93e2018-02-12 14:24:50 +02003730 if (ret)
Lu Baolu11ec7582017-10-05 11:21:42 +03003731 xhci_free_virt_device(xhci, udev->slot_id);
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003732}
3733
Lu Baolucd3f1792017-10-05 11:21:41 +03003734int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id)
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003735{
Lu Baolucd3f1792017-10-05 11:21:41 +03003736 struct xhci_command *command;
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003737 unsigned long flags;
3738 u32 state;
3739 int ret = 0;
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003740
Mathias Nyman103afda2017-12-08 17:59:08 +02003741 command = xhci_alloc_command(xhci, false, GFP_KERNEL);
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003742 if (!command)
3743 return -ENOMEM;
3744
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003745 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003746 /* Don't disable the slot if the host controller is dead. */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02003747 state = readl(&xhci->op_regs->status);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003748 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3749 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003750 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003751 kfree(command);
Lu Baoludcabc76f2017-10-05 11:21:43 +03003752 return -ENODEV;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003753 }
3754
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003755 ret = xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT,
3756 slot_id);
3757 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003758 spin_unlock_irqrestore(&xhci->lock, flags);
Lu Baolucd3f1792017-10-05 11:21:41 +03003759 kfree(command);
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003760 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003761 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003762 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003763 spin_unlock_irqrestore(&xhci->lock, flags);
Guoqing Zhangf9e609b2017-04-07 17:56:52 +03003764 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003765}
3766
3767/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003768 * Checks if we have enough host controller resources for the default control
3769 * endpoint.
3770 *
3771 * Must be called with xhci->lock held.
3772 */
3773static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3774{
3775 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003776 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3777 "Not enough ep ctxs: "
3778 "%u active, need to add 1, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003779 xhci->num_active_eps, xhci->limit_active_eps);
3780 return -ENOMEM;
3781 }
3782 xhci->num_active_eps += 1;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003783 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3784 "Adding 1 ep ctx, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003785 xhci->num_active_eps);
3786 return 0;
3787}
3788
3789
3790/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003791 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3792 * timed out, or allocating memory failed. Returns 1 on success.
3793 */
3794int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3795{
3796 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003797 struct xhci_virt_device *vdev;
3798 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003799 unsigned long flags;
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003800 int ret, slot_id;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003801 struct xhci_command *command;
3802
Mathias Nyman103afda2017-12-08 17:59:08 +02003803 command = xhci_alloc_command(xhci, true, GFP_KERNEL);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003804 if (!command)
3805 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003806
3807 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003808 ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003809 if (ret) {
3810 spin_unlock_irqrestore(&xhci->lock, flags);
3811 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
Lu Baolu87e44f22016-11-11 15:13:30 +02003812 xhci_free_command(xhci, command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003813 return 0;
3814 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003815 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003816 spin_unlock_irqrestore(&xhci->lock, flags);
3817
Mathias Nymanc311e392014-05-08 19:26:03 +03003818 wait_for_completion(command->completion);
Lu Baoluc2d3d492016-11-11 15:13:31 +02003819 slot_id = command->slot_id;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003820
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003821 if (!slot_id || command->status != COMP_SUCCESS) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003822 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharpbe982032014-05-08 19:25:59 +03003823 xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n",
3824 HCS_MAX_SLOTS(
3825 readl(&xhci->cap_regs->hcs_params1)));
Lu Baolu87e44f22016-11-11 15:13:30 +02003826 xhci_free_command(xhci, command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003827 return 0;
3828 }
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003829
Lu Baolucd3f1792017-10-05 11:21:41 +03003830 xhci_free_command(xhci, command);
3831
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003832 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3833 spin_lock_irqsave(&xhci->lock, flags);
3834 ret = xhci_reserve_host_control_ep_resources(xhci);
3835 if (ret) {
3836 spin_unlock_irqrestore(&xhci->lock, flags);
3837 xhci_warn(xhci, "Not enough host resources, "
3838 "active endpoint contexts = %u\n",
3839 xhci->num_active_eps);
3840 goto disable_slot;
3841 }
3842 spin_unlock_irqrestore(&xhci->lock, flags);
3843 }
3844 /* Use GFP_NOIO, since this function can be called from
Sarah Sharpa6d940d2010-12-28 13:08:42 -08003845 * xhci_discover_or_reset_device(), which may be called as part of
3846 * mass storage driver error handling.
3847 */
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003848 if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003849 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003850 goto disable_slot;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003851 }
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003852 vdev = xhci->devs[slot_id];
3853 slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx);
3854 trace_xhci_alloc_dev(slot_ctx);
3855
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003856 udev->slot_id = slot_id;
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003857
Lu Baolu02b6fdc2017-10-05 11:21:39 +03003858 xhci_debugfs_create_slot(xhci, slot_id);
3859
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003860#ifndef CONFIG_USB_DEFAULT_PERSIST
3861 /*
3862 * If resetting upon resume, we can't put the controller into runtime
3863 * suspend if there is a device attached.
3864 */
3865 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003866 pm_runtime_get_noresume(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003867#endif
3868
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003869 /* Is this a LS or FS device under a HS hub? */
3870 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003871 return 1;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003872
3873disable_slot:
Lu Baolu11ec7582017-10-05 11:21:42 +03003874 ret = xhci_disable_slot(xhci, udev->slot_id);
3875 if (ret)
3876 xhci_free_virt_device(xhci, udev->slot_id);
3877
3878 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003879}
3880
3881/*
Dan Williams48fc7db2013-12-05 17:07:27 -08003882 * Issue an Address Device command and optionally send a corresponding
3883 * SetAddress request to the device.
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003884 */
Dan Williams48fc7db2013-12-05 17:07:27 -08003885static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev,
3886 enum xhci_setup_dev setup)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003887{
Dan Williams6f8ffc02013-11-22 01:20:01 -08003888 const char *act = setup == SETUP_CONTEXT_ONLY ? "context" : "address";
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003889 unsigned long flags;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003890 struct xhci_virt_device *virt_dev;
3891 int ret = 0;
3892 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07003893 struct xhci_slot_ctx *slot_ctx;
3894 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003895 u64 temp_64;
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003896 struct xhci_command *command = NULL;
3897
3898 mutex_lock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003899
Lu Baolu90797ae2017-01-03 18:28:44 +02003900 if (xhci->xhc_state) { /* dying, removing or halted */
3901 ret = -ESHUTDOWN;
Roger Quadros448116b2015-09-21 17:46:15 +03003902 goto out;
Lu Baolu90797ae2017-01-03 18:28:44 +02003903 }
Roger Quadros448116b2015-09-21 17:46:15 +03003904
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003905 if (!udev->slot_id) {
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003906 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3907 "Bad Slot ID %d", udev->slot_id);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003908 ret = -EINVAL;
3909 goto out;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003910 }
3911
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003912 virt_dev = xhci->devs[udev->slot_id];
3913
Matt Evans7ed603e2011-03-29 13:40:56 +11003914 if (WARN_ON(!virt_dev)) {
3915 /*
3916 * In plug/unplug torture test with an NEC controller,
3917 * a zero-dereference was observed once due to virt_dev = 0.
3918 * Print useful debug rather than crash if it is observed again!
3919 */
3920 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3921 udev->slot_id);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003922 ret = -EINVAL;
3923 goto out;
Matt Evans7ed603e2011-03-29 13:40:56 +11003924 }
Felipe Balbi19a7d0d62017-04-07 17:56:57 +03003925 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3926 trace_xhci_setup_device_slot(slot_ctx);
Matt Evans7ed603e2011-03-29 13:40:56 +11003927
Mathias Nymanf161ead2015-01-09 17:18:28 +02003928 if (setup == SETUP_CONTEXT_ONLY) {
Mathias Nymanf161ead2015-01-09 17:18:28 +02003929 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3930 SLOT_STATE_DEFAULT) {
3931 xhci_dbg(xhci, "Slot already in default state\n");
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003932 goto out;
Mathias Nymanf161ead2015-01-09 17:18:28 +02003933 }
3934 }
3935
Mathias Nyman103afda2017-12-08 17:59:08 +02003936 command = xhci_alloc_command(xhci, true, GFP_KERNEL);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003937 if (!command) {
3938 ret = -ENOMEM;
3939 goto out;
3940 }
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003941
3942 command->in_ctx = virt_dev->in_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003943
Andiry Xuf0615c42010-10-14 07:22:48 -07003944 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Lin Wang4daf9df2015-01-09 16:06:31 +02003945 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003946 if (!ctrl_ctx) {
3947 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3948 __func__);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003949 ret = -EINVAL;
3950 goto out;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003951 }
Andiry Xuf0615c42010-10-14 07:22:48 -07003952 /*
3953 * If this is the first Set Address since device plug-in or
3954 * virt_device realloaction after a resume with an xHCI power loss,
3955 * then set up the slot context.
3956 */
3957 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003958 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07003959 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02003960 else
3961 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharpd31c2852011-11-03 13:06:08 -07003962 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3963 ctrl_ctx->drop_flags = 0;
3964
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003965 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003966 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003967
Sarah Sharpf88ba782009-05-14 11:44:22 -07003968 spin_lock_irqsave(&xhci->lock, flags);
Felipe Balbia711ede2017-01-23 14:20:23 +02003969 trace_xhci_setup_device(virt_dev);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003970 ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma,
Dan Williams48fc7db2013-12-05 17:07:27 -08003971 udev->slot_id, setup);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003972 if (ret) {
3973 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003974 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3975 "FIXME: allocate a command ring segment");
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003976 goto out;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003977 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003978 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003979 spin_unlock_irqrestore(&xhci->lock, flags);
3980
3981 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
Mathias Nymanc311e392014-05-08 19:26:03 +03003982 wait_for_completion(command->completion);
3983
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003984 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3985 * the SetAddress() "recovery interval" required by USB and aborting the
3986 * command on a timeout.
3987 */
Mathias Nyman9ea18332014-05-08 19:26:02 +03003988 switch (command->status) {
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003989 case COMP_COMMAND_ABORTED:
Mathias Nyman604d02a2017-05-17 18:32:05 +03003990 case COMP_COMMAND_RING_STOPPED:
Mathias Nymanc311e392014-05-08 19:26:03 +03003991 xhci_warn(xhci, "Timeout while waiting for setup device command\n");
3992 ret = -ETIME;
3993 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02003994 case COMP_CONTEXT_STATE_ERROR:
3995 case COMP_SLOT_NOT_ENABLED_ERROR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003996 xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n",
3997 act, udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003998 ret = -EINVAL;
3999 break;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02004000 case COMP_USB_TRANSACTION_ERROR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08004001 dev_warn(&udev->dev, "Device not responding to setup %s.\n", act);
Lu Baolu651aaf32017-10-05 11:21:45 +03004002
4003 mutex_unlock(&xhci->mutex);
4004 ret = xhci_disable_slot(xhci, udev->slot_id);
4005 if (!ret)
4006 xhci_alloc_dev(hcd, udev);
4007 kfree(command->completion);
4008 kfree(command);
4009 return -EPROTO;
Felipe Balbi0b7c1052017-01-23 14:20:06 +02004010 case COMP_INCOMPATIBLE_DEVICE_ERROR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08004011 dev_warn(&udev->dev,
4012 "ERROR: Incompatible device for setup %s command\n", act);
Alex Hef6ba6fe2011-06-08 18:34:06 +08004013 ret = -ENODEV;
4014 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07004015 case COMP_SUCCESS:
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03004016 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
Dan Williams6f8ffc02013-11-22 01:20:01 -08004017 "Successful setup %s command", act);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07004018 break;
4019 default:
Dan Williams6f8ffc02013-11-22 01:20:01 -08004020 xhci_err(xhci,
4021 "ERROR: unexpected setup %s command completion code 0x%x.\n",
Mathias Nyman9ea18332014-05-08 19:26:02 +03004022 act, command->status);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03004023 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07004024 ret = -EINVAL;
4025 break;
4026 }
Chris Bainbridgea00918d2015-05-19 16:30:51 +03004027 if (ret)
4028 goto out;
Sarah Sharpf7b2e402014-01-30 13:27:49 -08004029 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03004030 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
4031 "Op regs DCBAA ptr = %#016llx", temp_64);
4032 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
4033 "Slot ID %d dcbaa entry @%p = %#016llx",
4034 udev->slot_id,
4035 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
4036 (unsigned long long)
4037 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
4038 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
4039 "Output Context DMA address = %#08llx",
John Yound115b042009-07-27 12:05:15 -07004040 (unsigned long long)virt_dev->out_ctx->dma);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03004041 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02004042 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07004043 /*
4044 * USB core uses address 1 for the roothubs, so we add one to the
4045 * address given back to us by the HC.
4046 */
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03004047 trace_xhci_address_ctx(xhci, virt_dev->out_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02004048 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharpf94e01862009-04-27 19:58:38 -07004049 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07004050 ctrl_ctx->add_flags = 0;
4051 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07004052
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03004053 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
Dan Williamsa2cdc342013-10-16 12:25:44 -07004054 "Internal device address = %d",
4055 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03004056out:
4057 mutex_unlock(&xhci->mutex);
Lu Baolu87e44f22016-11-11 15:13:30 +02004058 if (command) {
4059 kfree(command->completion);
4060 kfree(command);
4061 }
Chris Bainbridgea00918d2015-05-19 16:30:51 +03004062 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07004063}
4064
Lu Baolu39693842017-04-07 17:57:04 +03004065static int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
Dan Williams48fc7db2013-12-05 17:07:27 -08004066{
4067 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ADDRESS);
4068}
4069
Lu Baolu39693842017-04-07 17:57:04 +03004070static int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev)
Dan Williams48fc7db2013-12-05 17:07:27 -08004071{
4072 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ONLY);
4073}
4074
Lan Tianyu3f5eb142013-03-19 16:48:12 +08004075/*
4076 * Transfer the port index into real index in the HW port status
4077 * registers. Caculate offset between the port's PORTSC register
4078 * and port status base. Divide the number of per port register
4079 * to get the real index. The raw port number bases 1.
4080 */
4081int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1)
4082{
Mathias Nyman38986ff2018-05-21 16:40:01 +03004083 struct xhci_hub *rhub;
Lan Tianyu3f5eb142013-03-19 16:48:12 +08004084
Mathias Nyman38986ff2018-05-21 16:40:01 +03004085 rhub = xhci_get_rhub(hcd);
4086 return rhub->ports[port1 - 1]->hw_portnum + 1;
Lan Tianyu3f5eb142013-03-19 16:48:12 +08004087}
4088
Mathias Nymana558ccd2013-05-23 17:14:30 +03004089/*
4090 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
4091 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
4092 */
Olof Johanssond5c82fe2013-07-23 11:58:20 -07004093static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
Mathias Nymana558ccd2013-05-23 17:14:30 +03004094 struct usb_device *udev, u16 max_exit_latency)
4095{
4096 struct xhci_virt_device *virt_dev;
4097 struct xhci_command *command;
4098 struct xhci_input_control_ctx *ctrl_ctx;
4099 struct xhci_slot_ctx *slot_ctx;
4100 unsigned long flags;
4101 int ret;
4102
4103 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nyman96044692014-09-11 13:55:50 +03004104
4105 virt_dev = xhci->devs[udev->slot_id];
4106
4107 /*
4108 * virt_dev might not exists yet if xHC resumed from hibernate (S4) and
4109 * xHC was re-initialized. Exit latency will be set later after
4110 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated
4111 */
4112
4113 if (!virt_dev || max_exit_latency == virt_dev->current_mel) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004114 spin_unlock_irqrestore(&xhci->lock, flags);
4115 return 0;
4116 }
4117
4118 /* Attempt to issue an Evaluate Context command to change the MEL. */
Mathias Nymana558ccd2013-05-23 17:14:30 +03004119 command = xhci->lpm_command;
Lin Wang4daf9df2015-01-09 16:06:31 +02004120 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07004121 if (!ctrl_ctx) {
4122 spin_unlock_irqrestore(&xhci->lock, flags);
4123 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4124 __func__);
4125 return -ENOMEM;
4126 }
4127
Mathias Nymana558ccd2013-05-23 17:14:30 +03004128 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
4129 spin_unlock_irqrestore(&xhci->lock, flags);
4130
Mathias Nymana558ccd2013-05-23 17:14:30 +03004131 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
4132 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
4133 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
4134 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
Mathias Nyman4801d4ea2014-11-27 18:19:15 +02004135 slot_ctx->dev_state = 0;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004136
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03004137 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
4138 "Set up evaluate context for LPM MEL change.");
Mathias Nymana558ccd2013-05-23 17:14:30 +03004139
4140 /* Issue and wait for the evaluate context command. */
4141 ret = xhci_configure_endpoint(xhci, udev, command,
4142 true, true);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004143
4144 if (!ret) {
4145 spin_lock_irqsave(&xhci->lock, flags);
4146 virt_dev->current_mel = max_exit_latency;
4147 spin_unlock_irqrestore(&xhci->lock, flags);
4148 }
4149 return ret;
4150}
4151
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004152#ifdef CONFIG_PM
Andiry Xu95743232011-09-23 14:19:51 -07004153
4154/* BESL to HIRD Encoding array for USB2 LPM */
4155static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
4156 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
4157
4158/* Calculate HIRD/BESL for USB2 PORTPMSC*/
Andiry Xuf99298b2011-12-12 16:45:28 +08004159static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
4160 struct usb_device *udev)
Andiry Xu95743232011-09-23 14:19:51 -07004161{
Andiry Xuf99298b2011-12-12 16:45:28 +08004162 int u2del, besl, besl_host;
4163 int besl_device = 0;
4164 u32 field;
Andiry Xu95743232011-09-23 14:19:51 -07004165
Andiry Xuf99298b2011-12-12 16:45:28 +08004166 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
4167 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4168
4169 if (field & USB_BESL_SUPPORT) {
4170 for (besl_host = 0; besl_host < 16; besl_host++) {
4171 if (xhci_besl_encoding[besl_host] >= u2del)
Andiry Xu95743232011-09-23 14:19:51 -07004172 break;
4173 }
Andiry Xuf99298b2011-12-12 16:45:28 +08004174 /* Use baseline BESL value as default */
4175 if (field & USB_BESL_BASELINE_VALID)
4176 besl_device = USB_GET_BESL_BASELINE(field);
4177 else if (field & USB_BESL_DEEP_VALID)
4178 besl_device = USB_GET_BESL_DEEP(field);
Andiry Xu95743232011-09-23 14:19:51 -07004179 } else {
4180 if (u2del <= 50)
Andiry Xuf99298b2011-12-12 16:45:28 +08004181 besl_host = 0;
Andiry Xu95743232011-09-23 14:19:51 -07004182 else
Andiry Xuf99298b2011-12-12 16:45:28 +08004183 besl_host = (u2del - 51) / 75 + 1;
Andiry Xu95743232011-09-23 14:19:51 -07004184 }
4185
Andiry Xuf99298b2011-12-12 16:45:28 +08004186 besl = besl_host + besl_device;
4187 if (besl > 15)
4188 besl = 15;
4189
4190 return besl;
Andiry Xu95743232011-09-23 14:19:51 -07004191}
4192
Mathias Nymana558ccd2013-05-23 17:14:30 +03004193/* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
4194static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev)
4195{
4196 u32 field;
4197 int l1;
4198 int besld = 0;
4199 int hirdm = 0;
4200
4201 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4202
4203 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
Mathias Nyman17f34862013-05-23 17:14:31 +03004204 l1 = udev->l1_params.timeout / 256;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004205
4206 /* device has preferred BESLD */
4207 if (field & USB_BESL_DEEP_VALID) {
4208 besld = USB_GET_BESL_DEEP(field);
4209 hirdm = 1;
4210 }
4211
4212 return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm);
4213}
4214
Lu Baolu39693842017-04-07 17:57:04 +03004215static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
Andiry Xu65580b432011-09-23 14:19:52 -07004216 struct usb_device *udev, int enable)
4217{
4218 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Mathias Nyman38986ff2018-05-21 16:40:01 +03004219 struct xhci_port **ports;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004220 __le32 __iomem *pm_addr, *hlpm_addr;
4221 u32 pm_val, hlpm_val, field;
Andiry Xu65580b432011-09-23 14:19:52 -07004222 unsigned int port_num;
4223 unsigned long flags;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004224 int hird, exit_latency;
4225 int ret;
Andiry Xu65580b432011-09-23 14:19:52 -07004226
Mathias Nymanb50107b2015-10-01 18:40:38 +03004227 if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support ||
Andiry Xu65580b432011-09-23 14:19:52 -07004228 !udev->lpm_capable)
4229 return -EPERM;
4230
4231 if (!udev->parent || udev->parent->parent ||
4232 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4233 return -EPERM;
4234
4235 if (udev->usb2_hw_lpm_capable != 1)
4236 return -EPERM;
4237
4238 spin_lock_irqsave(&xhci->lock, flags);
4239
Mathias Nyman38986ff2018-05-21 16:40:01 +03004240 ports = xhci->usb2_rhub.ports;
Andiry Xu65580b432011-09-23 14:19:52 -07004241 port_num = udev->portnum - 1;
Mathias Nyman38986ff2018-05-21 16:40:01 +03004242 pm_addr = ports[port_num]->addr + PORTPMSC;
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004243 pm_val = readl(pm_addr);
Mathias Nyman38986ff2018-05-21 16:40:01 +03004244 hlpm_addr = ports[port_num]->addr + PORTHLPMC;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004245 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
Andiry Xu65580b432011-09-23 14:19:52 -07004246
4247 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
Lin Wang654a55d2014-05-08 19:25:54 +03004248 enable ? "enable" : "disable", port_num + 1);
Andiry Xu65580b432011-09-23 14:19:52 -07004249
Thang Q. Nguyen4750bc72017-10-05 11:21:37 +03004250 if (enable && !(xhci->quirks & XHCI_HW_LPM_DISABLE)) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004251 /* Host supports BESL timeout instead of HIRD */
4252 if (udev->usb2_hw_lpm_besl_capable) {
4253 /* if device doesn't have a preferred BESL value use a
4254 * default one which works with mixed HIRD and BESL
4255 * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4256 */
4257 if ((field & USB_BESL_SUPPORT) &&
4258 (field & USB_BESL_BASELINE_VALID))
4259 hird = USB_GET_BESL_BASELINE(field);
4260 else
Mathias Nyman17f34862013-05-23 17:14:31 +03004261 hird = udev->l1_params.besl;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004262
4263 exit_latency = xhci_besl_encoding[hird];
4264 spin_unlock_irqrestore(&xhci->lock, flags);
4265
4266 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4267 * input context for link powermanagement evaluate
4268 * context commands. It is protected by hcd->bandwidth
4269 * mutex and is shared by all devices. We need to set
4270 * the max ext latency in USB 2 BESL LPM as well, so
4271 * use the same mutex and xhci_change_max_exit_latency()
4272 */
4273 mutex_lock(hcd->bandwidth_mutex);
4274 ret = xhci_change_max_exit_latency(xhci, udev,
4275 exit_latency);
4276 mutex_unlock(hcd->bandwidth_mutex);
4277
4278 if (ret < 0)
4279 return ret;
4280 spin_lock_irqsave(&xhci->lock, flags);
4281
4282 hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004283 writel(hlpm_val, hlpm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004284 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004285 readl(hlpm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004286 } else {
4287 hird = xhci_calculate_hird_besl(xhci, udev);
4288 }
4289
4290 pm_val &= ~PORT_HIRD_MASK;
Sarah Sharp58e21f72013-10-07 17:17:20 -07004291 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004292 writel(pm_val, pm_addr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004293 pm_val = readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004294 pm_val |= PORT_HLE;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004295 writel(pm_val, pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004296 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004297 readl(pm_addr);
Andiry Xu65580b432011-09-23 14:19:52 -07004298 } else {
Sarah Sharp58e21f72013-10-07 17:17:20 -07004299 pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK | PORT_L1DS_MASK);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004300 writel(pm_val, pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004301 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004302 readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004303 if (udev->usb2_hw_lpm_besl_capable) {
4304 spin_unlock_irqrestore(&xhci->lock, flags);
4305 mutex_lock(hcd->bandwidth_mutex);
4306 xhci_change_max_exit_latency(xhci, udev, 0);
4307 mutex_unlock(hcd->bandwidth_mutex);
4308 return 0;
4309 }
Andiry Xu65580b432011-09-23 14:19:52 -07004310 }
4311
4312 spin_unlock_irqrestore(&xhci->lock, flags);
4313 return 0;
4314}
4315
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004316/* check if a usb2 port supports a given extened capability protocol
4317 * only USB2 ports extended protocol capability values are cached.
4318 * Return 1 if capability is supported
4319 */
4320static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port,
4321 unsigned capability)
4322{
4323 u32 port_offset, port_count;
4324 int i;
4325
4326 for (i = 0; i < xhci->num_ext_caps; i++) {
4327 if (xhci->ext_caps[i] & capability) {
4328 /* port offsets starts at 1 */
4329 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1;
4330 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]);
4331 if (port >= port_offset &&
4332 port < port_offset + port_count)
4333 return 1;
4334 }
4335 }
4336 return 0;
4337}
4338
Lu Baolu39693842017-04-07 17:57:04 +03004339static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004340{
4341 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004342 int portnum = udev->portnum - 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004343
Mathias Nymanb50107b2015-10-01 18:40:38 +03004344 if (hcd->speed >= HCD_USB3 || !xhci->sw_lpm_support ||
Sarah Sharpde68bab2013-09-30 17:26:28 +03004345 !udev->lpm_capable)
4346 return 0;
4347
4348 /* we only support lpm for non-hub device connected to root hub yet */
4349 if (!udev->parent || udev->parent->parent ||
4350 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4351 return 0;
4352
4353 if (xhci->hw_lpm_support == 1 &&
4354 xhci_check_usb2_port_capability(
4355 xhci, portnum, XHCI_HLC)) {
4356 udev->usb2_hw_lpm_capable = 1;
4357 udev->l1_params.timeout = XHCI_L1_TIMEOUT;
4358 udev->l1_params.besl = XHCI_DEFAULT_BESL;
4359 if (xhci_check_usb2_port_capability(xhci, portnum,
4360 XHCI_BLC))
4361 udev->usb2_hw_lpm_besl_capable = 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004362 }
4363
4364 return 0;
4365}
4366
Sarah Sharp3b3db022012-05-09 10:55:03 -07004367/*---------------------- USB 3.0 Link PM functions ------------------------*/
4368
Sarah Sharpe3567d22012-05-16 13:36:24 -07004369/* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4370static unsigned long long xhci_service_interval_to_ns(
4371 struct usb_endpoint_descriptor *desc)
4372{
Oliver Neukum16b45fd2012-10-17 10:16:16 +02004373 return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004374}
4375
Sarah Sharp3b3db022012-05-09 10:55:03 -07004376static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
4377 enum usb3_link_state state)
4378{
4379 unsigned long long sel;
4380 unsigned long long pel;
4381 unsigned int max_sel_pel;
4382 char *state_name;
4383
4384 switch (state) {
4385 case USB3_LPM_U1:
4386 /* Convert SEL and PEL stored in nanoseconds to microseconds */
4387 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
4388 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
4389 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
4390 state_name = "U1";
4391 break;
4392 case USB3_LPM_U2:
4393 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
4394 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
4395 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
4396 state_name = "U2";
4397 break;
4398 default:
4399 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
4400 __func__);
Sarah Sharpe25e62a2012-06-07 11:10:32 -07004401 return USB3_LPM_DISABLED;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004402 }
4403
4404 if (sel <= max_sel_pel && pel <= max_sel_pel)
4405 return USB3_LPM_DEVICE_INITIATED;
4406
4407 if (sel > max_sel_pel)
4408 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4409 "due to long SEL %llu ms\n",
4410 state_name, sel);
4411 else
4412 dev_dbg(&udev->dev, "Device-initiated %s disabled "
Joe Perches03e64e92013-07-16 19:25:59 -07004413 "due to long PEL %llu ms\n",
Sarah Sharp3b3db022012-05-09 10:55:03 -07004414 state_name, pel);
4415 return USB3_LPM_DISABLED;
4416}
4417
Pratyush Anand9502c462014-07-04 17:01:23 +03004418/* The U1 timeout should be the maximum of the following values:
Sarah Sharpe3567d22012-05-16 13:36:24 -07004419 * - For control endpoints, U1 system exit latency (SEL) * 3
4420 * - For bulk endpoints, U1 SEL * 5
4421 * - For interrupt endpoints:
4422 * - Notification EPs, U1 SEL * 3
4423 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4424 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4425 */
Pratyush Anand9502c462014-07-04 17:01:23 +03004426static unsigned long long xhci_calculate_intel_u1_timeout(
4427 struct usb_device *udev,
Sarah Sharpe3567d22012-05-16 13:36:24 -07004428 struct usb_endpoint_descriptor *desc)
4429{
4430 unsigned long long timeout_ns;
4431 int ep_type;
4432 int intr_type;
4433
4434 ep_type = usb_endpoint_type(desc);
4435 switch (ep_type) {
4436 case USB_ENDPOINT_XFER_CONTROL:
4437 timeout_ns = udev->u1_params.sel * 3;
4438 break;
4439 case USB_ENDPOINT_XFER_BULK:
4440 timeout_ns = udev->u1_params.sel * 5;
4441 break;
4442 case USB_ENDPOINT_XFER_INT:
4443 intr_type = usb_endpoint_interrupt_type(desc);
4444 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
4445 timeout_ns = udev->u1_params.sel * 3;
4446 break;
4447 }
4448 /* Otherwise the calculation is the same as isoc eps */
Gustavo A. R. Silva7d864992017-10-25 13:49:01 -05004449 /* fall through */
Sarah Sharpe3567d22012-05-16 13:36:24 -07004450 case USB_ENDPOINT_XFER_ISOC:
4451 timeout_ns = xhci_service_interval_to_ns(desc);
Sarah Sharpc88db162012-05-21 08:44:33 -07004452 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004453 if (timeout_ns < udev->u1_params.sel * 2)
4454 timeout_ns = udev->u1_params.sel * 2;
4455 break;
4456 default:
4457 return 0;
4458 }
4459
Pratyush Anand9502c462014-07-04 17:01:23 +03004460 return timeout_ns;
4461}
4462
4463/* Returns the hub-encoded U1 timeout value. */
4464static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci,
4465 struct usb_device *udev,
4466 struct usb_endpoint_descriptor *desc)
4467{
4468 unsigned long long timeout_ns;
4469
4470 if (xhci->quirks & XHCI_INTEL_HOST)
4471 timeout_ns = xhci_calculate_intel_u1_timeout(udev, desc);
4472 else
4473 timeout_ns = udev->u1_params.sel;
4474
4475 /* The U1 timeout is encoded in 1us intervals.
4476 * Don't return a timeout of zero, because that's USB3_LPM_DISABLED.
4477 */
Sarah Sharpe3567d22012-05-16 13:36:24 -07004478 if (timeout_ns == USB3_LPM_DISABLED)
Pratyush Anand9502c462014-07-04 17:01:23 +03004479 timeout_ns = 1;
4480 else
4481 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004482
4483 /* If the necessary timeout value is bigger than what we can set in the
4484 * USB 3.0 hub, we have to disable hub-initiated U1.
4485 */
4486 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
4487 return timeout_ns;
4488 dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
4489 "due to long timeout %llu ms\n", timeout_ns);
4490 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
4491}
4492
Pratyush Anand9502c462014-07-04 17:01:23 +03004493/* The U2 timeout should be the maximum of:
Sarah Sharpe3567d22012-05-16 13:36:24 -07004494 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4495 * - largest bInterval of any active periodic endpoint (to avoid going
4496 * into lower power link states between intervals).
4497 * - the U2 Exit Latency of the device
4498 */
Pratyush Anand9502c462014-07-04 17:01:23 +03004499static unsigned long long xhci_calculate_intel_u2_timeout(
4500 struct usb_device *udev,
Sarah Sharpe3567d22012-05-16 13:36:24 -07004501 struct usb_endpoint_descriptor *desc)
4502{
4503 unsigned long long timeout_ns;
4504 unsigned long long u2_del_ns;
4505
4506 timeout_ns = 10 * 1000 * 1000;
4507
4508 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
4509 (xhci_service_interval_to_ns(desc) > timeout_ns))
4510 timeout_ns = xhci_service_interval_to_ns(desc);
4511
Oliver Neukum966e7a82012-10-17 12:17:50 +02004512 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004513 if (u2_del_ns > timeout_ns)
4514 timeout_ns = u2_del_ns;
4515
Pratyush Anand9502c462014-07-04 17:01:23 +03004516 return timeout_ns;
4517}
4518
4519/* Returns the hub-encoded U2 timeout value. */
4520static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci,
4521 struct usb_device *udev,
4522 struct usb_endpoint_descriptor *desc)
4523{
4524 unsigned long long timeout_ns;
4525
4526 if (xhci->quirks & XHCI_INTEL_HOST)
4527 timeout_ns = xhci_calculate_intel_u2_timeout(udev, desc);
4528 else
4529 timeout_ns = udev->u2_params.sel;
4530
Sarah Sharpe3567d22012-05-16 13:36:24 -07004531 /* The U2 timeout is encoded in 256us intervals */
Sarah Sharpc88db162012-05-21 08:44:33 -07004532 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004533 /* If the necessary timeout value is bigger than what we can set in the
4534 * USB 3.0 hub, we have to disable hub-initiated U2.
4535 */
4536 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
4537 return timeout_ns;
4538 dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
4539 "due to long timeout %llu ms\n", timeout_ns);
4540 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
4541}
4542
Sarah Sharp3b3db022012-05-09 10:55:03 -07004543static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4544 struct usb_device *udev,
4545 struct usb_endpoint_descriptor *desc,
4546 enum usb3_link_state state,
4547 u16 *timeout)
4548{
Pratyush Anand9502c462014-07-04 17:01:23 +03004549 if (state == USB3_LPM_U1)
4550 return xhci_calculate_u1_timeout(xhci, udev, desc);
4551 else if (state == USB3_LPM_U2)
4552 return xhci_calculate_u2_timeout(xhci, udev, desc);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004553
Sarah Sharp3b3db022012-05-09 10:55:03 -07004554 return USB3_LPM_DISABLED;
4555}
4556
4557static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4558 struct usb_device *udev,
4559 struct usb_endpoint_descriptor *desc,
4560 enum usb3_link_state state,
4561 u16 *timeout)
4562{
4563 u16 alt_timeout;
4564
4565 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
4566 desc, state, timeout);
4567
4568 /* If we found we can't enable hub-initiated LPM, or
4569 * the U1 or U2 exit latency was too high to allow
4570 * device-initiated LPM as well, just stop searching.
4571 */
4572 if (alt_timeout == USB3_LPM_DISABLED ||
4573 alt_timeout == USB3_LPM_DEVICE_INITIATED) {
4574 *timeout = alt_timeout;
4575 return -E2BIG;
4576 }
4577 if (alt_timeout > *timeout)
4578 *timeout = alt_timeout;
4579 return 0;
4580}
4581
4582static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
4583 struct usb_device *udev,
4584 struct usb_host_interface *alt,
4585 enum usb3_link_state state,
4586 u16 *timeout)
4587{
4588 int j;
4589
4590 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
4591 if (xhci_update_timeout_for_endpoint(xhci, udev,
4592 &alt->endpoint[j].desc, state, timeout))
4593 return -E2BIG;
4594 continue;
4595 }
4596 return 0;
4597}
4598
Sarah Sharpe3567d22012-05-16 13:36:24 -07004599static int xhci_check_intel_tier_policy(struct usb_device *udev,
4600 enum usb3_link_state state)
4601{
4602 struct usb_device *parent;
4603 unsigned int num_hubs;
4604
4605 if (state == USB3_LPM_U2)
4606 return 0;
4607
4608 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4609 for (parent = udev->parent, num_hubs = 0; parent->parent;
4610 parent = parent->parent)
4611 num_hubs++;
4612
4613 if (num_hubs < 2)
4614 return 0;
4615
4616 dev_dbg(&udev->dev, "Disabling U1 link state for device"
4617 " below second-tier hub.\n");
4618 dev_dbg(&udev->dev, "Plug device into first-tier hub "
4619 "to decrease power consumption.\n");
4620 return -E2BIG;
4621}
4622
Sarah Sharp3b3db022012-05-09 10:55:03 -07004623static int xhci_check_tier_policy(struct xhci_hcd *xhci,
4624 struct usb_device *udev,
4625 enum usb3_link_state state)
4626{
Sarah Sharpe3567d22012-05-16 13:36:24 -07004627 if (xhci->quirks & XHCI_INTEL_HOST)
4628 return xhci_check_intel_tier_policy(udev, state);
Pratyush Anand9502c462014-07-04 17:01:23 +03004629 else
4630 return 0;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004631}
4632
4633/* Returns the U1 or U2 timeout that should be enabled.
4634 * If the tier check or timeout setting functions return with a non-zero exit
4635 * code, that means the timeout value has been finalized and we shouldn't look
4636 * at any more endpoints.
4637 */
4638static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
4639 struct usb_device *udev, enum usb3_link_state state)
4640{
4641 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4642 struct usb_host_config *config;
4643 char *state_name;
4644 int i;
4645 u16 timeout = USB3_LPM_DISABLED;
4646
4647 if (state == USB3_LPM_U1)
4648 state_name = "U1";
4649 else if (state == USB3_LPM_U2)
4650 state_name = "U2";
4651 else {
4652 dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
4653 state);
4654 return timeout;
4655 }
4656
4657 if (xhci_check_tier_policy(xhci, udev, state) < 0)
4658 return timeout;
4659
4660 /* Gather some information about the currently installed configuration
4661 * and alternate interface settings.
4662 */
4663 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
4664 state, &timeout))
4665 return timeout;
4666
4667 config = udev->actconfig;
4668 if (!config)
4669 return timeout;
4670
Xenia Ragiadakou64ba4192013-08-26 23:29:46 +03004671 for (i = 0; i < config->desc.bNumInterfaces; i++) {
Sarah Sharp3b3db022012-05-09 10:55:03 -07004672 struct usb_driver *driver;
4673 struct usb_interface *intf = config->interface[i];
4674
4675 if (!intf)
4676 continue;
4677
4678 /* Check if any currently bound drivers want hub-initiated LPM
4679 * disabled.
4680 */
4681 if (intf->dev.driver) {
4682 driver = to_usb_driver(intf->dev.driver);
4683 if (driver && driver->disable_hub_initiated_lpm) {
4684 dev_dbg(&udev->dev, "Hub-initiated %s disabled "
4685 "at request of driver %s\n",
4686 state_name, driver->name);
4687 return xhci_get_timeout_no_hub_lpm(udev, state);
4688 }
4689 }
4690
4691 /* Not sure how this could happen... */
4692 if (!intf->cur_altsetting)
4693 continue;
4694
4695 if (xhci_update_timeout_for_interface(xhci, udev,
4696 intf->cur_altsetting,
4697 state, &timeout))
4698 return timeout;
4699 }
4700 return timeout;
4701}
4702
Sarah Sharp3b3db022012-05-09 10:55:03 -07004703static int calculate_max_exit_latency(struct usb_device *udev,
4704 enum usb3_link_state state_changed,
4705 u16 hub_encoded_timeout)
4706{
4707 unsigned long long u1_mel_us = 0;
4708 unsigned long long u2_mel_us = 0;
4709 unsigned long long mel_us = 0;
4710 bool disabling_u1;
4711 bool disabling_u2;
4712 bool enabling_u1;
4713 bool enabling_u2;
4714
4715 disabling_u1 = (state_changed == USB3_LPM_U1 &&
4716 hub_encoded_timeout == USB3_LPM_DISABLED);
4717 disabling_u2 = (state_changed == USB3_LPM_U2 &&
4718 hub_encoded_timeout == USB3_LPM_DISABLED);
4719
4720 enabling_u1 = (state_changed == USB3_LPM_U1 &&
4721 hub_encoded_timeout != USB3_LPM_DISABLED);
4722 enabling_u2 = (state_changed == USB3_LPM_U2 &&
4723 hub_encoded_timeout != USB3_LPM_DISABLED);
4724
4725 /* If U1 was already enabled and we're not disabling it,
4726 * or we're going to enable U1, account for the U1 max exit latency.
4727 */
4728 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
4729 enabling_u1)
4730 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
4731 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
4732 enabling_u2)
4733 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
4734
4735 if (u1_mel_us > u2_mel_us)
4736 mel_us = u1_mel_us;
4737 else
4738 mel_us = u2_mel_us;
4739 /* xHCI host controller max exit latency field is only 16 bits wide. */
4740 if (mel_us > MAX_EXIT) {
4741 dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
4742 "is too big.\n", mel_us);
4743 return -E2BIG;
4744 }
4745 return mel_us;
4746}
4747
4748/* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
Lu Baolu39693842017-04-07 17:57:04 +03004749static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharp3b3db022012-05-09 10:55:03 -07004750 struct usb_device *udev, enum usb3_link_state state)
4751{
4752 struct xhci_hcd *xhci;
4753 u16 hub_encoded_timeout;
4754 int mel;
4755 int ret;
4756
4757 xhci = hcd_to_xhci(hcd);
4758 /* The LPM timeout values are pretty host-controller specific, so don't
4759 * enable hub-initiated timeouts unless the vendor has provided
4760 * information about their timeout algorithm.
4761 */
4762 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4763 !xhci->devs[udev->slot_id])
4764 return USB3_LPM_DISABLED;
4765
4766 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
4767 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
4768 if (mel < 0) {
4769 /* Max Exit Latency is too big, disable LPM. */
4770 hub_encoded_timeout = USB3_LPM_DISABLED;
4771 mel = 0;
4772 }
4773
4774 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4775 if (ret)
4776 return ret;
4777 return hub_encoded_timeout;
4778}
4779
Lu Baolu39693842017-04-07 17:57:04 +03004780static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharp3b3db022012-05-09 10:55:03 -07004781 struct usb_device *udev, enum usb3_link_state state)
4782{
4783 struct xhci_hcd *xhci;
4784 u16 mel;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004785
4786 xhci = hcd_to_xhci(hcd);
4787 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4788 !xhci->devs[udev->slot_id])
4789 return 0;
4790
4791 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
Saurabh Karajgaonkarf1cda542015-08-04 14:04:09 +00004792 return xhci_change_max_exit_latency(xhci, udev, mel);
Sarah Sharp3b3db022012-05-09 10:55:03 -07004793}
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004794#else /* CONFIG_PM */
4795
Lu Baolu39693842017-04-07 17:57:04 +03004796static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004797 struct usb_device *udev, int enable)
4798{
4799 return 0;
4800}
4801
Lu Baolu39693842017-04-07 17:57:04 +03004802static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004803{
4804 return 0;
4805}
4806
Lu Baolu39693842017-04-07 17:57:04 +03004807static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004808 struct usb_device *udev, enum usb3_link_state state)
4809{
4810 return USB3_LPM_DISABLED;
4811}
4812
Lu Baolu39693842017-04-07 17:57:04 +03004813static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004814 struct usb_device *udev, enum usb3_link_state state)
4815{
4816 return 0;
4817}
4818#endif /* CONFIG_PM */
4819
Sarah Sharp3b3db022012-05-09 10:55:03 -07004820/*-------------------------------------------------------------------------*/
4821
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004822/* Once a hub descriptor is fetched for a device, we need to update the xHC's
4823 * internal data structures for the device.
4824 */
Lu Baolu39693842017-04-07 17:57:04 +03004825static int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004826 struct usb_tt *tt, gfp_t mem_flags)
4827{
4828 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4829 struct xhci_virt_device *vdev;
4830 struct xhci_command *config_cmd;
4831 struct xhci_input_control_ctx *ctrl_ctx;
4832 struct xhci_slot_ctx *slot_ctx;
4833 unsigned long flags;
4834 unsigned think_time;
4835 int ret;
4836
4837 /* Ignore root hubs */
4838 if (!hdev->parent)
4839 return 0;
4840
4841 vdev = xhci->devs[hdev->slot_id];
4842 if (!vdev) {
4843 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4844 return -EINVAL;
4845 }
Lu Baolu74e0b562017-04-07 17:57:05 +03004846
Mathias Nyman14d49b72017-12-08 17:59:07 +02004847 config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags);
Lu Baolu74e0b562017-04-07 17:57:05 +03004848 if (!config_cmd)
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004849 return -ENOMEM;
Lu Baolu74e0b562017-04-07 17:57:05 +03004850
Lin Wang4daf9df2015-01-09 16:06:31 +02004851 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07004852 if (!ctrl_ctx) {
4853 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4854 __func__);
4855 xhci_free_command(xhci, config_cmd);
4856 return -ENOMEM;
4857 }
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004858
4859 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp839c8172011-09-02 11:05:47 -07004860 if (hdev->speed == USB_SPEED_HIGH &&
4861 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4862 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4863 xhci_free_command(xhci, config_cmd);
4864 spin_unlock_irqrestore(&xhci->lock, flags);
4865 return -ENOMEM;
4866 }
4867
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004868 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004869 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004870 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004871 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
Chunfeng Yun096b1102015-12-04 15:53:43 +02004872 /*
4873 * refer to section 6.2.2: MTT should be 0 for full speed hub,
4874 * but it may be already set to 1 when setup an xHCI virtual
4875 * device, so clear it anyway.
4876 */
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004877 if (tt->multi)
Matt Evans28ccd292011-03-29 13:40:46 +11004878 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Chunfeng Yun096b1102015-12-04 15:53:43 +02004879 else if (hdev->speed == USB_SPEED_FULL)
4880 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
4881
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004882 if (xhci->hci_version > 0x95) {
4883 xhci_dbg(xhci, "xHCI version %x needs hub "
4884 "TT think time and number of ports\n",
4885 (unsigned int) xhci->hci_version);
Matt Evans28ccd292011-03-29 13:40:46 +11004886 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004887 /* Set TT think time - convert from ns to FS bit times.
4888 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4889 * 2 = 24 FS bit times, 3 = 32 FS bit times.
Andiry Xu700b4172011-05-05 18:14:05 +08004890 *
4891 * xHCI 1.0: this field shall be 0 if the device is not a
4892 * High-spped hub.
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004893 */
4894 think_time = tt->think_time;
4895 if (think_time != 0)
4896 think_time = (think_time / 666) - 1;
Andiry Xu700b4172011-05-05 18:14:05 +08004897 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4898 slot_ctx->tt_info |=
4899 cpu_to_le32(TT_THINK_TIME(think_time));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004900 } else {
4901 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4902 "TT think time or number of ports\n",
4903 (unsigned int) xhci->hci_version);
4904 }
4905 slot_ctx->dev_state = 0;
4906 spin_unlock_irqrestore(&xhci->lock, flags);
4907
4908 xhci_dbg(xhci, "Set up %s for hub device.\n",
4909 (xhci->hci_version > 0x95) ?
4910 "configure endpoint" : "evaluate context");
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004911
4912 /* Issue and wait for the configure endpoint or
4913 * evaluate context command.
4914 */
4915 if (xhci->hci_version > 0x95)
4916 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4917 false, false);
4918 else
4919 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4920 true, false);
4921
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004922 xhci_free_command(xhci, config_cmd);
4923 return ret;
4924}
4925
Lu Baolu39693842017-04-07 17:57:04 +03004926static int xhci_get_frame(struct usb_hcd *hcd)
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004927{
4928 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4929 /* EHCI mods by the periodic size. Why? */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004930 return readl(&xhci->run_regs->microframe_index) >> 3;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004931}
4932
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004933int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4934{
4935 struct xhci_hcd *xhci;
Arnd Bergmann4c39d4b2017-03-13 10:18:44 +08004936 /*
4937 * TODO: Check with DWC3 clients for sysdev according to
4938 * quirks
4939 */
4940 struct device *dev = hcd->self.sysdev;
Mathias Nyman0ee78c12018-03-16 16:33:06 +02004941 unsigned int minor_rev;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004942 int retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004943
Sarah Sharp1386ff72014-01-31 11:45:02 -08004944 /* Accept arbitrarily long scatter-gather lists */
4945 hcd->self.sg_tablesize = ~0;
Ming Leifc760512013-08-08 21:48:23 +08004946
Mathias Nymane2ed5112014-03-07 17:06:57 +02004947 /* support to build packet from discontinuous buffers */
4948 hcd->self.no_sg_constraint = 1;
4949
Hans de Goede19181bc2012-07-04 09:18:02 +02004950 /* XHCI controllers don't stop the ep queue on short packets :| */
4951 hcd->self.no_stop_on_short = 1;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004952
Mathias Nymanb50107b2015-10-01 18:40:38 +03004953 xhci = hcd_to_xhci(hcd);
4954
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004955 if (usb_hcd_is_primary_hcd(hcd)) {
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004956 xhci->main_hcd = hcd;
Mathias Nyman9ea95ec2018-05-21 16:39:53 +03004957 xhci->usb2_rhub.hcd = hcd;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004958 /* Mark the first roothub as being USB 2.0.
4959 * The xHCI driver will register the USB 3.0 roothub.
4960 */
4961 hcd->speed = HCD_USB2;
4962 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4963 /*
4964 * USB 2.0 roothub under xHCI has an integrated TT,
4965 * (rate matching hub) as opposed to having an OHCI/UHCI
4966 * companion controller.
4967 */
4968 hcd->has_tt = 1;
4969 } else {
Mathias Nyman0ee78c12018-03-16 16:33:06 +02004970 /*
4971 * Some 3.1 hosts return sbrn 0x30, use xhci supported protocol
4972 * minor revision instead of sbrn
4973 */
4974 minor_rev = xhci->usb3_rhub.min_rev;
4975 if (minor_rev) {
Mathias Nymanb50107b2015-10-01 18:40:38 +03004976 hcd->speed = HCD_USB31;
Mathias Nyman2c0e06f2016-01-25 15:30:45 +02004977 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS;
Mathias Nymanb50107b2015-10-01 18:40:38 +03004978 }
Mathias Nyman0ee78c12018-03-16 16:33:06 +02004979 xhci_info(xhci, "Host supports USB 3.%x %s SuperSpeed\n",
4980 minor_rev,
4981 minor_rev ? "Enhanced" : "");
4982
Mathias Nyman9ea95ec2018-05-21 16:39:53 +03004983 xhci->usb3_rhub.hcd = hcd;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004984 /* xHCI private pointer was set in xhci_pci_probe for the second
4985 * registered roothub.
4986 */
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004987 return 0;
4988 }
4989
Chris Bainbridgea00918d2015-05-19 16:30:51 +03004990 mutex_init(&xhci->mutex);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004991 xhci->cap_regs = hcd->regs;
4992 xhci->op_regs = hcd->regs +
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004993 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase));
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004994 xhci->run_regs = hcd->regs +
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004995 (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004996 /* Cache read-only capability registers */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004997 xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1);
4998 xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2);
4999 xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3);
5000 xhci->hcc_params = readl(&xhci->cap_regs->hc_capbase);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005001 xhci->hci_version = HC_VERSION(xhci->hcc_params);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02005002 xhci->hcc_params = readl(&xhci->cap_regs->hcc_params);
Lu Baolu04abb6d2015-10-01 18:40:31 +03005003 if (xhci->hci_version > 0x100)
5004 xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005005
Mathias Nyman757de492016-06-01 18:09:10 +03005006 xhci->quirks |= quirks;
Takashi Iwai4e6a1ee2013-12-09 12:42:48 +01005007
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005008 get_quirks(dev, xhci);
5009
George Cherian07f3cb72013-07-01 10:59:12 +05305010 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
5011 * success event after a short transfer. This quirk will ignore such
5012 * spurious event.
5013 */
5014 if (xhci->hci_version > 0x96)
5015 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
5016
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005017 /* Make sure the HC is halted. */
5018 retval = xhci_halt(xhci);
5019 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03005020 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005021
Marc Zyngier12de0a32018-05-23 18:41:37 +01005022 xhci_zero_64b_regs(xhci);
5023
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005024 xhci_dbg(xhci, "Resetting HCD\n");
5025 /* Reset the internal HC memory state and registers. */
5026 retval = xhci_reset(xhci);
5027 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03005028 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005029 xhci_dbg(xhci, "Reset complete\n");
5030
Yoshihiro Shimoda0a380be2016-04-08 16:25:07 +03005031 /*
5032 * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0)
5033 * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit
5034 * address memory pointers actually. So, this driver clears the AC64
5035 * bit of xhci->hcc_params to call dma_set_coherent_mask(dev,
5036 * DMA_BIT_MASK(32)) in this xhci_gen_setup().
5037 */
5038 if (xhci->quirks & XHCI_NO_64BIT_SUPPORT)
5039 xhci->hcc_params &= ~BIT(0);
5040
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03005041 /* Set dma_mask and coherent_dma_mask to 64-bits,
5042 * if xHC supports 64-bit addressing */
5043 if (HCC_64BIT_ADDR(xhci->hcc_params) &&
5044 !dma_set_mask(dev, DMA_BIT_MASK(64))) {
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005045 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03005046 dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
Duc Dangfda182d2015-10-09 13:30:13 +03005047 } else {
5048 /*
5049 * This is to avoid error in cases where a 32-bit USB
5050 * controller is used on a 64-bit capable system.
5051 */
5052 retval = dma_set_mask(dev, DMA_BIT_MASK(32));
5053 if (retval)
5054 return retval;
5055 xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n");
5056 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005057 }
5058
5059 xhci_dbg(xhci, "Calling HCD init\n");
5060 /* Initialize HCD and host controller data structures. */
5061 retval = xhci_init(hcd);
5062 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03005063 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005064 xhci_dbg(xhci, "Called HCD init\n");
Hans de Goede99705092015-01-16 17:54:01 +02005065
Marc Zyngier36b68572018-05-23 18:41:36 +01005066 xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%016llx\n",
Hans de Goede99705092015-01-16 17:54:01 +02005067 xhci->hcc_params, xhci->hci_version, xhci->quirks);
5068
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005069 return 0;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005070}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03005071EXPORT_SYMBOL_GPL(xhci_gen_setup);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07005072
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005073static const struct hc_driver xhci_hc_driver = {
5074 .description = "xhci-hcd",
5075 .product_desc = "xHCI Host Controller",
Yoshihiro Shimoda32479d42015-11-24 13:09:47 +02005076 .hcd_priv_size = sizeof(struct xhci_hcd),
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005077
5078 /*
5079 * generic hardware linkage
5080 */
5081 .irq = xhci_irq,
5082 .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
5083
5084 /*
5085 * basic lifecycle operations
5086 */
5087 .reset = NULL, /* set in xhci_init_driver() */
5088 .start = xhci_run,
5089 .stop = xhci_stop,
5090 .shutdown = xhci_shutdown,
5091
5092 /*
5093 * managing i/o requests and associated device resources
5094 */
5095 .urb_enqueue = xhci_urb_enqueue,
5096 .urb_dequeue = xhci_urb_dequeue,
5097 .alloc_dev = xhci_alloc_dev,
5098 .free_dev = xhci_free_dev,
5099 .alloc_streams = xhci_alloc_streams,
5100 .free_streams = xhci_free_streams,
5101 .add_endpoint = xhci_add_endpoint,
5102 .drop_endpoint = xhci_drop_endpoint,
5103 .endpoint_reset = xhci_endpoint_reset,
5104 .check_bandwidth = xhci_check_bandwidth,
5105 .reset_bandwidth = xhci_reset_bandwidth,
5106 .address_device = xhci_address_device,
5107 .enable_device = xhci_enable_device,
5108 .update_hub_device = xhci_update_hub_device,
5109 .reset_device = xhci_discover_or_reset_device,
5110
5111 /*
5112 * scheduling support
5113 */
5114 .get_frame_number = xhci_get_frame,
5115
5116 /*
5117 * root hub support
5118 */
5119 .hub_control = xhci_hub_control,
5120 .hub_status_data = xhci_hub_status_data,
5121 .bus_suspend = xhci_bus_suspend,
5122 .bus_resume = xhci_bus_resume,
5123
5124 /*
5125 * call back when device connected and addressed
5126 */
5127 .update_device = xhci_update_device,
5128 .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm,
5129 .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
5130 .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
5131 .find_raw_port_number = xhci_find_raw_port_number,
5132};
5133
Roger Quadroscd33a322015-05-29 17:01:46 +03005134void xhci_init_driver(struct hc_driver *drv,
5135 const struct xhci_driver_overrides *over)
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005136{
Roger Quadroscd33a322015-05-29 17:01:46 +03005137 BUG_ON(!over);
5138
5139 /* Copy the generic table to drv then apply the overrides */
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005140 *drv = xhci_hc_driver;
Roger Quadroscd33a322015-05-29 17:01:46 +03005141
5142 if (over) {
5143 drv->hcd_priv_size += over->extra_priv_size;
5144 if (over->reset)
5145 drv->reset = over->reset;
5146 if (over->start)
5147 drv->start = over->start;
5148 }
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005149}
5150EXPORT_SYMBOL_GPL(xhci_init_driver);
5151
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005152MODULE_DESCRIPTION(DRIVER_DESC);
5153MODULE_AUTHOR(DRIVER_AUTHOR);
5154MODULE_LICENSE("GPL");
5155
5156static int __init xhci_hcd_init(void)
5157{
Sarah Sharp98441972009-05-14 11:44:18 -07005158 /*
5159 * Check the compiler generated sizes of structures that must be laid
5160 * out in specific ways for hardware access.
5161 */
5162 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
5163 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
5164 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
5165 /* xhci_device_control has eight fields, and also
5166 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
5167 */
Sarah Sharp98441972009-05-14 11:44:18 -07005168 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
5169 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
5170 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
Lu Baolu04abb6d2015-10-01 18:40:31 +03005171 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 8*32/8);
Sarah Sharp98441972009-05-14 11:44:18 -07005172 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
5173 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
5174 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
Oliver Neukum1eaf35e2015-12-03 15:03:34 +01005175
5176 if (usb_disabled())
5177 return -ENODEV;
5178
Lu Baolu02b6fdc2017-10-05 11:21:39 +03005179 xhci_debugfs_create_root();
5180
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005181 return 0;
5182}
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005183
5184/*
5185 * If an init function is provided, an exit function must also be provided
5186 * to allow module unload.
5187 */
Lu Baolu02b6fdc2017-10-05 11:21:39 +03005188static void __exit xhci_hcd_fini(void)
5189{
5190 xhci_debugfs_remove_root();
5191}
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005192
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005193module_init(xhci_hcd_init);
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005194module_exit(xhci_hcd_fini);