blob: 6d3e298a81748fdb84c3e42d1b68ea6dacf744c1 [file] [log] [blame]
Sarah Sharp66d4ead2009-04-27 19:52:28 -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
Dong Nguyen43b86af2010-07-21 16:56:08 -070023#include <linux/pci.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070024#include <linux/irq.h>
Sarah Sharp8df75f42010-04-02 15:34:16 -070025#include <linux/log2.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070026#include <linux/module.h>
Sarah Sharpb0567b32009-08-07 14:04:36 -070027#include <linux/moduleparam.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090028#include <linux/slab.h>
Alexis R. Cortes71c731a2012-08-03 14:00:27 -050029#include <linux/dmi.h>
James Hogan008eb952013-07-26 13:34:43 +010030#include <linux/dma-mapping.h>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070031
32#include "xhci.h"
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +030033#include "xhci-trace.h"
Sarah Sharp66d4ead2009-04-27 19:52:28 -070034
35#define DRIVER_AUTHOR "Sarah Sharp"
36#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
37
Sarah Sharpb0567b32009-08-07 14:04:36 -070038/* 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
Sarah Sharp66d4ead2009-04-27 19:52:28 -070043/* TODO: copied from ehci-hcd.c - can this be refactored? */
44/*
Sarah Sharp2611bd182012-10-25 13:27:51 -070045 * xhci_handshake - spin reading hc until handshake completes or fails
Sarah Sharp66d4ead2009-04-27 19:52:28 -070046 * @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 */
Sarah Sharp2611bd182012-10-25 13:27:51 -070057int xhci_handshake(struct xhci_hcd *xhci, void __iomem *ptr,
Sarah Sharp66d4ead2009-04-27 19:52:28 -070058 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/*
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070076 * 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/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -070095 * 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
Andiry Xubdfca502011-01-06 15:43:39 +080099 * should halt within 16 ms of the run/stop bit being cleared.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700100 * Read HC Halted bit in the status register to see when the HC is finished.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700101 */
102int xhci_halt(struct xhci_hcd *xhci)
103{
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800104 int ret;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300105 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Halt the HC");
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -0700106 xhci_quiesce(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700107
Sarah Sharp2611bd182012-10-25 13:27:51 -0700108 ret = xhci_handshake(xhci, &xhci->op_regs->status,
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700109 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
Elric Fuc181bc52012-06-27 16:30:57 +0800110 if (!ret) {
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800111 xhci->xhc_state |= XHCI_STATE_HALTED;
Elric Fuc181bc52012-06-27 16:30:57 +0800112 xhci->cmd_ring_state = CMD_RING_STATE_STOPPED;
113 } else
Sarah Sharp5af98bb2012-03-16 12:58:20 -0700114 xhci_warn(xhci, "Host not halted after %u microseconds.\n",
115 XHCI_MAX_HALT_USEC);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800116 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700117}
118
119/*
Sarah Sharped074532010-05-24 13:25:21 -0700120 * Set the run bit and wait for the host to be running.
121 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -0800122static int xhci_start(struct xhci_hcd *xhci)
Sarah Sharped074532010-05-24 13:25:21 -0700123{
124 u32 temp;
125 int ret;
126
127 temp = xhci_readl(xhci, &xhci->op_regs->command);
128 temp |= (CMD_RUN);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300129 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Turn on HC, cmd = 0x%x.",
Sarah Sharped074532010-05-24 13:25:21 -0700130 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 */
Sarah Sharp2611bd182012-10-25 13:27:51 -0700137 ret = xhci_handshake(xhci, &xhci->op_regs->status,
Sarah Sharped074532010-05-24 13:25:21 -0700138 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);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -0800143 if (!ret)
144 xhci->xhc_state &= ~XHCI_STATE_HALTED;
Sarah Sharped074532010-05-24 13:25:21 -0700145 return ret;
146}
147
148/*
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800149 * Reset a halted HC.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700150 *
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;
Andiry Xuf370b992012-04-14 02:54:30 +0800159 int ret, i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700160
161 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700162 if ((state & STS_HALT) == 0) {
163 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
164 return 0;
165 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700166
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300167 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Reset the HC");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700168 command = xhci_readl(xhci, &xhci->op_regs->command);
169 command |= CMD_RESET;
170 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700171
Sarah Sharp2611bd182012-10-25 13:27:51 -0700172 ret = xhci_handshake(xhci, &xhci->op_regs->command,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700173 CMD_RESET, 0, 10 * 1000 * 1000);
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700174 if (ret)
175 return ret;
176
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300177 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
178 "Wait for controller to be ready for doorbell rings");
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700179 /*
180 * xHCI cannot write to any doorbells or operational registers other
181 * than status until the "Controller Not Ready" flag is cleared.
182 */
Sarah Sharp2611bd182012-10-25 13:27:51 -0700183 ret = xhci_handshake(xhci, &xhci->op_regs->status,
Sarah Sharp22ceac12012-07-23 16:06:08 -0700184 STS_CNR, 0, 10 * 1000 * 1000);
Andiry Xuf370b992012-04-14 02:54:30 +0800185
186 for (i = 0; i < 2; ++i) {
187 xhci->bus_state[i].port_c_suspend = 0;
188 xhci->bus_state[i].suspended_ports = 0;
189 xhci->bus_state[i].resuming_ports = 0;
190 }
191
192 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700193}
194
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700195#ifdef CONFIG_PCI
196static int xhci_free_msi(struct xhci_hcd *xhci)
Dong Nguyen43b86af2010-07-21 16:56:08 -0700197{
198 int i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700199
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700200 if (!xhci->msix_entries)
201 return -EINVAL;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700202
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700203 for (i = 0; i < xhci->msix_count; i++)
204 if (xhci->msix_entries[i].vector)
205 free_irq(xhci->msix_entries[i].vector,
206 xhci_to_hcd(xhci));
207 return 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700208}
209
210/*
211 * Set up MSI
212 */
213static int xhci_setup_msi(struct xhci_hcd *xhci)
214{
215 int ret;
216 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
217
218 ret = pci_enable_msi(pdev);
219 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300220 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
221 "failed to allocate MSI entry");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700222 return ret;
223 }
224
Alex Shi851ec162013-05-24 10:54:19 +0800225 ret = request_irq(pdev->irq, xhci_msi_irq,
Dong Nguyen43b86af2010-07-21 16:56:08 -0700226 0, "xhci_hcd", xhci_to_hcd(xhci));
227 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300228 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
229 "disable MSI interrupt");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700230 pci_disable_msi(pdev);
231 }
232
233 return ret;
234}
235
236/*
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700237 * Free IRQs
238 * free all IRQs request
239 */
240static void xhci_free_irq(struct xhci_hcd *xhci)
241{
242 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
243 int ret;
244
245 /* return if using legacy interrupt */
Felipe Balbicd704692012-02-29 16:46:23 +0200246 if (xhci_to_hcd(xhci)->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700247 return;
248
249 ret = xhci_free_msi(xhci);
250 if (!ret)
251 return;
Felipe Balbicd704692012-02-29 16:46:23 +0200252 if (pdev->irq > 0)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700253 free_irq(pdev->irq, xhci_to_hcd(xhci));
254
255 return;
256}
257
258/*
Dong Nguyen43b86af2010-07-21 16:56:08 -0700259 * Set up MSI-X
260 */
261static int xhci_setup_msix(struct xhci_hcd *xhci)
262{
263 int i, ret = 0;
Andiry Xu00292272010-12-27 17:39:02 +0800264 struct usb_hcd *hcd = xhci_to_hcd(xhci);
265 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700266
267 /*
268 * calculate number of msi-x vectors supported.
269 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
270 * with max number of interrupters based on the xhci HCSPARAMS1.
271 * - num_online_cpus: maximum msi-x vectors per CPUs core.
272 * Add additional 1 vector to ensure always available interrupt.
273 */
274 xhci->msix_count = min(num_online_cpus() + 1,
275 HCS_MAX_INTRS(xhci->hcs_params1));
276
277 xhci->msix_entries =
278 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
Greg Kroah-Hartman86871972010-11-11 09:41:02 -0800279 GFP_KERNEL);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700280 if (!xhci->msix_entries) {
281 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
282 return -ENOMEM;
283 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700284
285 for (i = 0; i < xhci->msix_count; i++) {
286 xhci->msix_entries[i].entry = i;
287 xhci->msix_entries[i].vector = 0;
288 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700289
290 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
291 if (ret) {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300292 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
293 "Failed to enable MSI-X");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700294 goto free_entries;
295 }
296
Dong Nguyen43b86af2010-07-21 16:56:08 -0700297 for (i = 0; i < xhci->msix_count; i++) {
298 ret = request_irq(xhci->msix_entries[i].vector,
Alex Shi851ec162013-05-24 10:54:19 +0800299 xhci_msi_irq,
Dong Nguyen43b86af2010-07-21 16:56:08 -0700300 0, "xhci_hcd", xhci_to_hcd(xhci));
301 if (ret)
302 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700303 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700304
Andiry Xu00292272010-12-27 17:39:02 +0800305 hcd->msix_enabled = 1;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700306 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700307
308disable_msix:
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300309 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "disable MSI-X interrupt");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700310 xhci_free_irq(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700311 pci_disable_msix(pdev);
312free_entries:
313 kfree(xhci->msix_entries);
314 xhci->msix_entries = NULL;
315 return ret;
316}
317
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700318/* Free any IRQs and disable MSI-X */
319static void xhci_cleanup_msix(struct xhci_hcd *xhci)
320{
Andiry Xu00292272010-12-27 17:39:02 +0800321 struct usb_hcd *hcd = xhci_to_hcd(xhci);
322 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700323
Dong Nguyen43b86af2010-07-21 16:56:08 -0700324 xhci_free_irq(xhci);
325
326 if (xhci->msix_entries) {
327 pci_disable_msix(pdev);
328 kfree(xhci->msix_entries);
329 xhci->msix_entries = NULL;
330 } else {
331 pci_disable_msi(pdev);
332 }
333
Andiry Xu00292272010-12-27 17:39:02 +0800334 hcd->msix_enabled = 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700335 return;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700336}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700337
Olof Johanssond5c82fe2013-07-23 11:58:20 -0700338static void __maybe_unused xhci_msix_sync_irqs(struct xhci_hcd *xhci)
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700339{
340 int i;
341
342 if (xhci->msix_entries) {
343 for (i = 0; i < xhci->msix_count; i++)
344 synchronize_irq(xhci->msix_entries[i].vector);
345 }
346}
347
348static int xhci_try_enable_msi(struct usb_hcd *hcd)
349{
350 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp52fb6122013-08-08 10:08:34 -0700351 struct pci_dev *pdev;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700352 int ret;
353
Sarah Sharp52fb6122013-08-08 10:08:34 -0700354 /* The xhci platform device has set up IRQs through usb_add_hcd. */
355 if (xhci->quirks & XHCI_PLAT)
356 return 0;
357
358 pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700359 /*
360 * Some Fresco Logic host controllers advertise MSI, but fail to
361 * generate interrupts. Don't even try to enable MSI.
362 */
363 if (xhci->quirks & XHCI_BROKEN_MSI)
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100364 goto legacy_irq;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700365
366 /* unregister the legacy interrupt */
367 if (hcd->irq)
368 free_irq(hcd->irq, hcd);
Felipe Balbicd704692012-02-29 16:46:23 +0200369 hcd->irq = 0;
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700370
371 ret = xhci_setup_msix(xhci);
372 if (ret)
373 /* fall back to msi*/
374 ret = xhci_setup_msi(xhci);
375
376 if (!ret)
Felipe Balbicd704692012-02-29 16:46:23 +0200377 /* hcd->irq is 0, we have MSI */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700378 return 0;
379
Sarah Sharp68d07f62012-02-13 16:25:57 -0800380 if (!pdev->irq) {
381 xhci_err(xhci, "No msi-x/msi found and no IRQ in BIOS\n");
382 return -EINVAL;
383 }
384
Hannes Reinecke00eed9c2013-03-04 17:14:43 +0100385 legacy_irq:
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700386 /* fall back to legacy interrupt*/
387 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
388 hcd->irq_descr, hcd);
389 if (ret) {
390 xhci_err(xhci, "request interrupt %d failed\n",
391 pdev->irq);
392 return ret;
393 }
394 hcd->irq = pdev->irq;
395 return 0;
396}
397
398#else
399
400static int xhci_try_enable_msi(struct usb_hcd *hcd)
401{
402 return 0;
403}
404
405static void xhci_cleanup_msix(struct xhci_hcd *xhci)
406{
407}
408
409static void xhci_msix_sync_irqs(struct xhci_hcd *xhci)
410{
411}
412
413#endif
414
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500415static void compliance_mode_recovery(unsigned long arg)
416{
417 struct xhci_hcd *xhci;
418 struct usb_hcd *hcd;
419 u32 temp;
420 int i;
421
422 xhci = (struct xhci_hcd *)arg;
423
424 for (i = 0; i < xhci->num_usb3_ports; i++) {
425 temp = xhci_readl(xhci, xhci->usb3_ports[i]);
426 if ((temp & PORT_PLS_MASK) == USB_SS_PORT_LS_COMP_MOD) {
427 /*
428 * Compliance Mode Detected. Letting USB Core
429 * handle the Warm Reset
430 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300431 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
432 "Compliance mode detected->port %d",
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500433 i + 1);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300434 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
435 "Attempting compliance mode recovery");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500436 hcd = xhci->shared_hcd;
437
438 if (hcd->state == HC_STATE_SUSPENDED)
439 usb_hcd_resume_root_hub(hcd);
440
441 usb_hcd_poll_rh_status(hcd);
442 }
443 }
444
445 if (xhci->port_status_u0 != ((1 << xhci->num_usb3_ports)-1))
446 mod_timer(&xhci->comp_mode_recovery_timer,
447 jiffies + msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
448}
449
450/*
451 * Quirk to work around issue generated by the SN65LVPE502CP USB3.0 re-driver
452 * that causes ports behind that hardware to enter compliance mode sometimes.
453 * The quirk creates a timer that polls every 2 seconds the link state of
454 * each host controller's port and recovers it by issuing a Warm reset
455 * if Compliance mode is detected, otherwise the port will become "dead" (no
456 * device connections or disconnections will be detected anymore). Becasue no
457 * status event is generated when entering compliance mode (per xhci spec),
458 * this quirk is needed on systems that have the failing hardware installed.
459 */
460static void compliance_mode_recovery_timer_init(struct xhci_hcd *xhci)
461{
462 xhci->port_status_u0 = 0;
463 init_timer(&xhci->comp_mode_recovery_timer);
464
465 xhci->comp_mode_recovery_timer.data = (unsigned long) xhci;
466 xhci->comp_mode_recovery_timer.function = compliance_mode_recovery;
467 xhci->comp_mode_recovery_timer.expires = jiffies +
468 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS);
469
470 set_timer_slack(&xhci->comp_mode_recovery_timer,
471 msecs_to_jiffies(COMP_MODE_RCVRY_MSECS));
472 add_timer(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300473 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
474 "Compliance mode recovery timer initialized");
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500475}
476
477/*
478 * This function identifies the systems that have installed the SN65LVPE502CP
479 * USB3.0 re-driver and that need the Compliance Mode Quirk.
480 * Systems:
481 * Vendor: Hewlett-Packard -> System Models: Z420, Z620 and Z820
482 */
Sarah Sharpc3897aa2013-04-18 10:02:03 -0700483bool xhci_compliance_mode_recovery_timer_quirk_check(void)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500484{
485 const char *dmi_product_name, *dmi_sys_vendor;
486
487 dmi_product_name = dmi_get_system_info(DMI_PRODUCT_NAME);
488 dmi_sys_vendor = dmi_get_system_info(DMI_SYS_VENDOR);
Vivek Gautam457a73d2012-09-22 18:11:19 +0530489 if (!dmi_product_name || !dmi_sys_vendor)
490 return false;
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500491
492 if (!(strstr(dmi_sys_vendor, "Hewlett-Packard")))
493 return false;
494
495 if (strstr(dmi_product_name, "Z420") ||
496 strstr(dmi_product_name, "Z620") ||
Alexis R. Cortes47080972012-10-17 14:09:12 -0500497 strstr(dmi_product_name, "Z820") ||
Alexis R. Cortesb0e4e602012-11-08 16:59:27 -0600498 strstr(dmi_product_name, "Z1 Workstation"))
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500499 return true;
500
501 return false;
502}
503
504static int xhci_all_ports_seen_u0(struct xhci_hcd *xhci)
505{
506 return (xhci->port_status_u0 == ((1 << xhci->num_usb3_ports)-1));
507}
508
509
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700510/*
511 * Initialize memory for HCD and xHC (one-time init).
512 *
513 * Program the PAGESIZE register, initialize the device context array, create
514 * device contexts (?), set up a command ring segment (or two?), create event
515 * ring (one for now).
516 */
517int xhci_init(struct usb_hcd *hcd)
518{
519 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
520 int retval = 0;
521
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300522 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700523 spin_lock_init(&xhci->lock);
Sebastian Andrzej Siewiord7826592011-09-13 16:41:10 -0700524 if (xhci->hci_version == 0x95 && link_quirk) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300525 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
526 "QUIRK: Not clearing Link TRB chain bits.");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700527 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
528 } else {
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300529 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
530 "xHCI doesn't need link TRB QUIRK");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700531 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700532 retval = xhci_mem_init(xhci, GFP_KERNEL);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300533 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Finished xhci_init");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700534
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500535 /* Initializing Compliance Mode Recovery Data If Needed */
Sarah Sharpc3897aa2013-04-18 10:02:03 -0700536 if (xhci_compliance_mode_recovery_timer_quirk_check()) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500537 xhci->quirks |= XHCI_COMP_MODE_QUIRK;
538 compliance_mode_recovery_timer_init(xhci);
539 }
540
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700541 return retval;
542}
543
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700544/*-------------------------------------------------------------------------*/
545
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700546
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800547static int xhci_run_finished(struct xhci_hcd *xhci)
548{
549 if (xhci_start(xhci)) {
550 xhci_halt(xhci);
551 return -ENODEV;
552 }
553 xhci->shared_hcd->state = HC_STATE_RUNNING;
Elric Fuc181bc52012-06-27 16:30:57 +0800554 xhci->cmd_ring_state = CMD_RING_STATE_RUNNING;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800555
556 if (xhci->quirks & XHCI_NEC_HOST)
557 xhci_ring_cmd_db(xhci);
558
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300559 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
560 "Finished xhci_run for USB3 roothub");
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800561 return 0;
562}
563
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700564/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700565 * Start the HC after it was halted.
566 *
567 * This function is called by the USB core when the HC driver is added.
568 * Its opposite is xhci_stop().
569 *
570 * xhci_init() must be called once before this function can be called.
571 * Reset the HC, enable device slot contexts, program DCBAAP, and
572 * set command ring pointer and event ring pointer.
573 *
574 * Setup MSI-X vectors and enable interrupts.
575 */
576int xhci_run(struct usb_hcd *hcd)
577{
578 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700579 u64 temp_64;
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700580 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700581 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700582
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800583 /* Start the xHCI host controller running only after the USB 2.0 roothub
584 * is setup.
585 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700586
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700587 hcd->uses_new_polling = 1;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800588 if (!usb_hcd_is_primary_hcd(hcd))
589 return xhci_run_finished(xhci);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700590
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300591 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "xhci_run");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700592
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700593 ret = xhci_try_enable_msi(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700594 if (ret)
Sebastian Andrzej Siewior3fd1ec52011-09-23 14:19:57 -0700595 return ret;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700596
Sarah Sharp66e49d82009-07-27 12:03:46 -0700597 xhci_dbg(xhci, "Command ring memory map follows:\n");
598 xhci_debug_ring(xhci, xhci->cmd_ring);
599 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
600 xhci_dbg_cmd_ptrs(xhci);
601
602 xhci_dbg(xhci, "ERST memory map follows:\n");
603 xhci_dbg_erst(xhci, &xhci->erst);
604 xhci_dbg(xhci, "Event ring:\n");
605 xhci_debug_ring(xhci, xhci->event_ring);
606 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
607 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
608 temp_64 &= ~ERST_PTR_MASK;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300609 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
610 "ERST deq = 64'h%0lx", (long unsigned int) temp_64);
Sarah Sharp66e49d82009-07-27 12:03:46 -0700611
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300612 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
613 "// Set the interrupt modulation register");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700614 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700615 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700616 temp |= (u32) 160;
617 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
618
619 /* Set the HCD state before we enable the irqs */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700620 temp = xhci_readl(xhci, &xhci->op_regs->command);
621 temp |= (CMD_EIE);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300622 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
623 "// Enable interrupts, cmd = 0x%x.", temp);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700624 xhci_writel(xhci, temp, &xhci->op_regs->command);
625
626 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300627 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
628 "// Enabling event ring interrupter %p by writing 0x%x to irq_pending",
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700629 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700630 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
631 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800632 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700633
Sarah Sharp02386342010-05-24 13:25:28 -0700634 if (xhci->quirks & XHCI_NEC_HOST)
635 xhci_queue_vendor_command(xhci, 0, 0, 0,
636 TRB_TYPE(TRB_NEC_GET_FW));
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700637
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300638 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
639 "Finished xhci_run for USB2 roothub");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700640 return 0;
641}
642
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800643static void xhci_only_stop_hcd(struct usb_hcd *hcd)
644{
645 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
646
647 spin_lock_irq(&xhci->lock);
648 xhci_halt(xhci);
649
650 /* The shared_hcd is going to be deallocated shortly (the USB core only
651 * calls this function when allocation fails in usb_add_hcd(), or
652 * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
653 */
654 xhci->shared_hcd = NULL;
655 spin_unlock_irq(&xhci->lock);
656}
657
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700658/*
659 * Stop xHCI driver.
660 *
661 * This function is called by the USB core when the HC driver is removed.
662 * Its opposite is xhci_run().
663 *
664 * Disable device contexts, disable IRQs, and quiesce the HC.
665 * Reset the HC, finish any completed transactions, and cleanup memory.
666 */
667void xhci_stop(struct usb_hcd *hcd)
668{
669 u32 temp;
670 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
671
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800672 if (!usb_hcd_is_primary_hcd(hcd)) {
673 xhci_only_stop_hcd(xhci->shared_hcd);
674 return;
675 }
676
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700677 spin_lock_irq(&xhci->lock);
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800678 /* Make sure the xHC is halted for a USB3 roothub
679 * (xhci_stop() could be called as part of failed init).
680 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700681 xhci_halt(xhci);
682 xhci_reset(xhci);
683 spin_unlock_irq(&xhci->lock);
684
Zhang Rui40a9fb12010-12-17 13:17:04 -0800685 xhci_cleanup_msix(xhci);
686
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500687 /* Deleting Compliance Mode Recovery Timer */
688 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
Tony Camuso58b1d792013-04-05 14:27:07 -0400689 (!(xhci_all_ports_seen_u0(xhci)))) {
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500690 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300691 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
692 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -0400693 __func__);
694 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500695
Andiry Xuc41136b2011-03-22 17:08:14 +0800696 if (xhci->quirks & XHCI_AMD_PLL_FIX)
697 usb_amd_dev_put();
698
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300699 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
700 "// Disabling event ring interrupts");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700701 temp = xhci_readl(xhci, &xhci->op_regs->status);
702 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
703 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
704 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
705 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800706 xhci_print_ir_set(xhci, 0);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700707
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300708 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "cleaning up memory");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700709 xhci_mem_cleanup(xhci);
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300710 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
711 "xhci_stop completed - status = %x",
712 xhci_readl(xhci, &xhci->op_regs->status));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700713}
714
715/*
716 * Shutdown HC (not bus-specific)
717 *
718 * This is called when the machine is rebooting or halting. We assume that the
719 * machine will be powered off, and the HC's internal state will be reset.
720 * Don't bother to free memory.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800721 *
722 * This will only ever be called with the main usb_hcd (the USB3 roothub).
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700723 */
724void xhci_shutdown(struct usb_hcd *hcd)
725{
726 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
727
Dan Carpenter052c7f92012-08-13 19:57:03 +0300728 if (xhci->quirks & XHCI_SPURIOUS_REBOOT)
Sarah Sharpe95829f2012-07-23 18:59:30 +0300729 usb_disable_xhci_ports(to_pci_dev(hcd->self.controller));
730
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700731 spin_lock_irq(&xhci->lock);
732 xhci_halt(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700733 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700734
Zhang Rui40a9fb12010-12-17 13:17:04 -0800735 xhci_cleanup_msix(xhci);
736
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300737 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
738 "xhci_shutdown completed - status = %x",
739 xhci_readl(xhci, &xhci->op_regs->status));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700740}
741
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700742#ifdef CONFIG_PM
Andiry Xu5535b1d2010-10-14 07:23:06 -0700743static void xhci_save_registers(struct xhci_hcd *xhci)
744{
745 xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
746 xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
747 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
748 xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700749 xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
750 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
751 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
Sarah Sharpc7713e72012-03-16 13:19:35 -0700752 xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
753 xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700754}
755
756static void xhci_restore_registers(struct xhci_hcd *xhci)
757{
758 xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
759 xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
760 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
761 xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700762 xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
763 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
Sarah Sharpfb3d85b2012-03-16 13:27:39 -0700764 xhci_write_64(xhci, xhci->s3.erst_dequeue, &xhci->ir_set->erst_dequeue);
Sarah Sharpc7713e72012-03-16 13:19:35 -0700765 xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
766 xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700767}
768
Sarah Sharp89821322010-11-12 11:59:31 -0800769static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
770{
771 u64 val_64;
772
773 /* step 2: initialize command ring buffer */
774 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
775 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
776 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
777 xhci->cmd_ring->dequeue) &
778 (u64) ~CMD_RING_RSVD_BITS) |
779 xhci->cmd_ring->cycle_state;
Xenia Ragiadakoud195fcf2013-08-14 06:33:55 +0300780 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
781 "// Setting command ring address to 0x%llx",
Sarah Sharp89821322010-11-12 11:59:31 -0800782 (long unsigned long) val_64);
783 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
784}
785
786/*
787 * The whole command ring must be cleared to zero when we suspend the host.
788 *
789 * The host doesn't save the command ring pointer in the suspend well, so we
790 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
791 * aligned, because of the reserved bits in the command ring dequeue pointer
792 * register. Therefore, we can't just set the dequeue pointer back in the
793 * middle of the ring (TRBs are 16-byte aligned).
794 */
795static void xhci_clear_command_ring(struct xhci_hcd *xhci)
796{
797 struct xhci_ring *ring;
798 struct xhci_segment *seg;
799
800 ring = xhci->cmd_ring;
801 seg = ring->deq_seg;
802 do {
Andiry Xu158886c2011-11-30 16:37:41 +0800803 memset(seg->trbs, 0,
804 sizeof(union xhci_trb) * (TRBS_PER_SEGMENT - 1));
805 seg->trbs[TRBS_PER_SEGMENT - 1].link.control &=
806 cpu_to_le32(~TRB_CYCLE);
Sarah Sharp89821322010-11-12 11:59:31 -0800807 seg = seg->next;
808 } while (seg != ring->deq_seg);
809
810 /* Reset the software enqueue and dequeue pointers */
811 ring->deq_seg = ring->first_seg;
812 ring->dequeue = ring->first_seg->trbs;
813 ring->enq_seg = ring->deq_seg;
814 ring->enqueue = ring->dequeue;
815
Andiry Xub008df62012-03-05 17:49:34 +0800816 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
Sarah Sharp89821322010-11-12 11:59:31 -0800817 /*
818 * Ring is now zeroed, so the HW should look for change of ownership
819 * when the cycle bit is set to 1.
820 */
821 ring->cycle_state = 1;
822
823 /*
824 * Reset the hardware dequeue pointer.
825 * Yes, this will need to be re-written after resume, but we're paranoid
826 * and want to make sure the hardware doesn't access bogus memory
827 * because, say, the BIOS or an SMI started the host without changing
828 * the command ring pointers.
829 */
830 xhci_set_cmd_ring_deq(xhci);
831}
832
Andiry Xu5535b1d2010-10-14 07:23:06 -0700833/*
834 * Stop HC (not bus-specific)
835 *
836 * This is called when the machine transition into S3/S4 mode.
837 *
838 */
839int xhci_suspend(struct xhci_hcd *xhci)
840{
841 int rc = 0;
Oliver Neukum455f5892013-09-30 15:50:54 +0200842 unsigned int delay = XHCI_MAX_HALT_USEC;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700843 struct usb_hcd *hcd = xhci_to_hcd(xhci);
844 u32 command;
845
Felipe Balbi77b84762012-10-19 10:55:16 +0300846 if (hcd->state != HC_STATE_SUSPENDED ||
847 xhci->shared_hcd->state != HC_STATE_SUSPENDED)
848 return -EINVAL;
849
Sarah Sharpc52804a2012-11-27 12:30:23 -0800850 /* Don't poll the roothubs on bus suspend. */
851 xhci_dbg(xhci, "%s: stopping port polling.\n", __func__);
852 clear_bit(HCD_FLAG_POLL_RH, &hcd->flags);
853 del_timer_sync(&hcd->rh_timer);
854
Andiry Xu5535b1d2010-10-14 07:23:06 -0700855 spin_lock_irq(&xhci->lock);
856 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Sarah Sharpb3209372011-03-07 11:24:07 -0800857 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700858 /* step 1: stop endpoint */
859 /* skipped assuming that port suspend has done */
860
861 /* step 2: clear Run/Stop bit */
862 command = xhci_readl(xhci, &xhci->op_regs->command);
863 command &= ~CMD_RUN;
864 xhci_writel(xhci, command, &xhci->op_regs->command);
Oliver Neukum455f5892013-09-30 15:50:54 +0200865
866 /* Some chips from Fresco Logic need an extraordinary delay */
867 delay *= (xhci->quirks & XHCI_SLOW_SUSPEND) ? 10 : 1;
868
Sarah Sharp2611bd182012-10-25 13:27:51 -0700869 if (xhci_handshake(xhci, &xhci->op_regs->status,
Oliver Neukum455f5892013-09-30 15:50:54 +0200870 STS_HALT, STS_HALT, delay)) {
Andiry Xu5535b1d2010-10-14 07:23:06 -0700871 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
872 spin_unlock_irq(&xhci->lock);
873 return -ETIMEDOUT;
874 }
Sarah Sharp89821322010-11-12 11:59:31 -0800875 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700876
877 /* step 3: save registers */
878 xhci_save_registers(xhci);
879
880 /* step 4: set CSS flag */
881 command = xhci_readl(xhci, &xhci->op_regs->command);
882 command |= CMD_CSS;
883 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp2611bd182012-10-25 13:27:51 -0700884 if (xhci_handshake(xhci, &xhci->op_regs->status,
885 STS_SAVE, 0, 10 * 1000)) {
Andiry Xu622eb782012-06-13 10:51:57 +0800886 xhci_warn(xhci, "WARN: xHC save state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -0700887 spin_unlock_irq(&xhci->lock);
888 return -ETIMEDOUT;
889 }
Andiry Xu5535b1d2010-10-14 07:23:06 -0700890 spin_unlock_irq(&xhci->lock);
891
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500892 /*
893 * Deleting Compliance Mode Recovery Timer because the xHCI Host
894 * is about to be suspended.
895 */
896 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
897 (!(xhci_all_ports_seen_u0(xhci)))) {
898 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300899 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
900 "%s: compliance mode recovery timer deleted",
Tony Camuso58b1d792013-04-05 14:27:07 -0400901 __func__);
Alexis R. Cortes71c731a2012-08-03 14:00:27 -0500902 }
903
Andiry Xu00292272010-12-27 17:39:02 +0800904 /* step 5: remove core well power */
905 /* synchronize irq when using MSI-X */
Sebastian Andrzej Siewior421aa842011-09-23 14:19:58 -0700906 xhci_msix_sync_irqs(xhci);
Andiry Xu00292272010-12-27 17:39:02 +0800907
Andiry Xu5535b1d2010-10-14 07:23:06 -0700908 return rc;
909}
910
911/*
912 * start xHC (not bus-specific)
913 *
914 * This is called when the machine transition from S3/S4 mode.
915 *
916 */
917int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
918{
919 u32 command, temp = 0;
920 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Sarah Sharp65b22f92010-12-17 12:35:05 -0800921 struct usb_hcd *secondary_hcd;
Alan Sternf69e3122011-11-03 11:37:10 -0400922 int retval = 0;
Tony Camuso77df9e02013-02-21 16:11:27 -0500923 bool comp_timer_running = false;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700924
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800925 /* Wait a bit if either of the roothubs need to settle from the
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300926 * transition into bus suspend.
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800927 */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800928 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
929 time_before(jiffies,
930 xhci->bus_state[1].next_statechange))
Andiry Xu5535b1d2010-10-14 07:23:06 -0700931 msleep(100);
932
Alan Sternf69e3122011-11-03 11:37:10 -0400933 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
934 set_bit(HCD_FLAG_HW_ACCESSIBLE, &xhci->shared_hcd->flags);
935
Andiry Xu5535b1d2010-10-14 07:23:06 -0700936 spin_lock_irq(&xhci->lock);
Maarten Lankhorstc877b3b2011-06-15 23:47:21 +0200937 if (xhci->quirks & XHCI_RESET_ON_RESUME)
938 hibernated = true;
Andiry Xu5535b1d2010-10-14 07:23:06 -0700939
940 if (!hibernated) {
941 /* step 1: restore register */
942 xhci_restore_registers(xhci);
943 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -0800944 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700945 /* step 3: restore state and start state*/
946 /* step 3: set CRS flag */
947 command = xhci_readl(xhci, &xhci->op_regs->command);
948 command |= CMD_CRS;
949 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp2611bd182012-10-25 13:27:51 -0700950 if (xhci_handshake(xhci, &xhci->op_regs->status,
Andiry Xu622eb782012-06-13 10:51:57 +0800951 STS_RESTORE, 0, 10 * 1000)) {
952 xhci_warn(xhci, "WARN: xHC restore state timeout\n");
Andiry Xu5535b1d2010-10-14 07:23:06 -0700953 spin_unlock_irq(&xhci->lock);
954 return -ETIMEDOUT;
955 }
956 temp = xhci_readl(xhci, &xhci->op_regs->status);
957 }
958
959 /* If restore operation fails, re-initialize the HC during resume */
960 if ((temp & STS_SRE) || hibernated) {
Tony Camuso77df9e02013-02-21 16:11:27 -0500961
962 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) &&
963 !(xhci_all_ports_seen_u0(xhci))) {
964 del_timer_sync(&xhci->comp_mode_recovery_timer);
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +0300965 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
966 "Compliance Mode Recovery Timer deleted!");
Tony Camuso77df9e02013-02-21 16:11:27 -0500967 }
968
Sarah Sharpfedd3832011-04-12 17:43:19 -0700969 /* Let the USB core know _both_ roothubs lost power. */
970 usb_root_hub_lost_power(xhci->main_hcd->self.root_hub);
971 usb_root_hub_lost_power(xhci->shared_hcd->self.root_hub);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700972
973 xhci_dbg(xhci, "Stop HCD\n");
974 xhci_halt(xhci);
975 xhci_reset(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700976 spin_unlock_irq(&xhci->lock);
Andiry Xu00292272010-12-27 17:39:02 +0800977 xhci_cleanup_msix(xhci);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700978
Andiry Xu5535b1d2010-10-14 07:23:06 -0700979 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
980 temp = xhci_readl(xhci, &xhci->op_regs->status);
981 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
982 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
983 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
984 &xhci->ir_set->irq_pending);
Dmitry Torokhov09ece302011-02-08 16:29:33 -0800985 xhci_print_ir_set(xhci, 0);
Andiry Xu5535b1d2010-10-14 07:23:06 -0700986
987 xhci_dbg(xhci, "cleaning up memory\n");
988 xhci_mem_cleanup(xhci);
989 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
990 xhci_readl(xhci, &xhci->op_regs->status));
991
Sarah Sharp65b22f92010-12-17 12:35:05 -0800992 /* USB core calls the PCI reinit and start functions twice:
993 * first with the primary HCD, and then with the secondary HCD.
994 * If we don't do the same, the host will never be started.
995 */
996 if (!usb_hcd_is_primary_hcd(hcd))
997 secondary_hcd = hcd;
998 else
999 secondary_hcd = xhci->shared_hcd;
1000
1001 xhci_dbg(xhci, "Initialize the xhci_hcd\n");
1002 retval = xhci_init(hcd->primary_hcd);
Andiry Xu5535b1d2010-10-14 07:23:06 -07001003 if (retval)
1004 return retval;
Tony Camuso77df9e02013-02-21 16:11:27 -05001005 comp_timer_running = true;
1006
Sarah Sharp65b22f92010-12-17 12:35:05 -08001007 xhci_dbg(xhci, "Start the primary HCD\n");
1008 retval = xhci_run(hcd->primary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001009 if (!retval) {
Alan Sternf69e3122011-11-03 11:37:10 -04001010 xhci_dbg(xhci, "Start the secondary HCD\n");
1011 retval = xhci_run(secondary_hcd);
Sarah Sharpb3209372011-03-07 11:24:07 -08001012 }
Andiry Xu5535b1d2010-10-14 07:23:06 -07001013 hcd->state = HC_STATE_SUSPENDED;
Sarah Sharpb3209372011-03-07 11:24:07 -08001014 xhci->shared_hcd->state = HC_STATE_SUSPENDED;
Alan Sternf69e3122011-11-03 11:37:10 -04001015 goto done;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001016 }
1017
Andiry Xu5535b1d2010-10-14 07:23:06 -07001018 /* step 4: set Run/Stop bit */
1019 command = xhci_readl(xhci, &xhci->op_regs->command);
1020 command |= CMD_RUN;
1021 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp2611bd182012-10-25 13:27:51 -07001022 xhci_handshake(xhci, &xhci->op_regs->status, STS_HALT,
Andiry Xu5535b1d2010-10-14 07:23:06 -07001023 0, 250 * 1000);
1024
1025 /* step 5: walk topology and initialize portsc,
1026 * portpmsc and portli
1027 */
1028 /* this is done in bus_resume */
1029
1030 /* step 6: restart each of the previously
1031 * Running endpoints by ringing their doorbells
1032 */
1033
Andiry Xu5535b1d2010-10-14 07:23:06 -07001034 spin_unlock_irq(&xhci->lock);
Alan Sternf69e3122011-11-03 11:37:10 -04001035
1036 done:
1037 if (retval == 0) {
1038 usb_hcd_resume_root_hub(hcd);
1039 usb_hcd_resume_root_hub(xhci->shared_hcd);
1040 }
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001041
1042 /*
1043 * If system is subject to the Quirk, Compliance Mode Timer needs to
1044 * be re-initialized Always after a system resume. Ports are subject
1045 * to suffer the Compliance Mode issue again. It doesn't matter if
1046 * ports have entered previously to U0 before system's suspension.
1047 */
Tony Camuso77df9e02013-02-21 16:11:27 -05001048 if ((xhci->quirks & XHCI_COMP_MODE_QUIRK) && !comp_timer_running)
Alexis R. Cortes71c731a2012-08-03 14:00:27 -05001049 compliance_mode_recovery_timer_init(xhci);
1050
Sarah Sharpc52804a2012-11-27 12:30:23 -08001051 /* Re-enable port polling. */
1052 xhci_dbg(xhci, "%s: starting port polling.\n", __func__);
1053 set_bit(HCD_FLAG_POLL_RH, &hcd->flags);
1054 usb_hcd_poll_rh_status(hcd);
1055
Alan Sternf69e3122011-11-03 11:37:10 -04001056 return retval;
Andiry Xu5535b1d2010-10-14 07:23:06 -07001057}
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -07001058#endif /* CONFIG_PM */
1059
Sarah Sharp7f84eef2009-04-27 19:53:56 -07001060/*-------------------------------------------------------------------------*/
1061
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001062/**
1063 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
1064 * HCDs. Find the index for an endpoint given its descriptor. Use the return
1065 * value to right shift 1 for the bitmask.
1066 *
1067 * Index = (epnum * 2) + direction - 1,
1068 * where direction = 0 for OUT, 1 for IN.
1069 * For control endpoints, the IN index is used (OUT index is unused), so
1070 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
1071 */
1072unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
1073{
1074 unsigned int index;
1075 if (usb_endpoint_xfer_control(desc))
1076 index = (unsigned int) (usb_endpoint_num(desc)*2);
1077 else
1078 index = (unsigned int) (usb_endpoint_num(desc)*2) +
1079 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
1080 return index;
1081}
1082
Julius Werner01c5f442013-04-15 15:55:04 -07001083/* The reverse operation to xhci_get_endpoint_index. Calculate the USB endpoint
1084 * address from the XHCI endpoint index.
1085 */
1086unsigned int xhci_get_endpoint_address(unsigned int ep_index)
1087{
1088 unsigned int number = DIV_ROUND_UP(ep_index, 2);
1089 unsigned int direction = ep_index % 2 ? USB_DIR_OUT : USB_DIR_IN;
1090 return direction | number;
1091}
1092
Sarah Sharpf94e01862009-04-27 19:58:38 -07001093/* Find the flag for this endpoint (for use in the control context). Use the
1094 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1095 * bit 1, etc.
1096 */
1097unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
1098{
1099 return 1 << (xhci_get_endpoint_index(desc) + 1);
1100}
1101
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001102/* Find the flag for this endpoint (for use in the control context). Use the
1103 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
1104 * bit 1, etc.
1105 */
1106unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
1107{
1108 return 1 << (ep_index + 1);
1109}
1110
Sarah Sharpf94e01862009-04-27 19:58:38 -07001111/* Compute the last valid endpoint context index. Basically, this is the
1112 * endpoint index plus one. For slot contexts with more than valid endpoint,
1113 * we find the most significant bit set in the added contexts flags.
1114 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
1115 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
1116 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001117unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001118{
1119 return fls(added_ctxs) - 1;
1120}
1121
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001122/* Returns 1 if the arguments are OK;
1123 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
1124 */
Dmitry Torokhov8212a492011-02-08 13:55:59 -08001125static int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -07001126 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
1127 const char *func) {
1128 struct xhci_hcd *xhci;
1129 struct xhci_virt_device *virt_dev;
1130
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001131 if (!hcd || (check_ep && !ep) || !udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001132 pr_debug("xHCI %s called with invalid args\n", func);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001133 return -EINVAL;
1134 }
1135 if (!udev->parent) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001136 pr_debug("xHCI %s called for root hub\n", func);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001137 return 0;
1138 }
Andiry Xu64927732010-10-14 07:22:45 -07001139
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001140 xhci = hcd_to_xhci(hcd);
Andiry Xu64927732010-10-14 07:22:45 -07001141 if (check_virt_dev) {
sifram.rajas@gmail.com73ddc242011-09-02 11:06:00 -07001142 if (!udev->slot_id || !xhci->devs[udev->slot_id]) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001143 xhci_dbg(xhci, "xHCI %s called with unaddressed device\n",
1144 func);
Andiry Xu64927732010-10-14 07:22:45 -07001145 return -EINVAL;
1146 }
1147
1148 virt_dev = xhci->devs[udev->slot_id];
1149 if (virt_dev->udev != udev) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03001150 xhci_dbg(xhci, "xHCI %s called with udev and "
Andiry Xu64927732010-10-14 07:22:45 -07001151 "virt_dev does not match\n", func);
1152 return -EINVAL;
1153 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001154 }
Andiry Xu64927732010-10-14 07:22:45 -07001155
Sarah Sharp203a8662013-07-24 10:27:13 -07001156 if (xhci->xhc_state & XHCI_STATE_HALTED)
1157 return -ENODEV;
1158
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001159 return 1;
1160}
1161
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001162static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001163 struct usb_device *udev, struct xhci_command *command,
1164 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001165
1166/*
1167 * Full speed devices may have a max packet size greater than 8 bytes, but the
1168 * USB core doesn't know that until it reads the first 8 bytes of the
1169 * descriptor. If the usb_device's max packet size changes after that point,
1170 * we need to issue an evaluate context command and wait on it.
1171 */
1172static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
1173 unsigned int ep_index, struct urb *urb)
1174{
1175 struct xhci_container_ctx *in_ctx;
1176 struct xhci_container_ctx *out_ctx;
1177 struct xhci_input_control_ctx *ctrl_ctx;
1178 struct xhci_ep_ctx *ep_ctx;
1179 int max_packet_size;
1180 int hw_max_packet_size;
1181 int ret = 0;
1182
1183 out_ctx = xhci->devs[slot_id]->out_ctx;
1184 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Matt Evans28ccd292011-03-29 13:40:46 +11001185 hw_max_packet_size = MAX_PACKET_DECODED(le32_to_cpu(ep_ctx->ep_info2));
Kuninori Morimoto29cc8892011-08-23 03:12:03 -07001186 max_packet_size = usb_endpoint_maxp(&urb->dev->ep0.desc);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001187 if (hw_max_packet_size != max_packet_size) {
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001188 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1189 "Max Packet Size for ep 0 changed.");
1190 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1191 "Max packet size in usb_device = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001192 max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001193 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1194 "Max packet size in xHCI HW = %d",
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001195 hw_max_packet_size);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001196 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1197 "Issuing evaluate context command.");
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001198
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001199 /* Set up the input context flags for the command */
1200 /* FIXME: This won't work if a non-default control endpoint
1201 * changes max packet sizes.
1202 */
Sarah Sharp92f8e762013-04-23 17:11:14 -07001203 in_ctx = xhci->devs[slot_id]->in_ctx;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001204 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001205 if (!ctrl_ctx) {
1206 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1207 __func__);
1208 return -ENOMEM;
1209 }
1210 /* Set up the modified control endpoint 0 */
1211 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1212 xhci->devs[slot_id]->out_ctx, ep_index);
1213
1214 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1215 ep_ctx->ep_info2 &= cpu_to_le32(~MAX_PACKET_MASK);
1216 ep_ctx->ep_info2 |= cpu_to_le32(MAX_PACKET(max_packet_size));
1217
Matt Evans28ccd292011-03-29 13:40:46 +11001218 ctrl_ctx->add_flags = cpu_to_le32(EP0_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001219 ctrl_ctx->drop_flags = 0;
1220
1221 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
1222 xhci_dbg_ctx(xhci, in_ctx, ep_index);
1223 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
1224 xhci_dbg_ctx(xhci, out_ctx, ep_index);
1225
Sarah Sharp913a8a32009-09-04 10:53:13 -07001226 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
1227 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001228
1229 /* Clean up the input context for later use by bandwidth
1230 * functions.
1231 */
Matt Evans28ccd292011-03-29 13:40:46 +11001232 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001233 }
1234 return ret;
1235}
1236
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001237/*
1238 * non-error returns are a promise to giveback() the urb later
1239 * we drop ownership so next owner (or urb unlink) can get it
1240 */
1241int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
1242{
1243 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Andiry Xu2ffdea22011-09-02 11:05:57 -07001244 struct xhci_td *buffer;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001245 unsigned long flags;
1246 int ret = 0;
1247 unsigned int slot_id, ep_index;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001248 struct urb_priv *urb_priv;
1249 int size, i;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001250
Andiry Xu64927732010-10-14 07:22:45 -07001251 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1252 true, true, __func__) <= 0)
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001253 return -EINVAL;
1254
1255 slot_id = urb->dev->slot_id;
1256 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001257
Alan Stern541c7d42010-06-22 16:39:10 -04001258 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001259 if (!in_interrupt())
1260 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
1261 ret = -ESHUTDOWN;
1262 goto exit;
1263 }
Andiry Xu8e51adc2010-07-22 15:23:31 -07001264
1265 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1266 size = urb->number_of_packets;
1267 else
1268 size = 1;
1269
1270 urb_priv = kzalloc(sizeof(struct urb_priv) +
1271 size * sizeof(struct xhci_td *), mem_flags);
1272 if (!urb_priv)
1273 return -ENOMEM;
1274
Andiry Xu2ffdea22011-09-02 11:05:57 -07001275 buffer = kzalloc(size * sizeof(struct xhci_td), mem_flags);
1276 if (!buffer) {
1277 kfree(urb_priv);
1278 return -ENOMEM;
1279 }
1280
Andiry Xu8e51adc2010-07-22 15:23:31 -07001281 for (i = 0; i < size; i++) {
Andiry Xu2ffdea22011-09-02 11:05:57 -07001282 urb_priv->td[i] = buffer;
1283 buffer++;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001284 }
1285
1286 urb_priv->length = size;
1287 urb_priv->td_cnt = 0;
1288 urb->hcpriv = urb_priv;
1289
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001290 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1291 /* Check to see if the max packet size for the default control
1292 * endpoint changed during FS device enumeration
1293 */
1294 if (urb->dev->speed == USB_SPEED_FULL) {
1295 ret = xhci_check_maxpacket(xhci, slot_id,
1296 ep_index, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001297 if (ret < 0) {
1298 xhci_urb_free_priv(xhci, urb_priv);
1299 urb->hcpriv = NULL;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001300 return ret;
Sarah Sharpd13565c2011-07-22 14:34:34 -07001301 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001302 }
1303
Sarah Sharpb11069f2009-07-27 12:03:23 -07001304 /* We have a spinlock and interrupts disabled, so we must pass
1305 * atomic context to this function, which may allocate memory.
1306 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001307 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001308 if (xhci->xhc_state & XHCI_STATE_DYING)
1309 goto dying;
Sarah Sharpb11069f2009-07-27 12:03:23 -07001310 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -07001311 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001312 if (ret)
1313 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001314 spin_unlock_irqrestore(&xhci->lock, flags);
1315 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1316 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001317 if (xhci->xhc_state & XHCI_STATE_DYING)
1318 goto dying;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001319 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1320 EP_GETTING_STREAMS) {
1321 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1322 "is transitioning to using streams.\n");
1323 ret = -EINVAL;
1324 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1325 EP_GETTING_NO_STREAMS) {
1326 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1327 "is transitioning to "
1328 "not having streams.\n");
1329 ret = -EINVAL;
1330 } else {
1331 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1332 slot_id, ep_index);
1333 }
Sarah Sharpd13565c2011-07-22 14:34:34 -07001334 if (ret)
1335 goto free_priv;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001336 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -07001337 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1338 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001339 if (xhci->xhc_state & XHCI_STATE_DYING)
1340 goto dying;
Sarah Sharp624defa2009-09-02 12:14:28 -07001341 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1342 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001343 if (ret)
1344 goto free_priv;
Sarah Sharp624defa2009-09-02 12:14:28 -07001345 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001346 } else {
Andiry Xu787f4e52010-07-22 15:23:52 -07001347 spin_lock_irqsave(&xhci->lock, flags);
1348 if (xhci->xhc_state & XHCI_STATE_DYING)
1349 goto dying;
1350 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1351 slot_id, ep_index);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001352 if (ret)
1353 goto free_priv;
Andiry Xu787f4e52010-07-22 15:23:52 -07001354 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001355 }
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001356exit:
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001357 return ret;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001358dying:
1359 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1360 "non-responsive xHCI host.\n",
1361 urb->ep->desc.bEndpointAddress, urb);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001362 ret = -ESHUTDOWN;
1363free_priv:
1364 xhci_urb_free_priv(xhci, urb_priv);
1365 urb->hcpriv = NULL;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001366 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharpd13565c2011-07-22 14:34:34 -07001367 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001368}
1369
Sarah Sharp021bff92010-07-29 22:12:20 -07001370/* Get the right ring for the given URB.
1371 * If the endpoint supports streams, boundary check the URB's stream ID.
1372 * If the endpoint doesn't support streams, return the singular endpoint ring.
1373 */
1374static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
1375 struct urb *urb)
1376{
1377 unsigned int slot_id;
1378 unsigned int ep_index;
1379 unsigned int stream_id;
1380 struct xhci_virt_ep *ep;
1381
1382 slot_id = urb->dev->slot_id;
1383 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1384 stream_id = urb->stream_id;
1385 ep = &xhci->devs[slot_id]->eps[ep_index];
1386 /* Common case: no streams */
1387 if (!(ep->ep_state & EP_HAS_STREAMS))
1388 return ep->ring;
1389
1390 if (stream_id == 0) {
1391 xhci_warn(xhci,
1392 "WARN: Slot ID %u, ep index %u has streams, "
1393 "but URB has no stream ID.\n",
1394 slot_id, ep_index);
1395 return NULL;
1396 }
1397
1398 if (stream_id < ep->stream_info->num_streams)
1399 return ep->stream_info->stream_rings[stream_id];
1400
1401 xhci_warn(xhci,
1402 "WARN: Slot ID %u, ep index %u has "
1403 "stream IDs 1 to %u allocated, "
1404 "but stream ID %u is requested.\n",
1405 slot_id, ep_index,
1406 ep->stream_info->num_streams - 1,
1407 stream_id);
1408 return NULL;
1409}
1410
Sarah Sharpae636742009-04-29 19:02:31 -07001411/*
1412 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1413 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1414 * should pick up where it left off in the TD, unless a Set Transfer Ring
1415 * Dequeue Pointer is issued.
1416 *
1417 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1418 * the ring. Since the ring is a contiguous structure, they can't be physically
1419 * removed. Instead, there are two options:
1420 *
1421 * 1) If the HC is in the middle of processing the URB to be canceled, we
1422 * simply move the ring's dequeue pointer past those TRBs using the Set
1423 * Transfer Ring Dequeue Pointer command. This will be the common case,
1424 * when drivers timeout on the last submitted URB and attempt to cancel.
1425 *
1426 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1427 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1428 * HC will need to invalidate the any TRBs it has cached after the stop
1429 * endpoint command, as noted in the xHCI 0.95 errata.
1430 *
1431 * 3) The TD may have completed by the time the Stop Endpoint Command
1432 * completes, so software needs to handle that case too.
1433 *
1434 * This function should protect against the TD enqueueing code ringing the
1435 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1436 * It also needs to account for multiple cancellations on happening at the same
1437 * time for the same endpoint.
1438 *
1439 * Note that this function can be called in any context, or so says
1440 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001441 */
1442int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1443{
Sarah Sharpae636742009-04-29 19:02:31 -07001444 unsigned long flags;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001445 int ret, i;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001446 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -07001447 struct xhci_hcd *xhci;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001448 struct urb_priv *urb_priv;
Sarah Sharpae636742009-04-29 19:02:31 -07001449 struct xhci_td *td;
1450 unsigned int ep_index;
1451 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001452 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -07001453
1454 xhci = hcd_to_xhci(hcd);
1455 spin_lock_irqsave(&xhci->lock, flags);
1456 /* Make sure the URB hasn't completed or been unlinked already */
1457 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1458 if (ret || !urb->hcpriv)
1459 goto done;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001460 temp = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpc6cc27c2011-03-11 10:20:58 -08001461 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_HALTED)) {
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001462 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1463 "HW died, freeing TD.");
Andiry Xu8e51adc2010-07-22 15:23:31 -07001464 urb_priv = urb->hcpriv;
Sarah Sharp585df1d2011-08-02 15:43:40 -07001465 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
1466 td = urb_priv->td[i];
1467 if (!list_empty(&td->td_list))
1468 list_del_init(&td->td_list);
1469 if (!list_empty(&td->cancelled_td_list))
1470 list_del_init(&td->cancelled_td_list);
1471 }
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001472
1473 usb_hcd_unlink_urb_from_ep(hcd, urb);
1474 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp214f76f2010-10-26 11:22:02 -07001475 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
Andiry Xu8e51adc2010-07-22 15:23:31 -07001476 xhci_urb_free_priv(xhci, urb_priv);
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001477 return ret;
1478 }
Sarah Sharp7bd89b42011-07-01 13:35:40 -07001479 if ((xhci->xhc_state & XHCI_STATE_DYING) ||
1480 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001481 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1482 "Ep 0x%x: URB %p to be canceled on "
1483 "non-responsive xHCI host.",
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001484 urb->ep->desc.bEndpointAddress, urb);
1485 /* Let the stop endpoint command watchdog timer (which set this
1486 * state) finish cleaning up the endpoint TD lists. We must
1487 * have caught it in the middle of dropping a lock and giving
1488 * back an URB.
1489 */
1490 goto done;
1491 }
Sarah Sharpae636742009-04-29 19:02:31 -07001492
Sarah Sharpae636742009-04-29 19:02:31 -07001493 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001494 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001495 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1496 if (!ep_ring) {
1497 ret = -EINVAL;
1498 goto done;
1499 }
1500
Andiry Xu8e51adc2010-07-22 15:23:31 -07001501 urb_priv = urb->hcpriv;
Sarah Sharp79688ac2011-12-19 16:56:04 -08001502 i = urb_priv->td_cnt;
1503 if (i < urb_priv->length)
Xenia Ragiadakouaa50b292013-08-14 06:33:54 +03001504 xhci_dbg_trace(xhci, trace_xhci_dbg_cancel_urb,
1505 "Cancel URB %p, dev %s, ep 0x%x, "
1506 "starting at offset 0x%llx",
Sarah Sharp79688ac2011-12-19 16:56:04 -08001507 urb, urb->dev->devpath,
1508 urb->ep->desc.bEndpointAddress,
1509 (unsigned long long) xhci_trb_virt_to_dma(
1510 urb_priv->td[i]->start_seg,
1511 urb_priv->td[i]->first_trb));
Andiry Xu8e51adc2010-07-22 15:23:31 -07001512
Sarah Sharp79688ac2011-12-19 16:56:04 -08001513 for (; i < urb_priv->length; i++) {
Andiry Xu8e51adc2010-07-22 15:23:31 -07001514 td = urb_priv->td[i];
1515 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1516 }
1517
Sarah Sharpae636742009-04-29 19:02:31 -07001518 /* Queue a stop endpoint command, but only if this is
1519 * the first cancellation to be handled.
1520 */
Sarah Sharp678539c2009-10-27 10:55:52 -07001521 if (!(ep->ep_state & EP_HALT_PENDING)) {
1522 ep->ep_state |= EP_HALT_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001523 ep->stop_cmds_pending++;
1524 ep->stop_cmd_timer.expires = jiffies +
1525 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1526 add_timer(&ep->stop_cmd_timer);
Andiry Xube88fe42010-10-14 07:22:57 -07001527 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001528 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -07001529 }
1530done:
1531 spin_unlock_irqrestore(&xhci->lock, flags);
1532 return ret;
Sarah Sharpd0e96f5a2009-04-27 19:58:01 -07001533}
1534
Sarah Sharpf94e01862009-04-27 19:58:38 -07001535/* Drop an endpoint from a new bandwidth configuration for this device.
1536 * Only one call to this function is allowed per endpoint before
1537 * check_bandwidth() or reset_bandwidth() must be called.
1538 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1539 * add the endpoint to the schedule with possibly new parameters denoted by a
1540 * different endpoint descriptor in usb_host_endpoint.
1541 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1542 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001543 *
1544 * The USB core will not allow URBs to be queued to an endpoint that is being
1545 * disabled, so there's no need for mutual exclusion to protect
1546 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001547 */
1548int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1549 struct usb_host_endpoint *ep)
1550{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001551 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001552 struct xhci_container_ctx *in_ctx, *out_ctx;
1553 struct xhci_input_control_ctx *ctrl_ctx;
1554 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001555 unsigned int last_ctx;
1556 unsigned int ep_index;
1557 struct xhci_ep_ctx *ep_ctx;
1558 u32 drop_flag;
1559 u32 new_add_flags, new_drop_flags, new_slot_info;
1560 int ret;
1561
Andiry Xu64927732010-10-14 07:22:45 -07001562 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001563 if (ret <= 0)
1564 return ret;
1565 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001566 if (xhci->xhc_state & XHCI_STATE_DYING)
1567 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001568
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001569 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001570 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1571 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1572 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1573 __func__, drop_flag);
1574 return 0;
1575 }
1576
Sarah Sharpf94e01862009-04-27 19:58:38 -07001577 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001578 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1579 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001580 if (!ctrl_ctx) {
1581 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1582 __func__);
1583 return 0;
1584 }
1585
Sarah Sharpf94e01862009-04-27 19:58:38 -07001586 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001587 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001588 /* If the HC already knows the endpoint is disabled,
1589 * or the HCD has noted it is disabled, ignore this request
1590 */
Matt Evansf5960b62011-06-01 10:22:55 +10001591 if (((ep_ctx->ep_info & cpu_to_le32(EP_STATE_MASK)) ==
1592 cpu_to_le32(EP_STATE_DISABLED)) ||
Matt Evans28ccd292011-03-29 13:40:46 +11001593 le32_to_cpu(ctrl_ctx->drop_flags) &
1594 xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001595 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1596 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001597 return 0;
1598 }
1599
Matt Evans28ccd292011-03-29 13:40:46 +11001600 ctrl_ctx->drop_flags |= cpu_to_le32(drop_flag);
1601 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001602
Matt Evans28ccd292011-03-29 13:40:46 +11001603 ctrl_ctx->add_flags &= cpu_to_le32(~drop_flag);
1604 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001605
Matt Evans28ccd292011-03-29 13:40:46 +11001606 last_ctx = xhci_last_valid_endpoint(le32_to_cpu(ctrl_ctx->add_flags));
John Yound115b042009-07-27 12:05:15 -07001607 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001608 /* Update the last valid endpoint context, if we deleted the last one */
Matt Evans28ccd292011-03-29 13:40:46 +11001609 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) >
1610 LAST_CTX(last_ctx)) {
1611 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1612 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001613 }
Matt Evans28ccd292011-03-29 13:40:46 +11001614 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001615
1616 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1617
Sarah Sharpf94e01862009-04-27 19:58:38 -07001618 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1619 (unsigned int) ep->desc.bEndpointAddress,
1620 udev->slot_id,
1621 (unsigned int) new_drop_flags,
1622 (unsigned int) new_add_flags,
1623 (unsigned int) new_slot_info);
1624 return 0;
1625}
1626
1627/* Add an endpoint to a new possible bandwidth configuration for this device.
1628 * Only one call to this function is allowed per endpoint before
1629 * check_bandwidth() or reset_bandwidth() must be called.
1630 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1631 * add the endpoint to the schedule with possibly new parameters denoted by a
1632 * different endpoint descriptor in usb_host_endpoint.
1633 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1634 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001635 *
1636 * The USB core will not allow URBs to be queued to an endpoint until the
1637 * configuration or alt setting is installed in the device, so there's no need
1638 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001639 */
1640int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1641 struct usb_host_endpoint *ep)
1642{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001643 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001644 struct xhci_container_ctx *in_ctx, *out_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001645 unsigned int ep_index;
John Yound115b042009-07-27 12:05:15 -07001646 struct xhci_slot_ctx *slot_ctx;
1647 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001648 u32 added_ctxs;
1649 unsigned int last_ctx;
1650 u32 new_add_flags, new_drop_flags, new_slot_info;
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001651 struct xhci_virt_device *virt_dev;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001652 int ret = 0;
1653
Andiry Xu64927732010-10-14 07:22:45 -07001654 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001655 if (ret <= 0) {
1656 /* So we won't queue a reset ep command for a root hub */
1657 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001658 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001659 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001660 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07001661 if (xhci->xhc_state & XHCI_STATE_DYING)
1662 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001663
1664 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1665 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1666 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1667 /* FIXME when we have to issue an evaluate endpoint command to
1668 * deal with ep0 max packet size changing once we get the
1669 * descriptors
1670 */
1671 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1672 __func__, added_ctxs);
1673 return 0;
1674 }
1675
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001676 virt_dev = xhci->devs[udev->slot_id];
1677 in_ctx = virt_dev->in_ctx;
1678 out_ctx = virt_dev->out_ctx;
John Yound115b042009-07-27 12:05:15 -07001679 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07001680 if (!ctrl_ctx) {
1681 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1682 __func__);
1683 return 0;
1684 }
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001685
Sarah Sharp92f8e762013-04-23 17:11:14 -07001686 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001687 /* If this endpoint is already in use, and the upper layers are trying
1688 * to add it again without dropping it, reject the addition.
1689 */
1690 if (virt_dev->eps[ep_index].ring &&
1691 !(le32_to_cpu(ctrl_ctx->drop_flags) &
1692 xhci_get_endpoint_flag(&ep->desc))) {
1693 xhci_warn(xhci, "Trying to add endpoint 0x%x "
1694 "without dropping it.\n",
1695 (unsigned int) ep->desc.bEndpointAddress);
1696 return -EINVAL;
1697 }
1698
Sarah Sharpf94e01862009-04-27 19:58:38 -07001699 /* If the HCD has already noted the endpoint is enabled,
1700 * ignore this request.
1701 */
Matt Evans28ccd292011-03-29 13:40:46 +11001702 if (le32_to_cpu(ctrl_ctx->add_flags) &
1703 xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001704 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1705 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001706 return 0;
1707 }
1708
Sarah Sharpf88ba782009-05-14 11:44:22 -07001709 /*
1710 * Configuration and alternate setting changes must be done in
1711 * process context, not interrupt context (or so documenation
1712 * for usb_set_interface() and usb_set_configuration() claim).
1713 */
Sarah Sharpfa75ac32011-06-05 23:10:04 -07001714 if (xhci_endpoint_init(xhci, virt_dev, udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001715 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1716 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001717 return -ENOMEM;
1718 }
1719
Matt Evans28ccd292011-03-29 13:40:46 +11001720 ctrl_ctx->add_flags |= cpu_to_le32(added_ctxs);
1721 new_add_flags = le32_to_cpu(ctrl_ctx->add_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001722
1723 /* If xhci_endpoint_disable() was called for this endpoint, but the
1724 * xHC hasn't been notified yet through the check_bandwidth() call,
1725 * this re-adds a new state for the endpoint from the new endpoint
1726 * descriptors. We must drop and re-add this endpoint, so we leave the
1727 * drop flags alone.
1728 */
Matt Evans28ccd292011-03-29 13:40:46 +11001729 new_drop_flags = le32_to_cpu(ctrl_ctx->drop_flags);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001730
John Yound115b042009-07-27 12:05:15 -07001731 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001732 /* Update the last valid endpoint context, if we just added one past */
Matt Evans28ccd292011-03-29 13:40:46 +11001733 if ((le32_to_cpu(slot_ctx->dev_info) & LAST_CTX_MASK) <
1734 LAST_CTX(last_ctx)) {
1735 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
1736 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(last_ctx));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001737 }
Matt Evans28ccd292011-03-29 13:40:46 +11001738 new_slot_info = le32_to_cpu(slot_ctx->dev_info);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001739
Sarah Sharpa1587d92009-07-27 12:03:15 -07001740 /* Store the usb_device pointer for later use */
1741 ep->hcpriv = udev;
1742
Sarah Sharpf94e01862009-04-27 19:58:38 -07001743 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1744 (unsigned int) ep->desc.bEndpointAddress,
1745 udev->slot_id,
1746 (unsigned int) new_drop_flags,
1747 (unsigned int) new_add_flags,
1748 (unsigned int) new_slot_info);
1749 return 0;
1750}
1751
John Yound115b042009-07-27 12:05:15 -07001752static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001753{
John Yound115b042009-07-27 12:05:15 -07001754 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001755 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001756 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001757 int i;
1758
Sarah Sharp92f8e762013-04-23 17:11:14 -07001759 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1760 if (!ctrl_ctx) {
1761 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
1762 __func__);
1763 return;
1764 }
1765
Sarah Sharpf94e01862009-04-27 19:58:38 -07001766 /* When a device's add flag and drop flag are zero, any subsequent
1767 * configure endpoint command will leave that endpoint's state
1768 * untouched. Make sure we don't leave any old state in the input
1769 * endpoint contexts.
1770 */
John Yound115b042009-07-27 12:05:15 -07001771 ctrl_ctx->drop_flags = 0;
1772 ctrl_ctx->add_flags = 0;
1773 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11001774 slot_ctx->dev_info &= cpu_to_le32(~LAST_CTX_MASK);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001775 /* Endpoint 0 is always valid */
Matt Evans28ccd292011-03-29 13:40:46 +11001776 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001777 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001778 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001779 ep_ctx->ep_info = 0;
1780 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001781 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001782 ep_ctx->tx_info = 0;
1783 }
1784}
1785
Sarah Sharpf2217e82009-08-07 14:04:43 -07001786static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001787 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001788{
1789 int ret;
1790
Sarah Sharp913a8a32009-09-04 10:53:13 -07001791 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001792 case COMP_ENOMEM:
1793 dev_warn(&udev->dev, "Not enough host controller resources "
1794 "for new device state.\n");
1795 ret = -ENOMEM;
1796 /* FIXME: can we allocate more resources for the HC? */
1797 break;
1798 case COMP_BW_ERR:
Hans de Goede71d85722012-01-04 23:29:18 +01001799 case COMP_2ND_BW_ERR:
Sarah Sharpf2217e82009-08-07 14:04:43 -07001800 dev_warn(&udev->dev, "Not enough bandwidth "
1801 "for new device state.\n");
1802 ret = -ENOSPC;
1803 /* FIXME: can we go back to the old state? */
1804 break;
1805 case COMP_TRB_ERR:
1806 /* the HCD set up something wrong */
1807 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1808 "add flag = 1, "
1809 "and endpoint is not disabled.\n");
1810 ret = -EINVAL;
1811 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001812 case COMP_DEV_ERR:
1813 dev_warn(&udev->dev, "ERROR: Incompatible device for endpoint "
1814 "configure command.\n");
1815 ret = -ENODEV;
1816 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001817 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001818 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1819 "Successful Endpoint Configure command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001820 ret = 0;
1821 break;
1822 default:
1823 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001824 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001825 ret = -EINVAL;
1826 break;
1827 }
1828 return ret;
1829}
1830
1831static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp00161f72011-04-28 12:23:23 -07001832 struct usb_device *udev, u32 *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001833{
1834 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001835 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001836
Sarah Sharp913a8a32009-09-04 10:53:13 -07001837 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001838 case COMP_EINVAL:
1839 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1840 "context command.\n");
1841 ret = -EINVAL;
1842 break;
1843 case COMP_EBADSLT:
1844 dev_warn(&udev->dev, "WARN: slot not enabled for"
1845 "evaluate context command.\n");
Sarah Sharpb8031342012-10-16 13:26:22 -07001846 ret = -EINVAL;
1847 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001848 case COMP_CTX_STATE:
1849 dev_warn(&udev->dev, "WARN: invalid context state for "
1850 "evaluate context command.\n");
1851 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1852 ret = -EINVAL;
1853 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08001854 case COMP_DEV_ERR:
1855 dev_warn(&udev->dev, "ERROR: Incompatible device for evaluate "
1856 "context command.\n");
1857 ret = -ENODEV;
1858 break;
Alex He1bb73a82011-05-05 18:14:12 +08001859 case COMP_MEL_ERR:
1860 /* Max Exit Latency too large error */
1861 dev_warn(&udev->dev, "WARN: Max Exit Latency too large\n");
1862 ret = -EINVAL;
1863 break;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001864 case COMP_SUCCESS:
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03001865 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
1866 "Successful evaluate context command");
Sarah Sharpf2217e82009-08-07 14:04:43 -07001867 ret = 0;
1868 break;
1869 default:
1870 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001871 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001872 ret = -EINVAL;
1873 break;
1874 }
1875 return ret;
1876}
1877
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001878static u32 xhci_count_num_new_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001879 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001880{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001881 u32 valid_add_flags;
1882 u32 valid_drop_flags;
1883
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001884 /* Ignore the slot flag (bit 0), and the default control endpoint flag
1885 * (bit 1). The default control endpoint is added during the Address
1886 * Device command and is never removed until the slot is disabled.
1887 */
1888 valid_add_flags = ctrl_ctx->add_flags >> 2;
1889 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1890
1891 /* Use hweight32 to count the number of ones in the add flags, or
1892 * number of endpoints added. Don't count endpoints that are changed
1893 * (both added and dropped).
1894 */
1895 return hweight32(valid_add_flags) -
1896 hweight32(valid_add_flags & valid_drop_flags);
1897}
1898
1899static unsigned int xhci_count_num_dropped_endpoints(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001900 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001901{
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001902 u32 valid_add_flags;
1903 u32 valid_drop_flags;
1904
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001905 valid_add_flags = ctrl_ctx->add_flags >> 2;
1906 valid_drop_flags = ctrl_ctx->drop_flags >> 2;
1907
1908 return hweight32(valid_drop_flags) -
1909 hweight32(valid_add_flags & valid_drop_flags);
1910}
1911
1912/*
1913 * We need to reserve the new number of endpoints before the configure endpoint
1914 * command completes. We can't subtract the dropped endpoints from the number
1915 * of active endpoints until the command completes because we can oversubscribe
1916 * the host in this case:
1917 *
1918 * - the first configure endpoint command drops more endpoints than it adds
1919 * - a second configure endpoint command that adds more endpoints is queued
1920 * - the first configure endpoint command fails, so the config is unchanged
1921 * - the second command may succeed, even though there isn't enough resources
1922 *
1923 * Must be called with xhci->lock held.
1924 */
1925static int xhci_reserve_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001926 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001927{
1928 u32 added_eps;
1929
Sarah Sharp92f8e762013-04-23 17:11:14 -07001930 added_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001931 if (xhci->num_active_eps + added_eps > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001932 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1933 "Not enough ep ctxs: "
1934 "%u active, need to add %u, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001935 xhci->num_active_eps, added_eps,
1936 xhci->limit_active_eps);
1937 return -ENOMEM;
1938 }
1939 xhci->num_active_eps += added_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001940 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1941 "Adding %u ep ctxs, %u now active.", added_eps,
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001942 xhci->num_active_eps);
1943 return 0;
1944}
1945
1946/*
1947 * The configure endpoint was failed by the xHC for some other reason, so we
1948 * need to revert the resources that failed configuration would have used.
1949 *
1950 * Must be called with xhci->lock held.
1951 */
1952static void xhci_free_host_resources(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001953 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001954{
1955 u32 num_failed_eps;
1956
Sarah Sharp92f8e762013-04-23 17:11:14 -07001957 num_failed_eps = xhci_count_num_new_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001958 xhci->num_active_eps -= num_failed_eps;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001959 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1960 "Removing %u failed ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001961 num_failed_eps,
1962 xhci->num_active_eps);
1963}
1964
1965/*
1966 * Now that the command has completed, clean up the active endpoint count by
1967 * subtracting out the endpoints that were dropped (but not changed).
1968 *
1969 * Must be called with xhci->lock held.
1970 */
1971static void xhci_finish_resource_reservation(struct xhci_hcd *xhci,
Sarah Sharp92f8e762013-04-23 17:11:14 -07001972 struct xhci_input_control_ctx *ctrl_ctx)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001973{
1974 u32 num_dropped_eps;
1975
Sarah Sharp92f8e762013-04-23 17:11:14 -07001976 num_dropped_eps = xhci_count_num_dropped_endpoints(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001977 xhci->num_active_eps -= num_dropped_eps;
1978 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03001979 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
1980 "Removing %u dropped ep ctxs, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07001981 num_dropped_eps,
1982 xhci->num_active_eps);
1983}
1984
Felipe Balbied384bd2012-08-07 14:10:03 +03001985static unsigned int xhci_get_block_size(struct usb_device *udev)
Sarah Sharpc29eea62011-09-02 11:05:52 -07001986{
1987 switch (udev->speed) {
1988 case USB_SPEED_LOW:
1989 case USB_SPEED_FULL:
1990 return FS_BLOCK;
1991 case USB_SPEED_HIGH:
1992 return HS_BLOCK;
1993 case USB_SPEED_SUPER:
1994 return SS_BLOCK;
1995 case USB_SPEED_UNKNOWN:
1996 case USB_SPEED_WIRELESS:
1997 default:
1998 /* Should never happen */
1999 return 1;
2000 }
2001}
2002
Felipe Balbied384bd2012-08-07 14:10:03 +03002003static unsigned int
2004xhci_get_largest_overhead(struct xhci_interval_bw *interval_bw)
Sarah Sharpc29eea62011-09-02 11:05:52 -07002005{
2006 if (interval_bw->overhead[LS_OVERHEAD_TYPE])
2007 return LS_OVERHEAD;
2008 if (interval_bw->overhead[FS_OVERHEAD_TYPE])
2009 return FS_OVERHEAD;
2010 return HS_OVERHEAD;
2011}
2012
2013/* If we are changing a LS/FS device under a HS hub,
2014 * make sure (if we are activating a new TT) that the HS bus has enough
2015 * bandwidth for this new TT.
2016 */
2017static int xhci_check_tt_bw_table(struct xhci_hcd *xhci,
2018 struct xhci_virt_device *virt_dev,
2019 int old_active_eps)
2020{
2021 struct xhci_interval_bw_table *bw_table;
2022 struct xhci_tt_bw_info *tt_info;
2023
2024 /* Find the bandwidth table for the root port this TT is attached to. */
2025 bw_table = &xhci->rh_bw[virt_dev->real_port - 1].bw_table;
2026 tt_info = virt_dev->tt_info;
2027 /* If this TT already had active endpoints, the bandwidth for this TT
2028 * has already been added. Removing all periodic endpoints (and thus
2029 * making the TT enactive) will only decrease the bandwidth used.
2030 */
2031 if (old_active_eps)
2032 return 0;
2033 if (old_active_eps == 0 && tt_info->active_eps != 0) {
2034 if (bw_table->bw_used + TT_HS_OVERHEAD > HS_BW_LIMIT)
2035 return -ENOMEM;
2036 return 0;
2037 }
2038 /* Not sure why we would have no new active endpoints...
2039 *
2040 * Maybe because of an Evaluate Context change for a hub update or a
2041 * control endpoint 0 max packet size change?
2042 * FIXME: skip the bandwidth calculation in that case.
2043 */
2044 return 0;
2045}
2046
Sarah Sharp2b698992011-09-13 16:41:13 -07002047static int xhci_check_ss_bw(struct xhci_hcd *xhci,
2048 struct xhci_virt_device *virt_dev)
2049{
2050 unsigned int bw_reserved;
2051
2052 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_IN, 100);
2053 if (virt_dev->bw_table->ss_bw_in > (SS_BW_LIMIT_IN - bw_reserved))
2054 return -ENOMEM;
2055
2056 bw_reserved = DIV_ROUND_UP(SS_BW_RESERVED*SS_BW_LIMIT_OUT, 100);
2057 if (virt_dev->bw_table->ss_bw_out > (SS_BW_LIMIT_OUT - bw_reserved))
2058 return -ENOMEM;
2059
2060 return 0;
2061}
2062
Sarah Sharpc29eea62011-09-02 11:05:52 -07002063/*
2064 * This algorithm is a very conservative estimate of the worst-case scheduling
2065 * scenario for any one interval. The hardware dynamically schedules the
2066 * packets, so we can't tell which microframe could be the limiting factor in
2067 * the bandwidth scheduling. This only takes into account periodic endpoints.
2068 *
2069 * Obviously, we can't solve an NP complete problem to find the minimum worst
2070 * case scenario. Instead, we come up with an estimate that is no less than
2071 * the worst case bandwidth used for any one microframe, but may be an
2072 * over-estimate.
2073 *
2074 * We walk the requirements for each endpoint by interval, starting with the
2075 * smallest interval, and place packets in the schedule where there is only one
2076 * possible way to schedule packets for that interval. In order to simplify
2077 * this algorithm, we record the largest max packet size for each interval, and
2078 * assume all packets will be that size.
2079 *
2080 * For interval 0, we obviously must schedule all packets for each interval.
2081 * The bandwidth for interval 0 is just the amount of data to be transmitted
2082 * (the sum of all max ESIT payload sizes, plus any overhead per packet times
2083 * the number of packets).
2084 *
2085 * For interval 1, we have two possible microframes to schedule those packets
2086 * in. For this algorithm, if we can schedule the same number of packets for
2087 * each possible scheduling opportunity (each microframe), we will do so. The
2088 * remaining number of packets will be saved to be transmitted in the gaps in
2089 * the next interval's scheduling sequence.
2090 *
2091 * As we move those remaining packets to be scheduled with interval 2 packets,
2092 * we have to double the number of remaining packets to transmit. This is
2093 * because the intervals are actually powers of 2, and we would be transmitting
2094 * the previous interval's packets twice in this interval. We also have to be
2095 * sure that when we look at the largest max packet size for this interval, we
2096 * also look at the largest max packet size for the remaining packets and take
2097 * the greater of the two.
2098 *
2099 * The algorithm continues to evenly distribute packets in each scheduling
2100 * opportunity, and push the remaining packets out, until we get to the last
2101 * interval. Then those packets and their associated overhead are just added
2102 * to the bandwidth used.
Sarah Sharp2e279802011-09-02 11:05:50 -07002103 */
2104static int xhci_check_bw_table(struct xhci_hcd *xhci,
2105 struct xhci_virt_device *virt_dev,
2106 int old_active_eps)
2107{
Sarah Sharpc29eea62011-09-02 11:05:52 -07002108 unsigned int bw_reserved;
2109 unsigned int max_bandwidth;
2110 unsigned int bw_used;
2111 unsigned int block_size;
2112 struct xhci_interval_bw_table *bw_table;
2113 unsigned int packet_size = 0;
2114 unsigned int overhead = 0;
2115 unsigned int packets_transmitted = 0;
2116 unsigned int packets_remaining = 0;
2117 unsigned int i;
2118
Sarah Sharp2b698992011-09-13 16:41:13 -07002119 if (virt_dev->udev->speed == USB_SPEED_SUPER)
2120 return xhci_check_ss_bw(xhci, virt_dev);
2121
Sarah Sharpc29eea62011-09-02 11:05:52 -07002122 if (virt_dev->udev->speed == USB_SPEED_HIGH) {
2123 max_bandwidth = HS_BW_LIMIT;
2124 /* Convert percent of bus BW reserved to blocks reserved */
2125 bw_reserved = DIV_ROUND_UP(HS_BW_RESERVED * max_bandwidth, 100);
2126 } else {
2127 max_bandwidth = FS_BW_LIMIT;
2128 bw_reserved = DIV_ROUND_UP(FS_BW_RESERVED * max_bandwidth, 100);
2129 }
2130
2131 bw_table = virt_dev->bw_table;
2132 /* We need to translate the max packet size and max ESIT payloads into
2133 * the units the hardware uses.
2134 */
2135 block_size = xhci_get_block_size(virt_dev->udev);
2136
2137 /* If we are manipulating a LS/FS device under a HS hub, double check
2138 * that the HS bus has enough bandwidth if we are activing a new TT.
2139 */
2140 if (virt_dev->tt_info) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002141 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2142 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002143 virt_dev->real_port);
2144 if (xhci_check_tt_bw_table(xhci, virt_dev, old_active_eps)) {
2145 xhci_warn(xhci, "Not enough bandwidth on HS bus for "
2146 "newly activated TT.\n");
2147 return -ENOMEM;
2148 }
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002149 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2150 "Recalculating BW for TT slot %u port %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002151 virt_dev->tt_info->slot_id,
2152 virt_dev->tt_info->ttport);
2153 } else {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002154 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2155 "Recalculating BW for rootport %u",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002156 virt_dev->real_port);
2157 }
2158
2159 /* Add in how much bandwidth will be used for interval zero, or the
2160 * rounded max ESIT payload + number of packets * largest overhead.
2161 */
2162 bw_used = DIV_ROUND_UP(bw_table->interval0_esit_payload, block_size) +
2163 bw_table->interval_bw[0].num_packets *
2164 xhci_get_largest_overhead(&bw_table->interval_bw[0]);
2165
2166 for (i = 1; i < XHCI_MAX_INTERVAL; i++) {
2167 unsigned int bw_added;
2168 unsigned int largest_mps;
2169 unsigned int interval_overhead;
2170
2171 /*
2172 * How many packets could we transmit in this interval?
2173 * If packets didn't fit in the previous interval, we will need
2174 * to transmit that many packets twice within this interval.
2175 */
2176 packets_remaining = 2 * packets_remaining +
2177 bw_table->interval_bw[i].num_packets;
2178
2179 /* Find the largest max packet size of this or the previous
2180 * interval.
2181 */
2182 if (list_empty(&bw_table->interval_bw[i].endpoints))
2183 largest_mps = 0;
2184 else {
2185 struct xhci_virt_ep *virt_ep;
2186 struct list_head *ep_entry;
2187
2188 ep_entry = bw_table->interval_bw[i].endpoints.next;
2189 virt_ep = list_entry(ep_entry,
2190 struct xhci_virt_ep, bw_endpoint_list);
2191 /* Convert to blocks, rounding up */
2192 largest_mps = DIV_ROUND_UP(
2193 virt_ep->bw_info.max_packet_size,
2194 block_size);
2195 }
2196 if (largest_mps > packet_size)
2197 packet_size = largest_mps;
2198
2199 /* Use the larger overhead of this or the previous interval. */
2200 interval_overhead = xhci_get_largest_overhead(
2201 &bw_table->interval_bw[i]);
2202 if (interval_overhead > overhead)
2203 overhead = interval_overhead;
2204
2205 /* How many packets can we evenly distribute across
2206 * (1 << (i + 1)) possible scheduling opportunities?
2207 */
2208 packets_transmitted = packets_remaining >> (i + 1);
2209
2210 /* Add in the bandwidth used for those scheduled packets */
2211 bw_added = packets_transmitted * (overhead + packet_size);
2212
2213 /* How many packets do we have remaining to transmit? */
2214 packets_remaining = packets_remaining % (1 << (i + 1));
2215
2216 /* What largest max packet size should those packets have? */
2217 /* If we've transmitted all packets, don't carry over the
2218 * largest packet size.
2219 */
2220 if (packets_remaining == 0) {
2221 packet_size = 0;
2222 overhead = 0;
2223 } else if (packets_transmitted > 0) {
2224 /* Otherwise if we do have remaining packets, and we've
2225 * scheduled some packets in this interval, take the
2226 * largest max packet size from endpoints with this
2227 * interval.
2228 */
2229 packet_size = largest_mps;
2230 overhead = interval_overhead;
2231 }
2232 /* Otherwise carry over packet_size and overhead from the last
2233 * time we had a remainder.
2234 */
2235 bw_used += bw_added;
2236 if (bw_used > max_bandwidth) {
2237 xhci_warn(xhci, "Not enough bandwidth. "
2238 "Proposed: %u, Max: %u\n",
2239 bw_used, max_bandwidth);
2240 return -ENOMEM;
2241 }
2242 }
2243 /*
2244 * Ok, we know we have some packets left over after even-handedly
2245 * scheduling interval 15. We don't know which microframes they will
2246 * fit into, so we over-schedule and say they will be scheduled every
2247 * microframe.
2248 */
2249 if (packets_remaining > 0)
2250 bw_used += overhead + packet_size;
2251
2252 if (!virt_dev->tt_info && virt_dev->udev->speed == USB_SPEED_HIGH) {
2253 unsigned int port_index = virt_dev->real_port - 1;
2254
2255 /* OK, we're manipulating a HS device attached to a
2256 * root port bandwidth domain. Include the number of active TTs
2257 * in the bandwidth used.
2258 */
2259 bw_used += TT_HS_OVERHEAD *
2260 xhci->rh_bw[port_index].num_active_tts;
2261 }
2262
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002263 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2264 "Final bandwidth: %u, Limit: %u, Reserved: %u, "
2265 "Available: %u " "percent",
Sarah Sharpc29eea62011-09-02 11:05:52 -07002266 bw_used, max_bandwidth, bw_reserved,
2267 (max_bandwidth - bw_used - bw_reserved) * 100 /
2268 max_bandwidth);
2269
2270 bw_used += bw_reserved;
2271 if (bw_used > max_bandwidth) {
2272 xhci_warn(xhci, "Not enough bandwidth. Proposed: %u, Max: %u\n",
2273 bw_used, max_bandwidth);
2274 return -ENOMEM;
2275 }
2276
2277 bw_table->bw_used = bw_used;
Sarah Sharp2e279802011-09-02 11:05:50 -07002278 return 0;
2279}
2280
2281static bool xhci_is_async_ep(unsigned int ep_type)
2282{
2283 return (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
2284 ep_type != ISOC_IN_EP &&
2285 ep_type != INT_IN_EP);
2286}
2287
Sarah Sharp2b698992011-09-13 16:41:13 -07002288static bool xhci_is_sync_in_ep(unsigned int ep_type)
2289{
Sarah Sharp392a07a2012-10-25 13:44:12 -07002290 return (ep_type == ISOC_IN_EP || ep_type == INT_IN_EP);
Sarah Sharp2b698992011-09-13 16:41:13 -07002291}
2292
2293static unsigned int xhci_get_ss_bw_consumed(struct xhci_bw_info *ep_bw)
2294{
2295 unsigned int mps = DIV_ROUND_UP(ep_bw->max_packet_size, SS_BLOCK);
2296
2297 if (ep_bw->ep_interval == 0)
2298 return SS_OVERHEAD_BURST +
2299 (ep_bw->mult * ep_bw->num_packets *
2300 (SS_OVERHEAD + mps));
2301 return DIV_ROUND_UP(ep_bw->mult * ep_bw->num_packets *
2302 (SS_OVERHEAD + mps + SS_OVERHEAD_BURST),
2303 1 << ep_bw->ep_interval);
2304
2305}
2306
Sarah Sharp2e279802011-09-02 11:05:50 -07002307void xhci_drop_ep_from_interval_table(struct xhci_hcd *xhci,
2308 struct xhci_bw_info *ep_bw,
2309 struct xhci_interval_bw_table *bw_table,
2310 struct usb_device *udev,
2311 struct xhci_virt_ep *virt_ep,
2312 struct xhci_tt_bw_info *tt_info)
2313{
2314 struct xhci_interval_bw *interval_bw;
2315 int normalized_interval;
2316
Sarah Sharp2b698992011-09-13 16:41:13 -07002317 if (xhci_is_async_ep(ep_bw->type))
Sarah Sharp2e279802011-09-02 11:05:50 -07002318 return;
2319
Sarah Sharp2b698992011-09-13 16:41:13 -07002320 if (udev->speed == USB_SPEED_SUPER) {
2321 if (xhci_is_sync_in_ep(ep_bw->type))
2322 xhci->devs[udev->slot_id]->bw_table->ss_bw_in -=
2323 xhci_get_ss_bw_consumed(ep_bw);
2324 else
2325 xhci->devs[udev->slot_id]->bw_table->ss_bw_out -=
2326 xhci_get_ss_bw_consumed(ep_bw);
2327 return;
2328 }
2329
2330 /* SuperSpeed endpoints never get added to intervals in the table, so
2331 * this check is only valid for HS/FS/LS devices.
2332 */
2333 if (list_empty(&virt_ep->bw_endpoint_list))
2334 return;
Sarah Sharp2e279802011-09-02 11:05:50 -07002335 /* For LS/FS devices, we need to translate the interval expressed in
2336 * microframes to frames.
2337 */
2338 if (udev->speed == USB_SPEED_HIGH)
2339 normalized_interval = ep_bw->ep_interval;
2340 else
2341 normalized_interval = ep_bw->ep_interval - 3;
2342
2343 if (normalized_interval == 0)
2344 bw_table->interval0_esit_payload -= ep_bw->max_esit_payload;
2345 interval_bw = &bw_table->interval_bw[normalized_interval];
2346 interval_bw->num_packets -= ep_bw->num_packets;
2347 switch (udev->speed) {
2348 case USB_SPEED_LOW:
2349 interval_bw->overhead[LS_OVERHEAD_TYPE] -= 1;
2350 break;
2351 case USB_SPEED_FULL:
2352 interval_bw->overhead[FS_OVERHEAD_TYPE] -= 1;
2353 break;
2354 case USB_SPEED_HIGH:
2355 interval_bw->overhead[HS_OVERHEAD_TYPE] -= 1;
2356 break;
2357 case USB_SPEED_SUPER:
2358 case USB_SPEED_UNKNOWN:
2359 case USB_SPEED_WIRELESS:
2360 /* Should never happen because only LS/FS/HS endpoints will get
2361 * added to the endpoint list.
2362 */
2363 return;
2364 }
2365 if (tt_info)
2366 tt_info->active_eps -= 1;
2367 list_del_init(&virt_ep->bw_endpoint_list);
2368}
2369
2370static void xhci_add_ep_to_interval_table(struct xhci_hcd *xhci,
2371 struct xhci_bw_info *ep_bw,
2372 struct xhci_interval_bw_table *bw_table,
2373 struct usb_device *udev,
2374 struct xhci_virt_ep *virt_ep,
2375 struct xhci_tt_bw_info *tt_info)
2376{
2377 struct xhci_interval_bw *interval_bw;
2378 struct xhci_virt_ep *smaller_ep;
2379 int normalized_interval;
2380
2381 if (xhci_is_async_ep(ep_bw->type))
2382 return;
2383
Sarah Sharp2b698992011-09-13 16:41:13 -07002384 if (udev->speed == USB_SPEED_SUPER) {
2385 if (xhci_is_sync_in_ep(ep_bw->type))
2386 xhci->devs[udev->slot_id]->bw_table->ss_bw_in +=
2387 xhci_get_ss_bw_consumed(ep_bw);
2388 else
2389 xhci->devs[udev->slot_id]->bw_table->ss_bw_out +=
2390 xhci_get_ss_bw_consumed(ep_bw);
2391 return;
2392 }
2393
Sarah Sharp2e279802011-09-02 11:05:50 -07002394 /* For LS/FS devices, we need to translate the interval expressed in
2395 * microframes to frames.
2396 */
2397 if (udev->speed == USB_SPEED_HIGH)
2398 normalized_interval = ep_bw->ep_interval;
2399 else
2400 normalized_interval = ep_bw->ep_interval - 3;
2401
2402 if (normalized_interval == 0)
2403 bw_table->interval0_esit_payload += ep_bw->max_esit_payload;
2404 interval_bw = &bw_table->interval_bw[normalized_interval];
2405 interval_bw->num_packets += ep_bw->num_packets;
2406 switch (udev->speed) {
2407 case USB_SPEED_LOW:
2408 interval_bw->overhead[LS_OVERHEAD_TYPE] += 1;
2409 break;
2410 case USB_SPEED_FULL:
2411 interval_bw->overhead[FS_OVERHEAD_TYPE] += 1;
2412 break;
2413 case USB_SPEED_HIGH:
2414 interval_bw->overhead[HS_OVERHEAD_TYPE] += 1;
2415 break;
2416 case USB_SPEED_SUPER:
2417 case USB_SPEED_UNKNOWN:
2418 case USB_SPEED_WIRELESS:
2419 /* Should never happen because only LS/FS/HS endpoints will get
2420 * added to the endpoint list.
2421 */
2422 return;
2423 }
2424
2425 if (tt_info)
2426 tt_info->active_eps += 1;
2427 /* Insert the endpoint into the list, largest max packet size first. */
2428 list_for_each_entry(smaller_ep, &interval_bw->endpoints,
2429 bw_endpoint_list) {
2430 if (ep_bw->max_packet_size >=
2431 smaller_ep->bw_info.max_packet_size) {
2432 /* Add the new ep before the smaller endpoint */
2433 list_add_tail(&virt_ep->bw_endpoint_list,
2434 &smaller_ep->bw_endpoint_list);
2435 return;
2436 }
2437 }
2438 /* Add the new endpoint at the end of the list. */
2439 list_add_tail(&virt_ep->bw_endpoint_list,
2440 &interval_bw->endpoints);
2441}
2442
2443void xhci_update_tt_active_eps(struct xhci_hcd *xhci,
2444 struct xhci_virt_device *virt_dev,
2445 int old_active_eps)
2446{
2447 struct xhci_root_port_bw_info *rh_bw_info;
2448 if (!virt_dev->tt_info)
2449 return;
2450
2451 rh_bw_info = &xhci->rh_bw[virt_dev->real_port - 1];
2452 if (old_active_eps == 0 &&
2453 virt_dev->tt_info->active_eps != 0) {
2454 rh_bw_info->num_active_tts += 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002455 rh_bw_info->bw_table.bw_used += TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002456 } else if (old_active_eps != 0 &&
2457 virt_dev->tt_info->active_eps == 0) {
2458 rh_bw_info->num_active_tts -= 1;
Sarah Sharpc29eea62011-09-02 11:05:52 -07002459 rh_bw_info->bw_table.bw_used -= TT_HS_OVERHEAD;
Sarah Sharp2e279802011-09-02 11:05:50 -07002460 }
2461}
2462
2463static int xhci_reserve_bandwidth(struct xhci_hcd *xhci,
2464 struct xhci_virt_device *virt_dev,
2465 struct xhci_container_ctx *in_ctx)
2466{
2467 struct xhci_bw_info ep_bw_info[31];
2468 int i;
2469 struct xhci_input_control_ctx *ctrl_ctx;
2470 int old_active_eps = 0;
2471
Sarah Sharp2e279802011-09-02 11:05:50 -07002472 if (virt_dev->tt_info)
2473 old_active_eps = virt_dev->tt_info->active_eps;
2474
2475 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002476 if (!ctrl_ctx) {
2477 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2478 __func__);
2479 return -ENOMEM;
2480 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002481
2482 for (i = 0; i < 31; i++) {
2483 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2484 continue;
2485
2486 /* Make a copy of the BW info in case we need to revert this */
2487 memcpy(&ep_bw_info[i], &virt_dev->eps[i].bw_info,
2488 sizeof(ep_bw_info[i]));
2489 /* Drop the endpoint from the interval table if the endpoint is
2490 * being dropped or changed.
2491 */
2492 if (EP_IS_DROPPED(ctrl_ctx, i))
2493 xhci_drop_ep_from_interval_table(xhci,
2494 &virt_dev->eps[i].bw_info,
2495 virt_dev->bw_table,
2496 virt_dev->udev,
2497 &virt_dev->eps[i],
2498 virt_dev->tt_info);
2499 }
2500 /* Overwrite the information stored in the endpoints' bw_info */
2501 xhci_update_bw_info(xhci, virt_dev->in_ctx, ctrl_ctx, virt_dev);
2502 for (i = 0; i < 31; i++) {
2503 /* Add any changed or added endpoints to the interval table */
2504 if (EP_IS_ADDED(ctrl_ctx, i))
2505 xhci_add_ep_to_interval_table(xhci,
2506 &virt_dev->eps[i].bw_info,
2507 virt_dev->bw_table,
2508 virt_dev->udev,
2509 &virt_dev->eps[i],
2510 virt_dev->tt_info);
2511 }
2512
2513 if (!xhci_check_bw_table(xhci, virt_dev, old_active_eps)) {
2514 /* Ok, this fits in the bandwidth we have.
2515 * Update the number of active TTs.
2516 */
2517 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
2518 return 0;
2519 }
2520
2521 /* We don't have enough bandwidth for this, revert the stored info. */
2522 for (i = 0; i < 31; i++) {
2523 if (!EP_IS_ADDED(ctrl_ctx, i) && !EP_IS_DROPPED(ctrl_ctx, i))
2524 continue;
2525
2526 /* Drop the new copies of any added or changed endpoints from
2527 * the interval table.
2528 */
2529 if (EP_IS_ADDED(ctrl_ctx, i)) {
2530 xhci_drop_ep_from_interval_table(xhci,
2531 &virt_dev->eps[i].bw_info,
2532 virt_dev->bw_table,
2533 virt_dev->udev,
2534 &virt_dev->eps[i],
2535 virt_dev->tt_info);
2536 }
2537 /* Revert the endpoint back to its old information */
2538 memcpy(&virt_dev->eps[i].bw_info, &ep_bw_info[i],
2539 sizeof(ep_bw_info[i]));
2540 /* Add any changed or dropped endpoints back into the table */
2541 if (EP_IS_DROPPED(ctrl_ctx, i))
2542 xhci_add_ep_to_interval_table(xhci,
2543 &virt_dev->eps[i].bw_info,
2544 virt_dev->bw_table,
2545 virt_dev->udev,
2546 &virt_dev->eps[i],
2547 virt_dev->tt_info);
2548 }
2549 return -ENOMEM;
2550}
2551
2552
Sarah Sharpf2217e82009-08-07 14:04:43 -07002553/* Issue a configure endpoint command or evaluate context command
2554 * and wait for it to finish.
2555 */
2556static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002557 struct usb_device *udev,
2558 struct xhci_command *command,
2559 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07002560{
2561 int ret;
2562 int timeleft;
2563 unsigned long flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002564 struct xhci_container_ctx *in_ctx;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002565 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002566 struct completion *cmd_completion;
Matt Evans28ccd292011-03-29 13:40:46 +11002567 u32 *cmd_status;
Sarah Sharp913a8a32009-09-04 10:53:13 -07002568 struct xhci_virt_device *virt_dev;
Elric Fu6e4468b2012-06-27 16:31:52 +08002569 union xhci_trb *cmd_trb;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002570
2571 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002572 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002573
Sarah Sharp750645f2011-09-02 11:05:43 -07002574 if (command)
2575 in_ctx = command->in_ctx;
2576 else
2577 in_ctx = virt_dev->in_ctx;
Sarah Sharp92f8e762013-04-23 17:11:14 -07002578 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2579 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07002580 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002581 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2582 __func__);
2583 return -ENOMEM;
2584 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002585
2586 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK) &&
Sarah Sharp92f8e762013-04-23 17:11:14 -07002587 xhci_reserve_host_resources(xhci, ctrl_ctx)) {
Sarah Sharp750645f2011-09-02 11:05:43 -07002588 spin_unlock_irqrestore(&xhci->lock, flags);
2589 xhci_warn(xhci, "Not enough host resources, "
2590 "active endpoint contexts = %u\n",
2591 xhci->num_active_eps);
2592 return -ENOMEM;
2593 }
Sarah Sharp2e279802011-09-02 11:05:50 -07002594 if ((xhci->quirks & XHCI_SW_BW_CHECKING) &&
2595 xhci_reserve_bandwidth(xhci, virt_dev, in_ctx)) {
2596 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002597 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2e279802011-09-02 11:05:50 -07002598 spin_unlock_irqrestore(&xhci->lock, flags);
2599 xhci_warn(xhci, "Not enough bandwidth\n");
2600 return -ENOMEM;
2601 }
Sarah Sharp750645f2011-09-02 11:05:43 -07002602
2603 if (command) {
Sarah Sharp913a8a32009-09-04 10:53:13 -07002604 cmd_completion = command->completion;
2605 cmd_status = &command->status;
Mathias Nymanec7e43e2013-08-30 18:25:49 +03002606 command->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002607 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
2608 } else {
Sarah Sharp913a8a32009-09-04 10:53:13 -07002609 cmd_completion = &virt_dev->cmd_completion;
2610 cmd_status = &virt_dev->cmd_status;
2611 }
Andiry Xu1d680642010-03-12 17:10:04 +08002612 init_completion(cmd_completion);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002613
Mathias Nymanec7e43e2013-08-30 18:25:49 +03002614 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002615 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07002616 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
2617 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002618 else
Sarah Sharp913a8a32009-09-04 10:53:13 -07002619 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
Sarah Sharp4b266542012-05-07 15:34:26 -07002620 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002621 if (ret < 0) {
Sarah Sharpc01591b2009-12-09 15:58:58 -08002622 if (command)
2623 list_del(&command->cmd_list);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002624 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK))
Sarah Sharp92f8e762013-04-23 17:11:14 -07002625 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002626 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03002627 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
2628 "FIXME allocate a new ring segment");
Sarah Sharpf2217e82009-08-07 14:04:43 -07002629 return -ENOMEM;
2630 }
2631 xhci_ring_cmd_db(xhci);
2632 spin_unlock_irqrestore(&xhci->lock, flags);
2633
2634 /* Wait for the configure endpoint command to complete */
2635 timeleft = wait_for_completion_interruptible_timeout(
Sarah Sharp913a8a32009-09-04 10:53:13 -07002636 cmd_completion,
Elric Fu6e4468b2012-06-27 16:31:52 +08002637 XHCI_CMD_DEFAULT_TIMEOUT);
Sarah Sharpf2217e82009-08-07 14:04:43 -07002638 if (timeleft <= 0) {
2639 xhci_warn(xhci, "%s while waiting for %s command\n",
2640 timeleft == 0 ? "Timeout" : "Signal",
2641 ctx_change == 0 ?
2642 "configure endpoint" :
2643 "evaluate context");
Elric Fu6e4468b2012-06-27 16:31:52 +08002644 /* cancel the configure endpoint command */
2645 ret = xhci_cancel_cmd(xhci, command, cmd_trb);
2646 if (ret < 0)
2647 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002648 return -ETIME;
2649 }
2650
2651 if (!ctx_change)
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002652 ret = xhci_configure_endpoint_result(xhci, udev, cmd_status);
2653 else
2654 ret = xhci_evaluate_context_result(xhci, udev, cmd_status);
2655
2656 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
2657 spin_lock_irqsave(&xhci->lock, flags);
2658 /* If the command failed, remove the reserved resources.
2659 * Otherwise, clean up the estimate to include dropped eps.
2660 */
2661 if (ret)
Sarah Sharp92f8e762013-04-23 17:11:14 -07002662 xhci_free_host_resources(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002663 else
Sarah Sharp92f8e762013-04-23 17:11:14 -07002664 xhci_finish_resource_reservation(xhci, ctrl_ctx);
Sarah Sharp2cf95c12011-05-11 16:14:58 -07002665 spin_unlock_irqrestore(&xhci->lock, flags);
2666 }
2667 return ret;
Sarah Sharpf2217e82009-08-07 14:04:43 -07002668}
2669
Sarah Sharpf88ba782009-05-14 11:44:22 -07002670/* Called after one or more calls to xhci_add_endpoint() or
2671 * xhci_drop_endpoint(). If this call fails, the USB core is expected
2672 * to call xhci_reset_bandwidth().
2673 *
2674 * Since we are in the middle of changing either configuration or
2675 * installing a new alt setting, the USB core won't allow URBs to be
2676 * enqueued for any endpoint on the old config or interface. Nothing
2677 * else should be touching the xhci->devs[slot_id] structure, so we
2678 * don't need to take the xhci->lock for manipulating that.
2679 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002680int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2681{
2682 int i;
2683 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002684 struct xhci_hcd *xhci;
2685 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07002686 struct xhci_input_control_ctx *ctrl_ctx;
2687 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002688
Andiry Xu64927732010-10-14 07:22:45 -07002689 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002690 if (ret <= 0)
2691 return ret;
2692 xhci = hcd_to_xhci(hcd);
Sarah Sharpfe6c6c12011-05-23 16:41:17 -07002693 if (xhci->xhc_state & XHCI_STATE_DYING)
2694 return -ENODEV;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002695
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002696 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002697 virt_dev = xhci->devs[udev->slot_id];
2698
2699 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
John Yound115b042009-07-27 12:05:15 -07002700 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07002701 if (!ctrl_ctx) {
2702 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2703 __func__);
2704 return -ENOMEM;
2705 }
Matt Evans28ccd292011-03-29 13:40:46 +11002706 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
2707 ctrl_ctx->add_flags &= cpu_to_le32(~EP0_FLAG);
2708 ctrl_ctx->drop_flags &= cpu_to_le32(~(SLOT_FLAG | EP0_FLAG));
Sarah Sharp2dc37532011-09-02 11:05:40 -07002709
2710 /* Don't issue the command if there's no endpoints to update. */
2711 if (ctrl_ctx->add_flags == cpu_to_le32(SLOT_FLAG) &&
2712 ctrl_ctx->drop_flags == 0)
2713 return 0;
2714
Sarah Sharpf94e01862009-04-27 19:58:38 -07002715 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07002716 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
2717 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002718 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002719
Sarah Sharp913a8a32009-09-04 10:53:13 -07002720 ret = xhci_configure_endpoint(xhci, udev, NULL,
2721 false, false);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002722 if (ret) {
2723 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002724 return ret;
2725 }
2726
2727 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07002728 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
Matt Evans28ccd292011-03-29 13:40:46 +11002729 LAST_CTX_TO_EP_NUM(le32_to_cpu(slot_ctx->dev_info)));
Sarah Sharpf94e01862009-04-27 19:58:38 -07002730
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002731 /* Free any rings that were dropped, but not changed. */
2732 for (i = 1; i < 31; ++i) {
Matt Evans4819fef2011-06-01 13:01:07 +10002733 if ((le32_to_cpu(ctrl_ctx->drop_flags) & (1 << (i + 1))) &&
2734 !(le32_to_cpu(ctrl_ctx->add_flags) & (1 << (i + 1))))
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002735 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2736 }
John Yound115b042009-07-27 12:05:15 -07002737 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp834cb0f2011-05-12 18:06:37 -07002738 /*
2739 * Install any rings for completely new endpoints or changed endpoints,
2740 * and free or cache any old rings from changed endpoints.
2741 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07002742 for (i = 1; i < 31; ++i) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002743 if (!virt_dev->eps[i].new_ring)
2744 continue;
2745 /* Only cache or free the old ring if it exists.
2746 * It may not if this is the first add of an endpoint.
2747 */
2748 if (virt_dev->eps[i].ring) {
Sarah Sharp412566b2009-12-09 15:59:01 -08002749 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002750 }
Sarah Sharp74f9fe22009-12-03 09:44:29 -08002751 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
2752 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002753 }
2754
Sarah Sharpf94e01862009-04-27 19:58:38 -07002755 return ret;
2756}
2757
2758void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
2759{
Sarah Sharpf94e01862009-04-27 19:58:38 -07002760 struct xhci_hcd *xhci;
2761 struct xhci_virt_device *virt_dev;
2762 int i, ret;
2763
Andiry Xu64927732010-10-14 07:22:45 -07002764 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002765 if (ret <= 0)
2766 return;
2767 xhci = hcd_to_xhci(hcd);
2768
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002769 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002770 virt_dev = xhci->devs[udev->slot_id];
2771 /* Free any rings allocated for added endpoints */
2772 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002773 if (virt_dev->eps[i].new_ring) {
2774 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
2775 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002776 }
2777 }
John Yound115b042009-07-27 12:05:15 -07002778 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07002779}
2780
Sarah Sharp5270b952009-09-04 10:53:11 -07002781static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002782 struct xhci_container_ctx *in_ctx,
2783 struct xhci_container_ctx *out_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002784 struct xhci_input_control_ctx *ctrl_ctx,
Sarah Sharp913a8a32009-09-04 10:53:13 -07002785 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07002786{
Matt Evans28ccd292011-03-29 13:40:46 +11002787 ctrl_ctx->add_flags = cpu_to_le32(add_flags);
2788 ctrl_ctx->drop_flags = cpu_to_le32(drop_flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002789 xhci_slot_copy(xhci, in_ctx, out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11002790 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharp5270b952009-09-04 10:53:11 -07002791
Sarah Sharp913a8a32009-09-04 10:53:13 -07002792 xhci_dbg(xhci, "Input Context:\n");
2793 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07002794}
2795
Dmitry Torokhov8212a492011-02-08 13:55:59 -08002796static void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002797 unsigned int slot_id, unsigned int ep_index,
2798 struct xhci_dequeue_state *deq_state)
2799{
Sarah Sharp92f8e762013-04-23 17:11:14 -07002800 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002801 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002802 struct xhci_ep_ctx *ep_ctx;
2803 u32 added_ctxs;
2804 dma_addr_t addr;
2805
Sarah Sharp92f8e762013-04-23 17:11:14 -07002806 in_ctx = xhci->devs[slot_id]->in_ctx;
2807 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
2808 if (!ctrl_ctx) {
2809 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
2810 __func__);
2811 return;
2812 }
2813
Sarah Sharp913a8a32009-09-04 10:53:13 -07002814 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
2815 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002816 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
2817 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
2818 deq_state->new_deq_ptr);
2819 if (addr == 0) {
2820 xhci_warn(xhci, "WARN Cannot submit config ep after "
2821 "reset ep command\n");
2822 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
2823 deq_state->new_deq_seg,
2824 deq_state->new_deq_ptr);
2825 return;
2826 }
Matt Evans28ccd292011-03-29 13:40:46 +11002827 ep_ctx->deq = cpu_to_le64(addr | deq_state->new_cycle_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002828
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002829 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07002830 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07002831 xhci->devs[slot_id]->out_ctx, ctrl_ctx,
2832 added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002833}
2834
Sarah Sharp82d10092009-08-07 14:04:52 -07002835void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002836 struct usb_device *udev, unsigned int ep_index)
Sarah Sharp82d10092009-08-07 14:04:52 -07002837{
2838 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002839 struct xhci_virt_ep *ep;
Sarah Sharp82d10092009-08-07 14:04:52 -07002840
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002841 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2842 "Cleaning up stalled endpoint ring");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002843 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07002844 /* We need to move the HW's dequeue pointer past this TD,
2845 * or it will attempt to resend it on the next doorbell ring.
2846 */
2847 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002848 ep_index, ep->stopped_stream, ep->stopped_td,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002849 &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07002850
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002851 /* HW with the reset endpoint quirk will use the saved dequeue state to
2852 * issue a configure endpoint command later.
2853 */
2854 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002855 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2856 "Queueing new dequeue state");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002857 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002858 ep_index, ep->stopped_stream, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002859 } else {
2860 /* Better hope no one uses the input context between now and the
2861 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07002862 * XXX: No idea how this hardware will react when stream rings
2863 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002864 */
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03002865 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
2866 "Setting up input context for "
2867 "configure endpoint command");
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07002868 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
2869 ep_index, &deq_state);
2870 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002871}
2872
Sarah Sharpa1587d92009-07-27 12:03:15 -07002873/* Deal with stalled endpoints. The core should have sent the control message
2874 * to clear the halt condition. However, we need to make the xHCI hardware
2875 * reset its sequence number, since a device will expect a sequence number of
2876 * zero after the halt condition is cleared.
2877 * Context: in_interrupt
2878 */
2879void xhci_endpoint_reset(struct usb_hcd *hcd,
2880 struct usb_host_endpoint *ep)
2881{
2882 struct xhci_hcd *xhci;
2883 struct usb_device *udev;
2884 unsigned int ep_index;
2885 unsigned long flags;
2886 int ret;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002887 struct xhci_virt_ep *virt_ep;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002888
2889 xhci = hcd_to_xhci(hcd);
2890 udev = (struct usb_device *) ep->hcpriv;
2891 /* Called with a root hub endpoint (or an endpoint that wasn't added
2892 * with xhci_add_endpoint()
2893 */
2894 if (!ep->hcpriv)
2895 return;
2896 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002897 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
2898 if (!virt_ep->stopped_td) {
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002899 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2900 "Endpoint 0x%x not halted, refusing to reset.",
2901 ep->desc.bEndpointAddress);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002902 return;
2903 }
Sarah Sharp82d10092009-08-07 14:04:52 -07002904 if (usb_endpoint_xfer_control(&ep->desc)) {
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002905 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2906 "Control endpoint stall already handled.");
Sarah Sharp82d10092009-08-07 14:04:52 -07002907 return;
2908 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07002909
Xenia Ragiadakoua0254322013-08-06 07:52:46 +03002910 xhci_dbg_trace(xhci, trace_xhci_dbg_reset_ep,
2911 "Queueing reset endpoint command");
Sarah Sharpa1587d92009-07-27 12:03:15 -07002912 spin_lock_irqsave(&xhci->lock, flags);
2913 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07002914 /*
2915 * Can't change the ring dequeue pointer until it's transitioned to the
2916 * stopped state, which is only upon a successful reset endpoint
2917 * command. Better hope that last command worked!
2918 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07002919 if (!ret) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07002920 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
2921 kfree(virt_ep->stopped_td);
Sarah Sharpa1587d92009-07-27 12:03:15 -07002922 xhci_ring_cmd_db(xhci);
2923 }
Sarah Sharp1624ae12010-05-06 13:40:08 -07002924 virt_ep->stopped_td = NULL;
2925 virt_ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07002926 virt_ep->stopped_stream = 0;
Sarah Sharpa1587d92009-07-27 12:03:15 -07002927 spin_unlock_irqrestore(&xhci->lock, flags);
2928
2929 if (ret)
2930 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
2931}
2932
Sarah Sharp8df75f42010-04-02 15:34:16 -07002933static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
2934 struct usb_device *udev, struct usb_host_endpoint *ep,
2935 unsigned int slot_id)
2936{
2937 int ret;
2938 unsigned int ep_index;
2939 unsigned int ep_state;
2940
2941 if (!ep)
2942 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07002943 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07002944 if (ret <= 0)
2945 return -EINVAL;
Alan Stern842f1692010-04-30 12:44:46 -04002946 if (ep->ss_ep_comp.bmAttributes == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07002947 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
2948 " descriptor for ep 0x%x does not support streams\n",
2949 ep->desc.bEndpointAddress);
2950 return -EINVAL;
2951 }
2952
2953 ep_index = xhci_get_endpoint_index(&ep->desc);
2954 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
2955 if (ep_state & EP_HAS_STREAMS ||
2956 ep_state & EP_GETTING_STREAMS) {
2957 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
2958 "already has streams set up.\n",
2959 ep->desc.bEndpointAddress);
2960 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
2961 "dynamic stream context array reallocation.\n");
2962 return -EINVAL;
2963 }
2964 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
2965 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
2966 "endpoint 0x%x; URBs are pending.\n",
2967 ep->desc.bEndpointAddress);
2968 return -EINVAL;
2969 }
2970 return 0;
2971}
2972
2973static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
2974 unsigned int *num_streams, unsigned int *num_stream_ctxs)
2975{
2976 unsigned int max_streams;
2977
2978 /* The stream context array size must be a power of two */
2979 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
2980 /*
2981 * Find out how many primary stream array entries the host controller
2982 * supports. Later we may use secondary stream arrays (similar to 2nd
2983 * level page entries), but that's an optional feature for xHCI host
2984 * controllers. xHCs must support at least 4 stream IDs.
2985 */
2986 max_streams = HCC_MAX_PSA(xhci->hcc_params);
2987 if (*num_stream_ctxs > max_streams) {
2988 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
2989 max_streams);
2990 *num_stream_ctxs = max_streams;
2991 *num_streams = max_streams;
2992 }
2993}
2994
2995/* Returns an error code if one of the endpoint already has streams.
2996 * This does not change any data structures, it only checks and gathers
2997 * information.
2998 */
2999static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
3000 struct usb_device *udev,
3001 struct usb_host_endpoint **eps, unsigned int num_eps,
3002 unsigned int *num_streams, u32 *changed_ep_bitmask)
3003{
Sarah Sharp8df75f42010-04-02 15:34:16 -07003004 unsigned int max_streams;
3005 unsigned int endpoint_flag;
3006 int i;
3007 int ret;
3008
3009 for (i = 0; i < num_eps; i++) {
3010 ret = xhci_check_streams_endpoint(xhci, udev,
3011 eps[i], udev->slot_id);
3012 if (ret < 0)
3013 return ret;
3014
Felipe Balbi18b7ede2012-01-02 13:35:41 +02003015 max_streams = usb_ss_max_streams(&eps[i]->ss_ep_comp);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003016 if (max_streams < (*num_streams - 1)) {
3017 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
3018 eps[i]->desc.bEndpointAddress,
3019 max_streams);
3020 *num_streams = max_streams+1;
3021 }
3022
3023 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
3024 if (*changed_ep_bitmask & endpoint_flag)
3025 return -EINVAL;
3026 *changed_ep_bitmask |= endpoint_flag;
3027 }
3028 return 0;
3029}
3030
3031static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
3032 struct usb_device *udev,
3033 struct usb_host_endpoint **eps, unsigned int num_eps)
3034{
3035 u32 changed_ep_bitmask = 0;
3036 unsigned int slot_id;
3037 unsigned int ep_index;
3038 unsigned int ep_state;
3039 int i;
3040
3041 slot_id = udev->slot_id;
3042 if (!xhci->devs[slot_id])
3043 return 0;
3044
3045 for (i = 0; i < num_eps; i++) {
3046 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3047 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
3048 /* Are streams already being freed for the endpoint? */
3049 if (ep_state & EP_GETTING_NO_STREAMS) {
3050 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003051 "endpoint 0x%x, "
3052 "streams are being disabled already\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003053 eps[i]->desc.bEndpointAddress);
3054 return 0;
3055 }
3056 /* Are there actually any streams to free? */
3057 if (!(ep_state & EP_HAS_STREAMS) &&
3058 !(ep_state & EP_GETTING_STREAMS)) {
3059 xhci_warn(xhci, "WARN Can't disable streams for "
Joe Perches03e64e92013-07-16 19:25:59 -07003060 "endpoint 0x%x, "
3061 "streams are already disabled!\n",
Sarah Sharp8df75f42010-04-02 15:34:16 -07003062 eps[i]->desc.bEndpointAddress);
3063 xhci_warn(xhci, "WARN xhci_free_streams() called "
3064 "with non-streams endpoint\n");
3065 return 0;
3066 }
3067 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
3068 }
3069 return changed_ep_bitmask;
3070}
3071
3072/*
3073 * The USB device drivers use this function (though the HCD interface in USB
3074 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
3075 * coordinate mass storage command queueing across multiple endpoints (basically
3076 * a stream ID == a task ID).
3077 *
3078 * Setting up streams involves allocating the same size stream context array
3079 * for each endpoint and issuing a configure endpoint command for all endpoints.
3080 *
3081 * Don't allow the call to succeed if one endpoint only supports one stream
3082 * (which means it doesn't support streams at all).
3083 *
3084 * Drivers may get less stream IDs than they asked for, if the host controller
3085 * hardware or endpoints claim they can't support the number of requested
3086 * stream IDs.
3087 */
3088int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
3089 struct usb_host_endpoint **eps, unsigned int num_eps,
3090 unsigned int num_streams, gfp_t mem_flags)
3091{
3092 int i, ret;
3093 struct xhci_hcd *xhci;
3094 struct xhci_virt_device *vdev;
3095 struct xhci_command *config_cmd;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003096 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003097 unsigned int ep_index;
3098 unsigned int num_stream_ctxs;
3099 unsigned long flags;
3100 u32 changed_ep_bitmask = 0;
3101
3102 if (!eps)
3103 return -EINVAL;
3104
3105 /* Add one to the number of streams requested to account for
3106 * stream 0 that is reserved for xHCI usage.
3107 */
3108 num_streams += 1;
3109 xhci = hcd_to_xhci(hcd);
3110 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
3111 num_streams);
3112
3113 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
3114 if (!config_cmd) {
3115 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
3116 return -ENOMEM;
3117 }
Sarah Sharp92f8e762013-04-23 17:11:14 -07003118 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
3119 if (!ctrl_ctx) {
3120 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3121 __func__);
3122 xhci_free_command(xhci, config_cmd);
3123 return -ENOMEM;
3124 }
Sarah Sharp8df75f42010-04-02 15:34:16 -07003125
3126 /* Check to make sure all endpoints are not already configured for
3127 * streams. While we're at it, find the maximum number of streams that
3128 * all the endpoints will support and check for duplicate endpoints.
3129 */
3130 spin_lock_irqsave(&xhci->lock, flags);
3131 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
3132 num_eps, &num_streams, &changed_ep_bitmask);
3133 if (ret < 0) {
3134 xhci_free_command(xhci, config_cmd);
3135 spin_unlock_irqrestore(&xhci->lock, flags);
3136 return ret;
3137 }
3138 if (num_streams <= 1) {
3139 xhci_warn(xhci, "WARN: endpoints can't handle "
3140 "more than one stream.\n");
3141 xhci_free_command(xhci, config_cmd);
3142 spin_unlock_irqrestore(&xhci->lock, flags);
3143 return -EINVAL;
3144 }
3145 vdev = xhci->devs[udev->slot_id];
Lucas De Marchi25985ed2011-03-30 22:57:33 -03003146 /* Mark each endpoint as being in transition, so
Sarah Sharp8df75f42010-04-02 15:34:16 -07003147 * xhci_urb_enqueue() will reject all URBs.
3148 */
3149 for (i = 0; i < num_eps; i++) {
3150 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3151 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
3152 }
3153 spin_unlock_irqrestore(&xhci->lock, flags);
3154
3155 /* Setup internal data structures and allocate HW data structures for
3156 * streams (but don't install the HW structures in the input context
3157 * until we're sure all memory allocation succeeded).
3158 */
3159 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
3160 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
3161 num_stream_ctxs, num_streams);
3162
3163 for (i = 0; i < num_eps; i++) {
3164 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3165 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
3166 num_stream_ctxs,
3167 num_streams, mem_flags);
3168 if (!vdev->eps[ep_index].stream_info)
3169 goto cleanup;
3170 /* Set maxPstreams in endpoint context and update deq ptr to
3171 * point to stream context array. FIXME
3172 */
3173 }
3174
3175 /* Set up the input context for a configure endpoint command. */
3176 for (i = 0; i < num_eps; i++) {
3177 struct xhci_ep_ctx *ep_ctx;
3178
3179 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3180 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
3181
3182 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
3183 vdev->out_ctx, ep_index);
3184 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
3185 vdev->eps[ep_index].stream_info);
3186 }
3187 /* Tell the HW to drop its old copy of the endpoint context info
3188 * and add the updated copy from the input context.
3189 */
3190 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003191 vdev->out_ctx, ctrl_ctx,
3192 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003193
3194 /* Issue and wait for the configure endpoint command */
3195 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
3196 false, false);
3197
3198 /* xHC rejected the configure endpoint command for some reason, so we
3199 * leave the old ring intact and free our internal streams data
3200 * structure.
3201 */
3202 if (ret < 0)
3203 goto cleanup;
3204
3205 spin_lock_irqsave(&xhci->lock, flags);
3206 for (i = 0; i < num_eps; i++) {
3207 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3208 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3209 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
3210 udev->slot_id, ep_index);
3211 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
3212 }
3213 xhci_free_command(xhci, config_cmd);
3214 spin_unlock_irqrestore(&xhci->lock, flags);
3215
3216 /* Subtract 1 for stream 0, which drivers can't use */
3217 return num_streams - 1;
3218
3219cleanup:
3220 /* If it didn't work, free the streams! */
3221 for (i = 0; i < num_eps; i++) {
3222 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3223 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003224 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003225 /* FIXME Unset maxPstreams in endpoint context and
3226 * update deq ptr to point to normal string ring.
3227 */
3228 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
3229 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3230 xhci_endpoint_zero(xhci, vdev, eps[i]);
3231 }
3232 xhci_free_command(xhci, config_cmd);
3233 return -ENOMEM;
3234}
3235
3236/* Transition the endpoint from using streams to being a "normal" endpoint
3237 * without streams.
3238 *
3239 * Modify the endpoint context state, submit a configure endpoint command,
3240 * and free all endpoint rings for streams if that completes successfully.
3241 */
3242int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
3243 struct usb_host_endpoint **eps, unsigned int num_eps,
3244 gfp_t mem_flags)
3245{
3246 int i, ret;
3247 struct xhci_hcd *xhci;
3248 struct xhci_virt_device *vdev;
3249 struct xhci_command *command;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003250 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003251 unsigned int ep_index;
3252 unsigned long flags;
3253 u32 changed_ep_bitmask;
3254
3255 xhci = hcd_to_xhci(hcd);
3256 vdev = xhci->devs[udev->slot_id];
3257
3258 /* Set up a configure endpoint command to remove the streams rings */
3259 spin_lock_irqsave(&xhci->lock, flags);
3260 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
3261 udev, eps, num_eps);
3262 if (changed_ep_bitmask == 0) {
3263 spin_unlock_irqrestore(&xhci->lock, flags);
3264 return -EINVAL;
3265 }
3266
3267 /* Use the xhci_command structure from the first endpoint. We may have
3268 * allocated too many, but the driver may call xhci_free_streams() for
3269 * each endpoint it grouped into one call to xhci_alloc_streams().
3270 */
3271 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
3272 command = vdev->eps[ep_index].stream_info->free_streams_command;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003273 ctrl_ctx = xhci_get_input_control_ctx(xhci, command->in_ctx);
3274 if (!ctrl_ctx) {
Emil Goode1f215692013-06-25 15:49:36 -07003275 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003276 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3277 __func__);
3278 return -EINVAL;
3279 }
3280
Sarah Sharp8df75f42010-04-02 15:34:16 -07003281 for (i = 0; i < num_eps; i++) {
3282 struct xhci_ep_ctx *ep_ctx;
3283
3284 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3285 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
3286 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
3287 EP_GETTING_NO_STREAMS;
3288
3289 xhci_endpoint_copy(xhci, command->in_ctx,
3290 vdev->out_ctx, ep_index);
3291 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
3292 &vdev->eps[ep_index]);
3293 }
3294 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
Sarah Sharp92f8e762013-04-23 17:11:14 -07003295 vdev->out_ctx, ctrl_ctx,
3296 changed_ep_bitmask, changed_ep_bitmask);
Sarah Sharp8df75f42010-04-02 15:34:16 -07003297 spin_unlock_irqrestore(&xhci->lock, flags);
3298
3299 /* Issue and wait for the configure endpoint command,
3300 * which must succeed.
3301 */
3302 ret = xhci_configure_endpoint(xhci, udev, command,
3303 false, true);
3304
3305 /* xHC rejected the configure endpoint command for some reason, so we
3306 * leave the streams rings intact.
3307 */
3308 if (ret < 0)
3309 return ret;
3310
3311 spin_lock_irqsave(&xhci->lock, flags);
3312 for (i = 0; i < num_eps; i++) {
3313 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
3314 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07003315 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07003316 /* FIXME Unset maxPstreams in endpoint context and
3317 * update deq ptr to point to normal string ring.
3318 */
3319 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
3320 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
3321 }
3322 spin_unlock_irqrestore(&xhci->lock, flags);
3323
3324 return 0;
3325}
3326
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003327/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003328 * Deletes endpoint resources for endpoints that were active before a Reset
3329 * Device command, or a Disable Slot command. The Reset Device command leaves
3330 * the control endpoint intact, whereas the Disable Slot command deletes it.
3331 *
3332 * Must be called with xhci->lock held.
3333 */
3334void xhci_free_device_endpoint_resources(struct xhci_hcd *xhci,
3335 struct xhci_virt_device *virt_dev, bool drop_control_ep)
3336{
3337 int i;
3338 unsigned int num_dropped_eps = 0;
3339 unsigned int drop_flags = 0;
3340
3341 for (i = (drop_control_ep ? 0 : 1); i < 31; i++) {
3342 if (virt_dev->eps[i].ring) {
3343 drop_flags |= 1 << i;
3344 num_dropped_eps++;
3345 }
3346 }
3347 xhci->num_active_eps -= num_dropped_eps;
3348 if (num_dropped_eps)
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003349 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3350 "Dropped %u ep ctxs, flags = 0x%x, "
3351 "%u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003352 num_dropped_eps, drop_flags,
3353 xhci->num_active_eps);
3354}
3355
3356/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003357 * This submits a Reset Device Command, which will set the device state to 0,
3358 * set the device address to 0, and disable all the endpoints except the default
3359 * control endpoint. The USB core should come back and call
3360 * xhci_address_device(), and then re-set up the configuration. If this is
3361 * called because of a usb_reset_and_verify_device(), then the old alternate
3362 * settings will be re-installed through the normal bandwidth allocation
3363 * functions.
3364 *
3365 * Wait for the Reset Device command to finish. Remove all structures
3366 * associated with the endpoints that were disabled. Clear the input device
3367 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07003368 *
3369 * If the virt_dev to be reset does not exist or does not match the udev,
3370 * it means the device is lost, possibly due to the xHC restore error and
3371 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
3372 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003373 */
Andiry Xuf0615c42010-10-14 07:22:48 -07003374int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003375{
3376 int ret, i;
3377 unsigned long flags;
3378 struct xhci_hcd *xhci;
3379 unsigned int slot_id;
3380 struct xhci_virt_device *virt_dev;
3381 struct xhci_command *reset_device_cmd;
3382 int timeleft;
3383 int last_freed_endpoint;
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003384 struct xhci_slot_ctx *slot_ctx;
Sarah Sharp2e279802011-09-02 11:05:50 -07003385 int old_active_eps = 0;
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003386
Andiry Xuf0615c42010-10-14 07:22:48 -07003387 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003388 if (ret <= 0)
3389 return ret;
3390 xhci = hcd_to_xhci(hcd);
3391 slot_id = udev->slot_id;
3392 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07003393 if (!virt_dev) {
3394 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3395 "not exist. Re-allocate the device\n", slot_id);
3396 ret = xhci_alloc_dev(hcd, udev);
3397 if (ret == 1)
3398 return 0;
3399 else
3400 return -EINVAL;
3401 }
3402
3403 if (virt_dev->udev != udev) {
3404 /* If the virt_dev and the udev does not match, this virt_dev
3405 * may belong to another udev.
3406 * Re-allocate the device.
3407 */
3408 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
3409 "not match the udev. Re-allocate the device\n",
3410 slot_id);
3411 ret = xhci_alloc_dev(hcd, udev);
3412 if (ret == 1)
3413 return 0;
3414 else
3415 return -EINVAL;
3416 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003417
Maarten Lankhorst001fd382011-06-01 23:27:50 +02003418 /* If device is not setup, there is no point in resetting it */
3419 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
3420 if (GET_SLOT_STATE(le32_to_cpu(slot_ctx->dev_state)) ==
3421 SLOT_STATE_DISABLED)
3422 return 0;
3423
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003424 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
3425 /* Allocate the command structure that holds the struct completion.
3426 * Assume we're in process context, since the normal device reset
3427 * process has to wait for the device anyway. Storage devices are
3428 * reset as part of error handling, so use GFP_NOIO instead of
3429 * GFP_KERNEL.
3430 */
3431 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
3432 if (!reset_device_cmd) {
3433 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
3434 return -ENOMEM;
3435 }
3436
3437 /* Attempt to submit the Reset Device command to the command ring */
3438 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanec7e43e2013-08-30 18:25:49 +03003439 reset_device_cmd->command_trb = xhci_find_next_enqueue(xhci->cmd_ring);
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08003440
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003441 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
3442 ret = xhci_queue_reset_device(xhci, slot_id);
3443 if (ret) {
3444 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3445 list_del(&reset_device_cmd->cmd_list);
3446 spin_unlock_irqrestore(&xhci->lock, flags);
3447 goto command_cleanup;
3448 }
3449 xhci_ring_cmd_db(xhci);
3450 spin_unlock_irqrestore(&xhci->lock, flags);
3451
3452 /* Wait for the Reset Device command to finish */
3453 timeleft = wait_for_completion_interruptible_timeout(
3454 reset_device_cmd->completion,
3455 USB_CTRL_SET_TIMEOUT);
3456 if (timeleft <= 0) {
3457 xhci_warn(xhci, "%s while waiting for reset device command\n",
3458 timeleft == 0 ? "Timeout" : "Signal");
3459 spin_lock_irqsave(&xhci->lock, flags);
3460 /* The timeout might have raced with the event ring handler, so
3461 * only delete from the list if the item isn't poisoned.
3462 */
3463 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
3464 list_del(&reset_device_cmd->cmd_list);
3465 spin_unlock_irqrestore(&xhci->lock, flags);
3466 ret = -ETIME;
3467 goto command_cleanup;
3468 }
3469
3470 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
3471 * unless we tried to reset a slot ID that wasn't enabled,
3472 * or the device wasn't in the addressed or configured state.
3473 */
3474 ret = reset_device_cmd->status;
3475 switch (ret) {
3476 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
3477 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003478 xhci_dbg(xhci, "Can't reset device (slot ID %u) in %s state\n",
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003479 slot_id,
3480 xhci_get_slot_state(xhci, virt_dev->out_ctx));
Xenia Ragiadakou38a532a2013-07-02 17:49:25 +03003481 xhci_dbg(xhci, "Not freeing device rings.\n");
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003482 /* Don't treat this as an error. May change my mind later. */
3483 ret = 0;
3484 goto command_cleanup;
3485 case COMP_SUCCESS:
3486 xhci_dbg(xhci, "Successful reset device command.\n");
3487 break;
3488 default:
3489 if (xhci_is_vendor_info_code(xhci, ret))
3490 break;
3491 xhci_warn(xhci, "Unknown completion code %u for "
3492 "reset device command.\n", ret);
3493 ret = -EINVAL;
3494 goto command_cleanup;
3495 }
3496
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003497 /* Free up host controller endpoint resources */
3498 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3499 spin_lock_irqsave(&xhci->lock, flags);
3500 /* Don't delete the default control endpoint resources */
3501 xhci_free_device_endpoint_resources(xhci, virt_dev, false);
3502 spin_unlock_irqrestore(&xhci->lock, flags);
3503 }
3504
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003505 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
3506 last_freed_endpoint = 1;
3507 for (i = 1; i < 31; ++i) {
Dmitry Torokhov2dea75d2011-04-12 23:06:28 -07003508 struct xhci_virt_ep *ep = &virt_dev->eps[i];
3509
3510 if (ep->ep_state & EP_HAS_STREAMS) {
3511 xhci_free_stream_info(xhci, ep->stream_info);
3512 ep->stream_info = NULL;
3513 ep->ep_state &= ~EP_HAS_STREAMS;
3514 }
3515
3516 if (ep->ring) {
3517 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
3518 last_freed_endpoint = i;
3519 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003520 if (!list_empty(&virt_dev->eps[i].bw_endpoint_list))
3521 xhci_drop_ep_from_interval_table(xhci,
3522 &virt_dev->eps[i].bw_info,
3523 virt_dev->bw_table,
3524 udev,
3525 &virt_dev->eps[i],
3526 virt_dev->tt_info);
Sarah Sharp9af5d712011-09-02 11:05:48 -07003527 xhci_clear_endpoint_bw_info(&virt_dev->eps[i].bw_info);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003528 }
Sarah Sharp2e279802011-09-02 11:05:50 -07003529 /* If necessary, update the number of active TTs on this root port */
3530 xhci_update_tt_active_eps(xhci, virt_dev, old_active_eps);
3531
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08003532 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
3533 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
3534 ret = 0;
3535
3536command_cleanup:
3537 xhci_free_command(xhci, reset_device_cmd);
3538 return ret;
3539}
3540
3541/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003542 * At this point, the struct usb_device is about to go away, the device has
3543 * disconnected, and all traffic has been stopped and the endpoints have been
3544 * disabled. Free any HC data structures associated with that device.
3545 */
3546void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
3547{
3548 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003549 struct xhci_virt_device *virt_dev;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003550 unsigned long flags;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003551 u32 state;
Andiry Xu64927732010-10-14 07:22:45 -07003552 int i, ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003553
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003554#ifndef CONFIG_USB_DEFAULT_PERSIST
3555 /*
3556 * We called pm_runtime_get_noresume when the device was attached.
3557 * Decrement the counter here to allow controller to runtime suspend
3558 * if no devices remain.
3559 */
3560 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003561 pm_runtime_put_noidle(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003562#endif
3563
Andiry Xu64927732010-10-14 07:22:45 -07003564 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003565 /* If the host is halted due to driver unload, we still need to free the
3566 * device.
3567 */
3568 if (ret <= 0 && ret != -ENODEV)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003569 return;
Andiry Xu64927732010-10-14 07:22:45 -07003570
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003571 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp6f5165c2009-10-27 10:57:01 -07003572
3573 /* Stop any wayward timer functions (which may grab the lock) */
3574 for (i = 0; i < 31; ++i) {
3575 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
3576 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
3577 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003578
Andiry Xu65580b432011-09-23 14:19:52 -07003579 if (udev->usb2_hw_lpm_enabled) {
3580 xhci_set_usb2_hardware_lpm(hcd, udev, 0);
3581 udev->usb2_hw_lpm_enabled = 0;
3582 }
3583
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003584 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003585 /* Don't disable the slot if the host controller is dead. */
3586 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharp7bd89b42011-07-01 13:35:40 -07003587 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING) ||
3588 (xhci->xhc_state & XHCI_STATE_HALTED)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07003589 xhci_free_virt_device(xhci, udev->slot_id);
3590 spin_unlock_irqrestore(&xhci->lock, flags);
3591 return;
3592 }
3593
Sarah Sharp23e3be12009-04-29 19:05:20 -07003594 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003595 spin_unlock_irqrestore(&xhci->lock, flags);
3596 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3597 return;
3598 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003599 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003600 spin_unlock_irqrestore(&xhci->lock, flags);
3601 /*
3602 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07003603 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003604 */
3605}
3606
3607/*
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003608 * Checks if we have enough host controller resources for the default control
3609 * endpoint.
3610 *
3611 * Must be called with xhci->lock held.
3612 */
3613static int xhci_reserve_host_control_ep_resources(struct xhci_hcd *xhci)
3614{
3615 if (xhci->num_active_eps + 1 > xhci->limit_active_eps) {
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003616 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3617 "Not enough ep ctxs: "
3618 "%u active, need to add 1, limit is %u.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003619 xhci->num_active_eps, xhci->limit_active_eps);
3620 return -ENOMEM;
3621 }
3622 xhci->num_active_eps += 1;
Xenia Ragiadakou4bdfe4c2013-08-06 07:52:45 +03003623 xhci_dbg_trace(xhci, trace_xhci_dbg_quirks,
3624 "Adding 1 ep ctx, %u now active.",
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003625 xhci->num_active_eps);
3626 return 0;
3627}
3628
3629
3630/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003631 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
3632 * timed out, or allocating memory failed. Returns 1 on success.
3633 */
3634int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
3635{
3636 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3637 unsigned long flags;
3638 int timeleft;
3639 int ret;
Elric Fu6e4468b2012-06-27 16:31:52 +08003640 union xhci_trb *cmd_trb;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003641
3642 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanec7e43e2013-08-30 18:25:49 +03003643 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
Sarah Sharp23e3be12009-04-29 19:05:20 -07003644 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003645 if (ret) {
3646 spin_unlock_irqrestore(&xhci->lock, flags);
3647 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
3648 return 0;
3649 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003650 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003651 spin_unlock_irqrestore(&xhci->lock, flags);
3652
3653 /* XXX: how much time for xHC slot assignment? */
3654 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
Elric Fu6e4468b2012-06-27 16:31:52 +08003655 XHCI_CMD_DEFAULT_TIMEOUT);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003656 if (timeleft <= 0) {
3657 xhci_warn(xhci, "%s while waiting for a slot\n",
3658 timeleft == 0 ? "Timeout" : "Signal");
Elric Fu6e4468b2012-06-27 16:31:52 +08003659 /* cancel the enable slot request */
3660 return xhci_cancel_cmd(xhci, NULL, cmd_trb);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003661 }
3662
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003663 if (!xhci->slot_id) {
3664 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003665 return 0;
3666 }
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003667
3668 if ((xhci->quirks & XHCI_EP_LIMIT_QUIRK)) {
3669 spin_lock_irqsave(&xhci->lock, flags);
3670 ret = xhci_reserve_host_control_ep_resources(xhci);
3671 if (ret) {
3672 spin_unlock_irqrestore(&xhci->lock, flags);
3673 xhci_warn(xhci, "Not enough host resources, "
3674 "active endpoint contexts = %u\n",
3675 xhci->num_active_eps);
3676 goto disable_slot;
3677 }
3678 spin_unlock_irqrestore(&xhci->lock, flags);
3679 }
3680 /* Use GFP_NOIO, since this function can be called from
Sarah Sharpa6d940d2010-12-28 13:08:42 -08003681 * xhci_discover_or_reset_device(), which may be called as part of
3682 * mass storage driver error handling.
3683 */
3684 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003685 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003686 goto disable_slot;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003687 }
3688 udev->slot_id = xhci->slot_id;
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003689
3690#ifndef CONFIG_USB_DEFAULT_PERSIST
3691 /*
3692 * If resetting upon resume, we can't put the controller into runtime
3693 * suspend if there is a device attached.
3694 */
3695 if (xhci->quirks & XHCI_RESET_ON_RESUME)
Sarah Sharpe7ecf062013-08-28 09:31:04 -07003696 pm_runtime_get_noresume(hcd->self.controller);
Shawn Nematbakhshc8476fb2013-08-19 10:36:13 -07003697#endif
3698
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003699 /* Is this a LS or FS device under a HS hub? */
3700 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003701 return 1;
Sarah Sharp2cf95c12011-05-11 16:14:58 -07003702
3703disable_slot:
3704 /* Disable slot, if we can do it without mem alloc */
3705 spin_lock_irqsave(&xhci->lock, flags);
3706 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
3707 xhci_ring_cmd_db(xhci);
3708 spin_unlock_irqrestore(&xhci->lock, flags);
3709 return 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003710}
3711
3712/*
3713 * Issue an Address Device command (which will issue a SetAddress request to
3714 * the device).
3715 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
3716 * we should only issue and wait on one address command at the same time.
3717 *
3718 * We add one to the device address issued by the hardware because the USB core
3719 * uses address 1 for the root hubs (even though they're not really devices).
3720 */
3721int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
3722{
3723 unsigned long flags;
3724 int timeleft;
3725 struct xhci_virt_device *virt_dev;
3726 int ret = 0;
3727 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07003728 struct xhci_slot_ctx *slot_ctx;
3729 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07003730 u64 temp_64;
Elric Fu6e4468b2012-06-27 16:31:52 +08003731 union xhci_trb *cmd_trb;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003732
3733 if (!udev->slot_id) {
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003734 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3735 "Bad Slot ID %d", udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003736 return -EINVAL;
3737 }
3738
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003739 virt_dev = xhci->devs[udev->slot_id];
3740
Matt Evans7ed603e2011-03-29 13:40:56 +11003741 if (WARN_ON(!virt_dev)) {
3742 /*
3743 * In plug/unplug torture test with an NEC controller,
3744 * a zero-dereference was observed once due to virt_dev = 0.
3745 * Print useful debug rather than crash if it is observed again!
3746 */
3747 xhci_warn(xhci, "Virt dev invalid for slot_id 0x%x!\n",
3748 udev->slot_id);
3749 return -EINVAL;
3750 }
3751
Andiry Xuf0615c42010-10-14 07:22:48 -07003752 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
Sarah Sharp92f8e762013-04-23 17:11:14 -07003753 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
3754 if (!ctrl_ctx) {
3755 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3756 __func__);
3757 return -EINVAL;
3758 }
Andiry Xuf0615c42010-10-14 07:22:48 -07003759 /*
3760 * If this is the first Set Address since device plug-in or
3761 * virt_device realloaction after a resume with an xHCI power loss,
3762 * then set up the slot context.
3763 */
3764 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003765 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07003766 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02003767 else
3768 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharpd31c2852011-11-03 13:06:08 -07003769 ctrl_ctx->add_flags = cpu_to_le32(SLOT_FLAG | EP0_FLAG);
3770 ctrl_ctx->drop_flags = 0;
3771
Sarah Sharp66e49d82009-07-27 12:03:46 -07003772 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003773 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003774 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
3775 slot_ctx->dev_info >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003776
Sarah Sharpf88ba782009-05-14 11:44:22 -07003777 spin_lock_irqsave(&xhci->lock, flags);
Mathias Nymanec7e43e2013-08-30 18:25:49 +03003778 cmd_trb = xhci_find_next_enqueue(xhci->cmd_ring);
John Yound115b042009-07-27 12:05:15 -07003779 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
3780 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003781 if (ret) {
3782 spin_unlock_irqrestore(&xhci->lock, flags);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003783 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3784 "FIXME: allocate a command ring segment");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003785 return ret;
3786 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07003787 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003788 spin_unlock_irqrestore(&xhci->lock, flags);
3789
3790 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
3791 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
Elric Fu6e4468b2012-06-27 16:31:52 +08003792 XHCI_CMD_DEFAULT_TIMEOUT);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003793 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
3794 * the SetAddress() "recovery interval" required by USB and aborting the
3795 * command on a timeout.
3796 */
3797 if (timeleft <= 0) {
Andiry Xucd681762011-09-23 14:19:55 -07003798 xhci_warn(xhci, "%s while waiting for address device command\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003799 timeleft == 0 ? "Timeout" : "Signal");
Elric Fu6e4468b2012-06-27 16:31:52 +08003800 /* cancel the address device command */
3801 ret = xhci_cancel_cmd(xhci, NULL, cmd_trb);
3802 if (ret < 0)
3803 return ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003804 return -ETIME;
3805 }
3806
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003807 switch (virt_dev->cmd_status) {
3808 case COMP_CTX_STATE:
3809 case COMP_EBADSLT:
3810 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
3811 udev->slot_id);
3812 ret = -EINVAL;
3813 break;
3814 case COMP_TX_ERR:
3815 dev_warn(&udev->dev, "Device not responding to set address.\n");
3816 ret = -EPROTO;
3817 break;
Alex Hef6ba6fe2011-06-08 18:34:06 +08003818 case COMP_DEV_ERR:
3819 dev_warn(&udev->dev, "ERROR: Incompatible device for address "
3820 "device command.\n");
3821 ret = -ENODEV;
3822 break;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003823 case COMP_SUCCESS:
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003824 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3825 "Successful Address Device command");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003826 break;
3827 default:
3828 xhci_err(xhci, "ERROR: unexpected command completion "
3829 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07003830 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003831 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003832 trace_xhci_address_ctx(xhci, virt_dev->out_ctx, 1);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003833 ret = -EINVAL;
3834 break;
3835 }
3836 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003837 return ret;
3838 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07003839 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003840 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3841 "Op regs DCBAA ptr = %#016llx", temp_64);
3842 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3843 "Slot ID %d dcbaa entry @%p = %#016llx",
3844 udev->slot_id,
3845 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
3846 (unsigned long long)
3847 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[udev->slot_id]));
3848 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3849 "Output Context DMA address = %#08llx",
John Yound115b042009-07-27 12:05:15 -07003850 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003851 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003852 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003853 trace_xhci_address_ctx(xhci, virt_dev->in_ctx,
3854 slot_ctx->dev_info >> 27);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003855 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07003856 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003857 /*
3858 * USB core uses address 1 for the roothubs, so we add one to the
3859 * address given back to us by the HC.
3860 */
John Yound115b042009-07-27 12:05:15 -07003861 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
Xenia Ragiadakou1d27fab2013-08-06 07:52:47 +03003862 trace_xhci_address_ctx(xhci, virt_dev->out_ctx,
3863 slot_ctx->dev_info >> 27);
Andiry Xuc8d4af82010-10-14 07:22:51 -07003864 /* Use kernel assigned address for devices; store xHC assigned
3865 * address locally. */
Matt Evans28ccd292011-03-29 13:40:46 +11003866 virt_dev->address = (le32_to_cpu(slot_ctx->dev_state) & DEV_ADDR_MASK)
3867 + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07003868 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07003869 ctrl_ctx->add_flags = 0;
3870 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003871
Xenia Ragiadakou84a99f62013-08-06 00:22:15 +03003872 xhci_dbg_trace(xhci, trace_xhci_dbg_address,
3873 "Internal device address = %d", virt_dev->address);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07003874
3875 return 0;
3876}
3877
Lan Tianyu3f5eb142013-03-19 16:48:12 +08003878/*
3879 * Transfer the port index into real index in the HW port status
3880 * registers. Caculate offset between the port's PORTSC register
3881 * and port status base. Divide the number of per port register
3882 * to get the real index. The raw port number bases 1.
3883 */
3884int xhci_find_raw_port_number(struct usb_hcd *hcd, int port1)
3885{
3886 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
3887 __le32 __iomem *base_addr = &xhci->op_regs->port_status_base;
3888 __le32 __iomem *addr;
3889 int raw_port;
3890
3891 if (hcd->speed != HCD_USB3)
3892 addr = xhci->usb2_ports[port1 - 1];
3893 else
3894 addr = xhci->usb3_ports[port1 - 1];
3895
3896 raw_port = (addr - base_addr)/NUM_PORT_REGS + 1;
3897 return raw_port;
3898}
3899
Mathias Nymana558ccd2013-05-23 17:14:30 +03003900/*
3901 * Issue an Evaluate Context command to change the Maximum Exit Latency in the
3902 * slot context. If that succeeds, store the new MEL in the xhci_virt_device.
3903 */
Olof Johanssond5c82fe2013-07-23 11:58:20 -07003904static int __maybe_unused xhci_change_max_exit_latency(struct xhci_hcd *xhci,
Mathias Nymana558ccd2013-05-23 17:14:30 +03003905 struct usb_device *udev, u16 max_exit_latency)
3906{
3907 struct xhci_virt_device *virt_dev;
3908 struct xhci_command *command;
3909 struct xhci_input_control_ctx *ctrl_ctx;
3910 struct xhci_slot_ctx *slot_ctx;
3911 unsigned long flags;
3912 int ret;
3913
3914 spin_lock_irqsave(&xhci->lock, flags);
3915 if (max_exit_latency == xhci->devs[udev->slot_id]->current_mel) {
3916 spin_unlock_irqrestore(&xhci->lock, flags);
3917 return 0;
3918 }
3919
3920 /* Attempt to issue an Evaluate Context command to change the MEL. */
3921 virt_dev = xhci->devs[udev->slot_id];
3922 command = xhci->lpm_command;
Sarah Sharp92f8e762013-04-23 17:11:14 -07003923 ctrl_ctx = xhci_get_input_control_ctx(xhci, command->in_ctx);
3924 if (!ctrl_ctx) {
3925 spin_unlock_irqrestore(&xhci->lock, flags);
3926 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
3927 __func__);
3928 return -ENOMEM;
3929 }
3930
Mathias Nymana558ccd2013-05-23 17:14:30 +03003931 xhci_slot_copy(xhci, command->in_ctx, virt_dev->out_ctx);
3932 spin_unlock_irqrestore(&xhci->lock, flags);
3933
Mathias Nymana558ccd2013-05-23 17:14:30 +03003934 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
3935 slot_ctx = xhci_get_slot_ctx(xhci, command->in_ctx);
3936 slot_ctx->dev_info2 &= cpu_to_le32(~((u32) MAX_EXIT));
3937 slot_ctx->dev_info2 |= cpu_to_le32(max_exit_latency);
3938
Xenia Ragiadakou3a7fa5b2013-07-31 07:35:27 +03003939 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
3940 "Set up evaluate context for LPM MEL change.");
Mathias Nymana558ccd2013-05-23 17:14:30 +03003941 xhci_dbg(xhci, "Slot %u Input Context:\n", udev->slot_id);
3942 xhci_dbg_ctx(xhci, command->in_ctx, 0);
3943
3944 /* Issue and wait for the evaluate context command. */
3945 ret = xhci_configure_endpoint(xhci, udev, command,
3946 true, true);
3947 xhci_dbg(xhci, "Slot %u Output Context:\n", udev->slot_id);
3948 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 0);
3949
3950 if (!ret) {
3951 spin_lock_irqsave(&xhci->lock, flags);
3952 virt_dev->current_mel = max_exit_latency;
3953 spin_unlock_irqrestore(&xhci->lock, flags);
3954 }
3955 return ret;
3956}
3957
Alan Stern84ebc102013-03-27 16:14:46 -04003958#ifdef CONFIG_PM_RUNTIME
Andiry Xu95743232011-09-23 14:19:51 -07003959
3960/* BESL to HIRD Encoding array for USB2 LPM */
3961static int xhci_besl_encoding[16] = {125, 150, 200, 300, 400, 500, 1000, 2000,
3962 3000, 4000, 5000, 6000, 7000, 8000, 9000, 10000};
3963
3964/* Calculate HIRD/BESL for USB2 PORTPMSC*/
Andiry Xuf99298b2011-12-12 16:45:28 +08003965static int xhci_calculate_hird_besl(struct xhci_hcd *xhci,
3966 struct usb_device *udev)
Andiry Xu95743232011-09-23 14:19:51 -07003967{
Andiry Xuf99298b2011-12-12 16:45:28 +08003968 int u2del, besl, besl_host;
3969 int besl_device = 0;
3970 u32 field;
Andiry Xu95743232011-09-23 14:19:51 -07003971
Andiry Xuf99298b2011-12-12 16:45:28 +08003972 u2del = HCS_U2_LATENCY(xhci->hcs_params3);
3973 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
3974
3975 if (field & USB_BESL_SUPPORT) {
3976 for (besl_host = 0; besl_host < 16; besl_host++) {
3977 if (xhci_besl_encoding[besl_host] >= u2del)
Andiry Xu95743232011-09-23 14:19:51 -07003978 break;
3979 }
Andiry Xuf99298b2011-12-12 16:45:28 +08003980 /* Use baseline BESL value as default */
3981 if (field & USB_BESL_BASELINE_VALID)
3982 besl_device = USB_GET_BESL_BASELINE(field);
3983 else if (field & USB_BESL_DEEP_VALID)
3984 besl_device = USB_GET_BESL_DEEP(field);
Andiry Xu95743232011-09-23 14:19:51 -07003985 } else {
3986 if (u2del <= 50)
Andiry Xuf99298b2011-12-12 16:45:28 +08003987 besl_host = 0;
Andiry Xu95743232011-09-23 14:19:51 -07003988 else
Andiry Xuf99298b2011-12-12 16:45:28 +08003989 besl_host = (u2del - 51) / 75 + 1;
Andiry Xu95743232011-09-23 14:19:51 -07003990 }
3991
Andiry Xuf99298b2011-12-12 16:45:28 +08003992 besl = besl_host + besl_device;
3993 if (besl > 15)
3994 besl = 15;
3995
3996 return besl;
Andiry Xu95743232011-09-23 14:19:51 -07003997}
3998
Mathias Nymana558ccd2013-05-23 17:14:30 +03003999/* Calculate BESLD, L1 timeout and HIRDM for USB2 PORTHLPMC */
4000static int xhci_calculate_usb2_hw_lpm_params(struct usb_device *udev)
4001{
4002 u32 field;
4003 int l1;
4004 int besld = 0;
4005 int hirdm = 0;
4006
4007 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
4008
4009 /* xHCI l1 is set in steps of 256us, xHCI 1.0 section 5.4.11.2 */
Mathias Nyman17f34862013-05-23 17:14:31 +03004010 l1 = udev->l1_params.timeout / 256;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004011
4012 /* device has preferred BESLD */
4013 if (field & USB_BESL_DEEP_VALID) {
4014 besld = USB_GET_BESL_DEEP(field);
4015 hirdm = 1;
4016 }
4017
4018 return PORT_BESLD(besld) | PORT_L1_TIMEOUT(l1) | PORT_HIRDM(hirdm);
4019}
4020
Andiry Xu95743232011-09-23 14:19:51 -07004021static int xhci_usb2_software_lpm_test(struct usb_hcd *hcd,
4022 struct usb_device *udev)
4023{
4024 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4025 struct dev_info *dev_info;
4026 __le32 __iomem **port_array;
4027 __le32 __iomem *addr, *pm_addr;
4028 u32 temp, dev_id;
4029 unsigned int port_num;
4030 unsigned long flags;
Andiry Xuf99298b2011-12-12 16:45:28 +08004031 int hird;
Andiry Xu95743232011-09-23 14:19:51 -07004032 int ret;
4033
4034 if (hcd->speed == HCD_USB3 || !xhci->sw_lpm_support ||
4035 !udev->lpm_capable)
4036 return -EINVAL;
4037
4038 /* we only support lpm for non-hub device connected to root hub yet */
4039 if (!udev->parent || udev->parent->parent ||
4040 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4041 return -EINVAL;
4042
4043 spin_lock_irqsave(&xhci->lock, flags);
4044
4045 /* Look for devices in lpm_failed_devs list */
4046 dev_id = le16_to_cpu(udev->descriptor.idVendor) << 16 |
4047 le16_to_cpu(udev->descriptor.idProduct);
4048 list_for_each_entry(dev_info, &xhci->lpm_failed_devs, list) {
4049 if (dev_info->dev_id == dev_id) {
4050 ret = -EINVAL;
4051 goto finish;
4052 }
4053 }
4054
4055 port_array = xhci->usb2_ports;
4056 port_num = udev->portnum - 1;
4057
4058 if (port_num > HCS_MAX_PORTS(xhci->hcs_params1)) {
4059 xhci_dbg(xhci, "invalid port number %d\n", udev->portnum);
4060 ret = -EINVAL;
4061 goto finish;
4062 }
4063
4064 /*
4065 * Test USB 2.0 software LPM.
4066 * FIXME: some xHCI 1.0 hosts may implement a new register to set up
4067 * hardware-controlled USB 2.0 LPM. See section 5.4.11 and 4.23.5.1.1.1
4068 * in the June 2011 errata release.
4069 */
4070 xhci_dbg(xhci, "test port %d software LPM\n", port_num);
4071 /*
4072 * Set L1 Device Slot and HIRD/BESL.
4073 * Check device's USB 2.0 extension descriptor to determine whether
4074 * HIRD or BESL shoule be used. See USB2.0 LPM errata.
4075 */
Mathias Nymanb6e76372013-05-23 17:14:29 +03004076 pm_addr = port_array[port_num] + PORTPMSC;
Andiry Xuf99298b2011-12-12 16:45:28 +08004077 hird = xhci_calculate_hird_besl(xhci, udev);
Andiry Xu95743232011-09-23 14:19:51 -07004078 temp = PORT_L1DS(udev->slot_id) | PORT_HIRD(hird);
4079 xhci_writel(xhci, temp, pm_addr);
4080
4081 /* Set port link state to U2(L1) */
4082 addr = port_array[port_num];
4083 xhci_set_link_state(xhci, port_array, port_num, XDEV_U2);
4084
4085 /* wait for ACK */
4086 spin_unlock_irqrestore(&xhci->lock, flags);
4087 msleep(10);
4088 spin_lock_irqsave(&xhci->lock, flags);
4089
4090 /* Check L1 Status */
Sarah Sharp2611bd182012-10-25 13:27:51 -07004091 ret = xhci_handshake(xhci, pm_addr,
4092 PORT_L1S_MASK, PORT_L1S_SUCCESS, 125);
Andiry Xu95743232011-09-23 14:19:51 -07004093 if (ret != -ETIMEDOUT) {
4094 /* enter L1 successfully */
4095 temp = xhci_readl(xhci, addr);
4096 xhci_dbg(xhci, "port %d entered L1 state, port status 0x%x\n",
4097 port_num, temp);
4098 ret = 0;
4099 } else {
4100 temp = xhci_readl(xhci, pm_addr);
4101 xhci_dbg(xhci, "port %d software lpm failed, L1 status %d\n",
4102 port_num, temp & PORT_L1S_MASK);
4103 ret = -EINVAL;
4104 }
4105
4106 /* Resume the port */
4107 xhci_set_link_state(xhci, port_array, port_num, XDEV_U0);
4108
4109 spin_unlock_irqrestore(&xhci->lock, flags);
4110 msleep(10);
4111 spin_lock_irqsave(&xhci->lock, flags);
4112
4113 /* Clear PLC */
4114 xhci_test_and_clear_bit(xhci, port_array, port_num, PORT_PLC);
4115
4116 /* Check PORTSC to make sure the device is in the right state */
4117 if (!ret) {
4118 temp = xhci_readl(xhci, addr);
4119 xhci_dbg(xhci, "resumed port %d status 0x%x\n", port_num, temp);
4120 if (!(temp & PORT_CONNECT) || !(temp & PORT_PE) ||
4121 (temp & PORT_PLS_MASK) != XDEV_U0) {
4122 xhci_dbg(xhci, "port L1 resume fail\n");
4123 ret = -EINVAL;
4124 }
4125 }
4126
4127 if (ret) {
4128 /* Insert dev to lpm_failed_devs list */
4129 xhci_warn(xhci, "device LPM test failed, may disconnect and "
4130 "re-enumerate\n");
4131 dev_info = kzalloc(sizeof(struct dev_info), GFP_ATOMIC);
4132 if (!dev_info) {
4133 ret = -ENOMEM;
4134 goto finish;
4135 }
4136 dev_info->dev_id = dev_id;
4137 INIT_LIST_HEAD(&dev_info->list);
4138 list_add(&dev_info->list, &xhci->lpm_failed_devs);
4139 } else {
4140 xhci_ring_device(xhci, udev->slot_id);
4141 }
4142
4143finish:
4144 spin_unlock_irqrestore(&xhci->lock, flags);
4145 return ret;
4146}
4147
Andiry Xu65580b432011-09-23 14:19:52 -07004148int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4149 struct usb_device *udev, int enable)
4150{
4151 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4152 __le32 __iomem **port_array;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004153 __le32 __iomem *pm_addr, *hlpm_addr;
4154 u32 pm_val, hlpm_val, field;
Andiry Xu65580b432011-09-23 14:19:52 -07004155 unsigned int port_num;
4156 unsigned long flags;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004157 int hird, exit_latency;
4158 int ret;
Andiry Xu65580b432011-09-23 14:19:52 -07004159
4160 if (hcd->speed == HCD_USB3 || !xhci->hw_lpm_support ||
4161 !udev->lpm_capable)
4162 return -EPERM;
4163
4164 if (!udev->parent || udev->parent->parent ||
4165 udev->descriptor.bDeviceClass == USB_CLASS_HUB)
4166 return -EPERM;
4167
4168 if (udev->usb2_hw_lpm_capable != 1)
4169 return -EPERM;
4170
4171 spin_lock_irqsave(&xhci->lock, flags);
4172
4173 port_array = xhci->usb2_ports;
4174 port_num = udev->portnum - 1;
Mathias Nymanb6e76372013-05-23 17:14:29 +03004175 pm_addr = port_array[port_num] + PORTPMSC;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004176 pm_val = xhci_readl(xhci, pm_addr);
4177 hlpm_addr = port_array[port_num] + PORTHLPMC;
4178 field = le32_to_cpu(udev->bos->ext_cap->bmAttributes);
Andiry Xu65580b432011-09-23 14:19:52 -07004179
4180 xhci_dbg(xhci, "%s port %d USB2 hardware LPM\n",
4181 enable ? "enable" : "disable", port_num);
4182
Andiry Xu65580b432011-09-23 14:19:52 -07004183 if (enable) {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004184 /* Host supports BESL timeout instead of HIRD */
4185 if (udev->usb2_hw_lpm_besl_capable) {
4186 /* if device doesn't have a preferred BESL value use a
4187 * default one which works with mixed HIRD and BESL
4188 * systems. See XHCI_DEFAULT_BESL definition in xhci.h
4189 */
4190 if ((field & USB_BESL_SUPPORT) &&
4191 (field & USB_BESL_BASELINE_VALID))
4192 hird = USB_GET_BESL_BASELINE(field);
4193 else
Mathias Nyman17f34862013-05-23 17:14:31 +03004194 hird = udev->l1_params.besl;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004195
4196 exit_latency = xhci_besl_encoding[hird];
4197 spin_unlock_irqrestore(&xhci->lock, flags);
4198
4199 /* USB 3.0 code dedicate one xhci->lpm_command->in_ctx
4200 * input context for link powermanagement evaluate
4201 * context commands. It is protected by hcd->bandwidth
4202 * mutex and is shared by all devices. We need to set
4203 * the max ext latency in USB 2 BESL LPM as well, so
4204 * use the same mutex and xhci_change_max_exit_latency()
4205 */
4206 mutex_lock(hcd->bandwidth_mutex);
4207 ret = xhci_change_max_exit_latency(xhci, udev,
4208 exit_latency);
4209 mutex_unlock(hcd->bandwidth_mutex);
4210
4211 if (ret < 0)
4212 return ret;
4213 spin_lock_irqsave(&xhci->lock, flags);
4214
4215 hlpm_val = xhci_calculate_usb2_hw_lpm_params(udev);
4216 xhci_writel(xhci, hlpm_val, hlpm_addr);
4217 /* flush write */
4218 xhci_readl(xhci, hlpm_addr);
4219 } else {
4220 hird = xhci_calculate_hird_besl(xhci, udev);
4221 }
4222
4223 pm_val &= ~PORT_HIRD_MASK;
4224 pm_val |= PORT_HIRD(hird) | PORT_RWE;
4225 xhci_writel(xhci, pm_val, pm_addr);
4226 pm_val = xhci_readl(xhci, pm_addr);
4227 pm_val |= PORT_HLE;
4228 xhci_writel(xhci, pm_val, pm_addr);
4229 /* flush write */
4230 xhci_readl(xhci, pm_addr);
Andiry Xu65580b432011-09-23 14:19:52 -07004231 } else {
Mathias Nymana558ccd2013-05-23 17:14:30 +03004232 pm_val &= ~(PORT_HLE | PORT_RWE | PORT_HIRD_MASK);
4233 xhci_writel(xhci, pm_val, pm_addr);
4234 /* flush write */
4235 xhci_readl(xhci, pm_addr);
4236 if (udev->usb2_hw_lpm_besl_capable) {
4237 spin_unlock_irqrestore(&xhci->lock, flags);
4238 mutex_lock(hcd->bandwidth_mutex);
4239 xhci_change_max_exit_latency(xhci, udev, 0);
4240 mutex_unlock(hcd->bandwidth_mutex);
4241 return 0;
4242 }
Andiry Xu65580b432011-09-23 14:19:52 -07004243 }
4244
4245 spin_unlock_irqrestore(&xhci->lock, flags);
4246 return 0;
4247}
4248
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004249/* check if a usb2 port supports a given extened capability protocol
4250 * only USB2 ports extended protocol capability values are cached.
4251 * Return 1 if capability is supported
4252 */
4253static int xhci_check_usb2_port_capability(struct xhci_hcd *xhci, int port,
4254 unsigned capability)
4255{
4256 u32 port_offset, port_count;
4257 int i;
4258
4259 for (i = 0; i < xhci->num_ext_caps; i++) {
4260 if (xhci->ext_caps[i] & capability) {
4261 /* port offsets starts at 1 */
4262 port_offset = XHCI_EXT_PORT_OFF(xhci->ext_caps[i]) - 1;
4263 port_count = XHCI_EXT_PORT_COUNT(xhci->ext_caps[i]);
4264 if (port >= port_offset &&
4265 port < port_offset + port_count)
4266 return 1;
4267 }
4268 }
4269 return 0;
4270}
4271
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004272int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4273{
4274 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4275 int ret;
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004276 int portnum = udev->portnum - 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004277
4278 ret = xhci_usb2_software_lpm_test(hcd, udev);
4279 if (!ret) {
4280 xhci_dbg(xhci, "software LPM test succeed\n");
Mathias Nymanb630d4b2013-05-23 17:14:28 +03004281 if (xhci->hw_lpm_support == 1 &&
4282 xhci_check_usb2_port_capability(xhci, portnum, XHCI_HLC)) {
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004283 udev->usb2_hw_lpm_capable = 1;
Mathias Nyman17f34862013-05-23 17:14:31 +03004284 udev->l1_params.timeout = XHCI_L1_TIMEOUT;
4285 udev->l1_params.besl = XHCI_DEFAULT_BESL;
Mathias Nymana558ccd2013-05-23 17:14:30 +03004286 if (xhci_check_usb2_port_capability(xhci, portnum,
4287 XHCI_BLC))
4288 udev->usb2_hw_lpm_besl_capable = 1;
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004289 ret = xhci_set_usb2_hardware_lpm(hcd, udev, 1);
4290 if (!ret)
4291 udev->usb2_hw_lpm_enabled = 1;
4292 }
4293 }
4294
4295 return 0;
4296}
4297
4298#else
4299
4300int xhci_set_usb2_hardware_lpm(struct usb_hcd *hcd,
4301 struct usb_device *udev, int enable)
4302{
4303 return 0;
4304}
4305
4306int xhci_update_device(struct usb_hcd *hcd, struct usb_device *udev)
4307{
4308 return 0;
4309}
4310
Alan Stern84ebc102013-03-27 16:14:46 -04004311#endif /* CONFIG_PM_RUNTIME */
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004312
Sarah Sharp3b3db022012-05-09 10:55:03 -07004313/*---------------------- USB 3.0 Link PM functions ------------------------*/
4314
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004315#ifdef CONFIG_PM
Sarah Sharpe3567d22012-05-16 13:36:24 -07004316/* Service interval in nanoseconds = 2^(bInterval - 1) * 125us * 1000ns / 1us */
4317static unsigned long long xhci_service_interval_to_ns(
4318 struct usb_endpoint_descriptor *desc)
4319{
Oliver Neukum16b45fd2012-10-17 10:16:16 +02004320 return (1ULL << (desc->bInterval - 1)) * 125 * 1000;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004321}
4322
Sarah Sharp3b3db022012-05-09 10:55:03 -07004323static u16 xhci_get_timeout_no_hub_lpm(struct usb_device *udev,
4324 enum usb3_link_state state)
4325{
4326 unsigned long long sel;
4327 unsigned long long pel;
4328 unsigned int max_sel_pel;
4329 char *state_name;
4330
4331 switch (state) {
4332 case USB3_LPM_U1:
4333 /* Convert SEL and PEL stored in nanoseconds to microseconds */
4334 sel = DIV_ROUND_UP(udev->u1_params.sel, 1000);
4335 pel = DIV_ROUND_UP(udev->u1_params.pel, 1000);
4336 max_sel_pel = USB3_LPM_MAX_U1_SEL_PEL;
4337 state_name = "U1";
4338 break;
4339 case USB3_LPM_U2:
4340 sel = DIV_ROUND_UP(udev->u2_params.sel, 1000);
4341 pel = DIV_ROUND_UP(udev->u2_params.pel, 1000);
4342 max_sel_pel = USB3_LPM_MAX_U2_SEL_PEL;
4343 state_name = "U2";
4344 break;
4345 default:
4346 dev_warn(&udev->dev, "%s: Can't get timeout for non-U1 or U2 state.\n",
4347 __func__);
Sarah Sharpe25e62a2012-06-07 11:10:32 -07004348 return USB3_LPM_DISABLED;
Sarah Sharp3b3db022012-05-09 10:55:03 -07004349 }
4350
4351 if (sel <= max_sel_pel && pel <= max_sel_pel)
4352 return USB3_LPM_DEVICE_INITIATED;
4353
4354 if (sel > max_sel_pel)
4355 dev_dbg(&udev->dev, "Device-initiated %s disabled "
4356 "due to long SEL %llu ms\n",
4357 state_name, sel);
4358 else
4359 dev_dbg(&udev->dev, "Device-initiated %s disabled "
Joe Perches03e64e92013-07-16 19:25:59 -07004360 "due to long PEL %llu ms\n",
Sarah Sharp3b3db022012-05-09 10:55:03 -07004361 state_name, pel);
4362 return USB3_LPM_DISABLED;
4363}
4364
Sarah Sharpe3567d22012-05-16 13:36:24 -07004365/* Returns the hub-encoded U1 timeout value.
4366 * The U1 timeout should be the maximum of the following values:
4367 * - For control endpoints, U1 system exit latency (SEL) * 3
4368 * - For bulk endpoints, U1 SEL * 5
4369 * - For interrupt endpoints:
4370 * - Notification EPs, U1 SEL * 3
4371 * - Periodic EPs, max(105% of bInterval, U1 SEL * 2)
4372 * - For isochronous endpoints, max(105% of bInterval, U1 SEL * 2)
4373 */
4374static u16 xhci_calculate_intel_u1_timeout(struct usb_device *udev,
4375 struct usb_endpoint_descriptor *desc)
4376{
4377 unsigned long long timeout_ns;
4378 int ep_type;
4379 int intr_type;
4380
4381 ep_type = usb_endpoint_type(desc);
4382 switch (ep_type) {
4383 case USB_ENDPOINT_XFER_CONTROL:
4384 timeout_ns = udev->u1_params.sel * 3;
4385 break;
4386 case USB_ENDPOINT_XFER_BULK:
4387 timeout_ns = udev->u1_params.sel * 5;
4388 break;
4389 case USB_ENDPOINT_XFER_INT:
4390 intr_type = usb_endpoint_interrupt_type(desc);
4391 if (intr_type == USB_ENDPOINT_INTR_NOTIFICATION) {
4392 timeout_ns = udev->u1_params.sel * 3;
4393 break;
4394 }
4395 /* Otherwise the calculation is the same as isoc eps */
4396 case USB_ENDPOINT_XFER_ISOC:
4397 timeout_ns = xhci_service_interval_to_ns(desc);
Sarah Sharpc88db162012-05-21 08:44:33 -07004398 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns * 105, 100);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004399 if (timeout_ns < udev->u1_params.sel * 2)
4400 timeout_ns = udev->u1_params.sel * 2;
4401 break;
4402 default:
4403 return 0;
4404 }
4405
4406 /* The U1 timeout is encoded in 1us intervals. */
Sarah Sharpc88db162012-05-21 08:44:33 -07004407 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004408 /* Don't return a timeout of zero, because that's USB3_LPM_DISABLED. */
4409 if (timeout_ns == USB3_LPM_DISABLED)
4410 timeout_ns++;
4411
4412 /* If the necessary timeout value is bigger than what we can set in the
4413 * USB 3.0 hub, we have to disable hub-initiated U1.
4414 */
4415 if (timeout_ns <= USB3_LPM_U1_MAX_TIMEOUT)
4416 return timeout_ns;
4417 dev_dbg(&udev->dev, "Hub-initiated U1 disabled "
4418 "due to long timeout %llu ms\n", timeout_ns);
4419 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U1);
4420}
4421
4422/* Returns the hub-encoded U2 timeout value.
4423 * The U2 timeout should be the maximum of:
4424 * - 10 ms (to avoid the bandwidth impact on the scheduler)
4425 * - largest bInterval of any active periodic endpoint (to avoid going
4426 * into lower power link states between intervals).
4427 * - the U2 Exit Latency of the device
4428 */
4429static u16 xhci_calculate_intel_u2_timeout(struct usb_device *udev,
4430 struct usb_endpoint_descriptor *desc)
4431{
4432 unsigned long long timeout_ns;
4433 unsigned long long u2_del_ns;
4434
4435 timeout_ns = 10 * 1000 * 1000;
4436
4437 if ((usb_endpoint_xfer_int(desc) || usb_endpoint_xfer_isoc(desc)) &&
4438 (xhci_service_interval_to_ns(desc) > timeout_ns))
4439 timeout_ns = xhci_service_interval_to_ns(desc);
4440
Oliver Neukum966e7a82012-10-17 12:17:50 +02004441 u2_del_ns = le16_to_cpu(udev->bos->ss_cap->bU2DevExitLat) * 1000ULL;
Sarah Sharpe3567d22012-05-16 13:36:24 -07004442 if (u2_del_ns > timeout_ns)
4443 timeout_ns = u2_del_ns;
4444
4445 /* The U2 timeout is encoded in 256us intervals */
Sarah Sharpc88db162012-05-21 08:44:33 -07004446 timeout_ns = DIV_ROUND_UP_ULL(timeout_ns, 256 * 1000);
Sarah Sharpe3567d22012-05-16 13:36:24 -07004447 /* If the necessary timeout value is bigger than what we can set in the
4448 * USB 3.0 hub, we have to disable hub-initiated U2.
4449 */
4450 if (timeout_ns <= USB3_LPM_U2_MAX_TIMEOUT)
4451 return timeout_ns;
4452 dev_dbg(&udev->dev, "Hub-initiated U2 disabled "
4453 "due to long timeout %llu ms\n", timeout_ns);
4454 return xhci_get_timeout_no_hub_lpm(udev, USB3_LPM_U2);
4455}
4456
Sarah Sharp3b3db022012-05-09 10:55:03 -07004457static u16 xhci_call_host_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4458 struct usb_device *udev,
4459 struct usb_endpoint_descriptor *desc,
4460 enum usb3_link_state state,
4461 u16 *timeout)
4462{
Sarah Sharpe3567d22012-05-16 13:36:24 -07004463 if (state == USB3_LPM_U1) {
4464 if (xhci->quirks & XHCI_INTEL_HOST)
4465 return xhci_calculate_intel_u1_timeout(udev, desc);
4466 } else {
4467 if (xhci->quirks & XHCI_INTEL_HOST)
4468 return xhci_calculate_intel_u2_timeout(udev, desc);
4469 }
4470
Sarah Sharp3b3db022012-05-09 10:55:03 -07004471 return USB3_LPM_DISABLED;
4472}
4473
4474static int xhci_update_timeout_for_endpoint(struct xhci_hcd *xhci,
4475 struct usb_device *udev,
4476 struct usb_endpoint_descriptor *desc,
4477 enum usb3_link_state state,
4478 u16 *timeout)
4479{
4480 u16 alt_timeout;
4481
4482 alt_timeout = xhci_call_host_update_timeout_for_endpoint(xhci, udev,
4483 desc, state, timeout);
4484
4485 /* If we found we can't enable hub-initiated LPM, or
4486 * the U1 or U2 exit latency was too high to allow
4487 * device-initiated LPM as well, just stop searching.
4488 */
4489 if (alt_timeout == USB3_LPM_DISABLED ||
4490 alt_timeout == USB3_LPM_DEVICE_INITIATED) {
4491 *timeout = alt_timeout;
4492 return -E2BIG;
4493 }
4494 if (alt_timeout > *timeout)
4495 *timeout = alt_timeout;
4496 return 0;
4497}
4498
4499static int xhci_update_timeout_for_interface(struct xhci_hcd *xhci,
4500 struct usb_device *udev,
4501 struct usb_host_interface *alt,
4502 enum usb3_link_state state,
4503 u16 *timeout)
4504{
4505 int j;
4506
4507 for (j = 0; j < alt->desc.bNumEndpoints; j++) {
4508 if (xhci_update_timeout_for_endpoint(xhci, udev,
4509 &alt->endpoint[j].desc, state, timeout))
4510 return -E2BIG;
4511 continue;
4512 }
4513 return 0;
4514}
4515
Sarah Sharpe3567d22012-05-16 13:36:24 -07004516static int xhci_check_intel_tier_policy(struct usb_device *udev,
4517 enum usb3_link_state state)
4518{
4519 struct usb_device *parent;
4520 unsigned int num_hubs;
4521
4522 if (state == USB3_LPM_U2)
4523 return 0;
4524
4525 /* Don't enable U1 if the device is on a 2nd tier hub or lower. */
4526 for (parent = udev->parent, num_hubs = 0; parent->parent;
4527 parent = parent->parent)
4528 num_hubs++;
4529
4530 if (num_hubs < 2)
4531 return 0;
4532
4533 dev_dbg(&udev->dev, "Disabling U1 link state for device"
4534 " below second-tier hub.\n");
4535 dev_dbg(&udev->dev, "Plug device into first-tier hub "
4536 "to decrease power consumption.\n");
4537 return -E2BIG;
4538}
4539
Sarah Sharp3b3db022012-05-09 10:55:03 -07004540static int xhci_check_tier_policy(struct xhci_hcd *xhci,
4541 struct usb_device *udev,
4542 enum usb3_link_state state)
4543{
Sarah Sharpe3567d22012-05-16 13:36:24 -07004544 if (xhci->quirks & XHCI_INTEL_HOST)
4545 return xhci_check_intel_tier_policy(udev, state);
Sarah Sharp3b3db022012-05-09 10:55:03 -07004546 return -EINVAL;
4547}
4548
4549/* Returns the U1 or U2 timeout that should be enabled.
4550 * If the tier check or timeout setting functions return with a non-zero exit
4551 * code, that means the timeout value has been finalized and we shouldn't look
4552 * at any more endpoints.
4553 */
4554static u16 xhci_calculate_lpm_timeout(struct usb_hcd *hcd,
4555 struct usb_device *udev, enum usb3_link_state state)
4556{
4557 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4558 struct usb_host_config *config;
4559 char *state_name;
4560 int i;
4561 u16 timeout = USB3_LPM_DISABLED;
4562
4563 if (state == USB3_LPM_U1)
4564 state_name = "U1";
4565 else if (state == USB3_LPM_U2)
4566 state_name = "U2";
4567 else {
4568 dev_warn(&udev->dev, "Can't enable unknown link state %i\n",
4569 state);
4570 return timeout;
4571 }
4572
4573 if (xhci_check_tier_policy(xhci, udev, state) < 0)
4574 return timeout;
4575
4576 /* Gather some information about the currently installed configuration
4577 * and alternate interface settings.
4578 */
4579 if (xhci_update_timeout_for_endpoint(xhci, udev, &udev->ep0.desc,
4580 state, &timeout))
4581 return timeout;
4582
4583 config = udev->actconfig;
4584 if (!config)
4585 return timeout;
4586
4587 for (i = 0; i < USB_MAXINTERFACES; i++) {
4588 struct usb_driver *driver;
4589 struct usb_interface *intf = config->interface[i];
4590
4591 if (!intf)
4592 continue;
4593
4594 /* Check if any currently bound drivers want hub-initiated LPM
4595 * disabled.
4596 */
4597 if (intf->dev.driver) {
4598 driver = to_usb_driver(intf->dev.driver);
4599 if (driver && driver->disable_hub_initiated_lpm) {
4600 dev_dbg(&udev->dev, "Hub-initiated %s disabled "
4601 "at request of driver %s\n",
4602 state_name, driver->name);
4603 return xhci_get_timeout_no_hub_lpm(udev, state);
4604 }
4605 }
4606
4607 /* Not sure how this could happen... */
4608 if (!intf->cur_altsetting)
4609 continue;
4610
4611 if (xhci_update_timeout_for_interface(xhci, udev,
4612 intf->cur_altsetting,
4613 state, &timeout))
4614 return timeout;
4615 }
4616 return timeout;
4617}
4618
Sarah Sharp3b3db022012-05-09 10:55:03 -07004619static int calculate_max_exit_latency(struct usb_device *udev,
4620 enum usb3_link_state state_changed,
4621 u16 hub_encoded_timeout)
4622{
4623 unsigned long long u1_mel_us = 0;
4624 unsigned long long u2_mel_us = 0;
4625 unsigned long long mel_us = 0;
4626 bool disabling_u1;
4627 bool disabling_u2;
4628 bool enabling_u1;
4629 bool enabling_u2;
4630
4631 disabling_u1 = (state_changed == USB3_LPM_U1 &&
4632 hub_encoded_timeout == USB3_LPM_DISABLED);
4633 disabling_u2 = (state_changed == USB3_LPM_U2 &&
4634 hub_encoded_timeout == USB3_LPM_DISABLED);
4635
4636 enabling_u1 = (state_changed == USB3_LPM_U1 &&
4637 hub_encoded_timeout != USB3_LPM_DISABLED);
4638 enabling_u2 = (state_changed == USB3_LPM_U2 &&
4639 hub_encoded_timeout != USB3_LPM_DISABLED);
4640
4641 /* If U1 was already enabled and we're not disabling it,
4642 * or we're going to enable U1, account for the U1 max exit latency.
4643 */
4644 if ((udev->u1_params.timeout != USB3_LPM_DISABLED && !disabling_u1) ||
4645 enabling_u1)
4646 u1_mel_us = DIV_ROUND_UP(udev->u1_params.mel, 1000);
4647 if ((udev->u2_params.timeout != USB3_LPM_DISABLED && !disabling_u2) ||
4648 enabling_u2)
4649 u2_mel_us = DIV_ROUND_UP(udev->u2_params.mel, 1000);
4650
4651 if (u1_mel_us > u2_mel_us)
4652 mel_us = u1_mel_us;
4653 else
4654 mel_us = u2_mel_us;
4655 /* xHCI host controller max exit latency field is only 16 bits wide. */
4656 if (mel_us > MAX_EXIT) {
4657 dev_warn(&udev->dev, "Link PM max exit latency of %lluus "
4658 "is too big.\n", mel_us);
4659 return -E2BIG;
4660 }
4661 return mel_us;
4662}
4663
4664/* Returns the USB3 hub-encoded value for the U1/U2 timeout. */
4665int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4666 struct usb_device *udev, enum usb3_link_state state)
4667{
4668 struct xhci_hcd *xhci;
4669 u16 hub_encoded_timeout;
4670 int mel;
4671 int ret;
4672
4673 xhci = hcd_to_xhci(hcd);
4674 /* The LPM timeout values are pretty host-controller specific, so don't
4675 * enable hub-initiated timeouts unless the vendor has provided
4676 * information about their timeout algorithm.
4677 */
4678 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4679 !xhci->devs[udev->slot_id])
4680 return USB3_LPM_DISABLED;
4681
4682 hub_encoded_timeout = xhci_calculate_lpm_timeout(hcd, udev, state);
4683 mel = calculate_max_exit_latency(udev, state, hub_encoded_timeout);
4684 if (mel < 0) {
4685 /* Max Exit Latency is too big, disable LPM. */
4686 hub_encoded_timeout = USB3_LPM_DISABLED;
4687 mel = 0;
4688 }
4689
4690 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4691 if (ret)
4692 return ret;
4693 return hub_encoded_timeout;
4694}
4695
4696int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4697 struct usb_device *udev, enum usb3_link_state state)
4698{
4699 struct xhci_hcd *xhci;
4700 u16 mel;
4701 int ret;
4702
4703 xhci = hcd_to_xhci(hcd);
4704 if (!xhci || !(xhci->quirks & XHCI_LPM_SUPPORT) ||
4705 !xhci->devs[udev->slot_id])
4706 return 0;
4707
4708 mel = calculate_max_exit_latency(udev, state, USB3_LPM_DISABLED);
4709 ret = xhci_change_max_exit_latency(xhci, udev, mel);
4710 if (ret)
4711 return ret;
4712 return 0;
4713}
Sarah Sharpb01bcbf2012-05-21 07:54:42 -07004714#else /* CONFIG_PM */
4715
4716int xhci_enable_usb3_lpm_timeout(struct usb_hcd *hcd,
4717 struct usb_device *udev, enum usb3_link_state state)
4718{
4719 return USB3_LPM_DISABLED;
4720}
4721
4722int xhci_disable_usb3_lpm_timeout(struct usb_hcd *hcd,
4723 struct usb_device *udev, enum usb3_link_state state)
4724{
4725 return 0;
4726}
4727#endif /* CONFIG_PM */
4728
Sarah Sharp3b3db022012-05-09 10:55:03 -07004729/*-------------------------------------------------------------------------*/
4730
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004731/* Once a hub descriptor is fetched for a device, we need to update the xHC's
4732 * internal data structures for the device.
4733 */
4734int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
4735 struct usb_tt *tt, gfp_t mem_flags)
4736{
4737 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4738 struct xhci_virt_device *vdev;
4739 struct xhci_command *config_cmd;
4740 struct xhci_input_control_ctx *ctrl_ctx;
4741 struct xhci_slot_ctx *slot_ctx;
4742 unsigned long flags;
4743 unsigned think_time;
4744 int ret;
4745
4746 /* Ignore root hubs */
4747 if (!hdev->parent)
4748 return 0;
4749
4750 vdev = xhci->devs[hdev->slot_id];
4751 if (!vdev) {
4752 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
4753 return -EINVAL;
4754 }
Sarah Sharpa1d78c12009-12-09 15:59:03 -08004755 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004756 if (!config_cmd) {
4757 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
4758 return -ENOMEM;
4759 }
Sarah Sharp92f8e762013-04-23 17:11:14 -07004760 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
4761 if (!ctrl_ctx) {
4762 xhci_warn(xhci, "%s: Could not get input context, bad type.\n",
4763 __func__);
4764 xhci_free_command(xhci, config_cmd);
4765 return -ENOMEM;
4766 }
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004767
4768 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp839c8172011-09-02 11:05:47 -07004769 if (hdev->speed == USB_SPEED_HIGH &&
4770 xhci_alloc_tt_info(xhci, vdev, hdev, tt, GFP_ATOMIC)) {
4771 xhci_dbg(xhci, "Could not allocate xHCI TT structure.\n");
4772 xhci_free_command(xhci, config_cmd);
4773 spin_unlock_irqrestore(&xhci->lock, flags);
4774 return -ENOMEM;
4775 }
4776
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004777 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004778 ctrl_ctx->add_flags |= cpu_to_le32(SLOT_FLAG);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004779 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
Matt Evans28ccd292011-03-29 13:40:46 +11004780 slot_ctx->dev_info |= cpu_to_le32(DEV_HUB);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004781 if (tt->multi)
Matt Evans28ccd292011-03-29 13:40:46 +11004782 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004783 if (xhci->hci_version > 0x95) {
4784 xhci_dbg(xhci, "xHCI version %x needs hub "
4785 "TT think time and number of ports\n",
4786 (unsigned int) xhci->hci_version);
Matt Evans28ccd292011-03-29 13:40:46 +11004787 slot_ctx->dev_info2 |= cpu_to_le32(XHCI_MAX_PORTS(hdev->maxchild));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004788 /* Set TT think time - convert from ns to FS bit times.
4789 * 0 = 8 FS bit times, 1 = 16 FS bit times,
4790 * 2 = 24 FS bit times, 3 = 32 FS bit times.
Andiry Xu700b4172011-05-05 18:14:05 +08004791 *
4792 * xHCI 1.0: this field shall be 0 if the device is not a
4793 * High-spped hub.
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004794 */
4795 think_time = tt->think_time;
4796 if (think_time != 0)
4797 think_time = (think_time / 666) - 1;
Andiry Xu700b4172011-05-05 18:14:05 +08004798 if (xhci->hci_version < 0x100 || hdev->speed == USB_SPEED_HIGH)
4799 slot_ctx->tt_info |=
4800 cpu_to_le32(TT_THINK_TIME(think_time));
Sarah Sharpac1c1b72009-09-04 10:53:20 -07004801 } else {
4802 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
4803 "TT think time or number of ports\n",
4804 (unsigned int) xhci->hci_version);
4805 }
4806 slot_ctx->dev_state = 0;
4807 spin_unlock_irqrestore(&xhci->lock, flags);
4808
4809 xhci_dbg(xhci, "Set up %s for hub device.\n",
4810 (xhci->hci_version > 0x95) ?
4811 "configure endpoint" : "evaluate context");
4812 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
4813 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
4814
4815 /* Issue and wait for the configure endpoint or
4816 * evaluate context command.
4817 */
4818 if (xhci->hci_version > 0x95)
4819 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4820 false, false);
4821 else
4822 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
4823 true, false);
4824
4825 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
4826 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
4827
4828 xhci_free_command(xhci, config_cmd);
4829 return ret;
4830}
4831
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004832int xhci_get_frame(struct usb_hcd *hcd)
4833{
4834 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
4835 /* EHCI mods by the periodic size. Why? */
4836 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
4837}
4838
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004839int xhci_gen_setup(struct usb_hcd *hcd, xhci_get_quirks_t get_quirks)
4840{
4841 struct xhci_hcd *xhci;
4842 struct device *dev = hcd->self.controller;
4843 int retval;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004844
Andiry Xufdaf8b32012-03-05 17:49:38 +08004845 /* Accept arbitrarily long scatter-gather lists */
4846 hcd->self.sg_tablesize = ~0;
Ming Leifc760512013-08-08 21:48:23 +08004847
4848 /* support to build packet from discontinuous buffers */
4849 hcd->self.no_sg_constraint = 1;
4850
Hans de Goede19181bc2012-07-04 09:18:02 +02004851 /* XHCI controllers don't stop the ep queue on short packets :| */
4852 hcd->self.no_stop_on_short = 1;
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004853
4854 if (usb_hcd_is_primary_hcd(hcd)) {
4855 xhci = kzalloc(sizeof(struct xhci_hcd), GFP_KERNEL);
4856 if (!xhci)
4857 return -ENOMEM;
4858 *((struct xhci_hcd **) hcd->hcd_priv) = xhci;
4859 xhci->main_hcd = hcd;
4860 /* Mark the first roothub as being USB 2.0.
4861 * The xHCI driver will register the USB 3.0 roothub.
4862 */
4863 hcd->speed = HCD_USB2;
4864 hcd->self.root_hub->speed = USB_SPEED_HIGH;
4865 /*
4866 * USB 2.0 roothub under xHCI has an integrated TT,
4867 * (rate matching hub) as opposed to having an OHCI/UHCI
4868 * companion controller.
4869 */
4870 hcd->has_tt = 1;
4871 } else {
4872 /* xHCI private pointer was set in xhci_pci_probe for the second
4873 * registered roothub.
4874 */
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004875 return 0;
4876 }
4877
4878 xhci->cap_regs = hcd->regs;
4879 xhci->op_regs = hcd->regs +
4880 HC_LENGTH(xhci_readl(xhci, &xhci->cap_regs->hc_capbase));
4881 xhci->run_regs = hcd->regs +
4882 (xhci_readl(xhci, &xhci->cap_regs->run_regs_off) & RTSOFF_MASK);
4883 /* Cache read-only capability registers */
4884 xhci->hcs_params1 = xhci_readl(xhci, &xhci->cap_regs->hcs_params1);
4885 xhci->hcs_params2 = xhci_readl(xhci, &xhci->cap_regs->hcs_params2);
4886 xhci->hcs_params3 = xhci_readl(xhci, &xhci->cap_regs->hcs_params3);
4887 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hc_capbase);
4888 xhci->hci_version = HC_VERSION(xhci->hcc_params);
4889 xhci->hcc_params = xhci_readl(xhci, &xhci->cap_regs->hcc_params);
4890 xhci_print_registers(xhci);
4891
4892 get_quirks(dev, xhci);
4893
George Cherian07f3cb72013-07-01 10:59:12 +05304894 /* In xhci controllers which follow xhci 1.0 spec gives a spurious
4895 * success event after a short transfer. This quirk will ignore such
4896 * spurious event.
4897 */
4898 if (xhci->hci_version > 0x96)
4899 xhci->quirks |= XHCI_SPURIOUS_SUCCESS;
4900
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004901 /* Make sure the HC is halted. */
4902 retval = xhci_halt(xhci);
4903 if (retval)
4904 goto error;
4905
4906 xhci_dbg(xhci, "Resetting HCD\n");
4907 /* Reset the internal HC memory state and registers. */
4908 retval = xhci_reset(xhci);
4909 if (retval)
4910 goto error;
4911 xhci_dbg(xhci, "Reset complete\n");
4912
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03004913 /* Set dma_mask and coherent_dma_mask to 64-bits,
4914 * if xHC supports 64-bit addressing */
4915 if (HCC_64BIT_ADDR(xhci->hcc_params) &&
4916 !dma_set_mask(dev, DMA_BIT_MASK(64))) {
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004917 xhci_dbg(xhci, "Enabling 64-bit DMA addresses.\n");
Xenia Ragiadakouc10cf112013-08-14 05:55:19 +03004918 dma_set_coherent_mask(dev, DMA_BIT_MASK(64));
Sebastian Andrzej Siewior552e0c42011-09-23 14:20:01 -07004919 }
4920
4921 xhci_dbg(xhci, "Calling HCD init\n");
4922 /* Initialize HCD and host controller data structures. */
4923 retval = xhci_init(hcd);
4924 if (retval)
4925 goto error;
4926 xhci_dbg(xhci, "Called HCD init\n");
4927 return 0;
4928error:
4929 kfree(xhci);
4930 return retval;
4931}
4932
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004933MODULE_DESCRIPTION(DRIVER_DESC);
4934MODULE_AUTHOR(DRIVER_AUTHOR);
4935MODULE_LICENSE("GPL");
4936
4937static int __init xhci_hcd_init(void)
4938{
Sebastian Andrzej Siewior0cc47d52011-09-23 14:20:02 -07004939 int retval;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004940
4941 retval = xhci_register_pci();
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004942 if (retval < 0) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03004943 pr_debug("Problem registering PCI driver.\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004944 return retval;
4945 }
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004946 retval = xhci_register_plat();
4947 if (retval < 0) {
Xenia Ragiadakou5c1127d2013-07-02 17:49:26 +03004948 pr_debug("Problem registering platform driver.\n");
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004949 goto unreg_pci;
4950 }
Sarah Sharp98441972009-05-14 11:44:18 -07004951 /*
4952 * Check the compiler generated sizes of structures that must be laid
4953 * out in specific ways for hardware access.
4954 */
4955 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
4956 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
4957 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
4958 /* xhci_device_control has eight fields, and also
4959 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
4960 */
Sarah Sharp98441972009-05-14 11:44:18 -07004961 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
4962 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
4963 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
4964 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
4965 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
4966 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
4967 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004968 return 0;
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004969unreg_pci:
4970 xhci_unregister_pci();
4971 return retval;
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004972}
4973module_init(xhci_hcd_init);
4974
4975static void __exit xhci_hcd_cleanup(void)
4976{
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004977 xhci_unregister_pci();
Sebastian Andrzej Siewior3429e912012-03-13 16:57:41 +02004978 xhci_unregister_plat();
Sarah Sharp66d4ead2009-04-27 19:52:28 -07004979}
4980module_exit(xhci_hcd_cleanup);