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