xref: /linux/net/ethtool/common.c (revision 0e59523b440c90ed1899476f32ad4e949282f43e)
1 // SPDX-License-Identifier: GPL-2.0-only
2 
3 #include <linux/ethtool_netlink.h>
4 #include <linux/net_tstamp.h>
5 #include <linux/once.h>
6 #include <linux/random.h>
7 #include <linux/unaligned.h>
8 #include <linux/phy.h>
9 #include <linux/rtnetlink.h>
10 #include <linux/ptp_clock_kernel.h>
11 #include <linux/phy_link_topology.h>
12 #include <net/netdev_queues.h>
13 
14 #include "common.h"
15 #include "netlink.h"
16 #include "../core/dev.h"
17 
18 
19 const char netdev_features_strings[NETDEV_FEATURE_COUNT][ETH_GSTRING_LEN] = {
20 	[NETIF_F_SG_BIT] =               "tx-scatter-gather",
21 	[NETIF_F_IP_CSUM_BIT] =          "tx-checksum-ipv4",
22 	[NETIF_F_HW_CSUM_BIT] =          "tx-checksum-ip-generic",
23 	[NETIF_F_IPV6_CSUM_BIT] =        "tx-checksum-ipv6",
24 	[NETIF_F_HIGHDMA_BIT] =          "highdma",
25 	[NETIF_F_FRAGLIST_BIT] =         "tx-scatter-gather-fraglist",
26 	[NETIF_F_HW_VLAN_CTAG_TX_BIT] =  "tx-vlan-hw-insert",
27 
28 	[NETIF_F_HW_VLAN_CTAG_RX_BIT] =  "rx-vlan-hw-parse",
29 	[NETIF_F_HW_VLAN_CTAG_FILTER_BIT] = "rx-vlan-filter",
30 	[NETIF_F_HW_VLAN_STAG_TX_BIT] =  "tx-vlan-stag-hw-insert",
31 	[NETIF_F_HW_VLAN_STAG_RX_BIT] =  "rx-vlan-stag-hw-parse",
32 	[NETIF_F_HW_VLAN_STAG_FILTER_BIT] = "rx-vlan-stag-filter",
33 	[NETIF_F_VLAN_CHALLENGED_BIT] =  "vlan-challenged",
34 	[NETIF_F_GSO_BIT] =              "tx-generic-segmentation",
35 	[NETIF_F_GRO_BIT] =              "rx-gro",
36 	[NETIF_F_GRO_HW_BIT] =           "rx-gro-hw",
37 	[NETIF_F_LRO_BIT] =              "rx-lro",
38 
39 	[NETIF_F_TSO_BIT] =              "tx-tcp-segmentation",
40 	[NETIF_F_GSO_ROBUST_BIT] =       "tx-gso-robust",
41 	[NETIF_F_TSO_ECN_BIT] =          "tx-tcp-ecn-segmentation",
42 	[NETIF_F_GSO_ACCECN_BIT] =	 "tx-tcp-accecn-segmentation",
43 	[NETIF_F_TSO_MANGLEID_BIT] =	 "tx-tcp-mangleid-segmentation",
44 	[NETIF_F_TSO6_BIT] =             "tx-tcp6-segmentation",
45 	[NETIF_F_FSO_BIT] =              "tx-fcoe-segmentation",
46 	[NETIF_F_GSO_GRE_BIT] =		 "tx-gre-segmentation",
47 	[NETIF_F_GSO_GRE_CSUM_BIT] =	 "tx-gre-csum-segmentation",
48 	[NETIF_F_GSO_IPXIP4_BIT] =	 "tx-ipxip4-segmentation",
49 	[NETIF_F_GSO_IPXIP6_BIT] =	 "tx-ipxip6-segmentation",
50 	[NETIF_F_GSO_UDP_TUNNEL_BIT] =	 "tx-udp_tnl-segmentation",
51 	[NETIF_F_GSO_UDP_TUNNEL_CSUM_BIT] = "tx-udp_tnl-csum-segmentation",
52 	[NETIF_F_GSO_PARTIAL_BIT] =	 "tx-gso-partial",
53 	[NETIF_F_GSO_TUNNEL_REMCSUM_BIT] = "tx-tunnel-remcsum-segmentation",
54 	[NETIF_F_GSO_SCTP_BIT] =	 "tx-sctp-segmentation",
55 	[NETIF_F_GSO_ESP_BIT] =		 "tx-esp-segmentation",
56 	[NETIF_F_GSO_UDP_L4_BIT] =	 "tx-udp-segmentation",
57 	[NETIF_F_GSO_FRAGLIST_BIT] =	 "tx-gso-list",
58 
59 	[NETIF_F_FCOE_CRC_BIT] =         "tx-checksum-fcoe-crc",
60 	[NETIF_F_SCTP_CRC_BIT] =        "tx-checksum-sctp",
61 	[NETIF_F_NTUPLE_BIT] =           "rx-ntuple-filter",
62 	[NETIF_F_RXHASH_BIT] =           "rx-hashing",
63 	[NETIF_F_RXCSUM_BIT] =           "rx-checksum",
64 	[NETIF_F_NOCACHE_COPY_BIT] =     "tx-nocache-copy",
65 	[NETIF_F_LOOPBACK_BIT] =         "loopback",
66 	[NETIF_F_RXFCS_BIT] =            "rx-fcs",
67 	[NETIF_F_RXALL_BIT] =            "rx-all",
68 	[NETIF_F_HW_L2FW_DOFFLOAD_BIT] = "l2-fwd-offload",
69 	[NETIF_F_HW_TC_BIT] =		 "hw-tc-offload",
70 	[NETIF_F_HW_ESP_BIT] =		 "esp-hw-offload",
71 	[NETIF_F_HW_ESP_TX_CSUM_BIT] =	 "esp-tx-csum-hw-offload",
72 	[NETIF_F_RX_UDP_TUNNEL_PORT_BIT] =	 "rx-udp_tunnel-port-offload",
73 	[NETIF_F_HW_TLS_TX_BIT] =	 "tls-hw-tx-offload",
74 	[NETIF_F_HW_TLS_RX_BIT] =	 "tls-hw-rx-offload",
75 	[NETIF_F_GRO_FRAGLIST_BIT] =	 "rx-gro-list",
76 	[NETIF_F_HW_MACSEC_BIT] =	 "macsec-hw-offload",
77 	[NETIF_F_GRO_UDP_FWD_BIT] =	 "rx-udp-gro-forwarding",
78 	[NETIF_F_HW_HSR_TAG_INS_BIT] =	 "hsr-tag-ins-offload",
79 	[NETIF_F_HW_HSR_TAG_RM_BIT] =	 "hsr-tag-rm-offload",
80 	[NETIF_F_HW_HSR_FWD_BIT] =	 "hsr-fwd-offload",
81 	[NETIF_F_HW_HSR_DUP_BIT] =	 "hsr-dup-offload",
82 };
83 
84 const char
85 rss_hash_func_strings[ETH_RSS_HASH_FUNCS_COUNT][ETH_GSTRING_LEN] = {
86 	[ETH_RSS_HASH_TOP_BIT] =	"toeplitz",
87 	[ETH_RSS_HASH_XOR_BIT] =	"xor",
88 	[ETH_RSS_HASH_CRC32_BIT] =	"crc32",
89 };
90 
91 const char
92 tunable_strings[__ETHTOOL_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
93 	[ETHTOOL_ID_UNSPEC]     = "Unspec",
94 	[ETHTOOL_RX_COPYBREAK]	= "rx-copybreak",
95 	[ETHTOOL_TX_COPYBREAK]	= "tx-copybreak",
96 	[ETHTOOL_PFC_PREVENTION_TOUT] = "pfc-prevention-tout",
97 	[ETHTOOL_TX_COPYBREAK_BUF_SIZE] = "tx-copybreak-buf-size",
98 };
99 
100 const char
101 phy_tunable_strings[__ETHTOOL_PHY_TUNABLE_COUNT][ETH_GSTRING_LEN] = {
102 	[ETHTOOL_ID_UNSPEC]     = "Unspec",
103 	[ETHTOOL_PHY_DOWNSHIFT]	= "phy-downshift",
104 	[ETHTOOL_PHY_FAST_LINK_DOWN] = "phy-fast-link-down",
105 	[ETHTOOL_PHY_EDPD]	= "phy-energy-detect-power-down",
106 	[ETHTOOL_PHY_SHORT_CABLE_PRESET] = "phy-short-cable-preset",
107 	[ETHTOOL_PHY_LPF_BW]	= "phy-lpf-bandwidth",
108 	[ETHTOOL_PHY_DSP_EQ_INIT_VALUE] = "phy-dsp-eq-init-value",
109 };
110 
111 #define __LINK_MODE_NAME(speed, type, duplex) \
112 	#speed "base" #type "/" #duplex
113 #define __DEFINE_LINK_MODE_NAME(speed, type, duplex) \
114 	[ETHTOOL_LINK_MODE(speed, type, duplex)] = \
115 	__LINK_MODE_NAME(speed, type, duplex)
116 #define __DEFINE_SPECIAL_MODE_NAME(_mode, _name) \
117 	[ETHTOOL_LINK_MODE_ ## _mode ## _BIT] = _name
118 
119 const char link_mode_names[][ETH_GSTRING_LEN] = {
120 	__DEFINE_LINK_MODE_NAME(10, T, Half),
121 	__DEFINE_LINK_MODE_NAME(10, T, Full),
122 	__DEFINE_LINK_MODE_NAME(100, T, Half),
123 	__DEFINE_LINK_MODE_NAME(100, T, Full),
124 	__DEFINE_LINK_MODE_NAME(1000, T, Half),
125 	__DEFINE_LINK_MODE_NAME(1000, T, Full),
126 	__DEFINE_SPECIAL_MODE_NAME(Autoneg, "Autoneg"),
127 	__DEFINE_SPECIAL_MODE_NAME(TP, "TP"),
128 	__DEFINE_SPECIAL_MODE_NAME(AUI, "AUI"),
129 	__DEFINE_SPECIAL_MODE_NAME(MII, "MII"),
130 	__DEFINE_SPECIAL_MODE_NAME(FIBRE, "FIBRE"),
131 	__DEFINE_SPECIAL_MODE_NAME(BNC, "BNC"),
132 	__DEFINE_LINK_MODE_NAME(10000, T, Full),
133 	__DEFINE_SPECIAL_MODE_NAME(Pause, "Pause"),
134 	__DEFINE_SPECIAL_MODE_NAME(Asym_Pause, "Asym_Pause"),
135 	__DEFINE_LINK_MODE_NAME(2500, X, Full),
136 	__DEFINE_SPECIAL_MODE_NAME(Backplane, "Backplane"),
137 	__DEFINE_LINK_MODE_NAME(1000, KX, Full),
138 	__DEFINE_LINK_MODE_NAME(10000, KX4, Full),
139 	__DEFINE_LINK_MODE_NAME(10000, KR, Full),
140 	__DEFINE_SPECIAL_MODE_NAME(10000baseR_FEC, "10000baseR_FEC"),
141 	__DEFINE_LINK_MODE_NAME(20000, MLD2, Full),
142 	__DEFINE_LINK_MODE_NAME(20000, KR2, Full),
143 	__DEFINE_LINK_MODE_NAME(40000, KR4, Full),
144 	__DEFINE_LINK_MODE_NAME(40000, CR4, Full),
145 	__DEFINE_LINK_MODE_NAME(40000, SR4, Full),
146 	__DEFINE_LINK_MODE_NAME(40000, LR4, Full),
147 	__DEFINE_LINK_MODE_NAME(56000, KR4, Full),
148 	__DEFINE_LINK_MODE_NAME(56000, CR4, Full),
149 	__DEFINE_LINK_MODE_NAME(56000, SR4, Full),
150 	__DEFINE_LINK_MODE_NAME(56000, LR4, Full),
151 	__DEFINE_LINK_MODE_NAME(25000, CR, Full),
152 	__DEFINE_LINK_MODE_NAME(25000, KR, Full),
153 	__DEFINE_LINK_MODE_NAME(25000, SR, Full),
154 	__DEFINE_LINK_MODE_NAME(50000, CR2, Full),
155 	__DEFINE_LINK_MODE_NAME(50000, KR2, Full),
156 	__DEFINE_LINK_MODE_NAME(100000, KR4, Full),
157 	__DEFINE_LINK_MODE_NAME(100000, SR4, Full),
158 	__DEFINE_LINK_MODE_NAME(100000, CR4, Full),
159 	__DEFINE_LINK_MODE_NAME(100000, LR4_ER4, Full),
160 	__DEFINE_LINK_MODE_NAME(50000, SR2, Full),
161 	__DEFINE_LINK_MODE_NAME(1000, X, Full),
162 	__DEFINE_LINK_MODE_NAME(10000, CR, Full),
163 	__DEFINE_LINK_MODE_NAME(10000, SR, Full),
164 	__DEFINE_LINK_MODE_NAME(10000, LR, Full),
165 	__DEFINE_LINK_MODE_NAME(10000, LRM, Full),
166 	__DEFINE_LINK_MODE_NAME(10000, ER, Full),
167 	__DEFINE_LINK_MODE_NAME(2500, T, Full),
168 	__DEFINE_LINK_MODE_NAME(5000, T, Full),
169 	__DEFINE_SPECIAL_MODE_NAME(FEC_NONE, "None"),
170 	__DEFINE_SPECIAL_MODE_NAME(FEC_RS, "RS"),
171 	__DEFINE_SPECIAL_MODE_NAME(FEC_BASER, "BASER"),
172 	__DEFINE_LINK_MODE_NAME(50000, KR, Full),
173 	__DEFINE_LINK_MODE_NAME(50000, SR, Full),
174 	__DEFINE_LINK_MODE_NAME(50000, CR, Full),
175 	__DEFINE_LINK_MODE_NAME(50000, LR_ER_FR, Full),
176 	__DEFINE_LINK_MODE_NAME(50000, DR, Full),
177 	__DEFINE_LINK_MODE_NAME(100000, KR2, Full),
178 	__DEFINE_LINK_MODE_NAME(100000, SR2, Full),
179 	__DEFINE_LINK_MODE_NAME(100000, CR2, Full),
180 	__DEFINE_LINK_MODE_NAME(100000, LR2_ER2_FR2, Full),
181 	__DEFINE_LINK_MODE_NAME(100000, DR2, Full),
182 	__DEFINE_LINK_MODE_NAME(200000, KR4, Full),
183 	__DEFINE_LINK_MODE_NAME(200000, SR4, Full),
184 	__DEFINE_LINK_MODE_NAME(200000, LR4_ER4_FR4, Full),
185 	__DEFINE_LINK_MODE_NAME(200000, DR4, Full),
186 	__DEFINE_LINK_MODE_NAME(200000, CR4, Full),
187 	__DEFINE_LINK_MODE_NAME(100, T1, Full),
188 	__DEFINE_LINK_MODE_NAME(1000, T1, Full),
189 	__DEFINE_LINK_MODE_NAME(400000, KR8, Full),
190 	__DEFINE_LINK_MODE_NAME(400000, SR8, Full),
191 	__DEFINE_LINK_MODE_NAME(400000, LR8_ER8_FR8, Full),
192 	__DEFINE_LINK_MODE_NAME(400000, DR8, Full),
193 	__DEFINE_LINK_MODE_NAME(400000, CR8, Full),
194 	__DEFINE_SPECIAL_MODE_NAME(FEC_LLRS, "LLRS"),
195 	__DEFINE_LINK_MODE_NAME(100000, KR, Full),
196 	__DEFINE_LINK_MODE_NAME(100000, SR, Full),
197 	__DEFINE_LINK_MODE_NAME(100000, LR_ER_FR, Full),
198 	__DEFINE_LINK_MODE_NAME(100000, DR, Full),
199 	__DEFINE_LINK_MODE_NAME(100000, CR, Full),
200 	__DEFINE_LINK_MODE_NAME(200000, KR2, Full),
201 	__DEFINE_LINK_MODE_NAME(200000, SR2, Full),
202 	__DEFINE_LINK_MODE_NAME(200000, LR2_ER2_FR2, Full),
203 	__DEFINE_LINK_MODE_NAME(200000, DR2, Full),
204 	__DEFINE_LINK_MODE_NAME(200000, CR2, Full),
205 	__DEFINE_LINK_MODE_NAME(400000, KR4, Full),
206 	__DEFINE_LINK_MODE_NAME(400000, SR4, Full),
207 	__DEFINE_LINK_MODE_NAME(400000, LR4_ER4_FR4, Full),
208 	__DEFINE_LINK_MODE_NAME(400000, DR4, Full),
209 	__DEFINE_LINK_MODE_NAME(400000, CR4, Full),
210 	__DEFINE_LINK_MODE_NAME(100, FX, Half),
211 	__DEFINE_LINK_MODE_NAME(100, FX, Full),
212 	__DEFINE_LINK_MODE_NAME(10, T1L, Full),
213 	__DEFINE_LINK_MODE_NAME(800000, CR8, Full),
214 	__DEFINE_LINK_MODE_NAME(800000, KR8, Full),
215 	__DEFINE_LINK_MODE_NAME(800000, DR8, Full),
216 	__DEFINE_LINK_MODE_NAME(800000, DR8_2, Full),
217 	__DEFINE_LINK_MODE_NAME(800000, SR8, Full),
218 	__DEFINE_LINK_MODE_NAME(800000, VR8, Full),
219 	__DEFINE_LINK_MODE_NAME(10, T1S, Full),
220 	__DEFINE_LINK_MODE_NAME(10, T1S, Half),
221 	__DEFINE_LINK_MODE_NAME(10, T1S_P2MP, Half),
222 	__DEFINE_LINK_MODE_NAME(10, T1BRR, Full),
223 	__DEFINE_LINK_MODE_NAME(200000, CR, Full),
224 	__DEFINE_LINK_MODE_NAME(200000, KR, Full),
225 	__DEFINE_LINK_MODE_NAME(200000, DR, Full),
226 	__DEFINE_LINK_MODE_NAME(200000, DR_2, Full),
227 	__DEFINE_LINK_MODE_NAME(200000, SR, Full),
228 	__DEFINE_LINK_MODE_NAME(200000, VR, Full),
229 	__DEFINE_LINK_MODE_NAME(400000, CR2, Full),
230 	__DEFINE_LINK_MODE_NAME(400000, KR2, Full),
231 	__DEFINE_LINK_MODE_NAME(400000, DR2, Full),
232 	__DEFINE_LINK_MODE_NAME(400000, DR2_2, Full),
233 	__DEFINE_LINK_MODE_NAME(400000, SR2, Full),
234 	__DEFINE_LINK_MODE_NAME(400000, VR2, Full),
235 	__DEFINE_LINK_MODE_NAME(800000, CR4, Full),
236 	__DEFINE_LINK_MODE_NAME(800000, KR4, Full),
237 	__DEFINE_LINK_MODE_NAME(800000, DR4, Full),
238 	__DEFINE_LINK_MODE_NAME(800000, DR4_2, Full),
239 	__DEFINE_LINK_MODE_NAME(800000, SR4, Full),
240 	__DEFINE_LINK_MODE_NAME(800000, VR4, Full),
241 	__DEFINE_LINK_MODE_NAME(1600000, CR8, Full),
242 	__DEFINE_LINK_MODE_NAME(1600000, KR8, Full),
243 	__DEFINE_LINK_MODE_NAME(1600000, DR8, Full),
244 	__DEFINE_LINK_MODE_NAME(1600000, DR8_2, Full),
245 };
246 static_assert(ARRAY_SIZE(link_mode_names) == __ETHTOOL_LINK_MODE_MASK_NBITS);
247 
248 #define __LINK_MODE_LANES_CR		1
249 #define __LINK_MODE_LANES_CR2		2
250 #define __LINK_MODE_LANES_CR4		4
251 #define __LINK_MODE_LANES_CR8		8
252 #define __LINK_MODE_LANES_DR		1
253 #define __LINK_MODE_LANES_DR_2		1
254 #define __LINK_MODE_LANES_DR2		2
255 #define __LINK_MODE_LANES_DR2_2		2
256 #define __LINK_MODE_LANES_DR4		4
257 #define __LINK_MODE_LANES_DR4_2		4
258 #define __LINK_MODE_LANES_DR8		8
259 #define __LINK_MODE_LANES_KR		1
260 #define __LINK_MODE_LANES_KR2		2
261 #define __LINK_MODE_LANES_KR4		4
262 #define __LINK_MODE_LANES_KR8		8
263 #define __LINK_MODE_LANES_SR		1
264 #define __LINK_MODE_LANES_SR2		2
265 #define __LINK_MODE_LANES_SR4		4
266 #define __LINK_MODE_LANES_SR8		8
267 #define __LINK_MODE_LANES_ER		1
268 #define __LINK_MODE_LANES_KX		1
269 #define __LINK_MODE_LANES_KX4		4
270 #define __LINK_MODE_LANES_LR		1
271 #define __LINK_MODE_LANES_LR4		4
272 #define __LINK_MODE_LANES_LR4_ER4	4
273 #define __LINK_MODE_LANES_LR_ER_FR	1
274 #define __LINK_MODE_LANES_LR2_ER2_FR2	2
275 #define __LINK_MODE_LANES_LR4_ER4_FR4	4
276 #define __LINK_MODE_LANES_LR8_ER8_FR8	8
277 #define __LINK_MODE_LANES_LRM		1
278 #define __LINK_MODE_LANES_MLD2		2
279 #define __LINK_MODE_LANES_T		1
280 #define __LINK_MODE_LANES_T1		1
281 #define __LINK_MODE_LANES_X		1
282 #define __LINK_MODE_LANES_FX		1
283 #define __LINK_MODE_LANES_T1L		1
284 #define __LINK_MODE_LANES_T1S		1
285 #define __LINK_MODE_LANES_T1S_P2MP	1
286 #define __LINK_MODE_LANES_VR		1
287 #define __LINK_MODE_LANES_VR2		2
288 #define __LINK_MODE_LANES_VR4		4
289 #define __LINK_MODE_LANES_VR8		8
290 #define __LINK_MODE_LANES_DR8_2		8
291 #define __LINK_MODE_LANES_T1BRR		1
292 
293 #define __DEFINE_LINK_MODE_PARAMS_PAIRS(_speed, _type, _min_pairs, _pairs, _duplex, _medium) \
294 	[ETHTOOL_LINK_MODE(_speed, _type, _duplex)] = {		\
295 		.speed  = SPEED_ ## _speed, \
296 		.lanes  = __LINK_MODE_LANES_ ## _type, \
297 		.min_pairs = _min_pairs, \
298 		.pairs = _pairs, \
299 		.duplex	= __DUPLEX_ ## _duplex, \
300 		.mediums = BIT(ETHTOOL_LINK_MEDIUM_BASE ## _medium) \
301 	}
302 
303 #define __DEFINE_LINK_MODE_PARAMS(_speed, _type, _duplex, _medium)	\
304 	[ETHTOOL_LINK_MODE(_speed, _type, _duplex)] = {		\
305 		.speed  = SPEED_ ## _speed, \
306 		.lanes  = __LINK_MODE_LANES_ ## _type, \
307 		.min_pairs = 0, \
308 		.pairs = 0, \
309 		.duplex	= __DUPLEX_ ## _duplex, \
310 		.mediums = BIT(ETHTOOL_LINK_MEDIUM_BASE ## _medium) \
311 	}
312 #define __DEFINE_LINK_MODE_PARAMS_MEDIUMS(_speed, _type, _duplex, _mediums)	\
313 	[ETHTOOL_LINK_MODE(_speed, _type, _duplex)] = {		\
314 		.speed  = SPEED_ ## _speed, \
315 		.lanes  = __LINK_MODE_LANES_ ## _type, \
316 		.min_pairs = 0, \
317 		.pairs = 0, \
318 		.duplex	= __DUPLEX_ ## _duplex, \
319 		.mediums = (_mediums) \
320 	}
321 #define __MED(_medium)	(BIT(ETHTOOL_LINK_MEDIUM_BASE ## _medium))
322 #define __DUPLEX_Half DUPLEX_HALF
323 #define __DUPLEX_Full DUPLEX_FULL
324 #define __DEFINE_SPECIAL_MODE_PARAMS(_mode) \
325 	[ETHTOOL_LINK_MODE_ ## _mode ## _BIT] = { \
326 		.speed	= SPEED_UNKNOWN, \
327 		.lanes	= 0, \
328 		.duplex	= DUPLEX_UNKNOWN, \
329 		.mediums = BIT(ETHTOOL_LINK_MEDIUM_NONE), \
330 	}
331 
332 const struct link_mode_info link_mode_params[] = {
333 	__DEFINE_LINK_MODE_PARAMS_PAIRS(10, T, 2, 4, Half, T),
334 	__DEFINE_LINK_MODE_PARAMS_PAIRS(10, T, 2, 4, Full, T),
335 	__DEFINE_LINK_MODE_PARAMS_PAIRS(100, T, 2, 4, Half, T),
336 	__DEFINE_LINK_MODE_PARAMS_PAIRS(100, T, 2, 4, Full, T),
337 	__DEFINE_LINK_MODE_PARAMS_PAIRS(1000, T, 4, 4, Half, T),
338 	__DEFINE_LINK_MODE_PARAMS_PAIRS(1000, T, 4, 4, Full, T),
339 	__DEFINE_SPECIAL_MODE_PARAMS(Autoneg),
340 	__DEFINE_SPECIAL_MODE_PARAMS(TP),
341 	__DEFINE_SPECIAL_MODE_PARAMS(AUI),
342 	__DEFINE_SPECIAL_MODE_PARAMS(MII),
343 	__DEFINE_SPECIAL_MODE_PARAMS(FIBRE),
344 	__DEFINE_SPECIAL_MODE_PARAMS(BNC),
345 	__DEFINE_LINK_MODE_PARAMS_PAIRS(10000, T, 4, 4, Full, T),
346 	__DEFINE_SPECIAL_MODE_PARAMS(Pause),
347 	__DEFINE_SPECIAL_MODE_PARAMS(Asym_Pause),
348 	__DEFINE_LINK_MODE_PARAMS_MEDIUMS(2500, X, Full,
349 					  __MED(C) | __MED(S) | __MED(L)),
350 	__DEFINE_SPECIAL_MODE_PARAMS(Backplane),
351 	__DEFINE_LINK_MODE_PARAMS(1000, KX, Full, K),
352 	__DEFINE_LINK_MODE_PARAMS(10000, KX4, Full, K),
353 	__DEFINE_LINK_MODE_PARAMS(10000, KR, Full, K),
354 	[ETHTOOL_LINK_MODE_10000baseR_FEC_BIT] = {
355 		.speed	= SPEED_10000,
356 		.lanes	= 1,
357 		.duplex = DUPLEX_FULL,
358 	},
359 	__DEFINE_LINK_MODE_PARAMS(20000, MLD2, Full, MLD),
360 	__DEFINE_LINK_MODE_PARAMS(20000, KR2, Full, K),
361 	__DEFINE_LINK_MODE_PARAMS(40000, KR4, Full, K),
362 	__DEFINE_LINK_MODE_PARAMS(40000, CR4, Full, C),
363 	__DEFINE_LINK_MODE_PARAMS(40000, SR4, Full, S),
364 	__DEFINE_LINK_MODE_PARAMS(40000, LR4, Full, L),
365 	__DEFINE_LINK_MODE_PARAMS(56000, KR4, Full, K),
366 	__DEFINE_LINK_MODE_PARAMS(56000, CR4, Full, C),
367 	__DEFINE_LINK_MODE_PARAMS(56000, SR4, Full, S),
368 	__DEFINE_LINK_MODE_PARAMS(56000, LR4, Full, L),
369 	__DEFINE_LINK_MODE_PARAMS(25000, CR, Full, C),
370 	__DEFINE_LINK_MODE_PARAMS(25000, KR, Full, K),
371 	__DEFINE_LINK_MODE_PARAMS(25000, SR, Full, S),
372 	__DEFINE_LINK_MODE_PARAMS(50000, CR2, Full, C),
373 	__DEFINE_LINK_MODE_PARAMS(50000, KR2, Full, K),
374 	__DEFINE_LINK_MODE_PARAMS(100000, KR4, Full, K),
375 	__DEFINE_LINK_MODE_PARAMS(100000, SR4, Full, S),
376 	__DEFINE_LINK_MODE_PARAMS(100000, CR4, Full, C),
377 	__DEFINE_LINK_MODE_PARAMS_MEDIUMS(100000, LR4_ER4, Full,
378 					  __MED(L) | __MED(E)),
379 	__DEFINE_LINK_MODE_PARAMS(50000, SR2, Full, S),
380 	__DEFINE_LINK_MODE_PARAMS_MEDIUMS(1000, X, Full,
381 					  __MED(C) | __MED(S) | __MED(L)),
382 	__DEFINE_LINK_MODE_PARAMS(10000, CR, Full, C),
383 	__DEFINE_LINK_MODE_PARAMS(10000, SR, Full, S),
384 	__DEFINE_LINK_MODE_PARAMS(10000, LR, Full, L),
385 	__DEFINE_LINK_MODE_PARAMS(10000, LRM, Full, L),
386 	__DEFINE_LINK_MODE_PARAMS(10000, ER, Full, E),
387 	__DEFINE_LINK_MODE_PARAMS_PAIRS(2500, T, 4, 4, Full, T),
388 	__DEFINE_LINK_MODE_PARAMS_PAIRS(5000, T, 4, 4, Full, T),
389 	__DEFINE_SPECIAL_MODE_PARAMS(FEC_NONE),
390 	__DEFINE_SPECIAL_MODE_PARAMS(FEC_RS),
391 	__DEFINE_SPECIAL_MODE_PARAMS(FEC_BASER),
392 	__DEFINE_LINK_MODE_PARAMS(50000, KR, Full, K),
393 	__DEFINE_LINK_MODE_PARAMS(50000, SR, Full, S),
394 	__DEFINE_LINK_MODE_PARAMS(50000, CR, Full, C),
395 	__DEFINE_LINK_MODE_PARAMS_MEDIUMS(50000, LR_ER_FR, Full,
396 					  __MED(L) | __MED(E) | __MED(F)),
397 	__DEFINE_LINK_MODE_PARAMS(50000, DR, Full, D),
398 	__DEFINE_LINK_MODE_PARAMS(100000, KR2, Full, K),
399 	__DEFINE_LINK_MODE_PARAMS(100000, SR2, Full, S),
400 	__DEFINE_LINK_MODE_PARAMS(100000, CR2, Full, C),
401 	__DEFINE_LINK_MODE_PARAMS_MEDIUMS(100000, LR2_ER2_FR2, Full,
402 					  __MED(L) | __MED(E) | __MED(F)),
403 	__DEFINE_LINK_MODE_PARAMS(100000, DR2, Full, D),
404 	__DEFINE_LINK_MODE_PARAMS(200000, KR4, Full, K),
405 	__DEFINE_LINK_MODE_PARAMS(200000, SR4, Full, S),
406 	__DEFINE_LINK_MODE_PARAMS_MEDIUMS(200000, LR4_ER4_FR4, Full,
407 					  __MED(L) | __MED(E) | __MED(F)),
408 	__DEFINE_LINK_MODE_PARAMS(200000, DR4, Full, D),
409 	__DEFINE_LINK_MODE_PARAMS(200000, CR4, Full, C),
410 	__DEFINE_LINK_MODE_PARAMS_PAIRS(100, T1, 1, 1, Full, T),
411 	__DEFINE_LINK_MODE_PARAMS_PAIRS(1000, T1, 1, 1, Full, T),
412 	__DEFINE_LINK_MODE_PARAMS(400000, KR8, Full, K),
413 	__DEFINE_LINK_MODE_PARAMS(400000, SR8, Full, S),
414 	__DEFINE_LINK_MODE_PARAMS_MEDIUMS(400000, LR8_ER8_FR8, Full,
415 					  __MED(L) | __MED(E) | __MED(F)),
416 	__DEFINE_LINK_MODE_PARAMS(400000, DR8, Full, D),
417 	__DEFINE_LINK_MODE_PARAMS(400000, CR8, Full, C),
418 	__DEFINE_SPECIAL_MODE_PARAMS(FEC_LLRS),
419 	__DEFINE_LINK_MODE_PARAMS(100000, KR, Full, K),
420 	__DEFINE_LINK_MODE_PARAMS(100000, SR, Full, S),
421 	__DEFINE_LINK_MODE_PARAMS_MEDIUMS(100000, LR_ER_FR, Full,
422 					  __MED(L) | __MED(E) | __MED(F)),
423 	__DEFINE_LINK_MODE_PARAMS(100000, DR, Full, D),
424 	__DEFINE_LINK_MODE_PARAMS(100000, CR, Full, C),
425 	__DEFINE_LINK_MODE_PARAMS(200000, KR2, Full, K),
426 	__DEFINE_LINK_MODE_PARAMS(200000, SR2, Full, S),
427 	__DEFINE_LINK_MODE_PARAMS_MEDIUMS(200000, LR2_ER2_FR2, Full,
428 					  __MED(L) | __MED(E) | __MED(F)),
429 	__DEFINE_LINK_MODE_PARAMS(200000, DR2, Full, D),
430 	__DEFINE_LINK_MODE_PARAMS(200000, CR2, Full, C),
431 	__DEFINE_LINK_MODE_PARAMS(400000, KR4, Full, K),
432 	__DEFINE_LINK_MODE_PARAMS(400000, SR4, Full, S),
433 	__DEFINE_LINK_MODE_PARAMS_MEDIUMS(400000, LR4_ER4_FR4, Full,
434 					  __MED(L) | __MED(E) | __MED(F)),
435 	__DEFINE_LINK_MODE_PARAMS(400000, DR4, Full, D),
436 	__DEFINE_LINK_MODE_PARAMS(400000, CR4, Full, C),
437 	__DEFINE_LINK_MODE_PARAMS(100, FX, Half, F),
438 	__DEFINE_LINK_MODE_PARAMS(100, FX, Full, F),
439 	__DEFINE_LINK_MODE_PARAMS_PAIRS(10, T1L, 1, 1, Full, T),
440 	__DEFINE_LINK_MODE_PARAMS(800000, CR8, Full, C),
441 	__DEFINE_LINK_MODE_PARAMS(800000, KR8, Full, K),
442 	__DEFINE_LINK_MODE_PARAMS(800000, DR8, Full, D),
443 	__DEFINE_LINK_MODE_PARAMS(800000, DR8_2, Full, D),
444 	__DEFINE_LINK_MODE_PARAMS(800000, SR8, Full, S),
445 	__DEFINE_LINK_MODE_PARAMS(800000, VR8, Full, V),
446 	__DEFINE_LINK_MODE_PARAMS_PAIRS(10, T1S, 1, 1, Full, T),
447 	__DEFINE_LINK_MODE_PARAMS_PAIRS(10, T1S, 1, 1, Half, T),
448 	__DEFINE_LINK_MODE_PARAMS_PAIRS(10, T1S_P2MP, 1, 1, Half, T),
449 	__DEFINE_LINK_MODE_PARAMS_PAIRS(10, T1BRR, 1, 1, Full, T),
450 	__DEFINE_LINK_MODE_PARAMS(200000, CR, Full, C),
451 	__DEFINE_LINK_MODE_PARAMS(200000, KR, Full, K),
452 	__DEFINE_LINK_MODE_PARAMS(200000, DR, Full, D),
453 	__DEFINE_LINK_MODE_PARAMS(200000, DR_2, Full, D),
454 	__DEFINE_LINK_MODE_PARAMS(200000, SR, Full, S),
455 	__DEFINE_LINK_MODE_PARAMS(200000, VR, Full, V),
456 	__DEFINE_LINK_MODE_PARAMS(400000, CR2, Full, C),
457 	__DEFINE_LINK_MODE_PARAMS(400000, KR2, Full, K),
458 	__DEFINE_LINK_MODE_PARAMS(400000, DR2, Full, D),
459 	__DEFINE_LINK_MODE_PARAMS(400000, DR2_2, Full, D),
460 	__DEFINE_LINK_MODE_PARAMS(400000, SR2, Full, S),
461 	__DEFINE_LINK_MODE_PARAMS(400000, VR2, Full, V),
462 	__DEFINE_LINK_MODE_PARAMS(800000, CR4, Full, C),
463 	__DEFINE_LINK_MODE_PARAMS(800000, KR4, Full, K),
464 	__DEFINE_LINK_MODE_PARAMS(800000, DR4, Full, D),
465 	__DEFINE_LINK_MODE_PARAMS(800000, DR4_2, Full, D),
466 	__DEFINE_LINK_MODE_PARAMS(800000, SR4, Full, S),
467 	__DEFINE_LINK_MODE_PARAMS(800000, VR4, Full, V),
468 	__DEFINE_LINK_MODE_PARAMS(1600000, CR8, Full, C),
469 	__DEFINE_LINK_MODE_PARAMS(1600000, KR8, Full, K),
470 	__DEFINE_LINK_MODE_PARAMS(1600000, DR8, Full, D),
471 	__DEFINE_LINK_MODE_PARAMS(1600000, DR8_2, Full, D),
472 };
473 static_assert(ARRAY_SIZE(link_mode_params) == __ETHTOOL_LINK_MODE_MASK_NBITS);
474 EXPORT_SYMBOL_GPL(link_mode_params);
475 
476 static const char ethtool_link_medium_names[][ETH_GSTRING_LEN] = {
477 	[ETHTOOL_LINK_MEDIUM_BASET] = "BaseT",
478 	[ETHTOOL_LINK_MEDIUM_BASEK] = "BaseK",
479 	[ETHTOOL_LINK_MEDIUM_BASES] = "BaseS",
480 	[ETHTOOL_LINK_MEDIUM_BASEC] = "BaseC",
481 	[ETHTOOL_LINK_MEDIUM_BASEL] = "BaseL",
482 	[ETHTOOL_LINK_MEDIUM_BASED] = "BaseD",
483 	[ETHTOOL_LINK_MEDIUM_BASEE] = "BaseE",
484 	[ETHTOOL_LINK_MEDIUM_BASEF] = "BaseF",
485 	[ETHTOOL_LINK_MEDIUM_BASEV] = "BaseV",
486 	[ETHTOOL_LINK_MEDIUM_BASEMLD] = "BaseMLD",
487 	[ETHTOOL_LINK_MEDIUM_NONE] = "None",
488 };
489 static_assert(ARRAY_SIZE(ethtool_link_medium_names) == __ETHTOOL_LINK_MEDIUM_LAST);
490 
491 const char netif_msg_class_names[][ETH_GSTRING_LEN] = {
492 	[NETIF_MSG_DRV_BIT]		= "drv",
493 	[NETIF_MSG_PROBE_BIT]		= "probe",
494 	[NETIF_MSG_LINK_BIT]		= "link",
495 	[NETIF_MSG_TIMER_BIT]		= "timer",
496 	[NETIF_MSG_IFDOWN_BIT]		= "ifdown",
497 	[NETIF_MSG_IFUP_BIT]		= "ifup",
498 	[NETIF_MSG_RX_ERR_BIT]		= "rx_err",
499 	[NETIF_MSG_TX_ERR_BIT]		= "tx_err",
500 	[NETIF_MSG_TX_QUEUED_BIT]	= "tx_queued",
501 	[NETIF_MSG_INTR_BIT]		= "intr",
502 	[NETIF_MSG_TX_DONE_BIT]		= "tx_done",
503 	[NETIF_MSG_RX_STATUS_BIT]	= "rx_status",
504 	[NETIF_MSG_PKTDATA_BIT]		= "pktdata",
505 	[NETIF_MSG_HW_BIT]		= "hw",
506 	[NETIF_MSG_WOL_BIT]		= "wol",
507 };
508 static_assert(ARRAY_SIZE(netif_msg_class_names) == NETIF_MSG_CLASS_COUNT);
509 
510 const char wol_mode_names[][ETH_GSTRING_LEN] = {
511 	[const_ilog2(WAKE_PHY)]		= "phy",
512 	[const_ilog2(WAKE_UCAST)]	= "ucast",
513 	[const_ilog2(WAKE_MCAST)]	= "mcast",
514 	[const_ilog2(WAKE_BCAST)]	= "bcast",
515 	[const_ilog2(WAKE_ARP)]		= "arp",
516 	[const_ilog2(WAKE_MAGIC)]	= "magic",
517 	[const_ilog2(WAKE_MAGICSECURE)]	= "magicsecure",
518 	[const_ilog2(WAKE_FILTER)]	= "filter",
519 };
520 static_assert(ARRAY_SIZE(wol_mode_names) == WOL_MODE_COUNT);
521 
522 const char sof_timestamping_names[][ETH_GSTRING_LEN] = {
523 	[const_ilog2(SOF_TIMESTAMPING_TX_HARDWARE)]  = "hardware-transmit",
524 	[const_ilog2(SOF_TIMESTAMPING_TX_SOFTWARE)]  = "software-transmit",
525 	[const_ilog2(SOF_TIMESTAMPING_RX_HARDWARE)]  = "hardware-receive",
526 	[const_ilog2(SOF_TIMESTAMPING_RX_SOFTWARE)]  = "software-receive",
527 	[const_ilog2(SOF_TIMESTAMPING_SOFTWARE)]     = "software-system-clock",
528 	[const_ilog2(SOF_TIMESTAMPING_SYS_HARDWARE)] = "hardware-legacy-clock",
529 	[const_ilog2(SOF_TIMESTAMPING_RAW_HARDWARE)] = "hardware-raw-clock",
530 	[const_ilog2(SOF_TIMESTAMPING_OPT_ID)]       = "option-id",
531 	[const_ilog2(SOF_TIMESTAMPING_TX_SCHED)]     = "sched-transmit",
532 	[const_ilog2(SOF_TIMESTAMPING_TX_ACK)]       = "ack-transmit",
533 	[const_ilog2(SOF_TIMESTAMPING_OPT_CMSG)]     = "option-cmsg",
534 	[const_ilog2(SOF_TIMESTAMPING_OPT_TSONLY)]   = "option-tsonly",
535 	[const_ilog2(SOF_TIMESTAMPING_OPT_STATS)]    = "option-stats",
536 	[const_ilog2(SOF_TIMESTAMPING_OPT_PKTINFO)]  = "option-pktinfo",
537 	[const_ilog2(SOF_TIMESTAMPING_OPT_TX_SWHW)]  = "option-tx-swhw",
538 	[const_ilog2(SOF_TIMESTAMPING_BIND_PHC)]     = "bind-phc",
539 	[const_ilog2(SOF_TIMESTAMPING_OPT_ID_TCP)]   = "option-id-tcp",
540 	[const_ilog2(SOF_TIMESTAMPING_OPT_RX_FILTER)] = "option-rx-filter",
541 	[const_ilog2(SOF_TIMESTAMPING_TX_COMPLETION)] = "tx-completion",
542 };
543 static_assert(ARRAY_SIZE(sof_timestamping_names) == __SOF_TIMESTAMPING_CNT);
544 
545 const char ts_tx_type_names[][ETH_GSTRING_LEN] = {
546 	[HWTSTAMP_TX_OFF]		= "off",
547 	[HWTSTAMP_TX_ON]		= "on",
548 	[HWTSTAMP_TX_ONESTEP_SYNC]	= "onestep-sync",
549 	[HWTSTAMP_TX_ONESTEP_P2P]	= "onestep-p2p",
550 };
551 static_assert(ARRAY_SIZE(ts_tx_type_names) == __HWTSTAMP_TX_CNT);
552 
553 const char ts_rx_filter_names[][ETH_GSTRING_LEN] = {
554 	[HWTSTAMP_FILTER_NONE]			= "none",
555 	[HWTSTAMP_FILTER_ALL]			= "all",
556 	[HWTSTAMP_FILTER_SOME]			= "some",
557 	[HWTSTAMP_FILTER_PTP_V1_L4_EVENT]	= "ptpv1-l4-event",
558 	[HWTSTAMP_FILTER_PTP_V1_L4_SYNC]	= "ptpv1-l4-sync",
559 	[HWTSTAMP_FILTER_PTP_V1_L4_DELAY_REQ]	= "ptpv1-l4-delay-req",
560 	[HWTSTAMP_FILTER_PTP_V2_L4_EVENT]	= "ptpv2-l4-event",
561 	[HWTSTAMP_FILTER_PTP_V2_L4_SYNC]	= "ptpv2-l4-sync",
562 	[HWTSTAMP_FILTER_PTP_V2_L4_DELAY_REQ]	= "ptpv2-l4-delay-req",
563 	[HWTSTAMP_FILTER_PTP_V2_L2_EVENT]	= "ptpv2-l2-event",
564 	[HWTSTAMP_FILTER_PTP_V2_L2_SYNC]	= "ptpv2-l2-sync",
565 	[HWTSTAMP_FILTER_PTP_V2_L2_DELAY_REQ]	= "ptpv2-l2-delay-req",
566 	[HWTSTAMP_FILTER_PTP_V2_EVENT]		= "ptpv2-event",
567 	[HWTSTAMP_FILTER_PTP_V2_SYNC]		= "ptpv2-sync",
568 	[HWTSTAMP_FILTER_PTP_V2_DELAY_REQ]	= "ptpv2-delay-req",
569 	[HWTSTAMP_FILTER_NTP_ALL]		= "ntp-all",
570 };
571 static_assert(ARRAY_SIZE(ts_rx_filter_names) == __HWTSTAMP_FILTER_CNT);
572 
573 const char ts_flags_names[][ETH_GSTRING_LEN] = {
574 	[const_ilog2(HWTSTAMP_FLAG_BONDED_PHC_INDEX)] = "bonded-phc-index",
575 };
576 static_assert(ARRAY_SIZE(ts_flags_names) == __HWTSTAMP_FLAG_CNT);
577 
578 const char udp_tunnel_type_names[][ETH_GSTRING_LEN] = {
579 	[ETHTOOL_UDP_TUNNEL_TYPE_VXLAN]		= "vxlan",
580 	[ETHTOOL_UDP_TUNNEL_TYPE_GENEVE]	= "geneve",
581 	[ETHTOOL_UDP_TUNNEL_TYPE_VXLAN_GPE]	= "vxlan-gpe",
582 };
583 static_assert(ARRAY_SIZE(udp_tunnel_type_names) ==
584 	      __ETHTOOL_UDP_TUNNEL_TYPE_CNT);
585 
586 /* return false if legacy contained non-0 deprecated fields
587  * maxtxpkt/maxrxpkt. rest of ksettings always updated
588  */
589 bool
590 convert_legacy_settings_to_link_ksettings(
591 	struct ethtool_link_ksettings *link_ksettings,
592 	const struct ethtool_cmd *legacy_settings)
593 {
594 	bool retval = true;
595 
596 	memset(link_ksettings, 0, sizeof(*link_ksettings));
597 
598 	/* This is used to tell users that driver is still using these
599 	 * deprecated legacy fields, and they should not use
600 	 * %ETHTOOL_GLINKSETTINGS/%ETHTOOL_SLINKSETTINGS
601 	 */
602 	if (legacy_settings->maxtxpkt ||
603 	    legacy_settings->maxrxpkt)
604 		retval = false;
605 
606 	ethtool_convert_legacy_u32_to_link_mode(
607 		link_ksettings->link_modes.supported,
608 		legacy_settings->supported);
609 	ethtool_convert_legacy_u32_to_link_mode(
610 		link_ksettings->link_modes.advertising,
611 		legacy_settings->advertising);
612 	ethtool_convert_legacy_u32_to_link_mode(
613 		link_ksettings->link_modes.lp_advertising,
614 		legacy_settings->lp_advertising);
615 	link_ksettings->base.speed
616 		= ethtool_cmd_speed(legacy_settings);
617 	link_ksettings->base.duplex
618 		= legacy_settings->duplex;
619 	link_ksettings->base.port
620 		= legacy_settings->port;
621 	link_ksettings->base.phy_address
622 		= legacy_settings->phy_address;
623 	link_ksettings->base.autoneg
624 		= legacy_settings->autoneg;
625 	link_ksettings->base.mdio_support
626 		= legacy_settings->mdio_support;
627 	link_ksettings->base.eth_tp_mdix
628 		= legacy_settings->eth_tp_mdix;
629 	link_ksettings->base.eth_tp_mdix_ctrl
630 		= legacy_settings->eth_tp_mdix_ctrl;
631 	return retval;
632 }
633 
634 int __ethtool_get_link(struct net_device *dev)
635 {
636 	if (!dev->ethtool_ops->get_link)
637 		return -EOPNOTSUPP;
638 
639 	return netif_running(dev) && dev->ethtool_ops->get_link(dev);
640 }
641 
642 int ethtool_get_rx_ring_count(struct net_device *dev)
643 {
644 	const struct ethtool_ops *ops = dev->ethtool_ops;
645 
646 	if (!ops->get_rx_ring_count)
647 		return -EOPNOTSUPP;
648 
649 	return ops->get_rx_ring_count(dev);
650 }
651 
652 static int ethtool_get_rxnfc_rule_count(struct net_device *dev)
653 {
654 	const struct ethtool_ops *ops = dev->ethtool_ops;
655 	struct ethtool_rxnfc info = {
656 		.cmd = ETHTOOL_GRXCLSRLCNT,
657 	};
658 	int err;
659 
660 	err = ops->get_rxnfc(dev, &info, NULL);
661 	if (err)
662 		return err;
663 
664 	return info.rule_cnt;
665 }
666 
667 /* Max offset for one RSS context */
668 static u32 ethtool_get_rss_ctx_max_channel(struct ethtool_rxfh_context *ctx)
669 {
670 	u32 max_ring = 0;
671 	u32 i, *tbl;
672 
673 	if (WARN_ON_ONCE(!ctx))
674 		return 0;
675 	tbl = ethtool_rxfh_context_indir(ctx);
676 	for (i = 0; i < ctx->indir_size; i++)
677 		max_ring = max(max_ring, tbl[i]);
678 	return max_ring;
679 }
680 
681 static int ethtool_get_max_rxnfc_channel(struct net_device *dev, u64 *max)
682 {
683 	const struct ethtool_ops *ops = dev->ethtool_ops;
684 	struct ethtool_rxnfc *info;
685 	int err, i, rule_cnt;
686 	u64 max_ring = 0;
687 
688 	if (!ops->get_rxnfc)
689 		return -EOPNOTSUPP;
690 
691 	rule_cnt = ethtool_get_rxnfc_rule_count(dev);
692 	if (rule_cnt <= 0)
693 		return -EINVAL;
694 
695 	info = kvzalloc_flex(*info, rule_locs, rule_cnt);
696 	if (!info)
697 		return -ENOMEM;
698 
699 	info->cmd = ETHTOOL_GRXCLSRLALL;
700 	info->rule_cnt = rule_cnt;
701 	err = ops->get_rxnfc(dev, info, info->rule_locs);
702 	if (err)
703 		goto err_free_info;
704 
705 	for (i = 0; i < rule_cnt; i++) {
706 		struct ethtool_rxnfc rule_info = {
707 			.cmd = ETHTOOL_GRXCLSRULE,
708 			.fs.location = info->rule_locs[i],
709 		};
710 
711 		err = ops->get_rxnfc(dev, &rule_info, NULL);
712 		if (err)
713 			goto err_free_info;
714 
715 		if (rule_info.fs.ring_cookie != RX_CLS_FLOW_DISC &&
716 		    rule_info.fs.ring_cookie != RX_CLS_FLOW_WAKE &&
717 		    !ethtool_get_flow_spec_ring_vf(rule_info.fs.ring_cookie)) {
718 			u64 ring = rule_info.fs.ring_cookie;
719 
720 			if (rule_info.flow_type & FLOW_RSS) {
721 				struct ethtool_rxfh_context *ctx;
722 
723 				ctx = xa_load(&dev->ethtool->rss_ctx,
724 					      rule_info.rss_context);
725 				ring += ethtool_get_rss_ctx_max_channel(ctx);
726 			}
727 			max_ring = max_t(u64, max_ring, ring);
728 		}
729 	}
730 
731 	kvfree(info);
732 	*max = max_ring;
733 	return 0;
734 
735 err_free_info:
736 	kvfree(info);
737 	return err;
738 }
739 
740 /* Max offset across all of a device's RSS contexts */
741 static u32 ethtool_get_max_rss_ctx_channel(struct net_device *dev)
742 {
743 	struct ethtool_rxfh_context *ctx;
744 	unsigned long context;
745 	u32 max_ring = 0;
746 
747 	mutex_lock(&dev->ethtool->rss_lock);
748 	xa_for_each(&dev->ethtool->rss_ctx, context, ctx)
749 		max_ring = max(max_ring, ethtool_get_rss_ctx_max_channel(ctx));
750 	mutex_unlock(&dev->ethtool->rss_lock);
751 
752 	return max_ring;
753 }
754 
755 static u32 ethtool_get_max_rxfh_channel(struct net_device *dev)
756 {
757 	struct ethtool_rxfh_param rxfh = {};
758 	u32 dev_size, current_max = 0;
759 	int ret;
760 
761 	/* While we do track whether RSS context has an indirection
762 	 * table explicitly set by the user, no driver looks at that bit.
763 	 * Assume drivers won't auto-regenerate the additional tables,
764 	 * to be safe.
765 	 */
766 	current_max = ethtool_get_max_rss_ctx_channel(dev);
767 
768 	if (!netif_is_rxfh_configured(dev))
769 		return current_max;
770 
771 	if (!dev->ethtool_ops->get_rxfh_indir_size ||
772 	    !dev->ethtool_ops->get_rxfh)
773 		return current_max;
774 	dev_size = dev->ethtool_ops->get_rxfh_indir_size(dev);
775 	if (dev_size == 0)
776 		return current_max;
777 
778 	rxfh.indir = kzalloc_objs(rxfh.indir[0], dev_size, GFP_USER);
779 	if (!rxfh.indir)
780 		return U32_MAX;
781 
782 	mutex_lock(&dev->ethtool->rss_lock);
783 	ret = dev->ethtool_ops->get_rxfh(dev, &rxfh);
784 	mutex_unlock(&dev->ethtool->rss_lock);
785 	if (ret) {
786 		current_max = U32_MAX;
787 		goto out_free;
788 	}
789 
790 	while (dev_size--)
791 		current_max = max(current_max, rxfh.indir[dev_size]);
792 
793 out_free:
794 	kfree(rxfh.indir);
795 	return current_max;
796 }
797 
798 int ethtool_check_max_channel(struct net_device *dev,
799 			      struct ethtool_channels channels,
800 			      struct genl_info *info)
801 {
802 	u64 max_rxnfc_in_use;
803 	u32 max_rxfh_in_use;
804 	int max_mp_in_use;
805 
806 	/* ensure the new Rx count fits within the configured Rx flow
807 	 * indirection table/rxnfc settings
808 	 */
809 	if (ethtool_get_max_rxnfc_channel(dev, &max_rxnfc_in_use))
810 		max_rxnfc_in_use = 0;
811 	max_rxfh_in_use = ethtool_get_max_rxfh_channel(dev);
812 	if (channels.combined_count + channels.rx_count <= max_rxfh_in_use) {
813 		if (info)
814 			GENL_SET_ERR_MSG_FMT(info, "requested channel counts are too low for existing indirection table (%d)", max_rxfh_in_use);
815 		return -EINVAL;
816 	}
817 	if (channels.combined_count + channels.rx_count <= max_rxnfc_in_use) {
818 		if (info)
819 			GENL_SET_ERR_MSG(info, "requested channel counts are too low for existing ntuple filter settings");
820 		return -EINVAL;
821 	}
822 
823 	max_mp_in_use = dev_get_min_mp_channel_count(dev);
824 	if (channels.combined_count + channels.rx_count <= max_mp_in_use) {
825 		if (info)
826 			GENL_SET_ERR_MSG_FMT(info, "requested channel counts are too low for existing memory provider setting (%d)", max_mp_in_use);
827 		return -EINVAL;
828 	}
829 
830 	return 0;
831 }
832 
833 int ethtool_check_rss_ctx_busy(struct net_device *dev, u32 rss_context)
834 {
835 	const struct ethtool_ops *ops = dev->ethtool_ops;
836 	struct ethtool_rxnfc *info;
837 	int rc, i, rule_cnt;
838 
839 	if (!ops->get_rxnfc)
840 		return 0;
841 
842 	rule_cnt = ethtool_get_rxnfc_rule_count(dev);
843 	if (!rule_cnt)
844 		return 0;
845 
846 	if (rule_cnt < 0)
847 		return -EINVAL;
848 
849 	info = kvzalloc_flex(*info, rule_locs, rule_cnt);
850 	if (!info)
851 		return -ENOMEM;
852 
853 	info->cmd = ETHTOOL_GRXCLSRLALL;
854 	info->rule_cnt = rule_cnt;
855 	rc = ops->get_rxnfc(dev, info, info->rule_locs);
856 	if (rc)
857 		goto out_free;
858 
859 	for (i = 0; i < rule_cnt; i++) {
860 		struct ethtool_rxnfc rule_info = {
861 			.cmd = ETHTOOL_GRXCLSRULE,
862 			.fs.location = info->rule_locs[i],
863 		};
864 
865 		rc = ops->get_rxnfc(dev, &rule_info, NULL);
866 		if (rc)
867 			goto out_free;
868 
869 		if (rule_info.fs.flow_type & FLOW_RSS &&
870 		    rule_info.rss_context == rss_context) {
871 			rc = -EBUSY;
872 			goto out_free;
873 		}
874 	}
875 
876 out_free:
877 	kvfree(info);
878 	return rc;
879 }
880 
881 struct ethtool_rxfh_context *
882 ethtool_rxfh_ctx_alloc(const struct ethtool_ops *ops,
883 		       u32 indir_size, u32 key_size)
884 {
885 	size_t indir_bytes, flex_len, key_off, size;
886 	struct ethtool_rxfh_context *ctx;
887 	u32 priv_bytes, indir_max;
888 	u16 key_max;
889 
890 	key_max = max(key_size, ops->rxfh_key_space);
891 	indir_max = max(indir_size, ops->rxfh_indir_space);
892 
893 	priv_bytes = ALIGN(ops->rxfh_priv_size, sizeof(u32));
894 	indir_bytes = array_size(indir_max, sizeof(u32));
895 
896 	key_off = size_add(priv_bytes, indir_bytes);
897 	flex_len = size_add(key_off, key_max);
898 	size = struct_size_t(struct ethtool_rxfh_context, data, flex_len);
899 
900 	ctx = kzalloc(size, GFP_KERNEL_ACCOUNT);
901 	if (!ctx)
902 		return NULL;
903 
904 	ctx->indir_size = indir_size;
905 	ctx->key_size = key_size;
906 	ctx->key_off = key_off;
907 	ctx->priv_size = ops->rxfh_priv_size;
908 
909 	return ctx;
910 }
911 
912 /* Check if fields configured for flow hash are symmetric - if src is included
913  * so is dst and vice versa.
914  */
915 int ethtool_rxfh_config_is_sym(u64 rxfh)
916 {
917 	bool sym;
918 
919 	sym = rxfh == (rxfh & (RXH_IP_SRC | RXH_IP_DST |
920 			       RXH_L4_B_0_1 | RXH_L4_B_2_3));
921 	sym &= !!(rxfh & RXH_IP_SRC)   == !!(rxfh & RXH_IP_DST);
922 	sym &= !!(rxfh & RXH_L4_B_0_1) == !!(rxfh & RXH_L4_B_2_3);
923 
924 	return sym;
925 }
926 
927 int ethtool_check_ops(const struct ethtool_ops *ops)
928 {
929 	if (WARN_ON(ops->set_coalesce && !ops->supported_coalesce_params))
930 		return -EINVAL;
931 	if (WARN_ON(ops->rxfh_max_num_contexts == 1))
932 		return -EINVAL;
933 	if (WARN_ON(ops->supported_input_xfrm && !ops->get_rxfh_fields))
934 		return -EINVAL;
935 	if (WARN_ON(ops->supported_input_xfrm &&
936 		    ops->rxfh_per_ctx_fields != ops->rxfh_per_ctx_key))
937 		return -EINVAL;
938 
939 	/* NOTE: sufficiently insane drivers may swap ethtool_ops at runtime,
940 	 * the fact that ops are checked at registration time does not
941 	 * mean the ops attached to a netdev later on are sane.
942 	 */
943 	return 0;
944 }
945 
946 void ethtool_ringparam_get_cfg(struct net_device *dev,
947 			       struct ethtool_ringparam *param,
948 			       struct kernel_ethtool_ringparam *kparam,
949 			       struct netlink_ext_ack *extack)
950 {
951 	memset(param, 0, sizeof(*param));
952 	memset(kparam, 0, sizeof(*kparam));
953 
954 	param->cmd = ETHTOOL_GRINGPARAM;
955 	dev->ethtool_ops->get_ringparam(dev, param, kparam, extack);
956 
957 	/* Driver gives us current state, we want to return current config */
958 	kparam->tcp_data_split = dev->cfg->hds_config;
959 	kparam->hds_thresh = dev->cfg->hds_thresh;
960 }
961 
962 void ethtool_ringparam_set_cfg(struct netdev_config *cfg,
963 			       const struct ethtool_ringparam *param)
964 {
965 	cfg->rings.rx_pending = param->rx_pending;
966 	cfg->rings.rx_mini_pending = param->rx_mini_pending;
967 	cfg->rings.rx_jumbo_pending = param->rx_jumbo_pending;
968 }
969 
970 static void ethtool_init_tsinfo(struct kernel_ethtool_ts_info *info)
971 {
972 	memset(info, 0, sizeof(*info));
973 	info->cmd = ETHTOOL_GET_TS_INFO;
974 	info->phc_index = -1;
975 }
976 
977 int ethtool_net_get_ts_info_by_phc(struct net_device *dev,
978 				   struct kernel_ethtool_ts_info *info,
979 				   struct hwtstamp_provider_desc *hwprov_desc)
980 {
981 	const struct ethtool_ops *ops = dev->ethtool_ops;
982 	int err;
983 
984 	if (!ops->get_ts_info)
985 		return -EOPNOTSUPP;
986 
987 	/* Does ptp comes from netdev */
988 	ethtool_init_tsinfo(info);
989 	info->phc_qualifier = hwprov_desc->qualifier;
990 	err = ops->get_ts_info(dev, info);
991 	if (err)
992 		return err;
993 
994 	if (info->phc_index == hwprov_desc->index &&
995 	    net_support_hwtstamp_qualifier(dev, hwprov_desc->qualifier))
996 		return 0;
997 
998 	return -ENODEV;
999 }
1000 
1001 struct phy_device *
1002 ethtool_phy_get_ts_info_by_phc(struct net_device *dev,
1003 			       struct kernel_ethtool_ts_info *info,
1004 			       struct hwtstamp_provider_desc *hwprov_desc)
1005 {
1006 	int err;
1007 
1008 	/* Only precise qualifier is supported in phydev */
1009 	if (hwprov_desc->qualifier != HWTSTAMP_PROVIDER_QUALIFIER_PRECISE)
1010 		return ERR_PTR(-ENODEV);
1011 
1012 	/* Look in the phy topology */
1013 	if (dev->link_topo) {
1014 		struct phy_device_node *pdn;
1015 		unsigned long phy_index;
1016 
1017 		xa_for_each(&dev->link_topo->phys, phy_index, pdn) {
1018 			if (!phy_has_tsinfo(pdn->phy))
1019 				continue;
1020 
1021 			ethtool_init_tsinfo(info);
1022 			err = phy_ts_info(pdn->phy, info);
1023 			if (err)
1024 				return ERR_PTR(err);
1025 
1026 			if (info->phc_index == hwprov_desc->index)
1027 				return pdn->phy;
1028 		}
1029 		return ERR_PTR(-ENODEV);
1030 	}
1031 
1032 	/* Look on the dev->phydev */
1033 	if (phy_has_tsinfo(dev->phydev)) {
1034 		ethtool_init_tsinfo(info);
1035 		err = phy_ts_info(dev->phydev, info);
1036 		if (err)
1037 			return ERR_PTR(err);
1038 
1039 		if (info->phc_index == hwprov_desc->index)
1040 			return dev->phydev;
1041 	}
1042 
1043 	return ERR_PTR(-ENODEV);
1044 }
1045 
1046 int ethtool_get_ts_info_by_phc(struct net_device *dev,
1047 			       struct kernel_ethtool_ts_info *info,
1048 			       struct hwtstamp_provider_desc *hwprov_desc)
1049 {
1050 	int err;
1051 
1052 	err = ethtool_net_get_ts_info_by_phc(dev, info, hwprov_desc);
1053 	if (err == -ENODEV || err == -EOPNOTSUPP) {
1054 		struct phy_device *phy;
1055 
1056 		phy = ethtool_phy_get_ts_info_by_phc(dev, info, hwprov_desc);
1057 		if (IS_ERR(phy))
1058 			return PTR_ERR(phy);
1059 
1060 		/* Report the phc source only if we have a real
1061 		 * phc source with an index.
1062 		 */
1063 		if (info->phc_index >= 0) {
1064 			info->phc_source = HWTSTAMP_SOURCE_PHYLIB;
1065 			info->phc_phyindex = phy->phyindex;
1066 		}
1067 		err = 0;
1068 	} else if (!err && info->phc_index >= 0) {
1069 		info->phc_source = HWTSTAMP_SOURCE_NETDEV;
1070 	}
1071 
1072 	info->so_timestamping |= SOF_TIMESTAMPING_RX_SOFTWARE |
1073 				 SOF_TIMESTAMPING_SOFTWARE;
1074 
1075 	return err;
1076 }
1077 
1078 int __ethtool_get_ts_info(struct net_device *dev,
1079 			  struct kernel_ethtool_ts_info *info)
1080 {
1081 	struct hwtstamp_provider *hwprov;
1082 	int err = 0;
1083 
1084 	rcu_read_lock();
1085 	hwprov = rcu_dereference(dev->hwprov);
1086 	/* No provider specified, use default behavior */
1087 	if (!hwprov) {
1088 		const struct ethtool_ops *ops = dev->ethtool_ops;
1089 		struct phy_device *phydev = dev->phydev;
1090 
1091 		ethtool_init_tsinfo(info);
1092 		if (phy_is_default_hwtstamp(phydev) &&
1093 		    phy_has_tsinfo(phydev)) {
1094 			err = phy_ts_info(phydev, info);
1095 			/* Report the phc source only if we have a real
1096 			 * phc source with an index.
1097 			 */
1098 			if (!err && info->phc_index >= 0) {
1099 				info->phc_source = HWTSTAMP_SOURCE_PHYLIB;
1100 				info->phc_phyindex = phydev->phyindex;
1101 			}
1102 		} else if (ops->get_ts_info) {
1103 			err = ops->get_ts_info(dev, info);
1104 			if (!err && info->phc_index >= 0)
1105 				info->phc_source = HWTSTAMP_SOURCE_NETDEV;
1106 		}
1107 
1108 		info->so_timestamping |= SOF_TIMESTAMPING_RX_SOFTWARE |
1109 					 SOF_TIMESTAMPING_SOFTWARE;
1110 
1111 		rcu_read_unlock();
1112 		return err;
1113 	}
1114 
1115 	err = ethtool_get_ts_info_by_phc(dev, info, &hwprov->desc);
1116 	rcu_read_unlock();
1117 	return err;
1118 }
1119 
1120 bool net_support_hwtstamp_qualifier(struct net_device *dev,
1121 				    enum hwtstamp_provider_qualifier qualifier)
1122 {
1123 	const struct ethtool_ops *ops = dev->ethtool_ops;
1124 
1125 	if (!ops)
1126 		return false;
1127 
1128 	/* Return true with precise qualifier and with NIC without
1129 	 * qualifier description to not break the old behavior.
1130 	 */
1131 	if (!ops->supported_hwtstamp_qualifiers &&
1132 	    qualifier == HWTSTAMP_PROVIDER_QUALIFIER_PRECISE)
1133 		return true;
1134 
1135 	if (ops->supported_hwtstamp_qualifiers & BIT(qualifier))
1136 		return true;
1137 
1138 	return false;
1139 }
1140 
1141 int ethtool_get_phc_vclocks(struct net_device *dev, int **vclock_index)
1142 {
1143 	struct kernel_ethtool_ts_info info = { };
1144 	int num = 0;
1145 
1146 	if (!__ethtool_get_ts_info(dev, &info))
1147 		num = ptp_get_vclocks_index(info.phc_index, vclock_index);
1148 
1149 	return num;
1150 }
1151 EXPORT_SYMBOL(ethtool_get_phc_vclocks);
1152 
1153 int ethtool_get_ts_info_by_layer(struct net_device *dev, struct kernel_ethtool_ts_info *info)
1154 {
1155 	return __ethtool_get_ts_info(dev, info);
1156 }
1157 EXPORT_SYMBOL(ethtool_get_ts_info_by_layer);
1158 
1159 const struct ethtool_phy_ops *ethtool_phy_ops;
1160 
1161 void ethtool_set_ethtool_phy_ops(const struct ethtool_phy_ops *ops)
1162 {
1163 	ASSERT_RTNL();
1164 	ethtool_phy_ops = ops;
1165 }
1166 EXPORT_SYMBOL_GPL(ethtool_set_ethtool_phy_ops);
1167 
1168 void
1169 ethtool_params_from_link_mode(struct ethtool_link_ksettings *link_ksettings,
1170 			      enum ethtool_link_mode_bit_indices link_mode)
1171 {
1172 	const struct link_mode_info *link_info;
1173 
1174 	if (WARN_ON_ONCE(link_mode >= __ETHTOOL_LINK_MODE_MASK_NBITS))
1175 		return;
1176 
1177 	link_info = &link_mode_params[link_mode];
1178 	link_ksettings->base.speed = link_info->speed;
1179 	link_ksettings->lanes = link_info->lanes;
1180 	link_ksettings->base.duplex = link_info->duplex;
1181 }
1182 EXPORT_SYMBOL_GPL(ethtool_params_from_link_mode);
1183 
1184 /**
1185  * ethtool_forced_speed_maps_init
1186  * @maps: Pointer to an array of Ethtool forced speed map
1187  * @size: Array size
1188  *
1189  * Initialize an array of Ethtool forced speed map to Ethtool link modes. This
1190  * should be called during driver module init.
1191  */
1192 void
1193 ethtool_forced_speed_maps_init(struct ethtool_forced_speed_map *maps, u32 size)
1194 {
1195 	for (u32 i = 0; i < size; i++) {
1196 		struct ethtool_forced_speed_map *map = &maps[i];
1197 
1198 		linkmode_set_bit_array(map->cap_arr, map->arr_size, map->caps);
1199 		map->cap_arr = NULL;
1200 		map->arr_size = 0;
1201 	}
1202 }
1203 EXPORT_SYMBOL_GPL(ethtool_forced_speed_maps_init);
1204 
1205 void ethtool_rxfh_context_lost(struct net_device *dev, u32 context_id)
1206 {
1207 	struct ethtool_rxfh_context *ctx;
1208 
1209 	WARN_ONCE(!rtnl_is_locked() &&
1210 		  !lockdep_is_held_type(&dev->ethtool->rss_lock, -1),
1211 		  "RSS context lock assertion failed\n");
1212 
1213 	netdev_err(dev, "device error, RSS context %d lost\n", context_id);
1214 	ctx = xa_erase(&dev->ethtool->rss_ctx, context_id);
1215 	kfree(ctx);
1216 	ethtool_rss_notify(dev, ETHTOOL_MSG_RSS_DELETE_NTF, context_id);
1217 }
1218 EXPORT_SYMBOL(ethtool_rxfh_context_lost);
1219 
1220 bool netif_is_rxfh_configured(const struct net_device *dev)
1221 {
1222 	return dev->ethtool->rss_indir_user_size;
1223 }
1224 EXPORT_SYMBOL(netif_is_rxfh_configured);
1225 
1226 /**
1227  * ethtool_rxfh_indir_lost - Notify core that the RSS indirection table was lost
1228  * @dev: network device
1229  *
1230  * Drivers should call this when the device can no longer maintain the
1231  * user-configured indirection table, typically after a HW fault recovery
1232  * that reduced the maximum queue count. Marks the default RSS context
1233  * indirection table as unconfigured and sends an %ETHTOOL_MSG_RSS_NTF
1234  * notification.
1235  */
1236 void ethtool_rxfh_indir_lost(struct net_device *dev)
1237 {
1238 	WARN_ONCE(!rtnl_is_locked() &&
1239 		  !lockdep_is_held_type(&dev->ethtool->rss_lock, -1),
1240 		  "RSS context lock assertion failed\n");
1241 
1242 	netdev_err(dev, "device error, RSS indirection table lost\n");
1243 	dev->ethtool->rss_indir_user_size = 0;
1244 	ethtool_rss_notify(dev, ETHTOOL_MSG_RSS_NTF, 0);
1245 }
1246 EXPORT_SYMBOL(ethtool_rxfh_indir_lost);
1247 
1248 static bool ethtool_rxfh_is_periodic(const u32 *tbl, u32 old_size, u32 new_size)
1249 {
1250 	u32 i;
1251 
1252 	for (i = new_size; i < old_size; i++)
1253 		if (tbl[i] != tbl[i % new_size])
1254 			return false;
1255 	return true;
1256 }
1257 
1258 static bool ethtool_rxfh_can_resize(const u32 *tbl, u32 old_size, u32 new_size,
1259 				    u32 user_size)
1260 {
1261 	if (new_size == old_size)
1262 		return true;
1263 
1264 	if (!user_size)
1265 		return true;
1266 
1267 	if (new_size < old_size) {
1268 		if (new_size < user_size)
1269 			return false;
1270 		if (old_size % new_size)
1271 			return false;
1272 		if (!ethtool_rxfh_is_periodic(tbl, old_size, new_size))
1273 			return false;
1274 		return true;
1275 	}
1276 
1277 	if (new_size % old_size)
1278 		return false;
1279 	return true;
1280 }
1281 
1282 /* Resize without validation; caller must have called can_resize first */
1283 static void ethtool_rxfh_resize(u32 *tbl, u32 old_size, u32 new_size)
1284 {
1285 	u32 i;
1286 
1287 	/* Grow: replicate existing pattern; shrink is a no-op on the data */
1288 	for (i = old_size; i < new_size; i++)
1289 		tbl[i] = tbl[i % old_size];
1290 }
1291 
1292 /**
1293  * ethtool_rxfh_indir_can_resize - Check if context 0 indir table can resize
1294  * @dev: network device
1295  * @tbl: indirection table
1296  * @old_size: current number of entries in the table
1297  * @new_size: desired number of entries
1298  *
1299  * Validate that @tbl can be resized from @old_size to @new_size without
1300  * data loss. Uses the user_size floor from context 0. When user_size is
1301  * zero the table is not user-configured and resize always succeeds.
1302  * Read-only; does not modify the table.
1303  *
1304  * Return: true if resize is possible, false otherwise.
1305  */
1306 bool ethtool_rxfh_indir_can_resize(struct net_device *dev, const u32 *tbl,
1307 				   u32 old_size, u32 new_size)
1308 {
1309 	return ethtool_rxfh_can_resize(tbl, old_size, new_size,
1310 				       dev->ethtool->rss_indir_user_size);
1311 }
1312 EXPORT_SYMBOL(ethtool_rxfh_indir_can_resize);
1313 
1314 /**
1315  * ethtool_rxfh_indir_resize - Fold or unfold context 0 indirection table
1316  * @dev: network device
1317  * @tbl: indirection table (must have room for max(old_size, new_size) entries)
1318  * @old_size: current number of entries in the table
1319  * @new_size: desired number of entries
1320  *
1321  * Resize the default RSS context indirection table in place. Caller
1322  * must have validated with ethtool_rxfh_indir_can_resize() first.
1323  */
1324 void ethtool_rxfh_indir_resize(struct net_device *dev, u32 *tbl,
1325 			       u32 old_size, u32 new_size)
1326 {
1327 	if (!dev->ethtool->rss_indir_user_size)
1328 		return;
1329 
1330 	ethtool_rxfh_resize(tbl, old_size, new_size);
1331 }
1332 EXPORT_SYMBOL(ethtool_rxfh_indir_resize);
1333 
1334 /**
1335  * ethtool_rxfh_ctxs_can_resize - Validate resize for all RSS contexts
1336  * @dev: network device
1337  * @new_indir_size: new indirection table size
1338  *
1339  * Validate that the indirection tables of all non-default RSS contexts
1340  * can be resized to @new_indir_size. Read-only; does not modify any
1341  * context. Intended to be paired with ethtool_rxfh_ctxs_resize().
1342  *
1343  * Return: 0 if all contexts can be resized, negative errno on failure.
1344  */
1345 int ethtool_rxfh_ctxs_can_resize(struct net_device *dev, u32 new_indir_size)
1346 {
1347 	struct ethtool_rxfh_context *ctx;
1348 	unsigned long context;
1349 	int ret = 0;
1350 
1351 	if (!dev->ethtool_ops->rxfh_indir_space ||
1352 	    new_indir_size > dev->ethtool_ops->rxfh_indir_space)
1353 		return -EINVAL;
1354 
1355 	mutex_lock(&dev->ethtool->rss_lock);
1356 	xa_for_each(&dev->ethtool->rss_ctx, context, ctx) {
1357 		u32 *indir = ethtool_rxfh_context_indir(ctx);
1358 
1359 		if (!ethtool_rxfh_can_resize(indir, ctx->indir_size,
1360 					     new_indir_size,
1361 					     ctx->indir_user_size)) {
1362 			ret = -EINVAL;
1363 			goto unlock;
1364 		}
1365 	}
1366 unlock:
1367 	mutex_unlock(&dev->ethtool->rss_lock);
1368 	return ret;
1369 }
1370 EXPORT_SYMBOL(ethtool_rxfh_ctxs_can_resize);
1371 
1372 /**
1373  * ethtool_rxfh_ctxs_resize - Resize all RSS context indirection tables
1374  * @dev: network device
1375  * @new_indir_size: new indirection table size
1376  *
1377  * Resize the indirection table of every non-default RSS context to
1378  * @new_indir_size. Caller must have validated with
1379  * ethtool_rxfh_ctxs_can_resize() first. An %ETHTOOL_MSG_RSS_NTF is
1380  * sent for each resized context.
1381  *
1382  * Notifications are sent outside the RSS lock to avoid holding the
1383  * mutex during notification delivery.
1384  */
1385 void ethtool_rxfh_ctxs_resize(struct net_device *dev, u32 new_indir_size)
1386 {
1387 	struct ethtool_rxfh_context *ctx;
1388 	unsigned long context;
1389 
1390 	mutex_lock(&dev->ethtool->rss_lock);
1391 	xa_for_each(&dev->ethtool->rss_ctx, context, ctx) {
1392 		ethtool_rxfh_resize(ethtool_rxfh_context_indir(ctx),
1393 				    ctx->indir_size, new_indir_size);
1394 		ctx->indir_size = new_indir_size;
1395 	}
1396 	mutex_unlock(&dev->ethtool->rss_lock);
1397 
1398 	xa_for_each(&dev->ethtool->rss_ctx, context, ctx)
1399 		ethtool_rss_notify(dev, ETHTOOL_MSG_RSS_NTF, context);
1400 }
1401 EXPORT_SYMBOL(ethtool_rxfh_ctxs_resize);
1402 
1403 enum ethtool_link_medium ethtool_str_to_medium(const char *str)
1404 {
1405 	int i;
1406 
1407 	for (i = 0; i < __ETHTOOL_LINK_MEDIUM_LAST; i++)
1408 		if (!strcmp(ethtool_link_medium_names[i], str))
1409 			return i;
1410 
1411 	return ETHTOOL_LINK_MEDIUM_NONE;
1412 }
1413 EXPORT_SYMBOL_GPL(ethtool_str_to_medium);
1414 
1415 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1416 bool netdev_rss_key_initialized __read_mostly;
1417 
1418 /* Toeplitz is linear over GF(2): the hash is the XOR of the 32-bit key
1419  * windows selected by the set bits of the input, and hardware indexes the
1420  * indirection table with the low order bits of the hash. Only the tail of
1421  * each window therefore matters for queue selection:
1422  *
1423  *	v(i) = key bits [i + 32 - q .. i + 31]
1424  *
1425  * for input bit @i, with q = log2(number of RX queues). Consecutive input
1426  * bits give windows overlapping in q - 1 positions, so the matrix formed by
1427  * the windows of the q lowest bits of a header field is a Toeplitz matrix
1428  * built from 2 * q - 1 key bits, not from q * q independent ones. A random
1429  * Toeplitz matrix over GF(2) is singular with probability 1/2, whatever its
1430  * size, and when it is singular the flows of a burst differing only in the
1431  * low order bits of that field (consecutive ephemeral ports, typically)
1432  * cannot reach all the RX queues no matter how many of them are configured.
1433  *
1434  * Keep the key random, but constrain the few bits that decide this. The
1435  * fixup is applied at every 16-bit aligned position of the key, rather than
1436  * at the offsets of the one hash input layout the software happens to know
1437  * about: hardware is free to hash whatever it wants, but the fields it picks
1438  * are 16 bits wide at the smallest and are not expected to straddle that
1439  * grid, so an encapsulated or offloaded layout is covered like the usual
1440  * 2-tuple and 4-tuple ones. Each position constrains 2 * q - 1 bits, so
1441  * NETDEV_RSS_KEY_QMAX is both enough for 256 queues and the largest value
1442  * keeping the ranges of two adjacent positions disjoint.
1443  */
1444 #define NETDEV_RSS_KEY_QMAX	8
1445 #define NETDEV_RSS_KEY_SPAN	(2 * NETDEV_RSS_KEY_QMAX - 1)
1446 
1447 static bool netdev_rss_key_bit(const u8 *key, unsigned int bit)
1448 {
1449 	return key[bit / BITS_PER_BYTE] & (0x80 >> (bit % BITS_PER_BYTE));
1450 }
1451 
1452 static void netdev_rss_key_assign_bit(u8 *key, unsigned int bit, bool value)
1453 {
1454 	u8 mask = 0x80 >> (bit % BITS_PER_BYTE);
1455 
1456 	if (value)
1457 		key[bit / BITS_PER_BYTE] |= mask;
1458 	else
1459 		key[bit / BITS_PER_BYTE] &= ~mask;
1460 }
1461 
1462 /* Writing d[t] for key bit (@lsb + 31 - t), the matrices of all the q values
1463  * up to NETDEV_RSS_KEY_QMAX are non singular if and only if
1464  *
1465  *	d[2 * i] = 1 ^ d[i] ^ d[i + 1] ^ ... ^ d[2 * i - 1]
1466  *
1467  * The odd positions stay free, so this is a one pass fixup rather than a
1468  * search. It is also a bijection onto the set of the values having the
1469  * property, so the key stays uniformly distributed over that set. It costs
1470  * NETDEV_RSS_KEY_QMAX bits of entropy per position.
1471  */
1472 static void netdev_rss_key_fixup_field(u8 *key, unsigned int lsb)
1473 {
1474 	bool d[NETDEV_RSS_KEY_SPAN];
1475 	unsigned int i, t;
1476 
1477 	for (t = 0; t < NETDEV_RSS_KEY_SPAN; t++)
1478 		d[t] = netdev_rss_key_bit(key, lsb + 31 - t);
1479 
1480 	for (i = 0; 2 * i < NETDEV_RSS_KEY_SPAN; i++) {
1481 		bool value = true;
1482 
1483 		for (t = i; t < 2 * i; t++)
1484 			value ^= d[t];
1485 
1486 		d[2 * i] = value;
1487 	}
1488 
1489 	for (t = 0; t < NETDEV_RSS_KEY_SPAN; t++)
1490 		netdev_rss_key_assign_bit(key, lsb + 31 - t, d[t]);
1491 }
1492 
1493 /* The 32 key bits starting at @bit, which is what input bit @bit contributes
1494  * to the hash. @bit + 32 must fit in the key.
1495  */
1496 static u32 netdev_rss_key_window(const u8 *key, unsigned int bit)
1497 {
1498 	unsigned int byte = bit / BITS_PER_BYTE;
1499 	unsigned int shift = bit % BITS_PER_BYTE;
1500 	u32 window = get_unaligned_be32(key + byte);
1501 
1502 	if (shift)
1503 		window = (window << shift) |
1504 			 (key[byte + 4] >> (BITS_PER_BYTE - shift));
1505 
1506 	return window;
1507 }
1508 
1509 /* Two input bits contributing the same window are indistinguishable to the
1510  * hash, since flipping both of them leaves it unchanged. For a uniformly
1511  * random key that is a 2 ** -32 event per pair of positions, but the fixup
1512  * makes it likelier: it derives 8 of every 16 bits from the 8 others, so a
1513  * 16-bit aligned word only takes 2 ** 8 values and two windows a whole
1514  * number of words apart collide with probability 2 ** -16 instead. Half a
1515  * word apart is less affected but still well clear of the random odds, so
1516  * cover every distance that is a multiple of 8. What is left after that is
1517  * below what a plain random key gives.
1518  *
1519  * Two windows at a distance that is a multiple of 8 are two windows at the
1520  * same offset modulo 8. Caching a whole class would put 253 u32 on the
1521  * stack, so cache one class modulo 16 and stream the class 8 bits above it
1522  * against it.
1523  */
1524 static bool netdev_rss_key_aliases(const u8 *key, unsigned int bits)
1525 {
1526 	u32 windows[NETDEV_RSS_KEY_LEN * BITS_PER_BYTE / 16];
1527 	unsigned int i, j, n, r;
1528 
1529 	for (r = 0; r < 16; r++) {
1530 		n = 0;
1531 		for (i = r; i + 32 <= bits; i += 16)
1532 			windows[n++] = netdev_rss_key_window(key, i);
1533 
1534 		for (i = 0; i < n; i++)
1535 			for (j = i + 1; j < n; j++)
1536 				if (windows[i] == windows[j])
1537 					return true;
1538 
1539 		if (r >= 8)
1540 			continue;
1541 
1542 		for (i = r + 8; i + 32 <= bits; i += 16) {
1543 			u32 window = netdev_rss_key_window(key, i);
1544 
1545 			for (j = 0; j < n; j++)
1546 				if (windows[j] == window)
1547 					return true;
1548 		}
1549 	}
1550 
1551 	return false;
1552 }
1553 
1554 static void netdev_rss_key_init(u8 *key, size_t len)
1555 {
1556 	unsigned int lsb, bits = len * BITS_PER_BYTE;
1557 
1558 	/* Four keys out of five come out of the fixup free of aliases, so
1559 	 * drawing another one is both simpler and cheaper than repairing.
1560 	 */
1561 	do {
1562 		get_random_bytes(key, len);
1563 
1564 		/* A field ending at bit @lsb uses key bits [.. , @lsb + 31],
1565 		 * so stop as soon as a 32-bit window no longer fits in the
1566 		 * key.
1567 		 */
1568 		for (lsb = 15; lsb + 32 <= bits; lsb += 16)
1569 			netdev_rss_key_fixup_field(key, lsb);
1570 	} while (netdev_rss_key_aliases(key, bits));
1571 
1572 	/* Pair with smp_rmb() in proc_do_rss_key(). */
1573 	smp_wmb();
1574 	WRITE_ONCE(netdev_rss_key_initialized, true);
1575 }
1576 
1577 void netdev_rss_key_fill(void *buffer, size_t len)
1578 {
1579 	if (WARN_ON_ONCE(len > sizeof(netdev_rss_key)))
1580 		len = sizeof(netdev_rss_key);
1581 	DO_ONCE(netdev_rss_key_init, netdev_rss_key, sizeof(netdev_rss_key));
1582 	memcpy(buffer, netdev_rss_key, len);
1583 }
1584 EXPORT_SYMBOL(netdev_rss_key_fill);
1585