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