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 u16 nfp_eth_media_table[] = {
297 	[NFP_MEDIA_1000BASE_CX]		= ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
298 	[NFP_MEDIA_1000BASE_KX]		= ETHTOOL_LINK_MODE_1000baseKX_Full_BIT,
299 	[NFP_MEDIA_10GBASE_KX4]		= ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
300 	[NFP_MEDIA_10GBASE_KR]		= ETHTOOL_LINK_MODE_10000baseKR_Full_BIT,
301 	[NFP_MEDIA_10GBASE_CX4]		= ETHTOOL_LINK_MODE_10000baseKX4_Full_BIT,
302 	[NFP_MEDIA_10GBASE_CR]		= ETHTOOL_LINK_MODE_10000baseCR_Full_BIT,
303 	[NFP_MEDIA_10GBASE_SR]		= ETHTOOL_LINK_MODE_10000baseSR_Full_BIT,
304 	[NFP_MEDIA_10GBASE_ER]		= ETHTOOL_LINK_MODE_10000baseER_Full_BIT,
305 	[NFP_MEDIA_25GBASE_KR]		= ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
306 	[NFP_MEDIA_25GBASE_KR_S]	= ETHTOOL_LINK_MODE_25000baseKR_Full_BIT,
307 	[NFP_MEDIA_25GBASE_CR]		= ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
308 	[NFP_MEDIA_25GBASE_CR_S]	= ETHTOOL_LINK_MODE_25000baseCR_Full_BIT,
309 	[NFP_MEDIA_25GBASE_SR]		= ETHTOOL_LINK_MODE_25000baseSR_Full_BIT,
310 	[NFP_MEDIA_40GBASE_CR4]		= ETHTOOL_LINK_MODE_40000baseCR4_Full_BIT,
311 	[NFP_MEDIA_40GBASE_KR4]		= ETHTOOL_LINK_MODE_40000baseKR4_Full_BIT,
312 	[NFP_MEDIA_40GBASE_SR4]		= ETHTOOL_LINK_MODE_40000baseSR4_Full_BIT,
313 	[NFP_MEDIA_40GBASE_LR4]		= ETHTOOL_LINK_MODE_40000baseLR4_Full_BIT,
314 	[NFP_MEDIA_50GBASE_KR]		= ETHTOOL_LINK_MODE_50000baseKR_Full_BIT,
315 	[NFP_MEDIA_50GBASE_SR]		= ETHTOOL_LINK_MODE_50000baseSR_Full_BIT,
316 	[NFP_MEDIA_50GBASE_CR]		= ETHTOOL_LINK_MODE_50000baseCR_Full_BIT,
317 	[NFP_MEDIA_50GBASE_LR]		= ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
318 	[NFP_MEDIA_50GBASE_ER]		= ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
319 	[NFP_MEDIA_50GBASE_FR]		= ETHTOOL_LINK_MODE_50000baseLR_ER_FR_Full_BIT,
320 	[NFP_MEDIA_100GBASE_KR4]	= ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
321 	[NFP_MEDIA_100GBASE_SR4]	= ETHTOOL_LINK_MODE_100000baseSR4_Full_BIT,
322 	[NFP_MEDIA_100GBASE_CR4]	= ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
323 	[NFP_MEDIA_100GBASE_KP4]	= ETHTOOL_LINK_MODE_100000baseKR4_Full_BIT,
324 	[NFP_MEDIA_100GBASE_CR10]	= ETHTOOL_LINK_MODE_100000baseCR4_Full_BIT,
325 };
326 
327 static void nfp_add_media_link_mode(struct nfp_port *port,
328 				    struct nfp_eth_table_port *eth_port,
329 				    struct ethtool_link_ksettings *cmd)
330 {
331 	u64 supported_modes[2], advertised_modes[2];
332 	struct nfp_eth_media_buf ethm = {
333 		.eth_index = eth_port->eth_index,
334 	};
335 	struct nfp_cpp *cpp = port->app->cpp;
336 
337 	if (nfp_eth_read_media(cpp, &ethm))
338 		return;
339 
340 	for (u32 i = 0; i < 2; i++) {
341 		supported_modes[i] = le64_to_cpu(ethm.supported_modes[i]);
342 		advertised_modes[i] = le64_to_cpu(ethm.advertised_modes[i]);
343 	}
344 
345 	for (u32 i = 0; i < NFP_MEDIA_LINK_MODES_NUMBER; i++) {
346 		if (i < 64) {
347 			if (supported_modes[0] & BIT_ULL(i))
348 				__set_bit(nfp_eth_media_table[i],
349 					  cmd->link_modes.supported);
350 
351 			if (advertised_modes[0] & BIT_ULL(i))
352 				__set_bit(nfp_eth_media_table[i],
353 					  cmd->link_modes.advertising);
354 		} else {
355 			if (supported_modes[1] & BIT_ULL(i - 64))
356 				__set_bit(nfp_eth_media_table[i],
357 					  cmd->link_modes.supported);
358 
359 			if (advertised_modes[1] & BIT_ULL(i - 64))
360 				__set_bit(nfp_eth_media_table[i],
361 					  cmd->link_modes.advertising);
362 		}
363 	}
364 }
365 
366 /**
367  * nfp_net_get_link_ksettings - Get Link Speed settings
368  * @netdev:	network interface device structure
369  * @cmd:	ethtool command
370  *
371  * Reports speed settings based on info in the BAR provided by the fw.
372  */
373 static int
374 nfp_net_get_link_ksettings(struct net_device *netdev,
375 			   struct ethtool_link_ksettings *cmd)
376 {
377 	struct nfp_eth_table_port *eth_port;
378 	struct nfp_port *port;
379 	struct nfp_net *nn;
380 	unsigned int speed;
381 	u16 sts;
382 
383 	/* Init to unknowns */
384 	ethtool_link_ksettings_zero_link_mode(cmd, supported);
385 	ethtool_link_ksettings_zero_link_mode(cmd, advertising);
386 	ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
387 	cmd->base.port = PORT_OTHER;
388 	cmd->base.speed = SPEED_UNKNOWN;
389 	cmd->base.duplex = DUPLEX_UNKNOWN;
390 
391 	port = nfp_port_from_netdev(netdev);
392 	eth_port = nfp_port_get_eth_port(port);
393 	if (eth_port) {
394 		ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
395 		ethtool_link_ksettings_add_link_mode(cmd, advertising, Pause);
396 		nfp_add_media_link_mode(port, eth_port, cmd);
397 		if (eth_port->supp_aneg) {
398 			ethtool_link_ksettings_add_link_mode(cmd, supported, Autoneg);
399 			if (eth_port->aneg == NFP_ANEG_AUTO) {
400 				ethtool_link_ksettings_add_link_mode(cmd, advertising, Autoneg);
401 				cmd->base.autoneg = AUTONEG_ENABLE;
402 			}
403 		}
404 		nfp_net_set_fec_link_mode(eth_port, cmd);
405 	}
406 
407 	if (!netif_carrier_ok(netdev))
408 		return 0;
409 
410 	/* Use link speed from ETH table if available, otherwise try the BAR */
411 	if (eth_port) {
412 		cmd->base.port = eth_port->port_type;
413 		cmd->base.speed = eth_port->speed;
414 		cmd->base.duplex = DUPLEX_FULL;
415 		return 0;
416 	}
417 
418 	if (!nfp_netdev_is_nfp_net(netdev))
419 		return -EOPNOTSUPP;
420 	nn = netdev_priv(netdev);
421 
422 	sts = nn_readw(nn, NFP_NET_CFG_STS);
423 	speed = nfp_net_lr2speed(FIELD_GET(NFP_NET_CFG_STS_LINK_RATE, sts));
424 	if (!speed)
425 		return -EOPNOTSUPP;
426 
427 	if (speed != SPEED_UNKNOWN) {
428 		cmd->base.speed = speed;
429 		cmd->base.duplex = DUPLEX_FULL;
430 	}
431 
432 	return 0;
433 }
434 
435 static int
436 nfp_net_set_link_ksettings(struct net_device *netdev,
437 			   const struct ethtool_link_ksettings *cmd)
438 {
439 	bool req_aneg = (cmd->base.autoneg == AUTONEG_ENABLE);
440 	struct nfp_eth_table_port *eth_port;
441 	struct nfp_port *port;
442 	struct nfp_nsp *nsp;
443 	int err;
444 
445 	port = nfp_port_from_netdev(netdev);
446 	eth_port = __nfp_port_get_eth_port(port);
447 	if (!eth_port)
448 		return -EOPNOTSUPP;
449 
450 	if (netif_running(netdev)) {
451 		netdev_warn(netdev, "Changing settings not allowed on an active interface. It may cause the port to be disabled until driver reload.\n");
452 		return -EBUSY;
453 	}
454 
455 	nsp = nfp_eth_config_start(port->app->cpp, eth_port->index);
456 	if (IS_ERR(nsp))
457 		return PTR_ERR(nsp);
458 
459 	if (req_aneg && !eth_port->supp_aneg) {
460 		netdev_warn(netdev, "Autoneg is not supported.\n");
461 		err = -EOPNOTSUPP;
462 		goto err_bad_set;
463 	}
464 
465 	err = __nfp_eth_set_aneg(nsp, req_aneg ? NFP_ANEG_AUTO : NFP_ANEG_DISABLED);
466 	if (err)
467 		goto err_bad_set;
468 
469 	if (cmd->base.speed != SPEED_UNKNOWN) {
470 		u32 speed = cmd->base.speed / eth_port->lanes;
471 
472 		if (req_aneg) {
473 			netdev_err(netdev, "Speed changing is not allowed when working on autoneg mode.\n");
474 			err = -EINVAL;
475 			goto err_bad_set;
476 		}
477 
478 		err = __nfp_eth_set_speed(nsp, speed);
479 		if (err)
480 			goto err_bad_set;
481 	}
482 
483 	err = nfp_eth_config_commit_end(nsp);
484 	if (err > 0)
485 		return 0; /* no change */
486 
487 	nfp_net_refresh_port_table(port);
488 
489 	return err;
490 
491 err_bad_set:
492 	nfp_eth_config_cleanup_end(nsp);
493 	return err;
494 }
495 
496 static void nfp_net_get_ringparam(struct net_device *netdev,
497 				  struct ethtool_ringparam *ring,
498 				  struct kernel_ethtool_ringparam *kernel_ring,
499 				  struct netlink_ext_ack *extack)
500 {
501 	struct nfp_net *nn = netdev_priv(netdev);
502 	u32 qc_max = nn->dev_info->max_qc_size;
503 
504 	ring->rx_max_pending = qc_max;
505 	ring->tx_max_pending = qc_max / nn->dp.ops->tx_min_desc_per_pkt;
506 	ring->rx_pending = nn->dp.rxd_cnt;
507 	ring->tx_pending = nn->dp.txd_cnt;
508 }
509 
510 static int nfp_net_set_ring_size(struct nfp_net *nn, u32 rxd_cnt, u32 txd_cnt)
511 {
512 	struct nfp_net_dp *dp;
513 
514 	dp = nfp_net_clone_dp(nn);
515 	if (!dp)
516 		return -ENOMEM;
517 
518 	dp->rxd_cnt = rxd_cnt;
519 	dp->txd_cnt = txd_cnt;
520 
521 	return nfp_net_ring_reconfig(nn, dp, NULL);
522 }
523 
524 static int nfp_net_set_ringparam(struct net_device *netdev,
525 				 struct ethtool_ringparam *ring,
526 				 struct kernel_ethtool_ringparam *kernel_ring,
527 				 struct netlink_ext_ack *extack)
528 {
529 	u32 tx_dpp, qc_min, qc_max, rxd_cnt, txd_cnt;
530 	struct nfp_net *nn = netdev_priv(netdev);
531 
532 	/* We don't have separate queues/rings for small/large frames. */
533 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
534 		return -EINVAL;
535 
536 	qc_min = nn->dev_info->min_qc_size;
537 	qc_max = nn->dev_info->max_qc_size;
538 	tx_dpp = nn->dp.ops->tx_min_desc_per_pkt;
539 	/* Round up to supported values */
540 	rxd_cnt = roundup_pow_of_two(ring->rx_pending);
541 	txd_cnt = roundup_pow_of_two(ring->tx_pending);
542 
543 	if (rxd_cnt < qc_min || rxd_cnt > qc_max ||
544 	    txd_cnt < qc_min / tx_dpp || txd_cnt > qc_max / tx_dpp)
545 		return -EINVAL;
546 
547 	if (nn->dp.rxd_cnt == rxd_cnt && nn->dp.txd_cnt == txd_cnt)
548 		return 0;
549 
550 	nn_dbg(nn, "Change ring size: RxQ %u->%u, TxQ %u->%u\n",
551 	       nn->dp.rxd_cnt, rxd_cnt, nn->dp.txd_cnt, txd_cnt);
552 
553 	return nfp_net_set_ring_size(nn, rxd_cnt, txd_cnt);
554 }
555 
556 static int nfp_test_link(struct net_device *netdev)
557 {
558 	if (!netif_carrier_ok(netdev) || !(netdev->flags & IFF_UP))
559 		return 1;
560 
561 	return 0;
562 }
563 
564 static int nfp_test_nsp(struct net_device *netdev)
565 {
566 	struct nfp_app *app = nfp_app_from_netdev(netdev);
567 	struct nfp_nsp_identify *nspi;
568 	struct nfp_nsp *nsp;
569 	int err;
570 
571 	nsp = nfp_nsp_open(app->cpp);
572 	if (IS_ERR(nsp)) {
573 		err = PTR_ERR(nsp);
574 		netdev_info(netdev, "NSP Test: failed to access the NSP: %d\n", err);
575 		goto exit;
576 	}
577 
578 	if (nfp_nsp_get_abi_ver_minor(nsp) < 15) {
579 		err = -EOPNOTSUPP;
580 		goto exit_close_nsp;
581 	}
582 
583 	nspi = kzalloc(sizeof(*nspi), GFP_KERNEL);
584 	if (!nspi) {
585 		err = -ENOMEM;
586 		goto exit_close_nsp;
587 	}
588 
589 	err = nfp_nsp_read_identify(nsp, nspi, sizeof(*nspi));
590 	if (err < 0)
591 		netdev_info(netdev, "NSP Test: reading bsp version failed %d\n", err);
592 
593 	kfree(nspi);
594 exit_close_nsp:
595 	nfp_nsp_close(nsp);
596 exit:
597 	return err;
598 }
599 
600 static int nfp_test_fw(struct net_device *netdev)
601 {
602 	struct nfp_net *nn = netdev_priv(netdev);
603 	int err;
604 
605 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_GEN);
606 	if (err)
607 		netdev_info(netdev, "FW Test: update failed %d\n", err);
608 
609 	return err;
610 }
611 
612 static int nfp_test_reg(struct net_device *netdev)
613 {
614 	struct nfp_app *app = nfp_app_from_netdev(netdev);
615 	struct nfp_cpp *cpp = app->cpp;
616 	u32 model = nfp_cpp_model(cpp);
617 	u32 value;
618 	int err;
619 
620 	err = nfp_cpp_model_autodetect(cpp, &value);
621 	if (err < 0) {
622 		netdev_info(netdev, "REG Test: NFP model detection failed %d\n", err);
623 		return err;
624 	}
625 
626 	return (value == model) ? 0 : 1;
627 }
628 
629 static bool link_test_supported(struct net_device *netdev)
630 {
631 	return true;
632 }
633 
634 static bool nsp_test_supported(struct net_device *netdev)
635 {
636 	if (nfp_app_from_netdev(netdev))
637 		return true;
638 
639 	return false;
640 }
641 
642 static bool fw_test_supported(struct net_device *netdev)
643 {
644 	if (nfp_netdev_is_nfp_net(netdev))
645 		return true;
646 
647 	return false;
648 }
649 
650 static bool reg_test_supported(struct net_device *netdev)
651 {
652 	if (nfp_app_from_netdev(netdev))
653 		return true;
654 
655 	return false;
656 }
657 
658 static struct nfp_self_test_item {
659 	char name[ETH_GSTRING_LEN];
660 	bool (*is_supported)(struct net_device *dev);
661 	int (*func)(struct net_device *dev);
662 } nfp_self_test[] = {
663 	{"Link Test", link_test_supported, nfp_test_link},
664 	{"NSP Test", nsp_test_supported, nfp_test_nsp},
665 	{"Firmware Test", fw_test_supported, nfp_test_fw},
666 	{"Register Test", reg_test_supported, nfp_test_reg}
667 };
668 
669 #define NFP_TEST_TOTAL_NUM ARRAY_SIZE(nfp_self_test)
670 
671 static void nfp_get_self_test_strings(struct net_device *netdev, u8 *data)
672 {
673 	int i;
674 
675 	for (i = 0; i < NFP_TEST_TOTAL_NUM; i++)
676 		if (nfp_self_test[i].is_supported(netdev))
677 			ethtool_sprintf(&data, nfp_self_test[i].name);
678 }
679 
680 static int nfp_get_self_test_count(struct net_device *netdev)
681 {
682 	int i, count = 0;
683 
684 	for (i = 0; i < NFP_TEST_TOTAL_NUM; i++)
685 		if (nfp_self_test[i].is_supported(netdev))
686 			count++;
687 
688 	return count;
689 }
690 
691 static void nfp_net_self_test(struct net_device *netdev, struct ethtool_test *eth_test,
692 			      u64 *data)
693 {
694 	int i, ret, count = 0;
695 
696 	netdev_info(netdev, "Start self test\n");
697 
698 	for (i = 0; i < NFP_TEST_TOTAL_NUM; i++) {
699 		if (nfp_self_test[i].is_supported(netdev)) {
700 			ret = nfp_self_test[i].func(netdev);
701 			if (ret)
702 				eth_test->flags |= ETH_TEST_FL_FAILED;
703 			data[count++] = ret;
704 		}
705 	}
706 
707 	netdev_info(netdev, "Test end\n");
708 }
709 
710 static unsigned int nfp_vnic_get_sw_stats_count(struct net_device *netdev)
711 {
712 	struct nfp_net *nn = netdev_priv(netdev);
713 
714 	return NN_RVEC_GATHER_STATS + nn->max_r_vecs * NN_RVEC_PER_Q_STATS +
715 		NN_CTRL_PATH_STATS;
716 }
717 
718 static u8 *nfp_vnic_get_sw_stats_strings(struct net_device *netdev, u8 *data)
719 {
720 	struct nfp_net *nn = netdev_priv(netdev);
721 	int i;
722 
723 	for (i = 0; i < nn->max_r_vecs; i++) {
724 		ethtool_sprintf(&data, "rvec_%u_rx_pkts", i);
725 		ethtool_sprintf(&data, "rvec_%u_tx_pkts", i);
726 		ethtool_sprintf(&data, "rvec_%u_tx_busy", i);
727 	}
728 
729 	ethtool_sprintf(&data, "hw_rx_csum_ok");
730 	ethtool_sprintf(&data, "hw_rx_csum_inner_ok");
731 	ethtool_sprintf(&data, "hw_rx_csum_complete");
732 	ethtool_sprintf(&data, "hw_rx_csum_err");
733 	ethtool_sprintf(&data, "rx_replace_buf_alloc_fail");
734 	ethtool_sprintf(&data, "rx_tls_decrypted_packets");
735 	ethtool_sprintf(&data, "hw_tx_csum");
736 	ethtool_sprintf(&data, "hw_tx_inner_csum");
737 	ethtool_sprintf(&data, "tx_gather");
738 	ethtool_sprintf(&data, "tx_lso");
739 	ethtool_sprintf(&data, "tx_tls_encrypted_packets");
740 	ethtool_sprintf(&data, "tx_tls_ooo");
741 	ethtool_sprintf(&data, "tx_tls_drop_no_sync_data");
742 
743 	ethtool_sprintf(&data, "hw_tls_no_space");
744 	ethtool_sprintf(&data, "rx_tls_resync_req_ok");
745 	ethtool_sprintf(&data, "rx_tls_resync_req_ign");
746 	ethtool_sprintf(&data, "rx_tls_resync_sent");
747 
748 	return data;
749 }
750 
751 static u64 *nfp_vnic_get_sw_stats(struct net_device *netdev, u64 *data)
752 {
753 	u64 gathered_stats[NN_RVEC_GATHER_STATS] = {};
754 	struct nfp_net *nn = netdev_priv(netdev);
755 	u64 tmp[NN_RVEC_GATHER_STATS];
756 	unsigned int i, j;
757 
758 	for (i = 0; i < nn->max_r_vecs; i++) {
759 		unsigned int start;
760 
761 		do {
762 			start = u64_stats_fetch_begin(&nn->r_vecs[i].rx_sync);
763 			data[0] = nn->r_vecs[i].rx_pkts;
764 			tmp[0] = nn->r_vecs[i].hw_csum_rx_ok;
765 			tmp[1] = nn->r_vecs[i].hw_csum_rx_inner_ok;
766 			tmp[2] = nn->r_vecs[i].hw_csum_rx_complete;
767 			tmp[3] = nn->r_vecs[i].hw_csum_rx_error;
768 			tmp[4] = nn->r_vecs[i].rx_replace_buf_alloc_fail;
769 			tmp[5] = nn->r_vecs[i].hw_tls_rx;
770 		} while (u64_stats_fetch_retry(&nn->r_vecs[i].rx_sync, start));
771 
772 		do {
773 			start = u64_stats_fetch_begin(&nn->r_vecs[i].tx_sync);
774 			data[1] = nn->r_vecs[i].tx_pkts;
775 			data[2] = nn->r_vecs[i].tx_busy;
776 			tmp[6] = nn->r_vecs[i].hw_csum_tx;
777 			tmp[7] = nn->r_vecs[i].hw_csum_tx_inner;
778 			tmp[8] = nn->r_vecs[i].tx_gather;
779 			tmp[9] = nn->r_vecs[i].tx_lso;
780 			tmp[10] = nn->r_vecs[i].hw_tls_tx;
781 			tmp[11] = nn->r_vecs[i].tls_tx_fallback;
782 			tmp[12] = nn->r_vecs[i].tls_tx_no_fallback;
783 		} while (u64_stats_fetch_retry(&nn->r_vecs[i].tx_sync, start));
784 
785 		data += NN_RVEC_PER_Q_STATS;
786 
787 		for (j = 0; j < NN_RVEC_GATHER_STATS; j++)
788 			gathered_stats[j] += tmp[j];
789 	}
790 
791 	for (j = 0; j < NN_RVEC_GATHER_STATS; j++)
792 		*data++ = gathered_stats[j];
793 
794 	*data++ = atomic_read(&nn->ktls_no_space);
795 	*data++ = atomic_read(&nn->ktls_rx_resync_req);
796 	*data++ = atomic_read(&nn->ktls_rx_resync_ign);
797 	*data++ = atomic_read(&nn->ktls_rx_resync_sent);
798 
799 	return data;
800 }
801 
802 static unsigned int nfp_vnic_get_hw_stats_count(unsigned int num_vecs)
803 {
804 	return NN_ET_GLOBAL_STATS_LEN + num_vecs * 4;
805 }
806 
807 static u8 *
808 nfp_vnic_get_hw_stats_strings(u8 *data, unsigned int num_vecs, bool repr)
809 {
810 	int swap_off, i;
811 
812 	BUILD_BUG_ON(NN_ET_GLOBAL_STATS_LEN < NN_ET_SWITCH_STATS_LEN * 2);
813 	/* If repr is true first add SWITCH_STATS_LEN and then subtract it
814 	 * effectively swapping the RX and TX statistics (giving us the RX
815 	 * and TX from perspective of the switch).
816 	 */
817 	swap_off = repr * NN_ET_SWITCH_STATS_LEN;
818 
819 	for (i = 0; i < NN_ET_SWITCH_STATS_LEN; i++)
820 		ethtool_sprintf(&data, nfp_net_et_stats[i + swap_off].name);
821 
822 	for (i = NN_ET_SWITCH_STATS_LEN; i < NN_ET_SWITCH_STATS_LEN * 2; i++)
823 		ethtool_sprintf(&data, nfp_net_et_stats[i - swap_off].name);
824 
825 	for (i = NN_ET_SWITCH_STATS_LEN * 2; i < NN_ET_GLOBAL_STATS_LEN; i++)
826 		ethtool_sprintf(&data, nfp_net_et_stats[i].name);
827 
828 	for (i = 0; i < num_vecs; i++) {
829 		ethtool_sprintf(&data, "rxq_%u_pkts", i);
830 		ethtool_sprintf(&data, "rxq_%u_bytes", i);
831 		ethtool_sprintf(&data, "txq_%u_pkts", i);
832 		ethtool_sprintf(&data, "txq_%u_bytes", i);
833 	}
834 
835 	return data;
836 }
837 
838 static u64 *
839 nfp_vnic_get_hw_stats(u64 *data, u8 __iomem *mem, unsigned int num_vecs)
840 {
841 	unsigned int i;
842 
843 	for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++)
844 		*data++ = readq(mem + nfp_net_et_stats[i].off);
845 
846 	for (i = 0; i < num_vecs; i++) {
847 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i));
848 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i) + 8);
849 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i));
850 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i) + 8);
851 	}
852 
853 	return data;
854 }
855 
856 static unsigned int nfp_vnic_get_tlv_stats_count(struct nfp_net *nn)
857 {
858 	return nn->tlv_caps.vnic_stats_cnt + nn->max_r_vecs * 4;
859 }
860 
861 static u8 *nfp_vnic_get_tlv_stats_strings(struct nfp_net *nn, u8 *data)
862 {
863 	unsigned int i, id;
864 	u8 __iomem *mem;
865 	u64 id_word = 0;
866 
867 	mem = nn->dp.ctrl_bar + nn->tlv_caps.vnic_stats_off;
868 	for (i = 0; i < nn->tlv_caps.vnic_stats_cnt; i++) {
869 		if (!(i % 4))
870 			id_word = readq(mem + i * 2);
871 
872 		id = (u16)id_word;
873 		id_word >>= 16;
874 
875 		if (id < ARRAY_SIZE(nfp_tlv_stat_names) &&
876 		    nfp_tlv_stat_names[id][0]) {
877 			memcpy(data, nfp_tlv_stat_names[id], ETH_GSTRING_LEN);
878 			data += ETH_GSTRING_LEN;
879 		} else {
880 			ethtool_sprintf(&data, "dev_unknown_stat%u", id);
881 		}
882 	}
883 
884 	for (i = 0; i < nn->max_r_vecs; i++) {
885 		ethtool_sprintf(&data, "rxq_%u_pkts", i);
886 		ethtool_sprintf(&data, "rxq_%u_bytes", i);
887 		ethtool_sprintf(&data, "txq_%u_pkts", i);
888 		ethtool_sprintf(&data, "txq_%u_bytes", i);
889 	}
890 
891 	return data;
892 }
893 
894 static u64 *nfp_vnic_get_tlv_stats(struct nfp_net *nn, u64 *data)
895 {
896 	u8 __iomem *mem;
897 	unsigned int i;
898 
899 	mem = nn->dp.ctrl_bar + nn->tlv_caps.vnic_stats_off;
900 	mem += roundup(2 * nn->tlv_caps.vnic_stats_cnt, 8);
901 	for (i = 0; i < nn->tlv_caps.vnic_stats_cnt; i++)
902 		*data++ = readq(mem + i * 8);
903 
904 	mem = nn->dp.ctrl_bar;
905 	for (i = 0; i < nn->max_r_vecs; i++) {
906 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i));
907 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i) + 8);
908 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i));
909 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i) + 8);
910 	}
911 
912 	return data;
913 }
914 
915 static unsigned int nfp_mac_get_stats_count(struct net_device *netdev)
916 {
917 	struct nfp_port *port;
918 
919 	port = nfp_port_from_netdev(netdev);
920 	if (!__nfp_port_get_eth_port(port) || !port->eth_stats)
921 		return 0;
922 
923 	return ARRAY_SIZE(nfp_mac_et_stats);
924 }
925 
926 static u8 *nfp_mac_get_stats_strings(struct net_device *netdev, u8 *data)
927 {
928 	struct nfp_port *port;
929 	unsigned int i;
930 
931 	port = nfp_port_from_netdev(netdev);
932 	if (!__nfp_port_get_eth_port(port) || !port->eth_stats)
933 		return data;
934 
935 	for (i = 0; i < ARRAY_SIZE(nfp_mac_et_stats); i++)
936 		ethtool_sprintf(&data, "mac.%s", nfp_mac_et_stats[i].name);
937 
938 	return data;
939 }
940 
941 static u64 *nfp_mac_get_stats(struct net_device *netdev, u64 *data)
942 {
943 	struct nfp_port *port;
944 	unsigned int i;
945 
946 	port = nfp_port_from_netdev(netdev);
947 	if (!__nfp_port_get_eth_port(port) || !port->eth_stats)
948 		return data;
949 
950 	for (i = 0; i < ARRAY_SIZE(nfp_mac_et_stats); i++)
951 		*data++ = readq(port->eth_stats + nfp_mac_et_stats[i].off);
952 
953 	return data;
954 }
955 
956 static void nfp_net_get_strings(struct net_device *netdev,
957 				u32 stringset, u8 *data)
958 {
959 	struct nfp_net *nn = netdev_priv(netdev);
960 
961 	switch (stringset) {
962 	case ETH_SS_STATS:
963 		data = nfp_vnic_get_sw_stats_strings(netdev, data);
964 		if (!nn->tlv_caps.vnic_stats_off)
965 			data = nfp_vnic_get_hw_stats_strings(data,
966 							     nn->max_r_vecs,
967 							     false);
968 		else
969 			data = nfp_vnic_get_tlv_stats_strings(nn, data);
970 		data = nfp_mac_get_stats_strings(netdev, data);
971 		data = nfp_app_port_get_stats_strings(nn->port, data);
972 		break;
973 	case ETH_SS_TEST:
974 		nfp_get_self_test_strings(netdev, data);
975 		break;
976 	}
977 }
978 
979 static void
980 nfp_net_get_stats(struct net_device *netdev, struct ethtool_stats *stats,
981 		  u64 *data)
982 {
983 	struct nfp_net *nn = netdev_priv(netdev);
984 
985 	data = nfp_vnic_get_sw_stats(netdev, data);
986 	if (!nn->tlv_caps.vnic_stats_off)
987 		data = nfp_vnic_get_hw_stats(data, nn->dp.ctrl_bar,
988 					     nn->max_r_vecs);
989 	else
990 		data = nfp_vnic_get_tlv_stats(nn, data);
991 	data = nfp_mac_get_stats(netdev, data);
992 	data = nfp_app_port_get_stats(nn->port, data);
993 }
994 
995 static int nfp_net_get_sset_count(struct net_device *netdev, int sset)
996 {
997 	struct nfp_net *nn = netdev_priv(netdev);
998 	unsigned int cnt;
999 
1000 	switch (sset) {
1001 	case ETH_SS_STATS:
1002 		cnt = nfp_vnic_get_sw_stats_count(netdev);
1003 		if (!nn->tlv_caps.vnic_stats_off)
1004 			cnt += nfp_vnic_get_hw_stats_count(nn->max_r_vecs);
1005 		else
1006 			cnt += nfp_vnic_get_tlv_stats_count(nn);
1007 		cnt += nfp_mac_get_stats_count(netdev);
1008 		cnt += nfp_app_port_get_stats_count(nn->port);
1009 		return cnt;
1010 	case ETH_SS_TEST:
1011 		return nfp_get_self_test_count(netdev);
1012 	default:
1013 		return -EOPNOTSUPP;
1014 	}
1015 }
1016 
1017 static void nfp_port_get_strings(struct net_device *netdev,
1018 				 u32 stringset, u8 *data)
1019 {
1020 	struct nfp_port *port = nfp_port_from_netdev(netdev);
1021 
1022 	switch (stringset) {
1023 	case ETH_SS_STATS:
1024 		if (nfp_port_is_vnic(port))
1025 			data = nfp_vnic_get_hw_stats_strings(data, 0, true);
1026 		else
1027 			data = nfp_mac_get_stats_strings(netdev, data);
1028 		data = nfp_app_port_get_stats_strings(port, data);
1029 		break;
1030 	case ETH_SS_TEST:
1031 		nfp_get_self_test_strings(netdev, data);
1032 		break;
1033 	}
1034 }
1035 
1036 static void
1037 nfp_port_get_stats(struct net_device *netdev, struct ethtool_stats *stats,
1038 		   u64 *data)
1039 {
1040 	struct nfp_port *port = nfp_port_from_netdev(netdev);
1041 
1042 	if (nfp_port_is_vnic(port))
1043 		data = nfp_vnic_get_hw_stats(data, port->vnic, 0);
1044 	else
1045 		data = nfp_mac_get_stats(netdev, data);
1046 	data = nfp_app_port_get_stats(port, data);
1047 }
1048 
1049 static int nfp_port_get_sset_count(struct net_device *netdev, int sset)
1050 {
1051 	struct nfp_port *port = nfp_port_from_netdev(netdev);
1052 	unsigned int count;
1053 
1054 	switch (sset) {
1055 	case ETH_SS_STATS:
1056 		if (nfp_port_is_vnic(port))
1057 			count = nfp_vnic_get_hw_stats_count(0);
1058 		else
1059 			count = nfp_mac_get_stats_count(netdev);
1060 		count += nfp_app_port_get_stats_count(port);
1061 		return count;
1062 	case ETH_SS_TEST:
1063 		return nfp_get_self_test_count(netdev);
1064 	default:
1065 		return -EOPNOTSUPP;
1066 	}
1067 }
1068 
1069 static int nfp_port_fec_ethtool_to_nsp(u32 fec)
1070 {
1071 	switch (fec) {
1072 	case ETHTOOL_FEC_AUTO:
1073 		return NFP_FEC_AUTO_BIT;
1074 	case ETHTOOL_FEC_OFF:
1075 		return NFP_FEC_DISABLED_BIT;
1076 	case ETHTOOL_FEC_RS:
1077 		return NFP_FEC_REED_SOLOMON_BIT;
1078 	case ETHTOOL_FEC_BASER:
1079 		return NFP_FEC_BASER_BIT;
1080 	default:
1081 		/* NSP only supports a single mode at a time */
1082 		return -EOPNOTSUPP;
1083 	}
1084 }
1085 
1086 static u32 nfp_port_fec_nsp_to_ethtool(u32 fec)
1087 {
1088 	u32 result = 0;
1089 
1090 	if (fec & NFP_FEC_AUTO)
1091 		result |= ETHTOOL_FEC_AUTO;
1092 	if (fec & NFP_FEC_BASER)
1093 		result |= ETHTOOL_FEC_BASER;
1094 	if (fec & NFP_FEC_REED_SOLOMON)
1095 		result |= ETHTOOL_FEC_RS;
1096 	if (fec & NFP_FEC_DISABLED)
1097 		result |= ETHTOOL_FEC_OFF;
1098 
1099 	return result ?: ETHTOOL_FEC_NONE;
1100 }
1101 
1102 static int
1103 nfp_port_get_fecparam(struct net_device *netdev,
1104 		      struct ethtool_fecparam *param)
1105 {
1106 	struct nfp_eth_table_port *eth_port;
1107 	struct nfp_port *port;
1108 
1109 	param->active_fec = ETHTOOL_FEC_NONE;
1110 	param->fec = ETHTOOL_FEC_NONE;
1111 
1112 	port = nfp_port_from_netdev(netdev);
1113 	eth_port = nfp_port_get_eth_port(port);
1114 	if (!eth_port)
1115 		return -EOPNOTSUPP;
1116 
1117 	if (!nfp_eth_can_support_fec(eth_port))
1118 		return 0;
1119 
1120 	param->fec = nfp_port_fec_nsp_to_ethtool(eth_port->fec_modes_supported);
1121 	param->active_fec = nfp_port_fec_nsp_to_ethtool(BIT(eth_port->act_fec));
1122 
1123 	return 0;
1124 }
1125 
1126 static int
1127 nfp_port_set_fecparam(struct net_device *netdev,
1128 		      struct ethtool_fecparam *param)
1129 {
1130 	struct nfp_eth_table_port *eth_port;
1131 	struct nfp_port *port;
1132 	int err, fec;
1133 
1134 	port = nfp_port_from_netdev(netdev);
1135 	eth_port = nfp_port_get_eth_port(port);
1136 	if (!eth_port)
1137 		return -EOPNOTSUPP;
1138 
1139 	if (!nfp_eth_can_support_fec(eth_port))
1140 		return -EOPNOTSUPP;
1141 
1142 	fec = nfp_port_fec_ethtool_to_nsp(param->fec);
1143 	if (fec < 0)
1144 		return fec;
1145 
1146 	err = nfp_eth_set_fec(port->app->cpp, eth_port->index, fec);
1147 	if (!err)
1148 		/* Only refresh if we did something */
1149 		nfp_net_refresh_port_table(port);
1150 
1151 	return err < 0 ? err : 0;
1152 }
1153 
1154 /* RX network flow classification (RSS, filters, etc)
1155  */
1156 static u32 ethtool_flow_to_nfp_flag(u32 flow_type)
1157 {
1158 	static const u32 xlate_ethtool_to_nfp[IPV6_FLOW + 1] = {
1159 		[TCP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_TCP,
1160 		[TCP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_TCP,
1161 		[UDP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_UDP,
1162 		[UDP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_UDP,
1163 		[IPV4_FLOW]	= NFP_NET_CFG_RSS_IPV4,
1164 		[IPV6_FLOW]	= NFP_NET_CFG_RSS_IPV6,
1165 	};
1166 
1167 	if (flow_type >= ARRAY_SIZE(xlate_ethtool_to_nfp))
1168 		return 0;
1169 
1170 	return xlate_ethtool_to_nfp[flow_type];
1171 }
1172 
1173 static int nfp_net_get_rss_hash_opts(struct nfp_net *nn,
1174 				     struct ethtool_rxnfc *cmd)
1175 {
1176 	u32 nfp_rss_flag;
1177 
1178 	cmd->data = 0;
1179 
1180 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1181 		return -EOPNOTSUPP;
1182 
1183 	nfp_rss_flag = ethtool_flow_to_nfp_flag(cmd->flow_type);
1184 	if (!nfp_rss_flag)
1185 		return -EINVAL;
1186 
1187 	cmd->data |= RXH_IP_SRC | RXH_IP_DST;
1188 	if (nn->rss_cfg & nfp_rss_flag)
1189 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1190 
1191 	return 0;
1192 }
1193 
1194 static int nfp_net_get_rxnfc(struct net_device *netdev,
1195 			     struct ethtool_rxnfc *cmd, u32 *rule_locs)
1196 {
1197 	struct nfp_net *nn = netdev_priv(netdev);
1198 
1199 	switch (cmd->cmd) {
1200 	case ETHTOOL_GRXRINGS:
1201 		cmd->data = nn->dp.num_rx_rings;
1202 		return 0;
1203 	case ETHTOOL_GRXFH:
1204 		return nfp_net_get_rss_hash_opts(nn, cmd);
1205 	default:
1206 		return -EOPNOTSUPP;
1207 	}
1208 }
1209 
1210 static int nfp_net_set_rss_hash_opt(struct nfp_net *nn,
1211 				    struct ethtool_rxnfc *nfc)
1212 {
1213 	u32 new_rss_cfg = nn->rss_cfg;
1214 	u32 nfp_rss_flag;
1215 	int err;
1216 
1217 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1218 		return -EOPNOTSUPP;
1219 
1220 	/* RSS only supports IP SA/DA and L4 src/dst ports  */
1221 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
1222 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
1223 		return -EINVAL;
1224 
1225 	/* We need at least the IP SA/DA fields for hashing */
1226 	if (!(nfc->data & RXH_IP_SRC) ||
1227 	    !(nfc->data & RXH_IP_DST))
1228 		return -EINVAL;
1229 
1230 	nfp_rss_flag = ethtool_flow_to_nfp_flag(nfc->flow_type);
1231 	if (!nfp_rss_flag)
1232 		return -EINVAL;
1233 
1234 	switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
1235 	case 0:
1236 		new_rss_cfg &= ~nfp_rss_flag;
1237 		break;
1238 	case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
1239 		new_rss_cfg |= nfp_rss_flag;
1240 		break;
1241 	default:
1242 		return -EINVAL;
1243 	}
1244 
1245 	new_rss_cfg |= FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc);
1246 	new_rss_cfg |= NFP_NET_CFG_RSS_MASK;
1247 
1248 	if (new_rss_cfg == nn->rss_cfg)
1249 		return 0;
1250 
1251 	writel(new_rss_cfg, nn->dp.ctrl_bar + NFP_NET_CFG_RSS_CTRL);
1252 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
1253 	if (err)
1254 		return err;
1255 
1256 	nn->rss_cfg = new_rss_cfg;
1257 
1258 	nn_dbg(nn, "Changed RSS config to 0x%x\n", nn->rss_cfg);
1259 	return 0;
1260 }
1261 
1262 static int nfp_net_set_rxnfc(struct net_device *netdev,
1263 			     struct ethtool_rxnfc *cmd)
1264 {
1265 	struct nfp_net *nn = netdev_priv(netdev);
1266 
1267 	switch (cmd->cmd) {
1268 	case ETHTOOL_SRXFH:
1269 		return nfp_net_set_rss_hash_opt(nn, cmd);
1270 	default:
1271 		return -EOPNOTSUPP;
1272 	}
1273 }
1274 
1275 static u32 nfp_net_get_rxfh_indir_size(struct net_device *netdev)
1276 {
1277 	struct nfp_net *nn = netdev_priv(netdev);
1278 
1279 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1280 		return 0;
1281 
1282 	return ARRAY_SIZE(nn->rss_itbl);
1283 }
1284 
1285 static u32 nfp_net_get_rxfh_key_size(struct net_device *netdev)
1286 {
1287 	struct nfp_net *nn = netdev_priv(netdev);
1288 
1289 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1290 		return -EOPNOTSUPP;
1291 
1292 	return nfp_net_rss_key_sz(nn);
1293 }
1294 
1295 static int nfp_net_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
1296 			    u8 *hfunc)
1297 {
1298 	struct nfp_net *nn = netdev_priv(netdev);
1299 	int i;
1300 
1301 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1302 		return -EOPNOTSUPP;
1303 
1304 	if (indir)
1305 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
1306 			indir[i] = nn->rss_itbl[i];
1307 	if (key)
1308 		memcpy(key, nn->rss_key, nfp_net_rss_key_sz(nn));
1309 	if (hfunc) {
1310 		*hfunc = nn->rss_hfunc;
1311 		if (*hfunc >= 1 << ETH_RSS_HASH_FUNCS_COUNT)
1312 			*hfunc = ETH_RSS_HASH_UNKNOWN;
1313 	}
1314 
1315 	return 0;
1316 }
1317 
1318 static int nfp_net_set_rxfh(struct net_device *netdev,
1319 			    const u32 *indir, const u8 *key,
1320 			    const u8 hfunc)
1321 {
1322 	struct nfp_net *nn = netdev_priv(netdev);
1323 	int i;
1324 
1325 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) ||
1326 	    !(hfunc == ETH_RSS_HASH_NO_CHANGE || hfunc == nn->rss_hfunc))
1327 		return -EOPNOTSUPP;
1328 
1329 	if (!key && !indir)
1330 		return 0;
1331 
1332 	if (key) {
1333 		memcpy(nn->rss_key, key, nfp_net_rss_key_sz(nn));
1334 		nfp_net_rss_write_key(nn);
1335 	}
1336 	if (indir) {
1337 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
1338 			nn->rss_itbl[i] = indir[i];
1339 
1340 		nfp_net_rss_write_itbl(nn);
1341 	}
1342 
1343 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
1344 }
1345 
1346 /* Dump BAR registers
1347  */
1348 static int nfp_net_get_regs_len(struct net_device *netdev)
1349 {
1350 	return NFP_NET_CFG_BAR_SZ;
1351 }
1352 
1353 static void nfp_net_get_regs(struct net_device *netdev,
1354 			     struct ethtool_regs *regs, void *p)
1355 {
1356 	struct nfp_net *nn = netdev_priv(netdev);
1357 	u32 *regs_buf = p;
1358 	int i;
1359 
1360 	regs->version = nn_readl(nn, NFP_NET_CFG_VERSION);
1361 
1362 	for (i = 0; i < NFP_NET_CFG_BAR_SZ / sizeof(u32); i++)
1363 		regs_buf[i] = readl(nn->dp.ctrl_bar + (i * sizeof(u32)));
1364 }
1365 
1366 static int nfp_net_get_coalesce(struct net_device *netdev,
1367 				struct ethtool_coalesce *ec,
1368 				struct kernel_ethtool_coalesce *kernel_coal,
1369 				struct netlink_ext_ack *extack)
1370 {
1371 	struct nfp_net *nn = netdev_priv(netdev);
1372 
1373 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
1374 		return -EINVAL;
1375 
1376 	ec->use_adaptive_rx_coalesce = nn->rx_coalesce_adapt_on;
1377 	ec->use_adaptive_tx_coalesce = nn->tx_coalesce_adapt_on;
1378 
1379 	ec->rx_coalesce_usecs       = nn->rx_coalesce_usecs;
1380 	ec->rx_max_coalesced_frames = nn->rx_coalesce_max_frames;
1381 	ec->tx_coalesce_usecs       = nn->tx_coalesce_usecs;
1382 	ec->tx_max_coalesced_frames = nn->tx_coalesce_max_frames;
1383 
1384 	return 0;
1385 }
1386 
1387 /* Other debug dumps
1388  */
1389 static int
1390 nfp_dump_nsp_diag(struct nfp_app *app, struct ethtool_dump *dump, void *buffer)
1391 {
1392 	struct nfp_resource *res;
1393 	int ret;
1394 
1395 	if (!app)
1396 		return -EOPNOTSUPP;
1397 
1398 	dump->version = 1;
1399 	dump->flag = NFP_DUMP_NSP_DIAG;
1400 
1401 	res = nfp_resource_acquire(app->cpp, NFP_RESOURCE_NSP_DIAG);
1402 	if (IS_ERR(res))
1403 		return PTR_ERR(res);
1404 
1405 	if (buffer) {
1406 		if (dump->len != nfp_resource_size(res)) {
1407 			ret = -EINVAL;
1408 			goto exit_release;
1409 		}
1410 
1411 		ret = nfp_cpp_read(app->cpp, nfp_resource_cpp_id(res),
1412 				   nfp_resource_address(res),
1413 				   buffer, dump->len);
1414 		if (ret != dump->len)
1415 			ret = ret < 0 ? ret : -EIO;
1416 		else
1417 			ret = 0;
1418 	} else {
1419 		dump->len = nfp_resource_size(res);
1420 		ret = 0;
1421 	}
1422 exit_release:
1423 	nfp_resource_release(res);
1424 
1425 	return ret;
1426 }
1427 
1428 /* Set the dump flag/level. Calculate the dump length for flag > 0 only (new TLV
1429  * based dumps), since flag 0 (default) calculates the length in
1430  * nfp_app_get_dump_flag(), and we need to support triggering a level 0 dump
1431  * without setting the flag first, for backward compatibility.
1432  */
1433 static int nfp_app_set_dump(struct net_device *netdev, struct ethtool_dump *val)
1434 {
1435 	struct nfp_app *app = nfp_app_from_netdev(netdev);
1436 	s64 len;
1437 
1438 	if (!app)
1439 		return -EOPNOTSUPP;
1440 
1441 	if (val->flag == NFP_DUMP_NSP_DIAG) {
1442 		app->pf->dump_flag = val->flag;
1443 		return 0;
1444 	}
1445 
1446 	if (!app->pf->dumpspec)
1447 		return -EOPNOTSUPP;
1448 
1449 	len = nfp_net_dump_calculate_size(app->pf, app->pf->dumpspec,
1450 					  val->flag);
1451 	if (len < 0)
1452 		return len;
1453 
1454 	app->pf->dump_flag = val->flag;
1455 	app->pf->dump_len = len;
1456 
1457 	return 0;
1458 }
1459 
1460 static int
1461 nfp_app_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
1462 {
1463 	struct nfp_app *app = nfp_app_from_netdev(netdev);
1464 
1465 	if (!app)
1466 		return -EOPNOTSUPP;
1467 
1468 	if (app->pf->dump_flag == NFP_DUMP_NSP_DIAG)
1469 		return nfp_dump_nsp_diag(app, dump, NULL);
1470 
1471 	dump->flag = app->pf->dump_flag;
1472 	dump->len = app->pf->dump_len;
1473 
1474 	return 0;
1475 }
1476 
1477 static int
1478 nfp_app_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
1479 		      void *buffer)
1480 {
1481 	struct nfp_app *app = nfp_app_from_netdev(netdev);
1482 
1483 	if (!app)
1484 		return -EOPNOTSUPP;
1485 
1486 	if (app->pf->dump_flag == NFP_DUMP_NSP_DIAG)
1487 		return nfp_dump_nsp_diag(app, dump, buffer);
1488 
1489 	dump->flag = app->pf->dump_flag;
1490 	dump->len = app->pf->dump_len;
1491 
1492 	return nfp_net_dump_populate_buffer(app->pf, app->pf->dumpspec, dump,
1493 					    buffer);
1494 }
1495 
1496 static int
1497 nfp_port_get_module_info(struct net_device *netdev,
1498 			 struct ethtool_modinfo *modinfo)
1499 {
1500 	struct nfp_eth_table_port *eth_port;
1501 	struct nfp_port *port;
1502 	unsigned int read_len;
1503 	struct nfp_nsp *nsp;
1504 	int err = 0;
1505 	u8 data;
1506 
1507 	port = nfp_port_from_netdev(netdev);
1508 	if (!port)
1509 		return -EOPNOTSUPP;
1510 
1511 	/* update port state to get latest interface */
1512 	set_bit(NFP_PORT_CHANGED, &port->flags);
1513 	eth_port = nfp_port_get_eth_port(port);
1514 	if (!eth_port)
1515 		return -EOPNOTSUPP;
1516 
1517 	nsp = nfp_nsp_open(port->app->cpp);
1518 	if (IS_ERR(nsp)) {
1519 		err = PTR_ERR(nsp);
1520 		netdev_err(netdev, "Failed to access the NSP: %d\n", err);
1521 		return err;
1522 	}
1523 
1524 	if (!nfp_nsp_has_read_module_eeprom(nsp)) {
1525 		netdev_info(netdev, "reading module EEPROM not supported. Please update flash\n");
1526 		err = -EOPNOTSUPP;
1527 		goto exit_close_nsp;
1528 	}
1529 
1530 	switch (eth_port->interface) {
1531 	case NFP_INTERFACE_SFP:
1532 	case NFP_INTERFACE_SFP28:
1533 		err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index,
1534 						 SFP_SFF8472_COMPLIANCE, &data,
1535 						 1, &read_len);
1536 		if (err < 0)
1537 			goto exit_close_nsp;
1538 
1539 		if (!data) {
1540 			modinfo->type = ETH_MODULE_SFF_8079;
1541 			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1542 		} else {
1543 			modinfo->type = ETH_MODULE_SFF_8472;
1544 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1545 		}
1546 		break;
1547 	case NFP_INTERFACE_QSFP:
1548 		err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index,
1549 						 SFP_SFF_REV_COMPLIANCE, &data,
1550 						 1, &read_len);
1551 		if (err < 0)
1552 			goto exit_close_nsp;
1553 
1554 		if (data < 0x3) {
1555 			modinfo->type = ETH_MODULE_SFF_8436;
1556 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_MAX_LEN;
1557 		} else {
1558 			modinfo->type = ETH_MODULE_SFF_8636;
1559 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1560 		}
1561 		break;
1562 	case NFP_INTERFACE_QSFP28:
1563 		modinfo->type = ETH_MODULE_SFF_8636;
1564 		modinfo->eeprom_len = ETH_MODULE_SFF_8636_MAX_LEN;
1565 		break;
1566 	default:
1567 		netdev_err(netdev, "Unsupported module 0x%x detected\n",
1568 			   eth_port->interface);
1569 		err = -EINVAL;
1570 	}
1571 
1572 exit_close_nsp:
1573 	nfp_nsp_close(nsp);
1574 	return err;
1575 }
1576 
1577 static int
1578 nfp_port_get_module_eeprom(struct net_device *netdev,
1579 			   struct ethtool_eeprom *eeprom, u8 *data)
1580 {
1581 	struct nfp_eth_table_port *eth_port;
1582 	struct nfp_port *port;
1583 	struct nfp_nsp *nsp;
1584 	int err;
1585 
1586 	port = nfp_port_from_netdev(netdev);
1587 	eth_port = __nfp_port_get_eth_port(port);
1588 	if (!eth_port)
1589 		return -EOPNOTSUPP;
1590 
1591 	nsp = nfp_nsp_open(port->app->cpp);
1592 	if (IS_ERR(nsp)) {
1593 		err = PTR_ERR(nsp);
1594 		netdev_err(netdev, "Failed to access the NSP: %d\n", err);
1595 		return err;
1596 	}
1597 
1598 	if (!nfp_nsp_has_read_module_eeprom(nsp)) {
1599 		netdev_info(netdev, "reading module EEPROM not supported. Please update flash\n");
1600 		err = -EOPNOTSUPP;
1601 		goto exit_close_nsp;
1602 	}
1603 
1604 	err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index,
1605 					 eeprom->offset, data, eeprom->len,
1606 					 &eeprom->len);
1607 	if (err < 0) {
1608 		if (eeprom->len) {
1609 			netdev_warn(netdev,
1610 				    "Incomplete read from module EEPROM: %d\n",
1611 				     err);
1612 			err = 0;
1613 		} else {
1614 			netdev_err(netdev,
1615 				   "Reading from module EEPROM failed: %d\n",
1616 				   err);
1617 		}
1618 	}
1619 
1620 exit_close_nsp:
1621 	nfp_nsp_close(nsp);
1622 	return err;
1623 }
1624 
1625 static int nfp_net_set_coalesce(struct net_device *netdev,
1626 				struct ethtool_coalesce *ec,
1627 				struct kernel_ethtool_coalesce *kernel_coal,
1628 				struct netlink_ext_ack *extack)
1629 {
1630 	struct nfp_net *nn = netdev_priv(netdev);
1631 	unsigned int factor;
1632 
1633 	/* Compute factor used to convert coalesce '_usecs' parameters to
1634 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
1635 	 * count.
1636 	 */
1637 	factor = nn->tlv_caps.me_freq_mhz / 16;
1638 
1639 	/* Each pair of (usecs, max_frames) fields specifies that interrupts
1640 	 * should be coalesced until
1641 	 *      (usecs > 0 && time_since_first_completion >= usecs) ||
1642 	 *      (max_frames > 0 && completed_frames >= max_frames)
1643 	 *
1644 	 * It is illegal to set both usecs and max_frames to zero as this would
1645 	 * cause interrupts to never be generated.  To disable coalescing, set
1646 	 * usecs = 0 and max_frames = 1.
1647 	 *
1648 	 * Some implementations ignore the value of max_frames and use the
1649 	 * condition time_since_first_completion >= usecs
1650 	 */
1651 
1652 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
1653 		return -EINVAL;
1654 
1655 	/* ensure valid configuration */
1656 	if (!ec->rx_coalesce_usecs && !ec->rx_max_coalesced_frames)
1657 		return -EINVAL;
1658 
1659 	if (!ec->tx_coalesce_usecs && !ec->tx_max_coalesced_frames)
1660 		return -EINVAL;
1661 
1662 	if (nfp_net_coalesce_para_check(ec->rx_coalesce_usecs * factor,
1663 					ec->rx_max_coalesced_frames))
1664 		return -EINVAL;
1665 
1666 	if (nfp_net_coalesce_para_check(ec->tx_coalesce_usecs * factor,
1667 					ec->tx_max_coalesced_frames))
1668 		return -EINVAL;
1669 
1670 	/* configuration is valid */
1671 	nn->rx_coalesce_adapt_on = !!ec->use_adaptive_rx_coalesce;
1672 	nn->tx_coalesce_adapt_on = !!ec->use_adaptive_tx_coalesce;
1673 
1674 	nn->rx_coalesce_usecs      = ec->rx_coalesce_usecs;
1675 	nn->rx_coalesce_max_frames = ec->rx_max_coalesced_frames;
1676 	nn->tx_coalesce_usecs      = ec->tx_coalesce_usecs;
1677 	nn->tx_coalesce_max_frames = ec->tx_max_coalesced_frames;
1678 
1679 	/* write configuration to device */
1680 	nfp_net_coalesce_write_cfg(nn);
1681 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
1682 }
1683 
1684 static void nfp_net_get_channels(struct net_device *netdev,
1685 				 struct ethtool_channels *channel)
1686 {
1687 	struct nfp_net *nn = netdev_priv(netdev);
1688 	unsigned int num_tx_rings;
1689 
1690 	num_tx_rings = nn->dp.num_tx_rings;
1691 	if (nn->dp.xdp_prog)
1692 		num_tx_rings -= nn->dp.num_rx_rings;
1693 
1694 	channel->max_rx = min(nn->max_rx_rings, nn->max_r_vecs);
1695 	channel->max_tx = min(nn->max_tx_rings, nn->max_r_vecs);
1696 	channel->max_combined = min(channel->max_rx, channel->max_tx);
1697 	channel->max_other = NFP_NET_NON_Q_VECTORS;
1698 	channel->combined_count = min(nn->dp.num_rx_rings, num_tx_rings);
1699 	channel->rx_count = nn->dp.num_rx_rings - channel->combined_count;
1700 	channel->tx_count = num_tx_rings - channel->combined_count;
1701 	channel->other_count = NFP_NET_NON_Q_VECTORS;
1702 }
1703 
1704 static int nfp_net_set_num_rings(struct nfp_net *nn, unsigned int total_rx,
1705 				 unsigned int total_tx)
1706 {
1707 	struct nfp_net_dp *dp;
1708 
1709 	dp = nfp_net_clone_dp(nn);
1710 	if (!dp)
1711 		return -ENOMEM;
1712 
1713 	dp->num_rx_rings = total_rx;
1714 	dp->num_tx_rings = total_tx;
1715 	/* nfp_net_check_config() will catch num_tx_rings > nn->max_tx_rings */
1716 	if (dp->xdp_prog)
1717 		dp->num_tx_rings += total_rx;
1718 
1719 	return nfp_net_ring_reconfig(nn, dp, NULL);
1720 }
1721 
1722 static int nfp_net_set_channels(struct net_device *netdev,
1723 				struct ethtool_channels *channel)
1724 {
1725 	struct nfp_net *nn = netdev_priv(netdev);
1726 	unsigned int total_rx, total_tx;
1727 
1728 	/* Reject unsupported */
1729 	if (channel->other_count != NFP_NET_NON_Q_VECTORS ||
1730 	    (channel->rx_count && channel->tx_count))
1731 		return -EINVAL;
1732 
1733 	total_rx = channel->combined_count + channel->rx_count;
1734 	total_tx = channel->combined_count + channel->tx_count;
1735 
1736 	if (total_rx > min(nn->max_rx_rings, nn->max_r_vecs) ||
1737 	    total_tx > min(nn->max_tx_rings, nn->max_r_vecs))
1738 		return -EINVAL;
1739 
1740 	return nfp_net_set_num_rings(nn, total_rx, total_tx);
1741 }
1742 
1743 static void nfp_port_get_pauseparam(struct net_device *netdev,
1744 				    struct ethtool_pauseparam *pause)
1745 {
1746 	struct nfp_eth_table_port *eth_port;
1747 	struct nfp_port *port;
1748 
1749 	port = nfp_port_from_netdev(netdev);
1750 	eth_port = nfp_port_get_eth_port(port);
1751 	if (!eth_port)
1752 		return;
1753 
1754 	/* Currently pause frame support is fixed */
1755 	pause->autoneg = AUTONEG_DISABLE;
1756 	pause->rx_pause = 1;
1757 	pause->tx_pause = 1;
1758 }
1759 
1760 static int nfp_net_set_phys_id(struct net_device *netdev,
1761 			       enum ethtool_phys_id_state state)
1762 {
1763 	struct nfp_eth_table_port *eth_port;
1764 	struct nfp_port *port;
1765 	int err;
1766 
1767 	port = nfp_port_from_netdev(netdev);
1768 	eth_port = __nfp_port_get_eth_port(port);
1769 	if (!eth_port)
1770 		return -EOPNOTSUPP;
1771 
1772 	switch (state) {
1773 	case ETHTOOL_ID_ACTIVE:
1774 		/* Control LED to blink */
1775 		err = nfp_eth_set_idmode(port->app->cpp, eth_port->index, 1);
1776 		break;
1777 
1778 	case ETHTOOL_ID_INACTIVE:
1779 		/* Control LED to normal mode */
1780 		err = nfp_eth_set_idmode(port->app->cpp, eth_port->index, 0);
1781 		break;
1782 
1783 	case ETHTOOL_ID_ON:
1784 	case ETHTOOL_ID_OFF:
1785 	default:
1786 		return -EOPNOTSUPP;
1787 	}
1788 
1789 	return err;
1790 }
1791 
1792 #define NFP_EEPROM_LEN ETH_ALEN
1793 
1794 static int
1795 nfp_net_get_eeprom_len(struct net_device *netdev)
1796 {
1797 	struct nfp_eth_table_port *eth_port;
1798 	struct nfp_port *port;
1799 
1800 	port = nfp_port_from_netdev(netdev);
1801 	eth_port = __nfp_port_get_eth_port(port);
1802 	if (!eth_port)
1803 		return 0;
1804 
1805 	return NFP_EEPROM_LEN;
1806 }
1807 
1808 static int
1809 nfp_net_get_nsp_hwindex(struct net_device *netdev,
1810 			struct nfp_nsp **nspptr,
1811 			u32 *index)
1812 {
1813 	struct nfp_eth_table_port *eth_port;
1814 	struct nfp_port *port;
1815 	struct nfp_nsp *nsp;
1816 	int err;
1817 
1818 	port = nfp_port_from_netdev(netdev);
1819 	eth_port = __nfp_port_get_eth_port(port);
1820 	if (!eth_port)
1821 		return -EOPNOTSUPP;
1822 
1823 	nsp = nfp_nsp_open(port->app->cpp);
1824 	if (IS_ERR(nsp)) {
1825 		err = PTR_ERR(nsp);
1826 		netdev_err(netdev, "Failed to access the NSP: %d\n", err);
1827 		return err;
1828 	}
1829 
1830 	if (!nfp_nsp_has_hwinfo_lookup(nsp)) {
1831 		netdev_err(netdev, "NSP doesn't support PF MAC generation\n");
1832 		nfp_nsp_close(nsp);
1833 		return -EOPNOTSUPP;
1834 	}
1835 
1836 	*nspptr = nsp;
1837 	*index = eth_port->eth_index;
1838 
1839 	return 0;
1840 }
1841 
1842 static int
1843 nfp_net_get_port_mac_by_hwinfo(struct net_device *netdev,
1844 			       u8 *mac_addr)
1845 {
1846 	char hwinfo[32] = {};
1847 	struct nfp_nsp *nsp;
1848 	u32 index;
1849 	int err;
1850 
1851 	err = nfp_net_get_nsp_hwindex(netdev, &nsp, &index);
1852 	if (err)
1853 		return err;
1854 
1855 	snprintf(hwinfo, sizeof(hwinfo), "eth%u.mac", index);
1856 	err = nfp_nsp_hwinfo_lookup(nsp, hwinfo, sizeof(hwinfo));
1857 	nfp_nsp_close(nsp);
1858 	if (err) {
1859 		netdev_err(netdev, "Reading persistent MAC address failed: %d\n",
1860 			   err);
1861 		return -EOPNOTSUPP;
1862 	}
1863 
1864 	if (sscanf(hwinfo, "%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1865 		   &mac_addr[0], &mac_addr[1], &mac_addr[2],
1866 		   &mac_addr[3], &mac_addr[4], &mac_addr[5]) != 6) {
1867 		netdev_err(netdev, "Can't parse persistent MAC address (%s)\n",
1868 			   hwinfo);
1869 		return -EOPNOTSUPP;
1870 	}
1871 
1872 	return 0;
1873 }
1874 
1875 static int
1876 nfp_net_set_port_mac_by_hwinfo(struct net_device *netdev,
1877 			       u8 *mac_addr)
1878 {
1879 	char hwinfo[32] = {};
1880 	struct nfp_nsp *nsp;
1881 	u32 index;
1882 	int err;
1883 
1884 	err = nfp_net_get_nsp_hwindex(netdev, &nsp, &index);
1885 	if (err)
1886 		return err;
1887 
1888 	snprintf(hwinfo, sizeof(hwinfo),
1889 		 "eth%u.mac=%02hhx:%02hhx:%02hhx:%02hhx:%02hhx:%02hhx",
1890 		 index, mac_addr[0], mac_addr[1], mac_addr[2], mac_addr[3],
1891 		 mac_addr[4], mac_addr[5]);
1892 
1893 	err = nfp_nsp_hwinfo_set(nsp, hwinfo, sizeof(hwinfo));
1894 	nfp_nsp_close(nsp);
1895 	if (err) {
1896 		netdev_err(netdev, "HWinfo set failed: %d, hwinfo: %s\n",
1897 			   err, hwinfo);
1898 		return -EOPNOTSUPP;
1899 	}
1900 
1901 	return 0;
1902 }
1903 
1904 static int
1905 nfp_net_get_eeprom(struct net_device *netdev,
1906 		   struct ethtool_eeprom *eeprom, u8 *bytes)
1907 {
1908 	struct nfp_net *nn = netdev_priv(netdev);
1909 	u8 buf[NFP_EEPROM_LEN] = {};
1910 
1911 	if (eeprom->len == 0)
1912 		return -EINVAL;
1913 
1914 	if (nfp_net_get_port_mac_by_hwinfo(netdev, buf))
1915 		return -EOPNOTSUPP;
1916 
1917 	eeprom->magic = nn->pdev->vendor | (nn->pdev->device << 16);
1918 	memcpy(bytes, buf + eeprom->offset, eeprom->len);
1919 
1920 	return 0;
1921 }
1922 
1923 static int
1924 nfp_net_set_eeprom(struct net_device *netdev,
1925 		   struct ethtool_eeprom *eeprom, u8 *bytes)
1926 {
1927 	struct nfp_net *nn = netdev_priv(netdev);
1928 	u8 buf[NFP_EEPROM_LEN] = {};
1929 
1930 	if (eeprom->len == 0)
1931 		return -EINVAL;
1932 
1933 	if (eeprom->magic != (nn->pdev->vendor | nn->pdev->device << 16))
1934 		return -EINVAL;
1935 
1936 	if (nfp_net_get_port_mac_by_hwinfo(netdev, buf))
1937 		return -EOPNOTSUPP;
1938 
1939 	memcpy(buf + eeprom->offset, bytes, eeprom->len);
1940 	if (nfp_net_set_port_mac_by_hwinfo(netdev, buf))
1941 		return -EOPNOTSUPP;
1942 
1943 	return 0;
1944 }
1945 
1946 static const struct ethtool_ops nfp_net_ethtool_ops = {
1947 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1948 				     ETHTOOL_COALESCE_MAX_FRAMES |
1949 				     ETHTOOL_COALESCE_USE_ADAPTIVE,
1950 	.get_drvinfo		= nfp_net_get_drvinfo,
1951 	.nway_reset             = nfp_net_nway_reset,
1952 	.get_link		= ethtool_op_get_link,
1953 	.get_ringparam		= nfp_net_get_ringparam,
1954 	.set_ringparam		= nfp_net_set_ringparam,
1955 	.self_test		= nfp_net_self_test,
1956 	.get_strings		= nfp_net_get_strings,
1957 	.get_ethtool_stats	= nfp_net_get_stats,
1958 	.get_sset_count		= nfp_net_get_sset_count,
1959 	.get_rxnfc		= nfp_net_get_rxnfc,
1960 	.set_rxnfc		= nfp_net_set_rxnfc,
1961 	.get_rxfh_indir_size	= nfp_net_get_rxfh_indir_size,
1962 	.get_rxfh_key_size	= nfp_net_get_rxfh_key_size,
1963 	.get_rxfh		= nfp_net_get_rxfh,
1964 	.set_rxfh		= nfp_net_set_rxfh,
1965 	.get_regs_len		= nfp_net_get_regs_len,
1966 	.get_regs		= nfp_net_get_regs,
1967 	.set_dump		= nfp_app_set_dump,
1968 	.get_dump_flag		= nfp_app_get_dump_flag,
1969 	.get_dump_data		= nfp_app_get_dump_data,
1970 	.get_eeprom_len         = nfp_net_get_eeprom_len,
1971 	.get_eeprom             = nfp_net_get_eeprom,
1972 	.set_eeprom             = nfp_net_set_eeprom,
1973 	.get_module_info	= nfp_port_get_module_info,
1974 	.get_module_eeprom	= nfp_port_get_module_eeprom,
1975 	.get_coalesce           = nfp_net_get_coalesce,
1976 	.set_coalesce           = nfp_net_set_coalesce,
1977 	.get_channels		= nfp_net_get_channels,
1978 	.set_channels		= nfp_net_set_channels,
1979 	.get_link_ksettings	= nfp_net_get_link_ksettings,
1980 	.set_link_ksettings	= nfp_net_set_link_ksettings,
1981 	.get_fecparam		= nfp_port_get_fecparam,
1982 	.set_fecparam		= nfp_port_set_fecparam,
1983 	.get_pauseparam		= nfp_port_get_pauseparam,
1984 	.set_phys_id		= nfp_net_set_phys_id,
1985 };
1986 
1987 const struct ethtool_ops nfp_port_ethtool_ops = {
1988 	.get_drvinfo		= nfp_app_get_drvinfo,
1989 	.nway_reset             = nfp_net_nway_reset,
1990 	.get_link		= ethtool_op_get_link,
1991 	.get_strings		= nfp_port_get_strings,
1992 	.get_ethtool_stats	= nfp_port_get_stats,
1993 	.self_test		= nfp_net_self_test,
1994 	.get_sset_count		= nfp_port_get_sset_count,
1995 	.set_dump		= nfp_app_set_dump,
1996 	.get_dump_flag		= nfp_app_get_dump_flag,
1997 	.get_dump_data		= nfp_app_get_dump_data,
1998 	.get_module_info	= nfp_port_get_module_info,
1999 	.get_module_eeprom	= nfp_port_get_module_eeprom,
2000 	.get_link_ksettings	= nfp_net_get_link_ksettings,
2001 	.set_link_ksettings	= nfp_net_set_link_ksettings,
2002 	.get_fecparam		= nfp_port_get_fecparam,
2003 	.set_fecparam		= nfp_port_set_fecparam,
2004 	.get_pauseparam		= nfp_port_get_pauseparam,
2005 	.set_phys_id		= nfp_net_set_phys_id,
2006 };
2007 
2008 void nfp_net_set_ethtool_ops(struct net_device *netdev)
2009 {
2010 	netdev->ethtool_ops = &nfp_net_ethtool_ops;
2011 }
2012