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