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