1 // SPDX-License-Identifier: (GPL-2.0-only OR BSD-2-Clause)
2 /* Copyright (C) 2015-2018 Netronome Systems, Inc. */
3 
4 /*
5  * nfp_net_ethtool.c
6  * Netronome network device driver: ethtool support
7  * Authors: Jakub Kicinski <jakub.kicinski@netronome.com>
8  *          Jason McMullan <jason.mcmullan@netronome.com>
9  *          Rolf Neugebauer <rolf.neugebauer@netronome.com>
10  *          Brad Petrus <brad.petrus@netronome.com>
11  */
12 
13 #include <linux/bitfield.h>
14 #include <linux/kernel.h>
15 #include <linux/netdevice.h>
16 #include <linux/etherdevice.h>
17 #include <linux/interrupt.h>
18 #include <linux/pci.h>
19 #include <linux/ethtool.h>
20 #include <linux/firmware.h>
21 #include <linux/sfp.h>
22 
23 #include "nfpcore/nfp.h"
24 #include "nfpcore/nfp_dev.h"
25 #include "nfpcore/nfp_nsp.h"
26 #include "nfp_app.h"
27 #include "nfp_main.h"
28 #include "nfp_net_ctrl.h"
29 #include "nfp_net_dp.h"
30 #include "nfp_net.h"
31 #include "nfp_port.h"
32 #include "nfpcore/nfp_cpp.h"
33 
34 struct nfp_et_stat {
35 	char name[ETH_GSTRING_LEN];
36 	int off;
37 };
38 
39 static const struct nfp_et_stat nfp_net_et_stats[] = {
40 	/* Stats from the device */
41 	{ "dev_rx_discards",	NFP_NET_CFG_STATS_RX_DISCARDS },
42 	{ "dev_rx_errors",	NFP_NET_CFG_STATS_RX_ERRORS },
43 	{ "dev_rx_bytes",	NFP_NET_CFG_STATS_RX_OCTETS },
44 	{ "dev_rx_uc_bytes",	NFP_NET_CFG_STATS_RX_UC_OCTETS },
45 	{ "dev_rx_mc_bytes",	NFP_NET_CFG_STATS_RX_MC_OCTETS },
46 	{ "dev_rx_bc_bytes",	NFP_NET_CFG_STATS_RX_BC_OCTETS },
47 	{ "dev_rx_pkts",	NFP_NET_CFG_STATS_RX_FRAMES },
48 	{ "dev_rx_mc_pkts",	NFP_NET_CFG_STATS_RX_MC_FRAMES },
49 	{ "dev_rx_bc_pkts",	NFP_NET_CFG_STATS_RX_BC_FRAMES },
50 
51 	{ "dev_tx_discards",	NFP_NET_CFG_STATS_TX_DISCARDS },
52 	{ "dev_tx_errors",	NFP_NET_CFG_STATS_TX_ERRORS },
53 	{ "dev_tx_bytes",	NFP_NET_CFG_STATS_TX_OCTETS },
54 	{ "dev_tx_uc_bytes",	NFP_NET_CFG_STATS_TX_UC_OCTETS },
55 	{ "dev_tx_mc_bytes",	NFP_NET_CFG_STATS_TX_MC_OCTETS },
56 	{ "dev_tx_bc_bytes",	NFP_NET_CFG_STATS_TX_BC_OCTETS },
57 	{ "dev_tx_pkts",	NFP_NET_CFG_STATS_TX_FRAMES },
58 	{ "dev_tx_mc_pkts",	NFP_NET_CFG_STATS_TX_MC_FRAMES },
59 	{ "dev_tx_bc_pkts",	NFP_NET_CFG_STATS_TX_BC_FRAMES },
60 
61 	{ "bpf_pass_pkts",	NFP_NET_CFG_STATS_APP0_FRAMES },
62 	{ "bpf_pass_bytes",	NFP_NET_CFG_STATS_APP0_BYTES },
63 	/* see comments in outro functions in nfp_bpf_jit.c to find out
64 	 * how different BPF modes use app-specific counters
65 	 */
66 	{ "bpf_app1_pkts",	NFP_NET_CFG_STATS_APP1_FRAMES },
67 	{ "bpf_app1_bytes",	NFP_NET_CFG_STATS_APP1_BYTES },
68 	{ "bpf_app2_pkts",	NFP_NET_CFG_STATS_APP2_FRAMES },
69 	{ "bpf_app2_bytes",	NFP_NET_CFG_STATS_APP2_BYTES },
70 	{ "bpf_app3_pkts",	NFP_NET_CFG_STATS_APP3_FRAMES },
71 	{ "bpf_app3_bytes",	NFP_NET_CFG_STATS_APP3_BYTES },
72 };
73 
74 static const struct nfp_et_stat nfp_mac_et_stats[] = {
75 	{ "rx_octets",			NFP_MAC_STATS_RX_IN_OCTETS, },
76 	{ "rx_frame_too_long_errors",
77 			NFP_MAC_STATS_RX_FRAME_TOO_LONG_ERRORS, },
78 	{ "rx_range_length_errors",	NFP_MAC_STATS_RX_RANGE_LENGTH_ERRORS, },
79 	{ "rx_vlan_received_ok",	NFP_MAC_STATS_RX_VLAN_RECEIVED_OK, },
80 	{ "rx_errors",			NFP_MAC_STATS_RX_IN_ERRORS, },
81 	{ "rx_broadcast_pkts",		NFP_MAC_STATS_RX_IN_BROADCAST_PKTS, },
82 	{ "rx_drop_events",		NFP_MAC_STATS_RX_DROP_EVENTS, },
83 	{ "rx_alignment_errors",	NFP_MAC_STATS_RX_ALIGNMENT_ERRORS, },
84 	{ "rx_pause_mac_ctrl_frames",
85 			NFP_MAC_STATS_RX_PAUSE_MAC_CTRL_FRAMES, },
86 	{ "rx_frames_received_ok",	NFP_MAC_STATS_RX_FRAMES_RECEIVED_OK, },
87 	{ "rx_frame_check_sequence_errors",
88 			NFP_MAC_STATS_RX_FRAME_CHECK_SEQUENCE_ERRORS, },
89 	{ "rx_unicast_pkts",		NFP_MAC_STATS_RX_UNICAST_PKTS, },
90 	{ "rx_multicast_pkts",		NFP_MAC_STATS_RX_MULTICAST_PKTS, },
91 	{ "rx_pkts",			NFP_MAC_STATS_RX_PKTS, },
92 	{ "rx_undersize_pkts",		NFP_MAC_STATS_RX_UNDERSIZE_PKTS, },
93 	{ "rx_pkts_64_octets",		NFP_MAC_STATS_RX_PKTS_64_OCTETS, },
94 	{ "rx_pkts_65_to_127_octets",
95 			NFP_MAC_STATS_RX_PKTS_65_TO_127_OCTETS, },
96 	{ "rx_pkts_128_to_255_octets",
97 			NFP_MAC_STATS_RX_PKTS_128_TO_255_OCTETS, },
98 	{ "rx_pkts_256_to_511_octets",
99 			NFP_MAC_STATS_RX_PKTS_256_TO_511_OCTETS, },
100 	{ "rx_pkts_512_to_1023_octets",
101 			NFP_MAC_STATS_RX_PKTS_512_TO_1023_OCTETS, },
102 	{ "rx_pkts_1024_to_1518_octets",
103 			NFP_MAC_STATS_RX_PKTS_1024_TO_1518_OCTETS, },
104 	{ "rx_pkts_1519_to_max_octets",
105 			NFP_MAC_STATS_RX_PKTS_1519_TO_MAX_OCTETS, },
106 	{ "rx_jabbers",			NFP_MAC_STATS_RX_JABBERS, },
107 	{ "rx_fragments",		NFP_MAC_STATS_RX_FRAGMENTS, },
108 	{ "rx_oversize_pkts",		NFP_MAC_STATS_RX_OVERSIZE_PKTS, },
109 	{ "rx_pause_frames_class0",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS0, },
110 	{ "rx_pause_frames_class1",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS1, },
111 	{ "rx_pause_frames_class2",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS2, },
112 	{ "rx_pause_frames_class3",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS3, },
113 	{ "rx_pause_frames_class4",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS4, },
114 	{ "rx_pause_frames_class5",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS5, },
115 	{ "rx_pause_frames_class6",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS6, },
116 	{ "rx_pause_frames_class7",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS7, },
117 	{ "rx_mac_ctrl_frames_received",
118 			NFP_MAC_STATS_RX_MAC_CTRL_FRAMES_RECEIVED, },
119 	{ "rx_mac_head_drop",		NFP_MAC_STATS_RX_MAC_HEAD_DROP, },
120 	{ "tx_queue_drop",		NFP_MAC_STATS_TX_QUEUE_DROP, },
121 	{ "tx_octets",			NFP_MAC_STATS_TX_OUT_OCTETS, },
122 	{ "tx_vlan_transmitted_ok",	NFP_MAC_STATS_TX_VLAN_TRANSMITTED_OK, },
123 	{ "tx_errors",			NFP_MAC_STATS_TX_OUT_ERRORS, },
124 	{ "tx_broadcast_pkts",		NFP_MAC_STATS_TX_BROADCAST_PKTS, },
125 	{ "tx_pause_mac_ctrl_frames",
126 			NFP_MAC_STATS_TX_PAUSE_MAC_CTRL_FRAMES, },
127 	{ "tx_frames_transmitted_ok",
128 			NFP_MAC_STATS_TX_FRAMES_TRANSMITTED_OK, },
129 	{ "tx_unicast_pkts",		NFP_MAC_STATS_TX_UNICAST_PKTS, },
130 	{ "tx_multicast_pkts",		NFP_MAC_STATS_TX_MULTICAST_PKTS, },
131 	{ "tx_pkts_64_octets",		NFP_MAC_STATS_TX_PKTS_64_OCTETS, },
132 	{ "tx_pkts_65_to_127_octets",
133 			NFP_MAC_STATS_TX_PKTS_65_TO_127_OCTETS, },
134 	{ "tx_pkts_128_to_255_octets",
135 			NFP_MAC_STATS_TX_PKTS_128_TO_255_OCTETS, },
136 	{ "tx_pkts_256_to_511_octets",
137 			NFP_MAC_STATS_TX_PKTS_256_TO_511_OCTETS, },
138 	{ "tx_pkts_512_to_1023_octets",
139 			NFP_MAC_STATS_TX_PKTS_512_TO_1023_OCTETS, },
140 	{ "tx_pkts_1024_to_1518_octets",
141 			NFP_MAC_STATS_TX_PKTS_1024_TO_1518_OCTETS, },
142 	{ "tx_pkts_1519_to_max_octets",
143 			NFP_MAC_STATS_TX_PKTS_1519_TO_MAX_OCTETS, },
144 	{ "tx_pause_frames_class0",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS0, },
145 	{ "tx_pause_frames_class1",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS1, },
146 	{ "tx_pause_frames_class2",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS2, },
147 	{ "tx_pause_frames_class3",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS3, },
148 	{ "tx_pause_frames_class4",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS4, },
149 	{ "tx_pause_frames_class5",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS5, },
150 	{ "tx_pause_frames_class6",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS6, },
151 	{ "tx_pause_frames_class7",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS7, },
152 };
153 
154 static const char nfp_tlv_stat_names[][ETH_GSTRING_LEN] = {
155 	[1]	= "dev_rx_discards",
156 	[2]	= "dev_rx_errors",
157 	[3]	= "dev_rx_bytes",
158 	[4]	= "dev_rx_uc_bytes",
159 	[5]	= "dev_rx_mc_bytes",
160 	[6]	= "dev_rx_bc_bytes",
161 	[7]	= "dev_rx_pkts",
162 	[8]	= "dev_rx_mc_pkts",
163 	[9]	= "dev_rx_bc_pkts",
164 
165 	[10]	= "dev_tx_discards",
166 	[11]	= "dev_tx_errors",
167 	[12]	= "dev_tx_bytes",
168 	[13]	= "dev_tx_uc_bytes",
169 	[14]	= "dev_tx_mc_bytes",
170 	[15]	= "dev_tx_bc_bytes",
171 	[16]	= "dev_tx_pkts",
172 	[17]	= "dev_tx_mc_pkts",
173 	[18]	= "dev_tx_bc_pkts",
174 };
175 
176 #define NN_ET_GLOBAL_STATS_LEN ARRAY_SIZE(nfp_net_et_stats)
177 #define NN_ET_SWITCH_STATS_LEN 9
178 #define NN_RVEC_GATHER_STATS	13
179 #define NN_RVEC_PER_Q_STATS	3
180 #define NN_CTRL_PATH_STATS	4
181 
182 #define SFP_SFF_REV_COMPLIANCE	1
183 
184 static void nfp_net_get_nspinfo(struct nfp_app *app, char *version)
185 {
186 	struct nfp_nsp *nsp;
187 
188 	if (!app)
189 		return;
190 
191 	nsp = nfp_nsp_open(app->cpp);
192 	if (IS_ERR(nsp))
193 		return;
194 
195 	snprintf(version, ETHTOOL_FWVERS_LEN, "%hu.%hu",
196 		 nfp_nsp_get_abi_ver_major(nsp),
197 		 nfp_nsp_get_abi_ver_minor(nsp));
198 
199 	nfp_nsp_close(nsp);
200 }
201 
202 static void
203 nfp_get_drvinfo(struct nfp_app *app, struct pci_dev *pdev,
204 		const char *vnic_version, struct ethtool_drvinfo *drvinfo)
205 {
206 	char nsp_version[ETHTOOL_FWVERS_LEN] = {};
207 
208 	strscpy(drvinfo->driver, dev_driver_string(&pdev->dev),
209 		sizeof(drvinfo->driver));
210 	nfp_net_get_nspinfo(app, nsp_version);
211 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
212 		 "%s %s %s %s", vnic_version, nsp_version,
213 		 nfp_app_mip_name(app), nfp_app_name(app));
214 }
215 
216 static void
217 nfp_net_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
218 {
219 	char vnic_version[ETHTOOL_FWVERS_LEN] = {};
220 	struct nfp_net *nn = netdev_priv(netdev);
221 
222 	snprintf(vnic_version, sizeof(vnic_version), "%d.%d.%d.%d",
223 		 nn->fw_ver.extend, nn->fw_ver.class,
224 		 nn->fw_ver.major, nn->fw_ver.minor);
225 	strscpy(drvinfo->bus_info, pci_name(nn->pdev),
226 		sizeof(drvinfo->bus_info));
227 
228 	nfp_get_drvinfo(nn->app, nn->pdev, vnic_version, drvinfo);
229 }
230 
231 static int
232 nfp_net_nway_reset(struct net_device *netdev)
233 {
234 	struct nfp_eth_table_port *eth_port;
235 	struct nfp_port *port;
236 	int err;
237 
238 	port = nfp_port_from_netdev(netdev);
239 	eth_port = nfp_port_get_eth_port(port);
240 	if (!eth_port)
241 		return -EOPNOTSUPP;
242 
243 	if (!netif_running(netdev))
244 		return 0;
245 
246 	err = nfp_eth_set_configured(port->app->cpp, eth_port->index, false);
247 	if (err) {
248 		netdev_info(netdev, "Link down failed: %d\n", err);
249 		return err;
250 	}
251 
252 	err = nfp_eth_set_configured(port->app->cpp, eth_port->index, true);
253 	if (err) {
254 		netdev_info(netdev, "Link up failed: %d\n", err);
255 		return err;
256 	}
257 
258 	netdev_info(netdev, "Link reset succeeded\n");
259 	return 0;
260 }
261 
262 static void
263 nfp_app_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
264 {
265 	struct nfp_app *app = nfp_app_from_netdev(netdev);
266 
267 	strscpy(drvinfo->bus_info, pci_name(app->pdev),
268 		sizeof(drvinfo->bus_info));
269 	nfp_get_drvinfo(app, app->pdev, "*", drvinfo);
270 }
271 
272 static void
273 nfp_net_set_fec_link_mode(struct nfp_eth_table_port *eth_port,
274 			  struct ethtool_link_ksettings *c)
275 {
276 	unsigned int modes;
277 
278 	ethtool_link_ksettings_add_link_mode(c, supported, FEC_NONE);
279 	if (!nfp_eth_can_support_fec(eth_port)) {
280 		ethtool_link_ksettings_add_link_mode(c, advertising, FEC_NONE);
281 		return;
282 	}
283 
284 	modes = nfp_eth_supported_fec_modes(eth_port);
285 	if (modes & NFP_FEC_BASER) {
286 		ethtool_link_ksettings_add_link_mode(c, supported, FEC_BASER);
287 		ethtool_link_ksettings_add_link_mode(c, advertising, FEC_BASER);
288 	}
289 
290 	if (modes & NFP_FEC_REED_SOLOMON) {
291 		ethtool_link_ksettings_add_link_mode(c, supported, FEC_RS);
292 		ethtool_link_ksettings_add_link_mode(c, advertising, FEC_RS);
293 	}
294 }
295 
296 static const struct nfp_eth_media_link_mode {
297 	u16 ethtool_link_mode;
298 	u16 speed;
299 } nfp_eth_media_table[NFP_MEDIA_LINK_MODES_NUMBER] = {
300 	[NFP_MEDIA_1000BASE_CX] = {
301 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
302 		.speed			= NFP_SPEED_1G,
303 	},
304 	[NFP_MEDIA_1000BASE_KX] = {
305 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
306 		.speed			= NFP_SPEED_1G,
307 	},
308 	[NFP_MEDIA_10GBASE_KX4] = {
309 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
310 		.speed			= NFP_SPEED_10G,
311 	},
312 	[NFP_MEDIA_10GBASE_KR] = {
313 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
314 		.speed			= NFP_SPEED_10G,
315 	},
316 	[NFP_MEDIA_10GBASE_LR] = {
317 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_10000baseLR_Full_BIT,
318 		.speed			= NFP_SPEED_10G,
319 	},
320 	[NFP_MEDIA_10GBASE_CX4] = {
321 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
322 		.speed			= NFP_SPEED_10G,
323 	},
324 	[NFP_MEDIA_10GBASE_CR] = {
325 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_10000baseCR_Full_BIT,
326 		.speed			= NFP_SPEED_10G,
327 	},
328 	[NFP_MEDIA_10GBASE_SR] = {
329 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_10000baseSR_Full_BIT,
330 		.speed			= NFP_SPEED_10G,
331 	},
332 	[NFP_MEDIA_10GBASE_ER] = {
333 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_10000baseER_Full_BIT,
334 		.speed			= NFP_SPEED_10G,
335 	},
336 	[NFP_MEDIA_25GBASE_KR] = {
337 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
338 		.speed			= NFP_SPEED_25G,
339 	},
340 	[NFP_MEDIA_25GBASE_KR_S] = {
341 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
342 		.speed			= NFP_SPEED_25G,
343 	},
344 	[NFP_MEDIA_25GBASE_CR] = {
345 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
346 		.speed			= NFP_SPEED_25G,
347 	},
348 	[NFP_MEDIA_25GBASE_CR_S] = {
349 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
350 		.speed			= NFP_SPEED_25G,
351 	},
352 	[NFP_MEDIA_25GBASE_SR] = {
353 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
354 		.speed			= NFP_SPEED_25G,
355 	},
356 	[NFP_MEDIA_25GBASE_LR] = {
357 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
358 		.speed			= NFP_SPEED_25G,
359 	},
360 	[NFP_MEDIA_25GBASE_ER] = {
361 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
362 		.speed			= NFP_SPEED_25G,
363 	},
364 	[NFP_MEDIA_40GBASE_CR4] = {
365 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
366 		.speed			= NFP_SPEED_40G,
367 	},
368 	[NFP_MEDIA_40GBASE_KR4] = {
369 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
370 		.speed			= NFP_SPEED_40G,
371 	},
372 	[NFP_MEDIA_40GBASE_SR4] = {
373 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
374 		.speed			= NFP_SPEED_40G,
375 	},
376 	[NFP_MEDIA_40GBASE_LR4] = {
377 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
378 		.speed			= NFP_SPEED_40G,
379 	},
380 	[NFP_MEDIA_50GBASE_KR] = {
381 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_50000baseKR_Full_BIT,
382 		.speed			= NFP_SPEED_50G,
383 	},
384 	[NFP_MEDIA_50GBASE_SR] = {
385 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_50000baseSR_Full_BIT,
386 		.speed			= NFP_SPEED_50G,
387 	},
388 	[NFP_MEDIA_50GBASE_CR] = {
389 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_50000baseCR_Full_BIT,
390 		.speed			= NFP_SPEED_50G,
391 	},
392 	[NFP_MEDIA_50GBASE_LR] = {
393 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
394 		.speed			= NFP_SPEED_50G,
395 	},
396 	[NFP_MEDIA_50GBASE_ER] = {
397 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
398 		.speed			= NFP_SPEED_50G,
399 	},
400 	[NFP_MEDIA_50GBASE_FR] = {
401 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
402 		.speed			= NFP_SPEED_50G,
403 	},
404 	[NFP_MEDIA_100GBASE_KR4] = {
405 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
406 		.speed			= NFP_SPEED_100G,
407 	},
408 	[NFP_MEDIA_100GBASE_SR4] = {
409 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
410 		.speed			= NFP_SPEED_100G,
411 	},
412 	[NFP_MEDIA_100GBASE_CR4] = {
413 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
414 		.speed			= NFP_SPEED_100G,
415 	},
416 	[NFP_MEDIA_100GBASE_KP4] = {
417 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
418 		.speed			= NFP_SPEED_100G,
419 	},
420 	[NFP_MEDIA_100GBASE_CR10] = {
421 		.ethtool_link_mode	= ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
422 		.speed			= NFP_SPEED_100G,
423 	},
424 };
425 
426 static const unsigned int nfp_eth_speed_map[NFP_SUP_SPEED_NUMBER] = {
427 	[NFP_SPEED_1G]		= SPEED_1000,
428 	[NFP_SPEED_10G]		= SPEED_10000,
429 	[NFP_SPEED_25G]		= SPEED_25000,
430 	[NFP_SPEED_40G]		= SPEED_40000,
431 	[NFP_SPEED_50G]		= SPEED_50000,
432 	[NFP_SPEED_100G]	= SPEED_100000,
433 };
434 
435 static void nfp_add_media_link_mode(struct nfp_port *port,
436 				    struct nfp_eth_table_port *eth_port,
437 				    struct ethtool_link_ksettings *cmd)
438 {
439 	bitmap_zero(port->speed_bitmap, NFP_SUP_SPEED_NUMBER);
440 
441 	for (u32 i = 0; i < NFP_MEDIA_LINK_MODES_NUMBER; i++) {
442 		if (i < 64) {
443 			if (eth_port->link_modes_supp[0] & BIT_ULL(i)) {
444 				__set_bit(nfp_eth_media_table[i].ethtool_link_mode,
445 					  cmd->link_modes.supported);
446 				__set_bit(nfp_eth_media_table[i].speed,
447 					  port->speed_bitmap);
448 			}
449 
450 			if (eth_port->link_modes_ad[0] & BIT_ULL(i))
451 				__set_bit(nfp_eth_media_table[i].ethtool_link_mode,
452 					  cmd->link_modes.advertising);
453 		} else {
454 			if (eth_port->link_modes_supp[1] & BIT_ULL(i - 64)) {
455 				__set_bit(nfp_eth_media_table[i].ethtool_link_mode,
456 					  cmd->link_modes.supported);
457 				__set_bit(nfp_eth_media_table[i].speed,
458 					  port->speed_bitmap);
459 			}
460 
461 			if (eth_port->link_modes_ad[1] & BIT_ULL(i - 64))
462 				__set_bit(nfp_eth_media_table[i].ethtool_link_mode,
463 					  cmd->link_modes.advertising);
464 		}
465 	}
466 
467 	/* We take all speeds as supported when it fails to read
468 	 * link modes due to old management firmware that doesn't
469 	 * support link modes reading or error occurring, so that
470 	 * speed change of this port is allowed.
471 	 */
472 	if (bitmap_empty(port->speed_bitmap, NFP_SUP_SPEED_NUMBER))
473 		bitmap_fill(port->speed_bitmap, NFP_SUP_SPEED_NUMBER);
474 }
475 
476 /**
477  * nfp_net_get_link_ksettings - Get Link Speed settings
478  * @netdev:	network interface device structure
479  * @cmd:	ethtool command
480  *
481  * Reports speed settings based on info in the BAR provided by the fw.
482  */
483 static int
484 nfp_net_get_link_ksettings(struct net_device *netdev,
485 			   struct ethtool_link_ksettings *cmd)
486 {
487 	struct nfp_eth_table_port *eth_port;
488 	struct nfp_port *port;
489 	struct nfp_net *nn;
490 	unsigned int speed;
491 	u16 sts;
492 
493 	/* Init to unknowns */
494 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
495 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
496 	ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
497 	cmd->base.port = PORT_OTHER;
498 	cmd->base.speed = SPEED_UNKNOWN;
499 	cmd->base.duplex = DUPLEX_UNKNOWN;
500 
501 	port = nfp_port_from_netdev(netdev);
502 	eth_port = nfp_port_get_eth_port(port);
503 	if (eth_port) {
504 		ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
505 		ethtool_link_ksettings_add_link_mode(cmd, advertising, Pause);
506 		nfp_add_media_link_mode(port, eth_port, cmd);
507 		if (eth_port->supp_aneg) {
508 			ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
509 			if (eth_port->aneg == NFP_ANEG_AUTO) {
510 				ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
511 				cmd->base.autoneg = AUTONEG_ENABLE;
512 			}
513 		}
514 		nfp_net_set_fec_link_mode(eth_port, cmd);
515 	}
516 
517 	if (!netif_carrier_ok(netdev))
518 		return 0;
519 
520 	/* Use link speed from ETH table if available, otherwise try the BAR */
521 	if (eth_port) {
522 		cmd->base.port = eth_port->port_type;
523 		cmd->base.speed = eth_port->speed;
524 		cmd->base.duplex = DUPLEX_FULL;
525 		return 0;
526 	}
527 
528 	if (!nfp_netdev_is_nfp_net(netdev))
529 		return -EOPNOTSUPP;
530 	nn = netdev_priv(netdev);
531 
532 	sts = nn_readw(nn, NFP_NET_CFG_STS);
533 	speed = nfp_net_lr2speed(FIELD_GET(NFP_NET_CFG_STS_LINK_RATE, sts));
534 	if (!speed)
535 		return -EOPNOTSUPP;
536 
537 	if (speed != SPEED_UNKNOWN) {
538 		cmd->base.speed = speed;
539 		cmd->base.duplex = DUPLEX_FULL;
540 	}
541 
542 	return 0;
543 }
544 
545 static int
546 nfp_net_set_link_ksettings(struct net_device *netdev,
547 			   const struct ethtool_link_ksettings *cmd)
548 {
549 	bool req_aneg = (cmd->base.autoneg == AUTONEG_ENABLE);
550 	struct nfp_eth_table_port *eth_port;
551 	struct nfp_port *port;
552 	struct nfp_nsp *nsp;
553 	int err;
554 
555 	port = nfp_port_from_netdev(netdev);
556 	eth_port = __nfp_port_get_eth_port(port);
557 	if (!eth_port)
558 		return -EOPNOTSUPP;
559 
560 	if (netif_running(netdev)) {
561 		netdev_warn(netdev, "Changing settings not allowed on an active interface. It may cause the port to be disabled until driver reload.\n");
562 		return -EBUSY;
563 	}
564 
565 	nsp = nfp_eth_config_start(port->app->cpp, eth_port->index);
566 	if (IS_ERR(nsp))
567 		return PTR_ERR(nsp);
568 
569 	if (req_aneg && !eth_port->supp_aneg) {
570 		netdev_warn(netdev, "Autoneg is not supported.\n");
571 		err = -EOPNOTSUPP;
572 		goto err_bad_set;
573 	}
574 
575 	err = __nfp_eth_set_aneg(nsp, req_aneg ? NFP_ANEG_AUTO : NFP_ANEG_DISABLED);
576 	if (err)
577 		goto err_bad_set;
578 
579 	if (cmd->base.speed != SPEED_UNKNOWN) {
580 		u32 speed = cmd->base.speed / eth_port->lanes;
581 		bool is_supported = false;
582 
583 		for (u32 i = 0; i < NFP_SUP_SPEED_NUMBER; i++) {
584 			if (cmd->base.speed == nfp_eth_speed_map[i] &&
585 			    test_bit(i, port->speed_bitmap)) {
586 				is_supported = true;
587 				break;
588 			}
589 		}
590 
591 		if (!is_supported) {
592 			netdev_err(netdev, "Speed %u is not supported.\n",
593 				   cmd->base.speed);
594 			err = -EINVAL;
595 			goto err_bad_set;
596 		}
597 
598 		if (req_aneg) {
599 			netdev_err(netdev, "Speed changing is not allowed when working on autoneg mode.\n");
600 			err = -EINVAL;
601 			goto err_bad_set;
602 		}
603 
604 		err = __nfp_eth_set_speed(nsp, speed);
605 		if (err)
606 			goto err_bad_set;
607 	}
608 
609 	err = nfp_eth_config_commit_end(nsp);
610 	if (err > 0)
611 		return 0; /* no change */
612 
613 	nfp_net_refresh_port_table(port);
614 
615 	return err;
616 
617 err_bad_set:
618 	nfp_eth_config_cleanup_end(nsp);
619 	return err;
620 }
621 
622 static void nfp_net_get_ringparam(struct net_device *netdev,
623 				  struct ethtool_ringparam *ring,
624 				  struct kernel_ethtool_ringparam *kernel_ring,
625 				  struct netlink_ext_ack *extack)
626 {
627 	struct nfp_net *nn = netdev_priv(netdev);
628 	u32 qc_max = nn->dev_info->max_qc_size;
629 
630 	ring->rx_max_pending = qc_max;
631 	ring->tx_max_pending = qc_max / nn->dp.ops->tx_min_desc_per_pkt;
632 	ring->rx_pending = nn->dp.rxd_cnt;
633 	ring->tx_pending = nn->dp.txd_cnt;
634 }
635 
636 static int nfp_net_set_ring_size(struct nfp_net *nn, u32 rxd_cnt, u32 txd_cnt)
637 {
638 	struct nfp_net_dp *dp;
639 
640 	dp = nfp_net_clone_dp(nn);
641 	if (!dp)
642 		return -ENOMEM;
643 
644 	dp->rxd_cnt = rxd_cnt;
645 	dp->txd_cnt = txd_cnt;
646 
647 	return nfp_net_ring_reconfig(nn, dp, NULL);
648 }
649 
650 static int nfp_net_set_ringparam(struct net_device *netdev,
651 				 struct ethtool_ringparam *ring,
652 				 struct kernel_ethtool_ringparam *kernel_ring,
653 				 struct netlink_ext_ack *extack)
654 {
655 	u32 tx_dpp, qc_min, qc_max, rxd_cnt, txd_cnt;
656 	struct nfp_net *nn = netdev_priv(netdev);
657 
658 	/* We don't have separate queues/rings for small/large frames. */
659 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
660 		return -EINVAL;
661 
662 	qc_min = nn->dev_info->min_qc_size;
663 	qc_max = nn->dev_info->max_qc_size;
664 	tx_dpp = nn->dp.ops->tx_min_desc_per_pkt;
665 	/* Round up to supported values */
666 	rxd_cnt = roundup_pow_of_two(ring->rx_pending);
667 	txd_cnt = roundup_pow_of_two(ring->tx_pending);
668 
669 	if (rxd_cnt < qc_min || rxd_cnt > qc_max ||
670 	    txd_cnt < qc_min / tx_dpp || txd_cnt > qc_max / tx_dpp)
671 		return -EINVAL;
672 
673 	if (nn->dp.rxd_cnt == rxd_cnt && nn->dp.txd_cnt == txd_cnt)
674 		return 0;
675 
676 	nn_dbg(nn, "Change ring size: RxQ %u->%u, TxQ %u->%u\n",
677 	       nn->dp.rxd_cnt, rxd_cnt, nn->dp.txd_cnt, txd_cnt);
678 
679 	return nfp_net_set_ring_size(nn, rxd_cnt, txd_cnt);
680 }
681 
682 static int nfp_test_link(struct net_device *netdev)
683 {
684 	if (!netif_carrier_ok(netdev) || !(netdev->flags & IFF_UP))
685 		return 1;
686 
687 	return 0;
688 }
689 
690 static int nfp_test_nsp(struct net_device *netdev)
691 {
692 	struct nfp_app *app = nfp_app_from_netdev(netdev);
693 	struct nfp_nsp_identify *nspi;
694 	struct nfp_nsp *nsp;
695 	int err;
696 
697 	nsp = nfp_nsp_open(app->cpp);
698 	if (IS_ERR(nsp)) {
699 		err = PTR_ERR(nsp);
700 		netdev_info(netdev, "NSP Test: failed to access the NSP: %d\n", err);
701 		goto exit;
702 	}
703 
704 	if (nfp_nsp_get_abi_ver_minor(nsp) < 15) {
705 		err = -EOPNOTSUPP;
706 		goto exit_close_nsp;
707 	}
708 
709 	nspi = kzalloc(sizeof(*nspi), GFP_KERNEL);
710 	if (!nspi) {
711 		err = -ENOMEM;
712 		goto exit_close_nsp;
713 	}
714 
715 	err = nfp_nsp_read_identify(nsp, nspi, sizeof(*nspi));
716 	if (err < 0)
717 		netdev_info(netdev, "NSP Test: reading bsp version failed %d\n", err);
718 
719 	kfree(nspi);
720 exit_close_nsp:
721 	nfp_nsp_close(nsp);
722 exit:
723 	return err;
724 }
725 
726 static int nfp_test_fw(struct net_device *netdev)
727 {
728 	struct nfp_net *nn = netdev_priv(netdev);
729 	int err;
730 
731 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
732 	if (err)
733 		netdev_info(netdev, "FW Test: update failed %d\n", err);
734 
735 	return err;
736 }
737 
738 static int nfp_test_reg(struct net_device *netdev)
739 {
740 	struct nfp_app *app = nfp_app_from_netdev(netdev);
741 	struct nfp_cpp *cpp = app->cpp;
742 	u32 model = nfp_cpp_model(cpp);
743 	u32 value;
744 	int err;
745 
746 	err = nfp_cpp_model_autodetect(cpp, &value);
747 	if (err < 0) {
748 		netdev_info(netdev, "REG Test: NFP model detection failed %d\n", err);
749 		return err;
750 	}
751 
752 	return (value == model) ? 0 : 1;
753 }
754 
755 static bool link_test_supported(struct net_device *netdev)
756 {
757 	return true;
758 }
759 
760 static bool nsp_test_supported(struct net_device *netdev)
761 {
762 	if (nfp_app_from_netdev(netdev))
763 		return true;
764 
765 	return false;
766 }
767 
768 static bool fw_test_supported(struct net_device *netdev)
769 {
770 	if (nfp_netdev_is_nfp_net(netdev))
771 		return true;
772 
773 	return false;
774 }
775 
776 static bool reg_test_supported(struct net_device *netdev)
777 {
778 	if (nfp_app_from_netdev(netdev))
779 		return true;
780 
781 	return false;
782 }
783 
784 static struct nfp_self_test_item {
785 	char name[ETH_GSTRING_LEN];
786 	bool (*is_supported)(struct net_device *dev);
787 	int (*func)(struct net_device *dev);
788 } nfp_self_test[] = {
789 	{"Link Test", link_test_supported, nfp_test_link},
790 	{"NSP Test", nsp_test_supported, nfp_test_nsp},
791 	{"Firmware Test", fw_test_supported, nfp_test_fw},
792 	{"Register Test", reg_test_supported, nfp_test_reg}
793 };
794 
795 #define NFP_TEST_TOTAL_NUM ARRAY_SIZE(nfp_self_test)
796 
797 static void nfp_get_self_test_strings(struct net_device *netdev, u8 *data)
798 {
799 	int i;
800 
801 	for (i = 0; i < NFP_TEST_TOTAL_NUM; i++)
802 		if (nfp_self_test[i].is_supported(netdev))
803 			ethtool_sprintf(&data, nfp_self_test[i].name);
804 }
805 
806 static int nfp_get_self_test_count(struct net_device *netdev)
807 {
808 	int i, count = 0;
809 
810 	for (i = 0; i < NFP_TEST_TOTAL_NUM; i++)
811 		if (nfp_self_test[i].is_supported(netdev))
812 			count++;
813 
814 	return count;
815 }
816 
817 static void nfp_net_self_test(struct net_device *netdev, struct ethtool_test *eth_test,
818 			      u64 *data)
819 {
820 	int i, ret, count = 0;
821 
822 	netdev_info(netdev, "Start self test\n");
823 
824 	for (i = 0; i < NFP_TEST_TOTAL_NUM; i++) {
825 		if (nfp_self_test[i].is_supported(netdev)) {
826 			ret = nfp_self_test[i].func(netdev);
827 			if (ret)
828 				eth_test->flags |= ETH_TEST_FL_FAILED;
829 			data[count++] = ret;
830 		}
831 	}
832 
833 	netdev_info(netdev, "Test end\n");
834 }
835 
836 static unsigned int nfp_vnic_get_sw_stats_count(struct net_device *netdev)
837 {
838 	struct nfp_net *nn = netdev_priv(netdev);
839 
840 	return NN_RVEC_GATHER_STATS + nn->max_r_vecs * NN_RVEC_PER_Q_STATS +
841 		NN_CTRL_PATH_STATS;
842 }
843 
844 static u8 *nfp_vnic_get_sw_stats_strings(struct net_device *netdev, u8 *data)
845 {
846 	struct nfp_net *nn = netdev_priv(netdev);
847 	int i;
848 
849 	for (i = 0; i < nn->max_r_vecs; i++) {
850 		ethtool_sprintf(&data, "rvec_%u_rx_pkts", i);
851 		ethtool_sprintf(&data, "rvec_%u_tx_pkts", i);
852 		ethtool_sprintf(&data, "rvec_%u_tx_busy", i);
853 	}
854 
855 	ethtool_sprintf(&data, "hw_rx_csum_ok");
856 	ethtool_sprintf(&data, "hw_rx_csum_inner_ok");
857 	ethtool_sprintf(&data, "hw_rx_csum_complete");
858 	ethtool_sprintf(&data, "hw_rx_csum_err");
859 	ethtool_sprintf(&data, "rx_replace_buf_alloc_fail");
860 	ethtool_sprintf(&data, "rx_tls_decrypted_packets");
861 	ethtool_sprintf(&data, "hw_tx_csum");
862 	ethtool_sprintf(&data, "hw_tx_inner_csum");
863 	ethtool_sprintf(&data, "tx_gather");
864 	ethtool_sprintf(&data, "tx_lso");
865 	ethtool_sprintf(&data, "tx_tls_encrypted_packets");
866 	ethtool_sprintf(&data, "tx_tls_ooo");
867 	ethtool_sprintf(&data, "tx_tls_drop_no_sync_data");
868 
869 	ethtool_sprintf(&data, "hw_tls_no_space");
870 	ethtool_sprintf(&data, "rx_tls_resync_req_ok");
871 	ethtool_sprintf(&data, "rx_tls_resync_req_ign");
872 	ethtool_sprintf(&data, "rx_tls_resync_sent");
873 
874 	return data;
875 }
876 
877 static u64 *nfp_vnic_get_sw_stats(struct net_device *netdev, u64 *data)
878 {
879 	u64 gathered_stats[NN_RVEC_GATHER_STATS] = {};
880 	struct nfp_net *nn = netdev_priv(netdev);
881 	u64 tmp[NN_RVEC_GATHER_STATS];
882 	unsigned int i, j;
883 
884 	for (i = 0; i < nn->max_r_vecs; i++) {
885 		unsigned int start;
886 
887 		do {
888 			start = u64_stats_fetch_begin(&nn->r_vecs[i].rx_sync);
889 			data[0] = nn->r_vecs[i].rx_pkts;
890 			tmp[0] = nn->r_vecs[i].hw_csum_rx_ok;
891 			tmp[1] = nn->r_vecs[i].hw_csum_rx_inner_ok;
892 			tmp[2] = nn->r_vecs[i].hw_csum_rx_complete;
893 			tmp[3] = nn->r_vecs[i].hw_csum_rx_error;
894 			tmp[4] = nn->r_vecs[i].rx_replace_buf_alloc_fail;
895 			tmp[5] = nn->r_vecs[i].hw_tls_rx;
896 		} while (u64_stats_fetch_retry(&nn->r_vecs[i].rx_sync, start));
897 
898 		do {
899 			start = u64_stats_fetch_begin(&nn->r_vecs[i].tx_sync);
900 			data[1] = nn->r_vecs[i].tx_pkts;
901 			data[2] = nn->r_vecs[i].tx_busy;
902 			tmp[6] = nn->r_vecs[i].hw_csum_tx;
903 			tmp[7] = nn->r_vecs[i].hw_csum_tx_inner;
904 			tmp[8] = nn->r_vecs[i].tx_gather;
905 			tmp[9] = nn->r_vecs[i].tx_lso;
906 			tmp[10] = nn->r_vecs[i].hw_tls_tx;
907 			tmp[11] = nn->r_vecs[i].tls_tx_fallback;
908 			tmp[12] = nn->r_vecs[i].tls_tx_no_fallback;
909 		} while (u64_stats_fetch_retry(&nn->r_vecs[i].tx_sync, start));
910 
911 		data += NN_RVEC_PER_Q_STATS;
912 
913 		for (j = 0; j < NN_RVEC_GATHER_STATS; j++)
914 			gathered_stats[j] += tmp[j];
915 	}
916 
917 	for (j = 0; j < NN_RVEC_GATHER_STATS; j++)
918 		*data++ = gathered_stats[j];
919 
920 	*data++ = atomic_read(&nn->ktls_no_space);
921 	*data++ = atomic_read(&nn->ktls_rx_resync_req);
922 	*data++ = atomic_read(&nn->ktls_rx_resync_ign);
923 	*data++ = atomic_read(&nn->ktls_rx_resync_sent);
924 
925 	return data;
926 }
927 
928 static unsigned int nfp_vnic_get_hw_stats_count(unsigned int num_vecs)
929 {
930 	return NN_ET_GLOBAL_STATS_LEN + num_vecs * 4;
931 }
932 
933 static u8 *
934 nfp_vnic_get_hw_stats_strings(u8 *data, unsigned int num_vecs, bool repr)
935 {
936 	int swap_off, i;
937 
938 	BUILD_BUG_ON(NN_ET_GLOBAL_STATS_LEN < NN_ET_SWITCH_STATS_LEN * 2);
939 	/* If repr is true first add SWITCH_STATS_LEN and then subtract it
940 	 * effectively swapping the RX and TX statistics (giving us the RX
941 	 * and TX from perspective of the switch).
942 	 */
943 	swap_off = repr * NN_ET_SWITCH_STATS_LEN;
944 
945 	for (i = 0; i < NN_ET_SWITCH_STATS_LEN; i++)
946 		ethtool_sprintf(&data, nfp_net_et_stats[i + swap_off].name);
947 
948 	for (i = NN_ET_SWITCH_STATS_LEN; i < NN_ET_SWITCH_STATS_LEN * 2; i++)
949 		ethtool_sprintf(&data, nfp_net_et_stats[i - swap_off].name);
950 
951 	for (i = NN_ET_SWITCH_STATS_LEN * 2; i < NN_ET_GLOBAL_STATS_LEN; i++)
952 		ethtool_sprintf(&data, nfp_net_et_stats[i].name);
953 
954 	for (i = 0; i < num_vecs; i++) {
955 		ethtool_sprintf(&data, "rxq_%u_pkts", i);
956 		ethtool_sprintf(&data, "rxq_%u_bytes", i);
957 		ethtool_sprintf(&data, "txq_%u_pkts", i);
958 		ethtool_sprintf(&data, "txq_%u_bytes", i);
959 	}
960 
961 	return data;
962 }
963 
964 static u64 *
965 nfp_vnic_get_hw_stats(u64 *data, u8 __iomem *mem, unsigned int num_vecs)
966 {
967 	unsigned int i;
968 
969 	for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++)
970 		*data++ = readq(mem + nfp_net_et_stats[i].off);
971 
972 	for (i = 0; i < num_vecs; i++) {
973 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i));
974 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i) + 8);
975 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i));
976 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i) + 8);
977 	}
978 
979 	return data;
980 }
981 
982 static unsigned int nfp_vnic_get_tlv_stats_count(struct nfp_net *nn)
983 {
984 	return nn->tlv_caps.vnic_stats_cnt + nn->max_r_vecs * 4;
985 }
986 
987 static u8 *nfp_vnic_get_tlv_stats_strings(struct nfp_net *nn, u8 *data)
988 {
989 	unsigned int i, id;
990 	u8 __iomem *mem;
991 	u64 id_word = 0;
992 
993 	mem = nn->dp.ctrl_bar + nn->tlv_caps.vnic_stats_off;
994 	for (i = 0; i < nn->tlv_caps.vnic_stats_cnt; i++) {
995 		if (!(i % 4))
996 			id_word = readq(mem + i * 2);
997 
998 		id = (u16)id_word;
999 		id_word >>= 16;
1000 
1001 		if (id < ARRAY_SIZE(nfp_tlv_stat_names) &&
1002 		    nfp_tlv_stat_names[id][0]) {
1003 			memcpy(data, nfp_tlv_stat_names[id], ETH_GSTRING_LEN);
1004 			data += ETH_GSTRING_LEN;
1005 		} else {
1006 			ethtool_sprintf(&data, "dev_unknown_stat%u", id);
1007 		}
1008 	}
1009 
1010 	for (i = 0; i < nn->max_r_vecs; i++) {
1011 		ethtool_sprintf(&data, "rxq_%u_pkts", i);
1012 		ethtool_sprintf(&data, "rxq_%u_bytes", i);
1013 		ethtool_sprintf(&data, "txq_%u_pkts", i);
1014 		ethtool_sprintf(&data, "txq_%u_bytes", i);
1015 	}
1016 
1017 	return data;
1018 }
1019 
1020 static u64 *nfp_vnic_get_tlv_stats(struct nfp_net *nn, u64 *data)
1021 {
1022 	u8 __iomem *mem;
1023 	unsigned int i;
1024 
1025 	mem = nn->dp.ctrl_bar + nn->tlv_caps.vnic_stats_off;
1026 	mem += roundup(2 * nn->tlv_caps.vnic_stats_cnt, 8);
1027 	for (i = 0; i < nn->tlv_caps.vnic_stats_cnt; i++)
1028 		*data++ = readq(mem + i * 8);
1029 
1030 	mem = nn->dp.ctrl_bar;
1031 	for (i = 0; i < nn->max_r_vecs; i++) {
1032 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i));
1033 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i) + 8);
1034 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i));
1035 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i) + 8);
1036 	}
1037 
1038 	return data;
1039 }
1040 
1041 static unsigned int nfp_mac_get_stats_count(struct net_device *netdev)
1042 {
1043 	struct nfp_port *port;
1044 
1045 	port = nfp_port_from_netdev(netdev);
1046 	if (!__nfp_port_get_eth_port(port) || !port->eth_stats)
1047 		return 0;
1048 
1049 	return ARRAY_SIZE(nfp_mac_et_stats);
1050 }
1051 
1052 static u8 *nfp_mac_get_stats_strings(struct net_device *netdev, u8 *data)
1053 {
1054 	struct nfp_port *port;
1055 	unsigned int i;
1056 
1057 	port = nfp_port_from_netdev(netdev);
1058 	if (!__nfp_port_get_eth_port(port) || !port->eth_stats)
1059 		return data;
1060 
1061 	for (i = 0; i < ARRAY_SIZE(nfp_mac_et_stats); i++)
1062 		ethtool_sprintf(&data, "mac.%s", nfp_mac_et_stats[i].name);
1063 
1064 	return data;
1065 }
1066 
1067 static u64 *nfp_mac_get_stats(struct net_device *netdev, u64 *data)
1068 {
1069 	struct nfp_port *port;
1070 	unsigned int i;
1071 
1072 	port = nfp_port_from_netdev(netdev);
1073 	if (!__nfp_port_get_eth_port(port) || !port->eth_stats)
1074 		return data;
1075 
1076 	for (i = 0; i < ARRAY_SIZE(nfp_mac_et_stats); i++)
1077 		*data++ = readq(port->eth_stats + nfp_mac_et_stats[i].off);
1078 
1079 	return data;
1080 }
1081 
1082 static void nfp_net_get_strings(struct net_device *netdev,
1083 				u32 stringset, u8 *data)
1084 {
1085 	struct nfp_net *nn = netdev_priv(netdev);
1086 
1087 	switch (stringset) {
1088 	case ETH_SS_STATS:
1089 		data = nfp_vnic_get_sw_stats_strings(netdev, data);
1090 		if (!nn->tlv_caps.vnic_stats_off)
1091 			data = nfp_vnic_get_hw_stats_strings(data,
1092 							     nn->max_r_vecs,
1093 							     false);
1094 		else
1095 			data = nfp_vnic_get_tlv_stats_strings(nn, data);
1096 		data = nfp_mac_get_stats_strings(netdev, data);
1097 		data = nfp_app_port_get_stats_strings(nn->port, data);
1098 		break;
1099 	case ETH_SS_TEST:
1100 		nfp_get_self_test_strings(netdev, data);
1101 		break;
1102 	}
1103 }
1104 
1105 static void
1106 nfp_net_get_stats(struct net_device *netdev, struct ethtool_stats *stats,
1107 		  u64 *data)
1108 {
1109 	struct nfp_net *nn = netdev_priv(netdev);
1110 
1111 	data = nfp_vnic_get_sw_stats(netdev, data);
1112 	if (!nn->tlv_caps.vnic_stats_off)
1113 		data = nfp_vnic_get_hw_stats(data, nn->dp.ctrl_bar,
1114 					     nn->max_r_vecs);
1115 	else
1116 		data = nfp_vnic_get_tlv_stats(nn, data);
1117 	data = nfp_mac_get_stats(netdev, data);
1118 	data = nfp_app_port_get_stats(nn->port, data);
1119 }
1120 
1121 static int nfp_net_get_sset_count(struct net_device *netdev, int sset)
1122 {
1123 	struct nfp_net *nn = netdev_priv(netdev);
1124 	unsigned int cnt;
1125 
1126 	switch (sset) {
1127 	case ETH_SS_STATS:
1128 		cnt = nfp_vnic_get_sw_stats_count(netdev);
1129 		if (!nn->tlv_caps.vnic_stats_off)
1130 			cnt += nfp_vnic_get_hw_stats_count(nn->max_r_vecs);
1131 		else
1132 			cnt += nfp_vnic_get_tlv_stats_count(nn);
1133 		cnt += nfp_mac_get_stats_count(netdev);
1134 		cnt += nfp_app_port_get_stats_count(nn->port);
1135 		return cnt;
1136 	case ETH_SS_TEST:
1137 		return nfp_get_self_test_count(netdev);
1138 	default:
1139 		return -EOPNOTSUPP;
1140 	}
1141 }
1142 
1143 static void nfp_port_get_strings(struct net_device *netdev,
1144 				 u32 stringset, u8 *data)
1145 {
1146 	struct nfp_port *port = nfp_port_from_netdev(netdev);
1147 
1148 	switch (stringset) {
1149 	case ETH_SS_STATS:
1150 		if (nfp_port_is_vnic(port))
1151 			data = nfp_vnic_get_hw_stats_strings(data, 0, true);
1152 		else
1153 			data = nfp_mac_get_stats_strings(netdev, data);
1154 		data = nfp_app_port_get_stats_strings(port, data);
1155 		break;
1156 	case ETH_SS_TEST:
1157 		nfp_get_self_test_strings(netdev, data);
1158 		break;
1159 	}
1160 }
1161 
1162 static void
1163 nfp_port_get_stats(struct net_device *netdev, struct ethtool_stats *stats,
1164 		   u64 *data)
1165 {
1166 	struct nfp_port *port = nfp_port_from_netdev(netdev);
1167 
1168 	if (nfp_port_is_vnic(port))
1169 		data = nfp_vnic_get_hw_stats(data, port->vnic, 0);
1170 	else
1171 		data = nfp_mac_get_stats(netdev, data);
1172 	data = nfp_app_port_get_stats(port, data);
1173 }
1174 
1175 static int nfp_port_get_sset_count(struct net_device *netdev, int sset)
1176 {
1177 	struct nfp_port *port = nfp_port_from_netdev(netdev);
1178 	unsigned int count;
1179 
1180 	switch (sset) {
1181 	case ETH_SS_STATS:
1182 		if (nfp_port_is_vnic(port))
1183 			count = nfp_vnic_get_hw_stats_count(0);
1184 		else
1185 			count = nfp_mac_get_stats_count(netdev);
1186 		count += nfp_app_port_get_stats_count(port);
1187 		return count;
1188 	case ETH_SS_TEST:
1189 		return nfp_get_self_test_count(netdev);
1190 	default:
1191 		return -EOPNOTSUPP;
1192 	}
1193 }
1194 
1195 static int nfp_port_fec_ethtool_to_nsp(u32 fec)
1196 {
1197 	switch (fec) {
1198 	case ETHTOOL_FEC_AUTO:
1199 		return NFP_FEC_AUTO_BIT;
1200 	case ETHTOOL_FEC_OFF:
1201 		return NFP_FEC_DISABLED_BIT;
1202 	case ETHTOOL_FEC_RS:
1203 		return NFP_FEC_REED_SOLOMON_BIT;
1204 	case ETHTOOL_FEC_BASER:
1205 		return NFP_FEC_BASER_BIT;
1206 	default:
1207 		/* NSP only supports a single mode at a time */
1208 		return -EOPNOTSUPP;
1209 	}
1210 }
1211 
1212 static u32 nfp_port_fec_nsp_to_ethtool(u32 fec)
1213 {
1214 	u32 result = 0;
1215 
1216 	if (fec & NFP_FEC_AUTO)
1217 		result |= ETHTOOL_FEC_AUTO;
1218 	if (fec & NFP_FEC_BASER)
1219 		result |= ETHTOOL_FEC_BASER;
1220 	if (fec & NFP_FEC_REED_SOLOMON)
1221 		result |= ETHTOOL_FEC_RS;
1222 	if (fec & NFP_FEC_DISABLED)
1223 		result |= ETHTOOL_FEC_OFF;
1224 
1225 	return result ?: ETHTOOL_FEC_NONE;
1226 }
1227 
1228 static int
1229 nfp_port_get_fecparam(struct net_device *netdev,
1230 		      struct ethtool_fecparam *param)
1231 {
1232 	struct nfp_eth_table_port *eth_port;
1233 	struct nfp_port *port;
1234 
1235 	param->active_fec = ETHTOOL_FEC_NONE;
1236 	param->fec = ETHTOOL_FEC_NONE;
1237 
1238 	port = nfp_port_from_netdev(netdev);
1239 	eth_port = nfp_port_get_eth_port(port);
1240 	if (!eth_port)
1241 		return -EOPNOTSUPP;
1242 
1243 	if (!nfp_eth_can_support_fec(eth_port))
1244 		return 0;
1245 
1246 	param->fec = nfp_port_fec_nsp_to_ethtool(eth_port->fec_modes_supported);
1247 	param->active_fec = nfp_port_fec_nsp_to_ethtool(BIT(eth_port->act_fec));
1248 
1249 	return 0;
1250 }
1251 
1252 static int
1253 nfp_port_set_fecparam(struct net_device *netdev,
1254 		      struct ethtool_fecparam *param)
1255 {
1256 	struct nfp_eth_table_port *eth_port;
1257 	struct nfp_port *port;
1258 	int err, fec;
1259 
1260 	port = nfp_port_from_netdev(netdev);
1261 	eth_port = nfp_port_get_eth_port(port);
1262 	if (!eth_port)
1263 		return -EOPNOTSUPP;
1264 
1265 	if (!nfp_eth_can_support_fec(eth_port))
1266 		return -EOPNOTSUPP;
1267 
1268 	fec = nfp_port_fec_ethtool_to_nsp(param->fec);
1269 	if (fec < 0)
1270 		return fec;
1271 
1272 	err = nfp_eth_set_fec(port->app->cpp, eth_port->index, fec);
1273 	if (!err)
1274 		/* Only refresh if we did something */
1275 		nfp_net_refresh_port_table(port);
1276 
1277 	return err < 0 ? err : 0;
1278 }
1279 
1280 /* RX network flow classification (RSS, filters, etc)
1281  */
1282 static u32 ethtool_flow_to_nfp_flag(u32 flow_type)
1283 {
1284 	static const u32 xlate_ethtool_to_nfp[IPV6_FLOW + 1] = {
1285 		[TCP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_TCP,
1286 		[TCP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_TCP,
1287 		[UDP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_UDP,
1288 		[UDP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_UDP,
1289 		[IPV4_FLOW]	= NFP_NET_CFG_RSS_IPV4,
1290 		[IPV6_FLOW]	= NFP_NET_CFG_RSS_IPV6,
1291 	};
1292 
1293 	if (flow_type >= ARRAY_SIZE(xlate_ethtool_to_nfp))
1294 		return 0;
1295 
1296 	return xlate_ethtool_to_nfp[flow_type];
1297 }
1298 
1299 static int nfp_net_get_rss_hash_opts(struct nfp_net *nn,
1300 				     struct ethtool_rxnfc *cmd)
1301 {
1302 	u32 nfp_rss_flag;
1303 
1304 	cmd->data = 0;
1305 
1306 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1307 		return -EOPNOTSUPP;
1308 
1309 	nfp_rss_flag = ethtool_flow_to_nfp_flag(cmd->flow_type);
1310 	if (!nfp_rss_flag)
1311 		return -EINVAL;
1312 
1313 	cmd->data |= RXH_IP_SRC | RXH_IP_DST;
1314 	if (nn->rss_cfg & nfp_rss_flag)
1315 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1316 
1317 	return 0;
1318 }
1319 
1320 static int nfp_net_get_rxnfc(struct net_device *netdev,
1321 			     struct ethtool_rxnfc *cmd, u32 *rule_locs)
1322 {
1323 	struct nfp_net *nn = netdev_priv(netdev);
1324 
1325 	switch (cmd->cmd) {
1326 	case ETHTOOL_GRXRINGS:
1327 		cmd->data = nn->dp.num_rx_rings;
1328 		return 0;
1329 	case ETHTOOL_GRXFH:
1330 		return nfp_net_get_rss_hash_opts(nn, cmd);
1331 	default:
1332 		return -EOPNOTSUPP;
1333 	}
1334 }
1335 
1336 static int nfp_net_set_rss_hash_opt(struct nfp_net *nn,
1337 				    struct ethtool_rxnfc *nfc)
1338 {
1339 	u32 new_rss_cfg = nn->rss_cfg;
1340 	u32 nfp_rss_flag;
1341 	int err;
1342 
1343 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1344 		return -EOPNOTSUPP;
1345 
1346 	/* RSS only supports IP SA/DA and L4 src/dst ports  */
1347 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
1348 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
1349 		return -EINVAL;
1350 
1351 	/* We need at least the IP SA/DA fields for hashing */
1352 	if (!(nfc->data & RXH_IP_SRC) ||
1353 	    !(nfc->data & RXH_IP_DST))
1354 		return -EINVAL;
1355 
1356 	nfp_rss_flag = ethtool_flow_to_nfp_flag(nfc->flow_type);
1357 	if (!nfp_rss_flag)
1358 		return -EINVAL;
1359 
1360 	switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1361 	case 0:
1362 		new_rss_cfg &= ~nfp_rss_flag;
1363 		break;
1364 	case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1365 		new_rss_cfg |= nfp_rss_flag;
1366 		break;
1367 	default:
1368 		return -EINVAL;
1369 	}
1370 
1371 	new_rss_cfg |= FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc);
1372 	new_rss_cfg |= NFP_NET_CFG_RSS_MASK;
1373 
1374 	if (new_rss_cfg == nn->rss_cfg)
1375 		return 0;
1376 
1377 	writel(new_rss_cfg, nn->dp.ctrl_bar + NFP_NET_CFG_RSS_CTRL);
1378 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
1379 	if (err)
1380 		return err;
1381 
1382 	nn->rss_cfg = new_rss_cfg;
1383 
1384 	nn_dbg(nn, "Changed RSS config to 0x%x\n", nn->rss_cfg);
1385 	return 0;
1386 }
1387 
1388 static int nfp_net_set_rxnfc(struct net_device *netdev,
1389 			     struct ethtool_rxnfc *cmd)
1390 {
1391 	struct nfp_net *nn = netdev_priv(netdev);
1392 
1393 	switch (cmd->cmd) {
1394 	case ETHTOOL_SRXFH:
1395 		return nfp_net_set_rss_hash_opt(nn, cmd);
1396 	default:
1397 		return -EOPNOTSUPP;
1398 	}
1399 }
1400 
1401 static u32 nfp_net_get_rxfh_indir_size(struct net_device *netdev)
1402 {
1403 	struct nfp_net *nn = netdev_priv(netdev);
1404 
1405 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1406 		return 0;
1407 
1408 	return ARRAY_SIZE(nn->rss_itbl);
1409 }
1410 
1411 static u32 nfp_net_get_rxfh_key_size(struct net_device *netdev)
1412 {
1413 	struct nfp_net *nn = netdev_priv(netdev);
1414 
1415 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1416 		return -EOPNOTSUPP;
1417 
1418 	return nfp_net_rss_key_sz(nn);
1419 }
1420 
1421 static int nfp_net_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
1422 			    u8 *hfunc)
1423 {
1424 	struct nfp_net *nn = netdev_priv(netdev);
1425 	int i;
1426 
1427 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1428 		return -EOPNOTSUPP;
1429 
1430 	if (indir)
1431 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
1432 			indir[i] = nn->rss_itbl[i];
1433 	if (key)
1434 		memcpy(key, nn->rss_key, nfp_net_rss_key_sz(nn));
1435 	if (hfunc) {
1436 		*hfunc = nn->rss_hfunc;
1437 		if (*hfunc >= 1 << ETH_RSS_HASH_FUNCS_COUNT)
1438 			*hfunc = ETH_RSS_HASH_UNKNOWN;
1439 	}
1440 
1441 	return 0;
1442 }
1443 
1444 static int nfp_net_set_rxfh(struct net_device *netdev,
1445 			    const u32 *indir, const u8 *key,
1446 			    const u8 hfunc)
1447 {
1448 	struct nfp_net *nn = netdev_priv(netdev);
1449 	int i;
1450 
1451 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) ||
1452 	    !(hfunc == ETH_RSS_HASH_NO_CHANGE || hfunc == nn->rss_hfunc))
1453 		return -EOPNOTSUPP;
1454 
1455 	if (!key && !indir)
1456 		return 0;
1457 
1458 	if (key) {
1459 		memcpy(nn->rss_key, key, nfp_net_rss_key_sz(nn));
1460 		nfp_net_rss_write_key(nn);
1461 	}
1462 	if (indir) {
1463 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
1464 			nn->rss_itbl[i] = indir[i];
1465 
1466 		nfp_net_rss_write_itbl(nn);
1467 	}
1468 
1469 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
1470 }
1471 
1472 /* Dump BAR registers
1473  */
1474 static int nfp_net_get_regs_len(struct net_device *netdev)
1475 {
1476 	return NFP_NET_CFG_BAR_SZ;
1477 }
1478 
1479 static void nfp_net_get_regs(struct net_device *netdev,
1480 			     struct ethtool_regs *regs, void *p)
1481 {
1482 	struct nfp_net *nn = netdev_priv(netdev);
1483 	u32 *regs_buf = p;
1484 	int i;
1485 
1486 	regs->version = nn_readl(nn, NFP_NET_CFG_VERSION);
1487 
1488 	for (i = 0; i < NFP_NET_CFG_BAR_SZ / sizeof(u32); i++)
1489 		regs_buf[i] = readl(nn->dp.ctrl_bar + (i * sizeof(u32)));
1490 }
1491 
1492 static int nfp_net_get_coalesce(struct net_device *netdev,
1493 				struct ethtool_coalesce *ec,
1494 				struct kernel_ethtool_coalesce *kernel_coal,
1495 				struct netlink_ext_ack *extack)
1496 {
1497 	struct nfp_net *nn = netdev_priv(netdev);
1498 
1499 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
1500 		return -EINVAL;
1501 
1502 	ec->use_adaptive_rx_coalesce = nn->rx_coalesce_adapt_on;
1503 	ec->use_adaptive_tx_coalesce = nn->tx_coalesce_adapt_on;
1504 
1505 	ec->rx_coalesce_usecs       = nn->rx_coalesce_usecs;
1506 	ec->rx_max_coalesced_frames = nn->rx_coalesce_max_frames;
1507 	ec->tx_coalesce_usecs       = nn->tx_coalesce_usecs;
1508 	ec->tx_max_coalesced_frames = nn->tx_coalesce_max_frames;
1509 
1510 	return 0;
1511 }
1512 
1513 /* Other debug dumps
1514  */
1515 static int
1516 nfp_dump_nsp_diag(struct nfp_app *app, struct ethtool_dump *dump, void *buffer)
1517 {
1518 	struct nfp_resource *res;
1519 	int ret;
1520 
1521 	if (!app)
1522 		return -EOPNOTSUPP;
1523 
1524 	dump->version = 1;
1525 	dump->flag = NFP_DUMP_NSP_DIAG;
1526 
1527 	res = nfp_resource_acquire(app->cpp, NFP_RESOURCE_NSP_DIAG);
1528 	if (IS_ERR(res))
1529 		return PTR_ERR(res);
1530 
1531 	if (buffer) {
1532 		if (dump->len != nfp_resource_size(res)) {
1533 			ret = -EINVAL;
1534 			goto exit_release;
1535 		}
1536 
1537 		ret = nfp_cpp_read(app->cpp, nfp_resource_cpp_id(res),
1538 				   nfp_resource_address(res),
1539 				   buffer, dump->len);
1540 		if (ret != dump->len)
1541 			ret = ret < 0 ? ret : -EIO;
1542 		else
1543 			ret = 0;
1544 	} else {
1545 		dump->len = nfp_resource_size(res);
1546 		ret = 0;
1547 	}
1548 exit_release:
1549 	nfp_resource_release(res);
1550 
1551 	return ret;
1552 }
1553 
1554 /* Set the dump flag/level. Calculate the dump length for flag > 0 only (new TLV
1555  * based dumps), since flag 0 (default) calculates the length in
1556  * nfp_app_get_dump_flag(), and we need to support triggering a level 0 dump
1557  * without setting the flag first, for backward compatibility.
1558  */
1559 static int nfp_app_set_dump(struct net_device *netdev, struct ethtool_dump *val)
1560 {
1561 	struct nfp_app *app = nfp_app_from_netdev(netdev);
1562 	s64 len;
1563 
1564 	if (!app)
1565 		return -EOPNOTSUPP;
1566 
1567 	if (val->flag == NFP_DUMP_NSP_DIAG) {
1568 		app->pf->dump_flag = val->flag;
1569 		return 0;
1570 	}
1571 
1572 	if (!app->pf->dumpspec)
1573 		return -EOPNOTSUPP;
1574 
1575 	len = nfp_net_dump_calculate_size(app->pf, app->pf->dumpspec,
1576 					  val->flag);
1577 	if (len < 0)
1578 		return len;
1579 
1580 	app->pf->dump_flag = val->flag;
1581 	app->pf->dump_len = len;
1582 
1583 	return 0;
1584 }
1585 
1586 static int
1587 nfp_app_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
1588 {
1589 	struct nfp_app *app = nfp_app_from_netdev(netdev);
1590 
1591 	if (!app)
1592 		return -EOPNOTSUPP;
1593 
1594 	if (app->pf->dump_flag == NFP_DUMP_NSP_DIAG)
1595 		return nfp_dump_nsp_diag(app, dump, NULL);
1596 
1597 	dump->flag = app->pf->dump_flag;
1598 	dump->len = app->pf->dump_len;
1599 
1600 	return 0;
1601 }
1602 
1603 static int
1604 nfp_app_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
1605 		      void *buffer)
1606 {
1607 	struct nfp_app *app = nfp_app_from_netdev(netdev);
1608 
1609 	if (!app)
1610 		return -EOPNOTSUPP;
1611 
1612 	if (app->pf->dump_flag == NFP_DUMP_NSP_DIAG)
1613 		return nfp_dump_nsp_diag(app, dump, buffer);
1614 
1615 	dump->flag = app->pf->dump_flag;
1616 	dump->len = app->pf->dump_len;
1617 
1618 	return nfp_net_dump_populate_buffer(app->pf, app->pf->dumpspec, dump,
1619 					    buffer);
1620 }
1621 
1622 static int
1623 nfp_port_get_module_info(struct net_device *netdev,
1624 			 struct ethtool_modinfo *modinfo)
1625 {
1626 	struct nfp_eth_table_port *eth_port;
1627 	struct nfp_port *port;
1628 	unsigned int read_len;
1629 	struct nfp_nsp *nsp;
1630 	int err = 0;
1631 	u8 data;
1632 
1633 	port = nfp_port_from_netdev(netdev);
1634 	if (!port)
1635 		return -EOPNOTSUPP;
1636 
1637 	/* update port state to get latest interface */
1638 	set_bit(NFP_PORT_CHANGED, &port->flags);
1639 	eth_port = nfp_port_get_eth_port(port);
1640 	if (!eth_port)
1641 		return -EOPNOTSUPP;
1642 
1643 	nsp = nfp_nsp_open(port->app->cpp);
1644 	if (IS_ERR(nsp)) {
1645 		err = PTR_ERR(nsp);
1646 		netdev_err(netdev, "Failed to access the NSP: %d\n", err);
1647 		return err;
1648 	}
1649 
1650 	if (!nfp_nsp_has_read_module_eeprom(nsp)) {
1651 		netdev_info(netdev, "reading module EEPROM not supported. Please update flash\n");
1652 		err = -EOPNOTSUPP;
1653 		goto exit_close_nsp;
1654 	}
1655 
1656 	switch (eth_port->interface) {
1657 	case NFP_INTERFACE_SFP:
1658 	case NFP_INTERFACE_SFP28:
1659 		err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index,
1660 						 SFP_SFF8472_COMPLIANCE, &data,
1661 						 1, &read_len);
1662 		if (err < 0)
1663 			goto exit_close_nsp;
1664 
1665 		if (!data) {
1666 			modinfo->type = ETH_MODULE_SFF_8079;
1667 			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1668 		} else {
1669 			modinfo->type = ETH_MODULE_SFF_8472;
1670 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1671 		}
1672 		break;
1673 	case NFP_INTERFACE_QSFP:
1674 		err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index,
1675 						 SFP_SFF_REV_COMPLIANCE, &data,
1676 						 1, &read_len);
1677 		if (err < 0)
1678 			goto exit_close_nsp;
1679 
1680 		if (data < 0x3) {
1681 			modinfo->type = ETH_MODULE_SFF_8436;
1682 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1683 		} else {
1684 			modinfo->type = ETH_MODULE_SFF_8636;
1685 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1686 		}
1687 		break;
1688 	case NFP_INTERFACE_QSFP28:
1689 		modinfo->type = ETH_MODULE_SFF_8636;
1690 		modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1691 		break;
1692 	default:
1693 		netdev_err(netdev, "Unsupported module 0x%x detected\n",
1694 			   eth_port->interface);
1695 		err = -EINVAL;
1696 	}
1697 
1698 exit_close_nsp:
1699 	nfp_nsp_close(nsp);
1700 	return err;
1701 }
1702 
1703 static int
1704 nfp_port_get_module_eeprom(struct net_device *netdev,
1705 			   struct ethtool_eeprom *eeprom, u8 *data)
1706 {
1707 	struct nfp_eth_table_port *eth_port;
1708 	struct nfp_port *port;
1709 	struct nfp_nsp *nsp;
1710 	int err;
1711 
1712 	port = nfp_port_from_netdev(netdev);
1713 	eth_port = __nfp_port_get_eth_port(port);
1714 	if (!eth_port)
1715 		return -EOPNOTSUPP;
1716 
1717 	nsp = nfp_nsp_open(port->app->cpp);
1718 	if (IS_ERR(nsp)) {
1719 		err = PTR_ERR(nsp);
1720 		netdev_err(netdev, "Failed to access the NSP: %d\n", err);
1721 		return err;
1722 	}
1723 
1724 	if (!nfp_nsp_has_read_module_eeprom(nsp)) {
1725 		netdev_info(netdev, "reading module EEPROM not supported. Please update flash\n");
1726 		err = -EOPNOTSUPP;
1727 		goto exit_close_nsp;
1728 	}
1729 
1730 	err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index,
1731 					 eeprom->offset, data, eeprom->len,
1732 					 &eeprom->len);
1733 	if (err < 0) {
1734 		if (eeprom->len) {
1735 			netdev_warn(netdev,
1736 				    "Incomplete read from module EEPROM: %d\n",
1737 				     err);
1738 			err = 0;
1739 		} else {
1740 			netdev_err(netdev,
1741 				   "Reading from module EEPROM failed: %d\n",
1742 				   err);
1743 		}
1744 	}
1745 
1746 exit_close_nsp:
1747 	nfp_nsp_close(nsp);
1748 	return err;
1749 }
1750 
1751 static int nfp_net_set_coalesce(struct net_device *netdev,
1752 				struct ethtool_coalesce *ec,
1753 				struct kernel_ethtool_coalesce *kernel_coal,
1754 				struct netlink_ext_ack *extack)
1755 {
1756 	struct nfp_net *nn = netdev_priv(netdev);
1757 	unsigned int factor;
1758 
1759 	/* Compute factor used to convert coalesce '_usecs' parameters to
1760 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
1761 	 * count.
1762 	 */
1763 	factor = nn->tlv_caps.me_freq_mhz / 16;
1764 
1765 	/* Each pair of (usecs, max_frames) fields specifies that interrupts
1766 	 * should be coalesced until
1767 	 *      (usecs > 0 && time_since_first_completion >= usecs) ||
1768 	 *      (max_frames > 0 && completed_frames >= max_frames)
1769 	 *
1770 	 * It is illegal to set both usecs and max_frames to zero as this would
1771 	 * cause interrupts to never be generated.  To disable coalescing, set
1772 	 * usecs = 0 and max_frames = 1.
1773 	 *
1774 	 * Some implementations ignore the value of max_frames and use the
1775 	 * condition time_since_first_completion >= usecs
1776 	 */
1777 
1778 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
1779 		return -EINVAL;
1780 
1781 	/* ensure valid configuration */
1782 	if (!ec->rx_coalesce_usecs && !ec->rx_max_coalesced_frames)
1783 		return -EINVAL;
1784 
1785 	if (!ec->tx_coalesce_usecs && !ec->tx_max_coalesced_frames)
1786 		return -EINVAL;
1787 
1788 	if (nfp_net_coalesce_para_check(ec->rx_coalesce_usecs * factor,
1789 					ec->rx_max_coalesced_frames))
1790 		return -EINVAL;
1791 
1792 	if (nfp_net_coalesce_para_check(ec->tx_coalesce_usecs * factor,
1793 					ec->tx_max_coalesced_frames))
1794 		return -EINVAL;
1795 
1796 	/* configuration is valid */
1797 	nn->rx_coalesce_adapt_on = !!ec->use_adaptive_rx_coalesce;
1798 	nn->tx_coalesce_adapt_on = !!ec->use_adaptive_tx_coalesce;
1799 
1800 	nn->rx_coalesce_usecs      = ec->rx_coalesce_usecs;
1801 	nn->rx_coalesce_max_frames = ec->rx_max_coalesced_frames;
1802 	nn->tx_coalesce_usecs      = ec->tx_coalesce_usecs;
1803 	nn->tx_coalesce_max_frames = ec->tx_max_coalesced_frames;
1804 
1805 	/* write configuration to device */
1806 	nfp_net_coalesce_write_cfg(nn);
1807 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
1808 }
1809 
1810 static void nfp_net_get_channels(struct net_device *netdev,
1811 				 struct ethtool_channels *channel)
1812 {
1813 	struct nfp_net *nn = netdev_priv(netdev);
1814 	unsigned int num_tx_rings;
1815 
1816 	num_tx_rings = nn->dp.num_tx_rings;
1817 	if (nn->dp.xdp_prog)
1818 		num_tx_rings -= nn->dp.num_rx_rings;
1819 
1820 	channel->max_rx = min(nn->max_rx_rings, nn->max_r_vecs);
1821 	channel->max_tx = min(nn->max_tx_rings, nn->max_r_vecs);
1822 	channel->max_combined = min(channel->max_rx, channel->max_tx);
1823 	channel->max_other = NFP_NET_NON_Q_VECTORS;
1824 	channel->combined_count = min(nn->dp.num_rx_rings, num_tx_rings);
1825 	channel->rx_count = nn->dp.num_rx_rings - channel->combined_count;
1826 	channel->tx_count = num_tx_rings - channel->combined_count;
1827 	channel->other_count = NFP_NET_NON_Q_VECTORS;
1828 }
1829 
1830 static int nfp_net_set_num_rings(struct nfp_net *nn, unsigned int total_rx,
1831 				 unsigned int total_tx)
1832 {
1833 	struct nfp_net_dp *dp;
1834 
1835 	dp = nfp_net_clone_dp(nn);
1836 	if (!dp)
1837 		return -ENOMEM;
1838 
1839 	dp->num_rx_rings = total_rx;
1840 	dp->num_tx_rings = total_tx;
1841 	/* nfp_net_check_config() will catch num_tx_rings > nn->max_tx_rings */
1842 	if (dp->xdp_prog)
1843 		dp->num_tx_rings += total_rx;
1844 
1845 	return nfp_net_ring_reconfig(nn, dp, NULL);
1846 }
1847 
1848 static int nfp_net_set_channels(struct net_device *netdev,
1849 				struct ethtool_channels *channel)
1850 {
1851 	struct nfp_net *nn = netdev_priv(netdev);
1852 	unsigned int total_rx, total_tx;
1853 
1854 	/* Reject unsupported */
1855 	if (channel->other_count != NFP_NET_NON_Q_VECTORS ||
1856 	    (channel->rx_count && channel->tx_count))
1857 		return -EINVAL;
1858 
1859 	total_rx = channel->combined_count + channel->rx_count;
1860 	total_tx = channel->combined_count + channel->tx_count;
1861 
1862 	if (total_rx > min(nn->max_rx_rings, nn->max_r_vecs) ||
1863 	    total_tx > min(nn->max_tx_rings, nn->max_r_vecs))
1864 		return -EINVAL;
1865 
1866 	return nfp_net_set_num_rings(nn, total_rx, total_tx);
1867 }
1868 
1869 static void nfp_port_get_pauseparam(struct net_device *netdev,
1870 				    struct ethtool_pauseparam *pause)
1871 {
1872 	struct nfp_eth_table_port *eth_port;
1873 	struct nfp_port *port;
1874 
1875 	port = nfp_port_from_netdev(netdev);
1876 	eth_port = nfp_port_get_eth_port(port);
1877 	if (!eth_port)
1878 		return;
1879 
1880 	/* Currently pause frame support is fixed */
1881 	pause->autoneg = AUTONEG_DISABLE;
1882 	pause->rx_pause = 1;
1883 	pause->tx_pause = 1;
1884 }
1885 
1886 static int nfp_net_set_phys_id(struct net_device *netdev,
1887 			       enum ethtool_phys_id_state state)
1888 {
1889 	struct nfp_eth_table_port *eth_port;
1890 	struct nfp_port *port;
1891 	int err;
1892 
1893 	port = nfp_port_from_netdev(netdev);
1894 	eth_port = __nfp_port_get_eth_port(port);
1895 	if (!eth_port)
1896 		return -EOPNOTSUPP;
1897 
1898 	switch (state) {
1899 	case ETHTOOL_ID_ACTIVE:
1900 		/* Control LED to blink */
1901 		err = nfp_eth_set_idmode(port->app->cpp, eth_port->index, 1);
1902 		break;
1903 
1904 	case ETHTOOL_ID_INACTIVE:
1905 		/* Control LED to normal mode */
1906 		err = nfp_eth_set_idmode(port->app->cpp, eth_port->index, 0);
1907 		break;
1908 
1909 	case ETHTOOL_ID_ON:
1910 	case ETHTOOL_ID_OFF:
1911 	default:
1912 		return -EOPNOTSUPP;
1913 	}
1914 
1915 	return err;
1916 }
1917 
1918 #define NFP_EEPROM_LEN ETH_ALEN
1919 
1920 static int
1921 nfp_net_get_eeprom_len(struct net_device *netdev)
1922 {
1923 	struct nfp_eth_table_port *eth_port;
1924 	struct nfp_port *port;
1925 
1926 	port = nfp_port_from_netdev(netdev);
1927 	eth_port = __nfp_port_get_eth_port(port);
1928 	if (!eth_port)
1929 		return 0;
1930 
1931 	return NFP_EEPROM_LEN;
1932 }
1933 
1934 static int
1935 nfp_net_get_nsp_hwindex(struct net_device *netdev,
1936 			struct nfp_nsp **nspptr,
1937 			u32 *index)
1938 {
1939 	struct nfp_eth_table_port *eth_port;
1940 	struct nfp_port *port;
1941 	struct nfp_nsp *nsp;
1942 	int err;
1943 
1944 	port = nfp_port_from_netdev(netdev);
1945 	eth_port = __nfp_port_get_eth_port(port);
1946 	if (!eth_port)
1947 		return -EOPNOTSUPP;
1948 
1949 	nsp = nfp_nsp_open(port->app->cpp);
1950 	if (IS_ERR(nsp)) {
1951 		err = PTR_ERR(nsp);
1952 		netdev_err(netdev, "Failed to access the NSP: %d\n", err);
1953 		return err;
1954 	}
1955 
1956 	if (!nfp_nsp_has_hwinfo_lookup(nsp)) {
1957 		netdev_err(netdev, "NSP doesn't support PF MAC generation\n");
1958 		nfp_nsp_close(nsp);
1959 		return -EOPNOTSUPP;
1960 	}
1961 
1962 	*nspptr = nsp;
1963 	*index = eth_port->eth_index;
1964 
1965 	return 0;
1966 }
1967 
1968 static int
1969 nfp_net_get_port_mac_by_hwinfo(struct net_device *netdev,
1970 			       u8 *mac_addr)
1971 {
1972 	char hwinfo[32] = {};
1973 	struct nfp_nsp *nsp;
1974 	u32 index;
1975 	int err;
1976 
1977 	err = nfp_net_get_nsp_hwindex(netdev, &nsp, &index);
1978 	if (err)
1979 		return err;
1980 
1981 	snprintf(hwinfo, sizeof(hwinfo), "eth%u.mac", index);
1982 	err = nfp_nsp_hwinfo_lookup(nsp, hwinfo, sizeof(hwinfo));
1983 	nfp_nsp_close(nsp);
1984 	if (err) {
1985 		netdev_err(netdev, "Reading persistent MAC address failed: %d\n",
1986 			   err);
1987 		return -EOPNOTSUPP;
1988 	}
1989 
1990 	if (sscanf(hwinfo, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1991 		   &mac_addr[0], &mac_addr[1], &mac_addr[2],
1992 		   &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) {
1993 		netdev_err(netdev, "Can't parse persistent MAC address (%s)\n",
1994 			   hwinfo);
1995 		return -EOPNOTSUPP;
1996 	}
1997 
1998 	return 0;
1999 }
2000 
2001 static int
2002 nfp_net_set_port_mac_by_hwinfo(struct net_device *netdev,
2003 			       u8 *mac_addr)
2004 {
2005 	char hwinfo[32] = {};
2006 	struct nfp_nsp *nsp;
2007 	u32 index;
2008 	int err;
2009 
2010 	err = nfp_net_get_nsp_hwindex(netdev, &nsp, &index);
2011 	if (err)
2012 		return err;
2013 
2014 	snprintf(hwinfo, sizeof(hwinfo),
2015 		 "eth%u.mac=%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
2016 		 index, mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3],
2017 		 mac_addr[4], mac_addr[5]);
2018 
2019 	err = nfp_nsp_hwinfo_set(nsp, hwinfo, sizeof(hwinfo));
2020 	nfp_nsp_close(nsp);
2021 	if (err) {
2022 		netdev_err(netdev, "HWinfo set failed: %d, hwinfo: %s\n",
2023 			   err, hwinfo);
2024 		return -EOPNOTSUPP;
2025 	}
2026 
2027 	return 0;
2028 }
2029 
2030 static int
2031 nfp_net_get_eeprom(struct net_device *netdev,
2032 		   struct ethtool_eeprom *eeprom, u8 *bytes)
2033 {
2034 	struct nfp_app *app = nfp_app_from_netdev(netdev);
2035 	u8 buf[NFP_EEPROM_LEN] = {};
2036 
2037 	if (nfp_net_get_port_mac_by_hwinfo(netdev, buf))
2038 		return -EOPNOTSUPP;
2039 
2040 	if (eeprom->len == 0)
2041 		return -EINVAL;
2042 
2043 	eeprom->magic = app->pdev->vendor | (app->pdev->device << 16);
2044 	memcpy(bytes, buf + eeprom->offset, eeprom->len);
2045 
2046 	return 0;
2047 }
2048 
2049 static int
2050 nfp_net_set_eeprom(struct net_device *netdev,
2051 		   struct ethtool_eeprom *eeprom, u8 *bytes)
2052 {
2053 	struct nfp_app *app = nfp_app_from_netdev(netdev);
2054 	u8 buf[NFP_EEPROM_LEN] = {};
2055 
2056 	if (nfp_net_get_port_mac_by_hwinfo(netdev, buf))
2057 		return -EOPNOTSUPP;
2058 
2059 	if (eeprom->len == 0)
2060 		return -EINVAL;
2061 
2062 	if (eeprom->magic != (app->pdev->vendor | app->pdev->device << 16))
2063 		return -EINVAL;
2064 
2065 	memcpy(buf + eeprom->offset, bytes, eeprom->len);
2066 	if (nfp_net_set_port_mac_by_hwinfo(netdev, buf))
2067 		return -EOPNOTSUPP;
2068 
2069 	return 0;
2070 }
2071 
2072 static const struct ethtool_ops nfp_net_ethtool_ops = {
2073 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
2074 				     ETHTOOL_COALESCE_MAX_FRAMES |
2075 				     ETHTOOL_COALESCE_USE_ADAPTIVE,
2076 	.get_drvinfo		= nfp_net_get_drvinfo,
2077 	.nway_reset             = nfp_net_nway_reset,
2078 	.get_link		= ethtool_op_get_link,
2079 	.get_ringparam		= nfp_net_get_ringparam,
2080 	.set_ringparam		= nfp_net_set_ringparam,
2081 	.self_test		= nfp_net_self_test,
2082 	.get_strings		= nfp_net_get_strings,
2083 	.get_ethtool_stats	= nfp_net_get_stats,
2084 	.get_sset_count		= nfp_net_get_sset_count,
2085 	.get_rxnfc		= nfp_net_get_rxnfc,
2086 	.set_rxnfc		= nfp_net_set_rxnfc,
2087 	.get_rxfh_indir_size	= nfp_net_get_rxfh_indir_size,
2088 	.get_rxfh_key_size	= nfp_net_get_rxfh_key_size,
2089 	.get_rxfh		= nfp_net_get_rxfh,
2090 	.set_rxfh		= nfp_net_set_rxfh,
2091 	.get_regs_len		= nfp_net_get_regs_len,
2092 	.get_regs		= nfp_net_get_regs,
2093 	.set_dump		= nfp_app_set_dump,
2094 	.get_dump_flag		= nfp_app_get_dump_flag,
2095 	.get_dump_data		= nfp_app_get_dump_data,
2096 	.get_eeprom_len         = nfp_net_get_eeprom_len,
2097 	.get_eeprom             = nfp_net_get_eeprom,
2098 	.set_eeprom             = nfp_net_set_eeprom,
2099 	.get_module_info	= nfp_port_get_module_info,
2100 	.get_module_eeprom	= nfp_port_get_module_eeprom,
2101 	.get_coalesce           = nfp_net_get_coalesce,
2102 	.set_coalesce           = nfp_net_set_coalesce,
2103 	.get_channels		= nfp_net_get_channels,
2104 	.set_channels		= nfp_net_set_channels,
2105 	.get_link_ksettings	= nfp_net_get_link_ksettings,
2106 	.set_link_ksettings	= nfp_net_set_link_ksettings,
2107 	.get_fecparam		= nfp_port_get_fecparam,
2108 	.set_fecparam		= nfp_port_set_fecparam,
2109 	.get_pauseparam		= nfp_port_get_pauseparam,
2110 	.set_phys_id		= nfp_net_set_phys_id,
2111 };
2112 
2113 const struct ethtool_ops nfp_port_ethtool_ops = {
2114 	.get_drvinfo		= nfp_app_get_drvinfo,
2115 	.nway_reset             = nfp_net_nway_reset,
2116 	.get_link		= ethtool_op_get_link,
2117 	.get_strings		= nfp_port_get_strings,
2118 	.get_ethtool_stats	= nfp_port_get_stats,
2119 	.self_test		= nfp_net_self_test,
2120 	.get_sset_count		= nfp_port_get_sset_count,
2121 	.set_dump		= nfp_app_set_dump,
2122 	.get_dump_flag		= nfp_app_get_dump_flag,
2123 	.get_dump_data		= nfp_app_get_dump_data,
2124 	.get_eeprom_len         = nfp_net_get_eeprom_len,
2125 	.get_eeprom             = nfp_net_get_eeprom,
2126 	.set_eeprom             = nfp_net_set_eeprom,
2127 	.get_module_info	= nfp_port_get_module_info,
2128 	.get_module_eeprom	= nfp_port_get_module_eeprom,
2129 	.get_link_ksettings	= nfp_net_get_link_ksettings,
2130 	.set_link_ksettings	= nfp_net_set_link_ksettings,
2131 	.get_fecparam		= nfp_port_get_fecparam,
2132 	.set_fecparam		= nfp_port_set_fecparam,
2133 	.get_pauseparam		= nfp_port_get_pauseparam,
2134 	.set_phys_id		= nfp_net_set_phys_id,
2135 };
2136 
2137 void nfp_net_set_ethtool_ops(struct net_device *netdev)
2138 {
2139 	netdev->ethtool_ops = &nfp_net_ethtool_ops;
2140 }
2141