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