1
2
3
4
5
6
7
8
9#include <linux/slab.h>
10#include <linux/errno.h>
11
12#include "tb.h"
13
14#define CAP_OFFSET_MAX 0xff
15#define VSE_CAP_OFFSET_MAX 0xffff
16#define TMU_ACCESS_EN BIT(20)
17
18static int tb_port_enable_tmu(struct tb_port *port, bool enable)
19{
20 struct tb_switch *sw = port->sw;
21 u32 value, offset;
22 int ret;
23
24
25
26
27
28 if (tb_switch_is_light_ridge(sw))
29 offset = 0x26;
30 else if (tb_switch_is_eagle_ridge(sw))
31 offset = 0x2a;
32 else
33 return 0;
34
35 ret = tb_sw_read(sw, &value, TB_CFG_SWITCH, offset, 1);
36 if (ret)
37 return ret;
38
39 if (enable)
40 value |= TMU_ACCESS_EN;
41 else
42 value &= ~TMU_ACCESS_EN;
43
44 return tb_sw_write(sw, &value, TB_CFG_SWITCH, offset, 1);
45}
46
47static void tb_port_dummy_read(struct tb_port *port)
48{
49
50
51
52
53
54
55 if (tb_switch_is_light_ridge(port->sw)) {
56 u32 dummy;
57
58 tb_port_read(port, &dummy, TB_CFG_PORT, 0, 1);
59 }
60}
61
62
63
64
65
66
67
68
69
70
71
72int tb_port_next_cap(struct tb_port *port, unsigned int offset)
73{
74 struct tb_cap_any header;
75 int ret;
76
77 if (!offset)
78 return port->config.first_cap_offset;
79
80 ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
81 if (ret)
82 return ret;
83
84 return header.basic.next;
85}
86
87static int __tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
88{
89 int offset = 0;
90
91 do {
92 struct tb_cap_any header;
93 int ret;
94
95 offset = tb_port_next_cap(port, offset);
96 if (offset < 0)
97 return offset;
98
99 ret = tb_port_read(port, &header, TB_CFG_PORT, offset, 1);
100 if (ret)
101 return ret;
102
103 if (header.basic.cap == cap)
104 return offset;
105 } while (offset > 0);
106
107 return -ENOENT;
108}
109
110
111
112
113
114
115
116
117
118
119int tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
120{
121 int ret;
122
123 ret = tb_port_enable_tmu(port, true);
124 if (ret)
125 return ret;
126
127 ret = __tb_port_find_cap(port, cap);
128
129 tb_port_dummy_read(port);
130 tb_port_enable_tmu(port, false);
131
132 return ret;
133}
134
135
136
137
138
139
140
141
142
143
144
145int tb_switch_next_cap(struct tb_switch *sw, unsigned int offset)
146{
147 struct tb_cap_any header;
148 int ret;
149
150 if (!offset)
151 return sw->config.first_cap_offset;
152
153 ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 2);
154 if (ret)
155 return ret;
156
157 switch (header.basic.cap) {
158 case TB_SWITCH_CAP_TMU:
159 ret = header.basic.next;
160 break;
161
162 case TB_SWITCH_CAP_VSE:
163 if (!header.extended_short.length)
164 ret = header.extended_long.next;
165 else
166 ret = header.extended_short.next;
167 break;
168
169 default:
170 tb_sw_dbg(sw, "unknown capability %#x at %#x\n",
171 header.basic.cap, offset);
172 ret = -EINVAL;
173 break;
174 }
175
176 return ret >= VSE_CAP_OFFSET_MAX ? 0 : ret;
177}
178
179
180
181
182
183
184
185
186
187
188int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap)
189{
190 int offset = 0;
191
192 do {
193 struct tb_cap_any header;
194 int ret;
195
196 offset = tb_switch_next_cap(sw, offset);
197 if (offset < 0)
198 return offset;
199
200 ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
201 if (ret)
202 return ret;
203
204 if (header.basic.cap == cap)
205 return offset;
206 } while (offset);
207
208 return -ENOENT;
209}
210
211
212
213
214
215
216
217
218
219
220
221int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec)
222{
223 int offset = 0;
224
225 do {
226 struct tb_cap_any header;
227 int ret;
228
229 offset = tb_switch_next_cap(sw, offset);
230 if (offset < 0)
231 return offset;
232
233 ret = tb_sw_read(sw, &header, TB_CFG_SWITCH, offset, 1);
234 if (ret)
235 return ret;
236
237 if (header.extended_short.cap == TB_SWITCH_CAP_VSE &&
238 header.extended_short.vsec_id == vsec)
239 return offset;
240 } while (offset);
241
242 return -ENOENT;
243}
244