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