1 /*
2  * Copyright (C) 2015 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/kernel.h>
44 #include <linux/netdevice.h>
45 #include <linux/etherdevice.h>
46 #include <linux/interrupt.h>
47 #include <linux/pci.h>
48 #include <linux/ethtool.h>
49 
50 #include "nfp_net_ctrl.h"
51 #include "nfp_net.h"
52 
53 /* Support for stats. Returns netdev, driver, and device stats */
54 enum { NETDEV_ET_STATS, NFP_NET_DRV_ET_STATS, NFP_NET_DEV_ET_STATS };
55 struct _nfp_net_et_stats {
56 	char name[ETH_GSTRING_LEN];
57 	int type;
58 	int sz;
59 	int off;
60 };
61 
62 #define NN_ET_NETDEV_STAT(m) NETDEV_ET_STATS,			\
63 		FIELD_SIZEOF(struct net_device_stats, m),	\
64 		offsetof(struct net_device_stats, m)
65 /* For stats in the control BAR (other than Q stats) */
66 #define NN_ET_DEV_STAT(m) NFP_NET_DEV_ET_STATS,			\
67 		sizeof(u64),					\
68 		(m)
69 static const struct _nfp_net_et_stats nfp_net_et_stats[] = {
70 	/* netdev stats */
71 	{"rx_packets", NN_ET_NETDEV_STAT(rx_packets)},
72 	{"tx_packets", NN_ET_NETDEV_STAT(tx_packets)},
73 	{"rx_bytes", NN_ET_NETDEV_STAT(rx_bytes)},
74 	{"tx_bytes", NN_ET_NETDEV_STAT(tx_bytes)},
75 	{"rx_errors", NN_ET_NETDEV_STAT(rx_errors)},
76 	{"tx_errors", NN_ET_NETDEV_STAT(tx_errors)},
77 	{"rx_dropped", NN_ET_NETDEV_STAT(rx_dropped)},
78 	{"tx_dropped", NN_ET_NETDEV_STAT(tx_dropped)},
79 	{"multicast", NN_ET_NETDEV_STAT(multicast)},
80 	{"collisions", NN_ET_NETDEV_STAT(collisions)},
81 	{"rx_over_errors", NN_ET_NETDEV_STAT(rx_over_errors)},
82 	{"rx_crc_errors", NN_ET_NETDEV_STAT(rx_crc_errors)},
83 	{"rx_frame_errors", NN_ET_NETDEV_STAT(rx_frame_errors)},
84 	{"rx_fifo_errors", NN_ET_NETDEV_STAT(rx_fifo_errors)},
85 	{"rx_missed_errors", NN_ET_NETDEV_STAT(rx_missed_errors)},
86 	{"tx_aborted_errors", NN_ET_NETDEV_STAT(tx_aborted_errors)},
87 	{"tx_carrier_errors", NN_ET_NETDEV_STAT(tx_carrier_errors)},
88 	{"tx_fifo_errors", NN_ET_NETDEV_STAT(tx_fifo_errors)},
89 	/* Stats from the device */
90 	{"dev_rx_discards", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_DISCARDS)},
91 	{"dev_rx_errors", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_ERRORS)},
92 	{"dev_rx_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_OCTETS)},
93 	{"dev_rx_uc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_UC_OCTETS)},
94 	{"dev_rx_mc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_MC_OCTETS)},
95 	{"dev_rx_bc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_BC_OCTETS)},
96 	{"dev_rx_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_FRAMES)},
97 	{"dev_rx_mc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_MC_FRAMES)},
98 	{"dev_rx_bc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_RX_BC_FRAMES)},
99 
100 	{"dev_tx_discards", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_DISCARDS)},
101 	{"dev_tx_errors", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_ERRORS)},
102 	{"dev_tx_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_OCTETS)},
103 	{"dev_tx_uc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_UC_OCTETS)},
104 	{"dev_tx_mc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_MC_OCTETS)},
105 	{"dev_tx_bc_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_BC_OCTETS)},
106 	{"dev_tx_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_FRAMES)},
107 	{"dev_tx_mc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_MC_FRAMES)},
108 	{"dev_tx_bc_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_TX_BC_FRAMES)},
109 
110 	{"bpf_pass_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP0_FRAMES)},
111 	{"bpf_pass_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP0_BYTES)},
112 	/* see comments in outro functions in nfp_bpf_jit.c to find out
113 	 * how different BPF modes use app-specific counters
114 	 */
115 	{"bpf_app1_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP1_FRAMES)},
116 	{"bpf_app1_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP1_BYTES)},
117 	{"bpf_app2_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP2_FRAMES)},
118 	{"bpf_app2_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP2_BYTES)},
119 	{"bpf_app3_pkts", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP3_FRAMES)},
120 	{"bpf_app3_bytes", NN_ET_DEV_STAT(NFP_NET_CFG_STATS_APP3_BYTES)},
121 };
122 
123 #define NN_ET_GLOBAL_STATS_LEN ARRAY_SIZE(nfp_net_et_stats)
124 #define NN_ET_RVEC_STATS_LEN (nn->num_r_vecs * 3)
125 #define NN_ET_RVEC_GATHER_STATS 7
126 #define NN_ET_QUEUE_STATS_LEN ((nn->num_tx_rings + nn->num_rx_rings) * 2)
127 #define NN_ET_STATS_LEN (NN_ET_GLOBAL_STATS_LEN + NN_ET_RVEC_GATHER_STATS + \
128 			 NN_ET_RVEC_STATS_LEN + NN_ET_QUEUE_STATS_LEN)
129 
130 static void nfp_net_get_drvinfo(struct net_device *netdev,
131 				struct ethtool_drvinfo *drvinfo)
132 {
133 	struct nfp_net *nn = netdev_priv(netdev);
134 
135 	strlcpy(drvinfo->driver, nfp_net_driver_name, sizeof(drvinfo->driver));
136 	strlcpy(drvinfo->version, nfp_net_driver_version,
137 		sizeof(drvinfo->version));
138 
139 	snprintf(drvinfo->fw_version, sizeof(drvinfo->fw_version),
140 		 "%d.%d.%d.%d",
141 		 nn->fw_ver.resv, nn->fw_ver.class,
142 		 nn->fw_ver.major, nn->fw_ver.minor);
143 	strlcpy(drvinfo->bus_info, pci_name(nn->pdev),
144 		sizeof(drvinfo->bus_info));
145 
146 	drvinfo->n_stats = NN_ET_STATS_LEN;
147 	drvinfo->regdump_len = NFP_NET_CFG_BAR_SZ;
148 }
149 
150 static void nfp_net_get_ringparam(struct net_device *netdev,
151 				  struct ethtool_ringparam *ring)
152 {
153 	struct nfp_net *nn = netdev_priv(netdev);
154 
155 	ring->rx_max_pending = NFP_NET_MAX_RX_DESCS;
156 	ring->tx_max_pending = NFP_NET_MAX_TX_DESCS;
157 	ring->rx_pending = nn->rxd_cnt;
158 	ring->tx_pending = nn->txd_cnt;
159 }
160 
161 static int nfp_net_set_ring_size(struct nfp_net *nn, u32 rxd_cnt, u32 txd_cnt)
162 {
163 	struct nfp_net_ring_set *reconfig_rx = NULL, *reconfig_tx = NULL;
164 	struct nfp_net_ring_set rx = {
165 		.n_rings = nn->num_rx_rings,
166 		.mtu = nn->netdev->mtu,
167 		.dcnt = rxd_cnt,
168 	};
169 	struct nfp_net_ring_set tx = {
170 		.n_rings = nn->num_tx_rings,
171 		.dcnt = txd_cnt,
172 	};
173 
174 	if (nn->rxd_cnt != rxd_cnt)
175 		reconfig_rx = &rx;
176 	if (nn->txd_cnt != txd_cnt)
177 		reconfig_tx = &tx;
178 
179 	return nfp_net_ring_reconfig(nn, &nn->xdp_prog,
180 				     reconfig_rx, reconfig_tx);
181 }
182 
183 static int nfp_net_set_ringparam(struct net_device *netdev,
184 				 struct ethtool_ringparam *ring)
185 {
186 	struct nfp_net *nn = netdev_priv(netdev);
187 	u32 rxd_cnt, txd_cnt;
188 
189 	/* We don't have separate queues/rings for small/large frames. */
190 	if (ring->rx_mini_pending || ring->rx_jumbo_pending)
191 		return -EINVAL;
192 
193 	/* Round up to supported values */
194 	rxd_cnt = roundup_pow_of_two(ring->rx_pending);
195 	txd_cnt = roundup_pow_of_two(ring->tx_pending);
196 
197 	if (rxd_cnt < NFP_NET_MIN_RX_DESCS || rxd_cnt > NFP_NET_MAX_RX_DESCS ||
198 	    txd_cnt < NFP_NET_MIN_TX_DESCS || txd_cnt > NFP_NET_MAX_TX_DESCS)
199 		return -EINVAL;
200 
201 	if (nn->rxd_cnt == rxd_cnt && nn->txd_cnt == txd_cnt)
202 		return 0;
203 
204 	nn_dbg(nn, "Change ring size: RxQ %u->%u, TxQ %u->%u\n",
205 	       nn->rxd_cnt, rxd_cnt, nn->txd_cnt, txd_cnt);
206 
207 	return nfp_net_set_ring_size(nn, rxd_cnt, txd_cnt);
208 }
209 
210 static void nfp_net_get_strings(struct net_device *netdev,
211 				u32 stringset, u8 *data)
212 {
213 	struct nfp_net *nn = netdev_priv(netdev);
214 	u8 *p = data;
215 	int i;
216 
217 	switch (stringset) {
218 	case ETH_SS_STATS:
219 		for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
220 			memcpy(p, nfp_net_et_stats[i].name, ETH_GSTRING_LEN);
221 			p += ETH_GSTRING_LEN;
222 		}
223 		for (i = 0; i < nn->num_r_vecs; i++) {
224 			sprintf(p, "rvec_%u_rx_pkts", i);
225 			p += ETH_GSTRING_LEN;
226 			sprintf(p, "rvec_%u_tx_pkts", i);
227 			p += ETH_GSTRING_LEN;
228 			sprintf(p, "rvec_%u_tx_busy", i);
229 			p += ETH_GSTRING_LEN;
230 		}
231 		strncpy(p, "hw_rx_csum_ok", ETH_GSTRING_LEN);
232 		p += ETH_GSTRING_LEN;
233 		strncpy(p, "hw_rx_csum_inner_ok", ETH_GSTRING_LEN);
234 		p += ETH_GSTRING_LEN;
235 		strncpy(p, "hw_rx_csum_err", ETH_GSTRING_LEN);
236 		p += ETH_GSTRING_LEN;
237 		strncpy(p, "hw_tx_csum", ETH_GSTRING_LEN);
238 		p += ETH_GSTRING_LEN;
239 		strncpy(p, "hw_tx_inner_csum", ETH_GSTRING_LEN);
240 		p += ETH_GSTRING_LEN;
241 		strncpy(p, "tx_gather", ETH_GSTRING_LEN);
242 		p += ETH_GSTRING_LEN;
243 		strncpy(p, "tx_lso", ETH_GSTRING_LEN);
244 		p += ETH_GSTRING_LEN;
245 		for (i = 0; i < nn->num_tx_rings; i++) {
246 			sprintf(p, "txq_%u_pkts", i);
247 			p += ETH_GSTRING_LEN;
248 			sprintf(p, "txq_%u_bytes", i);
249 			p += ETH_GSTRING_LEN;
250 		}
251 		for (i = 0; i < nn->num_rx_rings; i++) {
252 			sprintf(p, "rxq_%u_pkts", i);
253 			p += ETH_GSTRING_LEN;
254 			sprintf(p, "rxq_%u_bytes", i);
255 			p += ETH_GSTRING_LEN;
256 		}
257 		break;
258 	}
259 }
260 
261 static void nfp_net_get_stats(struct net_device *netdev,
262 			      struct ethtool_stats *stats, u64 *data)
263 {
264 	u64 gathered_stats[NN_ET_RVEC_GATHER_STATS] = {};
265 	struct nfp_net *nn = netdev_priv(netdev);
266 	struct rtnl_link_stats64 *netdev_stats;
267 	struct rtnl_link_stats64 temp = {};
268 	u64 tmp[NN_ET_RVEC_GATHER_STATS];
269 	u8 __iomem *io_p;
270 	int i, j, k;
271 	u8 *p;
272 
273 	netdev_stats = dev_get_stats(netdev, &temp);
274 
275 	for (i = 0; i < NN_ET_GLOBAL_STATS_LEN; i++) {
276 		switch (nfp_net_et_stats[i].type) {
277 		case NETDEV_ET_STATS:
278 			p = (char *)netdev_stats + nfp_net_et_stats[i].off;
279 			data[i] = nfp_net_et_stats[i].sz == sizeof(u64) ?
280 				*(u64 *)p : *(u32 *)p;
281 			break;
282 
283 		case NFP_NET_DEV_ET_STATS:
284 			io_p = nn->ctrl_bar + nfp_net_et_stats[i].off;
285 			data[i] = readq(io_p);
286 			break;
287 		}
288 	}
289 	for (j = 0; j < nn->num_r_vecs; j++) {
290 		unsigned int start;
291 
292 		do {
293 			start = u64_stats_fetch_begin(&nn->r_vecs[j].rx_sync);
294 			data[i++] = nn->r_vecs[j].rx_pkts;
295 			tmp[0] = nn->r_vecs[j].hw_csum_rx_ok;
296 			tmp[1] = nn->r_vecs[j].hw_csum_rx_inner_ok;
297 			tmp[2] = nn->r_vecs[j].hw_csum_rx_error;
298 		} while (u64_stats_fetch_retry(&nn->r_vecs[j].rx_sync, start));
299 
300 		do {
301 			start = u64_stats_fetch_begin(&nn->r_vecs[j].tx_sync);
302 			data[i++] = nn->r_vecs[j].tx_pkts;
303 			data[i++] = nn->r_vecs[j].tx_busy;
304 			tmp[3] = nn->r_vecs[j].hw_csum_tx;
305 			tmp[4] = nn->r_vecs[j].hw_csum_tx_inner;
306 			tmp[5] = nn->r_vecs[j].tx_gather;
307 			tmp[6] = nn->r_vecs[j].tx_lso;
308 		} while (u64_stats_fetch_retry(&nn->r_vecs[j].tx_sync, start));
309 
310 		for (k = 0; k < NN_ET_RVEC_GATHER_STATS; k++)
311 			gathered_stats[k] += tmp[k];
312 	}
313 	for (j = 0; j < NN_ET_RVEC_GATHER_STATS; j++)
314 		data[i++] = gathered_stats[j];
315 	for (j = 0; j < nn->num_tx_rings; j++) {
316 		io_p = nn->ctrl_bar + NFP_NET_CFG_TXR_STATS(j);
317 		data[i++] = readq(io_p);
318 		io_p = nn->ctrl_bar + NFP_NET_CFG_TXR_STATS(j) + 8;
319 		data[i++] = readq(io_p);
320 	}
321 	for (j = 0; j < nn->num_rx_rings; j++) {
322 		io_p = nn->ctrl_bar + NFP_NET_CFG_RXR_STATS(j);
323 		data[i++] = readq(io_p);
324 		io_p = nn->ctrl_bar + NFP_NET_CFG_RXR_STATS(j) + 8;
325 		data[i++] = readq(io_p);
326 	}
327 }
328 
329 static int nfp_net_get_sset_count(struct net_device *netdev, int sset)
330 {
331 	struct nfp_net *nn = netdev_priv(netdev);
332 
333 	switch (sset) {
334 	case ETH_SS_STATS:
335 		return NN_ET_STATS_LEN;
336 	default:
337 		return -EOPNOTSUPP;
338 	}
339 }
340 
341 /* RX network flow classification (RSS, filters, etc)
342  */
343 static u32 ethtool_flow_to_nfp_flag(u32 flow_type)
344 {
345 	static const u32 xlate_ethtool_to_nfp[IPV6_FLOW + 1] = {
346 		[TCP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_TCP,
347 		[TCP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_TCP,
348 		[UDP_V4_FLOW]	= NFP_NET_CFG_RSS_IPV4_UDP,
349 		[UDP_V6_FLOW]	= NFP_NET_CFG_RSS_IPV6_UDP,
350 		[IPV4_FLOW]	= NFP_NET_CFG_RSS_IPV4,
351 		[IPV6_FLOW]	= NFP_NET_CFG_RSS_IPV6,
352 	};
353 
354 	if (flow_type >= ARRAY_SIZE(xlate_ethtool_to_nfp))
355 		return 0;
356 
357 	return xlate_ethtool_to_nfp[flow_type];
358 }
359 
360 static int nfp_net_get_rss_hash_opts(struct nfp_net *nn,
361 				     struct ethtool_rxnfc *cmd)
362 {
363 	u32 nfp_rss_flag;
364 
365 	cmd->data = 0;
366 
367 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
368 		return -EOPNOTSUPP;
369 
370 	nfp_rss_flag = ethtool_flow_to_nfp_flag(cmd->flow_type);
371 	if (!nfp_rss_flag)
372 		return -EINVAL;
373 
374 	cmd->data |= RXH_IP_SRC | RXH_IP_DST;
375 	if (nn->rss_cfg & nfp_rss_flag)
376 		cmd->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
377 
378 	return 0;
379 }
380 
381 static int nfp_net_get_rxnfc(struct net_device *netdev,
382 			     struct ethtool_rxnfc *cmd, u32 *rule_locs)
383 {
384 	struct nfp_net *nn = netdev_priv(netdev);
385 
386 	switch (cmd->cmd) {
387 	case ETHTOOL_GRXRINGS:
388 		cmd->data = nn->num_rx_rings;
389 		return 0;
390 	case ETHTOOL_GRXFH:
391 		return nfp_net_get_rss_hash_opts(nn, cmd);
392 	default:
393 		return -EOPNOTSUPP;
394 	}
395 }
396 
397 static int nfp_net_set_rss_hash_opt(struct nfp_net *nn,
398 				    struct ethtool_rxnfc *nfc)
399 {
400 	u32 new_rss_cfg = nn->rss_cfg;
401 	u32 nfp_rss_flag;
402 	int err;
403 
404 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
405 		return -EOPNOTSUPP;
406 
407 	/* RSS only supports IP SA/DA and L4 src/dst ports  */
408 	if (nfc->data & ~(RXH_IP_SRC | RXH_IP_DST |
409 			  RXH_L4_B_0_1 | RXH_L4_B_2_3))
410 		return -EINVAL;
411 
412 	/* We need at least the IP SA/DA fields for hashing */
413 	if (!(nfc->data & RXH_IP_SRC) ||
414 	    !(nfc->data & RXH_IP_DST))
415 		return -EINVAL;
416 
417 	nfp_rss_flag = ethtool_flow_to_nfp_flag(nfc->flow_type);
418 	if (!nfp_rss_flag)
419 		return -EINVAL;
420 
421 	switch (nfc->data & (RXH_L4_B_0_1 | RXH_L4_B_2_3)) {
422 	case 0:
423 		new_rss_cfg &= ~nfp_rss_flag;
424 		break;
425 	case (RXH_L4_B_0_1 | RXH_L4_B_2_3):
426 		new_rss_cfg |= nfp_rss_flag;
427 		break;
428 	default:
429 		return -EINVAL;
430 	}
431 
432 	new_rss_cfg |= NFP_NET_CFG_RSS_TOEPLITZ;
433 	new_rss_cfg |= NFP_NET_CFG_RSS_MASK;
434 
435 	if (new_rss_cfg == nn->rss_cfg)
436 		return 0;
437 
438 	writel(new_rss_cfg, nn->ctrl_bar + NFP_NET_CFG_RSS_CTRL);
439 	err = nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
440 	if (err)
441 		return err;
442 
443 	nn->rss_cfg = new_rss_cfg;
444 
445 	nn_dbg(nn, "Changed RSS config to 0x%x\n", nn->rss_cfg);
446 	return 0;
447 }
448 
449 static int nfp_net_set_rxnfc(struct net_device *netdev,
450 			     struct ethtool_rxnfc *cmd)
451 {
452 	struct nfp_net *nn = netdev_priv(netdev);
453 
454 	switch (cmd->cmd) {
455 	case ETHTOOL_SRXFH:
456 		return nfp_net_set_rss_hash_opt(nn, cmd);
457 	default:
458 		return -EOPNOTSUPP;
459 	}
460 }
461 
462 static u32 nfp_net_get_rxfh_indir_size(struct net_device *netdev)
463 {
464 	struct nfp_net *nn = netdev_priv(netdev);
465 
466 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
467 		return 0;
468 
469 	return ARRAY_SIZE(nn->rss_itbl);
470 }
471 
472 static u32 nfp_net_get_rxfh_key_size(struct net_device *netdev)
473 {
474 	return NFP_NET_CFG_RSS_KEY_SZ;
475 }
476 
477 static int nfp_net_get_rxfh(struct net_device *netdev, u32 *indir, u8 *key,
478 			    u8 *hfunc)
479 {
480 	struct nfp_net *nn = netdev_priv(netdev);
481 	int i;
482 
483 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS))
484 		return -EOPNOTSUPP;
485 
486 	if (indir)
487 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
488 			indir[i] = nn->rss_itbl[i];
489 	if (key)
490 		memcpy(key, nn->rss_key, NFP_NET_CFG_RSS_KEY_SZ);
491 	if (hfunc)
492 		*hfunc = ETH_RSS_HASH_TOP;
493 
494 	return 0;
495 }
496 
497 static int nfp_net_set_rxfh(struct net_device *netdev,
498 			    const u32 *indir, const u8 *key,
499 			    const u8 hfunc)
500 {
501 	struct nfp_net *nn = netdev_priv(netdev);
502 	int i;
503 
504 	if (!(nn->cap & NFP_NET_CFG_CTRL_RSS) ||
505 	    !(hfunc == ETH_RSS_HASH_NO_CHANGE || hfunc == ETH_RSS_HASH_TOP))
506 		return -EOPNOTSUPP;
507 
508 	if (!key && !indir)
509 		return 0;
510 
511 	if (key) {
512 		memcpy(nn->rss_key, key, NFP_NET_CFG_RSS_KEY_SZ);
513 		nfp_net_rss_write_key(nn);
514 	}
515 	if (indir) {
516 		for (i = 0; i < ARRAY_SIZE(nn->rss_itbl); i++)
517 			nn->rss_itbl[i] = indir[i];
518 
519 		nfp_net_rss_write_itbl(nn);
520 	}
521 
522 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_RSS);
523 }
524 
525 /* Dump BAR registers
526  */
527 static int nfp_net_get_regs_len(struct net_device *netdev)
528 {
529 	return NFP_NET_CFG_BAR_SZ;
530 }
531 
532 static void nfp_net_get_regs(struct net_device *netdev,
533 			     struct ethtool_regs *regs, void *p)
534 {
535 	struct nfp_net *nn = netdev_priv(netdev);
536 	u32 *regs_buf = p;
537 	int i;
538 
539 	regs->version = nn_readl(nn, NFP_NET_CFG_VERSION);
540 
541 	for (i = 0; i < NFP_NET_CFG_BAR_SZ / sizeof(u32); i++)
542 		regs_buf[i] = readl(nn->ctrl_bar + (i * sizeof(u32)));
543 }
544 
545 static int nfp_net_get_coalesce(struct net_device *netdev,
546 				struct ethtool_coalesce *ec)
547 {
548 	struct nfp_net *nn = netdev_priv(netdev);
549 
550 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
551 		return -EINVAL;
552 
553 	ec->rx_coalesce_usecs       = nn->rx_coalesce_usecs;
554 	ec->rx_max_coalesced_frames = nn->rx_coalesce_max_frames;
555 	ec->tx_coalesce_usecs       = nn->tx_coalesce_usecs;
556 	ec->tx_max_coalesced_frames = nn->tx_coalesce_max_frames;
557 
558 	return 0;
559 }
560 
561 static int nfp_net_set_coalesce(struct net_device *netdev,
562 				struct ethtool_coalesce *ec)
563 {
564 	struct nfp_net *nn = netdev_priv(netdev);
565 	unsigned int factor;
566 
567 	if (ec->rx_coalesce_usecs_irq ||
568 	    ec->rx_max_coalesced_frames_irq ||
569 	    ec->tx_coalesce_usecs_irq ||
570 	    ec->tx_max_coalesced_frames_irq ||
571 	    ec->stats_block_coalesce_usecs ||
572 	    ec->use_adaptive_rx_coalesce ||
573 	    ec->use_adaptive_tx_coalesce ||
574 	    ec->pkt_rate_low ||
575 	    ec->rx_coalesce_usecs_low ||
576 	    ec->rx_max_coalesced_frames_low ||
577 	    ec->tx_coalesce_usecs_low ||
578 	    ec->tx_max_coalesced_frames_low ||
579 	    ec->pkt_rate_high ||
580 	    ec->rx_coalesce_usecs_high ||
581 	    ec->rx_max_coalesced_frames_high ||
582 	    ec->tx_coalesce_usecs_high ||
583 	    ec->tx_max_coalesced_frames_high ||
584 	    ec->rate_sample_interval)
585 		return -ENOTSUPP;
586 
587 	/* Compute factor used to convert coalesce '_usecs' parameters to
588 	 * ME timestamp ticks.  There are 16 ME clock cycles for each timestamp
589 	 * count.
590 	 */
591 	factor = nn->me_freq_mhz / 16;
592 
593 	/* Each pair of (usecs, max_frames) fields specifies that interrupts
594 	 * should be coalesced until
595 	 *      (usecs > 0 && time_since_first_completion >= usecs) ||
596 	 *      (max_frames > 0 && completed_frames >= max_frames)
597 	 *
598 	 * It is illegal to set both usecs and max_frames to zero as this would
599 	 * cause interrupts to never be generated.  To disable coalescing, set
600 	 * usecs = 0 and max_frames = 1.
601 	 *
602 	 * Some implementations ignore the value of max_frames and use the
603 	 * condition time_since_first_completion >= usecs
604 	 */
605 
606 	if (!(nn->cap & NFP_NET_CFG_CTRL_IRQMOD))
607 		return -EINVAL;
608 
609 	/* ensure valid configuration */
610 	if (!ec->rx_coalesce_usecs && !ec->rx_max_coalesced_frames)
611 		return -EINVAL;
612 
613 	if (!ec->tx_coalesce_usecs && !ec->tx_max_coalesced_frames)
614 		return -EINVAL;
615 
616 	if (ec->rx_coalesce_usecs * factor >= ((1 << 16) - 1))
617 		return -EINVAL;
618 
619 	if (ec->tx_coalesce_usecs * factor >= ((1 << 16) - 1))
620 		return -EINVAL;
621 
622 	if (ec->rx_max_coalesced_frames >= ((1 << 16) - 1))
623 		return -EINVAL;
624 
625 	if (ec->tx_max_coalesced_frames >= ((1 << 16) - 1))
626 		return -EINVAL;
627 
628 	/* configuration is valid */
629 	nn->rx_coalesce_usecs      = ec->rx_coalesce_usecs;
630 	nn->rx_coalesce_max_frames = ec->rx_max_coalesced_frames;
631 	nn->tx_coalesce_usecs      = ec->tx_coalesce_usecs;
632 	nn->tx_coalesce_max_frames = ec->tx_max_coalesced_frames;
633 
634 	/* write configuration to device */
635 	nfp_net_coalesce_write_cfg(nn);
636 	return nfp_net_reconfig(nn, NFP_NET_CFG_UPDATE_IRQMOD);
637 }
638 
639 static void nfp_net_get_channels(struct net_device *netdev,
640 				 struct ethtool_channels *channel)
641 {
642 	struct nfp_net *nn = netdev_priv(netdev);
643 	unsigned int num_tx_rings;
644 
645 	num_tx_rings = nn->num_tx_rings;
646 	if (nn->xdp_prog)
647 		num_tx_rings -= nn->num_rx_rings;
648 
649 	channel->max_rx = min(nn->max_rx_rings, nn->max_r_vecs);
650 	channel->max_tx = min(nn->max_tx_rings, nn->max_r_vecs);
651 	channel->max_combined = min(channel->max_rx, channel->max_tx);
652 	channel->max_other = NFP_NET_NON_Q_VECTORS;
653 	channel->combined_count = min(nn->num_rx_rings, num_tx_rings);
654 	channel->rx_count = nn->num_rx_rings - channel->combined_count;
655 	channel->tx_count = num_tx_rings - channel->combined_count;
656 	channel->other_count = NFP_NET_NON_Q_VECTORS;
657 }
658 
659 static int nfp_net_set_num_rings(struct nfp_net *nn, unsigned int total_rx,
660 				 unsigned int total_tx)
661 {
662 	struct nfp_net_ring_set *reconfig_rx = NULL, *reconfig_tx = NULL;
663 	struct nfp_net_ring_set rx = {
664 		.n_rings = total_rx,
665 		.mtu = nn->netdev->mtu,
666 		.dcnt = nn->rxd_cnt,
667 	};
668 	struct nfp_net_ring_set tx = {
669 		.n_rings = total_tx,
670 		.dcnt = nn->txd_cnt,
671 	};
672 
673 	if (nn->num_rx_rings != total_rx)
674 		reconfig_rx = &rx;
675 	if (nn->num_stack_tx_rings != total_tx ||
676 	    (nn->xdp_prog && reconfig_rx))
677 		reconfig_tx = &tx;
678 
679 	/* nfp_net_check_config() will catch tx.n_rings > nn->max_tx_rings */
680 	if (nn->xdp_prog)
681 		tx.n_rings += total_rx;
682 
683 	return nfp_net_ring_reconfig(nn, &nn->xdp_prog,
684 				     reconfig_rx, reconfig_tx);
685 }
686 
687 static int nfp_net_set_channels(struct net_device *netdev,
688 				struct ethtool_channels *channel)
689 {
690 	struct nfp_net *nn = netdev_priv(netdev);
691 	unsigned int total_rx, total_tx;
692 
693 	/* Reject unsupported */
694 	if (!channel->combined_count ||
695 	    channel->other_count != NFP_NET_NON_Q_VECTORS ||
696 	    (channel->rx_count && channel->tx_count))
697 		return -EINVAL;
698 
699 	total_rx = channel->combined_count + channel->rx_count;
700 	total_tx = channel->combined_count + channel->tx_count;
701 
702 	if (total_rx > min(nn->max_rx_rings, nn->max_r_vecs) ||
703 	    total_tx > min(nn->max_tx_rings, nn->max_r_vecs))
704 		return -EINVAL;
705 
706 	return nfp_net_set_num_rings(nn, total_rx, total_tx);
707 }
708 
709 static const struct ethtool_ops nfp_net_ethtool_ops = {
710 	.get_drvinfo		= nfp_net_get_drvinfo,
711 	.get_link		= ethtool_op_get_link,
712 	.get_ringparam		= nfp_net_get_ringparam,
713 	.set_ringparam		= nfp_net_set_ringparam,
714 	.get_strings		= nfp_net_get_strings,
715 	.get_ethtool_stats	= nfp_net_get_stats,
716 	.get_sset_count		= nfp_net_get_sset_count,
717 	.get_rxnfc		= nfp_net_get_rxnfc,
718 	.set_rxnfc		= nfp_net_set_rxnfc,
719 	.get_rxfh_indir_size	= nfp_net_get_rxfh_indir_size,
720 	.get_rxfh_key_size	= nfp_net_get_rxfh_key_size,
721 	.get_rxfh		= nfp_net_get_rxfh,
722 	.set_rxfh		= nfp_net_set_rxfh,
723 	.get_regs_len		= nfp_net_get_regs_len,
724 	.get_regs		= nfp_net_get_regs,
725 	.get_coalesce           = nfp_net_get_coalesce,
726 	.set_coalesce           = nfp_net_set_coalesce,
727 	.get_channels		= nfp_net_get_channels,
728 	.set_channels		= nfp_net_set_channels,
729 };
730 
731 void nfp_net_set_ethtool_ops(struct net_device *netdev)
732 {
733 	netdev->ethtool_ops = &nfp_net_ethtool_ops;
734 }
735