xref: /linux/drivers/usb/host/xhci-mem.c (revision 3a39d672e7f48b8d6b91a09afa4b55352773b4b5)
15fd54aceSGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0
266d4eaddSSarah Sharp /*
366d4eaddSSarah Sharp  * xHCI host controller driver
466d4eaddSSarah Sharp  *
566d4eaddSSarah Sharp  * Copyright (C) 2008 Intel Corp.
666d4eaddSSarah Sharp  *
766d4eaddSSarah Sharp  * Author: Sarah Sharp
866d4eaddSSarah Sharp  * Some code borrowed from the Linux EHCI driver.
966d4eaddSSarah Sharp  */
1066d4eaddSSarah Sharp 
1166d4eaddSSarah Sharp #include <linux/usb.h>
1234728498SAndy Shevchenko #include <linux/overflow.h>
130ebbab37SSarah Sharp #include <linux/pci.h>
145a0e3ad6STejun Heo #include <linux/slab.h>
15527c6d7fSSarah Sharp #include <linux/dmapool.h>
16008eb957SJames Hogan #include <linux/dma-mapping.h>
1766d4eaddSSarah Sharp 
1866d4eaddSSarah Sharp #include "xhci.h"
193a7fa5beSXenia Ragiadakou #include "xhci-trace.h"
2002b6fdc2SLu Baolu #include "xhci-debugfs.h"
2166d4eaddSSarah Sharp 
220ebbab37SSarah Sharp /*
230ebbab37SSarah Sharp  * Allocates a generic ring segment from the ring pool, sets the dma address,
240ebbab37SSarah Sharp  * initializes the segment to zero, and sets the private next pointer to NULL.
250ebbab37SSarah Sharp  *
260ebbab37SSarah Sharp  * Section 4.11.1.1:
270ebbab37SSarah Sharp  * "All components of all Command and Transfer TRBs shall be initialized to '0'"
280ebbab37SSarah Sharp  */
xhci_segment_alloc(struct xhci_hcd * xhci,unsigned int cycle_state,unsigned int max_packet,unsigned int num,gfp_t flags)29186a7ef1SAndiry Xu static struct xhci_segment *xhci_segment_alloc(struct xhci_hcd *xhci,
30f9c589e1SMathias Nyman 					       unsigned int cycle_state,
31f9c589e1SMathias Nyman 					       unsigned int max_packet,
32044818a6SLukas Wunner 					       unsigned int num,
33f9c589e1SMathias Nyman 					       gfp_t flags)
340ebbab37SSarah Sharp {
350ebbab37SSarah Sharp 	struct xhci_segment *seg;
360ebbab37SSarah Sharp 	dma_addr_t	dma;
37186a7ef1SAndiry Xu 	int		i;
38a965315eSAdam Wallis 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
390ebbab37SSarah Sharp 
40a965315eSAdam Wallis 	seg = kzalloc_node(sizeof(*seg), flags, dev_to_node(dev));
410ebbab37SSarah Sharp 	if (!seg)
42326b4810SRandy Dunlap 		return NULL;
430ebbab37SSarah Sharp 
4484c1eeb0SSaurabh Sengar 	seg->trbs = dma_pool_zalloc(xhci->segment_pool, flags, &dma);
450ebbab37SSarah Sharp 	if (!seg->trbs) {
460ebbab37SSarah Sharp 		kfree(seg);
47326b4810SRandy Dunlap 		return NULL;
480ebbab37SSarah Sharp 	}
490ebbab37SSarah Sharp 
50f9c589e1SMathias Nyman 	if (max_packet) {
51a965315eSAdam Wallis 		seg->bounce_buf = kzalloc_node(max_packet, flags,
52a965315eSAdam Wallis 					dev_to_node(dev));
53f9c589e1SMathias Nyman 		if (!seg->bounce_buf) {
54f9c589e1SMathias Nyman 			dma_pool_free(xhci->segment_pool, seg->trbs, dma);
55f9c589e1SMathias Nyman 			kfree(seg);
56f9c589e1SMathias Nyman 			return NULL;
57f9c589e1SMathias Nyman 		}
58f9c589e1SMathias Nyman 	}
59186a7ef1SAndiry Xu 	/* If the cycle state is 0, set the cycle bit to 1 for all the TRBs */
60186a7ef1SAndiry Xu 	if (cycle_state == 0) {
61186a7ef1SAndiry Xu 		for (i = 0; i < TRBS_PER_SEGMENT; i++)
62f9aeda81SOliver Neukum 			seg->trbs[i].link.control = cpu_to_le32(TRB_CYCLE);
63186a7ef1SAndiry Xu 	}
64044818a6SLukas Wunner 	seg->num = num;
650ebbab37SSarah Sharp 	seg->dma = dma;
660ebbab37SSarah Sharp 	seg->next = NULL;
670ebbab37SSarah Sharp 
680ebbab37SSarah Sharp 	return seg;
690ebbab37SSarah Sharp }
700ebbab37SSarah Sharp 
xhci_segment_free(struct xhci_hcd * xhci,struct xhci_segment * seg)710ebbab37SSarah Sharp static void xhci_segment_free(struct xhci_hcd *xhci, struct xhci_segment *seg)
720ebbab37SSarah Sharp {
730ebbab37SSarah Sharp 	if (seg->trbs) {
740ebbab37SSarah Sharp 		dma_pool_free(xhci->segment_pool, seg->trbs, seg->dma);
750ebbab37SSarah Sharp 		seg->trbs = NULL;
760ebbab37SSarah Sharp 	}
77f9c589e1SMathias Nyman 	kfree(seg->bounce_buf);
780ebbab37SSarah Sharp 	kfree(seg);
790ebbab37SSarah Sharp }
800ebbab37SSarah Sharp 
xhci_free_segments_for_ring(struct xhci_hcd * xhci,struct xhci_segment * first)8170d43601SAndiry Xu static void xhci_free_segments_for_ring(struct xhci_hcd *xhci,
8270d43601SAndiry Xu 				struct xhci_segment *first)
8370d43601SAndiry Xu {
8470d43601SAndiry Xu 	struct xhci_segment *seg;
8570d43601SAndiry Xu 
8670d43601SAndiry Xu 	seg = first->next;
872e8dd2deSNiklas Neronin 	while (seg && seg != first) {
8870d43601SAndiry Xu 		struct xhci_segment *next = seg->next;
8970d43601SAndiry Xu 		xhci_segment_free(xhci, seg);
9070d43601SAndiry Xu 		seg = next;
9170d43601SAndiry Xu 	}
9270d43601SAndiry Xu 	xhci_segment_free(xhci, first);
9370d43601SAndiry Xu }
9470d43601SAndiry Xu 
950ebbab37SSarah Sharp /*
960ebbab37SSarah Sharp  * Make the prev segment point to the next segment.
970ebbab37SSarah Sharp  *
980ebbab37SSarah Sharp  * Change the last TRB in the prev segment to be a Link TRB which points to the
990ebbab37SSarah Sharp  * DMA address of the next segment.  The caller needs to set any Link TRB
1000ebbab37SSarah Sharp  * related flags, such as End TRB, Toggle Cycle, and no snoop.
1010ebbab37SSarah Sharp  */
xhci_link_segments(struct xhci_segment * prev,struct xhci_segment * next,enum xhci_ring_type type,bool chain_links)102e3bc8004SMathias Nyman static void xhci_link_segments(struct xhci_segment *prev,
103e3bc8004SMathias Nyman 			       struct xhci_segment *next,
104e3bc8004SMathias Nyman 			       enum xhci_ring_type type, bool chain_links)
1050ebbab37SSarah Sharp {
1060ebbab37SSarah Sharp 	u32 val;
1070ebbab37SSarah Sharp 
1080ebbab37SSarah Sharp 	if (!prev || !next)
1090ebbab37SSarah Sharp 		return;
1100ebbab37SSarah Sharp 	prev->next = next;
1113b72fca0SAndiry Xu 	if (type != TYPE_EVENT) {
112f5960b69SMatt Evans 		prev->trbs[TRBS_PER_SEGMENT-1].link.segment_ptr =
113f5960b69SMatt Evans 			cpu_to_le64(next->dma);
1140ebbab37SSarah Sharp 
1150ebbab37SSarah Sharp 		/* Set the last TRB in the segment to have a TRB type ID of Link TRB */
11628ccd296SMatt Evans 		val = le32_to_cpu(prev->trbs[TRBS_PER_SEGMENT-1].link.control);
1170ebbab37SSarah Sharp 		val &= ~TRB_TYPE_BITMASK;
1180ebbab37SSarah Sharp 		val |= TRB_TYPE(TRB_LINK);
119e3bc8004SMathias Nyman 		if (chain_links)
120b0567b3fSSarah Sharp 			val |= TRB_CHAIN;
12128ccd296SMatt Evans 		prev->trbs[TRBS_PER_SEGMENT-1].link.control = cpu_to_le32(val);
1220ebbab37SSarah Sharp 	}
1230ebbab37SSarah Sharp }
1240ebbab37SSarah Sharp 
1258dfec614SAndiry Xu /*
1268dfec614SAndiry Xu  * Link the ring to the new segments.
1278dfec614SAndiry Xu  * Set Toggle Cycle for the new ring if needed.
1288dfec614SAndiry Xu  */
xhci_link_rings(struct xhci_hcd * xhci,struct xhci_ring * ring,struct xhci_segment * first,struct xhci_segment * last,unsigned int num_segs)1298dfec614SAndiry Xu static void xhci_link_rings(struct xhci_hcd *xhci, struct xhci_ring *ring,
1308dfec614SAndiry Xu 		struct xhci_segment *first, struct xhci_segment *last,
1318dfec614SAndiry Xu 		unsigned int num_segs)
1328dfec614SAndiry Xu {
13335899f58SLukas Wunner 	struct xhci_segment *next, *seg;
134e3bc8004SMathias Nyman 	bool chain_links;
1358dfec614SAndiry Xu 
1368dfec614SAndiry Xu 	if (!ring || !first || !last)
1378dfec614SAndiry Xu 		return;
1388dfec614SAndiry Xu 
1397476a221SNiklas Neronin 	chain_links = xhci_link_chain_quirk(xhci, ring->type);
140e3bc8004SMathias Nyman 
1418dfec614SAndiry Xu 	next = ring->enq_seg->next;
142e3bc8004SMathias Nyman 	xhci_link_segments(ring->enq_seg, first, ring->type, chain_links);
143e3bc8004SMathias Nyman 	xhci_link_segments(last, next, ring->type, chain_links);
1448dfec614SAndiry Xu 	ring->num_segs += num_segs;
1458dfec614SAndiry Xu 
14667ab841aSLukas Wunner 	if (ring->enq_seg == ring->last_seg) {
14767ab841aSLukas Wunner 		if (ring->type != TYPE_EVENT) {
1488dfec614SAndiry Xu 			ring->last_seg->trbs[TRBS_PER_SEGMENT-1].link.control
1498dfec614SAndiry Xu 				&= ~cpu_to_le32(LINK_TOGGLE);
1508dfec614SAndiry Xu 			last->trbs[TRBS_PER_SEGMENT-1].link.control
1518dfec614SAndiry Xu 				|= cpu_to_le32(LINK_TOGGLE);
15267ab841aSLukas Wunner 		}
1538dfec614SAndiry Xu 		ring->last_seg = last;
1548dfec614SAndiry Xu 	}
15535899f58SLukas Wunner 
156bbdd82c7SNiklas Neronin 	for (seg = ring->enq_seg; seg != ring->last_seg; seg = seg->next)
15735899f58SLukas Wunner 		seg->next->num = seg->num + 1;
1588dfec614SAndiry Xu }
1598dfec614SAndiry Xu 
16015341303SGerd Hoffmann /*
16115341303SGerd Hoffmann  * We need a radix tree for mapping physical addresses of TRBs to which stream
16215341303SGerd Hoffmann  * ID they belong to.  We need to do this because the host controller won't tell
16315341303SGerd Hoffmann  * us which stream ring the TRB came from.  We could store the stream ID in an
16415341303SGerd Hoffmann  * event data TRB, but that doesn't help us for the cancellation case, since the
16515341303SGerd Hoffmann  * endpoint may stop before it reaches that event data TRB.
16615341303SGerd Hoffmann  *
16715341303SGerd Hoffmann  * The radix tree maps the upper portion of the TRB DMA address to a ring
16815341303SGerd Hoffmann  * segment that has the same upper portion of DMA addresses.  For example, say I
16984c1e40fSHans de Goede  * have segments of size 1KB, that are always 1KB aligned.  A segment may
17015341303SGerd Hoffmann  * start at 0x10c91000 and end at 0x10c913f0.  If I use the upper 10 bits, the
17115341303SGerd Hoffmann  * key to the stream ID is 0x43244.  I can use the DMA address of the TRB to
17215341303SGerd Hoffmann  * pass the radix tree a key to get the right stream ID:
17315341303SGerd Hoffmann  *
17415341303SGerd Hoffmann  *	0x10c90fff >> 10 = 0x43243
17515341303SGerd Hoffmann  *	0x10c912c0 >> 10 = 0x43244
17615341303SGerd Hoffmann  *	0x10c91400 >> 10 = 0x43245
17715341303SGerd Hoffmann  *
17815341303SGerd Hoffmann  * Obviously, only those TRBs with DMA addresses that are within the segment
17915341303SGerd Hoffmann  * will make the radix tree return the stream ID for that ring.
18015341303SGerd Hoffmann  *
18115341303SGerd Hoffmann  * Caveats for the radix tree:
18215341303SGerd Hoffmann  *
18315341303SGerd Hoffmann  * The radix tree uses an unsigned long as a key pair.  On 32-bit systems, an
18415341303SGerd Hoffmann  * unsigned long will be 32-bits; on a 64-bit system an unsigned long will be
18515341303SGerd Hoffmann  * 64-bits.  Since we only request 32-bit DMA addresses, we can use that as the
18615341303SGerd Hoffmann  * key on 32-bit or 64-bit systems (it would also be fine if we asked for 64-bit
18715341303SGerd Hoffmann  * PCI DMA addresses on a 64-bit system).  There might be a problem on 32-bit
18815341303SGerd Hoffmann  * extended systems (where the DMA address can be bigger than 32-bits),
18915341303SGerd Hoffmann  * if we allow the PCI dma mask to be bigger than 32-bits.  So don't do that.
19015341303SGerd Hoffmann  */
xhci_insert_segment_mapping(struct radix_tree_root * trb_address_map,struct xhci_ring * ring,struct xhci_segment * seg,gfp_t mem_flags)191d5734223SSarah Sharp static int xhci_insert_segment_mapping(struct radix_tree_root *trb_address_map,
192d5734223SSarah Sharp 		struct xhci_ring *ring,
193d5734223SSarah Sharp 		struct xhci_segment *seg,
194d5734223SSarah Sharp 		gfp_t mem_flags)
19515341303SGerd Hoffmann {
19615341303SGerd Hoffmann 	unsigned long key;
19715341303SGerd Hoffmann 	int ret;
19815341303SGerd Hoffmann 
19915341303SGerd Hoffmann 	key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
20015341303SGerd Hoffmann 	/* Skip any segments that were already added. */
201d5734223SSarah Sharp 	if (radix_tree_lookup(trb_address_map, key))
202d5734223SSarah Sharp 		return 0;
20315341303SGerd Hoffmann 
20415341303SGerd Hoffmann 	ret = radix_tree_maybe_preload(mem_flags);
20515341303SGerd Hoffmann 	if (ret)
20615341303SGerd Hoffmann 		return ret;
207d5734223SSarah Sharp 	ret = radix_tree_insert(trb_address_map,
20815341303SGerd Hoffmann 			key, ring);
20915341303SGerd Hoffmann 	radix_tree_preload_end();
21015341303SGerd Hoffmann 	return ret;
211d5734223SSarah Sharp }
212d5734223SSarah Sharp 
xhci_remove_segment_mapping(struct radix_tree_root * trb_address_map,struct xhci_segment * seg)213d5734223SSarah Sharp static void xhci_remove_segment_mapping(struct radix_tree_root *trb_address_map,
214d5734223SSarah Sharp 		struct xhci_segment *seg)
215d5734223SSarah Sharp {
216d5734223SSarah Sharp 	unsigned long key;
217d5734223SSarah Sharp 
218d5734223SSarah Sharp 	key = (unsigned long)(seg->dma >> TRB_SEGMENT_SHIFT);
219d5734223SSarah Sharp 	if (radix_tree_lookup(trb_address_map, key))
220d5734223SSarah Sharp 		radix_tree_delete(trb_address_map, key);
221d5734223SSarah Sharp }
222d5734223SSarah Sharp 
xhci_update_stream_segment_mapping(struct radix_tree_root * trb_address_map,struct xhci_ring * ring,struct xhci_segment * first_seg,struct xhci_segment * last_seg,gfp_t mem_flags)223d5734223SSarah Sharp static int xhci_update_stream_segment_mapping(
224d5734223SSarah Sharp 		struct radix_tree_root *trb_address_map,
225d5734223SSarah Sharp 		struct xhci_ring *ring,
226d5734223SSarah Sharp 		struct xhci_segment *first_seg,
227d5734223SSarah Sharp 		struct xhci_segment *last_seg,
228d5734223SSarah Sharp 		gfp_t mem_flags)
229d5734223SSarah Sharp {
230d5734223SSarah Sharp 	struct xhci_segment *seg;
231d5734223SSarah Sharp 	struct xhci_segment *failed_seg;
232d5734223SSarah Sharp 	int ret;
233d5734223SSarah Sharp 
234d5734223SSarah Sharp 	if (WARN_ON_ONCE(trb_address_map == NULL))
235d5734223SSarah Sharp 		return 0;
236d5734223SSarah Sharp 
237d5734223SSarah Sharp 	seg = first_seg;
238d5734223SSarah Sharp 	do {
239d5734223SSarah Sharp 		ret = xhci_insert_segment_mapping(trb_address_map,
240d5734223SSarah Sharp 				ring, seg, mem_flags);
241d5734223SSarah Sharp 		if (ret)
242d5734223SSarah Sharp 			goto remove_streams;
243d5734223SSarah Sharp 		if (seg == last_seg)
244d5734223SSarah Sharp 			return 0;
24515341303SGerd Hoffmann 		seg = seg->next;
246d5734223SSarah Sharp 	} while (seg != first_seg);
24715341303SGerd Hoffmann 
24815341303SGerd Hoffmann 	return 0;
249d5734223SSarah Sharp 
250d5734223SSarah Sharp remove_streams:
251d5734223SSarah Sharp 	failed_seg = seg;
252d5734223SSarah Sharp 	seg = first_seg;
253d5734223SSarah Sharp 	do {
254d5734223SSarah Sharp 		xhci_remove_segment_mapping(trb_address_map, seg);
255d5734223SSarah Sharp 		if (seg == failed_seg)
256d5734223SSarah Sharp 			return ret;
257d5734223SSarah Sharp 		seg = seg->next;
258d5734223SSarah Sharp 	} while (seg != first_seg);
259d5734223SSarah Sharp 
260d5734223SSarah Sharp 	return ret;
26115341303SGerd Hoffmann }
26215341303SGerd Hoffmann 
xhci_remove_stream_mapping(struct xhci_ring * ring)26315341303SGerd Hoffmann static void xhci_remove_stream_mapping(struct xhci_ring *ring)
26415341303SGerd Hoffmann {
26515341303SGerd Hoffmann 	struct xhci_segment *seg;
26615341303SGerd Hoffmann 
26715341303SGerd Hoffmann 	if (WARN_ON_ONCE(ring->trb_address_map == NULL))
26815341303SGerd Hoffmann 		return;
26915341303SGerd Hoffmann 
27015341303SGerd Hoffmann 	seg = ring->first_seg;
27115341303SGerd Hoffmann 	do {
272d5734223SSarah Sharp 		xhci_remove_segment_mapping(ring->trb_address_map, seg);
27315341303SGerd Hoffmann 		seg = seg->next;
27415341303SGerd Hoffmann 	} while (seg != ring->first_seg);
27515341303SGerd Hoffmann }
27615341303SGerd Hoffmann 
xhci_update_stream_mapping(struct xhci_ring * ring,gfp_t mem_flags)277d5734223SSarah Sharp static int xhci_update_stream_mapping(struct xhci_ring *ring, gfp_t mem_flags)
278d5734223SSarah Sharp {
279d5734223SSarah Sharp 	return xhci_update_stream_segment_mapping(ring->trb_address_map, ring,
280d5734223SSarah Sharp 			ring->first_seg, ring->last_seg, mem_flags);
281d5734223SSarah Sharp }
282d5734223SSarah Sharp 
2830ebbab37SSarah Sharp /* XXX: Do we need the hcd structure in all these functions? */
xhci_ring_free(struct xhci_hcd * xhci,struct xhci_ring * ring)284f94e0186SSarah Sharp void xhci_ring_free(struct xhci_hcd *xhci, struct xhci_ring *ring)
2850ebbab37SSarah Sharp {
2860e6c7f74SKautuk Consul 	if (!ring)
2870ebbab37SSarah Sharp 		return;
28870d43601SAndiry Xu 
289b2d6edbbSLu Baolu 	trace_xhci_ring_free(ring);
290b2d6edbbSLu Baolu 
29115341303SGerd Hoffmann 	if (ring->first_seg) {
29215341303SGerd Hoffmann 		if (ring->type == TYPE_STREAM)
29315341303SGerd Hoffmann 			xhci_remove_stream_mapping(ring);
29470d43601SAndiry Xu 		xhci_free_segments_for_ring(xhci, ring->first_seg);
29515341303SGerd Hoffmann 	}
29670d43601SAndiry Xu 
2970ebbab37SSarah Sharp 	kfree(ring);
2980ebbab37SSarah Sharp }
2990ebbab37SSarah Sharp 
xhci_initialize_ring_info(struct xhci_ring * ring,unsigned int cycle_state)300ac286428SMathias Nyman void xhci_initialize_ring_info(struct xhci_ring *ring,
301186a7ef1SAndiry Xu 			       unsigned int cycle_state)
30274f9fe21SSarah Sharp {
30374f9fe21SSarah Sharp 	/* The ring is empty, so the enqueue pointer == dequeue pointer */
30474f9fe21SSarah Sharp 	ring->enqueue = ring->first_seg->trbs;
30574f9fe21SSarah Sharp 	ring->enq_seg = ring->first_seg;
30674f9fe21SSarah Sharp 	ring->dequeue = ring->enqueue;
30774f9fe21SSarah Sharp 	ring->deq_seg = ring->first_seg;
30874f9fe21SSarah Sharp 	/* The ring is initialized to 0. The producer must write 1 to the cycle
30974f9fe21SSarah Sharp 	 * bit to handover ownership of the TRB, so PCS = 1.  The consumer must
31074f9fe21SSarah Sharp 	 * compare CCS to the cycle bit to check ownership, so CCS = 1.
311186a7ef1SAndiry Xu 	 *
312186a7ef1SAndiry Xu 	 * New rings are initialized with cycle state equal to 1; if we are
313186a7ef1SAndiry Xu 	 * handling ring expansion, set the cycle state equal to the old ring.
31474f9fe21SSarah Sharp 	 */
315186a7ef1SAndiry Xu 	ring->cycle_state = cycle_state;
316b008df60SAndiry Xu 
317b008df60SAndiry Xu 	/*
318b008df60SAndiry Xu 	 * Each segment has a link TRB, and leave an extra TRB for SW
319b008df60SAndiry Xu 	 * accounting purpose
320b008df60SAndiry Xu 	 */
321b008df60SAndiry Xu 	ring->num_trbs_free = ring->num_segs * (TRBS_PER_SEGMENT - 1) - 1;
32274f9fe21SSarah Sharp }
323c99b38c4SMathias Nyman EXPORT_SYMBOL_GPL(xhci_initialize_ring_info);
32474f9fe21SSarah Sharp 
32570d43601SAndiry Xu /* Allocate segments and link them for a ring */
xhci_alloc_segments_for_ring(struct xhci_hcd * xhci,struct xhci_segment ** first,struct xhci_segment ** last,unsigned int num_segs,unsigned int cycle_state,enum xhci_ring_type type,unsigned int max_packet,gfp_t flags)32670d43601SAndiry Xu static int xhci_alloc_segments_for_ring(struct xhci_hcd *xhci,
327bbdd82c7SNiklas Neronin 					struct xhci_segment **first,
328bbdd82c7SNiklas Neronin 					struct xhci_segment **last,
329bbdd82c7SNiklas Neronin 					unsigned int num_segs,
330bbdd82c7SNiklas Neronin 					unsigned int cycle_state,
331bbdd82c7SNiklas Neronin 					enum xhci_ring_type type,
332bbdd82c7SNiklas Neronin 					unsigned int max_packet,
333bbdd82c7SNiklas Neronin 					gfp_t flags)
33470d43601SAndiry Xu {
33570d43601SAndiry Xu 	struct xhci_segment *prev;
336bbdd82c7SNiklas Neronin 	unsigned int num = 0;
337e3bc8004SMathias Nyman 	bool chain_links;
338e3bc8004SMathias Nyman 
3397476a221SNiklas Neronin 	chain_links = xhci_link_chain_quirk(xhci, type);
34070d43601SAndiry Xu 
341044818a6SLukas Wunner 	prev = xhci_segment_alloc(xhci, cycle_state, max_packet, num, flags);
34270d43601SAndiry Xu 	if (!prev)
34370d43601SAndiry Xu 		return -ENOMEM;
344044818a6SLukas Wunner 	num++;
34570d43601SAndiry Xu 
34670d43601SAndiry Xu 	*first = prev;
347044818a6SLukas Wunner 	while (num < num_segs) {
34870d43601SAndiry Xu 		struct xhci_segment	*next;
34970d43601SAndiry Xu 
350044818a6SLukas Wunner 		next = xhci_segment_alloc(xhci, cycle_state, max_packet, num,
351044818a6SLukas Wunner 					  flags);
3522e8dd2deSNiklas Neronin 		if (!next)
3532e8dd2deSNiklas Neronin 			goto free_segments;
35470d43601SAndiry Xu 
3552e8dd2deSNiklas Neronin 		xhci_link_segments(prev, next, type, chain_links);
35670d43601SAndiry Xu 		prev = next;
357044818a6SLukas Wunner 		num++;
35870d43601SAndiry Xu 	}
359e3bc8004SMathias Nyman 	xhci_link_segments(prev, *first, type, chain_links);
36070d43601SAndiry Xu 	*last = prev;
36170d43601SAndiry Xu 
36270d43601SAndiry Xu 	return 0;
3632e8dd2deSNiklas Neronin 
3642e8dd2deSNiklas Neronin free_segments:
3652e8dd2deSNiklas Neronin 	xhci_free_segments_for_ring(xhci, *first);
3662e8dd2deSNiklas Neronin 	return -ENOMEM;
36770d43601SAndiry Xu }
36870d43601SAndiry Xu 
369c3fa4e04SLee Jones /*
3700ebbab37SSarah Sharp  * Create a new ring with zero or more segments.
3710ebbab37SSarah Sharp  *
3720ebbab37SSarah Sharp  * Link each segment together into a ring.
3730ebbab37SSarah Sharp  * Set the end flag and the cycle toggle bit on the last segment.
3740ebbab37SSarah Sharp  * See section 4.9.1 and figures 15 and 16.
3750ebbab37SSarah Sharp  */
xhci_ring_alloc(struct xhci_hcd * xhci,unsigned int num_segs,unsigned int cycle_state,enum xhci_ring_type type,unsigned int max_packet,gfp_t flags)37667d2ea9fSLu Baolu struct xhci_ring *xhci_ring_alloc(struct xhci_hcd *xhci,
377186a7ef1SAndiry Xu 		unsigned int num_segs, unsigned int cycle_state,
378f9c589e1SMathias Nyman 		enum xhci_ring_type type, unsigned int max_packet, gfp_t flags)
3790ebbab37SSarah Sharp {
3800ebbab37SSarah Sharp 	struct xhci_ring	*ring;
38170d43601SAndiry Xu 	int ret;
382a965315eSAdam Wallis 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
3830ebbab37SSarah Sharp 
384a965315eSAdam Wallis 	ring = kzalloc_node(sizeof(*ring), flags, dev_to_node(dev));
3850ebbab37SSarah Sharp 	if (!ring)
386326b4810SRandy Dunlap 		return NULL;
3870ebbab37SSarah Sharp 
3883fe4fe08SAndiry Xu 	ring->num_segs = num_segs;
389f9c589e1SMathias Nyman 	ring->bounce_buf_len = max_packet;
390d0e96f5aSSarah Sharp 	INIT_LIST_HEAD(&ring->td_list);
3913b72fca0SAndiry Xu 	ring->type = type;
3920ebbab37SSarah Sharp 	if (num_segs == 0)
3930ebbab37SSarah Sharp 		return ring;
3940ebbab37SSarah Sharp 
395bbdd82c7SNiklas Neronin 	ret = xhci_alloc_segments_for_ring(xhci, &ring->first_seg, &ring->last_seg, num_segs,
396bbdd82c7SNiklas Neronin 					   cycle_state, type, max_packet, flags);
39770d43601SAndiry Xu 	if (ret)
3980ebbab37SSarah Sharp 		goto fail;
3990ebbab37SSarah Sharp 
4003b72fca0SAndiry Xu 	/* Only event ring does not use link TRB */
4013b72fca0SAndiry Xu 	if (type != TYPE_EVENT) {
4020ebbab37SSarah Sharp 		/* See section 4.9.2.1 and 6.4.4.1 */
40370d43601SAndiry Xu 		ring->last_seg->trbs[TRBS_PER_SEGMENT - 1].link.control |=
404f5960b69SMatt Evans 			cpu_to_le32(LINK_TOGGLE);
4050ebbab37SSarah Sharp 	}
406186a7ef1SAndiry Xu 	xhci_initialize_ring_info(ring, cycle_state);
407b2d6edbbSLu Baolu 	trace_xhci_ring_alloc(ring);
4080ebbab37SSarah Sharp 	return ring;
4090ebbab37SSarah Sharp 
4100ebbab37SSarah Sharp fail:
41168e5254aSJulius Werner 	kfree(ring);
412326b4810SRandy Dunlap 	return NULL;
4130ebbab37SSarah Sharp }
4140ebbab37SSarah Sharp 
xhci_free_endpoint_ring(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,unsigned int ep_index)415c5628a2aSMathias Nyman void xhci_free_endpoint_ring(struct xhci_hcd *xhci,
416412566bdSSarah Sharp 		struct xhci_virt_device *virt_dev,
417412566bdSSarah Sharp 		unsigned int ep_index)
418412566bdSSarah Sharp {
419412566bdSSarah Sharp 	xhci_ring_free(xhci, virt_dev->eps[ep_index].ring);
420412566bdSSarah Sharp 	virt_dev->eps[ep_index].ring = NULL;
421412566bdSSarah Sharp }
422412566bdSSarah Sharp 
4238dfec614SAndiry Xu /*
4248dfec614SAndiry Xu  * Expand an existing ring.
425c5628a2aSMathias Nyman  * Allocate a new ring which has same segment numbers and link the two rings.
4268dfec614SAndiry Xu  */
xhci_ring_expansion(struct xhci_hcd * xhci,struct xhci_ring * ring,unsigned int num_new_segs,gfp_t flags)4278dfec614SAndiry Xu int xhci_ring_expansion(struct xhci_hcd *xhci, struct xhci_ring *ring,
428f5af638fSMathias Nyman 				unsigned int num_new_segs, gfp_t flags)
4298dfec614SAndiry Xu {
4308dfec614SAndiry Xu 	struct xhci_segment	*first;
4318dfec614SAndiry Xu 	struct xhci_segment	*last;
4328dfec614SAndiry Xu 	int			ret;
4338dfec614SAndiry Xu 
434bbdd82c7SNiklas Neronin 	ret = xhci_alloc_segments_for_ring(xhci, &first, &last, num_new_segs, ring->cycle_state,
435bbdd82c7SNiklas Neronin 					   ring->type, ring->bounce_buf_len, flags);
4368dfec614SAndiry Xu 	if (ret)
4378dfec614SAndiry Xu 		return -ENOMEM;
4388dfec614SAndiry Xu 
4392e8dd2deSNiklas Neronin 	if (ring->type == TYPE_STREAM) {
440d5734223SSarah Sharp 		ret = xhci_update_stream_segment_mapping(ring->trb_address_map,
441d5734223SSarah Sharp 						ring, first, last, flags);
4422e8dd2deSNiklas Neronin 		if (ret)
4432e8dd2deSNiklas Neronin 			goto free_segments;
444d5734223SSarah Sharp 	}
445d5734223SSarah Sharp 
446f5af638fSMathias Nyman 	xhci_link_rings(xhci, ring, first, last, num_new_segs);
447b2d6edbbSLu Baolu 	trace_xhci_ring_expansion(ring);
44868ffb011SXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_ring_expansion,
44968ffb011SXenia Ragiadakou 			"ring expansion succeed, now has %d segments",
4508dfec614SAndiry Xu 			ring->num_segs);
4518dfec614SAndiry Xu 
4528dfec614SAndiry Xu 	return 0;
4532e8dd2deSNiklas Neronin 
4542e8dd2deSNiklas Neronin free_segments:
4552e8dd2deSNiklas Neronin 	xhci_free_segments_for_ring(xhci, first);
4562e8dd2deSNiklas Neronin 	return ret;
4578dfec614SAndiry Xu }
4588dfec614SAndiry Xu 
xhci_alloc_container_ctx(struct xhci_hcd * xhci,int type,gfp_t flags)45967d2ea9fSLu Baolu struct xhci_container_ctx *xhci_alloc_container_ctx(struct xhci_hcd *xhci,
460d115b048SJohn Youn 						    int type, gfp_t flags)
461d115b048SJohn Youn {
46229f9d54bSSarah Sharp 	struct xhci_container_ctx *ctx;
463a965315eSAdam Wallis 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
46429f9d54bSSarah Sharp 
46529f9d54bSSarah Sharp 	if ((type != XHCI_CTX_TYPE_DEVICE) && (type != XHCI_CTX_TYPE_INPUT))
46629f9d54bSSarah Sharp 		return NULL;
46729f9d54bSSarah Sharp 
468a965315eSAdam Wallis 	ctx = kzalloc_node(sizeof(*ctx), flags, dev_to_node(dev));
469d115b048SJohn Youn 	if (!ctx)
470d115b048SJohn Youn 		return NULL;
471d115b048SJohn Youn 
472d115b048SJohn Youn 	ctx->type = type;
473d115b048SJohn Youn 	ctx->size = HCC_64BYTE_CONTEXT(xhci->hcc_params) ? 2048 : 1024;
474d115b048SJohn Youn 	if (type == XHCI_CTX_TYPE_INPUT)
475d115b048SJohn Youn 		ctx->size += CTX_SIZE(xhci->hcc_params);
476d115b048SJohn Youn 
47784c1eeb0SSaurabh Sengar 	ctx->bytes = dma_pool_zalloc(xhci->device_pool, flags, &ctx->dma);
478025f880cSMathias Nyman 	if (!ctx->bytes) {
479025f880cSMathias Nyman 		kfree(ctx);
480025f880cSMathias Nyman 		return NULL;
481025f880cSMathias Nyman 	}
482d115b048SJohn Youn 	return ctx;
483d115b048SJohn Youn }
484d115b048SJohn Youn 
xhci_free_container_ctx(struct xhci_hcd * xhci,struct xhci_container_ctx * ctx)48567d2ea9fSLu Baolu void xhci_free_container_ctx(struct xhci_hcd *xhci,
486d115b048SJohn Youn 			     struct xhci_container_ctx *ctx)
487d115b048SJohn Youn {
488a1d78c16SSarah Sharp 	if (!ctx)
489a1d78c16SSarah Sharp 		return;
490d115b048SJohn Youn 	dma_pool_free(xhci->device_pool, ctx->bytes, ctx->dma);
491d115b048SJohn Youn 	kfree(ctx);
492d115b048SJohn Youn }
493d115b048SJohn Youn 
xhci_get_input_control_ctx(struct xhci_container_ctx * ctx)4944daf9df5SLin Wang struct xhci_input_control_ctx *xhci_get_input_control_ctx(
495d115b048SJohn Youn 					      struct xhci_container_ctx *ctx)
496d115b048SJohn Youn {
49792f8e767SSarah Sharp 	if (ctx->type != XHCI_CTX_TYPE_INPUT)
49892f8e767SSarah Sharp 		return NULL;
49992f8e767SSarah Sharp 
500d115b048SJohn Youn 	return (struct xhci_input_control_ctx *)ctx->bytes;
501d115b048SJohn Youn }
502d115b048SJohn Youn 
xhci_get_slot_ctx(struct xhci_hcd * xhci,struct xhci_container_ctx * ctx)503d115b048SJohn Youn struct xhci_slot_ctx *xhci_get_slot_ctx(struct xhci_hcd *xhci,
504d115b048SJohn Youn 					struct xhci_container_ctx *ctx)
505d115b048SJohn Youn {
506d115b048SJohn Youn 	if (ctx->type == XHCI_CTX_TYPE_DEVICE)
507d115b048SJohn Youn 		return (struct xhci_slot_ctx *)ctx->bytes;
508d115b048SJohn Youn 
509d115b048SJohn Youn 	return (struct xhci_slot_ctx *)
510d115b048SJohn Youn 		(ctx->bytes + CTX_SIZE(xhci->hcc_params));
511d115b048SJohn Youn }
512d115b048SJohn Youn 
xhci_get_ep_ctx(struct xhci_hcd * xhci,struct xhci_container_ctx * ctx,unsigned int ep_index)513d115b048SJohn Youn struct xhci_ep_ctx *xhci_get_ep_ctx(struct xhci_hcd *xhci,
514d115b048SJohn Youn 				    struct xhci_container_ctx *ctx,
515d115b048SJohn Youn 				    unsigned int ep_index)
516d115b048SJohn Youn {
517d115b048SJohn Youn 	/* increment ep index by offset of start of ep ctx array */
518d115b048SJohn Youn 	ep_index++;
519d115b048SJohn Youn 	if (ctx->type == XHCI_CTX_TYPE_INPUT)
520d115b048SJohn Youn 		ep_index++;
521d115b048SJohn Youn 
522d115b048SJohn Youn 	return (struct xhci_ep_ctx *)
523d115b048SJohn Youn 		(ctx->bytes + (ep_index * CTX_SIZE(xhci->hcc_params)));
524d115b048SJohn Youn }
52514295a15SChunfeng Yun EXPORT_SYMBOL_GPL(xhci_get_ep_ctx);
5268df75f42SSarah Sharp 
5278df75f42SSarah Sharp /***************** Streams structures manipulation *************************/
5288df75f42SSarah Sharp 
xhci_free_stream_ctx(struct xhci_hcd * xhci,unsigned int num_stream_ctxs,struct xhci_stream_ctx * stream_ctx,dma_addr_t dma)5298212a49dSDmitry Torokhov static void xhci_free_stream_ctx(struct xhci_hcd *xhci,
5308df75f42SSarah Sharp 		unsigned int num_stream_ctxs,
5318df75f42SSarah Sharp 		struct xhci_stream_ctx *stream_ctx, dma_addr_t dma)
5328df75f42SSarah Sharp {
5334c39d4b9SArnd Bergmann 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
5340c959508SNiklas Neronin 	size_t size = array_size(sizeof(struct xhci_stream_ctx), num_stream_ctxs);
5358df75f42SSarah Sharp 
536ee4aa54bSHans de Goede 	if (size > MEDIUM_STREAM_ARRAY_SIZE)
5373056a5caSAndy Shevchenko 		dma_free_coherent(dev, size, stream_ctx, dma);
5383056a5caSAndy Shevchenko 	else if (size > SMALL_STREAM_ARRAY_SIZE)
5393056a5caSAndy Shevchenko 		dma_pool_free(xhci->medium_streams_pool, stream_ctx, dma);
5408df75f42SSarah Sharp 	else
5413056a5caSAndy Shevchenko 		dma_pool_free(xhci->small_streams_pool, stream_ctx, dma);
5428df75f42SSarah Sharp }
5438df75f42SSarah Sharp 
5448df75f42SSarah Sharp /*
5458df75f42SSarah Sharp  * The stream context array for each endpoint with bulk streams enabled can
5468df75f42SSarah Sharp  * vary in size, based on:
5478df75f42SSarah Sharp  *  - how many streams the endpoint supports,
5488df75f42SSarah Sharp  *  - the maximum primary stream array size the host controller supports,
5498df75f42SSarah Sharp  *  - and how many streams the device driver asks for.
5508df75f42SSarah Sharp  *
5518df75f42SSarah Sharp  * The stream context array must be a power of 2, and can be as small as
5528df75f42SSarah Sharp  * 64 bytes or as large as 1MB.
5538df75f42SSarah Sharp  */
xhci_alloc_stream_ctx(struct xhci_hcd * xhci,unsigned int num_stream_ctxs,dma_addr_t * dma,gfp_t mem_flags)5548212a49dSDmitry Torokhov static struct xhci_stream_ctx *xhci_alloc_stream_ctx(struct xhci_hcd *xhci,
5558df75f42SSarah Sharp 		unsigned int num_stream_ctxs, dma_addr_t *dma,
5568df75f42SSarah Sharp 		gfp_t mem_flags)
5578df75f42SSarah Sharp {
5584c39d4b9SArnd Bergmann 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
5590c959508SNiklas Neronin 	size_t size = array_size(sizeof(struct xhci_stream_ctx), num_stream_ctxs);
5608df75f42SSarah Sharp 
561ee4aa54bSHans de Goede 	if (size > MEDIUM_STREAM_ARRAY_SIZE)
56285052fdbSAndy Shevchenko 		return dma_alloc_coherent(dev, size, dma, mem_flags);
56385052fdbSAndy Shevchenko 	if (size > SMALL_STREAM_ARRAY_SIZE)
56485052fdbSAndy Shevchenko 		return dma_pool_zalloc(xhci->medium_streams_pool, mem_flags, dma);
5658df75f42SSarah Sharp 	else
56685052fdbSAndy Shevchenko 		return dma_pool_zalloc(xhci->small_streams_pool, mem_flags, dma);
5678df75f42SSarah Sharp }
5688df75f42SSarah Sharp 
xhci_dma_to_transfer_ring(struct xhci_virt_ep * ep,u64 address)569e9df17ebSSarah Sharp struct xhci_ring *xhci_dma_to_transfer_ring(
570e9df17ebSSarah Sharp 		struct xhci_virt_ep *ep,
571e9df17ebSSarah Sharp 		u64 address)
572e9df17ebSSarah Sharp {
573e9df17ebSSarah Sharp 	if (ep->ep_state & EP_HAS_STREAMS)
574e9df17ebSSarah Sharp 		return radix_tree_lookup(&ep->stream_info->trb_address_map,
575eb8ccd2bSDavid Howells 				address >> TRB_SEGMENT_SHIFT);
576e9df17ebSSarah Sharp 	return ep->ring;
577e9df17ebSSarah Sharp }
578e9df17ebSSarah Sharp 
5798df75f42SSarah Sharp /*
5808df75f42SSarah Sharp  * Change an endpoint's internal structure so it supports stream IDs.  The
5818df75f42SSarah Sharp  * number of requested streams includes stream 0, which cannot be used by device
5828df75f42SSarah Sharp  * drivers.
5838df75f42SSarah Sharp  *
5848df75f42SSarah Sharp  * The number of stream contexts in the stream context array may be bigger than
5858df75f42SSarah Sharp  * the number of streams the driver wants to use.  This is because the number of
5868df75f42SSarah Sharp  * stream context array entries must be a power of two.
5878df75f42SSarah Sharp  */
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)5888df75f42SSarah Sharp struct xhci_stream_info *xhci_alloc_stream_info(struct xhci_hcd *xhci,
5898df75f42SSarah Sharp 		unsigned int num_stream_ctxs,
590f9c589e1SMathias Nyman 		unsigned int num_streams,
591f9c589e1SMathias Nyman 		unsigned int max_packet, gfp_t mem_flags)
5928df75f42SSarah Sharp {
5938df75f42SSarah Sharp 	struct xhci_stream_info *stream_info;
5948df75f42SSarah Sharp 	u32 cur_stream;
5958df75f42SSarah Sharp 	struct xhci_ring *cur_ring;
5968df75f42SSarah Sharp 	u64 addr;
5978df75f42SSarah Sharp 	int ret;
598a965315eSAdam Wallis 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
5998df75f42SSarah Sharp 
6004feb07d0SAndy Shevchenko 	xhci_dbg(xhci, "Allocating %u streams and %u stream context array entries.\n",
6018df75f42SSarah Sharp 			num_streams, num_stream_ctxs);
6028df75f42SSarah Sharp 	if (xhci->cmd_ring_reserved_trbs == MAX_RSVD_CMD_TRBS) {
6038df75f42SSarah Sharp 		xhci_dbg(xhci, "Command ring has no reserved TRBs available\n");
6048df75f42SSarah Sharp 		return NULL;
6058df75f42SSarah Sharp 	}
6068df75f42SSarah Sharp 	xhci->cmd_ring_reserved_trbs++;
6078df75f42SSarah Sharp 
608a965315eSAdam Wallis 	stream_info = kzalloc_node(sizeof(*stream_info), mem_flags,
609a965315eSAdam Wallis 			dev_to_node(dev));
6108df75f42SSarah Sharp 	if (!stream_info)
6118df75f42SSarah Sharp 		goto cleanup_trbs;
6128df75f42SSarah Sharp 
6138df75f42SSarah Sharp 	stream_info->num_streams = num_streams;
6148df75f42SSarah Sharp 	stream_info->num_stream_ctxs = num_stream_ctxs;
6158df75f42SSarah Sharp 
6168df75f42SSarah Sharp 	/* Initialize the array of virtual pointers to stream rings. */
617a965315eSAdam Wallis 	stream_info->stream_rings = kcalloc_node(
618a965315eSAdam Wallis 			num_streams, sizeof(struct xhci_ring *), mem_flags,
619a965315eSAdam Wallis 			dev_to_node(dev));
6208df75f42SSarah Sharp 	if (!stream_info->stream_rings)
6218df75f42SSarah Sharp 		goto cleanup_info;
6228df75f42SSarah Sharp 
6238df75f42SSarah Sharp 	/* Initialize the array of DMA addresses for stream rings for the HW. */
6248df75f42SSarah Sharp 	stream_info->stream_ctx_array = xhci_alloc_stream_ctx(xhci,
6258df75f42SSarah Sharp 			num_stream_ctxs, &stream_info->ctx_array_dma,
6268df75f42SSarah Sharp 			mem_flags);
6278df75f42SSarah Sharp 	if (!stream_info->stream_ctx_array)
6287e271f42SJianglei Nie 		goto cleanup_ring_array;
6298df75f42SSarah Sharp 
6308df75f42SSarah Sharp 	/* Allocate everything needed to free the stream rings later */
6318df75f42SSarah Sharp 	stream_info->free_streams_command =
63214d49b7aSMathias Nyman 		xhci_alloc_command_with_ctx(xhci, true, mem_flags);
6338df75f42SSarah Sharp 	if (!stream_info->free_streams_command)
6348df75f42SSarah Sharp 		goto cleanup_ctx;
6358df75f42SSarah Sharp 
6368df75f42SSarah Sharp 	INIT_RADIX_TREE(&stream_info->trb_address_map, GFP_ATOMIC);
6378df75f42SSarah Sharp 
6388df75f42SSarah Sharp 	/* Allocate rings for all the streams that the driver will use,
6398df75f42SSarah Sharp 	 * and add their segment DMA addresses to the radix tree.
6408df75f42SSarah Sharp 	 * Stream 0 is reserved.
6418df75f42SSarah Sharp 	 */
642f9c589e1SMathias Nyman 
6438df75f42SSarah Sharp 	for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
6448df75f42SSarah Sharp 		stream_info->stream_rings[cur_stream] =
645f9c589e1SMathias Nyman 			xhci_ring_alloc(xhci, 2, 1, TYPE_STREAM, max_packet,
646f9c589e1SMathias Nyman 					mem_flags);
6478df75f42SSarah Sharp 		cur_ring = stream_info->stream_rings[cur_stream];
6488df75f42SSarah Sharp 		if (!cur_ring)
6498df75f42SSarah Sharp 			goto cleanup_rings;
650e9df17ebSSarah Sharp 		cur_ring->stream_id = cur_stream;
65115341303SGerd Hoffmann 		cur_ring->trb_address_map = &stream_info->trb_address_map;
6528df75f42SSarah Sharp 		/* Set deq ptr, cycle bit, and stream context type */
6538df75f42SSarah Sharp 		addr = cur_ring->first_seg->dma |
6548df75f42SSarah Sharp 			SCT_FOR_CTX(SCT_PRI_TR) |
6558df75f42SSarah Sharp 			cur_ring->cycle_state;
656f5960b69SMatt Evans 		stream_info->stream_ctx_array[cur_stream].stream_ring =
657f5960b69SMatt Evans 			cpu_to_le64(addr);
65896be93a8SAndy Shevchenko 		xhci_dbg(xhci, "Setting stream %d ring ptr to 0x%08llx\n", cur_stream, addr);
6598df75f42SSarah Sharp 
66015341303SGerd Hoffmann 		ret = xhci_update_stream_mapping(cur_ring, mem_flags);
6618df75f42SSarah Sharp 		if (ret) {
6628df75f42SSarah Sharp 			xhci_ring_free(xhci, cur_ring);
6638df75f42SSarah Sharp 			stream_info->stream_rings[cur_stream] = NULL;
6648df75f42SSarah Sharp 			goto cleanup_rings;
6658df75f42SSarah Sharp 		}
6668df75f42SSarah Sharp 	}
6678df75f42SSarah Sharp 	/* Leave the other unused stream ring pointers in the stream context
6688df75f42SSarah Sharp 	 * array initialized to zero.  This will cause the xHC to give us an
6698df75f42SSarah Sharp 	 * error if the device asks for a stream ID we don't have setup (if it
6708df75f42SSarah Sharp 	 * was any other way, the host controller would assume the ring is
6718df75f42SSarah Sharp 	 * "empty" and wait forever for data to be queued to that stream ID).
6728df75f42SSarah Sharp 	 */
6738df75f42SSarah Sharp 
6748df75f42SSarah Sharp 	return stream_info;
6758df75f42SSarah Sharp 
6768df75f42SSarah Sharp cleanup_rings:
6778df75f42SSarah Sharp 	for (cur_stream = 1; cur_stream < num_streams; cur_stream++) {
6788df75f42SSarah Sharp 		cur_ring = stream_info->stream_rings[cur_stream];
6798df75f42SSarah Sharp 		if (cur_ring) {
6808df75f42SSarah Sharp 			xhci_ring_free(xhci, cur_ring);
6818df75f42SSarah Sharp 			stream_info->stream_rings[cur_stream] = NULL;
6828df75f42SSarah Sharp 		}
6838df75f42SSarah Sharp 	}
6848df75f42SSarah Sharp 	xhci_free_command(xhci, stream_info->free_streams_command);
6858df75f42SSarah Sharp cleanup_ctx:
6867e271f42SJianglei Nie 	xhci_free_stream_ctx(xhci,
6877e271f42SJianglei Nie 		stream_info->num_stream_ctxs,
6887e271f42SJianglei Nie 		stream_info->stream_ctx_array,
6897e271f42SJianglei Nie 		stream_info->ctx_array_dma);
6907e271f42SJianglei Nie cleanup_ring_array:
6918df75f42SSarah Sharp 	kfree(stream_info->stream_rings);
6928df75f42SSarah Sharp cleanup_info:
6938df75f42SSarah Sharp 	kfree(stream_info);
6948df75f42SSarah Sharp cleanup_trbs:
6958df75f42SSarah Sharp 	xhci->cmd_ring_reserved_trbs--;
6968df75f42SSarah Sharp 	return NULL;
6978df75f42SSarah Sharp }
6988df75f42SSarah Sharp /*
6998df75f42SSarah Sharp  * Sets the MaxPStreams field and the Linear Stream Array field.
7008df75f42SSarah Sharp  * Sets the dequeue pointer to the stream context array.
7018df75f42SSarah Sharp  */
xhci_setup_streams_ep_input_ctx(struct xhci_hcd * xhci,struct xhci_ep_ctx * ep_ctx,struct xhci_stream_info * stream_info)7028df75f42SSarah Sharp void xhci_setup_streams_ep_input_ctx(struct xhci_hcd *xhci,
7038df75f42SSarah Sharp 		struct xhci_ep_ctx *ep_ctx,
7048df75f42SSarah Sharp 		struct xhci_stream_info *stream_info)
7058df75f42SSarah Sharp {
7068df75f42SSarah Sharp 	u32 max_primary_streams;
7078df75f42SSarah Sharp 	/* MaxPStreams is the number of stream context array entries, not the
7088df75f42SSarah Sharp 	 * number we're actually using.  Must be in 2^(MaxPstreams + 1) format.
7098df75f42SSarah Sharp 	 * fls(0) = 0, fls(0x1) = 1, fls(0x10) = 2, fls(0x100) = 3, etc.
7108df75f42SSarah Sharp 	 */
7118df75f42SSarah Sharp 	max_primary_streams = fls(stream_info->num_stream_ctxs) - 2;
7123a7fa5beSXenia Ragiadakou 	xhci_dbg_trace(xhci,  trace_xhci_dbg_context_change,
7133a7fa5beSXenia Ragiadakou 			"Setting number of stream ctx array entries to %u",
7148df75f42SSarah Sharp 			1 << (max_primary_streams + 1));
71528ccd296SMatt Evans 	ep_ctx->ep_info &= cpu_to_le32(~EP_MAXPSTREAMS_MASK);
71628ccd296SMatt Evans 	ep_ctx->ep_info |= cpu_to_le32(EP_MAXPSTREAMS(max_primary_streams)
71728ccd296SMatt Evans 				       | EP_HAS_LSA);
71828ccd296SMatt Evans 	ep_ctx->deq  = cpu_to_le64(stream_info->ctx_array_dma);
7198df75f42SSarah Sharp }
7208df75f42SSarah Sharp 
7218df75f42SSarah Sharp /*
7228df75f42SSarah Sharp  * Sets the MaxPStreams field and the Linear Stream Array field to 0.
7238df75f42SSarah Sharp  * Reinstalls the "normal" endpoint ring (at its previous dequeue mark,
7248df75f42SSarah Sharp  * not at the beginning of the ring).
7258df75f42SSarah Sharp  */
xhci_setup_no_streams_ep_input_ctx(struct xhci_ep_ctx * ep_ctx,struct xhci_virt_ep * ep)7264daf9df5SLin Wang void xhci_setup_no_streams_ep_input_ctx(struct xhci_ep_ctx *ep_ctx,
7278df75f42SSarah Sharp 		struct xhci_virt_ep *ep)
7288df75f42SSarah Sharp {
7298df75f42SSarah Sharp 	dma_addr_t addr;
73028ccd296SMatt Evans 	ep_ctx->ep_info &= cpu_to_le32(~(EP_MAXPSTREAMS_MASK | EP_HAS_LSA));
7318df75f42SSarah Sharp 	addr = xhci_trb_virt_to_dma(ep->ring->deq_seg, ep->ring->dequeue);
73228ccd296SMatt Evans 	ep_ctx->deq  = cpu_to_le64(addr | ep->ring->cycle_state);
7338df75f42SSarah Sharp }
7348df75f42SSarah Sharp 
7358df75f42SSarah Sharp /* Frees all stream contexts associated with the endpoint,
7368df75f42SSarah Sharp  *
7378df75f42SSarah Sharp  * Caller should fix the endpoint context streams fields.
7388df75f42SSarah Sharp  */
xhci_free_stream_info(struct xhci_hcd * xhci,struct xhci_stream_info * stream_info)7398df75f42SSarah Sharp void xhci_free_stream_info(struct xhci_hcd *xhci,
7408df75f42SSarah Sharp 		struct xhci_stream_info *stream_info)
7418df75f42SSarah Sharp {
7428df75f42SSarah Sharp 	int cur_stream;
7438df75f42SSarah Sharp 	struct xhci_ring *cur_ring;
7448df75f42SSarah Sharp 
7458df75f42SSarah Sharp 	if (!stream_info)
7468df75f42SSarah Sharp 		return;
7478df75f42SSarah Sharp 
7488df75f42SSarah Sharp 	for (cur_stream = 1; cur_stream < stream_info->num_streams;
7498df75f42SSarah Sharp 			cur_stream++) {
7508df75f42SSarah Sharp 		cur_ring = stream_info->stream_rings[cur_stream];
7518df75f42SSarah Sharp 		if (cur_ring) {
7528df75f42SSarah Sharp 			xhci_ring_free(xhci, cur_ring);
7538df75f42SSarah Sharp 			stream_info->stream_rings[cur_stream] = NULL;
7548df75f42SSarah Sharp 		}
7558df75f42SSarah Sharp 	}
7568df75f42SSarah Sharp 	xhci_free_command(xhci, stream_info->free_streams_command);
7578df75f42SSarah Sharp 	xhci->cmd_ring_reserved_trbs--;
7588df75f42SSarah Sharp 	if (stream_info->stream_ctx_array)
7598df75f42SSarah Sharp 		xhci_free_stream_ctx(xhci,
7608df75f42SSarah Sharp 				stream_info->num_stream_ctxs,
7618df75f42SSarah Sharp 				stream_info->stream_ctx_array,
7628df75f42SSarah Sharp 				stream_info->ctx_array_dma);
7638df75f42SSarah Sharp 
7648df75f42SSarah Sharp 	kfree(stream_info->stream_rings);
7658df75f42SSarah Sharp 	kfree(stream_info);
7668df75f42SSarah Sharp }
7678df75f42SSarah Sharp 
7688df75f42SSarah Sharp 
7698df75f42SSarah Sharp /***************** Device context manipulation *************************/
7708df75f42SSarah Sharp 
xhci_free_tt_info(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,int slot_id)771839c817cSSarah Sharp static void xhci_free_tt_info(struct xhci_hcd *xhci,
772839c817cSSarah Sharp 		struct xhci_virt_device *virt_dev,
773839c817cSSarah Sharp 		int slot_id)
774839c817cSSarah Sharp {
775839c817cSSarah Sharp 	struct list_head *tt_list_head;
77646ed8f00STakashi Iwai 	struct xhci_tt_bw_info *tt_info, *next;
77746ed8f00STakashi Iwai 	bool slot_found = false;
778839c817cSSarah Sharp 
779839c817cSSarah Sharp 	/* If the device never made it past the Set Address stage,
78006790c19SNiklas Neronin 	 * it may not have the root hub port pointer set correctly.
781839c817cSSarah Sharp 	 */
78206790c19SNiklas Neronin 	if (!virt_dev->rhub_port) {
78306790c19SNiklas Neronin 		xhci_dbg(xhci, "Bad rhub port.\n");
784839c817cSSarah Sharp 		return;
785839c817cSSarah Sharp 	}
786839c817cSSarah Sharp 
78706790c19SNiklas Neronin 	tt_list_head = &(xhci->rh_bw[virt_dev->rhub_port->hw_portnum].tts);
78846ed8f00STakashi Iwai 	list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
78946ed8f00STakashi Iwai 		/* Multi-TT hubs will have more than one entry */
79046ed8f00STakashi Iwai 		if (tt_info->slot_id == slot_id) {
79146ed8f00STakashi Iwai 			slot_found = true;
79246ed8f00STakashi Iwai 			list_del(&tt_info->tt_list);
79346ed8f00STakashi Iwai 			kfree(tt_info);
79446ed8f00STakashi Iwai 		} else if (slot_found) {
795839c817cSSarah Sharp 			break;
796839c817cSSarah Sharp 		}
79746ed8f00STakashi Iwai 	}
798839c817cSSarah Sharp }
799839c817cSSarah Sharp 
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)800839c817cSSarah Sharp int xhci_alloc_tt_info(struct xhci_hcd *xhci,
801839c817cSSarah Sharp 		struct xhci_virt_device *virt_dev,
802839c817cSSarah Sharp 		struct usb_device *hdev,
803839c817cSSarah Sharp 		struct usb_tt *tt, gfp_t mem_flags)
804839c817cSSarah Sharp {
805839c817cSSarah Sharp 	struct xhci_tt_bw_info		*tt_info;
806839c817cSSarah Sharp 	unsigned int			num_ports;
807839c817cSSarah Sharp 	int				i, j;
808a965315eSAdam Wallis 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
809839c817cSSarah Sharp 
810839c817cSSarah Sharp 	if (!tt->multi)
811839c817cSSarah Sharp 		num_ports = 1;
812839c817cSSarah Sharp 	else
813839c817cSSarah Sharp 		num_ports = hdev->maxchild;
814839c817cSSarah Sharp 
815839c817cSSarah Sharp 	for (i = 0; i < num_ports; i++, tt_info++) {
816839c817cSSarah Sharp 		struct xhci_interval_bw_table *bw_table;
817839c817cSSarah Sharp 
818a965315eSAdam Wallis 		tt_info = kzalloc_node(sizeof(*tt_info), mem_flags,
819a965315eSAdam Wallis 				dev_to_node(dev));
820839c817cSSarah Sharp 		if (!tt_info)
821839c817cSSarah Sharp 			goto free_tts;
822839c817cSSarah Sharp 		INIT_LIST_HEAD(&tt_info->tt_list);
823839c817cSSarah Sharp 		list_add(&tt_info->tt_list,
82406790c19SNiklas Neronin 				&xhci->rh_bw[virt_dev->rhub_port->hw_portnum].tts);
825839c817cSSarah Sharp 		tt_info->slot_id = virt_dev->udev->slot_id;
826839c817cSSarah Sharp 		if (tt->multi)
827839c817cSSarah Sharp 			tt_info->ttport = i+1;
828839c817cSSarah Sharp 		bw_table = &tt_info->bw_table;
829839c817cSSarah Sharp 		for (j = 0; j < XHCI_MAX_INTERVAL; j++)
830839c817cSSarah Sharp 			INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
831839c817cSSarah Sharp 	}
832839c817cSSarah Sharp 	return 0;
833839c817cSSarah Sharp 
834839c817cSSarah Sharp free_tts:
835839c817cSSarah Sharp 	xhci_free_tt_info(xhci, virt_dev, virt_dev->udev->slot_id);
836839c817cSSarah Sharp 	return -ENOMEM;
837839c817cSSarah Sharp }
838839c817cSSarah Sharp 
839839c817cSSarah Sharp 
840839c817cSSarah Sharp /* All the xhci_tds in the ring's TD list should be freed at this point.
841839c817cSSarah Sharp  * Should be called with xhci->lock held if there is any chance the TT lists
842839c817cSSarah Sharp  * will be manipulated by the configure endpoint, allocate device, or update
843839c817cSSarah Sharp  * hub functions while this function is removing the TT entries from the list.
844839c817cSSarah Sharp  */
xhci_free_virt_device(struct xhci_hcd * xhci,int slot_id)8453ffbba95SSarah Sharp void xhci_free_virt_device(struct xhci_hcd *xhci, int slot_id)
8463ffbba95SSarah Sharp {
8473ffbba95SSarah Sharp 	struct xhci_virt_device *dev;
8483ffbba95SSarah Sharp 	int i;
8492e27980eSSarah Sharp 	int old_active_eps = 0;
8503ffbba95SSarah Sharp 
8513ffbba95SSarah Sharp 	/* Slot ID 0 is reserved */
8523ffbba95SSarah Sharp 	if (slot_id == 0 || !xhci->devs[slot_id])
8533ffbba95SSarah Sharp 		return;
8543ffbba95SSarah Sharp 
8553ffbba95SSarah Sharp 	dev = xhci->devs[slot_id];
856a711edeeSFelipe Balbi 
8578e595a5dSSarah Sharp 	xhci->dcbaa->dev_context_ptrs[slot_id] = 0;
8583ffbba95SSarah Sharp 	if (!dev)
8593ffbba95SSarah Sharp 		return;
8603ffbba95SSarah Sharp 
861d850c165SZhengjun Xing 	trace_xhci_free_virt_device(dev);
862d850c165SZhengjun Xing 
8632e27980eSSarah Sharp 	if (dev->tt_info)
8642e27980eSSarah Sharp 		old_active_eps = dev->tt_info->active_eps;
8652e27980eSSarah Sharp 
86698871e94SFelipe Balbi 	for (i = 0; i < 31; i++) {
86763a0d9abSSarah Sharp 		if (dev->eps[i].ring)
86863a0d9abSSarah Sharp 			xhci_ring_free(xhci, dev->eps[i].ring);
8698df75f42SSarah Sharp 		if (dev->eps[i].stream_info)
8708df75f42SSarah Sharp 			xhci_free_stream_info(xhci,
8718df75f42SSarah Sharp 					dev->eps[i].stream_info);
8725aed5b7cSMathias Nyman 		/*
8735aed5b7cSMathias Nyman 		 * Endpoints are normally deleted from the bandwidth list when
8745aed5b7cSMathias Nyman 		 * endpoints are dropped, before device is freed.
8755aed5b7cSMathias Nyman 		 * If host is dying or being removed then endpoints aren't
8765aed5b7cSMathias Nyman 		 * dropped cleanly, so delete the endpoint from list here.
8775aed5b7cSMathias Nyman 		 * Only applicable for hosts with software bandwidth checking.
8782e27980eSSarah Sharp 		 */
8795aed5b7cSMathias Nyman 
8805aed5b7cSMathias Nyman 		if (!list_empty(&dev->eps[i].bw_endpoint_list)) {
8815aed5b7cSMathias Nyman 			list_del_init(&dev->eps[i].bw_endpoint_list);
8825aed5b7cSMathias Nyman 			xhci_dbg(xhci, "Slot %u endpoint %u not removed from BW list!\n",
8832e27980eSSarah Sharp 				 slot_id, i);
8848df75f42SSarah Sharp 		}
8855aed5b7cSMathias Nyman 	}
886839c817cSSarah Sharp 	/* If this is a hub, free the TT(s) from the TT list */
887839c817cSSarah Sharp 	xhci_free_tt_info(xhci, dev, slot_id);
8882e27980eSSarah Sharp 	/* If necessary, update the number of active TTs on this root port */
8892e27980eSSarah Sharp 	xhci_update_tt_active_eps(xhci, dev, old_active_eps);
8903ffbba95SSarah Sharp 
8913ffbba95SSarah Sharp 	if (dev->in_ctx)
892d115b048SJohn Youn 		xhci_free_container_ctx(xhci, dev->in_ctx);
8933ffbba95SSarah Sharp 	if (dev->out_ctx)
894d115b048SJohn Youn 		xhci_free_container_ctx(xhci, dev->out_ctx);
895d115b048SJohn Youn 
896a400efe4SMathias Nyman 	if (dev->udev && dev->udev->slot_id)
897a400efe4SMathias Nyman 		dev->udev->slot_id = 0;
89874151b53SNiklas Neronin 	if (dev->rhub_port && dev->rhub_port->slot_id == slot_id)
89974151b53SNiklas Neronin 		dev->rhub_port->slot_id = 0;
9003ffbba95SSarah Sharp 	kfree(xhci->devs[slot_id]);
901326b4810SRandy Dunlap 	xhci->devs[slot_id] = NULL;
9023ffbba95SSarah Sharp }
9033ffbba95SSarah Sharp 
904ee8665e2SMathias Nyman /*
905ee8665e2SMathias Nyman  * Free a virt_device structure.
906ee8665e2SMathias Nyman  * If the virt_device added a tt_info (a hub) and has children pointing to
907ee8665e2SMathias Nyman  * that tt_info, then free the child first. Recursive.
908ee8665e2SMathias Nyman  * We can't rely on udev at this point to find child-parent relationships.
909ee8665e2SMathias Nyman  */
xhci_free_virt_devices_depth_first(struct xhci_hcd * xhci,int slot_id)9104ee925dfSJean-Philippe Menil static void xhci_free_virt_devices_depth_first(struct xhci_hcd *xhci, int slot_id)
911ee8665e2SMathias Nyman {
912ee8665e2SMathias Nyman 	struct xhci_virt_device *vdev;
913ee8665e2SMathias Nyman 	struct list_head *tt_list_head;
914ee8665e2SMathias Nyman 	struct xhci_tt_bw_info *tt_info, *next;
915ee8665e2SMathias Nyman 	int i;
916ee8665e2SMathias Nyman 
917ee8665e2SMathias Nyman 	vdev = xhci->devs[slot_id];
918ee8665e2SMathias Nyman 	if (!vdev)
919ee8665e2SMathias Nyman 		return;
920ee8665e2SMathias Nyman 
92106790c19SNiklas Neronin 	if (!vdev->rhub_port) {
92206790c19SNiklas Neronin 		xhci_dbg(xhci, "Bad rhub port.\n");
92380e45769SYu Chen 		goto out;
92480e45769SYu Chen 	}
92580e45769SYu Chen 
92606790c19SNiklas Neronin 	tt_list_head = &(xhci->rh_bw[vdev->rhub_port->hw_portnum].tts);
927ee8665e2SMathias Nyman 	list_for_each_entry_safe(tt_info, next, tt_list_head, tt_list) {
928ee8665e2SMathias Nyman 		/* is this a hub device that added a tt_info to the tts list */
929ee8665e2SMathias Nyman 		if (tt_info->slot_id == slot_id) {
930ee8665e2SMathias Nyman 			/* are any devices using this tt_info? */
931ee8665e2SMathias Nyman 			for (i = 1; i < HCS_MAX_SLOTS(xhci->hcs_params1); i++) {
932ee8665e2SMathias Nyman 				vdev = xhci->devs[i];
933ee8665e2SMathias Nyman 				if (vdev && (vdev->tt_info == tt_info))
934ee8665e2SMathias Nyman 					xhci_free_virt_devices_depth_first(
935ee8665e2SMathias Nyman 						xhci, i);
936ee8665e2SMathias Nyman 			}
937ee8665e2SMathias Nyman 		}
938ee8665e2SMathias Nyman 	}
93980e45769SYu Chen out:
940ee8665e2SMathias Nyman 	/* we are now at a leaf device */
94102b6fdc2SLu Baolu 	xhci_debugfs_remove_slot(xhci, slot_id);
942ee8665e2SMathias Nyman 	xhci_free_virt_device(xhci, slot_id);
943ee8665e2SMathias Nyman }
944ee8665e2SMathias Nyman 
xhci_alloc_virt_device(struct xhci_hcd * xhci,int slot_id,struct usb_device * udev,gfp_t flags)9453ffbba95SSarah Sharp int xhci_alloc_virt_device(struct xhci_hcd *xhci, int slot_id,
9463ffbba95SSarah Sharp 		struct usb_device *udev, gfp_t flags)
9473ffbba95SSarah Sharp {
9483ffbba95SSarah Sharp 	struct xhci_virt_device *dev;
94963a0d9abSSarah Sharp 	int i;
9503ffbba95SSarah Sharp 
9513ffbba95SSarah Sharp 	/* Slot ID 0 is reserved */
9523ffbba95SSarah Sharp 	if (slot_id == 0 || xhci->devs[slot_id]) {
9533ffbba95SSarah Sharp 		xhci_warn(xhci, "Bad Slot ID %d\n", slot_id);
9543ffbba95SSarah Sharp 		return 0;
9553ffbba95SSarah Sharp 	}
9563ffbba95SSarah Sharp 
9575d9b70f7SMathias Nyman 	dev = kzalloc(sizeof(*dev), flags);
9585d9b70f7SMathias Nyman 	if (!dev)
9593ffbba95SSarah Sharp 		return 0;
9603ffbba95SSarah Sharp 
961d70f4231SMathias Nyman 	dev->slot_id = slot_id;
962d70f4231SMathias Nyman 
963d115b048SJohn Youn 	/* Allocate the (output) device context that will be used in the HC. */
964d115b048SJohn Youn 	dev->out_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_DEVICE, flags);
9653ffbba95SSarah Sharp 	if (!dev->out_ctx)
9663ffbba95SSarah Sharp 		goto fail;
967d115b048SJohn Youn 
96896be93a8SAndy Shevchenko 	xhci_dbg(xhci, "Slot %d output ctx = 0x%pad (dma)\n", slot_id, &dev->out_ctx->dma);
9693ffbba95SSarah Sharp 
9703ffbba95SSarah Sharp 	/* Allocate the (input) device context for address device command */
971d115b048SJohn Youn 	dev->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT, flags);
9723ffbba95SSarah Sharp 	if (!dev->in_ctx)
9733ffbba95SSarah Sharp 		goto fail;
974d115b048SJohn Youn 
97596be93a8SAndy Shevchenko 	xhci_dbg(xhci, "Slot %d input ctx = 0x%pad (dma)\n", slot_id, &dev->in_ctx->dma);
9763ffbba95SSarah Sharp 
97725355e04SMathias Nyman 	/* Initialize the cancellation and bandwidth list for each ep */
9786f5165cfSSarah Sharp 	for (i = 0; i < 31; i++) {
979ab58f3bbSMathias Nyman 		dev->eps[i].ep_index = i;
980ab58f3bbSMathias Nyman 		dev->eps[i].vdev = dev;
98125355e04SMathias Nyman 		dev->eps[i].xhci = xhci;
98263a0d9abSSarah Sharp 		INIT_LIST_HEAD(&dev->eps[i].cancelled_td_list);
9832e27980eSSarah Sharp 		INIT_LIST_HEAD(&dev->eps[i].bw_endpoint_list);
9846f5165cfSSarah Sharp 	}
98563a0d9abSSarah Sharp 
9863ffbba95SSarah Sharp 	/* Allocate endpoint 0 ring */
987f9c589e1SMathias Nyman 	dev->eps[0].ring = xhci_ring_alloc(xhci, 2, 1, TYPE_CTRL, 0, flags);
98863a0d9abSSarah Sharp 	if (!dev->eps[0].ring)
9893ffbba95SSarah Sharp 		goto fail;
9903ffbba95SSarah Sharp 
99164927730SAndiry Xu 	dev->udev = udev;
992f94e0186SSarah Sharp 
99328c2d2efSSarah Sharp 	/* Point to output device context in dcbaa. */
99428ccd296SMatt Evans 	xhci->dcbaa->dev_context_ptrs[slot_id] = cpu_to_le64(dev->out_ctx->dma);
995700e2052SGreg Kroah-Hartman 	xhci_dbg(xhci, "Set slot id %d dcbaa entry %p to 0x%llx\n",
9963ffbba95SSarah Sharp 		 slot_id,
9978e595a5dSSarah Sharp 		 &xhci->dcbaa->dev_context_ptrs[slot_id],
998f5960b69SMatt Evans 		 le64_to_cpu(xhci->dcbaa->dev_context_ptrs[slot_id]));
9993ffbba95SSarah Sharp 
1000a711edeeSFelipe Balbi 	trace_xhci_alloc_virt_device(dev);
1001a711edeeSFelipe Balbi 
10025d9b70f7SMathias Nyman 	xhci->devs[slot_id] = dev;
10035d9b70f7SMathias Nyman 
10043ffbba95SSarah Sharp 	return 1;
10053ffbba95SSarah Sharp fail:
10065d9b70f7SMathias Nyman 
10075d9b70f7SMathias Nyman 	if (dev->in_ctx)
10085d9b70f7SMathias Nyman 		xhci_free_container_ctx(xhci, dev->in_ctx);
10095d9b70f7SMathias Nyman 	if (dev->out_ctx)
10105d9b70f7SMathias Nyman 		xhci_free_container_ctx(xhci, dev->out_ctx);
10115d9b70f7SMathias Nyman 	kfree(dev);
10125d9b70f7SMathias Nyman 
10133ffbba95SSarah Sharp 	return 0;
10143ffbba95SSarah Sharp }
10153ffbba95SSarah Sharp 
xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd * xhci,struct usb_device * udev)10162d1ee590SSarah Sharp void xhci_copy_ep0_dequeue_into_input_ctx(struct xhci_hcd *xhci,
10172d1ee590SSarah Sharp 		struct usb_device *udev)
10182d1ee590SSarah Sharp {
10192d1ee590SSarah Sharp 	struct xhci_virt_device *virt_dev;
10202d1ee590SSarah Sharp 	struct xhci_ep_ctx	*ep0_ctx;
10212d1ee590SSarah Sharp 	struct xhci_ring	*ep_ring;
10222d1ee590SSarah Sharp 
10232d1ee590SSarah Sharp 	virt_dev = xhci->devs[udev->slot_id];
10242d1ee590SSarah Sharp 	ep0_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, 0);
10252d1ee590SSarah Sharp 	ep_ring = virt_dev->eps[0].ring;
10262d1ee590SSarah Sharp 	/*
10272d1ee590SSarah Sharp 	 * FIXME we don't keep track of the dequeue pointer very well after a
10282d1ee590SSarah Sharp 	 * Set TR dequeue pointer, so we're setting the dequeue pointer of the
10292d1ee590SSarah Sharp 	 * host to our enqueue pointer.  This should only be called after a
10302d1ee590SSarah Sharp 	 * configured device has reset, so all control transfers should have
10312d1ee590SSarah Sharp 	 * been completed or cancelled before the reset.
10322d1ee590SSarah Sharp 	 */
103328ccd296SMatt Evans 	ep0_ctx->deq = cpu_to_le64(xhci_trb_virt_to_dma(ep_ring->enq_seg,
103428ccd296SMatt Evans 							ep_ring->enqueue)
103528ccd296SMatt Evans 				   | ep_ring->cycle_state);
10362d1ee590SSarah Sharp }
10372d1ee590SSarah Sharp 
1038f6ff0ac8SSarah Sharp /*
1039f6ff0ac8SSarah Sharp  * The xHCI roothub may have ports of differing speeds in any order in the port
104007f76190SMathias Nyman  * status registers.
1041f6ff0ac8SSarah Sharp  *
1042c9a63ec5SNiklas Neronin  * The xHCI hardware wants to know the roothub port that the USB device
1043f6ff0ac8SSarah Sharp  * is attached to (or the roothub port its ancestor hub is attached to).  All we
1044f6ff0ac8SSarah Sharp  * know is the index of that port under either the USB 2.0 or the USB 3.0
1045f6ff0ac8SSarah Sharp  * roothub, but that doesn't give us the real index into the HW port status
1046c9a63ec5SNiklas Neronin  * registers.
1047f6ff0ac8SSarah Sharp  */
xhci_find_rhub_port(struct xhci_hcd * xhci,struct usb_device * udev)1048c9a63ec5SNiklas Neronin static struct xhci_port *xhci_find_rhub_port(struct xhci_hcd *xhci, struct usb_device *udev)
1049f6ff0ac8SSarah Sharp {
1050f6ff0ac8SSarah Sharp 	struct usb_device *top_dev;
1051c9a63ec5SNiklas Neronin 	struct xhci_hub *rhub;
10523f5eb141SLan Tianyu 	struct usb_hcd *hcd;
10533f5eb141SLan Tianyu 
10540caf6b33SMathias Nyman 	if (udev->speed >= USB_SPEED_SUPER)
1055873f3236SHeiner Kallweit 		hcd = xhci_get_usb3_hcd(xhci);
10563f5eb141SLan Tianyu 	else
10573f5eb141SLan Tianyu 		hcd = xhci->main_hcd;
1058f6ff0ac8SSarah Sharp 
1059f6ff0ac8SSarah Sharp 	for (top_dev = udev; top_dev->parent && top_dev->parent->parent;
1060f6ff0ac8SSarah Sharp 			top_dev = top_dev->parent)
1061f6ff0ac8SSarah Sharp 		/* Found device below root hub */;
1062f6ff0ac8SSarah Sharp 
1063c9a63ec5SNiklas Neronin 	rhub = xhci_get_rhub(hcd);
1064c9a63ec5SNiklas Neronin 	return rhub->ports[top_dev->portnum - 1];
1065f6ff0ac8SSarah Sharp }
1066f6ff0ac8SSarah Sharp 
10673ffbba95SSarah Sharp /* Setup an xHCI virtual device for a Set Address command */
xhci_setup_addressable_virt_dev(struct xhci_hcd * xhci,struct usb_device * udev)10683ffbba95SSarah Sharp int xhci_setup_addressable_virt_dev(struct xhci_hcd *xhci, struct usb_device *udev)
10693ffbba95SSarah Sharp {
10703ffbba95SSarah Sharp 	struct xhci_virt_device *dev;
10713ffbba95SSarah Sharp 	struct xhci_ep_ctx	*ep0_ctx;
1072d115b048SJohn Youn 	struct xhci_slot_ctx    *slot_ctx;
1073bd18fd5cSMathias Nyman 	u32			max_packets;
10743ffbba95SSarah Sharp 
10753ffbba95SSarah Sharp 	dev = xhci->devs[udev->slot_id];
10763ffbba95SSarah Sharp 	/* Slot ID 0 is reserved */
10773ffbba95SSarah Sharp 	if (udev->slot_id == 0 || !dev) {
10783ffbba95SSarah Sharp 		xhci_warn(xhci, "Slot ID %d is not assigned to this device\n",
10793ffbba95SSarah Sharp 				udev->slot_id);
10803ffbba95SSarah Sharp 		return -EINVAL;
10813ffbba95SSarah Sharp 	}
1082d115b048SJohn Youn 	ep0_ctx = xhci_get_ep_ctx(xhci, dev->in_ctx, 0);
1083d115b048SJohn Youn 	slot_ctx = xhci_get_slot_ctx(xhci, dev->in_ctx);
10843ffbba95SSarah Sharp 
10853ffbba95SSarah Sharp 	/* 3) Only the control endpoint is valid - one endpoint context */
1086f5960b69SMatt Evans 	slot_ctx->dev_info |= cpu_to_le32(LAST_CTX(1) | udev->route);
10873ffbba95SSarah Sharp 	switch (udev->speed) {
10880caf6b33SMathias Nyman 	case USB_SPEED_SUPER_PLUS:
1089d7854041SMathias Nyman 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SSP);
1090d7854041SMathias Nyman 		max_packets = MAX_PACKET(512);
1091d7854041SMathias Nyman 		break;
10923ffbba95SSarah Sharp 	case USB_SPEED_SUPER:
1093f5960b69SMatt Evans 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_SS);
1094bd18fd5cSMathias Nyman 		max_packets = MAX_PACKET(512);
10953ffbba95SSarah Sharp 		break;
10963ffbba95SSarah Sharp 	case USB_SPEED_HIGH:
1097f5960b69SMatt Evans 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_HS);
1098bd18fd5cSMathias Nyman 		max_packets = MAX_PACKET(64);
10993ffbba95SSarah Sharp 		break;
1100bd18fd5cSMathias Nyman 	/* USB core guesses at a 64-byte max packet first for FS devices */
11013ffbba95SSarah Sharp 	case USB_SPEED_FULL:
1102f5960b69SMatt Evans 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_FS);
1103bd18fd5cSMathias Nyman 		max_packets = MAX_PACKET(64);
11043ffbba95SSarah Sharp 		break;
11053ffbba95SSarah Sharp 	case USB_SPEED_LOW:
1106f5960b69SMatt Evans 		slot_ctx->dev_info |= cpu_to_le32(SLOT_SPEED_LS);
1107bd18fd5cSMathias Nyman 		max_packets = MAX_PACKET(8);
11083ffbba95SSarah Sharp 		break;
11093ffbba95SSarah Sharp 	default:
11103ffbba95SSarah Sharp 		/* Speed was set earlier, this shouldn't happen. */
1111bd18fd5cSMathias Nyman 		return -EINVAL;
11123ffbba95SSarah Sharp 	}
11133ffbba95SSarah Sharp 	/* Find the root hub port this device is under */
111406790c19SNiklas Neronin 	dev->rhub_port = xhci_find_rhub_port(xhci, udev);
111506790c19SNiklas Neronin 	if (!dev->rhub_port)
1116f6ff0ac8SSarah Sharp 		return -EINVAL;
111774151b53SNiklas Neronin 	/* Slot ID is set to the device directly below the root hub */
111874151b53SNiklas Neronin 	if (!udev->parent->parent)
111974151b53SNiklas Neronin 		dev->rhub_port->slot_id = udev->slot_id;
112006790c19SNiklas Neronin 	slot_ctx->dev_info2 |= cpu_to_le32(ROOT_HUB_PORT(dev->rhub_port->hw_portnum + 1));
112106790c19SNiklas Neronin 	xhci_dbg(xhci, "Slot ID %d: HW portnum %d, hcd portnum %d\n",
112206790c19SNiklas Neronin 		 udev->slot_id, dev->rhub_port->hw_portnum, dev->rhub_port->hcd_portnum);
11233ffbba95SSarah Sharp 
1124839c817cSSarah Sharp 	/* Find the right bandwidth table that this device will be a part of.
1125839c817cSSarah Sharp 	 * If this is a full speed device attached directly to a root port (or a
1126839c817cSSarah Sharp 	 * decendent of one), it counts as a primary bandwidth domain, not a
1127839c817cSSarah Sharp 	 * secondary bandwidth domain under a TT.  An xhci_tt_info structure
1128839c817cSSarah Sharp 	 * will never be created for the HS root hub.
1129839c817cSSarah Sharp 	 */
1130839c817cSSarah Sharp 	if (!udev->tt || !udev->tt->hub->parent) {
113106790c19SNiklas Neronin 		dev->bw_table = &xhci->rh_bw[dev->rhub_port->hw_portnum].bw_table;
1132839c817cSSarah Sharp 	} else {
1133839c817cSSarah Sharp 		struct xhci_root_port_bw_info *rh_bw;
1134839c817cSSarah Sharp 		struct xhci_tt_bw_info *tt_bw;
1135839c817cSSarah Sharp 
113606790c19SNiklas Neronin 		rh_bw = &xhci->rh_bw[dev->rhub_port->hw_portnum];
1137839c817cSSarah Sharp 		/* Find the right TT. */
1138839c817cSSarah Sharp 		list_for_each_entry(tt_bw, &rh_bw->tts, tt_list) {
1139839c817cSSarah Sharp 			if (tt_bw->slot_id != udev->tt->hub->slot_id)
1140839c817cSSarah Sharp 				continue;
1141839c817cSSarah Sharp 
1142839c817cSSarah Sharp 			if (!dev->udev->tt->multi ||
1143839c817cSSarah Sharp 					(udev->tt->multi &&
1144839c817cSSarah Sharp 					 tt_bw->ttport == dev->udev->ttport)) {
1145839c817cSSarah Sharp 				dev->bw_table = &tt_bw->bw_table;
1146839c817cSSarah Sharp 				dev->tt_info = tt_bw;
1147839c817cSSarah Sharp 				break;
1148839c817cSSarah Sharp 			}
1149839c817cSSarah Sharp 		}
1150839c817cSSarah Sharp 		if (!dev->tt_info)
1151839c817cSSarah Sharp 			xhci_warn(xhci, "WARN: Didn't find a matching TT\n");
1152839c817cSSarah Sharp 	}
1153839c817cSSarah Sharp 
1154aa1b13efSSarah Sharp 	/* Is this a LS/FS device under an external HS hub? */
1155aa1b13efSSarah Sharp 	if (udev->tt && udev->tt->hub->parent) {
115628ccd296SMatt Evans 		slot_ctx->tt_info = cpu_to_le32(udev->tt->hub->slot_id |
115728ccd296SMatt Evans 						(udev->ttport << 8));
115807b6de10SSarah Sharp 		if (udev->tt->multi)
115928ccd296SMatt Evans 			slot_ctx->dev_info |= cpu_to_le32(DEV_MTT);
11603ffbba95SSarah Sharp 	}
1161700e2052SGreg Kroah-Hartman 	xhci_dbg(xhci, "udev->tt = %p\n", udev->tt);
11623ffbba95SSarah Sharp 	xhci_dbg(xhci, "udev->ttport = 0x%x\n", udev->ttport);
11633ffbba95SSarah Sharp 
11643ffbba95SSarah Sharp 	/* Step 4 - ring already allocated */
11653ffbba95SSarah Sharp 	/* Step 5 */
116628ccd296SMatt Evans 	ep0_ctx->ep_info2 = cpu_to_le32(EP_TYPE(CTRL_EP));
1167bd18fd5cSMathias Nyman 
11683ffbba95SSarah Sharp 	/* EP 0 can handle "burst" sizes of 1, so Max Burst Size field is 0 */
1169bd18fd5cSMathias Nyman 	ep0_ctx->ep_info2 |= cpu_to_le32(MAX_BURST(0) | ERROR_COUNT(3) |
1170bd18fd5cSMathias Nyman 					 max_packets);
11713ffbba95SSarah Sharp 
117228ccd296SMatt Evans 	ep0_ctx->deq = cpu_to_le64(dev->eps[0].ring->first_seg->dma |
117328ccd296SMatt Evans 				   dev->eps[0].ring->cycle_state);
11743ffbba95SSarah Sharp 
1175a711edeeSFelipe Balbi 	trace_xhci_setup_addressable_virt_device(dev);
1176a711edeeSFelipe Balbi 
11773ffbba95SSarah Sharp 	/* Steps 7 and 8 were done in xhci_alloc_virt_device() */
11783ffbba95SSarah Sharp 
11793ffbba95SSarah Sharp 	return 0;
11803ffbba95SSarah Sharp }
11813ffbba95SSarah Sharp 
1182dfa49c4aSDmitry Torokhov /*
1183dfa49c4aSDmitry Torokhov  * Convert interval expressed as 2^(bInterval - 1) == interval into
1184dfa49c4aSDmitry Torokhov  * straight exponent value 2^n == interval.
1185dfa49c4aSDmitry Torokhov  *
1186dfa49c4aSDmitry Torokhov  */
xhci_parse_exponent_interval(struct usb_device * udev,struct usb_host_endpoint * ep)1187dfa49c4aSDmitry Torokhov static unsigned int xhci_parse_exponent_interval(struct usb_device *udev,
1188dfa49c4aSDmitry Torokhov 		struct usb_host_endpoint *ep)
1189dfa49c4aSDmitry Torokhov {
1190dfa49c4aSDmitry Torokhov 	unsigned int interval;
1191dfa49c4aSDmitry Torokhov 
1192dfa49c4aSDmitry Torokhov 	interval = clamp_val(ep->desc.bInterval, 1, 16) - 1;
1193dfa49c4aSDmitry Torokhov 	if (interval != ep->desc.bInterval - 1)
1194dfa49c4aSDmitry Torokhov 		dev_warn(&udev->dev,
1195cd3c18baSDmitry Torokhov 			 "ep %#x - rounding interval to %d %sframes\n",
1196dfa49c4aSDmitry Torokhov 			 ep->desc.bEndpointAddress,
1197cd3c18baSDmitry Torokhov 			 1 << interval,
1198cd3c18baSDmitry Torokhov 			 udev->speed == USB_SPEED_FULL ? "" : "micro");
1199cd3c18baSDmitry Torokhov 
1200cd3c18baSDmitry Torokhov 	if (udev->speed == USB_SPEED_FULL) {
1201cd3c18baSDmitry Torokhov 		/*
1202cd3c18baSDmitry Torokhov 		 * Full speed isoc endpoints specify interval in frames,
1203cd3c18baSDmitry Torokhov 		 * not microframes. We are using microframes everywhere,
1204cd3c18baSDmitry Torokhov 		 * so adjust accordingly.
1205cd3c18baSDmitry Torokhov 		 */
1206cd3c18baSDmitry Torokhov 		interval += 3;	/* 1 frame = 2^3 uframes */
1207cd3c18baSDmitry Torokhov 	}
1208dfa49c4aSDmitry Torokhov 
1209dfa49c4aSDmitry Torokhov 	return interval;
1210dfa49c4aSDmitry Torokhov }
1211dfa49c4aSDmitry Torokhov 
1212dfa49c4aSDmitry Torokhov /*
1213340a3504SSarah Sharp  * Convert bInterval expressed in microframes (in 1-255 range) to exponent of
1214dfa49c4aSDmitry Torokhov  * microframes, rounded down to nearest power of 2.
1215dfa49c4aSDmitry Torokhov  */
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)1216340a3504SSarah Sharp static unsigned int xhci_microframes_to_exponent(struct usb_device *udev,
1217340a3504SSarah Sharp 		struct usb_host_endpoint *ep, unsigned int desc_interval,
1218340a3504SSarah Sharp 		unsigned int min_exponent, unsigned int max_exponent)
1219dfa49c4aSDmitry Torokhov {
1220dfa49c4aSDmitry Torokhov 	unsigned int interval;
1221dfa49c4aSDmitry Torokhov 
1222340a3504SSarah Sharp 	interval = fls(desc_interval) - 1;
1223340a3504SSarah Sharp 	interval = clamp_val(interval, min_exponent, max_exponent);
1224340a3504SSarah Sharp 	if ((1 << interval) != desc_interval)
1225a5da9568SMathias Nyman 		dev_dbg(&udev->dev,
1226dfa49c4aSDmitry Torokhov 			 "ep %#x - rounding interval to %d microframes, ep desc says %d microframes\n",
1227dfa49c4aSDmitry Torokhov 			 ep->desc.bEndpointAddress,
1228dfa49c4aSDmitry Torokhov 			 1 << interval,
1229340a3504SSarah Sharp 			 desc_interval);
1230dfa49c4aSDmitry Torokhov 
1231dfa49c4aSDmitry Torokhov 	return interval;
1232dfa49c4aSDmitry Torokhov }
1233dfa49c4aSDmitry Torokhov 
xhci_parse_microframe_interval(struct usb_device * udev,struct usb_host_endpoint * ep)1234340a3504SSarah Sharp static unsigned int xhci_parse_microframe_interval(struct usb_device *udev,
1235340a3504SSarah Sharp 		struct usb_host_endpoint *ep)
1236340a3504SSarah Sharp {
123755c1945eSSarah Sharp 	if (ep->desc.bInterval == 0)
123855c1945eSSarah Sharp 		return 0;
1239340a3504SSarah Sharp 	return xhci_microframes_to_exponent(udev, ep,
1240340a3504SSarah Sharp 			ep->desc.bInterval, 0, 15);
1241340a3504SSarah Sharp }
1242340a3504SSarah Sharp 
1243340a3504SSarah Sharp 
xhci_parse_frame_interval(struct usb_device * udev,struct usb_host_endpoint * ep)1244340a3504SSarah Sharp static unsigned int xhci_parse_frame_interval(struct usb_device *udev,
1245340a3504SSarah Sharp 		struct usb_host_endpoint *ep)
1246340a3504SSarah Sharp {
1247340a3504SSarah Sharp 	return xhci_microframes_to_exponent(udev, ep,
1248340a3504SSarah Sharp 			ep->desc.bInterval * 8, 3, 10);
1249340a3504SSarah Sharp }
1250340a3504SSarah Sharp 
1251f94e0186SSarah Sharp /* Return the polling or NAK interval.
1252f94e0186SSarah Sharp  *
1253f94e0186SSarah Sharp  * The polling interval is expressed in "microframes".  If xHCI's Interval field
1254f94e0186SSarah Sharp  * is set to N, it will service the endpoint every 2^(Interval)*125us.
1255f94e0186SSarah Sharp  *
1256f94e0186SSarah Sharp  * The NAK interval is one NAK per 1 to 255 microframes, or no NAKs if interval
1257f94e0186SSarah Sharp  * is set to 0.
1258f94e0186SSarah Sharp  */
xhci_get_endpoint_interval(struct usb_device * udev,struct usb_host_endpoint * ep)1259575688e1SDmitry Torokhov static unsigned int xhci_get_endpoint_interval(struct usb_device *udev,
1260f94e0186SSarah Sharp 		struct usb_host_endpoint *ep)
1261f94e0186SSarah Sharp {
1262f94e0186SSarah Sharp 	unsigned int interval = 0;
1263f94e0186SSarah Sharp 
1264f94e0186SSarah Sharp 	switch (udev->speed) {
1265f94e0186SSarah Sharp 	case USB_SPEED_HIGH:
1266f94e0186SSarah Sharp 		/* Max NAK rate */
1267f94e0186SSarah Sharp 		if (usb_endpoint_xfer_control(&ep->desc) ||
1268dfa49c4aSDmitry Torokhov 		    usb_endpoint_xfer_bulk(&ep->desc)) {
1269340a3504SSarah Sharp 			interval = xhci_parse_microframe_interval(udev, ep);
1270dfa49c4aSDmitry Torokhov 			break;
1271dfa49c4aSDmitry Torokhov 		}
1272df561f66SGustavo A. R. Silva 		fallthrough;	/* SS and HS isoc/int have same decoding */
1273dfa49c4aSDmitry Torokhov 
12740caf6b33SMathias Nyman 	case USB_SPEED_SUPER_PLUS:
1275f94e0186SSarah Sharp 	case USB_SPEED_SUPER:
1276f94e0186SSarah Sharp 		if (usb_endpoint_xfer_int(&ep->desc) ||
1277f94e0186SSarah Sharp 		    usb_endpoint_xfer_isoc(&ep->desc)) {
1278dfa49c4aSDmitry Torokhov 			interval = xhci_parse_exponent_interval(udev, ep);
1279f94e0186SSarah Sharp 		}
1280f94e0186SSarah Sharp 		break;
1281dfa49c4aSDmitry Torokhov 
1282f94e0186SSarah Sharp 	case USB_SPEED_FULL:
1283b513d447SSarah Sharp 		if (usb_endpoint_xfer_isoc(&ep->desc)) {
1284dfa49c4aSDmitry Torokhov 			interval = xhci_parse_exponent_interval(udev, ep);
1285dfa49c4aSDmitry Torokhov 			break;
1286dfa49c4aSDmitry Torokhov 		}
1287dfa49c4aSDmitry Torokhov 		/*
1288b513d447SSarah Sharp 		 * Fall through for interrupt endpoint interval decoding
1289dfa49c4aSDmitry Torokhov 		 * since it uses the same rules as low speed interrupt
1290dfa49c4aSDmitry Torokhov 		 * endpoints.
1291dfa49c4aSDmitry Torokhov 		 */
1292df561f66SGustavo A. R. Silva 		fallthrough;
1293dfa49c4aSDmitry Torokhov 
1294f94e0186SSarah Sharp 	case USB_SPEED_LOW:
1295f94e0186SSarah Sharp 		if (usb_endpoint_xfer_int(&ep->desc) ||
1296f94e0186SSarah Sharp 		    usb_endpoint_xfer_isoc(&ep->desc)) {
1297dfa49c4aSDmitry Torokhov 
1298dfa49c4aSDmitry Torokhov 			interval = xhci_parse_frame_interval(udev, ep);
1299f94e0186SSarah Sharp 		}
1300f94e0186SSarah Sharp 		break;
1301dfa49c4aSDmitry Torokhov 
1302f94e0186SSarah Sharp 	default:
1303f94e0186SSarah Sharp 		BUG();
1304f94e0186SSarah Sharp 	}
1305def4e6f7SMathias Nyman 	return interval;
1306f94e0186SSarah Sharp }
1307f94e0186SSarah Sharp 
1308c30c791cSSarah Sharp /* The "Mult" field in the endpoint context is only set for SuperSpeed isoc eps.
13091cf62246SSarah Sharp  * High speed endpoint descriptors can define "the number of additional
13101cf62246SSarah Sharp  * transaction opportunities per microframe", but that goes in the Max Burst
13111cf62246SSarah Sharp  * endpoint context field.
13121cf62246SSarah Sharp  */
xhci_get_endpoint_mult(struct usb_device * udev,struct usb_host_endpoint * ep)1313575688e1SDmitry Torokhov static u32 xhci_get_endpoint_mult(struct usb_device *udev,
13141cf62246SSarah Sharp 		struct usb_host_endpoint *ep)
13151cf62246SSarah Sharp {
13160caf6b33SMathias Nyman 	if (udev->speed < USB_SPEED_SUPER ||
1317c30c791cSSarah Sharp 			!usb_endpoint_xfer_isoc(&ep->desc))
13181cf62246SSarah Sharp 		return 0;
1319842f1690SAlan Stern 	return ep->ss_ep_comp.bmAttributes;
13201cf62246SSarah Sharp }
13211cf62246SSarah Sharp 
xhci_get_endpoint_max_burst(struct usb_device * udev,struct usb_host_endpoint * ep)1322def4e6f7SMathias Nyman static u32 xhci_get_endpoint_max_burst(struct usb_device *udev,
1323def4e6f7SMathias Nyman 				       struct usb_host_endpoint *ep)
1324def4e6f7SMathias Nyman {
1325def4e6f7SMathias Nyman 	/* Super speed and Plus have max burst in ep companion desc */
1326def4e6f7SMathias Nyman 	if (udev->speed >= USB_SPEED_SUPER)
1327def4e6f7SMathias Nyman 		return ep->ss_ep_comp.bMaxBurst;
1328def4e6f7SMathias Nyman 
1329def4e6f7SMathias Nyman 	if (udev->speed == USB_SPEED_HIGH &&
1330def4e6f7SMathias Nyman 	    (usb_endpoint_xfer_isoc(&ep->desc) ||
1331def4e6f7SMathias Nyman 	     usb_endpoint_xfer_int(&ep->desc)))
1332dcf5228cSFelipe Balbi 		return usb_endpoint_maxp_mult(&ep->desc) - 1;
1333def4e6f7SMathias Nyman 
1334def4e6f7SMathias Nyman 	return 0;
1335def4e6f7SMathias Nyman }
1336def4e6f7SMathias Nyman 
xhci_get_endpoint_type(struct usb_host_endpoint * ep)13374daf9df5SLin Wang static u32 xhci_get_endpoint_type(struct usb_host_endpoint *ep)
1338f94e0186SSarah Sharp {
1339f94e0186SSarah Sharp 	int in;
1340f94e0186SSarah Sharp 
1341f94e0186SSarah Sharp 	in = usb_endpoint_dir_in(&ep->desc);
1342def4e6f7SMathias Nyman 
1343c0e625c4SFelipe Balbi 	switch (usb_endpoint_type(&ep->desc)) {
1344c0e625c4SFelipe Balbi 	case USB_ENDPOINT_XFER_CONTROL:
1345def4e6f7SMathias Nyman 		return CTRL_EP;
1346c0e625c4SFelipe Balbi 	case USB_ENDPOINT_XFER_BULK:
1347def4e6f7SMathias Nyman 		return in ? BULK_IN_EP : BULK_OUT_EP;
1348c0e625c4SFelipe Balbi 	case USB_ENDPOINT_XFER_ISOC:
1349def4e6f7SMathias Nyman 		return in ? ISOC_IN_EP : ISOC_OUT_EP;
1350c0e625c4SFelipe Balbi 	case USB_ENDPOINT_XFER_INT:
1351def4e6f7SMathias Nyman 		return in ? INT_IN_EP : INT_OUT_EP;
1352c0e625c4SFelipe Balbi 	}
1353def4e6f7SMathias Nyman 	return 0;
1354f94e0186SSarah Sharp }
1355f94e0186SSarah Sharp 
13569238f25dSSarah Sharp /* Return the maximum endpoint service interval time (ESIT) payload.
13579238f25dSSarah Sharp  * Basically, this is the maxpacket size, multiplied by the burst size
13589238f25dSSarah Sharp  * and mult size.
13599238f25dSSarah Sharp  */
xhci_get_max_esit_payload(struct usb_device * udev,struct usb_host_endpoint * ep)13604daf9df5SLin Wang static u32 xhci_get_max_esit_payload(struct usb_device *udev,
13619238f25dSSarah Sharp 		struct usb_host_endpoint *ep)
13629238f25dSSarah Sharp {
13639238f25dSSarah Sharp 	int max_burst;
13649238f25dSSarah Sharp 	int max_packet;
13659238f25dSSarah Sharp 
13669238f25dSSarah Sharp 	/* Only applies for interrupt or isochronous endpoints */
13679238f25dSSarah Sharp 	if (usb_endpoint_xfer_control(&ep->desc) ||
13689238f25dSSarah Sharp 			usb_endpoint_xfer_bulk(&ep->desc))
13699238f25dSSarah Sharp 		return 0;
13709238f25dSSarah Sharp 
13718ef8a9f5SMathias Nyman 	/* SuperSpeedPlus Isoc ep sending over 48k per esit */
13728ef8a9f5SMathias Nyman 	if ((udev->speed >= USB_SPEED_SUPER_PLUS) &&
13738ef8a9f5SMathias Nyman 	    USB_SS_SSP_ISOC_COMP(ep->ss_ep_comp.bmAttributes))
13748ef8a9f5SMathias Nyman 		return le32_to_cpu(ep->ssp_isoc_ep_comp.dwBytesPerInterval);
137585052fdbSAndy Shevchenko 
13768ef8a9f5SMathias Nyman 	/* SuperSpeed or SuperSpeedPlus Isoc ep with less than 48k per esit */
137785052fdbSAndy Shevchenko 	if (udev->speed >= USB_SPEED_SUPER)
137864b3c304SSebastian Andrzej Siewior 		return le16_to_cpu(ep->ss_ep_comp.wBytesPerInterval);
13799238f25dSSarah Sharp 
1380734d3dddSFelipe Balbi 	max_packet = usb_endpoint_maxp(&ep->desc);
1381dcf5228cSFelipe Balbi 	max_burst = usb_endpoint_maxp_mult(&ep->desc);
13829238f25dSSarah Sharp 	/* A 0 in max burst means 1 transfer per ESIT */
1383dcf5228cSFelipe Balbi 	return max_packet * max_burst;
13849238f25dSSarah Sharp }
13859238f25dSSarah Sharp 
13868df75f42SSarah Sharp /* Set up an endpoint with one ring segment.  Do not allocate stream rings.
13878df75f42SSarah Sharp  * Drivers will have to call usb_alloc_streams() to do that.
13888df75f42SSarah Sharp  */
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)1389f94e0186SSarah Sharp int xhci_endpoint_init(struct xhci_hcd *xhci,
1390f94e0186SSarah Sharp 		struct xhci_virt_device *virt_dev,
1391f94e0186SSarah Sharp 		struct usb_device *udev,
1392f88ba78dSSarah Sharp 		struct usb_host_endpoint *ep,
1393f88ba78dSSarah Sharp 		gfp_t mem_flags)
1394f94e0186SSarah Sharp {
1395f94e0186SSarah Sharp 	unsigned int ep_index;
1396f94e0186SSarah Sharp 	struct xhci_ep_ctx *ep_ctx;
1397f94e0186SSarah Sharp 	struct xhci_ring *ep_ring;
1398f94e0186SSarah Sharp 	unsigned int max_packet;
1399def4e6f7SMathias Nyman 	enum xhci_ring_type ring_type;
14009238f25dSSarah Sharp 	u32 max_esit_payload;
140117d65554SMathias Nyman 	u32 endpoint_type;
1402def4e6f7SMathias Nyman 	unsigned int max_burst;
1403def4e6f7SMathias Nyman 	unsigned int interval;
1404def4e6f7SMathias Nyman 	unsigned int mult;
1405def4e6f7SMathias Nyman 	unsigned int avg_trb_len;
1406def4e6f7SMathias Nyman 	unsigned int err_count = 0;
1407f94e0186SSarah Sharp 
1408f94e0186SSarah Sharp 	ep_index = xhci_get_endpoint_index(&ep->desc);
1409d115b048SJohn Youn 	ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
1410f94e0186SSarah Sharp 
14114daf9df5SLin Wang 	endpoint_type = xhci_get_endpoint_type(ep);
141217d65554SMathias Nyman 	if (!endpoint_type)
141317d65554SMathias Nyman 		return -EINVAL;
141417d65554SMathias Nyman 
1415def4e6f7SMathias Nyman 	ring_type = usb_endpoint_type(&ep->desc);
1416f94e0186SSarah Sharp 
1417def4e6f7SMathias Nyman 	/*
1418def4e6f7SMathias Nyman 	 * Get values to fill the endpoint context, mostly from ep descriptor.
1419def4e6f7SMathias Nyman 	 * The average TRB buffer lengt for bulk endpoints is unclear as we
1420def4e6f7SMathias Nyman 	 * have no clue on scatter gather list entry size. For Isoc and Int,
1421def4e6f7SMathias Nyman 	 * set it to max available. See xHCI 1.1 spec 4.14.1.1 for details.
1422def4e6f7SMathias Nyman 	 */
1423def4e6f7SMathias Nyman 	max_esit_payload = xhci_get_max_esit_payload(udev, ep);
1424def4e6f7SMathias Nyman 	interval = xhci_get_endpoint_interval(udev, ep);
142569307ccbSRoger Quadros 
142669307ccbSRoger Quadros 	/* Periodic endpoint bInterval limit quirk */
142769307ccbSRoger Quadros 	if (usb_endpoint_xfer_int(&ep->desc) ||
142869307ccbSRoger Quadros 	    usb_endpoint_xfer_isoc(&ep->desc)) {
142969307ccbSRoger Quadros 		if ((xhci->quirks & XHCI_LIMIT_ENDPOINT_INTERVAL_7) &&
143069307ccbSRoger Quadros 		    udev->speed >= USB_SPEED_HIGH &&
143169307ccbSRoger Quadros 		    interval >= 7) {
143269307ccbSRoger Quadros 			interval = 6;
143369307ccbSRoger Quadros 		}
143469307ccbSRoger Quadros 	}
143569307ccbSRoger Quadros 
1436def4e6f7SMathias Nyman 	mult = xhci_get_endpoint_mult(udev, ep);
1437734d3dddSFelipe Balbi 	max_packet = usb_endpoint_maxp(&ep->desc);
1438def4e6f7SMathias Nyman 	max_burst = xhci_get_endpoint_max_burst(udev, ep);
1439def4e6f7SMathias Nyman 	avg_trb_len = max_esit_payload;
1440f94e0186SSarah Sharp 
1441f94e0186SSarah Sharp 	/* FIXME dig Mult and streams info out of ep companion desc */
1442f94e0186SSarah Sharp 
1443def4e6f7SMathias Nyman 	/* Allow 3 retries for everything but isoc, set CErr = 3 */
1444f94e0186SSarah Sharp 	if (!usb_endpoint_xfer_isoc(&ep->desc))
1445def4e6f7SMathias Nyman 		err_count = 3;
1446f148b9f4SMathias Nyman 	/* HS bulk max packet should be 512, FS bulk supports 8, 16, 32 or 64 */
1447f148b9f4SMathias Nyman 	if (usb_endpoint_xfer_bulk(&ep->desc)) {
1448f148b9f4SMathias Nyman 		if (udev->speed == USB_SPEED_HIGH)
1449e4f47e36SAlan Stern 			max_packet = 512;
1450f148b9f4SMathias Nyman 		if (udev->speed == USB_SPEED_FULL) {
1451f148b9f4SMathias Nyman 			max_packet = rounddown_pow_of_two(max_packet);
1452f148b9f4SMathias Nyman 			max_packet = clamp_val(max_packet, 8, 64);
1453f148b9f4SMathias Nyman 		}
1454f148b9f4SMathias Nyman 	}
1455def4e6f7SMathias Nyman 	/* xHCI 1.0 and 1.1 indicates that ctrl ep avg TRB Length should be 8 */
1456dca77945SMathias Nyman 	if (usb_endpoint_xfer_control(&ep->desc) && xhci->hci_version >= 0x100)
1457def4e6f7SMathias Nyman 		avg_trb_len = 8;
14588ef8a9f5SMathias Nyman 	/* xhci 1.1 with LEC support doesn't use mult field, use RsvdZ */
14598ef8a9f5SMathias Nyman 	if ((xhci->hci_version > 0x100) && HCC2_LEC(xhci->hcc_params2))
14608ef8a9f5SMathias Nyman 		mult = 0;
1461def4e6f7SMathias Nyman 
1462f9c589e1SMathias Nyman 	/* Set up the endpoint ring */
1463f9c589e1SMathias Nyman 	virt_dev->eps[ep_index].new_ring =
1464f9c589e1SMathias Nyman 		xhci_ring_alloc(xhci, 2, 1, ring_type, max_packet, mem_flags);
1465c5628a2aSMathias Nyman 	if (!virt_dev->eps[ep_index].new_ring)
1466f9c589e1SMathias Nyman 		return -ENOMEM;
1467c5628a2aSMathias Nyman 
1468f9c589e1SMathias Nyman 	virt_dev->eps[ep_index].skip = false;
1469f9c589e1SMathias Nyman 	ep_ring = virt_dev->eps[ep_index].new_ring;
1470f9c589e1SMathias Nyman 
1471def4e6f7SMathias Nyman 	/* Fill the endpoint context */
14728ef8a9f5SMathias Nyman 	ep_ctx->ep_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_HI(max_esit_payload) |
14738ef8a9f5SMathias Nyman 				      EP_INTERVAL(interval) |
1474def4e6f7SMathias Nyman 				      EP_MULT(mult));
1475def4e6f7SMathias Nyman 	ep_ctx->ep_info2 = cpu_to_le32(EP_TYPE(endpoint_type) |
1476def4e6f7SMathias Nyman 				       MAX_PACKET(max_packet) |
1477def4e6f7SMathias Nyman 				       MAX_BURST(max_burst) |
1478def4e6f7SMathias Nyman 				       ERROR_COUNT(err_count));
1479def4e6f7SMathias Nyman 	ep_ctx->deq = cpu_to_le64(ep_ring->first_seg->dma |
1480def4e6f7SMathias Nyman 				  ep_ring->cycle_state);
1481def4e6f7SMathias Nyman 
1482def4e6f7SMathias Nyman 	ep_ctx->tx_info = cpu_to_le32(EP_MAX_ESIT_PAYLOAD_LO(max_esit_payload) |
1483def4e6f7SMathias Nyman 				      EP_AVG_TRB_LENGTH(avg_trb_len));
14849238f25dSSarah Sharp 
1485f94e0186SSarah Sharp 	return 0;
1486f94e0186SSarah Sharp }
1487f94e0186SSarah Sharp 
xhci_endpoint_zero(struct xhci_hcd * xhci,struct xhci_virt_device * virt_dev,struct usb_host_endpoint * ep)1488f94e0186SSarah Sharp void xhci_endpoint_zero(struct xhci_hcd *xhci,
1489f94e0186SSarah Sharp 		struct xhci_virt_device *virt_dev,
1490f94e0186SSarah Sharp 		struct usb_host_endpoint *ep)
1491f94e0186SSarah Sharp {
1492f94e0186SSarah Sharp 	unsigned int ep_index;
1493f94e0186SSarah Sharp 	struct xhci_ep_ctx *ep_ctx;
1494f94e0186SSarah Sharp 
1495f94e0186SSarah Sharp 	ep_index = xhci_get_endpoint_index(&ep->desc);
1496d115b048SJohn Youn 	ep_ctx = xhci_get_ep_ctx(xhci, virt_dev->in_ctx, ep_index);
1497f94e0186SSarah Sharp 
1498f94e0186SSarah Sharp 	ep_ctx->ep_info = 0;
1499f94e0186SSarah Sharp 	ep_ctx->ep_info2 = 0;
15008e595a5dSSarah Sharp 	ep_ctx->deq = 0;
1501f94e0186SSarah Sharp 	ep_ctx->tx_info = 0;
1502f94e0186SSarah Sharp 	/* Don't free the endpoint ring until the set interface or configuration
1503f94e0186SSarah Sharp 	 * request succeeds.
1504f94e0186SSarah Sharp 	 */
1505f94e0186SSarah Sharp }
1506f94e0186SSarah Sharp 
xhci_clear_endpoint_bw_info(struct xhci_bw_info * bw_info)15079af5d71dSSarah Sharp void xhci_clear_endpoint_bw_info(struct xhci_bw_info *bw_info)
15089af5d71dSSarah Sharp {
15099af5d71dSSarah Sharp 	bw_info->ep_interval = 0;
15109af5d71dSSarah Sharp 	bw_info->mult = 0;
15119af5d71dSSarah Sharp 	bw_info->num_packets = 0;
15129af5d71dSSarah Sharp 	bw_info->max_packet_size = 0;
15139af5d71dSSarah Sharp 	bw_info->type = 0;
15149af5d71dSSarah Sharp 	bw_info->max_esit_payload = 0;
15159af5d71dSSarah Sharp }
15169af5d71dSSarah Sharp 
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)15179af5d71dSSarah Sharp void xhci_update_bw_info(struct xhci_hcd *xhci,
15189af5d71dSSarah Sharp 		struct xhci_container_ctx *in_ctx,
15199af5d71dSSarah Sharp 		struct xhci_input_control_ctx *ctrl_ctx,
15209af5d71dSSarah Sharp 		struct xhci_virt_device *virt_dev)
15219af5d71dSSarah Sharp {
15229af5d71dSSarah Sharp 	struct xhci_bw_info *bw_info;
15239af5d71dSSarah Sharp 	struct xhci_ep_ctx *ep_ctx;
15249af5d71dSSarah Sharp 	unsigned int ep_type;
15259af5d71dSSarah Sharp 	int i;
15269af5d71dSSarah Sharp 
152798871e94SFelipe Balbi 	for (i = 1; i < 31; i++) {
15289af5d71dSSarah Sharp 		bw_info = &virt_dev->eps[i].bw_info;
15299af5d71dSSarah Sharp 
15309af5d71dSSarah Sharp 		/* We can't tell what endpoint type is being dropped, but
15319af5d71dSSarah Sharp 		 * unconditionally clearing the bandwidth info for non-periodic
15329af5d71dSSarah Sharp 		 * endpoints should be harmless because the info will never be
15339af5d71dSSarah Sharp 		 * set in the first place.
15349af5d71dSSarah Sharp 		 */
15359af5d71dSSarah Sharp 		if (!EP_IS_ADDED(ctrl_ctx, i) && EP_IS_DROPPED(ctrl_ctx, i)) {
15369af5d71dSSarah Sharp 			/* Dropped endpoint */
15379af5d71dSSarah Sharp 			xhci_clear_endpoint_bw_info(bw_info);
15389af5d71dSSarah Sharp 			continue;
15399af5d71dSSarah Sharp 		}
15409af5d71dSSarah Sharp 
15419af5d71dSSarah Sharp 		if (EP_IS_ADDED(ctrl_ctx, i)) {
15429af5d71dSSarah Sharp 			ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, i);
15439af5d71dSSarah Sharp 			ep_type = CTX_TO_EP_TYPE(le32_to_cpu(ep_ctx->ep_info2));
15449af5d71dSSarah Sharp 
15459af5d71dSSarah Sharp 			/* Ignore non-periodic endpoints */
15469af5d71dSSarah Sharp 			if (ep_type != ISOC_OUT_EP && ep_type != INT_OUT_EP &&
15479af5d71dSSarah Sharp 					ep_type != ISOC_IN_EP &&
15489af5d71dSSarah Sharp 					ep_type != INT_IN_EP)
15499af5d71dSSarah Sharp 				continue;
15509af5d71dSSarah Sharp 
15519af5d71dSSarah Sharp 			/* Added or changed endpoint */
15529af5d71dSSarah Sharp 			bw_info->ep_interval = CTX_TO_EP_INTERVAL(
15539af5d71dSSarah Sharp 					le32_to_cpu(ep_ctx->ep_info));
1554170c0263SSarah Sharp 			/* Number of packets and mult are zero-based in the
1555170c0263SSarah Sharp 			 * input context, but we want one-based for the
1556170c0263SSarah Sharp 			 * interval table.
15579af5d71dSSarah Sharp 			 */
1558170c0263SSarah Sharp 			bw_info->mult = CTX_TO_EP_MULT(
1559170c0263SSarah Sharp 					le32_to_cpu(ep_ctx->ep_info)) + 1;
15609af5d71dSSarah Sharp 			bw_info->num_packets = CTX_TO_MAX_BURST(
15619af5d71dSSarah Sharp 					le32_to_cpu(ep_ctx->ep_info2)) + 1;
15629af5d71dSSarah Sharp 			bw_info->max_packet_size = MAX_PACKET_DECODED(
15639af5d71dSSarah Sharp 					le32_to_cpu(ep_ctx->ep_info2));
15649af5d71dSSarah Sharp 			bw_info->type = ep_type;
15659af5d71dSSarah Sharp 			bw_info->max_esit_payload = CTX_TO_MAX_ESIT_PAYLOAD(
15669af5d71dSSarah Sharp 					le32_to_cpu(ep_ctx->tx_info));
15679af5d71dSSarah Sharp 		}
15689af5d71dSSarah Sharp 	}
15699af5d71dSSarah Sharp }
15709af5d71dSSarah Sharp 
1571f2217e8eSSarah Sharp /* Copy output xhci_ep_ctx to the input xhci_ep_ctx copy.
1572f2217e8eSSarah Sharp  * Useful when you want to change one particular aspect of the endpoint and then
1573f2217e8eSSarah Sharp  * issue a configure endpoint command.
1574f2217e8eSSarah Sharp  */
xhci_endpoint_copy(struct xhci_hcd * xhci,struct xhci_container_ctx * in_ctx,struct xhci_container_ctx * out_ctx,unsigned int ep_index)1575f2217e8eSSarah Sharp void xhci_endpoint_copy(struct xhci_hcd *xhci,
1576913a8a34SSarah Sharp 		struct xhci_container_ctx *in_ctx,
1577913a8a34SSarah Sharp 		struct xhci_container_ctx *out_ctx,
1578913a8a34SSarah Sharp 		unsigned int ep_index)
1579f2217e8eSSarah Sharp {
1580f2217e8eSSarah Sharp 	struct xhci_ep_ctx *out_ep_ctx;
1581f2217e8eSSarah Sharp 	struct xhci_ep_ctx *in_ep_ctx;
1582f2217e8eSSarah Sharp 
1583913a8a34SSarah Sharp 	out_ep_ctx = xhci_get_ep_ctx(xhci, out_ctx, ep_index);
1584913a8a34SSarah Sharp 	in_ep_ctx = xhci_get_ep_ctx(xhci, in_ctx, ep_index);
1585f2217e8eSSarah Sharp 
1586f2217e8eSSarah Sharp 	in_ep_ctx->ep_info = out_ep_ctx->ep_info;
1587f2217e8eSSarah Sharp 	in_ep_ctx->ep_info2 = out_ep_ctx->ep_info2;
1588f2217e8eSSarah Sharp 	in_ep_ctx->deq = out_ep_ctx->deq;
1589f2217e8eSSarah Sharp 	in_ep_ctx->tx_info = out_ep_ctx->tx_info;
15900a3b5330SChunfeng Yun 	if (xhci->quirks & XHCI_MTK_HOST) {
15910a3b5330SChunfeng Yun 		in_ep_ctx->reserved[0] = out_ep_ctx->reserved[0];
15920a3b5330SChunfeng Yun 		in_ep_ctx->reserved[1] = out_ep_ctx->reserved[1];
15930a3b5330SChunfeng Yun 	}
1594f2217e8eSSarah Sharp }
1595f2217e8eSSarah Sharp 
1596f2217e8eSSarah Sharp /* Copy output xhci_slot_ctx to the input xhci_slot_ctx.
1597f2217e8eSSarah Sharp  * Useful when you want to change one particular aspect of the endpoint and then
1598f2217e8eSSarah Sharp  * issue a configure endpoint command.  Only the context entries field matters,
1599f2217e8eSSarah Sharp  * but we'll copy the whole thing anyway.
1600f2217e8eSSarah Sharp  */
xhci_slot_copy(struct xhci_hcd * xhci,struct xhci_container_ctx * in_ctx,struct xhci_container_ctx * out_ctx)1601913a8a34SSarah Sharp void xhci_slot_copy(struct xhci_hcd *xhci,
1602913a8a34SSarah Sharp 		struct xhci_container_ctx *in_ctx,
1603913a8a34SSarah Sharp 		struct xhci_container_ctx *out_ctx)
1604f2217e8eSSarah Sharp {
1605f2217e8eSSarah Sharp 	struct xhci_slot_ctx *in_slot_ctx;
1606f2217e8eSSarah Sharp 	struct xhci_slot_ctx *out_slot_ctx;
1607f2217e8eSSarah Sharp 
1608913a8a34SSarah Sharp 	in_slot_ctx = xhci_get_slot_ctx(xhci, in_ctx);
1609913a8a34SSarah Sharp 	out_slot_ctx = xhci_get_slot_ctx(xhci, out_ctx);
1610f2217e8eSSarah Sharp 
1611f2217e8eSSarah Sharp 	in_slot_ctx->dev_info = out_slot_ctx->dev_info;
1612f2217e8eSSarah Sharp 	in_slot_ctx->dev_info2 = out_slot_ctx->dev_info2;
1613f2217e8eSSarah Sharp 	in_slot_ctx->tt_info = out_slot_ctx->tt_info;
1614f2217e8eSSarah Sharp 	in_slot_ctx->dev_state = out_slot_ctx->dev_state;
1615f2217e8eSSarah Sharp }
1616f2217e8eSSarah Sharp 
1617254c80a3SJohn Youn /* Set up the scratchpad buffer array and scratchpad buffers, if needed. */
scratchpad_alloc(struct xhci_hcd * xhci,gfp_t flags)1618254c80a3SJohn Youn static int scratchpad_alloc(struct xhci_hcd *xhci, gfp_t flags)
1619254c80a3SJohn Youn {
1620254c80a3SJohn Youn 	int i;
16214c39d4b9SArnd Bergmann 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
1622254c80a3SJohn Youn 	int num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
1623254c80a3SJohn Youn 
1624d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
1625d195fcffSXenia Ragiadakou 			"Allocating %d scratchpad buffers", num_sp);
1626254c80a3SJohn Youn 
1627254c80a3SJohn Youn 	if (!num_sp)
1628254c80a3SJohn Youn 		return 0;
1629254c80a3SJohn Youn 
1630a965315eSAdam Wallis 	xhci->scratchpad = kzalloc_node(sizeof(*xhci->scratchpad), flags,
1631a965315eSAdam Wallis 				dev_to_node(dev));
1632254c80a3SJohn Youn 	if (!xhci->scratchpad)
1633254c80a3SJohn Youn 		goto fail_sp;
1634254c80a3SJohn Youn 
163522d45f01SSebastian Andrzej Siewior 	xhci->scratchpad->sp_array = dma_alloc_coherent(dev,
16360c959508SNiklas Neronin 				     array_size(sizeof(u64), num_sp),
163722d45f01SSebastian Andrzej Siewior 				     &xhci->scratchpad->sp_dma, flags);
1638254c80a3SJohn Youn 	if (!xhci->scratchpad->sp_array)
1639254c80a3SJohn Youn 		goto fail_sp2;
1640254c80a3SJohn Youn 
1641a965315eSAdam Wallis 	xhci->scratchpad->sp_buffers = kcalloc_node(num_sp, sizeof(void *),
1642a965315eSAdam Wallis 					flags, dev_to_node(dev));
1643254c80a3SJohn Youn 	if (!xhci->scratchpad->sp_buffers)
1644254c80a3SJohn Youn 		goto fail_sp3;
1645254c80a3SJohn Youn 
164628ccd296SMatt Evans 	xhci->dcbaa->dev_context_ptrs[0] = cpu_to_le64(xhci->scratchpad->sp_dma);
1647254c80a3SJohn Youn 	for (i = 0; i < num_sp; i++) {
1648254c80a3SJohn Youn 		dma_addr_t dma;
1649750afb08SLuis Chamberlain 		void *buf = dma_alloc_coherent(dev, xhci->page_size, &dma,
165022d45f01SSebastian Andrzej Siewior 					       flags);
1651254c80a3SJohn Youn 		if (!buf)
1652314eaf7dSPeter Chen 			goto fail_sp4;
1653254c80a3SJohn Youn 
1654254c80a3SJohn Youn 		xhci->scratchpad->sp_array[i] = dma;
1655254c80a3SJohn Youn 		xhci->scratchpad->sp_buffers[i] = buf;
1656254c80a3SJohn Youn 	}
1657254c80a3SJohn Youn 
1658254c80a3SJohn Youn 	return 0;
1659254c80a3SJohn Youn 
1660314eaf7dSPeter Chen  fail_sp4:
166176dc910aSAndy Shevchenko 	while (i--)
166222d45f01SSebastian Andrzej Siewior 		dma_free_coherent(dev, xhci->page_size,
1663254c80a3SJohn Youn 				    xhci->scratchpad->sp_buffers[i],
1664314eaf7dSPeter Chen 				    xhci->scratchpad->sp_array[i]);
1665254c80a3SJohn Youn 
1666254c80a3SJohn Youn 	kfree(xhci->scratchpad->sp_buffers);
1667254c80a3SJohn Youn 
1668254c80a3SJohn Youn  fail_sp3:
16690c959508SNiklas Neronin 	dma_free_coherent(dev, array_size(sizeof(u64), num_sp),
1670254c80a3SJohn Youn 			    xhci->scratchpad->sp_array,
1671254c80a3SJohn Youn 			    xhci->scratchpad->sp_dma);
1672254c80a3SJohn Youn 
1673254c80a3SJohn Youn  fail_sp2:
1674254c80a3SJohn Youn 	kfree(xhci->scratchpad);
1675254c80a3SJohn Youn 	xhci->scratchpad = NULL;
1676254c80a3SJohn Youn 
1677254c80a3SJohn Youn  fail_sp:
1678254c80a3SJohn Youn 	return -ENOMEM;
1679254c80a3SJohn Youn }
1680254c80a3SJohn Youn 
scratchpad_free(struct xhci_hcd * xhci)1681254c80a3SJohn Youn static void scratchpad_free(struct xhci_hcd *xhci)
1682254c80a3SJohn Youn {
1683254c80a3SJohn Youn 	int num_sp;
1684254c80a3SJohn Youn 	int i;
16854c39d4b9SArnd Bergmann 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
1686254c80a3SJohn Youn 
1687254c80a3SJohn Youn 	if (!xhci->scratchpad)
1688254c80a3SJohn Youn 		return;
1689254c80a3SJohn Youn 
1690254c80a3SJohn Youn 	num_sp = HCS_MAX_SCRATCHPAD(xhci->hcs_params2);
1691254c80a3SJohn Youn 
1692254c80a3SJohn Youn 	for (i = 0; i < num_sp; i++) {
16932a100047SXenia Ragiadakou 		dma_free_coherent(dev, xhci->page_size,
1694254c80a3SJohn Youn 				    xhci->scratchpad->sp_buffers[i],
1695314eaf7dSPeter Chen 				    xhci->scratchpad->sp_array[i]);
1696254c80a3SJohn Youn 	}
1697254c80a3SJohn Youn 	kfree(xhci->scratchpad->sp_buffers);
16980c959508SNiklas Neronin 	dma_free_coherent(dev, array_size(sizeof(u64), num_sp),
1699254c80a3SJohn Youn 			    xhci->scratchpad->sp_array,
1700254c80a3SJohn Youn 			    xhci->scratchpad->sp_dma);
1701254c80a3SJohn Youn 	kfree(xhci->scratchpad);
1702254c80a3SJohn Youn 	xhci->scratchpad = NULL;
1703254c80a3SJohn Youn }
1704254c80a3SJohn Youn 
xhci_alloc_command(struct xhci_hcd * xhci,bool allocate_completion,gfp_t mem_flags)1705913a8a34SSarah Sharp struct xhci_command *xhci_alloc_command(struct xhci_hcd *xhci,
1706103afda0SMathias Nyman 		bool allocate_completion, gfp_t mem_flags)
1707913a8a34SSarah Sharp {
1708913a8a34SSarah Sharp 	struct xhci_command *command;
1709a965315eSAdam Wallis 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
1710913a8a34SSarah Sharp 
1711a965315eSAdam Wallis 	command = kzalloc_node(sizeof(*command), mem_flags, dev_to_node(dev));
1712913a8a34SSarah Sharp 	if (!command)
1713913a8a34SSarah Sharp 		return NULL;
1714913a8a34SSarah Sharp 
1715913a8a34SSarah Sharp 	if (allocate_completion) {
1716913a8a34SSarah Sharp 		command->completion =
1717a965315eSAdam Wallis 			kzalloc_node(sizeof(struct completion), mem_flags,
1718a965315eSAdam Wallis 				dev_to_node(dev));
1719913a8a34SSarah Sharp 		if (!command->completion) {
172006e18291SJulia Lawall 			kfree(command);
1721913a8a34SSarah Sharp 			return NULL;
1722913a8a34SSarah Sharp 		}
1723913a8a34SSarah Sharp 		init_completion(command->completion);
1724913a8a34SSarah Sharp 	}
1725913a8a34SSarah Sharp 
1726913a8a34SSarah Sharp 	command->status = 0;
1727a769154cSHardik Gajjar 	/* set default timeout to 5000 ms */
1728a769154cSHardik Gajjar 	command->timeout_ms = XHCI_CMD_DEFAULT_TIMEOUT;
1729913a8a34SSarah Sharp 	INIT_LIST_HEAD(&command->cmd_list);
1730913a8a34SSarah Sharp 	return command;
1731913a8a34SSarah Sharp }
1732913a8a34SSarah Sharp 
xhci_alloc_command_with_ctx(struct xhci_hcd * xhci,bool allocate_completion,gfp_t mem_flags)173314d49b7aSMathias Nyman struct xhci_command *xhci_alloc_command_with_ctx(struct xhci_hcd *xhci,
173414d49b7aSMathias Nyman 		bool allocate_completion, gfp_t mem_flags)
173514d49b7aSMathias Nyman {
173614d49b7aSMathias Nyman 	struct xhci_command *command;
173714d49b7aSMathias Nyman 
1738103afda0SMathias Nyman 	command = xhci_alloc_command(xhci, allocate_completion, mem_flags);
173914d49b7aSMathias Nyman 	if (!command)
174014d49b7aSMathias Nyman 		return NULL;
174114d49b7aSMathias Nyman 
174214d49b7aSMathias Nyman 	command->in_ctx = xhci_alloc_container_ctx(xhci, XHCI_CTX_TYPE_INPUT,
174314d49b7aSMathias Nyman 						   mem_flags);
174414d49b7aSMathias Nyman 	if (!command->in_ctx) {
174514d49b7aSMathias Nyman 		kfree(command->completion);
174614d49b7aSMathias Nyman 		kfree(command);
174714d49b7aSMathias Nyman 		return NULL;
174814d49b7aSMathias Nyman 	}
174914d49b7aSMathias Nyman 	return command;
175014d49b7aSMathias Nyman }
175114d49b7aSMathias Nyman 
xhci_urb_free_priv(struct urb_priv * urb_priv)17524daf9df5SLin Wang void xhci_urb_free_priv(struct urb_priv *urb_priv)
17538e51adccSAndiry Xu {
17548e51adccSAndiry Xu 	kfree(urb_priv);
17558e51adccSAndiry Xu }
17568e51adccSAndiry Xu 
xhci_free_command(struct xhci_hcd * xhci,struct xhci_command * command)1757913a8a34SSarah Sharp void xhci_free_command(struct xhci_hcd *xhci,
1758913a8a34SSarah Sharp 		struct xhci_command *command)
1759913a8a34SSarah Sharp {
1760913a8a34SSarah Sharp 	xhci_free_container_ctx(xhci,
1761913a8a34SSarah Sharp 			command->in_ctx);
1762913a8a34SSarah Sharp 	kfree(command->completion);
1763913a8a34SSarah Sharp 	kfree(command);
1764913a8a34SSarah Sharp }
1765913a8a34SSarah Sharp 
xhci_alloc_erst(struct xhci_hcd * xhci,struct xhci_ring * evt_ring,struct xhci_erst * erst,gfp_t flags)17663c45a21fSLukas Wunner static int xhci_alloc_erst(struct xhci_hcd *xhci,
176767d2ea9fSLu Baolu 		    struct xhci_ring *evt_ring,
176867d2ea9fSLu Baolu 		    struct xhci_erst *erst,
176967d2ea9fSLu Baolu 		    gfp_t flags)
177067d2ea9fSLu Baolu {
177167d2ea9fSLu Baolu 	size_t size;
177267d2ea9fSLu Baolu 	unsigned int val;
177367d2ea9fSLu Baolu 	struct xhci_segment *seg;
177467d2ea9fSLu Baolu 	struct xhci_erst_entry *entry;
177567d2ea9fSLu Baolu 
17760c959508SNiklas Neronin 	size = array_size(sizeof(struct xhci_erst_entry), evt_ring->num_segs);
1777750afb08SLuis Chamberlain 	erst->entries = dma_alloc_coherent(xhci_to_hcd(xhci)->self.sysdev,
17782b80a29bSHimanshu Jha 					   size, &erst->erst_dma_addr, flags);
177967d2ea9fSLu Baolu 	if (!erst->entries)
178067d2ea9fSLu Baolu 		return -ENOMEM;
178167d2ea9fSLu Baolu 
178267d2ea9fSLu Baolu 	erst->num_entries = evt_ring->num_segs;
178367d2ea9fSLu Baolu 
178467d2ea9fSLu Baolu 	seg = evt_ring->first_seg;
178567d2ea9fSLu Baolu 	for (val = 0; val < evt_ring->num_segs; val++) {
178667d2ea9fSLu Baolu 		entry = &erst->entries[val];
178767d2ea9fSLu Baolu 		entry->seg_addr = cpu_to_le64(seg->dma);
178867d2ea9fSLu Baolu 		entry->seg_size = cpu_to_le32(TRBS_PER_SEGMENT);
178967d2ea9fSLu Baolu 		entry->rsvd = 0;
179067d2ea9fSLu Baolu 		seg = seg->next;
179167d2ea9fSLu Baolu 	}
179267d2ea9fSLu Baolu 
179367d2ea9fSLu Baolu 	return 0;
179467d2ea9fSLu Baolu }
179567d2ea9fSLu Baolu 
1796b17a57f8SMathias Nyman static void
xhci_remove_interrupter(struct xhci_hcd * xhci,struct xhci_interrupter * ir)179747f503cfSMathias Nyman xhci_remove_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir)
179867d2ea9fSLu Baolu {
1799b17a57f8SMathias Nyman 	u32 tmp;
180067d2ea9fSLu Baolu 
1801b17a57f8SMathias Nyman 	if (!ir)
1802b17a57f8SMathias Nyman 		return;
1803b17a57f8SMathias Nyman 
1804b17a57f8SMathias Nyman 	/*
1805b17a57f8SMathias Nyman 	 * Clean out interrupter registers except ERSTBA. Clearing either the
1806b17a57f8SMathias Nyman 	 * low or high 32 bits of ERSTBA immediately causes the controller to
1807b17a57f8SMathias Nyman 	 * dereference the partially cleared 64 bit address, causing IOMMU error.
1808b17a57f8SMathias Nyman 	 */
18094bf398e1SMathias Nyman 	if (ir->ir_set) {
1810b17a57f8SMathias Nyman 		tmp = readl(&ir->ir_set->erst_size);
1811b17a57f8SMathias Nyman 		tmp &= ERST_SIZE_MASK;
1812b17a57f8SMathias Nyman 		writel(tmp, &ir->ir_set->erst_size);
1813b17a57f8SMathias Nyman 
1814044818a6SLukas Wunner 		xhci_write_64(xhci, ERST_EHB, &ir->ir_set->erst_dequeue);
18154bf398e1SMathias Nyman 	}
181647f503cfSMathias Nyman }
1817b17a57f8SMathias Nyman 
181847f503cfSMathias Nyman static void
xhci_free_interrupter(struct xhci_hcd * xhci,struct xhci_interrupter * ir)181947f503cfSMathias Nyman xhci_free_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir)
182047f503cfSMathias Nyman {
182147f503cfSMathias Nyman 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
182247f503cfSMathias Nyman 	size_t erst_size;
182347f503cfSMathias Nyman 
182447f503cfSMathias Nyman 	if (!ir)
182547f503cfSMathias Nyman 		return;
182647f503cfSMathias Nyman 
18270c959508SNiklas Neronin 	erst_size = array_size(sizeof(struct xhci_erst_entry), ir->erst.num_entries);
182847f503cfSMathias Nyman 	if (ir->erst.entries)
182947f503cfSMathias Nyman 		dma_free_coherent(dev, erst_size,
183047f503cfSMathias Nyman 				  ir->erst.entries,
183147f503cfSMathias Nyman 				  ir->erst.erst_dma_addr);
183247f503cfSMathias Nyman 	ir->erst.entries = NULL;
183347f503cfSMathias Nyman 
183447f503cfSMathias Nyman 	/* free interrupter event ring */
1835b17a57f8SMathias Nyman 	if (ir->event_ring)
1836b17a57f8SMathias Nyman 		xhci_ring_free(xhci, ir->event_ring);
183747f503cfSMathias Nyman 
1838b17a57f8SMathias Nyman 	ir->event_ring = NULL;
1839b17a57f8SMathias Nyman 
1840b17a57f8SMathias Nyman 	kfree(ir);
184167d2ea9fSLu Baolu }
184267d2ea9fSLu Baolu 
xhci_remove_secondary_interrupter(struct usb_hcd * hcd,struct xhci_interrupter * ir)1843c99b38c4SMathias Nyman void xhci_remove_secondary_interrupter(struct usb_hcd *hcd, struct xhci_interrupter *ir)
1844c99b38c4SMathias Nyman {
1845c99b38c4SMathias Nyman 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
1846c99b38c4SMathias Nyman 	unsigned int intr_num;
1847c99b38c4SMathias Nyman 
1848c99b38c4SMathias Nyman 	spin_lock_irq(&xhci->lock);
1849c99b38c4SMathias Nyman 
1850a54a594dSMathias Nyman 	/* interrupter 0 is primary interrupter, don't touch it */
1851a54a594dSMathias Nyman 	if (!ir || !ir->intr_num || ir->intr_num >= xhci->max_interrupters) {
1852a54a594dSMathias Nyman 		xhci_dbg(xhci, "Invalid secondary interrupter, can't remove\n");
1853a54a594dSMathias Nyman 		spin_unlock_irq(&xhci->lock);
1854a54a594dSMathias Nyman 		return;
1855a54a594dSMathias Nyman 	}
1856a54a594dSMathias Nyman 
1857c99b38c4SMathias Nyman 	intr_num = ir->intr_num;
1858c99b38c4SMathias Nyman 
1859c99b38c4SMathias Nyman 	xhci_remove_interrupter(xhci, ir);
1860c99b38c4SMathias Nyman 	xhci->interrupters[intr_num] = NULL;
1861c99b38c4SMathias Nyman 
1862c99b38c4SMathias Nyman 	spin_unlock_irq(&xhci->lock);
1863c99b38c4SMathias Nyman 
1864c99b38c4SMathias Nyman 	xhci_free_interrupter(xhci, ir);
1865c99b38c4SMathias Nyman }
1866c99b38c4SMathias Nyman EXPORT_SYMBOL_GPL(xhci_remove_secondary_interrupter);
1867c99b38c4SMathias Nyman 
xhci_mem_cleanup(struct xhci_hcd * xhci)186866d4eaddSSarah Sharp void xhci_mem_cleanup(struct xhci_hcd *xhci)
186966d4eaddSSarah Sharp {
18704c39d4b9SArnd Bergmann 	struct device	*dev = xhci_to_hcd(xhci)->self.sysdev;
187132f1d2c5STakashi Iwai 	int i, j, num_ports;
18720ebbab37SSarah Sharp 
1873cb4d5ce5SOGAWA Hirofumi 	cancel_delayed_work_sync(&xhci->cmd_timer);
1874c311e391SMathias Nyman 
1875dcdb52d9SMarc Zyngier 	for (i = 0; xhci->interrupters && i < xhci->max_interrupters; i++) {
1876c99b38c4SMathias Nyman 		if (xhci->interrupters[i]) {
1877c99b38c4SMathias Nyman 			xhci_remove_interrupter(xhci, xhci->interrupters[i]);
1878c99b38c4SMathias Nyman 			xhci_free_interrupter(xhci, xhci->interrupters[i]);
1879c99b38c4SMathias Nyman 			xhci->interrupters[i] = NULL;
1880c99b38c4SMathias Nyman 		}
1881c99b38c4SMathias Nyman 	}
1882c99b38c4SMathias Nyman 	xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed interrupters");
18830ebbab37SSarah Sharp 
18840ebbab37SSarah Sharp 	if (xhci->cmd_ring)
18850ebbab37SSarah Sharp 		xhci_ring_free(xhci, xhci->cmd_ring);
18860ebbab37SSarah Sharp 	xhci->cmd_ring = NULL;
1887d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed command ring");
1888c9aa1a2dSMathias Nyman 	xhci_cleanup_command_queue(xhci);
18893ffbba95SSarah Sharp 
18905dc2808cSMathias Nyman 	num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
1891c207e7c5SMathias Nyman 	for (i = 0; i < num_ports && xhci->rh_bw; i++) {
18925dc2808cSMathias Nyman 		struct xhci_interval_bw_table *bwt = &xhci->rh_bw[i].bw_table;
18935dc2808cSMathias Nyman 		for (j = 0; j < XHCI_MAX_INTERVAL; j++) {
18945dc2808cSMathias Nyman 			struct list_head *ep = &bwt->interval_bw[j].endpoints;
18955dc2808cSMathias Nyman 			while (!list_empty(ep))
18965dc2808cSMathias Nyman 				list_del_init(ep->next);
18975dc2808cSMathias Nyman 		}
18985dc2808cSMathias Nyman 	}
18995dc2808cSMathias Nyman 
1900ee8665e2SMathias Nyman 	for (i = HCS_MAX_SLOTS(xhci->hcs_params1); i > 0; i--)
1901ee8665e2SMathias Nyman 		xhci_free_virt_devices_depth_first(xhci, i);
19023ffbba95SSarah Sharp 
19030ebbab37SSarah Sharp 	dma_pool_destroy(xhci->segment_pool);
19040ebbab37SSarah Sharp 	xhci->segment_pool = NULL;
1905d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed segment pool");
19063ffbba95SSarah Sharp 
19073ffbba95SSarah Sharp 	dma_pool_destroy(xhci->device_pool);
19083ffbba95SSarah Sharp 	xhci->device_pool = NULL;
1909d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init, "Freed device context pool");
19103ffbba95SSarah Sharp 
19118df75f42SSarah Sharp 	dma_pool_destroy(xhci->small_streams_pool);
19128df75f42SSarah Sharp 	xhci->small_streams_pool = NULL;
1913d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
1914d195fcffSXenia Ragiadakou 			"Freed small stream array pool");
19158df75f42SSarah Sharp 
19168df75f42SSarah Sharp 	dma_pool_destroy(xhci->medium_streams_pool);
19178df75f42SSarah Sharp 	xhci->medium_streams_pool = NULL;
1918d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
1919d195fcffSXenia Ragiadakou 			"Freed medium stream array pool");
19208df75f42SSarah Sharp 
1921a74588f9SSarah Sharp 	if (xhci->dcbaa)
19222a100047SXenia Ragiadakou 		dma_free_coherent(dev, sizeof(*xhci->dcbaa),
1923a74588f9SSarah Sharp 				xhci->dcbaa, xhci->dcbaa->dma);
1924a74588f9SSarah Sharp 	xhci->dcbaa = NULL;
19253ffbba95SSarah Sharp 
19265294bea4SSarah Sharp 	scratchpad_free(xhci);
1927da6699ceSSarah Sharp 
192888696ae4SVladimir Murzin 	if (!xhci->rh_bw)
192988696ae4SVladimir Murzin 		goto no_bw;
193088696ae4SVladimir Murzin 
193132f1d2c5STakashi Iwai 	for (i = 0; i < num_ports; i++) {
193232f1d2c5STakashi Iwai 		struct xhci_tt_bw_info *tt, *n;
193332f1d2c5STakashi Iwai 		list_for_each_entry_safe(tt, n, &xhci->rh_bw[i].tts, tt_list) {
193432f1d2c5STakashi Iwai 			list_del(&tt->tt_list);
193532f1d2c5STakashi Iwai 			kfree(tt);
193632f1d2c5STakashi Iwai 		}
1937f8a9e72dSOliver Neukum 	}
1938f8a9e72dSOliver Neukum 
193988696ae4SVladimir Murzin no_bw:
1940127329d7SHans de Goede 	xhci->cmd_ring_reserved_trbs = 0;
1941bcaa9d5cSMathias Nyman 	xhci->usb2_rhub.num_ports = 0;
1942bcaa9d5cSMathias Nyman 	xhci->usb3_rhub.num_ports = 0;
1943f8a9e72dSOliver Neukum 	xhci->num_active_eps = 0;
1944bcaa9d5cSMathias Nyman 	kfree(xhci->usb2_rhub.ports);
1945bcaa9d5cSMathias Nyman 	kfree(xhci->usb3_rhub.ports);
1946bcaa9d5cSMathias Nyman 	kfree(xhci->hw_ports);
1947839c817cSSarah Sharp 	kfree(xhci->rh_bw);
1948cf0ee7c6SMathias Nyman 	for (i = 0; i < xhci->num_port_caps; i++)
1949cf0ee7c6SMathias Nyman 		kfree(xhci->port_caps[i].psi);
1950cf0ee7c6SMathias Nyman 	kfree(xhci->port_caps);
1951c99b38c4SMathias Nyman 	kfree(xhci->interrupters);
1952cf0ee7c6SMathias Nyman 	xhci->num_port_caps = 0;
1953da6699ceSSarah Sharp 
1954bcaa9d5cSMathias Nyman 	xhci->usb2_rhub.ports = NULL;
1955bcaa9d5cSMathias Nyman 	xhci->usb3_rhub.ports = NULL;
1956bcaa9d5cSMathias Nyman 	xhci->hw_ports = NULL;
195771504062SLu Baolu 	xhci->rh_bw = NULL;
1958b31d9d6dSZhangjiantao (Kirin, nanjing) 	xhci->port_caps = NULL;
1959c99b38c4SMathias Nyman 	xhci->interrupters = NULL;
196071504062SLu Baolu 
196166d4eaddSSarah Sharp 	xhci->page_size = 0;
196266d4eaddSSarah Sharp 	xhci->page_shift = 0;
1963f6187f42SMathias Nyman 	xhci->usb2_rhub.bus_state.bus_suspended = 0;
1964f6187f42SMathias Nyman 	xhci->usb3_rhub.bus_state.bus_suspended = 0;
196566d4eaddSSarah Sharp }
196666d4eaddSSarah Sharp 
xhci_set_hc_event_deq(struct xhci_hcd * xhci,struct xhci_interrupter * ir)1967b17a57f8SMathias Nyman static void xhci_set_hc_event_deq(struct xhci_hcd *xhci, struct xhci_interrupter *ir)
1968257d585aSSarah Sharp {
1969257d585aSSarah Sharp 	dma_addr_t deq;
1970257d585aSSarah Sharp 
1971b17a57f8SMathias Nyman 	deq = xhci_trb_virt_to_dma(ir->event_ring->deq_seg,
1972b17a57f8SMathias Nyman 			ir->event_ring->dequeue);
197396eea587SAhmed S. Darwish 	if (!deq)
19744feb07d0SAndy Shevchenko 		xhci_warn(xhci, "WARN something wrong with SW event ring dequeue ptr.\n");
1975257d585aSSarah Sharp 	/* Update HC event ring dequeue pointer */
1976257d585aSSarah Sharp 	/* Don't clear the EHB bit (which is RW1C) because
1977257d585aSSarah Sharp 	 * there might be more events to service.
1978257d585aSSarah Sharp 	 */
1979d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
19804feb07d0SAndy Shevchenko 		       "// Write event ring dequeue pointer, preserving EHB bit");
198108cc5616SLukas Wunner 	xhci_write_64(xhci, deq & ERST_PTR_MASK, &ir->ir_set->erst_dequeue);
1982257d585aSSarah Sharp }
1983257d585aSSarah Sharp 
xhci_add_in_port(struct xhci_hcd * xhci,unsigned int num_ports,__le32 __iomem * addr,int max_caps)1984da6699ceSSarah Sharp static void xhci_add_in_port(struct xhci_hcd *xhci, unsigned int num_ports,
1985d5ddcdf4SMathias Nyman 		__le32 __iomem *addr, int max_caps)
1986da6699ceSSarah Sharp {
1987da6699ceSSarah Sharp 	u32 temp, port_offset, port_count;
1988da6699ceSSarah Sharp 	int i;
1989d9b0328dSWeitao Wang 	u8 major_revision, minor_revision, tmp_minor_revision;
199047189098SMathias Nyman 	struct xhci_hub *rhub;
1991a965315eSAdam Wallis 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
1992cf0ee7c6SMathias Nyman 	struct xhci_port_cap *port_cap;
1993da6699ceSSarah Sharp 
199447189098SMathias Nyman 	temp = readl(addr);
1995d5ddcdf4SMathias Nyman 	major_revision = XHCI_EXT_PORT_MAJOR(temp);
1996b72eb843SYD Tseng 	minor_revision = XHCI_EXT_PORT_MINOR(temp);
199747189098SMathias Nyman 
1998d5ddcdf4SMathias Nyman 	if (major_revision == 0x03) {
199947189098SMathias Nyman 		rhub = &xhci->usb3_rhub;
200064364bc9SThinh Nguyen 		/*
200164364bc9SThinh Nguyen 		 * Some hosts incorrectly use sub-minor version for minor
200264364bc9SThinh Nguyen 		 * version (i.e. 0x02 instead of 0x20 for bcdUSB 0x320 and 0x01
200364364bc9SThinh Nguyen 		 * for bcdUSB 0x310). Since there is no USB release with sub
200464364bc9SThinh Nguyen 		 * minor version 0x301 to 0x309, we can assume that they are
200564364bc9SThinh Nguyen 		 * incorrect and fix it here.
200664364bc9SThinh Nguyen 		 */
200764364bc9SThinh Nguyen 		if (minor_revision > 0x00 && minor_revision < 0x10)
200864364bc9SThinh Nguyen 			minor_revision <<= 4;
2009d9b0328dSWeitao Wang 		/*
2010d9b0328dSWeitao Wang 		 * Some zhaoxin's xHCI controller that follow usb3.1 spec
2011d9b0328dSWeitao Wang 		 * but only support Gen1.
2012d9b0328dSWeitao Wang 		 */
2013d9b0328dSWeitao Wang 		if (xhci->quirks & XHCI_ZHAOXIN_HOST) {
2014d9b0328dSWeitao Wang 			tmp_minor_revision = minor_revision;
2015d9b0328dSWeitao Wang 			minor_revision = 0;
2016d9b0328dSWeitao Wang 		}
2017d9b0328dSWeitao Wang 
2018d5ddcdf4SMathias Nyman 	} else if (major_revision <= 0x02) {
201947189098SMathias Nyman 		rhub = &xhci->usb2_rhub;
202047189098SMathias Nyman 	} else {
20214feb07d0SAndy Shevchenko 		xhci_warn(xhci, "Ignoring unknown port speed, Ext Cap %p, revision = 0x%x\n",
2022da6699ceSSarah Sharp 				addr, major_revision);
2023da6699ceSSarah Sharp 		/* Ignoring port protocol we can't understand. FIXME */
2024da6699ceSSarah Sharp 		return;
2025da6699ceSSarah Sharp 	}
2026da6699ceSSarah Sharp 
2027da6699ceSSarah Sharp 	/* Port offset and count in the third dword, see section 7.2 */
2028b0ba9720SXenia Ragiadakou 	temp = readl(addr + 2);
2029da6699ceSSarah Sharp 	port_offset = XHCI_EXT_PORT_OFF(temp);
2030da6699ceSSarah Sharp 	port_count = XHCI_EXT_PORT_COUNT(temp);
2031d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
20324feb07d0SAndy Shevchenko 		       "Ext Cap %p, port offset = %u, count = %u, revision = 0x%x",
2033da6699ceSSarah Sharp 		       addr, port_offset, port_count, major_revision);
2034da6699ceSSarah Sharp 	/* Port count includes the current port offset */
2035da6699ceSSarah Sharp 	if (port_offset == 0 || (port_offset + port_count - 1) > num_ports)
2036da6699ceSSarah Sharp 		/* WTF? "Valid values are ‘1’ to MaxPorts" */
2037da6699ceSSarah Sharp 		return;
2038fc71ff75SAndiry Xu 
2039cf0ee7c6SMathias Nyman 	port_cap = &xhci->port_caps[xhci->num_port_caps++];
2040cf0ee7c6SMathias Nyman 	if (xhci->num_port_caps > max_caps)
2041cf0ee7c6SMathias Nyman 		return;
2042fc57313dSMathias Nyman 
2043cf0ee7c6SMathias Nyman 	port_cap->psi_count = XHCI_EXT_PORT_PSIC(temp);
2044cf0ee7c6SMathias Nyman 
2045cf0ee7c6SMathias Nyman 	if (port_cap->psi_count) {
2046cf0ee7c6SMathias Nyman 		port_cap->psi = kcalloc_node(port_cap->psi_count,
2047cf0ee7c6SMathias Nyman 					     sizeof(*port_cap->psi),
2048cf0ee7c6SMathias Nyman 					     GFP_KERNEL, dev_to_node(dev));
2049cf0ee7c6SMathias Nyman 		if (!port_cap->psi)
2050cf0ee7c6SMathias Nyman 			port_cap->psi_count = 0;
2051cf0ee7c6SMathias Nyman 
2052cf0ee7c6SMathias Nyman 		port_cap->psi_uid_count++;
2053cf0ee7c6SMathias Nyman 		for (i = 0; i < port_cap->psi_count; i++) {
2054cf0ee7c6SMathias Nyman 			port_cap->psi[i] = readl(addr + 4 + i);
205547189098SMathias Nyman 
205647189098SMathias Nyman 			/* count unique ID values, two consecutive entries can
205747189098SMathias Nyman 			 * have the same ID if link is assymetric
205847189098SMathias Nyman 			 */
2059cf0ee7c6SMathias Nyman 			if (i && (XHCI_EXT_PORT_PSIV(port_cap->psi[i]) !=
2060cf0ee7c6SMathias Nyman 				  XHCI_EXT_PORT_PSIV(port_cap->psi[i - 1])))
2061cf0ee7c6SMathias Nyman 				port_cap->psi_uid_count++;
206247189098SMathias Nyman 
2063d9b0328dSWeitao Wang 			if (xhci->quirks & XHCI_ZHAOXIN_HOST &&
2064d9b0328dSWeitao Wang 			    major_revision == 0x03 &&
2065d9b0328dSWeitao Wang 			    XHCI_EXT_PORT_PSIV(port_cap->psi[i]) >= 5)
2066d9b0328dSWeitao Wang 				minor_revision = tmp_minor_revision;
2067d9b0328dSWeitao Wang 
206847189098SMathias Nyman 			xhci_dbg(xhci, "PSIV:%d PSIE:%d PLT:%d PFD:%d LP:%d PSIM:%d\n",
2069cf0ee7c6SMathias Nyman 				  XHCI_EXT_PORT_PSIV(port_cap->psi[i]),
2070cf0ee7c6SMathias Nyman 				  XHCI_EXT_PORT_PSIE(port_cap->psi[i]),
2071cf0ee7c6SMathias Nyman 				  XHCI_EXT_PORT_PLT(port_cap->psi[i]),
2072cf0ee7c6SMathias Nyman 				  XHCI_EXT_PORT_PFD(port_cap->psi[i]),
2073cf0ee7c6SMathias Nyman 				  XHCI_EXT_PORT_LP(port_cap->psi[i]),
2074cf0ee7c6SMathias Nyman 				  XHCI_EXT_PORT_PSIM(port_cap->psi[i]));
207547189098SMathias Nyman 		}
207647189098SMathias Nyman 	}
2077d9b0328dSWeitao Wang 
2078d9b0328dSWeitao Wang 	rhub->maj_rev = major_revision;
2079d9b0328dSWeitao Wang 
2080d9b0328dSWeitao Wang 	if (rhub->min_rev < minor_revision)
2081d9b0328dSWeitao Wang 		rhub->min_rev = minor_revision;
2082d9b0328dSWeitao Wang 
2083d9b0328dSWeitao Wang 	port_cap->maj_rev = major_revision;
2084d9b0328dSWeitao Wang 	port_cap->min_rev = minor_revision;
208563a1f845SMathias Nyman 	port_cap->protocol_caps = temp;
2086b630d4b9SMathias Nyman 
2087f1fd62a6SZeng Tao 	if ((xhci->hci_version >= 0x100) && (major_revision != 0x03) &&
2088f1fd62a6SZeng Tao 		 (temp & XHCI_HLC)) {
2089d195fcffSXenia Ragiadakou 		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2090d195fcffSXenia Ragiadakou 			       "xHCI 1.0: support USB2 hardware lpm");
2091fc71ff75SAndiry Xu 		xhci->hw_lpm_support = 1;
2092fc71ff75SAndiry Xu 	}
2093fc71ff75SAndiry Xu 
2094da6699ceSSarah Sharp 	port_offset--;
2095da6699ceSSarah Sharp 	for (i = port_offset; i < (port_offset + port_count); i++) {
2096bcaa9d5cSMathias Nyman 		struct xhci_port *hw_port = &xhci->hw_ports[i];
2097da6699ceSSarah Sharp 		/* Duplicate entry.  Ignore the port if the revisions differ. */
209807f76190SMathias Nyman 		if (hw_port->rhub) {
20994feb07d0SAndy Shevchenko 			xhci_warn(xhci, "Duplicate port entry, Ext Cap %p, port %u\n", addr, i);
21004feb07d0SAndy Shevchenko 			xhci_warn(xhci, "Port was marked as USB %u, duplicated as USB %u\n",
210107f76190SMathias Nyman 					hw_port->rhub->maj_rev, major_revision);
2102da6699ceSSarah Sharp 			/* Only adjust the roothub port counts if we haven't
2103da6699ceSSarah Sharp 			 * found a similar duplicate.
2104da6699ceSSarah Sharp 			 */
2105bcaa9d5cSMathias Nyman 			if (hw_port->rhub != rhub &&
2106bcaa9d5cSMathias Nyman 				 hw_port->hcd_portnum != DUPLICATE_ENTRY) {
2107bcaa9d5cSMathias Nyman 				hw_port->rhub->num_ports--;
2108bcaa9d5cSMathias Nyman 				hw_port->hcd_portnum = DUPLICATE_ENTRY;
2109bcaa9d5cSMathias Nyman 			}
2110f8bbeabcSSarah Sharp 			continue;
2111da6699ceSSarah Sharp 		}
2112bcaa9d5cSMathias Nyman 		hw_port->rhub = rhub;
2113cf0ee7c6SMathias Nyman 		hw_port->port_cap = port_cap;
2114bcaa9d5cSMathias Nyman 		rhub->num_ports++;
2115da6699ceSSarah Sharp 	}
2116da6699ceSSarah Sharp 	/* FIXME: Should we disable ports not in the Extended Capabilities? */
2117da6699ceSSarah Sharp }
2118da6699ceSSarah Sharp 
xhci_create_rhub_port_array(struct xhci_hcd * xhci,struct xhci_hub * rhub,gfp_t flags)2119bcaa9d5cSMathias Nyman static void xhci_create_rhub_port_array(struct xhci_hcd *xhci,
2120bcaa9d5cSMathias Nyman 					struct xhci_hub *rhub, gfp_t flags)
2121bcaa9d5cSMathias Nyman {
2122bcaa9d5cSMathias Nyman 	int port_index = 0;
2123bcaa9d5cSMathias Nyman 	int i;
2124a965315eSAdam Wallis 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
2125bcaa9d5cSMathias Nyman 
2126bcaa9d5cSMathias Nyman 	if (!rhub->num_ports)
2127bcaa9d5cSMathias Nyman 		return;
2128a75e2d60SColin Ian King 	rhub->ports = kcalloc_node(rhub->num_ports, sizeof(*rhub->ports),
2129a75e2d60SColin Ian King 			flags, dev_to_node(dev));
21308a157d2fSMathias Nyman 	if (!rhub->ports)
21318a157d2fSMathias Nyman 		return;
21328a157d2fSMathias Nyman 
2133bcaa9d5cSMathias Nyman 	for (i = 0; i < HCS_MAX_PORTS(xhci->hcs_params1); i++) {
2134bcaa9d5cSMathias Nyman 		if (xhci->hw_ports[i].rhub != rhub ||
2135bcaa9d5cSMathias Nyman 		    xhci->hw_ports[i].hcd_portnum == DUPLICATE_ENTRY)
2136bcaa9d5cSMathias Nyman 			continue;
2137bcaa9d5cSMathias Nyman 		xhci->hw_ports[i].hcd_portnum = port_index;
2138bcaa9d5cSMathias Nyman 		rhub->ports[port_index] = &xhci->hw_ports[i];
2139bcaa9d5cSMathias Nyman 		port_index++;
2140bcaa9d5cSMathias Nyman 		if (port_index == rhub->num_ports)
2141bcaa9d5cSMathias Nyman 			break;
2142bcaa9d5cSMathias Nyman 	}
2143bcaa9d5cSMathias Nyman }
2144bcaa9d5cSMathias Nyman 
2145da6699ceSSarah Sharp /*
2146da6699ceSSarah Sharp  * Scan the Extended Capabilities for the "Supported Protocol Capabilities" that
2147da6699ceSSarah Sharp  * specify what speeds each port is supposed to be.  We can't count on the port
2148da6699ceSSarah Sharp  * speed bits in the PORTSC register being correct until a device is connected,
2149da6699ceSSarah Sharp  * but we need to set up the two fake roothubs with the correct number of USB
2150da6699ceSSarah Sharp  * 3.0 and USB 2.0 ports at host controller initialization time.
2151da6699ceSSarah Sharp  */
xhci_setup_port_arrays(struct xhci_hcd * xhci,gfp_t flags)2152da6699ceSSarah Sharp static int xhci_setup_port_arrays(struct xhci_hcd *xhci, gfp_t flags)
2153da6699ceSSarah Sharp {
2154d5ddcdf4SMathias Nyman 	void __iomem *base;
2155d5ddcdf4SMathias Nyman 	u32 offset;
2156da6699ceSSarah Sharp 	unsigned int num_ports;
215707f76190SMathias Nyman 	int i, j;
2158b630d4b9SMathias Nyman 	int cap_count = 0;
2159d5ddcdf4SMathias Nyman 	u32 cap_start;
2160a965315eSAdam Wallis 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
2161da6699ceSSarah Sharp 
2162da6699ceSSarah Sharp 	num_ports = HCS_MAX_PORTS(xhci->hcs_params1);
2163a965315eSAdam Wallis 	xhci->hw_ports = kcalloc_node(num_ports, sizeof(*xhci->hw_ports),
2164a965315eSAdam Wallis 				flags, dev_to_node(dev));
216507f76190SMathias Nyman 	if (!xhci->hw_ports)
2166da6699ceSSarah Sharp 		return -ENOMEM;
2167da6699ceSSarah Sharp 
2168bcaa9d5cSMathias Nyman 	for (i = 0; i < num_ports; i++) {
2169bcaa9d5cSMathias Nyman 		xhci->hw_ports[i].addr = &xhci->op_regs->port_status_base +
2170bcaa9d5cSMathias Nyman 			NUM_PORT_REGS * i;
2171bcaa9d5cSMathias Nyman 		xhci->hw_ports[i].hw_portnum = i;
21722996e9fcSMathias Nyman 
21732996e9fcSMathias Nyman 		init_completion(&xhci->hw_ports[i].rexit_done);
21742996e9fcSMathias Nyman 		init_completion(&xhci->hw_ports[i].u3exit_done);
2175bcaa9d5cSMathias Nyman 	}
2176bcaa9d5cSMathias Nyman 
2177590b5b7dSKees Cook 	xhci->rh_bw = kcalloc_node(num_ports, sizeof(*xhci->rh_bw), flags,
2178a965315eSAdam Wallis 				   dev_to_node(dev));
2179839c817cSSarah Sharp 	if (!xhci->rh_bw)
2180839c817cSSarah Sharp 		return -ENOMEM;
21812e27980eSSarah Sharp 	for (i = 0; i < num_ports; i++) {
21822e27980eSSarah Sharp 		struct xhci_interval_bw_table *bw_table;
21832e27980eSSarah Sharp 
2184839c817cSSarah Sharp 		INIT_LIST_HEAD(&xhci->rh_bw[i].tts);
21852e27980eSSarah Sharp 		bw_table = &xhci->rh_bw[i].bw_table;
21862e27980eSSarah Sharp 		for (j = 0; j < XHCI_MAX_INTERVAL; j++)
21872e27980eSSarah Sharp 			INIT_LIST_HEAD(&bw_table->interval_bw[j].endpoints);
21882e27980eSSarah Sharp 	}
2189d5ddcdf4SMathias Nyman 	base = &xhci->cap_regs->hc_capbase;
2190839c817cSSarah Sharp 
2191d5ddcdf4SMathias Nyman 	cap_start = xhci_find_next_ext_cap(base, 0, XHCI_EXT_CAPS_PROTOCOL);
2192d5ddcdf4SMathias Nyman 	if (!cap_start) {
2193d5ddcdf4SMathias Nyman 		xhci_err(xhci, "No Extended Capability registers, unable to set up roothub\n");
2194d5ddcdf4SMathias Nyman 		return -ENODEV;
2195d5ddcdf4SMathias Nyman 	}
2196b630d4b9SMathias Nyman 
2197d5ddcdf4SMathias Nyman 	offset = cap_start;
2198b630d4b9SMathias Nyman 	/* count extended protocol capability entries for later caching */
2199d5ddcdf4SMathias Nyman 	while (offset) {
2200b630d4b9SMathias Nyman 		cap_count++;
2201d5ddcdf4SMathias Nyman 		offset = xhci_find_next_ext_cap(base, offset,
2202d5ddcdf4SMathias Nyman 						      XHCI_EXT_CAPS_PROTOCOL);
2203d5ddcdf4SMathias Nyman 	}
2204b630d4b9SMathias Nyman 
2205cf0ee7c6SMathias Nyman 	xhci->port_caps = kcalloc_node(cap_count, sizeof(*xhci->port_caps),
2206cf0ee7c6SMathias Nyman 				flags, dev_to_node(dev));
2207cf0ee7c6SMathias Nyman 	if (!xhci->port_caps)
2208cf0ee7c6SMathias Nyman 		return -ENOMEM;
2209cf0ee7c6SMathias Nyman 
2210d5ddcdf4SMathias Nyman 	offset = cap_start;
2211da6699ceSSarah Sharp 
2212d5ddcdf4SMathias Nyman 	while (offset) {
2213d5ddcdf4SMathias Nyman 		xhci_add_in_port(xhci, num_ports, base + offset, cap_count);
2214bcaa9d5cSMathias Nyman 		if (xhci->usb2_rhub.num_ports + xhci->usb3_rhub.num_ports ==
2215bcaa9d5cSMathias Nyman 		    num_ports)
2216bcaa9d5cSMathias Nyman 			break;
2217d5ddcdf4SMathias Nyman 		offset = xhci_find_next_ext_cap(base, offset,
2218d5ddcdf4SMathias Nyman 						XHCI_EXT_CAPS_PROTOCOL);
2219da6699ceSSarah Sharp 	}
2220bcaa9d5cSMathias Nyman 	if (xhci->usb2_rhub.num_ports == 0 && xhci->usb3_rhub.num_ports == 0) {
2221bcaa9d5cSMathias Nyman 		xhci_warn(xhci, "No ports on the roothubs?\n");
2222bcaa9d5cSMathias Nyman 		return -ENODEV;
2223bcaa9d5cSMathias Nyman 	}
2224d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2225d195fcffSXenia Ragiadakou 		       "Found %u USB 2.0 ports and %u USB 3.0 ports.",
222607f76190SMathias Nyman 		       xhci->usb2_rhub.num_ports, xhci->usb3_rhub.num_ports);
2227d30b2a20SSarah Sharp 
2228d30b2a20SSarah Sharp 	/* Place limits on the number of roothub ports so that the hub
2229d30b2a20SSarah Sharp 	 * descriptors aren't longer than the USB core will allocate.
2230d30b2a20SSarah Sharp 	 */
223107f76190SMathias Nyman 	if (xhci->usb3_rhub.num_ports > USB_SS_MAXPORTS) {
2232d195fcffSXenia Ragiadakou 		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
22335120a266SJohan Hovold 				"Limiting USB 3.0 roothub ports to %u.",
22345120a266SJohan Hovold 				USB_SS_MAXPORTS);
223507f76190SMathias Nyman 		xhci->usb3_rhub.num_ports = USB_SS_MAXPORTS;
2236d30b2a20SSarah Sharp 	}
223707f76190SMathias Nyman 	if (xhci->usb2_rhub.num_ports > USB_MAXCHILDREN) {
2238d195fcffSXenia Ragiadakou 		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2239d195fcffSXenia Ragiadakou 				"Limiting USB 2.0 roothub ports to %u.",
2240d30b2a20SSarah Sharp 				USB_MAXCHILDREN);
224107f76190SMathias Nyman 		xhci->usb2_rhub.num_ports = USB_MAXCHILDREN;
2242d30b2a20SSarah Sharp 	}
2243d30b2a20SSarah Sharp 
2244873f3236SHeiner Kallweit 	if (!xhci->usb2_rhub.num_ports)
2245873f3236SHeiner Kallweit 		xhci_info(xhci, "USB2 root hub has no ports\n");
2246873f3236SHeiner Kallweit 
2247873f3236SHeiner Kallweit 	if (!xhci->usb3_rhub.num_ports)
2248873f3236SHeiner Kallweit 		xhci_info(xhci, "USB3 root hub has no ports\n");
2249bcaa9d5cSMathias Nyman 
2250bcaa9d5cSMathias Nyman 	xhci_create_rhub_port_array(xhci, &xhci->usb2_rhub, flags);
2251bcaa9d5cSMathias Nyman 	xhci_create_rhub_port_array(xhci, &xhci->usb3_rhub, flags);
2252bcaa9d5cSMathias Nyman 
2253da6699ceSSarah Sharp 	return 0;
2254da6699ceSSarah Sharp }
22556648f29dSSarah Sharp 
2256b17a57f8SMathias Nyman static struct xhci_interrupter *
xhci_alloc_interrupter(struct xhci_hcd * xhci,unsigned int segs,gfp_t flags)2257db4460b6SNiklas Neronin xhci_alloc_interrupter(struct xhci_hcd *xhci, unsigned int segs, gfp_t flags)
2258b17a57f8SMathias Nyman {
2259b17a57f8SMathias Nyman 	struct device *dev = xhci_to_hcd(xhci)->self.sysdev;
2260b17a57f8SMathias Nyman 	struct xhci_interrupter *ir;
2261db4460b6SNiklas Neronin 	unsigned int max_segs;
2262b17a57f8SMathias Nyman 	int ret;
2263b17a57f8SMathias Nyman 
2264db4460b6SNiklas Neronin 	if (!segs)
2265db4460b6SNiklas Neronin 		segs = ERST_DEFAULT_SEGS;
2266db4460b6SNiklas Neronin 
2267db4460b6SNiklas Neronin 	max_segs = BIT(HCS_ERST_MAX(xhci->hcs_params2));
2268db4460b6SNiklas Neronin 	segs = min(segs, max_segs);
2269db4460b6SNiklas Neronin 
2270b17a57f8SMathias Nyman 	ir = kzalloc_node(sizeof(*ir), flags, dev_to_node(dev));
2271b17a57f8SMathias Nyman 	if (!ir)
2272b17a57f8SMathias Nyman 		return NULL;
2273b17a57f8SMathias Nyman 
2274db4460b6SNiklas Neronin 	ir->event_ring = xhci_ring_alloc(xhci, segs, 1, TYPE_EVENT, 0, flags);
2275b17a57f8SMathias Nyman 	if (!ir->event_ring) {
22764bf398e1SMathias Nyman 		xhci_warn(xhci, "Failed to allocate interrupter event ring\n");
22774bf398e1SMathias Nyman 		kfree(ir);
22784bf398e1SMathias Nyman 		return NULL;
2279b17a57f8SMathias Nyman 	}
2280b17a57f8SMathias Nyman 
2281b17a57f8SMathias Nyman 	ret = xhci_alloc_erst(xhci, ir->event_ring, &ir->erst, flags);
2282b17a57f8SMathias Nyman 	if (ret) {
22834bf398e1SMathias Nyman 		xhci_warn(xhci, "Failed to allocate interrupter erst\n");
22844bf398e1SMathias Nyman 		xhci_ring_free(xhci, ir->event_ring);
22854bf398e1SMathias Nyman 		kfree(ir);
22864bf398e1SMathias Nyman 		return NULL;
2287b17a57f8SMathias Nyman 	}
22884bf398e1SMathias Nyman 
22894bf398e1SMathias Nyman 	return ir;
22904bf398e1SMathias Nyman }
22914bf398e1SMathias Nyman 
22924bf398e1SMathias Nyman static int
xhci_add_interrupter(struct xhci_hcd * xhci,struct xhci_interrupter * ir,unsigned int intr_num)22934bf398e1SMathias Nyman xhci_add_interrupter(struct xhci_hcd *xhci, struct xhci_interrupter *ir,
22944bf398e1SMathias Nyman 		     unsigned int intr_num)
22954bf398e1SMathias Nyman {
22964bf398e1SMathias Nyman 	u64 erst_base;
22974bf398e1SMathias Nyman 	u32 erst_size;
22984bf398e1SMathias Nyman 
229909f19722SMathias Nyman 	if (intr_num >= xhci->max_interrupters) {
23004bf398e1SMathias Nyman 		xhci_warn(xhci, "Can't add interrupter %d, max interrupters %d\n",
23014bf398e1SMathias Nyman 			  intr_num, xhci->max_interrupters);
23024bf398e1SMathias Nyman 		return -EINVAL;
23034bf398e1SMathias Nyman 	}
23044bf398e1SMathias Nyman 
2305c99b38c4SMathias Nyman 	if (xhci->interrupters[intr_num]) {
2306c99b38c4SMathias Nyman 		xhci_warn(xhci, "Interrupter %d\n already set up", intr_num);
2307c99b38c4SMathias Nyman 		return -EINVAL;
2308c99b38c4SMathias Nyman 	}
2309c99b38c4SMathias Nyman 
2310c99b38c4SMathias Nyman 	xhci->interrupters[intr_num] = ir;
2311c99b38c4SMathias Nyman 	ir->intr_num = intr_num;
23124bf398e1SMathias Nyman 	ir->ir_set = &xhci->run_regs->ir_set[intr_num];
23134bf398e1SMathias Nyman 
2314b17a57f8SMathias Nyman 	/* set ERST count with the number of entries in the segment table */
2315b17a57f8SMathias Nyman 	erst_size = readl(&ir->ir_set->erst_size);
2316b17a57f8SMathias Nyman 	erst_size &= ERST_SIZE_MASK;
231728084d3fSJonathan Bell 	erst_size |= ir->event_ring->num_segs;
2318b17a57f8SMathias Nyman 	writel(erst_size, &ir->ir_set->erst_size);
2319b17a57f8SMathias Nyman 
2320b17a57f8SMathias Nyman 	erst_base = xhci_read_64(xhci, &ir->ir_set->erst_base);
2321cf97c5e0SLukas Wunner 	erst_base &= ERST_BASE_RSVDP;
2322cf97c5e0SLukas Wunner 	erst_base |= ir->erst.erst_dma_addr & ~ERST_BASE_RSVDP;
2323bc162403SDaehwan Jung 	if (xhci->quirks & XHCI_WRITE_64_HI_LO)
2324bc162403SDaehwan Jung 		hi_lo_writeq(erst_base, &ir->ir_set->erst_base);
2325bc162403SDaehwan Jung 	else
2326b17a57f8SMathias Nyman 		xhci_write_64(xhci, erst_base, &ir->ir_set->erst_base);
2327b17a57f8SMathias Nyman 
2328b17a57f8SMathias Nyman 	/* Set the event ring dequeue address of this interrupter */
2329b17a57f8SMathias Nyman 	xhci_set_hc_event_deq(xhci, ir);
2330b17a57f8SMathias Nyman 
23314bf398e1SMathias Nyman 	return 0;
2332b17a57f8SMathias Nyman }
2333b17a57f8SMathias Nyman 
2334c99b38c4SMathias Nyman struct xhci_interrupter *
xhci_create_secondary_interrupter(struct usb_hcd * hcd,unsigned int segs,u32 imod_interval)2335*9c0c11bbSMathias Nyman xhci_create_secondary_interrupter(struct usb_hcd *hcd, unsigned int segs,
2336*9c0c11bbSMathias Nyman 				  u32 imod_interval)
2337c99b38c4SMathias Nyman {
2338c99b38c4SMathias Nyman 	struct xhci_hcd *xhci = hcd_to_xhci(hcd);
2339c99b38c4SMathias Nyman 	struct xhci_interrupter *ir;
2340c99b38c4SMathias Nyman 	unsigned int i;
2341c99b38c4SMathias Nyman 	int err = -ENOSPC;
2342c99b38c4SMathias Nyman 
2343c99b38c4SMathias Nyman 	if (!xhci->interrupters || xhci->max_interrupters <= 1)
2344c99b38c4SMathias Nyman 		return NULL;
2345c99b38c4SMathias Nyman 
2346db4460b6SNiklas Neronin 	ir = xhci_alloc_interrupter(xhci, segs, GFP_KERNEL);
2347c99b38c4SMathias Nyman 	if (!ir)
2348c99b38c4SMathias Nyman 		return NULL;
2349c99b38c4SMathias Nyman 
2350c99b38c4SMathias Nyman 	spin_lock_irq(&xhci->lock);
2351c99b38c4SMathias Nyman 
2352c99b38c4SMathias Nyman 	/* Find available secondary interrupter, interrupter 0 is reserved for primary */
2353c99b38c4SMathias Nyman 	for (i = 1; i < xhci->max_interrupters; i++) {
2354c99b38c4SMathias Nyman 		if (xhci->interrupters[i] == NULL) {
2355c99b38c4SMathias Nyman 			err = xhci_add_interrupter(xhci, ir, i);
2356c99b38c4SMathias Nyman 			break;
2357c99b38c4SMathias Nyman 		}
2358c99b38c4SMathias Nyman 	}
2359c99b38c4SMathias Nyman 
2360c99b38c4SMathias Nyman 	spin_unlock_irq(&xhci->lock);
2361c99b38c4SMathias Nyman 
2362c99b38c4SMathias Nyman 	if (err) {
2363c99b38c4SMathias Nyman 		xhci_warn(xhci, "Failed to add secondary interrupter, max interrupters %d\n",
2364c99b38c4SMathias Nyman 			  xhci->max_interrupters);
2365c99b38c4SMathias Nyman 		xhci_free_interrupter(xhci, ir);
2366c99b38c4SMathias Nyman 		return NULL;
2367c99b38c4SMathias Nyman 	}
2368c99b38c4SMathias Nyman 
2369*9c0c11bbSMathias Nyman 	err = xhci_set_interrupter_moderation(ir, imod_interval);
2370*9c0c11bbSMathias Nyman 	if (err)
2371*9c0c11bbSMathias Nyman 		xhci_warn(xhci, "Failed to set interrupter %d moderation to %uns\n",
2372*9c0c11bbSMathias Nyman 			  i, imod_interval);
2373*9c0c11bbSMathias Nyman 
2374c99b38c4SMathias Nyman 	xhci_dbg(xhci, "Add secondary interrupter %d, max interrupters %d\n",
2375c99b38c4SMathias Nyman 		 i, xhci->max_interrupters);
2376c99b38c4SMathias Nyman 
2377c99b38c4SMathias Nyman 	return ir;
2378c99b38c4SMathias Nyman }
2379c99b38c4SMathias Nyman EXPORT_SYMBOL_GPL(xhci_create_secondary_interrupter);
2380c99b38c4SMathias Nyman 
xhci_mem_init(struct xhci_hcd * xhci,gfp_t flags)238166d4eaddSSarah Sharp int xhci_mem_init(struct xhci_hcd *xhci, gfp_t flags)
238266d4eaddSSarah Sharp {
2383c99b38c4SMathias Nyman 	struct xhci_interrupter *ir;
23844c39d4b9SArnd Bergmann 	struct device	*dev = xhci_to_hcd(xhci)->self.sysdev;
2385c99b38c4SMathias Nyman 	dma_addr_t	dma;
238666d4eaddSSarah Sharp 	unsigned int	val, val2;
23878e595a5dSSarah Sharp 	u64		val_64;
2388623bef9eSSarah Sharp 	u32		page_size, temp;
2389b17a57f8SMathias Nyman 	int		i;
239066d4eaddSSarah Sharp 
2391c9aa1a2dSMathias Nyman 	INIT_LIST_HEAD(&xhci->cmd_list);
2392331de00aSSergio Aguirre 
2393cb4d5ce5SOGAWA Hirofumi 	/* init command timeout work */
2394cb4d5ce5SOGAWA Hirofumi 	INIT_DELAYED_WORK(&xhci->cmd_timer, xhci_handle_command_timeout);
23951c111b6cSOGAWA Hirofumi 	init_completion(&xhci->cmd_ring_stop_completion);
2396cc8e4fc0SMathias Nyman 
2397b0ba9720SXenia Ragiadakou 	page_size = readl(&xhci->op_regs->page_size);
2398d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2399d195fcffSXenia Ragiadakou 			"Supported page size register = 0x%x", page_size);
240081720ec5SLinyu Yuan 	i = ffs(page_size);
240166d4eaddSSarah Sharp 	if (i < 16)
2402d195fcffSXenia Ragiadakou 		xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2403d195fcffSXenia Ragiadakou 			"Supported page size of %iK", (1 << (i+12)) / 1024);
240466d4eaddSSarah Sharp 	else
240566d4eaddSSarah Sharp 		xhci_warn(xhci, "WARN: no supported page size\n");
240666d4eaddSSarah Sharp 	/* Use 4K pages, since that's common and the minimum the HC supports */
240766d4eaddSSarah Sharp 	xhci->page_shift = 12;
240866d4eaddSSarah Sharp 	xhci->page_size = 1 << xhci->page_shift;
2409d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2410d195fcffSXenia Ragiadakou 			"HCD page size set to %iK", xhci->page_size / 1024);
241166d4eaddSSarah Sharp 
241266d4eaddSSarah Sharp 	/*
241366d4eaddSSarah Sharp 	 * Program the Number of Device Slots Enabled field in the CONFIG
241466d4eaddSSarah Sharp 	 * register with the max value of slots the HC can handle.
241566d4eaddSSarah Sharp 	 */
2416b0ba9720SXenia Ragiadakou 	val = HCS_MAX_SLOTS(readl(&xhci->cap_regs->hcs_params1));
2417d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2418d195fcffSXenia Ragiadakou 			"// xHC can handle at most %d device slots.", val);
2419b0ba9720SXenia Ragiadakou 	val2 = readl(&xhci->op_regs->config_reg);
242066d4eaddSSarah Sharp 	val |= (val2 & ~HCS_SLOTS_MASK);
2421d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2422d195fcffSXenia Ragiadakou 			"// Setting Max device slots reg = 0x%x.", val);
2423204b7793SXenia Ragiadakou 	writel(val, &xhci->op_regs->config_reg);
242466d4eaddSSarah Sharp 
24250ebbab37SSarah Sharp 	/*
2426ddfaee62SLinyu Yuan 	 * xHCI section 5.4.6 - Device Context array must be
2427a74588f9SSarah Sharp 	 * "physically contiguous and 64-byte (cache line) aligned".
2428a74588f9SSarah Sharp 	 */
242922d45f01SSebastian Andrzej Siewior 	xhci->dcbaa = dma_alloc_coherent(dev, sizeof(*xhci->dcbaa), &dma,
2430c95a9f83SDan Carpenter 			flags);
2431a74588f9SSarah Sharp 	if (!xhci->dcbaa)
2432a74588f9SSarah Sharp 		goto fail;
2433a74588f9SSarah Sharp 	xhci->dcbaa->dma = dma;
2434d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
243596be93a8SAndy Shevchenko 			"// Device context base array address = 0x%pad (DMA), %p (virt)",
243696be93a8SAndy Shevchenko 			&xhci->dcbaa->dma, xhci->dcbaa);
2437477632dfSSarah Sharp 	xhci_write_64(xhci, dma, &xhci->op_regs->dcbaa_ptr);
2438a74588f9SSarah Sharp 
2439a74588f9SSarah Sharp 	/*
24400ebbab37SSarah Sharp 	 * Initialize the ring segment pool.  The ring must be a contiguous
24410ebbab37SSarah Sharp 	 * structure comprised of TRBs.  The TRBs must be 16 byte aligned,
244284c1e40fSHans de Goede 	 * however, the command ring segment needs 64-byte aligned segments
244384c1e40fSHans de Goede 	 * and our use of dma addresses in the trb_address_map radix tree needs
244484c1e40fSHans de Goede 	 * TRB_SEGMENT_SIZE alignment, so we pick the greater alignment need.
24450ebbab37SSarah Sharp 	 */
24462a865a65SWeitao Wang 	if (xhci->quirks & XHCI_ZHAOXIN_TRB_FETCH)
24472a865a65SWeitao Wang 		xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
24482a865a65SWeitao Wang 				TRB_SEGMENT_SIZE * 2, TRB_SEGMENT_SIZE * 2, xhci->page_size * 2);
24492a865a65SWeitao Wang 	else
24500ebbab37SSarah Sharp 		xhci->segment_pool = dma_pool_create("xHCI ring segments", dev,
245184c1e40fSHans de Goede 				TRB_SEGMENT_SIZE, TRB_SEGMENT_SIZE, xhci->page_size);
2452d115b048SJohn Youn 
24533ffbba95SSarah Sharp 	/* See Table 46 and Note on Figure 55 */
24543ffbba95SSarah Sharp 	xhci->device_pool = dma_pool_create("xHCI input/output contexts", dev,
2455d115b048SJohn Youn 			2112, 64, xhci->page_size);
24563ffbba95SSarah Sharp 	if (!xhci->segment_pool || !xhci->device_pool)
24570ebbab37SSarah Sharp 		goto fail;
24580ebbab37SSarah Sharp 
24598df75f42SSarah Sharp 	/* Linear stream context arrays don't have any boundary restrictions,
24608df75f42SSarah Sharp 	 * and only need to be 16-byte aligned.
24618df75f42SSarah Sharp 	 */
24628df75f42SSarah Sharp 	xhci->small_streams_pool =
24638df75f42SSarah Sharp 		dma_pool_create("xHCI 256 byte stream ctx arrays",
24648df75f42SSarah Sharp 			dev, SMALL_STREAM_ARRAY_SIZE, 16, 0);
24658df75f42SSarah Sharp 	xhci->medium_streams_pool =
24668df75f42SSarah Sharp 		dma_pool_create("xHCI 1KB stream ctx arrays",
24678df75f42SSarah Sharp 			dev, MEDIUM_STREAM_ARRAY_SIZE, 16, 0);
24688df75f42SSarah Sharp 	/* Any stream context array bigger than MEDIUM_STREAM_ARRAY_SIZE
246922d45f01SSebastian Andrzej Siewior 	 * will be allocated with dma_alloc_coherent()
24708df75f42SSarah Sharp 	 */
24718df75f42SSarah Sharp 
24728df75f42SSarah Sharp 	if (!xhci->small_streams_pool || !xhci->medium_streams_pool)
24738df75f42SSarah Sharp 		goto fail;
24748df75f42SSarah Sharp 
24750ebbab37SSarah Sharp 	/* Set up the command ring to have one segments for now. */
2476f9c589e1SMathias Nyman 	xhci->cmd_ring = xhci_ring_alloc(xhci, 1, 1, TYPE_COMMAND, 0, flags);
24770ebbab37SSarah Sharp 	if (!xhci->cmd_ring)
24780ebbab37SSarah Sharp 		goto fail;
2479d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2480d195fcffSXenia Ragiadakou 			"Allocated command ring at %p", xhci->cmd_ring);
248196be93a8SAndy Shevchenko 	xhci_dbg_trace(xhci, trace_xhci_dbg_init, "First segment DMA is 0x%pad",
248296be93a8SAndy Shevchenko 			&xhci->cmd_ring->first_seg->dma);
24830ebbab37SSarah Sharp 
24840ebbab37SSarah Sharp 	/* Set the address in the Command Ring Control register */
2485f7b2e403SSarah Sharp 	val_64 = xhci_read_64(xhci, &xhci->op_regs->cmd_ring);
24868e595a5dSSarah Sharp 	val_64 = (val_64 & (u64) CMD_RING_RSVD_BITS) |
24878e595a5dSSarah Sharp 		(xhci->cmd_ring->first_seg->dma & (u64) ~CMD_RING_RSVD_BITS) |
24880ebbab37SSarah Sharp 		xhci->cmd_ring->cycle_state;
2489d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
24906fc091fbSPeter Chen 			"// Setting command ring address to 0x%016llx", val_64);
2491477632dfSSarah Sharp 	xhci_write_64(xhci, val_64, &xhci->op_regs->cmd_ring);
24920ebbab37SSarah Sharp 
2493dbc33303SSarah Sharp 	/* Reserve one command ring TRB for disabling LPM.
2494dbc33303SSarah Sharp 	 * Since the USB core grabs the shared usb_bus bandwidth mutex before
2495dbc33303SSarah Sharp 	 * disabling LPM, we only need to reserve one TRB for all devices.
2496dbc33303SSarah Sharp 	 */
2497dbc33303SSarah Sharp 	xhci->cmd_ring_reserved_trbs++;
2498dbc33303SSarah Sharp 
2499b0ba9720SXenia Ragiadakou 	val = readl(&xhci->cap_regs->db_off);
25000ebbab37SSarah Sharp 	val &= DBOFF_MASK;
2501d195fcffSXenia Ragiadakou 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
25024feb07d0SAndy Shevchenko 		       "// Doorbell array is located at offset 0x%x from cap regs base addr",
25034feb07d0SAndy Shevchenko 		       val);
2504c50a00f8SDmitry Torokhov 	xhci->dba = (void __iomem *) xhci->cap_regs + val;
25050ebbab37SSarah Sharp 
25064bf398e1SMathias Nyman 	/* Allocate and set up primary interrupter 0 with an event ring. */
2507b17a57f8SMathias Nyman 	xhci_dbg_trace(xhci, trace_xhci_dbg_init,
2508b17a57f8SMathias Nyman 		       "Allocating primary event ring");
2509c99b38c4SMathias Nyman 	xhci->interrupters = kcalloc_node(xhci->max_interrupters, sizeof(*xhci->interrupters),
2510c99b38c4SMathias Nyman 					  flags, dev_to_node(dev));
2511c99b38c4SMathias Nyman 
2512c99b38c4SMathias Nyman 	ir = xhci_alloc_interrupter(xhci, 0, flags);
2513c99b38c4SMathias Nyman 	if (!ir)
25140ebbab37SSarah Sharp 		goto fail;
25150ebbab37SSarah Sharp 
2516c99b38c4SMathias Nyman 	if (xhci_add_interrupter(xhci, ir, 0))
25174bf398e1SMathias Nyman 		goto fail;
25184bf398e1SMathias Nyman 
2519becbd202SMathias Nyman 	ir->isoc_bei_interval = AVOID_BEI_INTERVAL_MAX;
252090d551a5SMathias Nyman 
25210ebbab37SSarah Sharp 	/*
25220ebbab37SSarah Sharp 	 * XXX: Might need to set the Interrupter Moderation Register to
25230ebbab37SSarah Sharp 	 * something other than the default (~1ms minimum between interrupts).
25240ebbab37SSarah Sharp 	 * See section 5.5.1.2.
25250ebbab37SSarah Sharp 	 */
252698871e94SFelipe Balbi 	for (i = 0; i < MAX_HC_SLOTS; i++)
2527326b4810SRandy Dunlap 		xhci->devs[i] = NULL;
252866d4eaddSSarah Sharp 
2529254c80a3SJohn Youn 	if (scratchpad_alloc(xhci, flags))
2530254c80a3SJohn Youn 		goto fail;
2531da6699ceSSarah Sharp 	if (xhci_setup_port_arrays(xhci, flags))
2532da6699ceSSarah Sharp 		goto fail;
2533254c80a3SJohn Youn 
2534623bef9eSSarah Sharp 	/* Enable USB 3.0 device notifications for function remote wake, which
2535623bef9eSSarah Sharp 	 * is necessary for allowing USB 3.0 devices to do remote wakeup from
2536623bef9eSSarah Sharp 	 * U3 (device suspend).
2537623bef9eSSarah Sharp 	 */
2538b0ba9720SXenia Ragiadakou 	temp = readl(&xhci->op_regs->dev_notification);
2539623bef9eSSarah Sharp 	temp &= ~DEV_NOTE_MASK;
2540623bef9eSSarah Sharp 	temp |= DEV_NOTE_FWAKE;
2541204b7793SXenia Ragiadakou 	writel(temp, &xhci->op_regs->dev_notification);
2542623bef9eSSarah Sharp 
254366d4eaddSSarah Sharp 	return 0;
2544254c80a3SJohn Youn 
254566d4eaddSSarah Sharp fail:
2546159e1fccSSarah Sharp 	xhci_halt(xhci);
254714073ce9SMathias Nyman 	xhci_reset(xhci, XHCI_RESET_SHORT_USEC);
254866d4eaddSSarah Sharp 	xhci_mem_cleanup(xhci);
254966d4eaddSSarah Sharp 	return -ENOMEM;
255066d4eaddSSarah Sharp }
2551