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