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