xref: /linux/drivers/net/ethernet/netronome/nfp/nfp_net_ethtool.c (revision 9dbbc3b9d09d6deba9f3b9e1d5b355032ed46a75)
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_nsp.h"
25 #include "nfp_app.h"
26 #include "nfp_main.h"
27 #include "nfp_net_ctrl.h"
28 #include "nfp_net.h"
29 #include "nfp_port.h"
30 
31 struct nfp_et_stat {
32 	char name[ETH_GSTRING_LEN];
33 	int off;
34 };
35 
36 static const struct nfp_et_stat nfp_net_et_stats[] = {
37 	/* Stats from the device */
38 	{ "dev_rx_discards",	NFP_NET_CFG_STATS_RX_DISCARDS },
39 	{ "dev_rx_errors",	NFP_NET_CFG_STATS_RX_ERRORS },
40 	{ "dev_rx_bytes",	NFP_NET_CFG_STATS_RX_OCTETS },
41 	{ "dev_rx_uc_bytes",	NFP_NET_CFG_STATS_RX_UC_OCTETS },
42 	{ "dev_rx_mc_bytes",	NFP_NET_CFG_STATS_RX_MC_OCTETS },
43 	{ "dev_rx_bc_bytes",	NFP_NET_CFG_STATS_RX_BC_OCTETS },
44 	{ "dev_rx_pkts",	NFP_NET_CFG_STATS_RX_FRAMES },
45 	{ "dev_rx_mc_pkts",	NFP_NET_CFG_STATS_RX_MC_FRAMES },
46 	{ "dev_rx_bc_pkts",	NFP_NET_CFG_STATS_RX_BC_FRAMES },
47 
48 	{ "dev_tx_discards",	NFP_NET_CFG_STATS_TX_DISCARDS },
49 	{ "dev_tx_errors",	NFP_NET_CFG_STATS_TX_ERRORS },
50 	{ "dev_tx_bytes",	NFP_NET_CFG_STATS_TX_OCTETS },
51 	{ "dev_tx_uc_bytes",	NFP_NET_CFG_STATS_TX_UC_OCTETS },
52 	{ "dev_tx_mc_bytes",	NFP_NET_CFG_STATS_TX_MC_OCTETS },
53 	{ "dev_tx_bc_bytes",	NFP_NET_CFG_STATS_TX_BC_OCTETS },
54 	{ "dev_tx_pkts",	NFP_NET_CFG_STATS_TX_FRAMES },
55 	{ "dev_tx_mc_pkts",	NFP_NET_CFG_STATS_TX_MC_FRAMES },
56 	{ "dev_tx_bc_pkts",	NFP_NET_CFG_STATS_TX_BC_FRAMES },
57 
58 	{ "bpf_pass_pkts",	NFP_NET_CFG_STATS_APP0_FRAMES },
59 	{ "bpf_pass_bytes",	NFP_NET_CFG_STATS_APP0_BYTES },
60 	/* see comments in outro functions in nfp_bpf_jit.c to find out
61 	 * how different BPF modes use app-specific counters
62 	 */
63 	{ "bpf_app1_pkts",	NFP_NET_CFG_STATS_APP1_FRAMES },
64 	{ "bpf_app1_bytes",	NFP_NET_CFG_STATS_APP1_BYTES },
65 	{ "bpf_app2_pkts",	NFP_NET_CFG_STATS_APP2_FRAMES },
66 	{ "bpf_app2_bytes",	NFP_NET_CFG_STATS_APP2_BYTES },
67 	{ "bpf_app3_pkts",	NFP_NET_CFG_STATS_APP3_FRAMES },
68 	{ "bpf_app3_bytes",	NFP_NET_CFG_STATS_APP3_BYTES },
69 };
70 
71 static const struct nfp_et_stat nfp_mac_et_stats[] = {
72 	{ "rx_octets",			NFP_MAC_STATS_RX_IN_OCTETS, },
73 	{ "rx_frame_too_long_errors",
74 			NFP_MAC_STATS_RX_FRAME_TOO_LONG_ERRORS, },
75 	{ "rx_range_length_errors",	NFP_MAC_STATS_RX_RANGE_LENGTH_ERRORS, },
76 	{ "rx_vlan_received_ok",	NFP_MAC_STATS_RX_VLAN_RECEIVED_OK, },
77 	{ "rx_errors",			NFP_MAC_STATS_RX_IN_ERRORS, },
78 	{ "rx_broadcast_pkts",		NFP_MAC_STATS_RX_IN_BROADCAST_PKTS, },
79 	{ "rx_drop_events",		NFP_MAC_STATS_RX_DROP_EVENTS, },
80 	{ "rx_alignment_errors",	NFP_MAC_STATS_RX_ALIGNMENT_ERRORS, },
81 	{ "rx_pause_mac_ctrl_frames",
82 			NFP_MAC_STATS_RX_PAUSE_MAC_CTRL_FRAMES, },
83 	{ "rx_frames_received_ok",	NFP_MAC_STATS_RX_FRAMES_RECEIVED_OK, },
84 	{ "rx_frame_check_sequence_errors",
85 			NFP_MAC_STATS_RX_FRAME_CHECK_SEQUENCE_ERRORS, },
86 	{ "rx_unicast_pkts",		NFP_MAC_STATS_RX_UNICAST_PKTS, },
87 	{ "rx_multicast_pkts",		NFP_MAC_STATS_RX_MULTICAST_PKTS, },
88 	{ "rx_pkts",			NFP_MAC_STATS_RX_PKTS, },
89 	{ "rx_undersize_pkts",		NFP_MAC_STATS_RX_UNDERSIZE_PKTS, },
90 	{ "rx_pkts_64_octets",		NFP_MAC_STATS_RX_PKTS_64_OCTETS, },
91 	{ "rx_pkts_65_to_127_octets",
92 			NFP_MAC_STATS_RX_PKTS_65_TO_127_OCTETS, },
93 	{ "rx_pkts_128_to_255_octets",
94 			NFP_MAC_STATS_RX_PKTS_128_TO_255_OCTETS, },
95 	{ "rx_pkts_256_to_511_octets",
96 			NFP_MAC_STATS_RX_PKTS_256_TO_511_OCTETS, },
97 	{ "rx_pkts_512_to_1023_octets",
98 			NFP_MAC_STATS_RX_PKTS_512_TO_1023_OCTETS, },
99 	{ "rx_pkts_1024_to_1518_octets",
100 			NFP_MAC_STATS_RX_PKTS_1024_TO_1518_OCTETS, },
101 	{ "rx_pkts_1519_to_max_octets",
102 			NFP_MAC_STATS_RX_PKTS_1519_TO_MAX_OCTETS, },
103 	{ "rx_jabbers",			NFP_MAC_STATS_RX_JABBERS, },
104 	{ "rx_fragments",		NFP_MAC_STATS_RX_FRAGMENTS, },
105 	{ "rx_oversize_pkts",		NFP_MAC_STATS_RX_OVERSIZE_PKTS, },
106 	{ "rx_pause_frames_class0",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS0, },
107 	{ "rx_pause_frames_class1",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS1, },
108 	{ "rx_pause_frames_class2",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS2, },
109 	{ "rx_pause_frames_class3",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS3, },
110 	{ "rx_pause_frames_class4",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS4, },
111 	{ "rx_pause_frames_class5",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS5, },
112 	{ "rx_pause_frames_class6",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS6, },
113 	{ "rx_pause_frames_class7",	NFP_MAC_STATS_RX_PAUSE_FRAMES_CLASS7, },
114 	{ "rx_mac_ctrl_frames_received",
115 			NFP_MAC_STATS_RX_MAC_CTRL_FRAMES_RECEIVED, },
116 	{ "rx_mac_head_drop",		NFP_MAC_STATS_RX_MAC_HEAD_DROP, },
117 	{ "tx_queue_drop",		NFP_MAC_STATS_TX_QUEUE_DROP, },
118 	{ "tx_octets",			NFP_MAC_STATS_TX_OUT_OCTETS, },
119 	{ "tx_vlan_transmitted_ok",	NFP_MAC_STATS_TX_VLAN_TRANSMITTED_OK, },
120 	{ "tx_errors",			NFP_MAC_STATS_TX_OUT_ERRORS, },
121 	{ "tx_broadcast_pkts",		NFP_MAC_STATS_TX_BROADCAST_PKTS, },
122 	{ "tx_pause_mac_ctrl_frames",
123 			NFP_MAC_STATS_TX_PAUSE_MAC_CTRL_FRAMES, },
124 	{ "tx_frames_transmitted_ok",
125 			NFP_MAC_STATS_TX_FRAMES_TRANSMITTED_OK, },
126 	{ "tx_unicast_pkts",		NFP_MAC_STATS_TX_UNICAST_PKTS, },
127 	{ "tx_multicast_pkts",		NFP_MAC_STATS_TX_MULTICAST_PKTS, },
128 	{ "tx_pkts_64_octets",		NFP_MAC_STATS_TX_PKTS_64_OCTETS, },
129 	{ "tx_pkts_65_to_127_octets",
130 			NFP_MAC_STATS_TX_PKTS_65_TO_127_OCTETS, },
131 	{ "tx_pkts_128_to_255_octets",
132 			NFP_MAC_STATS_TX_PKTS_128_TO_255_OCTETS, },
133 	{ "tx_pkts_256_to_511_octets",
134 			NFP_MAC_STATS_TX_PKTS_256_TO_511_OCTETS, },
135 	{ "tx_pkts_512_to_1023_octets",
136 			NFP_MAC_STATS_TX_PKTS_512_TO_1023_OCTETS, },
137 	{ "tx_pkts_1024_to_1518_octets",
138 			NFP_MAC_STATS_TX_PKTS_1024_TO_1518_OCTETS, },
139 	{ "tx_pkts_1519_to_max_octets",
140 			NFP_MAC_STATS_TX_PKTS_1519_TO_MAX_OCTETS, },
141 	{ "tx_pause_frames_class0",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS0, },
142 	{ "tx_pause_frames_class1",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS1, },
143 	{ "tx_pause_frames_class2",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS2, },
144 	{ "tx_pause_frames_class3",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS3, },
145 	{ "tx_pause_frames_class4",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS4, },
146 	{ "tx_pause_frames_class5",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS5, },
147 	{ "tx_pause_frames_class6",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS6, },
148 	{ "tx_pause_frames_class7",	NFP_MAC_STATS_TX_PAUSE_FRAMES_CLASS7, },
149 };
150 
151 static const char nfp_tlv_stat_names[][ETH_GSTRING_LEN] = {
152 	[1]	= "dev_rx_discards",
153 	[2]	= "dev_rx_errors",
154 	[3]	= "dev_rx_bytes",
155 	[4]	= "dev_rx_uc_bytes",
156 	[5]	= "dev_rx_mc_bytes",
157 	[6]	= "dev_rx_bc_bytes",
158 	[7]	= "dev_rx_pkts",
159 	[8]	= "dev_rx_mc_pkts",
160 	[9]	= "dev_rx_bc_pkts",
161 
162 	[10]	= "dev_tx_discards",
163 	[11]	= "dev_tx_errors",
164 	[12]	= "dev_tx_bytes",
165 	[13]	= "dev_tx_uc_bytes",
166 	[14]	= "dev_tx_mc_bytes",
167 	[15]	= "dev_tx_bc_bytes",
168 	[16]	= "dev_tx_pkts",
169 	[17]	= "dev_tx_mc_pkts",
170 	[18]	= "dev_tx_bc_pkts",
171 };
172 
173 #define NN_ET_GLOBAL_STATS_LEN ARRAY_SIZE(nfp_net_et_stats)
174 #define NN_ET_SWITCH_STATS_LEN 9
175 #define NN_RVEC_GATHER_STATS	13
176 #define NN_RVEC_PER_Q_STATS	3
177 #define NN_CTRL_PATH_STATS	4
178 
179 #define SFP_SFF_REV_COMPLIANCE	1
180 
181 static void nfp_net_get_nspinfo(struct nfp_app *app, char *version)
182 {
183 	struct nfp_nsp *nsp;
184 
185 	if (!app)
186 		return;
187 
188 	nsp = nfp_nsp_open(app->cpp);
189 	if (IS_ERR(nsp))
190 		return;
191 
192 	snprintf(version, ETHTOOL_FWVERS_LEN, "%hu.%hu",
193 		 nfp_nsp_get_abi_ver_major(nsp),
194 		 nfp_nsp_get_abi_ver_minor(nsp));
195 
196 	nfp_nsp_close(nsp);
197 }
198 
199 static void
200 nfp_get_drvinfo(struct nfp_app *app, struct pci_dev *pdev,
201 		const char *vnic_version, struct ethtool_drvinfo *drvinfo)
202 {
203 	char nsp_version[ETHTOOL_FWVERS_LEN] = {};
204 
205 	strlcpy(drvinfo->driver, pdev->driver->name, sizeof(drvinfo->driver));
206 	nfp_net_get_nspinfo(app, nsp_version);
207 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
208 		 "%s %s %s %s", vnic_version, nsp_version,
209 		 nfp_app_mip_name(app), nfp_app_name(app));
210 }
211 
212 static void
213 nfp_net_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
214 {
215 	char vnic_version[ETHTOOL_FWVERS_LEN] = {};
216 	struct nfp_net *nn = netdev_priv(netdev);
217 
218 	snprintf(vnic_version, sizeof(vnic_version), "%d.%d.%d.%d",
219 		 nn->fw_ver.resv, nn->fw_ver.class,
220 		 nn->fw_ver.major, nn->fw_ver.minor);
221 	strlcpy(drvinfo->bus_info, pci_name(nn->pdev),
222 		sizeof(drvinfo->bus_info));
223 
224 	nfp_get_drvinfo(nn->app, nn->pdev, vnic_version, drvinfo);
225 }
226 
227 static void
228 nfp_app_get_drvinfo(struct net_device *netdev, struct ethtool_drvinfo *drvinfo)
229 {
230 	struct nfp_app *app = nfp_app_from_netdev(netdev);
231 
232 	strlcpy(drvinfo->bus_info, pci_name(app->pdev),
233 		sizeof(drvinfo->bus_info));
234 	nfp_get_drvinfo(app, app->pdev, "*", drvinfo);
235 }
236 
237 static void
238 nfp_net_set_fec_link_mode(struct nfp_eth_table_port *eth_port,
239 			  struct ethtool_link_ksettings *c)
240 {
241 	unsigned int modes;
242 
243 	ethtool_link_ksettings_add_link_mode(c, supported, FEC_NONE);
244 	if (!nfp_eth_can_support_fec(eth_port)) {
245 		ethtool_link_ksettings_add_link_mode(c, advertising, FEC_NONE);
246 		return;
247 	}
248 
249 	modes = nfp_eth_supported_fec_modes(eth_port);
250 	if (modes & NFP_FEC_BASER) {
251 		ethtool_link_ksettings_add_link_mode(c, supported, FEC_BASER);
252 		ethtool_link_ksettings_add_link_mode(c, advertising, FEC_BASER);
253 	}
254 
255 	if (modes & NFP_FEC_REED_SOLOMON) {
256 		ethtool_link_ksettings_add_link_mode(c, supported, FEC_RS);
257 		ethtool_link_ksettings_add_link_mode(c, advertising, FEC_RS);
258 	}
259 }
260 
261 /**
262  * nfp_net_get_link_ksettings - Get Link Speed settings
263  * @netdev:	network interface device structure
264  * @cmd:	ethtool command
265  *
266  * Reports speed settings based on info in the BAR provided by the fw.
267  */
268 static int
269 nfp_net_get_link_ksettings(struct net_device *netdev,
270 			   struct ethtool_link_ksettings *cmd)
271 {
272 	static const u32 ls_to_ethtool[] = {
273 		[NFP_NET_CFG_STS_LINK_RATE_UNSUPPORTED]	= 0,
274 		[NFP_NET_CFG_STS_LINK_RATE_UNKNOWN]	= SPEED_UNKNOWN,
275 		[NFP_NET_CFG_STS_LINK_RATE_1G]		= SPEED_1000,
276 		[NFP_NET_CFG_STS_LINK_RATE_10G]		= SPEED_10000,
277 		[NFP_NET_CFG_STS_LINK_RATE_25G]		= SPEED_25000,
278 		[NFP_NET_CFG_STS_LINK_RATE_40G]		= SPEED_40000,
279 		[NFP_NET_CFG_STS_LINK_RATE_50G]		= SPEED_50000,
280 		[NFP_NET_CFG_STS_LINK_RATE_100G]	= SPEED_100000,
281 	};
282 	struct nfp_eth_table_port *eth_port;
283 	struct nfp_port *port;
284 	struct nfp_net *nn;
285 	u32 sts, ls;
286 
287 	/* Init to unknowns */
288 	ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
289 	cmd->base.port = PORT_OTHER;
290 	cmd->base.speed = SPEED_UNKNOWN;
291 	cmd->base.duplex = DUPLEX_UNKNOWN;
292 
293 	port = nfp_port_from_netdev(netdev);
294 	eth_port = nfp_port_get_eth_port(port);
295 	if (eth_port) {
296 		cmd->base.autoneg = eth_port->aneg != NFP_ANEG_DISABLED ?
297 			AUTONEG_ENABLE : AUTONEG_DISABLE;
298 		nfp_net_set_fec_link_mode(eth_port, cmd);
299 	}
300 
301 	if (!netif_carrier_ok(netdev))
302 		return 0;
303 
304 	/* Use link speed from ETH table if available, otherwise try the BAR */
305 	if (eth_port) {
306 		cmd->base.port = eth_port->port_type;
307 		cmd->base.speed = eth_port->speed;
308 		cmd->base.duplex = DUPLEX_FULL;
309 		return 0;
310 	}
311 
312 	if (!nfp_netdev_is_nfp_net(netdev))
313 		return -EOPNOTSUPP;
314 	nn = netdev_priv(netdev);
315 
316 	sts = nn_readl(nn, NFP_NET_CFG_STS);
317 
318 	ls = FIELD_GET(NFP_NET_CFG_STS_LINK_RATE, sts);
319 	if (ls == NFP_NET_CFG_STS_LINK_RATE_UNSUPPORTED)
320 		return -EOPNOTSUPP;
321 
322 	if (ls == NFP_NET_CFG_STS_LINK_RATE_UNKNOWN ||
323 	    ls >= ARRAY_SIZE(ls_to_ethtool))
324 		return 0;
325 
326 	cmd->base.speed = ls_to_ethtool[ls];
327 	cmd->base.duplex = DUPLEX_FULL;
328 
329 	return 0;
330 }
331 
332 static int
333 nfp_net_set_link_ksettings(struct net_device *netdev,
334 			   const struct ethtool_link_ksettings *cmd)
335 {
336 	struct nfp_eth_table_port *eth_port;
337 	struct nfp_port *port;
338 	struct nfp_nsp *nsp;
339 	int err;
340 
341 	port = nfp_port_from_netdev(netdev);
342 	eth_port = __nfp_port_get_eth_port(port);
343 	if (!eth_port)
344 		return -EOPNOTSUPP;
345 
346 	if (netif_running(netdev)) {
347 		netdev_warn(netdev, "Changing settings not allowed on an active interface. It may cause the port to be disabled until driver reload.\n");
348 		return -EBUSY;
349 	}
350 
351 	nsp = nfp_eth_config_start(port->app->cpp, eth_port->index);
352 	if (IS_ERR(nsp))
353 		return PTR_ERR(nsp);
354 
355 	err = __nfp_eth_set_aneg(nsp, cmd->base.autoneg == AUTONEG_ENABLE ?
356 				 NFP_ANEG_AUTO : NFP_ANEG_DISABLED);
357 	if (err)
358 		goto err_bad_set;
359 	if (cmd->base.speed != SPEED_UNKNOWN) {
360 		u32 speed = cmd->base.speed / eth_port->lanes;
361 
362 		err = __nfp_eth_set_speed(nsp, speed);
363 		if (err)
364 			goto err_bad_set;
365 	}
366 
367 	err = nfp_eth_config_commit_end(nsp);
368 	if (err > 0)
369 		return 0; /* no change */
370 
371 	nfp_net_refresh_port_table(port);
372 
373 	return err;
374 
375 err_bad_set:
376 	nfp_eth_config_cleanup_end(nsp);
377 	return err;
378 }
379 
380 static void nfp_net_get_ringparam(struct net_device *netdev,
381 				  struct ethtool_ringparam *ring)
382 {
383 	struct nfp_net *nn = netdev_priv(netdev);
384 
385 	ring->rx_max_pending = NFP_NET_MAX_RX_DESCS;
386 	ring->tx_max_pending = NFP_NET_MAX_TX_DESCS;
387 	ring->rx_pending = nn->dp.rxd_cnt;
388 	ring->tx_pending = nn->dp.txd_cnt;
389 }
390 
391 static int nfp_net_set_ring_size(struct nfp_net *nn, u32 rxd_cnt, u32 txd_cnt)
392 {
393 	struct nfp_net_dp *dp;
394 
395 	dp = nfp_net_clone_dp(nn);
396 	if (!dp)
397 		return -ENOMEM;
398 
399 	dp->rxd_cnt = rxd_cnt;
400 	dp->txd_cnt = txd_cnt;
401 
402 	return nfp_net_ring_reconfig(nn, dp, NULL);
403 }
404 
405 static int nfp_net_set_ringparam(struct net_device *netdev,
406 				 struct ethtool_ringparam *ring)
407 {
408 	struct nfp_net *nn = netdev_priv(netdev);
409 	u32 rxd_cnt, txd_cnt;
410 
411 	/* We don't have separate queues/rings for small/large frames. */
412 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
413 		return -EINVAL;
414 
415 	/* Round up to supported values */
416 	rxd_cnt = roundup_pow_of_two(ring->rx_pending);
417 	txd_cnt = roundup_pow_of_two(ring->tx_pending);
418 
419 	if (rxd_cnt < NFP_NET_MIN_RX_DESCS || rxd_cnt > NFP_NET_MAX_RX_DESCS ||
420 	    txd_cnt < NFP_NET_MIN_TX_DESCS || txd_cnt > NFP_NET_MAX_TX_DESCS)
421 		return -EINVAL;
422 
423 	if (nn->dp.rxd_cnt == rxd_cnt && nn->dp.txd_cnt == txd_cnt)
424 		return 0;
425 
426 	nn_dbg(nn, "Change ring size: RxQ %u->%u, TxQ %u->%u\n",
427 	       nn->dp.rxd_cnt, rxd_cnt, nn->dp.txd_cnt, txd_cnt);
428 
429 	return nfp_net_set_ring_size(nn, rxd_cnt, txd_cnt);
430 }
431 
432 static unsigned int nfp_vnic_get_sw_stats_count(struct net_device *netdev)
433 {
434 	struct nfp_net *nn = netdev_priv(netdev);
435 
436 	return NN_RVEC_GATHER_STATS + nn->max_r_vecs * NN_RVEC_PER_Q_STATS +
437 		NN_CTRL_PATH_STATS;
438 }
439 
440 static u8 *nfp_vnic_get_sw_stats_strings(struct net_device *netdev, u8 *data)
441 {
442 	struct nfp_net *nn = netdev_priv(netdev);
443 	int i;
444 
445 	for (i = 0; i < nn->max_r_vecs; i++) {
446 		ethtool_sprintf(&data, "rvec_%u_rx_pkts", i);
447 		ethtool_sprintf(&data, "rvec_%u_tx_pkts", i);
448 		ethtool_sprintf(&data, "rvec_%u_tx_busy", i);
449 	}
450 
451 	ethtool_sprintf(&data, "hw_rx_csum_ok");
452 	ethtool_sprintf(&data, "hw_rx_csum_inner_ok");
453 	ethtool_sprintf(&data, "hw_rx_csum_complete");
454 	ethtool_sprintf(&data, "hw_rx_csum_err");
455 	ethtool_sprintf(&data, "rx_replace_buf_alloc_fail");
456 	ethtool_sprintf(&data, "rx_tls_decrypted_packets");
457 	ethtool_sprintf(&data, "hw_tx_csum");
458 	ethtool_sprintf(&data, "hw_tx_inner_csum");
459 	ethtool_sprintf(&data, "tx_gather");
460 	ethtool_sprintf(&data, "tx_lso");
461 	ethtool_sprintf(&data, "tx_tls_encrypted_packets");
462 	ethtool_sprintf(&data, "tx_tls_ooo");
463 	ethtool_sprintf(&data, "tx_tls_drop_no_sync_data");
464 
465 	ethtool_sprintf(&data, "hw_tls_no_space");
466 	ethtool_sprintf(&data, "rx_tls_resync_req_ok");
467 	ethtool_sprintf(&data, "rx_tls_resync_req_ign");
468 	ethtool_sprintf(&data, "rx_tls_resync_sent");
469 
470 	return data;
471 }
472 
473 static u64 *nfp_vnic_get_sw_stats(struct net_device *netdev, u64 *data)
474 {
475 	u64 gathered_stats[NN_RVEC_GATHER_STATS] = {};
476 	struct nfp_net *nn = netdev_priv(netdev);
477 	u64 tmp[NN_RVEC_GATHER_STATS];
478 	unsigned int i, j;
479 
480 	for (i = 0; i < nn->max_r_vecs; i++) {
481 		unsigned int start;
482 
483 		do {
484 			start = u64_stats_fetch_begin(&nn->r_vecs[i].rx_sync);
485 			data[0] = nn->r_vecs[i].rx_pkts;
486 			tmp[0] = nn->r_vecs[i].hw_csum_rx_ok;
487 			tmp[1] = nn->r_vecs[i].hw_csum_rx_inner_ok;
488 			tmp[2] = nn->r_vecs[i].hw_csum_rx_complete;
489 			tmp[3] = nn->r_vecs[i].hw_csum_rx_error;
490 			tmp[4] = nn->r_vecs[i].rx_replace_buf_alloc_fail;
491 			tmp[5] = nn->r_vecs[i].hw_tls_rx;
492 		} while (u64_stats_fetch_retry(&nn->r_vecs[i].rx_sync, start));
493 
494 		do {
495 			start = u64_stats_fetch_begin(&nn->r_vecs[i].tx_sync);
496 			data[1] = nn->r_vecs[i].tx_pkts;
497 			data[2] = nn->r_vecs[i].tx_busy;
498 			tmp[6] = nn->r_vecs[i].hw_csum_tx;
499 			tmp[7] = nn->r_vecs[i].hw_csum_tx_inner;
500 			tmp[8] = nn->r_vecs[i].tx_gather;
501 			tmp[9] = nn->r_vecs[i].tx_lso;
502 			tmp[10] = nn->r_vecs[i].hw_tls_tx;
503 			tmp[11] = nn->r_vecs[i].tls_tx_fallback;
504 			tmp[12] = nn->r_vecs[i].tls_tx_no_fallback;
505 		} while (u64_stats_fetch_retry(&nn->r_vecs[i].tx_sync, start));
506 
507 		data += NN_RVEC_PER_Q_STATS;
508 
509 		for (j = 0; j < NN_RVEC_GATHER_STATS; j++)
510 			gathered_stats[j] += tmp[j];
511 	}
512 
513 	for (j = 0; j < NN_RVEC_GATHER_STATS; j++)
514 		*data++ = gathered_stats[j];
515 
516 	*data++ = atomic_read(&nn->ktls_no_space);
517 	*data++ = atomic_read(&nn->ktls_rx_resync_req);
518 	*data++ = atomic_read(&nn->ktls_rx_resync_ign);
519 	*data++ = atomic_read(&nn->ktls_rx_resync_sent);
520 
521 	return data;
522 }
523 
524 static unsigned int nfp_vnic_get_hw_stats_count(unsigned int num_vecs)
525 {
526 	return NN_ET_GLOBAL_STATS_LEN + num_vecs * 4;
527 }
528 
529 static u8 *
530 nfp_vnic_get_hw_stats_strings(u8 *data, unsigned int num_vecs, bool repr)
531 {
532 	int swap_off, i;
533 
534 	BUILD_BUG_ON(NN_ET_GLOBAL_STATS_LEN < NN_ET_SWITCH_STATS_LEN * 2);
535 	/* If repr is true first add SWITCH_STATS_LEN and then subtract it
536 	 * effectively swapping the RX and TX statistics (giving us the RX
537 	 * and TX from perspective of the switch).
538 	 */
539 	swap_off = repr * NN_ET_SWITCH_STATS_LEN;
540 
541 	for (i = 0; i < NN_ET_SWITCH_STATS_LEN; i++)
542 		ethtool_sprintf(&data, nfp_net_et_stats[i + swap_off].name);
543 
544 	for (i = NN_ET_SWITCH_STATS_LEN; i < NN_ET_SWITCH_STATS_LEN * 2; i++)
545 		ethtool_sprintf(&data, nfp_net_et_stats[i - swap_off].name);
546 
547 	for (i = NN_ET_SWITCH_STATS_LEN * 2; i < NN_ET_GLOBAL_STATS_LEN; i++)
548 		ethtool_sprintf(&data, nfp_net_et_stats[i].name);
549 
550 	for (i = 0; i < num_vecs; i++) {
551 		ethtool_sprintf(&data, "rxq_%u_pkts", i);
552 		ethtool_sprintf(&data, "rxq_%u_bytes", i);
553 		ethtool_sprintf(&data, "txq_%u_pkts", i);
554 		ethtool_sprintf(&data, "txq_%u_bytes", i);
555 	}
556 
557 	return data;
558 }
559 
560 static u64 *
561 nfp_vnic_get_hw_stats(u64 *data, u8 __iomem *mem, unsigned int num_vecs)
562 {
563 	unsigned int i;
564 
565 	for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++)
566 		*data++ = readq(mem + nfp_net_et_stats[i].off);
567 
568 	for (i = 0; i < num_vecs; i++) {
569 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i));
570 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i) + 8);
571 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i));
572 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i) + 8);
573 	}
574 
575 	return data;
576 }
577 
578 static unsigned int nfp_vnic_get_tlv_stats_count(struct nfp_net *nn)
579 {
580 	return nn->tlv_caps.vnic_stats_cnt + nn->max_r_vecs * 4;
581 }
582 
583 static u8 *nfp_vnic_get_tlv_stats_strings(struct nfp_net *nn, u8 *data)
584 {
585 	unsigned int i, id;
586 	u8 __iomem *mem;
587 	u64 id_word = 0;
588 
589 	mem = nn->dp.ctrl_bar + nn->tlv_caps.vnic_stats_off;
590 	for (i = 0; i < nn->tlv_caps.vnic_stats_cnt; i++) {
591 		if (!(i % 4))
592 			id_word = readq(mem + i * 2);
593 
594 		id = (u16)id_word;
595 		id_word >>= 16;
596 
597 		if (id < ARRAY_SIZE(nfp_tlv_stat_names) &&
598 		    nfp_tlv_stat_names[id][0]) {
599 			memcpy(data, nfp_tlv_stat_names[id], ETH_GSTRING_LEN);
600 			data += ETH_GSTRING_LEN;
601 		} else {
602 			ethtool_sprintf(&data, "dev_unknown_stat%u", id);
603 		}
604 	}
605 
606 	for (i = 0; i < nn->max_r_vecs; i++) {
607 		ethtool_sprintf(&data, "rxq_%u_pkts", i);
608 		ethtool_sprintf(&data, "rxq_%u_bytes", i);
609 		ethtool_sprintf(&data, "txq_%u_pkts", i);
610 		ethtool_sprintf(&data, "txq_%u_bytes", i);
611 	}
612 
613 	return data;
614 }
615 
616 static u64 *nfp_vnic_get_tlv_stats(struct nfp_net *nn, u64 *data)
617 {
618 	u8 __iomem *mem;
619 	unsigned int i;
620 
621 	mem = nn->dp.ctrl_bar + nn->tlv_caps.vnic_stats_off;
622 	mem += roundup(2 * nn->tlv_caps.vnic_stats_cnt, 8);
623 	for (i = 0; i < nn->tlv_caps.vnic_stats_cnt; i++)
624 		*data++ = readq(mem + i * 8);
625 
626 	mem = nn->dp.ctrl_bar;
627 	for (i = 0; i < nn->max_r_vecs; i++) {
628 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i));
629 		*data++ = readq(mem + NFP_NET_CFG_RXR_STATS(i) + 8);
630 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i));
631 		*data++ = readq(mem + NFP_NET_CFG_TXR_STATS(i) + 8);
632 	}
633 
634 	return data;
635 }
636 
637 static unsigned int nfp_mac_get_stats_count(struct net_device *netdev)
638 {
639 	struct nfp_port *port;
640 
641 	port = nfp_port_from_netdev(netdev);
642 	if (!__nfp_port_get_eth_port(port) || !port->eth_stats)
643 		return 0;
644 
645 	return ARRAY_SIZE(nfp_mac_et_stats);
646 }
647 
648 static u8 *nfp_mac_get_stats_strings(struct net_device *netdev, u8 *data)
649 {
650 	struct nfp_port *port;
651 	unsigned int i;
652 
653 	port = nfp_port_from_netdev(netdev);
654 	if (!__nfp_port_get_eth_port(port) || !port->eth_stats)
655 		return data;
656 
657 	for (i = 0; i < ARRAY_SIZE(nfp_mac_et_stats); i++)
658 		ethtool_sprintf(&data, "mac.%s", nfp_mac_et_stats[i].name);
659 
660 	return data;
661 }
662 
663 static u64 *nfp_mac_get_stats(struct net_device *netdev, u64 *data)
664 {
665 	struct nfp_port *port;
666 	unsigned int i;
667 
668 	port = nfp_port_from_netdev(netdev);
669 	if (!__nfp_port_get_eth_port(port) || !port->eth_stats)
670 		return data;
671 
672 	for (i = 0; i < ARRAY_SIZE(nfp_mac_et_stats); i++)
673 		*data++ = readq(port->eth_stats + nfp_mac_et_stats[i].off);
674 
675 	return data;
676 }
677 
678 static void nfp_net_get_strings(struct net_device *netdev,
679 				u32 stringset, u8 *data)
680 {
681 	struct nfp_net *nn = netdev_priv(netdev);
682 
683 	switch (stringset) {
684 	case ETH_SS_STATS:
685 		data = nfp_vnic_get_sw_stats_strings(netdev, data);
686 		if (!nn->tlv_caps.vnic_stats_off)
687 			data = nfp_vnic_get_hw_stats_strings(data,
688 							     nn->max_r_vecs,
689 							     false);
690 		else
691 			data = nfp_vnic_get_tlv_stats_strings(nn, data);
692 		data = nfp_mac_get_stats_strings(netdev, data);
693 		data = nfp_app_port_get_stats_strings(nn->port, data);
694 		break;
695 	}
696 }
697 
698 static void
699 nfp_net_get_stats(struct net_device *netdev, struct ethtool_stats *stats,
700 		  u64 *data)
701 {
702 	struct nfp_net *nn = netdev_priv(netdev);
703 
704 	data = nfp_vnic_get_sw_stats(netdev, data);
705 	if (!nn->tlv_caps.vnic_stats_off)
706 		data = nfp_vnic_get_hw_stats(data, nn->dp.ctrl_bar,
707 					     nn->max_r_vecs);
708 	else
709 		data = nfp_vnic_get_tlv_stats(nn, data);
710 	data = nfp_mac_get_stats(netdev, data);
711 	data = nfp_app_port_get_stats(nn->port, data);
712 }
713 
714 static int nfp_net_get_sset_count(struct net_device *netdev, int sset)
715 {
716 	struct nfp_net *nn = netdev_priv(netdev);
717 	unsigned int cnt;
718 
719 	switch (sset) {
720 	case ETH_SS_STATS:
721 		cnt = nfp_vnic_get_sw_stats_count(netdev);
722 		if (!nn->tlv_caps.vnic_stats_off)
723 			cnt += nfp_vnic_get_hw_stats_count(nn->max_r_vecs);
724 		else
725 			cnt += nfp_vnic_get_tlv_stats_count(nn);
726 		cnt += nfp_mac_get_stats_count(netdev);
727 		cnt += nfp_app_port_get_stats_count(nn->port);
728 		return cnt;
729 	default:
730 		return -EOPNOTSUPP;
731 	}
732 }
733 
734 static void nfp_port_get_strings(struct net_device *netdev,
735 				 u32 stringset, u8 *data)
736 {
737 	struct nfp_port *port = nfp_port_from_netdev(netdev);
738 
739 	switch (stringset) {
740 	case ETH_SS_STATS:
741 		if (nfp_port_is_vnic(port))
742 			data = nfp_vnic_get_hw_stats_strings(data, 0, true);
743 		else
744 			data = nfp_mac_get_stats_strings(netdev, data);
745 		data = nfp_app_port_get_stats_strings(port, data);
746 		break;
747 	}
748 }
749 
750 static void
751 nfp_port_get_stats(struct net_device *netdev, struct ethtool_stats *stats,
752 		   u64 *data)
753 {
754 	struct nfp_port *port = nfp_port_from_netdev(netdev);
755 
756 	if (nfp_port_is_vnic(port))
757 		data = nfp_vnic_get_hw_stats(data, port->vnic, 0);
758 	else
759 		data = nfp_mac_get_stats(netdev, data);
760 	data = nfp_app_port_get_stats(port, data);
761 }
762 
763 static int nfp_port_get_sset_count(struct net_device *netdev, int sset)
764 {
765 	struct nfp_port *port = nfp_port_from_netdev(netdev);
766 	unsigned int count;
767 
768 	switch (sset) {
769 	case ETH_SS_STATS:
770 		if (nfp_port_is_vnic(port))
771 			count = nfp_vnic_get_hw_stats_count(0);
772 		else
773 			count = nfp_mac_get_stats_count(netdev);
774 		count += nfp_app_port_get_stats_count(port);
775 		return count;
776 	default:
777 		return -EOPNOTSUPP;
778 	}
779 }
780 
781 static int nfp_port_fec_ethtool_to_nsp(u32 fec)
782 {
783 	switch (fec) {
784 	case ETHTOOL_FEC_AUTO:
785 		return NFP_FEC_AUTO_BIT;
786 	case ETHTOOL_FEC_OFF:
787 		return NFP_FEC_DISABLED_BIT;
788 	case ETHTOOL_FEC_RS:
789 		return NFP_FEC_REED_SOLOMON_BIT;
790 	case ETHTOOL_FEC_BASER:
791 		return NFP_FEC_BASER_BIT;
792 	default:
793 		/* NSP only supports a single mode at a time */
794 		return -EOPNOTSUPP;
795 	}
796 }
797 
798 static u32 nfp_port_fec_nsp_to_ethtool(u32 fec)
799 {
800 	u32 result = 0;
801 
802 	if (fec & NFP_FEC_AUTO)
803 		result |= ETHTOOL_FEC_AUTO;
804 	if (fec & NFP_FEC_BASER)
805 		result |= ETHTOOL_FEC_BASER;
806 	if (fec & NFP_FEC_REED_SOLOMON)
807 		result |= ETHTOOL_FEC_RS;
808 	if (fec & NFP_FEC_DISABLED)
809 		result |= ETHTOOL_FEC_OFF;
810 
811 	return result ?: ETHTOOL_FEC_NONE;
812 }
813 
814 static int
815 nfp_port_get_fecparam(struct net_device *netdev,
816 		      struct ethtool_fecparam *param)
817 {
818 	struct nfp_eth_table_port *eth_port;
819 	struct nfp_port *port;
820 
821 	param->active_fec = ETHTOOL_FEC_NONE;
822 	param->fec = ETHTOOL_FEC_NONE;
823 
824 	port = nfp_port_from_netdev(netdev);
825 	eth_port = nfp_port_get_eth_port(port);
826 	if (!eth_port)
827 		return -EOPNOTSUPP;
828 
829 	if (!nfp_eth_can_support_fec(eth_port))
830 		return 0;
831 
832 	param->fec = nfp_port_fec_nsp_to_ethtool(eth_port->fec_modes_supported);
833 	param->active_fec = nfp_port_fec_nsp_to_ethtool(eth_port->fec);
834 
835 	return 0;
836 }
837 
838 static int
839 nfp_port_set_fecparam(struct net_device *netdev,
840 		      struct ethtool_fecparam *param)
841 {
842 	struct nfp_eth_table_port *eth_port;
843 	struct nfp_port *port;
844 	int err, fec;
845 
846 	port = nfp_port_from_netdev(netdev);
847 	eth_port = nfp_port_get_eth_port(port);
848 	if (!eth_port)
849 		return -EOPNOTSUPP;
850 
851 	if (!nfp_eth_can_support_fec(eth_port))
852 		return -EOPNOTSUPP;
853 
854 	fec = nfp_port_fec_ethtool_to_nsp(param->fec);
855 	if (fec < 0)
856 		return fec;
857 
858 	err = nfp_eth_set_fec(port->app->cpp, eth_port->index, fec);
859 	if (!err)
860 		/* Only refresh if we did something */
861 		nfp_net_refresh_port_table(port);
862 
863 	return err < 0 ? err : 0;
864 }
865 
866 /* RX network flow classification (RSS, filters, etc)
867  */
868 static u32 ethtool_flow_to_nfp_flag(u32 flow_type)
869 {
870 	static const u32 xlate_ethtool_to_nfp[IPV6_FLOW + 1] = {
871 		[TCP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_TCP,
872 		[TCP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_TCP,
873 		[UDP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_UDP,
874 		[UDP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_UDP,
875 		[IPV4_FLOW]	= NFP_NET_CFG_RSS_IPV4,
876 		[IPV6_FLOW]	= NFP_NET_CFG_RSS_IPV6,
877 	};
878 
879 	if (flow_type >= ARRAY_SIZE(xlate_ethtool_to_nfp))
880 		return 0;
881 
882 	return xlate_ethtool_to_nfp[flow_type];
883 }
884 
885 static int nfp_net_get_rss_hash_opts(struct nfp_net *nn,
886 				     struct ethtool_rxnfc *cmd)
887 {
888 	u32 nfp_rss_flag;
889 
890 	cmd->data = 0;
891 
892 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
893 		return -EOPNOTSUPP;
894 
895 	nfp_rss_flag = ethtool_flow_to_nfp_flag(cmd->flow_type);
896 	if (!nfp_rss_flag)
897 		return -EINVAL;
898 
899 	cmd->data |= RXH_IP_SRC | RXH_IP_DST;
900 	if (nn->rss_cfg & nfp_rss_flag)
901 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
902 
903 	return 0;
904 }
905 
906 static int nfp_net_get_rxnfc(struct net_device *netdev,
907 			     struct ethtool_rxnfc *cmd, u32 *rule_locs)
908 {
909 	struct nfp_net *nn = netdev_priv(netdev);
910 
911 	switch (cmd->cmd) {
912 	case ETHTOOL_GRXRINGS:
913 		cmd->data = nn->dp.num_rx_rings;
914 		return 0;
915 	case ETHTOOL_GRXFH:
916 		return nfp_net_get_rss_hash_opts(nn, cmd);
917 	default:
918 		return -EOPNOTSUPP;
919 	}
920 }
921 
922 static int nfp_net_set_rss_hash_opt(struct nfp_net *nn,
923 				    struct ethtool_rxnfc *nfc)
924 {
925 	u32 new_rss_cfg = nn->rss_cfg;
926 	u32 nfp_rss_flag;
927 	int err;
928 
929 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
930 		return -EOPNOTSUPP;
931 
932 	/* RSS only supports IP SA/DA and L4 src/dst ports  */
933 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
934 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
935 		return -EINVAL;
936 
937 	/* We need at least the IP SA/DA fields for hashing */
938 	if (!(nfc->data & RXH_IP_SRC) ||
939 	    !(nfc->data & RXH_IP_DST))
940 		return -EINVAL;
941 
942 	nfp_rss_flag = ethtool_flow_to_nfp_flag(nfc->flow_type);
943 	if (!nfp_rss_flag)
944 		return -EINVAL;
945 
946 	switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
947 	case 0:
948 		new_rss_cfg &= ~nfp_rss_flag;
949 		break;
950 	case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
951 		new_rss_cfg |= nfp_rss_flag;
952 		break;
953 	default:
954 		return -EINVAL;
955 	}
956 
957 	new_rss_cfg |= FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc);
958 	new_rss_cfg |= NFP_NET_CFG_RSS_MASK;
959 
960 	if (new_rss_cfg == nn->rss_cfg)
961 		return 0;
962 
963 	writel(new_rss_cfg, nn->dp.ctrl_bar + NFP_NET_CFG_RSS_CTRL);
964 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
965 	if (err)
966 		return err;
967 
968 	nn->rss_cfg = new_rss_cfg;
969 
970 	nn_dbg(nn, "Changed RSS config to 0x%x\n", nn->rss_cfg);
971 	return 0;
972 }
973 
974 static int nfp_net_set_rxnfc(struct net_device *netdev,
975 			     struct ethtool_rxnfc *cmd)
976 {
977 	struct nfp_net *nn = netdev_priv(netdev);
978 
979 	switch (cmd->cmd) {
980 	case ETHTOOL_SRXFH:
981 		return nfp_net_set_rss_hash_opt(nn, cmd);
982 	default:
983 		return -EOPNOTSUPP;
984 	}
985 }
986 
987 static u32 nfp_net_get_rxfh_indir_size(struct net_device *netdev)
988 {
989 	struct nfp_net *nn = netdev_priv(netdev);
990 
991 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
992 		return 0;
993 
994 	return ARRAY_SIZE(nn->rss_itbl);
995 }
996 
997 static u32 nfp_net_get_rxfh_key_size(struct net_device *netdev)
998 {
999 	struct nfp_net *nn = netdev_priv(netdev);
1000 
1001 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1002 		return -EOPNOTSUPP;
1003 
1004 	return nfp_net_rss_key_sz(nn);
1005 }
1006 
1007 static int nfp_net_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
1008 			    u8 *hfunc)
1009 {
1010 	struct nfp_net *nn = netdev_priv(netdev);
1011 	int i;
1012 
1013 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY))
1014 		return -EOPNOTSUPP;
1015 
1016 	if (indir)
1017 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
1018 			indir[i] = nn->rss_itbl[i];
1019 	if (key)
1020 		memcpy(key, nn->rss_key, nfp_net_rss_key_sz(nn));
1021 	if (hfunc) {
1022 		*hfunc = nn->rss_hfunc;
1023 		if (*hfunc >= 1 << ETH_RSS_HASH_FUNCS_COUNT)
1024 			*hfunc = ETH_RSS_HASH_UNKNOWN;
1025 	}
1026 
1027 	return 0;
1028 }
1029 
1030 static int nfp_net_set_rxfh(struct net_device *netdev,
1031 			    const u32 *indir, const u8 *key,
1032 			    const u8 hfunc)
1033 {
1034 	struct nfp_net *nn = netdev_priv(netdev);
1035 	int i;
1036 
1037 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS_ANY) ||
1038 	    !(hfunc == ETH_RSS_HASH_NO_CHANGE || hfunc == nn->rss_hfunc))
1039 		return -EOPNOTSUPP;
1040 
1041 	if (!key && !indir)
1042 		return 0;
1043 
1044 	if (key) {
1045 		memcpy(nn->rss_key, key, nfp_net_rss_key_sz(nn));
1046 		nfp_net_rss_write_key(nn);
1047 	}
1048 	if (indir) {
1049 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
1050 			nn->rss_itbl[i] = indir[i];
1051 
1052 		nfp_net_rss_write_itbl(nn);
1053 	}
1054 
1055 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
1056 }
1057 
1058 /* Dump BAR registers
1059  */
1060 static int nfp_net_get_regs_len(struct net_device *netdev)
1061 {
1062 	return NFP_NET_CFG_BAR_SZ;
1063 }
1064 
1065 static void nfp_net_get_regs(struct net_device *netdev,
1066 			     struct ethtool_regs *regs, void *p)
1067 {
1068 	struct nfp_net *nn = netdev_priv(netdev);
1069 	u32 *regs_buf = p;
1070 	int i;
1071 
1072 	regs->version = nn_readl(nn, NFP_NET_CFG_VERSION);
1073 
1074 	for (i = 0; i < NFP_NET_CFG_BAR_SZ / sizeof(u32); i++)
1075 		regs_buf[i] = readl(nn->dp.ctrl_bar + (i * sizeof(u32)));
1076 }
1077 
1078 static int nfp_net_get_coalesce(struct net_device *netdev,
1079 				struct ethtool_coalesce *ec)
1080 {
1081 	struct nfp_net *nn = netdev_priv(netdev);
1082 
1083 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
1084 		return -EINVAL;
1085 
1086 	ec->rx_coalesce_usecs       = nn->rx_coalesce_usecs;
1087 	ec->rx_max_coalesced_frames = nn->rx_coalesce_max_frames;
1088 	ec->tx_coalesce_usecs       = nn->tx_coalesce_usecs;
1089 	ec->tx_max_coalesced_frames = nn->tx_coalesce_max_frames;
1090 
1091 	return 0;
1092 }
1093 
1094 /* Other debug dumps
1095  */
1096 static int
1097 nfp_dump_nsp_diag(struct nfp_app *app, struct ethtool_dump *dump, void *buffer)
1098 {
1099 	struct nfp_resource *res;
1100 	int ret;
1101 
1102 	if (!app)
1103 		return -EOPNOTSUPP;
1104 
1105 	dump->version = 1;
1106 	dump->flag = NFP_DUMP_NSP_DIAG;
1107 
1108 	res = nfp_resource_acquire(app->cpp, NFP_RESOURCE_NSP_DIAG);
1109 	if (IS_ERR(res))
1110 		return PTR_ERR(res);
1111 
1112 	if (buffer) {
1113 		if (dump->len != nfp_resource_size(res)) {
1114 			ret = -EINVAL;
1115 			goto exit_release;
1116 		}
1117 
1118 		ret = nfp_cpp_read(app->cpp, nfp_resource_cpp_id(res),
1119 				   nfp_resource_address(res),
1120 				   buffer, dump->len);
1121 		if (ret != dump->len)
1122 			ret = ret < 0 ? ret : -EIO;
1123 		else
1124 			ret = 0;
1125 	} else {
1126 		dump->len = nfp_resource_size(res);
1127 		ret = 0;
1128 	}
1129 exit_release:
1130 	nfp_resource_release(res);
1131 
1132 	return ret;
1133 }
1134 
1135 /* Set the dump flag/level. Calculate the dump length for flag > 0 only (new TLV
1136  * based dumps), since flag 0 (default) calculates the length in
1137  * nfp_app_get_dump_flag(), and we need to support triggering a level 0 dump
1138  * without setting the flag first, for backward compatibility.
1139  */
1140 static int nfp_app_set_dump(struct net_device *netdev, struct ethtool_dump *val)
1141 {
1142 	struct nfp_app *app = nfp_app_from_netdev(netdev);
1143 	s64 len;
1144 
1145 	if (!app)
1146 		return -EOPNOTSUPP;
1147 
1148 	if (val->flag == NFP_DUMP_NSP_DIAG) {
1149 		app->pf->dump_flag = val->flag;
1150 		return 0;
1151 	}
1152 
1153 	if (!app->pf->dumpspec)
1154 		return -EOPNOTSUPP;
1155 
1156 	len = nfp_net_dump_calculate_size(app->pf, app->pf->dumpspec,
1157 					  val->flag);
1158 	if (len < 0)
1159 		return len;
1160 
1161 	app->pf->dump_flag = val->flag;
1162 	app->pf->dump_len = len;
1163 
1164 	return 0;
1165 }
1166 
1167 static int
1168 nfp_app_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
1169 {
1170 	struct nfp_app *app = nfp_app_from_netdev(netdev);
1171 
1172 	if (!app)
1173 		return -EOPNOTSUPP;
1174 
1175 	if (app->pf->dump_flag == NFP_DUMP_NSP_DIAG)
1176 		return nfp_dump_nsp_diag(app, dump, NULL);
1177 
1178 	dump->flag = app->pf->dump_flag;
1179 	dump->len = app->pf->dump_len;
1180 
1181 	return 0;
1182 }
1183 
1184 static int
1185 nfp_app_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
1186 		      void *buffer)
1187 {
1188 	struct nfp_app *app = nfp_app_from_netdev(netdev);
1189 
1190 	if (!app)
1191 		return -EOPNOTSUPP;
1192 
1193 	if (app->pf->dump_flag == NFP_DUMP_NSP_DIAG)
1194 		return nfp_dump_nsp_diag(app, dump, buffer);
1195 
1196 	dump->flag = app->pf->dump_flag;
1197 	dump->len = app->pf->dump_len;
1198 
1199 	return nfp_net_dump_populate_buffer(app->pf, app->pf->dumpspec, dump,
1200 					    buffer);
1201 }
1202 
1203 static int
1204 nfp_port_get_module_info(struct net_device *netdev,
1205 			 struct ethtool_modinfo *modinfo)
1206 {
1207 	struct nfp_eth_table_port *eth_port;
1208 	struct nfp_port *port;
1209 	unsigned int read_len;
1210 	struct nfp_nsp *nsp;
1211 	int err = 0;
1212 	u8 data;
1213 
1214 	port = nfp_port_from_netdev(netdev);
1215 	eth_port = nfp_port_get_eth_port(port);
1216 	if (!eth_port)
1217 		return -EOPNOTSUPP;
1218 
1219 	nsp = nfp_nsp_open(port->app->cpp);
1220 	if (IS_ERR(nsp)) {
1221 		err = PTR_ERR(nsp);
1222 		netdev_err(netdev, "Failed to access the NSP: %d\n", err);
1223 		return err;
1224 	}
1225 
1226 	if (!nfp_nsp_has_read_module_eeprom(nsp)) {
1227 		netdev_info(netdev, "reading module EEPROM not supported. Please update flash\n");
1228 		err = -EOPNOTSUPP;
1229 		goto exit_close_nsp;
1230 	}
1231 
1232 	switch (eth_port->interface) {
1233 	case NFP_INTERFACE_SFP:
1234 	case NFP_INTERFACE_SFP28:
1235 		err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index,
1236 						 SFP_SFF8472_COMPLIANCE, &data,
1237 						 1, &read_len);
1238 		if (err < 0)
1239 			goto exit_close_nsp;
1240 
1241 		if (!data) {
1242 			modinfo->type = ETH_MODULE_SFF_8079;
1243 			modinfo->eeprom_len = ETH_MODULE_SFF_8079_LEN;
1244 		} else {
1245 			modinfo->type = ETH_MODULE_SFF_8472;
1246 			modinfo->eeprom_len = ETH_MODULE_SFF_8472_LEN;
1247 		}
1248 		break;
1249 	case NFP_INTERFACE_QSFP:
1250 		err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index,
1251 						 SFP_SFF_REV_COMPLIANCE, &data,
1252 						 1, &read_len);
1253 		if (err < 0)
1254 			goto exit_close_nsp;
1255 
1256 		if (data < 0x3) {
1257 			modinfo->type = ETH_MODULE_SFF_8436;
1258 			modinfo->eeprom_len = ETH_MODULE_SFF_8436_LEN;
1259 		} else {
1260 			modinfo->type = ETH_MODULE_SFF_8636;
1261 			modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
1262 		}
1263 		break;
1264 	case NFP_INTERFACE_QSFP28:
1265 		modinfo->type = ETH_MODULE_SFF_8636;
1266 		modinfo->eeprom_len = ETH_MODULE_SFF_8636_LEN;
1267 		break;
1268 	default:
1269 		netdev_err(netdev, "Unsupported module 0x%x detected\n",
1270 			   eth_port->interface);
1271 		err = -EINVAL;
1272 	}
1273 
1274 exit_close_nsp:
1275 	nfp_nsp_close(nsp);
1276 	return err;
1277 }
1278 
1279 static int
1280 nfp_port_get_module_eeprom(struct net_device *netdev,
1281 			   struct ethtool_eeprom *eeprom, u8 *data)
1282 {
1283 	struct nfp_eth_table_port *eth_port;
1284 	struct nfp_port *port;
1285 	struct nfp_nsp *nsp;
1286 	int err;
1287 
1288 	port = nfp_port_from_netdev(netdev);
1289 	eth_port = __nfp_port_get_eth_port(port);
1290 	if (!eth_port)
1291 		return -EOPNOTSUPP;
1292 
1293 	nsp = nfp_nsp_open(port->app->cpp);
1294 	if (IS_ERR(nsp)) {
1295 		err = PTR_ERR(nsp);
1296 		netdev_err(netdev, "Failed to access the NSP: %d\n", err);
1297 		return err;
1298 	}
1299 
1300 	if (!nfp_nsp_has_read_module_eeprom(nsp)) {
1301 		netdev_info(netdev, "reading module EEPROM not supported. Please update flash\n");
1302 		err = -EOPNOTSUPP;
1303 		goto exit_close_nsp;
1304 	}
1305 
1306 	err = nfp_nsp_read_module_eeprom(nsp, eth_port->eth_index,
1307 					 eeprom->offset, data, eeprom->len,
1308 					 &eeprom->len);
1309 	if (err < 0) {
1310 		if (eeprom->len) {
1311 			netdev_warn(netdev,
1312 				    "Incomplete read from module EEPROM: %d\n",
1313 				     err);
1314 			err = 0;
1315 		} else {
1316 			netdev_err(netdev,
1317 				   "Reading from module EEPROM failed: %d\n",
1318 				   err);
1319 		}
1320 	}
1321 
1322 exit_close_nsp:
1323 	nfp_nsp_close(nsp);
1324 	return err;
1325 }
1326 
1327 static int nfp_net_set_coalesce(struct net_device *netdev,
1328 				struct ethtool_coalesce *ec)
1329 {
1330 	struct nfp_net *nn = netdev_priv(netdev);
1331 	unsigned int factor;
1332 
1333 	/* Compute factor used to convert coalesce '_usecs' parameters to
1334 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
1335 	 * count.
1336 	 */
1337 	factor = nn->me_freq_mhz / 16;
1338 
1339 	/* Each pair of (usecs, max_frames) fields specifies that interrupts
1340 	 * should be coalesced until
1341 	 *      (usecs > 0 && time_since_first_completion >= usecs) ||
1342 	 *      (max_frames > 0 && completed_frames >= max_frames)
1343 	 *
1344 	 * It is illegal to set both usecs and max_frames to zero as this would
1345 	 * cause interrupts to never be generated.  To disable coalescing, set
1346 	 * usecs = 0 and max_frames = 1.
1347 	 *
1348 	 * Some implementations ignore the value of max_frames and use the
1349 	 * condition time_since_first_completion >= usecs
1350 	 */
1351 
1352 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
1353 		return -EINVAL;
1354 
1355 	/* ensure valid configuration */
1356 	if (!ec->rx_coalesce_usecs && !ec->rx_max_coalesced_frames)
1357 		return -EINVAL;
1358 
1359 	if (!ec->tx_coalesce_usecs && !ec->tx_max_coalesced_frames)
1360 		return -EINVAL;
1361 
1362 	if (ec->rx_coalesce_usecs * factor >= ((1 << 16) - 1))
1363 		return -EINVAL;
1364 
1365 	if (ec->tx_coalesce_usecs * factor >= ((1 << 16) - 1))
1366 		return -EINVAL;
1367 
1368 	if (ec->rx_max_coalesced_frames >= ((1 << 16) - 1))
1369 		return -EINVAL;
1370 
1371 	if (ec->tx_max_coalesced_frames >= ((1 << 16) - 1))
1372 		return -EINVAL;
1373 
1374 	/* configuration is valid */
1375 	nn->rx_coalesce_usecs      = ec->rx_coalesce_usecs;
1376 	nn->rx_coalesce_max_frames = ec->rx_max_coalesced_frames;
1377 	nn->tx_coalesce_usecs      = ec->tx_coalesce_usecs;
1378 	nn->tx_coalesce_max_frames = ec->tx_max_coalesced_frames;
1379 
1380 	/* write configuration to device */
1381 	nfp_net_coalesce_write_cfg(nn);
1382 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
1383 }
1384 
1385 static void nfp_net_get_channels(struct net_device *netdev,
1386 				 struct ethtool_channels *channel)
1387 {
1388 	struct nfp_net *nn = netdev_priv(netdev);
1389 	unsigned int num_tx_rings;
1390 
1391 	num_tx_rings = nn->dp.num_tx_rings;
1392 	if (nn->dp.xdp_prog)
1393 		num_tx_rings -= nn->dp.num_rx_rings;
1394 
1395 	channel->max_rx = min(nn->max_rx_rings, nn->max_r_vecs);
1396 	channel->max_tx = min(nn->max_tx_rings, nn->max_r_vecs);
1397 	channel->max_combined = min(channel->max_rx, channel->max_tx);
1398 	channel->max_other = NFP_NET_NON_Q_VECTORS;
1399 	channel->combined_count = min(nn->dp.num_rx_rings, num_tx_rings);
1400 	channel->rx_count = nn->dp.num_rx_rings - channel->combined_count;
1401 	channel->tx_count = num_tx_rings - channel->combined_count;
1402 	channel->other_count = NFP_NET_NON_Q_VECTORS;
1403 }
1404 
1405 static int nfp_net_set_num_rings(struct nfp_net *nn, unsigned int total_rx,
1406 				 unsigned int total_tx)
1407 {
1408 	struct nfp_net_dp *dp;
1409 
1410 	dp = nfp_net_clone_dp(nn);
1411 	if (!dp)
1412 		return -ENOMEM;
1413 
1414 	dp->num_rx_rings = total_rx;
1415 	dp->num_tx_rings = total_tx;
1416 	/* nfp_net_check_config() will catch num_tx_rings > nn->max_tx_rings */
1417 	if (dp->xdp_prog)
1418 		dp->num_tx_rings += total_rx;
1419 
1420 	return nfp_net_ring_reconfig(nn, dp, NULL);
1421 }
1422 
1423 static int nfp_net_set_channels(struct net_device *netdev,
1424 				struct ethtool_channels *channel)
1425 {
1426 	struct nfp_net *nn = netdev_priv(netdev);
1427 	unsigned int total_rx, total_tx;
1428 
1429 	/* Reject unsupported */
1430 	if (channel->other_count != NFP_NET_NON_Q_VECTORS ||
1431 	    (channel->rx_count && channel->tx_count))
1432 		return -EINVAL;
1433 
1434 	total_rx = channel->combined_count + channel->rx_count;
1435 	total_tx = channel->combined_count + channel->tx_count;
1436 
1437 	if (total_rx > min(nn->max_rx_rings, nn->max_r_vecs) ||
1438 	    total_tx > min(nn->max_tx_rings, nn->max_r_vecs))
1439 		return -EINVAL;
1440 
1441 	return nfp_net_set_num_rings(nn, total_rx, total_tx);
1442 }
1443 
1444 static const struct ethtool_ops nfp_net_ethtool_ops = {
1445 	.supported_coalesce_params = ETHTOOL_COALESCE_USECS |
1446 				     ETHTOOL_COALESCE_MAX_FRAMES,
1447 	.get_drvinfo		= nfp_net_get_drvinfo,
1448 	.get_link		= ethtool_op_get_link,
1449 	.get_ringparam		= nfp_net_get_ringparam,
1450 	.set_ringparam		= nfp_net_set_ringparam,
1451 	.get_strings		= nfp_net_get_strings,
1452 	.get_ethtool_stats	= nfp_net_get_stats,
1453 	.get_sset_count		= nfp_net_get_sset_count,
1454 	.get_rxnfc		= nfp_net_get_rxnfc,
1455 	.set_rxnfc		= nfp_net_set_rxnfc,
1456 	.get_rxfh_indir_size	= nfp_net_get_rxfh_indir_size,
1457 	.get_rxfh_key_size	= nfp_net_get_rxfh_key_size,
1458 	.get_rxfh		= nfp_net_get_rxfh,
1459 	.set_rxfh		= nfp_net_set_rxfh,
1460 	.get_regs_len		= nfp_net_get_regs_len,
1461 	.get_regs		= nfp_net_get_regs,
1462 	.set_dump		= nfp_app_set_dump,
1463 	.get_dump_flag		= nfp_app_get_dump_flag,
1464 	.get_dump_data		= nfp_app_get_dump_data,
1465 	.get_module_info	= nfp_port_get_module_info,
1466 	.get_module_eeprom	= nfp_port_get_module_eeprom,
1467 	.get_coalesce           = nfp_net_get_coalesce,
1468 	.set_coalesce           = nfp_net_set_coalesce,
1469 	.get_channels		= nfp_net_get_channels,
1470 	.set_channels		= nfp_net_set_channels,
1471 	.get_link_ksettings	= nfp_net_get_link_ksettings,
1472 	.set_link_ksettings	= nfp_net_set_link_ksettings,
1473 	.get_fecparam		= nfp_port_get_fecparam,
1474 	.set_fecparam		= nfp_port_set_fecparam,
1475 };
1476 
1477 const struct ethtool_ops nfp_port_ethtool_ops = {
1478 	.get_drvinfo		= nfp_app_get_drvinfo,
1479 	.get_link		= ethtool_op_get_link,
1480 	.get_strings		= nfp_port_get_strings,
1481 	.get_ethtool_stats	= nfp_port_get_stats,
1482 	.get_sset_count		= nfp_port_get_sset_count,
1483 	.set_dump		= nfp_app_set_dump,
1484 	.get_dump_flag		= nfp_app_get_dump_flag,
1485 	.get_dump_data		= nfp_app_get_dump_data,
1486 	.get_module_info	= nfp_port_get_module_info,
1487 	.get_module_eeprom	= nfp_port_get_module_eeprom,
1488 	.get_link_ksettings	= nfp_net_get_link_ksettings,
1489 	.set_link_ksettings	= nfp_net_set_link_ksettings,
1490 	.get_fecparam		= nfp_port_get_fecparam,
1491 	.set_fecparam		= nfp_port_set_fecparam,
1492 };
1493 
1494 void nfp_net_set_ethtool_ops(struct net_device *netdev)
1495 {
1496 	netdev->ethtool_ops = &nfp_net_ethtool_ops;
1497 }
1498