xref: /linux/net/ethtool/common.c (revision b0e75977c6883283b55fbbdcd64d1c7111b134f2)
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 static void ethtool_init_tsinfo(struct kernel_ethtool_ts_info *info)
963 {
964 	memset(info, 0, sizeof(*info));
965 	info->cmd = ETHTOOL_GET_TS_INFO;
966 	info->phc_index = -1;
967 }
968 
969 int ethtool_net_get_ts_info_by_phc(struct net_device *dev,
970 				   struct kernel_ethtool_ts_info *info,
971 				   struct hwtstamp_provider_desc *hwprov_desc)
972 {
973 	const struct ethtool_ops *ops = dev->ethtool_ops;
974 	int err;
975 
976 	if (!ops->get_ts_info)
977 		return -EOPNOTSUPP;
978 
979 	/* Does ptp comes from netdev */
980 	ethtool_init_tsinfo(info);
981 	info->phc_qualifier = hwprov_desc->qualifier;
982 	err = ops->get_ts_info(dev, info);
983 	if (err)
984 		return err;
985 
986 	if (info->phc_index == hwprov_desc->index &&
987 	    net_support_hwtstamp_qualifier(dev, hwprov_desc->qualifier))
988 		return 0;
989 
990 	return -ENODEV;
991 }
992 
993 struct phy_device *
994 ethtool_phy_get_ts_info_by_phc(struct net_device *dev,
995 			       struct kernel_ethtool_ts_info *info,
996 			       struct hwtstamp_provider_desc *hwprov_desc)
997 {
998 	int err;
999 
1000 	/* Only precise qualifier is supported in phydev */
1001 	if (hwprov_desc->qualifier != HWTSTAMP_PROVIDER_QUALIFIER_PRECISE)
1002 		return ERR_PTR(-ENODEV);
1003 
1004 	/* Look in the phy topology */
1005 	if (dev->link_topo) {
1006 		struct phy_device_node *pdn;
1007 		unsigned long phy_index;
1008 
1009 		xa_for_each(&dev->link_topo->phys, phy_index, pdn) {
1010 			if (!phy_has_tsinfo(pdn->phy))
1011 				continue;
1012 
1013 			ethtool_init_tsinfo(info);
1014 			err = phy_ts_info(pdn->phy, info);
1015 			if (err)
1016 				return ERR_PTR(err);
1017 
1018 			if (info->phc_index == hwprov_desc->index)
1019 				return pdn->phy;
1020 		}
1021 		return ERR_PTR(-ENODEV);
1022 	}
1023 
1024 	/* Look on the dev->phydev */
1025 	if (phy_has_tsinfo(dev->phydev)) {
1026 		ethtool_init_tsinfo(info);
1027 		err = phy_ts_info(dev->phydev, info);
1028 		if (err)
1029 			return ERR_PTR(err);
1030 
1031 		if (info->phc_index == hwprov_desc->index)
1032 			return dev->phydev;
1033 	}
1034 
1035 	return ERR_PTR(-ENODEV);
1036 }
1037 
1038 int ethtool_get_ts_info_by_phc(struct net_device *dev,
1039 			       struct kernel_ethtool_ts_info *info,
1040 			       struct hwtstamp_provider_desc *hwprov_desc)
1041 {
1042 	int err;
1043 
1044 	err = ethtool_net_get_ts_info_by_phc(dev, info, hwprov_desc);
1045 	if (err == -ENODEV || err == -EOPNOTSUPP) {
1046 		struct phy_device *phy;
1047 
1048 		phy = ethtool_phy_get_ts_info_by_phc(dev, info, hwprov_desc);
1049 		if (IS_ERR(phy))
1050 			return PTR_ERR(phy);
1051 
1052 		/* Report the phc source only if we have a real
1053 		 * phc source with an index.
1054 		 */
1055 		if (info->phc_index >= 0) {
1056 			info->phc_source = HWTSTAMP_SOURCE_PHYLIB;
1057 			info->phc_phyindex = phy->phyindex;
1058 		}
1059 		err = 0;
1060 	} else if (!err && info->phc_index >= 0) {
1061 		info->phc_source = HWTSTAMP_SOURCE_NETDEV;
1062 	}
1063 
1064 	info->so_timestamping |= SOF_TIMESTAMPING_RX_SOFTWARE |
1065 				 SOF_TIMESTAMPING_SOFTWARE;
1066 
1067 	return err;
1068 }
1069 
1070 int __ethtool_get_ts_info(struct net_device *dev,
1071 			  struct kernel_ethtool_ts_info *info)
1072 {
1073 	struct hwtstamp_provider *hwprov;
1074 	int err = 0;
1075 
1076 	rcu_read_lock();
1077 	hwprov = rcu_dereference(dev->hwprov);
1078 	/* No provider specified, use default behavior */
1079 	if (!hwprov) {
1080 		const struct ethtool_ops *ops = dev->ethtool_ops;
1081 		struct phy_device *phydev = dev->phydev;
1082 
1083 		ethtool_init_tsinfo(info);
1084 		if (phy_is_default_hwtstamp(phydev) &&
1085 		    phy_has_tsinfo(phydev)) {
1086 			err = phy_ts_info(phydev, info);
1087 			/* Report the phc source only if we have a real
1088 			 * phc source with an index.
1089 			 */
1090 			if (!err && info->phc_index >= 0) {
1091 				info->phc_source = HWTSTAMP_SOURCE_PHYLIB;
1092 				info->phc_phyindex = phydev->phyindex;
1093 			}
1094 		} else if (ops->get_ts_info) {
1095 			err = ops->get_ts_info(dev, info);
1096 			if (!err && info->phc_index >= 0)
1097 				info->phc_source = HWTSTAMP_SOURCE_NETDEV;
1098 		}
1099 
1100 		info->so_timestamping |= SOF_TIMESTAMPING_RX_SOFTWARE |
1101 					 SOF_TIMESTAMPING_SOFTWARE;
1102 
1103 		rcu_read_unlock();
1104 		return err;
1105 	}
1106 
1107 	err = ethtool_get_ts_info_by_phc(dev, info, &hwprov->desc);
1108 	rcu_read_unlock();
1109 	return err;
1110 }
1111 
1112 bool net_support_hwtstamp_qualifier(struct net_device *dev,
1113 				    enum hwtstamp_provider_qualifier qualifier)
1114 {
1115 	const struct ethtool_ops *ops = dev->ethtool_ops;
1116 
1117 	if (!ops)
1118 		return false;
1119 
1120 	/* Return true with precise qualifier and with NIC without
1121 	 * qualifier description to not break the old behavior.
1122 	 */
1123 	if (!ops->supported_hwtstamp_qualifiers &&
1124 	    qualifier == HWTSTAMP_PROVIDER_QUALIFIER_PRECISE)
1125 		return true;
1126 
1127 	if (ops->supported_hwtstamp_qualifiers & BIT(qualifier))
1128 		return true;
1129 
1130 	return false;
1131 }
1132 
1133 int ethtool_get_phc_vclocks(struct net_device *dev, int **vclock_index)
1134 {
1135 	struct kernel_ethtool_ts_info info = { };
1136 	int num = 0;
1137 
1138 	if (!__ethtool_get_ts_info(dev, &info))
1139 		num = ptp_get_vclocks_index(info.phc_index, vclock_index);
1140 
1141 	return num;
1142 }
1143 EXPORT_SYMBOL(ethtool_get_phc_vclocks);
1144 
1145 int ethtool_get_ts_info_by_layer(struct net_device *dev, struct kernel_ethtool_ts_info *info)
1146 {
1147 	return __ethtool_get_ts_info(dev, info);
1148 }
1149 EXPORT_SYMBOL(ethtool_get_ts_info_by_layer);
1150 
1151 const struct ethtool_phy_ops *ethtool_phy_ops;
1152 
1153 void ethtool_set_ethtool_phy_ops(const struct ethtool_phy_ops *ops)
1154 {
1155 	ASSERT_RTNL();
1156 	ethtool_phy_ops = ops;
1157 }
1158 EXPORT_SYMBOL_GPL(ethtool_set_ethtool_phy_ops);
1159 
1160 void
1161 ethtool_params_from_link_mode(struct ethtool_link_ksettings *link_ksettings,
1162 			      enum ethtool_link_mode_bit_indices link_mode)
1163 {
1164 	const struct link_mode_info *link_info;
1165 
1166 	if (WARN_ON_ONCE(link_mode >= __ETHTOOL_LINK_MODE_MASK_NBITS))
1167 		return;
1168 
1169 	link_info = &link_mode_params[link_mode];
1170 	link_ksettings->base.speed = link_info->speed;
1171 	link_ksettings->lanes = link_info->lanes;
1172 	link_ksettings->base.duplex = link_info->duplex;
1173 }
1174 EXPORT_SYMBOL_GPL(ethtool_params_from_link_mode);
1175 
1176 /**
1177  * ethtool_forced_speed_maps_init
1178  * @maps: Pointer to an array of Ethtool forced speed map
1179  * @size: Array size
1180  *
1181  * Initialize an array of Ethtool forced speed map to Ethtool link modes. This
1182  * should be called during driver module init.
1183  */
1184 void
1185 ethtool_forced_speed_maps_init(struct ethtool_forced_speed_map *maps, u32 size)
1186 {
1187 	for (u32 i = 0; i < size; i++) {
1188 		struct ethtool_forced_speed_map *map = &maps[i];
1189 
1190 		linkmode_set_bit_array(map->cap_arr, map->arr_size, map->caps);
1191 		map->cap_arr = NULL;
1192 		map->arr_size = 0;
1193 	}
1194 }
1195 EXPORT_SYMBOL_GPL(ethtool_forced_speed_maps_init);
1196 
1197 void ethtool_rxfh_context_lost(struct net_device *dev, u32 context_id)
1198 {
1199 	struct ethtool_rxfh_context *ctx;
1200 
1201 	WARN_ONCE(!rtnl_is_locked() &&
1202 		  !lockdep_is_held_type(&dev->ethtool->rss_lock, -1),
1203 		  "RSS context lock assertion failed\n");
1204 
1205 	netdev_err(dev, "device error, RSS context %d lost\n", context_id);
1206 	ctx = xa_erase(&dev->ethtool->rss_ctx, context_id);
1207 	kfree(ctx);
1208 	ethtool_rss_notify(dev, ETHTOOL_MSG_RSS_DELETE_NTF, context_id);
1209 }
1210 EXPORT_SYMBOL(ethtool_rxfh_context_lost);
1211 
1212 bool netif_is_rxfh_configured(const struct net_device *dev)
1213 {
1214 	return dev->ethtool->rss_indir_user_size;
1215 }
1216 EXPORT_SYMBOL(netif_is_rxfh_configured);
1217 
1218 /**
1219  * ethtool_rxfh_indir_lost - Notify core that the RSS indirection table was lost
1220  * @dev: network device
1221  *
1222  * Drivers should call this when the device can no longer maintain the
1223  * user-configured indirection table, typically after a HW fault recovery
1224  * that reduced the maximum queue count. Marks the default RSS context
1225  * indirection table as unconfigured and sends an %ETHTOOL_MSG_RSS_NTF
1226  * notification.
1227  */
1228 void ethtool_rxfh_indir_lost(struct net_device *dev)
1229 {
1230 	WARN_ONCE(!rtnl_is_locked() &&
1231 		  !lockdep_is_held_type(&dev->ethtool->rss_lock, -1),
1232 		  "RSS context lock assertion failed\n");
1233 
1234 	netdev_err(dev, "device error, RSS indirection table lost\n");
1235 	dev->ethtool->rss_indir_user_size = 0;
1236 	ethtool_rss_notify(dev, ETHTOOL_MSG_RSS_NTF, 0);
1237 }
1238 EXPORT_SYMBOL(ethtool_rxfh_indir_lost);
1239 
1240 static bool ethtool_rxfh_is_periodic(const u32 *tbl, u32 old_size, u32 new_size)
1241 {
1242 	u32 i;
1243 
1244 	for (i = new_size; i < old_size; i++)
1245 		if (tbl[i] != tbl[i % new_size])
1246 			return false;
1247 	return true;
1248 }
1249 
1250 static bool ethtool_rxfh_can_resize(const u32 *tbl, u32 old_size, u32 new_size,
1251 				    u32 user_size)
1252 {
1253 	if (new_size == old_size)
1254 		return true;
1255 
1256 	if (!user_size)
1257 		return true;
1258 
1259 	if (new_size < old_size) {
1260 		if (new_size < user_size)
1261 			return false;
1262 		if (old_size % new_size)
1263 			return false;
1264 		if (!ethtool_rxfh_is_periodic(tbl, old_size, new_size))
1265 			return false;
1266 		return true;
1267 	}
1268 
1269 	if (new_size % old_size)
1270 		return false;
1271 	return true;
1272 }
1273 
1274 /* Resize without validation; caller must have called can_resize first */
1275 static void ethtool_rxfh_resize(u32 *tbl, u32 old_size, u32 new_size)
1276 {
1277 	u32 i;
1278 
1279 	/* Grow: replicate existing pattern; shrink is a no-op on the data */
1280 	for (i = old_size; i < new_size; i++)
1281 		tbl[i] = tbl[i % old_size];
1282 }
1283 
1284 /**
1285  * ethtool_rxfh_indir_can_resize - Check if context 0 indir table can resize
1286  * @dev: network device
1287  * @tbl: indirection table
1288  * @old_size: current number of entries in the table
1289  * @new_size: desired number of entries
1290  *
1291  * Validate that @tbl can be resized from @old_size to @new_size without
1292  * data loss. Uses the user_size floor from context 0. When user_size is
1293  * zero the table is not user-configured and resize always succeeds.
1294  * Read-only; does not modify the table.
1295  *
1296  * Return: true if resize is possible, false otherwise.
1297  */
1298 bool ethtool_rxfh_indir_can_resize(struct net_device *dev, const u32 *tbl,
1299 				   u32 old_size, u32 new_size)
1300 {
1301 	return ethtool_rxfh_can_resize(tbl, old_size, new_size,
1302 				       dev->ethtool->rss_indir_user_size);
1303 }
1304 EXPORT_SYMBOL(ethtool_rxfh_indir_can_resize);
1305 
1306 /**
1307  * ethtool_rxfh_indir_resize - Fold or unfold context 0 indirection table
1308  * @dev: network device
1309  * @tbl: indirection table (must have room for max(old_size, new_size) entries)
1310  * @old_size: current number of entries in the table
1311  * @new_size: desired number of entries
1312  *
1313  * Resize the default RSS context indirection table in place. Caller
1314  * must have validated with ethtool_rxfh_indir_can_resize() first.
1315  */
1316 void ethtool_rxfh_indir_resize(struct net_device *dev, u32 *tbl,
1317 			       u32 old_size, u32 new_size)
1318 {
1319 	if (!dev->ethtool->rss_indir_user_size)
1320 		return;
1321 
1322 	ethtool_rxfh_resize(tbl, old_size, new_size);
1323 }
1324 EXPORT_SYMBOL(ethtool_rxfh_indir_resize);
1325 
1326 /**
1327  * ethtool_rxfh_ctxs_can_resize - Validate resize for all RSS contexts
1328  * @dev: network device
1329  * @new_indir_size: new indirection table size
1330  *
1331  * Validate that the indirection tables of all non-default RSS contexts
1332  * can be resized to @new_indir_size. Read-only; does not modify any
1333  * context. Intended to be paired with ethtool_rxfh_ctxs_resize().
1334  *
1335  * Return: 0 if all contexts can be resized, negative errno on failure.
1336  */
1337 int ethtool_rxfh_ctxs_can_resize(struct net_device *dev, u32 new_indir_size)
1338 {
1339 	struct ethtool_rxfh_context *ctx;
1340 	unsigned long context;
1341 	int ret = 0;
1342 
1343 	if (!dev->ethtool_ops->rxfh_indir_space ||
1344 	    new_indir_size > dev->ethtool_ops->rxfh_indir_space)
1345 		return -EINVAL;
1346 
1347 	mutex_lock(&dev->ethtool->rss_lock);
1348 	xa_for_each(&dev->ethtool->rss_ctx, context, ctx) {
1349 		u32 *indir = ethtool_rxfh_context_indir(ctx);
1350 
1351 		if (!ethtool_rxfh_can_resize(indir, ctx->indir_size,
1352 					     new_indir_size,
1353 					     ctx->indir_user_size)) {
1354 			ret = -EINVAL;
1355 			goto unlock;
1356 		}
1357 	}
1358 unlock:
1359 	mutex_unlock(&dev->ethtool->rss_lock);
1360 	return ret;
1361 }
1362 EXPORT_SYMBOL(ethtool_rxfh_ctxs_can_resize);
1363 
1364 /**
1365  * ethtool_rxfh_ctxs_resize - Resize all RSS context indirection tables
1366  * @dev: network device
1367  * @new_indir_size: new indirection table size
1368  *
1369  * Resize the indirection table of every non-default RSS context to
1370  * @new_indir_size. Caller must have validated with
1371  * ethtool_rxfh_ctxs_can_resize() first. An %ETHTOOL_MSG_RSS_NTF is
1372  * sent for each resized context.
1373  *
1374  * Notifications are sent outside the RSS lock to avoid holding the
1375  * mutex during notification delivery.
1376  */
1377 void ethtool_rxfh_ctxs_resize(struct net_device *dev, u32 new_indir_size)
1378 {
1379 	struct ethtool_rxfh_context *ctx;
1380 	unsigned long context;
1381 
1382 	mutex_lock(&dev->ethtool->rss_lock);
1383 	xa_for_each(&dev->ethtool->rss_ctx, context, ctx) {
1384 		ethtool_rxfh_resize(ethtool_rxfh_context_indir(ctx),
1385 				    ctx->indir_size, new_indir_size);
1386 		ctx->indir_size = new_indir_size;
1387 	}
1388 	mutex_unlock(&dev->ethtool->rss_lock);
1389 
1390 	xa_for_each(&dev->ethtool->rss_ctx, context, ctx)
1391 		ethtool_rss_notify(dev, ETHTOOL_MSG_RSS_NTF, context);
1392 }
1393 EXPORT_SYMBOL(ethtool_rxfh_ctxs_resize);
1394 
1395 enum ethtool_link_medium ethtool_str_to_medium(const char *str)
1396 {
1397 	int i;
1398 
1399 	for (i = 0; i < __ETHTOOL_LINK_MEDIUM_LAST; i++)
1400 		if (!strcmp(ethtool_link_medium_names[i], str))
1401 			return i;
1402 
1403 	return ETHTOOL_LINK_MEDIUM_NONE;
1404 }
1405 EXPORT_SYMBOL_GPL(ethtool_str_to_medium);
1406 
1407 u8 netdev_rss_key[NETDEV_RSS_KEY_LEN] __read_mostly;
1408 bool netdev_rss_key_initialized __read_mostly;
1409 
1410 /* Toeplitz is linear over GF(2): the hash is the XOR of the 32-bit key
1411  * windows selected by the set bits of the input, and hardware indexes the
1412  * indirection table with the low order bits of the hash. Only the tail of
1413  * each window therefore matters for queue selection:
1414  *
1415  *	v(i) = key bits [i + 32 - q .. i + 31]
1416  *
1417  * for input bit @i, with q = log2(number of RX queues). Consecutive input
1418  * bits give windows overlapping in q - 1 positions, so the matrix formed by
1419  * the windows of the q lowest bits of a header field is a Toeplitz matrix
1420  * built from 2 * q - 1 key bits, not from q * q independent ones. A random
1421  * Toeplitz matrix over GF(2) is singular with probability 1/2, whatever its
1422  * size, and when it is singular the flows of a burst differing only in the
1423  * low order bits of that field (consecutive ephemeral ports, typically)
1424  * cannot reach all the RX queues no matter how many of them are configured.
1425  *
1426  * Keep the key random, but constrain the few bits that decide this. The
1427  * fixup is applied at every 16-bit aligned position of the key, rather than
1428  * at the offsets of the one hash input layout the software happens to know
1429  * about: hardware is free to hash whatever it wants, but the fields it picks
1430  * are 16 bits wide at the smallest and are not expected to straddle that
1431  * grid, so an encapsulated or offloaded layout is covered like the usual
1432  * 2-tuple and 4-tuple ones. Each position constrains 2 * q - 1 bits, so
1433  * NETDEV_RSS_KEY_QMAX is both enough for 256 queues and the largest value
1434  * keeping the ranges of two adjacent positions disjoint.
1435  */
1436 #define NETDEV_RSS_KEY_QMAX	8
1437 #define NETDEV_RSS_KEY_SPAN	(2 * NETDEV_RSS_KEY_QMAX - 1)
1438 
1439 static bool netdev_rss_key_bit(const u8 *key, unsigned int bit)
1440 {
1441 	return key[bit / BITS_PER_BYTE] & (0x80 >> (bit % BITS_PER_BYTE));
1442 }
1443 
1444 static void netdev_rss_key_assign_bit(u8 *key, unsigned int bit, bool value)
1445 {
1446 	u8 mask = 0x80 >> (bit % BITS_PER_BYTE);
1447 
1448 	if (value)
1449 		key[bit / BITS_PER_BYTE] |= mask;
1450 	else
1451 		key[bit / BITS_PER_BYTE] &= ~mask;
1452 }
1453 
1454 /* Writing d[t] for key bit (@lsb + 31 - t), the matrices of all the q values
1455  * up to NETDEV_RSS_KEY_QMAX are non singular if and only if
1456  *
1457  *	d[2 * i] = 1 ^ d[i] ^ d[i + 1] ^ ... ^ d[2 * i - 1]
1458  *
1459  * The odd positions stay free, so this is a one pass fixup rather than a
1460  * search. It is also a bijection onto the set of the values having the
1461  * property, so the key stays uniformly distributed over that set. It costs
1462  * NETDEV_RSS_KEY_QMAX bits of entropy per position.
1463  */
1464 static void netdev_rss_key_fixup_field(u8 *key, unsigned int lsb)
1465 {
1466 	bool d[NETDEV_RSS_KEY_SPAN];
1467 	unsigned int i, t;
1468 
1469 	for (t = 0; t < NETDEV_RSS_KEY_SPAN; t++)
1470 		d[t] = netdev_rss_key_bit(key, lsb + 31 - t);
1471 
1472 	for (i = 0; 2 * i < NETDEV_RSS_KEY_SPAN; i++) {
1473 		bool value = true;
1474 
1475 		for (t = i; t < 2 * i; t++)
1476 			value ^= d[t];
1477 
1478 		d[2 * i] = value;
1479 	}
1480 
1481 	for (t = 0; t < NETDEV_RSS_KEY_SPAN; t++)
1482 		netdev_rss_key_assign_bit(key, lsb + 31 - t, d[t]);
1483 }
1484 
1485 /* The 32 key bits starting at @bit, which is what input bit @bit contributes
1486  * to the hash. @bit + 32 must fit in the key.
1487  */
1488 static u32 netdev_rss_key_window(const u8 *key, unsigned int bit)
1489 {
1490 	unsigned int byte = bit / BITS_PER_BYTE;
1491 	unsigned int shift = bit % BITS_PER_BYTE;
1492 	u32 window = get_unaligned_be32(key + byte);
1493 
1494 	if (shift)
1495 		window = (window << shift) |
1496 			 (key[byte + 4] >> (BITS_PER_BYTE - shift));
1497 
1498 	return window;
1499 }
1500 
1501 /* Two input bits contributing the same window are indistinguishable to the
1502  * hash, since flipping both of them leaves it unchanged. For a uniformly
1503  * random key that is a 2 ** -32 event per pair of positions, but the fixup
1504  * makes it likelier: it derives 8 of every 16 bits from the 8 others, so a
1505  * 16-bit aligned word only takes 2 ** 8 values and two windows a whole
1506  * number of words apart collide with probability 2 ** -16 instead. Half a
1507  * word apart is less affected but still well clear of the random odds, so
1508  * cover every distance that is a multiple of 8. What is left after that is
1509  * below what a plain random key gives.
1510  *
1511  * Two windows at a distance that is a multiple of 8 are two windows at the
1512  * same offset modulo 8. Caching a whole class would put 253 u32 on the
1513  * stack, so cache one class modulo 16 and stream the class 8 bits above it
1514  * against it.
1515  */
1516 static bool netdev_rss_key_aliases(const u8 *key, unsigned int bits)
1517 {
1518 	u32 windows[NETDEV_RSS_KEY_LEN * BITS_PER_BYTE / 16];
1519 	unsigned int i, j, n, r;
1520 
1521 	for (r = 0; r < 16; r++) {
1522 		n = 0;
1523 		for (i = r; i + 32 <= bits; i += 16)
1524 			windows[n++] = netdev_rss_key_window(key, i);
1525 
1526 		for (i = 0; i < n; i++)
1527 			for (j = i + 1; j < n; j++)
1528 				if (windows[i] == windows[j])
1529 					return true;
1530 
1531 		if (r >= 8)
1532 			continue;
1533 
1534 		for (i = r + 8; i + 32 <= bits; i += 16) {
1535 			u32 window = netdev_rss_key_window(key, i);
1536 
1537 			for (j = 0; j < n; j++)
1538 				if (windows[j] == window)
1539 					return true;
1540 		}
1541 	}
1542 
1543 	return false;
1544 }
1545 
1546 static void netdev_rss_key_init(u8 *key, size_t len)
1547 {
1548 	unsigned int lsb, bits = len * BITS_PER_BYTE;
1549 
1550 	/* Four keys out of five come out of the fixup free of aliases, so
1551 	 * drawing another one is both simpler and cheaper than repairing.
1552 	 */
1553 	do {
1554 		get_random_bytes(key, len);
1555 
1556 		/* A field ending at bit @lsb uses key bits [.. , @lsb + 31],
1557 		 * so stop as soon as a 32-bit window no longer fits in the
1558 		 * key.
1559 		 */
1560 		for (lsb = 15; lsb + 32 <= bits; lsb += 16)
1561 			netdev_rss_key_fixup_field(key, lsb);
1562 	} while (netdev_rss_key_aliases(key, bits));
1563 
1564 	/* Pair with smp_rmb() in proc_do_rss_key(). */
1565 	smp_wmb();
1566 	WRITE_ONCE(netdev_rss_key_initialized, true);
1567 }
1568 
1569 void netdev_rss_key_fill(void *buffer, size_t len)
1570 {
1571 	if (WARN_ON_ONCE(len > sizeof(netdev_rss_key)))
1572 		len = sizeof(netdev_rss_key);
1573 	DO_ONCE(netdev_rss_key_init, netdev_rss_key, sizeof(netdev_rss_key));
1574 	memcpy(buffer, netdev_rss_key, len);
1575 }
1576 EXPORT_SYMBOL(netdev_rss_key_fill);
1577