1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * xHCI host controller driver
4 *
5 * Copyright (C) 2008 Intel Corp.
6 *
7 * Author: Sarah Sharp
8 * Some code borrowed from the Linux EHCI driver.
9 */
10
11 #include <linux/usb.h>
12 #include <linux/pci.h>
13 #include <linux/slab.h>
14 #include <linux/dmapool.h>
15 #include <linux/dma-mapping.h>
16
17 #include "xhci.h"
18 #include "xhci-trace.h"
19 #include "xhci-debugfs.h"
20
21 /*
22 * Allocates a generic ring segment from the ring pool, sets the dma address,
23 * initializes the segment to zero, and sets the private next pointer to NULL.
24 *
25 * Section 4.11.1.1:
26 * "All components of all Command and Transfer TRBs shall be initialized to '0'"
27 */
xhci_segment_alloc(struct xhci_hcd * xhci,unsigned int cycle_state,unsigned int max_packet,gfp_t flags)28 static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci,
29 unsigned int cycle_state,
30 unsigned int max_packet,
31 gfp_t flags)
32 {
33 struct xhci_segment *seg;
34 dma_addr_t dma;
35 int i;
36 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
37
38 seg = kzalloc_node(sizeof(*seg), flags, dev_to_node(dev));
39 if (!seg)
40 return NULL;
41
42 seg->trbs = dma_pool_zalloc(xhci->segment_pool, flags, &dma);
43 if (!seg->trbs) {
44 kfree(seg);
45 return NULL;
46 }
47
48 if (max_packet) {
49 seg->bounce_buf = kzalloc_node(max_packet, flags,
50 dev_to_node(dev));
51 if (!seg->bounce_buf) {
52 dma_pool_free(xhci->segment_pool, seg->trbs, dma);
53 kfree(seg);
54 return NULL;
55 }
56 }
57 /* If the cycle state is 0, set the cycle bit to 1 for all the TRBs */
58 if (cycle_state == 0) {
59 for (i = 0; i < TRBS_PER_SEGMENT; i++)
60 seg->trbs[i].link.control = cpu_to_le32(TRB_CYCLE);
61 }
62 seg->dma = dma;
63 seg->next = NULL;
64
65 return seg;
66 }
67
xhci_segment_free(struct xhci_hcd * xhci,struct xhci_segment * seg)68 void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
69 {
70 if (seg->trbs) {
71 dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
72 seg->trbs = NULL;
73 }
74 kfree(seg->bounce_buf);
75 kfree(seg);
76 }
77 EXPORT_SYMBOL_GPL(xhci_segment_free);
78
xhci_free_segments_for_ring(struct xhci_hcd * xhci,struct xhci_segment * first)79 void xhci_free_segments_for_ring(struct xhci_hcd *xhci,
80 struct xhci_segment *first)
81 {
82 struct xhci_segment *seg;
83
84 seg = first->next;
85 while (seg != first) {
86 struct xhci_segment *next = seg->next;
87 xhci_segment_free(xhci, seg);
88 seg = next;
89 }
90 xhci_segment_free(xhci, first);
91 }
92
93 /*
94 * Make the prev segment point to the next segment.
95 *
96 * Change the last TRB in the prev segment to be a Link TRB which points to the
97 * DMA address of the next segment. The caller needs to set any Link TRB
98 * related flags, such as End TRB, Toggle Cycle, and no snoop.
99 */
xhci_link_segments(struct xhci_segment * prev,struct xhci_segment * next,enum xhci_ring_type type,bool chain_links)100 void xhci_link_segments(struct xhci_segment *prev,
101 struct xhci_segment *next,
102 enum xhci_ring_type type, bool chain_links)
103 {
104 u32 val;
105
106 if (!prev || !next)
107 return;
108 prev->next = next;
109 if (type != TYPE_EVENT) {
110 prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
111 cpu_to_le64(next->dma);
112
113 /* Set the last TRB in the segment to have a TRB type ID of Link TRB */
114 val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control);
115 val &= ~TRB_TYPE_BITMASK;
116 val |= TRB_TYPE(TRB_LINK);
117 if (chain_links)
118 val |= TRB_CHAIN;
119 prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
120 }
121 }
122 EXPORT_SYMBOL_GPL(xhci_link_segments);
123
124 /*
125 * Link the ring to the new segments.
126 * Set Toggle Cycle for the new ring if needed.
127 */
xhci_link_rings(struct xhci_hcd * xhci,struct xhci_ring * ring,struct xhci_segment * first,struct xhci_segment * last,unsigned int num_segs)128 static void xhci_link_rings(struct xhci_hcd *xhci, struct xhci_ring *ring,
129 struct xhci_segment *first, struct xhci_segment *last,
130 unsigned int num_segs)
131 {
132 struct xhci_segment *next;
133 bool chain_links;
134
135 if (!ring || !first || !last)
136 return;
137
138 /* Set chain bit for 0.95 hosts, and for isoc rings on AMD 0.96 host */
139 chain_links = !!(xhci_link_trb_quirk(xhci) ||
140 (ring->type == TYPE_ISOC &&
141 (xhci->quirks & XHCI_AMD_0x96_HOST)));
142
143 next = ring->enq_seg->next;
144 xhci_link_segments(ring->enq_seg, first, ring->type, chain_links);
145 xhci_link_segments(last, next, ring->type, chain_links);
146 ring->num_segs += num_segs;
147 ring->num_trbs_free += (TRBS_PER_SEGMENT - 1) * num_segs;
148
149 if (ring->type != TYPE_EVENT && ring->enq_seg == ring->last_seg) {
150 ring->last_seg->trbs[TRBS_PER_SEGMENT-1].link.control
151 &= ~cpu_to_le32(LINK_TOGGLE);
152 last->trbs[TRBS_PER_SEGMENT-1].link.control
153 |= cpu_to_le32(LINK_TOGGLE);
154 ring->last_seg = last;
155 }
156 }
157
158 /*
159 * We need a radix tree for mapping physical addresses of TRBs to which stream
160 * ID they belong to. We need to do this because the host controller won't tell
161 * us which stream ring the TRB came from. We could store the stream ID in an
162 * event data TRB, but that doesn't help us for the cancellation case, since the
163 * endpoint may stop before it reaches that event data TRB.
164 *
165 * The radix tree maps the upper portion of the TRB DMA address to a ring
166 * segment that has the same upper portion of DMA addresses. For example, say I
167 * have segments of size 1KB, that are always 1KB aligned. A segment may
168 * start at 0x10c91000 and end at 0x10c913f0. If I use the upper 10 bits, the
169 * key to the stream ID is 0x43244. I can use the DMA address of the TRB to
170 * pass the radix tree a key to get the right stream ID:
171 *
172 * 0x10c90fff >> 10 = 0x43243
173 * 0x10c912c0 >> 10 = 0x43244
174 * 0x10c91400 >> 10 = 0x43245
175 *
176 * Obviously, only those TRBs with DMA addresses that are within the segment
177 * will make the radix tree return the stream ID for that ring.
178 *
179 * Caveats for the radix tree:
180 *
181 * The radix tree uses an unsigned long as a key pair. On 32-bit systems, an
182 * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
183 * 64-bits. Since we only request 32-bit DMA addresses, we can use that as the
184 * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
185 * PCI DMA addresses on a 64-bit system). There might be a problem on 32-bit
186 * extended systems (where the DMA address can be bigger than 32-bits),
187 * if we allow the PCI dma mask to be bigger than 32-bits. So don't do that.
188 */
xhci_insert_segment_mapping(struct radix_tree_root * trb_address_map,struct xhci_ring * ring,struct xhci_segment * seg,gfp_t mem_flags)189 static int xhci_insert_segment_mapping(struct radix_tree_root *trb_address_map,
190 struct xhci_ring *ring,
191 struct xhci_segment *seg,
192 gfp_t mem_flags)
193 {
194 unsigned long key;
195 int ret;
196
197 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
198 /* Skip any segments that were already added. */
199 if (radix_tree_lookup(trb_address_map, key))
200 return 0;
201
202 ret = radix_tree_maybe_preload(mem_flags);
203 if (ret)
204 return ret;
205 ret = radix_tree_insert(trb_address_map,
206 key, ring);
207 radix_tree_preload_end();
208 return ret;
209 }
210
xhci_remove_segment_mapping(struct radix_tree_root * trb_address_map,struct xhci_segment * seg)211 static void xhci_remove_segment_mapping(struct radix_tree_root *trb_address_map,
212 struct xhci_segment *seg)
213 {
214 unsigned long key;
215
216 key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
217 if (radix_tree_lookup(trb_address_map, key))
218 radix_tree_delete(trb_address_map, key);
219 }
220
xhci_update_stream_segment_mapping(struct radix_tree_root * trb_address_map,struct xhci_ring * ring,struct xhci_segment * first_seg,struct xhci_segment * last_seg,gfp_t mem_flags)221 static int xhci_update_stream_segment_mapping(
222 struct radix_tree_root *trb_address_map,
223 struct xhci_ring *ring,
224 struct xhci_segment *first_seg,
225 struct xhci_segment *last_seg,
226 gfp_t mem_flags)
227 {
228 struct xhci_segment *seg;
229 struct xhci_segment *failed_seg;
230 int ret;
231
232 if (WARN_ON_ONCE(trb_address_map == NULL))
233 return 0;
234
235 seg = first_seg;
236 do {
237 ret = xhci_insert_segment_mapping(trb_address_map,
238 ring, seg, mem_flags);
239 if (ret)
240 goto remove_streams;
241 if (seg == last_seg)
242 return 0;
243 seg = seg->next;
244 } while (seg != first_seg);
245
246 return 0;
247
248 remove_streams:
249 failed_seg = seg;
250 seg = first_seg;
251 do {
252 xhci_remove_segment_mapping(trb_address_map, seg);
253 if (seg == failed_seg)
254 return ret;
255 seg = seg->next;
256 } while (seg != first_seg);
257
258 return ret;
259 }
260
xhci_remove_stream_mapping(struct xhci_ring * ring)261 void xhci_remove_stream_mapping(struct xhci_ring *ring)
262 {
263 struct xhci_segment *seg;
264
265 if (WARN_ON_ONCE(ring->trb_address_map == NULL))
266 return;
267
268 seg = ring->first_seg;
269 do {
270 xhci_remove_segment_mapping(ring->trb_address_map, seg);
271 seg = seg->next;
272 } while (seg != ring->first_seg);
273 }
274 EXPORT_SYMBOL_GPL(xhci_remove_stream_mapping);
275
xhci_update_stream_mapping(struct xhci_ring * ring,gfp_t mem_flags)276 static int xhci_update_stream_mapping(struct xhci_ring *ring, gfp_t mem_flags)
277 {
278 return xhci_update_stream_segment_mapping(ring->trb_address_map, ring,
279 ring->first_seg, ring->last_seg, mem_flags);
280 }
281
282 /* XXX: Do we need the hcd structure in all these functions? */
xhci_ring_free(struct xhci_hcd * xhci,struct xhci_ring * ring)283 void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
284 {
285 if (!ring)
286 return;
287
288 trace_xhci_ring_free(ring);
289
290 if (ring->first_seg) {
291 if (ring->type == TYPE_STREAM)
292 xhci_remove_stream_mapping(ring);
293 xhci_free_segments_for_ring(xhci, ring->first_seg);
294 }
295
296 kfree(ring);
297 }
298 EXPORT_SYMBOL_GPL(xhci_ring_free);
299
xhci_initialize_ring_info(struct xhci_ring * ring,unsigned int cycle_state)300 void xhci_initialize_ring_info(struct xhci_ring *ring,
301 unsigned int cycle_state)
302 {
303 /* The ring is empty, so the enqueue pointer == dequeue pointer */
304 ring->enqueue = ring->first_seg->trbs;
305 ring->enq_seg = ring->first_seg;
306 ring->dequeue = ring->enqueue;
307 ring->deq_seg = ring->first_seg;
308 /* The ring is initialized to 0. The producer must write 1 to the cycle
309 * bit to handover ownership of the TRB, so PCS = 1. The consumer must
310 * compare CCS to the cycle bit to check ownership, so CCS = 1.
311 *
312 * New rings are initialized with cycle state equal to 1; if we are
313 * handling ring expansion, set the cycle state equal to the old ring.
314 */
315 ring->cycle_state = cycle_state;
316
317 /*
318 * Each segment has a link TRB, and leave an extra TRB for SW
319 * accounting purpose
320 */
321 ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
322 }
323 EXPORT_SYMBOL_GPL(xhci_initialize_ring_info);
324
325 /* Allocate segments and link them for a ring */
xhci_alloc_segments_for_ring(struct xhci_hcd * xhci,struct xhci_segment ** first,struct xhci_segment ** last,unsigned int num_segs,unsigned int cycle_state,enum xhci_ring_type type,unsigned int max_packet,gfp_t flags)326 static int xhci_alloc_segments_for_ring(struct xhci_hcd *xhci,
327 struct xhci_segment **first, struct xhci_segment **last,
328 unsigned int num_segs, unsigned int cycle_state,
329 enum xhci_ring_type type, unsigned int max_packet, gfp_t flags)
330 {
331 struct xhci_segment *prev;
332 bool chain_links;
333
334 /* Set chain bit for 0.95 hosts, and for isoc rings on AMD 0.96 host */
335 chain_links = !!(xhci_link_trb_quirk(xhci) ||
336 (type == TYPE_ISOC &&
337 (xhci->quirks & XHCI_AMD_0x96_HOST)));
338
339 prev = xhci_segment_alloc(xhci, cycle_state, max_packet, flags);
340 if (!prev)
341 return -ENOMEM;
342 num_segs--;
343
344 *first = prev;
345 while (num_segs > 0) {
346 struct xhci_segment *next;
347
348 next = xhci_segment_alloc(xhci, cycle_state, max_packet, flags);
349 if (!next) {
350 prev = *first;
351 while (prev) {
352 next = prev->next;
353 xhci_segment_free(xhci, prev);
354 prev = next;
355 }
356 return -ENOMEM;
357 }
358 xhci_link_segments(prev, next, type, chain_links);
359
360 prev = next;
361 num_segs--;
362 }
363 xhci_link_segments(prev, *first, type, chain_links);
364 *last = prev;
365
366 return 0;
367 }
368
xhci_vendor_free_container_ctx(struct xhci_hcd * xhci,struct xhci_container_ctx * ctx)369 static void xhci_vendor_free_container_ctx(struct xhci_hcd *xhci, struct xhci_container_ctx *ctx)
370 {
371 struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
372
373 if (ops && ops->free_container_ctx)
374 ops->free_container_ctx(xhci, ctx);
375 }
376
xhci_vendor_alloc_container_ctx(struct xhci_hcd * xhci,struct xhci_container_ctx * ctx,int type,gfp_t flags)377 static void xhci_vendor_alloc_container_ctx(struct xhci_hcd *xhci, struct xhci_container_ctx *ctx,
378 int type, gfp_t flags)
379 {
380 struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
381
382 if (ops && ops->alloc_container_ctx)
383 ops->alloc_container_ctx(xhci, ctx, type, flags);
384 }
385
xhci_vendor_alloc_transfer_ring(struct xhci_hcd * xhci,u32 endpoint_type,enum xhci_ring_type ring_type,unsigned int max_packet,gfp_t mem_flags)386 static struct xhci_ring *xhci_vendor_alloc_transfer_ring(struct xhci_hcd *xhci,
387 u32 endpoint_type, enum xhci_ring_type ring_type,
388 unsigned int max_packet, gfp_t mem_flags)
389 {
390 struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
391
392 if (ops && ops->alloc_transfer_ring)
393 return ops->alloc_transfer_ring(xhci, endpoint_type, ring_type,
394 max_packet, mem_flags);
395 return 0;
396 }
397
xhci_vendor_free_transfer_ring(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,unsigned int ep_index)398 void xhci_vendor_free_transfer_ring(struct xhci_hcd *xhci,
399 struct xhci_virt_device *virt_dev, unsigned int ep_index)
400 {
401 struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
402
403 if (ops && ops->free_transfer_ring)
404 ops->free_transfer_ring(xhci, virt_dev, ep_index);
405 }
406
xhci_vendor_is_usb_offload_enabled(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,unsigned int ep_index)407 bool xhci_vendor_is_usb_offload_enabled(struct xhci_hcd *xhci,
408 struct xhci_virt_device *virt_dev, unsigned int ep_index)
409 {
410 struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
411
412 if (ops && ops->is_usb_offload_enabled)
413 return ops->is_usb_offload_enabled(xhci, virt_dev, ep_index);
414 return false;
415 }
416
417 /*
418 * Create a new ring with zero or more segments.
419 *
420 * Link each segment together into a ring.
421 * Set the end flag and the cycle toggle bit on the last segment.
422 * See section 4.9.1 and figures 15 and 16.
423 */
xhci_ring_alloc(struct xhci_hcd * xhci,unsigned int num_segs,unsigned int cycle_state,enum xhci_ring_type type,unsigned int max_packet,gfp_t flags)424 struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
425 unsigned int num_segs, unsigned int cycle_state,
426 enum xhci_ring_type type, unsigned int max_packet, gfp_t flags)
427 {
428 struct xhci_ring *ring;
429 int ret;
430 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
431
432 ring = kzalloc_node(sizeof(*ring), flags, dev_to_node(dev));
433 if (!ring)
434 return NULL;
435
436 ring->num_segs = num_segs;
437 ring->bounce_buf_len = max_packet;
438 INIT_LIST_HEAD(&ring->td_list);
439 ring->type = type;
440 if (num_segs == 0)
441 return ring;
442
443 ret = xhci_alloc_segments_for_ring(xhci, &ring->first_seg,
444 &ring->last_seg, num_segs, cycle_state, type,
445 max_packet, flags);
446 if (ret)
447 goto fail;
448
449 /* Only event ring does not use link TRB */
450 if (type != TYPE_EVENT) {
451 /* See section 4.9.2.1 and 6.4.4.1 */
452 ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control |=
453 cpu_to_le32(LINK_TOGGLE);
454 }
455 xhci_initialize_ring_info(ring, cycle_state);
456 trace_xhci_ring_alloc(ring);
457 return ring;
458
459 fail:
460 kfree(ring);
461 return NULL;
462 }
463 EXPORT_SYMBOL_GPL(xhci_ring_alloc);
464
xhci_free_endpoint_ring(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,unsigned int ep_index)465 void xhci_free_endpoint_ring(struct xhci_hcd *xhci,
466 struct xhci_virt_device *virt_dev,
467 unsigned int ep_index)
468 {
469 if (xhci_vendor_is_usb_offload_enabled(xhci, virt_dev, ep_index))
470 xhci_vendor_free_transfer_ring(xhci, virt_dev, ep_index);
471 else
472 xhci_ring_free(xhci, virt_dev->eps[ep_index].ring);
473
474 virt_dev->eps[ep_index].ring = NULL;
475 }
476
477 /*
478 * Expand an existing ring.
479 * Allocate a new ring which has same segment numbers and link the two rings.
480 */
xhci_ring_expansion(struct xhci_hcd * xhci,struct xhci_ring * ring,unsigned int num_trbs,gfp_t flags)481 int xhci_ring_expansion(struct xhci_hcd *xhci, struct xhci_ring *ring,
482 unsigned int num_trbs, gfp_t flags)
483 {
484 struct xhci_segment *first;
485 struct xhci_segment *last;
486 unsigned int num_segs;
487 unsigned int num_segs_needed;
488 int ret;
489
490 num_segs_needed = (num_trbs + (TRBS_PER_SEGMENT - 1) - 1) /
491 (TRBS_PER_SEGMENT - 1);
492
493 /* Allocate number of segments we needed, or double the ring size */
494 num_segs = max(ring->num_segs, num_segs_needed);
495
496 ret = xhci_alloc_segments_for_ring(xhci, &first, &last,
497 num_segs, ring->cycle_state, ring->type,
498 ring->bounce_buf_len, flags);
499 if (ret)
500 return -ENOMEM;
501
502 if (ring->type == TYPE_STREAM)
503 ret = xhci_update_stream_segment_mapping(ring->trb_address_map,
504 ring, first, last, flags);
505 if (ret) {
506 struct xhci_segment *next;
507 do {
508 next = first->next;
509 xhci_segment_free(xhci, first);
510 if (first == last)
511 break;
512 first = next;
513 } while (true);
514 return ret;
515 }
516
517 xhci_link_rings(xhci, ring, first, last, num_segs);
518 trace_xhci_ring_expansion(ring);
519 xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion,
520 "ring expansion succeed, now has %d segments",
521 ring->num_segs);
522
523 return 0;
524 }
525
xhci_alloc_container_ctx(struct xhci_hcd * xhci,int type,gfp_t flags)526 struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci,
527 int type, gfp_t flags)
528 {
529 struct xhci_container_ctx *ctx;
530 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
531 struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
532
533 if ((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT))
534 return NULL;
535
536 ctx = kzalloc_node(sizeof(*ctx), flags, dev_to_node(dev));
537 if (!ctx)
538 return NULL;
539
540 ctx->type = type;
541 ctx->size = HCC_64BYTE_CONTEXT(xhci->hcc_params) ? 2048 : 1024;
542 if (type == XHCI_CTX_TYPE_INPUT)
543 ctx->size += CTX_SIZE(xhci->hcc_params);
544
545 if (xhci_vendor_is_usb_offload_enabled(xhci, NULL, 0) &&
546 (ops && ops->alloc_container_ctx))
547 xhci_vendor_alloc_container_ctx(xhci, ctx, type, flags);
548 else
549 ctx->bytes = dma_pool_zalloc(xhci->device_pool, flags, &ctx->dma);
550
551 if (!ctx->bytes) {
552 kfree(ctx);
553 return NULL;
554 }
555 return ctx;
556 }
557
xhci_free_container_ctx(struct xhci_hcd * xhci,struct xhci_container_ctx * ctx)558 void xhci_free_container_ctx(struct xhci_hcd *xhci,
559 struct xhci_container_ctx *ctx)
560 {
561 struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
562
563 if (!ctx)
564 return;
565 if (xhci_vendor_is_usb_offload_enabled(xhci, NULL, 0) &&
566 (ops && ops->free_container_ctx))
567 xhci_vendor_free_container_ctx(xhci, ctx);
568 else
569 dma_pool_free(xhci->device_pool, ctx->bytes, ctx->dma);
570
571 kfree(ctx);
572 }
573
xhci_get_input_control_ctx(struct xhci_container_ctx * ctx)574 struct xhci_input_control_ctx *xhci_get_input_control_ctx(
575 struct xhci_container_ctx *ctx)
576 {
577 if (ctx->type != XHCI_CTX_TYPE_INPUT)
578 return NULL;
579
580 return (struct xhci_input_control_ctx *)ctx->bytes;
581 }
582
xhci_get_slot_ctx(struct xhci_hcd * xhci,struct xhci_container_ctx * ctx)583 struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_hcd *xhci,
584 struct xhci_container_ctx *ctx)
585 {
586 if (ctx->type == XHCI_CTX_TYPE_DEVICE)
587 return (struct xhci_slot_ctx *)ctx->bytes;
588
589 return (struct xhci_slot_ctx *)
590 (ctx->bytes + CTX_SIZE(xhci->hcc_params));
591 }
592 EXPORT_SYMBOL_GPL(xhci_get_slot_ctx);
593
xhci_get_ep_ctx(struct xhci_hcd * xhci,struct xhci_container_ctx * ctx,unsigned int ep_index)594 struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci,
595 struct xhci_container_ctx *ctx,
596 unsigned int ep_index)
597 {
598 /* increment ep index by offset of start of ep ctx array */
599 ep_index++;
600 if (ctx->type == XHCI_CTX_TYPE_INPUT)
601 ep_index++;
602
603 return (struct xhci_ep_ctx *)
604 (ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params)));
605 }
606 EXPORT_SYMBOL_GPL(xhci_get_ep_ctx);
607
608 /***************** Streams structures manipulation *************************/
609
xhci_free_stream_ctx(struct xhci_hcd * xhci,unsigned int num_stream_ctxs,struct xhci_stream_ctx * stream_ctx,dma_addr_t dma)610 static void xhci_free_stream_ctx(struct xhci_hcd *xhci,
611 unsigned int num_stream_ctxs,
612 struct xhci_stream_ctx *stream_ctx, dma_addr_t dma)
613 {
614 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
615 size_t size = sizeof(struct xhci_stream_ctx) * num_stream_ctxs;
616
617 if (size > MEDIUM_STREAM_ARRAY_SIZE)
618 dma_free_coherent(dev, size,
619 stream_ctx, dma);
620 else if (size <= SMALL_STREAM_ARRAY_SIZE)
621 return dma_pool_free(xhci->small_streams_pool,
622 stream_ctx, dma);
623 else
624 return dma_pool_free(xhci->medium_streams_pool,
625 stream_ctx, dma);
626 }
627
628 /*
629 * The stream context array for each endpoint with bulk streams enabled can
630 * vary in size, based on:
631 * - how many streams the endpoint supports,
632 * - the maximum primary stream array size the host controller supports,
633 * - and how many streams the device driver asks for.
634 *
635 * The stream context array must be a power of 2, and can be as small as
636 * 64 bytes or as large as 1MB.
637 */
xhci_alloc_stream_ctx(struct xhci_hcd * xhci,unsigned int num_stream_ctxs,dma_addr_t * dma,gfp_t mem_flags)638 static struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci,
639 unsigned int num_stream_ctxs, dma_addr_t *dma,
640 gfp_t mem_flags)
641 {
642 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
643 size_t size = sizeof(struct xhci_stream_ctx) * num_stream_ctxs;
644
645 if (size > MEDIUM_STREAM_ARRAY_SIZE)
646 return dma_alloc_coherent(dev, size,
647 dma, mem_flags);
648 else if (size <= SMALL_STREAM_ARRAY_SIZE)
649 return dma_pool_alloc(xhci->small_streams_pool,
650 mem_flags, dma);
651 else
652 return dma_pool_alloc(xhci->medium_streams_pool,
653 mem_flags, dma);
654 }
655
xhci_dma_to_transfer_ring(struct xhci_virt_ep * ep,u64 address)656 struct xhci_ring *xhci_dma_to_transfer_ring(
657 struct xhci_virt_ep *ep,
658 u64 address)
659 {
660 if (ep->ep_state & EP_HAS_STREAMS)
661 return radix_tree_lookup(&ep->stream_info->trb_address_map,
662 address >> TRB_SEGMENT_SHIFT);
663 return ep->ring;
664 }
665
666 /*
667 * Change an endpoint's internal structure so it supports stream IDs. The
668 * number of requested streams includes stream 0, which cannot be used by device
669 * drivers.
670 *
671 * The number of stream contexts in the stream context array may be bigger than
672 * the number of streams the driver wants to use. This is because the number of
673 * stream context array entries must be a power of two.
674 */
xhci_alloc_stream_info(struct xhci_hcd * xhci,unsigned int num_stream_ctxs,unsigned int num_streams,unsigned int max_packet,gfp_t mem_flags)675 struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
676 unsigned int num_stream_ctxs,
677 unsigned int num_streams,
678 unsigned int max_packet, gfp_t mem_flags)
679 {
680 struct xhci_stream_info *stream_info;
681 u32 cur_stream;
682 struct xhci_ring *cur_ring;
683 u64 addr;
684 int ret;
685 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
686
687 xhci_dbg(xhci, "Allocating %u streams and %u "
688 "stream context array entries.\n",
689 num_streams, num_stream_ctxs);
690 if (xhci->cmd_ring_reserved_trbs == MAX_RSVD_CMD_TRBS) {
691 xhci_dbg(xhci, "Command ring has no reserved TRBs available\n");
692 return NULL;
693 }
694 xhci->cmd_ring_reserved_trbs++;
695
696 stream_info = kzalloc_node(sizeof(*stream_info), mem_flags,
697 dev_to_node(dev));
698 if (!stream_info)
699 goto cleanup_trbs;
700
701 stream_info->num_streams = num_streams;
702 stream_info->num_stream_ctxs = num_stream_ctxs;
703
704 /* Initialize the array of virtual pointers to stream rings. */
705 stream_info->stream_rings = kcalloc_node(
706 num_streams, sizeof(struct xhci_ring *), mem_flags,
707 dev_to_node(dev));
708 if (!stream_info->stream_rings)
709 goto cleanup_info;
710
711 /* Initialize the array of DMA addresses for stream rings for the HW. */
712 stream_info->stream_ctx_array = xhci_alloc_stream_ctx(xhci,
713 num_stream_ctxs, &stream_info->ctx_array_dma,
714 mem_flags);
715 if (!stream_info->stream_ctx_array)
716 goto cleanup_ring_array;
717 memset(stream_info->stream_ctx_array, 0,
718 sizeof(struct xhci_stream_ctx)*num_stream_ctxs);
719
720 /* Allocate everything needed to free the stream rings later */
721 stream_info->free_streams_command =
722 xhci_alloc_command_with_ctx(xhci, true, mem_flags);
723 if (!stream_info->free_streams_command)
724 goto cleanup_ctx;
725
726 INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC);
727
728 /* Allocate rings for all the streams that the driver will use,
729 * and add their segment DMA addresses to the radix tree.
730 * Stream 0 is reserved.
731 */
732
733 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
734 stream_info->stream_rings[cur_stream] =
735 xhci_ring_alloc(xhci, 2, 1, TYPE_STREAM, max_packet,
736 mem_flags);
737 cur_ring = stream_info->stream_rings[cur_stream];
738 if (!cur_ring)
739 goto cleanup_rings;
740 cur_ring->stream_id = cur_stream;
741 cur_ring->trb_address_map = &stream_info->trb_address_map;
742 /* Set deq ptr, cycle bit, and stream context type */
743 addr = cur_ring->first_seg->dma |
744 SCT_FOR_CTX(SCT_PRI_TR) |
745 cur_ring->cycle_state;
746 stream_info->stream_ctx_array[cur_stream].stream_ring =
747 cpu_to_le64(addr);
748 xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n",
749 cur_stream, (unsigned long long) addr);
750
751 ret = xhci_update_stream_mapping(cur_ring, mem_flags);
752 if (ret) {
753 xhci_ring_free(xhci, cur_ring);
754 stream_info->stream_rings[cur_stream] = NULL;
755 goto cleanup_rings;
756 }
757 }
758 /* Leave the other unused stream ring pointers in the stream context
759 * array initialized to zero. This will cause the xHC to give us an
760 * error if the device asks for a stream ID we don't have setup (if it
761 * was any other way, the host controller would assume the ring is
762 * "empty" and wait forever for data to be queued to that stream ID).
763 */
764
765 return stream_info;
766
767 cleanup_rings:
768 for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
769 cur_ring = stream_info->stream_rings[cur_stream];
770 if (cur_ring) {
771 xhci_ring_free(xhci, cur_ring);
772 stream_info->stream_rings[cur_stream] = NULL;
773 }
774 }
775 xhci_free_command(xhci, stream_info->free_streams_command);
776 cleanup_ctx:
777 xhci_free_stream_ctx(xhci,
778 stream_info->num_stream_ctxs,
779 stream_info->stream_ctx_array,
780 stream_info->ctx_array_dma);
781 cleanup_ring_array:
782 kfree(stream_info->stream_rings);
783 cleanup_info:
784 kfree(stream_info);
785 cleanup_trbs:
786 xhci->cmd_ring_reserved_trbs--;
787 return NULL;
788 }
789 /*
790 * Sets the MaxPStreams field and the Linear Stream Array field.
791 * Sets the dequeue pointer to the stream context array.
792 */
xhci_setup_streams_ep_input_ctx(struct xhci_hcd * xhci,struct xhci_ep_ctx * ep_ctx,struct xhci_stream_info * stream_info)793 void xhci_setup_streams_ep_input_ctx(struct xhci_hcd *xhci,
794 struct xhci_ep_ctx *ep_ctx,
795 struct xhci_stream_info *stream_info)
796 {
797 u32 max_primary_streams;
798 /* MaxPStreams is the number of stream context array entries, not the
799 * number we're actually using. Must be in 2^(MaxPstreams + 1) format.
800 * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
801 */
802 max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
803 xhci_dbg_trace(xhci, trace_xhci_dbg_context_change,
804 "Setting number of stream ctx array entries to %u",
805 1 << (max_primary_streams + 1));
806 ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK);
807 ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams)
808 | EP_HAS_LSA);
809 ep_ctx->deq = cpu_to_le64(stream_info->ctx_array_dma);
810 }
811
812 /*
813 * Sets the MaxPStreams field and the Linear Stream Array field to 0.
814 * Reinstalls the "normal" endpoint ring (at its previous dequeue mark,
815 * not at the beginning of the ring).
816 */
xhci_setup_no_streams_ep_input_ctx(struct xhci_ep_ctx * ep_ctx,struct xhci_virt_ep * ep)817 void xhci_setup_no_streams_ep_input_ctx(struct xhci_ep_ctx *ep_ctx,
818 struct xhci_virt_ep *ep)
819 {
820 dma_addr_t addr;
821 ep_ctx->ep_info &= cpu_to_le32(~(EP_MAXPSTREAMS_MASK | EP_HAS_LSA));
822 addr = xhci_trb_virt_to_dma(ep->ring->deq_seg, ep->ring->dequeue);
823 ep_ctx->deq = cpu_to_le64(addr | ep->ring->cycle_state);
824 }
825
826 /* Frees all stream contexts associated with the endpoint,
827 *
828 * Caller should fix the endpoint context streams fields.
829 */
xhci_free_stream_info(struct xhci_hcd * xhci,struct xhci_stream_info * stream_info)830 void xhci_free_stream_info(struct xhci_hcd *xhci,
831 struct xhci_stream_info *stream_info)
832 {
833 int cur_stream;
834 struct xhci_ring *cur_ring;
835
836 if (!stream_info)
837 return;
838
839 for (cur_stream = 1; cur_stream < stream_info->num_streams;
840 cur_stream++) {
841 cur_ring = stream_info->stream_rings[cur_stream];
842 if (cur_ring) {
843 xhci_ring_free(xhci, cur_ring);
844 stream_info->stream_rings[cur_stream] = NULL;
845 }
846 }
847 xhci_free_command(xhci, stream_info->free_streams_command);
848 xhci->cmd_ring_reserved_trbs--;
849 if (stream_info->stream_ctx_array)
850 xhci_free_stream_ctx(xhci,
851 stream_info->num_stream_ctxs,
852 stream_info->stream_ctx_array,
853 stream_info->ctx_array_dma);
854
855 kfree(stream_info->stream_rings);
856 kfree(stream_info);
857 }
858
859
860 /***************** Device context manipulation *************************/
861
xhci_free_tt_info(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,int slot_id)862 static void xhci_free_tt_info(struct xhci_hcd *xhci,
863 struct xhci_virt_device *virt_dev,
864 int slot_id)
865 {
866 struct list_head *tt_list_head;
867 struct xhci_tt_bw_info *tt_info, *next;
868 bool slot_found = false;
869
870 /* If the device never made it past the Set Address stage,
871 * it may not have the real_port set correctly.
872 */
873 if (virt_dev->real_port == 0 ||
874 virt_dev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) {
875 xhci_dbg(xhci, "Bad real port.\n");
876 return;
877 }
878
879 tt_list_head = &(xhci->rh_bw[virt_dev->real_port - 1].tts);
880 list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
881 /* Multi-TT hubs will have more than one entry */
882 if (tt_info->slot_id == slot_id) {
883 slot_found = true;
884 list_del(&tt_info->tt_list);
885 kfree(tt_info);
886 } else if (slot_found) {
887 break;
888 }
889 }
890 }
891
xhci_alloc_tt_info(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,struct usb_device * hdev,struct usb_tt * tt,gfp_t mem_flags)892 int xhci_alloc_tt_info(struct xhci_hcd *xhci,
893 struct xhci_virt_device *virt_dev,
894 struct usb_device *hdev,
895 struct usb_tt *tt, gfp_t mem_flags)
896 {
897 struct xhci_tt_bw_info *tt_info;
898 unsigned int num_ports;
899 int i, j;
900 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
901
902 if (!tt->multi)
903 num_ports = 1;
904 else
905 num_ports = hdev->maxchild;
906
907 for (i = 0; i < num_ports; i++, tt_info++) {
908 struct xhci_interval_bw_table *bw_table;
909
910 tt_info = kzalloc_node(sizeof(*tt_info), mem_flags,
911 dev_to_node(dev));
912 if (!tt_info)
913 goto free_tts;
914 INIT_LIST_HEAD(&tt_info->tt_list);
915 list_add(&tt_info->tt_list,
916 &xhci->rh_bw[virt_dev->real_port - 1].tts);
917 tt_info->slot_id = virt_dev->udev->slot_id;
918 if (tt->multi)
919 tt_info->ttport = i+1;
920 bw_table = &tt_info->bw_table;
921 for (j = 0; j < XHCI_MAX_INTERVAL; j++)
922 INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
923 }
924 return 0;
925
926 free_tts:
927 xhci_free_tt_info(xhci, virt_dev, virt_dev->udev->slot_id);
928 return -ENOMEM;
929 }
930
931
932 /* All the xhci_tds in the ring's TD list should be freed at this point.
933 * Should be called with xhci->lock held if there is any chance the TT lists
934 * will be manipulated by the configure endpoint, allocate device, or update
935 * hub functions while this function is removing the TT entries from the list.
936 */
xhci_free_virt_device(struct xhci_hcd * xhci,int slot_id)937 void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
938 {
939 struct xhci_virt_device *dev;
940 int i;
941 int old_active_eps = 0;
942
943 /* Slot ID 0 is reserved */
944 if (slot_id == 0 || !xhci->devs[slot_id])
945 return;
946
947 dev = xhci->devs[slot_id];
948
949 xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
950 if (!dev)
951 return;
952
953 trace_xhci_free_virt_device(dev);
954
955 if (dev->tt_info)
956 old_active_eps = dev->tt_info->active_eps;
957
958 for (i = 0; i < 31; i++) {
959 if (dev->eps[i].ring)
960 xhci_free_endpoint_ring(xhci, dev, i);
961 if (dev->eps[i].stream_info)
962 xhci_free_stream_info(xhci,
963 dev->eps[i].stream_info);
964 /*
965 * Endpoints are normally deleted from the bandwidth list when
966 * endpoints are dropped, before device is freed.
967 * If host is dying or being removed then endpoints aren't
968 * dropped cleanly, so delete the endpoint from list here.
969 * Only applicable for hosts with software bandwidth checking.
970 */
971
972 if (!list_empty(&dev->eps[i].bw_endpoint_list)) {
973 list_del_init(&dev->eps[i].bw_endpoint_list);
974 xhci_dbg(xhci, "Slot %u endpoint %u not removed from BW list!\n",
975 slot_id, i);
976 }
977 }
978 /* If this is a hub, free the TT(s) from the TT list */
979 xhci_free_tt_info(xhci, dev, slot_id);
980 /* If necessary, update the number of active TTs on this root port */
981 xhci_update_tt_active_eps(xhci, dev, old_active_eps);
982
983 if (dev->in_ctx)
984 xhci_free_container_ctx(xhci, dev->in_ctx);
985 if (dev->out_ctx)
986 xhci_free_container_ctx(xhci, dev->out_ctx);
987
988 if (dev->udev && dev->udev->slot_id)
989 dev->udev->slot_id = 0;
990 kfree(xhci->devs[slot_id]);
991 xhci->devs[slot_id] = NULL;
992 }
993
994 /*
995 * Free a virt_device structure.
996 * If the virt_device added a tt_info (a hub) and has children pointing to
997 * that tt_info, then free the child first. Recursive.
998 * We can't rely on udev at this point to find child-parent relationships.
999 */
xhci_free_virt_devices_depth_first(struct xhci_hcd * xhci,int slot_id)1000 static void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
1001 {
1002 struct xhci_virt_device *vdev;
1003 struct list_head *tt_list_head;
1004 struct xhci_tt_bw_info *tt_info, *next;
1005 int i;
1006
1007 vdev = xhci->devs[slot_id];
1008 if (!vdev)
1009 return;
1010
1011 if (vdev->real_port == 0 ||
1012 vdev->real_port > HCS_MAX_PORTS(xhci->hcs_params1)) {
1013 xhci_dbg(xhci, "Bad vdev->real_port.\n");
1014 goto out;
1015 }
1016
1017 tt_list_head = &(xhci->rh_bw[vdev->real_port - 1].tts);
1018 list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
1019 /* is this a hub device that added a tt_info to the tts list */
1020 if (tt_info->slot_id == slot_id) {
1021 /* are any devices using this tt_info? */
1022 for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
1023 vdev = xhci->devs[i];
1024 if (vdev && (vdev->tt_info == tt_info))
1025 xhci_free_virt_devices_depth_first(
1026 xhci, i);
1027 }
1028 }
1029 }
1030 out:
1031 /* we are now at a leaf device */
1032 xhci_debugfs_remove_slot(xhci, slot_id);
1033 xhci_free_virt_device(xhci, slot_id);
1034 }
1035
xhci_alloc_virt_device(struct xhci_hcd * xhci,int slot_id,struct usb_device * udev,gfp_t flags)1036 int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
1037 struct usb_device *udev, gfp_t flags)
1038 {
1039 struct xhci_virt_device *dev;
1040 int i;
1041
1042 /* Slot ID 0 is reserved */
1043 if (slot_id == 0 || xhci->devs[slot_id]) {
1044 xhci_warn(xhci, "Bad Slot ID %d\n", slot_id);
1045 return 0;
1046 }
1047
1048 dev = kzalloc(sizeof(*dev), flags);
1049 if (!dev)
1050 return 0;
1051
1052 dev->slot_id = slot_id;
1053
1054 /* Allocate the (output) device context that will be used in the HC. */
1055 dev->out_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags);
1056 if (!dev->out_ctx)
1057 goto fail;
1058
1059 xhci_dbg(xhci, "Slot %d output ctx = 0x%llx (dma)\n", slot_id,
1060 (unsigned long long)dev->out_ctx->dma);
1061
1062 /* Allocate the (input) device context for address device command */
1063 dev->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, flags);
1064 if (!dev->in_ctx)
1065 goto fail;
1066
1067 xhci_dbg(xhci, "Slot %d input ctx = 0x%llx (dma)\n", slot_id,
1068 (unsigned long long)dev->in_ctx->dma);
1069
1070 /* Initialize the cancellation and bandwidth list for each ep */
1071 for (i = 0; i < 31; i++) {
1072 dev->eps[i].ep_index = i;
1073 dev->eps[i].vdev = dev;
1074 dev->eps[i].xhci = xhci;
1075 INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list);
1076 INIT_LIST_HEAD(&dev->eps[i].bw_endpoint_list);
1077 }
1078
1079 /* Allocate endpoint 0 ring */
1080 dev->eps[0].ring = xhci_ring_alloc(xhci, 2, 1, TYPE_CTRL, 0, flags);
1081 if (!dev->eps[0].ring)
1082 goto fail;
1083
1084 dev->udev = udev;
1085
1086 /* Point to output device context in dcbaa. */
1087 xhci->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(dev->out_ctx->dma);
1088 xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n",
1089 slot_id,
1090 &xhci->dcbaa->dev_context_ptrs[slot_id],
1091 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[slot_id]));
1092
1093 trace_xhci_alloc_virt_device(dev);
1094
1095 xhci->devs[slot_id] = dev;
1096
1097 return 1;
1098 fail:
1099
1100 if (dev->in_ctx)
1101 xhci_free_container_ctx(xhci, dev->in_ctx);
1102 if (dev->out_ctx)
1103 xhci_free_container_ctx(xhci, dev->out_ctx);
1104 kfree(dev);
1105
1106 return 0;
1107 }
1108
xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd * xhci,struct usb_device * udev)1109 void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci,
1110 struct usb_device *udev)
1111 {
1112 struct xhci_virt_device *virt_dev;
1113 struct xhci_ep_ctx *ep0_ctx;
1114 struct xhci_ring *ep_ring;
1115
1116 virt_dev = xhci->devs[udev->slot_id];
1117 ep0_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, 0);
1118 ep_ring = virt_dev->eps[0].ring;
1119 /*
1120 * FIXME we don't keep track of the dequeue pointer very well after a
1121 * Set TR dequeue pointer, so we're setting the dequeue pointer of the
1122 * host to our enqueue pointer. This should only be called after a
1123 * configured device has reset, so all control transfers should have
1124 * been completed or cancelled before the reset.
1125 */
1126 ep0_ctx->deq = cpu_to_le64(xhci_trb_virt_to_dma(ep_ring->enq_seg,
1127 ep_ring->enqueue)
1128 | ep_ring->cycle_state);
1129 }
1130
1131 /*
1132 * The xHCI roothub may have ports of differing speeds in any order in the port
1133 * status registers.
1134 *
1135 * The xHCI hardware wants to know the roothub port number that the USB device
1136 * is attached to (or the roothub port its ancestor hub is attached to). All we
1137 * know is the index of that port under either the USB 2.0 or the USB 3.0
1138 * roothub, but that doesn't give us the real index into the HW port status
1139 * registers. Call xhci_find_raw_port_number() to get real index.
1140 */
xhci_find_real_port_number(struct xhci_hcd * xhci,struct usb_device * udev)1141 static u32 xhci_find_real_port_number(struct xhci_hcd *xhci,
1142 struct usb_device *udev)
1143 {
1144 struct usb_device *top_dev;
1145 struct usb_hcd *hcd;
1146
1147 if (udev->speed >= USB_SPEED_SUPER)
1148 hcd = xhci_get_usb3_hcd(xhci);
1149 else
1150 hcd = xhci->main_hcd;
1151
1152 for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
1153 top_dev = top_dev->parent)
1154 /* Found device below root hub */;
1155
1156 return xhci_find_raw_port_number(hcd, top_dev->portnum);
1157 }
1158
1159 /* Setup an xHCI virtual device for a Set Address command */
xhci_setup_addressable_virt_dev(struct xhci_hcd * xhci,struct usb_device * udev)1160 int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
1161 {
1162 struct xhci_virt_device *dev;
1163 struct xhci_ep_ctx *ep0_ctx;
1164 struct xhci_slot_ctx *slot_ctx;
1165 u32 port_num;
1166 u32 max_packets;
1167 struct usb_device *top_dev;
1168
1169 dev = xhci->devs[udev->slot_id];
1170 /* Slot ID 0 is reserved */
1171 if (udev->slot_id == 0 || !dev) {
1172 xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
1173 udev->slot_id);
1174 return -EINVAL;
1175 }
1176 ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0);
1177 slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx);
1178
1179 /* 3) Only the control endpoint is valid - one endpoint context */
1180 slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | udev->route);
1181 switch (udev->speed) {
1182 case USB_SPEED_SUPER_PLUS:
1183 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SSP);
1184 max_packets = MAX_PACKET(512);
1185 break;
1186 case USB_SPEED_SUPER:
1187 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
1188 max_packets = MAX_PACKET(512);
1189 break;
1190 case USB_SPEED_HIGH:
1191 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
1192 max_packets = MAX_PACKET(64);
1193 break;
1194 /* USB core guesses at a 64-byte max packet first for FS devices */
1195 case USB_SPEED_FULL:
1196 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
1197 max_packets = MAX_PACKET(64);
1198 break;
1199 case USB_SPEED_LOW:
1200 slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
1201 max_packets = MAX_PACKET(8);
1202 break;
1203 case USB_SPEED_WIRELESS:
1204 xhci_dbg(xhci, "FIXME xHCI doesn't support wireless speeds\n");
1205 return -EINVAL;
1206 default:
1207 /* Speed was set earlier, this shouldn't happen. */
1208 return -EINVAL;
1209 }
1210 /* Find the root hub port this device is under */
1211 port_num = xhci_find_real_port_number(xhci, udev);
1212 if (!port_num)
1213 return -EINVAL;
1214 slot_ctx->dev_info2 |= cpu_to_le32(ROOT_HUB_PORT(port_num));
1215 /* Set the port number in the virtual_device to the faked port number */
1216 for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
1217 top_dev = top_dev->parent)
1218 /* Found device below root hub */;
1219 dev->fake_port = top_dev->portnum;
1220 dev->real_port = port_num;
1221 xhci_dbg(xhci, "Set root hub portnum to %d\n", port_num);
1222 xhci_dbg(xhci, "Set fake root hub portnum to %d\n", dev->fake_port);
1223
1224 /* Find the right bandwidth table that this device will be a part of.
1225 * If this is a full speed device attached directly to a root port (or a
1226 * decendent of one), it counts as a primary bandwidth domain, not a
1227 * secondary bandwidth domain under a TT. An xhci_tt_info structure
1228 * will never be created for the HS root hub.
1229 */
1230 if (!udev->tt || !udev->tt->hub->parent) {
1231 dev->bw_table = &xhci->rh_bw[port_num - 1].bw_table;
1232 } else {
1233 struct xhci_root_port_bw_info *rh_bw;
1234 struct xhci_tt_bw_info *tt_bw;
1235
1236 rh_bw = &xhci->rh_bw[port_num - 1];
1237 /* Find the right TT. */
1238 list_for_each_entry(tt_bw, &rh_bw->tts, tt_list) {
1239 if (tt_bw->slot_id != udev->tt->hub->slot_id)
1240 continue;
1241
1242 if (!dev->udev->tt->multi ||
1243 (udev->tt->multi &&
1244 tt_bw->ttport == dev->udev->ttport)) {
1245 dev->bw_table = &tt_bw->bw_table;
1246 dev->tt_info = tt_bw;
1247 break;
1248 }
1249 }
1250 if (!dev->tt_info)
1251 xhci_warn(xhci, "WARN: Didn't find a matching TT\n");
1252 }
1253
1254 /* Is this a LS/FS device under an external HS hub? */
1255 if (udev->tt && udev->tt->hub->parent) {
1256 slot_ctx->tt_info = cpu_to_le32(udev->tt->hub->slot_id |
1257 (udev->ttport << 8));
1258 if (udev->tt->multi)
1259 slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
1260 }
1261 xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
1262 xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
1263
1264 /* Step 4 - ring already allocated */
1265 /* Step 5 */
1266 ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP));
1267
1268 /* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
1269 ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3) |
1270 max_packets);
1271
1272 ep0_ctx->deq = cpu_to_le64(dev->eps[0].ring->first_seg->dma |
1273 dev->eps[0].ring->cycle_state);
1274
1275 trace_xhci_setup_addressable_virt_device(dev);
1276
1277 /* Steps 7 and 8 were done in xhci_alloc_virt_device() */
1278
1279 return 0;
1280 }
1281
1282 /*
1283 * Convert interval expressed as 2^(bInterval - 1) == interval into
1284 * straight exponent value 2^n == interval.
1285 *
1286 */
xhci_parse_exponent_interval(struct usb_device * udev,struct usb_host_endpoint * ep)1287 static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
1288 struct usb_host_endpoint *ep)
1289 {
1290 unsigned int interval;
1291
1292 interval = clamp_val(ep->desc.bInterval, 1, 16) - 1;
1293 if (interval != ep->desc.bInterval - 1)
1294 dev_warn(&udev->dev,
1295 "ep %#x - rounding interval to %d %sframes\n",
1296 ep->desc.bEndpointAddress,
1297 1 << interval,
1298 udev->speed == USB_SPEED_FULL ? "" : "micro");
1299
1300 if (udev->speed == USB_SPEED_FULL) {
1301 /*
1302 * Full speed isoc endpoints specify interval in frames,
1303 * not microframes. We are using microframes everywhere,
1304 * so adjust accordingly.
1305 */
1306 interval += 3; /* 1 frame = 2^3 uframes */
1307 }
1308
1309 return interval;
1310 }
1311
1312 /*
1313 * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
1314 * microframes, rounded down to nearest power of 2.
1315 */
xhci_microframes_to_exponent(struct usb_device * udev,struct usb_host_endpoint * ep,unsigned int desc_interval,unsigned int min_exponent,unsigned int max_exponent)1316 static unsigned int xhci_microframes_to_exponent(struct usb_device *udev,
1317 struct usb_host_endpoint *ep, unsigned int desc_interval,
1318 unsigned int min_exponent, unsigned int max_exponent)
1319 {
1320 unsigned int interval;
1321
1322 interval = fls(desc_interval) - 1;
1323 interval = clamp_val(interval, min_exponent, max_exponent);
1324 if ((1 << interval) != desc_interval)
1325 dev_dbg(&udev->dev,
1326 "ep %#x - rounding interval to %d microframes, ep desc says %d microframes\n",
1327 ep->desc.bEndpointAddress,
1328 1 << interval,
1329 desc_interval);
1330
1331 return interval;
1332 }
1333
xhci_parse_microframe_interval(struct usb_device * udev,struct usb_host_endpoint * ep)1334 static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
1335 struct usb_host_endpoint *ep)
1336 {
1337 if (ep->desc.bInterval == 0)
1338 return 0;
1339 return xhci_microframes_to_exponent(udev, ep,
1340 ep->desc.bInterval, 0, 15);
1341 }
1342
1343
xhci_parse_frame_interval(struct usb_device * udev,struct usb_host_endpoint * ep)1344 static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
1345 struct usb_host_endpoint *ep)
1346 {
1347 return xhci_microframes_to_exponent(udev, ep,
1348 ep->desc.bInterval * 8, 3, 10);
1349 }
1350
1351 /* Return the polling or NAK interval.
1352 *
1353 * The polling interval is expressed in "microframes". If xHCI's Interval field
1354 * is set to N, it will service the endpoint every 2^(Interval)*125us.
1355 *
1356 * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
1357 * is set to 0.
1358 */
xhci_get_endpoint_interval(struct usb_device * udev,struct usb_host_endpoint * ep)1359 static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
1360 struct usb_host_endpoint *ep)
1361 {
1362 unsigned int interval = 0;
1363
1364 switch (udev->speed) {
1365 case USB_SPEED_HIGH:
1366 /* Max NAK rate */
1367 if (usb_endpoint_xfer_control(&ep->desc) ||
1368 usb_endpoint_xfer_bulk(&ep->desc)) {
1369 interval = xhci_parse_microframe_interval(udev, ep);
1370 break;
1371 }
1372 fallthrough; /* SS and HS isoc/int have same decoding */
1373
1374 case USB_SPEED_SUPER_PLUS:
1375 case USB_SPEED_SUPER:
1376 if (usb_endpoint_xfer_int(&ep->desc) ||
1377 usb_endpoint_xfer_isoc(&ep->desc)) {
1378 interval = xhci_parse_exponent_interval(udev, ep);
1379 }
1380 break;
1381
1382 case USB_SPEED_FULL:
1383 if (usb_endpoint_xfer_isoc(&ep->desc)) {
1384 interval = xhci_parse_exponent_interval(udev, ep);
1385 break;
1386 }
1387 /*
1388 * Fall through for interrupt endpoint interval decoding
1389 * since it uses the same rules as low speed interrupt
1390 * endpoints.
1391 */
1392 fallthrough;
1393
1394 case USB_SPEED_LOW:
1395 if (usb_endpoint_xfer_int(&ep->desc) ||
1396 usb_endpoint_xfer_isoc(&ep->desc)) {
1397
1398 interval = xhci_parse_frame_interval(udev, ep);
1399 }
1400 break;
1401
1402 default:
1403 BUG();
1404 }
1405 return interval;
1406 }
1407
1408 /* The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
1409 * High speed endpoint descriptors can define "the number of additional
1410 * transaction opportunities per microframe", but that goes in the Max Burst
1411 * endpoint context field.
1412 */
xhci_get_endpoint_mult(struct usb_device * udev,struct usb_host_endpoint * ep)1413 static u32 xhci_get_endpoint_mult(struct usb_device *udev,
1414 struct usb_host_endpoint *ep)
1415 {
1416 if (udev->speed < USB_SPEED_SUPER ||
1417 !usb_endpoint_xfer_isoc(&ep->desc))
1418 return 0;
1419 return ep->ss_ep_comp.bmAttributes;
1420 }
1421
xhci_get_endpoint_max_burst(struct usb_device * udev,struct usb_host_endpoint * ep)1422 static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
1423 struct usb_host_endpoint *ep)
1424 {
1425 /* Super speed and Plus have max burst in ep companion desc */
1426 if (udev->speed >= USB_SPEED_SUPER)
1427 return ep->ss_ep_comp.bMaxBurst;
1428
1429 if (udev->speed == USB_SPEED_HIGH &&
1430 (usb_endpoint_xfer_isoc(&ep->desc) ||
1431 usb_endpoint_xfer_int(&ep->desc)))
1432 return usb_endpoint_maxp_mult(&ep->desc) - 1;
1433
1434 return 0;
1435 }
1436
xhci_get_endpoint_type(struct usb_host_endpoint * ep)1437 static u32 xhci_get_endpoint_type(struct usb_host_endpoint *ep)
1438 {
1439 int in;
1440
1441 in = usb_endpoint_dir_in(&ep->desc);
1442
1443 switch (usb_endpoint_type(&ep->desc)) {
1444 case USB_ENDPOINT_XFER_CONTROL:
1445 return CTRL_EP;
1446 case USB_ENDPOINT_XFER_BULK:
1447 return in ? BULK_IN_EP : BULK_OUT_EP;
1448 case USB_ENDPOINT_XFER_ISOC:
1449 return in ? ISOC_IN_EP : ISOC_OUT_EP;
1450 case USB_ENDPOINT_XFER_INT:
1451 return in ? INT_IN_EP : INT_OUT_EP;
1452 }
1453 return 0;
1454 }
1455
1456 /* Return the maximum endpoint service interval time (ESIT) payload.
1457 * Basically, this is the maxpacket size, multiplied by the burst size
1458 * and mult size.
1459 */
xhci_get_max_esit_payload(struct usb_device * udev,struct usb_host_endpoint * ep)1460 static u32 xhci_get_max_esit_payload(struct usb_device *udev,
1461 struct usb_host_endpoint *ep)
1462 {
1463 int max_burst;
1464 int max_packet;
1465
1466 /* Only applies for interrupt or isochronous endpoints */
1467 if (usb_endpoint_xfer_control(&ep->desc) ||
1468 usb_endpoint_xfer_bulk(&ep->desc))
1469 return 0;
1470
1471 /* SuperSpeedPlus Isoc ep sending over 48k per esit */
1472 if ((udev->speed >= USB_SPEED_SUPER_PLUS) &&
1473 USB_SS_SSP_ISOC_COMP(ep->ss_ep_comp.bmAttributes))
1474 return le32_to_cpu(ep->ssp_isoc_ep_comp.dwBytesPerInterval);
1475 /* SuperSpeed or SuperSpeedPlus Isoc ep with less than 48k per esit */
1476 else if (udev->speed >= USB_SPEED_SUPER)
1477 return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
1478
1479 max_packet = usb_endpoint_maxp(&ep->desc);
1480 max_burst = usb_endpoint_maxp_mult(&ep->desc);
1481 /* A 0 in max burst means 1 transfer per ESIT */
1482 return max_packet * max_burst;
1483 }
1484
1485 /* Set up an endpoint with one ring segment. Do not allocate stream rings.
1486 * Drivers will have to call usb_alloc_streams() to do that.
1487 */
xhci_endpoint_init(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,struct usb_device * udev,struct usb_host_endpoint * ep,gfp_t mem_flags)1488 int xhci_endpoint_init(struct xhci_hcd *xhci,
1489 struct xhci_virt_device *virt_dev,
1490 struct usb_device *udev,
1491 struct usb_host_endpoint *ep,
1492 gfp_t mem_flags)
1493 {
1494 unsigned int ep_index;
1495 struct xhci_ep_ctx *ep_ctx;
1496 struct xhci_ring *ep_ring;
1497 unsigned int max_packet;
1498 enum xhci_ring_type ring_type;
1499 u32 max_esit_payload;
1500 u32 endpoint_type;
1501 unsigned int max_burst;
1502 unsigned int interval;
1503 unsigned int mult;
1504 unsigned int avg_trb_len;
1505 unsigned int err_count = 0;
1506
1507 ep_index = xhci_get_endpoint_index(&ep->desc);
1508 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
1509
1510 endpoint_type = xhci_get_endpoint_type(ep);
1511 if (!endpoint_type)
1512 return -EINVAL;
1513
1514 ring_type = usb_endpoint_type(&ep->desc);
1515
1516 /*
1517 * Get values to fill the endpoint context, mostly from ep descriptor.
1518 * The average TRB buffer lengt for bulk endpoints is unclear as we
1519 * have no clue on scatter gather list entry size. For Isoc and Int,
1520 * set it to max available. See xHCI 1.1 spec 4.14.1.1 for details.
1521 */
1522 max_esit_payload = xhci_get_max_esit_payload(udev, ep);
1523 interval = xhci_get_endpoint_interval(udev, ep);
1524
1525 /* Periodic endpoint bInterval limit quirk */
1526 if (usb_endpoint_xfer_int(&ep->desc) ||
1527 usb_endpoint_xfer_isoc(&ep->desc)) {
1528 if ((xhci->quirks & XHCI_LIMIT_ENDPOINT_INTERVAL_7) &&
1529 udev->speed >= USB_SPEED_HIGH &&
1530 interval >= 7) {
1531 interval = 6;
1532 }
1533 }
1534
1535 mult = xhci_get_endpoint_mult(udev, ep);
1536 max_packet = usb_endpoint_maxp(&ep->desc);
1537 max_burst = xhci_get_endpoint_max_burst(udev, ep);
1538 avg_trb_len = max_esit_payload;
1539
1540 /* FIXME dig Mult and streams info out of ep companion desc */
1541
1542 /* Allow 3 retries for everything but isoc, set CErr = 3 */
1543 if (!usb_endpoint_xfer_isoc(&ep->desc))
1544 err_count = 3;
1545 /* HS bulk max packet should be 512, FS bulk supports 8, 16, 32 or 64 */
1546 if (usb_endpoint_xfer_bulk(&ep->desc)) {
1547 if (udev->speed == USB_SPEED_HIGH)
1548 max_packet = 512;
1549 if (udev->speed == USB_SPEED_FULL) {
1550 max_packet = rounddown_pow_of_two(max_packet);
1551 max_packet = clamp_val(max_packet, 8, 64);
1552 }
1553 }
1554 /* xHCI 1.0 and 1.1 indicates that ctrl ep avg TRB Length should be 8 */
1555 if (usb_endpoint_xfer_control(&ep->desc) && xhci->hci_version >= 0x100)
1556 avg_trb_len = 8;
1557 /* xhci 1.1 with LEC support doesn't use mult field, use RsvdZ */
1558 if ((xhci->hci_version > 0x100) && HCC2_LEC(xhci->hcc_params2))
1559 mult = 0;
1560
1561 /* Set up the endpoint ring */
1562 if (xhci_vendor_is_usb_offload_enabled(xhci, virt_dev, ep_index) &&
1563 usb_endpoint_xfer_isoc(&ep->desc)) {
1564 virt_dev->eps[ep_index].new_ring =
1565 xhci_vendor_alloc_transfer_ring(xhci, endpoint_type, ring_type,
1566 max_packet, mem_flags);
1567 } else {
1568 virt_dev->eps[ep_index].new_ring =
1569 xhci_ring_alloc(xhci, 2, 1, ring_type, max_packet, mem_flags);
1570 }
1571
1572 if (!virt_dev->eps[ep_index].new_ring)
1573 return -ENOMEM;
1574
1575 virt_dev->eps[ep_index].skip = false;
1576 ep_ring = virt_dev->eps[ep_index].new_ring;
1577
1578 /* Fill the endpoint context */
1579 ep_ctx->ep_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
1580 EP_INTERVAL(interval) |
1581 EP_MULT(mult));
1582 ep_ctx->ep_info2 = cpu_to_le32(EP_TYPE(endpoint_type) |
1583 MAX_PACKET(max_packet) |
1584 MAX_BURST(max_burst) |
1585 ERROR_COUNT(err_count));
1586 ep_ctx->deq = cpu_to_le64(ep_ring->first_seg->dma |
1587 ep_ring->cycle_state);
1588
1589 ep_ctx->tx_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
1590 EP_AVG_TRB_LENGTH(avg_trb_len));
1591
1592 return 0;
1593 }
1594
xhci_endpoint_zero(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,struct usb_host_endpoint * ep)1595 void xhci_endpoint_zero(struct xhci_hcd *xhci,
1596 struct xhci_virt_device *virt_dev,
1597 struct usb_host_endpoint *ep)
1598 {
1599 unsigned int ep_index;
1600 struct xhci_ep_ctx *ep_ctx;
1601
1602 ep_index = xhci_get_endpoint_index(&ep->desc);
1603 ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
1604
1605 ep_ctx->ep_info = 0;
1606 ep_ctx->ep_info2 = 0;
1607 ep_ctx->deq = 0;
1608 ep_ctx->tx_info = 0;
1609 /* Don't free the endpoint ring until the set interface or configuration
1610 * request succeeds.
1611 */
1612 }
1613
xhci_clear_endpoint_bw_info(struct xhci_bw_info * bw_info)1614 void xhci_clear_endpoint_bw_info(struct xhci_bw_info *bw_info)
1615 {
1616 bw_info->ep_interval = 0;
1617 bw_info->mult = 0;
1618 bw_info->num_packets = 0;
1619 bw_info->max_packet_size = 0;
1620 bw_info->type = 0;
1621 bw_info->max_esit_payload = 0;
1622 }
1623
xhci_update_bw_info(struct xhci_hcd * xhci,struct xhci_container_ctx * in_ctx,struct xhci_input_control_ctx * ctrl_ctx,struct xhci_virt_device * virt_dev)1624 void xhci_update_bw_info(struct xhci_hcd *xhci,
1625 struct xhci_container_ctx *in_ctx,
1626 struct xhci_input_control_ctx *ctrl_ctx,
1627 struct xhci_virt_device *virt_dev)
1628 {
1629 struct xhci_bw_info *bw_info;
1630 struct xhci_ep_ctx *ep_ctx;
1631 unsigned int ep_type;
1632 int i;
1633
1634 for (i = 1; i < 31; i++) {
1635 bw_info = &virt_dev->eps[i].bw_info;
1636
1637 /* We can't tell what endpoint type is being dropped, but
1638 * unconditionally clearing the bandwidth info for non-periodic
1639 * endpoints should be harmless because the info will never be
1640 * set in the first place.
1641 */
1642 if (!EP_IS_ADDED(ctrl_ctx, i) && EP_IS_DROPPED(ctrl_ctx, i)) {
1643 /* Dropped endpoint */
1644 xhci_clear_endpoint_bw_info(bw_info);
1645 continue;
1646 }
1647
1648 if (EP_IS_ADDED(ctrl_ctx, i)) {
1649 ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, i);
1650 ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
1651
1652 /* Ignore non-periodic endpoints */
1653 if (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
1654 ep_type != ISOC_IN_EP &&
1655 ep_type != INT_IN_EP)
1656 continue;
1657
1658 /* Added or changed endpoint */
1659 bw_info->ep_interval = CTX_TO_EP_INTERVAL(
1660 le32_to_cpu(ep_ctx->ep_info));
1661 /* Number of packets and mult are zero-based in the
1662 * input context, but we want one-based for the
1663 * interval table.
1664 */
1665 bw_info->mult = CTX_TO_EP_MULT(
1666 le32_to_cpu(ep_ctx->ep_info)) + 1;
1667 bw_info->num_packets = CTX_TO_MAX_BURST(
1668 le32_to_cpu(ep_ctx->ep_info2)) + 1;
1669 bw_info->max_packet_size = MAX_PACKET_DECODED(
1670 le32_to_cpu(ep_ctx->ep_info2));
1671 bw_info->type = ep_type;
1672 bw_info->max_esit_payload = CTX_TO_MAX_ESIT_PAYLOAD(
1673 le32_to_cpu(ep_ctx->tx_info));
1674 }
1675 }
1676 }
1677
1678 /* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
1679 * Useful when you want to change one particular aspect of the endpoint and then
1680 * issue a configure endpoint command.
1681 */
xhci_endpoint_copy(struct xhci_hcd * xhci,struct xhci_container_ctx * in_ctx,struct xhci_container_ctx * out_ctx,unsigned int ep_index)1682 void xhci_endpoint_copy(struct xhci_hcd *xhci,
1683 struct xhci_container_ctx *in_ctx,
1684 struct xhci_container_ctx *out_ctx,
1685 unsigned int ep_index)
1686 {
1687 struct xhci_ep_ctx *out_ep_ctx;
1688 struct xhci_ep_ctx *in_ep_ctx;
1689
1690 out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1691 in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1692
1693 in_ep_ctx->ep_info = out_ep_ctx->ep_info;
1694 in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
1695 in_ep_ctx->deq = out_ep_ctx->deq;
1696 in_ep_ctx->tx_info = out_ep_ctx->tx_info;
1697 if (xhci->quirks & XHCI_MTK_HOST) {
1698 in_ep_ctx->reserved[0] = out_ep_ctx->reserved[0];
1699 in_ep_ctx->reserved[1] = out_ep_ctx->reserved[1];
1700 }
1701 }
1702
1703 /* Copy output xhci_slot_ctx to the input xhci_slot_ctx.
1704 * Useful when you want to change one particular aspect of the endpoint and then
1705 * issue a configure endpoint command. Only the context entries field matters,
1706 * but we'll copy the whole thing anyway.
1707 */
xhci_slot_copy(struct xhci_hcd * xhci,struct xhci_container_ctx * in_ctx,struct xhci_container_ctx * out_ctx)1708 void xhci_slot_copy(struct xhci_hcd *xhci,
1709 struct xhci_container_ctx *in_ctx,
1710 struct xhci_container_ctx *out_ctx)
1711 {
1712 struct xhci_slot_ctx *in_slot_ctx;
1713 struct xhci_slot_ctx *out_slot_ctx;
1714
1715 in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
1716 out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx);
1717
1718 in_slot_ctx->dev_info = out_slot_ctx->dev_info;
1719 in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
1720 in_slot_ctx->tt_info = out_slot_ctx->tt_info;
1721 in_slot_ctx->dev_state = out_slot_ctx->dev_state;
1722 }
1723
1724 /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
scratchpad_alloc(struct xhci_hcd * xhci,gfp_t flags)1725 static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
1726 {
1727 int i;
1728 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
1729 int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
1730
1731 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
1732 "Allocating %d scratchpad buffers", num_sp);
1733
1734 if (!num_sp)
1735 return 0;
1736
1737 xhci->scratchpad = kzalloc_node(sizeof(*xhci->scratchpad), flags,
1738 dev_to_node(dev));
1739 if (!xhci->scratchpad)
1740 goto fail_sp;
1741
1742 xhci->scratchpad->sp_array = dma_alloc_coherent(dev,
1743 num_sp * sizeof(u64),
1744 &xhci->scratchpad->sp_dma, flags);
1745 if (!xhci->scratchpad->sp_array)
1746 goto fail_sp2;
1747
1748 xhci->scratchpad->sp_buffers = kcalloc_node(num_sp, sizeof(void *),
1749 flags, dev_to_node(dev));
1750 if (!xhci->scratchpad->sp_buffers)
1751 goto fail_sp3;
1752
1753 xhci->dcbaa->dev_context_ptrs[0] = cpu_to_le64(xhci->scratchpad->sp_dma);
1754 for (i = 0; i < num_sp; i++) {
1755 dma_addr_t dma;
1756 void *buf = dma_alloc_coherent(dev, xhci->page_size, &dma,
1757 flags);
1758 if (!buf)
1759 goto fail_sp4;
1760
1761 xhci->scratchpad->sp_array[i] = dma;
1762 xhci->scratchpad->sp_buffers[i] = buf;
1763 }
1764
1765 return 0;
1766
1767 fail_sp4:
1768 for (i = i - 1; i >= 0; i--) {
1769 dma_free_coherent(dev, xhci->page_size,
1770 xhci->scratchpad->sp_buffers[i],
1771 xhci->scratchpad->sp_array[i]);
1772 }
1773
1774 kfree(xhci->scratchpad->sp_buffers);
1775
1776 fail_sp3:
1777 dma_free_coherent(dev, num_sp * sizeof(u64),
1778 xhci->scratchpad->sp_array,
1779 xhci->scratchpad->sp_dma);
1780
1781 fail_sp2:
1782 kfree(xhci->scratchpad);
1783 xhci->scratchpad = NULL;
1784
1785 fail_sp:
1786 return -ENOMEM;
1787 }
1788
scratchpad_free(struct xhci_hcd * xhci)1789 static void scratchpad_free(struct xhci_hcd *xhci)
1790 {
1791 int num_sp;
1792 int i;
1793 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
1794
1795 if (!xhci->scratchpad)
1796 return;
1797
1798 num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
1799
1800 for (i = 0; i < num_sp; i++) {
1801 dma_free_coherent(dev, xhci->page_size,
1802 xhci->scratchpad->sp_buffers[i],
1803 xhci->scratchpad->sp_array[i]);
1804 }
1805 kfree(xhci->scratchpad->sp_buffers);
1806 dma_free_coherent(dev, num_sp * sizeof(u64),
1807 xhci->scratchpad->sp_array,
1808 xhci->scratchpad->sp_dma);
1809 kfree(xhci->scratchpad);
1810 xhci->scratchpad = NULL;
1811 }
1812
xhci_alloc_command(struct xhci_hcd * xhci,bool allocate_completion,gfp_t mem_flags)1813 struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
1814 bool allocate_completion, gfp_t mem_flags)
1815 {
1816 struct xhci_command *command;
1817 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
1818
1819 command = kzalloc_node(sizeof(*command), mem_flags, dev_to_node(dev));
1820 if (!command)
1821 return NULL;
1822
1823 if (allocate_completion) {
1824 command->completion =
1825 kzalloc_node(sizeof(struct completion), mem_flags,
1826 dev_to_node(dev));
1827 if (!command->completion) {
1828 kfree(command);
1829 return NULL;
1830 }
1831 init_completion(command->completion);
1832 }
1833
1834 command->status = 0;
1835 INIT_LIST_HEAD(&command->cmd_list);
1836 return command;
1837 }
1838 EXPORT_SYMBOL_GPL(xhci_alloc_command);
1839
xhci_alloc_command_with_ctx(struct xhci_hcd * xhci,bool allocate_completion,gfp_t mem_flags)1840 struct xhci_command *xhci_alloc_command_with_ctx(struct xhci_hcd *xhci,
1841 bool allocate_completion, gfp_t mem_flags)
1842 {
1843 struct xhci_command *command;
1844
1845 command = xhci_alloc_command(xhci, allocate_completion, mem_flags);
1846 if (!command)
1847 return NULL;
1848
1849 command->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT,
1850 mem_flags);
1851 if (!command->in_ctx) {
1852 kfree(command->completion);
1853 kfree(command);
1854 return NULL;
1855 }
1856 return command;
1857 }
1858
xhci_urb_free_priv(struct urb_priv * urb_priv)1859 void xhci_urb_free_priv(struct urb_priv *urb_priv)
1860 {
1861 kfree(urb_priv);
1862 }
1863
xhci_free_command(struct xhci_hcd * xhci,struct xhci_command * command)1864 void xhci_free_command(struct xhci_hcd *xhci,
1865 struct xhci_command *command)
1866 {
1867 xhci_free_container_ctx(xhci,
1868 command->in_ctx);
1869 kfree(command->completion);
1870 kfree(command);
1871 }
1872 EXPORT_SYMBOL_GPL(xhci_free_command);
1873
xhci_alloc_erst(struct xhci_hcd * xhci,struct xhci_ring * evt_ring,struct xhci_erst * erst,gfp_t flags)1874 int xhci_alloc_erst(struct xhci_hcd *xhci,
1875 struct xhci_ring *evt_ring,
1876 struct xhci_erst *erst,
1877 gfp_t flags)
1878 {
1879 size_t size;
1880 unsigned int val;
1881 struct xhci_segment *seg;
1882 struct xhci_erst_entry *entry;
1883
1884 size = sizeof(struct xhci_erst_entry) * evt_ring->num_segs;
1885 erst->entries = dma_alloc_coherent(xhci_to_hcd(xhci)->self.sysdev,
1886 size, &erst->erst_dma_addr, flags);
1887 if (!erst->entries)
1888 return -ENOMEM;
1889
1890 erst->num_entries = evt_ring->num_segs;
1891
1892 seg = evt_ring->first_seg;
1893 for (val = 0; val < evt_ring->num_segs; val++) {
1894 entry = &erst->entries[val];
1895 entry->seg_addr = cpu_to_le64(seg->dma);
1896 entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
1897 entry->rsvd = 0;
1898 seg = seg->next;
1899 }
1900
1901 return 0;
1902 }
1903 EXPORT_SYMBOL_GPL(xhci_alloc_erst);
1904
xhci_free_erst(struct xhci_hcd * xhci,struct xhci_erst * erst)1905 void xhci_free_erst(struct xhci_hcd *xhci, struct xhci_erst *erst)
1906 {
1907 size_t size;
1908 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
1909
1910 size = sizeof(struct xhci_erst_entry) * (erst->num_entries);
1911 if (erst->entries)
1912 dma_free_coherent(dev, size,
1913 erst->entries,
1914 erst->erst_dma_addr);
1915 erst->entries = NULL;
1916 }
1917 EXPORT_SYMBOL_GPL(xhci_free_erst);
1918
xhci_vendor_alloc_dcbaa(struct xhci_hcd * xhci,gfp_t flags)1919 static struct xhci_device_context_array *xhci_vendor_alloc_dcbaa(
1920 struct xhci_hcd *xhci, gfp_t flags)
1921 {
1922 struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
1923
1924 if (ops && ops->alloc_dcbaa)
1925 return ops->alloc_dcbaa(xhci, flags);
1926 return 0;
1927 }
1928
xhci_vendor_free_dcbaa(struct xhci_hcd * xhci)1929 static void xhci_vendor_free_dcbaa(struct xhci_hcd *xhci)
1930 {
1931 struct xhci_vendor_ops *ops = xhci_vendor_get_ops(xhci);
1932
1933 if (ops && ops->free_dcbaa)
1934 ops->free_dcbaa(xhci);
1935 }
1936
xhci_mem_cleanup(struct xhci_hcd * xhci)1937 void xhci_mem_cleanup(struct xhci_hcd *xhci)
1938 {
1939 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
1940 int i, j, num_ports;
1941
1942 cancel_delayed_work_sync(&xhci->cmd_timer);
1943
1944 xhci_free_erst(xhci, &xhci->erst);
1945
1946 if (xhci->event_ring)
1947 xhci_ring_free(xhci, xhci->event_ring);
1948 xhci->event_ring = NULL;
1949 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed event ring");
1950
1951 if (xhci->cmd_ring)
1952 xhci_ring_free(xhci, xhci->cmd_ring);
1953 xhci->cmd_ring = NULL;
1954 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed command ring");
1955 xhci_cleanup_command_queue(xhci);
1956
1957 num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
1958 for (i = 0; i < num_ports && xhci->rh_bw; i++) {
1959 struct xhci_interval_bw_table *bwt = &xhci->rh_bw[i].bw_table;
1960 for (j = 0; j < XHCI_MAX_INTERVAL; j++) {
1961 struct list_head *ep = &bwt->interval_bw[j].endpoints;
1962 while (!list_empty(ep))
1963 list_del_init(ep->next);
1964 }
1965 }
1966
1967 for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--)
1968 xhci_free_virt_devices_depth_first(xhci, i);
1969
1970 dma_pool_destroy(xhci->segment_pool);
1971 xhci->segment_pool = NULL;
1972 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed segment pool");
1973
1974 dma_pool_destroy(xhci->device_pool);
1975 xhci->device_pool = NULL;
1976 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed device context pool");
1977
1978 dma_pool_destroy(xhci->small_streams_pool);
1979 xhci->small_streams_pool = NULL;
1980 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
1981 "Freed small stream array pool");
1982
1983 dma_pool_destroy(xhci->medium_streams_pool);
1984 xhci->medium_streams_pool = NULL;
1985 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
1986 "Freed medium stream array pool");
1987
1988 if (xhci_vendor_is_usb_offload_enabled(xhci, NULL, 0)) {
1989 xhci_vendor_free_dcbaa(xhci);
1990 } else {
1991 if (xhci->dcbaa)
1992 dma_free_coherent(dev, sizeof(*xhci->dcbaa),
1993 xhci->dcbaa, xhci->dcbaa->dma);
1994 }
1995 xhci->dcbaa = NULL;
1996
1997 scratchpad_free(xhci);
1998
1999 if (!xhci->rh_bw)
2000 goto no_bw;
2001
2002 for (i = 0; i < num_ports; i++) {
2003 struct xhci_tt_bw_info *tt, *n;
2004 list_for_each_entry_safe(tt, n, &xhci->rh_bw[i].tts, tt_list) {
2005 list_del(&tt->tt_list);
2006 kfree(tt);
2007 }
2008 }
2009
2010 no_bw:
2011 xhci->cmd_ring_reserved_trbs = 0;
2012 xhci->usb2_rhub.num_ports = 0;
2013 xhci->usb3_rhub.num_ports = 0;
2014 xhci->num_active_eps = 0;
2015 kfree(xhci->usb2_rhub.ports);
2016 kfree(xhci->usb3_rhub.ports);
2017 kfree(xhci->hw_ports);
2018 kfree(xhci->rh_bw);
2019 kfree(xhci->ext_caps);
2020 for (i = 0; i < xhci->num_port_caps; i++)
2021 kfree(xhci->port_caps[i].psi);
2022 kfree(xhci->port_caps);
2023 xhci->num_port_caps = 0;
2024
2025 xhci->usb2_rhub.ports = NULL;
2026 xhci->usb3_rhub.ports = NULL;
2027 xhci->hw_ports = NULL;
2028 xhci->rh_bw = NULL;
2029 xhci->ext_caps = NULL;
2030 xhci->port_caps = NULL;
2031
2032 xhci->page_size = 0;
2033 xhci->page_shift = 0;
2034 xhci->usb2_rhub.bus_state.bus_suspended = 0;
2035 xhci->usb3_rhub.bus_state.bus_suspended = 0;
2036 }
2037
xhci_test_trb_in_td(struct xhci_hcd * xhci,struct xhci_segment * input_seg,union xhci_trb * start_trb,union xhci_trb * end_trb,dma_addr_t input_dma,struct xhci_segment * result_seg,char * test_name,int test_number)2038 static int xhci_test_trb_in_td(struct xhci_hcd *xhci,
2039 struct xhci_segment *input_seg,
2040 union xhci_trb *start_trb,
2041 union xhci_trb *end_trb,
2042 dma_addr_t input_dma,
2043 struct xhci_segment *result_seg,
2044 char *test_name, int test_number)
2045 {
2046 unsigned long long start_dma;
2047 unsigned long long end_dma;
2048 struct xhci_segment *seg;
2049
2050 start_dma = xhci_trb_virt_to_dma(input_seg, start_trb);
2051 end_dma = xhci_trb_virt_to_dma(input_seg, end_trb);
2052
2053 seg = trb_in_td(xhci, input_seg, start_trb, end_trb, input_dma, false);
2054 if (seg != result_seg) {
2055 xhci_warn(xhci, "WARN: %s TRB math test %d failed!\n",
2056 test_name, test_number);
2057 xhci_warn(xhci, "Tested TRB math w/ seg %p and "
2058 "input DMA 0x%llx\n",
2059 input_seg,
2060 (unsigned long long) input_dma);
2061 xhci_warn(xhci, "starting TRB %p (0x%llx DMA), "
2062 "ending TRB %p (0x%llx DMA)\n",
2063 start_trb, start_dma,
2064 end_trb, end_dma);
2065 xhci_warn(xhci, "Expected seg %p, got seg %p\n",
2066 result_seg, seg);
2067 trb_in_td(xhci, input_seg, start_trb, end_trb, input_dma,
2068 true);
2069 return -1;
2070 }
2071 return 0;
2072 }
2073
2074 /* TRB math checks for xhci_trb_in_td(), using the command and event rings. */
xhci_check_trb_in_td_math(struct xhci_hcd * xhci)2075 int xhci_check_trb_in_td_math(struct xhci_hcd *xhci)
2076 {
2077 struct {
2078 dma_addr_t input_dma;
2079 struct xhci_segment *result_seg;
2080 } simple_test_vector [] = {
2081 /* A zeroed DMA field should fail */
2082 { 0, NULL },
2083 /* One TRB before the ring start should fail */
2084 { xhci->event_ring->first_seg->dma - 16, NULL },
2085 /* One byte before the ring start should fail */
2086 { xhci->event_ring->first_seg->dma - 1, NULL },
2087 /* Starting TRB should succeed */
2088 { xhci->event_ring->first_seg->dma, xhci->event_ring->first_seg },
2089 /* Ending TRB should succeed */
2090 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16,
2091 xhci->event_ring->first_seg },
2092 /* One byte after the ring end should fail */
2093 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 1)*16 + 1, NULL },
2094 /* One TRB after the ring end should fail */
2095 { xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT)*16, NULL },
2096 /* An address of all ones should fail */
2097 { (dma_addr_t) (~0), NULL },
2098 };
2099 struct {
2100 struct xhci_segment *input_seg;
2101 union xhci_trb *start_trb;
2102 union xhci_trb *end_trb;
2103 dma_addr_t input_dma;
2104 struct xhci_segment *result_seg;
2105 } complex_test_vector [] = {
2106 /* Test feeding a valid DMA address from a different ring */
2107 { .input_seg = xhci->event_ring->first_seg,
2108 .start_trb = xhci->event_ring->first_seg->trbs,
2109 .end_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
2110 .input_dma = xhci->cmd_ring->first_seg->dma,
2111 .result_seg = NULL,
2112 },
2113 /* Test feeding a valid end TRB from a different ring */
2114 { .input_seg = xhci->event_ring->first_seg,
2115 .start_trb = xhci->event_ring->first_seg->trbs,
2116 .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
2117 .input_dma = xhci->cmd_ring->first_seg->dma,
2118 .result_seg = NULL,
2119 },
2120 /* Test feeding a valid start and end TRB from a different ring */
2121 { .input_seg = xhci->event_ring->first_seg,
2122 .start_trb = xhci->cmd_ring->first_seg->trbs,
2123 .end_trb = &xhci->cmd_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
2124 .input_dma = xhci->cmd_ring->first_seg->dma,
2125 .result_seg = NULL,
2126 },
2127 /* TRB in this ring, but after this TD */
2128 { .input_seg = xhci->event_ring->first_seg,
2129 .start_trb = &xhci->event_ring->first_seg->trbs[0],
2130 .end_trb = &xhci->event_ring->first_seg->trbs[3],
2131 .input_dma = xhci->event_ring->first_seg->dma + 4*16,
2132 .result_seg = NULL,
2133 },
2134 /* TRB in this ring, but before this TD */
2135 { .input_seg = xhci->event_ring->first_seg,
2136 .start_trb = &xhci->event_ring->first_seg->trbs[3],
2137 .end_trb = &xhci->event_ring->first_seg->trbs[6],
2138 .input_dma = xhci->event_ring->first_seg->dma + 2*16,
2139 .result_seg = NULL,
2140 },
2141 /* TRB in this ring, but after this wrapped TD */
2142 { .input_seg = xhci->event_ring->first_seg,
2143 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
2144 .end_trb = &xhci->event_ring->first_seg->trbs[1],
2145 .input_dma = xhci->event_ring->first_seg->dma + 2*16,
2146 .result_seg = NULL,
2147 },
2148 /* TRB in this ring, but before this wrapped TD */
2149 { .input_seg = xhci->event_ring->first_seg,
2150 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
2151 .end_trb = &xhci->event_ring->first_seg->trbs[1],
2152 .input_dma = xhci->event_ring->first_seg->dma + (TRBS_PER_SEGMENT - 4)*16,
2153 .result_seg = NULL,
2154 },
2155 /* TRB not in this ring, and we have a wrapped TD */
2156 { .input_seg = xhci->event_ring->first_seg,
2157 .start_trb = &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 3],
2158 .end_trb = &xhci->event_ring->first_seg->trbs[1],
2159 .input_dma = xhci->cmd_ring->first_seg->dma + 2*16,
2160 .result_seg = NULL,
2161 },
2162 };
2163
2164 unsigned int num_tests;
2165 int i, ret;
2166
2167 num_tests = ARRAY_SIZE(simple_test_vector);
2168 for (i = 0; i < num_tests; i++) {
2169 ret = xhci_test_trb_in_td(xhci,
2170 xhci->event_ring->first_seg,
2171 xhci->event_ring->first_seg->trbs,
2172 &xhci->event_ring->first_seg->trbs[TRBS_PER_SEGMENT - 1],
2173 simple_test_vector[i].input_dma,
2174 simple_test_vector[i].result_seg,
2175 "Simple", i);
2176 if (ret < 0)
2177 return ret;
2178 }
2179
2180 num_tests = ARRAY_SIZE(complex_test_vector);
2181 for (i = 0; i < num_tests; i++) {
2182 ret = xhci_test_trb_in_td(xhci,
2183 complex_test_vector[i].input_seg,
2184 complex_test_vector[i].start_trb,
2185 complex_test_vector[i].end_trb,
2186 complex_test_vector[i].input_dma,
2187 complex_test_vector[i].result_seg,
2188 "Complex", i);
2189 if (ret < 0)
2190 return ret;
2191 }
2192 xhci_dbg(xhci, "TRB math tests passed.\n");
2193 return 0;
2194 }
2195 EXPORT_SYMBOL_GPL(xhci_check_trb_in_td_math);
2196
xhci_set_hc_event_deq(struct xhci_hcd * xhci)2197 static void xhci_set_hc_event_deq(struct xhci_hcd *xhci)
2198 {
2199 u64 temp;
2200 dma_addr_t deq;
2201
2202 deq = xhci_trb_virt_to_dma(xhci->event_ring->deq_seg,
2203 xhci->event_ring->dequeue);
2204 if (!deq)
2205 xhci_warn(xhci, "WARN something wrong with SW event ring "
2206 "dequeue ptr.\n");
2207 /* Update HC event ring dequeue pointer */
2208 temp = xhci_read_64(xhci, &xhci->ir_set->erst_dequeue);
2209 temp &= ERST_PTR_MASK;
2210 /* Don't clear the EHB bit (which is RW1C) because
2211 * there might be more events to service.
2212 */
2213 temp &= ~ERST_EHB;
2214 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2215 "// Write event ring dequeue pointer, "
2216 "preserving EHB bit");
2217 xhci_write_64(xhci, ((u64) deq & (u64) ~ERST_PTR_MASK) | temp,
2218 &xhci->ir_set->erst_dequeue);
2219 }
2220
xhci_add_in_port(struct xhci_hcd * xhci,unsigned int num_ports,__le32 __iomem * addr,int max_caps)2221 static void xhci_add_in_port(struct xhci_hcd *xhci, unsigned int num_ports,
2222 __le32 __iomem *addr, int max_caps)
2223 {
2224 u32 temp, port_offset, port_count;
2225 int i;
2226 u8 major_revision, minor_revision, tmp_minor_revision;
2227 struct xhci_hub *rhub;
2228 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
2229 struct xhci_port_cap *port_cap;
2230
2231 temp = readl(addr);
2232 major_revision = XHCI_EXT_PORT_MAJOR(temp);
2233 minor_revision = XHCI_EXT_PORT_MINOR(temp);
2234
2235 if (major_revision == 0x03) {
2236 rhub = &xhci->usb3_rhub;
2237 /*
2238 * Some hosts incorrectly use sub-minor version for minor
2239 * version (i.e. 0x02 instead of 0x20 for bcdUSB 0x320 and 0x01
2240 * for bcdUSB 0x310). Since there is no USB release with sub
2241 * minor version 0x301 to 0x309, we can assume that they are
2242 * incorrect and fix it here.
2243 */
2244 if (minor_revision > 0x00 && minor_revision < 0x10)
2245 minor_revision <<= 4;
2246 /*
2247 * Some zhaoxin's xHCI controller that follow usb3.1 spec
2248 * but only support Gen1.
2249 */
2250 if (xhci->quirks & XHCI_ZHAOXIN_HOST) {
2251 tmp_minor_revision = minor_revision;
2252 minor_revision = 0;
2253 }
2254
2255 } else if (major_revision <= 0x02) {
2256 rhub = &xhci->usb2_rhub;
2257 } else {
2258 xhci_warn(xhci, "Ignoring unknown port speed, "
2259 "Ext Cap %p, revision = 0x%x\n",
2260 addr, major_revision);
2261 /* Ignoring port protocol we can't understand. FIXME */
2262 return;
2263 }
2264
2265 /* Port offset and count in the third dword, see section 7.2 */
2266 temp = readl(addr + 2);
2267 port_offset = XHCI_EXT_PORT_OFF(temp);
2268 port_count = XHCI_EXT_PORT_COUNT(temp);
2269 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2270 "Ext Cap %p, port offset = %u, "
2271 "count = %u, revision = 0x%x",
2272 addr, port_offset, port_count, major_revision);
2273 /* Port count includes the current port offset */
2274 if (port_offset == 0 || (port_offset + port_count - 1) > num_ports)
2275 /* WTF? "Valid values are ‘1’ to MaxPorts" */
2276 return;
2277
2278 port_cap = &xhci->port_caps[xhci->num_port_caps++];
2279 if (xhci->num_port_caps > max_caps)
2280 return;
2281
2282 port_cap->psi_count = XHCI_EXT_PORT_PSIC(temp);
2283
2284 if (port_cap->psi_count) {
2285 port_cap->psi = kcalloc_node(port_cap->psi_count,
2286 sizeof(*port_cap->psi),
2287 GFP_KERNEL, dev_to_node(dev));
2288 if (!port_cap->psi)
2289 port_cap->psi_count = 0;
2290
2291 port_cap->psi_uid_count++;
2292 for (i = 0; i < port_cap->psi_count; i++) {
2293 port_cap->psi[i] = readl(addr + 4 + i);
2294
2295 /* count unique ID values, two consecutive entries can
2296 * have the same ID if link is assymetric
2297 */
2298 if (i && (XHCI_EXT_PORT_PSIV(port_cap->psi[i]) !=
2299 XHCI_EXT_PORT_PSIV(port_cap->psi[i - 1])))
2300 port_cap->psi_uid_count++;
2301
2302 if (xhci->quirks & XHCI_ZHAOXIN_HOST &&
2303 major_revision == 0x03 &&
2304 XHCI_EXT_PORT_PSIV(port_cap->psi[i]) >= 5)
2305 minor_revision = tmp_minor_revision;
2306
2307 xhci_dbg(xhci, "PSIV:%d PSIE:%d PLT:%d PFD:%d LP:%d PSIM:%d\n",
2308 XHCI_EXT_PORT_PSIV(port_cap->psi[i]),
2309 XHCI_EXT_PORT_PSIE(port_cap->psi[i]),
2310 XHCI_EXT_PORT_PLT(port_cap->psi[i]),
2311 XHCI_EXT_PORT_PFD(port_cap->psi[i]),
2312 XHCI_EXT_PORT_LP(port_cap->psi[i]),
2313 XHCI_EXT_PORT_PSIM(port_cap->psi[i]));
2314 }
2315 }
2316
2317 rhub->maj_rev = major_revision;
2318
2319 if (rhub->min_rev < minor_revision)
2320 rhub->min_rev = minor_revision;
2321
2322 port_cap->maj_rev = major_revision;
2323 port_cap->min_rev = minor_revision;
2324
2325 /* cache usb2 port capabilities */
2326 if (major_revision < 0x03 && xhci->num_ext_caps < max_caps)
2327 xhci->ext_caps[xhci->num_ext_caps++] = temp;
2328
2329 if ((xhci->hci_version >= 0x100) && (major_revision != 0x03) &&
2330 (temp & XHCI_HLC)) {
2331 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2332 "xHCI 1.0: support USB2 hardware lpm");
2333 xhci->hw_lpm_support = 1;
2334 }
2335
2336 port_offset--;
2337 for (i = port_offset; i < (port_offset + port_count); i++) {
2338 struct xhci_port *hw_port = &xhci->hw_ports[i];
2339 /* Duplicate entry. Ignore the port if the revisions differ. */
2340 if (hw_port->rhub) {
2341 xhci_warn(xhci, "Duplicate port entry, Ext Cap %p,"
2342 " port %u\n", addr, i);
2343 xhci_warn(xhci, "Port was marked as USB %u, "
2344 "duplicated as USB %u\n",
2345 hw_port->rhub->maj_rev, major_revision);
2346 /* Only adjust the roothub port counts if we haven't
2347 * found a similar duplicate.
2348 */
2349 if (hw_port->rhub != rhub &&
2350 hw_port->hcd_portnum != DUPLICATE_ENTRY) {
2351 hw_port->rhub->num_ports--;
2352 hw_port->hcd_portnum = DUPLICATE_ENTRY;
2353 }
2354 continue;
2355 }
2356 hw_port->rhub = rhub;
2357 hw_port->port_cap = port_cap;
2358 rhub->num_ports++;
2359 }
2360 /* FIXME: Should we disable ports not in the Extended Capabilities? */
2361 }
2362
xhci_create_rhub_port_array(struct xhci_hcd * xhci,struct xhci_hub * rhub,gfp_t flags)2363 static void xhci_create_rhub_port_array(struct xhci_hcd *xhci,
2364 struct xhci_hub *rhub, gfp_t flags)
2365 {
2366 int port_index = 0;
2367 int i;
2368 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
2369
2370 if (!rhub->num_ports)
2371 return;
2372 rhub->ports = kcalloc_node(rhub->num_ports, sizeof(*rhub->ports),
2373 flags, dev_to_node(dev));
2374 if (!rhub->ports)
2375 return;
2376
2377 for (i = 0; i < HCS_MAX_PORTS(xhci->hcs_params1); i++) {
2378 if (xhci->hw_ports[i].rhub != rhub ||
2379 xhci->hw_ports[i].hcd_portnum == DUPLICATE_ENTRY)
2380 continue;
2381 xhci->hw_ports[i].hcd_portnum = port_index;
2382 rhub->ports[port_index] = &xhci->hw_ports[i];
2383 port_index++;
2384 if (port_index == rhub->num_ports)
2385 break;
2386 }
2387 }
2388
2389 /*
2390 * Scan the Extended Capabilities for the "Supported Protocol Capabilities" that
2391 * specify what speeds each port is supposed to be. We can't count on the port
2392 * speed bits in the PORTSC register being correct until a device is connected,
2393 * but we need to set up the two fake roothubs with the correct number of USB
2394 * 3.0 and USB 2.0 ports at host controller initialization time.
2395 */
xhci_setup_port_arrays(struct xhci_hcd * xhci,gfp_t flags)2396 static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags)
2397 {
2398 void __iomem *base;
2399 u32 offset;
2400 unsigned int num_ports;
2401 int i, j;
2402 int cap_count = 0;
2403 u32 cap_start;
2404 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
2405
2406 num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
2407 xhci->hw_ports = kcalloc_node(num_ports, sizeof(*xhci->hw_ports),
2408 flags, dev_to_node(dev));
2409 if (!xhci->hw_ports)
2410 return -ENOMEM;
2411
2412 for (i = 0; i < num_ports; i++) {
2413 xhci->hw_ports[i].addr = &xhci->op_regs->port_status_base +
2414 NUM_PORT_REGS * i;
2415 xhci->hw_ports[i].hw_portnum = i;
2416 }
2417
2418 xhci->rh_bw = kcalloc_node(num_ports, sizeof(*xhci->rh_bw), flags,
2419 dev_to_node(dev));
2420 if (!xhci->rh_bw)
2421 return -ENOMEM;
2422 for (i = 0; i < num_ports; i++) {
2423 struct xhci_interval_bw_table *bw_table;
2424
2425 INIT_LIST_HEAD(&xhci->rh_bw[i].tts);
2426 bw_table = &xhci->rh_bw[i].bw_table;
2427 for (j = 0; j < XHCI_MAX_INTERVAL; j++)
2428 INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
2429 }
2430 base = &xhci->cap_regs->hc_capbase;
2431
2432 cap_start = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_PROTOCOL);
2433 if (!cap_start) {
2434 xhci_err(xhci, "No Extended Capability registers, unable to set up roothub\n");
2435 return -ENODEV;
2436 }
2437
2438 offset = cap_start;
2439 /* count extended protocol capability entries for later caching */
2440 while (offset) {
2441 cap_count++;
2442 offset = xhci_find_next_ext_cap(base, offset,
2443 XHCI_EXT_CAPS_PROTOCOL);
2444 }
2445
2446 xhci->ext_caps = kcalloc_node(cap_count, sizeof(*xhci->ext_caps),
2447 flags, dev_to_node(dev));
2448 if (!xhci->ext_caps)
2449 return -ENOMEM;
2450
2451 xhci->port_caps = kcalloc_node(cap_count, sizeof(*xhci->port_caps),
2452 flags, dev_to_node(dev));
2453 if (!xhci->port_caps)
2454 return -ENOMEM;
2455
2456 offset = cap_start;
2457
2458 while (offset) {
2459 xhci_add_in_port(xhci, num_ports, base + offset, cap_count);
2460 if (xhci->usb2_rhub.num_ports + xhci->usb3_rhub.num_ports ==
2461 num_ports)
2462 break;
2463 offset = xhci_find_next_ext_cap(base, offset,
2464 XHCI_EXT_CAPS_PROTOCOL);
2465 }
2466 if (xhci->usb2_rhub.num_ports == 0 && xhci->usb3_rhub.num_ports == 0) {
2467 xhci_warn(xhci, "No ports on the roothubs?\n");
2468 return -ENODEV;
2469 }
2470 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2471 "Found %u USB 2.0 ports and %u USB 3.0 ports.",
2472 xhci->usb2_rhub.num_ports, xhci->usb3_rhub.num_ports);
2473
2474 /* Place limits on the number of roothub ports so that the hub
2475 * descriptors aren't longer than the USB core will allocate.
2476 */
2477 if (xhci->usb3_rhub.num_ports > USB_SS_MAXPORTS) {
2478 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2479 "Limiting USB 3.0 roothub ports to %u.",
2480 USB_SS_MAXPORTS);
2481 xhci->usb3_rhub.num_ports = USB_SS_MAXPORTS;
2482 }
2483 if (xhci->usb2_rhub.num_ports > USB_MAXCHILDREN) {
2484 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2485 "Limiting USB 2.0 roothub ports to %u.",
2486 USB_MAXCHILDREN);
2487 xhci->usb2_rhub.num_ports = USB_MAXCHILDREN;
2488 }
2489
2490 if (!xhci->usb2_rhub.num_ports)
2491 xhci_info(xhci, "USB2 root hub has no ports\n");
2492
2493 if (!xhci->usb3_rhub.num_ports)
2494 xhci_info(xhci, "USB3 root hub has no ports\n");
2495
2496 xhci_create_rhub_port_array(xhci, &xhci->usb2_rhub, flags);
2497 xhci_create_rhub_port_array(xhci, &xhci->usb3_rhub, flags);
2498
2499 return 0;
2500 }
2501
xhci_mem_init(struct xhci_hcd * xhci,gfp_t flags)2502 int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
2503 {
2504 dma_addr_t dma;
2505 struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
2506 unsigned int val, val2;
2507 u64 val_64;
2508 u32 page_size, temp;
2509 int i, ret;
2510
2511 INIT_LIST_HEAD(&xhci->cmd_list);
2512
2513 /* init command timeout work */
2514 INIT_DELAYED_WORK(&xhci->cmd_timer, xhci_handle_command_timeout);
2515 init_completion(&xhci->cmd_ring_stop_completion);
2516
2517 page_size = readl(&xhci->op_regs->page_size);
2518 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2519 "Supported page size register = 0x%x", page_size);
2520 i = ffs(page_size);
2521 if (i < 16)
2522 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2523 "Supported page size of %iK", (1 << (i+12)) / 1024);
2524 else
2525 xhci_warn(xhci, "WARN: no supported page size\n");
2526 /* Use 4K pages, since that's common and the minimum the HC supports */
2527 xhci->page_shift = 12;
2528 xhci->page_size = 1 << xhci->page_shift;
2529 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2530 "HCD page size set to %iK", xhci->page_size / 1024);
2531
2532 /*
2533 * Program the Number of Device Slots Enabled field in the CONFIG
2534 * register with the max value of slots the HC can handle.
2535 */
2536 val = HCS_MAX_SLOTS(readl(&xhci->cap_regs->hcs_params1));
2537 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2538 "// xHC can handle at most %d device slots.", val);
2539 val2 = readl(&xhci->op_regs->config_reg);
2540 val |= (val2 & ~HCS_SLOTS_MASK);
2541 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2542 "// Setting Max device slots reg = 0x%x.", val);
2543 writel(val, &xhci->op_regs->config_reg);
2544
2545 /*
2546 * xHCI section 5.4.6 - Device Context array must be
2547 * "physically contiguous and 64-byte (cache line) aligned".
2548 */
2549 if (xhci_vendor_is_usb_offload_enabled(xhci, NULL, 0)) {
2550 xhci->dcbaa = xhci_vendor_alloc_dcbaa(xhci, flags);
2551 if (!xhci->dcbaa)
2552 goto fail;
2553 } else {
2554 xhci->dcbaa = dma_alloc_coherent(dev, sizeof(*xhci->dcbaa), &dma,
2555 flags);
2556 if (!xhci->dcbaa)
2557 goto fail;
2558 xhci->dcbaa->dma = dma;
2559 }
2560 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2561 "// Device context base array address = 0x%llx (DMA), %p (virt)",
2562 (unsigned long long)xhci->dcbaa->dma, xhci->dcbaa);
2563 xhci_write_64(xhci, xhci->dcbaa->dma, &xhci->op_regs->dcbaa_ptr);
2564
2565 /*
2566 * Initialize the ring segment pool. The ring must be a contiguous
2567 * structure comprised of TRBs. The TRBs must be 16 byte aligned,
2568 * however, the command ring segment needs 64-byte aligned segments
2569 * and our use of dma addresses in the trb_address_map radix tree needs
2570 * TRB_SEGMENT_SIZE alignment, so we pick the greater alignment need.
2571 */
2572 if (xhci->quirks & XHCI_ZHAOXIN_TRB_FETCH)
2573 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
2574 TRB_SEGMENT_SIZE * 2, TRB_SEGMENT_SIZE * 2, xhci->page_size * 2);
2575 else
2576 xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
2577 TRB_SEGMENT_SIZE, TRB_SEGMENT_SIZE, xhci->page_size);
2578
2579 /* See Table 46 and Note on Figure 55 */
2580 xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
2581 2112, 64, xhci->page_size);
2582 if (!xhci->segment_pool || !xhci->device_pool)
2583 goto fail;
2584
2585 /* Linear stream context arrays don't have any boundary restrictions,
2586 * and only need to be 16-byte aligned.
2587 */
2588 xhci->small_streams_pool =
2589 dma_pool_create("xHCI 256 byte stream ctx arrays",
2590 dev, SMALL_STREAM_ARRAY_SIZE, 16, 0);
2591 xhci->medium_streams_pool =
2592 dma_pool_create("xHCI 1KB stream ctx arrays",
2593 dev, MEDIUM_STREAM_ARRAY_SIZE, 16, 0);
2594 /* Any stream context array bigger than MEDIUM_STREAM_ARRAY_SIZE
2595 * will be allocated with dma_alloc_coherent()
2596 */
2597
2598 if (!xhci->small_streams_pool || !xhci->medium_streams_pool)
2599 goto fail;
2600
2601 /* Set up the command ring to have one segments for now. */
2602 xhci->cmd_ring = xhci_ring_alloc(xhci, 1, 1, TYPE_COMMAND, 0, flags);
2603 if (!xhci->cmd_ring)
2604 goto fail;
2605 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2606 "Allocated command ring at %p", xhci->cmd_ring);
2607 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "First segment DMA is 0x%llx",
2608 (unsigned long long)xhci->cmd_ring->first_seg->dma);
2609
2610 /* Set the address in the Command Ring Control register */
2611 val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
2612 val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
2613 (xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) |
2614 xhci->cmd_ring->cycle_state;
2615 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2616 "// Setting command ring address to 0x%016llx", val_64);
2617 xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
2618
2619 /* Reserve one command ring TRB for disabling LPM.
2620 * Since the USB core grabs the shared usb_bus bandwidth mutex before
2621 * disabling LPM, we only need to reserve one TRB for all devices.
2622 */
2623 xhci->cmd_ring_reserved_trbs++;
2624
2625 val = readl(&xhci->cap_regs->db_off);
2626 val &= DBOFF_MASK;
2627 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2628 "// Doorbell array is located at offset 0x%x"
2629 " from cap regs base addr", val);
2630 xhci->dba = (void __iomem *) xhci->cap_regs + val;
2631 /* Set ir_set to interrupt register set 0 */
2632 xhci->ir_set = &xhci->run_regs->ir_set[0];
2633
2634 /*
2635 * Event ring setup: Allocate a normal ring, but also setup
2636 * the event ring segment table (ERST). Section 4.9.3.
2637 */
2638 xhci_dbg_trace(xhci, trace_xhci_dbg_init, "// Allocating event ring");
2639 xhci->event_ring = xhci_ring_alloc(xhci, ERST_NUM_SEGS, 1, TYPE_EVENT,
2640 0, flags);
2641 if (!xhci->event_ring)
2642 goto fail;
2643 if (xhci_check_trb_in_td_math(xhci) < 0)
2644 goto fail;
2645
2646 ret = xhci_alloc_erst(xhci, xhci->event_ring, &xhci->erst, flags);
2647 if (ret)
2648 goto fail;
2649
2650 /* set ERST count with the number of entries in the segment table */
2651 val = readl(&xhci->ir_set->erst_size);
2652 val &= ERST_SIZE_MASK;
2653 val |= ERST_NUM_SEGS;
2654 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2655 "// Write ERST size = %i to ir_set 0 (some bits preserved)",
2656 val);
2657 writel(val, &xhci->ir_set->erst_size);
2658
2659 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2660 "// Set ERST entries to point to event ring.");
2661 /* set the segment table base address */
2662 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2663 "// Set ERST base address for ir_set 0 = 0x%llx",
2664 (unsigned long long)xhci->erst.erst_dma_addr);
2665 val_64 = xhci_read_64(xhci, &xhci->ir_set->erst_base);
2666 val_64 &= ERST_PTR_MASK;
2667 val_64 |= (xhci->erst.erst_dma_addr & (u64) ~ERST_PTR_MASK);
2668 xhci_write_64(xhci, val_64, &xhci->ir_set->erst_base);
2669
2670 /* Set the event ring dequeue address */
2671 xhci_set_hc_event_deq(xhci);
2672 xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2673 "Wrote ERST address to ir_set 0.");
2674
2675 xhci->isoc_bei_interval = AVOID_BEI_INTERVAL_MAX;
2676
2677 /*
2678 * XXX: Might need to set the Interrupter Moderation Register to
2679 * something other than the default (~1ms minimum between interrupts).
2680 * See section 5.5.1.2.
2681 */
2682 for (i = 0; i < MAX_HC_SLOTS; i++)
2683 xhci->devs[i] = NULL;
2684 for (i = 0; i < USB_MAXCHILDREN; i++) {
2685 xhci->usb2_rhub.bus_state.resume_done[i] = 0;
2686 xhci->usb3_rhub.bus_state.resume_done[i] = 0;
2687 /* Only the USB 2.0 completions will ever be used. */
2688 init_completion(&xhci->usb2_rhub.bus_state.rexit_done[i]);
2689 init_completion(&xhci->usb3_rhub.bus_state.u3exit_done[i]);
2690 }
2691
2692 if (scratchpad_alloc(xhci, flags))
2693 goto fail;
2694 if (xhci_setup_port_arrays(xhci, flags))
2695 goto fail;
2696
2697 /* Enable USB 3.0 device notifications for function remote wake, which
2698 * is necessary for allowing USB 3.0 devices to do remote wakeup from
2699 * U3 (device suspend).
2700 */
2701 temp = readl(&xhci->op_regs->dev_notification);
2702 temp &= ~DEV_NOTE_MASK;
2703 temp |= DEV_NOTE_FWAKE;
2704 writel(temp, &xhci->op_regs->dev_notification);
2705
2706 return 0;
2707
2708 fail:
2709 xhci_halt(xhci);
2710 xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
2711 xhci_mem_cleanup(xhci);
2712 return -ENOMEM;
2713 }
2714