blob: 8ecd610c62d5022c5765a8dbe368c8dfa5844fba [file] [log] [blame]
Greg Kroah-Hartmanb2441312017-11-01 15:07:57 +01001// SPDX-License-Identifier: GPL-2.0
Andreas Noevere2b87852014-06-03 22:04:03 +02002/*
Mika Westerberg15c67842018-10-01 12:31:22 +03003 * Thunderbolt driver - capabilities lookup
Andreas Noevere2b87852014-06-03 22:04:03 +02004 *
5 * Copyright (c) 2014 Andreas Noever <andreas.noever@gmail.com>
Mika Westerberg15c67842018-10-01 12:31:22 +03006 * Copyright (C) 2018, Intel Corporation
Andreas Noevere2b87852014-06-03 22:04:03 +02007 */
8
9#include <linux/slab.h>
10#include <linux/errno.h>
11
12#include "tb.h"
13
Mika Westerbergda2da042017-06-06 15:24:58 +030014#define CAP_OFFSET_MAX 0xff
15#define VSE_CAP_OFFSET_MAX 0xffff
Mika Westerberg8b0110d2019-01-08 18:55:09 +020016#define TMU_ACCESS_EN BIT(20)
Andreas Noevere2b87852014-06-03 22:04:03 +020017
Mika Westerberg8b0110d2019-01-08 18:55:09 +020018static int tb_port_enable_tmu(struct tb_port *port, bool enable)
Andreas Noevere2b87852014-06-03 22:04:03 +020019{
Mika Westerberg8b0110d2019-01-08 18:55:09 +020020 struct tb_switch *sw = port->sw;
21 u32 value, offset;
22 int ret;
Andreas Noevere2b87852014-06-03 22:04:03 +020023
Andreas Noevere2b87852014-06-03 22:04:03 +020024 /*
Mika Westerberg8b0110d2019-01-08 18:55:09 +020025 * Legacy devices need to have TMU access enabled before port
26 * space can be fully accessed.
Andreas Noevere2b87852014-06-03 22:04:03 +020027 */
Mika Westerberg17a8f812019-10-08 16:42:47 +030028 if (tb_switch_is_light_ridge(sw))
Mika Westerberg8b0110d2019-01-08 18:55:09 +020029 offset = 0x26;
Mika Westerberg17a8f812019-10-08 16:42:47 +030030 else if (tb_switch_is_eagle_ridge(sw))
Mika Westerberg8b0110d2019-01-08 18:55:09 +020031 offset = 0x2a;
Mika Westerbergda2da042017-06-06 15:24:58 +030032 else
Mika Westerberg8b0110d2019-01-08 18:55:09 +020033 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
Mika Westerbergffd003b2019-01-08 19:08:06 +020047static void tb_port_dummy_read(struct tb_port *port)
48{
49 /*
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.
54 */
Mika Westerberg17a8f812019-10-08 16:42:47 +030055 if (tb_switch_is_light_ridge(port->sw)) {
Mika Westerbergffd003b2019-01-08 19:08:06 +020056 u32 dummy;
57
58 tb_port_read(port, &dummy, TB_CFG_PORT, 0, 1);
59 }
60}
61
Mika Westerberg3c8b2282020-06-29 20:15:17 +030062/**
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)
66 *
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.
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
Mika Westerberg8b0110d2019-01-08 18:55:09 +020087static int __tb_port_find_cap(struct tb_port *port, enum tb_port_cap cap)
88{
Mika Westerberg3c8b2282020-06-29 20:15:17 +030089 int offset = 0;
Mika Westerbergda2da042017-06-06 15:24:58 +030090
91 do {
92 struct tb_cap_any header;
93 int ret;
94
Mika Westerberg3c8b2282020-06-29 20:15:17 +030095 offset = tb_port_next_cap(port, offset);
96 if (offset < 0)
97 return offset;
98
Mika Westerbergda2da042017-06-06 15:24:58 +030099 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;
Mika Westerberg3c8b2282020-06-29 20:15:17 +0300105 } while (offset > 0);
Mika Westerbergda2da042017-06-06 15:24:58 +0300106
107 return -ENOENT;
108}
109
Mika Westerberg8b0110d2019-01-08 18:55:09 +0200110/**
111 * tb_port_find_cap() - Find port capability
112 * @port: Port to find the capability for
113 * @cap: Capability to look
114 *
115 * Returns offset to start of capability or %-ENOENT if no such
116 * capability was found. Negative errno is returned if there was an
117 * error.
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
Mika Westerbergffd003b2019-01-08 19:08:06 +0200129 tb_port_dummy_read(port);
Mika Westerberg8b0110d2019-01-08 18:55:09 +0200130 tb_port_enable_tmu(port, false);
131
132 return ret;
133}
134
Rajmohan Maniaa43a9d2019-12-17 15:33:42 +0300135/**
Mika Westerberg6de057e2020-06-29 20:21:07 +0300136 * 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)
139 *
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.
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/**
Rajmohan Maniaa43a9d2019-12-17 15:33:42 +0300180 * tb_switch_find_cap() - Find switch capability
Lee Jones43669792021-01-27 11:25:44 +0000181 * @sw: Switch to find the capability for
Rajmohan Maniaa43a9d2019-12-17 15:33:42 +0300182 * @cap: Capability to look
183 *
184 * Returns offset to start of capability or %-ENOENT if no such
185 * capability was found. Negative errno is returned if there was an
186 * error.
187 */
188int tb_switch_find_cap(struct tb_switch *sw, enum tb_switch_cap cap)
Mika Westerbergda2da042017-06-06 15:24:58 +0300189{
Mika Westerberg6de057e2020-06-29 20:21:07 +0300190 int offset = 0;
Mika Westerbergda2da042017-06-06 15:24:58 +0300191
Mika Westerberg6de057e2020-06-29 20:21:07 +0300192 do {
Mika Westerbergda2da042017-06-06 15:24:58 +0300193 struct tb_cap_any header;
194 int ret;
195
Mika Westerberg6de057e2020-06-29 20:21:07 +0300196 offset = tb_switch_next_cap(sw, offset);
197 if (offset < 0)
198 return offset;
199
Mika Westerbergda2da042017-06-06 15:24:58 +0300200 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;
Mika Westerberg6de057e2020-06-29 20:21:07 +0300206 } while (offset);
Mika Westerbergda2da042017-06-06 15:24:58 +0300207
208 return -ENOENT;
Andreas Noevere2b87852014-06-03 22:04:03 +0200209}
210
211/**
Mika Westerbergda2da042017-06-06 15:24:58 +0300212 * 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
Andreas Noevere2b87852014-06-03 22:04:03 +0200215 *
Mika Westerbergda2da042017-06-06 15:24:58 +0300216 * 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
219 * negative errno.
Andreas Noevere2b87852014-06-03 22:04:03 +0200220 */
Mika Westerbergda2da042017-06-06 15:24:58 +0300221int tb_switch_find_vse_cap(struct tb_switch *sw, enum tb_switch_vse_cap vsec)
Andreas Noevere2b87852014-06-03 22:04:03 +0200222{
Mika Westerberg6de057e2020-06-29 20:21:07 +0300223 int offset = 0;
Mika Westerbergda2da042017-06-06 15:24:58 +0300224
Mika Westerberg6de057e2020-06-29 20:21:07 +0300225 do {
226 struct tb_cap_any header;
Mika Westerbergda2da042017-06-06 15:24:58 +0300227 int ret;
228
Mika Westerberg6de057e2020-06-29 20:21:07 +0300229 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);
Mika Westerbergda2da042017-06-06 15:24:58 +0300234 if (ret)
235 return ret;
236
Mika Westerberg6de057e2020-06-29 20:21:07 +0300237 if (header.extended_short.cap == TB_SWITCH_CAP_VSE &&
238 header.extended_short.vsec_id == vsec)
239 return offset;
240 } while (offset);
Mika Westerbergda2da042017-06-06 15:24:58 +0300241
242 return -ENOENT;
Andreas Noevere2b87852014-06-03 22:04:03 +0200243}