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