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