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