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)
20 struct tb_cap_basic basic
;
21 struct tb_cap_extended_short extended_short
;
22 struct tb_cap_extended_long extended_long
;
26 static int tb_port_enable_tmu(struct tb_port
*port
, bool enable
)
28 struct tb_switch
*sw
= port
->sw
;
33 * Legacy devices need to have TMU access enabled before port
34 * space can be fully accessed.
36 if (tb_switch_is_light_ridge(sw
))
38 else if (tb_switch_is_eagle_ridge(sw
))
43 ret
= tb_sw_read(sw
, &value
, TB_CFG_SWITCH
, offset
, 1);
48 value
|= TMU_ACCESS_EN
;
50 value
&= ~TMU_ACCESS_EN
;
52 return tb_sw_write(sw
, &value
, TB_CFG_SWITCH
, offset
, 1);
55 static void tb_port_dummy_read(struct tb_port
*port
)
58 * When reading from next capability pointer location in port
59 * config space the read data is not cleared on LR. To avoid
60 * reading stale data on next read perform one dummy read after
61 * port capabilities are walked.
63 if (tb_switch_is_light_ridge(port
->sw
)) {
66 tb_port_read(port
, &dummy
, TB_CFG_PORT
, 0, 1);
70 static int __tb_port_find_cap(struct tb_port
*port
, enum tb_port_cap cap
)
75 struct tb_cap_any header
;
78 ret
= tb_port_read(port
, &header
, TB_CFG_PORT
, offset
, 1);
82 if (header
.basic
.cap
== cap
)
85 offset
= header
.basic
.next
;
92 * tb_port_find_cap() - Find port capability
93 * @port: Port to find the capability for
94 * @cap: Capability to look
96 * Returns offset to start of capability or %-ENOENT if no such
97 * capability was found. Negative errno is returned if there was an
100 int tb_port_find_cap(struct tb_port
*port
, enum tb_port_cap cap
)
104 ret
= tb_port_enable_tmu(port
, true);
108 ret
= __tb_port_find_cap(port
, cap
);
110 tb_port_dummy_read(port
);
111 tb_port_enable_tmu(port
, false);
117 * tb_switch_find_cap() - Find switch capability
118 * @sw Switch to find the capability for
119 * @cap: Capability to look
121 * Returns offset to start of capability or %-ENOENT if no such
122 * capability was found. Negative errno is returned if there was an
125 int tb_switch_find_cap(struct tb_switch
*sw
, enum tb_switch_cap cap
)
127 int offset
= sw
->config
.first_cap_offset
;
129 while (offset
> 0 && offset
< CAP_OFFSET_MAX
) {
130 struct tb_cap_any header
;
133 ret
= tb_sw_read(sw
, &header
, TB_CFG_SWITCH
, offset
, 1);
137 if (header
.basic
.cap
== cap
)
140 offset
= header
.basic
.next
;
147 * tb_switch_find_vse_cap() - Find switch vendor specific capability
148 * @sw: Switch to find the capability for
149 * @vsec: Vendor specific capability to look
151 * Functions enumerates vendor specific capabilities (VSEC) of a switch
152 * and returns offset when capability matching @vsec is found. If no
153 * such capability is found returns %-ENOENT. In case of error returns
156 int tb_switch_find_vse_cap(struct tb_switch
*sw
, enum tb_switch_vse_cap vsec
)
158 struct tb_cap_any header
;
161 offset
= tb_switch_find_cap(sw
, TB_SWITCH_CAP_VSE
);
165 while (offset
> 0 && offset
< VSE_CAP_OFFSET_MAX
) {
168 ret
= tb_sw_read(sw
, &header
, TB_CFG_SWITCH
, offset
, 2);
173 * Extended vendor specific capabilities come in two
174 * flavors: short and long. The latter is used when
175 * offset is over 0xff.
177 if (offset
>= CAP_OFFSET_MAX
) {
178 if (header
.extended_long
.vsec_id
== vsec
)
180 offset
= header
.extended_long
.next
;
182 if (header
.extended_short
.vsec_id
== vsec
)
184 if (!header
.extended_short
.length
)
186 offset
= header
.extended_short
.next
;