blob: 4549068758f58e682aa411cfbd3b39053ea6ef3f [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>
Sarah Sharp66d4ead2009-04-27 19:52:28 -070029
30#include "xhci.h"
31
32#define DRIVER_AUTHOR "Sarah Sharp"
33#define DRIVER_DESC "'eXtensible' Host Controller (xHC) Driver"
34
Sarah Sharpb0567b32009-08-07 14:04:36 -070035/* Some 0.95 hardware can't handle the chain bit on a Link TRB being cleared */
36static int link_quirk;
37module_param(link_quirk, int, S_IRUGO | S_IWUSR);
38MODULE_PARM_DESC(link_quirk, "Don't clear the chain bit on a link TRB");
39
Sarah Sharp66d4ead2009-04-27 19:52:28 -070040/* TODO: copied from ehci-hcd.c - can this be refactored? */
41/*
42 * handshake - spin reading hc until handshake completes or fails
43 * @ptr: address of hc register to be read
44 * @mask: bits to look at in result of read
45 * @done: value of those bits when handshake succeeds
46 * @usec: timeout in microseconds
47 *
48 * Returns negative errno, or zero on success
49 *
50 * Success happens when the "mask" bits have the specified value (hardware
51 * handshake done). There are two failure modes: "usec" have passed (major
52 * hardware flakeout), or the register reads as all-ones (hardware removed).
53 */
54static int handshake(struct xhci_hcd *xhci, void __iomem *ptr,
55 u32 mask, u32 done, int usec)
56{
57 u32 result;
58
59 do {
60 result = xhci_readl(xhci, ptr);
61 if (result == ~(u32)0) /* card removed */
62 return -ENODEV;
63 result &= mask;
64 if (result == done)
65 return 0;
66 udelay(1);
67 usec--;
68 } while (usec > 0);
69 return -ETIMEDOUT;
70}
71
72/*
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -070073 * Disable interrupts and begin the xHCI halting process.
74 */
75void xhci_quiesce(struct xhci_hcd *xhci)
76{
77 u32 halted;
78 u32 cmd;
79 u32 mask;
80
81 mask = ~(XHCI_IRQS);
82 halted = xhci_readl(xhci, &xhci->op_regs->status) & STS_HALT;
83 if (!halted)
84 mask &= ~CMD_RUN;
85
86 cmd = xhci_readl(xhci, &xhci->op_regs->command);
87 cmd &= mask;
88 xhci_writel(xhci, cmd, &xhci->op_regs->command);
89}
90
91/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -070092 * Force HC into halt state.
93 *
94 * Disable any IRQs and clear the run/stop bit.
95 * HC will complete any current and actively pipelined transactions, and
Andiry Xubdfca502011-01-06 15:43:39 +080096 * should halt within 16 ms of the run/stop bit being cleared.
Sarah Sharp66d4ead2009-04-27 19:52:28 -070097 * Read HC Halted bit in the status register to see when the HC is finished.
Sarah Sharp66d4ead2009-04-27 19:52:28 -070098 */
99int xhci_halt(struct xhci_hcd *xhci)
100{
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700101 xhci_dbg(xhci, "// Halt the HC\n");
Sarah Sharp4f0f0ba2009-10-27 10:56:33 -0700102 xhci_quiesce(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700103
104 return handshake(xhci, &xhci->op_regs->status,
105 STS_HALT, STS_HALT, XHCI_MAX_HALT_USEC);
106}
107
108/*
Sarah Sharped074532010-05-24 13:25:21 -0700109 * Set the run bit and wait for the host to be running.
110 */
111int xhci_start(struct xhci_hcd *xhci)
112{
113 u32 temp;
114 int ret;
115
116 temp = xhci_readl(xhci, &xhci->op_regs->command);
117 temp |= (CMD_RUN);
118 xhci_dbg(xhci, "// Turn on HC, cmd = 0x%x.\n",
119 temp);
120 xhci_writel(xhci, temp, &xhci->op_regs->command);
121
122 /*
123 * Wait for the HCHalted Status bit to be 0 to indicate the host is
124 * running.
125 */
126 ret = handshake(xhci, &xhci->op_regs->status,
127 STS_HALT, 0, XHCI_MAX_HALT_USEC);
128 if (ret == -ETIMEDOUT)
129 xhci_err(xhci, "Host took too long to start, "
130 "waited %u microseconds.\n",
131 XHCI_MAX_HALT_USEC);
132 return ret;
133}
134
135/*
Sarah Sharpac04e6f2011-03-11 08:47:33 -0800136 * Reset a halted HC.
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700137 *
138 * This resets pipelines, timers, counters, state machines, etc.
139 * Transactions will be terminated immediately, and operational registers
140 * will be set to their defaults.
141 */
142int xhci_reset(struct xhci_hcd *xhci)
143{
144 u32 command;
145 u32 state;
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700146 int ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700147
148 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharpd3512f62009-07-27 12:03:50 -0700149 if ((state & STS_HALT) == 0) {
150 xhci_warn(xhci, "Host controller not halted, aborting reset.\n");
151 return 0;
152 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700153
154 xhci_dbg(xhci, "// Reset the HC\n");
155 command = xhci_readl(xhci, &xhci->op_regs->command);
156 command |= CMD_RESET;
157 xhci_writel(xhci, command, &xhci->op_regs->command);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700158
Sarah Sharp2d62f3e2010-05-24 13:25:15 -0700159 ret = handshake(xhci, &xhci->op_regs->command,
160 CMD_RESET, 0, 250 * 1000);
161 if (ret)
162 return ret;
163
164 xhci_dbg(xhci, "Wait for controller to be ready for doorbell rings\n");
165 /*
166 * xHCI cannot write to any doorbells or operational registers other
167 * than status until the "Controller Not Ready" flag is cleared.
168 */
169 return handshake(xhci, &xhci->op_regs->status, STS_CNR, 0, 250 * 1000);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700170}
171
Dong Nguyen43b86af2010-07-21 16:56:08 -0700172/*
173 * Free IRQs
174 * free all IRQs request
175 */
176static void xhci_free_irq(struct xhci_hcd *xhci)
177{
178 int i;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700179 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
180
Dong Nguyen43b86af2010-07-21 16:56:08 -0700181 /* return if using legacy interrupt */
182 if (xhci_to_hcd(xhci)->irq >= 0)
183 return;
184
185 if (xhci->msix_entries) {
186 for (i = 0; i < xhci->msix_count; i++)
187 if (xhci->msix_entries[i].vector)
188 free_irq(xhci->msix_entries[i].vector,
189 xhci_to_hcd(xhci));
190 } else if (pdev->irq >= 0)
191 free_irq(pdev->irq, xhci_to_hcd(xhci));
192
193 return;
194}
195
196/*
197 * Set up MSI
198 */
199static int xhci_setup_msi(struct xhci_hcd *xhci)
200{
201 int ret;
202 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
203
204 ret = pci_enable_msi(pdev);
205 if (ret) {
206 xhci_err(xhci, "failed to allocate MSI entry\n");
207 return ret;
208 }
209
210 ret = request_irq(pdev->irq, (irq_handler_t)xhci_msi_irq,
211 0, "xhci_hcd", xhci_to_hcd(xhci));
212 if (ret) {
213 xhci_err(xhci, "disable MSI interrupt\n");
214 pci_disable_msi(pdev);
215 }
216
217 return ret;
218}
219
220/*
221 * Set up MSI-X
222 */
223static int xhci_setup_msix(struct xhci_hcd *xhci)
224{
225 int i, ret = 0;
Andiry Xu00292272010-12-27 17:39:02 +0800226 struct usb_hcd *hcd = xhci_to_hcd(xhci);
227 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700228
229 /*
230 * calculate number of msi-x vectors supported.
231 * - HCS_MAX_INTRS: the max number of interrupts the host can handle,
232 * with max number of interrupters based on the xhci HCSPARAMS1.
233 * - num_online_cpus: maximum msi-x vectors per CPUs core.
234 * Add additional 1 vector to ensure always available interrupt.
235 */
236 xhci->msix_count = min(num_online_cpus() + 1,
237 HCS_MAX_INTRS(xhci->hcs_params1));
238
239 xhci->msix_entries =
240 kmalloc((sizeof(struct msix_entry))*xhci->msix_count,
Greg Kroah-Hartman86871972010-11-11 09:41:02 -0800241 GFP_KERNEL);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700242 if (!xhci->msix_entries) {
243 xhci_err(xhci, "Failed to allocate MSI-X entries\n");
244 return -ENOMEM;
245 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700246
247 for (i = 0; i < xhci->msix_count; i++) {
248 xhci->msix_entries[i].entry = i;
249 xhci->msix_entries[i].vector = 0;
250 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700251
252 ret = pci_enable_msix(pdev, xhci->msix_entries, xhci->msix_count);
253 if (ret) {
254 xhci_err(xhci, "Failed to enable MSI-X\n");
255 goto free_entries;
256 }
257
Dong Nguyen43b86af2010-07-21 16:56:08 -0700258 for (i = 0; i < xhci->msix_count; i++) {
259 ret = request_irq(xhci->msix_entries[i].vector,
260 (irq_handler_t)xhci_msi_irq,
261 0, "xhci_hcd", xhci_to_hcd(xhci));
262 if (ret)
263 goto disable_msix;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700264 }
Dong Nguyen43b86af2010-07-21 16:56:08 -0700265
Andiry Xu00292272010-12-27 17:39:02 +0800266 hcd->msix_enabled = 1;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700267 return ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700268
269disable_msix:
Dong Nguyen43b86af2010-07-21 16:56:08 -0700270 xhci_err(xhci, "disable MSI-X interrupt\n");
271 xhci_free_irq(xhci);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700272 pci_disable_msix(pdev);
273free_entries:
274 kfree(xhci->msix_entries);
275 xhci->msix_entries = NULL;
276 return ret;
277}
278
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700279/* Free any IRQs and disable MSI-X */
280static void xhci_cleanup_msix(struct xhci_hcd *xhci)
281{
Andiry Xu00292272010-12-27 17:39:02 +0800282 struct usb_hcd *hcd = xhci_to_hcd(xhci);
283 struct pci_dev *pdev = to_pci_dev(hcd->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700284
Dong Nguyen43b86af2010-07-21 16:56:08 -0700285 xhci_free_irq(xhci);
286
287 if (xhci->msix_entries) {
288 pci_disable_msix(pdev);
289 kfree(xhci->msix_entries);
290 xhci->msix_entries = NULL;
291 } else {
292 pci_disable_msi(pdev);
293 }
294
Andiry Xu00292272010-12-27 17:39:02 +0800295 hcd->msix_enabled = 0;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700296 return;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700297}
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700298
299/*
300 * Initialize memory for HCD and xHC (one-time init).
301 *
302 * Program the PAGESIZE register, initialize the device context array, create
303 * device contexts (?), set up a command ring segment (or two?), create event
304 * ring (one for now).
305 */
306int xhci_init(struct usb_hcd *hcd)
307{
308 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
309 int retval = 0;
310
311 xhci_dbg(xhci, "xhci_init\n");
312 spin_lock_init(&xhci->lock);
Sarah Sharpb0567b32009-08-07 14:04:36 -0700313 if (link_quirk) {
314 xhci_dbg(xhci, "QUIRK: Not clearing Link TRB chain bits.\n");
315 xhci->quirks |= XHCI_LINK_TRB_QUIRK;
316 } else {
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700317 xhci_dbg(xhci, "xHCI doesn't need link TRB QUIRK\n");
Sarah Sharpb0567b32009-08-07 14:04:36 -0700318 }
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700319 retval = xhci_mem_init(xhci, GFP_KERNEL);
320 xhci_dbg(xhci, "Finished xhci_init\n");
321
322 return retval;
323}
324
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700325/*-------------------------------------------------------------------------*/
326
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700327
328#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
Sarah Sharp23e3be12009-04-29 19:05:20 -0700329void xhci_event_ring_work(unsigned long arg)
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700330{
331 unsigned long flags;
332 int temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700333 u64 temp_64;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700334 struct xhci_hcd *xhci = (struct xhci_hcd *) arg;
335 int i, j;
336
337 xhci_dbg(xhci, "Poll event ring: %lu\n", jiffies);
338
339 spin_lock_irqsave(&xhci->lock, flags);
340 temp = xhci_readl(xhci, &xhci->op_regs->status);
341 xhci_dbg(xhci, "op reg status = 0x%x\n", temp);
Sarah Sharp6f5165c2009-10-27 10:57:01 -0700342 if (temp == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
Sarah Sharpe4ab05d2009-09-16 16:42:30 -0700343 xhci_dbg(xhci, "HW died, polling stopped.\n");
344 spin_unlock_irqrestore(&xhci->lock, flags);
345 return;
346 }
347
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700348 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
349 xhci_dbg(xhci, "ir_set 0 pending = 0x%x\n", temp);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700350 xhci_dbg(xhci, "HC error bitmask = 0x%x\n", xhci->error_bitmask);
351 xhci->error_bitmask = 0;
352 xhci_dbg(xhci, "Event ring:\n");
353 xhci_debug_segment(xhci, xhci->event_ring->deq_seg);
354 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
Sarah Sharp8e595a52009-07-27 12:03:31 -0700355 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
356 temp_64 &= ~ERST_PTR_MASK;
357 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700358 xhci_dbg(xhci, "Command ring:\n");
359 xhci_debug_segment(xhci, xhci->cmd_ring->deq_seg);
360 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
361 xhci_dbg_cmd_ptrs(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700362 for (i = 0; i < MAX_HC_SLOTS; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -0700363 if (!xhci->devs[i])
364 continue;
365 for (j = 0; j < 31; ++j) {
Sarah Sharpe9df17e2010-04-02 15:34:43 -0700366 xhci_dbg_ep_rings(xhci, i, j, &xhci->devs[i]->eps[j]);
Sarah Sharp3ffbba92009-04-27 19:57:38 -0700367 }
368 }
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700369 spin_unlock_irqrestore(&xhci->lock, flags);
370
371 if (!xhci->zombie)
372 mod_timer(&xhci->event_ring_timer, jiffies + POLL_TIMEOUT * HZ);
373 else
374 xhci_dbg(xhci, "Quit polling the event ring.\n");
375}
376#endif
377
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800378static int xhci_run_finished(struct xhci_hcd *xhci)
379{
380 if (xhci_start(xhci)) {
381 xhci_halt(xhci);
382 return -ENODEV;
383 }
384 xhci->shared_hcd->state = HC_STATE_RUNNING;
385
386 if (xhci->quirks & XHCI_NEC_HOST)
387 xhci_ring_cmd_db(xhci);
388
389 xhci_dbg(xhci, "Finished xhci_run for USB3 roothub\n");
390 return 0;
391}
392
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700393/*
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700394 * Start the HC after it was halted.
395 *
396 * This function is called by the USB core when the HC driver is added.
397 * Its opposite is xhci_stop().
398 *
399 * xhci_init() must be called once before this function can be called.
400 * Reset the HC, enable device slot contexts, program DCBAAP, and
401 * set command ring pointer and event ring pointer.
402 *
403 * Setup MSI-X vectors and enable interrupts.
404 */
405int xhci_run(struct usb_hcd *hcd)
406{
407 u32 temp;
Sarah Sharp8e595a52009-07-27 12:03:31 -0700408 u64 temp_64;
Dong Nguyen43b86af2010-07-21 16:56:08 -0700409 u32 ret;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700410 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700411 struct pci_dev *pdev = to_pci_dev(xhci_to_hcd(xhci)->self.controller);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700412
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800413 /* Start the xHCI host controller running only after the USB 2.0 roothub
414 * is setup.
415 */
416
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700417 hcd->uses_new_polling = 1;
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800418 if (!usb_hcd_is_primary_hcd(hcd))
419 return xhci_run_finished(xhci);
Sarah Sharp0f2a7932009-04-27 19:57:12 -0700420
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700421 xhci_dbg(xhci, "xhci_run\n");
Dong Nguyen43b86af2010-07-21 16:56:08 -0700422 /* unregister the legacy interrupt */
423 if (hcd->irq)
424 free_irq(hcd->irq, hcd);
425 hcd->irq = -1;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700426
Dong Nguyen43b86af2010-07-21 16:56:08 -0700427 ret = xhci_setup_msix(xhci);
428 if (ret)
429 /* fall back to msi*/
430 ret = xhci_setup_msi(xhci);
431
432 if (ret) {
433 /* fall back to legacy interrupt*/
434 ret = request_irq(pdev->irq, &usb_hcd_irq, IRQF_SHARED,
435 hcd->irq_descr, hcd);
436 if (ret) {
437 xhci_err(xhci, "request interrupt %d failed\n",
438 pdev->irq);
439 return ret;
440 }
441 hcd->irq = pdev->irq;
442 }
443
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700444#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
445 init_timer(&xhci->event_ring_timer);
446 xhci->event_ring_timer.data = (unsigned long) xhci;
Sarah Sharp23e3be12009-04-29 19:05:20 -0700447 xhci->event_ring_timer.function = xhci_event_ring_work;
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700448 /* Poll the event ring */
449 xhci->event_ring_timer.expires = jiffies + POLL_TIMEOUT * HZ;
450 xhci->zombie = 0;
451 xhci_dbg(xhci, "Setting event ring polling timer\n");
452 add_timer(&xhci->event_ring_timer);
453#endif
454
Sarah Sharp66e49d82009-07-27 12:03:46 -0700455 xhci_dbg(xhci, "Command ring memory map follows:\n");
456 xhci_debug_ring(xhci, xhci->cmd_ring);
457 xhci_dbg_ring_ptrs(xhci, xhci->cmd_ring);
458 xhci_dbg_cmd_ptrs(xhci);
459
460 xhci_dbg(xhci, "ERST memory map follows:\n");
461 xhci_dbg_erst(xhci, &xhci->erst);
462 xhci_dbg(xhci, "Event ring:\n");
463 xhci_debug_ring(xhci, xhci->event_ring);
464 xhci_dbg_ring_ptrs(xhci, xhci->event_ring);
465 temp_64 = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
466 temp_64 &= ~ERST_PTR_MASK;
467 xhci_dbg(xhci, "ERST deq = 64'h%0lx\n", (long unsigned int) temp_64);
468
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700469 xhci_dbg(xhci, "// Set the interrupt modulation register\n");
470 temp = xhci_readl(xhci, &xhci->ir_set->irq_control);
Sarah Sharpa4d88302009-05-14 11:44:26 -0700471 temp &= ~ER_IRQ_INTERVAL_MASK;
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700472 temp |= (u32) 160;
473 xhci_writel(xhci, temp, &xhci->ir_set->irq_control);
474
475 /* Set the HCD state before we enable the irqs */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700476 temp = xhci_readl(xhci, &xhci->op_regs->command);
477 temp |= (CMD_EIE);
478 xhci_dbg(xhci, "// Enable interrupts, cmd = 0x%x.\n",
479 temp);
480 xhci_writel(xhci, temp, &xhci->op_regs->command);
481
482 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -0700483 xhci_dbg(xhci, "// Enabling event ring interrupter %p by writing 0x%x to irq_pending\n",
484 xhci->ir_set, (unsigned int) ER_IRQ_ENABLE(temp));
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700485 xhci_writel(xhci, ER_IRQ_ENABLE(temp),
486 &xhci->ir_set->irq_pending);
487 xhci_print_ir_set(xhci, xhci->ir_set, 0);
488
Sarah Sharp02386342010-05-24 13:25:28 -0700489 if (xhci->quirks & XHCI_NEC_HOST)
490 xhci_queue_vendor_command(xhci, 0, 0, 0,
491 TRB_TYPE(TRB_NEC_GET_FW));
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700492
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800493 xhci_dbg(xhci, "Finished xhci_run for USB2 roothub\n");
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700494 return 0;
495}
496
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800497static void xhci_only_stop_hcd(struct usb_hcd *hcd)
498{
499 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
500
501 spin_lock_irq(&xhci->lock);
502 xhci_halt(xhci);
503
504 /* The shared_hcd is going to be deallocated shortly (the USB core only
505 * calls this function when allocation fails in usb_add_hcd(), or
506 * usb_remove_hcd() is called). So we need to unset xHCI's pointer.
507 */
508 xhci->shared_hcd = NULL;
509 spin_unlock_irq(&xhci->lock);
510}
511
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700512/*
513 * Stop xHCI driver.
514 *
515 * This function is called by the USB core when the HC driver is removed.
516 * Its opposite is xhci_run().
517 *
518 * Disable device contexts, disable IRQs, and quiesce the HC.
519 * Reset the HC, finish any completed transactions, and cleanup memory.
520 */
521void xhci_stop(struct usb_hcd *hcd)
522{
523 u32 temp;
524 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
525
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800526 if (!usb_hcd_is_primary_hcd(hcd)) {
527 xhci_only_stop_hcd(xhci->shared_hcd);
528 return;
529 }
530
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700531 spin_lock_irq(&xhci->lock);
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800532 /* Make sure the xHC is halted for a USB3 roothub
533 * (xhci_stop() could be called as part of failed init).
534 */
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700535 xhci_halt(xhci);
536 xhci_reset(xhci);
537 spin_unlock_irq(&xhci->lock);
538
Zhang Rui40a9fb12010-12-17 13:17:04 -0800539 xhci_cleanup_msix(xhci);
540
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700541#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
542 /* Tell the event ring poll function not to reschedule */
543 xhci->zombie = 1;
544 del_timer_sync(&xhci->event_ring_timer);
545#endif
546
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700547 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
548 temp = xhci_readl(xhci, &xhci->op_regs->status);
549 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
550 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
551 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
552 &xhci->ir_set->irq_pending);
553 xhci_print_ir_set(xhci, xhci->ir_set, 0);
554
555 xhci_dbg(xhci, "cleaning up memory\n");
556 xhci_mem_cleanup(xhci);
557 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
558 xhci_readl(xhci, &xhci->op_regs->status));
559}
560
561/*
562 * Shutdown HC (not bus-specific)
563 *
564 * This is called when the machine is rebooting or halting. We assume that the
565 * machine will be powered off, and the HC's internal state will be reset.
566 * Don't bother to free memory.
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800567 *
568 * This will only ever be called with the main usb_hcd (the USB3 roothub).
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700569 */
570void xhci_shutdown(struct usb_hcd *hcd)
571{
572 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
573
574 spin_lock_irq(&xhci->lock);
575 xhci_halt(xhci);
Dong Nguyen43b86af2010-07-21 16:56:08 -0700576 spin_unlock_irq(&xhci->lock);
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700577
Zhang Rui40a9fb12010-12-17 13:17:04 -0800578 xhci_cleanup_msix(xhci);
579
Sarah Sharp66d4ead2009-04-27 19:52:28 -0700580 xhci_dbg(xhci, "xhci_shutdown completed - status = %x\n",
581 xhci_readl(xhci, &xhci->op_regs->status));
582}
583
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700584#ifdef CONFIG_PM
Andiry Xu5535b1d52010-10-14 07:23:06 -0700585static void xhci_save_registers(struct xhci_hcd *xhci)
586{
587 xhci->s3.command = xhci_readl(xhci, &xhci->op_regs->command);
588 xhci->s3.dev_nt = xhci_readl(xhci, &xhci->op_regs->dev_notification);
589 xhci->s3.dcbaa_ptr = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
590 xhci->s3.config_reg = xhci_readl(xhci, &xhci->op_regs->config_reg);
591 xhci->s3.irq_pending = xhci_readl(xhci, &xhci->ir_set->irq_pending);
592 xhci->s3.irq_control = xhci_readl(xhci, &xhci->ir_set->irq_control);
593 xhci->s3.erst_size = xhci_readl(xhci, &xhci->ir_set->erst_size);
594 xhci->s3.erst_base = xhci_read_64(xhci, &xhci->ir_set->erst_base);
595 xhci->s3.erst_dequeue = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
596}
597
598static void xhci_restore_registers(struct xhci_hcd *xhci)
599{
600 xhci_writel(xhci, xhci->s3.command, &xhci->op_regs->command);
601 xhci_writel(xhci, xhci->s3.dev_nt, &xhci->op_regs->dev_notification);
602 xhci_write_64(xhci, xhci->s3.dcbaa_ptr, &xhci->op_regs->dcbaa_ptr);
603 xhci_writel(xhci, xhci->s3.config_reg, &xhci->op_regs->config_reg);
604 xhci_writel(xhci, xhci->s3.irq_pending, &xhci->ir_set->irq_pending);
605 xhci_writel(xhci, xhci->s3.irq_control, &xhci->ir_set->irq_control);
606 xhci_writel(xhci, xhci->s3.erst_size, &xhci->ir_set->erst_size);
607 xhci_write_64(xhci, xhci->s3.erst_base, &xhci->ir_set->erst_base);
608}
609
Sarah Sharp89821322010-11-12 11:59:31 -0800610static void xhci_set_cmd_ring_deq(struct xhci_hcd *xhci)
611{
612 u64 val_64;
613
614 /* step 2: initialize command ring buffer */
615 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
616 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
617 (xhci_trb_virt_to_dma(xhci->cmd_ring->deq_seg,
618 xhci->cmd_ring->dequeue) &
619 (u64) ~CMD_RING_RSVD_BITS) |
620 xhci->cmd_ring->cycle_state;
621 xhci_dbg(xhci, "// Setting command ring address to 0x%llx\n",
622 (long unsigned long) val_64);
623 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
624}
625
626/*
627 * The whole command ring must be cleared to zero when we suspend the host.
628 *
629 * The host doesn't save the command ring pointer in the suspend well, so we
630 * need to re-program it on resume. Unfortunately, the pointer must be 64-byte
631 * aligned, because of the reserved bits in the command ring dequeue pointer
632 * register. Therefore, we can't just set the dequeue pointer back in the
633 * middle of the ring (TRBs are 16-byte aligned).
634 */
635static void xhci_clear_command_ring(struct xhci_hcd *xhci)
636{
637 struct xhci_ring *ring;
638 struct xhci_segment *seg;
639
640 ring = xhci->cmd_ring;
641 seg = ring->deq_seg;
642 do {
643 memset(seg->trbs, 0, SEGMENT_SIZE);
644 seg = seg->next;
645 } while (seg != ring->deq_seg);
646
647 /* Reset the software enqueue and dequeue pointers */
648 ring->deq_seg = ring->first_seg;
649 ring->dequeue = ring->first_seg->trbs;
650 ring->enq_seg = ring->deq_seg;
651 ring->enqueue = ring->dequeue;
652
653 /*
654 * Ring is now zeroed, so the HW should look for change of ownership
655 * when the cycle bit is set to 1.
656 */
657 ring->cycle_state = 1;
658
659 /*
660 * Reset the hardware dequeue pointer.
661 * Yes, this will need to be re-written after resume, but we're paranoid
662 * and want to make sure the hardware doesn't access bogus memory
663 * because, say, the BIOS or an SMI started the host without changing
664 * the command ring pointers.
665 */
666 xhci_set_cmd_ring_deq(xhci);
667}
668
Andiry Xu5535b1d52010-10-14 07:23:06 -0700669/*
670 * Stop HC (not bus-specific)
671 *
672 * This is called when the machine transition into S3/S4 mode.
673 *
674 */
675int xhci_suspend(struct xhci_hcd *xhci)
676{
677 int rc = 0;
678 struct usb_hcd *hcd = xhci_to_hcd(xhci);
679 u32 command;
Andiry Xu00292272010-12-27 17:39:02 +0800680 int i;
Andiry Xu5535b1d52010-10-14 07:23:06 -0700681
682 spin_lock_irq(&xhci->lock);
683 clear_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
684 /* step 1: stop endpoint */
685 /* skipped assuming that port suspend has done */
686
687 /* step 2: clear Run/Stop bit */
688 command = xhci_readl(xhci, &xhci->op_regs->command);
689 command &= ~CMD_RUN;
690 xhci_writel(xhci, command, &xhci->op_regs->command);
691 if (handshake(xhci, &xhci->op_regs->status,
692 STS_HALT, STS_HALT, 100*100)) {
693 xhci_warn(xhci, "WARN: xHC CMD_RUN timeout\n");
694 spin_unlock_irq(&xhci->lock);
695 return -ETIMEDOUT;
696 }
Sarah Sharp89821322010-11-12 11:59:31 -0800697 xhci_clear_command_ring(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700698
699 /* step 3: save registers */
700 xhci_save_registers(xhci);
701
702 /* step 4: set CSS flag */
703 command = xhci_readl(xhci, &xhci->op_regs->command);
704 command |= CMD_CSS;
705 xhci_writel(xhci, command, &xhci->op_regs->command);
706 if (handshake(xhci, &xhci->op_regs->status, STS_SAVE, 0, 10*100)) {
707 xhci_warn(xhci, "WARN: xHC CMD_CSS timeout\n");
708 spin_unlock_irq(&xhci->lock);
709 return -ETIMEDOUT;
710 }
Andiry Xu5535b1d52010-10-14 07:23:06 -0700711 spin_unlock_irq(&xhci->lock);
712
Andiry Xu00292272010-12-27 17:39:02 +0800713 /* step 5: remove core well power */
714 /* synchronize irq when using MSI-X */
715 if (xhci->msix_entries) {
716 for (i = 0; i < xhci->msix_count; i++)
717 synchronize_irq(xhci->msix_entries[i].vector);
718 }
719
Andiry Xu5535b1d52010-10-14 07:23:06 -0700720 return rc;
721}
722
723/*
724 * start xHC (not bus-specific)
725 *
726 * This is called when the machine transition from S3/S4 mode.
727 *
728 */
729int xhci_resume(struct xhci_hcd *xhci, bool hibernated)
730{
731 u32 command, temp = 0;
732 struct usb_hcd *hcd = xhci_to_hcd(xhci);
Andiry Xu019a35f2011-01-06 15:43:17 +0800733 int retval;
Andiry Xu5535b1d52010-10-14 07:23:06 -0700734
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800735 /* Wait a bit if either of the roothubs need to settle from the
736 * transistion into bus suspend.
Sarah Sharp20b67cf2010-12-15 12:47:14 -0800737 */
Sarah Sharpf6ff0ac2010-12-16 11:21:10 -0800738 if (time_before(jiffies, xhci->bus_state[0].next_statechange) ||
739 time_before(jiffies,
740 xhci->bus_state[1].next_statechange))
Andiry Xu5535b1d52010-10-14 07:23:06 -0700741 msleep(100);
742
743 spin_lock_irq(&xhci->lock);
744
745 if (!hibernated) {
746 /* step 1: restore register */
747 xhci_restore_registers(xhci);
748 /* step 2: initialize command ring buffer */
Sarah Sharp89821322010-11-12 11:59:31 -0800749 xhci_set_cmd_ring_deq(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700750 /* step 3: restore state and start state*/
751 /* step 3: set CRS flag */
752 command = xhci_readl(xhci, &xhci->op_regs->command);
753 command |= CMD_CRS;
754 xhci_writel(xhci, command, &xhci->op_regs->command);
755 if (handshake(xhci, &xhci->op_regs->status,
756 STS_RESTORE, 0, 10*100)) {
757 xhci_dbg(xhci, "WARN: xHC CMD_CSS timeout\n");
758 spin_unlock_irq(&xhci->lock);
759 return -ETIMEDOUT;
760 }
761 temp = xhci_readl(xhci, &xhci->op_regs->status);
762 }
763
764 /* If restore operation fails, re-initialize the HC during resume */
765 if ((temp & STS_SRE) || hibernated) {
766 usb_root_hub_lost_power(hcd->self.root_hub);
767
768 xhci_dbg(xhci, "Stop HCD\n");
769 xhci_halt(xhci);
770 xhci_reset(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700771 spin_unlock_irq(&xhci->lock);
Andiry Xu00292272010-12-27 17:39:02 +0800772 xhci_cleanup_msix(xhci);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700773
774#ifdef CONFIG_USB_XHCI_HCD_DEBUGGING
775 /* Tell the event ring poll function not to reschedule */
776 xhci->zombie = 1;
777 del_timer_sync(&xhci->event_ring_timer);
778#endif
779
780 xhci_dbg(xhci, "// Disabling event ring interrupts\n");
781 temp = xhci_readl(xhci, &xhci->op_regs->status);
782 xhci_writel(xhci, temp & ~STS_EINT, &xhci->op_regs->status);
783 temp = xhci_readl(xhci, &xhci->ir_set->irq_pending);
784 xhci_writel(xhci, ER_IRQ_DISABLE(temp),
785 &xhci->ir_set->irq_pending);
786 xhci_print_ir_set(xhci, xhci->ir_set, 0);
787
788 xhci_dbg(xhci, "cleaning up memory\n");
789 xhci_mem_cleanup(xhci);
790 xhci_dbg(xhci, "xhci_stop completed - status = %x\n",
791 xhci_readl(xhci, &xhci->op_regs->status));
792
793 xhci_dbg(xhci, "Initialize the HCD\n");
794 retval = xhci_init(hcd);
795 if (retval)
796 return retval;
797
798 xhci_dbg(xhci, "Start the HCD\n");
799 retval = xhci_run(hcd);
800 if (!retval)
801 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
802 hcd->state = HC_STATE_SUSPENDED;
803 return retval;
804 }
805
Andiry Xu5535b1d52010-10-14 07:23:06 -0700806 /* step 4: set Run/Stop bit */
807 command = xhci_readl(xhci, &xhci->op_regs->command);
808 command |= CMD_RUN;
809 xhci_writel(xhci, command, &xhci->op_regs->command);
810 handshake(xhci, &xhci->op_regs->status, STS_HALT,
811 0, 250 * 1000);
812
813 /* step 5: walk topology and initialize portsc,
814 * portpmsc and portli
815 */
816 /* this is done in bus_resume */
817
818 /* step 6: restart each of the previously
819 * Running endpoints by ringing their doorbells
820 */
821
822 set_bit(HCD_FLAG_HW_ACCESSIBLE, &hcd->flags);
Andiry Xu5535b1d52010-10-14 07:23:06 -0700823
824 spin_unlock_irq(&xhci->lock);
825 return 0;
826}
Sarah Sharpb5b5c3a2010-10-15 11:24:14 -0700827#endif /* CONFIG_PM */
828
Sarah Sharp7f84eef2009-04-27 19:53:56 -0700829/*-------------------------------------------------------------------------*/
830
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700831/**
832 * xhci_get_endpoint_index - Used for passing endpoint bitmasks between the core and
833 * HCDs. Find the index for an endpoint given its descriptor. Use the return
834 * value to right shift 1 for the bitmask.
835 *
836 * Index = (epnum * 2) + direction - 1,
837 * where direction = 0 for OUT, 1 for IN.
838 * For control endpoints, the IN index is used (OUT index is unused), so
839 * index = (epnum * 2) + direction - 1 = (epnum * 2) + 1 - 1 = (epnum * 2)
840 */
841unsigned int xhci_get_endpoint_index(struct usb_endpoint_descriptor *desc)
842{
843 unsigned int index;
844 if (usb_endpoint_xfer_control(desc))
845 index = (unsigned int) (usb_endpoint_num(desc)*2);
846 else
847 index = (unsigned int) (usb_endpoint_num(desc)*2) +
848 (usb_endpoint_dir_in(desc) ? 1 : 0) - 1;
849 return index;
850}
851
Sarah Sharpf94e01862009-04-27 19:58:38 -0700852/* Find the flag for this endpoint (for use in the control context). Use the
853 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
854 * bit 1, etc.
855 */
856unsigned int xhci_get_endpoint_flag(struct usb_endpoint_descriptor *desc)
857{
858 return 1 << (xhci_get_endpoint_index(desc) + 1);
859}
860
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700861/* Find the flag for this endpoint (for use in the control context). Use the
862 * endpoint index to create a bitmask. The slot context is bit 0, endpoint 0 is
863 * bit 1, etc.
864 */
865unsigned int xhci_get_endpoint_flag_from_index(unsigned int ep_index)
866{
867 return 1 << (ep_index + 1);
868}
869
Sarah Sharpf94e01862009-04-27 19:58:38 -0700870/* Compute the last valid endpoint context index. Basically, this is the
871 * endpoint index plus one. For slot contexts with more than valid endpoint,
872 * we find the most significant bit set in the added contexts flags.
873 * e.g. ep 1 IN (with epnum 0x81) => added_ctxs = 0b1000
874 * fls(0b1000) = 4, but the endpoint context index is 3, so subtract one.
875 */
Sarah Sharpac9d8fe2009-08-07 14:04:55 -0700876unsigned int xhci_last_valid_endpoint(u32 added_ctxs)
Sarah Sharpf94e01862009-04-27 19:58:38 -0700877{
878 return fls(added_ctxs) - 1;
879}
880
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700881/* Returns 1 if the arguments are OK;
882 * returns 0 this is a root hub; returns -EINVAL for NULL pointers.
883 */
884int xhci_check_args(struct usb_hcd *hcd, struct usb_device *udev,
Andiry Xu64927732010-10-14 07:22:45 -0700885 struct usb_host_endpoint *ep, int check_ep, bool check_virt_dev,
886 const char *func) {
887 struct xhci_hcd *xhci;
888 struct xhci_virt_device *virt_dev;
889
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700890 if (!hcd || (check_ep && !ep) || !udev) {
891 printk(KERN_DEBUG "xHCI %s called with invalid args\n",
892 func);
893 return -EINVAL;
894 }
895 if (!udev->parent) {
896 printk(KERN_DEBUG "xHCI %s called for root hub\n",
897 func);
898 return 0;
899 }
Andiry Xu64927732010-10-14 07:22:45 -0700900
901 if (check_virt_dev) {
902 xhci = hcd_to_xhci(hcd);
903 if (!udev->slot_id || !xhci->devs
904 || !xhci->devs[udev->slot_id]) {
905 printk(KERN_DEBUG "xHCI %s called with unaddressed "
906 "device\n", func);
907 return -EINVAL;
908 }
909
910 virt_dev = xhci->devs[udev->slot_id];
911 if (virt_dev->udev != udev) {
912 printk(KERN_DEBUG "xHCI %s called with udev and "
913 "virt_dev does not match\n", func);
914 return -EINVAL;
915 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700916 }
Andiry Xu64927732010-10-14 07:22:45 -0700917
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700918 return 1;
919}
920
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700921static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -0700922 struct usb_device *udev, struct xhci_command *command,
923 bool ctx_change, bool must_succeed);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700924
925/*
926 * Full speed devices may have a max packet size greater than 8 bytes, but the
927 * USB core doesn't know that until it reads the first 8 bytes of the
928 * descriptor. If the usb_device's max packet size changes after that point,
929 * we need to issue an evaluate context command and wait on it.
930 */
931static int xhci_check_maxpacket(struct xhci_hcd *xhci, unsigned int slot_id,
932 unsigned int ep_index, struct urb *urb)
933{
934 struct xhci_container_ctx *in_ctx;
935 struct xhci_container_ctx *out_ctx;
936 struct xhci_input_control_ctx *ctrl_ctx;
937 struct xhci_ep_ctx *ep_ctx;
938 int max_packet_size;
939 int hw_max_packet_size;
940 int ret = 0;
941
942 out_ctx = xhci->devs[slot_id]->out_ctx;
943 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
944 hw_max_packet_size = MAX_PACKET_DECODED(ep_ctx->ep_info2);
945 max_packet_size = urb->dev->ep0.desc.wMaxPacketSize;
946 if (hw_max_packet_size != max_packet_size) {
947 xhci_dbg(xhci, "Max Packet Size for ep 0 changed.\n");
948 xhci_dbg(xhci, "Max packet size in usb_device = %d\n",
949 max_packet_size);
950 xhci_dbg(xhci, "Max packet size in xHCI HW = %d\n",
951 hw_max_packet_size);
952 xhci_dbg(xhci, "Issuing evaluate context command.\n");
953
954 /* Set up the modified control endpoint 0 */
Sarah Sharp913a8a32009-09-04 10:53:13 -0700955 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
956 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700957 in_ctx = xhci->devs[slot_id]->in_ctx;
958 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
959 ep_ctx->ep_info2 &= ~MAX_PACKET_MASK;
960 ep_ctx->ep_info2 |= MAX_PACKET(max_packet_size);
961
962 /* Set up the input context flags for the command */
963 /* FIXME: This won't work if a non-default control endpoint
964 * changes max packet sizes.
965 */
966 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
967 ctrl_ctx->add_flags = EP0_FLAG;
968 ctrl_ctx->drop_flags = 0;
969
970 xhci_dbg(xhci, "Slot %d input context\n", slot_id);
971 xhci_dbg_ctx(xhci, in_ctx, ep_index);
972 xhci_dbg(xhci, "Slot %d output context\n", slot_id);
973 xhci_dbg_ctx(xhci, out_ctx, ep_index);
974
Sarah Sharp913a8a32009-09-04 10:53:13 -0700975 ret = xhci_configure_endpoint(xhci, urb->dev, NULL,
976 true, false);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700977
978 /* Clean up the input context for later use by bandwidth
979 * functions.
980 */
981 ctrl_ctx->add_flags = SLOT_FLAG;
982 }
983 return ret;
984}
985
Sarah Sharpd0e96f52009-04-27 19:58:01 -0700986/*
987 * non-error returns are a promise to giveback() the urb later
988 * we drop ownership so next owner (or urb unlink) can get it
989 */
990int xhci_urb_enqueue(struct usb_hcd *hcd, struct urb *urb, gfp_t mem_flags)
991{
992 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
993 unsigned long flags;
994 int ret = 0;
995 unsigned int slot_id, ep_index;
Andiry Xu8e51adc2010-07-22 15:23:31 -0700996 struct urb_priv *urb_priv;
997 int size, i;
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -0700998
Andiry Xu64927732010-10-14 07:22:45 -0700999 if (!urb || xhci_check_args(hcd, urb->dev, urb->ep,
1000 true, true, __func__) <= 0)
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001001 return -EINVAL;
1002
1003 slot_id = urb->dev->slot_id;
1004 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001005
Alan Stern541c7d42010-06-22 16:39:10 -04001006 if (!HCD_HW_ACCESSIBLE(hcd)) {
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001007 if (!in_interrupt())
1008 xhci_dbg(xhci, "urb submitted during PCI suspend\n");
1009 ret = -ESHUTDOWN;
1010 goto exit;
1011 }
Andiry Xu8e51adc2010-07-22 15:23:31 -07001012
1013 if (usb_endpoint_xfer_isoc(&urb->ep->desc))
1014 size = urb->number_of_packets;
1015 else
1016 size = 1;
1017
1018 urb_priv = kzalloc(sizeof(struct urb_priv) +
1019 size * sizeof(struct xhci_td *), mem_flags);
1020 if (!urb_priv)
1021 return -ENOMEM;
1022
1023 for (i = 0; i < size; i++) {
1024 urb_priv->td[i] = kzalloc(sizeof(struct xhci_td), mem_flags);
1025 if (!urb_priv->td[i]) {
1026 urb_priv->length = i;
1027 xhci_urb_free_priv(xhci, urb_priv);
1028 return -ENOMEM;
1029 }
1030 }
1031
1032 urb_priv->length = size;
1033 urb_priv->td_cnt = 0;
1034 urb->hcpriv = urb_priv;
1035
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001036 if (usb_endpoint_xfer_control(&urb->ep->desc)) {
1037 /* Check to see if the max packet size for the default control
1038 * endpoint changed during FS device enumeration
1039 */
1040 if (urb->dev->speed == USB_SPEED_FULL) {
1041 ret = xhci_check_maxpacket(xhci, slot_id,
1042 ep_index, urb);
1043 if (ret < 0)
1044 return ret;
1045 }
1046
Sarah Sharpb11069f2009-07-27 12:03:23 -07001047 /* We have a spinlock and interrupts disabled, so we must pass
1048 * atomic context to this function, which may allocate memory.
1049 */
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001050 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001051 if (xhci->xhc_state & XHCI_STATE_DYING)
1052 goto dying;
Sarah Sharpb11069f2009-07-27 12:03:23 -07001053 ret = xhci_queue_ctrl_tx(xhci, GFP_ATOMIC, urb,
Sarah Sharp23e3be12009-04-29 19:05:20 -07001054 slot_id, ep_index);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001055 spin_unlock_irqrestore(&xhci->lock, flags);
1056 } else if (usb_endpoint_xfer_bulk(&urb->ep->desc)) {
1057 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001058 if (xhci->xhc_state & XHCI_STATE_DYING)
1059 goto dying;
Sarah Sharp8df75f42010-04-02 15:34:16 -07001060 if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1061 EP_GETTING_STREAMS) {
1062 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1063 "is transitioning to using streams.\n");
1064 ret = -EINVAL;
1065 } else if (xhci->devs[slot_id]->eps[ep_index].ep_state &
1066 EP_GETTING_NO_STREAMS) {
1067 xhci_warn(xhci, "WARN: Can't enqueue URB while bulk ep "
1068 "is transitioning to "
1069 "not having streams.\n");
1070 ret = -EINVAL;
1071 } else {
1072 ret = xhci_queue_bulk_tx(xhci, GFP_ATOMIC, urb,
1073 slot_id, ep_index);
1074 }
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001075 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp624defa2009-09-02 12:14:28 -07001076 } else if (usb_endpoint_xfer_int(&urb->ep->desc)) {
1077 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001078 if (xhci->xhc_state & XHCI_STATE_DYING)
1079 goto dying;
Sarah Sharp624defa2009-09-02 12:14:28 -07001080 ret = xhci_queue_intr_tx(xhci, GFP_ATOMIC, urb,
1081 slot_id, ep_index);
1082 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001083 } else {
Andiry Xu787f4e52010-07-22 15:23:52 -07001084 spin_lock_irqsave(&xhci->lock, flags);
1085 if (xhci->xhc_state & XHCI_STATE_DYING)
1086 goto dying;
1087 ret = xhci_queue_isoc_tx_prepare(xhci, GFP_ATOMIC, urb,
1088 slot_id, ep_index);
1089 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp2d3f1fa2009-08-07 14:04:49 -07001090 }
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001091exit:
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001092 return ret;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001093dying:
Andiry Xu8e51adc2010-07-22 15:23:31 -07001094 xhci_urb_free_priv(xhci, urb_priv);
1095 urb->hcpriv = NULL;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001096 xhci_dbg(xhci, "Ep 0x%x: URB %p submitted for "
1097 "non-responsive xHCI host.\n",
1098 urb->ep->desc.bEndpointAddress, urb);
1099 spin_unlock_irqrestore(&xhci->lock, flags);
1100 return -ESHUTDOWN;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001101}
1102
Sarah Sharp021bff92010-07-29 22:12:20 -07001103/* Get the right ring for the given URB.
1104 * If the endpoint supports streams, boundary check the URB's stream ID.
1105 * If the endpoint doesn't support streams, return the singular endpoint ring.
1106 */
1107static struct xhci_ring *xhci_urb_to_transfer_ring(struct xhci_hcd *xhci,
1108 struct urb *urb)
1109{
1110 unsigned int slot_id;
1111 unsigned int ep_index;
1112 unsigned int stream_id;
1113 struct xhci_virt_ep *ep;
1114
1115 slot_id = urb->dev->slot_id;
1116 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
1117 stream_id = urb->stream_id;
1118 ep = &xhci->devs[slot_id]->eps[ep_index];
1119 /* Common case: no streams */
1120 if (!(ep->ep_state & EP_HAS_STREAMS))
1121 return ep->ring;
1122
1123 if (stream_id == 0) {
1124 xhci_warn(xhci,
1125 "WARN: Slot ID %u, ep index %u has streams, "
1126 "but URB has no stream ID.\n",
1127 slot_id, ep_index);
1128 return NULL;
1129 }
1130
1131 if (stream_id < ep->stream_info->num_streams)
1132 return ep->stream_info->stream_rings[stream_id];
1133
1134 xhci_warn(xhci,
1135 "WARN: Slot ID %u, ep index %u has "
1136 "stream IDs 1 to %u allocated, "
1137 "but stream ID %u is requested.\n",
1138 slot_id, ep_index,
1139 ep->stream_info->num_streams - 1,
1140 stream_id);
1141 return NULL;
1142}
1143
Sarah Sharpae636742009-04-29 19:02:31 -07001144/*
1145 * Remove the URB's TD from the endpoint ring. This may cause the HC to stop
1146 * USB transfers, potentially stopping in the middle of a TRB buffer. The HC
1147 * should pick up where it left off in the TD, unless a Set Transfer Ring
1148 * Dequeue Pointer is issued.
1149 *
1150 * The TRBs that make up the buffers for the canceled URB will be "removed" from
1151 * the ring. Since the ring is a contiguous structure, they can't be physically
1152 * removed. Instead, there are two options:
1153 *
1154 * 1) If the HC is in the middle of processing the URB to be canceled, we
1155 * simply move the ring's dequeue pointer past those TRBs using the Set
1156 * Transfer Ring Dequeue Pointer command. This will be the common case,
1157 * when drivers timeout on the last submitted URB and attempt to cancel.
1158 *
1159 * 2) If the HC is in the middle of a different TD, we turn the TRBs into a
1160 * series of 1-TRB transfer no-op TDs. (No-ops shouldn't be chained.) The
1161 * HC will need to invalidate the any TRBs it has cached after the stop
1162 * endpoint command, as noted in the xHCI 0.95 errata.
1163 *
1164 * 3) The TD may have completed by the time the Stop Endpoint Command
1165 * completes, so software needs to handle that case too.
1166 *
1167 * This function should protect against the TD enqueueing code ringing the
1168 * doorbell while this code is waiting for a Stop Endpoint command to complete.
1169 * It also needs to account for multiple cancellations on happening at the same
1170 * time for the same endpoint.
1171 *
1172 * Note that this function can be called in any context, or so says
1173 * usb_hcd_unlink_urb()
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001174 */
1175int xhci_urb_dequeue(struct usb_hcd *hcd, struct urb *urb, int status)
1176{
Sarah Sharpae636742009-04-29 19:02:31 -07001177 unsigned long flags;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001178 int ret, i;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001179 u32 temp;
Sarah Sharpae636742009-04-29 19:02:31 -07001180 struct xhci_hcd *xhci;
Andiry Xu8e51adc2010-07-22 15:23:31 -07001181 struct urb_priv *urb_priv;
Sarah Sharpae636742009-04-29 19:02:31 -07001182 struct xhci_td *td;
1183 unsigned int ep_index;
1184 struct xhci_ring *ep_ring;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001185 struct xhci_virt_ep *ep;
Sarah Sharpae636742009-04-29 19:02:31 -07001186
1187 xhci = hcd_to_xhci(hcd);
1188 spin_lock_irqsave(&xhci->lock, flags);
1189 /* Make sure the URB hasn't completed or been unlinked already */
1190 ret = usb_hcd_check_unlink_urb(hcd, urb, status);
1191 if (ret || !urb->hcpriv)
1192 goto done;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001193 temp = xhci_readl(xhci, &xhci->op_regs->status);
1194 if (temp == 0xffffffff) {
1195 xhci_dbg(xhci, "HW died, freeing TD.\n");
Andiry Xu8e51adc2010-07-22 15:23:31 -07001196 urb_priv = urb->hcpriv;
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001197
1198 usb_hcd_unlink_urb_from_ep(hcd, urb);
1199 spin_unlock_irqrestore(&xhci->lock, flags);
Sarah Sharp214f76f2010-10-26 11:22:02 -07001200 usb_hcd_giveback_urb(hcd, urb, -ESHUTDOWN);
Andiry Xu8e51adc2010-07-22 15:23:31 -07001201 xhci_urb_free_priv(xhci, urb_priv);
Sarah Sharpe34b2fb2009-09-28 17:21:37 -07001202 return ret;
1203 }
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001204 if (xhci->xhc_state & XHCI_STATE_DYING) {
1205 xhci_dbg(xhci, "Ep 0x%x: URB %p to be canceled on "
1206 "non-responsive xHCI host.\n",
1207 urb->ep->desc.bEndpointAddress, urb);
1208 /* Let the stop endpoint command watchdog timer (which set this
1209 * state) finish cleaning up the endpoint TD lists. We must
1210 * have caught it in the middle of dropping a lock and giving
1211 * back an URB.
1212 */
1213 goto done;
1214 }
Sarah Sharpae636742009-04-29 19:02:31 -07001215
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001216 xhci_dbg(xhci, "Cancel URB %p\n", urb);
Sarah Sharp66e49d82009-07-27 12:03:46 -07001217 xhci_dbg(xhci, "Event ring:\n");
1218 xhci_debug_ring(xhci, xhci->event_ring);
Sarah Sharpae636742009-04-29 19:02:31 -07001219 ep_index = xhci_get_endpoint_index(&urb->ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001220 ep = &xhci->devs[urb->dev->slot_id]->eps[ep_index];
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001221 ep_ring = xhci_urb_to_transfer_ring(xhci, urb);
1222 if (!ep_ring) {
1223 ret = -EINVAL;
1224 goto done;
1225 }
1226
Sarah Sharp66e49d82009-07-27 12:03:46 -07001227 xhci_dbg(xhci, "Endpoint ring:\n");
1228 xhci_debug_ring(xhci, ep_ring);
Sarah Sharpae636742009-04-29 19:02:31 -07001229
Andiry Xu8e51adc2010-07-22 15:23:31 -07001230 urb_priv = urb->hcpriv;
1231
1232 for (i = urb_priv->td_cnt; i < urb_priv->length; i++) {
1233 td = urb_priv->td[i];
1234 list_add_tail(&td->cancelled_td_list, &ep->cancelled_td_list);
1235 }
1236
Sarah Sharpae636742009-04-29 19:02:31 -07001237 /* Queue a stop endpoint command, but only if this is
1238 * the first cancellation to be handled.
1239 */
Sarah Sharp678539c2009-10-27 10:55:52 -07001240 if (!(ep->ep_state & EP_HALT_PENDING)) {
1241 ep->ep_state |= EP_HALT_PENDING;
Sarah Sharp6f5165c2009-10-27 10:57:01 -07001242 ep->stop_cmds_pending++;
1243 ep->stop_cmd_timer.expires = jiffies +
1244 XHCI_STOP_EP_CMD_TIMEOUT * HZ;
1245 add_timer(&ep->stop_cmd_timer);
Andiry Xube88fe42010-10-14 07:22:57 -07001246 xhci_queue_stop_endpoint(xhci, urb->dev->slot_id, ep_index, 0);
Sarah Sharp23e3be12009-04-29 19:05:20 -07001247 xhci_ring_cmd_db(xhci);
Sarah Sharpae636742009-04-29 19:02:31 -07001248 }
1249done:
1250 spin_unlock_irqrestore(&xhci->lock, flags);
1251 return ret;
Sarah Sharpd0e96f52009-04-27 19:58:01 -07001252}
1253
Sarah Sharpf94e01862009-04-27 19:58:38 -07001254/* Drop an endpoint from a new bandwidth configuration for this device.
1255 * Only one call to this function is allowed per endpoint before
1256 * check_bandwidth() or reset_bandwidth() must be called.
1257 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1258 * add the endpoint to the schedule with possibly new parameters denoted by a
1259 * different endpoint descriptor in usb_host_endpoint.
1260 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1261 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001262 *
1263 * The USB core will not allow URBs to be queued to an endpoint that is being
1264 * disabled, so there's no need for mutual exclusion to protect
1265 * the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001266 */
1267int xhci_drop_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1268 struct usb_host_endpoint *ep)
1269{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001270 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001271 struct xhci_container_ctx *in_ctx, *out_ctx;
1272 struct xhci_input_control_ctx *ctrl_ctx;
1273 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001274 unsigned int last_ctx;
1275 unsigned int ep_index;
1276 struct xhci_ep_ctx *ep_ctx;
1277 u32 drop_flag;
1278 u32 new_add_flags, new_drop_flags, new_slot_info;
1279 int ret;
1280
Andiry Xu64927732010-10-14 07:22:45 -07001281 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001282 if (ret <= 0)
1283 return ret;
1284 xhci = hcd_to_xhci(hcd);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001285 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001286
1287 drop_flag = xhci_get_endpoint_flag(&ep->desc);
1288 if (drop_flag == SLOT_FLAG || drop_flag == EP0_FLAG) {
1289 xhci_dbg(xhci, "xHCI %s - can't drop slot or ep 0 %#x\n",
1290 __func__, drop_flag);
1291 return 0;
1292 }
1293
Sarah Sharpf94e01862009-04-27 19:58:38 -07001294 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001295 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1296 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001297 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001298 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001299 /* If the HC already knows the endpoint is disabled,
1300 * or the HCD has noted it is disabled, ignore this request
1301 */
1302 if ((ep_ctx->ep_info & EP_STATE_MASK) == EP_STATE_DISABLED ||
John Yound115b042009-07-27 12:05:15 -07001303 ctrl_ctx->drop_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001304 xhci_warn(xhci, "xHCI %s called with disabled ep %p\n",
1305 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001306 return 0;
1307 }
1308
John Yound115b042009-07-27 12:05:15 -07001309 ctrl_ctx->drop_flags |= drop_flag;
1310 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001311
Sarah Sharp0a023c62009-09-18 08:55:12 -07001312 ctrl_ctx->add_flags &= ~drop_flag;
John Yound115b042009-07-27 12:05:15 -07001313 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001314
John Yound115b042009-07-27 12:05:15 -07001315 last_ctx = xhci_last_valid_endpoint(ctrl_ctx->add_flags);
1316 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001317 /* Update the last valid endpoint context, if we deleted the last one */
John Yound115b042009-07-27 12:05:15 -07001318 if ((slot_ctx->dev_info & LAST_CTX_MASK) > LAST_CTX(last_ctx)) {
1319 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1320 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001321 }
John Yound115b042009-07-27 12:05:15 -07001322 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001323
1324 xhci_endpoint_zero(xhci, xhci->devs[udev->slot_id], ep);
1325
Sarah Sharpf94e01862009-04-27 19:58:38 -07001326 xhci_dbg(xhci, "drop ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1327 (unsigned int) ep->desc.bEndpointAddress,
1328 udev->slot_id,
1329 (unsigned int) new_drop_flags,
1330 (unsigned int) new_add_flags,
1331 (unsigned int) new_slot_info);
1332 return 0;
1333}
1334
1335/* Add an endpoint to a new possible bandwidth configuration for this device.
1336 * Only one call to this function is allowed per endpoint before
1337 * check_bandwidth() or reset_bandwidth() must be called.
1338 * A call to xhci_drop_endpoint() followed by a call to xhci_add_endpoint() will
1339 * add the endpoint to the schedule with possibly new parameters denoted by a
1340 * different endpoint descriptor in usb_host_endpoint.
1341 * A call to xhci_add_endpoint() followed by a call to xhci_drop_endpoint() is
1342 * not allowed.
Sarah Sharpf88ba782009-05-14 11:44:22 -07001343 *
1344 * The USB core will not allow URBs to be queued to an endpoint until the
1345 * configuration or alt setting is installed in the device, so there's no need
1346 * for mutual exclusion to protect the xhci->devs[slot_id] structure.
Sarah Sharpf94e01862009-04-27 19:58:38 -07001347 */
1348int xhci_add_endpoint(struct usb_hcd *hcd, struct usb_device *udev,
1349 struct usb_host_endpoint *ep)
1350{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001351 struct xhci_hcd *xhci;
John Yound115b042009-07-27 12:05:15 -07001352 struct xhci_container_ctx *in_ctx, *out_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001353 unsigned int ep_index;
1354 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001355 struct xhci_slot_ctx *slot_ctx;
1356 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001357 u32 added_ctxs;
1358 unsigned int last_ctx;
1359 u32 new_add_flags, new_drop_flags, new_slot_info;
1360 int ret = 0;
1361
Andiry Xu64927732010-10-14 07:22:45 -07001362 ret = xhci_check_args(hcd, udev, ep, 1, true, __func__);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001363 if (ret <= 0) {
1364 /* So we won't queue a reset ep command for a root hub */
1365 ep->hcpriv = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001366 return ret;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001367 }
Sarah Sharpf94e01862009-04-27 19:58:38 -07001368 xhci = hcd_to_xhci(hcd);
1369
1370 added_ctxs = xhci_get_endpoint_flag(&ep->desc);
1371 last_ctx = xhci_last_valid_endpoint(added_ctxs);
1372 if (added_ctxs == SLOT_FLAG || added_ctxs == EP0_FLAG) {
1373 /* FIXME when we have to issue an evaluate endpoint command to
1374 * deal with ep0 max packet size changing once we get the
1375 * descriptors
1376 */
1377 xhci_dbg(xhci, "xHCI %s - can't add slot or ep 0 %#x\n",
1378 __func__, added_ctxs);
1379 return 0;
1380 }
1381
Sarah Sharpf94e01862009-04-27 19:58:38 -07001382 in_ctx = xhci->devs[udev->slot_id]->in_ctx;
John Yound115b042009-07-27 12:05:15 -07001383 out_ctx = xhci->devs[udev->slot_id]->out_ctx;
1384 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001385 ep_index = xhci_get_endpoint_index(&ep->desc);
John Yound115b042009-07-27 12:05:15 -07001386 ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001387 /* If the HCD has already noted the endpoint is enabled,
1388 * ignore this request.
1389 */
John Yound115b042009-07-27 12:05:15 -07001390 if (ctrl_ctx->add_flags & xhci_get_endpoint_flag(&ep->desc)) {
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001391 xhci_warn(xhci, "xHCI %s called with enabled ep %p\n",
1392 __func__, ep);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001393 return 0;
1394 }
1395
Sarah Sharpf88ba782009-05-14 11:44:22 -07001396 /*
1397 * Configuration and alternate setting changes must be done in
1398 * process context, not interrupt context (or so documenation
1399 * for usb_set_interface() and usb_set_configuration() claim).
1400 */
1401 if (xhci_endpoint_init(xhci, xhci->devs[udev->slot_id],
Oliver Neukum319c3ea2009-12-16 19:43:59 +01001402 udev, ep, GFP_NOIO) < 0) {
Sarah Sharpf94e01862009-04-27 19:58:38 -07001403 dev_dbg(&udev->dev, "%s - could not initialize ep %#x\n",
1404 __func__, ep->desc.bEndpointAddress);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001405 return -ENOMEM;
1406 }
1407
John Yound115b042009-07-27 12:05:15 -07001408 ctrl_ctx->add_flags |= added_ctxs;
1409 new_add_flags = ctrl_ctx->add_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001410
1411 /* If xhci_endpoint_disable() was called for this endpoint, but the
1412 * xHC hasn't been notified yet through the check_bandwidth() call,
1413 * this re-adds a new state for the endpoint from the new endpoint
1414 * descriptors. We must drop and re-add this endpoint, so we leave the
1415 * drop flags alone.
1416 */
John Yound115b042009-07-27 12:05:15 -07001417 new_drop_flags = ctrl_ctx->drop_flags;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001418
John Yound115b042009-07-27 12:05:15 -07001419 slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001420 /* Update the last valid endpoint context, if we just added one past */
John Yound115b042009-07-27 12:05:15 -07001421 if ((slot_ctx->dev_info & LAST_CTX_MASK) < LAST_CTX(last_ctx)) {
1422 slot_ctx->dev_info &= ~LAST_CTX_MASK;
1423 slot_ctx->dev_info |= LAST_CTX(last_ctx);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001424 }
John Yound115b042009-07-27 12:05:15 -07001425 new_slot_info = slot_ctx->dev_info;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001426
Sarah Sharpa1587d92009-07-27 12:03:15 -07001427 /* Store the usb_device pointer for later use */
1428 ep->hcpriv = udev;
1429
Sarah Sharpf94e01862009-04-27 19:58:38 -07001430 xhci_dbg(xhci, "add ep 0x%x, slot id %d, new drop flags = %#x, new add flags = %#x, new slot info = %#x\n",
1431 (unsigned int) ep->desc.bEndpointAddress,
1432 udev->slot_id,
1433 (unsigned int) new_drop_flags,
1434 (unsigned int) new_add_flags,
1435 (unsigned int) new_slot_info);
1436 return 0;
1437}
1438
John Yound115b042009-07-27 12:05:15 -07001439static void xhci_zero_in_ctx(struct xhci_hcd *xhci, struct xhci_virt_device *virt_dev)
Sarah Sharpf94e01862009-04-27 19:58:38 -07001440{
John Yound115b042009-07-27 12:05:15 -07001441 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001442 struct xhci_ep_ctx *ep_ctx;
John Yound115b042009-07-27 12:05:15 -07001443 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001444 int i;
1445
1446 /* When a device's add flag and drop flag are zero, any subsequent
1447 * configure endpoint command will leave that endpoint's state
1448 * untouched. Make sure we don't leave any old state in the input
1449 * endpoint contexts.
1450 */
John Yound115b042009-07-27 12:05:15 -07001451 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1452 ctrl_ctx->drop_flags = 0;
1453 ctrl_ctx->add_flags = 0;
1454 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1455 slot_ctx->dev_info &= ~LAST_CTX_MASK;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001456 /* Endpoint 0 is always valid */
John Yound115b042009-07-27 12:05:15 -07001457 slot_ctx->dev_info |= LAST_CTX(1);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001458 for (i = 1; i < 31; ++i) {
John Yound115b042009-07-27 12:05:15 -07001459 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001460 ep_ctx->ep_info = 0;
1461 ep_ctx->ep_info2 = 0;
Sarah Sharp8e595a52009-07-27 12:03:31 -07001462 ep_ctx->deq = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001463 ep_ctx->tx_info = 0;
1464 }
1465}
1466
Sarah Sharpf2217e82009-08-07 14:04:43 -07001467static int xhci_configure_endpoint_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001468 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001469{
1470 int ret;
1471
Sarah Sharp913a8a32009-09-04 10:53:13 -07001472 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001473 case COMP_ENOMEM:
1474 dev_warn(&udev->dev, "Not enough host controller resources "
1475 "for new device state.\n");
1476 ret = -ENOMEM;
1477 /* FIXME: can we allocate more resources for the HC? */
1478 break;
1479 case COMP_BW_ERR:
1480 dev_warn(&udev->dev, "Not enough bandwidth "
1481 "for new device state.\n");
1482 ret = -ENOSPC;
1483 /* FIXME: can we go back to the old state? */
1484 break;
1485 case COMP_TRB_ERR:
1486 /* the HCD set up something wrong */
1487 dev_warn(&udev->dev, "ERROR: Endpoint drop flag = 0, "
1488 "add flag = 1, "
1489 "and endpoint is not disabled.\n");
1490 ret = -EINVAL;
1491 break;
1492 case COMP_SUCCESS:
1493 dev_dbg(&udev->dev, "Successful Endpoint Configure command\n");
1494 ret = 0;
1495 break;
1496 default:
1497 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001498 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001499 ret = -EINVAL;
1500 break;
1501 }
1502 return ret;
1503}
1504
1505static int xhci_evaluate_context_result(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001506 struct usb_device *udev, int *cmd_status)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001507{
1508 int ret;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001509 struct xhci_virt_device *virt_dev = xhci->devs[udev->slot_id];
Sarah Sharpf2217e82009-08-07 14:04:43 -07001510
Sarah Sharp913a8a32009-09-04 10:53:13 -07001511 switch (*cmd_status) {
Sarah Sharpf2217e82009-08-07 14:04:43 -07001512 case COMP_EINVAL:
1513 dev_warn(&udev->dev, "WARN: xHCI driver setup invalid evaluate "
1514 "context command.\n");
1515 ret = -EINVAL;
1516 break;
1517 case COMP_EBADSLT:
1518 dev_warn(&udev->dev, "WARN: slot not enabled for"
1519 "evaluate context command.\n");
1520 case COMP_CTX_STATE:
1521 dev_warn(&udev->dev, "WARN: invalid context state for "
1522 "evaluate context command.\n");
1523 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 1);
1524 ret = -EINVAL;
1525 break;
1526 case COMP_SUCCESS:
1527 dev_dbg(&udev->dev, "Successful evaluate context command\n");
1528 ret = 0;
1529 break;
1530 default:
1531 xhci_err(xhci, "ERROR: unexpected command completion "
Sarah Sharp913a8a32009-09-04 10:53:13 -07001532 "code 0x%x.\n", *cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001533 ret = -EINVAL;
1534 break;
1535 }
1536 return ret;
1537}
1538
1539/* Issue a configure endpoint command or evaluate context command
1540 * and wait for it to finish.
1541 */
1542static int xhci_configure_endpoint(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001543 struct usb_device *udev,
1544 struct xhci_command *command,
1545 bool ctx_change, bool must_succeed)
Sarah Sharpf2217e82009-08-07 14:04:43 -07001546{
1547 int ret;
1548 int timeleft;
1549 unsigned long flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001550 struct xhci_container_ctx *in_ctx;
1551 struct completion *cmd_completion;
1552 int *cmd_status;
1553 struct xhci_virt_device *virt_dev;
Sarah Sharpf2217e82009-08-07 14:04:43 -07001554
1555 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001556 virt_dev = xhci->devs[udev->slot_id];
1557 if (command) {
1558 in_ctx = command->in_ctx;
1559 cmd_completion = command->completion;
1560 cmd_status = &command->status;
1561 command->command_trb = xhci->cmd_ring->enqueue;
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08001562
1563 /* Enqueue pointer can be left pointing to the link TRB,
1564 * we must handle that
1565 */
1566 if ((command->command_trb->link.control & TRB_TYPE_BITMASK)
1567 == TRB_TYPE(TRB_LINK))
1568 command->command_trb =
1569 xhci->cmd_ring->enq_seg->next->trbs;
1570
Sarah Sharp913a8a32009-09-04 10:53:13 -07001571 list_add_tail(&command->cmd_list, &virt_dev->cmd_list);
1572 } else {
1573 in_ctx = virt_dev->in_ctx;
1574 cmd_completion = &virt_dev->cmd_completion;
1575 cmd_status = &virt_dev->cmd_status;
1576 }
Andiry Xu1d680642010-03-12 17:10:04 +08001577 init_completion(cmd_completion);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001578
Sarah Sharpf2217e82009-08-07 14:04:43 -07001579 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001580 ret = xhci_queue_configure_endpoint(xhci, in_ctx->dma,
1581 udev->slot_id, must_succeed);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001582 else
Sarah Sharp913a8a32009-09-04 10:53:13 -07001583 ret = xhci_queue_evaluate_context(xhci, in_ctx->dma,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001584 udev->slot_id);
1585 if (ret < 0) {
Sarah Sharpc01591b2009-12-09 15:58:58 -08001586 if (command)
1587 list_del(&command->cmd_list);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001588 spin_unlock_irqrestore(&xhci->lock, flags);
1589 xhci_dbg(xhci, "FIXME allocate a new ring segment\n");
1590 return -ENOMEM;
1591 }
1592 xhci_ring_cmd_db(xhci);
1593 spin_unlock_irqrestore(&xhci->lock, flags);
1594
1595 /* Wait for the configure endpoint command to complete */
1596 timeleft = wait_for_completion_interruptible_timeout(
Sarah Sharp913a8a32009-09-04 10:53:13 -07001597 cmd_completion,
Sarah Sharpf2217e82009-08-07 14:04:43 -07001598 USB_CTRL_SET_TIMEOUT);
1599 if (timeleft <= 0) {
1600 xhci_warn(xhci, "%s while waiting for %s command\n",
1601 timeleft == 0 ? "Timeout" : "Signal",
1602 ctx_change == 0 ?
1603 "configure endpoint" :
1604 "evaluate context");
1605 /* FIXME cancel the configure endpoint command */
1606 return -ETIME;
1607 }
1608
1609 if (!ctx_change)
Sarah Sharp913a8a32009-09-04 10:53:13 -07001610 return xhci_configure_endpoint_result(xhci, udev, cmd_status);
1611 return xhci_evaluate_context_result(xhci, udev, cmd_status);
Sarah Sharpf2217e82009-08-07 14:04:43 -07001612}
1613
Sarah Sharpf88ba782009-05-14 11:44:22 -07001614/* Called after one or more calls to xhci_add_endpoint() or
1615 * xhci_drop_endpoint(). If this call fails, the USB core is expected
1616 * to call xhci_reset_bandwidth().
1617 *
1618 * Since we are in the middle of changing either configuration or
1619 * installing a new alt setting, the USB core won't allow URBs to be
1620 * enqueued for any endpoint on the old config or interface. Nothing
1621 * else should be touching the xhci->devs[slot_id] structure, so we
1622 * don't need to take the xhci->lock for manipulating that.
1623 */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001624int xhci_check_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1625{
1626 int i;
1627 int ret = 0;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001628 struct xhci_hcd *xhci;
1629 struct xhci_virt_device *virt_dev;
John Yound115b042009-07-27 12:05:15 -07001630 struct xhci_input_control_ctx *ctrl_ctx;
1631 struct xhci_slot_ctx *slot_ctx;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001632
Andiry Xu64927732010-10-14 07:22:45 -07001633 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001634 if (ret <= 0)
1635 return ret;
1636 xhci = hcd_to_xhci(hcd);
1637
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001638 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001639 virt_dev = xhci->devs[udev->slot_id];
1640
1641 /* See section 4.6.6 - A0 = 1; A1 = D0 = D1 = 0 */
John Yound115b042009-07-27 12:05:15 -07001642 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
1643 ctrl_ctx->add_flags |= SLOT_FLAG;
1644 ctrl_ctx->add_flags &= ~EP0_FLAG;
1645 ctrl_ctx->drop_flags &= ~SLOT_FLAG;
1646 ctrl_ctx->drop_flags &= ~EP0_FLAG;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001647 xhci_dbg(xhci, "New Input Control Context:\n");
John Yound115b042009-07-27 12:05:15 -07001648 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
1649 xhci_dbg_ctx(xhci, virt_dev->in_ctx,
1650 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001651
Sarah Sharp913a8a32009-09-04 10:53:13 -07001652 ret = xhci_configure_endpoint(xhci, udev, NULL,
1653 false, false);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001654 if (ret) {
1655 /* Callee should call reset_bandwidth() */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001656 return ret;
1657 }
1658
1659 xhci_dbg(xhci, "Output context after successful config ep cmd:\n");
John Yound115b042009-07-27 12:05:15 -07001660 xhci_dbg_ctx(xhci, virt_dev->out_ctx,
1661 LAST_CTX_TO_EP_NUM(slot_ctx->dev_info));
Sarah Sharpf94e01862009-04-27 19:58:38 -07001662
John Yound115b042009-07-27 12:05:15 -07001663 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001664 /* Install new rings and free or cache any old rings */
Sarah Sharpf94e01862009-04-27 19:58:38 -07001665 for (i = 1; i < 31; ++i) {
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001666 if (!virt_dev->eps[i].new_ring)
1667 continue;
1668 /* Only cache or free the old ring if it exists.
1669 * It may not if this is the first add of an endpoint.
1670 */
1671 if (virt_dev->eps[i].ring) {
Sarah Sharp412566b2009-12-09 15:59:01 -08001672 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001673 }
Sarah Sharp74f9fe22009-12-03 09:44:29 -08001674 virt_dev->eps[i].ring = virt_dev->eps[i].new_ring;
1675 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001676 }
1677
Sarah Sharpf94e01862009-04-27 19:58:38 -07001678 return ret;
1679}
1680
1681void xhci_reset_bandwidth(struct usb_hcd *hcd, struct usb_device *udev)
1682{
Sarah Sharpf94e01862009-04-27 19:58:38 -07001683 struct xhci_hcd *xhci;
1684 struct xhci_virt_device *virt_dev;
1685 int i, ret;
1686
Andiry Xu64927732010-10-14 07:22:45 -07001687 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001688 if (ret <= 0)
1689 return;
1690 xhci = hcd_to_xhci(hcd);
1691
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07001692 xhci_dbg(xhci, "%s called for udev %p\n", __func__, udev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001693 virt_dev = xhci->devs[udev->slot_id];
1694 /* Free any rings allocated for added endpoints */
1695 for (i = 0; i < 31; ++i) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001696 if (virt_dev->eps[i].new_ring) {
1697 xhci_ring_free(xhci, virt_dev->eps[i].new_ring);
1698 virt_dev->eps[i].new_ring = NULL;
Sarah Sharpf94e01862009-04-27 19:58:38 -07001699 }
1700 }
John Yound115b042009-07-27 12:05:15 -07001701 xhci_zero_in_ctx(xhci, virt_dev);
Sarah Sharpf94e01862009-04-27 19:58:38 -07001702}
1703
Sarah Sharp5270b952009-09-04 10:53:11 -07001704static void xhci_setup_input_ctx_for_config_ep(struct xhci_hcd *xhci,
Sarah Sharp913a8a32009-09-04 10:53:13 -07001705 struct xhci_container_ctx *in_ctx,
1706 struct xhci_container_ctx *out_ctx,
1707 u32 add_flags, u32 drop_flags)
Sarah Sharp5270b952009-09-04 10:53:11 -07001708{
1709 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001710 ctrl_ctx = xhci_get_input_control_ctx(xhci, in_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001711 ctrl_ctx->add_flags = add_flags;
1712 ctrl_ctx->drop_flags = drop_flags;
Sarah Sharp913a8a32009-09-04 10:53:13 -07001713 xhci_slot_copy(xhci, in_ctx, out_ctx);
Sarah Sharp5270b952009-09-04 10:53:11 -07001714 ctrl_ctx->add_flags |= SLOT_FLAG;
1715
Sarah Sharp913a8a32009-09-04 10:53:13 -07001716 xhci_dbg(xhci, "Input Context:\n");
1717 xhci_dbg_ctx(xhci, in_ctx, xhci_last_valid_endpoint(add_flags));
Sarah Sharp5270b952009-09-04 10:53:11 -07001718}
1719
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001720void xhci_setup_input_ctx_for_quirk(struct xhci_hcd *xhci,
1721 unsigned int slot_id, unsigned int ep_index,
1722 struct xhci_dequeue_state *deq_state)
1723{
1724 struct xhci_container_ctx *in_ctx;
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001725 struct xhci_ep_ctx *ep_ctx;
1726 u32 added_ctxs;
1727 dma_addr_t addr;
1728
Sarah Sharp913a8a32009-09-04 10:53:13 -07001729 xhci_endpoint_copy(xhci, xhci->devs[slot_id]->in_ctx,
1730 xhci->devs[slot_id]->out_ctx, ep_index);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001731 in_ctx = xhci->devs[slot_id]->in_ctx;
1732 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1733 addr = xhci_trb_virt_to_dma(deq_state->new_deq_seg,
1734 deq_state->new_deq_ptr);
1735 if (addr == 0) {
1736 xhci_warn(xhci, "WARN Cannot submit config ep after "
1737 "reset ep command\n");
1738 xhci_warn(xhci, "WARN deq seg = %p, deq ptr = %p\n",
1739 deq_state->new_deq_seg,
1740 deq_state->new_deq_ptr);
1741 return;
1742 }
1743 ep_ctx->deq = addr | deq_state->new_cycle_state;
1744
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001745 added_ctxs = xhci_get_endpoint_flag_from_index(ep_index);
Sarah Sharp913a8a32009-09-04 10:53:13 -07001746 xhci_setup_input_ctx_for_config_ep(xhci, xhci->devs[slot_id]->in_ctx,
1747 xhci->devs[slot_id]->out_ctx, added_ctxs, added_ctxs);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001748}
1749
Sarah Sharp82d10092009-08-07 14:04:52 -07001750void xhci_cleanup_stalled_ring(struct xhci_hcd *xhci,
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001751 struct usb_device *udev, unsigned int ep_index)
Sarah Sharp82d10092009-08-07 14:04:52 -07001752{
1753 struct xhci_dequeue_state deq_state;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001754 struct xhci_virt_ep *ep;
Sarah Sharp82d10092009-08-07 14:04:52 -07001755
1756 xhci_dbg(xhci, "Cleaning up stalled endpoint ring\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001757 ep = &xhci->devs[udev->slot_id]->eps[ep_index];
Sarah Sharp82d10092009-08-07 14:04:52 -07001758 /* We need to move the HW's dequeue pointer past this TD,
1759 * or it will attempt to resend it on the next doorbell ring.
1760 */
1761 xhci_find_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001762 ep_index, ep->stopped_stream, ep->stopped_td,
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001763 &deq_state);
Sarah Sharp82d10092009-08-07 14:04:52 -07001764
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001765 /* HW with the reset endpoint quirk will use the saved dequeue state to
1766 * issue a configure endpoint command later.
1767 */
1768 if (!(xhci->quirks & XHCI_RESET_EP_QUIRK)) {
1769 xhci_dbg(xhci, "Queueing new dequeue state\n");
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001770 xhci_queue_new_dequeue_state(xhci, udev->slot_id,
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001771 ep_index, ep->stopped_stream, &deq_state);
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001772 } else {
1773 /* Better hope no one uses the input context between now and the
1774 * reset endpoint completion!
Sarah Sharpe9df17e2010-04-02 15:34:43 -07001775 * XXX: No idea how this hardware will react when stream rings
1776 * are enabled.
Sarah Sharpac9d8fe2009-08-07 14:04:55 -07001777 */
1778 xhci_dbg(xhci, "Setting up input context for "
1779 "configure endpoint command\n");
1780 xhci_setup_input_ctx_for_quirk(xhci, udev->slot_id,
1781 ep_index, &deq_state);
1782 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001783}
1784
Sarah Sharpa1587d92009-07-27 12:03:15 -07001785/* Deal with stalled endpoints. The core should have sent the control message
1786 * to clear the halt condition. However, we need to make the xHCI hardware
1787 * reset its sequence number, since a device will expect a sequence number of
1788 * zero after the halt condition is cleared.
1789 * Context: in_interrupt
1790 */
1791void xhci_endpoint_reset(struct usb_hcd *hcd,
1792 struct usb_host_endpoint *ep)
1793{
1794 struct xhci_hcd *xhci;
1795 struct usb_device *udev;
1796 unsigned int ep_index;
1797 unsigned long flags;
1798 int ret;
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001799 struct xhci_virt_ep *virt_ep;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001800
1801 xhci = hcd_to_xhci(hcd);
1802 udev = (struct usb_device *) ep->hcpriv;
1803 /* Called with a root hub endpoint (or an endpoint that wasn't added
1804 * with xhci_add_endpoint()
1805 */
1806 if (!ep->hcpriv)
1807 return;
1808 ep_index = xhci_get_endpoint_index(&ep->desc);
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001809 virt_ep = &xhci->devs[udev->slot_id]->eps[ep_index];
1810 if (!virt_ep->stopped_td) {
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001811 xhci_dbg(xhci, "Endpoint 0x%x not halted, refusing to reset.\n",
1812 ep->desc.bEndpointAddress);
1813 return;
1814 }
Sarah Sharp82d10092009-08-07 14:04:52 -07001815 if (usb_endpoint_xfer_control(&ep->desc)) {
1816 xhci_dbg(xhci, "Control endpoint stall already handled.\n");
1817 return;
1818 }
Sarah Sharpa1587d92009-07-27 12:03:15 -07001819
1820 xhci_dbg(xhci, "Queueing reset endpoint command\n");
1821 spin_lock_irqsave(&xhci->lock, flags);
1822 ret = xhci_queue_reset_ep(xhci, udev->slot_id, ep_index);
Sarah Sharpc92bcfa2009-07-27 12:05:21 -07001823 /*
1824 * Can't change the ring dequeue pointer until it's transitioned to the
1825 * stopped state, which is only upon a successful reset endpoint
1826 * command. Better hope that last command worked!
1827 */
Sarah Sharpa1587d92009-07-27 12:03:15 -07001828 if (!ret) {
Sarah Sharp63a0d9a2009-09-04 10:53:09 -07001829 xhci_cleanup_stalled_ring(xhci, udev, ep_index);
1830 kfree(virt_ep->stopped_td);
Sarah Sharpa1587d92009-07-27 12:03:15 -07001831 xhci_ring_cmd_db(xhci);
1832 }
Sarah Sharp1624ae12010-05-06 13:40:08 -07001833 virt_ep->stopped_td = NULL;
1834 virt_ep->stopped_trb = NULL;
Sarah Sharp5e5cf6f2010-05-06 13:40:18 -07001835 virt_ep->stopped_stream = 0;
Sarah Sharpa1587d92009-07-27 12:03:15 -07001836 spin_unlock_irqrestore(&xhci->lock, flags);
1837
1838 if (ret)
1839 xhci_warn(xhci, "FIXME allocate a new ring segment\n");
1840}
1841
Sarah Sharp8df75f42010-04-02 15:34:16 -07001842static int xhci_check_streams_endpoint(struct xhci_hcd *xhci,
1843 struct usb_device *udev, struct usb_host_endpoint *ep,
1844 unsigned int slot_id)
1845{
1846 int ret;
1847 unsigned int ep_index;
1848 unsigned int ep_state;
1849
1850 if (!ep)
1851 return -EINVAL;
Andiry Xu64927732010-10-14 07:22:45 -07001852 ret = xhci_check_args(xhci_to_hcd(xhci), udev, ep, 1, true, __func__);
Sarah Sharp8df75f42010-04-02 15:34:16 -07001853 if (ret <= 0)
1854 return -EINVAL;
Alan Stern842f1692010-04-30 12:44:46 -04001855 if (ep->ss_ep_comp.bmAttributes == 0) {
Sarah Sharp8df75f42010-04-02 15:34:16 -07001856 xhci_warn(xhci, "WARN: SuperSpeed Endpoint Companion"
1857 " descriptor for ep 0x%x does not support streams\n",
1858 ep->desc.bEndpointAddress);
1859 return -EINVAL;
1860 }
1861
1862 ep_index = xhci_get_endpoint_index(&ep->desc);
1863 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1864 if (ep_state & EP_HAS_STREAMS ||
1865 ep_state & EP_GETTING_STREAMS) {
1866 xhci_warn(xhci, "WARN: SuperSpeed bulk endpoint 0x%x "
1867 "already has streams set up.\n",
1868 ep->desc.bEndpointAddress);
1869 xhci_warn(xhci, "Send email to xHCI maintainer and ask for "
1870 "dynamic stream context array reallocation.\n");
1871 return -EINVAL;
1872 }
1873 if (!list_empty(&xhci->devs[slot_id]->eps[ep_index].ring->td_list)) {
1874 xhci_warn(xhci, "Cannot setup streams for SuperSpeed bulk "
1875 "endpoint 0x%x; URBs are pending.\n",
1876 ep->desc.bEndpointAddress);
1877 return -EINVAL;
1878 }
1879 return 0;
1880}
1881
1882static void xhci_calculate_streams_entries(struct xhci_hcd *xhci,
1883 unsigned int *num_streams, unsigned int *num_stream_ctxs)
1884{
1885 unsigned int max_streams;
1886
1887 /* The stream context array size must be a power of two */
1888 *num_stream_ctxs = roundup_pow_of_two(*num_streams);
1889 /*
1890 * Find out how many primary stream array entries the host controller
1891 * supports. Later we may use secondary stream arrays (similar to 2nd
1892 * level page entries), but that's an optional feature for xHCI host
1893 * controllers. xHCs must support at least 4 stream IDs.
1894 */
1895 max_streams = HCC_MAX_PSA(xhci->hcc_params);
1896 if (*num_stream_ctxs > max_streams) {
1897 xhci_dbg(xhci, "xHCI HW only supports %u stream ctx entries.\n",
1898 max_streams);
1899 *num_stream_ctxs = max_streams;
1900 *num_streams = max_streams;
1901 }
1902}
1903
1904/* Returns an error code if one of the endpoint already has streams.
1905 * This does not change any data structures, it only checks and gathers
1906 * information.
1907 */
1908static int xhci_calculate_streams_and_bitmask(struct xhci_hcd *xhci,
1909 struct usb_device *udev,
1910 struct usb_host_endpoint **eps, unsigned int num_eps,
1911 unsigned int *num_streams, u32 *changed_ep_bitmask)
1912{
Sarah Sharp8df75f42010-04-02 15:34:16 -07001913 unsigned int max_streams;
1914 unsigned int endpoint_flag;
1915 int i;
1916 int ret;
1917
1918 for (i = 0; i < num_eps; i++) {
1919 ret = xhci_check_streams_endpoint(xhci, udev,
1920 eps[i], udev->slot_id);
1921 if (ret < 0)
1922 return ret;
1923
Alan Stern842f1692010-04-30 12:44:46 -04001924 max_streams = USB_SS_MAX_STREAMS(
1925 eps[i]->ss_ep_comp.bmAttributes);
Sarah Sharp8df75f42010-04-02 15:34:16 -07001926 if (max_streams < (*num_streams - 1)) {
1927 xhci_dbg(xhci, "Ep 0x%x only supports %u stream IDs.\n",
1928 eps[i]->desc.bEndpointAddress,
1929 max_streams);
1930 *num_streams = max_streams+1;
1931 }
1932
1933 endpoint_flag = xhci_get_endpoint_flag(&eps[i]->desc);
1934 if (*changed_ep_bitmask & endpoint_flag)
1935 return -EINVAL;
1936 *changed_ep_bitmask |= endpoint_flag;
1937 }
1938 return 0;
1939}
1940
1941static u32 xhci_calculate_no_streams_bitmask(struct xhci_hcd *xhci,
1942 struct usb_device *udev,
1943 struct usb_host_endpoint **eps, unsigned int num_eps)
1944{
1945 u32 changed_ep_bitmask = 0;
1946 unsigned int slot_id;
1947 unsigned int ep_index;
1948 unsigned int ep_state;
1949 int i;
1950
1951 slot_id = udev->slot_id;
1952 if (!xhci->devs[slot_id])
1953 return 0;
1954
1955 for (i = 0; i < num_eps; i++) {
1956 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
1957 ep_state = xhci->devs[slot_id]->eps[ep_index].ep_state;
1958 /* Are streams already being freed for the endpoint? */
1959 if (ep_state & EP_GETTING_NO_STREAMS) {
1960 xhci_warn(xhci, "WARN Can't disable streams for "
1961 "endpoint 0x%x\n, "
1962 "streams are being disabled already.",
1963 eps[i]->desc.bEndpointAddress);
1964 return 0;
1965 }
1966 /* Are there actually any streams to free? */
1967 if (!(ep_state & EP_HAS_STREAMS) &&
1968 !(ep_state & EP_GETTING_STREAMS)) {
1969 xhci_warn(xhci, "WARN Can't disable streams for "
1970 "endpoint 0x%x\n, "
1971 "streams are already disabled!",
1972 eps[i]->desc.bEndpointAddress);
1973 xhci_warn(xhci, "WARN xhci_free_streams() called "
1974 "with non-streams endpoint\n");
1975 return 0;
1976 }
1977 changed_ep_bitmask |= xhci_get_endpoint_flag(&eps[i]->desc);
1978 }
1979 return changed_ep_bitmask;
1980}
1981
1982/*
1983 * The USB device drivers use this function (though the HCD interface in USB
1984 * core) to prepare a set of bulk endpoints to use streams. Streams are used to
1985 * coordinate mass storage command queueing across multiple endpoints (basically
1986 * a stream ID == a task ID).
1987 *
1988 * Setting up streams involves allocating the same size stream context array
1989 * for each endpoint and issuing a configure endpoint command for all endpoints.
1990 *
1991 * Don't allow the call to succeed if one endpoint only supports one stream
1992 * (which means it doesn't support streams at all).
1993 *
1994 * Drivers may get less stream IDs than they asked for, if the host controller
1995 * hardware or endpoints claim they can't support the number of requested
1996 * stream IDs.
1997 */
1998int xhci_alloc_streams(struct usb_hcd *hcd, struct usb_device *udev,
1999 struct usb_host_endpoint **eps, unsigned int num_eps,
2000 unsigned int num_streams, gfp_t mem_flags)
2001{
2002 int i, ret;
2003 struct xhci_hcd *xhci;
2004 struct xhci_virt_device *vdev;
2005 struct xhci_command *config_cmd;
2006 unsigned int ep_index;
2007 unsigned int num_stream_ctxs;
2008 unsigned long flags;
2009 u32 changed_ep_bitmask = 0;
2010
2011 if (!eps)
2012 return -EINVAL;
2013
2014 /* Add one to the number of streams requested to account for
2015 * stream 0 that is reserved for xHCI usage.
2016 */
2017 num_streams += 1;
2018 xhci = hcd_to_xhci(hcd);
2019 xhci_dbg(xhci, "Driver wants %u stream IDs (including stream 0).\n",
2020 num_streams);
2021
2022 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
2023 if (!config_cmd) {
2024 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
2025 return -ENOMEM;
2026 }
2027
2028 /* Check to make sure all endpoints are not already configured for
2029 * streams. While we're at it, find the maximum number of streams that
2030 * all the endpoints will support and check for duplicate endpoints.
2031 */
2032 spin_lock_irqsave(&xhci->lock, flags);
2033 ret = xhci_calculate_streams_and_bitmask(xhci, udev, eps,
2034 num_eps, &num_streams, &changed_ep_bitmask);
2035 if (ret < 0) {
2036 xhci_free_command(xhci, config_cmd);
2037 spin_unlock_irqrestore(&xhci->lock, flags);
2038 return ret;
2039 }
2040 if (num_streams <= 1) {
2041 xhci_warn(xhci, "WARN: endpoints can't handle "
2042 "more than one stream.\n");
2043 xhci_free_command(xhci, config_cmd);
2044 spin_unlock_irqrestore(&xhci->lock, flags);
2045 return -EINVAL;
2046 }
2047 vdev = xhci->devs[udev->slot_id];
2048 /* Mark each endpoint as being in transistion, so
2049 * xhci_urb_enqueue() will reject all URBs.
2050 */
2051 for (i = 0; i < num_eps; i++) {
2052 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2053 vdev->eps[ep_index].ep_state |= EP_GETTING_STREAMS;
2054 }
2055 spin_unlock_irqrestore(&xhci->lock, flags);
2056
2057 /* Setup internal data structures and allocate HW data structures for
2058 * streams (but don't install the HW structures in the input context
2059 * until we're sure all memory allocation succeeded).
2060 */
2061 xhci_calculate_streams_entries(xhci, &num_streams, &num_stream_ctxs);
2062 xhci_dbg(xhci, "Need %u stream ctx entries for %u stream IDs.\n",
2063 num_stream_ctxs, num_streams);
2064
2065 for (i = 0; i < num_eps; i++) {
2066 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2067 vdev->eps[ep_index].stream_info = xhci_alloc_stream_info(xhci,
2068 num_stream_ctxs,
2069 num_streams, mem_flags);
2070 if (!vdev->eps[ep_index].stream_info)
2071 goto cleanup;
2072 /* Set maxPstreams in endpoint context and update deq ptr to
2073 * point to stream context array. FIXME
2074 */
2075 }
2076
2077 /* Set up the input context for a configure endpoint command. */
2078 for (i = 0; i < num_eps; i++) {
2079 struct xhci_ep_ctx *ep_ctx;
2080
2081 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2082 ep_ctx = xhci_get_ep_ctx(xhci, config_cmd->in_ctx, ep_index);
2083
2084 xhci_endpoint_copy(xhci, config_cmd->in_ctx,
2085 vdev->out_ctx, ep_index);
2086 xhci_setup_streams_ep_input_ctx(xhci, ep_ctx,
2087 vdev->eps[ep_index].stream_info);
2088 }
2089 /* Tell the HW to drop its old copy of the endpoint context info
2090 * and add the updated copy from the input context.
2091 */
2092 xhci_setup_input_ctx_for_config_ep(xhci, config_cmd->in_ctx,
2093 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
2094
2095 /* Issue and wait for the configure endpoint command */
2096 ret = xhci_configure_endpoint(xhci, udev, config_cmd,
2097 false, false);
2098
2099 /* xHC rejected the configure endpoint command for some reason, so we
2100 * leave the old ring intact and free our internal streams data
2101 * structure.
2102 */
2103 if (ret < 0)
2104 goto cleanup;
2105
2106 spin_lock_irqsave(&xhci->lock, flags);
2107 for (i = 0; i < num_eps; i++) {
2108 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2109 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
2110 xhci_dbg(xhci, "Slot %u ep ctx %u now has streams.\n",
2111 udev->slot_id, ep_index);
2112 vdev->eps[ep_index].ep_state |= EP_HAS_STREAMS;
2113 }
2114 xhci_free_command(xhci, config_cmd);
2115 spin_unlock_irqrestore(&xhci->lock, flags);
2116
2117 /* Subtract 1 for stream 0, which drivers can't use */
2118 return num_streams - 1;
2119
2120cleanup:
2121 /* If it didn't work, free the streams! */
2122 for (i = 0; i < num_eps; i++) {
2123 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2124 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07002125 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07002126 /* FIXME Unset maxPstreams in endpoint context and
2127 * update deq ptr to point to normal string ring.
2128 */
2129 vdev->eps[ep_index].ep_state &= ~EP_GETTING_STREAMS;
2130 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
2131 xhci_endpoint_zero(xhci, vdev, eps[i]);
2132 }
2133 xhci_free_command(xhci, config_cmd);
2134 return -ENOMEM;
2135}
2136
2137/* Transition the endpoint from using streams to being a "normal" endpoint
2138 * without streams.
2139 *
2140 * Modify the endpoint context state, submit a configure endpoint command,
2141 * and free all endpoint rings for streams if that completes successfully.
2142 */
2143int xhci_free_streams(struct usb_hcd *hcd, struct usb_device *udev,
2144 struct usb_host_endpoint **eps, unsigned int num_eps,
2145 gfp_t mem_flags)
2146{
2147 int i, ret;
2148 struct xhci_hcd *xhci;
2149 struct xhci_virt_device *vdev;
2150 struct xhci_command *command;
2151 unsigned int ep_index;
2152 unsigned long flags;
2153 u32 changed_ep_bitmask;
2154
2155 xhci = hcd_to_xhci(hcd);
2156 vdev = xhci->devs[udev->slot_id];
2157
2158 /* Set up a configure endpoint command to remove the streams rings */
2159 spin_lock_irqsave(&xhci->lock, flags);
2160 changed_ep_bitmask = xhci_calculate_no_streams_bitmask(xhci,
2161 udev, eps, num_eps);
2162 if (changed_ep_bitmask == 0) {
2163 spin_unlock_irqrestore(&xhci->lock, flags);
2164 return -EINVAL;
2165 }
2166
2167 /* Use the xhci_command structure from the first endpoint. We may have
2168 * allocated too many, but the driver may call xhci_free_streams() for
2169 * each endpoint it grouped into one call to xhci_alloc_streams().
2170 */
2171 ep_index = xhci_get_endpoint_index(&eps[0]->desc);
2172 command = vdev->eps[ep_index].stream_info->free_streams_command;
2173 for (i = 0; i < num_eps; i++) {
2174 struct xhci_ep_ctx *ep_ctx;
2175
2176 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2177 ep_ctx = xhci_get_ep_ctx(xhci, command->in_ctx, ep_index);
2178 xhci->devs[udev->slot_id]->eps[ep_index].ep_state |=
2179 EP_GETTING_NO_STREAMS;
2180
2181 xhci_endpoint_copy(xhci, command->in_ctx,
2182 vdev->out_ctx, ep_index);
2183 xhci_setup_no_streams_ep_input_ctx(xhci, ep_ctx,
2184 &vdev->eps[ep_index]);
2185 }
2186 xhci_setup_input_ctx_for_config_ep(xhci, command->in_ctx,
2187 vdev->out_ctx, changed_ep_bitmask, changed_ep_bitmask);
2188 spin_unlock_irqrestore(&xhci->lock, flags);
2189
2190 /* Issue and wait for the configure endpoint command,
2191 * which must succeed.
2192 */
2193 ret = xhci_configure_endpoint(xhci, udev, command,
2194 false, true);
2195
2196 /* xHC rejected the configure endpoint command for some reason, so we
2197 * leave the streams rings intact.
2198 */
2199 if (ret < 0)
2200 return ret;
2201
2202 spin_lock_irqsave(&xhci->lock, flags);
2203 for (i = 0; i < num_eps; i++) {
2204 ep_index = xhci_get_endpoint_index(&eps[i]->desc);
2205 xhci_free_stream_info(xhci, vdev->eps[ep_index].stream_info);
Sarah Sharp8a007742010-04-30 15:37:56 -07002206 vdev->eps[ep_index].stream_info = NULL;
Sarah Sharp8df75f42010-04-02 15:34:16 -07002207 /* FIXME Unset maxPstreams in endpoint context and
2208 * update deq ptr to point to normal string ring.
2209 */
2210 vdev->eps[ep_index].ep_state &= ~EP_GETTING_NO_STREAMS;
2211 vdev->eps[ep_index].ep_state &= ~EP_HAS_STREAMS;
2212 }
2213 spin_unlock_irqrestore(&xhci->lock, flags);
2214
2215 return 0;
2216}
2217
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002218/*
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002219 * This submits a Reset Device Command, which will set the device state to 0,
2220 * set the device address to 0, and disable all the endpoints except the default
2221 * control endpoint. The USB core should come back and call
2222 * xhci_address_device(), and then re-set up the configuration. If this is
2223 * called because of a usb_reset_and_verify_device(), then the old alternate
2224 * settings will be re-installed through the normal bandwidth allocation
2225 * functions.
2226 *
2227 * Wait for the Reset Device command to finish. Remove all structures
2228 * associated with the endpoints that were disabled. Clear the input device
2229 * structure? Cache the rings? Reset the control endpoint 0 max packet size?
Andiry Xuf0615c42010-10-14 07:22:48 -07002230 *
2231 * If the virt_dev to be reset does not exist or does not match the udev,
2232 * it means the device is lost, possibly due to the xHC restore error and
2233 * re-initialization during S3/S4. In this case, call xhci_alloc_dev() to
2234 * re-allocate the device.
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002235 */
Andiry Xuf0615c42010-10-14 07:22:48 -07002236int xhci_discover_or_reset_device(struct usb_hcd *hcd, struct usb_device *udev)
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002237{
2238 int ret, i;
2239 unsigned long flags;
2240 struct xhci_hcd *xhci;
2241 unsigned int slot_id;
2242 struct xhci_virt_device *virt_dev;
2243 struct xhci_command *reset_device_cmd;
2244 int timeleft;
2245 int last_freed_endpoint;
2246
Andiry Xuf0615c42010-10-14 07:22:48 -07002247 ret = xhci_check_args(hcd, udev, NULL, 0, false, __func__);
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002248 if (ret <= 0)
2249 return ret;
2250 xhci = hcd_to_xhci(hcd);
2251 slot_id = udev->slot_id;
2252 virt_dev = xhci->devs[slot_id];
Andiry Xuf0615c42010-10-14 07:22:48 -07002253 if (!virt_dev) {
2254 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
2255 "not exist. Re-allocate the device\n", slot_id);
2256 ret = xhci_alloc_dev(hcd, udev);
2257 if (ret == 1)
2258 return 0;
2259 else
2260 return -EINVAL;
2261 }
2262
2263 if (virt_dev->udev != udev) {
2264 /* If the virt_dev and the udev does not match, this virt_dev
2265 * may belong to another udev.
2266 * Re-allocate the device.
2267 */
2268 xhci_dbg(xhci, "The device to be reset with slot ID %u does "
2269 "not match the udev. Re-allocate the device\n",
2270 slot_id);
2271 ret = xhci_alloc_dev(hcd, udev);
2272 if (ret == 1)
2273 return 0;
2274 else
2275 return -EINVAL;
2276 }
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002277
2278 xhci_dbg(xhci, "Resetting device with slot ID %u\n", slot_id);
2279 /* Allocate the command structure that holds the struct completion.
2280 * Assume we're in process context, since the normal device reset
2281 * process has to wait for the device anyway. Storage devices are
2282 * reset as part of error handling, so use GFP_NOIO instead of
2283 * GFP_KERNEL.
2284 */
2285 reset_device_cmd = xhci_alloc_command(xhci, false, true, GFP_NOIO);
2286 if (!reset_device_cmd) {
2287 xhci_dbg(xhci, "Couldn't allocate command structure.\n");
2288 return -ENOMEM;
2289 }
2290
2291 /* Attempt to submit the Reset Device command to the command ring */
2292 spin_lock_irqsave(&xhci->lock, flags);
2293 reset_device_cmd->command_trb = xhci->cmd_ring->enqueue;
Paul Zimmerman7a3783e2010-11-17 16:26:50 -08002294
2295 /* Enqueue pointer can be left pointing to the link TRB,
2296 * we must handle that
2297 */
2298 if ((reset_device_cmd->command_trb->link.control & TRB_TYPE_BITMASK)
2299 == TRB_TYPE(TRB_LINK))
2300 reset_device_cmd->command_trb =
2301 xhci->cmd_ring->enq_seg->next->trbs;
2302
Sarah Sharp2a8f82c2009-12-09 15:59:13 -08002303 list_add_tail(&reset_device_cmd->cmd_list, &virt_dev->cmd_list);
2304 ret = xhci_queue_reset_device(xhci, slot_id);
2305 if (ret) {
2306 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2307 list_del(&reset_device_cmd->cmd_list);
2308 spin_unlock_irqrestore(&xhci->lock, flags);
2309 goto command_cleanup;
2310 }
2311 xhci_ring_cmd_db(xhci);
2312 spin_unlock_irqrestore(&xhci->lock, flags);
2313
2314 /* Wait for the Reset Device command to finish */
2315 timeleft = wait_for_completion_interruptible_timeout(
2316 reset_device_cmd->completion,
2317 USB_CTRL_SET_TIMEOUT);
2318 if (timeleft <= 0) {
2319 xhci_warn(xhci, "%s while waiting for reset device command\n",
2320 timeleft == 0 ? "Timeout" : "Signal");
2321 spin_lock_irqsave(&xhci->lock, flags);
2322 /* The timeout might have raced with the event ring handler, so
2323 * only delete from the list if the item isn't poisoned.
2324 */
2325 if (reset_device_cmd->cmd_list.next != LIST_POISON1)
2326 list_del(&reset_device_cmd->cmd_list);
2327 spin_unlock_irqrestore(&xhci->lock, flags);
2328 ret = -ETIME;
2329 goto command_cleanup;
2330 }
2331
2332 /* The Reset Device command can't fail, according to the 0.95/0.96 spec,
2333 * unless we tried to reset a slot ID that wasn't enabled,
2334 * or the device wasn't in the addressed or configured state.
2335 */
2336 ret = reset_device_cmd->status;
2337 switch (ret) {
2338 case COMP_EBADSLT: /* 0.95 completion code for bad slot ID */
2339 case COMP_CTX_STATE: /* 0.96 completion code for same thing */
2340 xhci_info(xhci, "Can't reset device (slot ID %u) in %s state\n",
2341 slot_id,
2342 xhci_get_slot_state(xhci, virt_dev->out_ctx));
2343 xhci_info(xhci, "Not freeing device rings.\n");
2344 /* Don't treat this as an error. May change my mind later. */
2345 ret = 0;
2346 goto command_cleanup;
2347 case COMP_SUCCESS:
2348 xhci_dbg(xhci, "Successful reset device command.\n");
2349 break;
2350 default:
2351 if (xhci_is_vendor_info_code(xhci, ret))
2352 break;
2353 xhci_warn(xhci, "Unknown completion code %u for "
2354 "reset device command.\n", ret);
2355 ret = -EINVAL;
2356 goto command_cleanup;
2357 }
2358
2359 /* Everything but endpoint 0 is disabled, so free or cache the rings. */
2360 last_freed_endpoint = 1;
2361 for (i = 1; i < 31; ++i) {
2362 if (!virt_dev->eps[i].ring)
2363 continue;
2364 xhci_free_or_cache_endpoint_ring(xhci, virt_dev, i);
2365 last_freed_endpoint = i;
2366 }
2367 xhci_dbg(xhci, "Output context after successful reset device cmd:\n");
2368 xhci_dbg_ctx(xhci, virt_dev->out_ctx, last_freed_endpoint);
2369 ret = 0;
2370
2371command_cleanup:
2372 xhci_free_command(xhci, reset_device_cmd);
2373 return ret;
2374}
2375
2376/*
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002377 * At this point, the struct usb_device is about to go away, the device has
2378 * disconnected, and all traffic has been stopped and the endpoints have been
2379 * disabled. Free any HC data structures associated with that device.
2380 */
2381void xhci_free_dev(struct usb_hcd *hcd, struct usb_device *udev)
2382{
2383 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002384 struct xhci_virt_device *virt_dev;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002385 unsigned long flags;
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002386 u32 state;
Andiry Xu64927732010-10-14 07:22:45 -07002387 int i, ret;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002388
Andiry Xu64927732010-10-14 07:22:45 -07002389 ret = xhci_check_args(hcd, udev, NULL, 0, true, __func__);
2390 if (ret <= 0)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002391 return;
Andiry Xu64927732010-10-14 07:22:45 -07002392
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002393 virt_dev = xhci->devs[udev->slot_id];
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002394
2395 /* Stop any wayward timer functions (which may grab the lock) */
2396 for (i = 0; i < 31; ++i) {
2397 virt_dev->eps[i].ep_state &= ~EP_HALT_PENDING;
2398 del_timer_sync(&virt_dev->eps[i].stop_cmd_timer);
2399 }
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002400
2401 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002402 /* Don't disable the slot if the host controller is dead. */
2403 state = xhci_readl(xhci, &xhci->op_regs->status);
Sarah Sharp6f5165c2009-10-27 10:57:01 -07002404 if (state == 0xffffffff || (xhci->xhc_state & XHCI_STATE_DYING)) {
Sarah Sharpc526d0d2009-09-16 16:42:39 -07002405 xhci_free_virt_device(xhci, udev->slot_id);
2406 spin_unlock_irqrestore(&xhci->lock, flags);
2407 return;
2408 }
2409
Sarah Sharp23e3be12009-04-29 19:05:20 -07002410 if (xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002411 spin_unlock_irqrestore(&xhci->lock, flags);
2412 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2413 return;
2414 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002415 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002416 spin_unlock_irqrestore(&xhci->lock, flags);
2417 /*
2418 * Event command completion handler will free any data structures
Sarah Sharpf88ba782009-05-14 11:44:22 -07002419 * associated with the slot. XXX Can free sleep?
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002420 */
2421}
2422
2423/*
2424 * Returns 0 if the xHC ran out of device slots, the Enable Slot command
2425 * timed out, or allocating memory failed. Returns 1 on success.
2426 */
2427int xhci_alloc_dev(struct usb_hcd *hcd, struct usb_device *udev)
2428{
2429 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2430 unsigned long flags;
2431 int timeleft;
2432 int ret;
2433
2434 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07002435 ret = xhci_queue_slot_control(xhci, TRB_ENABLE_SLOT, 0);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002436 if (ret) {
2437 spin_unlock_irqrestore(&xhci->lock, flags);
2438 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2439 return 0;
2440 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002441 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002442 spin_unlock_irqrestore(&xhci->lock, flags);
2443
2444 /* XXX: how much time for xHC slot assignment? */
2445 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2446 USB_CTRL_SET_TIMEOUT);
2447 if (timeleft <= 0) {
2448 xhci_warn(xhci, "%s while waiting for a slot\n",
2449 timeleft == 0 ? "Timeout" : "Signal");
2450 /* FIXME cancel the enable slot request */
2451 return 0;
2452 }
2453
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002454 if (!xhci->slot_id) {
2455 xhci_err(xhci, "Error while assigning device slot ID\n");
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002456 return 0;
2457 }
Sarah Sharpa6d940d2010-12-28 13:08:42 -08002458 /* xhci_alloc_virt_device() does not touch rings; no need to lock.
2459 * Use GFP_NOIO, since this function can be called from
2460 * xhci_discover_or_reset_device(), which may be called as part of
2461 * mass storage driver error handling.
2462 */
2463 if (!xhci_alloc_virt_device(xhci, xhci->slot_id, udev, GFP_NOIO)) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002464 /* Disable slot, if we can do it without mem alloc */
2465 xhci_warn(xhci, "Could not allocate xHCI USB device data structures\n");
Sarah Sharpf88ba782009-05-14 11:44:22 -07002466 spin_lock_irqsave(&xhci->lock, flags);
Sarah Sharp23e3be12009-04-29 19:05:20 -07002467 if (!xhci_queue_slot_control(xhci, TRB_DISABLE_SLOT, udev->slot_id))
2468 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002469 spin_unlock_irqrestore(&xhci->lock, flags);
2470 return 0;
2471 }
2472 udev->slot_id = xhci->slot_id;
2473 /* Is this a LS or FS device under a HS hub? */
2474 /* Hub or peripherial? */
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002475 return 1;
2476}
2477
2478/*
2479 * Issue an Address Device command (which will issue a SetAddress request to
2480 * the device).
2481 * We should be protected by the usb_address0_mutex in khubd's hub_port_init, so
2482 * we should only issue and wait on one address command at the same time.
2483 *
2484 * We add one to the device address issued by the hardware because the USB core
2485 * uses address 1 for the root hubs (even though they're not really devices).
2486 */
2487int xhci_address_device(struct usb_hcd *hcd, struct usb_device *udev)
2488{
2489 unsigned long flags;
2490 int timeleft;
2491 struct xhci_virt_device *virt_dev;
2492 int ret = 0;
2493 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
John Yound115b042009-07-27 12:05:15 -07002494 struct xhci_slot_ctx *slot_ctx;
2495 struct xhci_input_control_ctx *ctrl_ctx;
Sarah Sharp8e595a52009-07-27 12:03:31 -07002496 u64 temp_64;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002497
2498 if (!udev->slot_id) {
2499 xhci_dbg(xhci, "Bad Slot ID %d\n", udev->slot_id);
2500 return -EINVAL;
2501 }
2502
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002503 virt_dev = xhci->devs[udev->slot_id];
2504
Andiry Xuf0615c42010-10-14 07:22:48 -07002505 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->in_ctx);
2506 /*
2507 * If this is the first Set Address since device plug-in or
2508 * virt_device realloaction after a resume with an xHCI power loss,
2509 * then set up the slot context.
2510 */
2511 if (!slot_ctx->dev_info)
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002512 xhci_setup_addressable_virt_dev(xhci, udev);
Andiry Xuf0615c42010-10-14 07:22:48 -07002513 /* Otherwise, update the control endpoint ring enqueue pointer. */
Sarah Sharp2d1ee592010-07-09 17:08:54 +02002514 else
2515 xhci_copy_ep0_dequeue_into_input_ctx(xhci, udev);
Sarah Sharp66e49d82009-07-27 12:03:46 -07002516 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002517 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002518
Sarah Sharpf88ba782009-05-14 11:44:22 -07002519 spin_lock_irqsave(&xhci->lock, flags);
John Yound115b042009-07-27 12:05:15 -07002520 ret = xhci_queue_address_device(xhci, virt_dev->in_ctx->dma,
2521 udev->slot_id);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002522 if (ret) {
2523 spin_unlock_irqrestore(&xhci->lock, flags);
2524 xhci_dbg(xhci, "FIXME: allocate a command ring segment\n");
2525 return ret;
2526 }
Sarah Sharp23e3be12009-04-29 19:05:20 -07002527 xhci_ring_cmd_db(xhci);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002528 spin_unlock_irqrestore(&xhci->lock, flags);
2529
2530 /* ctrl tx can take up to 5 sec; XXX: need more time for xHC? */
2531 timeleft = wait_for_completion_interruptible_timeout(&xhci->addr_dev,
2532 USB_CTRL_SET_TIMEOUT);
2533 /* FIXME: From section 4.3.4: "Software shall be responsible for timing
2534 * the SetAddress() "recovery interval" required by USB and aborting the
2535 * command on a timeout.
2536 */
2537 if (timeleft <= 0) {
2538 xhci_warn(xhci, "%s while waiting for a slot\n",
2539 timeleft == 0 ? "Timeout" : "Signal");
2540 /* FIXME cancel the address device command */
2541 return -ETIME;
2542 }
2543
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002544 switch (virt_dev->cmd_status) {
2545 case COMP_CTX_STATE:
2546 case COMP_EBADSLT:
2547 xhci_err(xhci, "Setup ERROR: address device command for slot %d.\n",
2548 udev->slot_id);
2549 ret = -EINVAL;
2550 break;
2551 case COMP_TX_ERR:
2552 dev_warn(&udev->dev, "Device not responding to set address.\n");
2553 ret = -EPROTO;
2554 break;
2555 case COMP_SUCCESS:
2556 xhci_dbg(xhci, "Successful Address Device command\n");
2557 break;
2558 default:
2559 xhci_err(xhci, "ERROR: unexpected command completion "
2560 "code 0x%x.\n", virt_dev->cmd_status);
Sarah Sharp66e49d82009-07-27 12:03:46 -07002561 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002562 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002563 ret = -EINVAL;
2564 break;
2565 }
2566 if (ret) {
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002567 return ret;
2568 }
Sarah Sharp8e595a52009-07-27 12:03:31 -07002569 temp_64 = xhci_read_64(xhci, &xhci->op_regs->dcbaa_ptr);
2570 xhci_dbg(xhci, "Op regs DCBAA ptr = %#016llx\n", temp_64);
2571 xhci_dbg(xhci, "Slot ID %d dcbaa entry @%p = %#016llx\n",
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002572 udev->slot_id,
Sarah Sharp8e595a52009-07-27 12:03:31 -07002573 &xhci->dcbaa->dev_context_ptrs[udev->slot_id],
2574 (unsigned long long)
2575 xhci->dcbaa->dev_context_ptrs[udev->slot_id]);
Greg Kroah-Hartman700e2052009-04-29 19:14:08 -07002576 xhci_dbg(xhci, "Output Context DMA address = %#08llx\n",
John Yound115b042009-07-27 12:05:15 -07002577 (unsigned long long)virt_dev->out_ctx->dma);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002578 xhci_dbg(xhci, "Slot ID %d Input Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002579 xhci_dbg_ctx(xhci, virt_dev->in_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002580 xhci_dbg(xhci, "Slot ID %d Output Context:\n", udev->slot_id);
John Yound115b042009-07-27 12:05:15 -07002581 xhci_dbg_ctx(xhci, virt_dev->out_ctx, 2);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002582 /*
2583 * USB core uses address 1 for the roothubs, so we add one to the
2584 * address given back to us by the HC.
2585 */
John Yound115b042009-07-27 12:05:15 -07002586 slot_ctx = xhci_get_slot_ctx(xhci, virt_dev->out_ctx);
Andiry Xuc8d4af82010-10-14 07:22:51 -07002587 /* Use kernel assigned address for devices; store xHC assigned
2588 * address locally. */
2589 virt_dev->address = (slot_ctx->dev_state & DEV_ADDR_MASK) + 1;
Sarah Sharpf94e01862009-04-27 19:58:38 -07002590 /* Zero the input context control for later use */
John Yound115b042009-07-27 12:05:15 -07002591 ctrl_ctx = xhci_get_input_control_ctx(xhci, virt_dev->in_ctx);
2592 ctrl_ctx->add_flags = 0;
2593 ctrl_ctx->drop_flags = 0;
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002594
Andiry Xuc8d4af82010-10-14 07:22:51 -07002595 xhci_dbg(xhci, "Internal device address = %d\n", virt_dev->address);
Sarah Sharp3ffbba92009-04-27 19:57:38 -07002596
2597 return 0;
2598}
2599
Sarah Sharpac1c1b72009-09-04 10:53:20 -07002600/* Once a hub descriptor is fetched for a device, we need to update the xHC's
2601 * internal data structures for the device.
2602 */
2603int xhci_update_hub_device(struct usb_hcd *hcd, struct usb_device *hdev,
2604 struct usb_tt *tt, gfp_t mem_flags)
2605{
2606 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2607 struct xhci_virt_device *vdev;
2608 struct xhci_command *config_cmd;
2609 struct xhci_input_control_ctx *ctrl_ctx;
2610 struct xhci_slot_ctx *slot_ctx;
2611 unsigned long flags;
2612 unsigned think_time;
2613 int ret;
2614
2615 /* Ignore root hubs */
2616 if (!hdev->parent)
2617 return 0;
2618
2619 vdev = xhci->devs[hdev->slot_id];
2620 if (!vdev) {
2621 xhci_warn(xhci, "Cannot update hub desc for unknown device.\n");
2622 return -EINVAL;
2623 }
Sarah Sharpa1d78c12009-12-09 15:59:03 -08002624 config_cmd = xhci_alloc_command(xhci, true, true, mem_flags);
Sarah Sharpac1c1b72009-09-04 10:53:20 -07002625 if (!config_cmd) {
2626 xhci_dbg(xhci, "Could not allocate xHCI command structure.\n");
2627 return -ENOMEM;
2628 }
2629
2630 spin_lock_irqsave(&xhci->lock, flags);
2631 xhci_slot_copy(xhci, config_cmd->in_ctx, vdev->out_ctx);
2632 ctrl_ctx = xhci_get_input_control_ctx(xhci, config_cmd->in_ctx);
2633 ctrl_ctx->add_flags |= SLOT_FLAG;
2634 slot_ctx = xhci_get_slot_ctx(xhci, config_cmd->in_ctx);
2635 slot_ctx->dev_info |= DEV_HUB;
2636 if (tt->multi)
2637 slot_ctx->dev_info |= DEV_MTT;
2638 if (xhci->hci_version > 0x95) {
2639 xhci_dbg(xhci, "xHCI version %x needs hub "
2640 "TT think time and number of ports\n",
2641 (unsigned int) xhci->hci_version);
2642 slot_ctx->dev_info2 |= XHCI_MAX_PORTS(hdev->maxchild);
2643 /* Set TT think time - convert from ns to FS bit times.
2644 * 0 = 8 FS bit times, 1 = 16 FS bit times,
2645 * 2 = 24 FS bit times, 3 = 32 FS bit times.
2646 */
2647 think_time = tt->think_time;
2648 if (think_time != 0)
2649 think_time = (think_time / 666) - 1;
2650 slot_ctx->tt_info |= TT_THINK_TIME(think_time);
2651 } else {
2652 xhci_dbg(xhci, "xHCI version %x doesn't need hub "
2653 "TT think time or number of ports\n",
2654 (unsigned int) xhci->hci_version);
2655 }
2656 slot_ctx->dev_state = 0;
2657 spin_unlock_irqrestore(&xhci->lock, flags);
2658
2659 xhci_dbg(xhci, "Set up %s for hub device.\n",
2660 (xhci->hci_version > 0x95) ?
2661 "configure endpoint" : "evaluate context");
2662 xhci_dbg(xhci, "Slot %u Input Context:\n", hdev->slot_id);
2663 xhci_dbg_ctx(xhci, config_cmd->in_ctx, 0);
2664
2665 /* Issue and wait for the configure endpoint or
2666 * evaluate context command.
2667 */
2668 if (xhci->hci_version > 0x95)
2669 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2670 false, false);
2671 else
2672 ret = xhci_configure_endpoint(xhci, hdev, config_cmd,
2673 true, false);
2674
2675 xhci_dbg(xhci, "Slot %u Output Context:\n", hdev->slot_id);
2676 xhci_dbg_ctx(xhci, vdev->out_ctx, 0);
2677
2678 xhci_free_command(xhci, config_cmd);
2679 return ret;
2680}
2681
Sarah Sharp66d4ead2009-04-27 19:52:28 -07002682int xhci_get_frame(struct usb_hcd *hcd)
2683{
2684 struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2685 /* EHCI mods by the periodic size. Why? */
2686 return xhci_readl(xhci, &xhci->run_regs->microframe_index) >> 3;
2687}
2688
2689MODULE_DESCRIPTION(DRIVER_DESC);
2690MODULE_AUTHOR(DRIVER_AUTHOR);
2691MODULE_LICENSE("GPL");
2692
2693static int __init xhci_hcd_init(void)
2694{
2695#ifdef CONFIG_PCI
2696 int retval = 0;
2697
2698 retval = xhci_register_pci();
2699
2700 if (retval < 0) {
2701 printk(KERN_DEBUG "Problem registering PCI driver.");
2702 return retval;
2703 }
2704#endif
Sarah Sharp98441972009-05-14 11:44:18 -07002705 /*
2706 * Check the compiler generated sizes of structures that must be laid
2707 * out in specific ways for hardware access.
2708 */
2709 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
2710 BUILD_BUG_ON(sizeof(struct xhci_slot_ctx) != 8*32/8);
2711 BUILD_BUG_ON(sizeof(struct xhci_ep_ctx) != 8*32/8);
2712 /* xhci_device_control has eight fields, and also
2713 * embeds one xhci_slot_ctx and 31 xhci_ep_ctx
2714 */
Sarah Sharp98441972009-05-14 11:44:18 -07002715 BUILD_BUG_ON(sizeof(struct xhci_stream_ctx) != 4*32/8);
2716 BUILD_BUG_ON(sizeof(union xhci_trb) != 4*32/8);
2717 BUILD_BUG_ON(sizeof(struct xhci_erst_entry) != 4*32/8);
2718 BUILD_BUG_ON(sizeof(struct xhci_cap_regs) != 7*32/8);
2719 BUILD_BUG_ON(sizeof(struct xhci_intr_reg) != 8*32/8);
2720 /* xhci_run_regs has eight fields and embeds 128 xhci_intr_regs */
2721 BUILD_BUG_ON(sizeof(struct xhci_run_regs) != (8+8*128)*32/8);
2722 BUILD_BUG_ON(sizeof(struct xhci_doorbell_array) != 256*32/8);
Sarah Sharp66d4ead2009-04-27 19:52:28 -07002723 return 0;
2724}
2725module_init(xhci_hcd_init);
2726
2727static void __exit xhci_hcd_cleanup(void)
2728{
2729#ifdef CONFIG_PCI
2730 xhci_unregister_pci();
2731#endif
2732}
2733module_exit(xhci_hcd_cleanup);