blob: d96652d3fb54e716026ac2fa507fe417395f92d4 [file] [log] [blame]
yuezonghe824eb0c2024-06-27 02:32:26 -07001/*
2 * xHCI host controller driver
3 *
4 * Copyright (C) 2008 Intel Corp.
5 *
6 * Author: Sarah Sharp
7 * Some code borrowed from the Linux EHCI driver.
8 *
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 *
13 * This program is distributed in the hope that it will be useful, but
14 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
15 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
16 * for more details.
17 *
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software Foundation,
20 * Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
21 */
22
23#include <linux/pci.h>
24#include <linux/irq.h>
25#include <linux/log2.h>
26#include <linux/module.h>
27#include <linux/moduleparam.h>
28#include <linux/slab.h>
29#include <linux/dmi.h>
30
31#include "xhci.h"
32
33#define DRIVER_AUTHOR "Sarah Sharp"
34#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
35
36#define PORT_WAKE_BITS (PORT_WKOC_E | PORT_WKDISC_E | PORT_WKCONN_E)
37
38/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
39static int link_quirk;
40module_param(link_quirk, int, S_IRUGO | S_IWUSR);
41MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
42
43/* TODO: copied from ehci-hcd.c - can this be refactored? */
44/*
45 * handshake - spin reading hc until handshake completes or fails
46 * @ptr: address of hc register to be read
47 * @mask: bits to look at in result of read
48 * @done: value of those bits when handshake succeeds
49 * @usec: timeout in microseconds
50 *
51 * Returns negative errno, or zero on success
52 *
53 * Success happens when the "mask" bits have the specified value (hardware
54 * handshake done). There are two failure modes: "usec" have passed (major
55 * hardware flakeout), or the register reads as all-ones (hardware removed).
56 */
57int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
58 u32 mask, u32 done, int usec)
59{
60 u32 result;
61
62 do {
63 result = xhci_readl(xhci, ptr);
64 if (result == ~(u32)0) /* card removed */
65 return -ENODEV;
66 result &= mask;
67 if (result == done)
68 return 0;
69 udelay(1);
70 usec--;
71 } while (usec > 0);
72 return -ETIMEDOUT;
73}
74
75/*
76 * Disable interrupts and begin the xHCI halting process.
77 */
78void xhci_quiesce(struct xhci_hcd *xhci)
79{
80 u32 halted;
81 u32 cmd;
82 u32 mask;
83
84 mask = ~(XHCI_IRQS);
85 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
86 if (!halted)
87 mask &= ~CMD_RUN;
88
89 cmd = xhci_readl(xhci, &xhci->op_regs->command);
90 cmd &= mask;
91 xhci_writel(xhci, cmd, &xhci->op_regs->command);
92}
93
94/*
95 * Force HC into halt state.
96 *
97 * Disable any IRQs and clear the run/stop bit.
98 * HC will complete any current and actively pipelined transactions, and
99 * should halt within 16 ms of the run/stop bit being cleared.
100 * Read HC Halted bit in the status register to see when the HC is finished.
101 */
102int xhci_halt(struct xhci_hcd *xhci)
103{
104 int ret;
105 xhci_dbg(xhci, "// Halt the HC\n");
106 xhci_quiesce(xhci);
107
108 ret = handshake(xhci, &xhci->op_regs->status,
109 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
110 if (!ret) {
111 xhci->xhc_state |= XHCI_STATE_HALTED;
112 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
113 } else
114 xhci_warn(xhci, "Host not halted after %u microseconds.\n",
115 XHCI_MAX_HALT_USEC);
116 return ret;
117}
118
119/*
120 * Set the run bit and wait for the host to be running.
121 */
122static int xhci_start(struct xhci_hcd *xhci)
123{
124 u32 temp;
125 int ret;
126
127 temp = xhci_readl(xhci, &xhci->op_regs->command);
128 temp |= (CMD_RUN);
129 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
130 temp);
131 xhci_writel(xhci, temp, &xhci->op_regs->command);
132
133 /*
134 * Wait for the HCHalted Status bit to be 0 to indicate the host is
135 * running.
136 */
137 ret = handshake(xhci, &xhci->op_regs->status,
138 STS_HALT, 0, XHCI_MAX_HALT_USEC);
139 if (ret == -ETIMEDOUT)
140 xhci_err(xhci, "Host took too long to start, "
141 "waited %u microseconds.\n",
142 XHCI_MAX_HALT_USEC);
143 if (!ret)
144 xhci->xhc_state &= ~XHCI_STATE_HALTED;
145 return ret;
146}
147
148/*
149 * Reset a halted HC.
150 *
151 * This resets pipelines, timers, counters, state machines, etc.
152 * Transactions will be terminated immediately, and operational registers
153 * will be set to their defaults.
154 */
155int xhci_reset(struct xhci_hcd *xhci)
156{
157 u32 command;
158 u32 state;
159 int ret, i;
160
161 state = xhci_readl(xhci, &xhci->op_regs->status);
162 if ((state & STS_HALT) == 0) {
163 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
164 return 0;
165 }
166
167 xhci_dbg(xhci, "// Reset the HC\n");
168 command = xhci_readl(xhci, &xhci->op_regs->command);
169 command |= CMD_RESET;
170 xhci_writel(xhci, command, &xhci->op_regs->command);
171
172 ret = handshake(xhci, &xhci->op_regs->command,
173 CMD_RESET, 0, 10 * 1000 * 1000);
174 if (ret)
175 return ret;
176
177 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
178 /*
179 * xHCI cannot write to any doorbells or operational registers other
180 * than status until the "Controller Not Ready" flag is cleared.
181 */
182 ret = handshake(xhci, &xhci->op_regs->status,
183 STS_CNR, 0, 10 * 1000 * 1000);
184
185 for (i = 0; i < 2; ++i) {
186 xhci->bus_state[i].port_c_suspend = 0;
187 xhci->bus_state[i].suspended_ports = 0;
188 xhci->bus_state[i].resuming_ports = 0;
189 }
190
191 return ret;
192}
193
194#ifdef CONFIG_PCI
195static int xhci_free_msi(struct xhci_hcd *xhci)
196{
197 int i;
198
199 if (!xhci->msix_entries)
200 return -EINVAL;
201
202 for (i = 0; i < xhci->msix_count; i++)
203 if (xhci->msix_entries[i].vector)
204 free_irq(xhci->msix_entries[i].vector,
205 xhci_to_hcd(xhci));
206 return 0;
207}
208
209/*
210 * Set up MSI
211 */
212static int xhci_setup_msi(struct xhci_hcd *xhci)
213{
214 int ret;
215 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
216
217 ret = pci_enable_msi(pdev);
218 if (ret) {
219 xhci_dbg(xhci, "failed to allocate MSI entry\n");
220 return ret;
221 }
222
223 ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
224 0, "xhci_hcd", xhci_to_hcd(xhci));
225 if (ret) {
226 xhci_dbg(xhci, "disable MSI interrupt\n");
227 pci_disable_msi(pdev);
228 }
229
230 return ret;
231}
232
233/*
234 * Free IRQs
235 * free all IRQs request
236 */
237static void xhci_free_irq(struct xhci_hcd *xhci)
238{
239 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
240 int ret;
241
242 /* return if using legacy interrupt */
243 if (xhci_to_hcd(xhci)->irq > 0)
244 return;
245
246 ret = xhci_free_msi(xhci);
247 if (!ret)
248 return;
249 if (pdev->irq > 0)
250 free_irq(pdev->irq, xhci_to_hcd(xhci));
251
252 return;
253}
254
255/*
256 * Set up MSI-X
257 */
258static int xhci_setup_msix(struct xhci_hcd *xhci)
259{
260 int i, ret = 0;
261 struct usb_hcd *hcd = xhci_to_hcd(xhci);
262 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
263
264 /*
265 * calculate number of msi-x vectors supported.
266 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
267 * with max number of interrupters based on the xhci HCSPARAMS1.
268 * - num_online_cpus: maximum msi-x vectors per CPUs core.
269 * Add additional 1 vector to ensure always available interrupt.
270 */
271 xhci->msix_count = min(num_online_cpus() + 1,
272 HCS_MAX_INTRS(xhci->hcs_params1));
273
274 xhci->msix_entries =
275 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
276 GFP_KERNEL);
277 if (!xhci->msix_entries) {
278 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
279 return -ENOMEM;
280 }
281
282 for (i = 0; i < xhci->msix_count; i++) {
283 xhci->msix_entries[i].entry = i;
284 xhci->msix_entries[i].vector = 0;
285 }
286
287 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
288 if (ret) {
289 xhci_dbg(xhci, "Failed to enable MSI-X\n");
290 goto free_entries;
291 }
292
293 for (i = 0; i < xhci->msix_count; i++) {
294 ret = request_irq(xhci->msix_entries[i].vector,
295 (irq_handler_t)xhci_msi_irq,
296 0, "xhci_hcd", xhci_to_hcd(xhci));
297 if (ret)
298 goto disable_msix;
299 }
300
301 hcd->msix_enabled = 1;
302 return ret;
303
304disable_msix:
305 xhci_dbg(xhci, "disable MSI-X interrupt\n");
306 xhci_free_irq(xhci);
307 pci_disable_msix(pdev);
308free_entries:
309 kfree(xhci->msix_entries);
310 xhci->msix_entries = NULL;
311 return ret;
312}
313
314/* Free any IRQs and disable MSI-X */
315static void xhci_cleanup_msix(struct xhci_hcd *xhci)
316{
317 struct usb_hcd *hcd = xhci_to_hcd(xhci);
318 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
319
320 if (xhci->quirks & XHCI_PLAT)
321 return;
322
323 xhci_free_irq(xhci);
324
325 if (xhci->msix_entries) {
326 pci_disable_msix(pdev);
327 kfree(xhci->msix_entries);
328 xhci->msix_entries = NULL;
329 } else {
330 pci_disable_msi(pdev);
331 }
332
333 hcd->msix_enabled = 0;
334 return;
335}
336
337static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
338{
339 int i;
340
341 if (xhci->msix_entries) {
342 for (i = 0; i < xhci->msix_count; i++)
343 synchronize_irq(xhci->msix_entries[i].vector);
344 }
345}
346
347static int xhci_try_enable_msi(struct usb_hcd *hcd)
348{
349 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
350 struct pci_dev *pdev;
351 int ret;
352
353 /* The xhci platform device has set up IRQs through usb_add_hcd. */
354 if (xhci->quirks & XHCI_PLAT)
355 return 0;
356
357 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
358 /*
359 * Some Fresco Logic host controllers advertise MSI, but fail to
360 * generate interrupts. Don't even try to enable MSI.
361 */
362 if (xhci->quirks & XHCI_BROKEN_MSI)
363 goto legacy_irq;
364
365 /* unregister the legacy interrupt */
366 if (hcd->irq)
367 free_irq(hcd->irq, hcd);
368 hcd->irq = 0;
369
370 ret = xhci_setup_msix(xhci);
371 if (ret)
372 /* fall back to msi*/
373 ret = xhci_setup_msi(xhci);
374
375 if (!ret)
376 /* hcd->irq is 0, we have MSI */
377 return 0;
378
379 if (!pdev->irq) {
380 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
381 return -EINVAL;
382 }
383
384 legacy_irq:
385 /* fall back to legacy interrupt*/
386 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
387 hcd->irq_descr, hcd);
388 if (ret) {
389 xhci_err(xhci, "request interrupt %d failed\n",
390 pdev->irq);
391 return ret;
392 }
393 hcd->irq = pdev->irq;
394 return 0;
395}
396
397#else
398
399static inline int xhci_try_enable_msi(struct usb_hcd *hcd)
400{
401 return 0;
402}
403
404static inline void xhci_cleanup_msix(struct xhci_hcd *xhci)
405{
406}
407
408static inline void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
409{
410}
411
412#endif
413
414static void compliance_mode_recovery(unsigned long arg)
415{
416 struct xhci_hcd *xhci;
417 struct usb_hcd *hcd;
418 u32 temp;
419 int i;
420
421 xhci = (struct xhci_hcd *)arg;
422
423 for (i = 0; i < xhci->num_usb3_ports; i++) {
424 temp = xhci_readl(xhci, xhci->usb3_ports[i]);
425 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
426 /*
427 * Compliance Mode Detected. Letting USB Core
428 * handle the Warm Reset
429 */
430 xhci_dbg(xhci, "Compliance Mode Detected->Port %d!\n",
431 i + 1);
432 xhci_dbg(xhci, "Attempting Recovery routine!\n");
433 hcd = xhci->shared_hcd;
434
435 if (hcd->state == HC_STATE_SUSPENDED)
436 usb_hcd_resume_root_hub(hcd);
437
438 usb_hcd_poll_rh_status(hcd);
439 }
440 }
441
442 if (xhci->port_status_u0 != ((1 << xhci->num_usb3_ports)-1))
443 mod_timer(&xhci->comp_mode_recovery_timer,
444 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
445}
446
447/*
448 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
449 * that causes ports behind that hardware to enter compliance mode sometimes.
450 * The quirk creates a timer that polls every 2 seconds the link state of
451 * each host controller's port and recovers it by issuing a Warm reset
452 * if Compliance mode is detected, otherwise the port will become "dead" (no
453 * device connections or disconnections will be detected anymore). Becasue no
454 * status event is generated when entering compliance mode (per xhci spec),
455 * this quirk is needed on systems that have the failing hardware installed.
456 */
457static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
458{
459 xhci->port_status_u0 = 0;
460 init_timer(&xhci->comp_mode_recovery_timer);
461
462 xhci->comp_mode_recovery_timer.data = (unsigned long) xhci;
463 xhci->comp_mode_recovery_timer.function = compliance_mode_recovery;
464 xhci->comp_mode_recovery_timer.expires = jiffies +
465 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
466
467 set_timer_slack(&xhci->comp_mode_recovery_timer,
468 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
469 add_timer(&xhci->comp_mode_recovery_timer);
470 xhci_dbg(xhci, "Compliance Mode Recovery Timer Initialized.\n");
471}
472
473/*
474 * This function identifies the systems that have installed the SN65LVPE502CP
475 * USB3.0 re-driver and that need the Compliance Mode Quirk.
476 * Systems:
477 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
478 */
479static bool compliance_mode_recovery_timer_quirk_check(void)
480{
481 const char *dmi_product_name, *dmi_sys_vendor;
482
483 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
484 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
485 if (!dmi_product_name || !dmi_sys_vendor)
486 return false;
487
488 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
489 return false;
490
491 if (strstr(dmi_product_name, "Z420") ||
492 strstr(dmi_product_name, "Z620") ||
493 strstr(dmi_product_name, "Z820") ||
494 strstr(dmi_product_name, "Z1 Workstation"))
495 return true;
496
497 return false;
498}
499
500static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
501{
502 return (xhci->port_status_u0 == ((1 << xhci->num_usb3_ports)-1));
503}
504
505
506/*
507 * Initialize memory for HCD and xHC (one-time init).
508 *
509 * Program the PAGESIZE register, initialize the device context array, create
510 * device contexts (?), set up a command ring segment (or two?), create event
511 * ring (one for now).
512 */
513int xhci_init(struct usb_hcd *hcd)
514{
515 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
516 int retval = 0;
517
518 xhci_dbg(xhci, "xhci_init\n");
519 spin_lock_init(&xhci->lock);
520 if (xhci->hci_version == 0x95 && link_quirk) {
521 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
522 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
523 } else {
524 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
525 }
526 retval = xhci_mem_init(xhci, GFP_KERNEL);
527 xhci_dbg(xhci, "Finished xhci_init\n");
528
529 /* Initializing Compliance Mode Recovery Data If Needed */
530 if (compliance_mode_recovery_timer_quirk_check()) {
531 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
532 compliance_mode_recovery_timer_init(xhci);
533 }
534
535 return retval;
536}
537
538/*-------------------------------------------------------------------------*/
539
540
541#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
542static void xhci_event_ring_work(unsigned long arg)
543{
544 unsigned long flags;
545 int temp;
546 u64 temp_64;
547 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
548 int i, j;
549
550 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
551
552 spin_lock_irqsave(&xhci->lock, flags);
553 temp = xhci_readl(xhci, &xhci->op_regs->status);
554 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
555 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
556 (xhci->xhc_state & XHCI_STATE_HALTED)) {
557 xhci_dbg(xhci, "HW died, polling stopped.\n");
558 spin_unlock_irqrestore(&xhci->lock, flags);
559 return;
560 }
561
562 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
563 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
564 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
565 xhci->error_bitmask = 0;
566 xhci_dbg(xhci, "Event ring:\n");
567 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
568 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
569 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
570 temp_64 &= ~ERST_PTR_MASK;
571 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
572 xhci_dbg(xhci, "Command ring:\n");
573 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
574 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
575 xhci_dbg_cmd_ptrs(xhci);
576 for (i = 0; i < MAX_HC_SLOTS; ++i) {
577 if (!xhci->devs[i])
578 continue;
579 for (j = 0; j < 31; ++j) {
580 xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
581 }
582 }
583 spin_unlock_irqrestore(&xhci->lock, flags);
584
585 if (!xhci->zombie)
586 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
587 else
588 xhci_dbg(xhci, "Quit polling the event ring.\n");
589}
590#endif
591
592static int xhci_run_finished(struct xhci_hcd *xhci)
593{
594 if (xhci_start(xhci)) {
595 xhci_halt(xhci);
596 return -ENODEV;
597 }
598 xhci->shared_hcd->state = HC_STATE_RUNNING;
599 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
600
601 if (xhci->quirks & XHCI_NEC_HOST)
602 xhci_ring_cmd_db(xhci);
603
604 xhci_dbg(xhci, "Finished xhci_run for USB3 roothub\n");
605 return 0;
606}
607
608/*
609 * Start the HC after it was halted.
610 *
611 * This function is called by the USB core when the HC driver is added.
612 * Its opposite is xhci_stop().
613 *
614 * xhci_init() must be called once before this function can be called.
615 * Reset the HC, enable device slot contexts, program DCBAAP, and
616 * set command ring pointer and event ring pointer.
617 *
618 * Setup MSI-X vectors and enable interrupts.
619 */
620int xhci_run(struct usb_hcd *hcd)
621{
622 u32 temp;
623 u64 temp_64;
624 int ret;
625 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
626
627 /* Start the xHCI host controller running only after the USB 2.0 roothub
628 * is setup.
629 */
630
631 hcd->uses_new_polling = 1;
632 if (!usb_hcd_is_primary_hcd(hcd))
633 return xhci_run_finished(xhci);
634
635 xhci_dbg(xhci, "xhci_run\n");
636
637 ret = xhci_try_enable_msi(hcd);
638 if (ret)
639 return ret;
640
641#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
642 init_timer(&xhci->event_ring_timer);
643 xhci->event_ring_timer.data = (unsigned long) xhci;
644 xhci->event_ring_timer.function = xhci_event_ring_work;
645 /* Poll the event ring */
646 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
647 xhci->zombie = 0;
648 xhci_dbg(xhci, "Setting event ring polling timer\n");
649 add_timer(&xhci->event_ring_timer);
650#endif
651
652 xhci_dbg(xhci, "Command ring memory map follows:\n");
653 xhci_debug_ring(xhci, xhci->cmd_ring);
654 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
655 xhci_dbg_cmd_ptrs(xhci);
656
657 xhci_dbg(xhci, "ERST memory map follows:\n");
658 xhci_dbg_erst(xhci, &xhci->erst);
659 xhci_dbg(xhci, "Event ring:\n");
660 xhci_debug_ring(xhci, xhci->event_ring);
661 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
662 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
663 temp_64 &= ~ERST_PTR_MASK;
664 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
665
666 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
667 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
668 temp &= ~ER_IRQ_INTERVAL_MASK;
669 temp |= (u32) 160;
670 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
671
672 /* Set the HCD state before we enable the irqs */
673 temp = xhci_readl(xhci, &xhci->op_regs->command);
674 temp |= (CMD_EIE);
675 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
676 temp);
677 xhci_writel(xhci, temp, &xhci->op_regs->command);
678
679 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
680 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
681 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
682 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
683 &xhci->ir_set->irq_pending);
684 xhci_print_ir_set(xhci, 0);
685
686 if (xhci->quirks & XHCI_NEC_HOST)
687 xhci_queue_vendor_command(xhci, 0, 0, 0,
688 TRB_TYPE(TRB_NEC_GET_FW));
689
690 xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
691 return 0;
692}
693
694static void xhci_only_stop_hcd(struct usb_hcd *hcd)
695{
696 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
697
698 spin_lock_irq(&xhci->lock);
699 xhci_halt(xhci);
700
701 /* The shared_hcd is going to be deallocated shortly (the USB core only
702 * calls this function when allocation fails in usb_add_hcd(), or
703 * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
704 */
705 xhci->shared_hcd = NULL;
706 spin_unlock_irq(&xhci->lock);
707}
708
709/*
710 * Stop xHCI driver.
711 *
712 * This function is called by the USB core when the HC driver is removed.
713 * Its opposite is xhci_run().
714 *
715 * Disable device contexts, disable IRQs, and quiesce the HC.
716 * Reset the HC, finish any completed transactions, and cleanup memory.
717 */
718void xhci_stop(struct usb_hcd *hcd)
719{
720 u32 temp;
721 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
722
723 if (!usb_hcd_is_primary_hcd(hcd)) {
724 xhci_only_stop_hcd(xhci->shared_hcd);
725 return;
726 }
727
728 spin_lock_irq(&xhci->lock);
729 /* Make sure the xHC is halted for a USB3 roothub
730 * (xhci_stop() could be called as part of failed init).
731 */
732 xhci_halt(xhci);
733 xhci_reset(xhci);
734 spin_unlock_irq(&xhci->lock);
735
736 xhci_cleanup_msix(xhci);
737
738#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
739 /* Tell the event ring poll function not to reschedule */
740 xhci->zombie = 1;
741 del_timer_sync(&xhci->event_ring_timer);
742#endif
743
744 /* Deleting Compliance Mode Recovery Timer */
745 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
746 (!(xhci_all_ports_seen_u0(xhci))))
747 del_timer_sync(&xhci->comp_mode_recovery_timer);
748
749 if (xhci->quirks & XHCI_AMD_PLL_FIX)
750 usb_amd_dev_put();
751
752 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
753 temp = xhci_readl(xhci, &xhci->op_regs->status);
754 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
755 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
756 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
757 &xhci->ir_set->irq_pending);
758 xhci_print_ir_set(xhci, 0);
759
760 xhci_dbg(xhci, "cleaning up memory\n");
761 xhci_mem_cleanup(xhci);
762 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
763 xhci_readl(xhci, &xhci->op_regs->status));
764}
765
766/*
767 * Shutdown HC (not bus-specific)
768 *
769 * This is called when the machine is rebooting or halting. We assume that the
770 * machine will be powered off, and the HC's internal state will be reset.
771 * Don't bother to free memory.
772 *
773 * This will only ever be called with the main usb_hcd (the USB3 roothub).
774 */
775void xhci_shutdown(struct usb_hcd *hcd)
776{
777 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
778
779 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
780 usb_disable_xhci_ports(to_pci_dev(hcd->self.controller));
781
782 spin_lock_irq(&xhci->lock);
783 xhci_halt(xhci);
784 /* Workaround for spurious wakeups at shutdown with HSW */
785 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
786 xhci_reset(xhci);
787 spin_unlock_irq(&xhci->lock);
788
789 xhci_cleanup_msix(xhci);
790
791 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
792 xhci_readl(xhci, &xhci->op_regs->status));
793
794 /* Yet another workaround for spurious wakeups at shutdown with HSW */
795 if (xhci->quirks & XHCI_SPURIOUS_WAKEUP)
796 pci_set_power_state(to_pci_dev(hcd->self.controller), PCI_D3hot);
797}
798
799#ifdef CONFIG_PM
800static void xhci_save_registers(struct xhci_hcd *xhci)
801{
802 xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
803 xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
804 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
805 xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
806 xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
807 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
808 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
809 xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
810 xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
811}
812
813static void xhci_restore_registers(struct xhci_hcd *xhci)
814{
815 xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
816 xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
817 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
818 xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
819 xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
820 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
821 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
822 xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
823 xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
824}
825
826static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
827{
828 u64 val_64;
829
830 /* step 2: initialize command ring buffer */
831 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
832 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
833 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
834 xhci->cmd_ring->dequeue) &
835 (u64) ~CMD_RING_RSVD_BITS) |
836 xhci->cmd_ring->cycle_state;
837 xhci_dbg(xhci, "// Setting command ring address to 0x%llx\n",
838 (long unsigned long) val_64);
839 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
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 */
851static 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 seg = ring->deq_seg;
858 do {
859 memset(seg->trbs, 0,
860 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
861 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
862 cpu_to_le32(~TRB_CYCLE);
863 seg = seg->next;
864 } while (seg != ring->deq_seg);
865
866 /* Reset the software enqueue and dequeue pointers */
867 ring->deq_seg = ring->first_seg;
868 ring->dequeue = ring->first_seg->trbs;
869 ring->enq_seg = ring->deq_seg;
870 ring->enqueue = ring->dequeue;
871
872 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
873 /*
874 * Ring is now zeroed, so the HW should look for change of ownership
875 * when the cycle bit is set to 1.
876 */
877 ring->cycle_state = 1;
878
879 /*
880 * Reset the hardware dequeue pointer.
881 * Yes, this will need to be re-written after resume, but we're paranoid
882 * and want to make sure the hardware doesn't access bogus memory
883 * because, say, the BIOS or an SMI started the host without changing
884 * the command ring pointers.
885 */
886 xhci_set_cmd_ring_deq(xhci);
887}
888
889static void xhci_disable_port_wake_on_bits(struct xhci_hcd *xhci)
890{
891 int port_index;
892 __le32 __iomem **port_array;
893 unsigned long flags;
894 u32 t1, t2;
895
896 spin_lock_irqsave(&xhci->lock, flags);
897
898 /* disble usb3 ports Wake bits*/
899 port_index = xhci->num_usb3_ports;
900 port_array = xhci->usb3_ports;
901 while (port_index--) {
902 t1 = readl(port_array[port_index]);
903 t1 = xhci_port_state_to_neutral(t1);
904 t2 = t1 & ~PORT_WAKE_BITS;
905 if (t1 != t2)
906 writel(t2, port_array[port_index]);
907 }
908
909 /* disble usb2 ports Wake bits*/
910 port_index = xhci->num_usb2_ports;
911 port_array = xhci->usb2_ports;
912 while (port_index--) {
913 t1 = readl(port_array[port_index]);
914 t1 = xhci_port_state_to_neutral(t1);
915 t2 = t1 & ~PORT_WAKE_BITS;
916 if (t1 != t2)
917 writel(t2, port_array[port_index]);
918 }
919
920 spin_unlock_irqrestore(&xhci->lock, flags);
921}
922
923/*
924 * Stop HC (not bus-specific)
925 *
926 * This is called when the machine transition into S3/S4 mode.
927 *
928 */
929int xhci_suspend(struct xhci_hcd *xhci, bool do_wakeup)
930{
931 int rc = 0;
932 unsigned int delay = XHCI_MAX_HALT_USEC;
933 struct usb_hcd *hcd = xhci_to_hcd(xhci);
934 u32 command;
935
936 /* Clear root port wake on bits if wakeup not allowed. */
937 if (!do_wakeup)
938 xhci_disable_port_wake_on_bits(xhci);
939
940 /* Don't poll the roothubs on bus suspend. */
941 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
942 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
943 del_timer_sync(&hcd->rh_timer);
944
945 spin_lock_irq(&xhci->lock);
946 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
947 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
948 /* step 1: stop endpoint */
949 /* skipped assuming that port suspend has done */
950
951 /* step 2: clear Run/Stop bit */
952 command = xhci_readl(xhci, &xhci->op_regs->command);
953 command &= ~CMD_RUN;
954 xhci_writel(xhci, command, &xhci->op_regs->command);
955
956 /* Some chips from Fresco Logic need an extraordinary delay */
957 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
958
959 if (handshake(xhci, &xhci->op_regs->status,
960 STS_HALT, STS_HALT, delay)) {
961 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
962 spin_unlock_irq(&xhci->lock);
963 return -ETIMEDOUT;
964 }
965 xhci_clear_command_ring(xhci);
966
967 /* step 3: save registers */
968 xhci_save_registers(xhci);
969
970 /* step 4: set CSS flag */
971 command = xhci_readl(xhci, &xhci->op_regs->command);
972 command |= CMD_CSS;
973 xhci_writel(xhci, command, &xhci->op_regs->command);
974 if (handshake(xhci, &xhci->op_regs->status, STS_SAVE, 0, 10 * 1000)) {
975 xhci_warn(xhci, "WARN: xHC save state timeout\n");
976 spin_unlock_irq(&xhci->lock);
977 return -ETIMEDOUT;
978 }
979 spin_unlock_irq(&xhci->lock);
980
981 /*
982 * Deleting Compliance Mode Recovery Timer because the xHCI Host
983 * is about to be suspended.
984 */
985 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
986 (!(xhci_all_ports_seen_u0(xhci)))) {
987 del_timer_sync(&xhci->comp_mode_recovery_timer);
988 xhci_dbg(xhci, "Compliance Mode Recovery Timer Deleted!\n");
989 }
990
991 /* step 5: remove core well power */
992 /* synchronize irq when using MSI-X */
993 xhci_msix_sync_irqs(xhci);
994
995 return rc;
996}
997
998/*
999 * start xHC (not bus-specific)
1000 *
1001 * This is called when the machine transition from S3/S4 mode.
1002 *
1003 */
1004int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
1005{
1006 u32 command, temp = 0, status;
1007 struct usb_hcd *hcd = xhci_to_hcd(xhci);
1008 struct usb_hcd *secondary_hcd;
1009 int retval = 0;
1010 bool comp_timer_running = false;
1011
1012 /* Wait a bit if either of the roothubs need to settle from the
1013 * transition into bus suspend.
1014 */
1015 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
1016 time_before(jiffies,
1017 xhci->bus_state[1].next_statechange))
1018 msleep(100);
1019
1020 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
1021 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
1022
1023 spin_lock_irq(&xhci->lock);
1024 if (xhci->quirks & XHCI_RESET_ON_RESUME)
1025 hibernated = true;
1026
1027 if (!hibernated) {
1028 /* step 1: restore register */
1029 xhci_restore_registers(xhci);
1030 /* step 2: initialize command ring buffer */
1031 xhci_set_cmd_ring_deq(xhci);
1032 /* step 3: restore state and start state*/
1033 /* step 3: set CRS flag */
1034 command = xhci_readl(xhci, &xhci->op_regs->command);
1035 command |= CMD_CRS;
1036 xhci_writel(xhci, command, &xhci->op_regs->command);
1037 if (handshake(xhci, &xhci->op_regs->status,
1038 STS_RESTORE, 0, 10 * 1000)) {
1039 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
1040 spin_unlock_irq(&xhci->lock);
1041 return -ETIMEDOUT;
1042 }
1043 temp = xhci_readl(xhci, &xhci->op_regs->status);
1044 }
1045
1046 /* If restore operation fails, re-initialize the HC during resume */
1047 if ((temp & STS_SRE) || hibernated) {
1048
1049 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
1050 !(xhci_all_ports_seen_u0(xhci))) {
1051 del_timer_sync(&xhci->comp_mode_recovery_timer);
1052 xhci_dbg(xhci, "Compliance Mode Recovery Timer deleted!\n");
1053 }
1054
1055 /* Let the USB core know _both_ roothubs lost power. */
1056 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
1057 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
1058
1059 xhci_dbg(xhci, "Stop HCD\n");
1060 xhci_halt(xhci);
1061 xhci_reset(xhci);
1062 spin_unlock_irq(&xhci->lock);
1063 xhci_cleanup_msix(xhci);
1064
1065#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
1066 /* Tell the event ring poll function not to reschedule */
1067 xhci->zombie = 1;
1068 del_timer_sync(&xhci->event_ring_timer);
1069#endif
1070
1071 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
1072 temp = xhci_readl(xhci, &xhci->op_regs->status);
1073 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
1074 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
1075 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
1076 &xhci->ir_set->irq_pending);
1077 xhci_print_ir_set(xhci, 0);
1078
1079 xhci_dbg(xhci, "cleaning up memory\n");
1080 xhci_mem_cleanup(xhci);
1081 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
1082 xhci_readl(xhci, &xhci->op_regs->status));
1083
1084 /* USB core calls the PCI reinit and start functions twice:
1085 * first with the primary HCD, and then with the secondary HCD.
1086 * If we don't do the same, the host will never be started.
1087 */
1088 if (!usb_hcd_is_primary_hcd(hcd))
1089 secondary_hcd = hcd;
1090 else
1091 secondary_hcd = xhci->shared_hcd;
1092
1093 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1094 retval = xhci_init(hcd->primary_hcd);
1095 if (retval)
1096 return retval;
1097 comp_timer_running = true;
1098
1099 xhci_dbg(xhci, "Start the primary HCD\n");
1100 retval = xhci_run(hcd->primary_hcd);
1101 if (!retval) {
1102 xhci_dbg(xhci, "Start the secondary HCD\n");
1103 retval = xhci_run(secondary_hcd);
1104 }
1105 hcd->state = HC_STATE_SUSPENDED;
1106 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
1107 goto done;
1108 }
1109
1110 /* step 4: set Run/Stop bit */
1111 command = xhci_readl(xhci, &xhci->op_regs->command);
1112 command |= CMD_RUN;
1113 xhci_writel(xhci, command, &xhci->op_regs->command);
1114 handshake(xhci, &xhci->op_regs->status, STS_HALT,
1115 0, 250 * 1000);
1116
1117 /* step 5: walk topology and initialize portsc,
1118 * portpmsc and portli
1119 */
1120 /* this is done in bus_resume */
1121
1122 /* step 6: restart each of the previously
1123 * Running endpoints by ringing their doorbells
1124 */
1125
1126 spin_unlock_irq(&xhci->lock);
1127
1128 done:
1129 if (retval == 0) {
1130 /* Resume root hubs only when have pending events. */
1131 status = readl(&xhci->op_regs->status);
1132 if (status & STS_EINT) {
1133 usb_hcd_resume_root_hub(hcd);
1134 usb_hcd_resume_root_hub(xhci->shared_hcd);
1135 }
1136 }
1137
1138 /*
1139 * If system is subject to the Quirk, Compliance Mode Timer needs to
1140 * be re-initialized Always after a system resume. Ports are subject
1141 * to suffer the Compliance Mode issue again. It doesn't matter if
1142 * ports have entered previously to U0 before system's suspension.
1143 */
1144 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
1145 compliance_mode_recovery_timer_init(xhci);
1146
1147 /* Re-enable port polling. */
1148 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
1149 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1150 usb_hcd_poll_rh_status(hcd);
1151
1152 return retval;
1153}
1154#endif /* CONFIG_PM */
1155
1156/*-------------------------------------------------------------------------*/
1157
1158/**
1159 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1160 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1161 * value to right shift 1 for the bitmask.
1162 *
1163 * Index = (epnum * 2) + direction - 1,
1164 * where direction = 0 for OUT, 1 for IN.
1165 * For control endpoints, the IN index is used (OUT index is unused), so
1166 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1167 */
1168unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1169{
1170 unsigned int index;
1171 if (usb_endpoint_xfer_control(desc))
1172 index = (unsigned int) (usb_endpoint_num(desc)*2);
1173 else
1174 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1175 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1176 return index;
1177}
1178
1179/* Find the flag for this endpoint (for use in the control context). Use the
1180 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1181 * bit 1, etc.
1182 */
1183unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
1184{
1185 return 1 << (xhci_get_endpoint_index(desc) + 1);
1186}
1187
1188/* Find the flag for this endpoint (for use in the control context). Use the
1189 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1190 * bit 1, etc.
1191 */
1192unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
1193{
1194 return 1 << (ep_index + 1);
1195}
1196
1197/* Compute the last valid endpoint context index. Basically, this is the
1198 * endpoint index plus one. For slot contexts with more than valid endpoint,
1199 * we find the most significant bit set in the added contexts flags.
1200 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1201 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1202 */
1203unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
1204{
1205 return fls(added_ctxs) - 1;
1206}
1207
1208/* Returns 1 if the arguments are OK;
1209 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1210 */
1211static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
1212 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1213 const char *func) {
1214 struct xhci_hcd *xhci;
1215 struct xhci_virt_device *virt_dev;
1216
1217 if (!hcd || (check_ep && !ep) || !udev) {
1218 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
1219 func);
1220 return -EINVAL;
1221 }
1222 if (!udev->parent) {
1223 printk(KERN_DEBUG "xHCI %s called for root hub\n",
1224 func);
1225 return 0;
1226 }
1227
1228 xhci = hcd_to_xhci(hcd);
1229 if (check_virt_dev) {
1230 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
1231 printk(KERN_DEBUG "xHCI %s called with unaddressed "
1232 "device\n", func);
1233 return -EINVAL;
1234 }
1235
1236 virt_dev = xhci->devs[udev->slot_id];
1237 if (virt_dev->udev != udev) {
1238 printk(KERN_DEBUG "xHCI %s called with udev and "
1239 "virt_dev does not match\n", func);
1240 return -EINVAL;
1241 }
1242 }
1243
1244 if (xhci->xhc_state & XHCI_STATE_HALTED)
1245 return -ENODEV;
1246
1247 return 1;
1248}
1249
1250static int xhci_configure_endpoint(struct xhci_hcd *xhci,
1251 struct usb_device *udev, struct xhci_command *command,
1252 bool ctx_change, bool must_succeed);
1253
1254/*
1255 * Full speed devices may have a max packet size greater than 8 bytes, but the
1256 * USB core doesn't know that until it reads the first 8 bytes of the
1257 * descriptor. If the usb_device's max packet size changes after that point,
1258 * we need to issue an evaluate context command and wait on it.
1259 */
1260static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1261 unsigned int ep_index, struct urb *urb)
1262{
1263 struct xhci_container_ctx *in_ctx;
1264 struct xhci_container_ctx *out_ctx;
1265 struct xhci_input_control_ctx *ctrl_ctx;
1266 struct xhci_ep_ctx *ep_ctx;
1267 int max_packet_size;
1268 int hw_max_packet_size;
1269 int ret = 0;
1270
1271 out_ctx = xhci->devs[slot_id]->out_ctx;
1272 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1273 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
1274 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
1275 if (hw_max_packet_size != max_packet_size) {
1276 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
1277 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
1278 max_packet_size);
1279 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
1280 hw_max_packet_size);
1281 xhci_dbg(xhci, "Issuing evaluate context command.\n");
1282
1283 /* Set up the modified control endpoint 0 */
1284 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1285 xhci->devs[slot_id]->out_ctx, ep_index);
1286 in_ctx = xhci->devs[slot_id]->in_ctx;
1287 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1288 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1289 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
1290
1291 /* Set up the input context flags for the command */
1292 /* FIXME: This won't work if a non-default control endpoint
1293 * changes max packet sizes.
1294 */
1295 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1296 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
1297 ctrl_ctx->drop_flags = 0;
1298
1299 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
1300 xhci_dbg_ctx(xhci, in_ctx, ep_index);
1301 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
1302 xhci_dbg_ctx(xhci, out_ctx, ep_index);
1303
1304 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
1305 true, false);
1306
1307 /* Clean up the input context for later use by bandwidth
1308 * functions.
1309 */
1310 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
1311 }
1312 return ret;
1313}
1314
1315/*
1316 * non-error returns are a promise to giveback() the urb later
1317 * we drop ownership so next owner (or urb unlink) can get it
1318 */
1319int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1320{
1321 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1322 struct xhci_td *buffer;
1323 unsigned long flags;
1324 int ret = 0;
1325 unsigned int slot_id, ep_index;
1326 struct urb_priv *urb_priv;
1327 int size, i;
1328
1329 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1330 true, true, __func__) <= 0)
1331 return -EINVAL;
1332
1333 slot_id = urb->dev->slot_id;
1334 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1335
1336 if (!HCD_HW_ACCESSIBLE(hcd)) {
1337 if (!in_interrupt())
1338 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
1339 ret = -ESHUTDOWN;
1340 goto exit;
1341 }
1342
1343 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1344 size = urb->number_of_packets;
1345 else
1346 size = 1;
1347
1348 urb_priv = kzalloc(sizeof(struct urb_priv) +
1349 size * sizeof(struct xhci_td *), mem_flags);
1350 if (!urb_priv)
1351 return -ENOMEM;
1352
1353 buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
1354 if (!buffer) {
1355 kfree(urb_priv);
1356 return -ENOMEM;
1357 }
1358
1359 for (i = 0; i < size; i++) {
1360 urb_priv->td[i] = buffer;
1361 buffer++;
1362 }
1363
1364 urb_priv->length = size;
1365 urb_priv->td_cnt = 0;
1366 urb->hcpriv = urb_priv;
1367
1368 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1369 /* Check to see if the max packet size for the default control
1370 * endpoint changed during FS device enumeration
1371 */
1372 if (urb->dev->speed == USB_SPEED_FULL) {
1373 ret = xhci_check_maxpacket(xhci, slot_id,
1374 ep_index, urb);
1375 if (ret < 0) {
1376 xhci_urb_free_priv(xhci, urb_priv);
1377 urb->hcpriv = NULL;
1378 return ret;
1379 }
1380 }
1381
1382 /* We have a spinlock and interrupts disabled, so we must pass
1383 * atomic context to this function, which may allocate memory.
1384 */
1385 spin_lock_irqsave(&xhci->lock, flags);
1386 if (xhci->xhc_state & XHCI_STATE_DYING)
1387 goto dying;
1388 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
1389 slot_id, ep_index);
1390 if (ret)
1391 goto free_priv;
1392 spin_unlock_irqrestore(&xhci->lock, flags);
1393 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1394 spin_lock_irqsave(&xhci->lock, flags);
1395 if (xhci->xhc_state & XHCI_STATE_DYING)
1396 goto dying;
1397 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1398 EP_GETTING_STREAMS) {
1399 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1400 "is transitioning to using streams.\n");
1401 ret = -EINVAL;
1402 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1403 EP_GETTING_NO_STREAMS) {
1404 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1405 "is transitioning to "
1406 "not having streams.\n");
1407 ret = -EINVAL;
1408 } else {
1409 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1410 slot_id, ep_index);
1411 }
1412 if (ret)
1413 goto free_priv;
1414 spin_unlock_irqrestore(&xhci->lock, flags);
1415 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1416 spin_lock_irqsave(&xhci->lock, flags);
1417 if (xhci->xhc_state & XHCI_STATE_DYING)
1418 goto dying;
1419 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1420 slot_id, ep_index);
1421 if (ret)
1422 goto free_priv;
1423 spin_unlock_irqrestore(&xhci->lock, flags);
1424 } else {
1425 spin_lock_irqsave(&xhci->lock, flags);
1426 if (xhci->xhc_state & XHCI_STATE_DYING)
1427 goto dying;
1428 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1429 slot_id, ep_index);
1430 if (ret)
1431 goto free_priv;
1432 spin_unlock_irqrestore(&xhci->lock, flags);
1433 }
1434exit:
1435 return ret;
1436dying:
1437 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1438 "non-responsive xHCI host.\n",
1439 urb->ep->desc.bEndpointAddress, urb);
1440 ret = -ESHUTDOWN;
1441free_priv:
1442 xhci_urb_free_priv(xhci, urb_priv);
1443 urb->hcpriv = NULL;
1444 spin_unlock_irqrestore(&xhci->lock, flags);
1445 return ret;
1446}
1447
1448/* Get the right ring for the given URB.
1449 * If the endpoint supports streams, boundary check the URB's stream ID.
1450 * If the endpoint doesn't support streams, return the singular endpoint ring.
1451 */
1452static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
1453 struct urb *urb)
1454{
1455 unsigned int slot_id;
1456 unsigned int ep_index;
1457 unsigned int stream_id;
1458 struct xhci_virt_ep *ep;
1459
1460 slot_id = urb->dev->slot_id;
1461 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1462 stream_id = urb->stream_id;
1463 ep = &xhci->devs[slot_id]->eps[ep_index];
1464 /* Common case: no streams */
1465 if (!(ep->ep_state & EP_HAS_STREAMS))
1466 return ep->ring;
1467
1468 if (stream_id == 0) {
1469 xhci_warn(xhci,
1470 "WARN: Slot ID %u, ep index %u has streams, "
1471 "but URB has no stream ID.\n",
1472 slot_id, ep_index);
1473 return NULL;
1474 }
1475
1476 if (stream_id < ep->stream_info->num_streams)
1477 return ep->stream_info->stream_rings[stream_id];
1478
1479 xhci_warn(xhci,
1480 "WARN: Slot ID %u, ep index %u has "
1481 "stream IDs 1 to %u allocated, "
1482 "but stream ID %u is requested.\n",
1483 slot_id, ep_index,
1484 ep->stream_info->num_streams - 1,
1485 stream_id);
1486 return NULL;
1487}
1488
1489/*
1490 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1491 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1492 * should pick up where it left off in the TD, unless a Set Transfer Ring
1493 * Dequeue Pointer is issued.
1494 *
1495 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1496 * the ring. Since the ring is a contiguous structure, they can't be physically
1497 * removed. Instead, there are two options:
1498 *
1499 * 1) If the HC is in the middle of processing the URB to be canceled, we
1500 * simply move the ring's dequeue pointer past those TRBs using the Set
1501 * Transfer Ring Dequeue Pointer command. This will be the common case,
1502 * when drivers timeout on the last submitted URB and attempt to cancel.
1503 *
1504 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1505 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1506 * HC will need to invalidate the any TRBs it has cached after the stop
1507 * endpoint command, as noted in the xHCI 0.95 errata.
1508 *
1509 * 3) The TD may have completed by the time the Stop Endpoint Command
1510 * completes, so software needs to handle that case too.
1511 *
1512 * This function should protect against the TD enqueueing code ringing the
1513 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1514 * It also needs to account for multiple cancellations on happening at the same
1515 * time for the same endpoint.
1516 *
1517 * Note that this function can be called in any context, or so says
1518 * usb_hcd_unlink_urb()
1519 */
1520int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1521{
1522 unsigned long flags;
1523 int ret, i;
1524 u32 temp;
1525 struct xhci_hcd *xhci;
1526 struct urb_priv *urb_priv;
1527 struct xhci_td *td;
1528 unsigned int ep_index;
1529 struct xhci_ring *ep_ring;
1530 struct xhci_virt_ep *ep;
1531
1532 xhci = hcd_to_xhci(hcd);
1533 spin_lock_irqsave(&xhci->lock, flags);
1534 /* Make sure the URB hasn't completed or been unlinked already */
1535 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1536 if (ret || !urb->hcpriv)
1537 goto done;
1538 temp = xhci_readl(xhci, &xhci->op_regs->status);
1539 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
1540 xhci_dbg(xhci, "HW died, freeing TD.\n");
1541 urb_priv = urb->hcpriv;
1542 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
1543 td = urb_priv->td[i];
1544 if (!list_empty(&td->td_list))
1545 list_del_init(&td->td_list);
1546 if (!list_empty(&td->cancelled_td_list))
1547 list_del_init(&td->cancelled_td_list);
1548 }
1549
1550 usb_hcd_unlink_urb_from_ep(hcd, urb);
1551 spin_unlock_irqrestore(&xhci->lock, flags);
1552 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
1553 xhci_urb_free_priv(xhci, urb_priv);
1554 return ret;
1555 }
1556 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
1557 (xhci->xhc_state & XHCI_STATE_HALTED)) {
1558 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
1559 "non-responsive xHCI host.\n",
1560 urb->ep->desc.bEndpointAddress, urb);
1561 /* Let the stop endpoint command watchdog timer (which set this
1562 * state) finish cleaning up the endpoint TD lists. We must
1563 * have caught it in the middle of dropping a lock and giving
1564 * back an URB.
1565 */
1566 goto done;
1567 }
1568
1569 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1570 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
1571 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1572 if (!ep_ring) {
1573 ret = -EINVAL;
1574 goto done;
1575 }
1576
1577 urb_priv = urb->hcpriv;
1578 i = urb_priv->td_cnt;
1579 if (i < urb_priv->length)
1580 xhci_dbg(xhci, "Cancel URB %p, dev %s, ep 0x%x, "
1581 "starting at offset 0x%llx\n",
1582 urb, urb->dev->devpath,
1583 urb->ep->desc.bEndpointAddress,
1584 (unsigned long long) xhci_trb_virt_to_dma(
1585 urb_priv->td[i]->start_seg,
1586 urb_priv->td[i]->first_trb));
1587
1588 for (; i < urb_priv->length; i++) {
1589 td = urb_priv->td[i];
1590 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1591 }
1592
1593 /* Queue a stop endpoint command, but only if this is
1594 * the first cancellation to be handled.
1595 */
1596 if (!(ep->ep_state & EP_HALT_PENDING)) {
1597 ep->ep_state |= EP_HALT_PENDING;
1598 ep->stop_cmds_pending++;
1599 ep->stop_cmd_timer.expires = jiffies +
1600 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1601 add_timer(&ep->stop_cmd_timer);
1602 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
1603 xhci_ring_cmd_db(xhci);
1604 }
1605done:
1606 spin_unlock_irqrestore(&xhci->lock, flags);
1607 return ret;
1608}
1609
1610/* Drop an endpoint from a new bandwidth configuration for this device.
1611 * Only one call to this function is allowed per endpoint before
1612 * check_bandwidth() or reset_bandwidth() must be called.
1613 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1614 * add the endpoint to the schedule with possibly new parameters denoted by a
1615 * different endpoint descriptor in usb_host_endpoint.
1616 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1617 * not allowed.
1618 *
1619 * The USB core will not allow URBs to be queued to an endpoint that is being
1620 * disabled, so there's no need for mutual exclusion to protect
1621 * the xhci->devs[slot_id] structure.
1622 */
1623int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1624 struct usb_host_endpoint *ep)
1625{
1626 struct xhci_hcd *xhci;
1627 struct xhci_container_ctx *in_ctx, *out_ctx;
1628 struct xhci_input_control_ctx *ctrl_ctx;
1629 struct xhci_slot_ctx *slot_ctx;
1630 unsigned int last_ctx;
1631 unsigned int ep_index;
1632 struct xhci_ep_ctx *ep_ctx;
1633 u32 drop_flag;
1634 u32 new_add_flags, new_drop_flags, new_slot_info;
1635 int ret;
1636
1637 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
1638 if (ret <= 0)
1639 return ret;
1640 xhci = hcd_to_xhci(hcd);
1641 if (xhci->xhc_state & XHCI_STATE_DYING)
1642 return -ENODEV;
1643
1644 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
1645 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1646 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1647 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1648 __func__, drop_flag);
1649 return 0;
1650 }
1651
1652 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
1653 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1654 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1655 ep_index = xhci_get_endpoint_index(&ep->desc);
1656 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1657 /* If the HC already knows the endpoint is disabled,
1658 * or the HCD has noted it is disabled, ignore this request
1659 */
1660 if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1661 cpu_to_le32(EP_STATE_DISABLED)) ||
1662 le32_to_cpu(ctrl_ctx->drop_flags) &
1663 xhci_get_endpoint_flag(&ep->desc)) {
1664 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1665 __func__, ep);
1666 return 0;
1667 }
1668
1669 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1670 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
1671
1672 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1673 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1674
1675 last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
1676 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
1677 /* Update the last valid endpoint context, if we deleted the last one */
1678 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
1679 LAST_CTX(last_ctx)) {
1680 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1681 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
1682 }
1683 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
1684
1685 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1686
1687 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1688 (unsigned int) ep->desc.bEndpointAddress,
1689 udev->slot_id,
1690 (unsigned int) new_drop_flags,
1691 (unsigned int) new_add_flags,
1692 (unsigned int) new_slot_info);
1693 return 0;
1694}
1695
1696/* Add an endpoint to a new possible bandwidth configuration for this device.
1697 * Only one call to this function is allowed per endpoint before
1698 * check_bandwidth() or reset_bandwidth() must be called.
1699 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1700 * add the endpoint to the schedule with possibly new parameters denoted by a
1701 * different endpoint descriptor in usb_host_endpoint.
1702 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1703 * not allowed.
1704 *
1705 * The USB core will not allow URBs to be queued to an endpoint until the
1706 * configuration or alt setting is installed in the device, so there's no need
1707 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
1708 */
1709int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1710 struct usb_host_endpoint *ep)
1711{
1712 struct xhci_hcd *xhci;
1713 struct xhci_container_ctx *in_ctx, *out_ctx;
1714 unsigned int ep_index;
1715 struct xhci_ep_ctx *ep_ctx;
1716 struct xhci_slot_ctx *slot_ctx;
1717 struct xhci_input_control_ctx *ctrl_ctx;
1718 u32 added_ctxs;
1719 unsigned int last_ctx;
1720 u32 new_add_flags, new_drop_flags, new_slot_info;
1721 struct xhci_virt_device *virt_dev;
1722 int ret = 0;
1723
1724 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
1725 if (ret <= 0) {
1726 /* So we won't queue a reset ep command for a root hub */
1727 ep->hcpriv = NULL;
1728 return ret;
1729 }
1730 xhci = hcd_to_xhci(hcd);
1731 if (xhci->xhc_state & XHCI_STATE_DYING)
1732 return -ENODEV;
1733
1734 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1735 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1736 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1737 /* FIXME when we have to issue an evaluate endpoint command to
1738 * deal with ep0 max packet size changing once we get the
1739 * descriptors
1740 */
1741 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1742 __func__, added_ctxs);
1743 return 0;
1744 }
1745
1746 virt_dev = xhci->devs[udev->slot_id];
1747 in_ctx = virt_dev->in_ctx;
1748 out_ctx = virt_dev->out_ctx;
1749 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1750 ep_index = xhci_get_endpoint_index(&ep->desc);
1751 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1752
1753 /* If this endpoint is already in use, and the upper layers are trying
1754 * to add it again without dropping it, reject the addition.
1755 */
1756 if (virt_dev->eps[ep_index].ring &&
1757 !(le32_to_cpu(ctrl_ctx->drop_flags) &
1758 xhci_get_endpoint_flag(&ep->desc))) {
1759 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1760 "without dropping it.\n",
1761 (unsigned int) ep->desc.bEndpointAddress);
1762 return -EINVAL;
1763 }
1764
1765 /* If the HCD has already noted the endpoint is enabled,
1766 * ignore this request.
1767 */
1768 if (le32_to_cpu(ctrl_ctx->add_flags) &
1769 xhci_get_endpoint_flag(&ep->desc)) {
1770 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1771 __func__, ep);
1772 return 0;
1773 }
1774
1775 /*
1776 * Configuration and alternate setting changes must be done in
1777 * process context, not interrupt context (or so documenation
1778 * for usb_set_interface() and usb_set_configuration() claim).
1779 */
1780 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
1781 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1782 __func__, ep->desc.bEndpointAddress);
1783 return -ENOMEM;
1784 }
1785
1786 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1787 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
1788
1789 /* If xhci_endpoint_disable() was called for this endpoint, but the
1790 * xHC hasn't been notified yet through the check_bandwidth() call,
1791 * this re-adds a new state for the endpoint from the new endpoint
1792 * descriptors. We must drop and re-add this endpoint, so we leave the
1793 * drop flags alone.
1794 */
1795 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
1796
1797 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
1798 /* Update the last valid endpoint context, if we just added one past */
1799 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
1800 LAST_CTX(last_ctx)) {
1801 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1802 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
1803 }
1804 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
1805
1806 /* Store the usb_device pointer for later use */
1807 ep->hcpriv = udev;
1808
1809 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1810 (unsigned int) ep->desc.bEndpointAddress,
1811 udev->slot_id,
1812 (unsigned int) new_drop_flags,
1813 (unsigned int) new_add_flags,
1814 (unsigned int) new_slot_info);
1815 return 0;
1816}
1817
1818static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
1819{
1820 struct xhci_input_control_ctx *ctrl_ctx;
1821 struct xhci_ep_ctx *ep_ctx;
1822 struct xhci_slot_ctx *slot_ctx;
1823 int i;
1824
1825 /* When a device's add flag and drop flag are zero, any subsequent
1826 * configure endpoint command will leave that endpoint's state
1827 * untouched. Make sure we don't leave any old state in the input
1828 * endpoint contexts.
1829 */
1830 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1831 ctrl_ctx->drop_flags = 0;
1832 ctrl_ctx->add_flags = 0;
1833 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1834 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1835 /* Endpoint 0 is always valid */
1836 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
1837 for (i = 1; i < 31; ++i) {
1838 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
1839 ep_ctx->ep_info = 0;
1840 ep_ctx->ep_info2 = 0;
1841 ep_ctx->deq = 0;
1842 ep_ctx->tx_info = 0;
1843 }
1844}
1845
1846static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
1847 struct usb_device *udev, u32 *cmd_status)
1848{
1849 int ret;
1850
1851 switch (*cmd_status) {
1852 case COMP_ENOMEM:
1853 dev_warn(&udev->dev, "Not enough host controller resources "
1854 "for new device state.\n");
1855 ret = -ENOMEM;
1856 /* FIXME: can we allocate more resources for the HC? */
1857 break;
1858 case COMP_BW_ERR:
1859 case COMP_2ND_BW_ERR:
1860 dev_warn(&udev->dev, "Not enough bandwidth "
1861 "for new device state.\n");
1862 ret = -ENOSPC;
1863 /* FIXME: can we go back to the old state? */
1864 break;
1865 case COMP_TRB_ERR:
1866 /* the HCD set up something wrong */
1867 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1868 "add flag = 1, "
1869 "and endpoint is not disabled.\n");
1870 ret = -EINVAL;
1871 break;
1872 case COMP_DEV_ERR:
1873 dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
1874 "configure command.\n");
1875 ret = -ENODEV;
1876 break;
1877 case COMP_SUCCESS:
1878 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1879 ret = 0;
1880 break;
1881 default:
1882 xhci_err(xhci, "ERROR: unexpected command completion "
1883 "code 0x%x.\n", *cmd_status);
1884 ret = -EINVAL;
1885 break;
1886 }
1887 return ret;
1888}
1889
1890static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
1891 struct usb_device *udev, u32 *cmd_status)
1892{
1893 int ret;
1894 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
1895
1896 switch (*cmd_status) {
1897 case COMP_EINVAL:
1898 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1899 "context command.\n");
1900 ret = -EINVAL;
1901 break;
1902 case COMP_EBADSLT:
1903 dev_warn(&udev->dev, "WARN: slot not enabled for"
1904 "evaluate context command.\n");
1905 case COMP_CTX_STATE:
1906 dev_warn(&udev->dev, "WARN: invalid context state for "
1907 "evaluate context command.\n");
1908 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1909 ret = -EINVAL;
1910 break;
1911 case COMP_DEV_ERR:
1912 dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
1913 "context command.\n");
1914 ret = -ENODEV;
1915 break;
1916 case COMP_MEL_ERR:
1917 /* Max Exit Latency too large error */
1918 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1919 ret = -EINVAL;
1920 break;
1921 case COMP_SUCCESS:
1922 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1923 ret = 0;
1924 break;
1925 default:
1926 xhci_err(xhci, "ERROR: unexpected command completion "
1927 "code 0x%x.\n", *cmd_status);
1928 ret = -EINVAL;
1929 break;
1930 }
1931 return ret;
1932}
1933
1934static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
1935 struct xhci_container_ctx *in_ctx)
1936{
1937 struct xhci_input_control_ctx *ctrl_ctx;
1938 u32 valid_add_flags;
1939 u32 valid_drop_flags;
1940
1941 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1942 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1943 * (bit 1). The default control endpoint is added during the Address
1944 * Device command and is never removed until the slot is disabled.
1945 */
1946 valid_add_flags = ctrl_ctx->add_flags >> 2;
1947 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1948
1949 /* Use hweight32 to count the number of ones in the add flags, or
1950 * number of endpoints added. Don't count endpoints that are changed
1951 * (both added and dropped).
1952 */
1953 return hweight32(valid_add_flags) -
1954 hweight32(valid_add_flags & valid_drop_flags);
1955}
1956
1957static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
1958 struct xhci_container_ctx *in_ctx)
1959{
1960 struct xhci_input_control_ctx *ctrl_ctx;
1961 u32 valid_add_flags;
1962 u32 valid_drop_flags;
1963
1964 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
1965 valid_add_flags = ctrl_ctx->add_flags >> 2;
1966 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1967
1968 return hweight32(valid_drop_flags) -
1969 hweight32(valid_add_flags & valid_drop_flags);
1970}
1971
1972/*
1973 * We need to reserve the new number of endpoints before the configure endpoint
1974 * command completes. We can't subtract the dropped endpoints from the number
1975 * of active endpoints until the command completes because we can oversubscribe
1976 * the host in this case:
1977 *
1978 * - the first configure endpoint command drops more endpoints than it adds
1979 * - a second configure endpoint command that adds more endpoints is queued
1980 * - the first configure endpoint command fails, so the config is unchanged
1981 * - the second command may succeed, even though there isn't enough resources
1982 *
1983 * Must be called with xhci->lock held.
1984 */
1985static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
1986 struct xhci_container_ctx *in_ctx)
1987{
1988 u32 added_eps;
1989
1990 added_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
1991 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
1992 xhci_dbg(xhci, "Not enough ep ctxs: "
1993 "%u active, need to add %u, limit is %u.\n",
1994 xhci->num_active_eps, added_eps,
1995 xhci->limit_active_eps);
1996 return -ENOMEM;
1997 }
1998 xhci->num_active_eps += added_eps;
1999 xhci_dbg(xhci, "Adding %u ep ctxs, %u now active.\n", added_eps,
2000 xhci->num_active_eps);
2001 return 0;
2002}
2003
2004/*
2005 * The configure endpoint was failed by the xHC for some other reason, so we
2006 * need to revert the resources that failed configuration would have used.
2007 *
2008 * Must be called with xhci->lock held.
2009 */
2010static void xhci_free_host_resources(struct xhci_hcd *xhci,
2011 struct xhci_container_ctx *in_ctx)
2012{
2013 u32 num_failed_eps;
2014
2015 num_failed_eps = xhci_count_num_new_endpoints(xhci, in_ctx);
2016 xhci->num_active_eps -= num_failed_eps;
2017 xhci_dbg(xhci, "Removing %u failed ep ctxs, %u now active.\n",
2018 num_failed_eps,
2019 xhci->num_active_eps);
2020}
2021
2022/*
2023 * Now that the command has completed, clean up the active endpoint count by
2024 * subtracting out the endpoints that were dropped (but not changed).
2025 *
2026 * Must be called with xhci->lock held.
2027 */
2028static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
2029 struct xhci_container_ctx *in_ctx)
2030{
2031 u32 num_dropped_eps;
2032
2033 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, in_ctx);
2034 xhci->num_active_eps -= num_dropped_eps;
2035 if (num_dropped_eps)
2036 xhci_dbg(xhci, "Removing %u dropped ep ctxs, %u now active.\n",
2037 num_dropped_eps,
2038 xhci->num_active_eps);
2039}
2040
2041unsigned int xhci_get_block_size(struct usb_device *udev)
2042{
2043 switch (udev->speed) {
2044 case USB_SPEED_LOW:
2045 case USB_SPEED_FULL:
2046 return FS_BLOCK;
2047 case USB_SPEED_HIGH:
2048 return HS_BLOCK;
2049 case USB_SPEED_SUPER:
2050 return SS_BLOCK;
2051 case USB_SPEED_UNKNOWN:
2052 case USB_SPEED_WIRELESS:
2053 default:
2054 /* Should never happen */
2055 return 1;
2056 }
2057}
2058
2059unsigned int xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
2060{
2061 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
2062 return LS_OVERHEAD;
2063 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2064 return FS_OVERHEAD;
2065 return HS_OVERHEAD;
2066}
2067
2068/* If we are changing a LS/FS device under a HS hub,
2069 * make sure (if we are activating a new TT) that the HS bus has enough
2070 * bandwidth for this new TT.
2071 */
2072static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2073 struct xhci_virt_device *virt_dev,
2074 int old_active_eps)
2075{
2076 struct xhci_interval_bw_table *bw_table;
2077 struct xhci_tt_bw_info *tt_info;
2078
2079 /* Find the bandwidth table for the root port this TT is attached to. */
2080 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2081 tt_info = virt_dev->tt_info;
2082 /* If this TT already had active endpoints, the bandwidth for this TT
2083 * has already been added. Removing all periodic endpoints (and thus
2084 * making the TT enactive) will only decrease the bandwidth used.
2085 */
2086 if (old_active_eps)
2087 return 0;
2088 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2089 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2090 return -ENOMEM;
2091 return 0;
2092 }
2093 /* Not sure why we would have no new active endpoints...
2094 *
2095 * Maybe because of an Evaluate Context change for a hub update or a
2096 * control endpoint 0 max packet size change?
2097 * FIXME: skip the bandwidth calculation in that case.
2098 */
2099 return 0;
2100}
2101
2102static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2103 struct xhci_virt_device *virt_dev)
2104{
2105 unsigned int bw_reserved;
2106
2107 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2108 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2109 return -ENOMEM;
2110
2111 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2112 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2113 return -ENOMEM;
2114
2115 return 0;
2116}
2117
2118/*
2119 * This algorithm is a very conservative estimate of the worst-case scheduling
2120 * scenario for any one interval. The hardware dynamically schedules the
2121 * packets, so we can't tell which microframe could be the limiting factor in
2122 * the bandwidth scheduling. This only takes into account periodic endpoints.
2123 *
2124 * Obviously, we can't solve an NP complete problem to find the minimum worst
2125 * case scenario. Instead, we come up with an estimate that is no less than
2126 * the worst case bandwidth used for any one microframe, but may be an
2127 * over-estimate.
2128 *
2129 * We walk the requirements for each endpoint by interval, starting with the
2130 * smallest interval, and place packets in the schedule where there is only one
2131 * possible way to schedule packets for that interval. In order to simplify
2132 * this algorithm, we record the largest max packet size for each interval, and
2133 * assume all packets will be that size.
2134 *
2135 * For interval 0, we obviously must schedule all packets for each interval.
2136 * The bandwidth for interval 0 is just the amount of data to be transmitted
2137 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2138 * the number of packets).
2139 *
2140 * For interval 1, we have two possible microframes to schedule those packets
2141 * in. For this algorithm, if we can schedule the same number of packets for
2142 * each possible scheduling opportunity (each microframe), we will do so. The
2143 * remaining number of packets will be saved to be transmitted in the gaps in
2144 * the next interval's scheduling sequence.
2145 *
2146 * As we move those remaining packets to be scheduled with interval 2 packets,
2147 * we have to double the number of remaining packets to transmit. This is
2148 * because the intervals are actually powers of 2, and we would be transmitting
2149 * the previous interval's packets twice in this interval. We also have to be
2150 * sure that when we look at the largest max packet size for this interval, we
2151 * also look at the largest max packet size for the remaining packets and take
2152 * the greater of the two.
2153 *
2154 * The algorithm continues to evenly distribute packets in each scheduling
2155 * opportunity, and push the remaining packets out, until we get to the last
2156 * interval. Then those packets and their associated overhead are just added
2157 * to the bandwidth used.
2158 */
2159static int xhci_check_bw_table(struct xhci_hcd *xhci,
2160 struct xhci_virt_device *virt_dev,
2161 int old_active_eps)
2162{
2163 unsigned int bw_reserved;
2164 unsigned int max_bandwidth;
2165 unsigned int bw_used;
2166 unsigned int block_size;
2167 struct xhci_interval_bw_table *bw_table;
2168 unsigned int packet_size = 0;
2169 unsigned int overhead = 0;
2170 unsigned int packets_transmitted = 0;
2171 unsigned int packets_remaining = 0;
2172 unsigned int i;
2173
2174 if (virt_dev->udev->speed == USB_SPEED_SUPER)
2175 return xhci_check_ss_bw(xhci, virt_dev);
2176
2177 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2178 max_bandwidth = HS_BW_LIMIT;
2179 /* Convert percent of bus BW reserved to blocks reserved */
2180 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2181 } else {
2182 max_bandwidth = FS_BW_LIMIT;
2183 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2184 }
2185
2186 bw_table = virt_dev->bw_table;
2187 /* We need to translate the max packet size and max ESIT payloads into
2188 * the units the hardware uses.
2189 */
2190 block_size = xhci_get_block_size(virt_dev->udev);
2191
2192 /* If we are manipulating a LS/FS device under a HS hub, double check
2193 * that the HS bus has enough bandwidth if we are activing a new TT.
2194 */
2195 if (virt_dev->tt_info) {
2196 xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
2197 virt_dev->real_port);
2198 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2199 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2200 "newly activated TT.\n");
2201 return -ENOMEM;
2202 }
2203 xhci_dbg(xhci, "Recalculating BW for TT slot %u port %u\n",
2204 virt_dev->tt_info->slot_id,
2205 virt_dev->tt_info->ttport);
2206 } else {
2207 xhci_dbg(xhci, "Recalculating BW for rootport %u\n",
2208 virt_dev->real_port);
2209 }
2210
2211 /* Add in how much bandwidth will be used for interval zero, or the
2212 * rounded max ESIT payload + number of packets * largest overhead.
2213 */
2214 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2215 bw_table->interval_bw[0].num_packets *
2216 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2217
2218 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2219 unsigned int bw_added;
2220 unsigned int largest_mps;
2221 unsigned int interval_overhead;
2222
2223 /*
2224 * How many packets could we transmit in this interval?
2225 * If packets didn't fit in the previous interval, we will need
2226 * to transmit that many packets twice within this interval.
2227 */
2228 packets_remaining = 2 * packets_remaining +
2229 bw_table->interval_bw[i].num_packets;
2230
2231 /* Find the largest max packet size of this or the previous
2232 * interval.
2233 */
2234 if (list_empty(&bw_table->interval_bw[i].endpoints))
2235 largest_mps = 0;
2236 else {
2237 struct xhci_virt_ep *virt_ep;
2238 struct list_head *ep_entry;
2239
2240 ep_entry = bw_table->interval_bw[i].endpoints.next;
2241 virt_ep = list_entry(ep_entry,
2242 struct xhci_virt_ep, bw_endpoint_list);
2243 /* Convert to blocks, rounding up */
2244 largest_mps = DIV_ROUND_UP(
2245 virt_ep->bw_info.max_packet_size,
2246 block_size);
2247 }
2248 if (largest_mps > packet_size)
2249 packet_size = largest_mps;
2250
2251 /* Use the larger overhead of this or the previous interval. */
2252 interval_overhead = xhci_get_largest_overhead(
2253 &bw_table->interval_bw[i]);
2254 if (interval_overhead > overhead)
2255 overhead = interval_overhead;
2256
2257 /* How many packets can we evenly distribute across
2258 * (1 << (i + 1)) possible scheduling opportunities?
2259 */
2260 packets_transmitted = packets_remaining >> (i + 1);
2261
2262 /* Add in the bandwidth used for those scheduled packets */
2263 bw_added = packets_transmitted * (overhead + packet_size);
2264
2265 /* How many packets do we have remaining to transmit? */
2266 packets_remaining = packets_remaining % (1 << (i + 1));
2267
2268 /* What largest max packet size should those packets have? */
2269 /* If we've transmitted all packets, don't carry over the
2270 * largest packet size.
2271 */
2272 if (packets_remaining == 0) {
2273 packet_size = 0;
2274 overhead = 0;
2275 } else if (packets_transmitted > 0) {
2276 /* Otherwise if we do have remaining packets, and we've
2277 * scheduled some packets in this interval, take the
2278 * largest max packet size from endpoints with this
2279 * interval.
2280 */
2281 packet_size = largest_mps;
2282 overhead = interval_overhead;
2283 }
2284 /* Otherwise carry over packet_size and overhead from the last
2285 * time we had a remainder.
2286 */
2287 bw_used += bw_added;
2288 if (bw_used > max_bandwidth) {
2289 xhci_warn(xhci, "Not enough bandwidth. "
2290 "Proposed: %u, Max: %u\n",
2291 bw_used, max_bandwidth);
2292 return -ENOMEM;
2293 }
2294 }
2295 /*
2296 * Ok, we know we have some packets left over after even-handedly
2297 * scheduling interval 15. We don't know which microframes they will
2298 * fit into, so we over-schedule and say they will be scheduled every
2299 * microframe.
2300 */
2301 if (packets_remaining > 0)
2302 bw_used += overhead + packet_size;
2303
2304 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2305 unsigned int port_index = virt_dev->real_port - 1;
2306
2307 /* OK, we're manipulating a HS device attached to a
2308 * root port bandwidth domain. Include the number of active TTs
2309 * in the bandwidth used.
2310 */
2311 bw_used += TT_HS_OVERHEAD *
2312 xhci->rh_bw[port_index].num_active_tts;
2313 }
2314
2315 xhci_dbg(xhci, "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2316 "Available: %u " "percent\n",
2317 bw_used, max_bandwidth, bw_reserved,
2318 (max_bandwidth - bw_used - bw_reserved) * 100 /
2319 max_bandwidth);
2320
2321 bw_used += bw_reserved;
2322 if (bw_used > max_bandwidth) {
2323 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2324 bw_used, max_bandwidth);
2325 return -ENOMEM;
2326 }
2327
2328 bw_table->bw_used = bw_used;
2329 return 0;
2330}
2331
2332static bool xhci_is_async_ep(unsigned int ep_type)
2333{
2334 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2335 ep_type != ISOC_IN_EP &&
2336 ep_type != INT_IN_EP);
2337}
2338
2339static bool xhci_is_sync_in_ep(unsigned int ep_type)
2340{
2341 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
2342}
2343
2344static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2345{
2346 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2347
2348 if (ep_bw->ep_interval == 0)
2349 return SS_OVERHEAD_BURST +
2350 (ep_bw->mult * ep_bw->num_packets *
2351 (SS_OVERHEAD + mps));
2352 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2353 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2354 1 << ep_bw->ep_interval);
2355
2356}
2357
2358void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
2359 struct xhci_bw_info *ep_bw,
2360 struct xhci_interval_bw_table *bw_table,
2361 struct usb_device *udev,
2362 struct xhci_virt_ep *virt_ep,
2363 struct xhci_tt_bw_info *tt_info)
2364{
2365 struct xhci_interval_bw *interval_bw;
2366 int normalized_interval;
2367
2368 if (xhci_is_async_ep(ep_bw->type))
2369 return;
2370
2371 if (udev->speed == USB_SPEED_SUPER) {
2372 if (xhci_is_sync_in_ep(ep_bw->type))
2373 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2374 xhci_get_ss_bw_consumed(ep_bw);
2375 else
2376 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2377 xhci_get_ss_bw_consumed(ep_bw);
2378 return;
2379 }
2380
2381 /* SuperSpeed endpoints never get added to intervals in the table, so
2382 * this check is only valid for HS/FS/LS devices.
2383 */
2384 if (list_empty(&virt_ep->bw_endpoint_list))
2385 return;
2386 /* For LS/FS devices, we need to translate the interval expressed in
2387 * microframes to frames.
2388 */
2389 if (udev->speed == USB_SPEED_HIGH)
2390 normalized_interval = ep_bw->ep_interval;
2391 else
2392 normalized_interval = ep_bw->ep_interval - 3;
2393
2394 if (normalized_interval == 0)
2395 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2396 interval_bw = &bw_table->interval_bw[normalized_interval];
2397 interval_bw->num_packets -= ep_bw->num_packets;
2398 switch (udev->speed) {
2399 case USB_SPEED_LOW:
2400 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2401 break;
2402 case USB_SPEED_FULL:
2403 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2404 break;
2405 case USB_SPEED_HIGH:
2406 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2407 break;
2408 case USB_SPEED_SUPER:
2409 case USB_SPEED_UNKNOWN:
2410 case USB_SPEED_WIRELESS:
2411 /* Should never happen because only LS/FS/HS endpoints will get
2412 * added to the endpoint list.
2413 */
2414 return;
2415 }
2416 if (tt_info)
2417 tt_info->active_eps -= 1;
2418 list_del_init(&virt_ep->bw_endpoint_list);
2419}
2420
2421static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2422 struct xhci_bw_info *ep_bw,
2423 struct xhci_interval_bw_table *bw_table,
2424 struct usb_device *udev,
2425 struct xhci_virt_ep *virt_ep,
2426 struct xhci_tt_bw_info *tt_info)
2427{
2428 struct xhci_interval_bw *interval_bw;
2429 struct xhci_virt_ep *smaller_ep;
2430 int normalized_interval;
2431
2432 if (xhci_is_async_ep(ep_bw->type))
2433 return;
2434
2435 if (udev->speed == USB_SPEED_SUPER) {
2436 if (xhci_is_sync_in_ep(ep_bw->type))
2437 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2438 xhci_get_ss_bw_consumed(ep_bw);
2439 else
2440 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2441 xhci_get_ss_bw_consumed(ep_bw);
2442 return;
2443 }
2444
2445 /* For LS/FS devices, we need to translate the interval expressed in
2446 * microframes to frames.
2447 */
2448 if (udev->speed == USB_SPEED_HIGH)
2449 normalized_interval = ep_bw->ep_interval;
2450 else
2451 normalized_interval = ep_bw->ep_interval - 3;
2452
2453 if (normalized_interval == 0)
2454 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2455 interval_bw = &bw_table->interval_bw[normalized_interval];
2456 interval_bw->num_packets += ep_bw->num_packets;
2457 switch (udev->speed) {
2458 case USB_SPEED_LOW:
2459 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2460 break;
2461 case USB_SPEED_FULL:
2462 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2463 break;
2464 case USB_SPEED_HIGH:
2465 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2466 break;
2467 case USB_SPEED_SUPER:
2468 case USB_SPEED_UNKNOWN:
2469 case USB_SPEED_WIRELESS:
2470 /* Should never happen because only LS/FS/HS endpoints will get
2471 * added to the endpoint list.
2472 */
2473 return;
2474 }
2475
2476 if (tt_info)
2477 tt_info->active_eps += 1;
2478 /* Insert the endpoint into the list, largest max packet size first. */
2479 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2480 bw_endpoint_list) {
2481 if (ep_bw->max_packet_size >=
2482 smaller_ep->bw_info.max_packet_size) {
2483 /* Add the new ep before the smaller endpoint */
2484 list_add_tail(&virt_ep->bw_endpoint_list,
2485 &smaller_ep->bw_endpoint_list);
2486 return;
2487 }
2488 }
2489 /* Add the new endpoint at the end of the list. */
2490 list_add_tail(&virt_ep->bw_endpoint_list,
2491 &interval_bw->endpoints);
2492}
2493
2494void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2495 struct xhci_virt_device *virt_dev,
2496 int old_active_eps)
2497{
2498 struct xhci_root_port_bw_info *rh_bw_info;
2499 if (!virt_dev->tt_info)
2500 return;
2501
2502 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2503 if (old_active_eps == 0 &&
2504 virt_dev->tt_info->active_eps != 0) {
2505 rh_bw_info->num_active_tts += 1;
2506 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
2507 } else if (old_active_eps != 0 &&
2508 virt_dev->tt_info->active_eps == 0) {
2509 rh_bw_info->num_active_tts -= 1;
2510 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
2511 }
2512}
2513
2514static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2515 struct xhci_virt_device *virt_dev,
2516 struct xhci_container_ctx *in_ctx)
2517{
2518 struct xhci_bw_info ep_bw_info[31];
2519 int i;
2520 struct xhci_input_control_ctx *ctrl_ctx;
2521 int old_active_eps = 0;
2522
2523 if (virt_dev->tt_info)
2524 old_active_eps = virt_dev->tt_info->active_eps;
2525
2526 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2527
2528 for (i = 0; i < 31; i++) {
2529 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2530 continue;
2531
2532 /* Make a copy of the BW info in case we need to revert this */
2533 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2534 sizeof(ep_bw_info[i]));
2535 /* Drop the endpoint from the interval table if the endpoint is
2536 * being dropped or changed.
2537 */
2538 if (EP_IS_DROPPED(ctrl_ctx, i))
2539 xhci_drop_ep_from_interval_table(xhci,
2540 &virt_dev->eps[i].bw_info,
2541 virt_dev->bw_table,
2542 virt_dev->udev,
2543 &virt_dev->eps[i],
2544 virt_dev->tt_info);
2545 }
2546 /* Overwrite the information stored in the endpoints' bw_info */
2547 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2548 for (i = 0; i < 31; i++) {
2549 /* Add any changed or added endpoints to the interval table */
2550 if (EP_IS_ADDED(ctrl_ctx, i))
2551 xhci_add_ep_to_interval_table(xhci,
2552 &virt_dev->eps[i].bw_info,
2553 virt_dev->bw_table,
2554 virt_dev->udev,
2555 &virt_dev->eps[i],
2556 virt_dev->tt_info);
2557 }
2558
2559 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2560 /* Ok, this fits in the bandwidth we have.
2561 * Update the number of active TTs.
2562 */
2563 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2564 return 0;
2565 }
2566
2567 /* We don't have enough bandwidth for this, revert the stored info. */
2568 for (i = 0; i < 31; i++) {
2569 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2570 continue;
2571
2572 /* Drop the new copies of any added or changed endpoints from
2573 * the interval table.
2574 */
2575 if (EP_IS_ADDED(ctrl_ctx, i)) {
2576 xhci_drop_ep_from_interval_table(xhci,
2577 &virt_dev->eps[i].bw_info,
2578 virt_dev->bw_table,
2579 virt_dev->udev,
2580 &virt_dev->eps[i],
2581 virt_dev->tt_info);
2582 }
2583 /* Revert the endpoint back to its old information */
2584 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2585 sizeof(ep_bw_info[i]));
2586 /* Add any changed or dropped endpoints back into the table */
2587 if (EP_IS_DROPPED(ctrl_ctx, i))
2588 xhci_add_ep_to_interval_table(xhci,
2589 &virt_dev->eps[i].bw_info,
2590 virt_dev->bw_table,
2591 virt_dev->udev,
2592 &virt_dev->eps[i],
2593 virt_dev->tt_info);
2594 }
2595 return -ENOMEM;
2596}
2597
2598
2599/* Issue a configure endpoint command or evaluate context command
2600 * and wait for it to finish.
2601 */
2602static int xhci_configure_endpoint(struct xhci_hcd *xhci,
2603 struct usb_device *udev,
2604 struct xhci_command *command,
2605 bool ctx_change, bool must_succeed)
2606{
2607 int ret;
2608 int timeleft;
2609 unsigned long flags;
2610 struct xhci_container_ctx *in_ctx;
2611 struct completion *cmd_completion;
2612 u32 *cmd_status;
2613 struct xhci_virt_device *virt_dev;
2614 union xhci_trb *cmd_trb;
2615
2616 spin_lock_irqsave(&xhci->lock, flags);
2617 virt_dev = xhci->devs[udev->slot_id];
2618
2619 if (command)
2620 in_ctx = command->in_ctx;
2621 else
2622 in_ctx = virt_dev->in_ctx;
2623
2624 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
2625 xhci_reserve_host_resources(xhci, in_ctx)) {
2626 spin_unlock_irqrestore(&xhci->lock, flags);
2627 xhci_warn(xhci, "Not enough host resources, "
2628 "active endpoint contexts = %u\n",
2629 xhci->num_active_eps);
2630 return -ENOMEM;
2631 }
2632 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
2633 xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
2634 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2635 xhci_free_host_resources(xhci, in_ctx);
2636 spin_unlock_irqrestore(&xhci->lock, flags);
2637 xhci_warn(xhci, "Not enough bandwidth\n");
2638 return -ENOMEM;
2639 }
2640
2641 if (command) {
2642 cmd_completion = command->completion;
2643 cmd_status = &command->status;
2644 command->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
2645 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
2646 } else {
2647 cmd_completion = &virt_dev->cmd_completion;
2648 cmd_status = &virt_dev->cmd_status;
2649 }
2650 init_completion(cmd_completion);
2651
2652 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
2653 if (!ctx_change)
2654 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
2655 udev->slot_id, must_succeed);
2656 else
2657 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
2658 udev->slot_id);
2659 if (ret < 0) {
2660 if (command)
2661 list_del(&command->cmd_list);
2662 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
2663 xhci_free_host_resources(xhci, in_ctx);
2664 spin_unlock_irqrestore(&xhci->lock, flags);
2665 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
2666 return -ENOMEM;
2667 }
2668 xhci_ring_cmd_db(xhci);
2669 spin_unlock_irqrestore(&xhci->lock, flags);
2670
2671 /* Wait for the configure endpoint command to complete */
2672 timeleft = wait_for_completion_interruptible_timeout(
2673 cmd_completion,
2674 XHCI_CMD_DEFAULT_TIMEOUT);
2675 if (timeleft <= 0) {
2676 xhci_warn(xhci, "%s while waiting for %s command\n",
2677 timeleft == 0 ? "Timeout" : "Signal",
2678 ctx_change == 0 ?
2679 "configure endpoint" :
2680 "evaluate context");
2681 /* cancel the configure endpoint command */
2682 ret = xhci_cancel_cmd(xhci, command, cmd_trb);
2683 if (ret < 0)
2684 return ret;
2685 return -ETIME;
2686 }
2687
2688 if (!ctx_change)
2689 ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
2690 else
2691 ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
2692
2693 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2694 spin_lock_irqsave(&xhci->lock, flags);
2695 /* If the command failed, remove the reserved resources.
2696 * Otherwise, clean up the estimate to include dropped eps.
2697 */
2698 if (ret)
2699 xhci_free_host_resources(xhci, in_ctx);
2700 else
2701 xhci_finish_resource_reservation(xhci, in_ctx);
2702 spin_unlock_irqrestore(&xhci->lock, flags);
2703 }
2704 return ret;
2705}
2706
2707/* Called after one or more calls to xhci_add_endpoint() or
2708 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2709 * to call xhci_reset_bandwidth().
2710 *
2711 * Since we are in the middle of changing either configuration or
2712 * installing a new alt setting, the USB core won't allow URBs to be
2713 * enqueued for any endpoint on the old config or interface. Nothing
2714 * else should be touching the xhci->devs[slot_id] structure, so we
2715 * don't need to take the xhci->lock for manipulating that.
2716 */
2717int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2718{
2719 int i;
2720 int ret = 0;
2721 struct xhci_hcd *xhci;
2722 struct xhci_virt_device *virt_dev;
2723 struct xhci_input_control_ctx *ctrl_ctx;
2724 struct xhci_slot_ctx *slot_ctx;
2725
2726 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
2727 if (ret <= 0)
2728 return ret;
2729 xhci = hcd_to_xhci(hcd);
2730 if (xhci->xhc_state & XHCI_STATE_DYING)
2731 return -ENODEV;
2732
2733 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
2734 virt_dev = xhci->devs[udev->slot_id];
2735
2736 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
2737 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2738 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2739 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2740 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
2741
2742 /* Don't issue the command if there's no endpoints to update. */
2743 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
2744 ctrl_ctx->drop_flags == 0)
2745 return 0;
2746
2747 xhci_dbg(xhci, "New Input Control Context:\n");
2748 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
2749 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
2750 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
2751
2752 ret = xhci_configure_endpoint(xhci, udev, NULL,
2753 false, false);
2754 if (ret) {
2755 /* Callee should call reset_bandwidth() */
2756 return ret;
2757 }
2758
2759 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
2760 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
2761 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
2762
2763 /* Free any rings that were dropped, but not changed. */
2764 for (i = 1; i < 31; ++i) {
2765 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
2766 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
2767 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2768 }
2769 xhci_zero_in_ctx(xhci, virt_dev);
2770 /*
2771 * Install any rings for completely new endpoints or changed endpoints,
2772 * and free or cache any old rings from changed endpoints.
2773 */
2774 for (i = 1; i < 31; ++i) {
2775 if (!virt_dev->eps[i].new_ring)
2776 continue;
2777 /* Only cache or free the old ring if it exists.
2778 * It may not if this is the first add of an endpoint.
2779 */
2780 if (virt_dev->eps[i].ring) {
2781 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2782 }
2783 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2784 virt_dev->eps[i].new_ring = NULL;
2785 }
2786
2787 return ret;
2788}
2789
2790void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2791{
2792 struct xhci_hcd *xhci;
2793 struct xhci_virt_device *virt_dev;
2794 int i, ret;
2795
2796 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
2797 if (ret <= 0)
2798 return;
2799 xhci = hcd_to_xhci(hcd);
2800
2801 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
2802 virt_dev = xhci->devs[udev->slot_id];
2803 /* Free any rings allocated for added endpoints */
2804 for (i = 0; i < 31; ++i) {
2805 if (virt_dev->eps[i].new_ring) {
2806 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2807 virt_dev->eps[i].new_ring = NULL;
2808 }
2809 }
2810 xhci_zero_in_ctx(xhci, virt_dev);
2811}
2812
2813static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
2814 struct xhci_container_ctx *in_ctx,
2815 struct xhci_container_ctx *out_ctx,
2816 u32 add_flags, u32 drop_flags)
2817{
2818 struct xhci_input_control_ctx *ctrl_ctx;
2819 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2820 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2821 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
2822 xhci_slot_copy(xhci, in_ctx, out_ctx);
2823 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2824
2825 xhci_dbg(xhci, "Input Context:\n");
2826 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
2827}
2828
2829static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
2830 unsigned int slot_id, unsigned int ep_index,
2831 struct xhci_dequeue_state *deq_state)
2832{
2833 struct xhci_container_ctx *in_ctx;
2834 struct xhci_ep_ctx *ep_ctx;
2835 u32 added_ctxs;
2836 dma_addr_t addr;
2837
2838 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2839 xhci->devs[slot_id]->out_ctx, ep_index);
2840 in_ctx = xhci->devs[slot_id]->in_ctx;
2841 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2842 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2843 deq_state->new_deq_ptr);
2844 if (addr == 0) {
2845 xhci_warn(xhci, "WARN Cannot submit config ep after "
2846 "reset ep command\n");
2847 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2848 deq_state->new_deq_seg,
2849 deq_state->new_deq_ptr);
2850 return;
2851 }
2852 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
2853
2854 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
2855 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
2856 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
2857}
2858
2859void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
2860 struct usb_device *udev, unsigned int ep_index)
2861{
2862 struct xhci_dequeue_state deq_state;
2863 struct xhci_virt_ep *ep;
2864
2865 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
2866 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
2867 /* We need to move the HW's dequeue pointer past this TD,
2868 * or it will attempt to resend it on the next doorbell ring.
2869 */
2870 xhci_find_new_dequeue_state(xhci, udev->slot_id,
2871 ep_index, ep->stopped_stream, ep->stopped_td,
2872 &deq_state);
2873
2874 /* HW with the reset endpoint quirk will use the saved dequeue state to
2875 * issue a configure endpoint command later.
2876 */
2877 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
2878 xhci_dbg(xhci, "Queueing new dequeue state\n");
2879 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
2880 ep_index, ep->stopped_stream, &deq_state);
2881 } else {
2882 /* Better hope no one uses the input context between now and the
2883 * reset endpoint completion!
2884 * XXX: No idea how this hardware will react when stream rings
2885 * are enabled.
2886 */
2887 xhci_dbg(xhci, "Setting up input context for "
2888 "configure endpoint command\n");
2889 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2890 ep_index, &deq_state);
2891 }
2892}
2893
2894/* Called when clearing halted device. The core should have sent the control
2895 * message to clear the device halt condition. The host side of the halt should
2896 * already be cleared with a reset endpoint command issued when the STALL tx
2897 * event was received.
2898 *
2899 * Context: in_interrupt
2900 */
2901
2902void xhci_endpoint_reset(struct usb_hcd *hcd,
2903 struct usb_host_endpoint *ep)
2904{
2905 struct xhci_hcd *xhci;
2906
2907 xhci = hcd_to_xhci(hcd);
2908 /*
2909 * We might need to implement the config ep cmd in xhci 4.8.1 note:
2910 * The Reset Endpoint Command may only be issued to endpoints in the
2911 * Halted state. If software wishes reset the Data Toggle or Sequence
2912 * Number of an endpoint that isn't in the Halted state, then software
2913 * may issue a Configure Endpoint Command with the Drop and Add bits set
2914 * for the target endpoint. that is in the Stopped state.
2915 */
2916 /* For now just print debug to follow the situation */
2917 xhci_dbg(xhci, "Endpoint 0x%x ep reset callback called\n",
2918 ep->desc.bEndpointAddress);
2919}
2920
2921static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2922 struct usb_device *udev, struct usb_host_endpoint *ep,
2923 unsigned int slot_id)
2924{
2925 int ret;
2926 unsigned int ep_index;
2927 unsigned int ep_state;
2928
2929 if (!ep)
2930 return -EINVAL;
2931 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
2932 if (ret <= 0)
2933 return -EINVAL;
2934 if (ep->ss_ep_comp.bmAttributes == 0) {
2935 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
2936 " descriptor for ep 0x%x does not support streams\n",
2937 ep->desc.bEndpointAddress);
2938 return -EINVAL;
2939 }
2940
2941 ep_index = xhci_get_endpoint_index(&ep->desc);
2942 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
2943 if (ep_state & EP_HAS_STREAMS ||
2944 ep_state & EP_GETTING_STREAMS) {
2945 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
2946 "already has streams set up.\n",
2947 ep->desc.bEndpointAddress);
2948 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
2949 "dynamic stream context array reallocation.\n");
2950 return -EINVAL;
2951 }
2952 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
2953 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
2954 "endpoint 0x%x; URBs are pending.\n",
2955 ep->desc.bEndpointAddress);
2956 return -EINVAL;
2957 }
2958 return 0;
2959}
2960
2961static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
2962 unsigned int *num_streams, unsigned int *num_stream_ctxs)
2963{
2964 unsigned int max_streams;
2965
2966 /* The stream context array size must be a power of two */
2967 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
2968 /*
2969 * Find out how many primary stream array entries the host controller
2970 * supports. Later we may use secondary stream arrays (similar to 2nd
2971 * level page entries), but that's an optional feature for xHCI host
2972 * controllers. xHCs must support at least 4 stream IDs.
2973 */
2974 max_streams = HCC_MAX_PSA(xhci->hcc_params);
2975 if (*num_stream_ctxs > max_streams) {
2976 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
2977 max_streams);
2978 *num_stream_ctxs = max_streams;
2979 *num_streams = max_streams;
2980 }
2981}
2982
2983/* Returns an error code if one of the endpoint already has streams.
2984 * This does not change any data structures, it only checks and gathers
2985 * information.
2986 */
2987static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
2988 struct usb_device *udev,
2989 struct usb_host_endpoint **eps, unsigned int num_eps,
2990 unsigned int *num_streams, u32 *changed_ep_bitmask)
2991{
2992 unsigned int max_streams;
2993 unsigned int endpoint_flag;
2994 int i;
2995 int ret;
2996
2997 for (i = 0; i < num_eps; i++) {
2998 ret = xhci_check_streams_endpoint(xhci, udev,
2999 eps[i], udev->slot_id);
3000 if (ret < 0)
3001 return ret;
3002
3003 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
3004 if (max_streams < (*num_streams - 1)) {
3005 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
3006 eps[i]->desc.bEndpointAddress,
3007 max_streams);
3008 *num_streams = max_streams+1;
3009 }
3010
3011 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
3012 if (*changed_ep_bitmask & endpoint_flag)
3013 return -EINVAL;
3014 *changed_ep_bitmask |= endpoint_flag;
3015 }
3016 return 0;
3017}
3018
3019static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3020 struct usb_device *udev,
3021 struct usb_host_endpoint **eps, unsigned int num_eps)
3022{
3023 u32 changed_ep_bitmask = 0;
3024 unsigned int slot_id;
3025 unsigned int ep_index;
3026 unsigned int ep_state;
3027 int i;
3028
3029 slot_id = udev->slot_id;
3030 if (!xhci->devs[slot_id])
3031 return 0;
3032
3033 for (i = 0; i < num_eps; i++) {
3034 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3035 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3036 /* Are streams already being freed for the endpoint? */
3037 if (ep_state & EP_GETTING_NO_STREAMS) {
3038 xhci_warn(xhci, "WARN Can't disable streams for "
3039 "endpoint 0x%x\n, "
3040 "streams are being disabled already.",
3041 eps[i]->desc.bEndpointAddress);
3042 return 0;
3043 }
3044 /* Are there actually any streams to free? */
3045 if (!(ep_state & EP_HAS_STREAMS) &&
3046 !(ep_state & EP_GETTING_STREAMS)) {
3047 xhci_warn(xhci, "WARN Can't disable streams for "
3048 "endpoint 0x%x\n, "
3049 "streams are already disabled!",
3050 eps[i]->desc.bEndpointAddress);
3051 xhci_warn(xhci, "WARN xhci_free_streams() called "
3052 "with non-streams endpoint\n");
3053 return 0;
3054 }
3055 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3056 }
3057 return changed_ep_bitmask;
3058}
3059
3060/*
3061 * The USB device drivers use this function (though the HCD interface in USB
3062 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3063 * coordinate mass storage command queueing across multiple endpoints (basically
3064 * a stream ID == a task ID).
3065 *
3066 * Setting up streams involves allocating the same size stream context array
3067 * for each endpoint and issuing a configure endpoint command for all endpoints.
3068 *
3069 * Don't allow the call to succeed if one endpoint only supports one stream
3070 * (which means it doesn't support streams at all).
3071 *
3072 * Drivers may get less stream IDs than they asked for, if the host controller
3073 * hardware or endpoints claim they can't support the number of requested
3074 * stream IDs.
3075 */
3076int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
3077 struct usb_host_endpoint **eps, unsigned int num_eps,
3078 unsigned int num_streams, gfp_t mem_flags)
3079{
3080 int i, ret;
3081 struct xhci_hcd *xhci;
3082 struct xhci_virt_device *vdev;
3083 struct xhci_command *config_cmd;
3084 unsigned int ep_index;
3085 unsigned int num_stream_ctxs;
3086 unsigned long flags;
3087 u32 changed_ep_bitmask = 0;
3088
3089 if (!eps)
3090 return -EINVAL;
3091
3092 /* Add one to the number of streams requested to account for
3093 * stream 0 that is reserved for xHCI usage.
3094 */
3095 num_streams += 1;
3096 xhci = hcd_to_xhci(hcd);
3097 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3098 num_streams);
3099
3100 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
3101 if (!config_cmd) {
3102 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
3103 return -ENOMEM;
3104 }
3105
3106 /* Check to make sure all endpoints are not already configured for
3107 * streams. While we're at it, find the maximum number of streams that
3108 * all the endpoints will support and check for duplicate endpoints.
3109 */
3110 spin_lock_irqsave(&xhci->lock, flags);
3111 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3112 num_eps, &num_streams, &changed_ep_bitmask);
3113 if (ret < 0) {
3114 xhci_free_command(xhci, config_cmd);
3115 spin_unlock_irqrestore(&xhci->lock, flags);
3116 return ret;
3117 }
3118 if (num_streams <= 1) {
3119 xhci_warn(xhci, "WARN: endpoints can't handle "
3120 "more than one stream.\n");
3121 xhci_free_command(xhci, config_cmd);
3122 spin_unlock_irqrestore(&xhci->lock, flags);
3123 return -EINVAL;
3124 }
3125 vdev = xhci->devs[udev->slot_id];
3126 /* Mark each endpoint as being in transition, so
3127 * xhci_urb_enqueue() will reject all URBs.
3128 */
3129 for (i = 0; i < num_eps; i++) {
3130 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3131 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3132 }
3133 spin_unlock_irqrestore(&xhci->lock, flags);
3134
3135 /* Setup internal data structures and allocate HW data structures for
3136 * streams (but don't install the HW structures in the input context
3137 * until we're sure all memory allocation succeeded).
3138 */
3139 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3140 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3141 num_stream_ctxs, num_streams);
3142
3143 for (i = 0; i < num_eps; i++) {
3144 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3145 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3146 num_stream_ctxs,
3147 num_streams, mem_flags);
3148 if (!vdev->eps[ep_index].stream_info)
3149 goto cleanup;
3150 /* Set maxPstreams in endpoint context and update deq ptr to
3151 * point to stream context array. FIXME
3152 */
3153 }
3154
3155 /* Set up the input context for a configure endpoint command. */
3156 for (i = 0; i < num_eps; i++) {
3157 struct xhci_ep_ctx *ep_ctx;
3158
3159 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3160 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3161
3162 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3163 vdev->out_ctx, ep_index);
3164 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3165 vdev->eps[ep_index].stream_info);
3166 }
3167 /* Tell the HW to drop its old copy of the endpoint context info
3168 * and add the updated copy from the input context.
3169 */
3170 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
3171 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
3172
3173 /* Issue and wait for the configure endpoint command */
3174 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3175 false, false);
3176
3177 /* xHC rejected the configure endpoint command for some reason, so we
3178 * leave the old ring intact and free our internal streams data
3179 * structure.
3180 */
3181 if (ret < 0)
3182 goto cleanup;
3183
3184 spin_lock_irqsave(&xhci->lock, flags);
3185 for (i = 0; i < num_eps; i++) {
3186 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3187 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3188 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3189 udev->slot_id, ep_index);
3190 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3191 }
3192 xhci_free_command(xhci, config_cmd);
3193 spin_unlock_irqrestore(&xhci->lock, flags);
3194
3195 /* Subtract 1 for stream 0, which drivers can't use */
3196 return num_streams - 1;
3197
3198cleanup:
3199 /* If it didn't work, free the streams! */
3200 for (i = 0; i < num_eps; i++) {
3201 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3202 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
3203 vdev->eps[ep_index].stream_info = NULL;
3204 /* FIXME Unset maxPstreams in endpoint context and
3205 * update deq ptr to point to normal string ring.
3206 */
3207 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3208 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3209 xhci_endpoint_zero(xhci, vdev, eps[i]);
3210 }
3211 xhci_free_command(xhci, config_cmd);
3212 return -ENOMEM;
3213}
3214
3215/* Transition the endpoint from using streams to being a "normal" endpoint
3216 * without streams.
3217 *
3218 * Modify the endpoint context state, submit a configure endpoint command,
3219 * and free all endpoint rings for streams if that completes successfully.
3220 */
3221int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
3222 struct usb_host_endpoint **eps, unsigned int num_eps,
3223 gfp_t mem_flags)
3224{
3225 int i, ret;
3226 struct xhci_hcd *xhci;
3227 struct xhci_virt_device *vdev;
3228 struct xhci_command *command;
3229 unsigned int ep_index;
3230 unsigned long flags;
3231 u32 changed_ep_bitmask;
3232
3233 xhci = hcd_to_xhci(hcd);
3234 vdev = xhci->devs[udev->slot_id];
3235
3236 /* Set up a configure endpoint command to remove the streams rings */
3237 spin_lock_irqsave(&xhci->lock, flags);
3238 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3239 udev, eps, num_eps);
3240 if (changed_ep_bitmask == 0) {
3241 spin_unlock_irqrestore(&xhci->lock, flags);
3242 return -EINVAL;
3243 }
3244
3245 /* Use the xhci_command structure from the first endpoint. We may have
3246 * allocated too many, but the driver may call xhci_free_streams() for
3247 * each endpoint it grouped into one call to xhci_alloc_streams().
3248 */
3249 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3250 command = vdev->eps[ep_index].stream_info->free_streams_command;
3251 for (i = 0; i < num_eps; i++) {
3252 struct xhci_ep_ctx *ep_ctx;
3253
3254 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3255 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3256 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3257 EP_GETTING_NO_STREAMS;
3258
3259 xhci_endpoint_copy(xhci, command->in_ctx,
3260 vdev->out_ctx, ep_index);
3261 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
3262 &vdev->eps[ep_index]);
3263 }
3264 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
3265 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
3266 spin_unlock_irqrestore(&xhci->lock, flags);
3267
3268 /* Issue and wait for the configure endpoint command,
3269 * which must succeed.
3270 */
3271 ret = xhci_configure_endpoint(xhci, udev, command,
3272 false, true);
3273
3274 /* xHC rejected the configure endpoint command for some reason, so we
3275 * leave the streams rings intact.
3276 */
3277 if (ret < 0)
3278 return ret;
3279
3280 spin_lock_irqsave(&xhci->lock, flags);
3281 for (i = 0; i < num_eps; i++) {
3282 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3283 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
3284 vdev->eps[ep_index].stream_info = NULL;
3285 /* FIXME Unset maxPstreams in endpoint context and
3286 * update deq ptr to point to normal string ring.
3287 */
3288 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3289 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3290 }
3291 spin_unlock_irqrestore(&xhci->lock, flags);
3292
3293 return 0;
3294}
3295
3296/*
3297 * Deletes endpoint resources for endpoints that were active before a Reset
3298 * Device command, or a Disable Slot command. The Reset Device command leaves
3299 * the control endpoint intact, whereas the Disable Slot command deletes it.
3300 *
3301 * Must be called with xhci->lock held.
3302 */
3303void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3304 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3305{
3306 int i;
3307 unsigned int num_dropped_eps = 0;
3308 unsigned int drop_flags = 0;
3309
3310 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3311 if (virt_dev->eps[i].ring) {
3312 drop_flags |= 1 << i;
3313 num_dropped_eps++;
3314 }
3315 }
3316 xhci->num_active_eps -= num_dropped_eps;
3317 if (num_dropped_eps)
3318 xhci_dbg(xhci, "Dropped %u ep ctxs, flags = 0x%x, "
3319 "%u now active.\n",
3320 num_dropped_eps, drop_flags,
3321 xhci->num_active_eps);
3322}
3323
3324/*
3325 * This submits a Reset Device Command, which will set the device state to 0,
3326 * set the device address to 0, and disable all the endpoints except the default
3327 * control endpoint. The USB core should come back and call
3328 * xhci_address_device(), and then re-set up the configuration. If this is
3329 * called because of a usb_reset_and_verify_device(), then the old alternate
3330 * settings will be re-installed through the normal bandwidth allocation
3331 * functions.
3332 *
3333 * Wait for the Reset Device command to finish. Remove all structures
3334 * associated with the endpoints that were disabled. Clear the input device
3335 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
3336 *
3337 * If the virt_dev to be reset does not exist or does not match the udev,
3338 * it means the device is lost, possibly due to the xHC restore error and
3339 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3340 * re-allocate the device.
3341 */
3342int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
3343{
3344 int ret, i;
3345 unsigned long flags;
3346 struct xhci_hcd *xhci;
3347 unsigned int slot_id;
3348 struct xhci_virt_device *virt_dev;
3349 struct xhci_command *reset_device_cmd;
3350 int timeleft;
3351 int last_freed_endpoint;
3352 struct xhci_slot_ctx *slot_ctx;
3353 int old_active_eps = 0;
3354
3355 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
3356 if (ret <= 0)
3357 return ret;
3358 xhci = hcd_to_xhci(hcd);
3359 slot_id = udev->slot_id;
3360 virt_dev = xhci->devs[slot_id];
3361 if (!virt_dev) {
3362 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3363 "not exist. Re-allocate the device\n", slot_id);
3364 ret = xhci_alloc_dev(hcd, udev);
3365 if (ret == 1)
3366 return 0;
3367 else
3368 return -EINVAL;
3369 }
3370
3371 if (virt_dev->udev != udev) {
3372 /* If the virt_dev and the udev does not match, this virt_dev
3373 * may belong to another udev.
3374 * Re-allocate the device.
3375 */
3376 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3377 "not match the udev. Re-allocate the device\n",
3378 slot_id);
3379 ret = xhci_alloc_dev(hcd, udev);
3380 if (ret == 1)
3381 return 0;
3382 else
3383 return -EINVAL;
3384 }
3385
3386 /* If device is not setup, there is no point in resetting it */
3387 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3388 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3389 SLOT_STATE_DISABLED)
3390 return 0;
3391
3392 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3393 /* Allocate the command structure that holds the struct completion.
3394 * Assume we're in process context, since the normal device reset
3395 * process has to wait for the device anyway. Storage devices are
3396 * reset as part of error handling, so use GFP_NOIO instead of
3397 * GFP_KERNEL.
3398 */
3399 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
3400 if (!reset_device_cmd) {
3401 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3402 return -ENOMEM;
3403 }
3404
3405 /* Attempt to submit the Reset Device command to the command ring */
3406 spin_lock_irqsave(&xhci->lock, flags);
3407 reset_device_cmd->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
3408
3409 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
3410 ret = xhci_queue_reset_device(xhci, slot_id);
3411 if (ret) {
3412 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3413 list_del(&reset_device_cmd->cmd_list);
3414 spin_unlock_irqrestore(&xhci->lock, flags);
3415 goto command_cleanup;
3416 }
3417 xhci_ring_cmd_db(xhci);
3418 spin_unlock_irqrestore(&xhci->lock, flags);
3419
3420 /* Wait for the Reset Device command to finish */
3421 timeleft = wait_for_completion_interruptible_timeout(
3422 reset_device_cmd->completion,
3423 USB_CTRL_SET_TIMEOUT);
3424 if (timeleft <= 0) {
3425 xhci_warn(xhci, "%s while waiting for reset device command\n",
3426 timeleft == 0 ? "Timeout" : "Signal");
3427 spin_lock_irqsave(&xhci->lock, flags);
3428 /* The timeout might have raced with the event ring handler, so
3429 * only delete from the list if the item isn't poisoned.
3430 */
3431 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
3432 list_del(&reset_device_cmd->cmd_list);
3433 spin_unlock_irqrestore(&xhci->lock, flags);
3434 ret = -ETIME;
3435 goto command_cleanup;
3436 }
3437
3438 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3439 * unless we tried to reset a slot ID that wasn't enabled,
3440 * or the device wasn't in the addressed or configured state.
3441 */
3442 ret = reset_device_cmd->status;
3443 switch (ret) {
3444 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
3445 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
3446 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
3447 slot_id,
3448 xhci_get_slot_state(xhci, virt_dev->out_ctx));
3449 xhci_info(xhci, "Not freeing device rings.\n");
3450 /* Don't treat this as an error. May change my mind later. */
3451 ret = 0;
3452 goto command_cleanup;
3453 case COMP_SUCCESS:
3454 xhci_dbg(xhci, "Successful reset device command.\n");
3455 break;
3456 default:
3457 if (xhci_is_vendor_info_code(xhci, ret))
3458 break;
3459 xhci_warn(xhci, "Unknown completion code %u for "
3460 "reset device command.\n", ret);
3461 ret = -EINVAL;
3462 goto command_cleanup;
3463 }
3464
3465 /* Free up host controller endpoint resources */
3466 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3467 spin_lock_irqsave(&xhci->lock, flags);
3468 /* Don't delete the default control endpoint resources */
3469 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3470 spin_unlock_irqrestore(&xhci->lock, flags);
3471 }
3472
3473 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
3474 last_freed_endpoint = 1;
3475 for (i = 1; i < 31; ++i) {
3476 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3477
3478 if (ep->ep_state & EP_HAS_STREAMS) {
3479 xhci_free_stream_info(xhci, ep->stream_info);
3480 ep->stream_info = NULL;
3481 ep->ep_state &= ~EP_HAS_STREAMS;
3482 }
3483
3484 if (ep->ring) {
3485 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
3486 last_freed_endpoint = i;
3487 }
3488 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3489 xhci_drop_ep_from_interval_table(xhci,
3490 &virt_dev->eps[i].bw_info,
3491 virt_dev->bw_table,
3492 udev,
3493 &virt_dev->eps[i],
3494 virt_dev->tt_info);
3495 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
3496 }
3497 /* If necessary, update the number of active TTs on this root port */
3498 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
3499
3500 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
3501 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
3502 ret = 0;
3503
3504command_cleanup:
3505 xhci_free_command(xhci, reset_device_cmd);
3506 return ret;
3507}
3508
3509/*
3510 * At this point, the struct usb_device is about to go away, the device has
3511 * disconnected, and all traffic has been stopped and the endpoints have been
3512 * disabled. Free any HC data structures associated with that device.
3513 */
3514void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
3515{
3516 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3517 struct xhci_virt_device *virt_dev;
3518 struct device *dev = hcd->self.controller;
3519 unsigned long flags;
3520 u32 state;
3521 int i, ret;
3522
3523#ifndef CONFIG_USB_DEFAULT_PERSIST
3524 /*
3525 * We called pm_runtime_get_noresume when the device was attached.
3526 * Decrement the counter here to allow controller to runtime suspend
3527 * if no devices remain.
3528 */
3529 if (xhci->quirks & XHCI_RESET_ON_RESUME)
3530 pm_runtime_put_noidle(dev);
3531#endif
3532
3533 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
3534 /* If the host is halted due to driver unload, we still need to free the
3535 * device.
3536 */
3537 if (ret <= 0 && ret != -ENODEV)
3538 return;
3539
3540 virt_dev = xhci->devs[udev->slot_id];
3541
3542 /* Stop any wayward timer functions (which may grab the lock) */
3543 for (i = 0; i < 31; ++i) {
3544 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
3545 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3546 }
3547
3548 if (udev->usb2_hw_lpm_enabled) {
3549 xhci_set_usb2_hardware_lpm(hcd, udev, 0);
3550 udev->usb2_hw_lpm_enabled = 0;
3551 }
3552
3553 spin_lock_irqsave(&xhci->lock, flags);
3554 /* Don't disable the slot if the host controller is dead. */
3555 state = xhci_readl(xhci, &xhci->op_regs->status);
3556 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3557 (xhci->xhc_state & XHCI_STATE_HALTED)) {
3558 xhci_free_virt_device(xhci, udev->slot_id);
3559 spin_unlock_irqrestore(&xhci->lock, flags);
3560 return;
3561 }
3562
3563 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
3564 spin_unlock_irqrestore(&xhci->lock, flags);
3565 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3566 return;
3567 }
3568 xhci_ring_cmd_db(xhci);
3569 spin_unlock_irqrestore(&xhci->lock, flags);
3570 /*
3571 * Event command completion handler will free any data structures
3572 * associated with the slot. XXX Can free sleep?
3573 */
3574}
3575
3576/*
3577 * Checks if we have enough host controller resources for the default control
3578 * endpoint.
3579 *
3580 * Must be called with xhci->lock held.
3581 */
3582static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3583{
3584 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
3585 xhci_dbg(xhci, "Not enough ep ctxs: "
3586 "%u active, need to add 1, limit is %u.\n",
3587 xhci->num_active_eps, xhci->limit_active_eps);
3588 return -ENOMEM;
3589 }
3590 xhci->num_active_eps += 1;
3591 xhci_dbg(xhci, "Adding 1 ep ctx, %u now active.\n",
3592 xhci->num_active_eps);
3593 return 0;
3594}
3595
3596
3597/*
3598 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3599 * timed out, or allocating memory failed. Returns 1 on success.
3600 */
3601int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3602{
3603 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3604 struct device *dev = hcd->self.controller;
3605 unsigned long flags;
3606 int timeleft;
3607 int ret;
3608 union xhci_trb *cmd_trb;
3609
3610 spin_lock_irqsave(&xhci->lock, flags);
3611 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
3612 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
3613 if (ret) {
3614 spin_unlock_irqrestore(&xhci->lock, flags);
3615 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3616 return 0;
3617 }
3618 xhci_ring_cmd_db(xhci);
3619 spin_unlock_irqrestore(&xhci->lock, flags);
3620
3621 /* XXX: how much time for xHC slot assignment? */
3622 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
3623 XHCI_CMD_DEFAULT_TIMEOUT);
3624 if (timeleft <= 0) {
3625 xhci_warn(xhci, "%s while waiting for a slot\n",
3626 timeleft == 0 ? "Timeout" : "Signal");
3627 /* cancel the enable slot request */
3628 return xhci_cancel_cmd(xhci, NULL, cmd_trb);
3629 }
3630
3631 if (!xhci->slot_id) {
3632 xhci_err(xhci, "Error while assigning device slot ID\n");
3633 return 0;
3634 }
3635
3636 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3637 spin_lock_irqsave(&xhci->lock, flags);
3638 ret = xhci_reserve_host_control_ep_resources(xhci);
3639 if (ret) {
3640 spin_unlock_irqrestore(&xhci->lock, flags);
3641 xhci_warn(xhci, "Not enough host resources, "
3642 "active endpoint contexts = %u\n",
3643 xhci->num_active_eps);
3644 goto disable_slot;
3645 }
3646 spin_unlock_irqrestore(&xhci->lock, flags);
3647 }
3648 /* Use GFP_NOIO, since this function can be called from
3649 * xhci_discover_or_reset_device(), which may be called as part of
3650 * mass storage driver error handling.
3651 */
3652 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
3653 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
3654 goto disable_slot;
3655 }
3656 udev->slot_id = xhci->slot_id;
3657
3658#ifndef CONFIG_USB_DEFAULT_PERSIST
3659 /*
3660 * If resetting upon resume, we can't put the controller into runtime
3661 * suspend if there is a device attached.
3662 */
3663 if (xhci->quirks & XHCI_RESET_ON_RESUME)
3664 pm_runtime_get_noresume(dev);
3665#endif
3666
3667 /* Is this a LS or FS device under a HS hub? */
3668 /* Hub or peripherial? */
3669 return 1;
3670
3671disable_slot:
3672 /* Disable slot, if we can do it without mem alloc */
3673 spin_lock_irqsave(&xhci->lock, flags);
3674 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
3675 xhci_ring_cmd_db(xhci);
3676 spin_unlock_irqrestore(&xhci->lock, flags);
3677 return 0;
3678}
3679
3680/*
3681 * Issue an Address Device command (which will issue a SetAddress request to
3682 * the device).
3683 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
3684 * we should only issue and wait on one address command at the same time.
3685 *
3686 * We add one to the device address issued by the hardware because the USB core
3687 * uses address 1 for the root hubs (even though they're not really devices).
3688 */
3689int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
3690{
3691 unsigned long flags;
3692 int timeleft;
3693 struct xhci_virt_device *virt_dev;
3694 int ret = 0;
3695 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3696 struct xhci_slot_ctx *slot_ctx;
3697 struct xhci_input_control_ctx *ctrl_ctx;
3698 u64 temp_64;
3699 union xhci_trb *cmd_trb;
3700
3701 if (!udev->slot_id) {
3702 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
3703 return -EINVAL;
3704 }
3705
3706 virt_dev = xhci->devs[udev->slot_id];
3707
3708 if (WARN_ON(!virt_dev)) {
3709 /*
3710 * In plug/unplug torture test with an NEC controller,
3711 * a zero-dereference was observed once due to virt_dev = 0.
3712 * Print useful debug rather than crash if it is observed again!
3713 */
3714 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3715 udev->slot_id);
3716 return -EINVAL;
3717 }
3718
3719 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
3720 /*
3721 * If this is the first Set Address since device plug-in or
3722 * virt_device realloaction after a resume with an xHCI power loss,
3723 * then set up the slot context.
3724 */
3725 if (!slot_ctx->dev_info)
3726 xhci_setup_addressable_virt_dev(xhci, udev);
3727 /* Otherwise, update the control endpoint ring enqueue pointer. */
3728 else
3729 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
3730 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
3731 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3732 ctrl_ctx->drop_flags = 0;
3733
3734 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
3735 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
3736
3737 spin_lock_irqsave(&xhci->lock, flags);
3738 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
3739 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
3740 udev->slot_id);
3741 if (ret) {
3742 spin_unlock_irqrestore(&xhci->lock, flags);
3743 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3744 return ret;
3745 }
3746 xhci_ring_cmd_db(xhci);
3747 spin_unlock_irqrestore(&xhci->lock, flags);
3748
3749 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
3750 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
3751 XHCI_CMD_DEFAULT_TIMEOUT);
3752 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3753 * the SetAddress() "recovery interval" required by USB and aborting the
3754 * command on a timeout.
3755 */
3756 if (timeleft <= 0) {
3757 xhci_warn(xhci, "%s while waiting for address device command\n",
3758 timeleft == 0 ? "Timeout" : "Signal");
3759 /* cancel the address device command */
3760 ret = xhci_cancel_cmd(xhci, NULL, cmd_trb);
3761 if (ret < 0)
3762 return ret;
3763 return -ETIME;
3764 }
3765
3766 switch (virt_dev->cmd_status) {
3767 case COMP_CTX_STATE:
3768 case COMP_EBADSLT:
3769 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
3770 udev->slot_id);
3771 ret = -EINVAL;
3772 break;
3773 case COMP_TX_ERR:
3774 dev_warn(&udev->dev, "Device not responding to set address.\n");
3775 ret = -EPROTO;
3776 break;
3777 case COMP_DEV_ERR:
3778 dev_warn(&udev->dev, "ERROR: Incompatible device for address "
3779 "device command.\n");
3780 ret = -ENODEV;
3781 break;
3782 case COMP_SUCCESS:
3783 xhci_dbg(xhci, "Successful Address Device command\n");
3784 break;
3785 default:
3786 xhci_err(xhci, "ERROR: unexpected command completion "
3787 "code 0x%x.\n", virt_dev->cmd_status);
3788 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
3789 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
3790 ret = -EINVAL;
3791 break;
3792 }
3793 if (ret) {
3794 return ret;
3795 }
3796 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
3797 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
3798 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
3799 udev->slot_id,
3800 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
3801 (unsigned long long)
3802 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
3803 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
3804 (unsigned long long)virt_dev->out_ctx->dma);
3805 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
3806 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
3807 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
3808 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
3809 /*
3810 * USB core uses address 1 for the roothubs, so we add one to the
3811 * address given back to us by the HC.
3812 */
3813 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3814 /* Use kernel assigned address for devices; store xHC assigned
3815 * address locally. */
3816 virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
3817 + 1;
3818 /* Zero the input context control for later use */
3819 ctrl_ctx->add_flags = 0;
3820 ctrl_ctx->drop_flags = 0;
3821
3822 xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
3823
3824 return 0;
3825}
3826
3827#ifdef CONFIG_USB_SUSPEND
3828
3829/* BESL to HIRD Encoding array for USB2 LPM */
3830static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
3831 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
3832
3833/* Calculate HIRD/BESL for USB2 PORTPMSC*/
3834static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
3835 struct usb_device *udev)
3836{
3837 int u2del, besl, besl_host;
3838 int besl_device = 0;
3839 u32 field;
3840
3841 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
3842 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
3843
3844 if (field & USB_BESL_SUPPORT) {
3845 for (besl_host = 0; besl_host < 16; besl_host++) {
3846 if (xhci_besl_encoding[besl_host] >= u2del)
3847 break;
3848 }
3849 /* Use baseline BESL value as default */
3850 if (field & USB_BESL_BASELINE_VALID)
3851 besl_device = USB_GET_BESL_BASELINE(field);
3852 else if (field & USB_BESL_DEEP_VALID)
3853 besl_device = USB_GET_BESL_DEEP(field);
3854 } else {
3855 if (u2del <= 50)
3856 besl_host = 0;
3857 else
3858 besl_host = (u2del - 51) / 75 + 1;
3859 }
3860
3861 besl = besl_host + besl_device;
3862 if (besl > 15)
3863 besl = 15;
3864
3865 return besl;
3866}
3867
3868static int xhci_usb2_software_lpm_test(struct usb_hcd *hcd,
3869 struct usb_device *udev)
3870{
3871 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3872 struct dev_info *dev_info;
3873 __le32 __iomem **port_array;
3874 __le32 __iomem *addr, *pm_addr;
3875 u32 temp, dev_id;
3876 unsigned int port_num;
3877 unsigned long flags;
3878 int hird;
3879 int ret;
3880
3881 if (hcd->speed == HCD_USB3 || !xhci->sw_lpm_support ||
3882 !udev->lpm_capable)
3883 return -EINVAL;
3884
3885 /* we only support lpm for non-hub device connected to root hub yet */
3886 if (!udev->parent || udev->parent->parent ||
3887 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
3888 return -EINVAL;
3889
3890 spin_lock_irqsave(&xhci->lock, flags);
3891
3892 /* Look for devices in lpm_failed_devs list */
3893 dev_id = le16_to_cpu(udev->descriptor.idVendor) << 16 |
3894 le16_to_cpu(udev->descriptor.idProduct);
3895 list_for_each_entry(dev_info, &xhci->lpm_failed_devs, list) {
3896 if (dev_info->dev_id == dev_id) {
3897 ret = -EINVAL;
3898 goto finish;
3899 }
3900 }
3901
3902 port_array = xhci->usb2_ports;
3903 port_num = udev->portnum - 1;
3904
3905 if (port_num > HCS_MAX_PORTS(xhci->hcs_params1)) {
3906 xhci_dbg(xhci, "invalid port number %d\n", udev->portnum);
3907 ret = -EINVAL;
3908 goto finish;
3909 }
3910
3911 /*
3912 * Test USB 2.0 software LPM.
3913 * FIXME: some xHCI 1.0 hosts may implement a new register to set up
3914 * hardware-controlled USB 2.0 LPM. See section 5.4.11 and 4.23.5.1.1.1
3915 * in the June 2011 errata release.
3916 */
3917 xhci_dbg(xhci, "test port %d software LPM\n", port_num);
3918 /*
3919 * Set L1 Device Slot and HIRD/BESL.
3920 * Check device's USB 2.0 extension descriptor to determine whether
3921 * HIRD or BESL shoule be used. See USB2.0 LPM errata.
3922 */
3923 pm_addr = port_array[port_num] + 1;
3924 hird = xhci_calculate_hird_besl(xhci, udev);
3925 temp = PORT_L1DS(udev->slot_id) | PORT_HIRD(hird);
3926 xhci_writel(xhci, temp, pm_addr);
3927
3928 /* Set port link state to U2(L1) */
3929 addr = port_array[port_num];
3930 xhci_set_link_state(xhci, port_array, port_num, XDEV_U2);
3931
3932 /* wait for ACK */
3933 spin_unlock_irqrestore(&xhci->lock, flags);
3934 msleep(10);
3935 spin_lock_irqsave(&xhci->lock, flags);
3936
3937 /* Check L1 Status */
3938 ret = handshake(xhci, pm_addr, PORT_L1S_MASK, PORT_L1S_SUCCESS, 125);
3939 if (ret != -ETIMEDOUT) {
3940 /* enter L1 successfully */
3941 temp = xhci_readl(xhci, addr);
3942 xhci_dbg(xhci, "port %d entered L1 state, port status 0x%x\n",
3943 port_num, temp);
3944 ret = 0;
3945 } else {
3946 temp = xhci_readl(xhci, pm_addr);
3947 xhci_dbg(xhci, "port %d software lpm failed, L1 status %d\n",
3948 port_num, temp & PORT_L1S_MASK);
3949 ret = -EINVAL;
3950 }
3951
3952 /* Resume the port */
3953 xhci_set_link_state(xhci, port_array, port_num, XDEV_U0);
3954
3955 spin_unlock_irqrestore(&xhci->lock, flags);
3956 msleep(10);
3957 spin_lock_irqsave(&xhci->lock, flags);
3958
3959 /* Clear PLC */
3960 xhci_test_and_clear_bit(xhci, port_array, port_num, PORT_PLC);
3961
3962 /* Check PORTSC to make sure the device is in the right state */
3963 if (!ret) {
3964 temp = xhci_readl(xhci, addr);
3965 xhci_dbg(xhci, "resumed port %d status 0x%x\n", port_num, temp);
3966 if (!(temp & PORT_CONNECT) || !(temp & PORT_PE) ||
3967 (temp & PORT_PLS_MASK) != XDEV_U0) {
3968 xhci_dbg(xhci, "port L1 resume fail\n");
3969 ret = -EINVAL;
3970 }
3971 }
3972
3973 if (ret) {
3974 /* Insert dev to lpm_failed_devs list */
3975 xhci_warn(xhci, "device LPM test failed, may disconnect and "
3976 "re-enumerate\n");
3977 dev_info = kzalloc(sizeof(struct dev_info), GFP_ATOMIC);
3978 if (!dev_info) {
3979 ret = -ENOMEM;
3980 goto finish;
3981 }
3982 dev_info->dev_id = dev_id;
3983 INIT_LIST_HEAD(&dev_info->list);
3984 list_add(&dev_info->list, &xhci->lpm_failed_devs);
3985 } else {
3986 xhci_ring_device(xhci, udev->slot_id);
3987 }
3988
3989finish:
3990 spin_unlock_irqrestore(&xhci->lock, flags);
3991 return ret;
3992}
3993
3994int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
3995 struct usb_device *udev, int enable)
3996{
3997 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3998 __le32 __iomem **port_array;
3999 __le32 __iomem *pm_addr;
4000 u32 temp;
4001 unsigned int port_num;
4002 unsigned long flags;
4003 int hird;
4004
4005 if (hcd->speed == HCD_USB3 || !xhci->hw_lpm_support ||
4006 !udev->lpm_capable)
4007 return -EPERM;
4008
4009 if (!udev->parent || udev->parent->parent ||
4010 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4011 return -EPERM;
4012
4013 if (udev->usb2_hw_lpm_capable != 1)
4014 return -EPERM;
4015
4016 spin_lock_irqsave(&xhci->lock, flags);
4017
4018 port_array = xhci->usb2_ports;
4019 port_num = udev->portnum - 1;
4020 pm_addr = port_array[port_num] + 1;
4021 temp = xhci_readl(xhci, pm_addr);
4022
4023 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
4024 enable ? "enable" : "disable", port_num);
4025
4026 hird = xhci_calculate_hird_besl(xhci, udev);
4027
4028 if (enable) {
4029 temp &= ~PORT_HIRD_MASK;
4030 temp |= PORT_HIRD(hird) | PORT_RWE;
4031 xhci_writel(xhci, temp, pm_addr);
4032 temp = xhci_readl(xhci, pm_addr);
4033 temp |= PORT_HLE;
4034 xhci_writel(xhci, temp, pm_addr);
4035 } else {
4036 temp &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK);
4037 xhci_writel(xhci, temp, pm_addr);
4038 }
4039
4040 spin_unlock_irqrestore(&xhci->lock, flags);
4041 return 0;
4042}
4043
4044int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4045{
4046 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4047 int ret;
4048
4049 ret = xhci_usb2_software_lpm_test(hcd, udev);
4050 if (!ret) {
4051 xhci_dbg(xhci, "software LPM test succeed\n");
4052 if (xhci->hw_lpm_support == 1) {
4053 udev->usb2_hw_lpm_capable = 1;
4054 ret = xhci_set_usb2_hardware_lpm(hcd, udev, 1);
4055 if (!ret)
4056 udev->usb2_hw_lpm_enabled = 1;
4057 }
4058 }
4059
4060 return 0;
4061}
4062
4063#else
4064
4065int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4066 struct usb_device *udev, int enable)
4067{
4068 return 0;
4069}
4070
4071int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4072{
4073 return 0;
4074}
4075
4076#endif /* CONFIG_USB_SUSPEND */
4077
4078/* Once a hub descriptor is fetched for a device, we need to update the xHC's
4079 * internal data structures for the device.
4080 */
4081int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
4082 struct usb_tt *tt, gfp_t mem_flags)
4083{
4084 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4085 struct xhci_virt_device *vdev;
4086 struct xhci_command *config_cmd;
4087 struct xhci_input_control_ctx *ctrl_ctx;
4088 struct xhci_slot_ctx *slot_ctx;
4089 unsigned long flags;
4090 unsigned think_time;
4091 int ret;
4092
4093 /* Ignore root hubs */
4094 if (!hdev->parent)
4095 return 0;
4096
4097 vdev = xhci->devs[hdev->slot_id];
4098 if (!vdev) {
4099 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4100 return -EINVAL;
4101 }
4102 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
4103 if (!config_cmd) {
4104 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
4105 return -ENOMEM;
4106 }
4107
4108 spin_lock_irqsave(&xhci->lock, flags);
4109 if (hdev->speed == USB_SPEED_HIGH &&
4110 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4111 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4112 xhci_free_command(xhci, config_cmd);
4113 spin_unlock_irqrestore(&xhci->lock, flags);
4114 return -ENOMEM;
4115 }
4116
4117 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
4118 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
4119 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
4120 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
4121 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
4122 if (tt->multi)
4123 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
4124 if (xhci->hci_version > 0x95) {
4125 xhci_dbg(xhci, "xHCI version %x needs hub "
4126 "TT think time and number of ports\n",
4127 (unsigned int) xhci->hci_version);
4128 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
4129 /* Set TT think time - convert from ns to FS bit times.
4130 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4131 * 2 = 24 FS bit times, 3 = 32 FS bit times.
4132 *
4133 * xHCI 1.0: this field shall be 0 if the device is not a
4134 * High-spped hub.
4135 */
4136 think_time = tt->think_time;
4137 if (think_time != 0)
4138 think_time = (think_time / 666) - 1;
4139 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4140 slot_ctx->tt_info |=
4141 cpu_to_le32(TT_THINK_TIME(think_time));
4142 } else {
4143 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4144 "TT think time or number of ports\n",
4145 (unsigned int) xhci->hci_version);
4146 }
4147 slot_ctx->dev_state = 0;
4148 spin_unlock_irqrestore(&xhci->lock, flags);
4149
4150 xhci_dbg(xhci, "Set up %s for hub device.\n",
4151 (xhci->hci_version > 0x95) ?
4152 "configure endpoint" : "evaluate context");
4153 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
4154 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
4155
4156 /* Issue and wait for the configure endpoint or
4157 * evaluate context command.
4158 */
4159 if (xhci->hci_version > 0x95)
4160 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4161 false, false);
4162 else
4163 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4164 true, false);
4165
4166 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
4167 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
4168
4169 xhci_free_command(xhci, config_cmd);
4170 return ret;
4171}
4172
4173int xhci_get_frame(struct usb_hcd *hcd)
4174{
4175 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4176 /* EHCI mods by the periodic size. Why? */
4177 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
4178}
4179
4180int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4181{
4182 struct xhci_hcd *xhci;
4183 struct device *dev = hcd->self.controller;
4184 int retval;
4185 u32 temp;
4186
4187 /* Accept arbitrarily long scatter-gather lists */
4188 hcd->self.sg_tablesize = ~0;
4189
4190 if (usb_hcd_is_primary_hcd(hcd)) {
4191 xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
4192 if (!xhci)
4193 return -ENOMEM;
4194 *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
4195 xhci->main_hcd = hcd;
4196 /* Mark the first roothub as being USB 2.0.
4197 * The xHCI driver will register the USB 3.0 roothub.
4198 */
4199 hcd->speed = HCD_USB2;
4200 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4201 /*
4202 * USB 2.0 roothub under xHCI has an integrated TT,
4203 * (rate matching hub) as opposed to having an OHCI/UHCI
4204 * companion controller.
4205 */
4206 hcd->has_tt = 1;
4207 } else {
4208 /* xHCI private pointer was set in xhci_pci_probe for the second
4209 * registered roothub.
4210 */
4211 xhci = hcd_to_xhci(hcd);
4212 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4213 if (HCC_64BIT_ADDR(temp)) {
4214 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
4215 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
4216 } else {
4217 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
4218 }
4219 return 0;
4220 }
4221
4222 xhci->cap_regs = hcd->regs;
4223 xhci->op_regs = hcd->regs +
4224 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
4225 xhci->run_regs = hcd->regs +
4226 (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
4227 /* Cache read-only capability registers */
4228 xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
4229 xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
4230 xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
4231 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
4232 xhci->hci_version = HC_VERSION(xhci->hcc_params);
4233 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4234 xhci_print_registers(xhci);
4235
4236 get_quirks(dev, xhci);
4237
4238 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4239 * success event after a short transfer. This quirk will ignore such
4240 * spurious event.
4241 */
4242 if (xhci->hci_version > 0x96)
4243 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
4244
4245 /* Make sure the HC is halted. */
4246 retval = xhci_halt(xhci);
4247 if (retval)
4248 goto error;
4249
4250 xhci_dbg(xhci, "Resetting HCD\n");
4251 /* Reset the internal HC memory state and registers. */
4252 retval = xhci_reset(xhci);
4253 if (retval)
4254 goto error;
4255 xhci_dbg(xhci, "Reset complete\n");
4256
4257 temp = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4258 if (HCC_64BIT_ADDR(temp)) {
4259 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
4260 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(64));
4261 } else {
4262 dma_set_mask(hcd->self.controller, DMA_BIT_MASK(32));
4263 }
4264
4265 xhci_dbg(xhci, "Calling HCD init\n");
4266 /* Initialize HCD and host controller data structures. */
4267 retval = xhci_init(hcd);
4268 if (retval)
4269 goto error;
4270 xhci_dbg(xhci, "Called HCD init\n");
4271 return 0;
4272error:
4273 kfree(xhci);
4274 return retval;
4275}
4276
4277MODULE_DESCRIPTION(DRIVER_DESC);
4278MODULE_AUTHOR(DRIVER_AUTHOR);
4279MODULE_LICENSE("GPL");
4280
4281static int __init xhci_hcd_init(void)
4282{
4283 int retval;
4284
4285 retval = xhci_register_pci();
4286 if (retval < 0) {
4287 printk(KERN_DEBUG "Problem registering PCI driver.");
4288 return retval;
4289 }
4290 retval = xhci_register_plat();
4291 if (retval < 0) {
4292 printk(KERN_DEBUG "Problem registering platform driver.");
4293 goto unreg_pci;
4294 }
4295 /*
4296 * Check the compiler generated sizes of structures that must be laid
4297 * out in specific ways for hardware access.
4298 */
4299 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
4300 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
4301 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
4302 /* xhci_device_control has eight fields, and also
4303 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
4304 */
4305 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
4306 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
4307 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
4308 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
4309 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
4310 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
4311 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
4312 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
4313 return 0;
4314unreg_pci:
4315 xhci_unregister_pci();
4316 return retval;
4317}
4318module_init(xhci_hcd_init);
4319
4320static void __exit xhci_hcd_cleanup(void)
4321{
4322 xhci_unregister_pci();
4323 xhci_unregister_plat();
4324}
4325module_exit(xhci_hcd_cleanup);