xref: /linux/drivers/net/ethernet/netronome/nfp/nfp_net_ethtool.c (revision e58e871becec2d3b04ed91c0c16fe8deac9c9dfa)
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_net_ctrl.h"
54 #include "nfp_net.h"
55 
56 enum nfp_dump_diag {
57 	NFP_DUMP_NSP_DIAG = 0,
58 };
59 
60 /* Support for stats. Returns netdev, driver, and device stats */
61 enum { NETDEV_ET_STATS, NFP_NET_DRV_ET_STATS, NFP_NET_DEV_ET_STATS };
62 struct _nfp_net_et_stats {
63 	char name[ETH_GSTRING_LEN];
64 	int type;
65 	int sz;
66 	int off;
67 };
68 
69 #define NN_ET_NETDEV_STAT(m) NETDEV_ET_STATS,			\
70 		FIELD_SIZEOF(struct net_device_stats, m),	\
71 		offsetof(struct net_device_stats, m)
72 /* For stats in the control BAR (other than Q stats) */
73 #define NN_ET_DEV_STAT(m) NFP_NET_DEV_ET_STATS,			\
74 		sizeof(u64),					\
75 		(m)
76 static const struct _nfp_net_et_stats nfp_net_et_stats[] = {
77 	/* netdev stats */
78 	{"rx_packets", NN_ET_NETDEV_STAT(rx_packets)},
79 	{"tx_packets", NN_ET_NETDEV_STAT(tx_packets)},
80 	{"rx_bytes", NN_ET_NETDEV_STAT(rx_bytes)},
81 	{"tx_bytes", NN_ET_NETDEV_STAT(tx_bytes)},
82 	{"rx_errors", NN_ET_NETDEV_STAT(rx_errors)},
83 	{"tx_errors", NN_ET_NETDEV_STAT(tx_errors)},
84 	{"rx_dropped", NN_ET_NETDEV_STAT(rx_dropped)},
85 	{"tx_dropped", NN_ET_NETDEV_STAT(tx_dropped)},
86 	{"multicast", NN_ET_NETDEV_STAT(multicast)},
87 	{"collisions", NN_ET_NETDEV_STAT(collisions)},
88 	{"rx_over_errors", NN_ET_NETDEV_STAT(rx_over_errors)},
89 	{"rx_crc_errors", NN_ET_NETDEV_STAT(rx_crc_errors)},
90 	{"rx_frame_errors", NN_ET_NETDEV_STAT(rx_frame_errors)},
91 	{"rx_fifo_errors", NN_ET_NETDEV_STAT(rx_fifo_errors)},
92 	{"rx_missed_errors", NN_ET_NETDEV_STAT(rx_missed_errors)},
93 	{"tx_aborted_errors", NN_ET_NETDEV_STAT(tx_aborted_errors)},
94 	{"tx_carrier_errors", NN_ET_NETDEV_STAT(tx_carrier_errors)},
95 	{"tx_fifo_errors", NN_ET_NETDEV_STAT(tx_fifo_errors)},
96 	/* Stats from the device */
97 	{"dev_rx_discards", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_DISCARDS)},
98 	{"dev_rx_errors", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_ERRORS)},
99 	{"dev_rx_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_OCTETS)},
100 	{"dev_rx_uc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_UC_OCTETS)},
101 	{"dev_rx_mc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_MC_OCTETS)},
102 	{"dev_rx_bc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_BC_OCTETS)},
103 	{"dev_rx_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_FRAMES)},
104 	{"dev_rx_mc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_MC_FRAMES)},
105 	{"dev_rx_bc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_BC_FRAMES)},
106 
107 	{"dev_tx_discards", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_DISCARDS)},
108 	{"dev_tx_errors", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_ERRORS)},
109 	{"dev_tx_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_OCTETS)},
110 	{"dev_tx_uc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_UC_OCTETS)},
111 	{"dev_tx_mc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_MC_OCTETS)},
112 	{"dev_tx_bc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_BC_OCTETS)},
113 	{"dev_tx_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_FRAMES)},
114 	{"dev_tx_mc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_MC_FRAMES)},
115 	{"dev_tx_bc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_BC_FRAMES)},
116 
117 	{"bpf_pass_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP0_FRAMES)},
118 	{"bpf_pass_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP0_BYTES)},
119 	/* see comments in outro functions in nfp_bpf_jit.c to find out
120 	 * how different BPF modes use app-specific counters
121 	 */
122 	{"bpf_app1_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP1_FRAMES)},
123 	{"bpf_app1_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP1_BYTES)},
124 	{"bpf_app2_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP2_FRAMES)},
125 	{"bpf_app2_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP2_BYTES)},
126 	{"bpf_app3_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP3_FRAMES)},
127 	{"bpf_app3_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP3_BYTES)},
128 };
129 
130 #define NN_ET_GLOBAL_STATS_LEN ARRAY_SIZE(nfp_net_et_stats)
131 #define NN_ET_RVEC_STATS_LEN (nn->dp.num_r_vecs * 3)
132 #define NN_ET_RVEC_GATHER_STATS 7
133 #define NN_ET_QUEUE_STATS_LEN ((nn->dp.num_tx_rings + nn->dp.num_rx_rings) * 2)
134 #define NN_ET_STATS_LEN (NN_ET_GLOBAL_STATS_LEN + NN_ET_RVEC_GATHER_STATS + \
135 			 NN_ET_RVEC_STATS_LEN + NN_ET_QUEUE_STATS_LEN)
136 
137 static void nfp_net_get_nspinfo(struct nfp_net *nn, char *version)
138 {
139 	struct nfp_nsp *nsp;
140 
141 	if (!nn->cpp)
142 		return;
143 
144 	nsp = nfp_nsp_open(nn->cpp);
145 	if (IS_ERR(nsp))
146 		return;
147 
148 	snprintf(version, ETHTOOL_FWVERS_LEN, "sp:%hu.%hu",
149 		 nfp_nsp_get_abi_ver_major(nsp),
150 		 nfp_nsp_get_abi_ver_minor(nsp));
151 
152 	nfp_nsp_close(nsp);
153 }
154 
155 static void nfp_net_get_drvinfo(struct net_device *netdev,
156 				struct ethtool_drvinfo *drvinfo)
157 {
158 	char nsp_version[ETHTOOL_FWVERS_LEN] = {};
159 	struct nfp_net *nn = netdev_priv(netdev);
160 
161 	strlcpy(drvinfo->driver, nn->pdev->driver->name,
162 		sizeof(drvinfo->driver));
163 	strlcpy(drvinfo->version, nfp_driver_version, sizeof(drvinfo->version));
164 
165 	nfp_net_get_nspinfo(nn, nsp_version);
166 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
167 		 "%d.%d.%d.%d %s",
168 		 nn->fw_ver.resv, nn->fw_ver.class,
169 		 nn->fw_ver.major, nn->fw_ver.minor, nsp_version);
170 	strlcpy(drvinfo->bus_info, pci_name(nn->pdev),
171 		sizeof(drvinfo->bus_info));
172 
173 	drvinfo->n_stats = NN_ET_STATS_LEN;
174 	drvinfo->regdump_len = NFP_NET_CFG_BAR_SZ;
175 }
176 
177 /**
178  * nfp_net_get_link_ksettings - Get Link Speed settings
179  * @netdev:	network interface device structure
180  * @cmd:	ethtool command
181  *
182  * Reports speed settings based on info in the BAR provided by the fw.
183  */
184 static int
185 nfp_net_get_link_ksettings(struct net_device *netdev,
186 			   struct ethtool_link_ksettings *cmd)
187 {
188 	static const u32 ls_to_ethtool[] = {
189 		[NFP_NET_CFG_STS_LINK_RATE_UNSUPPORTED]	= 0,
190 		[NFP_NET_CFG_STS_LINK_RATE_UNKNOWN]	= SPEED_UNKNOWN,
191 		[NFP_NET_CFG_STS_LINK_RATE_1G]		= SPEED_1000,
192 		[NFP_NET_CFG_STS_LINK_RATE_10G]		= SPEED_10000,
193 		[NFP_NET_CFG_STS_LINK_RATE_25G]		= SPEED_25000,
194 		[NFP_NET_CFG_STS_LINK_RATE_40G]		= SPEED_40000,
195 		[NFP_NET_CFG_STS_LINK_RATE_50G]		= SPEED_50000,
196 		[NFP_NET_CFG_STS_LINK_RATE_100G]	= SPEED_100000,
197 	};
198 	struct nfp_net *nn = netdev_priv(netdev);
199 	u32 sts, ls;
200 
201 	ethtool_link_ksettings_add_link_mode(cmd, supported, FIBRE);
202 	cmd->base.port = PORT_OTHER;
203 	cmd->base.speed = SPEED_UNKNOWN;
204 	cmd->base.duplex = DUPLEX_UNKNOWN;
205 
206 	if (nn->eth_port)
207 		cmd->base.autoneg = nn->eth_port->aneg != NFP_ANEG_DISABLED ?
208 			AUTONEG_ENABLE : AUTONEG_DISABLE;
209 
210 	if (!netif_carrier_ok(netdev))
211 		return 0;
212 
213 	/* Use link speed from ETH table if available, otherwise try the BAR */
214 	if (nn->eth_port) {
215 		int err;
216 
217 		if (nfp_net_link_changed_read_clear(nn)) {
218 			err = nfp_net_refresh_eth_port(nn);
219 			if (err)
220 				return err;
221 		}
222 
223 		cmd->base.port = nn->eth_port->port_type;
224 		cmd->base.speed = nn->eth_port->speed;
225 		cmd->base.duplex = DUPLEX_FULL;
226 		return 0;
227 	}
228 
229 	sts = nn_readl(nn, NFP_NET_CFG_STS);
230 
231 	ls = FIELD_GET(NFP_NET_CFG_STS_LINK_RATE, sts);
232 	if (ls == NFP_NET_CFG_STS_LINK_RATE_UNSUPPORTED)
233 		return -EOPNOTSUPP;
234 
235 	if (ls == NFP_NET_CFG_STS_LINK_RATE_UNKNOWN ||
236 	    ls >= ARRAY_SIZE(ls_to_ethtool))
237 		return 0;
238 
239 	cmd->base.speed = ls_to_ethtool[sts];
240 	cmd->base.duplex = DUPLEX_FULL;
241 
242 	return 0;
243 }
244 
245 static int
246 nfp_net_set_link_ksettings(struct net_device *netdev,
247 			   const struct ethtool_link_ksettings *cmd)
248 {
249 	struct nfp_net *nn = netdev_priv(netdev);
250 	struct nfp_nsp *nsp;
251 	int err;
252 
253 	if (!nn->eth_port)
254 		return -EOPNOTSUPP;
255 
256 	if (netif_running(netdev)) {
257 		nn_warn(nn, "Changing settings not allowed on an active interface. It may cause the port to be disabled until reboot.\n");
258 		return -EBUSY;
259 	}
260 
261 	nsp = nfp_eth_config_start(nn->cpp, nn->eth_port->index);
262 	if (IS_ERR(nsp))
263 		return PTR_ERR(nsp);
264 
265 	err = __nfp_eth_set_aneg(nsp, cmd->base.autoneg == AUTONEG_ENABLE ?
266 				 NFP_ANEG_AUTO : NFP_ANEG_DISABLED);
267 	if (err)
268 		goto err_bad_set;
269 	if (cmd->base.speed != SPEED_UNKNOWN) {
270 		u32 speed = cmd->base.speed / nn->eth_port->lanes;
271 
272 		err = __nfp_eth_set_speed(nsp, speed);
273 		if (err)
274 			goto err_bad_set;
275 	}
276 
277 	err = nfp_eth_config_commit_end(nsp);
278 	if (err > 0)
279 		return 0; /* no change */
280 
281 	nfp_net_refresh_port_table(nn);
282 
283 	return err;
284 
285 err_bad_set:
286 	nfp_eth_config_cleanup_end(nsp);
287 	return err;
288 }
289 
290 static void nfp_net_get_ringparam(struct net_device *netdev,
291 				  struct ethtool_ringparam *ring)
292 {
293 	struct nfp_net *nn = netdev_priv(netdev);
294 
295 	ring->rx_max_pending = NFP_NET_MAX_RX_DESCS;
296 	ring->tx_max_pending = NFP_NET_MAX_TX_DESCS;
297 	ring->rx_pending = nn->dp.rxd_cnt;
298 	ring->tx_pending = nn->dp.txd_cnt;
299 }
300 
301 static int nfp_net_set_ring_size(struct nfp_net *nn, u32 rxd_cnt, u32 txd_cnt)
302 {
303 	struct nfp_net_dp *dp;
304 
305 	dp = nfp_net_clone_dp(nn);
306 	if (!dp)
307 		return -ENOMEM;
308 
309 	dp->rxd_cnt = rxd_cnt;
310 	dp->txd_cnt = txd_cnt;
311 
312 	return nfp_net_ring_reconfig(nn, dp, NULL);
313 }
314 
315 static int nfp_net_set_ringparam(struct net_device *netdev,
316 				 struct ethtool_ringparam *ring)
317 {
318 	struct nfp_net *nn = netdev_priv(netdev);
319 	u32 rxd_cnt, txd_cnt;
320 
321 	/* We don't have separate queues/rings for small/large frames. */
322 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
323 		return -EINVAL;
324 
325 	/* Round up to supported values */
326 	rxd_cnt = roundup_pow_of_two(ring->rx_pending);
327 	txd_cnt = roundup_pow_of_two(ring->tx_pending);
328 
329 	if (rxd_cnt < NFP_NET_MIN_RX_DESCS || rxd_cnt > NFP_NET_MAX_RX_DESCS ||
330 	    txd_cnt < NFP_NET_MIN_TX_DESCS || txd_cnt > NFP_NET_MAX_TX_DESCS)
331 		return -EINVAL;
332 
333 	if (nn->dp.rxd_cnt == rxd_cnt && nn->dp.txd_cnt == txd_cnt)
334 		return 0;
335 
336 	nn_dbg(nn, "Change ring size: RxQ %u->%u, TxQ %u->%u\n",
337 	       nn->dp.rxd_cnt, rxd_cnt, nn->dp.txd_cnt, txd_cnt);
338 
339 	return nfp_net_set_ring_size(nn, rxd_cnt, txd_cnt);
340 }
341 
342 static void nfp_net_get_strings(struct net_device *netdev,
343 				u32 stringset, u8 *data)
344 {
345 	struct nfp_net *nn = netdev_priv(netdev);
346 	u8 *p = data;
347 	int i;
348 
349 	switch (stringset) {
350 	case ETH_SS_STATS:
351 		for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
352 			memcpy(p, nfp_net_et_stats[i].name, ETH_GSTRING_LEN);
353 			p += ETH_GSTRING_LEN;
354 		}
355 		for (i = 0; i < nn->dp.num_r_vecs; i++) {
356 			sprintf(p, "rvec_%u_rx_pkts", i);
357 			p += ETH_GSTRING_LEN;
358 			sprintf(p, "rvec_%u_tx_pkts", i);
359 			p += ETH_GSTRING_LEN;
360 			sprintf(p, "rvec_%u_tx_busy", i);
361 			p += ETH_GSTRING_LEN;
362 		}
363 		strncpy(p, "hw_rx_csum_ok", ETH_GSTRING_LEN);
364 		p += ETH_GSTRING_LEN;
365 		strncpy(p, "hw_rx_csum_inner_ok", ETH_GSTRING_LEN);
366 		p += ETH_GSTRING_LEN;
367 		strncpy(p, "hw_rx_csum_err", ETH_GSTRING_LEN);
368 		p += ETH_GSTRING_LEN;
369 		strncpy(p, "hw_tx_csum", ETH_GSTRING_LEN);
370 		p += ETH_GSTRING_LEN;
371 		strncpy(p, "hw_tx_inner_csum", ETH_GSTRING_LEN);
372 		p += ETH_GSTRING_LEN;
373 		strncpy(p, "tx_gather", ETH_GSTRING_LEN);
374 		p += ETH_GSTRING_LEN;
375 		strncpy(p, "tx_lso", ETH_GSTRING_LEN);
376 		p += ETH_GSTRING_LEN;
377 		for (i = 0; i < nn->dp.num_tx_rings; i++) {
378 			sprintf(p, "txq_%u_pkts", i);
379 			p += ETH_GSTRING_LEN;
380 			sprintf(p, "txq_%u_bytes", i);
381 			p += ETH_GSTRING_LEN;
382 		}
383 		for (i = 0; i < nn->dp.num_rx_rings; i++) {
384 			sprintf(p, "rxq_%u_pkts", i);
385 			p += ETH_GSTRING_LEN;
386 			sprintf(p, "rxq_%u_bytes", i);
387 			p += ETH_GSTRING_LEN;
388 		}
389 		break;
390 	}
391 }
392 
393 static void nfp_net_get_stats(struct net_device *netdev,
394 			      struct ethtool_stats *stats, u64 *data)
395 {
396 	u64 gathered_stats[NN_ET_RVEC_GATHER_STATS] = {};
397 	struct nfp_net *nn = netdev_priv(netdev);
398 	struct rtnl_link_stats64 *netdev_stats;
399 	struct rtnl_link_stats64 temp = {};
400 	u64 tmp[NN_ET_RVEC_GATHER_STATS];
401 	u8 __iomem *io_p;
402 	int i, j, k;
403 	u8 *p;
404 
405 	netdev_stats = dev_get_stats(netdev, &temp);
406 
407 	for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
408 		switch (nfp_net_et_stats[i].type) {
409 		case NETDEV_ET_STATS:
410 			p = (char *)netdev_stats + nfp_net_et_stats[i].off;
411 			data[i] = nfp_net_et_stats[i].sz == sizeof(u64) ?
412 				*(u64 *)p : *(u32 *)p;
413 			break;
414 
415 		case NFP_NET_DEV_ET_STATS:
416 			io_p = nn->dp.ctrl_bar + nfp_net_et_stats[i].off;
417 			data[i] = readq(io_p);
418 			break;
419 		}
420 	}
421 	for (j = 0; j < nn->dp.num_r_vecs; j++) {
422 		unsigned int start;
423 
424 		do {
425 			start = u64_stats_fetch_begin(&nn->r_vecs[j].rx_sync);
426 			data[i++] = nn->r_vecs[j].rx_pkts;
427 			tmp[0] = nn->r_vecs[j].hw_csum_rx_ok;
428 			tmp[1] = nn->r_vecs[j].hw_csum_rx_inner_ok;
429 			tmp[2] = nn->r_vecs[j].hw_csum_rx_error;
430 		} while (u64_stats_fetch_retry(&nn->r_vecs[j].rx_sync, start));
431 
432 		do {
433 			start = u64_stats_fetch_begin(&nn->r_vecs[j].tx_sync);
434 			data[i++] = nn->r_vecs[j].tx_pkts;
435 			data[i++] = nn->r_vecs[j].tx_busy;
436 			tmp[3] = nn->r_vecs[j].hw_csum_tx;
437 			tmp[4] = nn->r_vecs[j].hw_csum_tx_inner;
438 			tmp[5] = nn->r_vecs[j].tx_gather;
439 			tmp[6] = nn->r_vecs[j].tx_lso;
440 		} while (u64_stats_fetch_retry(&nn->r_vecs[j].tx_sync, start));
441 
442 		for (k = 0; k < NN_ET_RVEC_GATHER_STATS; k++)
443 			gathered_stats[k] += tmp[k];
444 	}
445 	for (j = 0; j < NN_ET_RVEC_GATHER_STATS; j++)
446 		data[i++] = gathered_stats[j];
447 	for (j = 0; j < nn->dp.num_tx_rings; j++) {
448 		io_p = nn->dp.ctrl_bar + NFP_NET_CFG_TXR_STATS(j);
449 		data[i++] = readq(io_p);
450 		io_p = nn->dp.ctrl_bar + NFP_NET_CFG_TXR_STATS(j) + 8;
451 		data[i++] = readq(io_p);
452 	}
453 	for (j = 0; j < nn->dp.num_rx_rings; j++) {
454 		io_p = nn->dp.ctrl_bar + NFP_NET_CFG_RXR_STATS(j);
455 		data[i++] = readq(io_p);
456 		io_p = nn->dp.ctrl_bar + NFP_NET_CFG_RXR_STATS(j) + 8;
457 		data[i++] = readq(io_p);
458 	}
459 }
460 
461 static int nfp_net_get_sset_count(struct net_device *netdev, int sset)
462 {
463 	struct nfp_net *nn = netdev_priv(netdev);
464 
465 	switch (sset) {
466 	case ETH_SS_STATS:
467 		return NN_ET_STATS_LEN;
468 	default:
469 		return -EOPNOTSUPP;
470 	}
471 }
472 
473 /* RX network flow classification (RSS, filters, etc)
474  */
475 static u32 ethtool_flow_to_nfp_flag(u32 flow_type)
476 {
477 	static const u32 xlate_ethtool_to_nfp[IPV6_FLOW + 1] = {
478 		[TCP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_TCP,
479 		[TCP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_TCP,
480 		[UDP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_UDP,
481 		[UDP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_UDP,
482 		[IPV4_FLOW]	= NFP_NET_CFG_RSS_IPV4,
483 		[IPV6_FLOW]	= NFP_NET_CFG_RSS_IPV6,
484 	};
485 
486 	if (flow_type >= ARRAY_SIZE(xlate_ethtool_to_nfp))
487 		return 0;
488 
489 	return xlate_ethtool_to_nfp[flow_type];
490 }
491 
492 static int nfp_net_get_rss_hash_opts(struct nfp_net *nn,
493 				     struct ethtool_rxnfc *cmd)
494 {
495 	u32 nfp_rss_flag;
496 
497 	cmd->data = 0;
498 
499 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
500 		return -EOPNOTSUPP;
501 
502 	nfp_rss_flag = ethtool_flow_to_nfp_flag(cmd->flow_type);
503 	if (!nfp_rss_flag)
504 		return -EINVAL;
505 
506 	cmd->data |= RXH_IP_SRC | RXH_IP_DST;
507 	if (nn->rss_cfg & nfp_rss_flag)
508 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
509 
510 	return 0;
511 }
512 
513 static int nfp_net_get_rxnfc(struct net_device *netdev,
514 			     struct ethtool_rxnfc *cmd, u32 *rule_locs)
515 {
516 	struct nfp_net *nn = netdev_priv(netdev);
517 
518 	switch (cmd->cmd) {
519 	case ETHTOOL_GRXRINGS:
520 		cmd->data = nn->dp.num_rx_rings;
521 		return 0;
522 	case ETHTOOL_GRXFH:
523 		return nfp_net_get_rss_hash_opts(nn, cmd);
524 	default:
525 		return -EOPNOTSUPP;
526 	}
527 }
528 
529 static int nfp_net_set_rss_hash_opt(struct nfp_net *nn,
530 				    struct ethtool_rxnfc *nfc)
531 {
532 	u32 new_rss_cfg = nn->rss_cfg;
533 	u32 nfp_rss_flag;
534 	int err;
535 
536 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
537 		return -EOPNOTSUPP;
538 
539 	/* RSS only supports IP SA/DA and L4 src/dst ports  */
540 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
541 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
542 		return -EINVAL;
543 
544 	/* We need at least the IP SA/DA fields for hashing */
545 	if (!(nfc->data & RXH_IP_SRC) ||
546 	    !(nfc->data & RXH_IP_DST))
547 		return -EINVAL;
548 
549 	nfp_rss_flag = ethtool_flow_to_nfp_flag(nfc->flow_type);
550 	if (!nfp_rss_flag)
551 		return -EINVAL;
552 
553 	switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
554 	case 0:
555 		new_rss_cfg &= ~nfp_rss_flag;
556 		break;
557 	case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
558 		new_rss_cfg |= nfp_rss_flag;
559 		break;
560 	default:
561 		return -EINVAL;
562 	}
563 
564 	new_rss_cfg |= FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc);
565 	new_rss_cfg |= NFP_NET_CFG_RSS_MASK;
566 
567 	if (new_rss_cfg == nn->rss_cfg)
568 		return 0;
569 
570 	writel(new_rss_cfg, nn->dp.ctrl_bar + NFP_NET_CFG_RSS_CTRL);
571 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
572 	if (err)
573 		return err;
574 
575 	nn->rss_cfg = new_rss_cfg;
576 
577 	nn_dbg(nn, "Changed RSS config to 0x%x\n", nn->rss_cfg);
578 	return 0;
579 }
580 
581 static int nfp_net_set_rxnfc(struct net_device *netdev,
582 			     struct ethtool_rxnfc *cmd)
583 {
584 	struct nfp_net *nn = netdev_priv(netdev);
585 
586 	switch (cmd->cmd) {
587 	case ETHTOOL_SRXFH:
588 		return nfp_net_set_rss_hash_opt(nn, cmd);
589 	default:
590 		return -EOPNOTSUPP;
591 	}
592 }
593 
594 static u32 nfp_net_get_rxfh_indir_size(struct net_device *netdev)
595 {
596 	struct nfp_net *nn = netdev_priv(netdev);
597 
598 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
599 		return 0;
600 
601 	return ARRAY_SIZE(nn->rss_itbl);
602 }
603 
604 static u32 nfp_net_get_rxfh_key_size(struct net_device *netdev)
605 {
606 	struct nfp_net *nn = netdev_priv(netdev);
607 
608 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
609 		return -EOPNOTSUPP;
610 
611 	return nfp_net_rss_key_sz(nn);
612 }
613 
614 static int nfp_net_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
615 			    u8 *hfunc)
616 {
617 	struct nfp_net *nn = netdev_priv(netdev);
618 	int i;
619 
620 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
621 		return -EOPNOTSUPP;
622 
623 	if (indir)
624 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
625 			indir[i] = nn->rss_itbl[i];
626 	if (key)
627 		memcpy(key, nn->rss_key, nfp_net_rss_key_sz(nn));
628 	if (hfunc) {
629 		*hfunc = nn->rss_hfunc;
630 		if (*hfunc >= 1 << ETH_RSS_HASH_FUNCS_COUNT)
631 			*hfunc = ETH_RSS_HASH_UNKNOWN;
632 	}
633 
634 	return 0;
635 }
636 
637 static int nfp_net_set_rxfh(struct net_device *netdev,
638 			    const u32 *indir, const u8 *key,
639 			    const u8 hfunc)
640 {
641 	struct nfp_net *nn = netdev_priv(netdev);
642 	int i;
643 
644 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
645 	    !(hfunc == ETH_RSS_HASH_NO_CHANGE || hfunc == nn->rss_hfunc))
646 		return -EOPNOTSUPP;
647 
648 	if (!key && !indir)
649 		return 0;
650 
651 	if (key) {
652 		memcpy(nn->rss_key, key, nfp_net_rss_key_sz(nn));
653 		nfp_net_rss_write_key(nn);
654 	}
655 	if (indir) {
656 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
657 			nn->rss_itbl[i] = indir[i];
658 
659 		nfp_net_rss_write_itbl(nn);
660 	}
661 
662 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
663 }
664 
665 /* Dump BAR registers
666  */
667 static int nfp_net_get_regs_len(struct net_device *netdev)
668 {
669 	return NFP_NET_CFG_BAR_SZ;
670 }
671 
672 static void nfp_net_get_regs(struct net_device *netdev,
673 			     struct ethtool_regs *regs, void *p)
674 {
675 	struct nfp_net *nn = netdev_priv(netdev);
676 	u32 *regs_buf = p;
677 	int i;
678 
679 	regs->version = nn_readl(nn, NFP_NET_CFG_VERSION);
680 
681 	for (i = 0; i < NFP_NET_CFG_BAR_SZ / sizeof(u32); i++)
682 		regs_buf[i] = readl(nn->dp.ctrl_bar + (i * sizeof(u32)));
683 }
684 
685 static int nfp_net_get_coalesce(struct net_device *netdev,
686 				struct ethtool_coalesce *ec)
687 {
688 	struct nfp_net *nn = netdev_priv(netdev);
689 
690 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
691 		return -EINVAL;
692 
693 	ec->rx_coalesce_usecs       = nn->rx_coalesce_usecs;
694 	ec->rx_max_coalesced_frames = nn->rx_coalesce_max_frames;
695 	ec->tx_coalesce_usecs       = nn->tx_coalesce_usecs;
696 	ec->tx_max_coalesced_frames = nn->tx_coalesce_max_frames;
697 
698 	return 0;
699 }
700 
701 /* Other debug dumps
702  */
703 static int
704 nfp_dump_nsp_diag(struct nfp_net *nn, struct ethtool_dump *dump, void *buffer)
705 {
706 	struct nfp_resource *res;
707 	int ret;
708 
709 	if (!nn->cpp)
710 		return -EOPNOTSUPP;
711 
712 	dump->version = 1;
713 	dump->flag = NFP_DUMP_NSP_DIAG;
714 
715 	res = nfp_resource_acquire(nn->cpp, NFP_RESOURCE_NSP_DIAG);
716 	if (IS_ERR(res))
717 		return PTR_ERR(res);
718 
719 	if (buffer) {
720 		if (dump->len != nfp_resource_size(res)) {
721 			ret = -EINVAL;
722 			goto exit_release;
723 		}
724 
725 		ret = nfp_cpp_read(nn->cpp, nfp_resource_cpp_id(res),
726 				   nfp_resource_address(res),
727 				   buffer, dump->len);
728 		if (ret != dump->len)
729 			ret = ret < 0 ? ret : -EIO;
730 		else
731 			ret = 0;
732 	} else {
733 		dump->len = nfp_resource_size(res);
734 		ret = 0;
735 	}
736 exit_release:
737 	nfp_resource_release(res);
738 
739 	return ret;
740 }
741 
742 static int nfp_net_set_dump(struct net_device *netdev, struct ethtool_dump *val)
743 {
744 	struct nfp_net *nn = netdev_priv(netdev);
745 
746 	if (!nn->cpp)
747 		return -EOPNOTSUPP;
748 
749 	if (val->flag != NFP_DUMP_NSP_DIAG)
750 		return -EINVAL;
751 
752 	nn->ethtool_dump_flag = val->flag;
753 
754 	return 0;
755 }
756 
757 static int
758 nfp_net_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
759 {
760 	return nfp_dump_nsp_diag(netdev_priv(netdev), dump, NULL);
761 }
762 
763 static int
764 nfp_net_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
765 		      void *buffer)
766 {
767 	return nfp_dump_nsp_diag(netdev_priv(netdev), dump, buffer);
768 }
769 
770 static int nfp_net_set_coalesce(struct net_device *netdev,
771 				struct ethtool_coalesce *ec)
772 {
773 	struct nfp_net *nn = netdev_priv(netdev);
774 	unsigned int factor;
775 
776 	if (ec->rx_coalesce_usecs_irq ||
777 	    ec->rx_max_coalesced_frames_irq ||
778 	    ec->tx_coalesce_usecs_irq ||
779 	    ec->tx_max_coalesced_frames_irq ||
780 	    ec->stats_block_coalesce_usecs ||
781 	    ec->use_adaptive_rx_coalesce ||
782 	    ec->use_adaptive_tx_coalesce ||
783 	    ec->pkt_rate_low ||
784 	    ec->rx_coalesce_usecs_low ||
785 	    ec->rx_max_coalesced_frames_low ||
786 	    ec->tx_coalesce_usecs_low ||
787 	    ec->tx_max_coalesced_frames_low ||
788 	    ec->pkt_rate_high ||
789 	    ec->rx_coalesce_usecs_high ||
790 	    ec->rx_max_coalesced_frames_high ||
791 	    ec->tx_coalesce_usecs_high ||
792 	    ec->tx_max_coalesced_frames_high ||
793 	    ec->rate_sample_interval)
794 		return -EOPNOTSUPP;
795 
796 	/* Compute factor used to convert coalesce '_usecs' parameters to
797 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
798 	 * count.
799 	 */
800 	factor = nn->me_freq_mhz / 16;
801 
802 	/* Each pair of (usecs, max_frames) fields specifies that interrupts
803 	 * should be coalesced until
804 	 *      (usecs > 0 && time_since_first_completion >= usecs) ||
805 	 *      (max_frames > 0 && completed_frames >= max_frames)
806 	 *
807 	 * It is illegal to set both usecs and max_frames to zero as this would
808 	 * cause interrupts to never be generated.  To disable coalescing, set
809 	 * usecs = 0 and max_frames = 1.
810 	 *
811 	 * Some implementations ignore the value of max_frames and use the
812 	 * condition time_since_first_completion >= usecs
813 	 */
814 
815 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
816 		return -EINVAL;
817 
818 	/* ensure valid configuration */
819 	if (!ec->rx_coalesce_usecs && !ec->rx_max_coalesced_frames)
820 		return -EINVAL;
821 
822 	if (!ec->tx_coalesce_usecs && !ec->tx_max_coalesced_frames)
823 		return -EINVAL;
824 
825 	if (ec->rx_coalesce_usecs * factor >= ((1 << 16) - 1))
826 		return -EINVAL;
827 
828 	if (ec->tx_coalesce_usecs * factor >= ((1 << 16) - 1))
829 		return -EINVAL;
830 
831 	if (ec->rx_max_coalesced_frames >= ((1 << 16) - 1))
832 		return -EINVAL;
833 
834 	if (ec->tx_max_coalesced_frames >= ((1 << 16) - 1))
835 		return -EINVAL;
836 
837 	/* configuration is valid */
838 	nn->rx_coalesce_usecs      = ec->rx_coalesce_usecs;
839 	nn->rx_coalesce_max_frames = ec->rx_max_coalesced_frames;
840 	nn->tx_coalesce_usecs      = ec->tx_coalesce_usecs;
841 	nn->tx_coalesce_max_frames = ec->tx_max_coalesced_frames;
842 
843 	/* write configuration to device */
844 	nfp_net_coalesce_write_cfg(nn);
845 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
846 }
847 
848 static void nfp_net_get_channels(struct net_device *netdev,
849 				 struct ethtool_channels *channel)
850 {
851 	struct nfp_net *nn = netdev_priv(netdev);
852 	unsigned int num_tx_rings;
853 
854 	num_tx_rings = nn->dp.num_tx_rings;
855 	if (nn->dp.xdp_prog)
856 		num_tx_rings -= nn->dp.num_rx_rings;
857 
858 	channel->max_rx = min(nn->max_rx_rings, nn->max_r_vecs);
859 	channel->max_tx = min(nn->max_tx_rings, nn->max_r_vecs);
860 	channel->max_combined = min(channel->max_rx, channel->max_tx);
861 	channel->max_other = NFP_NET_NON_Q_VECTORS;
862 	channel->combined_count = min(nn->dp.num_rx_rings, num_tx_rings);
863 	channel->rx_count = nn->dp.num_rx_rings - channel->combined_count;
864 	channel->tx_count = num_tx_rings - channel->combined_count;
865 	channel->other_count = NFP_NET_NON_Q_VECTORS;
866 }
867 
868 static int nfp_net_set_num_rings(struct nfp_net *nn, unsigned int total_rx,
869 				 unsigned int total_tx)
870 {
871 	struct nfp_net_dp *dp;
872 
873 	dp = nfp_net_clone_dp(nn);
874 	if (!dp)
875 		return -ENOMEM;
876 
877 	dp->num_rx_rings = total_rx;
878 	dp->num_tx_rings = total_tx;
879 	/* nfp_net_check_config() will catch num_tx_rings > nn->max_tx_rings */
880 	if (dp->xdp_prog)
881 		dp->num_tx_rings += total_rx;
882 
883 	return nfp_net_ring_reconfig(nn, dp, NULL);
884 }
885 
886 static int nfp_net_set_channels(struct net_device *netdev,
887 				struct ethtool_channels *channel)
888 {
889 	struct nfp_net *nn = netdev_priv(netdev);
890 	unsigned int total_rx, total_tx;
891 
892 	/* Reject unsupported */
893 	if (!channel->combined_count ||
894 	    channel->other_count != NFP_NET_NON_Q_VECTORS ||
895 	    (channel->rx_count && channel->tx_count))
896 		return -EINVAL;
897 
898 	total_rx = channel->combined_count + channel->rx_count;
899 	total_tx = channel->combined_count + channel->tx_count;
900 
901 	if (total_rx > min(nn->max_rx_rings, nn->max_r_vecs) ||
902 	    total_tx > min(nn->max_tx_rings, nn->max_r_vecs))
903 		return -EINVAL;
904 
905 	return nfp_net_set_num_rings(nn, total_rx, total_tx);
906 }
907 
908 static const struct ethtool_ops nfp_net_ethtool_ops = {
909 	.get_drvinfo		= nfp_net_get_drvinfo,
910 	.get_link		= ethtool_op_get_link,
911 	.get_ringparam		= nfp_net_get_ringparam,
912 	.set_ringparam		= nfp_net_set_ringparam,
913 	.get_strings		= nfp_net_get_strings,
914 	.get_ethtool_stats	= nfp_net_get_stats,
915 	.get_sset_count		= nfp_net_get_sset_count,
916 	.get_rxnfc		= nfp_net_get_rxnfc,
917 	.set_rxnfc		= nfp_net_set_rxnfc,
918 	.get_rxfh_indir_size	= nfp_net_get_rxfh_indir_size,
919 	.get_rxfh_key_size	= nfp_net_get_rxfh_key_size,
920 	.get_rxfh		= nfp_net_get_rxfh,
921 	.set_rxfh		= nfp_net_set_rxfh,
922 	.get_regs_len		= nfp_net_get_regs_len,
923 	.get_regs		= nfp_net_get_regs,
924 	.set_dump		= nfp_net_set_dump,
925 	.get_dump_flag		= nfp_net_get_dump_flag,
926 	.get_dump_data		= nfp_net_get_dump_data,
927 	.get_coalesce           = nfp_net_get_coalesce,
928 	.set_coalesce           = nfp_net_set_coalesce,
929 	.get_channels		= nfp_net_get_channels,
930 	.set_channels		= nfp_net_set_channels,
931 	.get_link_ksettings	= nfp_net_get_link_ksettings,
932 	.set_link_ksettings	= nfp_net_set_link_ksettings,
933 };
934 
935 void nfp_net_set_ethtool_ops(struct net_device *netdev)
936 {
937 	netdev->ethtool_ops = &nfp_net_ethtool_ops;
938 }
939