blob: ac704d4c4bfcd4f943f610837c7ffc566ce3868d [file] [log] [blame]
Sarah Sharp66d4ead2009-04-27 19:52:28 -07001/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
Dong Nguyen43b86af2010-07-21 16:56:08 -070023#include <linux/pci.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070024#include <linux/irq.h>
Sarah Sharp8df75f42010-04-02 15:34:16 -070025#include <linux/log2.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070026#include <linux/module.h>
Sarah Sharpb0567b32009-08-07 14:04:36 -070027#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Alexis R. Cortes71c731a2012-08-03 14:00:27 -050029#include <linux/dmi.h>
James Hogan008eb952013-07-26 13:34:43 +010030#include <linux/dma-mapping.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070031
32#include "xhci.h"
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +030033#include "xhci-trace.h"
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +020034#include "xhci-mtk.h"
Sarah Sharp66d4ead2009-04-27 19:52:28 -070035
36#define DRIVER_AUTHOR "Sarah Sharp"
37#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
38
Lu Baolua1377e52014-11-18 11:27:14 +020039#define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
40
Sarah Sharpb0567b32009-08-07 14:04:36 -070041/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
42static int link_quirk;
43module_param(link_quirk, int, S_IRUGO | S_IWUSR);
44MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
45
Takashi Iwai4e6a1ee2013-12-09 12:42:48 +010046static unsigned int quirks;
47module_param(quirks, uint, S_IRUGO);
48MODULE_PARM_DESC(quirks, "Bit flags for quirks to be enabled as default");
49
Sarah Sharp66d4ead2009-04-27 19:52:28 -070050/* TODO: copied from ehci-hcd.c - can this be refactored? */
51/*
Sarah Sharp2611bd182012-10-25 13:27:51 -070052 * xhci_handshake - spin reading hc until handshake completes or fails
Sarah Sharp66d4ead2009-04-27 19:52:28 -070053 * @ptr: address of hc register to be read
54 * @mask: bits to look at in result of read
55 * @done: value of those bits when handshake succeeds
56 * @usec: timeout in microseconds
57 *
58 * Returns negative errno, or zero on success
59 *
60 * Success happens when the "mask" bits have the specified value (hardware
61 * handshake done). There are two failure modes: "usec" have passed (major
62 * hardware flakeout), or the register reads as all-ones (hardware removed).
63 */
Lin Wangdc0b1772015-01-09 16:06:28 +020064int xhci_handshake(void __iomem *ptr, u32 mask, u32 done, int usec)
Sarah Sharp66d4ead2009-04-27 19:52:28 -070065{
66 u32 result;
67
68 do {
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +020069 result = readl(ptr);
Sarah Sharp66d4ead2009-04-27 19:52:28 -070070 if (result == ~(u32)0) /* card removed */
71 return -ENODEV;
72 result &= mask;
73 if (result == done)
74 return 0;
75 udelay(1);
76 usec--;
77 } while (usec > 0);
78 return -ETIMEDOUT;
79}
80
81/*
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070082 * Disable interrupts and begin the xHCI halting process.
83 */
84void xhci_quiesce(struct xhci_hcd *xhci)
85{
86 u32 halted;
87 u32 cmd;
88 u32 mask;
89
90 mask = ~(XHCI_IRQS);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +020091 halted = readl(&xhci->op_regs->status) & STS_HALT;
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070092 if (!halted)
93 mask &= ~CMD_RUN;
94
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +020095 cmd = readl(&xhci->op_regs->command);
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070096 cmd &= mask;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +020097 writel(cmd, &xhci->op_regs->command);
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070098}
99
100/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700101 * Force HC into halt state.
102 *
103 * Disable any IRQs and clear the run/stop bit.
104 * HC will complete any current and actively pipelined transactions, and
Andiry Xubdfca502011-01-06 15:43:39 +0800105 * should halt within 16 ms of the run/stop bit being cleared.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700106 * Read HC Halted bit in the status register to see when the HC is finished.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700107 */
108int xhci_halt(struct xhci_hcd *xhci)
109{
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800110 int ret;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300111 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Halt the HC");
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -0700112 xhci_quiesce(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700113
Lin Wangdc0b1772015-01-09 16:06:28 +0200114 ret = xhci_handshake(&xhci->op_regs->status,
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700115 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
Manu Gautama7806bc2016-09-23 16:12:07 +0530116 if (!ret)
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800117 xhci->xhc_state |= XHCI_STATE_HALTED;
Manu Gautama7806bc2016-09-23 16:12:07 +0530118 else
Sarah Sharp5af98bb2012-03-16 12:58:20 -0700119 xhci_warn(xhci, "Host not halted after %u microseconds.\n",
120 XHCI_MAX_HALT_USEC);
Manu Gautama7806bc2016-09-23 16:12:07 +0530121
122 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
123
124 if (delayed_work_pending(&xhci->cmd_timer)) {
125 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
126 "Cleanup command queue");
127 cancel_delayed_work(&xhci->cmd_timer);
128 xhci_cleanup_command_queue(xhci);
129 }
130
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800131 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700132}
133
134/*
Sarah Sharped074532010-05-24 13:25:21 -0700135 * Set the run bit and wait for the host to be running.
136 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -0800137static int xhci_start(struct xhci_hcd *xhci)
Sarah Sharped074532010-05-24 13:25:21 -0700138{
139 u32 temp;
140 int ret;
Hemant Kumar8e2be712017-03-17 14:02:00 -0700141 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sarah Sharped074532010-05-24 13:25:21 -0700142
Hemant Kumar8e2be712017-03-17 14:02:00 -0700143 /*
144 * disable irq to avoid xhci_irq flooding due to unhandeled port
145 * change event in halt state, as soon as xhci_start clears halt bit
146 */
147 disable_irq(hcd->irq);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200148 temp = readl(&xhci->op_regs->command);
Sarah Sharped074532010-05-24 13:25:21 -0700149 temp |= (CMD_RUN);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300150 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.",
Sarah Sharped074532010-05-24 13:25:21 -0700151 temp);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200152 writel(temp, &xhci->op_regs->command);
Sarah Sharped074532010-05-24 13:25:21 -0700153
154 /*
155 * Wait for the HCHalted Status bit to be 0 to indicate the host is
156 * running.
157 */
Lin Wangdc0b1772015-01-09 16:06:28 +0200158 ret = xhci_handshake(&xhci->op_regs->status,
Sarah Sharped074532010-05-24 13:25:21 -0700159 STS_HALT, 0, XHCI_MAX_HALT_USEC);
160 if (ret == -ETIMEDOUT)
161 xhci_err(xhci, "Host took too long to start, "
162 "waited %u microseconds.\n",
163 XHCI_MAX_HALT_USEC);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800164 if (!ret)
Mathias Nyman98d74f92016-04-08 16:25:10 +0300165 /* clear state flags. Including dying, halted or removing */
166 xhci->xhc_state = 0;
Roger Quadrose5bfeab2015-09-21 17:46:13 +0300167
Hemant Kumar8e2be712017-03-17 14:02:00 -0700168 enable_irq(hcd->irq);
169
Sarah Sharped074532010-05-24 13:25:21 -0700170 return ret;
171}
172
173/*
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800174 * Reset a halted HC.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700175 *
176 * This resets pipelines, timers, counters, state machines, etc.
177 * Transactions will be terminated immediately, and operational registers
178 * will be set to their defaults.
179 */
180int xhci_reset(struct xhci_hcd *xhci)
181{
182 u32 command;
183 u32 state;
Andiry Xuf370b992012-04-14 02:54:30 +0800184 int ret, i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700185
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200186 state = readl(&xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700187 if ((state & STS_HALT) == 0) {
188 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
189 return 0;
190 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700191
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300192 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200193 command = readl(&xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700194 command |= CMD_RESET;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200195 writel(command, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700196
Rajmohan Mania5964392015-11-18 10:48:20 +0200197 /* Existing Intel xHCI controllers require a delay of 1 mS,
198 * after setting the CMD_RESET bit, and before accessing any
199 * HC registers. This allows the HC to complete the
200 * reset operation and be ready for HC register access.
201 * Without this delay, the subsequent HC register access,
202 * may result in a system hang very rarely.
203 */
204 if (xhci->quirks & XHCI_INTEL_HOST)
205 udelay(1000);
206
Lin Wangdc0b1772015-01-09 16:06:28 +0200207 ret = xhci_handshake(&xhci->op_regs->command,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700208 CMD_RESET, 0, 10 * 1000 * 1000);
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700209 if (ret)
210 return ret;
211
Jiahau Chang24a950e2017-07-20 14:48:27 +0300212 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
213 usb_asmedia_modifyflowcontrol(to_pci_dev(xhci_to_hcd(xhci)->self.controller));
214
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300215 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
216 "Wait for controller to be ready for doorbell rings");
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700217 /*
218 * xHCI cannot write to any doorbells or operational registers other
219 * than status until the "Controller Not Ready" flag is cleared.
220 */
Lin Wangdc0b1772015-01-09 16:06:28 +0200221 ret = xhci_handshake(&xhci->op_regs->status,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700222 STS_CNR, 0, 10 * 1000 * 1000);
Andiry Xuf370b992012-04-14 02:54:30 +0800223
224 for (i = 0; i < 2; ++i) {
225 xhci->bus_state[i].port_c_suspend = 0;
226 xhci->bus_state[i].suspended_ports = 0;
227 xhci->bus_state[i].resuming_ports = 0;
228 }
229
230 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700231}
232
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700233#ifdef CONFIG_PCI
234static int xhci_free_msi(struct xhci_hcd *xhci)
Dong Nguyen43b86af2010-07-21 16:56:08 -0700235{
236 int i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700237
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700238 if (!xhci->msix_entries)
239 return -EINVAL;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700240
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700241 for (i = 0; i < xhci->msix_count; i++)
242 if (xhci->msix_entries[i].vector)
243 free_irq(xhci->msix_entries[i].vector,
244 xhci_to_hcd(xhci));
245 return 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700246}
247
248/*
249 * Set up MSI
250 */
251static int xhci_setup_msi(struct xhci_hcd *xhci)
252{
253 int ret;
Arnd Bergmanna9ff9112017-03-13 10:18:44 +0800254 /*
255 * TODO:Check with MSI Soc for sysdev
256 */
Dong Nguyen43b86af2010-07-21 16:56:08 -0700257 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
258
259 ret = pci_enable_msi(pdev);
260 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300261 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
262 "failed to allocate MSI entry");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700263 return ret;
264 }
265
Alex Shi851ec162013-05-24 10:54:19 +0800266 ret = request_irq(pdev->irq, xhci_msi_irq,
Dong Nguyen43b86af2010-07-21 16:56:08 -0700267 0, "xhci_hcd", xhci_to_hcd(xhci));
268 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300269 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
270 "disable MSI interrupt");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700271 pci_disable_msi(pdev);
272 }
273
274 return ret;
275}
276
277/*
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700278 * Free IRQs
279 * free all IRQs request
280 */
281static void xhci_free_irq(struct xhci_hcd *xhci)
282{
Arnd Bergmanna9ff9112017-03-13 10:18:44 +0800283 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.sysdev);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700284 int ret;
285
286 /* return if using legacy interrupt */
Felipe Balbicd704692012-02-29 16:46:23 +0200287 if (xhci_to_hcd(xhci)->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700288 return;
289
290 ret = xhci_free_msi(xhci);
291 if (!ret)
292 return;
Felipe Balbicd704692012-02-29 16:46:23 +0200293 if (pdev->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700294 free_irq(pdev->irq, xhci_to_hcd(xhci));
295
296 return;
297}
298
299/*
Dong Nguyen43b86af2010-07-21 16:56:08 -0700300 * Set up MSI-X
301 */
302static int xhci_setup_msix(struct xhci_hcd *xhci)
303{
304 int i, ret = 0;
Andiry Xu00292272010-12-27 17:39:02 +0800305 struct usb_hcd *hcd = xhci_to_hcd(xhci);
306 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700307
308 /*
309 * calculate number of msi-x vectors supported.
310 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
311 * with max number of interrupters based on the xhci HCSPARAMS1.
312 * - num_online_cpus: maximum msi-x vectors per CPUs core.
313 * Add additional 1 vector to ensure always available interrupt.
314 */
315 xhci->msix_count = min(num_online_cpus() + 1,
316 HCS_MAX_INTRS(xhci->hcs_params1));
317
318 xhci->msix_entries =
319 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
Greg Kroah-Hartman86871972010-11-11 09:41:02 -0800320 GFP_KERNEL);
Wolfram Sangf4c46f12016-08-25 19:39:10 +0200321 if (!xhci->msix_entries)
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700322 return -ENOMEM;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700323
324 for (i = 0; i < xhci->msix_count; i++) {
325 xhci->msix_entries[i].entry = i;
326 xhci->msix_entries[i].vector = 0;
327 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700328
Alexander Gordeeva62445a2014-05-08 19:25:58 +0300329 ret = pci_enable_msix_exact(pdev, xhci->msix_entries, xhci->msix_count);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700330 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300331 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
332 "Failed to enable MSI-X");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700333 goto free_entries;
334 }
335
Dong Nguyen43b86af2010-07-21 16:56:08 -0700336 for (i = 0; i < xhci->msix_count; i++) {
337 ret = request_irq(xhci->msix_entries[i].vector,
Alex Shi851ec162013-05-24 10:54:19 +0800338 xhci_msi_irq,
Dong Nguyen43b86af2010-07-21 16:56:08 -0700339 0, "xhci_hcd", xhci_to_hcd(xhci));
340 if (ret)
341 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700342 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700343
Andiry Xu00292272010-12-27 17:39:02 +0800344 hcd->msix_enabled = 1;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700345 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700346
347disable_msix:
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300348 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700349 xhci_free_irq(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700350 pci_disable_msix(pdev);
351free_entries:
352 kfree(xhci->msix_entries);
353 xhci->msix_entries = NULL;
354 return ret;
355}
356
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700357/* Free any IRQs and disable MSI-X */
358static void xhci_cleanup_msix(struct xhci_hcd *xhci)
359{
Andiry Xu00292272010-12-27 17:39:02 +0800360 struct usb_hcd *hcd = xhci_to_hcd(xhci);
361 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700362
Jack Pham90053552013-11-15 14:53:14 -0800363 if (xhci->quirks & XHCI_PLAT)
364 return;
365
Dong Nguyen43b86af2010-07-21 16:56:08 -0700366 xhci_free_irq(xhci);
367
368 if (xhci->msix_entries) {
369 pci_disable_msix(pdev);
370 kfree(xhci->msix_entries);
371 xhci->msix_entries = NULL;
372 } else {
373 pci_disable_msi(pdev);
374 }
375
Andiry Xu00292272010-12-27 17:39:02 +0800376 hcd->msix_enabled = 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700377 return;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700378}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700379
Olof Johanssond5c82fe2013-07-23 11:58:20 -0700380static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700381{
382 int i;
383
384 if (xhci->msix_entries) {
385 for (i = 0; i < xhci->msix_count; i++)
386 synchronize_irq(xhci->msix_entries[i].vector);
387 }
388}
389
390static int xhci_try_enable_msi(struct usb_hcd *hcd)
391{
392 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp52fb6122013-08-08 10:08:34 -0700393 struct pci_dev *pdev;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700394 int ret;
395
Sarah Sharp52fb6122013-08-08 10:08:34 -0700396 /* The xhci platform device has set up IRQs through usb_add_hcd. */
397 if (xhci->quirks & XHCI_PLAT)
398 return 0;
399
400 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700401 /*
402 * Some Fresco Logic host controllers advertise MSI, but fail to
403 * generate interrupts. Don't even try to enable MSI.
404 */
405 if (xhci->quirks & XHCI_BROKEN_MSI)
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100406 goto legacy_irq;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700407
408 /* unregister the legacy interrupt */
409 if (hcd->irq)
410 free_irq(hcd->irq, hcd);
Felipe Balbicd704692012-02-29 16:46:23 +0200411 hcd->irq = 0;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700412
413 ret = xhci_setup_msix(xhci);
414 if (ret)
415 /* fall back to msi*/
416 ret = xhci_setup_msi(xhci);
417
418 if (!ret)
Felipe Balbicd704692012-02-29 16:46:23 +0200419 /* hcd->irq is 0, we have MSI */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700420 return 0;
421
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
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500461static void compliance_mode_recovery(unsigned long arg)
462{
463 struct xhci_hcd *xhci;
464 struct usb_hcd *hcd;
465 u32 temp;
466 int i;
467
468 xhci = (struct xhci_hcd *)arg;
469
470 for (i = 0; i < xhci->num_usb3_ports; i++) {
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200471 temp = readl(xhci->usb3_ports[i]);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500472 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
473 /*
474 * Compliance Mode Detected. Letting USB Core
475 * handle the Warm Reset
476 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300477 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
478 "Compliance mode detected->port %d",
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500479 i + 1);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300480 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
481 "Attempting compliance mode recovery");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500482 hcd = xhci->shared_hcd;
483
484 if (hcd->state == HC_STATE_SUSPENDED)
485 usb_hcd_resume_root_hub(hcd);
486
487 usb_hcd_poll_rh_status(hcd);
488 }
489 }
490
491 if (xhci->port_status_u0 != ((1 << xhci->num_usb3_ports)-1))
492 mod_timer(&xhci->comp_mode_recovery_timer,
493 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
494}
495
496/*
497 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
498 * that causes ports behind that hardware to enter compliance mode sometimes.
499 * The quirk creates a timer that polls every 2 seconds the link state of
500 * each host controller's port and recovers it by issuing a Warm reset
501 * if Compliance mode is detected, otherwise the port will become "dead" (no
502 * device connections or disconnections will be detected anymore). Becasue no
503 * status event is generated when entering compliance mode (per xhci spec),
504 * this quirk is needed on systems that have the failing hardware installed.
505 */
506static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
507{
508 xhci->port_status_u0 = 0;
Julia Lawallfc8abe02015-01-09 16:06:29 +0200509 setup_timer(&xhci->comp_mode_recovery_timer,
510 compliance_mode_recovery, (unsigned long)xhci);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500511 xhci->comp_mode_recovery_timer.expires = jiffies +
512 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
513
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500514 add_timer(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300515 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
516 "Compliance mode recovery timer initialized");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500517}
518
519/*
520 * This function identifies the systems that have installed the SN65LVPE502CP
521 * USB3.0 re-driver and that need the Compliance Mode Quirk.
522 * Systems:
523 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
524 */
Andrew Brestickere1cd9722014-10-03 11:35:27 +0300525static bool xhci_compliance_mode_recovery_timer_quirk_check(void)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500526{
527 const char *dmi_product_name, *dmi_sys_vendor;
528
529 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
530 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
Vivek Gautam457a73d2012-09-22 18:11:19 +0530531 if (!dmi_product_name || !dmi_sys_vendor)
532 return false;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500533
534 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
535 return false;
536
537 if (strstr(dmi_product_name, "Z420") ||
538 strstr(dmi_product_name, "Z620") ||
Alexis R. Cortes47080972012-10-17 14:09:12 -0500539 strstr(dmi_product_name, "Z820") ||
Alexis R. Cortesb0e4e602012-11-08 16:59:27 -0600540 strstr(dmi_product_name, "Z1 Workstation"))
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500541 return true;
542
543 return false;
544}
545
546static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
547{
548 return (xhci->port_status_u0 == ((1 << xhci->num_usb3_ports)-1));
549}
550
551
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700552/*
553 * Initialize memory for HCD and xHC (one-time init).
554 *
555 * Program the PAGESIZE register, initialize the device context array, create
556 * device contexts (?), set up a command ring segment (or two?), create event
557 * ring (one for now).
558 */
559int xhci_init(struct usb_hcd *hcd)
560{
561 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
562 int retval = 0;
563
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300564 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700565 spin_lock_init(&xhci->lock);
Sebastian Andrzej Siewiord7826592011-09-13 16:41:10 -0700566 if (xhci->hci_version == 0x95 && link_quirk) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300567 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
568 "QUIRK: Not clearing Link TRB chain bits.");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700569 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
570 } else {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300571 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
572 "xHCI doesn't need link TRB QUIRK");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700573 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700574 retval = xhci_mem_init(xhci, GFP_KERNEL);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300575 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700576
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500577 /* Initializing Compliance Mode Recovery Data If Needed */
Sarah Sharpc3897aa2013-04-18 10:02:03 -0700578 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500579 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
580 compliance_mode_recovery_timer_init(xhci);
581 }
582
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700583 return retval;
584}
585
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700586/*-------------------------------------------------------------------------*/
587
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700588
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800589static int xhci_run_finished(struct xhci_hcd *xhci)
590{
591 if (xhci_start(xhci)) {
592 xhci_halt(xhci);
593 return -ENODEV;
594 }
595 xhci->shared_hcd->state = HC_STATE_RUNNING;
Elric Fuc181bc52012-06-27 16:30:57 +0800596 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800597
598 if (xhci->quirks & XHCI_NEC_HOST)
599 xhci_ring_cmd_db(xhci);
600
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300601 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
602 "Finished xhci_run for USB3 roothub");
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800603 return 0;
604}
605
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700606/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700607 * Start the HC after it was halted.
608 *
609 * This function is called by the USB core when the HC driver is added.
610 * Its opposite is xhci_stop().
611 *
612 * xhci_init() must be called once before this function can be called.
613 * Reset the HC, enable device slot contexts, program DCBAAP, and
614 * set command ring pointer and event ring pointer.
615 *
616 * Setup MSI-X vectors and enable interrupts.
617 */
618int xhci_run(struct usb_hcd *hcd)
619{
620 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700621 u64 temp_64;
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700622 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700623 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700624
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800625 /* Start the xHCI host controller running only after the USB 2.0 roothub
626 * is setup.
627 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700628
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700629 hcd->uses_new_polling = 1;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800630 if (!usb_hcd_is_primary_hcd(hcd))
631 return xhci_run_finished(xhci);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700632
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300633 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700634
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700635 ret = xhci_try_enable_msi(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700636 if (ret)
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700637 return ret;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700638
Sarah Sharp66e49d82009-07-27 12:03:46 -0700639 xhci_dbg(xhci, "Command ring memory map follows:\n");
640 xhci_debug_ring(xhci, xhci->cmd_ring);
641 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
642 xhci_dbg_cmd_ptrs(xhci);
643
644 xhci_dbg(xhci, "ERST memory map follows:\n");
645 xhci_dbg_erst(xhci, &xhci->erst);
646 xhci_dbg(xhci, "Event ring:\n");
647 xhci_debug_ring(xhci, xhci->event_ring);
648 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800649 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700650 temp_64 &= ~ERST_PTR_MASK;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300651 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
652 "ERST deq = 64'h%0lx", (long unsigned int) temp_64);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700653
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300654 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
655 "// Set the interrupt modulation register");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200656 temp = readl(&xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700657 temp &= ~ER_IRQ_INTERVAL_MASK;
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +0200658 /*
659 * the increment interval is 8 times as much as that defined
660 * in xHCI spec on MTK's controller
661 */
662 temp |= (u32) ((xhci->quirks & XHCI_MTK_HOST) ? 20 : 160);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200663 writel(temp, &xhci->ir_set->irq_control);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700664
665 /* Set the HCD state before we enable the irqs */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200666 temp = readl(&xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700667 temp |= (CMD_EIE);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300668 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
669 "// Enable interrupts, cmd = 0x%x.", temp);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200670 writel(temp, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700671
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200672 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300673 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
674 "// Enabling event ring interrupter %p by writing 0x%x to irq_pending",
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700675 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200676 writel(ER_IRQ_ENABLE(temp), &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800677 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700678
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300679 if (xhci->quirks & XHCI_NEC_HOST) {
680 struct xhci_command *command;
681 command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
682 if (!command)
683 return -ENOMEM;
684 xhci_queue_vendor_command(xhci, command, 0, 0, 0,
Sarah Sharp02386342010-05-24 13:25:28 -0700685 TRB_TYPE(TRB_NEC_GET_FW));
Mathias Nymanddba5cd2014-05-08 19:26:00 +0300686 }
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300687 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
688 "Finished xhci_run for USB2 roothub");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700689 return 0;
690}
Andrew Bresticker436e8c72014-10-03 11:35:28 +0300691EXPORT_SYMBOL_GPL(xhci_run);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700692
693/*
694 * Stop xHCI driver.
695 *
696 * This function is called by the USB core when the HC driver is removed.
697 * Its opposite is xhci_run().
698 *
699 * Disable device contexts, disable IRQs, and quiesce the HC.
700 * Reset the HC, finish any completed transactions, and cleanup memory.
701 */
702void xhci_stop(struct usb_hcd *hcd)
703{
704 u32 temp;
705 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
706
Roger Quadros8c24d6d2015-09-21 17:46:14 +0300707 mutex_lock(&xhci->mutex);
Roger Quadros8c24d6d2015-09-21 17:46:14 +0300708
Gabriel Krisman Bertazi27a41a82016-06-01 18:09:07 +0300709 if (!(xhci->xhc_state & XHCI_STATE_HALTED)) {
710 spin_lock_irq(&xhci->lock);
711
712 xhci->xhc_state |= XHCI_STATE_HALTED;
713 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
714 xhci_halt(xhci);
715 xhci_reset(xhci);
716
717 spin_unlock_irq(&xhci->lock);
718 }
719
720 if (!usb_hcd_is_primary_hcd(hcd)) {
721 mutex_unlock(&xhci->mutex);
722 return;
723 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700724
Zhang Rui40a9fb12010-12-17 13:17:04 -0800725 xhci_cleanup_msix(xhci);
726
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500727 /* Deleting Compliance Mode Recovery Timer */
728 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
Tony Camuso58b1d792013-04-05 14:27:07 -0400729 (!(xhci_all_ports_seen_u0(xhci)))) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500730 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300731 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
732 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -0400733 __func__);
734 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500735
Andiry Xuc41136b2011-03-22 17:08:14 +0800736 if (xhci->quirks & XHCI_AMD_PLL_FIX)
737 usb_amd_dev_put();
738
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300739 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
740 "// Disabling event ring interrupts");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200741 temp = readl(&xhci->op_regs->status);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200742 writel(temp & ~STS_EINT, &xhci->op_regs->status);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200743 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200744 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800745 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700746
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300747 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700748 xhci_mem_cleanup(xhci);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300749 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
750 "xhci_stop completed - status = %x",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200751 readl(&xhci->op_regs->status));
Roger Quadros85ac90f2015-09-21 17:46:12 +0300752 mutex_unlock(&xhci->mutex);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700753}
754
755/*
756 * Shutdown HC (not bus-specific)
757 *
758 * This is called when the machine is rebooting or halting. We assume that the
759 * machine will be powered off, and the HC's internal state will be reset.
760 * Don't bother to free memory.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800761 *
762 * This will only ever be called with the main usb_hcd (the USB3 roothub).
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700763 */
764void xhci_shutdown(struct usb_hcd *hcd)
765{
766 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
767
Dan Carpenter052c7f92012-08-13 19:57:03 +0300768 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
Arnd Bergmanna9ff9112017-03-13 10:18:44 +0800769 usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev));
Sarah Sharpe95829f2012-07-23 18:59:30 +0300770
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700771 spin_lock_irq(&xhci->lock);
Lei wang00e47512018-02-02 11:22:38 +0800772 if (!HCD_HW_ACCESSIBLE(hcd)) {
773 spin_unlock_irq(&xhci->lock);
774 return;
775 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700776 xhci_halt(xhci);
Takashi Iwai638298d2013-09-12 08:11:06 +0200777 /* Workaround for spurious wakeups at shutdown with HSW */
778 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
779 xhci_reset(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700780 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700781
Zhang Rui40a9fb12010-12-17 13:17:04 -0800782 xhci_cleanup_msix(xhci);
783
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300784 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
785 "xhci_shutdown completed - status = %x",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200786 readl(&xhci->op_regs->status));
Takashi Iwai638298d2013-09-12 08:11:06 +0200787
788 /* Yet another workaround for spurious wakeups at shutdown with HSW */
789 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
Arnd Bergmanna9ff9112017-03-13 10:18:44 +0800790 pci_set_power_state(to_pci_dev(hcd->self.sysdev), PCI_D3hot);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700791}
792
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700793#ifdef CONFIG_PM
Andiry Xu5535b1d2010-10-14 07:23:06 -0700794static void xhci_save_registers(struct xhci_hcd *xhci)
795{
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200796 xhci->s3.command = readl(&xhci->op_regs->command);
797 xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800798 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200799 xhci->s3.config_reg = readl(&xhci->op_regs->config_reg);
800 xhci->s3.erst_size = readl(&xhci->ir_set->erst_size);
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800801 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
802 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200803 xhci->s3.irq_pending = readl(&xhci->ir_set->irq_pending);
804 xhci->s3.irq_control = readl(&xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700805}
806
807static void xhci_restore_registers(struct xhci_hcd *xhci)
808{
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200809 writel(xhci->s3.command, &xhci->op_regs->command);
810 writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
Sarah Sharp477632d2014-01-29 14:02:00 -0800811 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200812 writel(xhci->s3.config_reg, &xhci->op_regs->config_reg);
813 writel(xhci->s3.erst_size, &xhci->ir_set->erst_size);
Sarah Sharp477632d2014-01-29 14:02:00 -0800814 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
815 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200816 writel(xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
817 writel(xhci->s3.irq_control, &xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700818}
819
Sarah Sharp89821322010-11-12 11:59:31 -0800820static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
821{
822 u64 val_64;
823
824 /* step 2: initialize command ring buffer */
Sarah Sharpf7b2e402014-01-30 13:27:49 -0800825 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
Sarah Sharp89821322010-11-12 11:59:31 -0800826 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
827 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
828 xhci->cmd_ring->dequeue) &
829 (u64) ~CMD_RING_RSVD_BITS) |
830 xhci->cmd_ring->cycle_state;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300831 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
832 "// Setting command ring address to 0x%llx",
Sarah Sharp89821322010-11-12 11:59:31 -0800833 (long unsigned long) val_64);
Sarah Sharp477632d2014-01-29 14:02:00 -0800834 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
Sarah Sharp89821322010-11-12 11:59:31 -0800835}
836
837/*
838 * The whole command ring must be cleared to zero when we suspend the host.
839 *
840 * The host doesn't save the command ring pointer in the suspend well, so we
841 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
842 * aligned, because of the reserved bits in the command ring dequeue pointer
843 * register. Therefore, we can't just set the dequeue pointer back in the
844 * middle of the ring (TRBs are 16-byte aligned).
845 */
846static void xhci_clear_command_ring(struct xhci_hcd *xhci)
847{
848 struct xhci_ring *ring;
849 struct xhci_segment *seg;
850
851 ring = xhci->cmd_ring;
852 seg = ring->deq_seg;
853 do {
Andiry Xu158886c2011-11-30 16:37:41 +0800854 memset(seg->trbs, 0,
855 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
856 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
857 cpu_to_le32(~TRB_CYCLE);
Sarah Sharp89821322010-11-12 11:59:31 -0800858 seg = seg->next;
859 } while (seg != ring->deq_seg);
860
861 /* Reset the software enqueue and dequeue pointers */
862 ring->deq_seg = ring->first_seg;
863 ring->dequeue = ring->first_seg->trbs;
864 ring->enq_seg = ring->deq_seg;
865 ring->enqueue = ring->dequeue;
866
Andiry Xub008df62012-03-05 17:49:34 +0800867 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
Sarah Sharp89821322010-11-12 11:59:31 -0800868 /*
869 * Ring is now zeroed, so the HW should look for change of ownership
870 * when the cycle bit is set to 1.
871 */
872 ring->cycle_state = 1;
873
874 /*
875 * Reset the hardware dequeue pointer.
876 * Yes, this will need to be re-written after resume, but we're paranoid
877 * and want to make sure the hardware doesn't access bogus memory
878 * because, say, the BIOS or an SMI started the host without changing
879 * the command ring pointers.
880 */
881 xhci_set_cmd_ring_deq(xhci);
882}
883
Lu Baolua1377e52014-11-18 11:27:14 +0200884static void xhci_disable_port_wake_on_bits(struct xhci_hcd *xhci)
885{
886 int port_index;
887 __le32 __iomem **port_array;
888 unsigned long flags;
889 u32 t1, t2;
890
891 spin_lock_irqsave(&xhci->lock, flags);
892
893 /* disble usb3 ports Wake bits*/
894 port_index = xhci->num_usb3_ports;
895 port_array = xhci->usb3_ports;
896 while (port_index--) {
897 t1 = readl(port_array[port_index]);
898 t1 = xhci_port_state_to_neutral(t1);
899 t2 = t1 & ~PORT_WAKE_BITS;
900 if (t1 != t2)
901 writel(t2, port_array[port_index]);
902 }
903
904 /* disble usb2 ports Wake bits*/
905 port_index = xhci->num_usb2_ports;
906 port_array = xhci->usb2_ports;
907 while (port_index--) {
908 t1 = readl(port_array[port_index]);
909 t1 = xhci_port_state_to_neutral(t1);
910 t2 = t1 & ~PORT_WAKE_BITS;
911 if (t1 != t2)
912 writel(t2, port_array[port_index]);
913 }
914
915 spin_unlock_irqrestore(&xhci->lock, flags);
916}
917
Andiry Xu5535b1d2010-10-14 07:23:06 -0700918/*
919 * Stop HC (not bus-specific)
920 *
921 * This is called when the machine transition into S3/S4 mode.
922 *
923 */
Lu Baolua1377e52014-11-18 11:27:14 +0200924int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup)
Andiry Xu5535b1d2010-10-14 07:23:06 -0700925{
926 int rc = 0;
Oliver Neukum455f5892013-09-30 15:50:54 +0200927 unsigned int delay = XHCI_MAX_HALT_USEC;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700928 struct usb_hcd *hcd = xhci_to_hcd(xhci);
929 u32 command;
930
Roger Quadros9fa733f2015-05-29 17:01:50 +0300931 if (!hcd->state)
932 return 0;
933
Felipe Balbi77b84762012-10-19 10:55:16 +0300934 if (hcd->state != HC_STATE_SUSPENDED ||
935 xhci->shared_hcd->state != HC_STATE_SUSPENDED)
936 return -EINVAL;
937
Lu Baolua1377e52014-11-18 11:27:14 +0200938 /* Clear root port wake on bits if wakeup not allowed. */
939 if (!do_wakeup)
940 xhci_disable_port_wake_on_bits(xhci);
941
Sarah Sharpc52804a2012-11-27 12:30:23 -0800942 /* Don't poll the roothubs on bus suspend. */
943 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
944 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
945 del_timer_sync(&hcd->rh_timer);
Al Cooper14e61a12014-08-20 16:41:57 +0300946 clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
947 del_timer_sync(&xhci->shared_hcd->rh_timer);
Sarah Sharpc52804a2012-11-27 12:30:23 -0800948
Andiry Xu5535b1d2010-10-14 07:23:06 -0700949 spin_lock_irq(&xhci->lock);
950 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sarah Sharpb3209372011-03-07 11:24:07 -0800951 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700952 /* step 1: stop endpoint */
953 /* skipped assuming that port suspend has done */
954
955 /* step 2: clear Run/Stop bit */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200956 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700957 command &= ~CMD_RUN;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200958 writel(command, &xhci->op_regs->command);
Oliver Neukum455f5892013-09-30 15:50:54 +0200959
960 /* Some chips from Fresco Logic need an extraordinary delay */
961 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
962
Lin Wangdc0b1772015-01-09 16:06:28 +0200963 if (xhci_handshake(&xhci->op_regs->status,
Oliver Neukum455f5892013-09-30 15:50:54 +0200964 STS_HALT, STS_HALT, delay)) {
Andiry Xu5535b1d2010-10-14 07:23:06 -0700965 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
966 spin_unlock_irq(&xhci->lock);
967 return -ETIMEDOUT;
968 }
Sarah Sharp89821322010-11-12 11:59:31 -0800969 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700970
971 /* step 3: save registers */
972 xhci_save_registers(xhci);
973
974 /* step 4: set CSS flag */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +0200975 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700976 command |= CMD_CSS;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +0200977 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +0200978 if (xhci_handshake(&xhci->op_regs->status,
Sarah Sharp2611bd182012-10-25 13:27:51 -0700979 STS_SAVE, 0, 10 * 1000)) {
Andiry Xu622eb782012-06-13 10:51:57 +0800980 xhci_warn(xhci, "WARN: xHC save state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -0700981 spin_unlock_irq(&xhci->lock);
982 return -ETIMEDOUT;
983 }
Andiry Xu5535b1d2010-10-14 07:23:06 -0700984 spin_unlock_irq(&xhci->lock);
985
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500986 /*
987 * Deleting Compliance Mode Recovery Timer because the xHCI Host
988 * is about to be suspended.
989 */
990 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
991 (!(xhci_all_ports_seen_u0(xhci)))) {
992 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300993 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
994 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -0400995 __func__);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500996 }
997
Andiry Xu00292272010-12-27 17:39:02 +0800998 /* step 5: remove core well power */
999 /* synchronize irq when using MSI-X */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -07001000 xhci_msix_sync_irqs(xhci);
Andiry Xu00292272010-12-27 17:39:02 +08001001
Andiry Xu5535b1d2010-10-14 07:23:06 -07001002 return rc;
1003}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03001004EXPORT_SYMBOL_GPL(xhci_suspend);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001005
1006/*
1007 * start xHC (not bus-specific)
1008 *
1009 * This is called when the machine transition from S3/S4 mode.
1010 *
1011 */
1012int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
1013{
Wang, Yud6236f62014-06-24 17:14:44 +03001014 u32 command, temp = 0, status;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001015 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sarah Sharp65b22f92010-12-17 12:35:05 -08001016 struct usb_hcd *secondary_hcd;
Alan Sternf69e3122011-11-03 11:37:10 -04001017 int retval = 0;
Tony Camuso77df9e02013-02-21 16:11:27 -05001018 bool comp_timer_running = false;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001019
Roger Quadros9fa733f2015-05-29 17:01:50 +03001020 if (!hcd->state)
1021 return 0;
1022
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001023 /* Wait a bit if either of the roothubs need to settle from the
Lucas De Marchi25985ed2011-03-30 22:57:33 -03001024 * transition into bus suspend.
Sarah Sharp20b67cf2010-12-15 12:47:14 -08001025 */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -08001026 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
1027 time_before(jiffies,
1028 xhci->bus_state[1].next_statechange))
Andiry Xu5535b1d2010-10-14 07:23:06 -07001029 msleep(100);
1030
Alan Sternf69e3122011-11-03 11:37:10 -04001031 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1032 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
1033
Andiry Xu5535b1d2010-10-14 07:23:06 -07001034 spin_lock_irq(&xhci->lock);
Maarten Lankhorstc877b3b2011-06-15 23:47:21 +02001035 if (xhci->quirks & XHCI_RESET_ON_RESUME)
1036 hibernated = true;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001037
1038 if (!hibernated) {
1039 /* step 1: restore register */
1040 xhci_restore_registers(xhci);
1041 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -08001042 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001043 /* step 3: restore state and start state*/
1044 /* step 3: set CRS flag */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001045 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001046 command |= CMD_CRS;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001047 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +02001048 if (xhci_handshake(&xhci->op_regs->status,
Andiry Xu622eb782012-06-13 10:51:57 +08001049 STS_RESTORE, 0, 10 * 1000)) {
1050 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -07001051 spin_unlock_irq(&xhci->lock);
1052 return -ETIMEDOUT;
1053 }
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001054 temp = readl(&xhci->op_regs->status);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001055 }
1056
1057 /* If restore operation fails, re-initialize the HC during resume */
1058 if ((temp & STS_SRE) || hibernated) {
Tony Camuso77df9e02013-02-21 16:11:27 -05001059
1060 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1061 !(xhci_all_ports_seen_u0(xhci))) {
1062 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001063 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1064 "Compliance Mode Recovery Timer deleted!");
Tony Camuso77df9e02013-02-21 16:11:27 -05001065 }
1066
Sarah Sharpfedd3832011-04-12 17:43:19 -07001067 /* Let the USB core know _both_ roothubs lost power. */
1068 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
1069 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001070
1071 xhci_dbg(xhci, "Stop HCD\n");
1072 xhci_halt(xhci);
1073 xhci_reset(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001074 spin_unlock_irq(&xhci->lock);
Andiry Xu00292272010-12-27 17:39:02 +08001075 xhci_cleanup_msix(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001076
Andiry Xu5535b1d2010-10-14 07:23:06 -07001077 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001078 temp = readl(&xhci->op_regs->status);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001079 writel(temp & ~STS_EINT, &xhci->op_regs->status);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001080 temp = readl(&xhci->ir_set->irq_pending);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001081 writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -08001082 xhci_print_ir_set(xhci, 0);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001083
1084 xhci_dbg(xhci, "cleaning up memory\n");
1085 xhci_mem_cleanup(xhci);
1086 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001087 readl(&xhci->op_regs->status));
Andiry Xu5535b1d2010-10-14 07:23:06 -07001088
Sarah Sharp65b22f92010-12-17 12:35:05 -08001089 /* USB core calls the PCI reinit and start functions twice:
1090 * first with the primary HCD, and then with the secondary HCD.
1091 * If we don't do the same, the host will never be started.
1092 */
1093 if (!usb_hcd_is_primary_hcd(hcd))
1094 secondary_hcd = hcd;
1095 else
1096 secondary_hcd = xhci->shared_hcd;
1097
1098 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1099 retval = xhci_init(hcd->primary_hcd);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001100 if (retval)
1101 return retval;
Tony Camuso77df9e02013-02-21 16:11:27 -05001102 comp_timer_running = true;
1103
Sarah Sharp65b22f92010-12-17 12:35:05 -08001104 xhci_dbg(xhci, "Start the primary HCD\n");
1105 retval = xhci_run(hcd->primary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001106 if (!retval) {
Alan Sternf69e3122011-11-03 11:37:10 -04001107 xhci_dbg(xhci, "Start the secondary HCD\n");
1108 retval = xhci_run(secondary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001109 }
Andiry Xu5535b1d2010-10-14 07:23:06 -07001110 hcd->state = HC_STATE_SUSPENDED;
Sarah Sharpb3209372011-03-07 11:24:07 -08001111 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
Alan Sternf69e3122011-11-03 11:37:10 -04001112 goto done;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001113 }
1114
Andiry Xu5535b1d2010-10-14 07:23:06 -07001115 /* step 4: set Run/Stop bit */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001116 command = readl(&xhci->op_regs->command);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001117 command |= CMD_RUN;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02001118 writel(command, &xhci->op_regs->command);
Lin Wangdc0b1772015-01-09 16:06:28 +02001119 xhci_handshake(&xhci->op_regs->status, STS_HALT,
Andiry Xu5535b1d2010-10-14 07:23:06 -07001120 0, 250 * 1000);
1121
1122 /* step 5: walk topology and initialize portsc,
1123 * portpmsc and portli
1124 */
1125 /* this is done in bus_resume */
1126
1127 /* step 6: restart each of the previously
1128 * Running endpoints by ringing their doorbells
1129 */
1130
Andiry Xu5535b1d2010-10-14 07:23:06 -07001131 spin_unlock_irq(&xhci->lock);
Alan Sternf69e3122011-11-03 11:37:10 -04001132
1133 done:
1134 if (retval == 0) {
Wang, Yud6236f62014-06-24 17:14:44 +03001135 /* Resume root hubs only when have pending events. */
1136 status = readl(&xhci->op_regs->status);
1137 if (status & STS_EINT) {
Wang, Yud6236f62014-06-24 17:14:44 +03001138 usb_hcd_resume_root_hub(xhci->shared_hcd);
Mathias Nyman671ffdf2016-04-08 16:25:06 +03001139 usb_hcd_resume_root_hub(hcd);
Wang, Yud6236f62014-06-24 17:14:44 +03001140 }
Alan Sternf69e3122011-11-03 11:37:10 -04001141 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001142
1143 /*
1144 * If system is subject to the Quirk, Compliance Mode Timer needs to
1145 * be re-initialized Always after a system resume. Ports are subject
1146 * to suffer the Compliance Mode issue again. It doesn't matter if
1147 * ports have entered previously to U0 before system's suspension.
1148 */
Tony Camuso77df9e02013-02-21 16:11:27 -05001149 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001150 compliance_mode_recovery_timer_init(xhci);
1151
Jiahau Chang24a950e2017-07-20 14:48:27 +03001152 if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
1153 usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller));
1154
Sarah Sharpc52804a2012-11-27 12:30:23 -08001155 /* Re-enable port polling. */
1156 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
Al Cooper14e61a12014-08-20 16:41:57 +03001157 set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
1158 usb_hcd_poll_rh_status(xhci->shared_hcd);
Mathias Nyman671ffdf2016-04-08 16:25:06 +03001159 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1160 usb_hcd_poll_rh_status(hcd);
Sarah Sharpc52804a2012-11-27 12:30:23 -08001161
Alan Sternf69e3122011-11-03 11:37:10 -04001162 return retval;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001163}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03001164EXPORT_SYMBOL_GPL(xhci_resume);
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -07001165#endif /* CONFIG_PM */
1166
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001167/*-------------------------------------------------------------------------*/
1168
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001169/**
1170 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1171 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1172 * value to right shift 1 for the bitmask.
1173 *
1174 * Index = (epnum * 2) + direction - 1,
1175 * where direction = 0 for OUT, 1 for IN.
1176 * For control endpoints, the IN index is used (OUT index is unused), so
1177 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1178 */
1179unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1180{
1181 unsigned int index;
1182 if (usb_endpoint_xfer_control(desc))
1183 index = (unsigned int) (usb_endpoint_num(desc)*2);
1184 else
1185 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1186 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1187 return index;
1188}
1189
Julius Werner01c5f442013-04-15 15:55:04 -07001190/* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1191 * address from the XHCI endpoint index.
1192 */
1193unsigned int xhci_get_endpoint_address(unsigned int ep_index)
1194{
1195 unsigned int number = DIV_ROUND_UP(ep_index, 2);
1196 unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
1197 return direction | number;
1198}
1199
Sarah Sharpf94e01862009-04-27 19:58:38 -07001200/* Find the flag for this endpoint (for use in the control context). Use the
1201 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1202 * bit 1, etc.
1203 */
1204unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
1205{
1206 return 1 << (xhci_get_endpoint_index(desc) + 1);
1207}
1208
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001209/* Find the flag for this endpoint (for use in the control context). Use the
1210 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1211 * bit 1, etc.
1212 */
1213unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
1214{
1215 return 1 << (ep_index + 1);
1216}
1217
Sarah Sharpf94e01862009-04-27 19:58:38 -07001218/* Compute the last valid endpoint context index. Basically, this is the
1219 * endpoint index plus one. For slot contexts with more than valid endpoint,
1220 * we find the most significant bit set in the added contexts flags.
1221 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1222 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1223 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001224unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001225{
1226 return fls(added_ctxs) - 1;
1227}
1228
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001229/* Returns 1 if the arguments are OK;
1230 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1231 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -08001232static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -07001233 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1234 const char *func) {
1235 struct xhci_hcd *xhci;
1236 struct xhci_virt_device *virt_dev;
1237
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001238 if (!hcd || (check_ep && !ep) || !udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001239 pr_debug("xHCI %s called with invalid args\n", func);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001240 return -EINVAL;
1241 }
1242 if (!udev->parent) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001243 pr_debug("xHCI %s called for root hub\n", func);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001244 return 0;
1245 }
Andiry Xu64927732010-10-14 07:22:45 -07001246
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001247 xhci = hcd_to_xhci(hcd);
Andiry Xu64927732010-10-14 07:22:45 -07001248 if (check_virt_dev) {
sifram.rajas@gmail.com73ddc242011-09-02 11:06:00 -07001249 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001250 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
1251 func);
Andiry Xu64927732010-10-14 07:22:45 -07001252 return -EINVAL;
1253 }
1254
1255 virt_dev = xhci->devs[udev->slot_id];
1256 if (virt_dev->udev != udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001257 xhci_dbg(xhci, "xHCI %s called with udev and "
Andiry Xu64927732010-10-14 07:22:45 -07001258 "virt_dev does not match\n", func);
1259 return -EINVAL;
1260 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001261 }
Andiry Xu64927732010-10-14 07:22:45 -07001262
Sarah Sharp203a8662013-07-24 10:27:13 -07001263 if (xhci->xhc_state & XHCI_STATE_HALTED)
1264 return -ENODEV;
1265
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001266 return 1;
1267}
1268
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001269static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001270 struct usb_device *udev, struct xhci_command *command,
1271 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001272
1273/*
1274 * Full speed devices may have a max packet size greater than 8 bytes, but the
1275 * USB core doesn't know that until it reads the first 8 bytes of the
1276 * descriptor. If the usb_device's max packet size changes after that point,
1277 * we need to issue an evaluate context command and wait on it.
1278 */
1279static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1280 unsigned int ep_index, struct urb *urb)
1281{
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001282 struct xhci_container_ctx *out_ctx;
1283 struct xhci_input_control_ctx *ctrl_ctx;
1284 struct xhci_ep_ctx *ep_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001285 struct xhci_command *command;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001286 int max_packet_size;
1287 int hw_max_packet_size;
1288 int ret = 0;
1289
1290 out_ctx = xhci->devs[slot_id]->out_ctx;
1291 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001292 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001293 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001294 if (hw_max_packet_size != max_packet_size) {
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001295 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1296 "Max Packet Size for ep 0 changed.");
1297 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1298 "Max packet size in usb_device = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001299 max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001300 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1301 "Max packet size in xHCI HW = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001302 hw_max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001303 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1304 "Issuing evaluate context command.");
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001305
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001306 /* Set up the input context flags for the command */
1307 /* FIXME: This won't work if a non-default control endpoint
1308 * changes max packet sizes.
1309 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001310
1311 command = xhci_alloc_command(xhci, false, true, GFP_KERNEL);
1312 if (!command)
1313 return -ENOMEM;
1314
1315 command->in_ctx = xhci->devs[slot_id]->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001316 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001317 if (!ctrl_ctx) {
1318 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1319 __func__);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001320 ret = -ENOMEM;
1321 goto command_cleanup;
Sarah Sharp92f8e762013-04-23 17:11:14 -07001322 }
1323 /* Set up the modified control endpoint 0 */
1324 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1325 xhci->devs[slot_id]->out_ctx, ep_index);
1326
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001327 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001328 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1329 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
1330
Matt Evans28ccd292011-03-29 13:40:46 +11001331 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001332 ctrl_ctx->drop_flags = 0;
1333
1334 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001335 xhci_dbg_ctx(xhci, command->in_ctx, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001336 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
1337 xhci_dbg_ctx(xhci, out_ctx, ep_index);
1338
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001339 ret = xhci_configure_endpoint(xhci, urb->dev, command,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001340 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001341
1342 /* Clean up the input context for later use by bandwidth
1343 * functions.
1344 */
Matt Evans28ccd292011-03-29 13:40:46 +11001345 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001346command_cleanup:
1347 kfree(command->completion);
1348 kfree(command);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001349 }
1350 return ret;
1351}
1352
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001353/*
1354 * non-error returns are a promise to giveback() the urb later
1355 * we drop ownership so next owner (or urb unlink) can get it
1356 */
1357int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1358{
1359 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Andiry Xu2ffdea22011-09-02 11:05:57 -07001360 struct xhci_td *buffer;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001361 unsigned long flags;
1362 int ret = 0;
1363 unsigned int slot_id, ep_index;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001364 struct urb_priv *urb_priv;
1365 int size, i;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001366
Andiry Xu64927732010-10-14 07:22:45 -07001367 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1368 true, true, __func__) <= 0)
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001369 return -EINVAL;
1370
1371 slot_id = urb->dev->slot_id;
1372 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001373
Alan Stern541c7d42010-06-22 16:39:10 -04001374 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001375 if (!in_interrupt())
1376 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
1377 ret = -ESHUTDOWN;
1378 goto exit;
1379 }
Andiry Xu8e51adc2010-07-22 15:23:31 -07001380
1381 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1382 size = urb->number_of_packets;
Reyad Attiyat4758dcd2015-08-06 19:23:58 +03001383 else if (usb_endpoint_is_bulk_out(&urb->ep->desc) &&
1384 urb->transfer_buffer_length > 0 &&
1385 urb->transfer_flags & URB_ZERO_PACKET &&
1386 !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc)))
1387 size = 2;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001388 else
1389 size = 1;
1390
1391 urb_priv = kzalloc(sizeof(struct urb_priv) +
1392 size * sizeof(struct xhci_td *), mem_flags);
1393 if (!urb_priv)
1394 return -ENOMEM;
1395
Andiry Xu2ffdea22011-09-02 11:05:57 -07001396 buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
1397 if (!buffer) {
1398 kfree(urb_priv);
1399 return -ENOMEM;
1400 }
1401
Andiry Xu8e51adc2010-07-22 15:23:31 -07001402 for (i = 0; i < size; i++) {
Andiry Xu2ffdea22011-09-02 11:05:57 -07001403 urb_priv->td[i] = buffer;
1404 buffer++;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001405 }
1406
1407 urb_priv->length = size;
1408 urb_priv->td_cnt = 0;
1409 urb->hcpriv = urb_priv;
1410
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001411 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1412 /* Check to see if the max packet size for the default control
1413 * endpoint changed during FS device enumeration
1414 */
1415 if (urb->dev->speed == USB_SPEED_FULL) {
1416 ret = xhci_check_maxpacket(xhci, slot_id,
1417 ep_index, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001418 if (ret < 0) {
Lin Wang4daf9df2015-01-09 16:06:31 +02001419 xhci_urb_free_priv(urb_priv);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001420 urb->hcpriv = NULL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001421 return ret;
Sarah Sharpd13565c2011-07-22 14:34:34 -07001422 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001423 }
1424
Sarah Sharpb11069f2009-07-27 12:03:23 -07001425 /* We have a spinlock and interrupts disabled, so we must pass
1426 * atomic context to this function, which may allocate memory.
1427 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001428 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001429 if (xhci->xhc_state & XHCI_STATE_DYING)
1430 goto dying;
Sarah Sharpb11069f2009-07-27 12:03:23 -07001431 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -07001432 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001433 if (ret)
1434 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001435 spin_unlock_irqrestore(&xhci->lock, flags);
1436 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1437 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001438 if (xhci->xhc_state & XHCI_STATE_DYING)
1439 goto dying;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001440 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1441 EP_GETTING_STREAMS) {
1442 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1443 "is transitioning to using streams.\n");
1444 ret = -EINVAL;
1445 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1446 EP_GETTING_NO_STREAMS) {
1447 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1448 "is transitioning to "
1449 "not having streams.\n");
1450 ret = -EINVAL;
1451 } else {
1452 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1453 slot_id, ep_index);
1454 }
Sarah Sharpd13565c2011-07-22 14:34:34 -07001455 if (ret)
1456 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001457 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -07001458 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1459 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001460 if (xhci->xhc_state & XHCI_STATE_DYING)
1461 goto dying;
Sarah Sharp624defa2009-09-02 12:14:28 -07001462 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1463 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001464 if (ret)
1465 goto free_priv;
Sarah Sharp624defa2009-09-02 12:14:28 -07001466 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001467 } else {
Andiry Xu787f4e52010-07-22 15:23:52 -07001468 spin_lock_irqsave(&xhci->lock, flags);
1469 if (xhci->xhc_state & XHCI_STATE_DYING)
1470 goto dying;
1471 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1472 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001473 if (ret)
1474 goto free_priv;
Andiry Xu787f4e52010-07-22 15:23:52 -07001475 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001476 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001477exit:
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001478 return ret;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001479dying:
1480 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1481 "non-responsive xHCI host.\n",
1482 urb->ep->desc.bEndpointAddress, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001483 ret = -ESHUTDOWN;
1484free_priv:
Lin Wang4daf9df2015-01-09 16:06:31 +02001485 xhci_urb_free_priv(urb_priv);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001486 urb->hcpriv = NULL;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001487 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001488 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001489}
1490
Sarah Sharpae636742009-04-29 19:02:31 -07001491/*
1492 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1493 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1494 * should pick up where it left off in the TD, unless a Set Transfer Ring
1495 * Dequeue Pointer is issued.
1496 *
1497 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1498 * the ring. Since the ring is a contiguous structure, they can't be physically
1499 * removed. Instead, there are two options:
1500 *
1501 * 1) If the HC is in the middle of processing the URB to be canceled, we
1502 * simply move the ring's dequeue pointer past those TRBs using the Set
1503 * Transfer Ring Dequeue Pointer command. This will be the common case,
1504 * when drivers timeout on the last submitted URB and attempt to cancel.
1505 *
1506 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1507 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1508 * HC will need to invalidate the any TRBs it has cached after the stop
1509 * endpoint command, as noted in the xHCI 0.95 errata.
1510 *
1511 * 3) The TD may have completed by the time the Stop Endpoint Command
1512 * completes, so software needs to handle that case too.
1513 *
1514 * This function should protect against the TD enqueueing code ringing the
1515 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1516 * It also needs to account for multiple cancellations on happening at the same
1517 * time for the same endpoint.
1518 *
1519 * Note that this function can be called in any context, or so says
1520 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001521 */
1522int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1523{
Sarah Sharpae636742009-04-29 19:02:31 -07001524 unsigned long flags;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001525 int ret, i;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001526 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -07001527 struct xhci_hcd *xhci;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001528 struct urb_priv *urb_priv;
Sarah Sharpae636742009-04-29 19:02:31 -07001529 struct xhci_td *td;
1530 unsigned int ep_index;
1531 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001532 struct xhci_virt_ep *ep;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001533 struct xhci_command *command;
Sarah Sharpae636742009-04-29 19:02:31 -07001534
1535 xhci = hcd_to_xhci(hcd);
1536 spin_lock_irqsave(&xhci->lock, flags);
1537 /* Make sure the URB hasn't completed or been unlinked already */
1538 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1539 if (ret || !urb->hcpriv)
1540 goto done;
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02001541 temp = readl(&xhci->op_regs->status);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -08001542 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001543 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1544 "HW died, freeing TD.");
Andiry Xu8e51adc2010-07-22 15:23:31 -07001545 urb_priv = urb->hcpriv;
Mathias Nyman5c821712016-01-26 17:50:12 +02001546 for (i = urb_priv->td_cnt;
1547 i < urb_priv->length && xhci->devs[urb->dev->slot_id];
1548 i++) {
Sarah Sharp585df1d2011-08-02 15:43:40 -07001549 td = urb_priv->td[i];
1550 if (!list_empty(&td->td_list))
1551 list_del_init(&td->td_list);
1552 if (!list_empty(&td->cancelled_td_list))
1553 list_del_init(&td->cancelled_td_list);
1554 }
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001555
1556 usb_hcd_unlink_urb_from_ep(hcd, urb);
1557 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp214f76f2010-10-26 11:22:02 -07001558 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
Lin Wang4daf9df2015-01-09 16:06:31 +02001559 xhci_urb_free_priv(urb_priv);
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001560 return ret;
1561 }
Sarah Sharpae636742009-04-29 19:02:31 -07001562
Sarah Sharpae636742009-04-29 19:02:31 -07001563 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001564 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001565 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1566 if (!ep_ring) {
1567 ret = -EINVAL;
1568 goto done;
1569 }
1570
Andiry Xu8e51adc2010-07-22 15:23:31 -07001571 urb_priv = urb->hcpriv;
Sarah Sharp79688ac2011-12-19 16:56:04 -08001572 i = urb_priv->td_cnt;
1573 if (i < urb_priv->length)
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001574 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1575 "Cancel URB %p, dev %s, ep 0x%x, "
1576 "starting at offset 0x%llx",
Sarah Sharp79688ac2011-12-19 16:56:04 -08001577 urb, urb->dev->devpath,
1578 urb->ep->desc.bEndpointAddress,
1579 (unsigned long long) xhci_trb_virt_to_dma(
1580 urb_priv->td[i]->start_seg,
1581 urb_priv->td[i]->first_trb));
Andiry Xu8e51adc2010-07-22 15:23:31 -07001582
Sarah Sharp79688ac2011-12-19 16:56:04 -08001583 for (; i < urb_priv->length; i++) {
Andiry Xu8e51adc2010-07-22 15:23:31 -07001584 td = urb_priv->td[i];
1585 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1586 }
1587
Sarah Sharpae636742009-04-29 19:02:31 -07001588 /* Queue a stop endpoint command, but only if this is
1589 * the first cancellation to be handled.
1590 */
Sarah Sharp678539c2009-10-27 10:55:52 -07001591 if (!(ep->ep_state & EP_HALT_PENDING)) {
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001592 command = xhci_alloc_command(xhci, false, false, GFP_ATOMIC);
Hans de Goedea0ee6192014-07-25 22:01:21 +02001593 if (!command) {
1594 ret = -ENOMEM;
1595 goto done;
1596 }
Sarah Sharp678539c2009-10-27 10:55:52 -07001597 ep->ep_state |= EP_HALT_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001598 ep->stop_cmds_pending++;
1599 ep->stop_cmd_timer.expires = jiffies +
1600 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1601 add_timer(&ep->stop_cmd_timer);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03001602 xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id,
1603 ep_index, 0);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001604 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -07001605 }
1606done:
1607 spin_unlock_irqrestore(&xhci->lock, flags);
1608 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001609}
1610
Sarah Sharpf94e01862009-04-27 19:58:38 -07001611/* Drop an endpoint from a new bandwidth configuration for this device.
1612 * Only one call to this function is allowed per endpoint before
1613 * check_bandwidth() or reset_bandwidth() must be called.
1614 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1615 * add the endpoint to the schedule with possibly new parameters denoted by a
1616 * different endpoint descriptor in usb_host_endpoint.
1617 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1618 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001619 *
1620 * The USB core will not allow URBs to be queued to an endpoint that is being
1621 * disabled, so there's no need for mutual exclusion to protect
1622 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001623 */
1624int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1625 struct usb_host_endpoint *ep)
1626{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001627 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001628 struct xhci_container_ctx *in_ctx, *out_ctx;
1629 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001630 unsigned int ep_index;
1631 struct xhci_ep_ctx *ep_ctx;
1632 u32 drop_flag;
Julius Wernerd6759132014-06-24 17:14:42 +03001633 u32 new_add_flags, new_drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001634 int ret;
1635
Andiry Xu64927732010-10-14 07:22:45 -07001636 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001637 if (ret <= 0)
1638 return ret;
1639 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001640 if (xhci->xhc_state & XHCI_STATE_DYING)
1641 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001642
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001643 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001644 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1645 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1646 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1647 __func__, drop_flag);
1648 return 0;
1649 }
1650
Sarah Sharpf94e01862009-04-27 19:58:38 -07001651 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001652 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001653 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001654 if (!ctrl_ctx) {
1655 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1656 __func__);
1657 return 0;
1658 }
1659
Sarah Sharpf94e01862009-04-27 19:58:38 -07001660 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001661 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001662 /* If the HC already knows the endpoint is disabled,
1663 * or the HCD has noted it is disabled, ignore this request
1664 */
Matt Evansf5960b62011-06-01 10:22:55 +10001665 if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1666 cpu_to_le32(EP_STATE_DISABLED)) ||
Matt Evans28ccd292011-03-29 13:40:46 +11001667 le32_to_cpu(ctrl_ctx->drop_flags) &
1668 xhci_get_endpoint_flag(&ep->desc)) {
Hans de Goedea6134132015-01-16 17:54:02 +02001669 /* Do not warn when called after a usb_device_reset */
1670 if (xhci->devs[udev->slot_id]->eps[ep_index].ring != NULL)
1671 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1672 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001673 return 0;
1674 }
1675
Matt Evans28ccd292011-03-29 13:40:46 +11001676 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1677 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001678
Matt Evans28ccd292011-03-29 13:40:46 +11001679 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1680 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001681
Sarah Sharpf94e01862009-04-27 19:58:38 -07001682 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1683
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001684 if (xhci->quirks & XHCI_MTK_HOST)
1685 xhci_mtk_drop_ep_quirk(hcd, udev, ep);
1686
Julius Wernerd6759132014-06-24 17:14:42 +03001687 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 -07001688 (unsigned int) ep->desc.bEndpointAddress,
1689 udev->slot_id,
1690 (unsigned int) new_drop_flags,
Julius Wernerd6759132014-06-24 17:14:42 +03001691 (unsigned int) new_add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001692 return 0;
1693}
1694
1695/* Add an endpoint to a new possible bandwidth configuration for this device.
1696 * Only one call to this function is allowed per endpoint before
1697 * check_bandwidth() or reset_bandwidth() must be called.
1698 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1699 * add the endpoint to the schedule with possibly new parameters denoted by a
1700 * different endpoint descriptor in usb_host_endpoint.
1701 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1702 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001703 *
1704 * The USB core will not allow URBs to be queued to an endpoint until the
1705 * configuration or alt setting is installed in the device, so there's no need
1706 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001707 */
1708int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1709 struct usb_host_endpoint *ep)
1710{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001711 struct xhci_hcd *xhci;
Lin Wang92c96912015-01-09 16:06:27 +02001712 struct xhci_container_ctx *in_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001713 unsigned int ep_index;
John Yound115b042009-07-27 12:05:15 -07001714 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001715 u32 added_ctxs;
Julius Wernerd6759132014-06-24 17:14:42 +03001716 u32 new_add_flags, new_drop_flags;
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001717 struct xhci_virt_device *virt_dev;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001718 int ret = 0;
1719
Andiry Xu64927732010-10-14 07:22:45 -07001720 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001721 if (ret <= 0) {
1722 /* So we won't queue a reset ep command for a root hub */
1723 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001724 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001725 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001726 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001727 if (xhci->xhc_state & XHCI_STATE_DYING)
1728 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001729
1730 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001731 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1732 /* FIXME when we have to issue an evaluate endpoint command to
1733 * deal with ep0 max packet size changing once we get the
1734 * descriptors
1735 */
1736 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1737 __func__, added_ctxs);
1738 return 0;
1739 }
1740
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001741 virt_dev = xhci->devs[udev->slot_id];
1742 in_ctx = virt_dev->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02001743 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001744 if (!ctrl_ctx) {
1745 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1746 __func__);
1747 return 0;
1748 }
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001749
Sarah Sharp92f8e762013-04-23 17:11:14 -07001750 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001751 /* If this endpoint is already in use, and the upper layers are trying
1752 * to add it again without dropping it, reject the addition.
1753 */
1754 if (virt_dev->eps[ep_index].ring &&
Lin Wang92c96912015-01-09 16:06:27 +02001755 !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) {
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001756 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1757 "without dropping it.\n",
1758 (unsigned int) ep->desc.bEndpointAddress);
1759 return -EINVAL;
1760 }
1761
Sarah Sharpf94e01862009-04-27 19:58:38 -07001762 /* If the HCD has already noted the endpoint is enabled,
1763 * ignore this request.
1764 */
Lin Wang92c96912015-01-09 16:06:27 +02001765 if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001766 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1767 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001768 return 0;
1769 }
1770
Sarah Sharpf88ba782009-05-14 11:44:22 -07001771 /*
1772 * Configuration and alternate setting changes must be done in
1773 * process context, not interrupt context (or so documenation
1774 * for usb_set_interface() and usb_set_configuration() claim).
1775 */
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001776 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001777 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1778 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001779 return -ENOMEM;
1780 }
1781
Chunfeng Yun0cbd4b32015-11-24 13:09:55 +02001782 if (xhci->quirks & XHCI_MTK_HOST) {
1783 ret = xhci_mtk_add_ep_quirk(hcd, udev, ep);
1784 if (ret < 0) {
1785 xhci_free_or_cache_endpoint_ring(xhci,
1786 virt_dev, ep_index);
1787 return ret;
1788 }
1789 }
1790
Matt Evans28ccd292011-03-29 13:40:46 +11001791 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1792 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001793
1794 /* If xhci_endpoint_disable() was called for this endpoint, but the
1795 * xHC hasn't been notified yet through the check_bandwidth() call,
1796 * this re-adds a new state for the endpoint from the new endpoint
1797 * descriptors. We must drop and re-add this endpoint, so we leave the
1798 * drop flags alone.
1799 */
Matt Evans28ccd292011-03-29 13:40:46 +11001800 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001801
Sarah Sharpa1587d92009-07-27 12:03:15 -07001802 /* Store the usb_device pointer for later use */
1803 ep->hcpriv = udev;
1804
Julius Wernerd6759132014-06-24 17:14:42 +03001805 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 -07001806 (unsigned int) ep->desc.bEndpointAddress,
1807 udev->slot_id,
1808 (unsigned int) new_drop_flags,
Julius Wernerd6759132014-06-24 17:14:42 +03001809 (unsigned int) new_add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001810 return 0;
1811}
1812
John Yound115b042009-07-27 12:05:15 -07001813static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001814{
John Yound115b042009-07-27 12:05:15 -07001815 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001816 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001817 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001818 int i;
1819
Lin Wang4daf9df2015-01-09 16:06:31 +02001820 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001821 if (!ctrl_ctx) {
1822 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1823 __func__);
1824 return;
1825 }
1826
Sarah Sharpf94e01862009-04-27 19:58:38 -07001827 /* When a device's add flag and drop flag are zero, any subsequent
1828 * configure endpoint command will leave that endpoint's state
1829 * untouched. Make sure we don't leave any old state in the input
1830 * endpoint contexts.
1831 */
John Yound115b042009-07-27 12:05:15 -07001832 ctrl_ctx->drop_flags = 0;
1833 ctrl_ctx->add_flags = 0;
1834 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001835 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001836 /* Endpoint 0 is always valid */
Matt Evans28ccd292011-03-29 13:40:46 +11001837 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001838 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001839 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001840 ep_ctx->ep_info = 0;
1841 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001842 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001843 ep_ctx->tx_info = 0;
1844 }
1845}
1846
Sarah Sharpf2217e82009-08-07 14:04:43 -07001847static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001848 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001849{
1850 int ret;
1851
Sarah Sharp913a8a32009-09-04 10:53:13 -07001852 switch (*cmd_status) {
Mathias Nymanc311e392014-05-08 19:26:03 +03001853 case COMP_CMD_ABORT:
1854 case COMP_CMD_STOP:
1855 xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
1856 ret = -ETIME;
1857 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001858 case COMP_ENOMEM:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001859 dev_warn(&udev->dev,
1860 "Not enough host controller resources for new device state.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001861 ret = -ENOMEM;
1862 /* FIXME: can we allocate more resources for the HC? */
1863 break;
1864 case COMP_BW_ERR:
Hans de Goede71d85722012-01-04 23:29:18 +01001865 case COMP_2ND_BW_ERR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001866 dev_warn(&udev->dev,
1867 "Not enough bandwidth for new device state.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001868 ret = -ENOSPC;
1869 /* FIXME: can we go back to the old state? */
1870 break;
1871 case COMP_TRB_ERR:
1872 /* the HCD set up something wrong */
1873 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1874 "add flag = 1, "
1875 "and endpoint is not disabled.\n");
1876 ret = -EINVAL;
1877 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001878 case COMP_DEV_ERR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001879 dev_warn(&udev->dev,
1880 "ERROR: Incompatible device for endpoint configure command.\n");
Alex Hef6ba6fe2011-06-08 18:34:06 +08001881 ret = -ENODEV;
1882 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001883 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001884 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1885 "Successful Endpoint Configure command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001886 ret = 0;
1887 break;
1888 default:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001889 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
1890 *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001891 ret = -EINVAL;
1892 break;
1893 }
1894 return ret;
1895}
1896
1897static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001898 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001899{
1900 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001901 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001902
Sarah Sharp913a8a32009-09-04 10:53:13 -07001903 switch (*cmd_status) {
Mathias Nymanc311e392014-05-08 19:26:03 +03001904 case COMP_CMD_ABORT:
1905 case COMP_CMD_STOP:
1906 xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
1907 ret = -ETIME;
1908 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001909 case COMP_EINVAL:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001910 dev_warn(&udev->dev,
1911 "WARN: xHCI driver setup invalid evaluate context command.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001912 ret = -EINVAL;
1913 break;
1914 case COMP_EBADSLT:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001915 dev_warn(&udev->dev,
1916 "WARN: slot not enabled for evaluate context command.\n");
Sarah Sharpb8031342012-10-16 13:26:22 -07001917 ret = -EINVAL;
1918 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001919 case COMP_CTX_STATE:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001920 dev_warn(&udev->dev,
1921 "WARN: invalid context state for evaluate context command.\n");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001922 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1923 ret = -EINVAL;
1924 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001925 case COMP_DEV_ERR:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001926 dev_warn(&udev->dev,
1927 "ERROR: Incompatible device for evaluate context command.\n");
Alex Hef6ba6fe2011-06-08 18:34:06 +08001928 ret = -ENODEV;
1929 break;
Alex He1bb73a82011-05-05 18:14:12 +08001930 case COMP_MEL_ERR:
1931 /* Max Exit Latency too large error */
1932 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1933 ret = -EINVAL;
1934 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001935 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001936 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1937 "Successful evaluate context command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001938 ret = 0;
1939 break;
1940 default:
Oliver Neukum288c0f42014-06-02 15:25:17 +02001941 xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
1942 *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001943 ret = -EINVAL;
1944 break;
1945 }
1946 return ret;
1947}
1948
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001949static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001950 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001951{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001952 u32 valid_add_flags;
1953 u32 valid_drop_flags;
1954
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001955 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1956 * (bit 1). The default control endpoint is added during the Address
1957 * Device command and is never removed until the slot is disabled.
1958 */
Xenia Ragiadakouef734002013-09-09 21:03:06 +03001959 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
1960 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001961
1962 /* Use hweight32 to count the number of ones in the add flags, or
1963 * number of endpoints added. Don't count endpoints that are changed
1964 * (both added and dropped).
1965 */
1966 return hweight32(valid_add_flags) -
1967 hweight32(valid_add_flags & valid_drop_flags);
1968}
1969
1970static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001971 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001972{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001973 u32 valid_add_flags;
1974 u32 valid_drop_flags;
1975
Xenia Ragiadakou78d1ff02013-09-09 21:03:07 +03001976 valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
1977 valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001978
1979 return hweight32(valid_drop_flags) -
1980 hweight32(valid_add_flags & valid_drop_flags);
1981}
1982
1983/*
1984 * We need to reserve the new number of endpoints before the configure endpoint
1985 * command completes. We can't subtract the dropped endpoints from the number
1986 * of active endpoints until the command completes because we can oversubscribe
1987 * the host in this case:
1988 *
1989 * - the first configure endpoint command drops more endpoints than it adds
1990 * - a second configure endpoint command that adds more endpoints is queued
1991 * - the first configure endpoint command fails, so the config is unchanged
1992 * - the second command may succeed, even though there isn't enough resources
1993 *
1994 * Must be called with xhci->lock held.
1995 */
1996static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001997 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001998{
1999 u32 added_eps;
2000
Sarah Sharp92f8e762013-04-23 17:11:14 -07002001 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002002 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002003 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2004 "Not enough ep ctxs: "
2005 "%u active, need to add %u, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002006 xhci->num_active_eps, added_eps,
2007 xhci->limit_active_eps);
2008 return -ENOMEM;
2009 }
2010 xhci->num_active_eps += added_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002011 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2012 "Adding %u ep ctxs, %u now active.", added_eps,
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002013 xhci->num_active_eps);
2014 return 0;
2015}
2016
2017/*
2018 * The configure endpoint was failed by the xHC for some other reason, so we
2019 * need to revert the resources that failed configuration would have used.
2020 *
2021 * Must be called with xhci->lock held.
2022 */
2023static void xhci_free_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002024 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002025{
2026 u32 num_failed_eps;
2027
Sarah Sharp92f8e762013-04-23 17:11:14 -07002028 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002029 xhci->num_active_eps -= num_failed_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002030 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2031 "Removing %u failed ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002032 num_failed_eps,
2033 xhci->num_active_eps);
2034}
2035
2036/*
2037 * Now that the command has completed, clean up the active endpoint count by
2038 * subtracting out the endpoints that were dropped (but not changed).
2039 *
2040 * Must be called with xhci->lock held.
2041 */
2042static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002043 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002044{
2045 u32 num_dropped_eps;
2046
Sarah Sharp92f8e762013-04-23 17:11:14 -07002047 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002048 xhci->num_active_eps -= num_dropped_eps;
2049 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002050 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2051 "Removing %u dropped ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002052 num_dropped_eps,
2053 xhci->num_active_eps);
2054}
2055
Felipe Balbied384bd2012-08-07 14:10:03 +03002056static unsigned int xhci_get_block_size(struct usb_device *udev)
Sarah Sharpc29eea62011-09-02 11:05:52 -07002057{
2058 switch (udev->speed) {
2059 case USB_SPEED_LOW:
2060 case USB_SPEED_FULL:
2061 return FS_BLOCK;
2062 case USB_SPEED_HIGH:
2063 return HS_BLOCK;
2064 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002065 case USB_SPEED_SUPER_PLUS:
Sarah Sharpc29eea62011-09-02 11:05:52 -07002066 return SS_BLOCK;
2067 case USB_SPEED_UNKNOWN:
2068 case USB_SPEED_WIRELESS:
2069 default:
2070 /* Should never happen */
2071 return 1;
2072 }
2073}
2074
Felipe Balbied384bd2012-08-07 14:10:03 +03002075static unsigned int
2076xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
Sarah Sharpc29eea62011-09-02 11:05:52 -07002077{
2078 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
2079 return LS_OVERHEAD;
2080 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2081 return FS_OVERHEAD;
2082 return HS_OVERHEAD;
2083}
2084
2085/* If we are changing a LS/FS device under a HS hub,
2086 * make sure (if we are activating a new TT) that the HS bus has enough
2087 * bandwidth for this new TT.
2088 */
2089static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2090 struct xhci_virt_device *virt_dev,
2091 int old_active_eps)
2092{
2093 struct xhci_interval_bw_table *bw_table;
2094 struct xhci_tt_bw_info *tt_info;
2095
2096 /* Find the bandwidth table for the root port this TT is attached to. */
2097 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2098 tt_info = virt_dev->tt_info;
2099 /* If this TT already had active endpoints, the bandwidth for this TT
2100 * has already been added. Removing all periodic endpoints (and thus
2101 * making the TT enactive) will only decrease the bandwidth used.
2102 */
2103 if (old_active_eps)
2104 return 0;
2105 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2106 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2107 return -ENOMEM;
2108 return 0;
2109 }
2110 /* Not sure why we would have no new active endpoints...
2111 *
2112 * Maybe because of an Evaluate Context change for a hub update or a
2113 * control endpoint 0 max packet size change?
2114 * FIXME: skip the bandwidth calculation in that case.
2115 */
2116 return 0;
2117}
2118
Sarah Sharp2b698992011-09-13 16:41:13 -07002119static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2120 struct xhci_virt_device *virt_dev)
2121{
2122 unsigned int bw_reserved;
2123
2124 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2125 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2126 return -ENOMEM;
2127
2128 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2129 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2130 return -ENOMEM;
2131
2132 return 0;
2133}
2134
Sarah Sharpc29eea62011-09-02 11:05:52 -07002135/*
2136 * This algorithm is a very conservative estimate of the worst-case scheduling
2137 * scenario for any one interval. The hardware dynamically schedules the
2138 * packets, so we can't tell which microframe could be the limiting factor in
2139 * the bandwidth scheduling. This only takes into account periodic endpoints.
2140 *
2141 * Obviously, we can't solve an NP complete problem to find the minimum worst
2142 * case scenario. Instead, we come up with an estimate that is no less than
2143 * the worst case bandwidth used for any one microframe, but may be an
2144 * over-estimate.
2145 *
2146 * We walk the requirements for each endpoint by interval, starting with the
2147 * smallest interval, and place packets in the schedule where there is only one
2148 * possible way to schedule packets for that interval. In order to simplify
2149 * this algorithm, we record the largest max packet size for each interval, and
2150 * assume all packets will be that size.
2151 *
2152 * For interval 0, we obviously must schedule all packets for each interval.
2153 * The bandwidth for interval 0 is just the amount of data to be transmitted
2154 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2155 * the number of packets).
2156 *
2157 * For interval 1, we have two possible microframes to schedule those packets
2158 * in. For this algorithm, if we can schedule the same number of packets for
2159 * each possible scheduling opportunity (each microframe), we will do so. The
2160 * remaining number of packets will be saved to be transmitted in the gaps in
2161 * the next interval's scheduling sequence.
2162 *
2163 * As we move those remaining packets to be scheduled with interval 2 packets,
2164 * we have to double the number of remaining packets to transmit. This is
2165 * because the intervals are actually powers of 2, and we would be transmitting
2166 * the previous interval's packets twice in this interval. We also have to be
2167 * sure that when we look at the largest max packet size for this interval, we
2168 * also look at the largest max packet size for the remaining packets and take
2169 * the greater of the two.
2170 *
2171 * The algorithm continues to evenly distribute packets in each scheduling
2172 * opportunity, and push the remaining packets out, until we get to the last
2173 * interval. Then those packets and their associated overhead are just added
2174 * to the bandwidth used.
Sarah Sharp2e279802011-09-02 11:05:50 -07002175 */
2176static int xhci_check_bw_table(struct xhci_hcd *xhci,
2177 struct xhci_virt_device *virt_dev,
2178 int old_active_eps)
2179{
Sarah Sharpc29eea62011-09-02 11:05:52 -07002180 unsigned int bw_reserved;
2181 unsigned int max_bandwidth;
2182 unsigned int bw_used;
2183 unsigned int block_size;
2184 struct xhci_interval_bw_table *bw_table;
2185 unsigned int packet_size = 0;
2186 unsigned int overhead = 0;
2187 unsigned int packets_transmitted = 0;
2188 unsigned int packets_remaining = 0;
2189 unsigned int i;
2190
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002191 if (virt_dev->udev->speed >= USB_SPEED_SUPER)
Sarah Sharp2b698992011-09-13 16:41:13 -07002192 return xhci_check_ss_bw(xhci, virt_dev);
2193
Sarah Sharpc29eea62011-09-02 11:05:52 -07002194 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2195 max_bandwidth = HS_BW_LIMIT;
2196 /* Convert percent of bus BW reserved to blocks reserved */
2197 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2198 } else {
2199 max_bandwidth = FS_BW_LIMIT;
2200 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2201 }
2202
2203 bw_table = virt_dev->bw_table;
2204 /* We need to translate the max packet size and max ESIT payloads into
2205 * the units the hardware uses.
2206 */
2207 block_size = xhci_get_block_size(virt_dev->udev);
2208
2209 /* If we are manipulating a LS/FS device under a HS hub, double check
2210 * that the HS bus has enough bandwidth if we are activing a new TT.
2211 */
2212 if (virt_dev->tt_info) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002213 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2214 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002215 virt_dev->real_port);
2216 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2217 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2218 "newly activated TT.\n");
2219 return -ENOMEM;
2220 }
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002221 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2222 "Recalculating BW for TT slot %u port %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002223 virt_dev->tt_info->slot_id,
2224 virt_dev->tt_info->ttport);
2225 } else {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002226 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2227 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002228 virt_dev->real_port);
2229 }
2230
2231 /* Add in how much bandwidth will be used for interval zero, or the
2232 * rounded max ESIT payload + number of packets * largest overhead.
2233 */
2234 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2235 bw_table->interval_bw[0].num_packets *
2236 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2237
2238 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2239 unsigned int bw_added;
2240 unsigned int largest_mps;
2241 unsigned int interval_overhead;
2242
2243 /*
2244 * How many packets could we transmit in this interval?
2245 * If packets didn't fit in the previous interval, we will need
2246 * to transmit that many packets twice within this interval.
2247 */
2248 packets_remaining = 2 * packets_remaining +
2249 bw_table->interval_bw[i].num_packets;
2250
2251 /* Find the largest max packet size of this or the previous
2252 * interval.
2253 */
2254 if (list_empty(&bw_table->interval_bw[i].endpoints))
2255 largest_mps = 0;
2256 else {
2257 struct xhci_virt_ep *virt_ep;
2258 struct list_head *ep_entry;
2259
2260 ep_entry = bw_table->interval_bw[i].endpoints.next;
2261 virt_ep = list_entry(ep_entry,
2262 struct xhci_virt_ep, bw_endpoint_list);
2263 /* Convert to blocks, rounding up */
2264 largest_mps = DIV_ROUND_UP(
2265 virt_ep->bw_info.max_packet_size,
2266 block_size);
2267 }
2268 if (largest_mps > packet_size)
2269 packet_size = largest_mps;
2270
2271 /* Use the larger overhead of this or the previous interval. */
2272 interval_overhead = xhci_get_largest_overhead(
2273 &bw_table->interval_bw[i]);
2274 if (interval_overhead > overhead)
2275 overhead = interval_overhead;
2276
2277 /* How many packets can we evenly distribute across
2278 * (1 << (i + 1)) possible scheduling opportunities?
2279 */
2280 packets_transmitted = packets_remaining >> (i + 1);
2281
2282 /* Add in the bandwidth used for those scheduled packets */
2283 bw_added = packets_transmitted * (overhead + packet_size);
2284
2285 /* How many packets do we have remaining to transmit? */
2286 packets_remaining = packets_remaining % (1 << (i + 1));
2287
2288 /* What largest max packet size should those packets have? */
2289 /* If we've transmitted all packets, don't carry over the
2290 * largest packet size.
2291 */
2292 if (packets_remaining == 0) {
2293 packet_size = 0;
2294 overhead = 0;
2295 } else if (packets_transmitted > 0) {
2296 /* Otherwise if we do have remaining packets, and we've
2297 * scheduled some packets in this interval, take the
2298 * largest max packet size from endpoints with this
2299 * interval.
2300 */
2301 packet_size = largest_mps;
2302 overhead = interval_overhead;
2303 }
2304 /* Otherwise carry over packet_size and overhead from the last
2305 * time we had a remainder.
2306 */
2307 bw_used += bw_added;
2308 if (bw_used > max_bandwidth) {
2309 xhci_warn(xhci, "Not enough bandwidth. "
2310 "Proposed: %u, Max: %u\n",
2311 bw_used, max_bandwidth);
2312 return -ENOMEM;
2313 }
2314 }
2315 /*
2316 * Ok, we know we have some packets left over after even-handedly
2317 * scheduling interval 15. We don't know which microframes they will
2318 * fit into, so we over-schedule and say they will be scheduled every
2319 * microframe.
2320 */
2321 if (packets_remaining > 0)
2322 bw_used += overhead + packet_size;
2323
2324 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2325 unsigned int port_index = virt_dev->real_port - 1;
2326
2327 /* OK, we're manipulating a HS device attached to a
2328 * root port bandwidth domain. Include the number of active TTs
2329 * in the bandwidth used.
2330 */
2331 bw_used += TT_HS_OVERHEAD *
2332 xhci->rh_bw[port_index].num_active_tts;
2333 }
2334
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002335 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2336 "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2337 "Available: %u " "percent",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002338 bw_used, max_bandwidth, bw_reserved,
2339 (max_bandwidth - bw_used - bw_reserved) * 100 /
2340 max_bandwidth);
2341
2342 bw_used += bw_reserved;
2343 if (bw_used > max_bandwidth) {
2344 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2345 bw_used, max_bandwidth);
2346 return -ENOMEM;
2347 }
2348
2349 bw_table->bw_used = bw_used;
Sarah Sharp2e279802011-09-02 11:05:50 -07002350 return 0;
2351}
2352
2353static bool xhci_is_async_ep(unsigned int ep_type)
2354{
2355 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2356 ep_type != ISOC_IN_EP &&
2357 ep_type != INT_IN_EP);
2358}
2359
Sarah Sharp2b698992011-09-13 16:41:13 -07002360static bool xhci_is_sync_in_ep(unsigned int ep_type)
2361{
Sarah Sharp392a07a2012-10-25 13:44:12 -07002362 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
Sarah Sharp2b698992011-09-13 16:41:13 -07002363}
2364
2365static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2366{
2367 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2368
2369 if (ep_bw->ep_interval == 0)
2370 return SS_OVERHEAD_BURST +
2371 (ep_bw->mult * ep_bw->num_packets *
2372 (SS_OVERHEAD + mps));
2373 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2374 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2375 1 << ep_bw->ep_interval);
2376
2377}
2378
Sarah Sharp2e279802011-09-02 11:05:50 -07002379void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
2380 struct xhci_bw_info *ep_bw,
2381 struct xhci_interval_bw_table *bw_table,
2382 struct usb_device *udev,
2383 struct xhci_virt_ep *virt_ep,
2384 struct xhci_tt_bw_info *tt_info)
2385{
2386 struct xhci_interval_bw *interval_bw;
2387 int normalized_interval;
2388
Sarah Sharp2b698992011-09-13 16:41:13 -07002389 if (xhci_is_async_ep(ep_bw->type))
Sarah Sharp2e279802011-09-02 11:05:50 -07002390 return;
2391
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002392 if (udev->speed >= USB_SPEED_SUPER) {
Sarah Sharp2b698992011-09-13 16:41:13 -07002393 if (xhci_is_sync_in_ep(ep_bw->type))
2394 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2395 xhci_get_ss_bw_consumed(ep_bw);
2396 else
2397 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2398 xhci_get_ss_bw_consumed(ep_bw);
2399 return;
2400 }
2401
2402 /* SuperSpeed endpoints never get added to intervals in the table, so
2403 * this check is only valid for HS/FS/LS devices.
2404 */
2405 if (list_empty(&virt_ep->bw_endpoint_list))
2406 return;
Sarah Sharp2e279802011-09-02 11:05:50 -07002407 /* For LS/FS devices, we need to translate the interval expressed in
2408 * microframes to frames.
2409 */
2410 if (udev->speed == USB_SPEED_HIGH)
2411 normalized_interval = ep_bw->ep_interval;
2412 else
2413 normalized_interval = ep_bw->ep_interval - 3;
2414
2415 if (normalized_interval == 0)
2416 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2417 interval_bw = &bw_table->interval_bw[normalized_interval];
2418 interval_bw->num_packets -= ep_bw->num_packets;
2419 switch (udev->speed) {
2420 case USB_SPEED_LOW:
2421 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2422 break;
2423 case USB_SPEED_FULL:
2424 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2425 break;
2426 case USB_SPEED_HIGH:
2427 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2428 break;
2429 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002430 case USB_SPEED_SUPER_PLUS:
Sarah Sharp2e279802011-09-02 11:05:50 -07002431 case USB_SPEED_UNKNOWN:
2432 case USB_SPEED_WIRELESS:
2433 /* Should never happen because only LS/FS/HS endpoints will get
2434 * added to the endpoint list.
2435 */
2436 return;
2437 }
2438 if (tt_info)
2439 tt_info->active_eps -= 1;
2440 list_del_init(&virt_ep->bw_endpoint_list);
2441}
2442
2443static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2444 struct xhci_bw_info *ep_bw,
2445 struct xhci_interval_bw_table *bw_table,
2446 struct usb_device *udev,
2447 struct xhci_virt_ep *virt_ep,
2448 struct xhci_tt_bw_info *tt_info)
2449{
2450 struct xhci_interval_bw *interval_bw;
2451 struct xhci_virt_ep *smaller_ep;
2452 int normalized_interval;
2453
2454 if (xhci_is_async_ep(ep_bw->type))
2455 return;
2456
Sarah Sharp2b698992011-09-13 16:41:13 -07002457 if (udev->speed == USB_SPEED_SUPER) {
2458 if (xhci_is_sync_in_ep(ep_bw->type))
2459 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2460 xhci_get_ss_bw_consumed(ep_bw);
2461 else
2462 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2463 xhci_get_ss_bw_consumed(ep_bw);
2464 return;
2465 }
2466
Sarah Sharp2e279802011-09-02 11:05:50 -07002467 /* For LS/FS devices, we need to translate the interval expressed in
2468 * microframes to frames.
2469 */
2470 if (udev->speed == USB_SPEED_HIGH)
2471 normalized_interval = ep_bw->ep_interval;
2472 else
2473 normalized_interval = ep_bw->ep_interval - 3;
2474
2475 if (normalized_interval == 0)
2476 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2477 interval_bw = &bw_table->interval_bw[normalized_interval];
2478 interval_bw->num_packets += ep_bw->num_packets;
2479 switch (udev->speed) {
2480 case USB_SPEED_LOW:
2481 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2482 break;
2483 case USB_SPEED_FULL:
2484 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2485 break;
2486 case USB_SPEED_HIGH:
2487 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2488 break;
2489 case USB_SPEED_SUPER:
Mathias Nyman0caf6b32016-01-25 15:30:44 +02002490 case USB_SPEED_SUPER_PLUS:
Sarah Sharp2e279802011-09-02 11:05:50 -07002491 case USB_SPEED_UNKNOWN:
2492 case USB_SPEED_WIRELESS:
2493 /* Should never happen because only LS/FS/HS endpoints will get
2494 * added to the endpoint list.
2495 */
2496 return;
2497 }
2498
2499 if (tt_info)
2500 tt_info->active_eps += 1;
2501 /* Insert the endpoint into the list, largest max packet size first. */
2502 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2503 bw_endpoint_list) {
2504 if (ep_bw->max_packet_size >=
2505 smaller_ep->bw_info.max_packet_size) {
2506 /* Add the new ep before the smaller endpoint */
2507 list_add_tail(&virt_ep->bw_endpoint_list,
2508 &smaller_ep->bw_endpoint_list);
2509 return;
2510 }
2511 }
2512 /* Add the new endpoint at the end of the list. */
2513 list_add_tail(&virt_ep->bw_endpoint_list,
2514 &interval_bw->endpoints);
2515}
2516
2517void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2518 struct xhci_virt_device *virt_dev,
2519 int old_active_eps)
2520{
2521 struct xhci_root_port_bw_info *rh_bw_info;
2522 if (!virt_dev->tt_info)
2523 return;
2524
2525 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2526 if (old_active_eps == 0 &&
2527 virt_dev->tt_info->active_eps != 0) {
2528 rh_bw_info->num_active_tts += 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002529 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002530 } else if (old_active_eps != 0 &&
2531 virt_dev->tt_info->active_eps == 0) {
2532 rh_bw_info->num_active_tts -= 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002533 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002534 }
2535}
2536
2537static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2538 struct xhci_virt_device *virt_dev,
2539 struct xhci_container_ctx *in_ctx)
2540{
2541 struct xhci_bw_info ep_bw_info[31];
2542 int i;
2543 struct xhci_input_control_ctx *ctrl_ctx;
2544 int old_active_eps = 0;
2545
Sarah Sharp2e279802011-09-02 11:05:50 -07002546 if (virt_dev->tt_info)
2547 old_active_eps = virt_dev->tt_info->active_eps;
2548
Lin Wang4daf9df2015-01-09 16:06:31 +02002549 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002550 if (!ctrl_ctx) {
2551 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2552 __func__);
2553 return -ENOMEM;
2554 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002555
2556 for (i = 0; i < 31; i++) {
2557 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2558 continue;
2559
2560 /* Make a copy of the BW info in case we need to revert this */
2561 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2562 sizeof(ep_bw_info[i]));
2563 /* Drop the endpoint from the interval table if the endpoint is
2564 * being dropped or changed.
2565 */
2566 if (EP_IS_DROPPED(ctrl_ctx, i))
2567 xhci_drop_ep_from_interval_table(xhci,
2568 &virt_dev->eps[i].bw_info,
2569 virt_dev->bw_table,
2570 virt_dev->udev,
2571 &virt_dev->eps[i],
2572 virt_dev->tt_info);
2573 }
2574 /* Overwrite the information stored in the endpoints' bw_info */
2575 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2576 for (i = 0; i < 31; i++) {
2577 /* Add any changed or added endpoints to the interval table */
2578 if (EP_IS_ADDED(ctrl_ctx, i))
2579 xhci_add_ep_to_interval_table(xhci,
2580 &virt_dev->eps[i].bw_info,
2581 virt_dev->bw_table,
2582 virt_dev->udev,
2583 &virt_dev->eps[i],
2584 virt_dev->tt_info);
2585 }
2586
2587 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2588 /* Ok, this fits in the bandwidth we have.
2589 * Update the number of active TTs.
2590 */
2591 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2592 return 0;
2593 }
2594
2595 /* We don't have enough bandwidth for this, revert the stored info. */
2596 for (i = 0; i < 31; i++) {
2597 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2598 continue;
2599
2600 /* Drop the new copies of any added or changed endpoints from
2601 * the interval table.
2602 */
2603 if (EP_IS_ADDED(ctrl_ctx, i)) {
2604 xhci_drop_ep_from_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 /* Revert the endpoint back to its old information */
2612 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2613 sizeof(ep_bw_info[i]));
2614 /* Add any changed or dropped endpoints back into the table */
2615 if (EP_IS_DROPPED(ctrl_ctx, i))
2616 xhci_add_ep_to_interval_table(xhci,
2617 &virt_dev->eps[i].bw_info,
2618 virt_dev->bw_table,
2619 virt_dev->udev,
2620 &virt_dev->eps[i],
2621 virt_dev->tt_info);
2622 }
2623 return -ENOMEM;
2624}
2625
2626
Sarah Sharpf2217e82009-08-07 14:04:43 -07002627/* Issue a configure endpoint command or evaluate context command
2628 * and wait for it to finish.
2629 */
2630static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002631 struct usb_device *udev,
2632 struct xhci_command *command,
2633 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07002634{
2635 int ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002636 unsigned long flags;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002637 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002638 struct xhci_virt_device *virt_dev;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002639
2640 if (!command)
2641 return -EINVAL;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002642
2643 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002644 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002645
Lin Wang4daf9df2015-01-09 16:06:31 +02002646 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002647 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07002648 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002649 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2650 __func__);
2651 return -ENOMEM;
2652 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002653
2654 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
Sarah Sharp92f8e762013-04-23 17:11:14 -07002655 xhci_reserve_host_resources(xhci, ctrl_ctx)) {
Sarah Sharp750645f2011-09-02 11:05:43 -07002656 spin_unlock_irqrestore(&xhci->lock, flags);
2657 xhci_warn(xhci, "Not enough host resources, "
2658 "active endpoint contexts = %u\n",
2659 xhci->num_active_eps);
2660 return -ENOMEM;
2661 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002662 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002663 xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) {
Sarah Sharp2e279802011-09-02 11:05:50 -07002664 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002665 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2e279802011-09-02 11:05:50 -07002666 spin_unlock_irqrestore(&xhci->lock, flags);
2667 xhci_warn(xhci, "Not enough bandwidth\n");
2668 return -ENOMEM;
2669 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002670
Sarah Sharpf2217e82009-08-07 14:04:43 -07002671 if (!ctx_change)
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002672 ret = xhci_queue_configure_endpoint(xhci, command,
2673 command->in_ctx->dma,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002674 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002675 else
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002676 ret = xhci_queue_evaluate_context(xhci, command,
2677 command->in_ctx->dma,
Sarah Sharp4b266542012-05-07 15:34:26 -07002678 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002679 if (ret < 0) {
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002680 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002681 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002682 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03002683 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
2684 "FIXME allocate a new ring segment");
Sarah Sharpf2217e82009-08-07 14:04:43 -07002685 return -ENOMEM;
2686 }
2687 xhci_ring_cmd_db(xhci);
2688 spin_unlock_irqrestore(&xhci->lock, flags);
2689
2690 /* Wait for the configure endpoint command to complete */
Mathias Nymanc311e392014-05-08 19:26:03 +03002691 wait_for_completion(command->completion);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002692
2693 if (!ctx_change)
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002694 ret = xhci_configure_endpoint_result(xhci, udev,
2695 &command->status);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002696 else
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002697 ret = xhci_evaluate_context_result(xhci, udev,
2698 &command->status);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002699
2700 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2701 spin_lock_irqsave(&xhci->lock, flags);
2702 /* If the command failed, remove the reserved resources.
2703 * Otherwise, clean up the estimate to include dropped eps.
2704 */
2705 if (ret)
Sarah Sharp92f8e762013-04-23 17:11:14 -07002706 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002707 else
Sarah Sharp92f8e762013-04-23 17:11:14 -07002708 xhci_finish_resource_reservation(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002709 spin_unlock_irqrestore(&xhci->lock, flags);
2710 }
2711 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002712}
2713
Hans de Goededf613832013-10-04 00:29:45 +02002714static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci,
2715 struct xhci_virt_device *vdev, int i)
2716{
2717 struct xhci_virt_ep *ep = &vdev->eps[i];
2718
2719 if (ep->ep_state & EP_HAS_STREAMS) {
2720 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n",
2721 xhci_get_endpoint_address(i));
2722 xhci_free_stream_info(xhci, ep->stream_info);
2723 ep->stream_info = NULL;
2724 ep->ep_state &= ~EP_HAS_STREAMS;
2725 }
2726}
2727
Sarah Sharpf88ba782009-05-14 11:44:22 -07002728/* Called after one or more calls to xhci_add_endpoint() or
2729 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2730 * to call xhci_reset_bandwidth().
2731 *
2732 * Since we are in the middle of changing either configuration or
2733 * installing a new alt setting, the USB core won't allow URBs to be
2734 * enqueued for any endpoint on the old config or interface. Nothing
2735 * else should be touching the xhci->devs[slot_id] structure, so we
2736 * don't need to take the xhci->lock for manipulating that.
2737 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002738int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2739{
2740 int i;
2741 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002742 struct xhci_hcd *xhci;
2743 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07002744 struct xhci_input_control_ctx *ctrl_ctx;
2745 struct xhci_slot_ctx *slot_ctx;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002746 struct xhci_command *command;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002747
Andiry Xu64927732010-10-14 07:22:45 -07002748 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002749 if (ret <= 0)
2750 return ret;
2751 xhci = hcd_to_xhci(hcd);
Mathias Nyman98d74f92016-04-08 16:25:10 +03002752 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
2753 (xhci->xhc_state & XHCI_STATE_REMOVING))
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07002754 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002755
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002756 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002757 virt_dev = xhci->devs[udev->slot_id];
2758
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002759 command = xhci_alloc_command(xhci, false, true, GFP_KERNEL);
2760 if (!command)
2761 return -ENOMEM;
2762
2763 command->in_ctx = virt_dev->in_ctx;
2764
Sarah Sharpf94e01862009-04-27 19:58:38 -07002765 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
Lin Wang4daf9df2015-01-09 16:06:31 +02002766 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002767 if (!ctrl_ctx) {
2768 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2769 __func__);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002770 ret = -ENOMEM;
2771 goto command_cleanup;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002772 }
Matt Evans28ccd292011-03-29 13:40:46 +11002773 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2774 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2775 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
Sarah Sharp2dc37532011-09-02 11:05:40 -07002776
2777 /* Don't issue the command if there's no endpoints to update. */
2778 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002779 ctrl_ctx->drop_flags == 0) {
2780 ret = 0;
2781 goto command_cleanup;
2782 }
Julius Wernerd6759132014-06-24 17:14:42 +03002783 /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
John Yound115b042009-07-27 12:05:15 -07002784 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Julius Wernerd6759132014-06-24 17:14:42 +03002785 for (i = 31; i >= 1; i--) {
2786 __le32 le32 = cpu_to_le32(BIT(i));
2787
2788 if ((virt_dev->eps[i-1].ring && !(ctrl_ctx->drop_flags & le32))
2789 || (ctrl_ctx->add_flags & le32) || i == 1) {
2790 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
2791 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
2792 break;
2793 }
2794 }
2795 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07002796 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002797 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002798
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002799 ret = xhci_configure_endpoint(xhci, udev, command,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002800 false, false);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002801 if (ret)
Sarah Sharpf94e01862009-04-27 19:58:38 -07002802 /* Callee should call reset_bandwidth() */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002803 goto command_cleanup;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002804
2805 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07002806 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002807 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002808
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002809 /* Free any rings that were dropped, but not changed. */
2810 for (i = 1; i < 31; ++i) {
Matt Evans4819fef2011-06-01 13:01:07 +10002811 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
Hans de Goededf613832013-10-04 00:29:45 +02002812 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) {
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002813 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Hans de Goededf613832013-10-04 00:29:45 +02002814 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
2815 }
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002816 }
John Yound115b042009-07-27 12:05:15 -07002817 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002818 /*
2819 * Install any rings for completely new endpoints or changed endpoints,
2820 * and free or cache any old rings from changed endpoints.
2821 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002822 for (i = 1; i < 31; ++i) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002823 if (!virt_dev->eps[i].new_ring)
2824 continue;
2825 /* Only cache or free the old ring if it exists.
2826 * It may not if this is the first add of an endpoint.
2827 */
2828 if (virt_dev->eps[i].ring) {
Sarah Sharp412566b2009-12-09 15:59:01 -08002829 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002830 }
Hans de Goededf613832013-10-04 00:29:45 +02002831 xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002832 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2833 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002834 }
Mathias Nymanddba5cd2014-05-08 19:26:00 +03002835command_cleanup:
2836 kfree(command->completion);
2837 kfree(command);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002838
Sarah Sharpf94e01862009-04-27 19:58:38 -07002839 return ret;
2840}
2841
2842void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2843{
Sarah Sharpf94e01862009-04-27 19:58:38 -07002844 struct xhci_hcd *xhci;
2845 struct xhci_virt_device *virt_dev;
2846 int i, ret;
2847
Andiry Xu64927732010-10-14 07:22:45 -07002848 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002849 if (ret <= 0)
2850 return;
2851 xhci = hcd_to_xhci(hcd);
2852
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002853 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002854 virt_dev = xhci->devs[udev->slot_id];
2855 /* Free any rings allocated for added endpoints */
2856 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002857 if (virt_dev->eps[i].new_ring) {
2858 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2859 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002860 }
2861 }
John Yound115b042009-07-27 12:05:15 -07002862 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002863}
2864
Sarah Sharp5270b952009-09-04 10:53:11 -07002865static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002866 struct xhci_container_ctx *in_ctx,
2867 struct xhci_container_ctx *out_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002868 struct xhci_input_control_ctx *ctrl_ctx,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002869 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07002870{
Matt Evans28ccd292011-03-29 13:40:46 +11002871 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2872 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002873 xhci_slot_copy(xhci, in_ctx, out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002874 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharp5270b952009-09-04 10:53:11 -07002875
Sarah Sharp913a8a32009-09-04 10:53:13 -07002876 xhci_dbg(xhci, "Input Context:\n");
2877 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07002878}
2879
Dmitry Torokhov8212a492011-02-08 13:55:59 -08002880static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002881 unsigned int slot_id, unsigned int ep_index,
2882 struct xhci_dequeue_state *deq_state)
2883{
Sarah Sharp92f8e762013-04-23 17:11:14 -07002884 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002885 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002886 struct xhci_ep_ctx *ep_ctx;
2887 u32 added_ctxs;
2888 dma_addr_t addr;
2889
Sarah Sharp92f8e762013-04-23 17:11:14 -07002890 in_ctx = xhci->devs[slot_id]->in_ctx;
Lin Wang4daf9df2015-01-09 16:06:31 +02002891 ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002892 if (!ctrl_ctx) {
2893 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2894 __func__);
2895 return;
2896 }
2897
Sarah Sharp913a8a32009-09-04 10:53:13 -07002898 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2899 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002900 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2901 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2902 deq_state->new_deq_ptr);
2903 if (addr == 0) {
2904 xhci_warn(xhci, "WARN Cannot submit config ep after "
2905 "reset ep command\n");
2906 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2907 deq_state->new_deq_seg,
2908 deq_state->new_deq_ptr);
2909 return;
2910 }
Matt Evans28ccd292011-03-29 13:40:46 +11002911 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002912
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002913 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002914 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002915 xhci->devs[slot_id]->out_ctx, ctrl_ctx,
2916 added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002917}
2918
Sarah Sharp82d10092009-08-07 14:04:52 -07002919void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Mathias Nymand97b4f82014-11-27 18:19:16 +02002920 unsigned int ep_index, struct xhci_td *td)
Sarah Sharp82d10092009-08-07 14:04:52 -07002921{
2922 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002923 struct xhci_virt_ep *ep;
Mathias Nymand97b4f82014-11-27 18:19:16 +02002924 struct usb_device *udev = td->urb->dev;
Sarah Sharp82d10092009-08-07 14:04:52 -07002925
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002926 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2927 "Cleaning up stalled endpoint ring");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002928 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07002929 /* We need to move the HW's dequeue pointer past this TD,
2930 * or it will attempt to resend it on the next doorbell ring.
2931 */
2932 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Mathias Nymand97b4f82014-11-27 18:19:16 +02002933 ep_index, ep->stopped_stream, td, &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07002934
Mathias Nyman365038d2014-08-19 15:17:58 +03002935 if (!deq_state.new_deq_ptr || !deq_state.new_deq_seg)
2936 return;
2937
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002938 /* HW with the reset endpoint quirk will use the saved dequeue state to
2939 * issue a configure endpoint command later.
2940 */
2941 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002942 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2943 "Queueing new dequeue state");
Hans de Goede1e3452e2014-08-20 16:41:52 +03002944 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002945 ep_index, ep->stopped_stream, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002946 } else {
2947 /* Better hope no one uses the input context between now and the
2948 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002949 * XXX: No idea how this hardware will react when stream rings
2950 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002951 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002952 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2953 "Setting up input context for "
2954 "configure endpoint command");
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002955 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2956 ep_index, &deq_state);
2957 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002958}
2959
Mathias Nymand0167ad2015-03-10 19:49:00 +02002960/* Called when clearing halted device. The core should have sent the control
Mathias Nyman8e71a322014-11-18 11:27:12 +02002961 * message to clear the device halt condition. The host side of the halt should
Mathias Nymand0167ad2015-03-10 19:49:00 +02002962 * already be cleared with a reset endpoint command issued when the STALL tx
2963 * event was received.
2964 *
2965 * Context: in_interrupt
Sarah Sharpa1587d92009-07-27 12:03:15 -07002966 */
Mathias Nyman8e71a322014-11-18 11:27:12 +02002967
Sarah Sharpa1587d92009-07-27 12:03:15 -07002968void xhci_endpoint_reset(struct usb_hcd *hcd,
2969 struct usb_host_endpoint *ep)
2970{
2971 struct xhci_hcd *xhci;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002972
2973 xhci = hcd_to_xhci(hcd);
Sarah Sharpa1587d92009-07-27 12:03:15 -07002974
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002975 /*
Mathias Nymand0167ad2015-03-10 19:49:00 +02002976 * We might need to implement the config ep cmd in xhci 4.8.1 note:
Mathias Nyman8e71a322014-11-18 11:27:12 +02002977 * The Reset Endpoint Command may only be issued to endpoints in the
2978 * Halted state. If software wishes reset the Data Toggle or Sequence
2979 * Number of an endpoint that isn't in the Halted state, then software
2980 * may issue a Configure Endpoint Command with the Drop and Add bits set
2981 * for the target endpoint. that is in the Stopped state.
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002982 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07002983
Mathias Nymand0167ad2015-03-10 19:49:00 +02002984 /* For now just print debug to follow the situation */
2985 xhci_dbg(xhci, "Endpoint 0x%x ep reset callback called\n",
2986 ep->desc.bEndpointAddress);
Sarah Sharpa1587d92009-07-27 12:03:15 -07002987}
2988
Sarah Sharp8df75f42010-04-02 15:34:16 -07002989static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2990 struct usb_device *udev, struct usb_host_endpoint *ep,
2991 unsigned int slot_id)
2992{
2993 int ret;
2994 unsigned int ep_index;
2995 unsigned int ep_state;
2996
2997 if (!ep)
2998 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07002999 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003000 if (ret <= 0)
3001 return -EINVAL;
Hans de Goedea3901532013-10-04 17:05:55 +02003002 if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07003003 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
3004 " descriptor for ep 0x%x does not support streams\n",
3005 ep->desc.bEndpointAddress);
3006 return -EINVAL;
3007 }
3008
3009 ep_index = xhci_get_endpoint_index(&ep->desc);
3010 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3011 if (ep_state & EP_HAS_STREAMS ||
3012 ep_state & EP_GETTING_STREAMS) {
3013 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
3014 "already has streams set up.\n",
3015 ep->desc.bEndpointAddress);
3016 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
3017 "dynamic stream context array reallocation.\n");
3018 return -EINVAL;
3019 }
3020 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
3021 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
3022 "endpoint 0x%x; URBs are pending.\n",
3023 ep->desc.bEndpointAddress);
3024 return -EINVAL;
3025 }
3026 return 0;
3027}
3028
3029static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
3030 unsigned int *num_streams, unsigned int *num_stream_ctxs)
3031{
3032 unsigned int max_streams;
3033
3034 /* The stream context array size must be a power of two */
3035 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
3036 /*
3037 * Find out how many primary stream array entries the host controller
3038 * supports. Later we may use secondary stream arrays (similar to 2nd
3039 * level page entries), but that's an optional feature for xHCI host
3040 * controllers. xHCs must support at least 4 stream IDs.
3041 */
3042 max_streams = HCC_MAX_PSA(xhci->hcc_params);
3043 if (*num_stream_ctxs > max_streams) {
3044 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
3045 max_streams);
3046 *num_stream_ctxs = max_streams;
3047 *num_streams = max_streams;
3048 }
3049}
3050
3051/* Returns an error code if one of the endpoint already has streams.
3052 * This does not change any data structures, it only checks and gathers
3053 * information.
3054 */
3055static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
3056 struct usb_device *udev,
3057 struct usb_host_endpoint **eps, unsigned int num_eps,
3058 unsigned int *num_streams, u32 *changed_ep_bitmask)
3059{
Sarah Sharp8df75f42010-04-02 15:34:16 -07003060 unsigned int max_streams;
3061 unsigned int endpoint_flag;
3062 int i;
3063 int ret;
3064
3065 for (i = 0; i < num_eps; i++) {
3066 ret = xhci_check_streams_endpoint(xhci, udev,
3067 eps[i], udev->slot_id);
3068 if (ret < 0)
3069 return ret;
3070
Felipe Balbi18b7ede2012-01-02 13:35:41 +02003071 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003072 if (max_streams < (*num_streams - 1)) {
3073 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
3074 eps[i]->desc.bEndpointAddress,
3075 max_streams);
3076 *num_streams = max_streams+1;
3077 }
3078
3079 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
3080 if (*changed_ep_bitmask & endpoint_flag)
3081 return -EINVAL;
3082 *changed_ep_bitmask |= endpoint_flag;
3083 }
3084 return 0;
3085}
3086
3087static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3088 struct usb_device *udev,
3089 struct usb_host_endpoint **eps, unsigned int num_eps)
3090{
3091 u32 changed_ep_bitmask = 0;
3092 unsigned int slot_id;
3093 unsigned int ep_index;
3094 unsigned int ep_state;
3095 int i;
3096
3097 slot_id = udev->slot_id;
3098 if (!xhci->devs[slot_id])
3099 return 0;
3100
3101 for (i = 0; i < num_eps; i++) {
3102 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3103 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3104 /* Are streams already being freed for the endpoint? */
3105 if (ep_state & EP_GETTING_NO_STREAMS) {
3106 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003107 "endpoint 0x%x, "
3108 "streams are being disabled already\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003109 eps[i]->desc.bEndpointAddress);
3110 return 0;
3111 }
3112 /* Are there actually any streams to free? */
3113 if (!(ep_state & EP_HAS_STREAMS) &&
3114 !(ep_state & EP_GETTING_STREAMS)) {
3115 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003116 "endpoint 0x%x, "
3117 "streams are already disabled!\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003118 eps[i]->desc.bEndpointAddress);
3119 xhci_warn(xhci, "WARN xhci_free_streams() called "
3120 "with non-streams endpoint\n");
3121 return 0;
3122 }
3123 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3124 }
3125 return changed_ep_bitmask;
3126}
3127
3128/*
Luis de Bethencourtc2a298d2015-06-30 16:48:54 +02003129 * The USB device drivers use this function (through the HCD interface in USB
Sarah Sharp8df75f42010-04-02 15:34:16 -07003130 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3131 * coordinate mass storage command queueing across multiple endpoints (basically
3132 * a stream ID == a task ID).
3133 *
3134 * Setting up streams involves allocating the same size stream context array
3135 * for each endpoint and issuing a configure endpoint command for all endpoints.
3136 *
3137 * Don't allow the call to succeed if one endpoint only supports one stream
3138 * (which means it doesn't support streams at all).
3139 *
3140 * Drivers may get less stream IDs than they asked for, if the host controller
3141 * hardware or endpoints claim they can't support the number of requested
3142 * stream IDs.
3143 */
3144int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
3145 struct usb_host_endpoint **eps, unsigned int num_eps,
3146 unsigned int num_streams, gfp_t mem_flags)
3147{
3148 int i, ret;
3149 struct xhci_hcd *xhci;
3150 struct xhci_virt_device *vdev;
3151 struct xhci_command *config_cmd;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003152 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003153 unsigned int ep_index;
3154 unsigned int num_stream_ctxs;
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003155 unsigned int max_packet;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003156 unsigned long flags;
3157 u32 changed_ep_bitmask = 0;
3158
3159 if (!eps)
3160 return -EINVAL;
3161
3162 /* Add one to the number of streams requested to account for
3163 * stream 0 that is reserved for xHCI usage.
3164 */
3165 num_streams += 1;
3166 xhci = hcd_to_xhci(hcd);
3167 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3168 num_streams);
3169
Hans de Goedef7920882013-11-15 12:14:38 +01003170 /* MaxPSASize value 0 (2 streams) means streams are not supported */
Hans de Goede8f873c12014-07-25 22:01:18 +02003171 if ((xhci->quirks & XHCI_BROKEN_STREAMS) ||
3172 HCC_MAX_PSA(xhci->hcc_params) < 4) {
Hans de Goedef7920882013-11-15 12:14:38 +01003173 xhci_dbg(xhci, "xHCI controller does not support streams.\n");
3174 return -ENOSYS;
3175 }
3176
Sarah Sharp8df75f42010-04-02 15:34:16 -07003177 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
3178 if (!config_cmd) {
3179 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
3180 return -ENOMEM;
3181 }
Lin Wang4daf9df2015-01-09 16:06:31 +02003182 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003183 if (!ctrl_ctx) {
3184 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3185 __func__);
3186 xhci_free_command(xhci, config_cmd);
3187 return -ENOMEM;
3188 }
Sarah Sharp8df75f42010-04-02 15:34:16 -07003189
3190 /* Check to make sure all endpoints are not already configured for
3191 * streams. While we're at it, find the maximum number of streams that
3192 * all the endpoints will support and check for duplicate endpoints.
3193 */
3194 spin_lock_irqsave(&xhci->lock, flags);
3195 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3196 num_eps, &num_streams, &changed_ep_bitmask);
3197 if (ret < 0) {
3198 xhci_free_command(xhci, config_cmd);
3199 spin_unlock_irqrestore(&xhci->lock, flags);
3200 return ret;
3201 }
3202 if (num_streams <= 1) {
3203 xhci_warn(xhci, "WARN: endpoints can't handle "
3204 "more than one stream.\n");
3205 xhci_free_command(xhci, config_cmd);
3206 spin_unlock_irqrestore(&xhci->lock, flags);
3207 return -EINVAL;
3208 }
3209 vdev = xhci->devs[udev->slot_id];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003210 /* Mark each endpoint as being in transition, so
Sarah Sharp8df75f42010-04-02 15:34:16 -07003211 * xhci_urb_enqueue() will reject all URBs.
3212 */
3213 for (i = 0; i < num_eps; i++) {
3214 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3215 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3216 }
3217 spin_unlock_irqrestore(&xhci->lock, flags);
3218
3219 /* Setup internal data structures and allocate HW data structures for
3220 * streams (but don't install the HW structures in the input context
3221 * until we're sure all memory allocation succeeded).
3222 */
3223 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3224 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3225 num_stream_ctxs, num_streams);
3226
3227 for (i = 0; i < num_eps; i++) {
3228 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003229 max_packet = GET_MAX_PACKET(usb_endpoint_maxp(&eps[i]->desc));
Sarah Sharp8df75f42010-04-02 15:34:16 -07003230 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3231 num_stream_ctxs,
Mathias Nymanf9c589e2016-06-21 10:58:02 +03003232 num_streams,
3233 max_packet, mem_flags);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003234 if (!vdev->eps[ep_index].stream_info)
3235 goto cleanup;
3236 /* Set maxPstreams in endpoint context and update deq ptr to
3237 * point to stream context array. FIXME
3238 */
3239 }
3240
3241 /* Set up the input context for a configure endpoint command. */
3242 for (i = 0; i < num_eps; i++) {
3243 struct xhci_ep_ctx *ep_ctx;
3244
3245 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3246 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3247
3248 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3249 vdev->out_ctx, ep_index);
3250 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3251 vdev->eps[ep_index].stream_info);
3252 }
3253 /* Tell the HW to drop its old copy of the endpoint context info
3254 * and add the updated copy from the input context.
3255 */
3256 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003257 vdev->out_ctx, ctrl_ctx,
3258 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003259
3260 /* Issue and wait for the configure endpoint command */
3261 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3262 false, false);
3263
3264 /* xHC rejected the configure endpoint command for some reason, so we
3265 * leave the old ring intact and free our internal streams data
3266 * structure.
3267 */
3268 if (ret < 0)
3269 goto cleanup;
3270
3271 spin_lock_irqsave(&xhci->lock, flags);
3272 for (i = 0; i < num_eps; i++) {
3273 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3274 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3275 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3276 udev->slot_id, ep_index);
3277 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3278 }
3279 xhci_free_command(xhci, config_cmd);
3280 spin_unlock_irqrestore(&xhci->lock, flags);
3281
3282 /* Subtract 1 for stream 0, which drivers can't use */
3283 return num_streams - 1;
3284
3285cleanup:
3286 /* If it didn't work, free the streams! */
3287 for (i = 0; i < num_eps; i++) {
3288 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3289 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003290 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003291 /* FIXME Unset maxPstreams in endpoint context and
3292 * update deq ptr to point to normal string ring.
3293 */
3294 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3295 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3296 xhci_endpoint_zero(xhci, vdev, eps[i]);
3297 }
3298 xhci_free_command(xhci, config_cmd);
3299 return -ENOMEM;
3300}
3301
3302/* Transition the endpoint from using streams to being a "normal" endpoint
3303 * without streams.
3304 *
3305 * Modify the endpoint context state, submit a configure endpoint command,
3306 * and free all endpoint rings for streams if that completes successfully.
3307 */
3308int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
3309 struct usb_host_endpoint **eps, unsigned int num_eps,
3310 gfp_t mem_flags)
3311{
3312 int i, ret;
3313 struct xhci_hcd *xhci;
3314 struct xhci_virt_device *vdev;
3315 struct xhci_command *command;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003316 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003317 unsigned int ep_index;
3318 unsigned long flags;
3319 u32 changed_ep_bitmask;
3320
3321 xhci = hcd_to_xhci(hcd);
3322 vdev = xhci->devs[udev->slot_id];
3323
3324 /* Set up a configure endpoint command to remove the streams rings */
3325 spin_lock_irqsave(&xhci->lock, flags);
3326 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3327 udev, eps, num_eps);
3328 if (changed_ep_bitmask == 0) {
3329 spin_unlock_irqrestore(&xhci->lock, flags);
3330 return -EINVAL;
3331 }
3332
3333 /* Use the xhci_command structure from the first endpoint. We may have
3334 * allocated too many, but the driver may call xhci_free_streams() for
3335 * each endpoint it grouped into one call to xhci_alloc_streams().
3336 */
3337 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3338 command = vdev->eps[ep_index].stream_info->free_streams_command;
Lin Wang4daf9df2015-01-09 16:06:31 +02003339 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003340 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07003341 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003342 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3343 __func__);
3344 return -EINVAL;
3345 }
3346
Sarah Sharp8df75f42010-04-02 15:34:16 -07003347 for (i = 0; i < num_eps; i++) {
3348 struct xhci_ep_ctx *ep_ctx;
3349
3350 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3351 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3352 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3353 EP_GETTING_NO_STREAMS;
3354
3355 xhci_endpoint_copy(xhci, command->in_ctx,
3356 vdev->out_ctx, ep_index);
Lin Wang4daf9df2015-01-09 16:06:31 +02003357 xhci_setup_no_streams_ep_input_ctx(ep_ctx,
Sarah Sharp8df75f42010-04-02 15:34:16 -07003358 &vdev->eps[ep_index]);
3359 }
3360 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003361 vdev->out_ctx, ctrl_ctx,
3362 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003363 spin_unlock_irqrestore(&xhci->lock, flags);
3364
3365 /* Issue and wait for the configure endpoint command,
3366 * which must succeed.
3367 */
3368 ret = xhci_configure_endpoint(xhci, udev, command,
3369 false, true);
3370
3371 /* xHC rejected the configure endpoint command for some reason, so we
3372 * leave the streams rings intact.
3373 */
3374 if (ret < 0)
3375 return ret;
3376
3377 spin_lock_irqsave(&xhci->lock, flags);
3378 for (i = 0; i < num_eps; i++) {
3379 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3380 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003381 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003382 /* FIXME Unset maxPstreams in endpoint context and
3383 * update deq ptr to point to normal string ring.
3384 */
3385 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3386 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3387 }
3388 spin_unlock_irqrestore(&xhci->lock, flags);
3389
3390 return 0;
3391}
3392
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003393/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003394 * Deletes endpoint resources for endpoints that were active before a Reset
3395 * Device command, or a Disable Slot command. The Reset Device command leaves
3396 * the control endpoint intact, whereas the Disable Slot command deletes it.
3397 *
3398 * Must be called with xhci->lock held.
3399 */
3400void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3401 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3402{
3403 int i;
3404 unsigned int num_dropped_eps = 0;
3405 unsigned int drop_flags = 0;
3406
3407 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3408 if (virt_dev->eps[i].ring) {
3409 drop_flags |= 1 << i;
3410 num_dropped_eps++;
3411 }
3412 }
3413 xhci->num_active_eps -= num_dropped_eps;
3414 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003415 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3416 "Dropped %u ep ctxs, flags = 0x%x, "
3417 "%u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003418 num_dropped_eps, drop_flags,
3419 xhci->num_active_eps);
3420}
3421
3422/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003423 * This submits a Reset Device Command, which will set the device state to 0,
3424 * set the device address to 0, and disable all the endpoints except the default
3425 * control endpoint. The USB core should come back and call
3426 * xhci_address_device(), and then re-set up the configuration. If this is
3427 * called because of a usb_reset_and_verify_device(), then the old alternate
3428 * settings will be re-installed through the normal bandwidth allocation
3429 * functions.
3430 *
3431 * Wait for the Reset Device command to finish. Remove all structures
3432 * associated with the endpoints that were disabled. Clear the input device
3433 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07003434 *
3435 * If the virt_dev to be reset does not exist or does not match the udev,
3436 * it means the device is lost, possibly due to the xHC restore error and
3437 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3438 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003439 */
Andiry Xuf0615c42010-10-14 07:22:48 -07003440int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003441{
3442 int ret, i;
3443 unsigned long flags;
3444 struct xhci_hcd *xhci;
3445 unsigned int slot_id;
3446 struct xhci_virt_device *virt_dev;
3447 struct xhci_command *reset_device_cmd;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003448 int last_freed_endpoint;
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003449 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp2e279802011-09-02 11:05:50 -07003450 int old_active_eps = 0;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003451
Andiry Xuf0615c42010-10-14 07:22:48 -07003452 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003453 if (ret <= 0)
3454 return ret;
3455 xhci = hcd_to_xhci(hcd);
3456 slot_id = udev->slot_id;
3457 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07003458 if (!virt_dev) {
3459 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3460 "not exist. Re-allocate the device\n", slot_id);
3461 ret = xhci_alloc_dev(hcd, udev);
3462 if (ret == 1)
3463 return 0;
3464 else
3465 return -EINVAL;
3466 }
3467
Brian Campbell326124a2015-07-21 17:20:28 +03003468 if (virt_dev->tt_info)
3469 old_active_eps = virt_dev->tt_info->active_eps;
3470
Andiry Xuf0615c42010-10-14 07:22:48 -07003471 if (virt_dev->udev != udev) {
3472 /* If the virt_dev and the udev does not match, this virt_dev
3473 * may belong to another udev.
3474 * Re-allocate the device.
3475 */
3476 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3477 "not match the udev. Re-allocate the device\n",
3478 slot_id);
3479 ret = xhci_alloc_dev(hcd, udev);
3480 if (ret == 1)
3481 return 0;
3482 else
3483 return -EINVAL;
3484 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003485
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003486 /* If device is not setup, there is no point in resetting it */
3487 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3488 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3489 SLOT_STATE_DISABLED)
3490 return 0;
3491
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003492 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3493 /* Allocate the command structure that holds the struct completion.
3494 * Assume we're in process context, since the normal device reset
3495 * process has to wait for the device anyway. Storage devices are
3496 * reset as part of error handling, so use GFP_NOIO instead of
3497 * GFP_KERNEL.
3498 */
3499 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
3500 if (!reset_device_cmd) {
3501 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3502 return -ENOMEM;
3503 }
3504
3505 /* Attempt to submit the Reset Device command to the command ring */
3506 spin_lock_irqsave(&xhci->lock, flags);
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08003507
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003508 ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003509 if (ret) {
3510 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003511 spin_unlock_irqrestore(&xhci->lock, flags);
3512 goto command_cleanup;
3513 }
3514 xhci_ring_cmd_db(xhci);
3515 spin_unlock_irqrestore(&xhci->lock, flags);
3516
3517 /* Wait for the Reset Device command to finish */
Mathias Nymanc311e392014-05-08 19:26:03 +03003518 wait_for_completion(reset_device_cmd->completion);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003519
3520 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3521 * unless we tried to reset a slot ID that wasn't enabled,
3522 * or the device wasn't in the addressed or configured state.
3523 */
3524 ret = reset_device_cmd->status;
3525 switch (ret) {
Mathias Nymanc311e392014-05-08 19:26:03 +03003526 case COMP_CMD_ABORT:
3527 case COMP_CMD_STOP:
3528 xhci_warn(xhci, "Timeout waiting for reset device command\n");
3529 ret = -ETIME;
3530 goto command_cleanup;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003531 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
3532 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003533 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n",
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003534 slot_id,
3535 xhci_get_slot_state(xhci, virt_dev->out_ctx));
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003536 xhci_dbg(xhci, "Not freeing device rings.\n");
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003537 /* Don't treat this as an error. May change my mind later. */
3538 ret = 0;
3539 goto command_cleanup;
3540 case COMP_SUCCESS:
3541 xhci_dbg(xhci, "Successful reset device command.\n");
3542 break;
3543 default:
3544 if (xhci_is_vendor_info_code(xhci, ret))
3545 break;
3546 xhci_warn(xhci, "Unknown completion code %u for "
3547 "reset device command.\n", ret);
3548 ret = -EINVAL;
3549 goto command_cleanup;
3550 }
3551
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003552 /* Free up host controller endpoint resources */
3553 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3554 spin_lock_irqsave(&xhci->lock, flags);
3555 /* Don't delete the default control endpoint resources */
3556 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3557 spin_unlock_irqrestore(&xhci->lock, flags);
3558 }
3559
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003560 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
3561 last_freed_endpoint = 1;
3562 for (i = 1; i < 31; ++i) {
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003563 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3564
3565 if (ep->ep_state & EP_HAS_STREAMS) {
Hans de Goededf613832013-10-04 00:29:45 +02003566 xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n",
3567 xhci_get_endpoint_address(i));
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003568 xhci_free_stream_info(xhci, ep->stream_info);
3569 ep->stream_info = NULL;
3570 ep->ep_state &= ~EP_HAS_STREAMS;
3571 }
3572
3573 if (ep->ring) {
3574 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
3575 last_freed_endpoint = i;
3576 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003577 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3578 xhci_drop_ep_from_interval_table(xhci,
3579 &virt_dev->eps[i].bw_info,
3580 virt_dev->bw_table,
3581 udev,
3582 &virt_dev->eps[i],
3583 virt_dev->tt_info);
Sarah Sharp9af5d712011-09-02 11:05:48 -07003584 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003585 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003586 /* If necessary, update the number of active TTs on this root port */
3587 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
3588
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003589 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
3590 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
3591 ret = 0;
3592
3593command_cleanup:
3594 xhci_free_command(xhci, reset_device_cmd);
3595 return ret;
3596}
3597
3598/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003599 * At this point, the struct usb_device is about to go away, the device has
3600 * disconnected, and all traffic has been stopped and the endpoints have been
3601 * disabled. Free any HC data structures associated with that device.
3602 */
3603void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
3604{
3605 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003606 struct xhci_virt_device *virt_dev;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003607 unsigned long flags;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003608 u32 state;
Andiry Xu64927732010-10-14 07:22:45 -07003609 int i, ret;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003610 struct xhci_command *command;
3611
3612 command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
3613 if (!command)
3614 return;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003615
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003616#ifndef CONFIG_USB_DEFAULT_PERSIST
3617 /*
3618 * We called pm_runtime_get_noresume when the device was attached.
3619 * Decrement the counter here to allow controller to runtime suspend
3620 * if no devices remain.
3621 */
3622 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003623 pm_runtime_put_noidle(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003624#endif
3625
Andiry Xu64927732010-10-14 07:22:45 -07003626 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003627 /* If the host is halted due to driver unload, we still need to free the
3628 * device.
3629 */
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003630 if (ret <= 0 && ret != -ENODEV) {
3631 kfree(command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003632 return;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003633 }
Andiry Xu64927732010-10-14 07:22:45 -07003634
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003635 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003636
3637 /* Stop any wayward timer functions (which may grab the lock) */
3638 for (i = 0; i < 31; ++i) {
3639 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
3640 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3641 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003642
3643 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003644 /* Don't disable the slot if the host controller is dead. */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02003645 state = readl(&xhci->op_regs->status);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003646 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3647 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003648 xhci_free_virt_device(xhci, udev->slot_id);
3649 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003650 kfree(command);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003651 return;
3652 }
3653
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003654 if (xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT,
3655 udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003656 spin_unlock_irqrestore(&xhci->lock, flags);
3657 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3658 return;
3659 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003660 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003661 spin_unlock_irqrestore(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003662
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003663 /*
3664 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07003665 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003666 */
3667}
3668
3669/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003670 * Checks if we have enough host controller resources for the default control
3671 * endpoint.
3672 *
3673 * Must be called with xhci->lock held.
3674 */
3675static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3676{
3677 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003678 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3679 "Not enough ep ctxs: "
3680 "%u active, need to add 1, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003681 xhci->num_active_eps, xhci->limit_active_eps);
3682 return -ENOMEM;
3683 }
3684 xhci->num_active_eps += 1;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003685 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3686 "Adding 1 ep ctx, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003687 xhci->num_active_eps);
3688 return 0;
3689}
3690
3691
3692/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003693 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3694 * timed out, or allocating memory failed. Returns 1 on success.
3695 */
3696int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3697{
3698 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3699 unsigned long flags;
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003700 int ret, slot_id;
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003701 struct xhci_command *command;
3702
3703 command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
3704 if (!command)
3705 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003706
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003707 /* xhci->slot_id and xhci->addr_dev are not thread-safe */
3708 mutex_lock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003709 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003710 command->completion = &xhci->addr_dev;
3711 ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003712 if (ret) {
3713 spin_unlock_irqrestore(&xhci->lock, flags);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003714 mutex_unlock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003715 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003716 kfree(command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003717 return 0;
3718 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003719 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003720 spin_unlock_irqrestore(&xhci->lock, flags);
3721
Mathias Nymanc311e392014-05-08 19:26:03 +03003722 wait_for_completion(command->completion);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003723 slot_id = xhci->slot_id;
3724 mutex_unlock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003725
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003726 if (!slot_id || command->status != COMP_SUCCESS) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003727 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharpbe982032014-05-08 19:25:59 +03003728 xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n",
3729 HCS_MAX_SLOTS(
3730 readl(&xhci->cap_regs->hcs_params1)));
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003731 kfree(command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003732 return 0;
3733 }
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003734
3735 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3736 spin_lock_irqsave(&xhci->lock, flags);
3737 ret = xhci_reserve_host_control_ep_resources(xhci);
3738 if (ret) {
3739 spin_unlock_irqrestore(&xhci->lock, flags);
3740 xhci_warn(xhci, "Not enough host resources, "
3741 "active endpoint contexts = %u\n",
3742 xhci->num_active_eps);
3743 goto disable_slot;
3744 }
3745 spin_unlock_irqrestore(&xhci->lock, flags);
3746 }
3747 /* Use GFP_NOIO, since this function can be called from
Sarah Sharpa6d940d2010-12-28 13:08:42 -08003748 * xhci_discover_or_reset_device(), which may be called as part of
3749 * mass storage driver error handling.
3750 */
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003751 if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003752 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003753 goto disable_slot;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003754 }
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003755 udev->slot_id = slot_id;
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003756
3757#ifndef CONFIG_USB_DEFAULT_PERSIST
3758 /*
3759 * If resetting upon resume, we can't put the controller into runtime
3760 * suspend if there is a device attached.
3761 */
3762 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003763 pm_runtime_get_noresume(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003764#endif
3765
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003766
3767 kfree(command);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003768 /* Is this a LS or FS device under a HS hub? */
3769 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003770 return 1;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003771
3772disable_slot:
3773 /* Disable slot, if we can do it without mem alloc */
3774 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003775 command->completion = NULL;
3776 command->status = 0;
3777 if (!xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT,
3778 udev->slot_id))
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003779 xhci_ring_cmd_db(xhci);
3780 spin_unlock_irqrestore(&xhci->lock, flags);
3781 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003782}
3783
3784/*
Dan Williams48fc7db2013-12-05 17:07:27 -08003785 * Issue an Address Device command and optionally send a corresponding
3786 * SetAddress request to the device.
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003787 */
Dan Williams48fc7db2013-12-05 17:07:27 -08003788static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev,
3789 enum xhci_setup_dev setup)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003790{
Dan Williams6f8ffc02013-11-22 01:20:01 -08003791 const char *act = setup == SETUP_CONTEXT_ONLY ? "context" : "address";
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003792 unsigned long flags;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003793 struct xhci_virt_device *virt_dev;
3794 int ret = 0;
3795 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07003796 struct xhci_slot_ctx *slot_ctx;
3797 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003798 u64 temp_64;
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003799 struct xhci_command *command = NULL;
3800
3801 mutex_lock(&xhci->mutex);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003802
Lu Baolua2118d02017-01-03 18:28:44 +02003803 if (xhci->xhc_state) { /* dying, removing or halted */
3804 ret = -ESHUTDOWN;
Roger Quadros448116b2015-09-21 17:46:15 +03003805 goto out;
Lu Baolua2118d02017-01-03 18:28:44 +02003806 }
Roger Quadros448116b2015-09-21 17:46:15 +03003807
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003808 if (!udev->slot_id) {
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003809 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3810 "Bad Slot ID %d", udev->slot_id);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003811 ret = -EINVAL;
3812 goto out;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003813 }
3814
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003815 virt_dev = xhci->devs[udev->slot_id];
3816
Matt Evans7ed603e2011-03-29 13:40:56 +11003817 if (WARN_ON(!virt_dev)) {
3818 /*
3819 * In plug/unplug torture test with an NEC controller,
3820 * a zero-dereference was observed once due to virt_dev = 0.
3821 * Print useful debug rather than crash if it is observed again!
3822 */
3823 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3824 udev->slot_id);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003825 ret = -EINVAL;
3826 goto out;
Matt Evans7ed603e2011-03-29 13:40:56 +11003827 }
3828
Mathias Nymanf161ead2015-01-09 17:18:28 +02003829 if (setup == SETUP_CONTEXT_ONLY) {
3830 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3831 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3832 SLOT_STATE_DEFAULT) {
3833 xhci_dbg(xhci, "Slot already in default state\n");
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003834 goto out;
Mathias Nymanf161ead2015-01-09 17:18:28 +02003835 }
3836 }
3837
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003838 command = xhci_alloc_command(xhci, false, false, GFP_KERNEL);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003839 if (!command) {
3840 ret = -ENOMEM;
3841 goto out;
3842 }
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003843
3844 command->in_ctx = virt_dev->in_ctx;
3845 command->completion = &xhci->addr_dev;
3846
Andiry Xuf0615c42010-10-14 07:22:48 -07003847 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Lin Wang4daf9df2015-01-09 16:06:31 +02003848 ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003849 if (!ctrl_ctx) {
3850 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3851 __func__);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003852 ret = -EINVAL;
3853 goto out;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003854 }
Andiry Xuf0615c42010-10-14 07:22:48 -07003855 /*
3856 * If this is the first Set Address since device plug-in or
3857 * virt_device realloaction after a resume with an xHCI power loss,
3858 * then set up the slot context.
3859 */
3860 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003861 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07003862 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02003863 else
3864 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharpd31c2852011-11-03 13:06:08 -07003865 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3866 ctrl_ctx->drop_flags = 0;
3867
Sarah Sharp66e49d82009-07-27 12:03:46 -07003868 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003869 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003870 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003871 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003872
Sarah Sharpf88ba782009-05-14 11:44:22 -07003873 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003874 ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma,
Dan Williams48fc7db2013-12-05 17:07:27 -08003875 udev->slot_id, setup);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003876 if (ret) {
3877 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003878 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3879 "FIXME: allocate a command ring segment");
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003880 goto out;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003881 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003882 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003883 spin_unlock_irqrestore(&xhci->lock, flags);
3884
3885 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
Mathias Nymanc311e392014-05-08 19:26:03 +03003886 wait_for_completion(command->completion);
3887
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003888 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3889 * the SetAddress() "recovery interval" required by USB and aborting the
3890 * command on a timeout.
3891 */
Mathias Nyman9ea18332014-05-08 19:26:02 +03003892 switch (command->status) {
Mathias Nymanc311e392014-05-08 19:26:03 +03003893 case COMP_CMD_ABORT:
3894 case COMP_CMD_STOP:
3895 xhci_warn(xhci, "Timeout while waiting for setup device command\n");
3896 ret = -ETIME;
3897 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003898 case COMP_CTX_STATE:
3899 case COMP_EBADSLT:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003900 xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n",
3901 act, udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003902 ret = -EINVAL;
3903 break;
3904 case COMP_TX_ERR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003905 dev_warn(&udev->dev, "Device not responding to setup %s.\n", act);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003906 ret = -EPROTO;
3907 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08003908 case COMP_DEV_ERR:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003909 dev_warn(&udev->dev,
3910 "ERROR: Incompatible device for setup %s command\n", act);
Alex Hef6ba6fe2011-06-08 18:34:06 +08003911 ret = -ENODEV;
3912 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003913 case COMP_SUCCESS:
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003914 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
Dan Williams6f8ffc02013-11-22 01:20:01 -08003915 "Successful setup %s command", act);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003916 break;
3917 default:
Dan Williams6f8ffc02013-11-22 01:20:01 -08003918 xhci_err(xhci,
3919 "ERROR: unexpected setup %s command completion code 0x%x.\n",
Mathias Nyman9ea18332014-05-08 19:26:02 +03003920 act, command->status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07003921 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003922 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003923 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003924 ret = -EINVAL;
3925 break;
3926 }
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003927 if (ret)
3928 goto out;
Sarah Sharpf7b2e402014-01-30 13:27:49 -08003929 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003930 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3931 "Op regs DCBAA ptr = %#016llx", temp_64);
3932 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3933 "Slot ID %d dcbaa entry @%p = %#016llx",
3934 udev->slot_id,
3935 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
3936 (unsigned long long)
3937 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
3938 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3939 "Output Context DMA address = %#08llx",
John Yound115b042009-07-27 12:05:15 -07003940 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003941 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003942 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003943 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003944 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003945 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003946 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003947 /*
3948 * USB core uses address 1 for the roothubs, so we add one to the
3949 * address given back to us by the HC.
3950 */
John Yound115b042009-07-27 12:05:15 -07003951 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003952 trace_xhci_address_ctx(xhci, virt_dev->out_ctx,
Xenia Ragiadakou0c052aa2013-11-15 03:18:07 +02003953 le32_to_cpu(slot_ctx->dev_info) >> 27);
Sarah Sharpf94e01862009-04-27 19:58:38 -07003954 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07003955 ctrl_ctx->add_flags = 0;
3956 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003957
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003958 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
Dan Williamsa2cdc342013-10-16 12:25:44 -07003959 "Internal device address = %d",
3960 le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003961out:
3962 mutex_unlock(&xhci->mutex);
Mathias Nymanddba5cd2014-05-08 19:26:00 +03003963 kfree(command);
Chris Bainbridgea00918d2015-05-19 16:30:51 +03003964 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003965}
3966
Dan Williams48fc7db2013-12-05 17:07:27 -08003967int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
3968{
3969 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ADDRESS);
3970}
3971
3972int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev)
3973{
3974 return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ONLY);
3975}
3976
Lan Tianyu3f5eb142013-03-19 16:48:12 +08003977/*
3978 * Transfer the port index into real index in the HW port status
3979 * registers. Caculate offset between the port's PORTSC register
3980 * and port status base. Divide the number of per port register
3981 * to get the real index. The raw port number bases 1.
3982 */
3983int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1)
3984{
3985 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3986 __le32 __iomem *base_addr = &xhci->op_regs->port_status_base;
3987 __le32 __iomem *addr;
3988 int raw_port;
3989
Mathias Nymanb50107b2015-10-01 18:40:38 +03003990 if (hcd->speed < HCD_USB3)
Lan Tianyu3f5eb142013-03-19 16:48:12 +08003991 addr = xhci->usb2_ports[port1 - 1];
3992 else
3993 addr = xhci->usb3_ports[port1 - 1];
3994
3995 raw_port = (addr - base_addr)/NUM_PORT_REGS + 1;
3996 return raw_port;
3997}
3998
Mathias Nymana558ccd2013-05-23 17:14:30 +03003999/*
4000 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
4001 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
4002 */
Olof Johanssond5c82fe2013-07-23 11:58:20 -07004003static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
Mathias Nymana558ccd2013-05-23 17:14:30 +03004004 struct usb_device *udev, u16 max_exit_latency)
4005{
4006 struct xhci_virt_device *virt_dev;
4007 struct xhci_command *command;
4008 struct xhci_input_control_ctx *ctrl_ctx;
4009 struct xhci_slot_ctx *slot_ctx;
4010 unsigned long flags;
4011 int ret;
4012
4013 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nyman96044692014-09-11 13:55:50 +03004014
4015 virt_dev = xhci->devs[udev->slot_id];
4016
4017 /*
4018 * virt_dev might not exists yet if xHC resumed from hibernate (S4) and
4019 * xHC was re-initialized. Exit latency will be set later after
4020 * hub_port_finish_reset() is done and xhci->devs[] are re-allocated
4021 */
4022
4023 if (!virt_dev || max_exit_latency == virt_dev->current_mel) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004024 spin_unlock_irqrestore(&xhci->lock, flags);
4025 return 0;
4026 }
4027
4028 /* Attempt to issue an Evaluate Context command to change the MEL. */
Mathias Nymana558ccd2013-05-23 17:14:30 +03004029 command = xhci->lpm_command;
Lin Wang4daf9df2015-01-09 16:06:31 +02004030 ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07004031 if (!ctrl_ctx) {
4032 spin_unlock_irqrestore(&xhci->lock, flags);
4033 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4034 __func__);
4035 return -ENOMEM;
4036 }
4037
Mathias Nymana558ccd2013-05-23 17:14:30 +03004038 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
4039 spin_unlock_irqrestore(&xhci->lock, flags);
4040
Mathias Nymana558ccd2013-05-23 17:14:30 +03004041 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
4042 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
4043 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
4044 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
Mathias Nyman4801d4ea2014-11-27 18:19:15 +02004045 slot_ctx->dev_state = 0;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004046
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03004047 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
4048 "Set up evaluate context for LPM MEL change.");
Mathias Nymana558ccd2013-05-23 17:14:30 +03004049 xhci_dbg(xhci, "Slot %u Input Context:\n", udev->slot_id);
4050 xhci_dbg_ctx(xhci, command->in_ctx, 0);
4051
4052 /* Issue and wait for the evaluate context command. */
4053 ret = xhci_configure_endpoint(xhci, udev, command,
4054 true, true);
4055 xhci_dbg(xhci, "Slot %u Output Context:\n", udev->slot_id);
4056 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 0);
4057
4058 if (!ret) {
4059 spin_lock_irqsave(&xhci->lock, flags);
4060 virt_dev->current_mel = max_exit_latency;
4061 spin_unlock_irqrestore(&xhci->lock, flags);
4062 }
4063 return ret;
4064}
4065
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004066#ifdef CONFIG_PM
Andiry Xu95743232011-09-23 14:19:51 -07004067
4068/* BESL to HIRD Encoding array for USB2 LPM */
4069static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
4070 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
4071
4072/* Calculate HIRD/BESL for USB2 PORTPMSC*/
Andiry Xuf99298b2011-12-12 16:45:28 +08004073static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
4074 struct usb_device *udev)
Andiry Xu95743232011-09-23 14:19:51 -07004075{
Andiry Xuf99298b2011-12-12 16:45:28 +08004076 int u2del, besl, besl_host;
4077 int besl_device = 0;
4078 u32 field;
Andiry Xu95743232011-09-23 14:19:51 -07004079
Andiry Xuf99298b2011-12-12 16:45:28 +08004080 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
4081 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4082
4083 if (field & USB_BESL_SUPPORT) {
4084 for (besl_host = 0; besl_host < 16; besl_host++) {
4085 if (xhci_besl_encoding[besl_host] >= u2del)
Andiry Xu95743232011-09-23 14:19:51 -07004086 break;
4087 }
Andiry Xuf99298b2011-12-12 16:45:28 +08004088 /* Use baseline BESL value as default */
4089 if (field & USB_BESL_BASELINE_VALID)
4090 besl_device = USB_GET_BESL_BASELINE(field);
4091 else if (field & USB_BESL_DEEP_VALID)
4092 besl_device = USB_GET_BESL_DEEP(field);
Andiry Xu95743232011-09-23 14:19:51 -07004093 } else {
4094 if (u2del <= 50)
Andiry Xuf99298b2011-12-12 16:45:28 +08004095 besl_host = 0;
Andiry Xu95743232011-09-23 14:19:51 -07004096 else
Andiry Xuf99298b2011-12-12 16:45:28 +08004097 besl_host = (u2del - 51) / 75 + 1;
Andiry Xu95743232011-09-23 14:19:51 -07004098 }
4099
Andiry Xuf99298b2011-12-12 16:45:28 +08004100 besl = besl_host + besl_device;
4101 if (besl > 15)
4102 besl = 15;
4103
4104 return besl;
Andiry Xu95743232011-09-23 14:19:51 -07004105}
4106
Mathias Nymana558ccd2013-05-23 17:14:30 +03004107/* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
4108static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev)
4109{
4110 u32 field;
4111 int l1;
4112 int besld = 0;
4113 int hirdm = 0;
4114
4115 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4116
4117 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
Mathias Nyman17f34862013-05-23 17:14:31 +03004118 l1 = udev->l1_params.timeout / 256;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004119
4120 /* device has preferred BESLD */
4121 if (field & USB_BESL_DEEP_VALID) {
4122 besld = USB_GET_BESL_DEEP(field);
4123 hirdm = 1;
4124 }
4125
4126 return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm);
4127}
4128
Andiry Xu65580b432011-09-23 14:19:52 -07004129int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4130 struct usb_device *udev, int enable)
4131{
4132 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4133 __le32 __iomem **port_array;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004134 __le32 __iomem *pm_addr, *hlpm_addr;
4135 u32 pm_val, hlpm_val, field;
Andiry Xu65580b432011-09-23 14:19:52 -07004136 unsigned int port_num;
4137 unsigned long flags;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004138 int hird, exit_latency;
4139 int ret;
Andiry Xu65580b432011-09-23 14:19:52 -07004140
Mathias Nymanb50107b2015-10-01 18:40:38 +03004141 if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support ||
Andiry Xu65580b432011-09-23 14:19:52 -07004142 !udev->lpm_capable)
4143 return -EPERM;
4144
4145 if (!udev->parent || udev->parent->parent ||
4146 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4147 return -EPERM;
4148
4149 if (udev->usb2_hw_lpm_capable != 1)
4150 return -EPERM;
4151
4152 spin_lock_irqsave(&xhci->lock, flags);
4153
4154 port_array = xhci->usb2_ports;
4155 port_num = udev->portnum - 1;
Mathias Nymanb6e76372013-05-23 17:14:29 +03004156 pm_addr = port_array[port_num] + PORTPMSC;
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004157 pm_val = readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004158 hlpm_addr = port_array[port_num] + PORTHLPMC;
4159 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
Andiry Xu65580b432011-09-23 14:19:52 -07004160
4161 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
Lin Wang654a55d2014-05-08 19:25:54 +03004162 enable ? "enable" : "disable", port_num + 1);
Andiry Xu65580b432011-09-23 14:19:52 -07004163
Andiry Xu65580b432011-09-23 14:19:52 -07004164 if (enable) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004165 /* Host supports BESL timeout instead of HIRD */
4166 if (udev->usb2_hw_lpm_besl_capable) {
4167 /* if device doesn't have a preferred BESL value use a
4168 * default one which works with mixed HIRD and BESL
4169 * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4170 */
4171 if ((field & USB_BESL_SUPPORT) &&
4172 (field & USB_BESL_BASELINE_VALID))
4173 hird = USB_GET_BESL_BASELINE(field);
4174 else
Mathias Nyman17f34862013-05-23 17:14:31 +03004175 hird = udev->l1_params.besl;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004176
4177 exit_latency = xhci_besl_encoding[hird];
4178 spin_unlock_irqrestore(&xhci->lock, flags);
4179
4180 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4181 * input context for link powermanagement evaluate
4182 * context commands. It is protected by hcd->bandwidth
4183 * mutex and is shared by all devices. We need to set
4184 * the max ext latency in USB 2 BESL LPM as well, so
4185 * use the same mutex and xhci_change_max_exit_latency()
4186 */
4187 mutex_lock(hcd->bandwidth_mutex);
4188 ret = xhci_change_max_exit_latency(xhci, udev,
4189 exit_latency);
4190 mutex_unlock(hcd->bandwidth_mutex);
4191
4192 if (ret < 0)
4193 return ret;
4194 spin_lock_irqsave(&xhci->lock, flags);
4195
4196 hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004197 writel(hlpm_val, hlpm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004198 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004199 readl(hlpm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004200 } else {
4201 hird = xhci_calculate_hird_besl(xhci, udev);
4202 }
4203
4204 pm_val &= ~PORT_HIRD_MASK;
Sarah Sharp58e21f72013-10-07 17:17:20 -07004205 pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004206 writel(pm_val, pm_addr);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004207 pm_val = readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004208 pm_val |= PORT_HLE;
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004209 writel(pm_val, pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004210 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004211 readl(pm_addr);
Andiry Xu65580b432011-09-23 14:19:52 -07004212 } else {
Sarah Sharp58e21f72013-10-07 17:17:20 -07004213 pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK | PORT_L1DS_MASK);
Xenia Ragiadakou204b7792013-11-15 05:34:07 +02004214 writel(pm_val, pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004215 /* flush write */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004216 readl(pm_addr);
Mathias Nymana558ccd2013-05-23 17:14:30 +03004217 if (udev->usb2_hw_lpm_besl_capable) {
4218 spin_unlock_irqrestore(&xhci->lock, flags);
4219 mutex_lock(hcd->bandwidth_mutex);
4220 xhci_change_max_exit_latency(xhci, udev, 0);
4221 mutex_unlock(hcd->bandwidth_mutex);
4222 return 0;
4223 }
Andiry Xu65580b432011-09-23 14:19:52 -07004224 }
4225
4226 spin_unlock_irqrestore(&xhci->lock, flags);
4227 return 0;
4228}
4229
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004230/* check if a usb2 port supports a given extened capability protocol
4231 * only USB2 ports extended protocol capability values are cached.
4232 * Return 1 if capability is supported
4233 */
4234static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port,
4235 unsigned capability)
4236{
4237 u32 port_offset, port_count;
4238 int i;
4239
4240 for (i = 0; i < xhci->num_ext_caps; i++) {
4241 if (xhci->ext_caps[i] & capability) {
4242 /* port offsets starts at 1 */
4243 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1;
4244 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]);
4245 if (port >= port_offset &&
4246 port < port_offset + port_count)
4247 return 1;
4248 }
4249 }
4250 return 0;
4251}
4252
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004253int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4254{
4255 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004256 int portnum = udev->portnum - 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004257
Mathias Nymanb50107b2015-10-01 18:40:38 +03004258 if (hcd->speed >= HCD_USB3 || !xhci->sw_lpm_support ||
Sarah Sharpde68bab2013-09-30 17:26:28 +03004259 !udev->lpm_capable)
4260 return 0;
4261
4262 /* we only support lpm for non-hub device connected to root hub yet */
4263 if (!udev->parent || udev->parent->parent ||
4264 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4265 return 0;
4266
4267 if (xhci->hw_lpm_support == 1 &&
4268 xhci_check_usb2_port_capability(
4269 xhci, portnum, XHCI_HLC)) {
4270 udev->usb2_hw_lpm_capable = 1;
4271 udev->l1_params.timeout = XHCI_L1_TIMEOUT;
4272 udev->l1_params.besl = XHCI_DEFAULT_BESL;
4273 if (xhci_check_usb2_port_capability(xhci, portnum,
4274 XHCI_BLC))
4275 udev->usb2_hw_lpm_besl_capable = 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004276 }
4277
4278 return 0;
4279}
4280
Sarah Sharp3b3db022012-05-09 10:55:03 -07004281/*---------------------- USB 3.0 Link PM functions ------------------------*/
4282
Sarah Sharpe3567d22012-05-16 13:36:24 -07004283/* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4284static unsigned long long xhci_service_interval_to_ns(
4285 struct usb_endpoint_descriptor *desc)
4286{
Oliver Neukum16b45fd2012-10-17 10:16:16 +02004287 return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004288}
4289
Sarah Sharp3b3db022012-05-09 10:55:03 -07004290static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
4291 enum usb3_link_state state)
4292{
4293 unsigned long long sel;
4294 unsigned long long pel;
4295 unsigned int max_sel_pel;
4296 char *state_name;
4297
4298 switch (state) {
4299 case USB3_LPM_U1:
4300 /* Convert SEL and PEL stored in nanoseconds to microseconds */
4301 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
4302 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
4303 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
4304 state_name = "U1";
4305 break;
4306 case USB3_LPM_U2:
4307 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
4308 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
4309 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
4310 state_name = "U2";
4311 break;
4312 default:
4313 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
4314 __func__);
Sarah Sharpe25e62a2012-06-07 11:10:32 -07004315 return USB3_LPM_DISABLED;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004316 }
4317
4318 if (sel <= max_sel_pel && pel <= max_sel_pel)
4319 return USB3_LPM_DEVICE_INITIATED;
4320
4321 if (sel > max_sel_pel)
4322 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4323 "due to long SEL %llu ms\n",
4324 state_name, sel);
4325 else
4326 dev_dbg(&udev->dev, "Device-initiated %s disabled "
Joe Perches03e64e92013-07-16 19:25:59 -07004327 "due to long PEL %llu ms\n",
Sarah Sharp3b3db022012-05-09 10:55:03 -07004328 state_name, pel);
4329 return USB3_LPM_DISABLED;
4330}
4331
Pratyush Anand9502c462014-07-04 17:01:23 +03004332/* The U1 timeout should be the maximum of the following values:
Sarah Sharpe3567d22012-05-16 13:36:24 -07004333 * - For control endpoints, U1 system exit latency (SEL) * 3
4334 * - For bulk endpoints, U1 SEL * 5
4335 * - For interrupt endpoints:
4336 * - Notification EPs, U1 SEL * 3
4337 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4338 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4339 */
Pratyush Anand9502c462014-07-04 17:01:23 +03004340static unsigned long long xhci_calculate_intel_u1_timeout(
4341 struct usb_device *udev,
Sarah Sharpe3567d22012-05-16 13:36:24 -07004342 struct usb_endpoint_descriptor *desc)
4343{
4344 unsigned long long timeout_ns;
4345 int ep_type;
4346 int intr_type;
4347
4348 ep_type = usb_endpoint_type(desc);
4349 switch (ep_type) {
4350 case USB_ENDPOINT_XFER_CONTROL:
4351 timeout_ns = udev->u1_params.sel * 3;
4352 break;
4353 case USB_ENDPOINT_XFER_BULK:
4354 timeout_ns = udev->u1_params.sel * 5;
4355 break;
4356 case USB_ENDPOINT_XFER_INT:
4357 intr_type = usb_endpoint_interrupt_type(desc);
4358 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
4359 timeout_ns = udev->u1_params.sel * 3;
4360 break;
4361 }
4362 /* Otherwise the calculation is the same as isoc eps */
4363 case USB_ENDPOINT_XFER_ISOC:
4364 timeout_ns = xhci_service_interval_to_ns(desc);
Sarah Sharpc88db162012-05-21 08:44:33 -07004365 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004366 if (timeout_ns < udev->u1_params.sel * 2)
4367 timeout_ns = udev->u1_params.sel * 2;
4368 break;
4369 default:
4370 return 0;
4371 }
4372
Pratyush Anand9502c462014-07-04 17:01:23 +03004373 return timeout_ns;
4374}
4375
4376/* Returns the hub-encoded U1 timeout value. */
4377static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci,
4378 struct usb_device *udev,
4379 struct usb_endpoint_descriptor *desc)
4380{
4381 unsigned long long timeout_ns;
4382
4383 if (xhci->quirks & XHCI_INTEL_HOST)
4384 timeout_ns = xhci_calculate_intel_u1_timeout(udev, desc);
4385 else
4386 timeout_ns = udev->u1_params.sel;
4387
4388 /* The U1 timeout is encoded in 1us intervals.
4389 * Don't return a timeout of zero, because that's USB3_LPM_DISABLED.
4390 */
Sarah Sharpe3567d22012-05-16 13:36:24 -07004391 if (timeout_ns == USB3_LPM_DISABLED)
Pratyush Anand9502c462014-07-04 17:01:23 +03004392 timeout_ns = 1;
4393 else
4394 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004395
4396 /* If the necessary timeout value is bigger than what we can set in the
4397 * USB 3.0 hub, we have to disable hub-initiated U1.
4398 */
4399 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
4400 return timeout_ns;
4401 dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
4402 "due to long timeout %llu ms\n", timeout_ns);
4403 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
4404}
4405
Pratyush Anand9502c462014-07-04 17:01:23 +03004406/* The U2 timeout should be the maximum of:
Sarah Sharpe3567d22012-05-16 13:36:24 -07004407 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4408 * - largest bInterval of any active periodic endpoint (to avoid going
4409 * into lower power link states between intervals).
4410 * - the U2 Exit Latency of the device
4411 */
Pratyush Anand9502c462014-07-04 17:01:23 +03004412static unsigned long long xhci_calculate_intel_u2_timeout(
4413 struct usb_device *udev,
Sarah Sharpe3567d22012-05-16 13:36:24 -07004414 struct usb_endpoint_descriptor *desc)
4415{
4416 unsigned long long timeout_ns;
4417 unsigned long long u2_del_ns;
4418
4419 timeout_ns = 10 * 1000 * 1000;
4420
4421 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
4422 (xhci_service_interval_to_ns(desc) > timeout_ns))
4423 timeout_ns = xhci_service_interval_to_ns(desc);
4424
Oliver Neukum966e7a82012-10-17 12:17:50 +02004425 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004426 if (u2_del_ns > timeout_ns)
4427 timeout_ns = u2_del_ns;
4428
Pratyush Anand9502c462014-07-04 17:01:23 +03004429 return timeout_ns;
4430}
4431
4432/* Returns the hub-encoded U2 timeout value. */
4433static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci,
4434 struct usb_device *udev,
4435 struct usb_endpoint_descriptor *desc)
4436{
4437 unsigned long long timeout_ns;
4438
4439 if (xhci->quirks & XHCI_INTEL_HOST)
4440 timeout_ns = xhci_calculate_intel_u2_timeout(udev, desc);
4441 else
4442 timeout_ns = udev->u2_params.sel;
4443
Sarah Sharpe3567d22012-05-16 13:36:24 -07004444 /* The U2 timeout is encoded in 256us intervals */
Sarah Sharpc88db162012-05-21 08:44:33 -07004445 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004446 /* If the necessary timeout value is bigger than what we can set in the
4447 * USB 3.0 hub, we have to disable hub-initiated U2.
4448 */
4449 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
4450 return timeout_ns;
4451 dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
4452 "due to long timeout %llu ms\n", timeout_ns);
4453 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
4454}
4455
Sarah Sharp3b3db022012-05-09 10:55:03 -07004456static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4457 struct usb_device *udev,
4458 struct usb_endpoint_descriptor *desc,
4459 enum usb3_link_state state,
4460 u16 *timeout)
4461{
Pratyush Anand9502c462014-07-04 17:01:23 +03004462 if (state == USB3_LPM_U1)
4463 return xhci_calculate_u1_timeout(xhci, udev, desc);
4464 else if (state == USB3_LPM_U2)
4465 return xhci_calculate_u2_timeout(xhci, udev, desc);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004466
Sarah Sharp3b3db022012-05-09 10:55:03 -07004467 return USB3_LPM_DISABLED;
4468}
4469
4470static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4471 struct usb_device *udev,
4472 struct usb_endpoint_descriptor *desc,
4473 enum usb3_link_state state,
4474 u16 *timeout)
4475{
4476 u16 alt_timeout;
4477
4478 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
4479 desc, state, timeout);
4480
4481 /* If we found we can't enable hub-initiated LPM, or
4482 * the U1 or U2 exit latency was too high to allow
4483 * device-initiated LPM as well, just stop searching.
4484 */
4485 if (alt_timeout == USB3_LPM_DISABLED ||
4486 alt_timeout == USB3_LPM_DEVICE_INITIATED) {
4487 *timeout = alt_timeout;
4488 return -E2BIG;
4489 }
4490 if (alt_timeout > *timeout)
4491 *timeout = alt_timeout;
4492 return 0;
4493}
4494
4495static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
4496 struct usb_device *udev,
4497 struct usb_host_interface *alt,
4498 enum usb3_link_state state,
4499 u16 *timeout)
4500{
4501 int j;
4502
4503 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
4504 if (xhci_update_timeout_for_endpoint(xhci, udev,
4505 &alt->endpoint[j].desc, state, timeout))
4506 return -E2BIG;
4507 continue;
4508 }
4509 return 0;
4510}
4511
Sarah Sharpe3567d22012-05-16 13:36:24 -07004512static int xhci_check_intel_tier_policy(struct usb_device *udev,
4513 enum usb3_link_state state)
4514{
4515 struct usb_device *parent;
4516 unsigned int num_hubs;
4517
4518 if (state == USB3_LPM_U2)
4519 return 0;
4520
4521 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4522 for (parent = udev->parent, num_hubs = 0; parent->parent;
4523 parent = parent->parent)
4524 num_hubs++;
4525
4526 if (num_hubs < 2)
4527 return 0;
4528
4529 dev_dbg(&udev->dev, "Disabling U1 link state for device"
4530 " below second-tier hub.\n");
4531 dev_dbg(&udev->dev, "Plug device into first-tier hub "
4532 "to decrease power consumption.\n");
4533 return -E2BIG;
4534}
4535
Sarah Sharp3b3db022012-05-09 10:55:03 -07004536static int xhci_check_tier_policy(struct xhci_hcd *xhci,
4537 struct usb_device *udev,
4538 enum usb3_link_state state)
4539{
Sarah Sharpe3567d22012-05-16 13:36:24 -07004540 if (xhci->quirks & XHCI_INTEL_HOST)
4541 return xhci_check_intel_tier_policy(udev, state);
Pratyush Anand9502c462014-07-04 17:01:23 +03004542 else
4543 return 0;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004544}
4545
4546/* Returns the U1 or U2 timeout that should be enabled.
4547 * If the tier check or timeout setting functions return with a non-zero exit
4548 * code, that means the timeout value has been finalized and we shouldn't look
4549 * at any more endpoints.
4550 */
4551static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
4552 struct usb_device *udev, enum usb3_link_state state)
4553{
4554 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4555 struct usb_host_config *config;
4556 char *state_name;
4557 int i;
4558 u16 timeout = USB3_LPM_DISABLED;
4559
4560 if (state == USB3_LPM_U1)
4561 state_name = "U1";
4562 else if (state == USB3_LPM_U2)
4563 state_name = "U2";
4564 else {
4565 dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
4566 state);
4567 return timeout;
4568 }
4569
4570 if (xhci_check_tier_policy(xhci, udev, state) < 0)
4571 return timeout;
4572
4573 /* Gather some information about the currently installed configuration
4574 * and alternate interface settings.
4575 */
4576 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
4577 state, &timeout))
4578 return timeout;
4579
4580 config = udev->actconfig;
4581 if (!config)
4582 return timeout;
4583
Xenia Ragiadakou64ba4192013-08-26 23:29:46 +03004584 for (i = 0; i < config->desc.bNumInterfaces; i++) {
Sarah Sharp3b3db022012-05-09 10:55:03 -07004585 struct usb_driver *driver;
4586 struct usb_interface *intf = config->interface[i];
4587
4588 if (!intf)
4589 continue;
4590
4591 /* Check if any currently bound drivers want hub-initiated LPM
4592 * disabled.
4593 */
4594 if (intf->dev.driver) {
4595 driver = to_usb_driver(intf->dev.driver);
4596 if (driver && driver->disable_hub_initiated_lpm) {
4597 dev_dbg(&udev->dev, "Hub-initiated %s disabled "
4598 "at request of driver %s\n",
4599 state_name, driver->name);
4600 return xhci_get_timeout_no_hub_lpm(udev, state);
4601 }
4602 }
4603
4604 /* Not sure how this could happen... */
4605 if (!intf->cur_altsetting)
4606 continue;
4607
4608 if (xhci_update_timeout_for_interface(xhci, udev,
4609 intf->cur_altsetting,
4610 state, &timeout))
4611 return timeout;
4612 }
4613 return timeout;
4614}
4615
Sarah Sharp3b3db022012-05-09 10:55:03 -07004616static int calculate_max_exit_latency(struct usb_device *udev,
4617 enum usb3_link_state state_changed,
4618 u16 hub_encoded_timeout)
4619{
4620 unsigned long long u1_mel_us = 0;
4621 unsigned long long u2_mel_us = 0;
4622 unsigned long long mel_us = 0;
4623 bool disabling_u1;
4624 bool disabling_u2;
4625 bool enabling_u1;
4626 bool enabling_u2;
4627
4628 disabling_u1 = (state_changed == USB3_LPM_U1 &&
4629 hub_encoded_timeout == USB3_LPM_DISABLED);
4630 disabling_u2 = (state_changed == USB3_LPM_U2 &&
4631 hub_encoded_timeout == USB3_LPM_DISABLED);
4632
4633 enabling_u1 = (state_changed == USB3_LPM_U1 &&
4634 hub_encoded_timeout != USB3_LPM_DISABLED);
4635 enabling_u2 = (state_changed == USB3_LPM_U2 &&
4636 hub_encoded_timeout != USB3_LPM_DISABLED);
4637
4638 /* If U1 was already enabled and we're not disabling it,
4639 * or we're going to enable U1, account for the U1 max exit latency.
4640 */
4641 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
4642 enabling_u1)
4643 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
4644 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
4645 enabling_u2)
4646 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
4647
4648 if (u1_mel_us > u2_mel_us)
4649 mel_us = u1_mel_us;
4650 else
4651 mel_us = u2_mel_us;
4652 /* xHCI host controller max exit latency field is only 16 bits wide. */
4653 if (mel_us > MAX_EXIT) {
4654 dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
4655 "is too big.\n", mel_us);
4656 return -E2BIG;
4657 }
4658 return mel_us;
4659}
4660
4661/* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
4662int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4663 struct usb_device *udev, enum usb3_link_state state)
4664{
4665 struct xhci_hcd *xhci;
4666 u16 hub_encoded_timeout;
4667 int mel;
4668 int ret;
4669
4670 xhci = hcd_to_xhci(hcd);
4671 /* The LPM timeout values are pretty host-controller specific, so don't
4672 * enable hub-initiated timeouts unless the vendor has provided
4673 * information about their timeout algorithm.
4674 */
4675 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4676 !xhci->devs[udev->slot_id])
4677 return USB3_LPM_DISABLED;
4678
4679 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
4680 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
4681 if (mel < 0) {
4682 /* Max Exit Latency is too big, disable LPM. */
4683 hub_encoded_timeout = USB3_LPM_DISABLED;
4684 mel = 0;
4685 }
4686
4687 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4688 if (ret)
4689 return ret;
4690 return hub_encoded_timeout;
4691}
4692
4693int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4694 struct usb_device *udev, enum usb3_link_state state)
4695{
4696 struct xhci_hcd *xhci;
4697 u16 mel;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004698
4699 xhci = hcd_to_xhci(hcd);
4700 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4701 !xhci->devs[udev->slot_id])
4702 return 0;
4703
4704 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
Saurabh Karajgaonkarf1cda542015-08-04 14:04:09 +00004705 return xhci_change_max_exit_latency(xhci, udev, mel);
Sarah Sharp3b3db022012-05-09 10:55:03 -07004706}
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004707#else /* CONFIG_PM */
4708
Rafael J. Wysockiceb6c9c2014-11-29 23:47:05 +01004709int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4710 struct usb_device *udev, int enable)
4711{
4712 return 0;
4713}
4714
4715int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4716{
4717 return 0;
4718}
4719
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004720int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4721 struct usb_device *udev, enum usb3_link_state state)
4722{
4723 return USB3_LPM_DISABLED;
4724}
4725
4726int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4727 struct usb_device *udev, enum usb3_link_state state)
4728{
4729 return 0;
4730}
4731#endif /* CONFIG_PM */
4732
Sarah Sharp3b3db022012-05-09 10:55:03 -07004733/*-------------------------------------------------------------------------*/
4734
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004735/* Once a hub descriptor is fetched for a device, we need to update the xHC's
4736 * internal data structures for the device.
4737 */
4738int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
4739 struct usb_tt *tt, gfp_t mem_flags)
4740{
4741 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4742 struct xhci_virt_device *vdev;
4743 struct xhci_command *config_cmd;
4744 struct xhci_input_control_ctx *ctrl_ctx;
4745 struct xhci_slot_ctx *slot_ctx;
4746 unsigned long flags;
4747 unsigned think_time;
4748 int ret;
4749
4750 /* Ignore root hubs */
4751 if (!hdev->parent)
4752 return 0;
4753
4754 vdev = xhci->devs[hdev->slot_id];
4755 if (!vdev) {
4756 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4757 return -EINVAL;
4758 }
Sarah Sharpa1d78c12009-12-09 15:59:03 -08004759 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004760 if (!config_cmd) {
4761 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
4762 return -ENOMEM;
4763 }
Lin Wang4daf9df2015-01-09 16:06:31 +02004764 ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07004765 if (!ctrl_ctx) {
4766 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4767 __func__);
4768 xhci_free_command(xhci, config_cmd);
4769 return -ENOMEM;
4770 }
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004771
4772 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp839c8172011-09-02 11:05:47 -07004773 if (hdev->speed == USB_SPEED_HIGH &&
4774 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4775 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4776 xhci_free_command(xhci, config_cmd);
4777 spin_unlock_irqrestore(&xhci->lock, flags);
4778 return -ENOMEM;
4779 }
4780
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004781 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004782 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004783 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004784 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
Chunfeng Yun096b1102015-12-04 15:53:43 +02004785 /*
4786 * refer to section 6.2.2: MTT should be 0 for full speed hub,
4787 * but it may be already set to 1 when setup an xHCI virtual
4788 * device, so clear it anyway.
4789 */
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004790 if (tt->multi)
Matt Evans28ccd292011-03-29 13:40:46 +11004791 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Chunfeng Yun096b1102015-12-04 15:53:43 +02004792 else if (hdev->speed == USB_SPEED_FULL)
4793 slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
4794
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004795 if (xhci->hci_version > 0x95) {
4796 xhci_dbg(xhci, "xHCI version %x needs hub "
4797 "TT think time and number of ports\n",
4798 (unsigned int) xhci->hci_version);
Matt Evans28ccd292011-03-29 13:40:46 +11004799 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004800 /* Set TT think time - convert from ns to FS bit times.
4801 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4802 * 2 = 24 FS bit times, 3 = 32 FS bit times.
Andiry Xu700b4172011-05-05 18:14:05 +08004803 *
4804 * xHCI 1.0: this field shall be 0 if the device is not a
4805 * High-spped hub.
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004806 */
4807 think_time = tt->think_time;
4808 if (think_time != 0)
4809 think_time = (think_time / 666) - 1;
Andiry Xu700b4172011-05-05 18:14:05 +08004810 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4811 slot_ctx->tt_info |=
4812 cpu_to_le32(TT_THINK_TIME(think_time));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004813 } else {
4814 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4815 "TT think time or number of ports\n",
4816 (unsigned int) xhci->hci_version);
4817 }
4818 slot_ctx->dev_state = 0;
4819 spin_unlock_irqrestore(&xhci->lock, flags);
4820
4821 xhci_dbg(xhci, "Set up %s for hub device.\n",
4822 (xhci->hci_version > 0x95) ?
4823 "configure endpoint" : "evaluate context");
4824 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
4825 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
4826
4827 /* Issue and wait for the configure endpoint or
4828 * evaluate context command.
4829 */
4830 if (xhci->hci_version > 0x95)
4831 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4832 false, false);
4833 else
4834 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4835 true, false);
4836
4837 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
4838 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
4839
4840 xhci_free_command(xhci, config_cmd);
4841 return ret;
4842}
4843
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004844int xhci_get_frame(struct usb_hcd *hcd)
4845{
4846 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4847 /* EHCI mods by the periodic size. Why? */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004848 return readl(&xhci->run_regs->microframe_index) >> 3;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004849}
4850
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004851int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4852{
4853 struct xhci_hcd *xhci;
Arnd Bergmanna9ff9112017-03-13 10:18:44 +08004854 /*
4855 * TODO: Check with DWC3 clients for sysdev according to
4856 * quirks
4857 */
4858 struct device *dev = hcd->self.sysdev;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004859 int retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004860
Sarah Sharp1386ff72014-01-31 11:45:02 -08004861 /* Accept arbitrarily long scatter-gather lists */
4862 hcd->self.sg_tablesize = ~0;
Ming Leifc760512013-08-08 21:48:23 +08004863
Mathias Nymane2ed5112014-03-07 17:06:57 +02004864 /* support to build packet from discontinuous buffers */
4865 hcd->self.no_sg_constraint = 1;
4866
Hans de Goede19181bc2012-07-04 09:18:02 +02004867 /* XHCI controllers don't stop the ep queue on short packets :| */
4868 hcd->self.no_stop_on_short = 1;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004869
Mathias Nymanb50107b2015-10-01 18:40:38 +03004870 xhci = hcd_to_xhci(hcd);
4871
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004872 if (usb_hcd_is_primary_hcd(hcd)) {
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004873 xhci->main_hcd = hcd;
4874 /* Mark the first roothub as being USB 2.0.
4875 * The xHCI driver will register the USB 3.0 roothub.
4876 */
4877 hcd->speed = HCD_USB2;
4878 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4879 /*
4880 * USB 2.0 roothub under xHCI has an integrated TT,
4881 * (rate matching hub) as opposed to having an OHCI/UHCI
4882 * companion controller.
4883 */
4884 hcd->has_tt = 1;
4885 } else {
Mathias Nymandc1858a2017-10-06 17:45:27 +03004886 /* Some 3.1 hosts return sbrn 0x30, can't rely on sbrn alone */
4887 if (xhci->sbrn == 0x31 || xhci->usb3_rhub.min_rev >= 1) {
Mathias Nymanb50107b2015-10-01 18:40:38 +03004888 xhci_info(xhci, "Host supports USB 3.1 Enhanced SuperSpeed\n");
4889 hcd->speed = HCD_USB31;
Mathias Nyman2c0e06f2016-01-25 15:30:45 +02004890 hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS;
Mathias Nymanb50107b2015-10-01 18:40:38 +03004891 }
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004892 /* xHCI private pointer was set in xhci_pci_probe for the second
4893 * registered roothub.
4894 */
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004895 return 0;
4896 }
4897
Chris Bainbridgea00918d2015-05-19 16:30:51 +03004898 mutex_init(&xhci->mutex);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004899 xhci->cap_regs = hcd->regs;
4900 xhci->op_regs = hcd->regs +
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004901 HC_LENGTH(readl(&xhci->cap_regs->hc_capbase));
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004902 xhci->run_regs = hcd->regs +
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004903 (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004904 /* Cache read-only capability registers */
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004905 xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1);
4906 xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2);
4907 xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3);
4908 xhci->hcc_params = readl(&xhci->cap_regs->hc_capbase);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004909 xhci->hci_version = HC_VERSION(xhci->hcc_params);
Xenia Ragiadakoub0ba9722013-11-15 05:34:06 +02004910 xhci->hcc_params = readl(&xhci->cap_regs->hcc_params);
Lu Baolu04abb6d2015-10-01 18:40:31 +03004911 if (xhci->hci_version > 0x100)
4912 xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004913 xhci_print_registers(xhci);
4914
Mathias Nyman757de492016-06-01 18:09:10 +03004915 xhci->quirks |= quirks;
Takashi Iwai4e6a1ee2013-12-09 12:42:48 +01004916
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004917 get_quirks(dev, xhci);
4918
George Cherian07f3cb72013-07-01 10:59:12 +05304919 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4920 * success event after a short transfer. This quirk will ignore such
4921 * spurious event.
4922 */
4923 if (xhci->hci_version > 0x96)
4924 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
4925
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004926 /* Make sure the HC is halted. */
4927 retval = xhci_halt(xhci);
4928 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004929 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004930
4931 xhci_dbg(xhci, "Resetting HCD\n");
4932 /* Reset the internal HC memory state and registers. */
4933 retval = xhci_reset(xhci);
4934 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004935 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004936 xhci_dbg(xhci, "Reset complete\n");
4937
Yoshihiro Shimoda0a380be2016-04-08 16:25:07 +03004938 /*
4939 * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0)
4940 * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit
4941 * address memory pointers actually. So, this driver clears the AC64
4942 * bit of xhci->hcc_params to call dma_set_coherent_mask(dev,
4943 * DMA_BIT_MASK(32)) in this xhci_gen_setup().
4944 */
4945 if (xhci->quirks & XHCI_NO_64BIT_SUPPORT)
4946 xhci->hcc_params &= ~BIT(0);
4947
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03004948 /* Set dma_mask and coherent_dma_mask to 64-bits,
4949 * if xHC supports 64-bit addressing */
4950 if (HCC_64BIT_ADDR(xhci->hcc_params) &&
4951 !dma_set_mask(dev, DMA_BIT_MASK(64))) {
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004952 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03004953 dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
Duc Dangfda182d2015-10-09 13:30:13 +03004954 } else {
4955 /*
4956 * This is to avoid error in cases where a 32-bit USB
4957 * controller is used on a 64-bit capable system.
4958 */
4959 retval = dma_set_mask(dev, DMA_BIT_MASK(32));
4960 if (retval)
4961 return retval;
4962 xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n");
4963 dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004964 }
4965
4966 xhci_dbg(xhci, "Calling HCD init\n");
4967 /* Initialize HCD and host controller data structures. */
4968 retval = xhci_init(hcd);
4969 if (retval)
Roger Quadroscd33a322015-05-29 17:01:46 +03004970 return retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004971 xhci_dbg(xhci, "Called HCD init\n");
Hans de Goede99705092015-01-16 17:54:01 +02004972
4973 xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%08x\n",
4974 xhci->hcc_params, xhci->hci_version, xhci->quirks);
4975
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004976 return 0;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004977}
Andrew Bresticker436e8c72014-10-03 11:35:28 +03004978EXPORT_SYMBOL_GPL(xhci_gen_setup);
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004979
Hemant Kumar1346a802017-09-22 15:03:45 -07004980static phys_addr_t xhci_get_sec_event_ring_phys_addr(struct usb_hcd *hcd,
4981 unsigned int intr_num, dma_addr_t *dma)
Hemant Kumar258b4b42016-03-22 19:34:20 -07004982{
4983 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Hemant Kumar1346a802017-09-22 15:03:45 -07004984 struct device *dev = hcd->self.sysdev;
4985 struct sg_table sgt;
4986 phys_addr_t pa;
Hemant Kumar258b4b42016-03-22 19:34:20 -07004987
Hemant Kumar48cb3882017-02-07 11:50:13 -08004988 if (intr_num >= xhci->max_interrupters) {
4989 xhci_err(xhci, "intr num %d >= max intrs %d\n", intr_num,
Hemant Kumar258b4b42016-03-22 19:34:20 -07004990 xhci->max_interrupters);
4991 return 0;
4992 }
4993
4994 if (!(xhci->xhc_state & XHCI_STATE_HALTED) &&
4995 xhci->sec_event_ring && xhci->sec_event_ring[intr_num]
Hemant Kumar1346a802017-09-22 15:03:45 -07004996 && xhci->sec_event_ring[intr_num]->first_seg) {
4997
4998 dma_get_sgtable(dev, &sgt,
4999 xhci->sec_event_ring[intr_num]->first_seg->trbs,
5000 xhci->sec_event_ring[intr_num]->first_seg->dma,
5001 TRB_SEGMENT_SIZE);
5002
5003 *dma = xhci->sec_event_ring[intr_num]->first_seg->dma;
5004
5005 pa = page_to_phys(sg_page(sgt.sgl));
5006 sg_free_table(&sgt);
5007
5008 return pa;
5009 }
Hemant Kumar258b4b42016-03-22 19:34:20 -07005010
5011 return 0;
5012}
5013
Hemant Kumar1346a802017-09-22 15:03:45 -07005014static phys_addr_t xhci_get_xfer_ring_phys_addr(struct usb_hcd *hcd,
5015 struct usb_device *udev, struct usb_host_endpoint *ep, dma_addr_t *dma)
Hemant Kumar258b4b42016-03-22 19:34:20 -07005016{
5017 int ret;
5018 unsigned int ep_index;
5019 struct xhci_virt_device *virt_dev;
Hemant Kumar1346a802017-09-22 15:03:45 -07005020 struct device *dev = hcd->self.sysdev;
Hemant Kumar258b4b42016-03-22 19:34:20 -07005021 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Hemant Kumar1346a802017-09-22 15:03:45 -07005022 struct sg_table sgt;
5023 phys_addr_t pa;
Hemant Kumar258b4b42016-03-22 19:34:20 -07005024
5025 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
5026 if (ret <= 0) {
5027 xhci_err(xhci, "%s: invalid args\n", __func__);
5028 return 0;
5029 }
5030
5031 virt_dev = xhci->devs[udev->slot_id];
5032 ep_index = xhci_get_endpoint_index(&ep->desc);
5033
5034 if (virt_dev->eps[ep_index].ring &&
Hemant Kumar1346a802017-09-22 15:03:45 -07005035 virt_dev->eps[ep_index].ring->first_seg) {
5036
5037 dma_get_sgtable(dev, &sgt,
5038 virt_dev->eps[ep_index].ring->first_seg->trbs,
5039 virt_dev->eps[ep_index].ring->first_seg->dma,
5040 TRB_SEGMENT_SIZE);
5041
5042 *dma = virt_dev->eps[ep_index].ring->first_seg->dma;
5043
5044 pa = page_to_phys(sg_page(sgt.sgl));
5045 sg_free_table(&sgt);
5046
5047 return pa;
5048 }
Hemant Kumar258b4b42016-03-22 19:34:20 -07005049
5050 return 0;
5051}
5052
Hemant Kumarf0cdec42017-08-18 16:59:33 -07005053int xhci_get_core_id(struct usb_hcd *hcd)
5054{
5055 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
5056
5057 return xhci->core_id;
5058}
5059
Hemant Kumaree68f1732017-11-14 20:06:15 -08005060static int xhci_stop_endpoint(struct usb_hcd *hcd,
5061 struct usb_device *udev, struct usb_host_endpoint *ep)
5062{
5063 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
5064 unsigned int ep_index;
5065 struct xhci_virt_device *virt_dev;
5066 struct xhci_command *cmd;
5067 unsigned long flags;
5068 int ret = 0;
5069
5070 cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
5071 if (!cmd)
5072 return -ENOMEM;
5073
5074 spin_lock_irqsave(&xhci->lock, flags);
5075 virt_dev = xhci->devs[udev->slot_id];
5076 if (!virt_dev) {
5077 ret = -ENODEV;
5078 goto err;
5079 }
5080
5081 ep_index = xhci_get_endpoint_index(&ep->desc);
5082 if (virt_dev->eps[ep_index].ring &&
5083 virt_dev->eps[ep_index].ring->dequeue) {
5084 ret = xhci_queue_stop_endpoint(xhci, cmd, udev->slot_id,
5085 ep_index, 0);
5086 if (ret)
5087 goto err;
5088
5089 xhci_ring_cmd_db(xhci);
5090 spin_unlock_irqrestore(&xhci->lock, flags);
5091
5092 /* Wait for stop endpoint command to finish */
5093 wait_for_completion(cmd->completion);
5094
5095 if (cmd->status == COMP_CMD_ABORT ||
5096 cmd->status == COMP_CMD_STOP) {
5097 xhci_warn(xhci,
5098 "stop endpoint command timeout for ep%d%s\n",
5099 usb_endpoint_num(&ep->desc),
5100 usb_endpoint_dir_in(&ep->desc) ? "in" : "out");
5101 ret = -ETIME;
5102 }
5103 goto free_cmd;
5104 }
5105
5106err:
5107 spin_unlock_irqrestore(&xhci->lock, flags);
5108free_cmd:
5109 xhci_free_command(xhci, cmd);
5110 return ret;
5111}
5112
5113
5114
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005115static const struct hc_driver xhci_hc_driver = {
5116 .description = "xhci-hcd",
5117 .product_desc = "xHCI Host Controller",
Yoshihiro Shimoda32479d42015-11-24 13:09:47 +02005118 .hcd_priv_size = sizeof(struct xhci_hcd),
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005119
5120 /*
5121 * generic hardware linkage
5122 */
5123 .irq = xhci_irq,
5124 .flags = HCD_MEMORY | HCD_USB3 | HCD_SHARED,
5125
5126 /*
5127 * basic lifecycle operations
5128 */
5129 .reset = NULL, /* set in xhci_init_driver() */
5130 .start = xhci_run,
5131 .stop = xhci_stop,
5132 .shutdown = xhci_shutdown,
5133
5134 /*
5135 * managing i/o requests and associated device resources
5136 */
5137 .urb_enqueue = xhci_urb_enqueue,
5138 .urb_dequeue = xhci_urb_dequeue,
5139 .alloc_dev = xhci_alloc_dev,
5140 .free_dev = xhci_free_dev,
5141 .alloc_streams = xhci_alloc_streams,
5142 .free_streams = xhci_free_streams,
5143 .add_endpoint = xhci_add_endpoint,
5144 .drop_endpoint = xhci_drop_endpoint,
5145 .endpoint_reset = xhci_endpoint_reset,
5146 .check_bandwidth = xhci_check_bandwidth,
5147 .reset_bandwidth = xhci_reset_bandwidth,
5148 .address_device = xhci_address_device,
5149 .enable_device = xhci_enable_device,
5150 .update_hub_device = xhci_update_hub_device,
5151 .reset_device = xhci_discover_or_reset_device,
5152
5153 /*
5154 * scheduling support
5155 */
5156 .get_frame_number = xhci_get_frame,
5157
5158 /*
5159 * root hub support
5160 */
5161 .hub_control = xhci_hub_control,
5162 .hub_status_data = xhci_hub_status_data,
5163 .bus_suspend = xhci_bus_suspend,
5164 .bus_resume = xhci_bus_resume,
5165
5166 /*
5167 * call back when device connected and addressed
5168 */
5169 .update_device = xhci_update_device,
5170 .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm,
5171 .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
5172 .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
5173 .find_raw_port_number = xhci_find_raw_port_number,
Hemant Kumar8aad8422016-03-22 13:41:59 -07005174 .sec_event_ring_setup = xhci_sec_event_ring_setup,
5175 .sec_event_ring_cleanup = xhci_sec_event_ring_cleanup,
Hemant Kumar1346a802017-09-22 15:03:45 -07005176 .get_sec_event_ring_phys_addr = xhci_get_sec_event_ring_phys_addr,
5177 .get_xfer_ring_phys_addr = xhci_get_xfer_ring_phys_addr,
Hemant Kumarf0cdec42017-08-18 16:59:33 -07005178 .get_core_id = xhci_get_core_id,
Hemant Kumaree68f1732017-11-14 20:06:15 -08005179 .stop_endpoint = xhci_stop_endpoint,
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005180};
5181
Roger Quadroscd33a322015-05-29 17:01:46 +03005182void xhci_init_driver(struct hc_driver *drv,
5183 const struct xhci_driver_overrides *over)
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005184{
Roger Quadroscd33a322015-05-29 17:01:46 +03005185 BUG_ON(!over);
5186
5187 /* Copy the generic table to drv then apply the overrides */
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005188 *drv = xhci_hc_driver;
Roger Quadroscd33a322015-05-29 17:01:46 +03005189
5190 if (over) {
5191 drv->hcd_priv_size += over->extra_priv_size;
5192 if (over->reset)
5193 drv->reset = over->reset;
5194 if (over->start)
5195 drv->start = over->start;
5196 }
Andrew Bresticker1885d9a2014-10-03 11:35:26 +03005197}
5198EXPORT_SYMBOL_GPL(xhci_init_driver);
5199
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005200MODULE_DESCRIPTION(DRIVER_DESC);
5201MODULE_AUTHOR(DRIVER_AUTHOR);
5202MODULE_LICENSE("GPL");
5203
5204static int __init xhci_hcd_init(void)
5205{
Sarah Sharp98441972009-05-14 11:44:18 -07005206 /*
5207 * Check the compiler generated sizes of structures that must be laid
5208 * out in specific ways for hardware access.
5209 */
5210 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
5211 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
5212 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
5213 /* xhci_device_control has eight fields, and also
5214 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
5215 */
Sarah Sharp98441972009-05-14 11:44:18 -07005216 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
5217 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
5218 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
Lu Baolu04abb6d2015-10-01 18:40:31 +03005219 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 8*32/8);
Sarah Sharp98441972009-05-14 11:44:18 -07005220 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
5221 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
5222 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
Oliver Neukum1eaf35e2015-12-03 15:03:34 +01005223
5224 if (usb_disabled())
5225 return -ENODEV;
5226
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005227 return 0;
5228}
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005229
5230/*
5231 * If an init function is provided, an exit function must also be provided
5232 * to allow module unload.
5233 */
5234static void __exit xhci_hcd_fini(void) { }
5235
Sarah Sharp66d4ead2009-04-27 19:52:28 -07005236module_init(xhci_hcd_init);
Arthur Demchenkovb04c8462015-05-19 16:30:50 +03005237module_exit(xhci_hcd_fini);