1 // SPDX-License-Identifier: GPL-2.0
3 * Thunderbolt driver - capabilities lookup
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
6 * Copyright (C) 2018, Intel Corporation
9 #include <linux/slab.h>
10 #include <linux/errno.h>
14 #define CAP_OFFSET_MAX 0xff
15 #define VSE_CAP_OFFSET_MAX 0xffff
16 #define TMU_ACCESS_EN BIT(20)
18 static int tb_port_enable_tmu(struct tb_port
*port
, bool enable
)
20 struct tb_switch
*sw
= port
->sw
;
25 * Legacy devices need to have TMU access enabled before port
26 * space can be fully accessed.
28 if (tb_switch_is_light_ridge(sw
))
30 else if (tb_switch_is_eagle_ridge(sw
))
35 ret
= tb_sw_read(sw
, &value
, TB_CFG_SWITCH
, offset
, 1);
40 value
|= TMU_ACCESS_EN
;
42 value
&= ~TMU_ACCESS_EN
;
44 return tb_sw_write(sw
, &value
, TB_CFG_SWITCH
, offset
, 1);
47 static void tb_port_dummy_read(struct tb_port
*port
)
50 * When reading from next capability pointer location in port
51 * config space the read data is not cleared on LR. To avoid
52 * reading stale data on next read perform one dummy read after
53 * port capabilities are walked.
55 if (tb_switch_is_light_ridge(port
->sw
)) {
58 tb_port_read(port
, &dummy
, TB_CFG_PORT
, 0, 1);
63 * tb_port_next_cap() - Return next capability in the linked list
64 * @port: Port to find the capability for
65 * @offset: Previous capability offset (%0 for start)
67 * Returns dword offset of the next capability in port config space
68 * capability list and returns it. Passing %0 returns the first entry in
69 * the capability list. If no next capability is found returns %0. In case
70 * of failure returns negative errno.
72 int tb_port_next_cap(struct tb_port
*port
, unsigned int offset
)
74 struct tb_cap_any header
;
78 return port
->config
.first_cap_offset
;
80 ret
= tb_port_read(port
, &header
, TB_CFG_PORT
, offset
, 1);
84 return header
.basic
.next
;
87 static int __tb_port_find_cap(struct tb_port
*port
, enum tb_port_cap cap
)
92 struct tb_cap_any header
;
95 offset
= tb_port_next_cap(port
, offset
);
99 ret
= tb_port_read(port
, &header
, TB_CFG_PORT
, offset
, 1);
103 if (header
.basic
.cap
== cap
)
105 } while (offset
> 0);
111 * tb_port_find_cap() - Find port capability
112 * @port: Port to find the capability for
113 * @cap: Capability to look
115 * Returns offset to start of capability or %-ENOENT if no such
116 * capability was found. Negative errno is returned if there was an
119 int tb_port_find_cap(struct tb_port
*port
, enum tb_port_cap cap
)
123 ret
= tb_port_enable_tmu(port
, true);
127 ret
= __tb_port_find_cap(port
, cap
);
129 tb_port_dummy_read(port
);
130 tb_port_enable_tmu(port
, false);
136 * tb_switch_next_cap() - Return next capability in the linked list
137 * @sw: Switch to find the capability for
138 * @offset: Previous capability offset (%0 for start)
140 * Finds dword offset of the next capability in router config space
141 * capability list and returns it. Passing %0 returns the first entry in
142 * the capability list. If no next capability is found returns %0. In case
143 * of failure returns negative errno.
145 int tb_switch_next_cap(struct tb_switch
*sw
, unsigned int offset
)
147 struct tb_cap_any header
;
151 return sw
->config
.first_cap_offset
;
153 ret
= tb_sw_read(sw
, &header
, TB_CFG_SWITCH
, offset
, 2);
157 switch (header
.basic
.cap
) {
158 case TB_SWITCH_CAP_TMU
:
159 ret
= header
.basic
.next
;
162 case TB_SWITCH_CAP_VSE
:
163 if (!header
.extended_short
.length
)
164 ret
= header
.extended_long
.next
;
166 ret
= header
.extended_short
.next
;
170 tb_sw_dbg(sw
, "unknown capability %#x at %#x\n",
171 header
.basic
.cap
, offset
);
176 return ret
>= VSE_CAP_OFFSET_MAX
? 0 : ret
;
180 * tb_switch_find_cap() - Find switch capability
181 * @sw Switch to find the capability for
182 * @cap: Capability to look
184 * Returns offset to start of capability or %-ENOENT if no such
185 * capability was found. Negative errno is returned if there was an
188 int tb_switch_find_cap(struct tb_switch
*sw
, enum tb_switch_cap cap
)
193 struct tb_cap_any header
;
196 offset
= tb_switch_next_cap(sw
, offset
);
200 ret
= tb_sw_read(sw
, &header
, TB_CFG_SWITCH
, offset
, 1);
204 if (header
.basic
.cap
== cap
)
212 * tb_switch_find_vse_cap() - Find switch vendor specific capability
213 * @sw: Switch to find the capability for
214 * @vsec: Vendor specific capability to look
216 * Functions enumerates vendor specific capabilities (VSEC) of a switch
217 * and returns offset when capability matching @vsec is found. If no
218 * such capability is found returns %-ENOENT. In case of error returns
221 int tb_switch_find_vse_cap(struct tb_switch
*sw
, enum tb_switch_vse_cap vsec
)
226 struct tb_cap_any header
;
229 offset
= tb_switch_next_cap(sw
, offset
);
233 ret
= tb_sw_read(sw
, &header
, TB_CFG_SWITCH
, offset
, 1);
237 if (header
.extended_short
.cap
== TB_SWITCH_CAP_VSE
&&
238 header
.extended_short
.vsec_id
== vsec
)