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