xref: /openbmc/linux/drivers/net/ethernet/netronome/nfp/nfp_net_ethtool.c (revision 4f139972b489f8bc2c821aa25ac65018d92af3f7)
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 && nfp_net_link_changed_read_clear(nn))
215 		nfp_net_refresh_port_config(nn);
216 	/* Separate if - on FW error the port could've disappeared from table */
217 	if (nn->eth_port) {
218 		cmd->base.port = nn->eth_port->port_type;
219 		cmd->base.speed = nn->eth_port->speed;
220 		cmd->base.duplex = DUPLEX_FULL;
221 		return 0;
222 	}
223 
224 	sts = nn_readl(nn, NFP_NET_CFG_STS);
225 
226 	ls = FIELD_GET(NFP_NET_CFG_STS_LINK_RATE, sts);
227 	if (ls == NFP_NET_CFG_STS_LINK_RATE_UNSUPPORTED)
228 		return -EOPNOTSUPP;
229 
230 	if (ls == NFP_NET_CFG_STS_LINK_RATE_UNKNOWN ||
231 	    ls >= ARRAY_SIZE(ls_to_ethtool))
232 		return 0;
233 
234 	cmd->base.speed = ls_to_ethtool[sts];
235 	cmd->base.duplex = DUPLEX_FULL;
236 
237 	return 0;
238 }
239 
240 static int
241 nfp_net_set_link_ksettings(struct net_device *netdev,
242 			   const struct ethtool_link_ksettings *cmd)
243 {
244 	struct nfp_net *nn = netdev_priv(netdev);
245 	struct nfp_nsp *nsp;
246 	int err;
247 
248 	if (!nn->eth_port)
249 		return -EOPNOTSUPP;
250 
251 	if (netif_running(netdev)) {
252 		nn_warn(nn, "Changing settings not allowed on an active interface. It may cause the port to be disabled until reboot.\n");
253 		return -EBUSY;
254 	}
255 
256 	nsp = nfp_eth_config_start(nn->cpp, nn->eth_port->index);
257 	if (IS_ERR(nsp))
258 		return PTR_ERR(nsp);
259 
260 	err = __nfp_eth_set_aneg(nsp, cmd->base.autoneg == AUTONEG_ENABLE ?
261 				 NFP_ANEG_AUTO : NFP_ANEG_DISABLED);
262 	if (err)
263 		goto err_bad_set;
264 	if (cmd->base.speed != SPEED_UNKNOWN) {
265 		u32 speed = cmd->base.speed / nn->eth_port->lanes;
266 
267 		err = __nfp_eth_set_speed(nsp, speed);
268 		if (err)
269 			goto err_bad_set;
270 	}
271 
272 	err = nfp_eth_config_commit_end(nsp);
273 	if (err > 0)
274 		return 0; /* no change */
275 
276 	nfp_net_refresh_port_config(nn);
277 
278 	return err;
279 
280 err_bad_set:
281 	nfp_eth_config_cleanup_end(nsp);
282 	return err;
283 }
284 
285 static void nfp_net_get_ringparam(struct net_device *netdev,
286 				  struct ethtool_ringparam *ring)
287 {
288 	struct nfp_net *nn = netdev_priv(netdev);
289 
290 	ring->rx_max_pending = NFP_NET_MAX_RX_DESCS;
291 	ring->tx_max_pending = NFP_NET_MAX_TX_DESCS;
292 	ring->rx_pending = nn->dp.rxd_cnt;
293 	ring->tx_pending = nn->dp.txd_cnt;
294 }
295 
296 static int nfp_net_set_ring_size(struct nfp_net *nn, u32 rxd_cnt, u32 txd_cnt)
297 {
298 	struct nfp_net_dp *dp;
299 
300 	dp = nfp_net_clone_dp(nn);
301 	if (!dp)
302 		return -ENOMEM;
303 
304 	dp->rxd_cnt = rxd_cnt;
305 	dp->txd_cnt = txd_cnt;
306 
307 	return nfp_net_ring_reconfig(nn, dp);
308 }
309 
310 static int nfp_net_set_ringparam(struct net_device *netdev,
311 				 struct ethtool_ringparam *ring)
312 {
313 	struct nfp_net *nn = netdev_priv(netdev);
314 	u32 rxd_cnt, txd_cnt;
315 
316 	/* We don't have separate queues/rings for small/large frames. */
317 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
318 		return -EINVAL;
319 
320 	/* Round up to supported values */
321 	rxd_cnt = roundup_pow_of_two(ring->rx_pending);
322 	txd_cnt = roundup_pow_of_two(ring->tx_pending);
323 
324 	if (rxd_cnt < NFP_NET_MIN_RX_DESCS || rxd_cnt > NFP_NET_MAX_RX_DESCS ||
325 	    txd_cnt < NFP_NET_MIN_TX_DESCS || txd_cnt > NFP_NET_MAX_TX_DESCS)
326 		return -EINVAL;
327 
328 	if (nn->dp.rxd_cnt == rxd_cnt && nn->dp.txd_cnt == txd_cnt)
329 		return 0;
330 
331 	nn_dbg(nn, "Change ring size: RxQ %u->%u, TxQ %u->%u\n",
332 	       nn->dp.rxd_cnt, rxd_cnt, nn->dp.txd_cnt, txd_cnt);
333 
334 	return nfp_net_set_ring_size(nn, rxd_cnt, txd_cnt);
335 }
336 
337 static void nfp_net_get_strings(struct net_device *netdev,
338 				u32 stringset, u8 *data)
339 {
340 	struct nfp_net *nn = netdev_priv(netdev);
341 	u8 *p = data;
342 	int i;
343 
344 	switch (stringset) {
345 	case ETH_SS_STATS:
346 		for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
347 			memcpy(p, nfp_net_et_stats[i].name, ETH_GSTRING_LEN);
348 			p += ETH_GSTRING_LEN;
349 		}
350 		for (i = 0; i < nn->dp.num_r_vecs; i++) {
351 			sprintf(p, "rvec_%u_rx_pkts", i);
352 			p += ETH_GSTRING_LEN;
353 			sprintf(p, "rvec_%u_tx_pkts", i);
354 			p += ETH_GSTRING_LEN;
355 			sprintf(p, "rvec_%u_tx_busy", i);
356 			p += ETH_GSTRING_LEN;
357 		}
358 		strncpy(p, "hw_rx_csum_ok", ETH_GSTRING_LEN);
359 		p += ETH_GSTRING_LEN;
360 		strncpy(p, "hw_rx_csum_inner_ok", ETH_GSTRING_LEN);
361 		p += ETH_GSTRING_LEN;
362 		strncpy(p, "hw_rx_csum_err", ETH_GSTRING_LEN);
363 		p += ETH_GSTRING_LEN;
364 		strncpy(p, "hw_tx_csum", ETH_GSTRING_LEN);
365 		p += ETH_GSTRING_LEN;
366 		strncpy(p, "hw_tx_inner_csum", ETH_GSTRING_LEN);
367 		p += ETH_GSTRING_LEN;
368 		strncpy(p, "tx_gather", ETH_GSTRING_LEN);
369 		p += ETH_GSTRING_LEN;
370 		strncpy(p, "tx_lso", ETH_GSTRING_LEN);
371 		p += ETH_GSTRING_LEN;
372 		for (i = 0; i < nn->dp.num_tx_rings; i++) {
373 			sprintf(p, "txq_%u_pkts", i);
374 			p += ETH_GSTRING_LEN;
375 			sprintf(p, "txq_%u_bytes", i);
376 			p += ETH_GSTRING_LEN;
377 		}
378 		for (i = 0; i < nn->dp.num_rx_rings; i++) {
379 			sprintf(p, "rxq_%u_pkts", i);
380 			p += ETH_GSTRING_LEN;
381 			sprintf(p, "rxq_%u_bytes", i);
382 			p += ETH_GSTRING_LEN;
383 		}
384 		break;
385 	}
386 }
387 
388 static void nfp_net_get_stats(struct net_device *netdev,
389 			      struct ethtool_stats *stats, u64 *data)
390 {
391 	u64 gathered_stats[NN_ET_RVEC_GATHER_STATS] = {};
392 	struct nfp_net *nn = netdev_priv(netdev);
393 	struct rtnl_link_stats64 *netdev_stats;
394 	struct rtnl_link_stats64 temp = {};
395 	u64 tmp[NN_ET_RVEC_GATHER_STATS];
396 	u8 __iomem *io_p;
397 	int i, j, k;
398 	u8 *p;
399 
400 	netdev_stats = dev_get_stats(netdev, &temp);
401 
402 	for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
403 		switch (nfp_net_et_stats[i].type) {
404 		case NETDEV_ET_STATS:
405 			p = (char *)netdev_stats + nfp_net_et_stats[i].off;
406 			data[i] = nfp_net_et_stats[i].sz == sizeof(u64) ?
407 				*(u64 *)p : *(u32 *)p;
408 			break;
409 
410 		case NFP_NET_DEV_ET_STATS:
411 			io_p = nn->dp.ctrl_bar + nfp_net_et_stats[i].off;
412 			data[i] = readq(io_p);
413 			break;
414 		}
415 	}
416 	for (j = 0; j < nn->dp.num_r_vecs; j++) {
417 		unsigned int start;
418 
419 		do {
420 			start = u64_stats_fetch_begin(&nn->r_vecs[j].rx_sync);
421 			data[i++] = nn->r_vecs[j].rx_pkts;
422 			tmp[0] = nn->r_vecs[j].hw_csum_rx_ok;
423 			tmp[1] = nn->r_vecs[j].hw_csum_rx_inner_ok;
424 			tmp[2] = nn->r_vecs[j].hw_csum_rx_error;
425 		} while (u64_stats_fetch_retry(&nn->r_vecs[j].rx_sync, start));
426 
427 		do {
428 			start = u64_stats_fetch_begin(&nn->r_vecs[j].tx_sync);
429 			data[i++] = nn->r_vecs[j].tx_pkts;
430 			data[i++] = nn->r_vecs[j].tx_busy;
431 			tmp[3] = nn->r_vecs[j].hw_csum_tx;
432 			tmp[4] = nn->r_vecs[j].hw_csum_tx_inner;
433 			tmp[5] = nn->r_vecs[j].tx_gather;
434 			tmp[6] = nn->r_vecs[j].tx_lso;
435 		} while (u64_stats_fetch_retry(&nn->r_vecs[j].tx_sync, start));
436 
437 		for (k = 0; k < NN_ET_RVEC_GATHER_STATS; k++)
438 			gathered_stats[k] += tmp[k];
439 	}
440 	for (j = 0; j < NN_ET_RVEC_GATHER_STATS; j++)
441 		data[i++] = gathered_stats[j];
442 	for (j = 0; j < nn->dp.num_tx_rings; j++) {
443 		io_p = nn->dp.ctrl_bar + NFP_NET_CFG_TXR_STATS(j);
444 		data[i++] = readq(io_p);
445 		io_p = nn->dp.ctrl_bar + NFP_NET_CFG_TXR_STATS(j) + 8;
446 		data[i++] = readq(io_p);
447 	}
448 	for (j = 0; j < nn->dp.num_rx_rings; j++) {
449 		io_p = nn->dp.ctrl_bar + NFP_NET_CFG_RXR_STATS(j);
450 		data[i++] = readq(io_p);
451 		io_p = nn->dp.ctrl_bar + NFP_NET_CFG_RXR_STATS(j) + 8;
452 		data[i++] = readq(io_p);
453 	}
454 }
455 
456 static int nfp_net_get_sset_count(struct net_device *netdev, int sset)
457 {
458 	struct nfp_net *nn = netdev_priv(netdev);
459 
460 	switch (sset) {
461 	case ETH_SS_STATS:
462 		return NN_ET_STATS_LEN;
463 	default:
464 		return -EOPNOTSUPP;
465 	}
466 }
467 
468 /* RX network flow classification (RSS, filters, etc)
469  */
470 static u32 ethtool_flow_to_nfp_flag(u32 flow_type)
471 {
472 	static const u32 xlate_ethtool_to_nfp[IPV6_FLOW + 1] = {
473 		[TCP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_TCP,
474 		[TCP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_TCP,
475 		[UDP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_UDP,
476 		[UDP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_UDP,
477 		[IPV4_FLOW]	= NFP_NET_CFG_RSS_IPV4,
478 		[IPV6_FLOW]	= NFP_NET_CFG_RSS_IPV6,
479 	};
480 
481 	if (flow_type >= ARRAY_SIZE(xlate_ethtool_to_nfp))
482 		return 0;
483 
484 	return xlate_ethtool_to_nfp[flow_type];
485 }
486 
487 static int nfp_net_get_rss_hash_opts(struct nfp_net *nn,
488 				     struct ethtool_rxnfc *cmd)
489 {
490 	u32 nfp_rss_flag;
491 
492 	cmd->data = 0;
493 
494 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
495 		return -EOPNOTSUPP;
496 
497 	nfp_rss_flag = ethtool_flow_to_nfp_flag(cmd->flow_type);
498 	if (!nfp_rss_flag)
499 		return -EINVAL;
500 
501 	cmd->data |= RXH_IP_SRC | RXH_IP_DST;
502 	if (nn->rss_cfg & nfp_rss_flag)
503 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
504 
505 	return 0;
506 }
507 
508 static int nfp_net_get_rxnfc(struct net_device *netdev,
509 			     struct ethtool_rxnfc *cmd, u32 *rule_locs)
510 {
511 	struct nfp_net *nn = netdev_priv(netdev);
512 
513 	switch (cmd->cmd) {
514 	case ETHTOOL_GRXRINGS:
515 		cmd->data = nn->dp.num_rx_rings;
516 		return 0;
517 	case ETHTOOL_GRXFH:
518 		return nfp_net_get_rss_hash_opts(nn, cmd);
519 	default:
520 		return -EOPNOTSUPP;
521 	}
522 }
523 
524 static int nfp_net_set_rss_hash_opt(struct nfp_net *nn,
525 				    struct ethtool_rxnfc *nfc)
526 {
527 	u32 new_rss_cfg = nn->rss_cfg;
528 	u32 nfp_rss_flag;
529 	int err;
530 
531 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
532 		return -EOPNOTSUPP;
533 
534 	/* RSS only supports IP SA/DA and L4 src/dst ports  */
535 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
536 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
537 		return -EINVAL;
538 
539 	/* We need at least the IP SA/DA fields for hashing */
540 	if (!(nfc->data & RXH_IP_SRC) ||
541 	    !(nfc->data & RXH_IP_DST))
542 		return -EINVAL;
543 
544 	nfp_rss_flag = ethtool_flow_to_nfp_flag(nfc->flow_type);
545 	if (!nfp_rss_flag)
546 		return -EINVAL;
547 
548 	switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
549 	case 0:
550 		new_rss_cfg &= ~nfp_rss_flag;
551 		break;
552 	case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
553 		new_rss_cfg |= nfp_rss_flag;
554 		break;
555 	default:
556 		return -EINVAL;
557 	}
558 
559 	new_rss_cfg |= FIELD_PREP(NFP_NET_CFG_RSS_HFUNC, nn->rss_hfunc);
560 	new_rss_cfg |= NFP_NET_CFG_RSS_MASK;
561 
562 	if (new_rss_cfg == nn->rss_cfg)
563 		return 0;
564 
565 	writel(new_rss_cfg, nn->dp.ctrl_bar + NFP_NET_CFG_RSS_CTRL);
566 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
567 	if (err)
568 		return err;
569 
570 	nn->rss_cfg = new_rss_cfg;
571 
572 	nn_dbg(nn, "Changed RSS config to 0x%x\n", nn->rss_cfg);
573 	return 0;
574 }
575 
576 static int nfp_net_set_rxnfc(struct net_device *netdev,
577 			     struct ethtool_rxnfc *cmd)
578 {
579 	struct nfp_net *nn = netdev_priv(netdev);
580 
581 	switch (cmd->cmd) {
582 	case ETHTOOL_SRXFH:
583 		return nfp_net_set_rss_hash_opt(nn, cmd);
584 	default:
585 		return -EOPNOTSUPP;
586 	}
587 }
588 
589 static u32 nfp_net_get_rxfh_indir_size(struct net_device *netdev)
590 {
591 	struct nfp_net *nn = netdev_priv(netdev);
592 
593 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
594 		return 0;
595 
596 	return ARRAY_SIZE(nn->rss_itbl);
597 }
598 
599 static u32 nfp_net_get_rxfh_key_size(struct net_device *netdev)
600 {
601 	struct nfp_net *nn = netdev_priv(netdev);
602 
603 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
604 		return -EOPNOTSUPP;
605 
606 	return nfp_net_rss_key_sz(nn);
607 }
608 
609 static int nfp_net_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
610 			    u8 *hfunc)
611 {
612 	struct nfp_net *nn = netdev_priv(netdev);
613 	int i;
614 
615 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
616 		return -EOPNOTSUPP;
617 
618 	if (indir)
619 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
620 			indir[i] = nn->rss_itbl[i];
621 	if (key)
622 		memcpy(key, nn->rss_key, nfp_net_rss_key_sz(nn));
623 	if (hfunc) {
624 		*hfunc = nn->rss_hfunc;
625 		if (*hfunc >= 1 << ETH_RSS_HASH_FUNCS_COUNT)
626 			*hfunc = ETH_RSS_HASH_UNKNOWN;
627 	}
628 
629 	return 0;
630 }
631 
632 static int nfp_net_set_rxfh(struct net_device *netdev,
633 			    const u32 *indir, const u8 *key,
634 			    const u8 hfunc)
635 {
636 	struct nfp_net *nn = netdev_priv(netdev);
637 	int i;
638 
639 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
640 	    !(hfunc == ETH_RSS_HASH_NO_CHANGE || hfunc == nn->rss_hfunc))
641 		return -EOPNOTSUPP;
642 
643 	if (!key && !indir)
644 		return 0;
645 
646 	if (key) {
647 		memcpy(nn->rss_key, key, nfp_net_rss_key_sz(nn));
648 		nfp_net_rss_write_key(nn);
649 	}
650 	if (indir) {
651 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
652 			nn->rss_itbl[i] = indir[i];
653 
654 		nfp_net_rss_write_itbl(nn);
655 	}
656 
657 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
658 }
659 
660 /* Dump BAR registers
661  */
662 static int nfp_net_get_regs_len(struct net_device *netdev)
663 {
664 	return NFP_NET_CFG_BAR_SZ;
665 }
666 
667 static void nfp_net_get_regs(struct net_device *netdev,
668 			     struct ethtool_regs *regs, void *p)
669 {
670 	struct nfp_net *nn = netdev_priv(netdev);
671 	u32 *regs_buf = p;
672 	int i;
673 
674 	regs->version = nn_readl(nn, NFP_NET_CFG_VERSION);
675 
676 	for (i = 0; i < NFP_NET_CFG_BAR_SZ / sizeof(u32); i++)
677 		regs_buf[i] = readl(nn->dp.ctrl_bar + (i * sizeof(u32)));
678 }
679 
680 static int nfp_net_get_coalesce(struct net_device *netdev,
681 				struct ethtool_coalesce *ec)
682 {
683 	struct nfp_net *nn = netdev_priv(netdev);
684 
685 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
686 		return -EINVAL;
687 
688 	ec->rx_coalesce_usecs       = nn->rx_coalesce_usecs;
689 	ec->rx_max_coalesced_frames = nn->rx_coalesce_max_frames;
690 	ec->tx_coalesce_usecs       = nn->tx_coalesce_usecs;
691 	ec->tx_max_coalesced_frames = nn->tx_coalesce_max_frames;
692 
693 	return 0;
694 }
695 
696 /* Other debug dumps
697  */
698 static int
699 nfp_dump_nsp_diag(struct nfp_net *nn, struct ethtool_dump *dump, void *buffer)
700 {
701 	struct nfp_resource *res;
702 	int ret;
703 
704 	if (!nn->cpp)
705 		return -EOPNOTSUPP;
706 
707 	dump->version = 1;
708 	dump->flag = NFP_DUMP_NSP_DIAG;
709 
710 	res = nfp_resource_acquire(nn->cpp, NFP_RESOURCE_NSP_DIAG);
711 	if (IS_ERR(res))
712 		return PTR_ERR(res);
713 
714 	if (buffer) {
715 		if (dump->len != nfp_resource_size(res)) {
716 			ret = -EINVAL;
717 			goto exit_release;
718 		}
719 
720 		ret = nfp_cpp_read(nn->cpp, nfp_resource_cpp_id(res),
721 				   nfp_resource_address(res),
722 				   buffer, dump->len);
723 		if (ret != dump->len)
724 			ret = ret < 0 ? ret : -EIO;
725 		else
726 			ret = 0;
727 	} else {
728 		dump->len = nfp_resource_size(res);
729 		ret = 0;
730 	}
731 exit_release:
732 	nfp_resource_release(res);
733 
734 	return ret;
735 }
736 
737 static int nfp_net_set_dump(struct net_device *netdev, struct ethtool_dump *val)
738 {
739 	struct nfp_net *nn = netdev_priv(netdev);
740 
741 	if (!nn->cpp)
742 		return -EOPNOTSUPP;
743 
744 	if (val->flag != NFP_DUMP_NSP_DIAG)
745 		return -EINVAL;
746 
747 	nn->ethtool_dump_flag = val->flag;
748 
749 	return 0;
750 }
751 
752 static int
753 nfp_net_get_dump_flag(struct net_device *netdev, struct ethtool_dump *dump)
754 {
755 	return nfp_dump_nsp_diag(netdev_priv(netdev), dump, NULL);
756 }
757 
758 static int
759 nfp_net_get_dump_data(struct net_device *netdev, struct ethtool_dump *dump,
760 		      void *buffer)
761 {
762 	return nfp_dump_nsp_diag(netdev_priv(netdev), dump, buffer);
763 }
764 
765 static int nfp_net_set_coalesce(struct net_device *netdev,
766 				struct ethtool_coalesce *ec)
767 {
768 	struct nfp_net *nn = netdev_priv(netdev);
769 	unsigned int factor;
770 
771 	if (ec->rx_coalesce_usecs_irq ||
772 	    ec->rx_max_coalesced_frames_irq ||
773 	    ec->tx_coalesce_usecs_irq ||
774 	    ec->tx_max_coalesced_frames_irq ||
775 	    ec->stats_block_coalesce_usecs ||
776 	    ec->use_adaptive_rx_coalesce ||
777 	    ec->use_adaptive_tx_coalesce ||
778 	    ec->pkt_rate_low ||
779 	    ec->rx_coalesce_usecs_low ||
780 	    ec->rx_max_coalesced_frames_low ||
781 	    ec->tx_coalesce_usecs_low ||
782 	    ec->tx_max_coalesced_frames_low ||
783 	    ec->pkt_rate_high ||
784 	    ec->rx_coalesce_usecs_high ||
785 	    ec->rx_max_coalesced_frames_high ||
786 	    ec->tx_coalesce_usecs_high ||
787 	    ec->tx_max_coalesced_frames_high ||
788 	    ec->rate_sample_interval)
789 		return -ENOTSUPP;
790 
791 	/* Compute factor used to convert coalesce '_usecs' parameters to
792 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
793 	 * count.
794 	 */
795 	factor = nn->me_freq_mhz / 16;
796 
797 	/* Each pair of (usecs, max_frames) fields specifies that interrupts
798 	 * should be coalesced until
799 	 *      (usecs > 0 && time_since_first_completion >= usecs) ||
800 	 *      (max_frames > 0 && completed_frames >= max_frames)
801 	 *
802 	 * It is illegal to set both usecs and max_frames to zero as this would
803 	 * cause interrupts to never be generated.  To disable coalescing, set
804 	 * usecs = 0 and max_frames = 1.
805 	 *
806 	 * Some implementations ignore the value of max_frames and use the
807 	 * condition time_since_first_completion >= usecs
808 	 */
809 
810 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
811 		return -EINVAL;
812 
813 	/* ensure valid configuration */
814 	if (!ec->rx_coalesce_usecs && !ec->rx_max_coalesced_frames)
815 		return -EINVAL;
816 
817 	if (!ec->tx_coalesce_usecs && !ec->tx_max_coalesced_frames)
818 		return -EINVAL;
819 
820 	if (ec->rx_coalesce_usecs * factor >= ((1 << 16) - 1))
821 		return -EINVAL;
822 
823 	if (ec->tx_coalesce_usecs * factor >= ((1 << 16) - 1))
824 		return -EINVAL;
825 
826 	if (ec->rx_max_coalesced_frames >= ((1 << 16) - 1))
827 		return -EINVAL;
828 
829 	if (ec->tx_max_coalesced_frames >= ((1 << 16) - 1))
830 		return -EINVAL;
831 
832 	/* configuration is valid */
833 	nn->rx_coalesce_usecs      = ec->rx_coalesce_usecs;
834 	nn->rx_coalesce_max_frames = ec->rx_max_coalesced_frames;
835 	nn->tx_coalesce_usecs      = ec->tx_coalesce_usecs;
836 	nn->tx_coalesce_max_frames = ec->tx_max_coalesced_frames;
837 
838 	/* write configuration to device */
839 	nfp_net_coalesce_write_cfg(nn);
840 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
841 }
842 
843 static void nfp_net_get_channels(struct net_device *netdev,
844 				 struct ethtool_channels *channel)
845 {
846 	struct nfp_net *nn = netdev_priv(netdev);
847 	unsigned int num_tx_rings;
848 
849 	num_tx_rings = nn->dp.num_tx_rings;
850 	if (nn->dp.xdp_prog)
851 		num_tx_rings -= nn->dp.num_rx_rings;
852 
853 	channel->max_rx = min(nn->max_rx_rings, nn->max_r_vecs);
854 	channel->max_tx = min(nn->max_tx_rings, nn->max_r_vecs);
855 	channel->max_combined = min(channel->max_rx, channel->max_tx);
856 	channel->max_other = NFP_NET_NON_Q_VECTORS;
857 	channel->combined_count = min(nn->dp.num_rx_rings, num_tx_rings);
858 	channel->rx_count = nn->dp.num_rx_rings - channel->combined_count;
859 	channel->tx_count = num_tx_rings - channel->combined_count;
860 	channel->other_count = NFP_NET_NON_Q_VECTORS;
861 }
862 
863 static int nfp_net_set_num_rings(struct nfp_net *nn, unsigned int total_rx,
864 				 unsigned int total_tx)
865 {
866 	struct nfp_net_dp *dp;
867 
868 	dp = nfp_net_clone_dp(nn);
869 	if (!dp)
870 		return -ENOMEM;
871 
872 	dp->num_rx_rings = total_rx;
873 	dp->num_tx_rings = total_tx;
874 	/* nfp_net_check_config() will catch num_tx_rings > nn->max_tx_rings */
875 	if (dp->xdp_prog)
876 		dp->num_tx_rings += total_rx;
877 
878 	return nfp_net_ring_reconfig(nn, dp);
879 }
880 
881 static int nfp_net_set_channels(struct net_device *netdev,
882 				struct ethtool_channels *channel)
883 {
884 	struct nfp_net *nn = netdev_priv(netdev);
885 	unsigned int total_rx, total_tx;
886 
887 	/* Reject unsupported */
888 	if (!channel->combined_count ||
889 	    channel->other_count != NFP_NET_NON_Q_VECTORS ||
890 	    (channel->rx_count && channel->tx_count))
891 		return -EINVAL;
892 
893 	total_rx = channel->combined_count + channel->rx_count;
894 	total_tx = channel->combined_count + channel->tx_count;
895 
896 	if (total_rx > min(nn->max_rx_rings, nn->max_r_vecs) ||
897 	    total_tx > min(nn->max_tx_rings, nn->max_r_vecs))
898 		return -EINVAL;
899 
900 	return nfp_net_set_num_rings(nn, total_rx, total_tx);
901 }
902 
903 static const struct ethtool_ops nfp_net_ethtool_ops = {
904 	.get_drvinfo		= nfp_net_get_drvinfo,
905 	.get_link		= ethtool_op_get_link,
906 	.get_ringparam		= nfp_net_get_ringparam,
907 	.set_ringparam		= nfp_net_set_ringparam,
908 	.get_strings		= nfp_net_get_strings,
909 	.get_ethtool_stats	= nfp_net_get_stats,
910 	.get_sset_count		= nfp_net_get_sset_count,
911 	.get_rxnfc		= nfp_net_get_rxnfc,
912 	.set_rxnfc		= nfp_net_set_rxnfc,
913 	.get_rxfh_indir_size	= nfp_net_get_rxfh_indir_size,
914 	.get_rxfh_key_size	= nfp_net_get_rxfh_key_size,
915 	.get_rxfh		= nfp_net_get_rxfh,
916 	.set_rxfh		= nfp_net_set_rxfh,
917 	.get_regs_len		= nfp_net_get_regs_len,
918 	.get_regs		= nfp_net_get_regs,
919 	.set_dump		= nfp_net_set_dump,
920 	.get_dump_flag		= nfp_net_get_dump_flag,
921 	.get_dump_data		= nfp_net_get_dump_data,
922 	.get_coalesce           = nfp_net_get_coalesce,
923 	.set_coalesce           = nfp_net_set_coalesce,
924 	.get_channels		= nfp_net_get_channels,
925 	.set_channels		= nfp_net_set_channels,
926 	.get_link_ksettings	= nfp_net_get_link_ksettings,
927 	.set_link_ksettings	= nfp_net_set_link_ksettings,
928 };
929 
930 void nfp_net_set_ethtool_ops(struct net_device *netdev)
931 {
932 	netdev->ethtool_ops = &nfp_net_ethtool_ops;
933 }
934