1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun * xHCI host controller driver
4*4882a593Smuzhiyun *
5*4882a593Smuzhiyun * Copyright (C) 2008 Intel Corp.
6*4882a593Smuzhiyun *
7*4882a593Smuzhiyun * Author: Sarah Sharp
8*4882a593Smuzhiyun * Some code borrowed from the Linux EHCI driver.
9*4882a593Smuzhiyun */
10*4882a593Smuzhiyun
11*4882a593Smuzhiyun #include <linux/pci.h>
12*4882a593Smuzhiyun #include <linux/iopoll.h>
13*4882a593Smuzhiyun #include <linux/irq.h>
14*4882a593Smuzhiyun #include <linux/log2.h>
15*4882a593Smuzhiyun #include <linux/module.h>
16*4882a593Smuzhiyun #include <linux/moduleparam.h>
17*4882a593Smuzhiyun #include <linux/slab.h>
18*4882a593Smuzhiyun #include <linux/dmi.h>
19*4882a593Smuzhiyun #include <linux/dma-mapping.h>
20*4882a593Smuzhiyun
21*4882a593Smuzhiyun #include "xhci.h"
22*4882a593Smuzhiyun #include "xhci-trace.h"
23*4882a593Smuzhiyun #include "xhci-debugfs.h"
24*4882a593Smuzhiyun #include "xhci-dbgcap.h"
25*4882a593Smuzhiyun
26*4882a593Smuzhiyun #define DRIVER_AUTHOR "Sarah Sharp"
27*4882a593Smuzhiyun #define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
28*4882a593Smuzhiyun
29*4882a593Smuzhiyun #define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
30*4882a593Smuzhiyun
31*4882a593Smuzhiyun /* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
32*4882a593Smuzhiyun static int link_quirk;
33*4882a593Smuzhiyun module_param(link_quirk, int, S_IRUGO | S_IWUSR);
34*4882a593Smuzhiyun MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
35*4882a593Smuzhiyun
36*4882a593Smuzhiyun static unsigned long long quirks;
37*4882a593Smuzhiyun module_param(quirks, ullong, S_IRUGO);
38*4882a593Smuzhiyun MODULE_PARM_DESC(quirks, "Bit flags for quirks to be enabled as default");
39*4882a593Smuzhiyun
td_on_ring(struct xhci_td * td,struct xhci_ring * ring)40*4882a593Smuzhiyun static bool td_on_ring(struct xhci_td *td, struct xhci_ring *ring)
41*4882a593Smuzhiyun {
42*4882a593Smuzhiyun struct xhci_segment *seg = ring->first_seg;
43*4882a593Smuzhiyun
44*4882a593Smuzhiyun if (!td || !td->start_seg)
45*4882a593Smuzhiyun return false;
46*4882a593Smuzhiyun do {
47*4882a593Smuzhiyun if (seg == td->start_seg)
48*4882a593Smuzhiyun return true;
49*4882a593Smuzhiyun seg = seg->next;
50*4882a593Smuzhiyun } while (seg && seg != ring->first_seg);
51*4882a593Smuzhiyun
52*4882a593Smuzhiyun return false;
53*4882a593Smuzhiyun }
54*4882a593Smuzhiyun
55*4882a593Smuzhiyun /*
56*4882a593Smuzhiyun * xhci_handshake - spin reading hc until handshake completes or fails
57*4882a593Smuzhiyun * @ptr: address of hc register to be read
58*4882a593Smuzhiyun * @mask: bits to look at in result of read
59*4882a593Smuzhiyun * @done: value of those bits when handshake succeeds
60*4882a593Smuzhiyun * @usec: timeout in microseconds
61*4882a593Smuzhiyun *
62*4882a593Smuzhiyun * Returns negative errno, or zero on success
63*4882a593Smuzhiyun *
64*4882a593Smuzhiyun * Success happens when the "mask" bits have the specified value (hardware
65*4882a593Smuzhiyun * handshake done). There are two failure modes: "usec" have passed (major
66*4882a593Smuzhiyun * hardware flakeout), or the register reads as all-ones (hardware removed).
67*4882a593Smuzhiyun */
xhci_handshake(void __iomem * ptr,u32 mask,u32 done,u64 timeout_us)68*4882a593Smuzhiyun int xhci_handshake(void __iomem *ptr, u32 mask, u32 done, u64 timeout_us)
69*4882a593Smuzhiyun {
70*4882a593Smuzhiyun u32 result;
71*4882a593Smuzhiyun int ret;
72*4882a593Smuzhiyun
73*4882a593Smuzhiyun ret = readl_poll_timeout_atomic(ptr, result,
74*4882a593Smuzhiyun (result & mask) == done ||
75*4882a593Smuzhiyun result == U32_MAX,
76*4882a593Smuzhiyun 1, timeout_us);
77*4882a593Smuzhiyun if (result == U32_MAX) /* card removed */
78*4882a593Smuzhiyun return -ENODEV;
79*4882a593Smuzhiyun
80*4882a593Smuzhiyun return ret;
81*4882a593Smuzhiyun }
82*4882a593Smuzhiyun
83*4882a593Smuzhiyun /*
84*4882a593Smuzhiyun * Disable interrupts and begin the xHCI halting process.
85*4882a593Smuzhiyun */
xhci_quiesce(struct xhci_hcd * xhci)86*4882a593Smuzhiyun void xhci_quiesce(struct xhci_hcd *xhci)
87*4882a593Smuzhiyun {
88*4882a593Smuzhiyun u32 halted;
89*4882a593Smuzhiyun u32 cmd;
90*4882a593Smuzhiyun u32 mask;
91*4882a593Smuzhiyun
92*4882a593Smuzhiyun mask = ~(XHCI_IRQS);
93*4882a593Smuzhiyun halted = readl(&xhci->op_regs->status) & STS_HALT;
94*4882a593Smuzhiyun if (!halted)
95*4882a593Smuzhiyun mask &= ~CMD_RUN;
96*4882a593Smuzhiyun
97*4882a593Smuzhiyun cmd = readl(&xhci->op_regs->command);
98*4882a593Smuzhiyun cmd &= mask;
99*4882a593Smuzhiyun writel(cmd, &xhci->op_regs->command);
100*4882a593Smuzhiyun }
101*4882a593Smuzhiyun
102*4882a593Smuzhiyun /*
103*4882a593Smuzhiyun * Force HC into halt state.
104*4882a593Smuzhiyun *
105*4882a593Smuzhiyun * Disable any IRQs and clear the run/stop bit.
106*4882a593Smuzhiyun * HC will complete any current and actively pipelined transactions, and
107*4882a593Smuzhiyun * should halt within 16 ms of the run/stop bit being cleared.
108*4882a593Smuzhiyun * Read HC Halted bit in the status register to see when the HC is finished.
109*4882a593Smuzhiyun */
xhci_halt(struct xhci_hcd * xhci)110*4882a593Smuzhiyun int xhci_halt(struct xhci_hcd *xhci)
111*4882a593Smuzhiyun {
112*4882a593Smuzhiyun int ret;
113*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Halt the HC");
114*4882a593Smuzhiyun xhci_quiesce(xhci);
115*4882a593Smuzhiyun
116*4882a593Smuzhiyun ret = xhci_handshake(&xhci->op_regs->status,
117*4882a593Smuzhiyun STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
118*4882a593Smuzhiyun if (ret) {
119*4882a593Smuzhiyun xhci_warn(xhci, "Host halt failed, %d\n", ret);
120*4882a593Smuzhiyun return ret;
121*4882a593Smuzhiyun }
122*4882a593Smuzhiyun xhci->xhc_state |= XHCI_STATE_HALTED;
123*4882a593Smuzhiyun xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
124*4882a593Smuzhiyun return ret;
125*4882a593Smuzhiyun }
126*4882a593Smuzhiyun
127*4882a593Smuzhiyun /*
128*4882a593Smuzhiyun * Set the run bit and wait for the host to be running.
129*4882a593Smuzhiyun */
xhci_start(struct xhci_hcd * xhci)130*4882a593Smuzhiyun int xhci_start(struct xhci_hcd *xhci)
131*4882a593Smuzhiyun {
132*4882a593Smuzhiyun u32 temp;
133*4882a593Smuzhiyun int ret;
134*4882a593Smuzhiyun
135*4882a593Smuzhiyun temp = readl(&xhci->op_regs->command);
136*4882a593Smuzhiyun temp |= (CMD_RUN);
137*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.",
138*4882a593Smuzhiyun temp);
139*4882a593Smuzhiyun writel(temp, &xhci->op_regs->command);
140*4882a593Smuzhiyun
141*4882a593Smuzhiyun /*
142*4882a593Smuzhiyun * Wait for the HCHalted Status bit to be 0 to indicate the host is
143*4882a593Smuzhiyun * running.
144*4882a593Smuzhiyun */
145*4882a593Smuzhiyun ret = xhci_handshake(&xhci->op_regs->status,
146*4882a593Smuzhiyun STS_HALT, 0, XHCI_MAX_HALT_USEC);
147*4882a593Smuzhiyun if (ret == -ETIMEDOUT)
148*4882a593Smuzhiyun xhci_err(xhci, "Host took too long to start, "
149*4882a593Smuzhiyun "waited %u microseconds.\n",
150*4882a593Smuzhiyun XHCI_MAX_HALT_USEC);
151*4882a593Smuzhiyun if (!ret)
152*4882a593Smuzhiyun /* clear state flags. Including dying, halted or removing */
153*4882a593Smuzhiyun xhci->xhc_state = 0;
154*4882a593Smuzhiyun
155*4882a593Smuzhiyun return ret;
156*4882a593Smuzhiyun }
157*4882a593Smuzhiyun
158*4882a593Smuzhiyun /*
159*4882a593Smuzhiyun * Reset a halted HC.
160*4882a593Smuzhiyun *
161*4882a593Smuzhiyun * This resets pipelines, timers, counters, state machines, etc.
162*4882a593Smuzhiyun * Transactions will be terminated immediately, and operational registers
163*4882a593Smuzhiyun * will be set to their defaults.
164*4882a593Smuzhiyun */
xhci_reset(struct xhci_hcd * xhci,u64 timeout_us)165*4882a593Smuzhiyun int xhci_reset(struct xhci_hcd *xhci, u64 timeout_us)
166*4882a593Smuzhiyun {
167*4882a593Smuzhiyun u32 command;
168*4882a593Smuzhiyun u32 state;
169*4882a593Smuzhiyun int ret;
170*4882a593Smuzhiyun
171*4882a593Smuzhiyun state = readl(&xhci->op_regs->status);
172*4882a593Smuzhiyun
173*4882a593Smuzhiyun if (state == ~(u32)0) {
174*4882a593Smuzhiyun xhci_warn(xhci, "Host not accessible, reset failed.\n");
175*4882a593Smuzhiyun return -ENODEV;
176*4882a593Smuzhiyun }
177*4882a593Smuzhiyun
178*4882a593Smuzhiyun if ((state & STS_HALT) == 0) {
179*4882a593Smuzhiyun xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
180*4882a593Smuzhiyun return 0;
181*4882a593Smuzhiyun }
182*4882a593Smuzhiyun
183*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC");
184*4882a593Smuzhiyun command = readl(&xhci->op_regs->command);
185*4882a593Smuzhiyun command |= CMD_RESET;
186*4882a593Smuzhiyun writel(command, &xhci->op_regs->command);
187*4882a593Smuzhiyun
188*4882a593Smuzhiyun /* Existing Intel xHCI controllers require a delay of 1 mS,
189*4882a593Smuzhiyun * after setting the CMD_RESET bit, and before accessing any
190*4882a593Smuzhiyun * HC registers. This allows the HC to complete the
191*4882a593Smuzhiyun * reset operation and be ready for HC register access.
192*4882a593Smuzhiyun * Without this delay, the subsequent HC register access,
193*4882a593Smuzhiyun * may result in a system hang very rarely.
194*4882a593Smuzhiyun */
195*4882a593Smuzhiyun if (xhci->quirks & XHCI_INTEL_HOST)
196*4882a593Smuzhiyun udelay(1000);
197*4882a593Smuzhiyun
198*4882a593Smuzhiyun ret = xhci_handshake(&xhci->op_regs->command, CMD_RESET, 0, timeout_us);
199*4882a593Smuzhiyun if (ret)
200*4882a593Smuzhiyun return ret;
201*4882a593Smuzhiyun
202*4882a593Smuzhiyun if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
203*4882a593Smuzhiyun usb_asmedia_modifyflowcontrol(to_pci_dev(xhci_to_hcd(xhci)->self.controller));
204*4882a593Smuzhiyun
205*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
206*4882a593Smuzhiyun "Wait for controller to be ready for doorbell rings");
207*4882a593Smuzhiyun /*
208*4882a593Smuzhiyun * xHCI cannot write to any doorbells or operational registers other
209*4882a593Smuzhiyun * than status until the "Controller Not Ready" flag is cleared.
210*4882a593Smuzhiyun */
211*4882a593Smuzhiyun ret = xhci_handshake(&xhci->op_regs->status, STS_CNR, 0, timeout_us);
212*4882a593Smuzhiyun
213*4882a593Smuzhiyun xhci->usb2_rhub.bus_state.port_c_suspend = 0;
214*4882a593Smuzhiyun xhci->usb2_rhub.bus_state.suspended_ports = 0;
215*4882a593Smuzhiyun xhci->usb2_rhub.bus_state.resuming_ports = 0;
216*4882a593Smuzhiyun xhci->usb3_rhub.bus_state.port_c_suspend = 0;
217*4882a593Smuzhiyun xhci->usb3_rhub.bus_state.suspended_ports = 0;
218*4882a593Smuzhiyun xhci->usb3_rhub.bus_state.resuming_ports = 0;
219*4882a593Smuzhiyun
220*4882a593Smuzhiyun return ret;
221*4882a593Smuzhiyun }
222*4882a593Smuzhiyun
xhci_zero_64b_regs(struct xhci_hcd * xhci)223*4882a593Smuzhiyun static void xhci_zero_64b_regs(struct xhci_hcd *xhci)
224*4882a593Smuzhiyun {
225*4882a593Smuzhiyun struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
226*4882a593Smuzhiyun int err, i;
227*4882a593Smuzhiyun u64 val;
228*4882a593Smuzhiyun u32 intrs;
229*4882a593Smuzhiyun
230*4882a593Smuzhiyun /*
231*4882a593Smuzhiyun * Some Renesas controllers get into a weird state if they are
232*4882a593Smuzhiyun * reset while programmed with 64bit addresses (they will preserve
233*4882a593Smuzhiyun * the top half of the address in internal, non visible
234*4882a593Smuzhiyun * registers). You end up with half the address coming from the
235*4882a593Smuzhiyun * kernel, and the other half coming from the firmware. Also,
236*4882a593Smuzhiyun * changing the programming leads to extra accesses even if the
237*4882a593Smuzhiyun * controller is supposed to be halted. The controller ends up with
238*4882a593Smuzhiyun * a fatal fault, and is then ripe for being properly reset.
239*4882a593Smuzhiyun *
240*4882a593Smuzhiyun * Special care is taken to only apply this if the device is behind
241*4882a593Smuzhiyun * an iommu. Doing anything when there is no iommu is definitely
242*4882a593Smuzhiyun * unsafe...
243*4882a593Smuzhiyun */
244*4882a593Smuzhiyun if (!(xhci->quirks & XHCI_ZERO_64B_REGS) || !device_iommu_mapped(dev))
245*4882a593Smuzhiyun return;
246*4882a593Smuzhiyun
247*4882a593Smuzhiyun xhci_info(xhci, "Zeroing 64bit base registers, expecting fault\n");
248*4882a593Smuzhiyun
249*4882a593Smuzhiyun /* Clear HSEIE so that faults do not get signaled */
250*4882a593Smuzhiyun val = readl(&xhci->op_regs->command);
251*4882a593Smuzhiyun val &= ~CMD_HSEIE;
252*4882a593Smuzhiyun writel(val, &xhci->op_regs->command);
253*4882a593Smuzhiyun
254*4882a593Smuzhiyun /* Clear HSE (aka FATAL) */
255*4882a593Smuzhiyun val = readl(&xhci->op_regs->status);
256*4882a593Smuzhiyun val |= STS_FATAL;
257*4882a593Smuzhiyun writel(val, &xhci->op_regs->status);
258*4882a593Smuzhiyun
259*4882a593Smuzhiyun /* Now zero the registers, and brace for impact */
260*4882a593Smuzhiyun val = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
261*4882a593Smuzhiyun if (upper_32_bits(val))
262*4882a593Smuzhiyun xhci_write_64(xhci, 0, &xhci->op_regs->dcbaa_ptr);
263*4882a593Smuzhiyun val = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
264*4882a593Smuzhiyun if (upper_32_bits(val))
265*4882a593Smuzhiyun xhci_write_64(xhci, 0, &xhci->op_regs->cmd_ring);
266*4882a593Smuzhiyun
267*4882a593Smuzhiyun intrs = min_t(u32, HCS_MAX_INTRS(xhci->hcs_params1),
268*4882a593Smuzhiyun ARRAY_SIZE(xhci->run_regs->ir_set));
269*4882a593Smuzhiyun
270*4882a593Smuzhiyun for (i = 0; i < intrs; i++) {
271*4882a593Smuzhiyun struct xhci_intr_reg __iomem *ir;
272*4882a593Smuzhiyun
273*4882a593Smuzhiyun ir = &xhci->run_regs->ir_set[i];
274*4882a593Smuzhiyun val = xhci_read_64(xhci, &ir->erst_base);
275*4882a593Smuzhiyun if (upper_32_bits(val))
276*4882a593Smuzhiyun xhci_write_64(xhci, 0, &ir->erst_base);
277*4882a593Smuzhiyun val= xhci_read_64(xhci, &ir->erst_dequeue);
278*4882a593Smuzhiyun if (upper_32_bits(val))
279*4882a593Smuzhiyun xhci_write_64(xhci, 0, &ir->erst_dequeue);
280*4882a593Smuzhiyun }
281*4882a593Smuzhiyun
282*4882a593Smuzhiyun /* Wait for the fault to appear. It will be cleared on reset */
283*4882a593Smuzhiyun err = xhci_handshake(&xhci->op_regs->status,
284*4882a593Smuzhiyun STS_FATAL, STS_FATAL,
285*4882a593Smuzhiyun XHCI_MAX_HALT_USEC);
286*4882a593Smuzhiyun if (!err)
287*4882a593Smuzhiyun xhci_info(xhci, "Fault detected\n");
288*4882a593Smuzhiyun }
289*4882a593Smuzhiyun
290*4882a593Smuzhiyun #ifdef CONFIG_USB_PCI
291*4882a593Smuzhiyun /*
292*4882a593Smuzhiyun * Set up MSI
293*4882a593Smuzhiyun */
xhci_setup_msi(struct xhci_hcd * xhci)294*4882a593Smuzhiyun static int xhci_setup_msi(struct xhci_hcd *xhci)
295*4882a593Smuzhiyun {
296*4882a593Smuzhiyun int ret;
297*4882a593Smuzhiyun /*
298*4882a593Smuzhiyun * TODO:Check with MSI Soc for sysdev
299*4882a593Smuzhiyun */
300*4882a593Smuzhiyun struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
301*4882a593Smuzhiyun
302*4882a593Smuzhiyun ret = pci_alloc_irq_vectors(pdev, 1, 1, PCI_IRQ_MSI);
303*4882a593Smuzhiyun if (ret < 0) {
304*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
305*4882a593Smuzhiyun "failed to allocate MSI entry");
306*4882a593Smuzhiyun return ret;
307*4882a593Smuzhiyun }
308*4882a593Smuzhiyun
309*4882a593Smuzhiyun ret = request_irq(pdev->irq, xhci_msi_irq,
310*4882a593Smuzhiyun 0, "xhci_hcd", xhci_to_hcd(xhci));
311*4882a593Smuzhiyun if (ret) {
312*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
313*4882a593Smuzhiyun "disable MSI interrupt");
314*4882a593Smuzhiyun pci_free_irq_vectors(pdev);
315*4882a593Smuzhiyun }
316*4882a593Smuzhiyun
317*4882a593Smuzhiyun return ret;
318*4882a593Smuzhiyun }
319*4882a593Smuzhiyun
320*4882a593Smuzhiyun /*
321*4882a593Smuzhiyun * Set up MSI-X
322*4882a593Smuzhiyun */
xhci_setup_msix(struct xhci_hcd * xhci)323*4882a593Smuzhiyun static int xhci_setup_msix(struct xhci_hcd *xhci)
324*4882a593Smuzhiyun {
325*4882a593Smuzhiyun int i, ret = 0;
326*4882a593Smuzhiyun struct usb_hcd *hcd = xhci_to_hcd(xhci);
327*4882a593Smuzhiyun struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
328*4882a593Smuzhiyun
329*4882a593Smuzhiyun /*
330*4882a593Smuzhiyun * calculate number of msi-x vectors supported.
331*4882a593Smuzhiyun * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
332*4882a593Smuzhiyun * with max number of interrupters based on the xhci HCSPARAMS1.
333*4882a593Smuzhiyun * - num_online_cpus: maximum msi-x vectors per CPUs core.
334*4882a593Smuzhiyun * Add additional 1 vector to ensure always available interrupt.
335*4882a593Smuzhiyun */
336*4882a593Smuzhiyun xhci->msix_count = min(num_online_cpus() + 1,
337*4882a593Smuzhiyun HCS_MAX_INTRS(xhci->hcs_params1));
338*4882a593Smuzhiyun
339*4882a593Smuzhiyun ret = pci_alloc_irq_vectors(pdev, xhci->msix_count, xhci->msix_count,
340*4882a593Smuzhiyun PCI_IRQ_MSIX);
341*4882a593Smuzhiyun if (ret < 0) {
342*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
343*4882a593Smuzhiyun "Failed to enable MSI-X");
344*4882a593Smuzhiyun return ret;
345*4882a593Smuzhiyun }
346*4882a593Smuzhiyun
347*4882a593Smuzhiyun for (i = 0; i < xhci->msix_count; i++) {
348*4882a593Smuzhiyun ret = request_irq(pci_irq_vector(pdev, i), xhci_msi_irq, 0,
349*4882a593Smuzhiyun "xhci_hcd", xhci_to_hcd(xhci));
350*4882a593Smuzhiyun if (ret)
351*4882a593Smuzhiyun goto disable_msix;
352*4882a593Smuzhiyun }
353*4882a593Smuzhiyun
354*4882a593Smuzhiyun hcd->msix_enabled = 1;
355*4882a593Smuzhiyun return ret;
356*4882a593Smuzhiyun
357*4882a593Smuzhiyun disable_msix:
358*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt");
359*4882a593Smuzhiyun while (--i >= 0)
360*4882a593Smuzhiyun free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci));
361*4882a593Smuzhiyun pci_free_irq_vectors(pdev);
362*4882a593Smuzhiyun return ret;
363*4882a593Smuzhiyun }
364*4882a593Smuzhiyun
365*4882a593Smuzhiyun /* Free any IRQs and disable MSI-X */
xhci_cleanup_msix(struct xhci_hcd * xhci)366*4882a593Smuzhiyun static void xhci_cleanup_msix(struct xhci_hcd *xhci)
367*4882a593Smuzhiyun {
368*4882a593Smuzhiyun struct usb_hcd *hcd = xhci_to_hcd(xhci);
369*4882a593Smuzhiyun struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
370*4882a593Smuzhiyun
371*4882a593Smuzhiyun if (xhci->quirks & XHCI_PLAT)
372*4882a593Smuzhiyun return;
373*4882a593Smuzhiyun
374*4882a593Smuzhiyun /* return if using legacy interrupt */
375*4882a593Smuzhiyun if (hcd->irq > 0)
376*4882a593Smuzhiyun return;
377*4882a593Smuzhiyun
378*4882a593Smuzhiyun if (hcd->msix_enabled) {
379*4882a593Smuzhiyun int i;
380*4882a593Smuzhiyun
381*4882a593Smuzhiyun for (i = 0; i < xhci->msix_count; i++)
382*4882a593Smuzhiyun free_irq(pci_irq_vector(pdev, i), xhci_to_hcd(xhci));
383*4882a593Smuzhiyun } else {
384*4882a593Smuzhiyun free_irq(pci_irq_vector(pdev, 0), xhci_to_hcd(xhci));
385*4882a593Smuzhiyun }
386*4882a593Smuzhiyun
387*4882a593Smuzhiyun pci_free_irq_vectors(pdev);
388*4882a593Smuzhiyun hcd->msix_enabled = 0;
389*4882a593Smuzhiyun }
390*4882a593Smuzhiyun
xhci_msix_sync_irqs(struct xhci_hcd * xhci)391*4882a593Smuzhiyun static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci)
392*4882a593Smuzhiyun {
393*4882a593Smuzhiyun struct usb_hcd *hcd = xhci_to_hcd(xhci);
394*4882a593Smuzhiyun
395*4882a593Smuzhiyun if (hcd->msix_enabled) {
396*4882a593Smuzhiyun struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
397*4882a593Smuzhiyun int i;
398*4882a593Smuzhiyun
399*4882a593Smuzhiyun for (i = 0; i < xhci->msix_count; i++)
400*4882a593Smuzhiyun synchronize_irq(pci_irq_vector(pdev, i));
401*4882a593Smuzhiyun }
402*4882a593Smuzhiyun }
403*4882a593Smuzhiyun
xhci_try_enable_msi(struct usb_hcd * hcd)404*4882a593Smuzhiyun static int xhci_try_enable_msi(struct usb_hcd *hcd)
405*4882a593Smuzhiyun {
406*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
407*4882a593Smuzhiyun struct pci_dev *pdev;
408*4882a593Smuzhiyun int ret;
409*4882a593Smuzhiyun
410*4882a593Smuzhiyun /* The xhci platform device has set up IRQs through usb_add_hcd. */
411*4882a593Smuzhiyun if (xhci->quirks & XHCI_PLAT)
412*4882a593Smuzhiyun return 0;
413*4882a593Smuzhiyun
414*4882a593Smuzhiyun pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
415*4882a593Smuzhiyun /*
416*4882a593Smuzhiyun * Some Fresco Logic host controllers advertise MSI, but fail to
417*4882a593Smuzhiyun * generate interrupts. Don't even try to enable MSI.
418*4882a593Smuzhiyun */
419*4882a593Smuzhiyun if (xhci->quirks & XHCI_BROKEN_MSI)
420*4882a593Smuzhiyun goto legacy_irq;
421*4882a593Smuzhiyun
422*4882a593Smuzhiyun /* unregister the legacy interrupt */
423*4882a593Smuzhiyun if (hcd->irq)
424*4882a593Smuzhiyun free_irq(hcd->irq, hcd);
425*4882a593Smuzhiyun hcd->irq = 0;
426*4882a593Smuzhiyun
427*4882a593Smuzhiyun ret = xhci_setup_msix(xhci);
428*4882a593Smuzhiyun if (ret)
429*4882a593Smuzhiyun /* fall back to msi*/
430*4882a593Smuzhiyun ret = xhci_setup_msi(xhci);
431*4882a593Smuzhiyun
432*4882a593Smuzhiyun if (!ret) {
433*4882a593Smuzhiyun hcd->msi_enabled = 1;
434*4882a593Smuzhiyun return 0;
435*4882a593Smuzhiyun }
436*4882a593Smuzhiyun
437*4882a593Smuzhiyun if (!pdev->irq) {
438*4882a593Smuzhiyun xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
439*4882a593Smuzhiyun return -EINVAL;
440*4882a593Smuzhiyun }
441*4882a593Smuzhiyun
442*4882a593Smuzhiyun legacy_irq:
443*4882a593Smuzhiyun if (!strlen(hcd->irq_descr))
444*4882a593Smuzhiyun snprintf(hcd->irq_descr, sizeof(hcd->irq_descr), "%s:usb%d",
445*4882a593Smuzhiyun hcd->driver->description, hcd->self.busnum);
446*4882a593Smuzhiyun
447*4882a593Smuzhiyun /* fall back to legacy interrupt*/
448*4882a593Smuzhiyun ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
449*4882a593Smuzhiyun hcd->irq_descr, hcd);
450*4882a593Smuzhiyun if (ret) {
451*4882a593Smuzhiyun xhci_err(xhci, "request interrupt %d failed\n",
452*4882a593Smuzhiyun pdev->irq);
453*4882a593Smuzhiyun return ret;
454*4882a593Smuzhiyun }
455*4882a593Smuzhiyun hcd->irq = pdev->irq;
456*4882a593Smuzhiyun return 0;
457*4882a593Smuzhiyun }
458*4882a593Smuzhiyun
459*4882a593Smuzhiyun #else
460*4882a593Smuzhiyun
xhci_try_enable_msi(struct usb_hcd * hcd)461*4882a593Smuzhiyun static inline int xhci_try_enable_msi(struct usb_hcd *hcd)
462*4882a593Smuzhiyun {
463*4882a593Smuzhiyun return 0;
464*4882a593Smuzhiyun }
465*4882a593Smuzhiyun
xhci_cleanup_msix(struct xhci_hcd * xhci)466*4882a593Smuzhiyun static inline void xhci_cleanup_msix(struct xhci_hcd *xhci)
467*4882a593Smuzhiyun {
468*4882a593Smuzhiyun }
469*4882a593Smuzhiyun
xhci_msix_sync_irqs(struct xhci_hcd * xhci)470*4882a593Smuzhiyun static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
471*4882a593Smuzhiyun {
472*4882a593Smuzhiyun }
473*4882a593Smuzhiyun
474*4882a593Smuzhiyun #endif
475*4882a593Smuzhiyun
compliance_mode_recovery(struct timer_list * t)476*4882a593Smuzhiyun static void compliance_mode_recovery(struct timer_list *t)
477*4882a593Smuzhiyun {
478*4882a593Smuzhiyun struct xhci_hcd *xhci;
479*4882a593Smuzhiyun struct usb_hcd *hcd;
480*4882a593Smuzhiyun struct xhci_hub *rhub;
481*4882a593Smuzhiyun u32 temp;
482*4882a593Smuzhiyun int i;
483*4882a593Smuzhiyun
484*4882a593Smuzhiyun xhci = from_timer(xhci, t, comp_mode_recovery_timer);
485*4882a593Smuzhiyun rhub = &xhci->usb3_rhub;
486*4882a593Smuzhiyun
487*4882a593Smuzhiyun for (i = 0; i < rhub->num_ports; i++) {
488*4882a593Smuzhiyun temp = readl(rhub->ports[i]->addr);
489*4882a593Smuzhiyun if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
490*4882a593Smuzhiyun /*
491*4882a593Smuzhiyun * Compliance Mode Detected. Letting USB Core
492*4882a593Smuzhiyun * handle the Warm Reset
493*4882a593Smuzhiyun */
494*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
495*4882a593Smuzhiyun "Compliance mode detected->port %d",
496*4882a593Smuzhiyun i + 1);
497*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
498*4882a593Smuzhiyun "Attempting compliance mode recovery");
499*4882a593Smuzhiyun hcd = xhci->shared_hcd;
500*4882a593Smuzhiyun
501*4882a593Smuzhiyun if (hcd->state == HC_STATE_SUSPENDED)
502*4882a593Smuzhiyun usb_hcd_resume_root_hub(hcd);
503*4882a593Smuzhiyun
504*4882a593Smuzhiyun usb_hcd_poll_rh_status(hcd);
505*4882a593Smuzhiyun }
506*4882a593Smuzhiyun }
507*4882a593Smuzhiyun
508*4882a593Smuzhiyun if (xhci->port_status_u0 != ((1 << rhub->num_ports) - 1))
509*4882a593Smuzhiyun mod_timer(&xhci->comp_mode_recovery_timer,
510*4882a593Smuzhiyun jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
511*4882a593Smuzhiyun }
512*4882a593Smuzhiyun
513*4882a593Smuzhiyun /*
514*4882a593Smuzhiyun * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
515*4882a593Smuzhiyun * that causes ports behind that hardware to enter compliance mode sometimes.
516*4882a593Smuzhiyun * The quirk creates a timer that polls every 2 seconds the link state of
517*4882a593Smuzhiyun * each host controller's port and recovers it by issuing a Warm reset
518*4882a593Smuzhiyun * if Compliance mode is detected, otherwise the port will become "dead" (no
519*4882a593Smuzhiyun * device connections or disconnections will be detected anymore). Becasue no
520*4882a593Smuzhiyun * status event is generated when entering compliance mode (per xhci spec),
521*4882a593Smuzhiyun * this quirk is needed on systems that have the failing hardware installed.
522*4882a593Smuzhiyun */
compliance_mode_recovery_timer_init(struct xhci_hcd * xhci)523*4882a593Smuzhiyun static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
524*4882a593Smuzhiyun {
525*4882a593Smuzhiyun xhci->port_status_u0 = 0;
526*4882a593Smuzhiyun timer_setup(&xhci->comp_mode_recovery_timer, compliance_mode_recovery,
527*4882a593Smuzhiyun 0);
528*4882a593Smuzhiyun xhci->comp_mode_recovery_timer.expires = jiffies +
529*4882a593Smuzhiyun msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
530*4882a593Smuzhiyun
531*4882a593Smuzhiyun add_timer(&xhci->comp_mode_recovery_timer);
532*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
533*4882a593Smuzhiyun "Compliance mode recovery timer initialized");
534*4882a593Smuzhiyun }
535*4882a593Smuzhiyun
536*4882a593Smuzhiyun /*
537*4882a593Smuzhiyun * This function identifies the systems that have installed the SN65LVPE502CP
538*4882a593Smuzhiyun * USB3.0 re-driver and that need the Compliance Mode Quirk.
539*4882a593Smuzhiyun * Systems:
540*4882a593Smuzhiyun * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
541*4882a593Smuzhiyun */
xhci_compliance_mode_recovery_timer_quirk_check(void)542*4882a593Smuzhiyun static bool xhci_compliance_mode_recovery_timer_quirk_check(void)
543*4882a593Smuzhiyun {
544*4882a593Smuzhiyun const char *dmi_product_name, *dmi_sys_vendor;
545*4882a593Smuzhiyun
546*4882a593Smuzhiyun dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
547*4882a593Smuzhiyun dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
548*4882a593Smuzhiyun if (!dmi_product_name || !dmi_sys_vendor)
549*4882a593Smuzhiyun return false;
550*4882a593Smuzhiyun
551*4882a593Smuzhiyun if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
552*4882a593Smuzhiyun return false;
553*4882a593Smuzhiyun
554*4882a593Smuzhiyun if (strstr(dmi_product_name, "Z420") ||
555*4882a593Smuzhiyun strstr(dmi_product_name, "Z620") ||
556*4882a593Smuzhiyun strstr(dmi_product_name, "Z820") ||
557*4882a593Smuzhiyun strstr(dmi_product_name, "Z1 Workstation"))
558*4882a593Smuzhiyun return true;
559*4882a593Smuzhiyun
560*4882a593Smuzhiyun return false;
561*4882a593Smuzhiyun }
562*4882a593Smuzhiyun
xhci_all_ports_seen_u0(struct xhci_hcd * xhci)563*4882a593Smuzhiyun static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
564*4882a593Smuzhiyun {
565*4882a593Smuzhiyun return (xhci->port_status_u0 == ((1 << xhci->usb3_rhub.num_ports) - 1));
566*4882a593Smuzhiyun }
567*4882a593Smuzhiyun
568*4882a593Smuzhiyun
569*4882a593Smuzhiyun /*
570*4882a593Smuzhiyun * Initialize memory for HCD and xHC (one-time init).
571*4882a593Smuzhiyun *
572*4882a593Smuzhiyun * Program the PAGESIZE register, initialize the device context array, create
573*4882a593Smuzhiyun * device contexts (?), set up a command ring segment (or two?), create event
574*4882a593Smuzhiyun * ring (one for now).
575*4882a593Smuzhiyun */
xhci_init(struct usb_hcd * hcd)576*4882a593Smuzhiyun static int xhci_init(struct usb_hcd *hcd)
577*4882a593Smuzhiyun {
578*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
579*4882a593Smuzhiyun int retval = 0;
580*4882a593Smuzhiyun
581*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init");
582*4882a593Smuzhiyun spin_lock_init(&xhci->lock);
583*4882a593Smuzhiyun if (xhci->hci_version == 0x95 && link_quirk) {
584*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
585*4882a593Smuzhiyun "QUIRK: Not clearing Link TRB chain bits.");
586*4882a593Smuzhiyun xhci->quirks |= XHCI_LINK_TRB_QUIRK;
587*4882a593Smuzhiyun } else {
588*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
589*4882a593Smuzhiyun "xHCI doesn't need link TRB QUIRK");
590*4882a593Smuzhiyun }
591*4882a593Smuzhiyun retval = xhci_mem_init(xhci, GFP_KERNEL);
592*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init");
593*4882a593Smuzhiyun
594*4882a593Smuzhiyun /* Initializing Compliance Mode Recovery Data If Needed */
595*4882a593Smuzhiyun if (xhci_compliance_mode_recovery_timer_quirk_check()) {
596*4882a593Smuzhiyun xhci->quirks |= XHCI_COMP_MODE_QUIRK;
597*4882a593Smuzhiyun compliance_mode_recovery_timer_init(xhci);
598*4882a593Smuzhiyun }
599*4882a593Smuzhiyun
600*4882a593Smuzhiyun return retval;
601*4882a593Smuzhiyun }
602*4882a593Smuzhiyun
603*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
604*4882a593Smuzhiyun
605*4882a593Smuzhiyun
xhci_run_finished(struct xhci_hcd * xhci)606*4882a593Smuzhiyun static int xhci_run_finished(struct xhci_hcd *xhci)
607*4882a593Smuzhiyun {
608*4882a593Smuzhiyun if (xhci_start(xhci)) {
609*4882a593Smuzhiyun xhci_halt(xhci);
610*4882a593Smuzhiyun return -ENODEV;
611*4882a593Smuzhiyun }
612*4882a593Smuzhiyun xhci->shared_hcd->state = HC_STATE_RUNNING;
613*4882a593Smuzhiyun xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
614*4882a593Smuzhiyun
615*4882a593Smuzhiyun if (xhci->quirks & XHCI_NEC_HOST)
616*4882a593Smuzhiyun xhci_ring_cmd_db(xhci);
617*4882a593Smuzhiyun
618*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
619*4882a593Smuzhiyun "Finished xhci_run for USB3 roothub");
620*4882a593Smuzhiyun return 0;
621*4882a593Smuzhiyun }
622*4882a593Smuzhiyun
623*4882a593Smuzhiyun /*
624*4882a593Smuzhiyun * Start the HC after it was halted.
625*4882a593Smuzhiyun *
626*4882a593Smuzhiyun * This function is called by the USB core when the HC driver is added.
627*4882a593Smuzhiyun * Its opposite is xhci_stop().
628*4882a593Smuzhiyun *
629*4882a593Smuzhiyun * xhci_init() must be called once before this function can be called.
630*4882a593Smuzhiyun * Reset the HC, enable device slot contexts, program DCBAAP, and
631*4882a593Smuzhiyun * set command ring pointer and event ring pointer.
632*4882a593Smuzhiyun *
633*4882a593Smuzhiyun * Setup MSI-X vectors and enable interrupts.
634*4882a593Smuzhiyun */
xhci_run(struct usb_hcd * hcd)635*4882a593Smuzhiyun int xhci_run(struct usb_hcd *hcd)
636*4882a593Smuzhiyun {
637*4882a593Smuzhiyun u32 temp;
638*4882a593Smuzhiyun u64 temp_64;
639*4882a593Smuzhiyun int ret;
640*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
641*4882a593Smuzhiyun
642*4882a593Smuzhiyun /* Start the xHCI host controller running only after the USB 2.0 roothub
643*4882a593Smuzhiyun * is setup.
644*4882a593Smuzhiyun */
645*4882a593Smuzhiyun
646*4882a593Smuzhiyun hcd->uses_new_polling = 1;
647*4882a593Smuzhiyun if (!usb_hcd_is_primary_hcd(hcd))
648*4882a593Smuzhiyun return xhci_run_finished(xhci);
649*4882a593Smuzhiyun
650*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");
651*4882a593Smuzhiyun
652*4882a593Smuzhiyun ret = xhci_try_enable_msi(hcd);
653*4882a593Smuzhiyun if (ret)
654*4882a593Smuzhiyun return ret;
655*4882a593Smuzhiyun
656*4882a593Smuzhiyun temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
657*4882a593Smuzhiyun temp_64 &= ~ERST_PTR_MASK;
658*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
659*4882a593Smuzhiyun "ERST deq = 64'h%0lx", (long unsigned int) temp_64);
660*4882a593Smuzhiyun
661*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
662*4882a593Smuzhiyun "// Set the interrupt modulation register");
663*4882a593Smuzhiyun temp = readl(&xhci->ir_set->irq_control);
664*4882a593Smuzhiyun temp &= ~ER_IRQ_INTERVAL_MASK;
665*4882a593Smuzhiyun temp |= (xhci->imod_interval / 250) & ER_IRQ_INTERVAL_MASK;
666*4882a593Smuzhiyun writel(temp, &xhci->ir_set->irq_control);
667*4882a593Smuzhiyun
668*4882a593Smuzhiyun /* Set the HCD state before we enable the irqs */
669*4882a593Smuzhiyun temp = readl(&xhci->op_regs->command);
670*4882a593Smuzhiyun temp |= (CMD_EIE);
671*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
672*4882a593Smuzhiyun "// Enable interrupts, cmd = 0x%x.", temp);
673*4882a593Smuzhiyun writel(temp, &xhci->op_regs->command);
674*4882a593Smuzhiyun
675*4882a593Smuzhiyun temp = readl(&xhci->ir_set->irq_pending);
676*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
677*4882a593Smuzhiyun "// Enabling event ring interrupter %p by writing 0x%x to irq_pending",
678*4882a593Smuzhiyun xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
679*4882a593Smuzhiyun writel(ER_IRQ_ENABLE(temp), &xhci->ir_set->irq_pending);
680*4882a593Smuzhiyun
681*4882a593Smuzhiyun if (xhci->quirks & XHCI_NEC_HOST) {
682*4882a593Smuzhiyun struct xhci_command *command;
683*4882a593Smuzhiyun
684*4882a593Smuzhiyun command = xhci_alloc_command(xhci, false, GFP_KERNEL);
685*4882a593Smuzhiyun if (!command)
686*4882a593Smuzhiyun return -ENOMEM;
687*4882a593Smuzhiyun
688*4882a593Smuzhiyun ret = xhci_queue_vendor_command(xhci, command, 0, 0, 0,
689*4882a593Smuzhiyun TRB_TYPE(TRB_NEC_GET_FW));
690*4882a593Smuzhiyun if (ret)
691*4882a593Smuzhiyun xhci_free_command(xhci, command);
692*4882a593Smuzhiyun }
693*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
694*4882a593Smuzhiyun "Finished xhci_run for USB2 roothub");
695*4882a593Smuzhiyun
696*4882a593Smuzhiyun xhci_dbc_init(xhci);
697*4882a593Smuzhiyun
698*4882a593Smuzhiyun xhci_debugfs_init(xhci);
699*4882a593Smuzhiyun
700*4882a593Smuzhiyun return 0;
701*4882a593Smuzhiyun }
702*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_run);
703*4882a593Smuzhiyun
704*4882a593Smuzhiyun /*
705*4882a593Smuzhiyun * Stop xHCI driver.
706*4882a593Smuzhiyun *
707*4882a593Smuzhiyun * This function is called by the USB core when the HC driver is removed.
708*4882a593Smuzhiyun * Its opposite is xhci_run().
709*4882a593Smuzhiyun *
710*4882a593Smuzhiyun * Disable device contexts, disable IRQs, and quiesce the HC.
711*4882a593Smuzhiyun * Reset the HC, finish any completed transactions, and cleanup memory.
712*4882a593Smuzhiyun */
xhci_stop(struct usb_hcd * hcd)713*4882a593Smuzhiyun static void xhci_stop(struct usb_hcd *hcd)
714*4882a593Smuzhiyun {
715*4882a593Smuzhiyun u32 temp;
716*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
717*4882a593Smuzhiyun
718*4882a593Smuzhiyun mutex_lock(&xhci->mutex);
719*4882a593Smuzhiyun
720*4882a593Smuzhiyun /* Only halt host and free memory after both hcds are removed */
721*4882a593Smuzhiyun if (!usb_hcd_is_primary_hcd(hcd)) {
722*4882a593Smuzhiyun mutex_unlock(&xhci->mutex);
723*4882a593Smuzhiyun return;
724*4882a593Smuzhiyun }
725*4882a593Smuzhiyun
726*4882a593Smuzhiyun xhci_dbc_exit(xhci);
727*4882a593Smuzhiyun
728*4882a593Smuzhiyun spin_lock_irq(&xhci->lock);
729*4882a593Smuzhiyun xhci->xhc_state |= XHCI_STATE_HALTED;
730*4882a593Smuzhiyun xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
731*4882a593Smuzhiyun xhci_halt(xhci);
732*4882a593Smuzhiyun xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
733*4882a593Smuzhiyun spin_unlock_irq(&xhci->lock);
734*4882a593Smuzhiyun
735*4882a593Smuzhiyun xhci_cleanup_msix(xhci);
736*4882a593Smuzhiyun
737*4882a593Smuzhiyun /* Deleting Compliance Mode Recovery Timer */
738*4882a593Smuzhiyun if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
739*4882a593Smuzhiyun (!(xhci_all_ports_seen_u0(xhci)))) {
740*4882a593Smuzhiyun del_timer_sync(&xhci->comp_mode_recovery_timer);
741*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
742*4882a593Smuzhiyun "%s: compliance mode recovery timer deleted",
743*4882a593Smuzhiyun __func__);
744*4882a593Smuzhiyun }
745*4882a593Smuzhiyun
746*4882a593Smuzhiyun if (xhci->quirks & XHCI_AMD_PLL_FIX)
747*4882a593Smuzhiyun usb_amd_dev_put();
748*4882a593Smuzhiyun
749*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
750*4882a593Smuzhiyun "// Disabling event ring interrupts");
751*4882a593Smuzhiyun temp = readl(&xhci->op_regs->status);
752*4882a593Smuzhiyun writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
753*4882a593Smuzhiyun temp = readl(&xhci->ir_set->irq_pending);
754*4882a593Smuzhiyun writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
755*4882a593Smuzhiyun
756*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory");
757*4882a593Smuzhiyun xhci_mem_cleanup(xhci);
758*4882a593Smuzhiyun xhci_debugfs_exit(xhci);
759*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
760*4882a593Smuzhiyun "xhci_stop completed - status = %x",
761*4882a593Smuzhiyun readl(&xhci->op_regs->status));
762*4882a593Smuzhiyun mutex_unlock(&xhci->mutex);
763*4882a593Smuzhiyun }
764*4882a593Smuzhiyun
765*4882a593Smuzhiyun /*
766*4882a593Smuzhiyun * Shutdown HC (not bus-specific)
767*4882a593Smuzhiyun *
768*4882a593Smuzhiyun * This is called when the machine is rebooting or halting. We assume that the
769*4882a593Smuzhiyun * machine will be powered off, and the HC's internal state will be reset.
770*4882a593Smuzhiyun * Don't bother to free memory.
771*4882a593Smuzhiyun *
772*4882a593Smuzhiyun * This will only ever be called with the main usb_hcd (the USB3 roothub).
773*4882a593Smuzhiyun */
xhci_shutdown(struct usb_hcd * hcd)774*4882a593Smuzhiyun void xhci_shutdown(struct usb_hcd *hcd)
775*4882a593Smuzhiyun {
776*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
777*4882a593Smuzhiyun
778*4882a593Smuzhiyun if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
779*4882a593Smuzhiyun usb_disable_xhci_ports(to_pci_dev(hcd->self.sysdev));
780*4882a593Smuzhiyun
781*4882a593Smuzhiyun /* Don't poll the roothubs after shutdown. */
782*4882a593Smuzhiyun xhci_dbg(xhci, "%s: stopping usb%d port polling.\n",
783*4882a593Smuzhiyun __func__, hcd->self.busnum);
784*4882a593Smuzhiyun clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
785*4882a593Smuzhiyun del_timer_sync(&hcd->rh_timer);
786*4882a593Smuzhiyun
787*4882a593Smuzhiyun if (xhci->shared_hcd) {
788*4882a593Smuzhiyun clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
789*4882a593Smuzhiyun del_timer_sync(&xhci->shared_hcd->rh_timer);
790*4882a593Smuzhiyun }
791*4882a593Smuzhiyun
792*4882a593Smuzhiyun spin_lock_irq(&xhci->lock);
793*4882a593Smuzhiyun xhci_halt(xhci);
794*4882a593Smuzhiyun
795*4882a593Smuzhiyun /*
796*4882a593Smuzhiyun * Workaround for spurious wakeps at shutdown with HSW, and for boot
797*4882a593Smuzhiyun * firmware delay in ADL-P PCH if port are left in U3 at shutdown
798*4882a593Smuzhiyun */
799*4882a593Smuzhiyun if (xhci->quirks & XHCI_SPURIOUS_WAKEUP ||
800*4882a593Smuzhiyun xhci->quirks & XHCI_RESET_TO_DEFAULT)
801*4882a593Smuzhiyun xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
802*4882a593Smuzhiyun
803*4882a593Smuzhiyun spin_unlock_irq(&xhci->lock);
804*4882a593Smuzhiyun
805*4882a593Smuzhiyun xhci_cleanup_msix(xhci);
806*4882a593Smuzhiyun
807*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
808*4882a593Smuzhiyun "xhci_shutdown completed - status = %x",
809*4882a593Smuzhiyun readl(&xhci->op_regs->status));
810*4882a593Smuzhiyun }
811*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_shutdown);
812*4882a593Smuzhiyun
813*4882a593Smuzhiyun #ifdef CONFIG_PM
xhci_save_registers(struct xhci_hcd * xhci)814*4882a593Smuzhiyun static void xhci_save_registers(struct xhci_hcd *xhci)
815*4882a593Smuzhiyun {
816*4882a593Smuzhiyun xhci->s3.command = readl(&xhci->op_regs->command);
817*4882a593Smuzhiyun xhci->s3.dev_nt = readl(&xhci->op_regs->dev_notification);
818*4882a593Smuzhiyun xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
819*4882a593Smuzhiyun xhci->s3.config_reg = readl(&xhci->op_regs->config_reg);
820*4882a593Smuzhiyun xhci->s3.erst_size = readl(&xhci->ir_set->erst_size);
821*4882a593Smuzhiyun xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
822*4882a593Smuzhiyun xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
823*4882a593Smuzhiyun xhci->s3.irq_pending = readl(&xhci->ir_set->irq_pending);
824*4882a593Smuzhiyun xhci->s3.irq_control = readl(&xhci->ir_set->irq_control);
825*4882a593Smuzhiyun }
826*4882a593Smuzhiyun
xhci_restore_registers(struct xhci_hcd * xhci)827*4882a593Smuzhiyun static void xhci_restore_registers(struct xhci_hcd *xhci)
828*4882a593Smuzhiyun {
829*4882a593Smuzhiyun writel(xhci->s3.command, &xhci->op_regs->command);
830*4882a593Smuzhiyun writel(xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
831*4882a593Smuzhiyun xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
832*4882a593Smuzhiyun writel(xhci->s3.config_reg, &xhci->op_regs->config_reg);
833*4882a593Smuzhiyun writel(xhci->s3.erst_size, &xhci->ir_set->erst_size);
834*4882a593Smuzhiyun xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
835*4882a593Smuzhiyun xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
836*4882a593Smuzhiyun writel(xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
837*4882a593Smuzhiyun writel(xhci->s3.irq_control, &xhci->ir_set->irq_control);
838*4882a593Smuzhiyun }
839*4882a593Smuzhiyun
xhci_set_cmd_ring_deq(struct xhci_hcd * xhci)840*4882a593Smuzhiyun static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
841*4882a593Smuzhiyun {
842*4882a593Smuzhiyun u64 val_64;
843*4882a593Smuzhiyun
844*4882a593Smuzhiyun /* step 2: initialize command ring buffer */
845*4882a593Smuzhiyun val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
846*4882a593Smuzhiyun val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
847*4882a593Smuzhiyun (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
848*4882a593Smuzhiyun xhci->cmd_ring->dequeue) &
849*4882a593Smuzhiyun (u64) ~CMD_RING_RSVD_BITS) |
850*4882a593Smuzhiyun xhci->cmd_ring->cycle_state;
851*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_init,
852*4882a593Smuzhiyun "// Setting command ring address to 0x%llx",
853*4882a593Smuzhiyun (long unsigned long) val_64);
854*4882a593Smuzhiyun xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
855*4882a593Smuzhiyun }
856*4882a593Smuzhiyun
857*4882a593Smuzhiyun /*
858*4882a593Smuzhiyun * The whole command ring must be cleared to zero when we suspend the host.
859*4882a593Smuzhiyun *
860*4882a593Smuzhiyun * The host doesn't save the command ring pointer in the suspend well, so we
861*4882a593Smuzhiyun * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
862*4882a593Smuzhiyun * aligned, because of the reserved bits in the command ring dequeue pointer
863*4882a593Smuzhiyun * register. Therefore, we can't just set the dequeue pointer back in the
864*4882a593Smuzhiyun * middle of the ring (TRBs are 16-byte aligned).
865*4882a593Smuzhiyun */
xhci_clear_command_ring(struct xhci_hcd * xhci)866*4882a593Smuzhiyun static void xhci_clear_command_ring(struct xhci_hcd *xhci)
867*4882a593Smuzhiyun {
868*4882a593Smuzhiyun struct xhci_ring *ring;
869*4882a593Smuzhiyun struct xhci_segment *seg;
870*4882a593Smuzhiyun
871*4882a593Smuzhiyun ring = xhci->cmd_ring;
872*4882a593Smuzhiyun seg = ring->deq_seg;
873*4882a593Smuzhiyun do {
874*4882a593Smuzhiyun memset(seg->trbs, 0,
875*4882a593Smuzhiyun sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
876*4882a593Smuzhiyun seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
877*4882a593Smuzhiyun cpu_to_le32(~TRB_CYCLE);
878*4882a593Smuzhiyun seg = seg->next;
879*4882a593Smuzhiyun } while (seg != ring->deq_seg);
880*4882a593Smuzhiyun
881*4882a593Smuzhiyun /* Reset the software enqueue and dequeue pointers */
882*4882a593Smuzhiyun ring->deq_seg = ring->first_seg;
883*4882a593Smuzhiyun ring->dequeue = ring->first_seg->trbs;
884*4882a593Smuzhiyun ring->enq_seg = ring->deq_seg;
885*4882a593Smuzhiyun ring->enqueue = ring->dequeue;
886*4882a593Smuzhiyun
887*4882a593Smuzhiyun ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
888*4882a593Smuzhiyun /*
889*4882a593Smuzhiyun * Ring is now zeroed, so the HW should look for change of ownership
890*4882a593Smuzhiyun * when the cycle bit is set to 1.
891*4882a593Smuzhiyun */
892*4882a593Smuzhiyun ring->cycle_state = 1;
893*4882a593Smuzhiyun
894*4882a593Smuzhiyun /*
895*4882a593Smuzhiyun * Reset the hardware dequeue pointer.
896*4882a593Smuzhiyun * Yes, this will need to be re-written after resume, but we're paranoid
897*4882a593Smuzhiyun * and want to make sure the hardware doesn't access bogus memory
898*4882a593Smuzhiyun * because, say, the BIOS or an SMI started the host without changing
899*4882a593Smuzhiyun * the command ring pointers.
900*4882a593Smuzhiyun */
901*4882a593Smuzhiyun xhci_set_cmd_ring_deq(xhci);
902*4882a593Smuzhiyun }
903*4882a593Smuzhiyun
904*4882a593Smuzhiyun /*
905*4882a593Smuzhiyun * Disable port wake bits if do_wakeup is not set.
906*4882a593Smuzhiyun *
907*4882a593Smuzhiyun * Also clear a possible internal port wake state left hanging for ports that
908*4882a593Smuzhiyun * detected termination but never successfully enumerated (trained to 0U).
909*4882a593Smuzhiyun * Internal wake causes immediate xHCI wake after suspend. PORT_CSC write done
910*4882a593Smuzhiyun * at enumeration clears this wake, force one here as well for unconnected ports
911*4882a593Smuzhiyun */
912*4882a593Smuzhiyun
xhci_disable_hub_port_wake(struct xhci_hcd * xhci,struct xhci_hub * rhub,bool do_wakeup)913*4882a593Smuzhiyun static void xhci_disable_hub_port_wake(struct xhci_hcd *xhci,
914*4882a593Smuzhiyun struct xhci_hub *rhub,
915*4882a593Smuzhiyun bool do_wakeup)
916*4882a593Smuzhiyun {
917*4882a593Smuzhiyun unsigned long flags;
918*4882a593Smuzhiyun u32 t1, t2, portsc;
919*4882a593Smuzhiyun int i;
920*4882a593Smuzhiyun
921*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
922*4882a593Smuzhiyun
923*4882a593Smuzhiyun for (i = 0; i < rhub->num_ports; i++) {
924*4882a593Smuzhiyun portsc = readl(rhub->ports[i]->addr);
925*4882a593Smuzhiyun t1 = xhci_port_state_to_neutral(portsc);
926*4882a593Smuzhiyun t2 = t1;
927*4882a593Smuzhiyun
928*4882a593Smuzhiyun /* clear wake bits if do_wake is not set */
929*4882a593Smuzhiyun if (!do_wakeup)
930*4882a593Smuzhiyun t2 &= ~PORT_WAKE_BITS;
931*4882a593Smuzhiyun
932*4882a593Smuzhiyun /* Don't touch csc bit if connected or connect change is set */
933*4882a593Smuzhiyun if (!(portsc & (PORT_CSC | PORT_CONNECT)))
934*4882a593Smuzhiyun t2 |= PORT_CSC;
935*4882a593Smuzhiyun
936*4882a593Smuzhiyun if (t1 != t2) {
937*4882a593Smuzhiyun writel(t2, rhub->ports[i]->addr);
938*4882a593Smuzhiyun xhci_dbg(xhci, "config port %d-%d wake bits, portsc: 0x%x, write: 0x%x\n",
939*4882a593Smuzhiyun rhub->hcd->self.busnum, i + 1, portsc, t2);
940*4882a593Smuzhiyun }
941*4882a593Smuzhiyun }
942*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
943*4882a593Smuzhiyun }
944*4882a593Smuzhiyun
xhci_pending_portevent(struct xhci_hcd * xhci)945*4882a593Smuzhiyun static bool xhci_pending_portevent(struct xhci_hcd *xhci)
946*4882a593Smuzhiyun {
947*4882a593Smuzhiyun struct xhci_port **ports;
948*4882a593Smuzhiyun int port_index;
949*4882a593Smuzhiyun u32 status;
950*4882a593Smuzhiyun u32 portsc;
951*4882a593Smuzhiyun
952*4882a593Smuzhiyun status = readl(&xhci->op_regs->status);
953*4882a593Smuzhiyun if (status & STS_EINT)
954*4882a593Smuzhiyun return true;
955*4882a593Smuzhiyun /*
956*4882a593Smuzhiyun * Checking STS_EINT is not enough as there is a lag between a change
957*4882a593Smuzhiyun * bit being set and the Port Status Change Event that it generated
958*4882a593Smuzhiyun * being written to the Event Ring. See note in xhci 1.1 section 4.19.2.
959*4882a593Smuzhiyun */
960*4882a593Smuzhiyun
961*4882a593Smuzhiyun port_index = xhci->usb2_rhub.num_ports;
962*4882a593Smuzhiyun ports = xhci->usb2_rhub.ports;
963*4882a593Smuzhiyun while (port_index--) {
964*4882a593Smuzhiyun portsc = readl(ports[port_index]->addr);
965*4882a593Smuzhiyun if (portsc & PORT_CHANGE_MASK ||
966*4882a593Smuzhiyun (portsc & PORT_PLS_MASK) == XDEV_RESUME)
967*4882a593Smuzhiyun return true;
968*4882a593Smuzhiyun }
969*4882a593Smuzhiyun port_index = xhci->usb3_rhub.num_ports;
970*4882a593Smuzhiyun ports = xhci->usb3_rhub.ports;
971*4882a593Smuzhiyun while (port_index--) {
972*4882a593Smuzhiyun portsc = readl(ports[port_index]->addr);
973*4882a593Smuzhiyun if (portsc & PORT_CHANGE_MASK ||
974*4882a593Smuzhiyun (portsc & PORT_PLS_MASK) == XDEV_RESUME)
975*4882a593Smuzhiyun return true;
976*4882a593Smuzhiyun }
977*4882a593Smuzhiyun return false;
978*4882a593Smuzhiyun }
979*4882a593Smuzhiyun
980*4882a593Smuzhiyun /*
981*4882a593Smuzhiyun * Stop HC (not bus-specific)
982*4882a593Smuzhiyun *
983*4882a593Smuzhiyun * This is called when the machine transition into S3/S4 mode.
984*4882a593Smuzhiyun *
985*4882a593Smuzhiyun */
xhci_suspend(struct xhci_hcd * xhci,bool do_wakeup)986*4882a593Smuzhiyun int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup)
987*4882a593Smuzhiyun {
988*4882a593Smuzhiyun int rc = 0;
989*4882a593Smuzhiyun unsigned int delay = XHCI_MAX_HALT_USEC * 2;
990*4882a593Smuzhiyun struct usb_hcd *hcd = xhci_to_hcd(xhci);
991*4882a593Smuzhiyun u32 command;
992*4882a593Smuzhiyun u32 res;
993*4882a593Smuzhiyun
994*4882a593Smuzhiyun if (!hcd->state)
995*4882a593Smuzhiyun return 0;
996*4882a593Smuzhiyun
997*4882a593Smuzhiyun if (hcd->state != HC_STATE_SUSPENDED ||
998*4882a593Smuzhiyun xhci->shared_hcd->state != HC_STATE_SUSPENDED)
999*4882a593Smuzhiyun return -EINVAL;
1000*4882a593Smuzhiyun
1001*4882a593Smuzhiyun /* Clear root port wake on bits if wakeup not allowed. */
1002*4882a593Smuzhiyun xhci_disable_hub_port_wake(xhci, &xhci->usb3_rhub, do_wakeup);
1003*4882a593Smuzhiyun xhci_disable_hub_port_wake(xhci, &xhci->usb2_rhub, do_wakeup);
1004*4882a593Smuzhiyun
1005*4882a593Smuzhiyun if (!HCD_HW_ACCESSIBLE(hcd))
1006*4882a593Smuzhiyun return 0;
1007*4882a593Smuzhiyun
1008*4882a593Smuzhiyun xhci_dbc_suspend(xhci);
1009*4882a593Smuzhiyun
1010*4882a593Smuzhiyun /* Don't poll the roothubs on bus suspend. */
1011*4882a593Smuzhiyun xhci_dbg(xhci, "%s: stopping usb%d port polling.\n",
1012*4882a593Smuzhiyun __func__, hcd->self.busnum);
1013*4882a593Smuzhiyun clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1014*4882a593Smuzhiyun del_timer_sync(&hcd->rh_timer);
1015*4882a593Smuzhiyun clear_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
1016*4882a593Smuzhiyun del_timer_sync(&xhci->shared_hcd->rh_timer);
1017*4882a593Smuzhiyun
1018*4882a593Smuzhiyun if (xhci->quirks & XHCI_SUSPEND_DELAY)
1019*4882a593Smuzhiyun usleep_range(1000, 1500);
1020*4882a593Smuzhiyun
1021*4882a593Smuzhiyun spin_lock_irq(&xhci->lock);
1022*4882a593Smuzhiyun clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1023*4882a593Smuzhiyun clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
1024*4882a593Smuzhiyun /* step 1: stop endpoint */
1025*4882a593Smuzhiyun /* skipped assuming that port suspend has done */
1026*4882a593Smuzhiyun
1027*4882a593Smuzhiyun /* step 2: clear Run/Stop bit */
1028*4882a593Smuzhiyun command = readl(&xhci->op_regs->command);
1029*4882a593Smuzhiyun command &= ~CMD_RUN;
1030*4882a593Smuzhiyun writel(command, &xhci->op_regs->command);
1031*4882a593Smuzhiyun
1032*4882a593Smuzhiyun /* Some chips from Fresco Logic need an extraordinary delay */
1033*4882a593Smuzhiyun delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
1034*4882a593Smuzhiyun
1035*4882a593Smuzhiyun if (xhci_handshake(&xhci->op_regs->status,
1036*4882a593Smuzhiyun STS_HALT, STS_HALT, delay)) {
1037*4882a593Smuzhiyun xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
1038*4882a593Smuzhiyun spin_unlock_irq(&xhci->lock);
1039*4882a593Smuzhiyun return -ETIMEDOUT;
1040*4882a593Smuzhiyun }
1041*4882a593Smuzhiyun xhci_clear_command_ring(xhci);
1042*4882a593Smuzhiyun
1043*4882a593Smuzhiyun /* step 3: save registers */
1044*4882a593Smuzhiyun xhci_save_registers(xhci);
1045*4882a593Smuzhiyun
1046*4882a593Smuzhiyun /* step 4: set CSS flag */
1047*4882a593Smuzhiyun command = readl(&xhci->op_regs->command);
1048*4882a593Smuzhiyun command |= CMD_CSS;
1049*4882a593Smuzhiyun writel(command, &xhci->op_regs->command);
1050*4882a593Smuzhiyun xhci->broken_suspend = 0;
1051*4882a593Smuzhiyun if (xhci_handshake(&xhci->op_regs->status,
1052*4882a593Smuzhiyun STS_SAVE, 0, 20 * 1000)) {
1053*4882a593Smuzhiyun /*
1054*4882a593Smuzhiyun * AMD SNPS xHC 3.0 occasionally does not clear the
1055*4882a593Smuzhiyun * SSS bit of USBSTS and when driver tries to poll
1056*4882a593Smuzhiyun * to see if the xHC clears BIT(8) which never happens
1057*4882a593Smuzhiyun * and driver assumes that controller is not responding
1058*4882a593Smuzhiyun * and times out. To workaround this, its good to check
1059*4882a593Smuzhiyun * if SRE and HCE bits are not set (as per xhci
1060*4882a593Smuzhiyun * Section 5.4.2) and bypass the timeout.
1061*4882a593Smuzhiyun */
1062*4882a593Smuzhiyun res = readl(&xhci->op_regs->status);
1063*4882a593Smuzhiyun if ((xhci->quirks & XHCI_SNPS_BROKEN_SUSPEND) &&
1064*4882a593Smuzhiyun (((res & STS_SRE) == 0) &&
1065*4882a593Smuzhiyun ((res & STS_HCE) == 0))) {
1066*4882a593Smuzhiyun xhci->broken_suspend = 1;
1067*4882a593Smuzhiyun } else {
1068*4882a593Smuzhiyun xhci_warn(xhci, "WARN: xHC save state timeout\n");
1069*4882a593Smuzhiyun spin_unlock_irq(&xhci->lock);
1070*4882a593Smuzhiyun return -ETIMEDOUT;
1071*4882a593Smuzhiyun }
1072*4882a593Smuzhiyun }
1073*4882a593Smuzhiyun spin_unlock_irq(&xhci->lock);
1074*4882a593Smuzhiyun
1075*4882a593Smuzhiyun /*
1076*4882a593Smuzhiyun * Deleting Compliance Mode Recovery Timer because the xHCI Host
1077*4882a593Smuzhiyun * is about to be suspended.
1078*4882a593Smuzhiyun */
1079*4882a593Smuzhiyun if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1080*4882a593Smuzhiyun (!(xhci_all_ports_seen_u0(xhci)))) {
1081*4882a593Smuzhiyun del_timer_sync(&xhci->comp_mode_recovery_timer);
1082*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1083*4882a593Smuzhiyun "%s: compliance mode recovery timer deleted",
1084*4882a593Smuzhiyun __func__);
1085*4882a593Smuzhiyun }
1086*4882a593Smuzhiyun
1087*4882a593Smuzhiyun /* step 5: remove core well power */
1088*4882a593Smuzhiyun /* synchronize irq when using MSI-X */
1089*4882a593Smuzhiyun xhci_msix_sync_irqs(xhci);
1090*4882a593Smuzhiyun
1091*4882a593Smuzhiyun return rc;
1092*4882a593Smuzhiyun }
1093*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_suspend);
1094*4882a593Smuzhiyun
1095*4882a593Smuzhiyun /*
1096*4882a593Smuzhiyun * start xHC (not bus-specific)
1097*4882a593Smuzhiyun *
1098*4882a593Smuzhiyun * This is called when the machine transition from S3/S4 mode.
1099*4882a593Smuzhiyun *
1100*4882a593Smuzhiyun */
xhci_resume(struct xhci_hcd * xhci,bool hibernated)1101*4882a593Smuzhiyun int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
1102*4882a593Smuzhiyun {
1103*4882a593Smuzhiyun u32 command, temp = 0;
1104*4882a593Smuzhiyun struct usb_hcd *hcd = xhci_to_hcd(xhci);
1105*4882a593Smuzhiyun struct usb_hcd *secondary_hcd;
1106*4882a593Smuzhiyun int retval = 0;
1107*4882a593Smuzhiyun bool comp_timer_running = false;
1108*4882a593Smuzhiyun bool pending_portevent = false;
1109*4882a593Smuzhiyun bool reinit_xhc = false;
1110*4882a593Smuzhiyun
1111*4882a593Smuzhiyun if (!hcd->state)
1112*4882a593Smuzhiyun return 0;
1113*4882a593Smuzhiyun
1114*4882a593Smuzhiyun /* Wait a bit if either of the roothubs need to settle from the
1115*4882a593Smuzhiyun * transition into bus suspend.
1116*4882a593Smuzhiyun */
1117*4882a593Smuzhiyun
1118*4882a593Smuzhiyun if (time_before(jiffies, xhci->usb2_rhub.bus_state.next_statechange) ||
1119*4882a593Smuzhiyun time_before(jiffies, xhci->usb3_rhub.bus_state.next_statechange))
1120*4882a593Smuzhiyun msleep(100);
1121*4882a593Smuzhiyun
1122*4882a593Smuzhiyun set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1123*4882a593Smuzhiyun set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
1124*4882a593Smuzhiyun
1125*4882a593Smuzhiyun spin_lock_irq(&xhci->lock);
1126*4882a593Smuzhiyun
1127*4882a593Smuzhiyun if (hibernated || xhci->quirks & XHCI_RESET_ON_RESUME || xhci->broken_suspend)
1128*4882a593Smuzhiyun reinit_xhc = true;
1129*4882a593Smuzhiyun
1130*4882a593Smuzhiyun if (!reinit_xhc) {
1131*4882a593Smuzhiyun /*
1132*4882a593Smuzhiyun * Some controllers might lose power during suspend, so wait
1133*4882a593Smuzhiyun * for controller not ready bit to clear, just as in xHC init.
1134*4882a593Smuzhiyun */
1135*4882a593Smuzhiyun retval = xhci_handshake(&xhci->op_regs->status,
1136*4882a593Smuzhiyun STS_CNR, 0, 10 * 1000 * 1000);
1137*4882a593Smuzhiyun if (retval) {
1138*4882a593Smuzhiyun xhci_warn(xhci, "Controller not ready at resume %d\n",
1139*4882a593Smuzhiyun retval);
1140*4882a593Smuzhiyun spin_unlock_irq(&xhci->lock);
1141*4882a593Smuzhiyun return retval;
1142*4882a593Smuzhiyun }
1143*4882a593Smuzhiyun /* step 1: restore register */
1144*4882a593Smuzhiyun xhci_restore_registers(xhci);
1145*4882a593Smuzhiyun /* step 2: initialize command ring buffer */
1146*4882a593Smuzhiyun xhci_set_cmd_ring_deq(xhci);
1147*4882a593Smuzhiyun /* step 3: restore state and start state*/
1148*4882a593Smuzhiyun /* step 3: set CRS flag */
1149*4882a593Smuzhiyun command = readl(&xhci->op_regs->command);
1150*4882a593Smuzhiyun command |= CMD_CRS;
1151*4882a593Smuzhiyun writel(command, &xhci->op_regs->command);
1152*4882a593Smuzhiyun /*
1153*4882a593Smuzhiyun * Some controllers take up to 55+ ms to complete the controller
1154*4882a593Smuzhiyun * restore so setting the timeout to 100ms. Xhci specification
1155*4882a593Smuzhiyun * doesn't mention any timeout value.
1156*4882a593Smuzhiyun */
1157*4882a593Smuzhiyun if (xhci_handshake(&xhci->op_regs->status,
1158*4882a593Smuzhiyun STS_RESTORE, 0, 100 * 1000)) {
1159*4882a593Smuzhiyun xhci_warn(xhci, "WARN: xHC restore state timeout\n");
1160*4882a593Smuzhiyun spin_unlock_irq(&xhci->lock);
1161*4882a593Smuzhiyun return -ETIMEDOUT;
1162*4882a593Smuzhiyun }
1163*4882a593Smuzhiyun }
1164*4882a593Smuzhiyun
1165*4882a593Smuzhiyun temp = readl(&xhci->op_regs->status);
1166*4882a593Smuzhiyun
1167*4882a593Smuzhiyun /* re-initialize the HC on Restore Error, or Host Controller Error */
1168*4882a593Smuzhiyun if (temp & (STS_SRE | STS_HCE)) {
1169*4882a593Smuzhiyun reinit_xhc = true;
1170*4882a593Smuzhiyun if (!xhci->broken_suspend)
1171*4882a593Smuzhiyun xhci_warn(xhci, "xHC error in resume, USBSTS 0x%x, Reinit\n", temp);
1172*4882a593Smuzhiyun }
1173*4882a593Smuzhiyun
1174*4882a593Smuzhiyun if (reinit_xhc) {
1175*4882a593Smuzhiyun if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1176*4882a593Smuzhiyun !(xhci_all_ports_seen_u0(xhci))) {
1177*4882a593Smuzhiyun del_timer_sync(&xhci->comp_mode_recovery_timer);
1178*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1179*4882a593Smuzhiyun "Compliance Mode Recovery Timer deleted!");
1180*4882a593Smuzhiyun }
1181*4882a593Smuzhiyun
1182*4882a593Smuzhiyun /* Let the USB core know _both_ roothubs lost power. */
1183*4882a593Smuzhiyun usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
1184*4882a593Smuzhiyun usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
1185*4882a593Smuzhiyun
1186*4882a593Smuzhiyun xhci_dbg(xhci, "Stop HCD\n");
1187*4882a593Smuzhiyun xhci_halt(xhci);
1188*4882a593Smuzhiyun xhci_zero_64b_regs(xhci);
1189*4882a593Smuzhiyun retval = xhci_reset(xhci, XHCI_RESET_LONG_USEC);
1190*4882a593Smuzhiyun spin_unlock_irq(&xhci->lock);
1191*4882a593Smuzhiyun if (retval)
1192*4882a593Smuzhiyun return retval;
1193*4882a593Smuzhiyun xhci_cleanup_msix(xhci);
1194*4882a593Smuzhiyun
1195*4882a593Smuzhiyun xhci_dbg(xhci, "// Disabling event ring interrupts\n");
1196*4882a593Smuzhiyun temp = readl(&xhci->op_regs->status);
1197*4882a593Smuzhiyun writel((temp & ~0x1fff) | STS_EINT, &xhci->op_regs->status);
1198*4882a593Smuzhiyun temp = readl(&xhci->ir_set->irq_pending);
1199*4882a593Smuzhiyun writel(ER_IRQ_DISABLE(temp), &xhci->ir_set->irq_pending);
1200*4882a593Smuzhiyun
1201*4882a593Smuzhiyun xhci_dbg(xhci, "cleaning up memory\n");
1202*4882a593Smuzhiyun xhci_mem_cleanup(xhci);
1203*4882a593Smuzhiyun xhci_debugfs_exit(xhci);
1204*4882a593Smuzhiyun xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
1205*4882a593Smuzhiyun readl(&xhci->op_regs->status));
1206*4882a593Smuzhiyun
1207*4882a593Smuzhiyun /* USB core calls the PCI reinit and start functions twice:
1208*4882a593Smuzhiyun * first with the primary HCD, and then with the secondary HCD.
1209*4882a593Smuzhiyun * If we don't do the same, the host will never be started.
1210*4882a593Smuzhiyun */
1211*4882a593Smuzhiyun if (!usb_hcd_is_primary_hcd(hcd))
1212*4882a593Smuzhiyun secondary_hcd = hcd;
1213*4882a593Smuzhiyun else
1214*4882a593Smuzhiyun secondary_hcd = xhci->shared_hcd;
1215*4882a593Smuzhiyun
1216*4882a593Smuzhiyun xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1217*4882a593Smuzhiyun retval = xhci_init(hcd->primary_hcd);
1218*4882a593Smuzhiyun if (retval)
1219*4882a593Smuzhiyun return retval;
1220*4882a593Smuzhiyun comp_timer_running = true;
1221*4882a593Smuzhiyun
1222*4882a593Smuzhiyun xhci_dbg(xhci, "Start the primary HCD\n");
1223*4882a593Smuzhiyun retval = xhci_run(hcd->primary_hcd);
1224*4882a593Smuzhiyun if (!retval) {
1225*4882a593Smuzhiyun xhci_dbg(xhci, "Start the secondary HCD\n");
1226*4882a593Smuzhiyun retval = xhci_run(secondary_hcd);
1227*4882a593Smuzhiyun }
1228*4882a593Smuzhiyun hcd->state = HC_STATE_SUSPENDED;
1229*4882a593Smuzhiyun xhci->shared_hcd->state = HC_STATE_SUSPENDED;
1230*4882a593Smuzhiyun goto done;
1231*4882a593Smuzhiyun }
1232*4882a593Smuzhiyun
1233*4882a593Smuzhiyun /* step 4: set Run/Stop bit */
1234*4882a593Smuzhiyun command = readl(&xhci->op_regs->command);
1235*4882a593Smuzhiyun command |= CMD_RUN;
1236*4882a593Smuzhiyun writel(command, &xhci->op_regs->command);
1237*4882a593Smuzhiyun xhci_handshake(&xhci->op_regs->status, STS_HALT,
1238*4882a593Smuzhiyun 0, 250 * 1000);
1239*4882a593Smuzhiyun
1240*4882a593Smuzhiyun /* step 5: walk topology and initialize portsc,
1241*4882a593Smuzhiyun * portpmsc and portli
1242*4882a593Smuzhiyun */
1243*4882a593Smuzhiyun /* this is done in bus_resume */
1244*4882a593Smuzhiyun
1245*4882a593Smuzhiyun /* step 6: restart each of the previously
1246*4882a593Smuzhiyun * Running endpoints by ringing their doorbells
1247*4882a593Smuzhiyun */
1248*4882a593Smuzhiyun
1249*4882a593Smuzhiyun spin_unlock_irq(&xhci->lock);
1250*4882a593Smuzhiyun
1251*4882a593Smuzhiyun xhci_dbc_resume(xhci);
1252*4882a593Smuzhiyun
1253*4882a593Smuzhiyun done:
1254*4882a593Smuzhiyun if (retval == 0) {
1255*4882a593Smuzhiyun /*
1256*4882a593Smuzhiyun * Resume roothubs only if there are pending events.
1257*4882a593Smuzhiyun * USB 3 devices resend U3 LFPS wake after a 100ms delay if
1258*4882a593Smuzhiyun * the first wake signalling failed, give it that chance.
1259*4882a593Smuzhiyun */
1260*4882a593Smuzhiyun pending_portevent = xhci_pending_portevent(xhci);
1261*4882a593Smuzhiyun if (!pending_portevent) {
1262*4882a593Smuzhiyun msleep(120);
1263*4882a593Smuzhiyun pending_portevent = xhci_pending_portevent(xhci);
1264*4882a593Smuzhiyun }
1265*4882a593Smuzhiyun
1266*4882a593Smuzhiyun if (pending_portevent) {
1267*4882a593Smuzhiyun usb_hcd_resume_root_hub(xhci->shared_hcd);
1268*4882a593Smuzhiyun usb_hcd_resume_root_hub(hcd);
1269*4882a593Smuzhiyun }
1270*4882a593Smuzhiyun }
1271*4882a593Smuzhiyun /*
1272*4882a593Smuzhiyun * If system is subject to the Quirk, Compliance Mode Timer needs to
1273*4882a593Smuzhiyun * be re-initialized Always after a system resume. Ports are subject
1274*4882a593Smuzhiyun * to suffer the Compliance Mode issue again. It doesn't matter if
1275*4882a593Smuzhiyun * ports have entered previously to U0 before system's suspension.
1276*4882a593Smuzhiyun */
1277*4882a593Smuzhiyun if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
1278*4882a593Smuzhiyun compliance_mode_recovery_timer_init(xhci);
1279*4882a593Smuzhiyun
1280*4882a593Smuzhiyun if (xhci->quirks & XHCI_ASMEDIA_MODIFY_FLOWCONTROL)
1281*4882a593Smuzhiyun usb_asmedia_modifyflowcontrol(to_pci_dev(hcd->self.controller));
1282*4882a593Smuzhiyun
1283*4882a593Smuzhiyun /* Re-enable port polling. */
1284*4882a593Smuzhiyun xhci_dbg(xhci, "%s: starting usb%d port polling.\n",
1285*4882a593Smuzhiyun __func__, hcd->self.busnum);
1286*4882a593Smuzhiyun set_bit(HCD_FLAG_POLL_RH, &xhci->shared_hcd->flags);
1287*4882a593Smuzhiyun usb_hcd_poll_rh_status(xhci->shared_hcd);
1288*4882a593Smuzhiyun set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1289*4882a593Smuzhiyun usb_hcd_poll_rh_status(hcd);
1290*4882a593Smuzhiyun
1291*4882a593Smuzhiyun return retval;
1292*4882a593Smuzhiyun }
1293*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_resume);
1294*4882a593Smuzhiyun #endif /* CONFIG_PM */
1295*4882a593Smuzhiyun
1296*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
1297*4882a593Smuzhiyun
1298*4882a593Smuzhiyun /*
1299*4882a593Smuzhiyun * Bypass the DMA mapping if URB is suitable for Immediate Transfer (IDT),
1300*4882a593Smuzhiyun * we'll copy the actual data into the TRB address register. This is limited to
1301*4882a593Smuzhiyun * transfers up to 8 bytes on output endpoints of any kind with wMaxPacketSize
1302*4882a593Smuzhiyun * >= 8 bytes. If suitable for IDT only one Transfer TRB per TD is allowed.
1303*4882a593Smuzhiyun */
xhci_map_urb_for_dma(struct usb_hcd * hcd,struct urb * urb,gfp_t mem_flags)1304*4882a593Smuzhiyun static int xhci_map_urb_for_dma(struct usb_hcd *hcd, struct urb *urb,
1305*4882a593Smuzhiyun gfp_t mem_flags)
1306*4882a593Smuzhiyun {
1307*4882a593Smuzhiyun if (xhci_urb_suitable_for_idt(urb))
1308*4882a593Smuzhiyun return 0;
1309*4882a593Smuzhiyun
1310*4882a593Smuzhiyun return usb_hcd_map_urb_for_dma(hcd, urb, mem_flags);
1311*4882a593Smuzhiyun }
1312*4882a593Smuzhiyun
1313*4882a593Smuzhiyun /*
1314*4882a593Smuzhiyun * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1315*4882a593Smuzhiyun * HCDs. Find the index for an endpoint given its descriptor. Use the return
1316*4882a593Smuzhiyun * value to right shift 1 for the bitmask.
1317*4882a593Smuzhiyun *
1318*4882a593Smuzhiyun * Index = (epnum * 2) + direction - 1,
1319*4882a593Smuzhiyun * where direction = 0 for OUT, 1 for IN.
1320*4882a593Smuzhiyun * For control endpoints, the IN index is used (OUT index is unused), so
1321*4882a593Smuzhiyun * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1322*4882a593Smuzhiyun */
xhci_get_endpoint_index(struct usb_endpoint_descriptor * desc)1323*4882a593Smuzhiyun unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1324*4882a593Smuzhiyun {
1325*4882a593Smuzhiyun unsigned int index;
1326*4882a593Smuzhiyun if (usb_endpoint_xfer_control(desc))
1327*4882a593Smuzhiyun index = (unsigned int) (usb_endpoint_num(desc)*2);
1328*4882a593Smuzhiyun else
1329*4882a593Smuzhiyun index = (unsigned int) (usb_endpoint_num(desc)*2) +
1330*4882a593Smuzhiyun (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1331*4882a593Smuzhiyun return index;
1332*4882a593Smuzhiyun }
1333*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_get_endpoint_index);
1334*4882a593Smuzhiyun
1335*4882a593Smuzhiyun /* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1336*4882a593Smuzhiyun * address from the XHCI endpoint index.
1337*4882a593Smuzhiyun */
xhci_get_endpoint_address(unsigned int ep_index)1338*4882a593Smuzhiyun unsigned int xhci_get_endpoint_address(unsigned int ep_index)
1339*4882a593Smuzhiyun {
1340*4882a593Smuzhiyun unsigned int number = DIV_ROUND_UP(ep_index, 2);
1341*4882a593Smuzhiyun unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
1342*4882a593Smuzhiyun return direction | number;
1343*4882a593Smuzhiyun }
1344*4882a593Smuzhiyun
1345*4882a593Smuzhiyun /* Find the flag for this endpoint (for use in the control context). Use the
1346*4882a593Smuzhiyun * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1347*4882a593Smuzhiyun * bit 1, etc.
1348*4882a593Smuzhiyun */
xhci_get_endpoint_flag(struct usb_endpoint_descriptor * desc)1349*4882a593Smuzhiyun static unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
1350*4882a593Smuzhiyun {
1351*4882a593Smuzhiyun return 1 << (xhci_get_endpoint_index(desc) + 1);
1352*4882a593Smuzhiyun }
1353*4882a593Smuzhiyun
1354*4882a593Smuzhiyun /* Compute the last valid endpoint context index. Basically, this is the
1355*4882a593Smuzhiyun * endpoint index plus one. For slot contexts with more than valid endpoint,
1356*4882a593Smuzhiyun * we find the most significant bit set in the added contexts flags.
1357*4882a593Smuzhiyun * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1358*4882a593Smuzhiyun * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1359*4882a593Smuzhiyun */
xhci_last_valid_endpoint(u32 added_ctxs)1360*4882a593Smuzhiyun unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
1361*4882a593Smuzhiyun {
1362*4882a593Smuzhiyun return fls(added_ctxs) - 1;
1363*4882a593Smuzhiyun }
1364*4882a593Smuzhiyun
1365*4882a593Smuzhiyun /* Returns 1 if the arguments are OK;
1366*4882a593Smuzhiyun * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1367*4882a593Smuzhiyun */
xhci_check_args(struct usb_hcd * hcd,struct usb_device * udev,struct usb_host_endpoint * ep,int check_ep,bool check_virt_dev,const char * func)1368*4882a593Smuzhiyun static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
1369*4882a593Smuzhiyun struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1370*4882a593Smuzhiyun const char *func) {
1371*4882a593Smuzhiyun struct xhci_hcd *xhci;
1372*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
1373*4882a593Smuzhiyun
1374*4882a593Smuzhiyun if (!hcd || (check_ep && !ep) || !udev) {
1375*4882a593Smuzhiyun pr_debug("xHCI %s called with invalid args\n", func);
1376*4882a593Smuzhiyun return -EINVAL;
1377*4882a593Smuzhiyun }
1378*4882a593Smuzhiyun if (!udev->parent) {
1379*4882a593Smuzhiyun pr_debug("xHCI %s called for root hub\n", func);
1380*4882a593Smuzhiyun return 0;
1381*4882a593Smuzhiyun }
1382*4882a593Smuzhiyun
1383*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
1384*4882a593Smuzhiyun if (check_virt_dev) {
1385*4882a593Smuzhiyun if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
1386*4882a593Smuzhiyun xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
1387*4882a593Smuzhiyun func);
1388*4882a593Smuzhiyun return -EINVAL;
1389*4882a593Smuzhiyun }
1390*4882a593Smuzhiyun
1391*4882a593Smuzhiyun virt_dev = xhci->devs[udev->slot_id];
1392*4882a593Smuzhiyun if (virt_dev->udev != udev) {
1393*4882a593Smuzhiyun xhci_dbg(xhci, "xHCI %s called with udev and "
1394*4882a593Smuzhiyun "virt_dev does not match\n", func);
1395*4882a593Smuzhiyun return -EINVAL;
1396*4882a593Smuzhiyun }
1397*4882a593Smuzhiyun }
1398*4882a593Smuzhiyun
1399*4882a593Smuzhiyun if (xhci->xhc_state & XHCI_STATE_HALTED)
1400*4882a593Smuzhiyun return -ENODEV;
1401*4882a593Smuzhiyun
1402*4882a593Smuzhiyun return 1;
1403*4882a593Smuzhiyun }
1404*4882a593Smuzhiyun
1405*4882a593Smuzhiyun static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1406*4882a593Smuzhiyun struct usb_device *udev, struct xhci_command *command,
1407*4882a593Smuzhiyun bool ctx_change, bool must_succeed);
1408*4882a593Smuzhiyun
1409*4882a593Smuzhiyun /*
1410*4882a593Smuzhiyun * Full speed devices may have a max packet size greater than 8 bytes, but the
1411*4882a593Smuzhiyun * USB core doesn't know that until it reads the first 8 bytes of the
1412*4882a593Smuzhiyun * descriptor. If the usb_device's max packet size changes after that point,
1413*4882a593Smuzhiyun * we need to issue an evaluate context command and wait on it.
1414*4882a593Smuzhiyun */
xhci_check_maxpacket(struct xhci_hcd * xhci,unsigned int slot_id,unsigned int ep_index,struct urb * urb,gfp_t mem_flags)1415*4882a593Smuzhiyun static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1416*4882a593Smuzhiyun unsigned int ep_index, struct urb *urb, gfp_t mem_flags)
1417*4882a593Smuzhiyun {
1418*4882a593Smuzhiyun struct xhci_container_ctx *out_ctx;
1419*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
1420*4882a593Smuzhiyun struct xhci_ep_ctx *ep_ctx;
1421*4882a593Smuzhiyun struct xhci_command *command;
1422*4882a593Smuzhiyun int max_packet_size;
1423*4882a593Smuzhiyun int hw_max_packet_size;
1424*4882a593Smuzhiyun int ret = 0;
1425*4882a593Smuzhiyun
1426*4882a593Smuzhiyun out_ctx = xhci->devs[slot_id]->out_ctx;
1427*4882a593Smuzhiyun ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1428*4882a593Smuzhiyun hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
1429*4882a593Smuzhiyun max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
1430*4882a593Smuzhiyun if (hw_max_packet_size != max_packet_size) {
1431*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1432*4882a593Smuzhiyun "Max Packet Size for ep 0 changed.");
1433*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1434*4882a593Smuzhiyun "Max packet size in usb_device = %d",
1435*4882a593Smuzhiyun max_packet_size);
1436*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1437*4882a593Smuzhiyun "Max packet size in xHCI HW = %d",
1438*4882a593Smuzhiyun hw_max_packet_size);
1439*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1440*4882a593Smuzhiyun "Issuing evaluate context command.");
1441*4882a593Smuzhiyun
1442*4882a593Smuzhiyun /* Set up the input context flags for the command */
1443*4882a593Smuzhiyun /* FIXME: This won't work if a non-default control endpoint
1444*4882a593Smuzhiyun * changes max packet sizes.
1445*4882a593Smuzhiyun */
1446*4882a593Smuzhiyun
1447*4882a593Smuzhiyun command = xhci_alloc_command(xhci, true, mem_flags);
1448*4882a593Smuzhiyun if (!command)
1449*4882a593Smuzhiyun return -ENOMEM;
1450*4882a593Smuzhiyun
1451*4882a593Smuzhiyun command->in_ctx = xhci->devs[slot_id]->in_ctx;
1452*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
1453*4882a593Smuzhiyun if (!ctrl_ctx) {
1454*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1455*4882a593Smuzhiyun __func__);
1456*4882a593Smuzhiyun ret = -ENOMEM;
1457*4882a593Smuzhiyun goto command_cleanup;
1458*4882a593Smuzhiyun }
1459*4882a593Smuzhiyun /* Set up the modified control endpoint 0 */
1460*4882a593Smuzhiyun xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1461*4882a593Smuzhiyun xhci->devs[slot_id]->out_ctx, ep_index);
1462*4882a593Smuzhiyun
1463*4882a593Smuzhiyun ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
1464*4882a593Smuzhiyun ep_ctx->ep_info &= cpu_to_le32(~EP_STATE_MASK);/* must clear */
1465*4882a593Smuzhiyun ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1466*4882a593Smuzhiyun ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
1467*4882a593Smuzhiyun
1468*4882a593Smuzhiyun ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
1469*4882a593Smuzhiyun ctrl_ctx->drop_flags = 0;
1470*4882a593Smuzhiyun
1471*4882a593Smuzhiyun ret = xhci_configure_endpoint(xhci, urb->dev, command,
1472*4882a593Smuzhiyun true, false);
1473*4882a593Smuzhiyun
1474*4882a593Smuzhiyun /* Clean up the input context for later use by bandwidth
1475*4882a593Smuzhiyun * functions.
1476*4882a593Smuzhiyun */
1477*4882a593Smuzhiyun ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
1478*4882a593Smuzhiyun command_cleanup:
1479*4882a593Smuzhiyun kfree(command->completion);
1480*4882a593Smuzhiyun kfree(command);
1481*4882a593Smuzhiyun }
1482*4882a593Smuzhiyun return ret;
1483*4882a593Smuzhiyun }
1484*4882a593Smuzhiyun
1485*4882a593Smuzhiyun /*
1486*4882a593Smuzhiyun * non-error returns are a promise to giveback() the urb later
1487*4882a593Smuzhiyun * we drop ownership so next owner (or urb unlink) can get it
1488*4882a593Smuzhiyun */
xhci_urb_enqueue(struct usb_hcd * hcd,struct urb * urb,gfp_t mem_flags)1489*4882a593Smuzhiyun static int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1490*4882a593Smuzhiyun {
1491*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1492*4882a593Smuzhiyun unsigned long flags;
1493*4882a593Smuzhiyun int ret = 0;
1494*4882a593Smuzhiyun unsigned int slot_id, ep_index;
1495*4882a593Smuzhiyun unsigned int *ep_state;
1496*4882a593Smuzhiyun struct urb_priv *urb_priv;
1497*4882a593Smuzhiyun int num_tds;
1498*4882a593Smuzhiyun
1499*4882a593Smuzhiyun if (!urb)
1500*4882a593Smuzhiyun return -EINVAL;
1501*4882a593Smuzhiyun ret = xhci_check_args(hcd, urb->dev, urb->ep,
1502*4882a593Smuzhiyun true, true, __func__);
1503*4882a593Smuzhiyun if (ret <= 0)
1504*4882a593Smuzhiyun return ret ? ret : -EINVAL;
1505*4882a593Smuzhiyun
1506*4882a593Smuzhiyun slot_id = urb->dev->slot_id;
1507*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1508*4882a593Smuzhiyun ep_state = &xhci->devs[slot_id]->eps[ep_index].ep_state;
1509*4882a593Smuzhiyun
1510*4882a593Smuzhiyun if (!HCD_HW_ACCESSIBLE(hcd)) {
1511*4882a593Smuzhiyun if (!in_interrupt())
1512*4882a593Smuzhiyun xhci_dbg(xhci, "urb submitted during PCI suspend\n");
1513*4882a593Smuzhiyun return -ESHUTDOWN;
1514*4882a593Smuzhiyun }
1515*4882a593Smuzhiyun if (xhci->devs[slot_id]->flags & VDEV_PORT_ERROR) {
1516*4882a593Smuzhiyun xhci_dbg(xhci, "Can't queue urb, port error, link inactive\n");
1517*4882a593Smuzhiyun return -ENODEV;
1518*4882a593Smuzhiyun }
1519*4882a593Smuzhiyun
1520*4882a593Smuzhiyun if (xhci_vendor_usb_offload_skip_urb(xhci, urb)) {
1521*4882a593Smuzhiyun xhci_dbg(xhci, "skip urb for usb offload\n");
1522*4882a593Smuzhiyun return -EOPNOTSUPP;
1523*4882a593Smuzhiyun }
1524*4882a593Smuzhiyun
1525*4882a593Smuzhiyun if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1526*4882a593Smuzhiyun num_tds = urb->number_of_packets;
1527*4882a593Smuzhiyun else if (usb_endpoint_is_bulk_out(&urb->ep->desc) &&
1528*4882a593Smuzhiyun urb->transfer_buffer_length > 0 &&
1529*4882a593Smuzhiyun urb->transfer_flags & URB_ZERO_PACKET &&
1530*4882a593Smuzhiyun !(urb->transfer_buffer_length % usb_endpoint_maxp(&urb->ep->desc)))
1531*4882a593Smuzhiyun num_tds = 2;
1532*4882a593Smuzhiyun else
1533*4882a593Smuzhiyun num_tds = 1;
1534*4882a593Smuzhiyun
1535*4882a593Smuzhiyun urb_priv = kzalloc(struct_size(urb_priv, td, num_tds), mem_flags);
1536*4882a593Smuzhiyun if (!urb_priv)
1537*4882a593Smuzhiyun return -ENOMEM;
1538*4882a593Smuzhiyun
1539*4882a593Smuzhiyun urb_priv->num_tds = num_tds;
1540*4882a593Smuzhiyun urb_priv->num_tds_done = 0;
1541*4882a593Smuzhiyun urb->hcpriv = urb_priv;
1542*4882a593Smuzhiyun
1543*4882a593Smuzhiyun trace_xhci_urb_enqueue(urb);
1544*4882a593Smuzhiyun
1545*4882a593Smuzhiyun if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1546*4882a593Smuzhiyun /* Check to see if the max packet size for the default control
1547*4882a593Smuzhiyun * endpoint changed during FS device enumeration
1548*4882a593Smuzhiyun */
1549*4882a593Smuzhiyun if (urb->dev->speed == USB_SPEED_FULL) {
1550*4882a593Smuzhiyun ret = xhci_check_maxpacket(xhci, slot_id,
1551*4882a593Smuzhiyun ep_index, urb, mem_flags);
1552*4882a593Smuzhiyun if (ret < 0) {
1553*4882a593Smuzhiyun xhci_urb_free_priv(urb_priv);
1554*4882a593Smuzhiyun urb->hcpriv = NULL;
1555*4882a593Smuzhiyun return ret;
1556*4882a593Smuzhiyun }
1557*4882a593Smuzhiyun }
1558*4882a593Smuzhiyun }
1559*4882a593Smuzhiyun
1560*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
1561*4882a593Smuzhiyun
1562*4882a593Smuzhiyun if (xhci->xhc_state & XHCI_STATE_DYING) {
1563*4882a593Smuzhiyun xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for non-responsive xHCI host.\n",
1564*4882a593Smuzhiyun urb->ep->desc.bEndpointAddress, urb);
1565*4882a593Smuzhiyun ret = -ESHUTDOWN;
1566*4882a593Smuzhiyun goto free_priv;
1567*4882a593Smuzhiyun }
1568*4882a593Smuzhiyun if (*ep_state & (EP_GETTING_STREAMS | EP_GETTING_NO_STREAMS)) {
1569*4882a593Smuzhiyun xhci_warn(xhci, "WARN: Can't enqueue URB, ep in streams transition state %x\n",
1570*4882a593Smuzhiyun *ep_state);
1571*4882a593Smuzhiyun ret = -EINVAL;
1572*4882a593Smuzhiyun goto free_priv;
1573*4882a593Smuzhiyun }
1574*4882a593Smuzhiyun if (*ep_state & EP_SOFT_CLEAR_TOGGLE) {
1575*4882a593Smuzhiyun xhci_warn(xhci, "Can't enqueue URB while manually clearing toggle\n");
1576*4882a593Smuzhiyun ret = -EINVAL;
1577*4882a593Smuzhiyun goto free_priv;
1578*4882a593Smuzhiyun }
1579*4882a593Smuzhiyun
1580*4882a593Smuzhiyun switch (usb_endpoint_type(&urb->ep->desc)) {
1581*4882a593Smuzhiyun
1582*4882a593Smuzhiyun case USB_ENDPOINT_XFER_CONTROL:
1583*4882a593Smuzhiyun ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
1584*4882a593Smuzhiyun slot_id, ep_index);
1585*4882a593Smuzhiyun break;
1586*4882a593Smuzhiyun case USB_ENDPOINT_XFER_BULK:
1587*4882a593Smuzhiyun ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1588*4882a593Smuzhiyun slot_id, ep_index);
1589*4882a593Smuzhiyun break;
1590*4882a593Smuzhiyun case USB_ENDPOINT_XFER_INT:
1591*4882a593Smuzhiyun ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1592*4882a593Smuzhiyun slot_id, ep_index);
1593*4882a593Smuzhiyun break;
1594*4882a593Smuzhiyun case USB_ENDPOINT_XFER_ISOC:
1595*4882a593Smuzhiyun ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1596*4882a593Smuzhiyun slot_id, ep_index);
1597*4882a593Smuzhiyun }
1598*4882a593Smuzhiyun
1599*4882a593Smuzhiyun if (ret) {
1600*4882a593Smuzhiyun free_priv:
1601*4882a593Smuzhiyun xhci_urb_free_priv(urb_priv);
1602*4882a593Smuzhiyun urb->hcpriv = NULL;
1603*4882a593Smuzhiyun }
1604*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
1605*4882a593Smuzhiyun return ret;
1606*4882a593Smuzhiyun }
1607*4882a593Smuzhiyun
1608*4882a593Smuzhiyun /*
1609*4882a593Smuzhiyun * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1610*4882a593Smuzhiyun * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1611*4882a593Smuzhiyun * should pick up where it left off in the TD, unless a Set Transfer Ring
1612*4882a593Smuzhiyun * Dequeue Pointer is issued.
1613*4882a593Smuzhiyun *
1614*4882a593Smuzhiyun * The TRBs that make up the buffers for the canceled URB will be "removed" from
1615*4882a593Smuzhiyun * the ring. Since the ring is a contiguous structure, they can't be physically
1616*4882a593Smuzhiyun * removed. Instead, there are two options:
1617*4882a593Smuzhiyun *
1618*4882a593Smuzhiyun * 1) If the HC is in the middle of processing the URB to be canceled, we
1619*4882a593Smuzhiyun * simply move the ring's dequeue pointer past those TRBs using the Set
1620*4882a593Smuzhiyun * Transfer Ring Dequeue Pointer command. This will be the common case,
1621*4882a593Smuzhiyun * when drivers timeout on the last submitted URB and attempt to cancel.
1622*4882a593Smuzhiyun *
1623*4882a593Smuzhiyun * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1624*4882a593Smuzhiyun * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1625*4882a593Smuzhiyun * HC will need to invalidate the any TRBs it has cached after the stop
1626*4882a593Smuzhiyun * endpoint command, as noted in the xHCI 0.95 errata.
1627*4882a593Smuzhiyun *
1628*4882a593Smuzhiyun * 3) The TD may have completed by the time the Stop Endpoint Command
1629*4882a593Smuzhiyun * completes, so software needs to handle that case too.
1630*4882a593Smuzhiyun *
1631*4882a593Smuzhiyun * This function should protect against the TD enqueueing code ringing the
1632*4882a593Smuzhiyun * doorbell while this code is waiting for a Stop Endpoint command to complete.
1633*4882a593Smuzhiyun * It also needs to account for multiple cancellations on happening at the same
1634*4882a593Smuzhiyun * time for the same endpoint.
1635*4882a593Smuzhiyun *
1636*4882a593Smuzhiyun * Note that this function can be called in any context, or so says
1637*4882a593Smuzhiyun * usb_hcd_unlink_urb()
1638*4882a593Smuzhiyun */
xhci_urb_dequeue(struct usb_hcd * hcd,struct urb * urb,int status)1639*4882a593Smuzhiyun static int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1640*4882a593Smuzhiyun {
1641*4882a593Smuzhiyun unsigned long flags;
1642*4882a593Smuzhiyun int ret, i;
1643*4882a593Smuzhiyun u32 temp;
1644*4882a593Smuzhiyun struct xhci_hcd *xhci;
1645*4882a593Smuzhiyun struct urb_priv *urb_priv;
1646*4882a593Smuzhiyun struct xhci_td *td;
1647*4882a593Smuzhiyun unsigned int ep_index;
1648*4882a593Smuzhiyun struct xhci_ring *ep_ring;
1649*4882a593Smuzhiyun struct xhci_virt_ep *ep;
1650*4882a593Smuzhiyun struct xhci_command *command;
1651*4882a593Smuzhiyun struct xhci_virt_device *vdev;
1652*4882a593Smuzhiyun
1653*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
1654*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
1655*4882a593Smuzhiyun
1656*4882a593Smuzhiyun trace_xhci_urb_dequeue(urb);
1657*4882a593Smuzhiyun
1658*4882a593Smuzhiyun /* Make sure the URB hasn't completed or been unlinked already */
1659*4882a593Smuzhiyun ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1660*4882a593Smuzhiyun if (ret)
1661*4882a593Smuzhiyun goto done;
1662*4882a593Smuzhiyun
1663*4882a593Smuzhiyun /* give back URB now if we can't queue it for cancel */
1664*4882a593Smuzhiyun vdev = xhci->devs[urb->dev->slot_id];
1665*4882a593Smuzhiyun urb_priv = urb->hcpriv;
1666*4882a593Smuzhiyun if (!vdev || !urb_priv)
1667*4882a593Smuzhiyun goto err_giveback;
1668*4882a593Smuzhiyun
1669*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1670*4882a593Smuzhiyun ep = &vdev->eps[ep_index];
1671*4882a593Smuzhiyun ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1672*4882a593Smuzhiyun if (!ep || !ep_ring)
1673*4882a593Smuzhiyun goto err_giveback;
1674*4882a593Smuzhiyun
1675*4882a593Smuzhiyun /* If xHC is dead take it down and return ALL URBs in xhci_hc_died() */
1676*4882a593Smuzhiyun temp = readl(&xhci->op_regs->status);
1677*4882a593Smuzhiyun if (temp == ~(u32)0 || xhci->xhc_state & XHCI_STATE_DYING) {
1678*4882a593Smuzhiyun xhci_hc_died(xhci);
1679*4882a593Smuzhiyun goto done;
1680*4882a593Smuzhiyun }
1681*4882a593Smuzhiyun
1682*4882a593Smuzhiyun /*
1683*4882a593Smuzhiyun * check ring is not re-allocated since URB was enqueued. If it is, then
1684*4882a593Smuzhiyun * make sure none of the ring related pointers in this URB private data
1685*4882a593Smuzhiyun * are touched, such as td_list, otherwise we overwrite freed data
1686*4882a593Smuzhiyun */
1687*4882a593Smuzhiyun if (!td_on_ring(&urb_priv->td[0], ep_ring)) {
1688*4882a593Smuzhiyun xhci_err(xhci, "Canceled URB td not found on endpoint ring");
1689*4882a593Smuzhiyun for (i = urb_priv->num_tds_done; i < urb_priv->num_tds; i++) {
1690*4882a593Smuzhiyun td = &urb_priv->td[i];
1691*4882a593Smuzhiyun if (!list_empty(&td->cancelled_td_list))
1692*4882a593Smuzhiyun list_del_init(&td->cancelled_td_list);
1693*4882a593Smuzhiyun }
1694*4882a593Smuzhiyun goto err_giveback;
1695*4882a593Smuzhiyun }
1696*4882a593Smuzhiyun
1697*4882a593Smuzhiyun if (xhci->xhc_state & XHCI_STATE_HALTED) {
1698*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1699*4882a593Smuzhiyun "HC halted, freeing TD manually.");
1700*4882a593Smuzhiyun for (i = urb_priv->num_tds_done;
1701*4882a593Smuzhiyun i < urb_priv->num_tds;
1702*4882a593Smuzhiyun i++) {
1703*4882a593Smuzhiyun td = &urb_priv->td[i];
1704*4882a593Smuzhiyun if (!list_empty(&td->td_list))
1705*4882a593Smuzhiyun list_del_init(&td->td_list);
1706*4882a593Smuzhiyun if (!list_empty(&td->cancelled_td_list))
1707*4882a593Smuzhiyun list_del_init(&td->cancelled_td_list);
1708*4882a593Smuzhiyun }
1709*4882a593Smuzhiyun goto err_giveback;
1710*4882a593Smuzhiyun }
1711*4882a593Smuzhiyun
1712*4882a593Smuzhiyun i = urb_priv->num_tds_done;
1713*4882a593Smuzhiyun if (i < urb_priv->num_tds)
1714*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1715*4882a593Smuzhiyun "Cancel URB %p, dev %s, ep 0x%x, "
1716*4882a593Smuzhiyun "starting at offset 0x%llx",
1717*4882a593Smuzhiyun urb, urb->dev->devpath,
1718*4882a593Smuzhiyun urb->ep->desc.bEndpointAddress,
1719*4882a593Smuzhiyun (unsigned long long) xhci_trb_virt_to_dma(
1720*4882a593Smuzhiyun urb_priv->td[i].start_seg,
1721*4882a593Smuzhiyun urb_priv->td[i].first_trb));
1722*4882a593Smuzhiyun
1723*4882a593Smuzhiyun for (; i < urb_priv->num_tds; i++) {
1724*4882a593Smuzhiyun td = &urb_priv->td[i];
1725*4882a593Smuzhiyun /* TD can already be on cancelled list if ep halted on it */
1726*4882a593Smuzhiyun if (list_empty(&td->cancelled_td_list)) {
1727*4882a593Smuzhiyun td->cancel_status = TD_DIRTY;
1728*4882a593Smuzhiyun list_add_tail(&td->cancelled_td_list,
1729*4882a593Smuzhiyun &ep->cancelled_td_list);
1730*4882a593Smuzhiyun }
1731*4882a593Smuzhiyun }
1732*4882a593Smuzhiyun
1733*4882a593Smuzhiyun /* Queue a stop endpoint command, but only if this is
1734*4882a593Smuzhiyun * the first cancellation to be handled.
1735*4882a593Smuzhiyun */
1736*4882a593Smuzhiyun if (!(ep->ep_state & EP_STOP_CMD_PENDING)) {
1737*4882a593Smuzhiyun command = xhci_alloc_command(xhci, false, GFP_ATOMIC);
1738*4882a593Smuzhiyun if (!command) {
1739*4882a593Smuzhiyun ret = -ENOMEM;
1740*4882a593Smuzhiyun goto done;
1741*4882a593Smuzhiyun }
1742*4882a593Smuzhiyun ep->ep_state |= EP_STOP_CMD_PENDING;
1743*4882a593Smuzhiyun ep->stop_cmd_timer.expires = jiffies +
1744*4882a593Smuzhiyun XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1745*4882a593Smuzhiyun add_timer(&ep->stop_cmd_timer);
1746*4882a593Smuzhiyun xhci_queue_stop_endpoint(xhci, command, urb->dev->slot_id,
1747*4882a593Smuzhiyun ep_index, 0);
1748*4882a593Smuzhiyun xhci_ring_cmd_db(xhci);
1749*4882a593Smuzhiyun }
1750*4882a593Smuzhiyun done:
1751*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
1752*4882a593Smuzhiyun return ret;
1753*4882a593Smuzhiyun
1754*4882a593Smuzhiyun err_giveback:
1755*4882a593Smuzhiyun if (urb_priv)
1756*4882a593Smuzhiyun xhci_urb_free_priv(urb_priv);
1757*4882a593Smuzhiyun usb_hcd_unlink_urb_from_ep(hcd, urb);
1758*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
1759*4882a593Smuzhiyun usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
1760*4882a593Smuzhiyun return ret;
1761*4882a593Smuzhiyun }
1762*4882a593Smuzhiyun
1763*4882a593Smuzhiyun /* Drop an endpoint from a new bandwidth configuration for this device.
1764*4882a593Smuzhiyun * Only one call to this function is allowed per endpoint before
1765*4882a593Smuzhiyun * check_bandwidth() or reset_bandwidth() must be called.
1766*4882a593Smuzhiyun * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1767*4882a593Smuzhiyun * add the endpoint to the schedule with possibly new parameters denoted by a
1768*4882a593Smuzhiyun * different endpoint descriptor in usb_host_endpoint.
1769*4882a593Smuzhiyun * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1770*4882a593Smuzhiyun * not allowed.
1771*4882a593Smuzhiyun *
1772*4882a593Smuzhiyun * The USB core will not allow URBs to be queued to an endpoint that is being
1773*4882a593Smuzhiyun * disabled, so there's no need for mutual exclusion to protect
1774*4882a593Smuzhiyun * the xhci->devs[slot_id] structure.
1775*4882a593Smuzhiyun */
xhci_drop_endpoint(struct usb_hcd * hcd,struct usb_device * udev,struct usb_host_endpoint * ep)1776*4882a593Smuzhiyun int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1777*4882a593Smuzhiyun struct usb_host_endpoint *ep)
1778*4882a593Smuzhiyun {
1779*4882a593Smuzhiyun struct xhci_hcd *xhci;
1780*4882a593Smuzhiyun struct xhci_container_ctx *in_ctx, *out_ctx;
1781*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
1782*4882a593Smuzhiyun unsigned int ep_index;
1783*4882a593Smuzhiyun struct xhci_ep_ctx *ep_ctx;
1784*4882a593Smuzhiyun u32 drop_flag;
1785*4882a593Smuzhiyun u32 new_add_flags, new_drop_flags;
1786*4882a593Smuzhiyun int ret;
1787*4882a593Smuzhiyun
1788*4882a593Smuzhiyun ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
1789*4882a593Smuzhiyun if (ret <= 0)
1790*4882a593Smuzhiyun return ret;
1791*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
1792*4882a593Smuzhiyun if (xhci->xhc_state & XHCI_STATE_DYING)
1793*4882a593Smuzhiyun return -ENODEV;
1794*4882a593Smuzhiyun
1795*4882a593Smuzhiyun xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1796*4882a593Smuzhiyun drop_flag = xhci_get_endpoint_flag(&ep->desc);
1797*4882a593Smuzhiyun if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1798*4882a593Smuzhiyun xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1799*4882a593Smuzhiyun __func__, drop_flag);
1800*4882a593Smuzhiyun return 0;
1801*4882a593Smuzhiyun }
1802*4882a593Smuzhiyun
1803*4882a593Smuzhiyun in_ctx = xhci->devs[udev->slot_id]->in_ctx;
1804*4882a593Smuzhiyun out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1805*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
1806*4882a593Smuzhiyun if (!ctrl_ctx) {
1807*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1808*4882a593Smuzhiyun __func__);
1809*4882a593Smuzhiyun return 0;
1810*4882a593Smuzhiyun }
1811*4882a593Smuzhiyun
1812*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&ep->desc);
1813*4882a593Smuzhiyun ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1814*4882a593Smuzhiyun /* If the HC already knows the endpoint is disabled,
1815*4882a593Smuzhiyun * or the HCD has noted it is disabled, ignore this request
1816*4882a593Smuzhiyun */
1817*4882a593Smuzhiyun if ((GET_EP_CTX_STATE(ep_ctx) == EP_STATE_DISABLED) ||
1818*4882a593Smuzhiyun le32_to_cpu(ctrl_ctx->drop_flags) &
1819*4882a593Smuzhiyun xhci_get_endpoint_flag(&ep->desc)) {
1820*4882a593Smuzhiyun /* Do not warn when called after a usb_device_reset */
1821*4882a593Smuzhiyun if (xhci->devs[udev->slot_id]->eps[ep_index].ring != NULL)
1822*4882a593Smuzhiyun xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1823*4882a593Smuzhiyun __func__, ep);
1824*4882a593Smuzhiyun return 0;
1825*4882a593Smuzhiyun }
1826*4882a593Smuzhiyun
1827*4882a593Smuzhiyun ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1828*4882a593Smuzhiyun new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
1829*4882a593Smuzhiyun
1830*4882a593Smuzhiyun ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1831*4882a593Smuzhiyun new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1832*4882a593Smuzhiyun
1833*4882a593Smuzhiyun xhci_debugfs_remove_endpoint(xhci, xhci->devs[udev->slot_id], ep_index);
1834*4882a593Smuzhiyun
1835*4882a593Smuzhiyun xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1836*4882a593Smuzhiyun
1837*4882a593Smuzhiyun xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
1838*4882a593Smuzhiyun (unsigned int) ep->desc.bEndpointAddress,
1839*4882a593Smuzhiyun udev->slot_id,
1840*4882a593Smuzhiyun (unsigned int) new_drop_flags,
1841*4882a593Smuzhiyun (unsigned int) new_add_flags);
1842*4882a593Smuzhiyun return 0;
1843*4882a593Smuzhiyun }
1844*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_drop_endpoint);
1845*4882a593Smuzhiyun
1846*4882a593Smuzhiyun /* Add an endpoint to a new possible bandwidth configuration for this device.
1847*4882a593Smuzhiyun * Only one call to this function is allowed per endpoint before
1848*4882a593Smuzhiyun * check_bandwidth() or reset_bandwidth() must be called.
1849*4882a593Smuzhiyun * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1850*4882a593Smuzhiyun * add the endpoint to the schedule with possibly new parameters denoted by a
1851*4882a593Smuzhiyun * different endpoint descriptor in usb_host_endpoint.
1852*4882a593Smuzhiyun * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1853*4882a593Smuzhiyun * not allowed.
1854*4882a593Smuzhiyun *
1855*4882a593Smuzhiyun * The USB core will not allow URBs to be queued to an endpoint until the
1856*4882a593Smuzhiyun * configuration or alt setting is installed in the device, so there's no need
1857*4882a593Smuzhiyun * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1858*4882a593Smuzhiyun */
xhci_add_endpoint(struct usb_hcd * hcd,struct usb_device * udev,struct usb_host_endpoint * ep)1859*4882a593Smuzhiyun int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1860*4882a593Smuzhiyun struct usb_host_endpoint *ep)
1861*4882a593Smuzhiyun {
1862*4882a593Smuzhiyun struct xhci_hcd *xhci;
1863*4882a593Smuzhiyun struct xhci_container_ctx *in_ctx;
1864*4882a593Smuzhiyun unsigned int ep_index;
1865*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
1866*4882a593Smuzhiyun struct xhci_ep_ctx *ep_ctx;
1867*4882a593Smuzhiyun u32 added_ctxs;
1868*4882a593Smuzhiyun u32 new_add_flags, new_drop_flags;
1869*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
1870*4882a593Smuzhiyun int ret = 0;
1871*4882a593Smuzhiyun
1872*4882a593Smuzhiyun ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
1873*4882a593Smuzhiyun if (ret <= 0) {
1874*4882a593Smuzhiyun /* So we won't queue a reset ep command for a root hub */
1875*4882a593Smuzhiyun ep->hcpriv = NULL;
1876*4882a593Smuzhiyun return ret;
1877*4882a593Smuzhiyun }
1878*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
1879*4882a593Smuzhiyun if (xhci->xhc_state & XHCI_STATE_DYING)
1880*4882a593Smuzhiyun return -ENODEV;
1881*4882a593Smuzhiyun
1882*4882a593Smuzhiyun added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1883*4882a593Smuzhiyun if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1884*4882a593Smuzhiyun /* FIXME when we have to issue an evaluate endpoint command to
1885*4882a593Smuzhiyun * deal with ep0 max packet size changing once we get the
1886*4882a593Smuzhiyun * descriptors
1887*4882a593Smuzhiyun */
1888*4882a593Smuzhiyun xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1889*4882a593Smuzhiyun __func__, added_ctxs);
1890*4882a593Smuzhiyun return 0;
1891*4882a593Smuzhiyun }
1892*4882a593Smuzhiyun
1893*4882a593Smuzhiyun virt_dev = xhci->devs[udev->slot_id];
1894*4882a593Smuzhiyun in_ctx = virt_dev->in_ctx;
1895*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
1896*4882a593Smuzhiyun if (!ctrl_ctx) {
1897*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1898*4882a593Smuzhiyun __func__);
1899*4882a593Smuzhiyun return 0;
1900*4882a593Smuzhiyun }
1901*4882a593Smuzhiyun
1902*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&ep->desc);
1903*4882a593Smuzhiyun /* If this endpoint is already in use, and the upper layers are trying
1904*4882a593Smuzhiyun * to add it again without dropping it, reject the addition.
1905*4882a593Smuzhiyun */
1906*4882a593Smuzhiyun if (virt_dev->eps[ep_index].ring &&
1907*4882a593Smuzhiyun !(le32_to_cpu(ctrl_ctx->drop_flags) & added_ctxs)) {
1908*4882a593Smuzhiyun xhci_warn(xhci, "Trying to add endpoint 0x%x "
1909*4882a593Smuzhiyun "without dropping it.\n",
1910*4882a593Smuzhiyun (unsigned int) ep->desc.bEndpointAddress);
1911*4882a593Smuzhiyun return -EINVAL;
1912*4882a593Smuzhiyun }
1913*4882a593Smuzhiyun
1914*4882a593Smuzhiyun /* If the HCD has already noted the endpoint is enabled,
1915*4882a593Smuzhiyun * ignore this request.
1916*4882a593Smuzhiyun */
1917*4882a593Smuzhiyun if (le32_to_cpu(ctrl_ctx->add_flags) & added_ctxs) {
1918*4882a593Smuzhiyun xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1919*4882a593Smuzhiyun __func__, ep);
1920*4882a593Smuzhiyun return 0;
1921*4882a593Smuzhiyun }
1922*4882a593Smuzhiyun
1923*4882a593Smuzhiyun /*
1924*4882a593Smuzhiyun * Configuration and alternate setting changes must be done in
1925*4882a593Smuzhiyun * process context, not interrupt context (or so documenation
1926*4882a593Smuzhiyun * for usb_set_interface() and usb_set_configuration() claim).
1927*4882a593Smuzhiyun */
1928*4882a593Smuzhiyun if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
1929*4882a593Smuzhiyun dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1930*4882a593Smuzhiyun __func__, ep->desc.bEndpointAddress);
1931*4882a593Smuzhiyun return -ENOMEM;
1932*4882a593Smuzhiyun }
1933*4882a593Smuzhiyun
1934*4882a593Smuzhiyun ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1935*4882a593Smuzhiyun new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1936*4882a593Smuzhiyun
1937*4882a593Smuzhiyun /* If xhci_endpoint_disable() was called for this endpoint, but the
1938*4882a593Smuzhiyun * xHC hasn't been notified yet through the check_bandwidth() call,
1939*4882a593Smuzhiyun * this re-adds a new state for the endpoint from the new endpoint
1940*4882a593Smuzhiyun * descriptors. We must drop and re-add this endpoint, so we leave the
1941*4882a593Smuzhiyun * drop flags alone.
1942*4882a593Smuzhiyun */
1943*4882a593Smuzhiyun new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
1944*4882a593Smuzhiyun
1945*4882a593Smuzhiyun /* Store the usb_device pointer for later use */
1946*4882a593Smuzhiyun ep->hcpriv = udev;
1947*4882a593Smuzhiyun
1948*4882a593Smuzhiyun ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
1949*4882a593Smuzhiyun trace_xhci_add_endpoint(ep_ctx);
1950*4882a593Smuzhiyun
1951*4882a593Smuzhiyun xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x\n",
1952*4882a593Smuzhiyun (unsigned int) ep->desc.bEndpointAddress,
1953*4882a593Smuzhiyun udev->slot_id,
1954*4882a593Smuzhiyun (unsigned int) new_drop_flags,
1955*4882a593Smuzhiyun (unsigned int) new_add_flags);
1956*4882a593Smuzhiyun return 0;
1957*4882a593Smuzhiyun }
1958*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_add_endpoint);
1959*4882a593Smuzhiyun
xhci_zero_in_ctx(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev)1960*4882a593Smuzhiyun static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
1961*4882a593Smuzhiyun {
1962*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
1963*4882a593Smuzhiyun struct xhci_ep_ctx *ep_ctx;
1964*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
1965*4882a593Smuzhiyun int i;
1966*4882a593Smuzhiyun
1967*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
1968*4882a593Smuzhiyun if (!ctrl_ctx) {
1969*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1970*4882a593Smuzhiyun __func__);
1971*4882a593Smuzhiyun return;
1972*4882a593Smuzhiyun }
1973*4882a593Smuzhiyun
1974*4882a593Smuzhiyun /* When a device's add flag and drop flag are zero, any subsequent
1975*4882a593Smuzhiyun * configure endpoint command will leave that endpoint's state
1976*4882a593Smuzhiyun * untouched. Make sure we don't leave any old state in the input
1977*4882a593Smuzhiyun * endpoint contexts.
1978*4882a593Smuzhiyun */
1979*4882a593Smuzhiyun ctrl_ctx->drop_flags = 0;
1980*4882a593Smuzhiyun ctrl_ctx->add_flags = 0;
1981*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1982*4882a593Smuzhiyun slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1983*4882a593Smuzhiyun /* Endpoint 0 is always valid */
1984*4882a593Smuzhiyun slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
1985*4882a593Smuzhiyun for (i = 1; i < 31; i++) {
1986*4882a593Smuzhiyun ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
1987*4882a593Smuzhiyun ep_ctx->ep_info = 0;
1988*4882a593Smuzhiyun ep_ctx->ep_info2 = 0;
1989*4882a593Smuzhiyun ep_ctx->deq = 0;
1990*4882a593Smuzhiyun ep_ctx->tx_info = 0;
1991*4882a593Smuzhiyun }
1992*4882a593Smuzhiyun }
1993*4882a593Smuzhiyun
xhci_configure_endpoint_result(struct xhci_hcd * xhci,struct usb_device * udev,u32 * cmd_status)1994*4882a593Smuzhiyun static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
1995*4882a593Smuzhiyun struct usb_device *udev, u32 *cmd_status)
1996*4882a593Smuzhiyun {
1997*4882a593Smuzhiyun int ret;
1998*4882a593Smuzhiyun
1999*4882a593Smuzhiyun switch (*cmd_status) {
2000*4882a593Smuzhiyun case COMP_COMMAND_ABORTED:
2001*4882a593Smuzhiyun case COMP_COMMAND_RING_STOPPED:
2002*4882a593Smuzhiyun xhci_warn(xhci, "Timeout while waiting for configure endpoint command\n");
2003*4882a593Smuzhiyun ret = -ETIME;
2004*4882a593Smuzhiyun break;
2005*4882a593Smuzhiyun case COMP_RESOURCE_ERROR:
2006*4882a593Smuzhiyun dev_warn(&udev->dev,
2007*4882a593Smuzhiyun "Not enough host controller resources for new device state.\n");
2008*4882a593Smuzhiyun ret = -ENOMEM;
2009*4882a593Smuzhiyun /* FIXME: can we allocate more resources for the HC? */
2010*4882a593Smuzhiyun break;
2011*4882a593Smuzhiyun case COMP_BANDWIDTH_ERROR:
2012*4882a593Smuzhiyun case COMP_SECONDARY_BANDWIDTH_ERROR:
2013*4882a593Smuzhiyun dev_warn(&udev->dev,
2014*4882a593Smuzhiyun "Not enough bandwidth for new device state.\n");
2015*4882a593Smuzhiyun ret = -ENOSPC;
2016*4882a593Smuzhiyun /* FIXME: can we go back to the old state? */
2017*4882a593Smuzhiyun break;
2018*4882a593Smuzhiyun case COMP_TRB_ERROR:
2019*4882a593Smuzhiyun /* the HCD set up something wrong */
2020*4882a593Smuzhiyun dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
2021*4882a593Smuzhiyun "add flag = 1, "
2022*4882a593Smuzhiyun "and endpoint is not disabled.\n");
2023*4882a593Smuzhiyun ret = -EINVAL;
2024*4882a593Smuzhiyun break;
2025*4882a593Smuzhiyun case COMP_INCOMPATIBLE_DEVICE_ERROR:
2026*4882a593Smuzhiyun dev_warn(&udev->dev,
2027*4882a593Smuzhiyun "ERROR: Incompatible device for endpoint configure command.\n");
2028*4882a593Smuzhiyun ret = -ENODEV;
2029*4882a593Smuzhiyun break;
2030*4882a593Smuzhiyun case COMP_SUCCESS:
2031*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
2032*4882a593Smuzhiyun "Successful Endpoint Configure command");
2033*4882a593Smuzhiyun ret = 0;
2034*4882a593Smuzhiyun break;
2035*4882a593Smuzhiyun default:
2036*4882a593Smuzhiyun xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
2037*4882a593Smuzhiyun *cmd_status);
2038*4882a593Smuzhiyun ret = -EINVAL;
2039*4882a593Smuzhiyun break;
2040*4882a593Smuzhiyun }
2041*4882a593Smuzhiyun return ret;
2042*4882a593Smuzhiyun }
2043*4882a593Smuzhiyun
xhci_evaluate_context_result(struct xhci_hcd * xhci,struct usb_device * udev,u32 * cmd_status)2044*4882a593Smuzhiyun static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
2045*4882a593Smuzhiyun struct usb_device *udev, u32 *cmd_status)
2046*4882a593Smuzhiyun {
2047*4882a593Smuzhiyun int ret;
2048*4882a593Smuzhiyun
2049*4882a593Smuzhiyun switch (*cmd_status) {
2050*4882a593Smuzhiyun case COMP_COMMAND_ABORTED:
2051*4882a593Smuzhiyun case COMP_COMMAND_RING_STOPPED:
2052*4882a593Smuzhiyun xhci_warn(xhci, "Timeout while waiting for evaluate context command\n");
2053*4882a593Smuzhiyun ret = -ETIME;
2054*4882a593Smuzhiyun break;
2055*4882a593Smuzhiyun case COMP_PARAMETER_ERROR:
2056*4882a593Smuzhiyun dev_warn(&udev->dev,
2057*4882a593Smuzhiyun "WARN: xHCI driver setup invalid evaluate context command.\n");
2058*4882a593Smuzhiyun ret = -EINVAL;
2059*4882a593Smuzhiyun break;
2060*4882a593Smuzhiyun case COMP_SLOT_NOT_ENABLED_ERROR:
2061*4882a593Smuzhiyun dev_warn(&udev->dev,
2062*4882a593Smuzhiyun "WARN: slot not enabled for evaluate context command.\n");
2063*4882a593Smuzhiyun ret = -EINVAL;
2064*4882a593Smuzhiyun break;
2065*4882a593Smuzhiyun case COMP_CONTEXT_STATE_ERROR:
2066*4882a593Smuzhiyun dev_warn(&udev->dev,
2067*4882a593Smuzhiyun "WARN: invalid context state for evaluate context command.\n");
2068*4882a593Smuzhiyun ret = -EINVAL;
2069*4882a593Smuzhiyun break;
2070*4882a593Smuzhiyun case COMP_INCOMPATIBLE_DEVICE_ERROR:
2071*4882a593Smuzhiyun dev_warn(&udev->dev,
2072*4882a593Smuzhiyun "ERROR: Incompatible device for evaluate context command.\n");
2073*4882a593Smuzhiyun ret = -ENODEV;
2074*4882a593Smuzhiyun break;
2075*4882a593Smuzhiyun case COMP_MAX_EXIT_LATENCY_TOO_LARGE_ERROR:
2076*4882a593Smuzhiyun /* Max Exit Latency too large error */
2077*4882a593Smuzhiyun dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
2078*4882a593Smuzhiyun ret = -EINVAL;
2079*4882a593Smuzhiyun break;
2080*4882a593Smuzhiyun case COMP_SUCCESS:
2081*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
2082*4882a593Smuzhiyun "Successful evaluate context command");
2083*4882a593Smuzhiyun ret = 0;
2084*4882a593Smuzhiyun break;
2085*4882a593Smuzhiyun default:
2086*4882a593Smuzhiyun xhci_err(xhci, "ERROR: unexpected command completion code 0x%x.\n",
2087*4882a593Smuzhiyun *cmd_status);
2088*4882a593Smuzhiyun ret = -EINVAL;
2089*4882a593Smuzhiyun break;
2090*4882a593Smuzhiyun }
2091*4882a593Smuzhiyun return ret;
2092*4882a593Smuzhiyun }
2093*4882a593Smuzhiyun
xhci_count_num_new_endpoints(struct xhci_hcd * xhci,struct xhci_input_control_ctx * ctrl_ctx)2094*4882a593Smuzhiyun static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
2095*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx)
2096*4882a593Smuzhiyun {
2097*4882a593Smuzhiyun u32 valid_add_flags;
2098*4882a593Smuzhiyun u32 valid_drop_flags;
2099*4882a593Smuzhiyun
2100*4882a593Smuzhiyun /* Ignore the slot flag (bit 0), and the default control endpoint flag
2101*4882a593Smuzhiyun * (bit 1). The default control endpoint is added during the Address
2102*4882a593Smuzhiyun * Device command and is never removed until the slot is disabled.
2103*4882a593Smuzhiyun */
2104*4882a593Smuzhiyun valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
2105*4882a593Smuzhiyun valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
2106*4882a593Smuzhiyun
2107*4882a593Smuzhiyun /* Use hweight32 to count the number of ones in the add flags, or
2108*4882a593Smuzhiyun * number of endpoints added. Don't count endpoints that are changed
2109*4882a593Smuzhiyun * (both added and dropped).
2110*4882a593Smuzhiyun */
2111*4882a593Smuzhiyun return hweight32(valid_add_flags) -
2112*4882a593Smuzhiyun hweight32(valid_add_flags & valid_drop_flags);
2113*4882a593Smuzhiyun }
2114*4882a593Smuzhiyun
xhci_count_num_dropped_endpoints(struct xhci_hcd * xhci,struct xhci_input_control_ctx * ctrl_ctx)2115*4882a593Smuzhiyun static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
2116*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx)
2117*4882a593Smuzhiyun {
2118*4882a593Smuzhiyun u32 valid_add_flags;
2119*4882a593Smuzhiyun u32 valid_drop_flags;
2120*4882a593Smuzhiyun
2121*4882a593Smuzhiyun valid_add_flags = le32_to_cpu(ctrl_ctx->add_flags) >> 2;
2122*4882a593Smuzhiyun valid_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags) >> 2;
2123*4882a593Smuzhiyun
2124*4882a593Smuzhiyun return hweight32(valid_drop_flags) -
2125*4882a593Smuzhiyun hweight32(valid_add_flags & valid_drop_flags);
2126*4882a593Smuzhiyun }
2127*4882a593Smuzhiyun
2128*4882a593Smuzhiyun /*
2129*4882a593Smuzhiyun * We need to reserve the new number of endpoints before the configure endpoint
2130*4882a593Smuzhiyun * command completes. We can't subtract the dropped endpoints from the number
2131*4882a593Smuzhiyun * of active endpoints until the command completes because we can oversubscribe
2132*4882a593Smuzhiyun * the host in this case:
2133*4882a593Smuzhiyun *
2134*4882a593Smuzhiyun * - the first configure endpoint command drops more endpoints than it adds
2135*4882a593Smuzhiyun * - a second configure endpoint command that adds more endpoints is queued
2136*4882a593Smuzhiyun * - the first configure endpoint command fails, so the config is unchanged
2137*4882a593Smuzhiyun * - the second command may succeed, even though there isn't enough resources
2138*4882a593Smuzhiyun *
2139*4882a593Smuzhiyun * Must be called with xhci->lock held.
2140*4882a593Smuzhiyun */
xhci_reserve_host_resources(struct xhci_hcd * xhci,struct xhci_input_control_ctx * ctrl_ctx)2141*4882a593Smuzhiyun static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
2142*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx)
2143*4882a593Smuzhiyun {
2144*4882a593Smuzhiyun u32 added_eps;
2145*4882a593Smuzhiyun
2146*4882a593Smuzhiyun added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
2147*4882a593Smuzhiyun if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
2148*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2149*4882a593Smuzhiyun "Not enough ep ctxs: "
2150*4882a593Smuzhiyun "%u active, need to add %u, limit is %u.",
2151*4882a593Smuzhiyun xhci->num_active_eps, added_eps,
2152*4882a593Smuzhiyun xhci->limit_active_eps);
2153*4882a593Smuzhiyun return -ENOMEM;
2154*4882a593Smuzhiyun }
2155*4882a593Smuzhiyun xhci->num_active_eps += added_eps;
2156*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2157*4882a593Smuzhiyun "Adding %u ep ctxs, %u now active.", added_eps,
2158*4882a593Smuzhiyun xhci->num_active_eps);
2159*4882a593Smuzhiyun return 0;
2160*4882a593Smuzhiyun }
2161*4882a593Smuzhiyun
2162*4882a593Smuzhiyun /*
2163*4882a593Smuzhiyun * The configure endpoint was failed by the xHC for some other reason, so we
2164*4882a593Smuzhiyun * need to revert the resources that failed configuration would have used.
2165*4882a593Smuzhiyun *
2166*4882a593Smuzhiyun * Must be called with xhci->lock held.
2167*4882a593Smuzhiyun */
xhci_free_host_resources(struct xhci_hcd * xhci,struct xhci_input_control_ctx * ctrl_ctx)2168*4882a593Smuzhiyun static void xhci_free_host_resources(struct xhci_hcd *xhci,
2169*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx)
2170*4882a593Smuzhiyun {
2171*4882a593Smuzhiyun u32 num_failed_eps;
2172*4882a593Smuzhiyun
2173*4882a593Smuzhiyun num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
2174*4882a593Smuzhiyun xhci->num_active_eps -= num_failed_eps;
2175*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2176*4882a593Smuzhiyun "Removing %u failed ep ctxs, %u now active.",
2177*4882a593Smuzhiyun num_failed_eps,
2178*4882a593Smuzhiyun xhci->num_active_eps);
2179*4882a593Smuzhiyun }
2180*4882a593Smuzhiyun
2181*4882a593Smuzhiyun /*
2182*4882a593Smuzhiyun * Now that the command has completed, clean up the active endpoint count by
2183*4882a593Smuzhiyun * subtracting out the endpoints that were dropped (but not changed).
2184*4882a593Smuzhiyun *
2185*4882a593Smuzhiyun * Must be called with xhci->lock held.
2186*4882a593Smuzhiyun */
xhci_finish_resource_reservation(struct xhci_hcd * xhci,struct xhci_input_control_ctx * ctrl_ctx)2187*4882a593Smuzhiyun static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
2188*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx)
2189*4882a593Smuzhiyun {
2190*4882a593Smuzhiyun u32 num_dropped_eps;
2191*4882a593Smuzhiyun
2192*4882a593Smuzhiyun num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx);
2193*4882a593Smuzhiyun xhci->num_active_eps -= num_dropped_eps;
2194*4882a593Smuzhiyun if (num_dropped_eps)
2195*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2196*4882a593Smuzhiyun "Removing %u dropped ep ctxs, %u now active.",
2197*4882a593Smuzhiyun num_dropped_eps,
2198*4882a593Smuzhiyun xhci->num_active_eps);
2199*4882a593Smuzhiyun }
2200*4882a593Smuzhiyun
xhci_get_block_size(struct usb_device * udev)2201*4882a593Smuzhiyun static unsigned int xhci_get_block_size(struct usb_device *udev)
2202*4882a593Smuzhiyun {
2203*4882a593Smuzhiyun switch (udev->speed) {
2204*4882a593Smuzhiyun case USB_SPEED_LOW:
2205*4882a593Smuzhiyun case USB_SPEED_FULL:
2206*4882a593Smuzhiyun return FS_BLOCK;
2207*4882a593Smuzhiyun case USB_SPEED_HIGH:
2208*4882a593Smuzhiyun return HS_BLOCK;
2209*4882a593Smuzhiyun case USB_SPEED_SUPER:
2210*4882a593Smuzhiyun case USB_SPEED_SUPER_PLUS:
2211*4882a593Smuzhiyun return SS_BLOCK;
2212*4882a593Smuzhiyun case USB_SPEED_UNKNOWN:
2213*4882a593Smuzhiyun case USB_SPEED_WIRELESS:
2214*4882a593Smuzhiyun default:
2215*4882a593Smuzhiyun /* Should never happen */
2216*4882a593Smuzhiyun return 1;
2217*4882a593Smuzhiyun }
2218*4882a593Smuzhiyun }
2219*4882a593Smuzhiyun
2220*4882a593Smuzhiyun static unsigned int
xhci_get_largest_overhead(struct xhci_interval_bw * interval_bw)2221*4882a593Smuzhiyun xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
2222*4882a593Smuzhiyun {
2223*4882a593Smuzhiyun if (interval_bw->overhead[LS_OVERHEAD_TYPE])
2224*4882a593Smuzhiyun return LS_OVERHEAD;
2225*4882a593Smuzhiyun if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2226*4882a593Smuzhiyun return FS_OVERHEAD;
2227*4882a593Smuzhiyun return HS_OVERHEAD;
2228*4882a593Smuzhiyun }
2229*4882a593Smuzhiyun
2230*4882a593Smuzhiyun /* If we are changing a LS/FS device under a HS hub,
2231*4882a593Smuzhiyun * make sure (if we are activating a new TT) that the HS bus has enough
2232*4882a593Smuzhiyun * bandwidth for this new TT.
2233*4882a593Smuzhiyun */
xhci_check_tt_bw_table(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,int old_active_eps)2234*4882a593Smuzhiyun static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2235*4882a593Smuzhiyun struct xhci_virt_device *virt_dev,
2236*4882a593Smuzhiyun int old_active_eps)
2237*4882a593Smuzhiyun {
2238*4882a593Smuzhiyun struct xhci_interval_bw_table *bw_table;
2239*4882a593Smuzhiyun struct xhci_tt_bw_info *tt_info;
2240*4882a593Smuzhiyun
2241*4882a593Smuzhiyun /* Find the bandwidth table for the root port this TT is attached to. */
2242*4882a593Smuzhiyun bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2243*4882a593Smuzhiyun tt_info = virt_dev->tt_info;
2244*4882a593Smuzhiyun /* If this TT already had active endpoints, the bandwidth for this TT
2245*4882a593Smuzhiyun * has already been added. Removing all periodic endpoints (and thus
2246*4882a593Smuzhiyun * making the TT enactive) will only decrease the bandwidth used.
2247*4882a593Smuzhiyun */
2248*4882a593Smuzhiyun if (old_active_eps)
2249*4882a593Smuzhiyun return 0;
2250*4882a593Smuzhiyun if (old_active_eps == 0 && tt_info->active_eps != 0) {
2251*4882a593Smuzhiyun if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2252*4882a593Smuzhiyun return -ENOMEM;
2253*4882a593Smuzhiyun return 0;
2254*4882a593Smuzhiyun }
2255*4882a593Smuzhiyun /* Not sure why we would have no new active endpoints...
2256*4882a593Smuzhiyun *
2257*4882a593Smuzhiyun * Maybe because of an Evaluate Context change for a hub update or a
2258*4882a593Smuzhiyun * control endpoint 0 max packet size change?
2259*4882a593Smuzhiyun * FIXME: skip the bandwidth calculation in that case.
2260*4882a593Smuzhiyun */
2261*4882a593Smuzhiyun return 0;
2262*4882a593Smuzhiyun }
2263*4882a593Smuzhiyun
xhci_check_ss_bw(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev)2264*4882a593Smuzhiyun static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2265*4882a593Smuzhiyun struct xhci_virt_device *virt_dev)
2266*4882a593Smuzhiyun {
2267*4882a593Smuzhiyun unsigned int bw_reserved;
2268*4882a593Smuzhiyun
2269*4882a593Smuzhiyun bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2270*4882a593Smuzhiyun if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2271*4882a593Smuzhiyun return -ENOMEM;
2272*4882a593Smuzhiyun
2273*4882a593Smuzhiyun bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2274*4882a593Smuzhiyun if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2275*4882a593Smuzhiyun return -ENOMEM;
2276*4882a593Smuzhiyun
2277*4882a593Smuzhiyun return 0;
2278*4882a593Smuzhiyun }
2279*4882a593Smuzhiyun
2280*4882a593Smuzhiyun /*
2281*4882a593Smuzhiyun * This algorithm is a very conservative estimate of the worst-case scheduling
2282*4882a593Smuzhiyun * scenario for any one interval. The hardware dynamically schedules the
2283*4882a593Smuzhiyun * packets, so we can't tell which microframe could be the limiting factor in
2284*4882a593Smuzhiyun * the bandwidth scheduling. This only takes into account periodic endpoints.
2285*4882a593Smuzhiyun *
2286*4882a593Smuzhiyun * Obviously, we can't solve an NP complete problem to find the minimum worst
2287*4882a593Smuzhiyun * case scenario. Instead, we come up with an estimate that is no less than
2288*4882a593Smuzhiyun * the worst case bandwidth used for any one microframe, but may be an
2289*4882a593Smuzhiyun * over-estimate.
2290*4882a593Smuzhiyun *
2291*4882a593Smuzhiyun * We walk the requirements for each endpoint by interval, starting with the
2292*4882a593Smuzhiyun * smallest interval, and place packets in the schedule where there is only one
2293*4882a593Smuzhiyun * possible way to schedule packets for that interval. In order to simplify
2294*4882a593Smuzhiyun * this algorithm, we record the largest max packet size for each interval, and
2295*4882a593Smuzhiyun * assume all packets will be that size.
2296*4882a593Smuzhiyun *
2297*4882a593Smuzhiyun * For interval 0, we obviously must schedule all packets for each interval.
2298*4882a593Smuzhiyun * The bandwidth for interval 0 is just the amount of data to be transmitted
2299*4882a593Smuzhiyun * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2300*4882a593Smuzhiyun * the number of packets).
2301*4882a593Smuzhiyun *
2302*4882a593Smuzhiyun * For interval 1, we have two possible microframes to schedule those packets
2303*4882a593Smuzhiyun * in. For this algorithm, if we can schedule the same number of packets for
2304*4882a593Smuzhiyun * each possible scheduling opportunity (each microframe), we will do so. The
2305*4882a593Smuzhiyun * remaining number of packets will be saved to be transmitted in the gaps in
2306*4882a593Smuzhiyun * the next interval's scheduling sequence.
2307*4882a593Smuzhiyun *
2308*4882a593Smuzhiyun * As we move those remaining packets to be scheduled with interval 2 packets,
2309*4882a593Smuzhiyun * we have to double the number of remaining packets to transmit. This is
2310*4882a593Smuzhiyun * because the intervals are actually powers of 2, and we would be transmitting
2311*4882a593Smuzhiyun * the previous interval's packets twice in this interval. We also have to be
2312*4882a593Smuzhiyun * sure that when we look at the largest max packet size for this interval, we
2313*4882a593Smuzhiyun * also look at the largest max packet size for the remaining packets and take
2314*4882a593Smuzhiyun * the greater of the two.
2315*4882a593Smuzhiyun *
2316*4882a593Smuzhiyun * The algorithm continues to evenly distribute packets in each scheduling
2317*4882a593Smuzhiyun * opportunity, and push the remaining packets out, until we get to the last
2318*4882a593Smuzhiyun * interval. Then those packets and their associated overhead are just added
2319*4882a593Smuzhiyun * to the bandwidth used.
2320*4882a593Smuzhiyun */
xhci_check_bw_table(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,int old_active_eps)2321*4882a593Smuzhiyun static int xhci_check_bw_table(struct xhci_hcd *xhci,
2322*4882a593Smuzhiyun struct xhci_virt_device *virt_dev,
2323*4882a593Smuzhiyun int old_active_eps)
2324*4882a593Smuzhiyun {
2325*4882a593Smuzhiyun unsigned int bw_reserved;
2326*4882a593Smuzhiyun unsigned int max_bandwidth;
2327*4882a593Smuzhiyun unsigned int bw_used;
2328*4882a593Smuzhiyun unsigned int block_size;
2329*4882a593Smuzhiyun struct xhci_interval_bw_table *bw_table;
2330*4882a593Smuzhiyun unsigned int packet_size = 0;
2331*4882a593Smuzhiyun unsigned int overhead = 0;
2332*4882a593Smuzhiyun unsigned int packets_transmitted = 0;
2333*4882a593Smuzhiyun unsigned int packets_remaining = 0;
2334*4882a593Smuzhiyun unsigned int i;
2335*4882a593Smuzhiyun
2336*4882a593Smuzhiyun if (virt_dev->udev->speed >= USB_SPEED_SUPER)
2337*4882a593Smuzhiyun return xhci_check_ss_bw(xhci, virt_dev);
2338*4882a593Smuzhiyun
2339*4882a593Smuzhiyun if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2340*4882a593Smuzhiyun max_bandwidth = HS_BW_LIMIT;
2341*4882a593Smuzhiyun /* Convert percent of bus BW reserved to blocks reserved */
2342*4882a593Smuzhiyun bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2343*4882a593Smuzhiyun } else {
2344*4882a593Smuzhiyun max_bandwidth = FS_BW_LIMIT;
2345*4882a593Smuzhiyun bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2346*4882a593Smuzhiyun }
2347*4882a593Smuzhiyun
2348*4882a593Smuzhiyun bw_table = virt_dev->bw_table;
2349*4882a593Smuzhiyun /* We need to translate the max packet size and max ESIT payloads into
2350*4882a593Smuzhiyun * the units the hardware uses.
2351*4882a593Smuzhiyun */
2352*4882a593Smuzhiyun block_size = xhci_get_block_size(virt_dev->udev);
2353*4882a593Smuzhiyun
2354*4882a593Smuzhiyun /* If we are manipulating a LS/FS device under a HS hub, double check
2355*4882a593Smuzhiyun * that the HS bus has enough bandwidth if we are activing a new TT.
2356*4882a593Smuzhiyun */
2357*4882a593Smuzhiyun if (virt_dev->tt_info) {
2358*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2359*4882a593Smuzhiyun "Recalculating BW for rootport %u",
2360*4882a593Smuzhiyun virt_dev->real_port);
2361*4882a593Smuzhiyun if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2362*4882a593Smuzhiyun xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2363*4882a593Smuzhiyun "newly activated TT.\n");
2364*4882a593Smuzhiyun return -ENOMEM;
2365*4882a593Smuzhiyun }
2366*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2367*4882a593Smuzhiyun "Recalculating BW for TT slot %u port %u",
2368*4882a593Smuzhiyun virt_dev->tt_info->slot_id,
2369*4882a593Smuzhiyun virt_dev->tt_info->ttport);
2370*4882a593Smuzhiyun } else {
2371*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2372*4882a593Smuzhiyun "Recalculating BW for rootport %u",
2373*4882a593Smuzhiyun virt_dev->real_port);
2374*4882a593Smuzhiyun }
2375*4882a593Smuzhiyun
2376*4882a593Smuzhiyun /* Add in how much bandwidth will be used for interval zero, or the
2377*4882a593Smuzhiyun * rounded max ESIT payload + number of packets * largest overhead.
2378*4882a593Smuzhiyun */
2379*4882a593Smuzhiyun bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2380*4882a593Smuzhiyun bw_table->interval_bw[0].num_packets *
2381*4882a593Smuzhiyun xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2382*4882a593Smuzhiyun
2383*4882a593Smuzhiyun for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2384*4882a593Smuzhiyun unsigned int bw_added;
2385*4882a593Smuzhiyun unsigned int largest_mps;
2386*4882a593Smuzhiyun unsigned int interval_overhead;
2387*4882a593Smuzhiyun
2388*4882a593Smuzhiyun /*
2389*4882a593Smuzhiyun * How many packets could we transmit in this interval?
2390*4882a593Smuzhiyun * If packets didn't fit in the previous interval, we will need
2391*4882a593Smuzhiyun * to transmit that many packets twice within this interval.
2392*4882a593Smuzhiyun */
2393*4882a593Smuzhiyun packets_remaining = 2 * packets_remaining +
2394*4882a593Smuzhiyun bw_table->interval_bw[i].num_packets;
2395*4882a593Smuzhiyun
2396*4882a593Smuzhiyun /* Find the largest max packet size of this or the previous
2397*4882a593Smuzhiyun * interval.
2398*4882a593Smuzhiyun */
2399*4882a593Smuzhiyun if (list_empty(&bw_table->interval_bw[i].endpoints))
2400*4882a593Smuzhiyun largest_mps = 0;
2401*4882a593Smuzhiyun else {
2402*4882a593Smuzhiyun struct xhci_virt_ep *virt_ep;
2403*4882a593Smuzhiyun struct list_head *ep_entry;
2404*4882a593Smuzhiyun
2405*4882a593Smuzhiyun ep_entry = bw_table->interval_bw[i].endpoints.next;
2406*4882a593Smuzhiyun virt_ep = list_entry(ep_entry,
2407*4882a593Smuzhiyun struct xhci_virt_ep, bw_endpoint_list);
2408*4882a593Smuzhiyun /* Convert to blocks, rounding up */
2409*4882a593Smuzhiyun largest_mps = DIV_ROUND_UP(
2410*4882a593Smuzhiyun virt_ep->bw_info.max_packet_size,
2411*4882a593Smuzhiyun block_size);
2412*4882a593Smuzhiyun }
2413*4882a593Smuzhiyun if (largest_mps > packet_size)
2414*4882a593Smuzhiyun packet_size = largest_mps;
2415*4882a593Smuzhiyun
2416*4882a593Smuzhiyun /* Use the larger overhead of this or the previous interval. */
2417*4882a593Smuzhiyun interval_overhead = xhci_get_largest_overhead(
2418*4882a593Smuzhiyun &bw_table->interval_bw[i]);
2419*4882a593Smuzhiyun if (interval_overhead > overhead)
2420*4882a593Smuzhiyun overhead = interval_overhead;
2421*4882a593Smuzhiyun
2422*4882a593Smuzhiyun /* How many packets can we evenly distribute across
2423*4882a593Smuzhiyun * (1 << (i + 1)) possible scheduling opportunities?
2424*4882a593Smuzhiyun */
2425*4882a593Smuzhiyun packets_transmitted = packets_remaining >> (i + 1);
2426*4882a593Smuzhiyun
2427*4882a593Smuzhiyun /* Add in the bandwidth used for those scheduled packets */
2428*4882a593Smuzhiyun bw_added = packets_transmitted * (overhead + packet_size);
2429*4882a593Smuzhiyun
2430*4882a593Smuzhiyun /* How many packets do we have remaining to transmit? */
2431*4882a593Smuzhiyun packets_remaining = packets_remaining % (1 << (i + 1));
2432*4882a593Smuzhiyun
2433*4882a593Smuzhiyun /* What largest max packet size should those packets have? */
2434*4882a593Smuzhiyun /* If we've transmitted all packets, don't carry over the
2435*4882a593Smuzhiyun * largest packet size.
2436*4882a593Smuzhiyun */
2437*4882a593Smuzhiyun if (packets_remaining == 0) {
2438*4882a593Smuzhiyun packet_size = 0;
2439*4882a593Smuzhiyun overhead = 0;
2440*4882a593Smuzhiyun } else if (packets_transmitted > 0) {
2441*4882a593Smuzhiyun /* Otherwise if we do have remaining packets, and we've
2442*4882a593Smuzhiyun * scheduled some packets in this interval, take the
2443*4882a593Smuzhiyun * largest max packet size from endpoints with this
2444*4882a593Smuzhiyun * interval.
2445*4882a593Smuzhiyun */
2446*4882a593Smuzhiyun packet_size = largest_mps;
2447*4882a593Smuzhiyun overhead = interval_overhead;
2448*4882a593Smuzhiyun }
2449*4882a593Smuzhiyun /* Otherwise carry over packet_size and overhead from the last
2450*4882a593Smuzhiyun * time we had a remainder.
2451*4882a593Smuzhiyun */
2452*4882a593Smuzhiyun bw_used += bw_added;
2453*4882a593Smuzhiyun if (bw_used > max_bandwidth) {
2454*4882a593Smuzhiyun xhci_warn(xhci, "Not enough bandwidth. "
2455*4882a593Smuzhiyun "Proposed: %u, Max: %u\n",
2456*4882a593Smuzhiyun bw_used, max_bandwidth);
2457*4882a593Smuzhiyun return -ENOMEM;
2458*4882a593Smuzhiyun }
2459*4882a593Smuzhiyun }
2460*4882a593Smuzhiyun /*
2461*4882a593Smuzhiyun * Ok, we know we have some packets left over after even-handedly
2462*4882a593Smuzhiyun * scheduling interval 15. We don't know which microframes they will
2463*4882a593Smuzhiyun * fit into, so we over-schedule and say they will be scheduled every
2464*4882a593Smuzhiyun * microframe.
2465*4882a593Smuzhiyun */
2466*4882a593Smuzhiyun if (packets_remaining > 0)
2467*4882a593Smuzhiyun bw_used += overhead + packet_size;
2468*4882a593Smuzhiyun
2469*4882a593Smuzhiyun if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2470*4882a593Smuzhiyun unsigned int port_index = virt_dev->real_port - 1;
2471*4882a593Smuzhiyun
2472*4882a593Smuzhiyun /* OK, we're manipulating a HS device attached to a
2473*4882a593Smuzhiyun * root port bandwidth domain. Include the number of active TTs
2474*4882a593Smuzhiyun * in the bandwidth used.
2475*4882a593Smuzhiyun */
2476*4882a593Smuzhiyun bw_used += TT_HS_OVERHEAD *
2477*4882a593Smuzhiyun xhci->rh_bw[port_index].num_active_tts;
2478*4882a593Smuzhiyun }
2479*4882a593Smuzhiyun
2480*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2481*4882a593Smuzhiyun "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2482*4882a593Smuzhiyun "Available: %u " "percent",
2483*4882a593Smuzhiyun bw_used, max_bandwidth, bw_reserved,
2484*4882a593Smuzhiyun (max_bandwidth - bw_used - bw_reserved) * 100 /
2485*4882a593Smuzhiyun max_bandwidth);
2486*4882a593Smuzhiyun
2487*4882a593Smuzhiyun bw_used += bw_reserved;
2488*4882a593Smuzhiyun if (bw_used > max_bandwidth) {
2489*4882a593Smuzhiyun xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2490*4882a593Smuzhiyun bw_used, max_bandwidth);
2491*4882a593Smuzhiyun return -ENOMEM;
2492*4882a593Smuzhiyun }
2493*4882a593Smuzhiyun
2494*4882a593Smuzhiyun bw_table->bw_used = bw_used;
2495*4882a593Smuzhiyun return 0;
2496*4882a593Smuzhiyun }
2497*4882a593Smuzhiyun
xhci_is_async_ep(unsigned int ep_type)2498*4882a593Smuzhiyun static bool xhci_is_async_ep(unsigned int ep_type)
2499*4882a593Smuzhiyun {
2500*4882a593Smuzhiyun return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2501*4882a593Smuzhiyun ep_type != ISOC_IN_EP &&
2502*4882a593Smuzhiyun ep_type != INT_IN_EP);
2503*4882a593Smuzhiyun }
2504*4882a593Smuzhiyun
xhci_is_sync_in_ep(unsigned int ep_type)2505*4882a593Smuzhiyun static bool xhci_is_sync_in_ep(unsigned int ep_type)
2506*4882a593Smuzhiyun {
2507*4882a593Smuzhiyun return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
2508*4882a593Smuzhiyun }
2509*4882a593Smuzhiyun
xhci_get_ss_bw_consumed(struct xhci_bw_info * ep_bw)2510*4882a593Smuzhiyun static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2511*4882a593Smuzhiyun {
2512*4882a593Smuzhiyun unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2513*4882a593Smuzhiyun
2514*4882a593Smuzhiyun if (ep_bw->ep_interval == 0)
2515*4882a593Smuzhiyun return SS_OVERHEAD_BURST +
2516*4882a593Smuzhiyun (ep_bw->mult * ep_bw->num_packets *
2517*4882a593Smuzhiyun (SS_OVERHEAD + mps));
2518*4882a593Smuzhiyun return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2519*4882a593Smuzhiyun (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2520*4882a593Smuzhiyun 1 << ep_bw->ep_interval);
2521*4882a593Smuzhiyun
2522*4882a593Smuzhiyun }
2523*4882a593Smuzhiyun
xhci_drop_ep_from_interval_table(struct xhci_hcd * xhci,struct xhci_bw_info * ep_bw,struct xhci_interval_bw_table * bw_table,struct usb_device * udev,struct xhci_virt_ep * virt_ep,struct xhci_tt_bw_info * tt_info)2524*4882a593Smuzhiyun static void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
2525*4882a593Smuzhiyun struct xhci_bw_info *ep_bw,
2526*4882a593Smuzhiyun struct xhci_interval_bw_table *bw_table,
2527*4882a593Smuzhiyun struct usb_device *udev,
2528*4882a593Smuzhiyun struct xhci_virt_ep *virt_ep,
2529*4882a593Smuzhiyun struct xhci_tt_bw_info *tt_info)
2530*4882a593Smuzhiyun {
2531*4882a593Smuzhiyun struct xhci_interval_bw *interval_bw;
2532*4882a593Smuzhiyun int normalized_interval;
2533*4882a593Smuzhiyun
2534*4882a593Smuzhiyun if (xhci_is_async_ep(ep_bw->type))
2535*4882a593Smuzhiyun return;
2536*4882a593Smuzhiyun
2537*4882a593Smuzhiyun if (udev->speed >= USB_SPEED_SUPER) {
2538*4882a593Smuzhiyun if (xhci_is_sync_in_ep(ep_bw->type))
2539*4882a593Smuzhiyun xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2540*4882a593Smuzhiyun xhci_get_ss_bw_consumed(ep_bw);
2541*4882a593Smuzhiyun else
2542*4882a593Smuzhiyun xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2543*4882a593Smuzhiyun xhci_get_ss_bw_consumed(ep_bw);
2544*4882a593Smuzhiyun return;
2545*4882a593Smuzhiyun }
2546*4882a593Smuzhiyun
2547*4882a593Smuzhiyun /* SuperSpeed endpoints never get added to intervals in the table, so
2548*4882a593Smuzhiyun * this check is only valid for HS/FS/LS devices.
2549*4882a593Smuzhiyun */
2550*4882a593Smuzhiyun if (list_empty(&virt_ep->bw_endpoint_list))
2551*4882a593Smuzhiyun return;
2552*4882a593Smuzhiyun /* For LS/FS devices, we need to translate the interval expressed in
2553*4882a593Smuzhiyun * microframes to frames.
2554*4882a593Smuzhiyun */
2555*4882a593Smuzhiyun if (udev->speed == USB_SPEED_HIGH)
2556*4882a593Smuzhiyun normalized_interval = ep_bw->ep_interval;
2557*4882a593Smuzhiyun else
2558*4882a593Smuzhiyun normalized_interval = ep_bw->ep_interval - 3;
2559*4882a593Smuzhiyun
2560*4882a593Smuzhiyun if (normalized_interval == 0)
2561*4882a593Smuzhiyun bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2562*4882a593Smuzhiyun interval_bw = &bw_table->interval_bw[normalized_interval];
2563*4882a593Smuzhiyun interval_bw->num_packets -= ep_bw->num_packets;
2564*4882a593Smuzhiyun switch (udev->speed) {
2565*4882a593Smuzhiyun case USB_SPEED_LOW:
2566*4882a593Smuzhiyun interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2567*4882a593Smuzhiyun break;
2568*4882a593Smuzhiyun case USB_SPEED_FULL:
2569*4882a593Smuzhiyun interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2570*4882a593Smuzhiyun break;
2571*4882a593Smuzhiyun case USB_SPEED_HIGH:
2572*4882a593Smuzhiyun interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2573*4882a593Smuzhiyun break;
2574*4882a593Smuzhiyun case USB_SPEED_SUPER:
2575*4882a593Smuzhiyun case USB_SPEED_SUPER_PLUS:
2576*4882a593Smuzhiyun case USB_SPEED_UNKNOWN:
2577*4882a593Smuzhiyun case USB_SPEED_WIRELESS:
2578*4882a593Smuzhiyun /* Should never happen because only LS/FS/HS endpoints will get
2579*4882a593Smuzhiyun * added to the endpoint list.
2580*4882a593Smuzhiyun */
2581*4882a593Smuzhiyun return;
2582*4882a593Smuzhiyun }
2583*4882a593Smuzhiyun if (tt_info)
2584*4882a593Smuzhiyun tt_info->active_eps -= 1;
2585*4882a593Smuzhiyun list_del_init(&virt_ep->bw_endpoint_list);
2586*4882a593Smuzhiyun }
2587*4882a593Smuzhiyun
xhci_add_ep_to_interval_table(struct xhci_hcd * xhci,struct xhci_bw_info * ep_bw,struct xhci_interval_bw_table * bw_table,struct usb_device * udev,struct xhci_virt_ep * virt_ep,struct xhci_tt_bw_info * tt_info)2588*4882a593Smuzhiyun static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2589*4882a593Smuzhiyun struct xhci_bw_info *ep_bw,
2590*4882a593Smuzhiyun struct xhci_interval_bw_table *bw_table,
2591*4882a593Smuzhiyun struct usb_device *udev,
2592*4882a593Smuzhiyun struct xhci_virt_ep *virt_ep,
2593*4882a593Smuzhiyun struct xhci_tt_bw_info *tt_info)
2594*4882a593Smuzhiyun {
2595*4882a593Smuzhiyun struct xhci_interval_bw *interval_bw;
2596*4882a593Smuzhiyun struct xhci_virt_ep *smaller_ep;
2597*4882a593Smuzhiyun int normalized_interval;
2598*4882a593Smuzhiyun
2599*4882a593Smuzhiyun if (xhci_is_async_ep(ep_bw->type))
2600*4882a593Smuzhiyun return;
2601*4882a593Smuzhiyun
2602*4882a593Smuzhiyun if (udev->speed == USB_SPEED_SUPER) {
2603*4882a593Smuzhiyun if (xhci_is_sync_in_ep(ep_bw->type))
2604*4882a593Smuzhiyun xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2605*4882a593Smuzhiyun xhci_get_ss_bw_consumed(ep_bw);
2606*4882a593Smuzhiyun else
2607*4882a593Smuzhiyun xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2608*4882a593Smuzhiyun xhci_get_ss_bw_consumed(ep_bw);
2609*4882a593Smuzhiyun return;
2610*4882a593Smuzhiyun }
2611*4882a593Smuzhiyun
2612*4882a593Smuzhiyun /* For LS/FS devices, we need to translate the interval expressed in
2613*4882a593Smuzhiyun * microframes to frames.
2614*4882a593Smuzhiyun */
2615*4882a593Smuzhiyun if (udev->speed == USB_SPEED_HIGH)
2616*4882a593Smuzhiyun normalized_interval = ep_bw->ep_interval;
2617*4882a593Smuzhiyun else
2618*4882a593Smuzhiyun normalized_interval = ep_bw->ep_interval - 3;
2619*4882a593Smuzhiyun
2620*4882a593Smuzhiyun if (normalized_interval == 0)
2621*4882a593Smuzhiyun bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2622*4882a593Smuzhiyun interval_bw = &bw_table->interval_bw[normalized_interval];
2623*4882a593Smuzhiyun interval_bw->num_packets += ep_bw->num_packets;
2624*4882a593Smuzhiyun switch (udev->speed) {
2625*4882a593Smuzhiyun case USB_SPEED_LOW:
2626*4882a593Smuzhiyun interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2627*4882a593Smuzhiyun break;
2628*4882a593Smuzhiyun case USB_SPEED_FULL:
2629*4882a593Smuzhiyun interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2630*4882a593Smuzhiyun break;
2631*4882a593Smuzhiyun case USB_SPEED_HIGH:
2632*4882a593Smuzhiyun interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2633*4882a593Smuzhiyun break;
2634*4882a593Smuzhiyun case USB_SPEED_SUPER:
2635*4882a593Smuzhiyun case USB_SPEED_SUPER_PLUS:
2636*4882a593Smuzhiyun case USB_SPEED_UNKNOWN:
2637*4882a593Smuzhiyun case USB_SPEED_WIRELESS:
2638*4882a593Smuzhiyun /* Should never happen because only LS/FS/HS endpoints will get
2639*4882a593Smuzhiyun * added to the endpoint list.
2640*4882a593Smuzhiyun */
2641*4882a593Smuzhiyun return;
2642*4882a593Smuzhiyun }
2643*4882a593Smuzhiyun
2644*4882a593Smuzhiyun if (tt_info)
2645*4882a593Smuzhiyun tt_info->active_eps += 1;
2646*4882a593Smuzhiyun /* Insert the endpoint into the list, largest max packet size first. */
2647*4882a593Smuzhiyun list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2648*4882a593Smuzhiyun bw_endpoint_list) {
2649*4882a593Smuzhiyun if (ep_bw->max_packet_size >=
2650*4882a593Smuzhiyun smaller_ep->bw_info.max_packet_size) {
2651*4882a593Smuzhiyun /* Add the new ep before the smaller endpoint */
2652*4882a593Smuzhiyun list_add_tail(&virt_ep->bw_endpoint_list,
2653*4882a593Smuzhiyun &smaller_ep->bw_endpoint_list);
2654*4882a593Smuzhiyun return;
2655*4882a593Smuzhiyun }
2656*4882a593Smuzhiyun }
2657*4882a593Smuzhiyun /* Add the new endpoint at the end of the list. */
2658*4882a593Smuzhiyun list_add_tail(&virt_ep->bw_endpoint_list,
2659*4882a593Smuzhiyun &interval_bw->endpoints);
2660*4882a593Smuzhiyun }
2661*4882a593Smuzhiyun
xhci_update_tt_active_eps(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,int old_active_eps)2662*4882a593Smuzhiyun void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2663*4882a593Smuzhiyun struct xhci_virt_device *virt_dev,
2664*4882a593Smuzhiyun int old_active_eps)
2665*4882a593Smuzhiyun {
2666*4882a593Smuzhiyun struct xhci_root_port_bw_info *rh_bw_info;
2667*4882a593Smuzhiyun if (!virt_dev->tt_info)
2668*4882a593Smuzhiyun return;
2669*4882a593Smuzhiyun
2670*4882a593Smuzhiyun rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2671*4882a593Smuzhiyun if (old_active_eps == 0 &&
2672*4882a593Smuzhiyun virt_dev->tt_info->active_eps != 0) {
2673*4882a593Smuzhiyun rh_bw_info->num_active_tts += 1;
2674*4882a593Smuzhiyun rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
2675*4882a593Smuzhiyun } else if (old_active_eps != 0 &&
2676*4882a593Smuzhiyun virt_dev->tt_info->active_eps == 0) {
2677*4882a593Smuzhiyun rh_bw_info->num_active_tts -= 1;
2678*4882a593Smuzhiyun rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
2679*4882a593Smuzhiyun }
2680*4882a593Smuzhiyun }
2681*4882a593Smuzhiyun
xhci_reserve_bandwidth(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,struct xhci_container_ctx * in_ctx)2682*4882a593Smuzhiyun static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2683*4882a593Smuzhiyun struct xhci_virt_device *virt_dev,
2684*4882a593Smuzhiyun struct xhci_container_ctx *in_ctx)
2685*4882a593Smuzhiyun {
2686*4882a593Smuzhiyun struct xhci_bw_info ep_bw_info[31];
2687*4882a593Smuzhiyun int i;
2688*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
2689*4882a593Smuzhiyun int old_active_eps = 0;
2690*4882a593Smuzhiyun
2691*4882a593Smuzhiyun if (virt_dev->tt_info)
2692*4882a593Smuzhiyun old_active_eps = virt_dev->tt_info->active_eps;
2693*4882a593Smuzhiyun
2694*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(in_ctx);
2695*4882a593Smuzhiyun if (!ctrl_ctx) {
2696*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2697*4882a593Smuzhiyun __func__);
2698*4882a593Smuzhiyun return -ENOMEM;
2699*4882a593Smuzhiyun }
2700*4882a593Smuzhiyun
2701*4882a593Smuzhiyun for (i = 0; i < 31; i++) {
2702*4882a593Smuzhiyun if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2703*4882a593Smuzhiyun continue;
2704*4882a593Smuzhiyun
2705*4882a593Smuzhiyun /* Make a copy of the BW info in case we need to revert this */
2706*4882a593Smuzhiyun memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2707*4882a593Smuzhiyun sizeof(ep_bw_info[i]));
2708*4882a593Smuzhiyun /* Drop the endpoint from the interval table if the endpoint is
2709*4882a593Smuzhiyun * being dropped or changed.
2710*4882a593Smuzhiyun */
2711*4882a593Smuzhiyun if (EP_IS_DROPPED(ctrl_ctx, i))
2712*4882a593Smuzhiyun xhci_drop_ep_from_interval_table(xhci,
2713*4882a593Smuzhiyun &virt_dev->eps[i].bw_info,
2714*4882a593Smuzhiyun virt_dev->bw_table,
2715*4882a593Smuzhiyun virt_dev->udev,
2716*4882a593Smuzhiyun &virt_dev->eps[i],
2717*4882a593Smuzhiyun virt_dev->tt_info);
2718*4882a593Smuzhiyun }
2719*4882a593Smuzhiyun /* Overwrite the information stored in the endpoints' bw_info */
2720*4882a593Smuzhiyun xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2721*4882a593Smuzhiyun for (i = 0; i < 31; i++) {
2722*4882a593Smuzhiyun /* Add any changed or added endpoints to the interval table */
2723*4882a593Smuzhiyun if (EP_IS_ADDED(ctrl_ctx, i))
2724*4882a593Smuzhiyun xhci_add_ep_to_interval_table(xhci,
2725*4882a593Smuzhiyun &virt_dev->eps[i].bw_info,
2726*4882a593Smuzhiyun virt_dev->bw_table,
2727*4882a593Smuzhiyun virt_dev->udev,
2728*4882a593Smuzhiyun &virt_dev->eps[i],
2729*4882a593Smuzhiyun virt_dev->tt_info);
2730*4882a593Smuzhiyun }
2731*4882a593Smuzhiyun
2732*4882a593Smuzhiyun if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2733*4882a593Smuzhiyun /* Ok, this fits in the bandwidth we have.
2734*4882a593Smuzhiyun * Update the number of active TTs.
2735*4882a593Smuzhiyun */
2736*4882a593Smuzhiyun xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2737*4882a593Smuzhiyun return 0;
2738*4882a593Smuzhiyun }
2739*4882a593Smuzhiyun
2740*4882a593Smuzhiyun /* We don't have enough bandwidth for this, revert the stored info. */
2741*4882a593Smuzhiyun for (i = 0; i < 31; i++) {
2742*4882a593Smuzhiyun if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2743*4882a593Smuzhiyun continue;
2744*4882a593Smuzhiyun
2745*4882a593Smuzhiyun /* Drop the new copies of any added or changed endpoints from
2746*4882a593Smuzhiyun * the interval table.
2747*4882a593Smuzhiyun */
2748*4882a593Smuzhiyun if (EP_IS_ADDED(ctrl_ctx, i)) {
2749*4882a593Smuzhiyun xhci_drop_ep_from_interval_table(xhci,
2750*4882a593Smuzhiyun &virt_dev->eps[i].bw_info,
2751*4882a593Smuzhiyun virt_dev->bw_table,
2752*4882a593Smuzhiyun virt_dev->udev,
2753*4882a593Smuzhiyun &virt_dev->eps[i],
2754*4882a593Smuzhiyun virt_dev->tt_info);
2755*4882a593Smuzhiyun }
2756*4882a593Smuzhiyun /* Revert the endpoint back to its old information */
2757*4882a593Smuzhiyun memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2758*4882a593Smuzhiyun sizeof(ep_bw_info[i]));
2759*4882a593Smuzhiyun /* Add any changed or dropped endpoints back into the table */
2760*4882a593Smuzhiyun if (EP_IS_DROPPED(ctrl_ctx, i))
2761*4882a593Smuzhiyun xhci_add_ep_to_interval_table(xhci,
2762*4882a593Smuzhiyun &virt_dev->eps[i].bw_info,
2763*4882a593Smuzhiyun virt_dev->bw_table,
2764*4882a593Smuzhiyun virt_dev->udev,
2765*4882a593Smuzhiyun &virt_dev->eps[i],
2766*4882a593Smuzhiyun virt_dev->tt_info);
2767*4882a593Smuzhiyun }
2768*4882a593Smuzhiyun return -ENOMEM;
2769*4882a593Smuzhiyun }
2770*4882a593Smuzhiyun
2771*4882a593Smuzhiyun
2772*4882a593Smuzhiyun /* Issue a configure endpoint command or evaluate context command
2773*4882a593Smuzhiyun * and wait for it to finish.
2774*4882a593Smuzhiyun */
xhci_configure_endpoint(struct xhci_hcd * xhci,struct usb_device * udev,struct xhci_command * command,bool ctx_change,bool must_succeed)2775*4882a593Smuzhiyun static int xhci_configure_endpoint(struct xhci_hcd *xhci,
2776*4882a593Smuzhiyun struct usb_device *udev,
2777*4882a593Smuzhiyun struct xhci_command *command,
2778*4882a593Smuzhiyun bool ctx_change, bool must_succeed)
2779*4882a593Smuzhiyun {
2780*4882a593Smuzhiyun int ret;
2781*4882a593Smuzhiyun unsigned long flags;
2782*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
2783*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
2784*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
2785*4882a593Smuzhiyun
2786*4882a593Smuzhiyun if (!command)
2787*4882a593Smuzhiyun return -EINVAL;
2788*4882a593Smuzhiyun
2789*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
2790*4882a593Smuzhiyun
2791*4882a593Smuzhiyun if (xhci->xhc_state & XHCI_STATE_DYING) {
2792*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
2793*4882a593Smuzhiyun return -ESHUTDOWN;
2794*4882a593Smuzhiyun }
2795*4882a593Smuzhiyun
2796*4882a593Smuzhiyun virt_dev = xhci->devs[udev->slot_id];
2797*4882a593Smuzhiyun
2798*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
2799*4882a593Smuzhiyun if (!ctrl_ctx) {
2800*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
2801*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2802*4882a593Smuzhiyun __func__);
2803*4882a593Smuzhiyun return -ENOMEM;
2804*4882a593Smuzhiyun }
2805*4882a593Smuzhiyun
2806*4882a593Smuzhiyun if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
2807*4882a593Smuzhiyun xhci_reserve_host_resources(xhci, ctrl_ctx)) {
2808*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
2809*4882a593Smuzhiyun xhci_warn(xhci, "Not enough host resources, "
2810*4882a593Smuzhiyun "active endpoint contexts = %u\n",
2811*4882a593Smuzhiyun xhci->num_active_eps);
2812*4882a593Smuzhiyun return -ENOMEM;
2813*4882a593Smuzhiyun }
2814*4882a593Smuzhiyun if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
2815*4882a593Smuzhiyun xhci_reserve_bandwidth(xhci, virt_dev, command->in_ctx)) {
2816*4882a593Smuzhiyun if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2817*4882a593Smuzhiyun xhci_free_host_resources(xhci, ctrl_ctx);
2818*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
2819*4882a593Smuzhiyun xhci_warn(xhci, "Not enough bandwidth\n");
2820*4882a593Smuzhiyun return -ENOMEM;
2821*4882a593Smuzhiyun }
2822*4882a593Smuzhiyun
2823*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
2824*4882a593Smuzhiyun
2825*4882a593Smuzhiyun trace_xhci_configure_endpoint_ctrl_ctx(ctrl_ctx);
2826*4882a593Smuzhiyun trace_xhci_configure_endpoint(slot_ctx);
2827*4882a593Smuzhiyun
2828*4882a593Smuzhiyun if (!ctx_change)
2829*4882a593Smuzhiyun ret = xhci_queue_configure_endpoint(xhci, command,
2830*4882a593Smuzhiyun command->in_ctx->dma,
2831*4882a593Smuzhiyun udev->slot_id, must_succeed);
2832*4882a593Smuzhiyun else
2833*4882a593Smuzhiyun ret = xhci_queue_evaluate_context(xhci, command,
2834*4882a593Smuzhiyun command->in_ctx->dma,
2835*4882a593Smuzhiyun udev->slot_id, must_succeed);
2836*4882a593Smuzhiyun if (ret < 0) {
2837*4882a593Smuzhiyun if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2838*4882a593Smuzhiyun xhci_free_host_resources(xhci, ctrl_ctx);
2839*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
2840*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
2841*4882a593Smuzhiyun "FIXME allocate a new ring segment");
2842*4882a593Smuzhiyun return -ENOMEM;
2843*4882a593Smuzhiyun }
2844*4882a593Smuzhiyun xhci_ring_cmd_db(xhci);
2845*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
2846*4882a593Smuzhiyun
2847*4882a593Smuzhiyun /* Wait for the configure endpoint command to complete */
2848*4882a593Smuzhiyun wait_for_completion(command->completion);
2849*4882a593Smuzhiyun
2850*4882a593Smuzhiyun if (!ctx_change)
2851*4882a593Smuzhiyun ret = xhci_configure_endpoint_result(xhci, udev,
2852*4882a593Smuzhiyun &command->status);
2853*4882a593Smuzhiyun else
2854*4882a593Smuzhiyun ret = xhci_evaluate_context_result(xhci, udev,
2855*4882a593Smuzhiyun &command->status);
2856*4882a593Smuzhiyun
2857*4882a593Smuzhiyun if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2858*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
2859*4882a593Smuzhiyun /* If the command failed, remove the reserved resources.
2860*4882a593Smuzhiyun * Otherwise, clean up the estimate to include dropped eps.
2861*4882a593Smuzhiyun */
2862*4882a593Smuzhiyun if (ret)
2863*4882a593Smuzhiyun xhci_free_host_resources(xhci, ctrl_ctx);
2864*4882a593Smuzhiyun else
2865*4882a593Smuzhiyun xhci_finish_resource_reservation(xhci, ctrl_ctx);
2866*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
2867*4882a593Smuzhiyun }
2868*4882a593Smuzhiyun if (ret)
2869*4882a593Smuzhiyun goto failed;
2870*4882a593Smuzhiyun
2871*4882a593Smuzhiyun ret = xhci_vendor_sync_dev_ctx(xhci, udev->slot_id);
2872*4882a593Smuzhiyun if (ret)
2873*4882a593Smuzhiyun xhci_warn(xhci, "sync device context failed, ret=%d", ret);
2874*4882a593Smuzhiyun
2875*4882a593Smuzhiyun failed:
2876*4882a593Smuzhiyun return ret;
2877*4882a593Smuzhiyun }
2878*4882a593Smuzhiyun
xhci_check_bw_drop_ep_streams(struct xhci_hcd * xhci,struct xhci_virt_device * vdev,int i)2879*4882a593Smuzhiyun static void xhci_check_bw_drop_ep_streams(struct xhci_hcd *xhci,
2880*4882a593Smuzhiyun struct xhci_virt_device *vdev, int i)
2881*4882a593Smuzhiyun {
2882*4882a593Smuzhiyun struct xhci_virt_ep *ep = &vdev->eps[i];
2883*4882a593Smuzhiyun
2884*4882a593Smuzhiyun if (ep->ep_state & EP_HAS_STREAMS) {
2885*4882a593Smuzhiyun xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on set_interface, freeing streams.\n",
2886*4882a593Smuzhiyun xhci_get_endpoint_address(i));
2887*4882a593Smuzhiyun xhci_free_stream_info(xhci, ep->stream_info);
2888*4882a593Smuzhiyun ep->stream_info = NULL;
2889*4882a593Smuzhiyun ep->ep_state &= ~EP_HAS_STREAMS;
2890*4882a593Smuzhiyun }
2891*4882a593Smuzhiyun }
2892*4882a593Smuzhiyun
2893*4882a593Smuzhiyun /* Called after one or more calls to xhci_add_endpoint() or
2894*4882a593Smuzhiyun * xhci_drop_endpoint(). If this call fails, the USB core is expected
2895*4882a593Smuzhiyun * to call xhci_reset_bandwidth().
2896*4882a593Smuzhiyun *
2897*4882a593Smuzhiyun * Since we are in the middle of changing either configuration or
2898*4882a593Smuzhiyun * installing a new alt setting, the USB core won't allow URBs to be
2899*4882a593Smuzhiyun * enqueued for any endpoint on the old config or interface. Nothing
2900*4882a593Smuzhiyun * else should be touching the xhci->devs[slot_id] structure, so we
2901*4882a593Smuzhiyun * don't need to take the xhci->lock for manipulating that.
2902*4882a593Smuzhiyun */
xhci_check_bandwidth(struct usb_hcd * hcd,struct usb_device * udev)2903*4882a593Smuzhiyun int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2904*4882a593Smuzhiyun {
2905*4882a593Smuzhiyun int i;
2906*4882a593Smuzhiyun int ret = 0;
2907*4882a593Smuzhiyun struct xhci_hcd *xhci;
2908*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
2909*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
2910*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
2911*4882a593Smuzhiyun struct xhci_command *command;
2912*4882a593Smuzhiyun
2913*4882a593Smuzhiyun ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
2914*4882a593Smuzhiyun if (ret <= 0)
2915*4882a593Smuzhiyun return ret;
2916*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
2917*4882a593Smuzhiyun if ((xhci->xhc_state & XHCI_STATE_DYING) ||
2918*4882a593Smuzhiyun (xhci->xhc_state & XHCI_STATE_REMOVING))
2919*4882a593Smuzhiyun return -ENODEV;
2920*4882a593Smuzhiyun
2921*4882a593Smuzhiyun xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
2922*4882a593Smuzhiyun virt_dev = xhci->devs[udev->slot_id];
2923*4882a593Smuzhiyun
2924*4882a593Smuzhiyun command = xhci_alloc_command(xhci, true, GFP_KERNEL);
2925*4882a593Smuzhiyun if (!command)
2926*4882a593Smuzhiyun return -ENOMEM;
2927*4882a593Smuzhiyun
2928*4882a593Smuzhiyun command->in_ctx = virt_dev->in_ctx;
2929*4882a593Smuzhiyun
2930*4882a593Smuzhiyun /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
2931*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
2932*4882a593Smuzhiyun if (!ctrl_ctx) {
2933*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2934*4882a593Smuzhiyun __func__);
2935*4882a593Smuzhiyun ret = -ENOMEM;
2936*4882a593Smuzhiyun goto command_cleanup;
2937*4882a593Smuzhiyun }
2938*4882a593Smuzhiyun ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2939*4882a593Smuzhiyun ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2940*4882a593Smuzhiyun ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
2941*4882a593Smuzhiyun
2942*4882a593Smuzhiyun /* Don't issue the command if there's no endpoints to update. */
2943*4882a593Smuzhiyun if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
2944*4882a593Smuzhiyun ctrl_ctx->drop_flags == 0) {
2945*4882a593Smuzhiyun ret = 0;
2946*4882a593Smuzhiyun goto command_cleanup;
2947*4882a593Smuzhiyun }
2948*4882a593Smuzhiyun /* Fix up Context Entries field. Minimum value is EP0 == BIT(1). */
2949*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
2950*4882a593Smuzhiyun for (i = 31; i >= 1; i--) {
2951*4882a593Smuzhiyun __le32 le32 = cpu_to_le32(BIT(i));
2952*4882a593Smuzhiyun
2953*4882a593Smuzhiyun if ((virt_dev->eps[i-1].ring && !(ctrl_ctx->drop_flags & le32))
2954*4882a593Smuzhiyun || (ctrl_ctx->add_flags & le32) || i == 1) {
2955*4882a593Smuzhiyun slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
2956*4882a593Smuzhiyun slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(i));
2957*4882a593Smuzhiyun break;
2958*4882a593Smuzhiyun }
2959*4882a593Smuzhiyun }
2960*4882a593Smuzhiyun
2961*4882a593Smuzhiyun ret = xhci_configure_endpoint(xhci, udev, command,
2962*4882a593Smuzhiyun false, false);
2963*4882a593Smuzhiyun if (ret)
2964*4882a593Smuzhiyun /* Callee should call reset_bandwidth() */
2965*4882a593Smuzhiyun goto command_cleanup;
2966*4882a593Smuzhiyun
2967*4882a593Smuzhiyun /* Free any rings that were dropped, but not changed. */
2968*4882a593Smuzhiyun for (i = 1; i < 31; i++) {
2969*4882a593Smuzhiyun if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
2970*4882a593Smuzhiyun !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1)))) {
2971*4882a593Smuzhiyun xhci_free_endpoint_ring(xhci, virt_dev, i);
2972*4882a593Smuzhiyun xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
2973*4882a593Smuzhiyun }
2974*4882a593Smuzhiyun }
2975*4882a593Smuzhiyun xhci_zero_in_ctx(xhci, virt_dev);
2976*4882a593Smuzhiyun /*
2977*4882a593Smuzhiyun * Install any rings for completely new endpoints or changed endpoints,
2978*4882a593Smuzhiyun * and free any old rings from changed endpoints.
2979*4882a593Smuzhiyun */
2980*4882a593Smuzhiyun for (i = 1; i < 31; i++) {
2981*4882a593Smuzhiyun if (!virt_dev->eps[i].new_ring)
2982*4882a593Smuzhiyun continue;
2983*4882a593Smuzhiyun /* Only free the old ring if it exists.
2984*4882a593Smuzhiyun * It may not if this is the first add of an endpoint.
2985*4882a593Smuzhiyun */
2986*4882a593Smuzhiyun if (virt_dev->eps[i].ring) {
2987*4882a593Smuzhiyun xhci_free_endpoint_ring(xhci, virt_dev, i);
2988*4882a593Smuzhiyun }
2989*4882a593Smuzhiyun xhci_check_bw_drop_ep_streams(xhci, virt_dev, i);
2990*4882a593Smuzhiyun virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2991*4882a593Smuzhiyun virt_dev->eps[i].new_ring = NULL;
2992*4882a593Smuzhiyun xhci_debugfs_create_endpoint(xhci, virt_dev, i);
2993*4882a593Smuzhiyun }
2994*4882a593Smuzhiyun command_cleanup:
2995*4882a593Smuzhiyun kfree(command->completion);
2996*4882a593Smuzhiyun kfree(command);
2997*4882a593Smuzhiyun
2998*4882a593Smuzhiyun return ret;
2999*4882a593Smuzhiyun }
3000*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_check_bandwidth);
3001*4882a593Smuzhiyun
xhci_reset_bandwidth(struct usb_hcd * hcd,struct usb_device * udev)3002*4882a593Smuzhiyun void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
3003*4882a593Smuzhiyun {
3004*4882a593Smuzhiyun struct xhci_hcd *xhci;
3005*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
3006*4882a593Smuzhiyun int i, ret;
3007*4882a593Smuzhiyun
3008*4882a593Smuzhiyun ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
3009*4882a593Smuzhiyun if (ret <= 0)
3010*4882a593Smuzhiyun return;
3011*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
3012*4882a593Smuzhiyun
3013*4882a593Smuzhiyun xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
3014*4882a593Smuzhiyun virt_dev = xhci->devs[udev->slot_id];
3015*4882a593Smuzhiyun /* Free any rings allocated for added endpoints */
3016*4882a593Smuzhiyun for (i = 0; i < 31; i++) {
3017*4882a593Smuzhiyun if (virt_dev->eps[i].new_ring) {
3018*4882a593Smuzhiyun xhci_debugfs_remove_endpoint(xhci, virt_dev, i);
3019*4882a593Smuzhiyun if (xhci_vendor_is_usb_offload_enabled(xhci, virt_dev, i))
3020*4882a593Smuzhiyun xhci_vendor_free_transfer_ring(xhci, virt_dev, i);
3021*4882a593Smuzhiyun else
3022*4882a593Smuzhiyun xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
3023*4882a593Smuzhiyun
3024*4882a593Smuzhiyun virt_dev->eps[i].new_ring = NULL;
3025*4882a593Smuzhiyun }
3026*4882a593Smuzhiyun }
3027*4882a593Smuzhiyun xhci_zero_in_ctx(xhci, virt_dev);
3028*4882a593Smuzhiyun }
3029*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_reset_bandwidth);
3030*4882a593Smuzhiyun
xhci_setup_input_ctx_for_config_ep(struct xhci_hcd * xhci,struct xhci_container_ctx * in_ctx,struct xhci_container_ctx * out_ctx,struct xhci_input_control_ctx * ctrl_ctx,u32 add_flags,u32 drop_flags)3031*4882a593Smuzhiyun static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
3032*4882a593Smuzhiyun struct xhci_container_ctx *in_ctx,
3033*4882a593Smuzhiyun struct xhci_container_ctx *out_ctx,
3034*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx,
3035*4882a593Smuzhiyun u32 add_flags, u32 drop_flags)
3036*4882a593Smuzhiyun {
3037*4882a593Smuzhiyun ctrl_ctx->add_flags = cpu_to_le32(add_flags);
3038*4882a593Smuzhiyun ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
3039*4882a593Smuzhiyun xhci_slot_copy(xhci, in_ctx, out_ctx);
3040*4882a593Smuzhiyun ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
3041*4882a593Smuzhiyun }
3042*4882a593Smuzhiyun
xhci_endpoint_disable(struct usb_hcd * hcd,struct usb_host_endpoint * host_ep)3043*4882a593Smuzhiyun static void xhci_endpoint_disable(struct usb_hcd *hcd,
3044*4882a593Smuzhiyun struct usb_host_endpoint *host_ep)
3045*4882a593Smuzhiyun {
3046*4882a593Smuzhiyun struct xhci_hcd *xhci;
3047*4882a593Smuzhiyun struct xhci_virt_device *vdev;
3048*4882a593Smuzhiyun struct xhci_virt_ep *ep;
3049*4882a593Smuzhiyun struct usb_device *udev;
3050*4882a593Smuzhiyun unsigned long flags;
3051*4882a593Smuzhiyun unsigned int ep_index;
3052*4882a593Smuzhiyun
3053*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
3054*4882a593Smuzhiyun rescan:
3055*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3056*4882a593Smuzhiyun
3057*4882a593Smuzhiyun udev = (struct usb_device *)host_ep->hcpriv;
3058*4882a593Smuzhiyun if (!udev || !udev->slot_id)
3059*4882a593Smuzhiyun goto done;
3060*4882a593Smuzhiyun
3061*4882a593Smuzhiyun vdev = xhci->devs[udev->slot_id];
3062*4882a593Smuzhiyun if (!vdev)
3063*4882a593Smuzhiyun goto done;
3064*4882a593Smuzhiyun
3065*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&host_ep->desc);
3066*4882a593Smuzhiyun ep = &vdev->eps[ep_index];
3067*4882a593Smuzhiyun if (!ep)
3068*4882a593Smuzhiyun goto done;
3069*4882a593Smuzhiyun
3070*4882a593Smuzhiyun /* wait for hub_tt_work to finish clearing hub TT */
3071*4882a593Smuzhiyun if (ep->ep_state & EP_CLEARING_TT) {
3072*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3073*4882a593Smuzhiyun schedule_timeout_uninterruptible(1);
3074*4882a593Smuzhiyun goto rescan;
3075*4882a593Smuzhiyun }
3076*4882a593Smuzhiyun
3077*4882a593Smuzhiyun if (ep->ep_state)
3078*4882a593Smuzhiyun xhci_dbg(xhci, "endpoint disable with ep_state 0x%x\n",
3079*4882a593Smuzhiyun ep->ep_state);
3080*4882a593Smuzhiyun done:
3081*4882a593Smuzhiyun host_ep->hcpriv = NULL;
3082*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3083*4882a593Smuzhiyun }
3084*4882a593Smuzhiyun
3085*4882a593Smuzhiyun /*
3086*4882a593Smuzhiyun * Called after usb core issues a clear halt control message.
3087*4882a593Smuzhiyun * The host side of the halt should already be cleared by a reset endpoint
3088*4882a593Smuzhiyun * command issued when the STALL event was received.
3089*4882a593Smuzhiyun *
3090*4882a593Smuzhiyun * The reset endpoint command may only be issued to endpoints in the halted
3091*4882a593Smuzhiyun * state. For software that wishes to reset the data toggle or sequence number
3092*4882a593Smuzhiyun * of an endpoint that isn't in the halted state this function will issue a
3093*4882a593Smuzhiyun * configure endpoint command with the Drop and Add bits set for the target
3094*4882a593Smuzhiyun * endpoint. Refer to the additional note in xhci spcification section 4.6.8.
3095*4882a593Smuzhiyun */
3096*4882a593Smuzhiyun
xhci_endpoint_reset(struct usb_hcd * hcd,struct usb_host_endpoint * host_ep)3097*4882a593Smuzhiyun static void xhci_endpoint_reset(struct usb_hcd *hcd,
3098*4882a593Smuzhiyun struct usb_host_endpoint *host_ep)
3099*4882a593Smuzhiyun {
3100*4882a593Smuzhiyun struct xhci_hcd *xhci;
3101*4882a593Smuzhiyun struct usb_device *udev;
3102*4882a593Smuzhiyun struct xhci_virt_device *vdev;
3103*4882a593Smuzhiyun struct xhci_virt_ep *ep;
3104*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
3105*4882a593Smuzhiyun struct xhci_command *stop_cmd, *cfg_cmd;
3106*4882a593Smuzhiyun unsigned int ep_index;
3107*4882a593Smuzhiyun unsigned long flags;
3108*4882a593Smuzhiyun u32 ep_flag;
3109*4882a593Smuzhiyun int err;
3110*4882a593Smuzhiyun
3111*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
3112*4882a593Smuzhiyun if (!host_ep->hcpriv)
3113*4882a593Smuzhiyun return;
3114*4882a593Smuzhiyun udev = (struct usb_device *) host_ep->hcpriv;
3115*4882a593Smuzhiyun vdev = xhci->devs[udev->slot_id];
3116*4882a593Smuzhiyun
3117*4882a593Smuzhiyun /*
3118*4882a593Smuzhiyun * vdev may be lost due to xHC restore error and re-initialization
3119*4882a593Smuzhiyun * during S3/S4 resume. A new vdev will be allocated later by
3120*4882a593Smuzhiyun * xhci_discover_or_reset_device()
3121*4882a593Smuzhiyun */
3122*4882a593Smuzhiyun if (!udev->slot_id || !vdev)
3123*4882a593Smuzhiyun return;
3124*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&host_ep->desc);
3125*4882a593Smuzhiyun ep = &vdev->eps[ep_index];
3126*4882a593Smuzhiyun if (!ep)
3127*4882a593Smuzhiyun return;
3128*4882a593Smuzhiyun
3129*4882a593Smuzhiyun /* Bail out if toggle is already being cleared by a endpoint reset */
3130*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3131*4882a593Smuzhiyun if (ep->ep_state & EP_HARD_CLEAR_TOGGLE) {
3132*4882a593Smuzhiyun ep->ep_state &= ~EP_HARD_CLEAR_TOGGLE;
3133*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3134*4882a593Smuzhiyun return;
3135*4882a593Smuzhiyun }
3136*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3137*4882a593Smuzhiyun /* Only interrupt and bulk ep's use data toggle, USB2 spec 5.5.4-> */
3138*4882a593Smuzhiyun if (usb_endpoint_xfer_control(&host_ep->desc) ||
3139*4882a593Smuzhiyun usb_endpoint_xfer_isoc(&host_ep->desc))
3140*4882a593Smuzhiyun return;
3141*4882a593Smuzhiyun
3142*4882a593Smuzhiyun ep_flag = xhci_get_endpoint_flag(&host_ep->desc);
3143*4882a593Smuzhiyun
3144*4882a593Smuzhiyun if (ep_flag == SLOT_FLAG || ep_flag == EP0_FLAG)
3145*4882a593Smuzhiyun return;
3146*4882a593Smuzhiyun
3147*4882a593Smuzhiyun stop_cmd = xhci_alloc_command(xhci, true, GFP_NOWAIT);
3148*4882a593Smuzhiyun if (!stop_cmd)
3149*4882a593Smuzhiyun return;
3150*4882a593Smuzhiyun
3151*4882a593Smuzhiyun cfg_cmd = xhci_alloc_command_with_ctx(xhci, true, GFP_NOWAIT);
3152*4882a593Smuzhiyun if (!cfg_cmd)
3153*4882a593Smuzhiyun goto cleanup;
3154*4882a593Smuzhiyun
3155*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3156*4882a593Smuzhiyun
3157*4882a593Smuzhiyun /* block queuing new trbs and ringing ep doorbell */
3158*4882a593Smuzhiyun ep->ep_state |= EP_SOFT_CLEAR_TOGGLE;
3159*4882a593Smuzhiyun
3160*4882a593Smuzhiyun /*
3161*4882a593Smuzhiyun * Make sure endpoint ring is empty before resetting the toggle/seq.
3162*4882a593Smuzhiyun * Driver is required to synchronously cancel all transfer request.
3163*4882a593Smuzhiyun * Stop the endpoint to force xHC to update the output context
3164*4882a593Smuzhiyun */
3165*4882a593Smuzhiyun
3166*4882a593Smuzhiyun if (!list_empty(&ep->ring->td_list)) {
3167*4882a593Smuzhiyun dev_err(&udev->dev, "EP not empty, refuse reset\n");
3168*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3169*4882a593Smuzhiyun xhci_free_command(xhci, cfg_cmd);
3170*4882a593Smuzhiyun goto cleanup;
3171*4882a593Smuzhiyun }
3172*4882a593Smuzhiyun
3173*4882a593Smuzhiyun err = xhci_queue_stop_endpoint(xhci, stop_cmd, udev->slot_id,
3174*4882a593Smuzhiyun ep_index, 0);
3175*4882a593Smuzhiyun if (err < 0) {
3176*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3177*4882a593Smuzhiyun xhci_free_command(xhci, cfg_cmd);
3178*4882a593Smuzhiyun xhci_dbg(xhci, "%s: Failed to queue stop ep command, %d ",
3179*4882a593Smuzhiyun __func__, err);
3180*4882a593Smuzhiyun goto cleanup;
3181*4882a593Smuzhiyun }
3182*4882a593Smuzhiyun
3183*4882a593Smuzhiyun xhci_ring_cmd_db(xhci);
3184*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3185*4882a593Smuzhiyun
3186*4882a593Smuzhiyun wait_for_completion(stop_cmd->completion);
3187*4882a593Smuzhiyun
3188*4882a593Smuzhiyun err = xhci_vendor_sync_dev_ctx(xhci, udev->slot_id);
3189*4882a593Smuzhiyun if (err) {
3190*4882a593Smuzhiyun xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
3191*4882a593Smuzhiyun __func__, err);
3192*4882a593Smuzhiyun goto cleanup;
3193*4882a593Smuzhiyun }
3194*4882a593Smuzhiyun
3195*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3196*4882a593Smuzhiyun
3197*4882a593Smuzhiyun /* config ep command clears toggle if add and drop ep flags are set */
3198*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(cfg_cmd->in_ctx);
3199*4882a593Smuzhiyun if (!ctrl_ctx) {
3200*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3201*4882a593Smuzhiyun xhci_free_command(xhci, cfg_cmd);
3202*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3203*4882a593Smuzhiyun __func__);
3204*4882a593Smuzhiyun goto cleanup;
3205*4882a593Smuzhiyun }
3206*4882a593Smuzhiyun
3207*4882a593Smuzhiyun xhci_setup_input_ctx_for_config_ep(xhci, cfg_cmd->in_ctx, vdev->out_ctx,
3208*4882a593Smuzhiyun ctrl_ctx, ep_flag, ep_flag);
3209*4882a593Smuzhiyun xhci_endpoint_copy(xhci, cfg_cmd->in_ctx, vdev->out_ctx, ep_index);
3210*4882a593Smuzhiyun
3211*4882a593Smuzhiyun err = xhci_queue_configure_endpoint(xhci, cfg_cmd, cfg_cmd->in_ctx->dma,
3212*4882a593Smuzhiyun udev->slot_id, false);
3213*4882a593Smuzhiyun if (err < 0) {
3214*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3215*4882a593Smuzhiyun xhci_free_command(xhci, cfg_cmd);
3216*4882a593Smuzhiyun xhci_dbg(xhci, "%s: Failed to queue config ep command, %d ",
3217*4882a593Smuzhiyun __func__, err);
3218*4882a593Smuzhiyun goto cleanup;
3219*4882a593Smuzhiyun }
3220*4882a593Smuzhiyun
3221*4882a593Smuzhiyun xhci_ring_cmd_db(xhci);
3222*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3223*4882a593Smuzhiyun
3224*4882a593Smuzhiyun wait_for_completion(cfg_cmd->completion);
3225*4882a593Smuzhiyun
3226*4882a593Smuzhiyun err = xhci_vendor_sync_dev_ctx(xhci, udev->slot_id);
3227*4882a593Smuzhiyun if (err)
3228*4882a593Smuzhiyun xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
3229*4882a593Smuzhiyun __func__, err);
3230*4882a593Smuzhiyun
3231*4882a593Smuzhiyun xhci_free_command(xhci, cfg_cmd);
3232*4882a593Smuzhiyun cleanup:
3233*4882a593Smuzhiyun xhci_free_command(xhci, stop_cmd);
3234*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3235*4882a593Smuzhiyun if (ep->ep_state & EP_SOFT_CLEAR_TOGGLE)
3236*4882a593Smuzhiyun ep->ep_state &= ~EP_SOFT_CLEAR_TOGGLE;
3237*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3238*4882a593Smuzhiyun }
3239*4882a593Smuzhiyun
xhci_check_streams_endpoint(struct xhci_hcd * xhci,struct usb_device * udev,struct usb_host_endpoint * ep,unsigned int slot_id)3240*4882a593Smuzhiyun static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
3241*4882a593Smuzhiyun struct usb_device *udev, struct usb_host_endpoint *ep,
3242*4882a593Smuzhiyun unsigned int slot_id)
3243*4882a593Smuzhiyun {
3244*4882a593Smuzhiyun int ret;
3245*4882a593Smuzhiyun unsigned int ep_index;
3246*4882a593Smuzhiyun unsigned int ep_state;
3247*4882a593Smuzhiyun
3248*4882a593Smuzhiyun if (!ep)
3249*4882a593Smuzhiyun return -EINVAL;
3250*4882a593Smuzhiyun ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
3251*4882a593Smuzhiyun if (ret <= 0)
3252*4882a593Smuzhiyun return ret ? ret : -EINVAL;
3253*4882a593Smuzhiyun if (usb_ss_max_streams(&ep->ss_ep_comp) == 0) {
3254*4882a593Smuzhiyun xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
3255*4882a593Smuzhiyun " descriptor for ep 0x%x does not support streams\n",
3256*4882a593Smuzhiyun ep->desc.bEndpointAddress);
3257*4882a593Smuzhiyun return -EINVAL;
3258*4882a593Smuzhiyun }
3259*4882a593Smuzhiyun
3260*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&ep->desc);
3261*4882a593Smuzhiyun ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3262*4882a593Smuzhiyun if (ep_state & EP_HAS_STREAMS ||
3263*4882a593Smuzhiyun ep_state & EP_GETTING_STREAMS) {
3264*4882a593Smuzhiyun xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
3265*4882a593Smuzhiyun "already has streams set up.\n",
3266*4882a593Smuzhiyun ep->desc.bEndpointAddress);
3267*4882a593Smuzhiyun xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
3268*4882a593Smuzhiyun "dynamic stream context array reallocation.\n");
3269*4882a593Smuzhiyun return -EINVAL;
3270*4882a593Smuzhiyun }
3271*4882a593Smuzhiyun if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
3272*4882a593Smuzhiyun xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
3273*4882a593Smuzhiyun "endpoint 0x%x; URBs are pending.\n",
3274*4882a593Smuzhiyun ep->desc.bEndpointAddress);
3275*4882a593Smuzhiyun return -EINVAL;
3276*4882a593Smuzhiyun }
3277*4882a593Smuzhiyun return 0;
3278*4882a593Smuzhiyun }
3279*4882a593Smuzhiyun
xhci_calculate_streams_entries(struct xhci_hcd * xhci,unsigned int * num_streams,unsigned int * num_stream_ctxs)3280*4882a593Smuzhiyun static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
3281*4882a593Smuzhiyun unsigned int *num_streams, unsigned int *num_stream_ctxs)
3282*4882a593Smuzhiyun {
3283*4882a593Smuzhiyun unsigned int max_streams;
3284*4882a593Smuzhiyun
3285*4882a593Smuzhiyun /* The stream context array size must be a power of two */
3286*4882a593Smuzhiyun *num_stream_ctxs = roundup_pow_of_two(*num_streams);
3287*4882a593Smuzhiyun /*
3288*4882a593Smuzhiyun * Find out how many primary stream array entries the host controller
3289*4882a593Smuzhiyun * supports. Later we may use secondary stream arrays (similar to 2nd
3290*4882a593Smuzhiyun * level page entries), but that's an optional feature for xHCI host
3291*4882a593Smuzhiyun * controllers. xHCs must support at least 4 stream IDs.
3292*4882a593Smuzhiyun */
3293*4882a593Smuzhiyun max_streams = HCC_MAX_PSA(xhci->hcc_params);
3294*4882a593Smuzhiyun if (*num_stream_ctxs > max_streams) {
3295*4882a593Smuzhiyun xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
3296*4882a593Smuzhiyun max_streams);
3297*4882a593Smuzhiyun *num_stream_ctxs = max_streams;
3298*4882a593Smuzhiyun *num_streams = max_streams;
3299*4882a593Smuzhiyun }
3300*4882a593Smuzhiyun }
3301*4882a593Smuzhiyun
3302*4882a593Smuzhiyun /* Returns an error code if one of the endpoint already has streams.
3303*4882a593Smuzhiyun * This does not change any data structures, it only checks and gathers
3304*4882a593Smuzhiyun * information.
3305*4882a593Smuzhiyun */
xhci_calculate_streams_and_bitmask(struct xhci_hcd * xhci,struct usb_device * udev,struct usb_host_endpoint ** eps,unsigned int num_eps,unsigned int * num_streams,u32 * changed_ep_bitmask)3306*4882a593Smuzhiyun static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
3307*4882a593Smuzhiyun struct usb_device *udev,
3308*4882a593Smuzhiyun struct usb_host_endpoint **eps, unsigned int num_eps,
3309*4882a593Smuzhiyun unsigned int *num_streams, u32 *changed_ep_bitmask)
3310*4882a593Smuzhiyun {
3311*4882a593Smuzhiyun unsigned int max_streams;
3312*4882a593Smuzhiyun unsigned int endpoint_flag;
3313*4882a593Smuzhiyun int i;
3314*4882a593Smuzhiyun int ret;
3315*4882a593Smuzhiyun
3316*4882a593Smuzhiyun for (i = 0; i < num_eps; i++) {
3317*4882a593Smuzhiyun ret = xhci_check_streams_endpoint(xhci, udev,
3318*4882a593Smuzhiyun eps[i], udev->slot_id);
3319*4882a593Smuzhiyun if (ret < 0)
3320*4882a593Smuzhiyun return ret;
3321*4882a593Smuzhiyun
3322*4882a593Smuzhiyun max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
3323*4882a593Smuzhiyun if (max_streams < (*num_streams - 1)) {
3324*4882a593Smuzhiyun xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
3325*4882a593Smuzhiyun eps[i]->desc.bEndpointAddress,
3326*4882a593Smuzhiyun max_streams);
3327*4882a593Smuzhiyun *num_streams = max_streams+1;
3328*4882a593Smuzhiyun }
3329*4882a593Smuzhiyun
3330*4882a593Smuzhiyun endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
3331*4882a593Smuzhiyun if (*changed_ep_bitmask & endpoint_flag)
3332*4882a593Smuzhiyun return -EINVAL;
3333*4882a593Smuzhiyun *changed_ep_bitmask |= endpoint_flag;
3334*4882a593Smuzhiyun }
3335*4882a593Smuzhiyun return 0;
3336*4882a593Smuzhiyun }
3337*4882a593Smuzhiyun
xhci_calculate_no_streams_bitmask(struct xhci_hcd * xhci,struct usb_device * udev,struct usb_host_endpoint ** eps,unsigned int num_eps)3338*4882a593Smuzhiyun static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3339*4882a593Smuzhiyun struct usb_device *udev,
3340*4882a593Smuzhiyun struct usb_host_endpoint **eps, unsigned int num_eps)
3341*4882a593Smuzhiyun {
3342*4882a593Smuzhiyun u32 changed_ep_bitmask = 0;
3343*4882a593Smuzhiyun unsigned int slot_id;
3344*4882a593Smuzhiyun unsigned int ep_index;
3345*4882a593Smuzhiyun unsigned int ep_state;
3346*4882a593Smuzhiyun int i;
3347*4882a593Smuzhiyun
3348*4882a593Smuzhiyun slot_id = udev->slot_id;
3349*4882a593Smuzhiyun if (!xhci->devs[slot_id])
3350*4882a593Smuzhiyun return 0;
3351*4882a593Smuzhiyun
3352*4882a593Smuzhiyun for (i = 0; i < num_eps; i++) {
3353*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3354*4882a593Smuzhiyun ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3355*4882a593Smuzhiyun /* Are streams already being freed for the endpoint? */
3356*4882a593Smuzhiyun if (ep_state & EP_GETTING_NO_STREAMS) {
3357*4882a593Smuzhiyun xhci_warn(xhci, "WARN Can't disable streams for "
3358*4882a593Smuzhiyun "endpoint 0x%x, "
3359*4882a593Smuzhiyun "streams are being disabled already\n",
3360*4882a593Smuzhiyun eps[i]->desc.bEndpointAddress);
3361*4882a593Smuzhiyun return 0;
3362*4882a593Smuzhiyun }
3363*4882a593Smuzhiyun /* Are there actually any streams to free? */
3364*4882a593Smuzhiyun if (!(ep_state & EP_HAS_STREAMS) &&
3365*4882a593Smuzhiyun !(ep_state & EP_GETTING_STREAMS)) {
3366*4882a593Smuzhiyun xhci_warn(xhci, "WARN Can't disable streams for "
3367*4882a593Smuzhiyun "endpoint 0x%x, "
3368*4882a593Smuzhiyun "streams are already disabled!\n",
3369*4882a593Smuzhiyun eps[i]->desc.bEndpointAddress);
3370*4882a593Smuzhiyun xhci_warn(xhci, "WARN xhci_free_streams() called "
3371*4882a593Smuzhiyun "with non-streams endpoint\n");
3372*4882a593Smuzhiyun return 0;
3373*4882a593Smuzhiyun }
3374*4882a593Smuzhiyun changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3375*4882a593Smuzhiyun }
3376*4882a593Smuzhiyun return changed_ep_bitmask;
3377*4882a593Smuzhiyun }
3378*4882a593Smuzhiyun
3379*4882a593Smuzhiyun /*
3380*4882a593Smuzhiyun * The USB device drivers use this function (through the HCD interface in USB
3381*4882a593Smuzhiyun * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3382*4882a593Smuzhiyun * coordinate mass storage command queueing across multiple endpoints (basically
3383*4882a593Smuzhiyun * a stream ID == a task ID).
3384*4882a593Smuzhiyun *
3385*4882a593Smuzhiyun * Setting up streams involves allocating the same size stream context array
3386*4882a593Smuzhiyun * for each endpoint and issuing a configure endpoint command for all endpoints.
3387*4882a593Smuzhiyun *
3388*4882a593Smuzhiyun * Don't allow the call to succeed if one endpoint only supports one stream
3389*4882a593Smuzhiyun * (which means it doesn't support streams at all).
3390*4882a593Smuzhiyun *
3391*4882a593Smuzhiyun * Drivers may get less stream IDs than they asked for, if the host controller
3392*4882a593Smuzhiyun * hardware or endpoints claim they can't support the number of requested
3393*4882a593Smuzhiyun * stream IDs.
3394*4882a593Smuzhiyun */
xhci_alloc_streams(struct usb_hcd * hcd,struct usb_device * udev,struct usb_host_endpoint ** eps,unsigned int num_eps,unsigned int num_streams,gfp_t mem_flags)3395*4882a593Smuzhiyun static int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
3396*4882a593Smuzhiyun struct usb_host_endpoint **eps, unsigned int num_eps,
3397*4882a593Smuzhiyun unsigned int num_streams, gfp_t mem_flags)
3398*4882a593Smuzhiyun {
3399*4882a593Smuzhiyun int i, ret;
3400*4882a593Smuzhiyun struct xhci_hcd *xhci;
3401*4882a593Smuzhiyun struct xhci_virt_device *vdev;
3402*4882a593Smuzhiyun struct xhci_command *config_cmd;
3403*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
3404*4882a593Smuzhiyun unsigned int ep_index;
3405*4882a593Smuzhiyun unsigned int num_stream_ctxs;
3406*4882a593Smuzhiyun unsigned int max_packet;
3407*4882a593Smuzhiyun unsigned long flags;
3408*4882a593Smuzhiyun u32 changed_ep_bitmask = 0;
3409*4882a593Smuzhiyun
3410*4882a593Smuzhiyun if (!eps)
3411*4882a593Smuzhiyun return -EINVAL;
3412*4882a593Smuzhiyun
3413*4882a593Smuzhiyun /* Add one to the number of streams requested to account for
3414*4882a593Smuzhiyun * stream 0 that is reserved for xHCI usage.
3415*4882a593Smuzhiyun */
3416*4882a593Smuzhiyun num_streams += 1;
3417*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
3418*4882a593Smuzhiyun xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3419*4882a593Smuzhiyun num_streams);
3420*4882a593Smuzhiyun
3421*4882a593Smuzhiyun /* MaxPSASize value 0 (2 streams) means streams are not supported */
3422*4882a593Smuzhiyun if ((xhci->quirks & XHCI_BROKEN_STREAMS) ||
3423*4882a593Smuzhiyun HCC_MAX_PSA(xhci->hcc_params) < 4) {
3424*4882a593Smuzhiyun xhci_dbg(xhci, "xHCI controller does not support streams.\n");
3425*4882a593Smuzhiyun return -ENOSYS;
3426*4882a593Smuzhiyun }
3427*4882a593Smuzhiyun
3428*4882a593Smuzhiyun config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags);
3429*4882a593Smuzhiyun if (!config_cmd)
3430*4882a593Smuzhiyun return -ENOMEM;
3431*4882a593Smuzhiyun
3432*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
3433*4882a593Smuzhiyun if (!ctrl_ctx) {
3434*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3435*4882a593Smuzhiyun __func__);
3436*4882a593Smuzhiyun xhci_free_command(xhci, config_cmd);
3437*4882a593Smuzhiyun return -ENOMEM;
3438*4882a593Smuzhiyun }
3439*4882a593Smuzhiyun
3440*4882a593Smuzhiyun /* Check to make sure all endpoints are not already configured for
3441*4882a593Smuzhiyun * streams. While we're at it, find the maximum number of streams that
3442*4882a593Smuzhiyun * all the endpoints will support and check for duplicate endpoints.
3443*4882a593Smuzhiyun */
3444*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3445*4882a593Smuzhiyun ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3446*4882a593Smuzhiyun num_eps, &num_streams, &changed_ep_bitmask);
3447*4882a593Smuzhiyun if (ret < 0) {
3448*4882a593Smuzhiyun xhci_free_command(xhci, config_cmd);
3449*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3450*4882a593Smuzhiyun return ret;
3451*4882a593Smuzhiyun }
3452*4882a593Smuzhiyun if (num_streams <= 1) {
3453*4882a593Smuzhiyun xhci_warn(xhci, "WARN: endpoints can't handle "
3454*4882a593Smuzhiyun "more than one stream.\n");
3455*4882a593Smuzhiyun xhci_free_command(xhci, config_cmd);
3456*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3457*4882a593Smuzhiyun return -EINVAL;
3458*4882a593Smuzhiyun }
3459*4882a593Smuzhiyun vdev = xhci->devs[udev->slot_id];
3460*4882a593Smuzhiyun /* Mark each endpoint as being in transition, so
3461*4882a593Smuzhiyun * xhci_urb_enqueue() will reject all URBs.
3462*4882a593Smuzhiyun */
3463*4882a593Smuzhiyun for (i = 0; i < num_eps; i++) {
3464*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3465*4882a593Smuzhiyun vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3466*4882a593Smuzhiyun }
3467*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3468*4882a593Smuzhiyun
3469*4882a593Smuzhiyun /* Setup internal data structures and allocate HW data structures for
3470*4882a593Smuzhiyun * streams (but don't install the HW structures in the input context
3471*4882a593Smuzhiyun * until we're sure all memory allocation succeeded).
3472*4882a593Smuzhiyun */
3473*4882a593Smuzhiyun xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3474*4882a593Smuzhiyun xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3475*4882a593Smuzhiyun num_stream_ctxs, num_streams);
3476*4882a593Smuzhiyun
3477*4882a593Smuzhiyun for (i = 0; i < num_eps; i++) {
3478*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3479*4882a593Smuzhiyun max_packet = usb_endpoint_maxp(&eps[i]->desc);
3480*4882a593Smuzhiyun vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3481*4882a593Smuzhiyun num_stream_ctxs,
3482*4882a593Smuzhiyun num_streams,
3483*4882a593Smuzhiyun max_packet, mem_flags);
3484*4882a593Smuzhiyun if (!vdev->eps[ep_index].stream_info)
3485*4882a593Smuzhiyun goto cleanup;
3486*4882a593Smuzhiyun /* Set maxPstreams in endpoint context and update deq ptr to
3487*4882a593Smuzhiyun * point to stream context array. FIXME
3488*4882a593Smuzhiyun */
3489*4882a593Smuzhiyun }
3490*4882a593Smuzhiyun
3491*4882a593Smuzhiyun /* Set up the input context for a configure endpoint command. */
3492*4882a593Smuzhiyun for (i = 0; i < num_eps; i++) {
3493*4882a593Smuzhiyun struct xhci_ep_ctx *ep_ctx;
3494*4882a593Smuzhiyun
3495*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3496*4882a593Smuzhiyun ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3497*4882a593Smuzhiyun
3498*4882a593Smuzhiyun xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3499*4882a593Smuzhiyun vdev->out_ctx, ep_index);
3500*4882a593Smuzhiyun xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3501*4882a593Smuzhiyun vdev->eps[ep_index].stream_info);
3502*4882a593Smuzhiyun }
3503*4882a593Smuzhiyun /* Tell the HW to drop its old copy of the endpoint context info
3504*4882a593Smuzhiyun * and add the updated copy from the input context.
3505*4882a593Smuzhiyun */
3506*4882a593Smuzhiyun xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
3507*4882a593Smuzhiyun vdev->out_ctx, ctrl_ctx,
3508*4882a593Smuzhiyun changed_ep_bitmask, changed_ep_bitmask);
3509*4882a593Smuzhiyun
3510*4882a593Smuzhiyun /* Issue and wait for the configure endpoint command */
3511*4882a593Smuzhiyun ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3512*4882a593Smuzhiyun false, false);
3513*4882a593Smuzhiyun
3514*4882a593Smuzhiyun /* xHC rejected the configure endpoint command for some reason, so we
3515*4882a593Smuzhiyun * leave the old ring intact and free our internal streams data
3516*4882a593Smuzhiyun * structure.
3517*4882a593Smuzhiyun */
3518*4882a593Smuzhiyun if (ret < 0)
3519*4882a593Smuzhiyun goto cleanup;
3520*4882a593Smuzhiyun
3521*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3522*4882a593Smuzhiyun for (i = 0; i < num_eps; i++) {
3523*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3524*4882a593Smuzhiyun vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3525*4882a593Smuzhiyun xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3526*4882a593Smuzhiyun udev->slot_id, ep_index);
3527*4882a593Smuzhiyun vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3528*4882a593Smuzhiyun }
3529*4882a593Smuzhiyun xhci_free_command(xhci, config_cmd);
3530*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3531*4882a593Smuzhiyun
3532*4882a593Smuzhiyun for (i = 0; i < num_eps; i++) {
3533*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3534*4882a593Smuzhiyun xhci_debugfs_create_stream_files(xhci, vdev, ep_index);
3535*4882a593Smuzhiyun }
3536*4882a593Smuzhiyun /* Subtract 1 for stream 0, which drivers can't use */
3537*4882a593Smuzhiyun return num_streams - 1;
3538*4882a593Smuzhiyun
3539*4882a593Smuzhiyun cleanup:
3540*4882a593Smuzhiyun /* If it didn't work, free the streams! */
3541*4882a593Smuzhiyun for (i = 0; i < num_eps; i++) {
3542*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3543*4882a593Smuzhiyun xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
3544*4882a593Smuzhiyun vdev->eps[ep_index].stream_info = NULL;
3545*4882a593Smuzhiyun /* FIXME Unset maxPstreams in endpoint context and
3546*4882a593Smuzhiyun * update deq ptr to point to normal string ring.
3547*4882a593Smuzhiyun */
3548*4882a593Smuzhiyun vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3549*4882a593Smuzhiyun vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3550*4882a593Smuzhiyun xhci_endpoint_zero(xhci, vdev, eps[i]);
3551*4882a593Smuzhiyun }
3552*4882a593Smuzhiyun xhci_free_command(xhci, config_cmd);
3553*4882a593Smuzhiyun return -ENOMEM;
3554*4882a593Smuzhiyun }
3555*4882a593Smuzhiyun
3556*4882a593Smuzhiyun /* Transition the endpoint from using streams to being a "normal" endpoint
3557*4882a593Smuzhiyun * without streams.
3558*4882a593Smuzhiyun *
3559*4882a593Smuzhiyun * Modify the endpoint context state, submit a configure endpoint command,
3560*4882a593Smuzhiyun * and free all endpoint rings for streams if that completes successfully.
3561*4882a593Smuzhiyun */
xhci_free_streams(struct usb_hcd * hcd,struct usb_device * udev,struct usb_host_endpoint ** eps,unsigned int num_eps,gfp_t mem_flags)3562*4882a593Smuzhiyun static int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
3563*4882a593Smuzhiyun struct usb_host_endpoint **eps, unsigned int num_eps,
3564*4882a593Smuzhiyun gfp_t mem_flags)
3565*4882a593Smuzhiyun {
3566*4882a593Smuzhiyun int i, ret;
3567*4882a593Smuzhiyun struct xhci_hcd *xhci;
3568*4882a593Smuzhiyun struct xhci_virt_device *vdev;
3569*4882a593Smuzhiyun struct xhci_command *command;
3570*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
3571*4882a593Smuzhiyun unsigned int ep_index;
3572*4882a593Smuzhiyun unsigned long flags;
3573*4882a593Smuzhiyun u32 changed_ep_bitmask;
3574*4882a593Smuzhiyun
3575*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
3576*4882a593Smuzhiyun vdev = xhci->devs[udev->slot_id];
3577*4882a593Smuzhiyun
3578*4882a593Smuzhiyun /* Set up a configure endpoint command to remove the streams rings */
3579*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3580*4882a593Smuzhiyun changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3581*4882a593Smuzhiyun udev, eps, num_eps);
3582*4882a593Smuzhiyun if (changed_ep_bitmask == 0) {
3583*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3584*4882a593Smuzhiyun return -EINVAL;
3585*4882a593Smuzhiyun }
3586*4882a593Smuzhiyun
3587*4882a593Smuzhiyun /* Use the xhci_command structure from the first endpoint. We may have
3588*4882a593Smuzhiyun * allocated too many, but the driver may call xhci_free_streams() for
3589*4882a593Smuzhiyun * each endpoint it grouped into one call to xhci_alloc_streams().
3590*4882a593Smuzhiyun */
3591*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3592*4882a593Smuzhiyun command = vdev->eps[ep_index].stream_info->free_streams_command;
3593*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
3594*4882a593Smuzhiyun if (!ctrl_ctx) {
3595*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3596*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3597*4882a593Smuzhiyun __func__);
3598*4882a593Smuzhiyun return -EINVAL;
3599*4882a593Smuzhiyun }
3600*4882a593Smuzhiyun
3601*4882a593Smuzhiyun for (i = 0; i < num_eps; i++) {
3602*4882a593Smuzhiyun struct xhci_ep_ctx *ep_ctx;
3603*4882a593Smuzhiyun
3604*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3605*4882a593Smuzhiyun ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3606*4882a593Smuzhiyun xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3607*4882a593Smuzhiyun EP_GETTING_NO_STREAMS;
3608*4882a593Smuzhiyun
3609*4882a593Smuzhiyun xhci_endpoint_copy(xhci, command->in_ctx,
3610*4882a593Smuzhiyun vdev->out_ctx, ep_index);
3611*4882a593Smuzhiyun xhci_setup_no_streams_ep_input_ctx(ep_ctx,
3612*4882a593Smuzhiyun &vdev->eps[ep_index]);
3613*4882a593Smuzhiyun }
3614*4882a593Smuzhiyun xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
3615*4882a593Smuzhiyun vdev->out_ctx, ctrl_ctx,
3616*4882a593Smuzhiyun changed_ep_bitmask, changed_ep_bitmask);
3617*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3618*4882a593Smuzhiyun
3619*4882a593Smuzhiyun /* Issue and wait for the configure endpoint command,
3620*4882a593Smuzhiyun * which must succeed.
3621*4882a593Smuzhiyun */
3622*4882a593Smuzhiyun ret = xhci_configure_endpoint(xhci, udev, command,
3623*4882a593Smuzhiyun false, true);
3624*4882a593Smuzhiyun
3625*4882a593Smuzhiyun /* xHC rejected the configure endpoint command for some reason, so we
3626*4882a593Smuzhiyun * leave the streams rings intact.
3627*4882a593Smuzhiyun */
3628*4882a593Smuzhiyun if (ret < 0)
3629*4882a593Smuzhiyun return ret;
3630*4882a593Smuzhiyun
3631*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3632*4882a593Smuzhiyun for (i = 0; i < num_eps; i++) {
3633*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3634*4882a593Smuzhiyun xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
3635*4882a593Smuzhiyun vdev->eps[ep_index].stream_info = NULL;
3636*4882a593Smuzhiyun /* FIXME Unset maxPstreams in endpoint context and
3637*4882a593Smuzhiyun * update deq ptr to point to normal string ring.
3638*4882a593Smuzhiyun */
3639*4882a593Smuzhiyun vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3640*4882a593Smuzhiyun vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3641*4882a593Smuzhiyun }
3642*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3643*4882a593Smuzhiyun
3644*4882a593Smuzhiyun return 0;
3645*4882a593Smuzhiyun }
3646*4882a593Smuzhiyun
3647*4882a593Smuzhiyun /*
3648*4882a593Smuzhiyun * Deletes endpoint resources for endpoints that were active before a Reset
3649*4882a593Smuzhiyun * Device command, or a Disable Slot command. The Reset Device command leaves
3650*4882a593Smuzhiyun * the control endpoint intact, whereas the Disable Slot command deletes it.
3651*4882a593Smuzhiyun *
3652*4882a593Smuzhiyun * Must be called with xhci->lock held.
3653*4882a593Smuzhiyun */
xhci_free_device_endpoint_resources(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,bool drop_control_ep)3654*4882a593Smuzhiyun void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3655*4882a593Smuzhiyun struct xhci_virt_device *virt_dev, bool drop_control_ep)
3656*4882a593Smuzhiyun {
3657*4882a593Smuzhiyun int i;
3658*4882a593Smuzhiyun unsigned int num_dropped_eps = 0;
3659*4882a593Smuzhiyun unsigned int drop_flags = 0;
3660*4882a593Smuzhiyun
3661*4882a593Smuzhiyun for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3662*4882a593Smuzhiyun if (virt_dev->eps[i].ring) {
3663*4882a593Smuzhiyun drop_flags |= 1 << i;
3664*4882a593Smuzhiyun num_dropped_eps++;
3665*4882a593Smuzhiyun }
3666*4882a593Smuzhiyun }
3667*4882a593Smuzhiyun xhci->num_active_eps -= num_dropped_eps;
3668*4882a593Smuzhiyun if (num_dropped_eps)
3669*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3670*4882a593Smuzhiyun "Dropped %u ep ctxs, flags = 0x%x, "
3671*4882a593Smuzhiyun "%u now active.",
3672*4882a593Smuzhiyun num_dropped_eps, drop_flags,
3673*4882a593Smuzhiyun xhci->num_active_eps);
3674*4882a593Smuzhiyun }
3675*4882a593Smuzhiyun
3676*4882a593Smuzhiyun /*
3677*4882a593Smuzhiyun * This submits a Reset Device Command, which will set the device state to 0,
3678*4882a593Smuzhiyun * set the device address to 0, and disable all the endpoints except the default
3679*4882a593Smuzhiyun * control endpoint. The USB core should come back and call
3680*4882a593Smuzhiyun * xhci_address_device(), and then re-set up the configuration. If this is
3681*4882a593Smuzhiyun * called because of a usb_reset_and_verify_device(), then the old alternate
3682*4882a593Smuzhiyun * settings will be re-installed through the normal bandwidth allocation
3683*4882a593Smuzhiyun * functions.
3684*4882a593Smuzhiyun *
3685*4882a593Smuzhiyun * Wait for the Reset Device command to finish. Remove all structures
3686*4882a593Smuzhiyun * associated with the endpoints that were disabled. Clear the input device
3687*4882a593Smuzhiyun * structure? Reset the control endpoint 0 max packet size?
3688*4882a593Smuzhiyun *
3689*4882a593Smuzhiyun * If the virt_dev to be reset does not exist or does not match the udev,
3690*4882a593Smuzhiyun * it means the device is lost, possibly due to the xHC restore error and
3691*4882a593Smuzhiyun * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3692*4882a593Smuzhiyun * re-allocate the device.
3693*4882a593Smuzhiyun */
xhci_discover_or_reset_device(struct usb_hcd * hcd,struct usb_device * udev)3694*4882a593Smuzhiyun static int xhci_discover_or_reset_device(struct usb_hcd *hcd,
3695*4882a593Smuzhiyun struct usb_device *udev)
3696*4882a593Smuzhiyun {
3697*4882a593Smuzhiyun int ret, i;
3698*4882a593Smuzhiyun unsigned long flags;
3699*4882a593Smuzhiyun struct xhci_hcd *xhci;
3700*4882a593Smuzhiyun unsigned int slot_id;
3701*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
3702*4882a593Smuzhiyun struct xhci_command *reset_device_cmd;
3703*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
3704*4882a593Smuzhiyun int old_active_eps = 0;
3705*4882a593Smuzhiyun
3706*4882a593Smuzhiyun ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
3707*4882a593Smuzhiyun if (ret <= 0)
3708*4882a593Smuzhiyun return ret;
3709*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
3710*4882a593Smuzhiyun slot_id = udev->slot_id;
3711*4882a593Smuzhiyun virt_dev = xhci->devs[slot_id];
3712*4882a593Smuzhiyun if (!virt_dev) {
3713*4882a593Smuzhiyun xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3714*4882a593Smuzhiyun "not exist. Re-allocate the device\n", slot_id);
3715*4882a593Smuzhiyun ret = xhci_alloc_dev(hcd, udev);
3716*4882a593Smuzhiyun if (ret == 1)
3717*4882a593Smuzhiyun return 0;
3718*4882a593Smuzhiyun else
3719*4882a593Smuzhiyun return -EINVAL;
3720*4882a593Smuzhiyun }
3721*4882a593Smuzhiyun
3722*4882a593Smuzhiyun if (virt_dev->tt_info)
3723*4882a593Smuzhiyun old_active_eps = virt_dev->tt_info->active_eps;
3724*4882a593Smuzhiyun
3725*4882a593Smuzhiyun if (virt_dev->udev != udev) {
3726*4882a593Smuzhiyun /* If the virt_dev and the udev does not match, this virt_dev
3727*4882a593Smuzhiyun * may belong to another udev.
3728*4882a593Smuzhiyun * Re-allocate the device.
3729*4882a593Smuzhiyun */
3730*4882a593Smuzhiyun xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3731*4882a593Smuzhiyun "not match the udev. Re-allocate the device\n",
3732*4882a593Smuzhiyun slot_id);
3733*4882a593Smuzhiyun ret = xhci_alloc_dev(hcd, udev);
3734*4882a593Smuzhiyun if (ret == 1)
3735*4882a593Smuzhiyun return 0;
3736*4882a593Smuzhiyun else
3737*4882a593Smuzhiyun return -EINVAL;
3738*4882a593Smuzhiyun }
3739*4882a593Smuzhiyun
3740*4882a593Smuzhiyun /* If device is not setup, there is no point in resetting it */
3741*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3742*4882a593Smuzhiyun if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3743*4882a593Smuzhiyun SLOT_STATE_DISABLED)
3744*4882a593Smuzhiyun return 0;
3745*4882a593Smuzhiyun
3746*4882a593Smuzhiyun trace_xhci_discover_or_reset_device(slot_ctx);
3747*4882a593Smuzhiyun
3748*4882a593Smuzhiyun xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3749*4882a593Smuzhiyun /* Allocate the command structure that holds the struct completion.
3750*4882a593Smuzhiyun * Assume we're in process context, since the normal device reset
3751*4882a593Smuzhiyun * process has to wait for the device anyway. Storage devices are
3752*4882a593Smuzhiyun * reset as part of error handling, so use GFP_NOIO instead of
3753*4882a593Smuzhiyun * GFP_KERNEL.
3754*4882a593Smuzhiyun */
3755*4882a593Smuzhiyun reset_device_cmd = xhci_alloc_command(xhci, true, GFP_NOIO);
3756*4882a593Smuzhiyun if (!reset_device_cmd) {
3757*4882a593Smuzhiyun xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3758*4882a593Smuzhiyun return -ENOMEM;
3759*4882a593Smuzhiyun }
3760*4882a593Smuzhiyun
3761*4882a593Smuzhiyun /* Attempt to submit the Reset Device command to the command ring */
3762*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3763*4882a593Smuzhiyun
3764*4882a593Smuzhiyun ret = xhci_queue_reset_device(xhci, reset_device_cmd, slot_id);
3765*4882a593Smuzhiyun if (ret) {
3766*4882a593Smuzhiyun xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3767*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3768*4882a593Smuzhiyun goto command_cleanup;
3769*4882a593Smuzhiyun }
3770*4882a593Smuzhiyun xhci_ring_cmd_db(xhci);
3771*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3772*4882a593Smuzhiyun
3773*4882a593Smuzhiyun /* Wait for the Reset Device command to finish */
3774*4882a593Smuzhiyun wait_for_completion(reset_device_cmd->completion);
3775*4882a593Smuzhiyun
3776*4882a593Smuzhiyun ret = xhci_vendor_sync_dev_ctx(xhci, slot_id);
3777*4882a593Smuzhiyun if (ret) {
3778*4882a593Smuzhiyun xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
3779*4882a593Smuzhiyun __func__, ret);
3780*4882a593Smuzhiyun goto command_cleanup;
3781*4882a593Smuzhiyun }
3782*4882a593Smuzhiyun
3783*4882a593Smuzhiyun /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3784*4882a593Smuzhiyun * unless we tried to reset a slot ID that wasn't enabled,
3785*4882a593Smuzhiyun * or the device wasn't in the addressed or configured state.
3786*4882a593Smuzhiyun */
3787*4882a593Smuzhiyun ret = reset_device_cmd->status;
3788*4882a593Smuzhiyun switch (ret) {
3789*4882a593Smuzhiyun case COMP_COMMAND_ABORTED:
3790*4882a593Smuzhiyun case COMP_COMMAND_RING_STOPPED:
3791*4882a593Smuzhiyun xhci_warn(xhci, "Timeout waiting for reset device command\n");
3792*4882a593Smuzhiyun ret = -ETIME;
3793*4882a593Smuzhiyun goto command_cleanup;
3794*4882a593Smuzhiyun case COMP_SLOT_NOT_ENABLED_ERROR: /* 0.95 completion for bad slot ID */
3795*4882a593Smuzhiyun case COMP_CONTEXT_STATE_ERROR: /* 0.96 completion code for same thing */
3796*4882a593Smuzhiyun xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n",
3797*4882a593Smuzhiyun slot_id,
3798*4882a593Smuzhiyun xhci_get_slot_state(xhci, virt_dev->out_ctx));
3799*4882a593Smuzhiyun xhci_dbg(xhci, "Not freeing device rings.\n");
3800*4882a593Smuzhiyun /* Don't treat this as an error. May change my mind later. */
3801*4882a593Smuzhiyun ret = 0;
3802*4882a593Smuzhiyun goto command_cleanup;
3803*4882a593Smuzhiyun case COMP_SUCCESS:
3804*4882a593Smuzhiyun xhci_dbg(xhci, "Successful reset device command.\n");
3805*4882a593Smuzhiyun break;
3806*4882a593Smuzhiyun default:
3807*4882a593Smuzhiyun if (xhci_is_vendor_info_code(xhci, ret))
3808*4882a593Smuzhiyun break;
3809*4882a593Smuzhiyun xhci_warn(xhci, "Unknown completion code %u for "
3810*4882a593Smuzhiyun "reset device command.\n", ret);
3811*4882a593Smuzhiyun ret = -EINVAL;
3812*4882a593Smuzhiyun goto command_cleanup;
3813*4882a593Smuzhiyun }
3814*4882a593Smuzhiyun
3815*4882a593Smuzhiyun /* Free up host controller endpoint resources */
3816*4882a593Smuzhiyun if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3817*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3818*4882a593Smuzhiyun /* Don't delete the default control endpoint resources */
3819*4882a593Smuzhiyun xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3820*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3821*4882a593Smuzhiyun }
3822*4882a593Smuzhiyun
3823*4882a593Smuzhiyun /* Everything but endpoint 0 is disabled, so free the rings. */
3824*4882a593Smuzhiyun for (i = 1; i < 31; i++) {
3825*4882a593Smuzhiyun struct xhci_virt_ep *ep = &virt_dev->eps[i];
3826*4882a593Smuzhiyun
3827*4882a593Smuzhiyun if (ep->ep_state & EP_HAS_STREAMS) {
3828*4882a593Smuzhiyun xhci_warn(xhci, "WARN: endpoint 0x%02x has streams on device reset, freeing streams.\n",
3829*4882a593Smuzhiyun xhci_get_endpoint_address(i));
3830*4882a593Smuzhiyun xhci_free_stream_info(xhci, ep->stream_info);
3831*4882a593Smuzhiyun ep->stream_info = NULL;
3832*4882a593Smuzhiyun ep->ep_state &= ~EP_HAS_STREAMS;
3833*4882a593Smuzhiyun }
3834*4882a593Smuzhiyun
3835*4882a593Smuzhiyun if (ep->ring) {
3836*4882a593Smuzhiyun xhci_debugfs_remove_endpoint(xhci, virt_dev, i);
3837*4882a593Smuzhiyun xhci_free_endpoint_ring(xhci, virt_dev, i);
3838*4882a593Smuzhiyun }
3839*4882a593Smuzhiyun if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3840*4882a593Smuzhiyun xhci_drop_ep_from_interval_table(xhci,
3841*4882a593Smuzhiyun &virt_dev->eps[i].bw_info,
3842*4882a593Smuzhiyun virt_dev->bw_table,
3843*4882a593Smuzhiyun udev,
3844*4882a593Smuzhiyun &virt_dev->eps[i],
3845*4882a593Smuzhiyun virt_dev->tt_info);
3846*4882a593Smuzhiyun xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
3847*4882a593Smuzhiyun }
3848*4882a593Smuzhiyun /* If necessary, update the number of active TTs on this root port */
3849*4882a593Smuzhiyun xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
3850*4882a593Smuzhiyun virt_dev->flags = 0;
3851*4882a593Smuzhiyun ret = 0;
3852*4882a593Smuzhiyun
3853*4882a593Smuzhiyun command_cleanup:
3854*4882a593Smuzhiyun xhci_free_command(xhci, reset_device_cmd);
3855*4882a593Smuzhiyun return ret;
3856*4882a593Smuzhiyun }
3857*4882a593Smuzhiyun
3858*4882a593Smuzhiyun /*
3859*4882a593Smuzhiyun * At this point, the struct usb_device is about to go away, the device has
3860*4882a593Smuzhiyun * disconnected, and all traffic has been stopped and the endpoints have been
3861*4882a593Smuzhiyun * disabled. Free any HC data structures associated with that device.
3862*4882a593Smuzhiyun */
xhci_free_dev(struct usb_hcd * hcd,struct usb_device * udev)3863*4882a593Smuzhiyun static void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
3864*4882a593Smuzhiyun {
3865*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3866*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
3867*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
3868*4882a593Smuzhiyun int i, ret;
3869*4882a593Smuzhiyun
3870*4882a593Smuzhiyun /*
3871*4882a593Smuzhiyun * We called pm_runtime_get_noresume when the device was attached.
3872*4882a593Smuzhiyun * Decrement the counter here to allow controller to runtime suspend
3873*4882a593Smuzhiyun * if no devices remain.
3874*4882a593Smuzhiyun */
3875*4882a593Smuzhiyun if (xhci->quirks & XHCI_RESET_ON_RESUME)
3876*4882a593Smuzhiyun pm_runtime_put_noidle(hcd->self.controller);
3877*4882a593Smuzhiyun
3878*4882a593Smuzhiyun ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
3879*4882a593Smuzhiyun /* If the host is halted due to driver unload, we still need to free the
3880*4882a593Smuzhiyun * device.
3881*4882a593Smuzhiyun */
3882*4882a593Smuzhiyun if (ret <= 0 && ret != -ENODEV)
3883*4882a593Smuzhiyun return;
3884*4882a593Smuzhiyun
3885*4882a593Smuzhiyun virt_dev = xhci->devs[udev->slot_id];
3886*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3887*4882a593Smuzhiyun trace_xhci_free_dev(slot_ctx);
3888*4882a593Smuzhiyun
3889*4882a593Smuzhiyun /* Stop any wayward timer functions (which may grab the lock) */
3890*4882a593Smuzhiyun for (i = 0; i < 31; i++) {
3891*4882a593Smuzhiyun virt_dev->eps[i].ep_state &= ~EP_STOP_CMD_PENDING;
3892*4882a593Smuzhiyun del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3893*4882a593Smuzhiyun }
3894*4882a593Smuzhiyun virt_dev->udev = NULL;
3895*4882a593Smuzhiyun xhci_disable_slot(xhci, udev->slot_id);
3896*4882a593Smuzhiyun xhci_free_virt_device(xhci, udev->slot_id);
3897*4882a593Smuzhiyun }
3898*4882a593Smuzhiyun
xhci_disable_slot(struct xhci_hcd * xhci,u32 slot_id)3899*4882a593Smuzhiyun int xhci_disable_slot(struct xhci_hcd *xhci, u32 slot_id)
3900*4882a593Smuzhiyun {
3901*4882a593Smuzhiyun struct xhci_command *command;
3902*4882a593Smuzhiyun unsigned long flags;
3903*4882a593Smuzhiyun u32 state;
3904*4882a593Smuzhiyun int ret = 0;
3905*4882a593Smuzhiyun
3906*4882a593Smuzhiyun command = xhci_alloc_command(xhci, true, GFP_KERNEL);
3907*4882a593Smuzhiyun if (!command)
3908*4882a593Smuzhiyun return -ENOMEM;
3909*4882a593Smuzhiyun
3910*4882a593Smuzhiyun xhci_debugfs_remove_slot(xhci, slot_id);
3911*4882a593Smuzhiyun
3912*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3913*4882a593Smuzhiyun /* Don't disable the slot if the host controller is dead. */
3914*4882a593Smuzhiyun state = readl(&xhci->op_regs->status);
3915*4882a593Smuzhiyun if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3916*4882a593Smuzhiyun (xhci->xhc_state & XHCI_STATE_HALTED)) {
3917*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3918*4882a593Smuzhiyun kfree(command);
3919*4882a593Smuzhiyun return -ENODEV;
3920*4882a593Smuzhiyun }
3921*4882a593Smuzhiyun
3922*4882a593Smuzhiyun ret = xhci_queue_slot_control(xhci, command, TRB_DISABLE_SLOT,
3923*4882a593Smuzhiyun slot_id);
3924*4882a593Smuzhiyun if (ret) {
3925*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3926*4882a593Smuzhiyun kfree(command);
3927*4882a593Smuzhiyun return ret;
3928*4882a593Smuzhiyun }
3929*4882a593Smuzhiyun xhci_ring_cmd_db(xhci);
3930*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3931*4882a593Smuzhiyun
3932*4882a593Smuzhiyun wait_for_completion(command->completion);
3933*4882a593Smuzhiyun
3934*4882a593Smuzhiyun if (command->status != COMP_SUCCESS)
3935*4882a593Smuzhiyun xhci_warn(xhci, "Unsuccessful disable slot %u command, status %d\n",
3936*4882a593Smuzhiyun slot_id, command->status);
3937*4882a593Smuzhiyun
3938*4882a593Smuzhiyun xhci_free_command(xhci, command);
3939*4882a593Smuzhiyun
3940*4882a593Smuzhiyun return ret;
3941*4882a593Smuzhiyun }
3942*4882a593Smuzhiyun
3943*4882a593Smuzhiyun /*
3944*4882a593Smuzhiyun * Checks if we have enough host controller resources for the default control
3945*4882a593Smuzhiyun * endpoint.
3946*4882a593Smuzhiyun *
3947*4882a593Smuzhiyun * Must be called with xhci->lock held.
3948*4882a593Smuzhiyun */
xhci_reserve_host_control_ep_resources(struct xhci_hcd * xhci)3949*4882a593Smuzhiyun static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3950*4882a593Smuzhiyun {
3951*4882a593Smuzhiyun if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
3952*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3953*4882a593Smuzhiyun "Not enough ep ctxs: "
3954*4882a593Smuzhiyun "%u active, need to add 1, limit is %u.",
3955*4882a593Smuzhiyun xhci->num_active_eps, xhci->limit_active_eps);
3956*4882a593Smuzhiyun return -ENOMEM;
3957*4882a593Smuzhiyun }
3958*4882a593Smuzhiyun xhci->num_active_eps += 1;
3959*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3960*4882a593Smuzhiyun "Adding 1 ep ctx, %u now active.",
3961*4882a593Smuzhiyun xhci->num_active_eps);
3962*4882a593Smuzhiyun return 0;
3963*4882a593Smuzhiyun }
3964*4882a593Smuzhiyun
3965*4882a593Smuzhiyun
3966*4882a593Smuzhiyun /*
3967*4882a593Smuzhiyun * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3968*4882a593Smuzhiyun * timed out, or allocating memory failed. Returns 1 on success.
3969*4882a593Smuzhiyun */
xhci_alloc_dev(struct usb_hcd * hcd,struct usb_device * udev)3970*4882a593Smuzhiyun int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3971*4882a593Smuzhiyun {
3972*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3973*4882a593Smuzhiyun struct xhci_virt_device *vdev;
3974*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
3975*4882a593Smuzhiyun unsigned long flags;
3976*4882a593Smuzhiyun int ret, slot_id;
3977*4882a593Smuzhiyun struct xhci_command *command;
3978*4882a593Smuzhiyun
3979*4882a593Smuzhiyun command = xhci_alloc_command(xhci, true, GFP_KERNEL);
3980*4882a593Smuzhiyun if (!command)
3981*4882a593Smuzhiyun return 0;
3982*4882a593Smuzhiyun
3983*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
3984*4882a593Smuzhiyun ret = xhci_queue_slot_control(xhci, command, TRB_ENABLE_SLOT, 0);
3985*4882a593Smuzhiyun if (ret) {
3986*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3987*4882a593Smuzhiyun xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3988*4882a593Smuzhiyun xhci_free_command(xhci, command);
3989*4882a593Smuzhiyun return 0;
3990*4882a593Smuzhiyun }
3991*4882a593Smuzhiyun xhci_ring_cmd_db(xhci);
3992*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
3993*4882a593Smuzhiyun
3994*4882a593Smuzhiyun wait_for_completion(command->completion);
3995*4882a593Smuzhiyun slot_id = command->slot_id;
3996*4882a593Smuzhiyun
3997*4882a593Smuzhiyun if (!slot_id || command->status != COMP_SUCCESS) {
3998*4882a593Smuzhiyun xhci_err(xhci, "Error while assigning device slot ID\n");
3999*4882a593Smuzhiyun xhci_err(xhci, "Max number of devices this xHCI host supports is %u.\n",
4000*4882a593Smuzhiyun HCS_MAX_SLOTS(
4001*4882a593Smuzhiyun readl(&xhci->cap_regs->hcs_params1)));
4002*4882a593Smuzhiyun xhci_free_command(xhci, command);
4003*4882a593Smuzhiyun return 0;
4004*4882a593Smuzhiyun }
4005*4882a593Smuzhiyun
4006*4882a593Smuzhiyun xhci_free_command(xhci, command);
4007*4882a593Smuzhiyun
4008*4882a593Smuzhiyun if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
4009*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
4010*4882a593Smuzhiyun ret = xhci_reserve_host_control_ep_resources(xhci);
4011*4882a593Smuzhiyun if (ret) {
4012*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4013*4882a593Smuzhiyun xhci_warn(xhci, "Not enough host resources, "
4014*4882a593Smuzhiyun "active endpoint contexts = %u\n",
4015*4882a593Smuzhiyun xhci->num_active_eps);
4016*4882a593Smuzhiyun goto disable_slot;
4017*4882a593Smuzhiyun }
4018*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4019*4882a593Smuzhiyun }
4020*4882a593Smuzhiyun /* Use GFP_NOIO, since this function can be called from
4021*4882a593Smuzhiyun * xhci_discover_or_reset_device(), which may be called as part of
4022*4882a593Smuzhiyun * mass storage driver error handling.
4023*4882a593Smuzhiyun */
4024*4882a593Smuzhiyun if (!xhci_alloc_virt_device(xhci, slot_id, udev, GFP_NOIO)) {
4025*4882a593Smuzhiyun xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
4026*4882a593Smuzhiyun goto disable_slot;
4027*4882a593Smuzhiyun }
4028*4882a593Smuzhiyun
4029*4882a593Smuzhiyun ret = xhci_vendor_sync_dev_ctx(xhci, slot_id);
4030*4882a593Smuzhiyun if (ret) {
4031*4882a593Smuzhiyun xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
4032*4882a593Smuzhiyun __func__, ret);
4033*4882a593Smuzhiyun goto disable_slot;
4034*4882a593Smuzhiyun }
4035*4882a593Smuzhiyun
4036*4882a593Smuzhiyun vdev = xhci->devs[slot_id];
4037*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(xhci, vdev->out_ctx);
4038*4882a593Smuzhiyun trace_xhci_alloc_dev(slot_ctx);
4039*4882a593Smuzhiyun
4040*4882a593Smuzhiyun udev->slot_id = slot_id;
4041*4882a593Smuzhiyun
4042*4882a593Smuzhiyun xhci_debugfs_create_slot(xhci, slot_id);
4043*4882a593Smuzhiyun
4044*4882a593Smuzhiyun /*
4045*4882a593Smuzhiyun * If resetting upon resume, we can't put the controller into runtime
4046*4882a593Smuzhiyun * suspend if there is a device attached.
4047*4882a593Smuzhiyun */
4048*4882a593Smuzhiyun if (xhci->quirks & XHCI_RESET_ON_RESUME)
4049*4882a593Smuzhiyun pm_runtime_get_noresume(hcd->self.controller);
4050*4882a593Smuzhiyun
4051*4882a593Smuzhiyun /* Is this a LS or FS device under a HS hub? */
4052*4882a593Smuzhiyun /* Hub or peripherial? */
4053*4882a593Smuzhiyun return 1;
4054*4882a593Smuzhiyun
4055*4882a593Smuzhiyun disable_slot:
4056*4882a593Smuzhiyun xhci_disable_slot(xhci, udev->slot_id);
4057*4882a593Smuzhiyun xhci_free_virt_device(xhci, udev->slot_id);
4058*4882a593Smuzhiyun
4059*4882a593Smuzhiyun return 0;
4060*4882a593Smuzhiyun }
4061*4882a593Smuzhiyun
4062*4882a593Smuzhiyun /*
4063*4882a593Smuzhiyun * Issue an Address Device command and optionally send a corresponding
4064*4882a593Smuzhiyun * SetAddress request to the device.
4065*4882a593Smuzhiyun */
xhci_setup_device(struct usb_hcd * hcd,struct usb_device * udev,enum xhci_setup_dev setup)4066*4882a593Smuzhiyun static int xhci_setup_device(struct usb_hcd *hcd, struct usb_device *udev,
4067*4882a593Smuzhiyun enum xhci_setup_dev setup)
4068*4882a593Smuzhiyun {
4069*4882a593Smuzhiyun const char *act = setup == SETUP_CONTEXT_ONLY ? "context" : "address";
4070*4882a593Smuzhiyun unsigned long flags;
4071*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
4072*4882a593Smuzhiyun int ret = 0;
4073*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4074*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
4075*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
4076*4882a593Smuzhiyun u64 temp_64;
4077*4882a593Smuzhiyun struct xhci_command *command = NULL;
4078*4882a593Smuzhiyun
4079*4882a593Smuzhiyun mutex_lock(&xhci->mutex);
4080*4882a593Smuzhiyun
4081*4882a593Smuzhiyun if (xhci->xhc_state) { /* dying, removing or halted */
4082*4882a593Smuzhiyun ret = -ESHUTDOWN;
4083*4882a593Smuzhiyun goto out;
4084*4882a593Smuzhiyun }
4085*4882a593Smuzhiyun
4086*4882a593Smuzhiyun if (!udev->slot_id) {
4087*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_address,
4088*4882a593Smuzhiyun "Bad Slot ID %d", udev->slot_id);
4089*4882a593Smuzhiyun ret = -EINVAL;
4090*4882a593Smuzhiyun goto out;
4091*4882a593Smuzhiyun }
4092*4882a593Smuzhiyun
4093*4882a593Smuzhiyun virt_dev = xhci->devs[udev->slot_id];
4094*4882a593Smuzhiyun
4095*4882a593Smuzhiyun if (WARN_ON(!virt_dev)) {
4096*4882a593Smuzhiyun /*
4097*4882a593Smuzhiyun * In plug/unplug torture test with an NEC controller,
4098*4882a593Smuzhiyun * a zero-dereference was observed once due to virt_dev = 0.
4099*4882a593Smuzhiyun * Print useful debug rather than crash if it is observed again!
4100*4882a593Smuzhiyun */
4101*4882a593Smuzhiyun xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
4102*4882a593Smuzhiyun udev->slot_id);
4103*4882a593Smuzhiyun ret = -EINVAL;
4104*4882a593Smuzhiyun goto out;
4105*4882a593Smuzhiyun }
4106*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
4107*4882a593Smuzhiyun trace_xhci_setup_device_slot(slot_ctx);
4108*4882a593Smuzhiyun
4109*4882a593Smuzhiyun if (setup == SETUP_CONTEXT_ONLY) {
4110*4882a593Smuzhiyun if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
4111*4882a593Smuzhiyun SLOT_STATE_DEFAULT) {
4112*4882a593Smuzhiyun xhci_dbg(xhci, "Slot already in default state\n");
4113*4882a593Smuzhiyun goto out;
4114*4882a593Smuzhiyun }
4115*4882a593Smuzhiyun }
4116*4882a593Smuzhiyun
4117*4882a593Smuzhiyun command = xhci_alloc_command(xhci, true, GFP_KERNEL);
4118*4882a593Smuzhiyun if (!command) {
4119*4882a593Smuzhiyun ret = -ENOMEM;
4120*4882a593Smuzhiyun goto out;
4121*4882a593Smuzhiyun }
4122*4882a593Smuzhiyun
4123*4882a593Smuzhiyun command->in_ctx = virt_dev->in_ctx;
4124*4882a593Smuzhiyun
4125*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
4126*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(virt_dev->in_ctx);
4127*4882a593Smuzhiyun if (!ctrl_ctx) {
4128*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4129*4882a593Smuzhiyun __func__);
4130*4882a593Smuzhiyun ret = -EINVAL;
4131*4882a593Smuzhiyun goto out;
4132*4882a593Smuzhiyun }
4133*4882a593Smuzhiyun /*
4134*4882a593Smuzhiyun * If this is the first Set Address since device plug-in or
4135*4882a593Smuzhiyun * virt_device realloaction after a resume with an xHCI power loss,
4136*4882a593Smuzhiyun * then set up the slot context.
4137*4882a593Smuzhiyun */
4138*4882a593Smuzhiyun if (!slot_ctx->dev_info)
4139*4882a593Smuzhiyun xhci_setup_addressable_virt_dev(xhci, udev);
4140*4882a593Smuzhiyun /* Otherwise, update the control endpoint ring enqueue pointer. */
4141*4882a593Smuzhiyun else
4142*4882a593Smuzhiyun xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
4143*4882a593Smuzhiyun ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
4144*4882a593Smuzhiyun ctrl_ctx->drop_flags = 0;
4145*4882a593Smuzhiyun
4146*4882a593Smuzhiyun trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
4147*4882a593Smuzhiyun le32_to_cpu(slot_ctx->dev_info) >> 27);
4148*4882a593Smuzhiyun
4149*4882a593Smuzhiyun trace_xhci_address_ctrl_ctx(ctrl_ctx);
4150*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
4151*4882a593Smuzhiyun trace_xhci_setup_device(virt_dev);
4152*4882a593Smuzhiyun ret = xhci_queue_address_device(xhci, command, virt_dev->in_ctx->dma,
4153*4882a593Smuzhiyun udev->slot_id, setup);
4154*4882a593Smuzhiyun if (ret) {
4155*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4156*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_address,
4157*4882a593Smuzhiyun "FIXME: allocate a command ring segment");
4158*4882a593Smuzhiyun goto out;
4159*4882a593Smuzhiyun }
4160*4882a593Smuzhiyun xhci_ring_cmd_db(xhci);
4161*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4162*4882a593Smuzhiyun
4163*4882a593Smuzhiyun /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
4164*4882a593Smuzhiyun wait_for_completion(command->completion);
4165*4882a593Smuzhiyun
4166*4882a593Smuzhiyun ret = xhci_vendor_sync_dev_ctx(xhci, udev->slot_id);
4167*4882a593Smuzhiyun if (ret) {
4168*4882a593Smuzhiyun xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
4169*4882a593Smuzhiyun __func__, ret);
4170*4882a593Smuzhiyun goto out;
4171*4882a593Smuzhiyun }
4172*4882a593Smuzhiyun
4173*4882a593Smuzhiyun /* FIXME: From section 4.3.4: "Software shall be responsible for timing
4174*4882a593Smuzhiyun * the SetAddress() "recovery interval" required by USB and aborting the
4175*4882a593Smuzhiyun * command on a timeout.
4176*4882a593Smuzhiyun */
4177*4882a593Smuzhiyun switch (command->status) {
4178*4882a593Smuzhiyun case COMP_COMMAND_ABORTED:
4179*4882a593Smuzhiyun case COMP_COMMAND_RING_STOPPED:
4180*4882a593Smuzhiyun xhci_warn(xhci, "Timeout while waiting for setup device command\n");
4181*4882a593Smuzhiyun ret = -ETIME;
4182*4882a593Smuzhiyun break;
4183*4882a593Smuzhiyun case COMP_CONTEXT_STATE_ERROR:
4184*4882a593Smuzhiyun case COMP_SLOT_NOT_ENABLED_ERROR:
4185*4882a593Smuzhiyun xhci_err(xhci, "Setup ERROR: setup %s command for slot %d.\n",
4186*4882a593Smuzhiyun act, udev->slot_id);
4187*4882a593Smuzhiyun ret = -EINVAL;
4188*4882a593Smuzhiyun break;
4189*4882a593Smuzhiyun case COMP_USB_TRANSACTION_ERROR:
4190*4882a593Smuzhiyun dev_warn(&udev->dev, "Device not responding to setup %s.\n", act);
4191*4882a593Smuzhiyun
4192*4882a593Smuzhiyun mutex_unlock(&xhci->mutex);
4193*4882a593Smuzhiyun ret = xhci_disable_slot(xhci, udev->slot_id);
4194*4882a593Smuzhiyun xhci_free_virt_device(xhci, udev->slot_id);
4195*4882a593Smuzhiyun if (!ret)
4196*4882a593Smuzhiyun xhci_alloc_dev(hcd, udev);
4197*4882a593Smuzhiyun kfree(command->completion);
4198*4882a593Smuzhiyun kfree(command);
4199*4882a593Smuzhiyun return -EPROTO;
4200*4882a593Smuzhiyun case COMP_INCOMPATIBLE_DEVICE_ERROR:
4201*4882a593Smuzhiyun dev_warn(&udev->dev,
4202*4882a593Smuzhiyun "ERROR: Incompatible device for setup %s command\n", act);
4203*4882a593Smuzhiyun ret = -ENODEV;
4204*4882a593Smuzhiyun break;
4205*4882a593Smuzhiyun case COMP_SUCCESS:
4206*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_address,
4207*4882a593Smuzhiyun "Successful setup %s command", act);
4208*4882a593Smuzhiyun break;
4209*4882a593Smuzhiyun default:
4210*4882a593Smuzhiyun xhci_err(xhci,
4211*4882a593Smuzhiyun "ERROR: unexpected setup %s command completion code 0x%x.\n",
4212*4882a593Smuzhiyun act, command->status);
4213*4882a593Smuzhiyun trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1);
4214*4882a593Smuzhiyun ret = -EINVAL;
4215*4882a593Smuzhiyun break;
4216*4882a593Smuzhiyun }
4217*4882a593Smuzhiyun if (ret)
4218*4882a593Smuzhiyun goto out;
4219*4882a593Smuzhiyun temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
4220*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_address,
4221*4882a593Smuzhiyun "Op regs DCBAA ptr = %#016llx", temp_64);
4222*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_address,
4223*4882a593Smuzhiyun "Slot ID %d dcbaa entry @%p = %#016llx",
4224*4882a593Smuzhiyun udev->slot_id,
4225*4882a593Smuzhiyun &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
4226*4882a593Smuzhiyun (unsigned long long)
4227*4882a593Smuzhiyun le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
4228*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_address,
4229*4882a593Smuzhiyun "Output Context DMA address = %#08llx",
4230*4882a593Smuzhiyun (unsigned long long)virt_dev->out_ctx->dma);
4231*4882a593Smuzhiyun trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
4232*4882a593Smuzhiyun le32_to_cpu(slot_ctx->dev_info) >> 27);
4233*4882a593Smuzhiyun /*
4234*4882a593Smuzhiyun * USB core uses address 1 for the roothubs, so we add one to the
4235*4882a593Smuzhiyun * address given back to us by the HC.
4236*4882a593Smuzhiyun */
4237*4882a593Smuzhiyun trace_xhci_address_ctx(xhci, virt_dev->out_ctx,
4238*4882a593Smuzhiyun le32_to_cpu(slot_ctx->dev_info) >> 27);
4239*4882a593Smuzhiyun /* Zero the input context control for later use */
4240*4882a593Smuzhiyun ctrl_ctx->add_flags = 0;
4241*4882a593Smuzhiyun ctrl_ctx->drop_flags = 0;
4242*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
4243*4882a593Smuzhiyun udev->devaddr = (u8)(le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
4244*4882a593Smuzhiyun
4245*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_address,
4246*4882a593Smuzhiyun "Internal device address = %d",
4247*4882a593Smuzhiyun le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK);
4248*4882a593Smuzhiyun out:
4249*4882a593Smuzhiyun mutex_unlock(&xhci->mutex);
4250*4882a593Smuzhiyun if (command) {
4251*4882a593Smuzhiyun kfree(command->completion);
4252*4882a593Smuzhiyun kfree(command);
4253*4882a593Smuzhiyun }
4254*4882a593Smuzhiyun return ret;
4255*4882a593Smuzhiyun }
4256*4882a593Smuzhiyun
xhci_address_device(struct usb_hcd * hcd,struct usb_device * udev)4257*4882a593Smuzhiyun int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
4258*4882a593Smuzhiyun {
4259*4882a593Smuzhiyun return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ADDRESS);
4260*4882a593Smuzhiyun }
4261*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_address_device);
4262*4882a593Smuzhiyun
xhci_enable_device(struct usb_hcd * hcd,struct usb_device * udev)4263*4882a593Smuzhiyun static int xhci_enable_device(struct usb_hcd *hcd, struct usb_device *udev)
4264*4882a593Smuzhiyun {
4265*4882a593Smuzhiyun return xhci_setup_device(hcd, udev, SETUP_CONTEXT_ONLY);
4266*4882a593Smuzhiyun }
4267*4882a593Smuzhiyun
4268*4882a593Smuzhiyun /*
4269*4882a593Smuzhiyun * Transfer the port index into real index in the HW port status
4270*4882a593Smuzhiyun * registers. Caculate offset between the port's PORTSC register
4271*4882a593Smuzhiyun * and port status base. Divide the number of per port register
4272*4882a593Smuzhiyun * to get the real index. The raw port number bases 1.
4273*4882a593Smuzhiyun */
xhci_find_raw_port_number(struct usb_hcd * hcd,int port1)4274*4882a593Smuzhiyun int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1)
4275*4882a593Smuzhiyun {
4276*4882a593Smuzhiyun struct xhci_hub *rhub;
4277*4882a593Smuzhiyun
4278*4882a593Smuzhiyun rhub = xhci_get_rhub(hcd);
4279*4882a593Smuzhiyun return rhub->ports[port1 - 1]->hw_portnum + 1;
4280*4882a593Smuzhiyun }
4281*4882a593Smuzhiyun
4282*4882a593Smuzhiyun /*
4283*4882a593Smuzhiyun * Issue an Evaluate Context command to change the Maximum Exit Latency in the
4284*4882a593Smuzhiyun * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
4285*4882a593Smuzhiyun */
xhci_change_max_exit_latency(struct xhci_hcd * xhci,struct usb_device * udev,u16 max_exit_latency)4286*4882a593Smuzhiyun static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
4287*4882a593Smuzhiyun struct usb_device *udev, u16 max_exit_latency)
4288*4882a593Smuzhiyun {
4289*4882a593Smuzhiyun struct xhci_virt_device *virt_dev;
4290*4882a593Smuzhiyun struct xhci_command *command;
4291*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
4292*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
4293*4882a593Smuzhiyun unsigned long flags;
4294*4882a593Smuzhiyun int ret;
4295*4882a593Smuzhiyun
4296*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
4297*4882a593Smuzhiyun
4298*4882a593Smuzhiyun virt_dev = xhci->devs[udev->slot_id];
4299*4882a593Smuzhiyun
4300*4882a593Smuzhiyun /*
4301*4882a593Smuzhiyun * virt_dev might not exists yet if xHC resumed from hibernate (S4) and
4302*4882a593Smuzhiyun * xHC was re-initialized. Exit latency will be set later after
4303*4882a593Smuzhiyun * hub_port_finish_reset() is done and xhci->devs[] are re-allocated
4304*4882a593Smuzhiyun */
4305*4882a593Smuzhiyun
4306*4882a593Smuzhiyun if (!virt_dev || max_exit_latency == virt_dev->current_mel) {
4307*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4308*4882a593Smuzhiyun return 0;
4309*4882a593Smuzhiyun }
4310*4882a593Smuzhiyun
4311*4882a593Smuzhiyun /* Attempt to issue an Evaluate Context command to change the MEL. */
4312*4882a593Smuzhiyun command = xhci->lpm_command;
4313*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(command->in_ctx);
4314*4882a593Smuzhiyun if (!ctrl_ctx) {
4315*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4316*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4317*4882a593Smuzhiyun __func__);
4318*4882a593Smuzhiyun return -ENOMEM;
4319*4882a593Smuzhiyun }
4320*4882a593Smuzhiyun
4321*4882a593Smuzhiyun ret = xhci_vendor_sync_dev_ctx(xhci, udev->slot_id);
4322*4882a593Smuzhiyun if (ret) {
4323*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4324*4882a593Smuzhiyun xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
4325*4882a593Smuzhiyun __func__, ret);
4326*4882a593Smuzhiyun return ret;
4327*4882a593Smuzhiyun }
4328*4882a593Smuzhiyun
4329*4882a593Smuzhiyun xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
4330*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4331*4882a593Smuzhiyun
4332*4882a593Smuzhiyun ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
4333*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
4334*4882a593Smuzhiyun slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
4335*4882a593Smuzhiyun slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
4336*4882a593Smuzhiyun slot_ctx->dev_state = 0;
4337*4882a593Smuzhiyun
4338*4882a593Smuzhiyun xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
4339*4882a593Smuzhiyun "Set up evaluate context for LPM MEL change.");
4340*4882a593Smuzhiyun
4341*4882a593Smuzhiyun /* Issue and wait for the evaluate context command. */
4342*4882a593Smuzhiyun ret = xhci_configure_endpoint(xhci, udev, command,
4343*4882a593Smuzhiyun true, true);
4344*4882a593Smuzhiyun
4345*4882a593Smuzhiyun if (!ret) {
4346*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
4347*4882a593Smuzhiyun virt_dev->current_mel = max_exit_latency;
4348*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4349*4882a593Smuzhiyun }
4350*4882a593Smuzhiyun return ret;
4351*4882a593Smuzhiyun }
4352*4882a593Smuzhiyun
xhci_vendor_get_ops(struct xhci_hcd * xhci)4353*4882a593Smuzhiyun struct xhci_vendor_ops *xhci_vendor_get_ops(struct xhci_hcd *xhci)
4354*4882a593Smuzhiyun {
4355*4882a593Smuzhiyun return xhci->vendor_ops;
4356*4882a593Smuzhiyun }
4357*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_vendor_get_ops);
4358*4882a593Smuzhiyun
xhci_vendor_sync_dev_ctx(struct xhci_hcd * xhci,unsigned int slot_id)4359*4882a593Smuzhiyun int xhci_vendor_sync_dev_ctx(struct xhci_hcd *xhci, unsigned int slot_id)
4360*4882a593Smuzhiyun {
4361*4882a593Smuzhiyun struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
4362*4882a593Smuzhiyun
4363*4882a593Smuzhiyun if (ops && ops->sync_dev_ctx)
4364*4882a593Smuzhiyun return ops->sync_dev_ctx(xhci, slot_id);
4365*4882a593Smuzhiyun return 0;
4366*4882a593Smuzhiyun }
4367*4882a593Smuzhiyun
xhci_vendor_usb_offload_skip_urb(struct xhci_hcd * xhci,struct urb * urb)4368*4882a593Smuzhiyun bool xhci_vendor_usb_offload_skip_urb(struct xhci_hcd *xhci, struct urb *urb)
4369*4882a593Smuzhiyun {
4370*4882a593Smuzhiyun struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
4371*4882a593Smuzhiyun
4372*4882a593Smuzhiyun if (ops && ops->usb_offload_skip_urb)
4373*4882a593Smuzhiyun return ops->usb_offload_skip_urb(xhci, urb);
4374*4882a593Smuzhiyun return false;
4375*4882a593Smuzhiyun }
4376*4882a593Smuzhiyun
4377*4882a593Smuzhiyun #ifdef CONFIG_PM
4378*4882a593Smuzhiyun
4379*4882a593Smuzhiyun /* BESL to HIRD Encoding array for USB2 LPM */
4380*4882a593Smuzhiyun static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
4381*4882a593Smuzhiyun 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
4382*4882a593Smuzhiyun
4383*4882a593Smuzhiyun /* Calculate HIRD/BESL for USB2 PORTPMSC*/
xhci_calculate_hird_besl(struct xhci_hcd * xhci,struct usb_device * udev)4384*4882a593Smuzhiyun static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
4385*4882a593Smuzhiyun struct usb_device *udev)
4386*4882a593Smuzhiyun {
4387*4882a593Smuzhiyun int u2del, besl, besl_host;
4388*4882a593Smuzhiyun int besl_device = 0;
4389*4882a593Smuzhiyun u32 field;
4390*4882a593Smuzhiyun
4391*4882a593Smuzhiyun u2del = HCS_U2_LATENCY(xhci->hcs_params3);
4392*4882a593Smuzhiyun field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4393*4882a593Smuzhiyun
4394*4882a593Smuzhiyun if (field & USB_BESL_SUPPORT) {
4395*4882a593Smuzhiyun for (besl_host = 0; besl_host < 16; besl_host++) {
4396*4882a593Smuzhiyun if (xhci_besl_encoding[besl_host] >= u2del)
4397*4882a593Smuzhiyun break;
4398*4882a593Smuzhiyun }
4399*4882a593Smuzhiyun /* Use baseline BESL value as default */
4400*4882a593Smuzhiyun if (field & USB_BESL_BASELINE_VALID)
4401*4882a593Smuzhiyun besl_device = USB_GET_BESL_BASELINE(field);
4402*4882a593Smuzhiyun else if (field & USB_BESL_DEEP_VALID)
4403*4882a593Smuzhiyun besl_device = USB_GET_BESL_DEEP(field);
4404*4882a593Smuzhiyun } else {
4405*4882a593Smuzhiyun if (u2del <= 50)
4406*4882a593Smuzhiyun besl_host = 0;
4407*4882a593Smuzhiyun else
4408*4882a593Smuzhiyun besl_host = (u2del - 51) / 75 + 1;
4409*4882a593Smuzhiyun }
4410*4882a593Smuzhiyun
4411*4882a593Smuzhiyun besl = besl_host + besl_device;
4412*4882a593Smuzhiyun if (besl > 15)
4413*4882a593Smuzhiyun besl = 15;
4414*4882a593Smuzhiyun
4415*4882a593Smuzhiyun return besl;
4416*4882a593Smuzhiyun }
4417*4882a593Smuzhiyun
4418*4882a593Smuzhiyun /* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
xhci_calculate_usb2_hw_lpm_params(struct usb_device * udev)4419*4882a593Smuzhiyun static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev)
4420*4882a593Smuzhiyun {
4421*4882a593Smuzhiyun u32 field;
4422*4882a593Smuzhiyun int l1;
4423*4882a593Smuzhiyun int besld = 0;
4424*4882a593Smuzhiyun int hirdm = 0;
4425*4882a593Smuzhiyun
4426*4882a593Smuzhiyun field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4427*4882a593Smuzhiyun
4428*4882a593Smuzhiyun /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
4429*4882a593Smuzhiyun l1 = udev->l1_params.timeout / 256;
4430*4882a593Smuzhiyun
4431*4882a593Smuzhiyun /* device has preferred BESLD */
4432*4882a593Smuzhiyun if (field & USB_BESL_DEEP_VALID) {
4433*4882a593Smuzhiyun besld = USB_GET_BESL_DEEP(field);
4434*4882a593Smuzhiyun hirdm = 1;
4435*4882a593Smuzhiyun }
4436*4882a593Smuzhiyun
4437*4882a593Smuzhiyun return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm);
4438*4882a593Smuzhiyun }
4439*4882a593Smuzhiyun
xhci_set_usb2_hardware_lpm(struct usb_hcd * hcd,struct usb_device * udev,int enable)4440*4882a593Smuzhiyun static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4441*4882a593Smuzhiyun struct usb_device *udev, int enable)
4442*4882a593Smuzhiyun {
4443*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4444*4882a593Smuzhiyun struct xhci_port **ports;
4445*4882a593Smuzhiyun __le32 __iomem *pm_addr, *hlpm_addr;
4446*4882a593Smuzhiyun u32 pm_val, hlpm_val, field;
4447*4882a593Smuzhiyun unsigned int port_num;
4448*4882a593Smuzhiyun unsigned long flags;
4449*4882a593Smuzhiyun int hird, exit_latency;
4450*4882a593Smuzhiyun int ret;
4451*4882a593Smuzhiyun
4452*4882a593Smuzhiyun if (xhci->quirks & XHCI_HW_LPM_DISABLE)
4453*4882a593Smuzhiyun return -EPERM;
4454*4882a593Smuzhiyun
4455*4882a593Smuzhiyun if (hcd->speed >= HCD_USB3 || !xhci->hw_lpm_support ||
4456*4882a593Smuzhiyun !udev->lpm_capable)
4457*4882a593Smuzhiyun return -EPERM;
4458*4882a593Smuzhiyun
4459*4882a593Smuzhiyun if (!udev->parent || udev->parent->parent ||
4460*4882a593Smuzhiyun udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4461*4882a593Smuzhiyun return -EPERM;
4462*4882a593Smuzhiyun
4463*4882a593Smuzhiyun if (udev->usb2_hw_lpm_capable != 1)
4464*4882a593Smuzhiyun return -EPERM;
4465*4882a593Smuzhiyun
4466*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
4467*4882a593Smuzhiyun
4468*4882a593Smuzhiyun ports = xhci->usb2_rhub.ports;
4469*4882a593Smuzhiyun port_num = udev->portnum - 1;
4470*4882a593Smuzhiyun pm_addr = ports[port_num]->addr + PORTPMSC;
4471*4882a593Smuzhiyun pm_val = readl(pm_addr);
4472*4882a593Smuzhiyun hlpm_addr = ports[port_num]->addr + PORTHLPMC;
4473*4882a593Smuzhiyun
4474*4882a593Smuzhiyun xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
4475*4882a593Smuzhiyun enable ? "enable" : "disable", port_num + 1);
4476*4882a593Smuzhiyun
4477*4882a593Smuzhiyun if (enable) {
4478*4882a593Smuzhiyun /* Host supports BESL timeout instead of HIRD */
4479*4882a593Smuzhiyun if (udev->usb2_hw_lpm_besl_capable) {
4480*4882a593Smuzhiyun /* if device doesn't have a preferred BESL value use a
4481*4882a593Smuzhiyun * default one which works with mixed HIRD and BESL
4482*4882a593Smuzhiyun * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4483*4882a593Smuzhiyun */
4484*4882a593Smuzhiyun field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4485*4882a593Smuzhiyun if ((field & USB_BESL_SUPPORT) &&
4486*4882a593Smuzhiyun (field & USB_BESL_BASELINE_VALID))
4487*4882a593Smuzhiyun hird = USB_GET_BESL_BASELINE(field);
4488*4882a593Smuzhiyun else
4489*4882a593Smuzhiyun hird = udev->l1_params.besl;
4490*4882a593Smuzhiyun
4491*4882a593Smuzhiyun exit_latency = xhci_besl_encoding[hird];
4492*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4493*4882a593Smuzhiyun
4494*4882a593Smuzhiyun /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4495*4882a593Smuzhiyun * input context for link powermanagement evaluate
4496*4882a593Smuzhiyun * context commands. It is protected by hcd->bandwidth
4497*4882a593Smuzhiyun * mutex and is shared by all devices. We need to set
4498*4882a593Smuzhiyun * the max ext latency in USB 2 BESL LPM as well, so
4499*4882a593Smuzhiyun * use the same mutex and xhci_change_max_exit_latency()
4500*4882a593Smuzhiyun */
4501*4882a593Smuzhiyun mutex_lock(hcd->bandwidth_mutex);
4502*4882a593Smuzhiyun ret = xhci_change_max_exit_latency(xhci, udev,
4503*4882a593Smuzhiyun exit_latency);
4504*4882a593Smuzhiyun mutex_unlock(hcd->bandwidth_mutex);
4505*4882a593Smuzhiyun
4506*4882a593Smuzhiyun if (ret < 0)
4507*4882a593Smuzhiyun return ret;
4508*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
4509*4882a593Smuzhiyun
4510*4882a593Smuzhiyun hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev);
4511*4882a593Smuzhiyun writel(hlpm_val, hlpm_addr);
4512*4882a593Smuzhiyun /* flush write */
4513*4882a593Smuzhiyun readl(hlpm_addr);
4514*4882a593Smuzhiyun } else {
4515*4882a593Smuzhiyun hird = xhci_calculate_hird_besl(xhci, udev);
4516*4882a593Smuzhiyun }
4517*4882a593Smuzhiyun
4518*4882a593Smuzhiyun pm_val &= ~PORT_HIRD_MASK;
4519*4882a593Smuzhiyun pm_val |= PORT_HIRD(hird) | PORT_RWE | PORT_L1DS(udev->slot_id);
4520*4882a593Smuzhiyun writel(pm_val, pm_addr);
4521*4882a593Smuzhiyun pm_val = readl(pm_addr);
4522*4882a593Smuzhiyun pm_val |= PORT_HLE;
4523*4882a593Smuzhiyun writel(pm_val, pm_addr);
4524*4882a593Smuzhiyun /* flush write */
4525*4882a593Smuzhiyun readl(pm_addr);
4526*4882a593Smuzhiyun } else {
4527*4882a593Smuzhiyun pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK | PORT_L1DS_MASK);
4528*4882a593Smuzhiyun writel(pm_val, pm_addr);
4529*4882a593Smuzhiyun /* flush write */
4530*4882a593Smuzhiyun readl(pm_addr);
4531*4882a593Smuzhiyun if (udev->usb2_hw_lpm_besl_capable) {
4532*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4533*4882a593Smuzhiyun mutex_lock(hcd->bandwidth_mutex);
4534*4882a593Smuzhiyun xhci_change_max_exit_latency(xhci, udev, 0);
4535*4882a593Smuzhiyun mutex_unlock(hcd->bandwidth_mutex);
4536*4882a593Smuzhiyun readl_poll_timeout(ports[port_num]->addr, pm_val,
4537*4882a593Smuzhiyun (pm_val & PORT_PLS_MASK) == XDEV_U0,
4538*4882a593Smuzhiyun 100, 10000);
4539*4882a593Smuzhiyun return 0;
4540*4882a593Smuzhiyun }
4541*4882a593Smuzhiyun }
4542*4882a593Smuzhiyun
4543*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
4544*4882a593Smuzhiyun return 0;
4545*4882a593Smuzhiyun }
4546*4882a593Smuzhiyun
4547*4882a593Smuzhiyun /* check if a usb2 port supports a given extened capability protocol
4548*4882a593Smuzhiyun * only USB2 ports extended protocol capability values are cached.
4549*4882a593Smuzhiyun * Return 1 if capability is supported
4550*4882a593Smuzhiyun */
xhci_check_usb2_port_capability(struct xhci_hcd * xhci,int port,unsigned capability)4551*4882a593Smuzhiyun static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port,
4552*4882a593Smuzhiyun unsigned capability)
4553*4882a593Smuzhiyun {
4554*4882a593Smuzhiyun u32 port_offset, port_count;
4555*4882a593Smuzhiyun int i;
4556*4882a593Smuzhiyun
4557*4882a593Smuzhiyun for (i = 0; i < xhci->num_ext_caps; i++) {
4558*4882a593Smuzhiyun if (xhci->ext_caps[i] & capability) {
4559*4882a593Smuzhiyun /* port offsets starts at 1 */
4560*4882a593Smuzhiyun port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1;
4561*4882a593Smuzhiyun port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]);
4562*4882a593Smuzhiyun if (port >= port_offset &&
4563*4882a593Smuzhiyun port < port_offset + port_count)
4564*4882a593Smuzhiyun return 1;
4565*4882a593Smuzhiyun }
4566*4882a593Smuzhiyun }
4567*4882a593Smuzhiyun return 0;
4568*4882a593Smuzhiyun }
4569*4882a593Smuzhiyun
xhci_update_device(struct usb_hcd * hcd,struct usb_device * udev)4570*4882a593Smuzhiyun static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4571*4882a593Smuzhiyun {
4572*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4573*4882a593Smuzhiyun int portnum = udev->portnum - 1;
4574*4882a593Smuzhiyun
4575*4882a593Smuzhiyun if (hcd->speed >= HCD_USB3 || !udev->lpm_capable)
4576*4882a593Smuzhiyun return 0;
4577*4882a593Smuzhiyun
4578*4882a593Smuzhiyun /* we only support lpm for non-hub device connected to root hub yet */
4579*4882a593Smuzhiyun if (!udev->parent || udev->parent->parent ||
4580*4882a593Smuzhiyun udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4581*4882a593Smuzhiyun return 0;
4582*4882a593Smuzhiyun
4583*4882a593Smuzhiyun if (xhci->hw_lpm_support == 1 &&
4584*4882a593Smuzhiyun xhci_check_usb2_port_capability(
4585*4882a593Smuzhiyun xhci, portnum, XHCI_HLC)) {
4586*4882a593Smuzhiyun udev->usb2_hw_lpm_capable = 1;
4587*4882a593Smuzhiyun udev->l1_params.timeout = XHCI_L1_TIMEOUT;
4588*4882a593Smuzhiyun udev->l1_params.besl = XHCI_DEFAULT_BESL;
4589*4882a593Smuzhiyun if (xhci_check_usb2_port_capability(xhci, portnum,
4590*4882a593Smuzhiyun XHCI_BLC))
4591*4882a593Smuzhiyun udev->usb2_hw_lpm_besl_capable = 1;
4592*4882a593Smuzhiyun }
4593*4882a593Smuzhiyun
4594*4882a593Smuzhiyun return 0;
4595*4882a593Smuzhiyun }
4596*4882a593Smuzhiyun
4597*4882a593Smuzhiyun /*---------------------- USB 3.0 Link PM functions ------------------------*/
4598*4882a593Smuzhiyun
4599*4882a593Smuzhiyun /* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
xhci_service_interval_to_ns(struct usb_endpoint_descriptor * desc)4600*4882a593Smuzhiyun static unsigned long long xhci_service_interval_to_ns(
4601*4882a593Smuzhiyun struct usb_endpoint_descriptor *desc)
4602*4882a593Smuzhiyun {
4603*4882a593Smuzhiyun return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
4604*4882a593Smuzhiyun }
4605*4882a593Smuzhiyun
xhci_get_timeout_no_hub_lpm(struct usb_device * udev,enum usb3_link_state state)4606*4882a593Smuzhiyun static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
4607*4882a593Smuzhiyun enum usb3_link_state state)
4608*4882a593Smuzhiyun {
4609*4882a593Smuzhiyun unsigned long long sel;
4610*4882a593Smuzhiyun unsigned long long pel;
4611*4882a593Smuzhiyun unsigned int max_sel_pel;
4612*4882a593Smuzhiyun char *state_name;
4613*4882a593Smuzhiyun
4614*4882a593Smuzhiyun switch (state) {
4615*4882a593Smuzhiyun case USB3_LPM_U1:
4616*4882a593Smuzhiyun /* Convert SEL and PEL stored in nanoseconds to microseconds */
4617*4882a593Smuzhiyun sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
4618*4882a593Smuzhiyun pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
4619*4882a593Smuzhiyun max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
4620*4882a593Smuzhiyun state_name = "U1";
4621*4882a593Smuzhiyun break;
4622*4882a593Smuzhiyun case USB3_LPM_U2:
4623*4882a593Smuzhiyun sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
4624*4882a593Smuzhiyun pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
4625*4882a593Smuzhiyun max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
4626*4882a593Smuzhiyun state_name = "U2";
4627*4882a593Smuzhiyun break;
4628*4882a593Smuzhiyun default:
4629*4882a593Smuzhiyun dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
4630*4882a593Smuzhiyun __func__);
4631*4882a593Smuzhiyun return USB3_LPM_DISABLED;
4632*4882a593Smuzhiyun }
4633*4882a593Smuzhiyun
4634*4882a593Smuzhiyun if (sel <= max_sel_pel && pel <= max_sel_pel)
4635*4882a593Smuzhiyun return USB3_LPM_DEVICE_INITIATED;
4636*4882a593Smuzhiyun
4637*4882a593Smuzhiyun if (sel > max_sel_pel)
4638*4882a593Smuzhiyun dev_dbg(&udev->dev, "Device-initiated %s disabled "
4639*4882a593Smuzhiyun "due to long SEL %llu ms\n",
4640*4882a593Smuzhiyun state_name, sel);
4641*4882a593Smuzhiyun else
4642*4882a593Smuzhiyun dev_dbg(&udev->dev, "Device-initiated %s disabled "
4643*4882a593Smuzhiyun "due to long PEL %llu ms\n",
4644*4882a593Smuzhiyun state_name, pel);
4645*4882a593Smuzhiyun return USB3_LPM_DISABLED;
4646*4882a593Smuzhiyun }
4647*4882a593Smuzhiyun
4648*4882a593Smuzhiyun /* The U1 timeout should be the maximum of the following values:
4649*4882a593Smuzhiyun * - For control endpoints, U1 system exit latency (SEL) * 3
4650*4882a593Smuzhiyun * - For bulk endpoints, U1 SEL * 5
4651*4882a593Smuzhiyun * - For interrupt endpoints:
4652*4882a593Smuzhiyun * - Notification EPs, U1 SEL * 3
4653*4882a593Smuzhiyun * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4654*4882a593Smuzhiyun * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4655*4882a593Smuzhiyun */
xhci_calculate_intel_u1_timeout(struct usb_device * udev,struct usb_endpoint_descriptor * desc)4656*4882a593Smuzhiyun static unsigned long long xhci_calculate_intel_u1_timeout(
4657*4882a593Smuzhiyun struct usb_device *udev,
4658*4882a593Smuzhiyun struct usb_endpoint_descriptor *desc)
4659*4882a593Smuzhiyun {
4660*4882a593Smuzhiyun unsigned long long timeout_ns;
4661*4882a593Smuzhiyun int ep_type;
4662*4882a593Smuzhiyun int intr_type;
4663*4882a593Smuzhiyun
4664*4882a593Smuzhiyun ep_type = usb_endpoint_type(desc);
4665*4882a593Smuzhiyun switch (ep_type) {
4666*4882a593Smuzhiyun case USB_ENDPOINT_XFER_CONTROL:
4667*4882a593Smuzhiyun timeout_ns = udev->u1_params.sel * 3;
4668*4882a593Smuzhiyun break;
4669*4882a593Smuzhiyun case USB_ENDPOINT_XFER_BULK:
4670*4882a593Smuzhiyun timeout_ns = udev->u1_params.sel * 5;
4671*4882a593Smuzhiyun break;
4672*4882a593Smuzhiyun case USB_ENDPOINT_XFER_INT:
4673*4882a593Smuzhiyun intr_type = usb_endpoint_interrupt_type(desc);
4674*4882a593Smuzhiyun if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
4675*4882a593Smuzhiyun timeout_ns = udev->u1_params.sel * 3;
4676*4882a593Smuzhiyun break;
4677*4882a593Smuzhiyun }
4678*4882a593Smuzhiyun /* Otherwise the calculation is the same as isoc eps */
4679*4882a593Smuzhiyun fallthrough;
4680*4882a593Smuzhiyun case USB_ENDPOINT_XFER_ISOC:
4681*4882a593Smuzhiyun timeout_ns = xhci_service_interval_to_ns(desc);
4682*4882a593Smuzhiyun timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
4683*4882a593Smuzhiyun if (timeout_ns < udev->u1_params.sel * 2)
4684*4882a593Smuzhiyun timeout_ns = udev->u1_params.sel * 2;
4685*4882a593Smuzhiyun break;
4686*4882a593Smuzhiyun default:
4687*4882a593Smuzhiyun return 0;
4688*4882a593Smuzhiyun }
4689*4882a593Smuzhiyun
4690*4882a593Smuzhiyun return timeout_ns;
4691*4882a593Smuzhiyun }
4692*4882a593Smuzhiyun
4693*4882a593Smuzhiyun /* Returns the hub-encoded U1 timeout value. */
xhci_calculate_u1_timeout(struct xhci_hcd * xhci,struct usb_device * udev,struct usb_endpoint_descriptor * desc)4694*4882a593Smuzhiyun static u16 xhci_calculate_u1_timeout(struct xhci_hcd *xhci,
4695*4882a593Smuzhiyun struct usb_device *udev,
4696*4882a593Smuzhiyun struct usb_endpoint_descriptor *desc)
4697*4882a593Smuzhiyun {
4698*4882a593Smuzhiyun unsigned long long timeout_ns;
4699*4882a593Smuzhiyun
4700*4882a593Smuzhiyun /* Prevent U1 if service interval is shorter than U1 exit latency */
4701*4882a593Smuzhiyun if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) {
4702*4882a593Smuzhiyun if (xhci_service_interval_to_ns(desc) <= udev->u1_params.mel) {
4703*4882a593Smuzhiyun dev_dbg(&udev->dev, "Disable U1, ESIT shorter than exit latency\n");
4704*4882a593Smuzhiyun return USB3_LPM_DISABLED;
4705*4882a593Smuzhiyun }
4706*4882a593Smuzhiyun }
4707*4882a593Smuzhiyun
4708*4882a593Smuzhiyun if (xhci->quirks & XHCI_INTEL_HOST)
4709*4882a593Smuzhiyun timeout_ns = xhci_calculate_intel_u1_timeout(udev, desc);
4710*4882a593Smuzhiyun else
4711*4882a593Smuzhiyun timeout_ns = udev->u1_params.sel;
4712*4882a593Smuzhiyun
4713*4882a593Smuzhiyun /* The U1 timeout is encoded in 1us intervals.
4714*4882a593Smuzhiyun * Don't return a timeout of zero, because that's USB3_LPM_DISABLED.
4715*4882a593Smuzhiyun */
4716*4882a593Smuzhiyun if (timeout_ns == USB3_LPM_DISABLED)
4717*4882a593Smuzhiyun timeout_ns = 1;
4718*4882a593Smuzhiyun else
4719*4882a593Smuzhiyun timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
4720*4882a593Smuzhiyun
4721*4882a593Smuzhiyun /* If the necessary timeout value is bigger than what we can set in the
4722*4882a593Smuzhiyun * USB 3.0 hub, we have to disable hub-initiated U1.
4723*4882a593Smuzhiyun */
4724*4882a593Smuzhiyun if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
4725*4882a593Smuzhiyun return timeout_ns;
4726*4882a593Smuzhiyun dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
4727*4882a593Smuzhiyun "due to long timeout %llu ms\n", timeout_ns);
4728*4882a593Smuzhiyun return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
4729*4882a593Smuzhiyun }
4730*4882a593Smuzhiyun
4731*4882a593Smuzhiyun /* The U2 timeout should be the maximum of:
4732*4882a593Smuzhiyun * - 10 ms (to avoid the bandwidth impact on the scheduler)
4733*4882a593Smuzhiyun * - largest bInterval of any active periodic endpoint (to avoid going
4734*4882a593Smuzhiyun * into lower power link states between intervals).
4735*4882a593Smuzhiyun * - the U2 Exit Latency of the device
4736*4882a593Smuzhiyun */
xhci_calculate_intel_u2_timeout(struct usb_device * udev,struct usb_endpoint_descriptor * desc)4737*4882a593Smuzhiyun static unsigned long long xhci_calculate_intel_u2_timeout(
4738*4882a593Smuzhiyun struct usb_device *udev,
4739*4882a593Smuzhiyun struct usb_endpoint_descriptor *desc)
4740*4882a593Smuzhiyun {
4741*4882a593Smuzhiyun unsigned long long timeout_ns;
4742*4882a593Smuzhiyun unsigned long long u2_del_ns;
4743*4882a593Smuzhiyun
4744*4882a593Smuzhiyun timeout_ns = 10 * 1000 * 1000;
4745*4882a593Smuzhiyun
4746*4882a593Smuzhiyun if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
4747*4882a593Smuzhiyun (xhci_service_interval_to_ns(desc) > timeout_ns))
4748*4882a593Smuzhiyun timeout_ns = xhci_service_interval_to_ns(desc);
4749*4882a593Smuzhiyun
4750*4882a593Smuzhiyun u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
4751*4882a593Smuzhiyun if (u2_del_ns > timeout_ns)
4752*4882a593Smuzhiyun timeout_ns = u2_del_ns;
4753*4882a593Smuzhiyun
4754*4882a593Smuzhiyun return timeout_ns;
4755*4882a593Smuzhiyun }
4756*4882a593Smuzhiyun
4757*4882a593Smuzhiyun /* Returns the hub-encoded U2 timeout value. */
xhci_calculate_u2_timeout(struct xhci_hcd * xhci,struct usb_device * udev,struct usb_endpoint_descriptor * desc)4758*4882a593Smuzhiyun static u16 xhci_calculate_u2_timeout(struct xhci_hcd *xhci,
4759*4882a593Smuzhiyun struct usb_device *udev,
4760*4882a593Smuzhiyun struct usb_endpoint_descriptor *desc)
4761*4882a593Smuzhiyun {
4762*4882a593Smuzhiyun unsigned long long timeout_ns;
4763*4882a593Smuzhiyun
4764*4882a593Smuzhiyun /* Prevent U2 if service interval is shorter than U2 exit latency */
4765*4882a593Smuzhiyun if (usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) {
4766*4882a593Smuzhiyun if (xhci_service_interval_to_ns(desc) <= udev->u2_params.mel) {
4767*4882a593Smuzhiyun dev_dbg(&udev->dev, "Disable U2, ESIT shorter than exit latency\n");
4768*4882a593Smuzhiyun return USB3_LPM_DISABLED;
4769*4882a593Smuzhiyun }
4770*4882a593Smuzhiyun }
4771*4882a593Smuzhiyun
4772*4882a593Smuzhiyun if (xhci->quirks & XHCI_INTEL_HOST)
4773*4882a593Smuzhiyun timeout_ns = xhci_calculate_intel_u2_timeout(udev, desc);
4774*4882a593Smuzhiyun else
4775*4882a593Smuzhiyun timeout_ns = udev->u2_params.sel;
4776*4882a593Smuzhiyun
4777*4882a593Smuzhiyun /* The U2 timeout is encoded in 256us intervals */
4778*4882a593Smuzhiyun timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
4779*4882a593Smuzhiyun /* If the necessary timeout value is bigger than what we can set in the
4780*4882a593Smuzhiyun * USB 3.0 hub, we have to disable hub-initiated U2.
4781*4882a593Smuzhiyun */
4782*4882a593Smuzhiyun if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
4783*4882a593Smuzhiyun return timeout_ns;
4784*4882a593Smuzhiyun dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
4785*4882a593Smuzhiyun "due to long timeout %llu ms\n", timeout_ns);
4786*4882a593Smuzhiyun return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
4787*4882a593Smuzhiyun }
4788*4882a593Smuzhiyun
xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd * xhci,struct usb_device * udev,struct usb_endpoint_descriptor * desc,enum usb3_link_state state,u16 * timeout)4789*4882a593Smuzhiyun static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4790*4882a593Smuzhiyun struct usb_device *udev,
4791*4882a593Smuzhiyun struct usb_endpoint_descriptor *desc,
4792*4882a593Smuzhiyun enum usb3_link_state state,
4793*4882a593Smuzhiyun u16 *timeout)
4794*4882a593Smuzhiyun {
4795*4882a593Smuzhiyun if (state == USB3_LPM_U1)
4796*4882a593Smuzhiyun return xhci_calculate_u1_timeout(xhci, udev, desc);
4797*4882a593Smuzhiyun else if (state == USB3_LPM_U2)
4798*4882a593Smuzhiyun return xhci_calculate_u2_timeout(xhci, udev, desc);
4799*4882a593Smuzhiyun
4800*4882a593Smuzhiyun return USB3_LPM_DISABLED;
4801*4882a593Smuzhiyun }
4802*4882a593Smuzhiyun
xhci_update_timeout_for_endpoint(struct xhci_hcd * xhci,struct usb_device * udev,struct usb_endpoint_descriptor * desc,enum usb3_link_state state,u16 * timeout)4803*4882a593Smuzhiyun static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4804*4882a593Smuzhiyun struct usb_device *udev,
4805*4882a593Smuzhiyun struct usb_endpoint_descriptor *desc,
4806*4882a593Smuzhiyun enum usb3_link_state state,
4807*4882a593Smuzhiyun u16 *timeout)
4808*4882a593Smuzhiyun {
4809*4882a593Smuzhiyun u16 alt_timeout;
4810*4882a593Smuzhiyun
4811*4882a593Smuzhiyun alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
4812*4882a593Smuzhiyun desc, state, timeout);
4813*4882a593Smuzhiyun
4814*4882a593Smuzhiyun /* If we found we can't enable hub-initiated LPM, and
4815*4882a593Smuzhiyun * the U1 or U2 exit latency was too high to allow
4816*4882a593Smuzhiyun * device-initiated LPM as well, then we will disable LPM
4817*4882a593Smuzhiyun * for this device, so stop searching any further.
4818*4882a593Smuzhiyun */
4819*4882a593Smuzhiyun if (alt_timeout == USB3_LPM_DISABLED) {
4820*4882a593Smuzhiyun *timeout = alt_timeout;
4821*4882a593Smuzhiyun return -E2BIG;
4822*4882a593Smuzhiyun }
4823*4882a593Smuzhiyun if (alt_timeout > *timeout)
4824*4882a593Smuzhiyun *timeout = alt_timeout;
4825*4882a593Smuzhiyun return 0;
4826*4882a593Smuzhiyun }
4827*4882a593Smuzhiyun
xhci_update_timeout_for_interface(struct xhci_hcd * xhci,struct usb_device * udev,struct usb_host_interface * alt,enum usb3_link_state state,u16 * timeout)4828*4882a593Smuzhiyun static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
4829*4882a593Smuzhiyun struct usb_device *udev,
4830*4882a593Smuzhiyun struct usb_host_interface *alt,
4831*4882a593Smuzhiyun enum usb3_link_state state,
4832*4882a593Smuzhiyun u16 *timeout)
4833*4882a593Smuzhiyun {
4834*4882a593Smuzhiyun int j;
4835*4882a593Smuzhiyun
4836*4882a593Smuzhiyun for (j = 0; j < alt->desc.bNumEndpoints; j++) {
4837*4882a593Smuzhiyun if (xhci_update_timeout_for_endpoint(xhci, udev,
4838*4882a593Smuzhiyun &alt->endpoint[j].desc, state, timeout))
4839*4882a593Smuzhiyun return -E2BIG;
4840*4882a593Smuzhiyun continue;
4841*4882a593Smuzhiyun }
4842*4882a593Smuzhiyun return 0;
4843*4882a593Smuzhiyun }
4844*4882a593Smuzhiyun
xhci_check_intel_tier_policy(struct usb_device * udev,enum usb3_link_state state)4845*4882a593Smuzhiyun static int xhci_check_intel_tier_policy(struct usb_device *udev,
4846*4882a593Smuzhiyun enum usb3_link_state state)
4847*4882a593Smuzhiyun {
4848*4882a593Smuzhiyun struct usb_device *parent;
4849*4882a593Smuzhiyun unsigned int num_hubs;
4850*4882a593Smuzhiyun
4851*4882a593Smuzhiyun if (state == USB3_LPM_U2)
4852*4882a593Smuzhiyun return 0;
4853*4882a593Smuzhiyun
4854*4882a593Smuzhiyun /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4855*4882a593Smuzhiyun for (parent = udev->parent, num_hubs = 0; parent->parent;
4856*4882a593Smuzhiyun parent = parent->parent)
4857*4882a593Smuzhiyun num_hubs++;
4858*4882a593Smuzhiyun
4859*4882a593Smuzhiyun if (num_hubs < 2)
4860*4882a593Smuzhiyun return 0;
4861*4882a593Smuzhiyun
4862*4882a593Smuzhiyun dev_dbg(&udev->dev, "Disabling U1 link state for device"
4863*4882a593Smuzhiyun " below second-tier hub.\n");
4864*4882a593Smuzhiyun dev_dbg(&udev->dev, "Plug device into first-tier hub "
4865*4882a593Smuzhiyun "to decrease power consumption.\n");
4866*4882a593Smuzhiyun return -E2BIG;
4867*4882a593Smuzhiyun }
4868*4882a593Smuzhiyun
xhci_check_tier_policy(struct xhci_hcd * xhci,struct usb_device * udev,enum usb3_link_state state)4869*4882a593Smuzhiyun static int xhci_check_tier_policy(struct xhci_hcd *xhci,
4870*4882a593Smuzhiyun struct usb_device *udev,
4871*4882a593Smuzhiyun enum usb3_link_state state)
4872*4882a593Smuzhiyun {
4873*4882a593Smuzhiyun if (xhci->quirks & XHCI_INTEL_HOST)
4874*4882a593Smuzhiyun return xhci_check_intel_tier_policy(udev, state);
4875*4882a593Smuzhiyun else
4876*4882a593Smuzhiyun return 0;
4877*4882a593Smuzhiyun }
4878*4882a593Smuzhiyun
4879*4882a593Smuzhiyun /* Returns the U1 or U2 timeout that should be enabled.
4880*4882a593Smuzhiyun * If the tier check or timeout setting functions return with a non-zero exit
4881*4882a593Smuzhiyun * code, that means the timeout value has been finalized and we shouldn't look
4882*4882a593Smuzhiyun * at any more endpoints.
4883*4882a593Smuzhiyun */
xhci_calculate_lpm_timeout(struct usb_hcd * hcd,struct usb_device * udev,enum usb3_link_state state)4884*4882a593Smuzhiyun static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
4885*4882a593Smuzhiyun struct usb_device *udev, enum usb3_link_state state)
4886*4882a593Smuzhiyun {
4887*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4888*4882a593Smuzhiyun struct usb_host_config *config;
4889*4882a593Smuzhiyun char *state_name;
4890*4882a593Smuzhiyun int i;
4891*4882a593Smuzhiyun u16 timeout = USB3_LPM_DISABLED;
4892*4882a593Smuzhiyun
4893*4882a593Smuzhiyun if (state == USB3_LPM_U1)
4894*4882a593Smuzhiyun state_name = "U1";
4895*4882a593Smuzhiyun else if (state == USB3_LPM_U2)
4896*4882a593Smuzhiyun state_name = "U2";
4897*4882a593Smuzhiyun else {
4898*4882a593Smuzhiyun dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
4899*4882a593Smuzhiyun state);
4900*4882a593Smuzhiyun return timeout;
4901*4882a593Smuzhiyun }
4902*4882a593Smuzhiyun
4903*4882a593Smuzhiyun if (xhci_check_tier_policy(xhci, udev, state) < 0)
4904*4882a593Smuzhiyun return timeout;
4905*4882a593Smuzhiyun
4906*4882a593Smuzhiyun /* Gather some information about the currently installed configuration
4907*4882a593Smuzhiyun * and alternate interface settings.
4908*4882a593Smuzhiyun */
4909*4882a593Smuzhiyun if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
4910*4882a593Smuzhiyun state, &timeout))
4911*4882a593Smuzhiyun return timeout;
4912*4882a593Smuzhiyun
4913*4882a593Smuzhiyun config = udev->actconfig;
4914*4882a593Smuzhiyun if (!config)
4915*4882a593Smuzhiyun return timeout;
4916*4882a593Smuzhiyun
4917*4882a593Smuzhiyun for (i = 0; i < config->desc.bNumInterfaces; i++) {
4918*4882a593Smuzhiyun struct usb_driver *driver;
4919*4882a593Smuzhiyun struct usb_interface *intf = config->interface[i];
4920*4882a593Smuzhiyun
4921*4882a593Smuzhiyun if (!intf)
4922*4882a593Smuzhiyun continue;
4923*4882a593Smuzhiyun
4924*4882a593Smuzhiyun /* Check if any currently bound drivers want hub-initiated LPM
4925*4882a593Smuzhiyun * disabled.
4926*4882a593Smuzhiyun */
4927*4882a593Smuzhiyun if (intf->dev.driver) {
4928*4882a593Smuzhiyun driver = to_usb_driver(intf->dev.driver);
4929*4882a593Smuzhiyun if (driver && driver->disable_hub_initiated_lpm) {
4930*4882a593Smuzhiyun dev_dbg(&udev->dev, "Hub-initiated %s disabled at request of driver %s\n",
4931*4882a593Smuzhiyun state_name, driver->name);
4932*4882a593Smuzhiyun timeout = xhci_get_timeout_no_hub_lpm(udev,
4933*4882a593Smuzhiyun state);
4934*4882a593Smuzhiyun if (timeout == USB3_LPM_DISABLED)
4935*4882a593Smuzhiyun return timeout;
4936*4882a593Smuzhiyun }
4937*4882a593Smuzhiyun }
4938*4882a593Smuzhiyun
4939*4882a593Smuzhiyun /* Not sure how this could happen... */
4940*4882a593Smuzhiyun if (!intf->cur_altsetting)
4941*4882a593Smuzhiyun continue;
4942*4882a593Smuzhiyun
4943*4882a593Smuzhiyun if (xhci_update_timeout_for_interface(xhci, udev,
4944*4882a593Smuzhiyun intf->cur_altsetting,
4945*4882a593Smuzhiyun state, &timeout))
4946*4882a593Smuzhiyun return timeout;
4947*4882a593Smuzhiyun }
4948*4882a593Smuzhiyun return timeout;
4949*4882a593Smuzhiyun }
4950*4882a593Smuzhiyun
calculate_max_exit_latency(struct usb_device * udev,enum usb3_link_state state_changed,u16 hub_encoded_timeout)4951*4882a593Smuzhiyun static int calculate_max_exit_latency(struct usb_device *udev,
4952*4882a593Smuzhiyun enum usb3_link_state state_changed,
4953*4882a593Smuzhiyun u16 hub_encoded_timeout)
4954*4882a593Smuzhiyun {
4955*4882a593Smuzhiyun unsigned long long u1_mel_us = 0;
4956*4882a593Smuzhiyun unsigned long long u2_mel_us = 0;
4957*4882a593Smuzhiyun unsigned long long mel_us = 0;
4958*4882a593Smuzhiyun bool disabling_u1;
4959*4882a593Smuzhiyun bool disabling_u2;
4960*4882a593Smuzhiyun bool enabling_u1;
4961*4882a593Smuzhiyun bool enabling_u2;
4962*4882a593Smuzhiyun
4963*4882a593Smuzhiyun disabling_u1 = (state_changed == USB3_LPM_U1 &&
4964*4882a593Smuzhiyun hub_encoded_timeout == USB3_LPM_DISABLED);
4965*4882a593Smuzhiyun disabling_u2 = (state_changed == USB3_LPM_U2 &&
4966*4882a593Smuzhiyun hub_encoded_timeout == USB3_LPM_DISABLED);
4967*4882a593Smuzhiyun
4968*4882a593Smuzhiyun enabling_u1 = (state_changed == USB3_LPM_U1 &&
4969*4882a593Smuzhiyun hub_encoded_timeout != USB3_LPM_DISABLED);
4970*4882a593Smuzhiyun enabling_u2 = (state_changed == USB3_LPM_U2 &&
4971*4882a593Smuzhiyun hub_encoded_timeout != USB3_LPM_DISABLED);
4972*4882a593Smuzhiyun
4973*4882a593Smuzhiyun /* If U1 was already enabled and we're not disabling it,
4974*4882a593Smuzhiyun * or we're going to enable U1, account for the U1 max exit latency.
4975*4882a593Smuzhiyun */
4976*4882a593Smuzhiyun if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
4977*4882a593Smuzhiyun enabling_u1)
4978*4882a593Smuzhiyun u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
4979*4882a593Smuzhiyun if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
4980*4882a593Smuzhiyun enabling_u2)
4981*4882a593Smuzhiyun u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
4982*4882a593Smuzhiyun
4983*4882a593Smuzhiyun if (u1_mel_us > u2_mel_us)
4984*4882a593Smuzhiyun mel_us = u1_mel_us;
4985*4882a593Smuzhiyun else
4986*4882a593Smuzhiyun mel_us = u2_mel_us;
4987*4882a593Smuzhiyun /* xHCI host controller max exit latency field is only 16 bits wide. */
4988*4882a593Smuzhiyun if (mel_us > MAX_EXIT) {
4989*4882a593Smuzhiyun dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
4990*4882a593Smuzhiyun "is too big.\n", mel_us);
4991*4882a593Smuzhiyun return -E2BIG;
4992*4882a593Smuzhiyun }
4993*4882a593Smuzhiyun return mel_us;
4994*4882a593Smuzhiyun }
4995*4882a593Smuzhiyun
4996*4882a593Smuzhiyun /* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
xhci_enable_usb3_lpm_timeout(struct usb_hcd * hcd,struct usb_device * udev,enum usb3_link_state state)4997*4882a593Smuzhiyun static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4998*4882a593Smuzhiyun struct usb_device *udev, enum usb3_link_state state)
4999*4882a593Smuzhiyun {
5000*4882a593Smuzhiyun struct xhci_hcd *xhci;
5001*4882a593Smuzhiyun u16 hub_encoded_timeout;
5002*4882a593Smuzhiyun int mel;
5003*4882a593Smuzhiyun int ret;
5004*4882a593Smuzhiyun
5005*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
5006*4882a593Smuzhiyun /* The LPM timeout values are pretty host-controller specific, so don't
5007*4882a593Smuzhiyun * enable hub-initiated timeouts unless the vendor has provided
5008*4882a593Smuzhiyun * information about their timeout algorithm.
5009*4882a593Smuzhiyun */
5010*4882a593Smuzhiyun if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
5011*4882a593Smuzhiyun !xhci->devs[udev->slot_id])
5012*4882a593Smuzhiyun return USB3_LPM_DISABLED;
5013*4882a593Smuzhiyun
5014*4882a593Smuzhiyun hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
5015*4882a593Smuzhiyun mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
5016*4882a593Smuzhiyun if (mel < 0) {
5017*4882a593Smuzhiyun /* Max Exit Latency is too big, disable LPM. */
5018*4882a593Smuzhiyun hub_encoded_timeout = USB3_LPM_DISABLED;
5019*4882a593Smuzhiyun mel = 0;
5020*4882a593Smuzhiyun }
5021*4882a593Smuzhiyun
5022*4882a593Smuzhiyun ret = xhci_change_max_exit_latency(xhci, udev, mel);
5023*4882a593Smuzhiyun if (ret)
5024*4882a593Smuzhiyun return ret;
5025*4882a593Smuzhiyun return hub_encoded_timeout;
5026*4882a593Smuzhiyun }
5027*4882a593Smuzhiyun
xhci_disable_usb3_lpm_timeout(struct usb_hcd * hcd,struct usb_device * udev,enum usb3_link_state state)5028*4882a593Smuzhiyun static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
5029*4882a593Smuzhiyun struct usb_device *udev, enum usb3_link_state state)
5030*4882a593Smuzhiyun {
5031*4882a593Smuzhiyun struct xhci_hcd *xhci;
5032*4882a593Smuzhiyun u16 mel;
5033*4882a593Smuzhiyun
5034*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
5035*4882a593Smuzhiyun if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
5036*4882a593Smuzhiyun !xhci->devs[udev->slot_id])
5037*4882a593Smuzhiyun return 0;
5038*4882a593Smuzhiyun
5039*4882a593Smuzhiyun mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
5040*4882a593Smuzhiyun return xhci_change_max_exit_latency(xhci, udev, mel);
5041*4882a593Smuzhiyun }
5042*4882a593Smuzhiyun #else /* CONFIG_PM */
5043*4882a593Smuzhiyun
xhci_set_usb2_hardware_lpm(struct usb_hcd * hcd,struct usb_device * udev,int enable)5044*4882a593Smuzhiyun static int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
5045*4882a593Smuzhiyun struct usb_device *udev, int enable)
5046*4882a593Smuzhiyun {
5047*4882a593Smuzhiyun return 0;
5048*4882a593Smuzhiyun }
5049*4882a593Smuzhiyun
xhci_update_device(struct usb_hcd * hcd,struct usb_device * udev)5050*4882a593Smuzhiyun static int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
5051*4882a593Smuzhiyun {
5052*4882a593Smuzhiyun return 0;
5053*4882a593Smuzhiyun }
5054*4882a593Smuzhiyun
xhci_enable_usb3_lpm_timeout(struct usb_hcd * hcd,struct usb_device * udev,enum usb3_link_state state)5055*4882a593Smuzhiyun static int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
5056*4882a593Smuzhiyun struct usb_device *udev, enum usb3_link_state state)
5057*4882a593Smuzhiyun {
5058*4882a593Smuzhiyun return USB3_LPM_DISABLED;
5059*4882a593Smuzhiyun }
5060*4882a593Smuzhiyun
xhci_disable_usb3_lpm_timeout(struct usb_hcd * hcd,struct usb_device * udev,enum usb3_link_state state)5061*4882a593Smuzhiyun static int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
5062*4882a593Smuzhiyun struct usb_device *udev, enum usb3_link_state state)
5063*4882a593Smuzhiyun {
5064*4882a593Smuzhiyun return 0;
5065*4882a593Smuzhiyun }
5066*4882a593Smuzhiyun #endif /* CONFIG_PM */
5067*4882a593Smuzhiyun
5068*4882a593Smuzhiyun /*-------------------------------------------------------------------------*/
5069*4882a593Smuzhiyun
5070*4882a593Smuzhiyun /* Once a hub descriptor is fetched for a device, we need to update the xHC's
5071*4882a593Smuzhiyun * internal data structures for the device.
5072*4882a593Smuzhiyun */
xhci_update_hub_device(struct usb_hcd * hcd,struct usb_device * hdev,struct usb_tt * tt,gfp_t mem_flags)5073*4882a593Smuzhiyun static int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
5074*4882a593Smuzhiyun struct usb_tt *tt, gfp_t mem_flags)
5075*4882a593Smuzhiyun {
5076*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
5077*4882a593Smuzhiyun struct xhci_virt_device *vdev;
5078*4882a593Smuzhiyun struct xhci_command *config_cmd;
5079*4882a593Smuzhiyun struct xhci_input_control_ctx *ctrl_ctx;
5080*4882a593Smuzhiyun struct xhci_slot_ctx *slot_ctx;
5081*4882a593Smuzhiyun unsigned long flags;
5082*4882a593Smuzhiyun unsigned think_time;
5083*4882a593Smuzhiyun int ret;
5084*4882a593Smuzhiyun
5085*4882a593Smuzhiyun /* Ignore root hubs */
5086*4882a593Smuzhiyun if (!hdev->parent)
5087*4882a593Smuzhiyun return 0;
5088*4882a593Smuzhiyun
5089*4882a593Smuzhiyun vdev = xhci->devs[hdev->slot_id];
5090*4882a593Smuzhiyun if (!vdev) {
5091*4882a593Smuzhiyun xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
5092*4882a593Smuzhiyun return -EINVAL;
5093*4882a593Smuzhiyun }
5094*4882a593Smuzhiyun
5095*4882a593Smuzhiyun config_cmd = xhci_alloc_command_with_ctx(xhci, true, mem_flags);
5096*4882a593Smuzhiyun if (!config_cmd)
5097*4882a593Smuzhiyun return -ENOMEM;
5098*4882a593Smuzhiyun
5099*4882a593Smuzhiyun ctrl_ctx = xhci_get_input_control_ctx(config_cmd->in_ctx);
5100*4882a593Smuzhiyun if (!ctrl_ctx) {
5101*4882a593Smuzhiyun xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
5102*4882a593Smuzhiyun __func__);
5103*4882a593Smuzhiyun xhci_free_command(xhci, config_cmd);
5104*4882a593Smuzhiyun return -ENOMEM;
5105*4882a593Smuzhiyun }
5106*4882a593Smuzhiyun
5107*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
5108*4882a593Smuzhiyun if (hdev->speed == USB_SPEED_HIGH &&
5109*4882a593Smuzhiyun xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
5110*4882a593Smuzhiyun xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
5111*4882a593Smuzhiyun xhci_free_command(xhci, config_cmd);
5112*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
5113*4882a593Smuzhiyun return -ENOMEM;
5114*4882a593Smuzhiyun }
5115*4882a593Smuzhiyun
5116*4882a593Smuzhiyun ret = xhci_vendor_sync_dev_ctx(xhci, hdev->slot_id);
5117*4882a593Smuzhiyun if (ret) {
5118*4882a593Smuzhiyun xhci_warn(xhci, "%s: Failed to sync device context failed, err=%d",
5119*4882a593Smuzhiyun __func__, ret);
5120*4882a593Smuzhiyun xhci_free_command(xhci, config_cmd);
5121*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
5122*4882a593Smuzhiyun return ret;
5123*4882a593Smuzhiyun }
5124*4882a593Smuzhiyun
5125*4882a593Smuzhiyun xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
5126*4882a593Smuzhiyun ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
5127*4882a593Smuzhiyun slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
5128*4882a593Smuzhiyun slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
5129*4882a593Smuzhiyun /*
5130*4882a593Smuzhiyun * refer to section 6.2.2: MTT should be 0 for full speed hub,
5131*4882a593Smuzhiyun * but it may be already set to 1 when setup an xHCI virtual
5132*4882a593Smuzhiyun * device, so clear it anyway.
5133*4882a593Smuzhiyun */
5134*4882a593Smuzhiyun if (tt->multi)
5135*4882a593Smuzhiyun slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
5136*4882a593Smuzhiyun else if (hdev->speed == USB_SPEED_FULL)
5137*4882a593Smuzhiyun slot_ctx->dev_info &= cpu_to_le32(~DEV_MTT);
5138*4882a593Smuzhiyun
5139*4882a593Smuzhiyun if (xhci->hci_version > 0x95) {
5140*4882a593Smuzhiyun xhci_dbg(xhci, "xHCI version %x needs hub "
5141*4882a593Smuzhiyun "TT think time and number of ports\n",
5142*4882a593Smuzhiyun (unsigned int) xhci->hci_version);
5143*4882a593Smuzhiyun slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
5144*4882a593Smuzhiyun /* Set TT think time - convert from ns to FS bit times.
5145*4882a593Smuzhiyun * 0 = 8 FS bit times, 1 = 16 FS bit times,
5146*4882a593Smuzhiyun * 2 = 24 FS bit times, 3 = 32 FS bit times.
5147*4882a593Smuzhiyun *
5148*4882a593Smuzhiyun * xHCI 1.0: this field shall be 0 if the device is not a
5149*4882a593Smuzhiyun * High-spped hub.
5150*4882a593Smuzhiyun */
5151*4882a593Smuzhiyun think_time = tt->think_time;
5152*4882a593Smuzhiyun if (think_time != 0)
5153*4882a593Smuzhiyun think_time = (think_time / 666) - 1;
5154*4882a593Smuzhiyun if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
5155*4882a593Smuzhiyun slot_ctx->tt_info |=
5156*4882a593Smuzhiyun cpu_to_le32(TT_THINK_TIME(think_time));
5157*4882a593Smuzhiyun } else {
5158*4882a593Smuzhiyun xhci_dbg(xhci, "xHCI version %x doesn't need hub "
5159*4882a593Smuzhiyun "TT think time or number of ports\n",
5160*4882a593Smuzhiyun (unsigned int) xhci->hci_version);
5161*4882a593Smuzhiyun }
5162*4882a593Smuzhiyun slot_ctx->dev_state = 0;
5163*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
5164*4882a593Smuzhiyun
5165*4882a593Smuzhiyun xhci_dbg(xhci, "Set up %s for hub device.\n",
5166*4882a593Smuzhiyun (xhci->hci_version > 0x95) ?
5167*4882a593Smuzhiyun "configure endpoint" : "evaluate context");
5168*4882a593Smuzhiyun
5169*4882a593Smuzhiyun /* Issue and wait for the configure endpoint or
5170*4882a593Smuzhiyun * evaluate context command.
5171*4882a593Smuzhiyun */
5172*4882a593Smuzhiyun if (xhci->hci_version > 0x95)
5173*4882a593Smuzhiyun ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
5174*4882a593Smuzhiyun false, false);
5175*4882a593Smuzhiyun else
5176*4882a593Smuzhiyun ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
5177*4882a593Smuzhiyun true, false);
5178*4882a593Smuzhiyun
5179*4882a593Smuzhiyun xhci_free_command(xhci, config_cmd);
5180*4882a593Smuzhiyun return ret;
5181*4882a593Smuzhiyun }
5182*4882a593Smuzhiyun
xhci_get_frame(struct usb_hcd * hcd)5183*4882a593Smuzhiyun static int xhci_get_frame(struct usb_hcd *hcd)
5184*4882a593Smuzhiyun {
5185*4882a593Smuzhiyun struct xhci_hcd *xhci = hcd_to_xhci(hcd);
5186*4882a593Smuzhiyun /* EHCI mods by the periodic size. Why? */
5187*4882a593Smuzhiyun return readl(&xhci->run_regs->microframe_index) >> 3;
5188*4882a593Smuzhiyun }
5189*4882a593Smuzhiyun
xhci_gen_setup(struct usb_hcd * hcd,xhci_get_quirks_t get_quirks)5190*4882a593Smuzhiyun int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
5191*4882a593Smuzhiyun {
5192*4882a593Smuzhiyun struct xhci_hcd *xhci;
5193*4882a593Smuzhiyun /*
5194*4882a593Smuzhiyun * TODO: Check with DWC3 clients for sysdev according to
5195*4882a593Smuzhiyun * quirks
5196*4882a593Smuzhiyun */
5197*4882a593Smuzhiyun struct device *dev = hcd->self.sysdev;
5198*4882a593Smuzhiyun unsigned int minor_rev;
5199*4882a593Smuzhiyun int retval;
5200*4882a593Smuzhiyun
5201*4882a593Smuzhiyun /* Accept arbitrarily long scatter-gather lists */
5202*4882a593Smuzhiyun hcd->self.sg_tablesize = ~0;
5203*4882a593Smuzhiyun
5204*4882a593Smuzhiyun /* support to build packet from discontinuous buffers */
5205*4882a593Smuzhiyun hcd->self.no_sg_constraint = 1;
5206*4882a593Smuzhiyun
5207*4882a593Smuzhiyun /* XHCI controllers don't stop the ep queue on short packets :| */
5208*4882a593Smuzhiyun hcd->self.no_stop_on_short = 1;
5209*4882a593Smuzhiyun
5210*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
5211*4882a593Smuzhiyun
5212*4882a593Smuzhiyun if (usb_hcd_is_primary_hcd(hcd)) {
5213*4882a593Smuzhiyun xhci->main_hcd = hcd;
5214*4882a593Smuzhiyun xhci->usb2_rhub.hcd = hcd;
5215*4882a593Smuzhiyun /* Mark the first roothub as being USB 2.0.
5216*4882a593Smuzhiyun * The xHCI driver will register the USB 3.0 roothub.
5217*4882a593Smuzhiyun */
5218*4882a593Smuzhiyun hcd->speed = HCD_USB2;
5219*4882a593Smuzhiyun hcd->self.root_hub->speed = USB_SPEED_HIGH;
5220*4882a593Smuzhiyun /*
5221*4882a593Smuzhiyun * USB 2.0 roothub under xHCI has an integrated TT,
5222*4882a593Smuzhiyun * (rate matching hub) as opposed to having an OHCI/UHCI
5223*4882a593Smuzhiyun * companion controller.
5224*4882a593Smuzhiyun */
5225*4882a593Smuzhiyun hcd->has_tt = 1;
5226*4882a593Smuzhiyun } else {
5227*4882a593Smuzhiyun /*
5228*4882a593Smuzhiyun * Early xHCI 1.1 spec did not mention USB 3.1 capable hosts
5229*4882a593Smuzhiyun * should return 0x31 for sbrn, or that the minor revision
5230*4882a593Smuzhiyun * is a two digit BCD containig minor and sub-minor numbers.
5231*4882a593Smuzhiyun * This was later clarified in xHCI 1.2.
5232*4882a593Smuzhiyun *
5233*4882a593Smuzhiyun * Some USB 3.1 capable hosts therefore have sbrn 0x30, and
5234*4882a593Smuzhiyun * minor revision set to 0x1 instead of 0x10.
5235*4882a593Smuzhiyun */
5236*4882a593Smuzhiyun if (xhci->usb3_rhub.min_rev == 0x1)
5237*4882a593Smuzhiyun minor_rev = 1;
5238*4882a593Smuzhiyun else
5239*4882a593Smuzhiyun minor_rev = xhci->usb3_rhub.min_rev / 0x10;
5240*4882a593Smuzhiyun
5241*4882a593Smuzhiyun switch (minor_rev) {
5242*4882a593Smuzhiyun case 2:
5243*4882a593Smuzhiyun hcd->speed = HCD_USB32;
5244*4882a593Smuzhiyun hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS;
5245*4882a593Smuzhiyun hcd->self.root_hub->rx_lanes = 2;
5246*4882a593Smuzhiyun hcd->self.root_hub->tx_lanes = 2;
5247*4882a593Smuzhiyun break;
5248*4882a593Smuzhiyun case 1:
5249*4882a593Smuzhiyun hcd->speed = HCD_USB31;
5250*4882a593Smuzhiyun hcd->self.root_hub->speed = USB_SPEED_SUPER_PLUS;
5251*4882a593Smuzhiyun break;
5252*4882a593Smuzhiyun }
5253*4882a593Smuzhiyun xhci_info(xhci, "Host supports USB 3.%x %sSuperSpeed\n",
5254*4882a593Smuzhiyun minor_rev,
5255*4882a593Smuzhiyun minor_rev ? "Enhanced " : "");
5256*4882a593Smuzhiyun
5257*4882a593Smuzhiyun xhci->usb3_rhub.hcd = hcd;
5258*4882a593Smuzhiyun /* xHCI private pointer was set in xhci_pci_probe for the second
5259*4882a593Smuzhiyun * registered roothub.
5260*4882a593Smuzhiyun */
5261*4882a593Smuzhiyun return 0;
5262*4882a593Smuzhiyun }
5263*4882a593Smuzhiyun
5264*4882a593Smuzhiyun mutex_init(&xhci->mutex);
5265*4882a593Smuzhiyun xhci->cap_regs = hcd->regs;
5266*4882a593Smuzhiyun xhci->op_regs = hcd->regs +
5267*4882a593Smuzhiyun HC_LENGTH(readl(&xhci->cap_regs->hc_capbase));
5268*4882a593Smuzhiyun xhci->run_regs = hcd->regs +
5269*4882a593Smuzhiyun (readl(&xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
5270*4882a593Smuzhiyun /* Cache read-only capability registers */
5271*4882a593Smuzhiyun xhci->hcs_params1 = readl(&xhci->cap_regs->hcs_params1);
5272*4882a593Smuzhiyun xhci->hcs_params2 = readl(&xhci->cap_regs->hcs_params2);
5273*4882a593Smuzhiyun xhci->hcs_params3 = readl(&xhci->cap_regs->hcs_params3);
5274*4882a593Smuzhiyun xhci->hcc_params = readl(&xhci->cap_regs->hc_capbase);
5275*4882a593Smuzhiyun xhci->hci_version = HC_VERSION(xhci->hcc_params);
5276*4882a593Smuzhiyun xhci->hcc_params = readl(&xhci->cap_regs->hcc_params);
5277*4882a593Smuzhiyun if (xhci->hci_version > 0x100)
5278*4882a593Smuzhiyun xhci->hcc_params2 = readl(&xhci->cap_regs->hcc_params2);
5279*4882a593Smuzhiyun
5280*4882a593Smuzhiyun xhci->quirks |= quirks;
5281*4882a593Smuzhiyun
5282*4882a593Smuzhiyun get_quirks(dev, xhci);
5283*4882a593Smuzhiyun
5284*4882a593Smuzhiyun /* In xhci controllers which follow xhci 1.0 spec gives a spurious
5285*4882a593Smuzhiyun * success event after a short transfer. This quirk will ignore such
5286*4882a593Smuzhiyun * spurious event.
5287*4882a593Smuzhiyun */
5288*4882a593Smuzhiyun if (xhci->hci_version > 0x96)
5289*4882a593Smuzhiyun xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
5290*4882a593Smuzhiyun
5291*4882a593Smuzhiyun /* Make sure the HC is halted. */
5292*4882a593Smuzhiyun retval = xhci_halt(xhci);
5293*4882a593Smuzhiyun if (retval)
5294*4882a593Smuzhiyun return retval;
5295*4882a593Smuzhiyun
5296*4882a593Smuzhiyun xhci_zero_64b_regs(xhci);
5297*4882a593Smuzhiyun
5298*4882a593Smuzhiyun xhci_dbg(xhci, "Resetting HCD\n");
5299*4882a593Smuzhiyun /* Reset the internal HC memory state and registers. */
5300*4882a593Smuzhiyun retval = xhci_reset(xhci, XHCI_RESET_LONG_USEC);
5301*4882a593Smuzhiyun if (retval)
5302*4882a593Smuzhiyun return retval;
5303*4882a593Smuzhiyun xhci_dbg(xhci, "Reset complete\n");
5304*4882a593Smuzhiyun
5305*4882a593Smuzhiyun /*
5306*4882a593Smuzhiyun * On some xHCI controllers (e.g. R-Car SoCs), the AC64 bit (bit 0)
5307*4882a593Smuzhiyun * of HCCPARAMS1 is set to 1. However, the xHCs don't support 64-bit
5308*4882a593Smuzhiyun * address memory pointers actually. So, this driver clears the AC64
5309*4882a593Smuzhiyun * bit of xhci->hcc_params to call dma_set_coherent_mask(dev,
5310*4882a593Smuzhiyun * DMA_BIT_MASK(32)) in this xhci_gen_setup().
5311*4882a593Smuzhiyun */
5312*4882a593Smuzhiyun if (xhci->quirks & XHCI_NO_64BIT_SUPPORT)
5313*4882a593Smuzhiyun xhci->hcc_params &= ~BIT(0);
5314*4882a593Smuzhiyun
5315*4882a593Smuzhiyun /* Set dma_mask and coherent_dma_mask to 64-bits,
5316*4882a593Smuzhiyun * if xHC supports 64-bit addressing */
5317*4882a593Smuzhiyun if (HCC_64BIT_ADDR(xhci->hcc_params) &&
5318*4882a593Smuzhiyun !dma_set_mask(dev, DMA_BIT_MASK(64))) {
5319*4882a593Smuzhiyun xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
5320*4882a593Smuzhiyun dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
5321*4882a593Smuzhiyun } else {
5322*4882a593Smuzhiyun /*
5323*4882a593Smuzhiyun * This is to avoid error in cases where a 32-bit USB
5324*4882a593Smuzhiyun * controller is used on a 64-bit capable system.
5325*4882a593Smuzhiyun */
5326*4882a593Smuzhiyun retval = dma_set_mask(dev, DMA_BIT_MASK(32));
5327*4882a593Smuzhiyun if (retval)
5328*4882a593Smuzhiyun return retval;
5329*4882a593Smuzhiyun xhci_dbg(xhci, "Enabling 32-bit DMA addresses.\n");
5330*4882a593Smuzhiyun dma_set_coherent_mask(dev, DMA_BIT_MASK(32));
5331*4882a593Smuzhiyun }
5332*4882a593Smuzhiyun
5333*4882a593Smuzhiyun xhci_dbg(xhci, "Calling HCD init\n");
5334*4882a593Smuzhiyun /* Initialize HCD and host controller data structures. */
5335*4882a593Smuzhiyun retval = xhci_init(hcd);
5336*4882a593Smuzhiyun if (retval)
5337*4882a593Smuzhiyun return retval;
5338*4882a593Smuzhiyun xhci_dbg(xhci, "Called HCD init\n");
5339*4882a593Smuzhiyun
5340*4882a593Smuzhiyun xhci_info(xhci, "hcc params 0x%08x hci version 0x%x quirks 0x%016llx\n",
5341*4882a593Smuzhiyun xhci->hcc_params, xhci->hci_version, xhci->quirks);
5342*4882a593Smuzhiyun
5343*4882a593Smuzhiyun return 0;
5344*4882a593Smuzhiyun }
5345*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_gen_setup);
5346*4882a593Smuzhiyun
xhci_clear_tt_buffer_complete(struct usb_hcd * hcd,struct usb_host_endpoint * ep)5347*4882a593Smuzhiyun static void xhci_clear_tt_buffer_complete(struct usb_hcd *hcd,
5348*4882a593Smuzhiyun struct usb_host_endpoint *ep)
5349*4882a593Smuzhiyun {
5350*4882a593Smuzhiyun struct xhci_hcd *xhci;
5351*4882a593Smuzhiyun struct usb_device *udev;
5352*4882a593Smuzhiyun unsigned int slot_id;
5353*4882a593Smuzhiyun unsigned int ep_index;
5354*4882a593Smuzhiyun unsigned long flags;
5355*4882a593Smuzhiyun
5356*4882a593Smuzhiyun xhci = hcd_to_xhci(hcd);
5357*4882a593Smuzhiyun
5358*4882a593Smuzhiyun spin_lock_irqsave(&xhci->lock, flags);
5359*4882a593Smuzhiyun udev = (struct usb_device *)ep->hcpriv;
5360*4882a593Smuzhiyun slot_id = udev->slot_id;
5361*4882a593Smuzhiyun ep_index = xhci_get_endpoint_index(&ep->desc);
5362*4882a593Smuzhiyun
5363*4882a593Smuzhiyun xhci->devs[slot_id]->eps[ep_index].ep_state &= ~EP_CLEARING_TT;
5364*4882a593Smuzhiyun xhci_ring_doorbell_for_active_rings(xhci, slot_id, ep_index);
5365*4882a593Smuzhiyun spin_unlock_irqrestore(&xhci->lock, flags);
5366*4882a593Smuzhiyun }
5367*4882a593Smuzhiyun
5368*4882a593Smuzhiyun static const struct hc_driver xhci_hc_driver = {
5369*4882a593Smuzhiyun .description = "xhci-hcd",
5370*4882a593Smuzhiyun .product_desc = "xHCI Host Controller",
5371*4882a593Smuzhiyun .hcd_priv_size = sizeof(struct xhci_hcd),
5372*4882a593Smuzhiyun
5373*4882a593Smuzhiyun /*
5374*4882a593Smuzhiyun * generic hardware linkage
5375*4882a593Smuzhiyun */
5376*4882a593Smuzhiyun .irq = xhci_irq,
5377*4882a593Smuzhiyun .flags = HCD_MEMORY | HCD_DMA | HCD_USB3 | HCD_SHARED |
5378*4882a593Smuzhiyun HCD_BH,
5379*4882a593Smuzhiyun
5380*4882a593Smuzhiyun /*
5381*4882a593Smuzhiyun * basic lifecycle operations
5382*4882a593Smuzhiyun */
5383*4882a593Smuzhiyun .reset = NULL, /* set in xhci_init_driver() */
5384*4882a593Smuzhiyun .start = xhci_run,
5385*4882a593Smuzhiyun .stop = xhci_stop,
5386*4882a593Smuzhiyun .shutdown = xhci_shutdown,
5387*4882a593Smuzhiyun
5388*4882a593Smuzhiyun /*
5389*4882a593Smuzhiyun * managing i/o requests and associated device resources
5390*4882a593Smuzhiyun */
5391*4882a593Smuzhiyun .map_urb_for_dma = xhci_map_urb_for_dma,
5392*4882a593Smuzhiyun .urb_enqueue = xhci_urb_enqueue,
5393*4882a593Smuzhiyun .urb_dequeue = xhci_urb_dequeue,
5394*4882a593Smuzhiyun .alloc_dev = xhci_alloc_dev,
5395*4882a593Smuzhiyun .free_dev = xhci_free_dev,
5396*4882a593Smuzhiyun .alloc_streams = xhci_alloc_streams,
5397*4882a593Smuzhiyun .free_streams = xhci_free_streams,
5398*4882a593Smuzhiyun .add_endpoint = xhci_add_endpoint,
5399*4882a593Smuzhiyun .drop_endpoint = xhci_drop_endpoint,
5400*4882a593Smuzhiyun .endpoint_disable = xhci_endpoint_disable,
5401*4882a593Smuzhiyun .endpoint_reset = xhci_endpoint_reset,
5402*4882a593Smuzhiyun .check_bandwidth = xhci_check_bandwidth,
5403*4882a593Smuzhiyun .reset_bandwidth = xhci_reset_bandwidth,
5404*4882a593Smuzhiyun .address_device = xhci_address_device,
5405*4882a593Smuzhiyun .enable_device = xhci_enable_device,
5406*4882a593Smuzhiyun .update_hub_device = xhci_update_hub_device,
5407*4882a593Smuzhiyun .reset_device = xhci_discover_or_reset_device,
5408*4882a593Smuzhiyun
5409*4882a593Smuzhiyun /*
5410*4882a593Smuzhiyun * scheduling support
5411*4882a593Smuzhiyun */
5412*4882a593Smuzhiyun .get_frame_number = xhci_get_frame,
5413*4882a593Smuzhiyun
5414*4882a593Smuzhiyun /*
5415*4882a593Smuzhiyun * root hub support
5416*4882a593Smuzhiyun */
5417*4882a593Smuzhiyun .hub_control = xhci_hub_control,
5418*4882a593Smuzhiyun .hub_status_data = xhci_hub_status_data,
5419*4882a593Smuzhiyun .bus_suspend = xhci_bus_suspend,
5420*4882a593Smuzhiyun .bus_resume = xhci_bus_resume,
5421*4882a593Smuzhiyun .get_resuming_ports = xhci_get_resuming_ports,
5422*4882a593Smuzhiyun
5423*4882a593Smuzhiyun /*
5424*4882a593Smuzhiyun * call back when device connected and addressed
5425*4882a593Smuzhiyun */
5426*4882a593Smuzhiyun .update_device = xhci_update_device,
5427*4882a593Smuzhiyun .set_usb2_hw_lpm = xhci_set_usb2_hardware_lpm,
5428*4882a593Smuzhiyun .enable_usb3_lpm_timeout = xhci_enable_usb3_lpm_timeout,
5429*4882a593Smuzhiyun .disable_usb3_lpm_timeout = xhci_disable_usb3_lpm_timeout,
5430*4882a593Smuzhiyun .find_raw_port_number = xhci_find_raw_port_number,
5431*4882a593Smuzhiyun .clear_tt_buffer_complete = xhci_clear_tt_buffer_complete,
5432*4882a593Smuzhiyun };
5433*4882a593Smuzhiyun
xhci_init_driver(struct hc_driver * drv,const struct xhci_driver_overrides * over)5434*4882a593Smuzhiyun void xhci_init_driver(struct hc_driver *drv,
5435*4882a593Smuzhiyun const struct xhci_driver_overrides *over)
5436*4882a593Smuzhiyun {
5437*4882a593Smuzhiyun BUG_ON(!over);
5438*4882a593Smuzhiyun
5439*4882a593Smuzhiyun /* Copy the generic table to drv then apply the overrides */
5440*4882a593Smuzhiyun *drv = xhci_hc_driver;
5441*4882a593Smuzhiyun
5442*4882a593Smuzhiyun if (over) {
5443*4882a593Smuzhiyun drv->hcd_priv_size += over->extra_priv_size;
5444*4882a593Smuzhiyun if (over->reset)
5445*4882a593Smuzhiyun drv->reset = over->reset;
5446*4882a593Smuzhiyun if (over->start)
5447*4882a593Smuzhiyun drv->start = over->start;
5448*4882a593Smuzhiyun if (over->add_endpoint)
5449*4882a593Smuzhiyun drv->add_endpoint = over->add_endpoint;
5450*4882a593Smuzhiyun if (over->drop_endpoint)
5451*4882a593Smuzhiyun drv->drop_endpoint = over->drop_endpoint;
5452*4882a593Smuzhiyun if (over->check_bandwidth)
5453*4882a593Smuzhiyun drv->check_bandwidth = over->check_bandwidth;
5454*4882a593Smuzhiyun if (over->reset_bandwidth)
5455*4882a593Smuzhiyun drv->reset_bandwidth = over->reset_bandwidth;
5456*4882a593Smuzhiyun if (over->address_device)
5457*4882a593Smuzhiyun drv->address_device = over->address_device;
5458*4882a593Smuzhiyun if (over->bus_suspend)
5459*4882a593Smuzhiyun drv->bus_suspend = over->bus_suspend;
5460*4882a593Smuzhiyun if (over->bus_resume)
5461*4882a593Smuzhiyun drv->bus_resume = over->bus_resume;
5462*4882a593Smuzhiyun }
5463*4882a593Smuzhiyun }
5464*4882a593Smuzhiyun EXPORT_SYMBOL_GPL(xhci_init_driver);
5465*4882a593Smuzhiyun
5466*4882a593Smuzhiyun MODULE_DESCRIPTION(DRIVER_DESC);
5467*4882a593Smuzhiyun MODULE_AUTHOR(DRIVER_AUTHOR);
5468*4882a593Smuzhiyun MODULE_LICENSE("GPL");
5469*4882a593Smuzhiyun
xhci_hcd_init(void)5470*4882a593Smuzhiyun static int __init xhci_hcd_init(void)
5471*4882a593Smuzhiyun {
5472*4882a593Smuzhiyun /*
5473*4882a593Smuzhiyun * Check the compiler generated sizes of structures that must be laid
5474*4882a593Smuzhiyun * out in specific ways for hardware access.
5475*4882a593Smuzhiyun */
5476*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
5477*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
5478*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
5479*4882a593Smuzhiyun /* xhci_device_control has eight fields, and also
5480*4882a593Smuzhiyun * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
5481*4882a593Smuzhiyun */
5482*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
5483*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
5484*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
5485*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 8*32/8);
5486*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
5487*4882a593Smuzhiyun /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
5488*4882a593Smuzhiyun BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
5489*4882a593Smuzhiyun
5490*4882a593Smuzhiyun if (usb_disabled())
5491*4882a593Smuzhiyun return -ENODEV;
5492*4882a593Smuzhiyun
5493*4882a593Smuzhiyun xhci_debugfs_create_root();
5494*4882a593Smuzhiyun
5495*4882a593Smuzhiyun return 0;
5496*4882a593Smuzhiyun }
5497*4882a593Smuzhiyun
5498*4882a593Smuzhiyun /*
5499*4882a593Smuzhiyun * If an init function is provided, an exit function must also be provided
5500*4882a593Smuzhiyun * to allow module unload.
5501*4882a593Smuzhiyun */
xhci_hcd_fini(void)5502*4882a593Smuzhiyun static void __exit xhci_hcd_fini(void)
5503*4882a593Smuzhiyun {
5504*4882a593Smuzhiyun xhci_debugfs_remove_root();
5505*4882a593Smuzhiyun }
5506*4882a593Smuzhiyun
5507*4882a593Smuzhiyun module_init(xhci_hcd_init);
5508*4882a593Smuzhiyun module_exit(xhci_hcd_fini);
5509