xref: /OK3568_Linux_fs/kernel/drivers/thunderbolt/cap.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * Thunderbolt driver - capabilities lookup
4*4882a593Smuzhiyun  *
5*4882a593Smuzhiyun  * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6*4882a593Smuzhiyun  * Copyright (C) 2018, Intel Corporation
7*4882a593Smuzhiyun  */
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun #include <linux/slab.h>
10*4882a593Smuzhiyun #include <linux/errno.h>
11*4882a593Smuzhiyun 
12*4882a593Smuzhiyun #include "tb.h"
13*4882a593Smuzhiyun 
14*4882a593Smuzhiyun #define CAP_OFFSET_MAX		0xff
15*4882a593Smuzhiyun #define VSE_CAP_OFFSET_MAX	0xffff
16*4882a593Smuzhiyun #define TMU_ACCESS_EN		BIT(20)
17*4882a593Smuzhiyun 
tb_port_enable_tmu(struct tb_port * port,bool enable)18*4882a593Smuzhiyun static int tb_port_enable_tmu(struct tb_port *port, bool enable)
19*4882a593Smuzhiyun {
20*4882a593Smuzhiyun 	struct tb_switch *sw = port->sw;
21*4882a593Smuzhiyun 	u32 value, offset;
22*4882a593Smuzhiyun 	int ret;
23*4882a593Smuzhiyun 
24*4882a593Smuzhiyun 	/*
25*4882a593Smuzhiyun 	 * Legacy devices need to have TMU access enabled before port
26*4882a593Smuzhiyun 	 * space can be fully accessed.
27*4882a593Smuzhiyun 	 */
28*4882a593Smuzhiyun 	if (tb_switch_is_light_ridge(sw))
29*4882a593Smuzhiyun 		offset = 0x26;
30*4882a593Smuzhiyun 	else if (tb_switch_is_eagle_ridge(sw))
31*4882a593Smuzhiyun 		offset = 0x2a;
32*4882a593Smuzhiyun 	else
33*4882a593Smuzhiyun 		return 0;
34*4882a593Smuzhiyun 
35*4882a593Smuzhiyun 	ret = tb_sw_read(sw, &value, TB_CFG_SWITCH, offset, 1);
36*4882a593Smuzhiyun 	if (ret)
37*4882a593Smuzhiyun 		return ret;
38*4882a593Smuzhiyun 
39*4882a593Smuzhiyun 	if (enable)
40*4882a593Smuzhiyun 		value |= TMU_ACCESS_EN;
41*4882a593Smuzhiyun 	else
42*4882a593Smuzhiyun 		value &= ~TMU_ACCESS_EN;
43*4882a593Smuzhiyun 
44*4882a593Smuzhiyun 	return tb_sw_write(sw, &value, TB_CFG_SWITCH, offset, 1);
45*4882a593Smuzhiyun }
46*4882a593Smuzhiyun 
tb_port_dummy_read(struct tb_port * port)47*4882a593Smuzhiyun static void tb_port_dummy_read(struct tb_port *port)
48*4882a593Smuzhiyun {
49*4882a593Smuzhiyun 	/*
50*4882a593Smuzhiyun 	 * When reading from next capability pointer location in port
51*4882a593Smuzhiyun 	 * config space the read data is not cleared on LR. To avoid
52*4882a593Smuzhiyun 	 * reading stale data on next read perform one dummy read after
53*4882a593Smuzhiyun 	 * port capabilities are walked.
54*4882a593Smuzhiyun 	 */
55*4882a593Smuzhiyun 	if (tb_switch_is_light_ridge(port->sw)) {
56*4882a593Smuzhiyun 		u32 dummy;
57*4882a593Smuzhiyun 
58*4882a593Smuzhiyun 		tb_port_read(port, &dummy, TB_CFG_PORT, 0, 1);
59*4882a593Smuzhiyun 	}
60*4882a593Smuzhiyun }
61*4882a593Smuzhiyun 
62*4882a593Smuzhiyun /**
63*4882a593Smuzhiyun  * tb_port_next_cap() - Return next capability in the linked list
64*4882a593Smuzhiyun  * @port: Port to find the capability for
65*4882a593Smuzhiyun  * @offset: Previous capability offset (%0 for start)
66*4882a593Smuzhiyun  *
67*4882a593Smuzhiyun  * Returns dword offset of the next capability in port config space
68*4882a593Smuzhiyun  * capability list and returns it. Passing %0 returns the first entry in
69*4882a593Smuzhiyun  * the capability list. If no next capability is found returns %0. In case
70*4882a593Smuzhiyun  * of failure returns negative errno.
71*4882a593Smuzhiyun  */
tb_port_next_cap(struct tb_port * port,unsigned int offset)72*4882a593Smuzhiyun int tb_port_next_cap(struct tb_port *port, unsigned int offset)
73*4882a593Smuzhiyun {
74*4882a593Smuzhiyun 	struct tb_cap_any header;
75*4882a593Smuzhiyun 	int ret;
76*4882a593Smuzhiyun 
77*4882a593Smuzhiyun 	if (!offset)
78*4882a593Smuzhiyun 		return port->config.first_cap_offset;
79*4882a593Smuzhiyun 
80*4882a593Smuzhiyun 	ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
81*4882a593Smuzhiyun 	if (ret)
82*4882a593Smuzhiyun 		return ret;
83*4882a593Smuzhiyun 
84*4882a593Smuzhiyun 	return header.basic.next;
85*4882a593Smuzhiyun }
86*4882a593Smuzhiyun 
__tb_port_find_cap(struct tb_port * port,enum tb_port_cap cap)87*4882a593Smuzhiyun static int __tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
88*4882a593Smuzhiyun {
89*4882a593Smuzhiyun 	int offset = 0;
90*4882a593Smuzhiyun 
91*4882a593Smuzhiyun 	do {
92*4882a593Smuzhiyun 		struct tb_cap_any header;
93*4882a593Smuzhiyun 		int ret;
94*4882a593Smuzhiyun 
95*4882a593Smuzhiyun 		offset = tb_port_next_cap(port, offset);
96*4882a593Smuzhiyun 		if (offset < 0)
97*4882a593Smuzhiyun 			return offset;
98*4882a593Smuzhiyun 
99*4882a593Smuzhiyun 		ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
100*4882a593Smuzhiyun 		if (ret)
101*4882a593Smuzhiyun 			return ret;
102*4882a593Smuzhiyun 
103*4882a593Smuzhiyun 		if (header.basic.cap == cap)
104*4882a593Smuzhiyun 			return offset;
105*4882a593Smuzhiyun 	} while (offset > 0);
106*4882a593Smuzhiyun 
107*4882a593Smuzhiyun 	return -ENOENT;
108*4882a593Smuzhiyun }
109*4882a593Smuzhiyun 
110*4882a593Smuzhiyun /**
111*4882a593Smuzhiyun  * tb_port_find_cap() - Find port capability
112*4882a593Smuzhiyun  * @port: Port to find the capability for
113*4882a593Smuzhiyun  * @cap: Capability to look
114*4882a593Smuzhiyun  *
115*4882a593Smuzhiyun  * Returns offset to start of capability or %-ENOENT if no such
116*4882a593Smuzhiyun  * capability was found. Negative errno is returned if there was an
117*4882a593Smuzhiyun  * error.
118*4882a593Smuzhiyun  */
tb_port_find_cap(struct tb_port * port,enum tb_port_cap cap)119*4882a593Smuzhiyun int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
120*4882a593Smuzhiyun {
121*4882a593Smuzhiyun 	int ret;
122*4882a593Smuzhiyun 
123*4882a593Smuzhiyun 	ret = tb_port_enable_tmu(port, true);
124*4882a593Smuzhiyun 	if (ret)
125*4882a593Smuzhiyun 		return ret;
126*4882a593Smuzhiyun 
127*4882a593Smuzhiyun 	ret = __tb_port_find_cap(port, cap);
128*4882a593Smuzhiyun 
129*4882a593Smuzhiyun 	tb_port_dummy_read(port);
130*4882a593Smuzhiyun 	tb_port_enable_tmu(port, false);
131*4882a593Smuzhiyun 
132*4882a593Smuzhiyun 	return ret;
133*4882a593Smuzhiyun }
134*4882a593Smuzhiyun 
135*4882a593Smuzhiyun /**
136*4882a593Smuzhiyun  * tb_switch_next_cap() - Return next capability in the linked list
137*4882a593Smuzhiyun  * @sw: Switch to find the capability for
138*4882a593Smuzhiyun  * @offset: Previous capability offset (%0 for start)
139*4882a593Smuzhiyun  *
140*4882a593Smuzhiyun  * Finds dword offset of the next capability in router config space
141*4882a593Smuzhiyun  * capability list and returns it. Passing %0 returns the first entry in
142*4882a593Smuzhiyun  * the capability list. If no next capability is found returns %0. In case
143*4882a593Smuzhiyun  * of failure returns negative errno.
144*4882a593Smuzhiyun  */
tb_switch_next_cap(struct tb_switch * sw,unsigned int offset)145*4882a593Smuzhiyun int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset)
146*4882a593Smuzhiyun {
147*4882a593Smuzhiyun 	struct tb_cap_any header;
148*4882a593Smuzhiyun 	int ret;
149*4882a593Smuzhiyun 
150*4882a593Smuzhiyun 	if (!offset)
151*4882a593Smuzhiyun 		return sw->config.first_cap_offset;
152*4882a593Smuzhiyun 
153*4882a593Smuzhiyun 	ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 2);
154*4882a593Smuzhiyun 	if (ret)
155*4882a593Smuzhiyun 		return ret;
156*4882a593Smuzhiyun 
157*4882a593Smuzhiyun 	switch (header.basic.cap) {
158*4882a593Smuzhiyun 	case TB_SWITCH_CAP_TMU:
159*4882a593Smuzhiyun 		ret = header.basic.next;
160*4882a593Smuzhiyun 		break;
161*4882a593Smuzhiyun 
162*4882a593Smuzhiyun 	case TB_SWITCH_CAP_VSE:
163*4882a593Smuzhiyun 		if (!header.extended_short.length)
164*4882a593Smuzhiyun 			ret = header.extended_long.next;
165*4882a593Smuzhiyun 		else
166*4882a593Smuzhiyun 			ret = header.extended_short.next;
167*4882a593Smuzhiyun 		break;
168*4882a593Smuzhiyun 
169*4882a593Smuzhiyun 	default:
170*4882a593Smuzhiyun 		tb_sw_dbg(sw, "unknown capability %#x at %#x\n",
171*4882a593Smuzhiyun 			  header.basic.cap, offset);
172*4882a593Smuzhiyun 		ret = -EINVAL;
173*4882a593Smuzhiyun 		break;
174*4882a593Smuzhiyun 	}
175*4882a593Smuzhiyun 
176*4882a593Smuzhiyun 	return ret >= VSE_CAP_OFFSET_MAX ? 0 : ret;
177*4882a593Smuzhiyun }
178*4882a593Smuzhiyun 
179*4882a593Smuzhiyun /**
180*4882a593Smuzhiyun  * tb_switch_find_cap() - Find switch capability
181*4882a593Smuzhiyun  * @sw Switch to find the capability for
182*4882a593Smuzhiyun  * @cap: Capability to look
183*4882a593Smuzhiyun  *
184*4882a593Smuzhiyun  * Returns offset to start of capability or %-ENOENT if no such
185*4882a593Smuzhiyun  * capability was found. Negative errno is returned if there was an
186*4882a593Smuzhiyun  * error.
187*4882a593Smuzhiyun  */
tb_switch_find_cap(struct tb_switch * sw,enum tb_switch_cap cap)188*4882a593Smuzhiyun int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap)
189*4882a593Smuzhiyun {
190*4882a593Smuzhiyun 	int offset = 0;
191*4882a593Smuzhiyun 
192*4882a593Smuzhiyun 	do {
193*4882a593Smuzhiyun 		struct tb_cap_any header;
194*4882a593Smuzhiyun 		int ret;
195*4882a593Smuzhiyun 
196*4882a593Smuzhiyun 		offset = tb_switch_next_cap(sw, offset);
197*4882a593Smuzhiyun 		if (offset < 0)
198*4882a593Smuzhiyun 			return offset;
199*4882a593Smuzhiyun 
200*4882a593Smuzhiyun 		ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
201*4882a593Smuzhiyun 		if (ret)
202*4882a593Smuzhiyun 			return ret;
203*4882a593Smuzhiyun 
204*4882a593Smuzhiyun 		if (header.basic.cap == cap)
205*4882a593Smuzhiyun 			return offset;
206*4882a593Smuzhiyun 	} while (offset);
207*4882a593Smuzhiyun 
208*4882a593Smuzhiyun 	return -ENOENT;
209*4882a593Smuzhiyun }
210*4882a593Smuzhiyun 
211*4882a593Smuzhiyun /**
212*4882a593Smuzhiyun  * tb_switch_find_vse_cap() - Find switch vendor specific capability
213*4882a593Smuzhiyun  * @sw: Switch to find the capability for
214*4882a593Smuzhiyun  * @vsec: Vendor specific capability to look
215*4882a593Smuzhiyun  *
216*4882a593Smuzhiyun  * Functions enumerates vendor specific capabilities (VSEC) of a switch
217*4882a593Smuzhiyun  * and returns offset when capability matching @vsec is found. If no
218*4882a593Smuzhiyun  * such capability is found returns %-ENOENT. In case of error returns
219*4882a593Smuzhiyun  * negative errno.
220*4882a593Smuzhiyun  */
tb_switch_find_vse_cap(struct tb_switch * sw,enum tb_switch_vse_cap vsec)221*4882a593Smuzhiyun int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec)
222*4882a593Smuzhiyun {
223*4882a593Smuzhiyun 	int offset = 0;
224*4882a593Smuzhiyun 
225*4882a593Smuzhiyun 	do {
226*4882a593Smuzhiyun 		struct tb_cap_any header;
227*4882a593Smuzhiyun 		int ret;
228*4882a593Smuzhiyun 
229*4882a593Smuzhiyun 		offset = tb_switch_next_cap(sw, offset);
230*4882a593Smuzhiyun 		if (offset < 0)
231*4882a593Smuzhiyun 			return offset;
232*4882a593Smuzhiyun 
233*4882a593Smuzhiyun 		ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
234*4882a593Smuzhiyun 		if (ret)
235*4882a593Smuzhiyun 			return ret;
236*4882a593Smuzhiyun 
237*4882a593Smuzhiyun 		if (header.extended_short.cap == TB_SWITCH_CAP_VSE &&
238*4882a593Smuzhiyun 		    header.extended_short.vsec_id == vsec)
239*4882a593Smuzhiyun 			return offset;
240*4882a593Smuzhiyun 	} while (offset);
241*4882a593Smuzhiyun 
242*4882a593Smuzhiyun 	return -ENOENT;
243*4882a593Smuzhiyun }
244