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