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