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