1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2019 Solarflare Communications Inc.
5  *
6  * This program is free software; you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License version 2 as published
8  * by the Free Software Foundation, incorporated herein by reference.
9  */
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include "net_driver.h"
13 #include "mcdi.h"
14 #include "nic.h"
15 #include "selftest.h"
16 #include "rx_common.h"
17 #include "ethtool_common.h"
18 #include "mcdi_port_common.h"
19 
20 struct efx_sw_stat_desc {
21 	const char *name;
22 	enum {
23 		EFX_ETHTOOL_STAT_SOURCE_nic,
24 		EFX_ETHTOOL_STAT_SOURCE_channel,
25 		EFX_ETHTOOL_STAT_SOURCE_tx_queue
26 	} source;
27 	unsigned int offset;
28 	u64 (*get_stat)(void *field); /* Reader function */
29 };
30 
31 /* Initialiser for a struct efx_sw_stat_desc with type-checking */
32 #define EFX_ETHTOOL_STAT(stat_name, source_name, field, field_type, \
33 				get_stat_function) {			\
34 	.name = #stat_name,						\
35 	.source = EFX_ETHTOOL_STAT_SOURCE_##source_name,		\
36 	.offset = ((((field_type *) 0) ==				\
37 		      &((struct efx_##source_name *)0)->field) ?	\
38 		    offsetof(struct efx_##source_name, field) :		\
39 		    offsetof(struct efx_##source_name, field)),		\
40 	.get_stat = get_stat_function,					\
41 }
42 
43 static u64 efx_get_uint_stat(void *field)
44 {
45 	return *(unsigned int *)field;
46 }
47 
48 static u64 efx_get_atomic_stat(void *field)
49 {
50 	return atomic_read((atomic_t *) field);
51 }
52 
53 #define EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(field)		\
54 	EFX_ETHTOOL_STAT(field, nic, field,			\
55 			 atomic_t, efx_get_atomic_stat)
56 
57 #define EFX_ETHTOOL_UINT_CHANNEL_STAT(field)			\
58 	EFX_ETHTOOL_STAT(field, channel, n_##field,		\
59 			 unsigned int, efx_get_uint_stat)
60 #define EFX_ETHTOOL_UINT_CHANNEL_STAT_NO_N(field)		\
61 	EFX_ETHTOOL_STAT(field, channel, field,			\
62 			 unsigned int, efx_get_uint_stat)
63 
64 #define EFX_ETHTOOL_UINT_TXQ_STAT(field)			\
65 	EFX_ETHTOOL_STAT(tx_##field, tx_queue, field,		\
66 			 unsigned int, efx_get_uint_stat)
67 
68 static const struct efx_sw_stat_desc efx_sw_stat_desc[] = {
69 	EFX_ETHTOOL_UINT_TXQ_STAT(merge_events),
70 	EFX_ETHTOOL_UINT_TXQ_STAT(tso_bursts),
71 	EFX_ETHTOOL_UINT_TXQ_STAT(tso_long_headers),
72 	EFX_ETHTOOL_UINT_TXQ_STAT(tso_packets),
73 	EFX_ETHTOOL_UINT_TXQ_STAT(tso_fallbacks),
74 	EFX_ETHTOOL_UINT_TXQ_STAT(pushes),
75 	EFX_ETHTOOL_UINT_TXQ_STAT(pio_packets),
76 	EFX_ETHTOOL_UINT_TXQ_STAT(cb_packets),
77 	EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(rx_reset),
78 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tobe_disc),
79 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_ip_hdr_chksum_err),
80 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tcp_udp_chksum_err),
81 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_inner_ip_hdr_chksum_err),
82 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_inner_tcp_udp_chksum_err),
83 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_outer_ip_hdr_chksum_err),
84 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_outer_tcp_udp_chksum_err),
85 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_eth_crc_err),
86 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_mcast_mismatch),
87 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_frm_trunc),
88 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_events),
89 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_packets),
90 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_drops),
91 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_bad_drops),
92 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_tx),
93 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_redirect),
94 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_mport_bad),
95 #ifdef CONFIG_RFS_ACCEL
96 	EFX_ETHTOOL_UINT_CHANNEL_STAT_NO_N(rfs_filter_count),
97 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rfs_succeeded),
98 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rfs_failed),
99 #endif
100 };
101 
102 #define EFX_ETHTOOL_SW_STAT_COUNT ARRAY_SIZE(efx_sw_stat_desc)
103 
104 static const char efx_ethtool_priv_flags_strings[][ETH_GSTRING_LEN] = {
105 	"log-tc-errors",
106 };
107 
108 #define EFX_ETHTOOL_PRIV_FLAGS_LOG_TC_ERRS		BIT(0)
109 
110 #define EFX_ETHTOOL_PRIV_FLAGS_COUNT ARRAY_SIZE(efx_ethtool_priv_flags_strings)
111 
112 void efx_ethtool_get_drvinfo(struct net_device *net_dev,
113 			     struct ethtool_drvinfo *info)
114 {
115 	struct efx_nic *efx = efx_netdev_priv(net_dev);
116 
117 	strscpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
118 	efx_mcdi_print_fwver(efx, info->fw_version,
119 			     sizeof(info->fw_version));
120 	strscpy(info->bus_info, pci_name(efx->pci_dev), sizeof(info->bus_info));
121 }
122 
123 u32 efx_ethtool_get_msglevel(struct net_device *net_dev)
124 {
125 	struct efx_nic *efx = efx_netdev_priv(net_dev);
126 
127 	return efx->msg_enable;
128 }
129 
130 void efx_ethtool_set_msglevel(struct net_device *net_dev, u32 msg_enable)
131 {
132 	struct efx_nic *efx = efx_netdev_priv(net_dev);
133 
134 	efx->msg_enable = msg_enable;
135 }
136 
137 void efx_ethtool_self_test(struct net_device *net_dev,
138 			   struct ethtool_test *test, u64 *data)
139 {
140 	struct efx_nic *efx = efx_netdev_priv(net_dev);
141 	struct efx_self_tests *efx_tests;
142 	bool already_up;
143 	int rc = -ENOMEM;
144 
145 	efx_tests = kzalloc(sizeof(*efx_tests), GFP_KERNEL);
146 	if (!efx_tests)
147 		goto fail;
148 
149 	if (!efx_net_active(efx->state)) {
150 		rc = -EBUSY;
151 		goto out;
152 	}
153 
154 	netif_info(efx, drv, efx->net_dev, "starting %sline testing\n",
155 		   (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
156 
157 	/* We need rx buffers and interrupts. */
158 	already_up = (efx->net_dev->flags & IFF_UP);
159 	if (!already_up) {
160 		rc = dev_open(efx->net_dev, NULL);
161 		if (rc) {
162 			netif_err(efx, drv, efx->net_dev,
163 				  "failed opening device.\n");
164 			goto out;
165 		}
166 	}
167 
168 	rc = efx_selftest(efx, efx_tests, test->flags);
169 
170 	if (!already_up)
171 		dev_close(efx->net_dev);
172 
173 	netif_info(efx, drv, efx->net_dev, "%s %sline self-tests\n",
174 		   rc == 0 ? "passed" : "failed",
175 		   (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
176 
177 out:
178 	efx_ethtool_fill_self_tests(efx, efx_tests, NULL, data);
179 	kfree(efx_tests);
180 fail:
181 	if (rc)
182 		test->flags |= ETH_TEST_FL_FAILED;
183 }
184 
185 void efx_ethtool_get_pauseparam(struct net_device *net_dev,
186 				struct ethtool_pauseparam *pause)
187 {
188 	struct efx_nic *efx = efx_netdev_priv(net_dev);
189 
190 	pause->rx_pause = !!(efx->wanted_fc & EFX_FC_RX);
191 	pause->tx_pause = !!(efx->wanted_fc & EFX_FC_TX);
192 	pause->autoneg = !!(efx->wanted_fc & EFX_FC_AUTO);
193 }
194 
195 int efx_ethtool_set_pauseparam(struct net_device *net_dev,
196 			       struct ethtool_pauseparam *pause)
197 {
198 	struct efx_nic *efx = efx_netdev_priv(net_dev);
199 	u8 wanted_fc, old_fc;
200 	u32 old_adv;
201 	int rc = 0;
202 
203 	mutex_lock(&efx->mac_lock);
204 
205 	wanted_fc = ((pause->rx_pause ? EFX_FC_RX : 0) |
206 		     (pause->tx_pause ? EFX_FC_TX : 0) |
207 		     (pause->autoneg ? EFX_FC_AUTO : 0));
208 
209 	if ((wanted_fc & EFX_FC_TX) && !(wanted_fc & EFX_FC_RX)) {
210 		netif_dbg(efx, drv, efx->net_dev,
211 			  "Flow control unsupported: tx ON rx OFF\n");
212 		rc = -EINVAL;
213 		goto out;
214 	}
215 
216 	if ((wanted_fc & EFX_FC_AUTO) && !efx->link_advertising[0]) {
217 		netif_dbg(efx, drv, efx->net_dev,
218 			  "Autonegotiation is disabled\n");
219 		rc = -EINVAL;
220 		goto out;
221 	}
222 
223 	/* Hook for Falcon bug 11482 workaround */
224 	if (efx->type->prepare_enable_fc_tx &&
225 	    (wanted_fc & EFX_FC_TX) && !(efx->wanted_fc & EFX_FC_TX))
226 		efx->type->prepare_enable_fc_tx(efx);
227 
228 	old_adv = efx->link_advertising[0];
229 	old_fc = efx->wanted_fc;
230 	efx_link_set_wanted_fc(efx, wanted_fc);
231 	if (efx->link_advertising[0] != old_adv ||
232 	    (efx->wanted_fc ^ old_fc) & EFX_FC_AUTO) {
233 		rc = efx_mcdi_port_reconfigure(efx);
234 		if (rc) {
235 			netif_err(efx, drv, efx->net_dev,
236 				  "Unable to advertise requested flow "
237 				  "control setting\n");
238 			goto out;
239 		}
240 	}
241 
242 	/* Reconfigure the MAC. The PHY *may* generate a link state change event
243 	 * if the user just changed the advertised capabilities, but there's no
244 	 * harm doing this twice */
245 	efx_mac_reconfigure(efx, false);
246 
247 out:
248 	mutex_unlock(&efx->mac_lock);
249 
250 	return rc;
251 }
252 
253 /**
254  * efx_fill_test - fill in an individual self-test entry
255  * @test_index:		Index of the test
256  * @strings:		Ethtool strings, or %NULL
257  * @data:		Ethtool test results, or %NULL
258  * @test:		Pointer to test result (used only if data != %NULL)
259  * @unit_format:	Unit name format (e.g. "chan\%d")
260  * @unit_id:		Unit id (e.g. 0 for "chan0")
261  * @test_format:	Test name format (e.g. "loopback.\%s.tx.sent")
262  * @test_id:		Test id (e.g. "PHYXS" for "loopback.PHYXS.tx_sent")
263  *
264  * Fill in an individual self-test entry.
265  */
266 static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
267 			  int *test, const char *unit_format, int unit_id,
268 			  const char *test_format, const char *test_id)
269 {
270 	char unit_str[ETH_GSTRING_LEN], test_str[ETH_GSTRING_LEN];
271 
272 	/* Fill data value, if applicable */
273 	if (data)
274 		data[test_index] = *test;
275 
276 	/* Fill string, if applicable */
277 	if (strings) {
278 		if (strchr(unit_format, '%'))
279 			snprintf(unit_str, sizeof(unit_str),
280 				 unit_format, unit_id);
281 		else
282 			strcpy(unit_str, unit_format);
283 		snprintf(test_str, sizeof(test_str), test_format, test_id);
284 		snprintf(strings + test_index * ETH_GSTRING_LEN,
285 			 ETH_GSTRING_LEN,
286 			 "%-6s %-24s", unit_str, test_str);
287 	}
288 }
289 
290 #define EFX_CHANNEL_NAME(_channel) "chan%d", _channel->channel
291 #define EFX_TX_QUEUE_NAME(_tx_queue) "txq%d", _tx_queue->label
292 #define EFX_LOOPBACK_NAME(_mode, _counter)			\
293 	"loopback.%s." _counter, STRING_TABLE_LOOKUP(_mode, efx_loopback_mode)
294 
295 /**
296  * efx_fill_loopback_test - fill in a block of loopback self-test entries
297  * @efx:		Efx NIC
298  * @lb_tests:		Efx loopback self-test results structure
299  * @mode:		Loopback test mode
300  * @test_index:		Starting index of the test
301  * @strings:		Ethtool strings, or %NULL
302  * @data:		Ethtool test results, or %NULL
303  *
304  * Fill in a block of loopback self-test entries.  Return new test
305  * index.
306  */
307 static int efx_fill_loopback_test(struct efx_nic *efx,
308 				  struct efx_loopback_self_tests *lb_tests,
309 				  enum efx_loopback_mode mode,
310 				  unsigned int test_index,
311 				  u8 *strings, u64 *data)
312 {
313 	struct efx_channel *channel =
314 		efx_get_channel(efx, efx->tx_channel_offset);
315 	struct efx_tx_queue *tx_queue;
316 
317 	efx_for_each_channel_tx_queue(tx_queue, channel) {
318 		efx_fill_test(test_index++, strings, data,
319 			      &lb_tests->tx_sent[tx_queue->label],
320 			      EFX_TX_QUEUE_NAME(tx_queue),
321 			      EFX_LOOPBACK_NAME(mode, "tx_sent"));
322 		efx_fill_test(test_index++, strings, data,
323 			      &lb_tests->tx_done[tx_queue->label],
324 			      EFX_TX_QUEUE_NAME(tx_queue),
325 			      EFX_LOOPBACK_NAME(mode, "tx_done"));
326 	}
327 	efx_fill_test(test_index++, strings, data,
328 		      &lb_tests->rx_good,
329 		      "rx", 0,
330 		      EFX_LOOPBACK_NAME(mode, "rx_good"));
331 	efx_fill_test(test_index++, strings, data,
332 		      &lb_tests->rx_bad,
333 		      "rx", 0,
334 		      EFX_LOOPBACK_NAME(mode, "rx_bad"));
335 
336 	return test_index;
337 }
338 
339 /**
340  * efx_ethtool_fill_self_tests - get self-test details
341  * @efx:		Efx NIC
342  * @tests:		Efx self-test results structure, or %NULL
343  * @strings:		Ethtool strings, or %NULL
344  * @data:		Ethtool test results, or %NULL
345  *
346  * Get self-test number of strings, strings, and/or test results.
347  * Return number of strings (== number of test results).
348  *
349  * The reason for merging these three functions is to make sure that
350  * they can never be inconsistent.
351  */
352 int efx_ethtool_fill_self_tests(struct efx_nic *efx,
353 				struct efx_self_tests *tests,
354 				u8 *strings, u64 *data)
355 {
356 	struct efx_channel *channel;
357 	unsigned int n = 0, i;
358 	enum efx_loopback_mode mode;
359 
360 	efx_fill_test(n++, strings, data, &tests->phy_alive,
361 		      "phy", 0, "alive", NULL);
362 	efx_fill_test(n++, strings, data, &tests->nvram,
363 		      "core", 0, "nvram", NULL);
364 	efx_fill_test(n++, strings, data, &tests->interrupt,
365 		      "core", 0, "interrupt", NULL);
366 
367 	/* Event queues */
368 	efx_for_each_channel(channel, efx) {
369 		efx_fill_test(n++, strings, data,
370 			      &tests->eventq_dma[channel->channel],
371 			      EFX_CHANNEL_NAME(channel),
372 			      "eventq.dma", NULL);
373 		efx_fill_test(n++, strings, data,
374 			      &tests->eventq_int[channel->channel],
375 			      EFX_CHANNEL_NAME(channel),
376 			      "eventq.int", NULL);
377 	}
378 
379 	efx_fill_test(n++, strings, data, &tests->memory,
380 		      "core", 0, "memory", NULL);
381 	efx_fill_test(n++, strings, data, &tests->registers,
382 		      "core", 0, "registers", NULL);
383 
384 	for (i = 0; true; ++i) {
385 		const char *name;
386 
387 		EFX_WARN_ON_PARANOID(i >= EFX_MAX_PHY_TESTS);
388 		name = efx_mcdi_phy_test_name(efx, i);
389 		if (name == NULL)
390 			break;
391 
392 		efx_fill_test(n++, strings, data, &tests->phy_ext[i], "phy", 0, name, NULL);
393 	}
394 
395 	/* Loopback tests */
396 	for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
397 		if (!(efx->loopback_modes & (1 << mode)))
398 			continue;
399 		n = efx_fill_loopback_test(efx,
400 					   &tests->loopback[mode], mode, n,
401 					   strings, data);
402 	}
403 
404 	return n;
405 }
406 
407 static size_t efx_describe_per_queue_stats(struct efx_nic *efx, u8 *strings)
408 {
409 	size_t n_stats = 0;
410 	struct efx_channel *channel;
411 
412 	efx_for_each_channel(channel, efx) {
413 		if (efx_channel_has_tx_queues(channel)) {
414 			n_stats++;
415 			if (strings != NULL) {
416 				snprintf(strings, ETH_GSTRING_LEN,
417 					 "tx-%u.tx_packets",
418 					 channel->tx_queue[0].queue /
419 					 EFX_MAX_TXQ_PER_CHANNEL);
420 
421 				strings += ETH_GSTRING_LEN;
422 			}
423 		}
424 	}
425 	efx_for_each_channel(channel, efx) {
426 		if (efx_channel_has_rx_queue(channel)) {
427 			n_stats++;
428 			if (strings != NULL) {
429 				snprintf(strings, ETH_GSTRING_LEN,
430 					 "rx-%d.rx_packets", channel->channel);
431 				strings += ETH_GSTRING_LEN;
432 			}
433 		}
434 	}
435 	if (efx->xdp_tx_queue_count && efx->xdp_tx_queues) {
436 		unsigned short xdp;
437 
438 		for (xdp = 0; xdp < efx->xdp_tx_queue_count; xdp++) {
439 			n_stats++;
440 			if (strings) {
441 				snprintf(strings, ETH_GSTRING_LEN,
442 					 "tx-xdp-cpu-%hu.tx_packets", xdp);
443 				strings += ETH_GSTRING_LEN;
444 			}
445 		}
446 	}
447 
448 	return n_stats;
449 }
450 
451 int efx_ethtool_get_sset_count(struct net_device *net_dev, int string_set)
452 {
453 	struct efx_nic *efx = efx_netdev_priv(net_dev);
454 
455 	switch (string_set) {
456 	case ETH_SS_STATS:
457 		return efx->type->describe_stats(efx, NULL) +
458 		       EFX_ETHTOOL_SW_STAT_COUNT +
459 		       efx_describe_per_queue_stats(efx, NULL) +
460 		       efx_ptp_describe_stats(efx, NULL);
461 	case ETH_SS_TEST:
462 		return efx_ethtool_fill_self_tests(efx, NULL, NULL, NULL);
463 	case ETH_SS_PRIV_FLAGS:
464 		return EFX_ETHTOOL_PRIV_FLAGS_COUNT;
465 	default:
466 		return -EINVAL;
467 	}
468 }
469 
470 void efx_ethtool_get_strings(struct net_device *net_dev,
471 			     u32 string_set, u8 *strings)
472 {
473 	struct efx_nic *efx = efx_netdev_priv(net_dev);
474 	int i;
475 
476 	switch (string_set) {
477 	case ETH_SS_STATS:
478 		strings += (efx->type->describe_stats(efx, strings) *
479 			    ETH_GSTRING_LEN);
480 		for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++)
481 			strscpy(strings + i * ETH_GSTRING_LEN,
482 				efx_sw_stat_desc[i].name, ETH_GSTRING_LEN);
483 		strings += EFX_ETHTOOL_SW_STAT_COUNT * ETH_GSTRING_LEN;
484 		strings += (efx_describe_per_queue_stats(efx, strings) *
485 			    ETH_GSTRING_LEN);
486 		efx_ptp_describe_stats(efx, strings);
487 		break;
488 	case ETH_SS_TEST:
489 		efx_ethtool_fill_self_tests(efx, NULL, strings, NULL);
490 		break;
491 	case ETH_SS_PRIV_FLAGS:
492 		for (i = 0; i < EFX_ETHTOOL_PRIV_FLAGS_COUNT; i++)
493 			strscpy(strings + i * ETH_GSTRING_LEN,
494 				efx_ethtool_priv_flags_strings[i],
495 				ETH_GSTRING_LEN);
496 		break;
497 	default:
498 		/* No other string sets */
499 		break;
500 	}
501 }
502 
503 u32 efx_ethtool_get_priv_flags(struct net_device *net_dev)
504 {
505 	struct efx_nic *efx = efx_netdev_priv(net_dev);
506 	u32 ret_flags = 0;
507 
508 	if (efx->log_tc_errs)
509 		ret_flags |= EFX_ETHTOOL_PRIV_FLAGS_LOG_TC_ERRS;
510 
511 	return ret_flags;
512 }
513 
514 int efx_ethtool_set_priv_flags(struct net_device *net_dev, u32 flags)
515 {
516 	struct efx_nic *efx = efx_netdev_priv(net_dev);
517 
518 	efx->log_tc_errs =
519 		!!(flags & EFX_ETHTOOL_PRIV_FLAGS_LOG_TC_ERRS);
520 
521 	return 0;
522 }
523 
524 void efx_ethtool_get_stats(struct net_device *net_dev,
525 			   struct ethtool_stats *stats,
526 			   u64 *data)
527 {
528 	struct efx_nic *efx = efx_netdev_priv(net_dev);
529 	const struct efx_sw_stat_desc *stat;
530 	struct efx_channel *channel;
531 	struct efx_tx_queue *tx_queue;
532 	struct efx_rx_queue *rx_queue;
533 	int i;
534 
535 	spin_lock_bh(&efx->stats_lock);
536 
537 	/* Get NIC statistics */
538 	data += efx->type->update_stats(efx, data, NULL);
539 
540 	/* Get software statistics */
541 	for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++) {
542 		stat = &efx_sw_stat_desc[i];
543 		switch (stat->source) {
544 		case EFX_ETHTOOL_STAT_SOURCE_nic:
545 			data[i] = stat->get_stat((void *)efx + stat->offset);
546 			break;
547 		case EFX_ETHTOOL_STAT_SOURCE_channel:
548 			data[i] = 0;
549 			efx_for_each_channel(channel, efx)
550 				data[i] += stat->get_stat((void *)channel +
551 							  stat->offset);
552 			break;
553 		case EFX_ETHTOOL_STAT_SOURCE_tx_queue:
554 			data[i] = 0;
555 			efx_for_each_channel(channel, efx) {
556 				efx_for_each_channel_tx_queue(tx_queue, channel)
557 					data[i] +=
558 						stat->get_stat((void *)tx_queue
559 							       + stat->offset);
560 			}
561 			break;
562 		}
563 	}
564 	data += EFX_ETHTOOL_SW_STAT_COUNT;
565 
566 	spin_unlock_bh(&efx->stats_lock);
567 
568 	efx_for_each_channel(channel, efx) {
569 		if (efx_channel_has_tx_queues(channel)) {
570 			*data = 0;
571 			efx_for_each_channel_tx_queue(tx_queue, channel) {
572 				*data += tx_queue->tx_packets;
573 			}
574 			data++;
575 		}
576 	}
577 	efx_for_each_channel(channel, efx) {
578 		if (efx_channel_has_rx_queue(channel)) {
579 			*data = 0;
580 			efx_for_each_channel_rx_queue(rx_queue, channel) {
581 				*data += rx_queue->rx_packets;
582 			}
583 			data++;
584 		}
585 	}
586 	if (efx->xdp_tx_queue_count && efx->xdp_tx_queues) {
587 		int xdp;
588 
589 		for (xdp = 0; xdp < efx->xdp_tx_queue_count; xdp++) {
590 			data[0] = efx->xdp_tx_queues[xdp]->tx_packets;
591 			data++;
592 		}
593 	}
594 
595 	efx_ptp_update_stats(efx, data);
596 }
597 
598 /* This must be called with rtnl_lock held. */
599 int efx_ethtool_get_link_ksettings(struct net_device *net_dev,
600 				   struct ethtool_link_ksettings *cmd)
601 {
602 	struct efx_nic *efx = efx_netdev_priv(net_dev);
603 	struct efx_link_state *link_state = &efx->link_state;
604 
605 	mutex_lock(&efx->mac_lock);
606 	efx_mcdi_phy_get_link_ksettings(efx, cmd);
607 	mutex_unlock(&efx->mac_lock);
608 
609 	/* Both MACs support pause frames (bidirectional and respond-only) */
610 	ethtool_link_ksettings_add_link_mode(cmd, supported, Pause);
611 	ethtool_link_ksettings_add_link_mode(cmd, supported, Asym_Pause);
612 
613 	if (LOOPBACK_INTERNAL(efx)) {
614 		cmd->base.speed = link_state->speed;
615 		cmd->base.duplex = link_state->fd ? DUPLEX_FULL : DUPLEX_HALF;
616 	}
617 
618 	return 0;
619 }
620 
621 /* This must be called with rtnl_lock held. */
622 int efx_ethtool_set_link_ksettings(struct net_device *net_dev,
623 				   const struct ethtool_link_ksettings *cmd)
624 {
625 	struct efx_nic *efx = efx_netdev_priv(net_dev);
626 	int rc;
627 
628 	/* GMAC does not support 1000Mbps HD */
629 	if ((cmd->base.speed == SPEED_1000) &&
630 	    (cmd->base.duplex != DUPLEX_FULL)) {
631 		netif_dbg(efx, drv, efx->net_dev,
632 			  "rejecting unsupported 1000Mbps HD setting\n");
633 		return -EINVAL;
634 	}
635 
636 	mutex_lock(&efx->mac_lock);
637 	rc = efx_mcdi_phy_set_link_ksettings(efx, cmd);
638 	mutex_unlock(&efx->mac_lock);
639 	return rc;
640 }
641 
642 int efx_ethtool_get_fecparam(struct net_device *net_dev,
643 			     struct ethtool_fecparam *fecparam)
644 {
645 	struct efx_nic *efx = efx_netdev_priv(net_dev);
646 	int rc;
647 
648 	mutex_lock(&efx->mac_lock);
649 	rc = efx_mcdi_phy_get_fecparam(efx, fecparam);
650 	mutex_unlock(&efx->mac_lock);
651 
652 	return rc;
653 }
654 
655 int efx_ethtool_set_fecparam(struct net_device *net_dev,
656 			     struct ethtool_fecparam *fecparam)
657 {
658 	struct efx_nic *efx = efx_netdev_priv(net_dev);
659 	int rc;
660 
661 	mutex_lock(&efx->mac_lock);
662 	rc = efx_mcdi_phy_set_fecparam(efx, fecparam);
663 	mutex_unlock(&efx->mac_lock);
664 
665 	return rc;
666 }
667 
668 /* MAC address mask including only I/G bit */
669 static const u8 mac_addr_ig_mask[ETH_ALEN] __aligned(2) = {0x01, 0, 0, 0, 0, 0};
670 
671 #define IP4_ADDR_FULL_MASK	((__force __be32)~0)
672 #define IP_PROTO_FULL_MASK	0xFF
673 #define PORT_FULL_MASK		((__force __be16)~0)
674 #define ETHER_TYPE_FULL_MASK	((__force __be16)~0)
675 
676 static inline void ip6_fill_mask(__be32 *mask)
677 {
678 	mask[0] = mask[1] = mask[2] = mask[3] = ~(__be32)0;
679 }
680 
681 static int efx_ethtool_get_class_rule(struct efx_nic *efx,
682 				      struct ethtool_rx_flow_spec *rule,
683 				      u32 *rss_context)
684 {
685 	struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
686 	struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
687 	struct ethtool_usrip4_spec *uip_entry = &rule->h_u.usr_ip4_spec;
688 	struct ethtool_usrip4_spec *uip_mask = &rule->m_u.usr_ip4_spec;
689 	struct ethtool_tcpip6_spec *ip6_entry = &rule->h_u.tcp_ip6_spec;
690 	struct ethtool_tcpip6_spec *ip6_mask = &rule->m_u.tcp_ip6_spec;
691 	struct ethtool_usrip6_spec *uip6_entry = &rule->h_u.usr_ip6_spec;
692 	struct ethtool_usrip6_spec *uip6_mask = &rule->m_u.usr_ip6_spec;
693 	struct ethhdr *mac_entry = &rule->h_u.ether_spec;
694 	struct ethhdr *mac_mask = &rule->m_u.ether_spec;
695 	struct efx_filter_spec spec;
696 	int rc;
697 
698 	rc = efx_filter_get_filter_safe(efx, EFX_FILTER_PRI_MANUAL,
699 					rule->location, &spec);
700 	if (rc)
701 		return rc;
702 
703 	if (spec.dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
704 		rule->ring_cookie = RX_CLS_FLOW_DISC;
705 	else
706 		rule->ring_cookie = spec.dmaq_id;
707 
708 	if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) &&
709 	    spec.ether_type == htons(ETH_P_IP) &&
710 	    (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) &&
711 	    (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) &&
712 	    !(spec.match_flags &
713 	      ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
714 		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
715 		EFX_FILTER_MATCH_IP_PROTO |
716 		EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) {
717 		rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ?
718 				   TCP_V4_FLOW : UDP_V4_FLOW);
719 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
720 			ip_entry->ip4dst = spec.loc_host[0];
721 			ip_mask->ip4dst = IP4_ADDR_FULL_MASK;
722 		}
723 		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
724 			ip_entry->ip4src = spec.rem_host[0];
725 			ip_mask->ip4src = IP4_ADDR_FULL_MASK;
726 		}
727 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) {
728 			ip_entry->pdst = spec.loc_port;
729 			ip_mask->pdst = PORT_FULL_MASK;
730 		}
731 		if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) {
732 			ip_entry->psrc = spec.rem_port;
733 			ip_mask->psrc = PORT_FULL_MASK;
734 		}
735 	} else if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) &&
736 	    spec.ether_type == htons(ETH_P_IPV6) &&
737 	    (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) &&
738 	    (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) &&
739 	    !(spec.match_flags &
740 	      ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
741 		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
742 		EFX_FILTER_MATCH_IP_PROTO |
743 		EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) {
744 		rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ?
745 				   TCP_V6_FLOW : UDP_V6_FLOW);
746 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
747 			memcpy(ip6_entry->ip6dst, spec.loc_host,
748 			       sizeof(ip6_entry->ip6dst));
749 			ip6_fill_mask(ip6_mask->ip6dst);
750 		}
751 		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
752 			memcpy(ip6_entry->ip6src, spec.rem_host,
753 			       sizeof(ip6_entry->ip6src));
754 			ip6_fill_mask(ip6_mask->ip6src);
755 		}
756 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) {
757 			ip6_entry->pdst = spec.loc_port;
758 			ip6_mask->pdst = PORT_FULL_MASK;
759 		}
760 		if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) {
761 			ip6_entry->psrc = spec.rem_port;
762 			ip6_mask->psrc = PORT_FULL_MASK;
763 		}
764 	} else if (!(spec.match_flags &
765 		     ~(EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG |
766 		       EFX_FILTER_MATCH_REM_MAC | EFX_FILTER_MATCH_ETHER_TYPE |
767 		       EFX_FILTER_MATCH_OUTER_VID))) {
768 		rule->flow_type = ETHER_FLOW;
769 		if (spec.match_flags &
770 		    (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG)) {
771 			ether_addr_copy(mac_entry->h_dest, spec.loc_mac);
772 			if (spec.match_flags & EFX_FILTER_MATCH_LOC_MAC)
773 				eth_broadcast_addr(mac_mask->h_dest);
774 			else
775 				ether_addr_copy(mac_mask->h_dest,
776 						mac_addr_ig_mask);
777 		}
778 		if (spec.match_flags & EFX_FILTER_MATCH_REM_MAC) {
779 			ether_addr_copy(mac_entry->h_source, spec.rem_mac);
780 			eth_broadcast_addr(mac_mask->h_source);
781 		}
782 		if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) {
783 			mac_entry->h_proto = spec.ether_type;
784 			mac_mask->h_proto = ETHER_TYPE_FULL_MASK;
785 		}
786 	} else if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE &&
787 		   spec.ether_type == htons(ETH_P_IP) &&
788 		   !(spec.match_flags &
789 		     ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
790 		       EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
791 		       EFX_FILTER_MATCH_IP_PROTO))) {
792 		rule->flow_type = IPV4_USER_FLOW;
793 		uip_entry->ip_ver = ETH_RX_NFC_IP4;
794 		if (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) {
795 			uip_mask->proto = IP_PROTO_FULL_MASK;
796 			uip_entry->proto = spec.ip_proto;
797 		}
798 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
799 			uip_entry->ip4dst = spec.loc_host[0];
800 			uip_mask->ip4dst = IP4_ADDR_FULL_MASK;
801 		}
802 		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
803 			uip_entry->ip4src = spec.rem_host[0];
804 			uip_mask->ip4src = IP4_ADDR_FULL_MASK;
805 		}
806 	} else if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE &&
807 		   spec.ether_type == htons(ETH_P_IPV6) &&
808 		   !(spec.match_flags &
809 		     ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
810 		       EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
811 		       EFX_FILTER_MATCH_IP_PROTO))) {
812 		rule->flow_type = IPV6_USER_FLOW;
813 		if (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) {
814 			uip6_mask->l4_proto = IP_PROTO_FULL_MASK;
815 			uip6_entry->l4_proto = spec.ip_proto;
816 		}
817 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
818 			memcpy(uip6_entry->ip6dst, spec.loc_host,
819 			       sizeof(uip6_entry->ip6dst));
820 			ip6_fill_mask(uip6_mask->ip6dst);
821 		}
822 		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
823 			memcpy(uip6_entry->ip6src, spec.rem_host,
824 			       sizeof(uip6_entry->ip6src));
825 			ip6_fill_mask(uip6_mask->ip6src);
826 		}
827 	} else {
828 		/* The above should handle all filters that we insert */
829 		WARN_ON(1);
830 		return -EINVAL;
831 	}
832 
833 	if (spec.match_flags & EFX_FILTER_MATCH_OUTER_VID) {
834 		rule->flow_type |= FLOW_EXT;
835 		rule->h_ext.vlan_tci = spec.outer_vid;
836 		rule->m_ext.vlan_tci = htons(0xfff);
837 	}
838 
839 	if (spec.flags & EFX_FILTER_FLAG_RX_RSS) {
840 		rule->flow_type |= FLOW_RSS;
841 		*rss_context = spec.rss_context;
842 	}
843 
844 	return rc;
845 }
846 
847 int efx_ethtool_get_rxnfc(struct net_device *net_dev,
848 			  struct ethtool_rxnfc *info, u32 *rule_locs)
849 {
850 	struct efx_nic *efx = efx_netdev_priv(net_dev);
851 	u32 rss_context = 0;
852 	s32 rc = 0;
853 
854 	switch (info->cmd) {
855 	case ETHTOOL_GRXRINGS:
856 		info->data = efx->n_rx_channels;
857 		return 0;
858 
859 	case ETHTOOL_GRXFH: {
860 		struct efx_rss_context *ctx = &efx->rss_context;
861 		__u64 data;
862 
863 		mutex_lock(&efx->rss_lock);
864 		if (info->flow_type & FLOW_RSS && info->rss_context) {
865 			ctx = efx_find_rss_context_entry(efx, info->rss_context);
866 			if (!ctx) {
867 				rc = -ENOENT;
868 				goto out_unlock;
869 			}
870 		}
871 
872 		data = 0;
873 		if (!efx_rss_active(ctx)) /* No RSS */
874 			goto out_setdata_unlock;
875 
876 		switch (info->flow_type & ~FLOW_RSS) {
877 		case UDP_V4_FLOW:
878 		case UDP_V6_FLOW:
879 			if (ctx->rx_hash_udp_4tuple)
880 				data = (RXH_L4_B_0_1 | RXH_L4_B_2_3 |
881 					RXH_IP_SRC | RXH_IP_DST);
882 			else
883 				data = RXH_IP_SRC | RXH_IP_DST;
884 			break;
885 		case TCP_V4_FLOW:
886 		case TCP_V6_FLOW:
887 			data = (RXH_L4_B_0_1 | RXH_L4_B_2_3 |
888 				RXH_IP_SRC | RXH_IP_DST);
889 			break;
890 		case SCTP_V4_FLOW:
891 		case SCTP_V6_FLOW:
892 		case AH_ESP_V4_FLOW:
893 		case AH_ESP_V6_FLOW:
894 		case IPV4_FLOW:
895 		case IPV6_FLOW:
896 			data = RXH_IP_SRC | RXH_IP_DST;
897 			break;
898 		default:
899 			break;
900 		}
901 out_setdata_unlock:
902 		info->data = data;
903 out_unlock:
904 		mutex_unlock(&efx->rss_lock);
905 		return rc;
906 	}
907 
908 	case ETHTOOL_GRXCLSRLCNT:
909 		info->data = efx_filter_get_rx_id_limit(efx);
910 		if (info->data == 0)
911 			return -EOPNOTSUPP;
912 		info->data |= RX_CLS_LOC_SPECIAL;
913 		info->rule_cnt =
914 			efx_filter_count_rx_used(efx, EFX_FILTER_PRI_MANUAL);
915 		return 0;
916 
917 	case ETHTOOL_GRXCLSRULE:
918 		if (efx_filter_get_rx_id_limit(efx) == 0)
919 			return -EOPNOTSUPP;
920 		rc = efx_ethtool_get_class_rule(efx, &info->fs, &rss_context);
921 		if (rc < 0)
922 			return rc;
923 		if (info->fs.flow_type & FLOW_RSS)
924 			info->rss_context = rss_context;
925 		return 0;
926 
927 	case ETHTOOL_GRXCLSRLALL:
928 		info->data = efx_filter_get_rx_id_limit(efx);
929 		if (info->data == 0)
930 			return -EOPNOTSUPP;
931 		rc = efx_filter_get_rx_ids(efx, EFX_FILTER_PRI_MANUAL,
932 					   rule_locs, info->rule_cnt);
933 		if (rc < 0)
934 			return rc;
935 		info->rule_cnt = rc;
936 		return 0;
937 
938 	default:
939 		return -EOPNOTSUPP;
940 	}
941 }
942 
943 static inline bool ip6_mask_is_full(__be32 mask[4])
944 {
945 	return !~(mask[0] & mask[1] & mask[2] & mask[3]);
946 }
947 
948 static inline bool ip6_mask_is_empty(__be32 mask[4])
949 {
950 	return !(mask[0] | mask[1] | mask[2] | mask[3]);
951 }
952 
953 static int efx_ethtool_set_class_rule(struct efx_nic *efx,
954 				      struct ethtool_rx_flow_spec *rule,
955 				      u32 rss_context)
956 {
957 	struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
958 	struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
959 	struct ethtool_usrip4_spec *uip_entry = &rule->h_u.usr_ip4_spec;
960 	struct ethtool_usrip4_spec *uip_mask = &rule->m_u.usr_ip4_spec;
961 	struct ethtool_tcpip6_spec *ip6_entry = &rule->h_u.tcp_ip6_spec;
962 	struct ethtool_tcpip6_spec *ip6_mask = &rule->m_u.tcp_ip6_spec;
963 	struct ethtool_usrip6_spec *uip6_entry = &rule->h_u.usr_ip6_spec;
964 	struct ethtool_usrip6_spec *uip6_mask = &rule->m_u.usr_ip6_spec;
965 	u32 flow_type = rule->flow_type & ~(FLOW_EXT | FLOW_RSS);
966 	struct ethhdr *mac_entry = &rule->h_u.ether_spec;
967 	struct ethhdr *mac_mask = &rule->m_u.ether_spec;
968 	enum efx_filter_flags flags = 0;
969 	struct efx_filter_spec spec;
970 	int rc;
971 
972 	/* Check that user wants us to choose the location */
973 	if (rule->location != RX_CLS_LOC_ANY)
974 		return -EINVAL;
975 
976 	/* Range-check ring_cookie */
977 	if (rule->ring_cookie >= efx->n_rx_channels &&
978 	    rule->ring_cookie != RX_CLS_FLOW_DISC)
979 		return -EINVAL;
980 
981 	/* Check for unsupported extensions */
982 	if ((rule->flow_type & FLOW_EXT) &&
983 	    (rule->m_ext.vlan_etype || rule->m_ext.data[0] ||
984 	     rule->m_ext.data[1]))
985 		return -EINVAL;
986 
987 	if (efx->rx_scatter)
988 		flags |= EFX_FILTER_FLAG_RX_SCATTER;
989 	if (rule->flow_type & FLOW_RSS)
990 		flags |= EFX_FILTER_FLAG_RX_RSS;
991 
992 	efx_filter_init_rx(&spec, EFX_FILTER_PRI_MANUAL, flags,
993 			   (rule->ring_cookie == RX_CLS_FLOW_DISC) ?
994 			   EFX_FILTER_RX_DMAQ_ID_DROP : rule->ring_cookie);
995 
996 	if (rule->flow_type & FLOW_RSS)
997 		spec.rss_context = rss_context;
998 
999 	switch (flow_type) {
1000 	case TCP_V4_FLOW:
1001 	case UDP_V4_FLOW:
1002 		spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE |
1003 				    EFX_FILTER_MATCH_IP_PROTO);
1004 		spec.ether_type = htons(ETH_P_IP);
1005 		spec.ip_proto = flow_type == TCP_V4_FLOW ? IPPROTO_TCP
1006 							 : IPPROTO_UDP;
1007 		if (ip_mask->ip4dst) {
1008 			if (ip_mask->ip4dst != IP4_ADDR_FULL_MASK)
1009 				return -EINVAL;
1010 			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1011 			spec.loc_host[0] = ip_entry->ip4dst;
1012 		}
1013 		if (ip_mask->ip4src) {
1014 			if (ip_mask->ip4src != IP4_ADDR_FULL_MASK)
1015 				return -EINVAL;
1016 			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1017 			spec.rem_host[0] = ip_entry->ip4src;
1018 		}
1019 		if (ip_mask->pdst) {
1020 			if (ip_mask->pdst != PORT_FULL_MASK)
1021 				return -EINVAL;
1022 			spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT;
1023 			spec.loc_port = ip_entry->pdst;
1024 		}
1025 		if (ip_mask->psrc) {
1026 			if (ip_mask->psrc != PORT_FULL_MASK)
1027 				return -EINVAL;
1028 			spec.match_flags |= EFX_FILTER_MATCH_REM_PORT;
1029 			spec.rem_port = ip_entry->psrc;
1030 		}
1031 		if (ip_mask->tos)
1032 			return -EINVAL;
1033 		break;
1034 
1035 	case TCP_V6_FLOW:
1036 	case UDP_V6_FLOW:
1037 		spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE |
1038 				    EFX_FILTER_MATCH_IP_PROTO);
1039 		spec.ether_type = htons(ETH_P_IPV6);
1040 		spec.ip_proto = flow_type == TCP_V6_FLOW ? IPPROTO_TCP
1041 							 : IPPROTO_UDP;
1042 		if (!ip6_mask_is_empty(ip6_mask->ip6dst)) {
1043 			if (!ip6_mask_is_full(ip6_mask->ip6dst))
1044 				return -EINVAL;
1045 			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1046 			memcpy(spec.loc_host, ip6_entry->ip6dst, sizeof(spec.loc_host));
1047 		}
1048 		if (!ip6_mask_is_empty(ip6_mask->ip6src)) {
1049 			if (!ip6_mask_is_full(ip6_mask->ip6src))
1050 				return -EINVAL;
1051 			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1052 			memcpy(spec.rem_host, ip6_entry->ip6src, sizeof(spec.rem_host));
1053 		}
1054 		if (ip6_mask->pdst) {
1055 			if (ip6_mask->pdst != PORT_FULL_MASK)
1056 				return -EINVAL;
1057 			spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT;
1058 			spec.loc_port = ip6_entry->pdst;
1059 		}
1060 		if (ip6_mask->psrc) {
1061 			if (ip6_mask->psrc != PORT_FULL_MASK)
1062 				return -EINVAL;
1063 			spec.match_flags |= EFX_FILTER_MATCH_REM_PORT;
1064 			spec.rem_port = ip6_entry->psrc;
1065 		}
1066 		if (ip6_mask->tclass)
1067 			return -EINVAL;
1068 		break;
1069 
1070 	case IPV4_USER_FLOW:
1071 		if (uip_mask->l4_4_bytes || uip_mask->tos || uip_mask->ip_ver ||
1072 		    uip_entry->ip_ver != ETH_RX_NFC_IP4)
1073 			return -EINVAL;
1074 		spec.match_flags = EFX_FILTER_MATCH_ETHER_TYPE;
1075 		spec.ether_type = htons(ETH_P_IP);
1076 		if (uip_mask->ip4dst) {
1077 			if (uip_mask->ip4dst != IP4_ADDR_FULL_MASK)
1078 				return -EINVAL;
1079 			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1080 			spec.loc_host[0] = uip_entry->ip4dst;
1081 		}
1082 		if (uip_mask->ip4src) {
1083 			if (uip_mask->ip4src != IP4_ADDR_FULL_MASK)
1084 				return -EINVAL;
1085 			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1086 			spec.rem_host[0] = uip_entry->ip4src;
1087 		}
1088 		if (uip_mask->proto) {
1089 			if (uip_mask->proto != IP_PROTO_FULL_MASK)
1090 				return -EINVAL;
1091 			spec.match_flags |= EFX_FILTER_MATCH_IP_PROTO;
1092 			spec.ip_proto = uip_entry->proto;
1093 		}
1094 		break;
1095 
1096 	case IPV6_USER_FLOW:
1097 		if (uip6_mask->l4_4_bytes || uip6_mask->tclass)
1098 			return -EINVAL;
1099 		spec.match_flags = EFX_FILTER_MATCH_ETHER_TYPE;
1100 		spec.ether_type = htons(ETH_P_IPV6);
1101 		if (!ip6_mask_is_empty(uip6_mask->ip6dst)) {
1102 			if (!ip6_mask_is_full(uip6_mask->ip6dst))
1103 				return -EINVAL;
1104 			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1105 			memcpy(spec.loc_host, uip6_entry->ip6dst, sizeof(spec.loc_host));
1106 		}
1107 		if (!ip6_mask_is_empty(uip6_mask->ip6src)) {
1108 			if (!ip6_mask_is_full(uip6_mask->ip6src))
1109 				return -EINVAL;
1110 			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1111 			memcpy(spec.rem_host, uip6_entry->ip6src, sizeof(spec.rem_host));
1112 		}
1113 		if (uip6_mask->l4_proto) {
1114 			if (uip6_mask->l4_proto != IP_PROTO_FULL_MASK)
1115 				return -EINVAL;
1116 			spec.match_flags |= EFX_FILTER_MATCH_IP_PROTO;
1117 			spec.ip_proto = uip6_entry->l4_proto;
1118 		}
1119 		break;
1120 
1121 	case ETHER_FLOW:
1122 		if (!is_zero_ether_addr(mac_mask->h_dest)) {
1123 			if (ether_addr_equal(mac_mask->h_dest,
1124 					     mac_addr_ig_mask))
1125 				spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC_IG;
1126 			else if (is_broadcast_ether_addr(mac_mask->h_dest))
1127 				spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC;
1128 			else
1129 				return -EINVAL;
1130 			ether_addr_copy(spec.loc_mac, mac_entry->h_dest);
1131 		}
1132 		if (!is_zero_ether_addr(mac_mask->h_source)) {
1133 			if (!is_broadcast_ether_addr(mac_mask->h_source))
1134 				return -EINVAL;
1135 			spec.match_flags |= EFX_FILTER_MATCH_REM_MAC;
1136 			ether_addr_copy(spec.rem_mac, mac_entry->h_source);
1137 		}
1138 		if (mac_mask->h_proto) {
1139 			if (mac_mask->h_proto != ETHER_TYPE_FULL_MASK)
1140 				return -EINVAL;
1141 			spec.match_flags |= EFX_FILTER_MATCH_ETHER_TYPE;
1142 			spec.ether_type = mac_entry->h_proto;
1143 		}
1144 		break;
1145 
1146 	default:
1147 		return -EINVAL;
1148 	}
1149 
1150 	if ((rule->flow_type & FLOW_EXT) && rule->m_ext.vlan_tci) {
1151 		if (rule->m_ext.vlan_tci != htons(0xfff))
1152 			return -EINVAL;
1153 		spec.match_flags |= EFX_FILTER_MATCH_OUTER_VID;
1154 		spec.outer_vid = rule->h_ext.vlan_tci;
1155 	}
1156 
1157 	rc = efx_filter_insert_filter(efx, &spec, true);
1158 	if (rc < 0)
1159 		return rc;
1160 
1161 	rule->location = rc;
1162 	return 0;
1163 }
1164 
1165 int efx_ethtool_set_rxnfc(struct net_device *net_dev,
1166 			  struct ethtool_rxnfc *info)
1167 {
1168 	struct efx_nic *efx = efx_netdev_priv(net_dev);
1169 
1170 	if (efx_filter_get_rx_id_limit(efx) == 0)
1171 		return -EOPNOTSUPP;
1172 
1173 	switch (info->cmd) {
1174 	case ETHTOOL_SRXCLSRLINS:
1175 		return efx_ethtool_set_class_rule(efx, &info->fs,
1176 						  info->rss_context);
1177 
1178 	case ETHTOOL_SRXCLSRLDEL:
1179 		return efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_MANUAL,
1180 						 info->fs.location);
1181 
1182 	default:
1183 		return -EOPNOTSUPP;
1184 	}
1185 }
1186 
1187 u32 efx_ethtool_get_rxfh_indir_size(struct net_device *net_dev)
1188 {
1189 	struct efx_nic *efx = efx_netdev_priv(net_dev);
1190 
1191 	if (efx->n_rx_channels == 1)
1192 		return 0;
1193 	return ARRAY_SIZE(efx->rss_context.rx_indir_table);
1194 }
1195 
1196 u32 efx_ethtool_get_rxfh_key_size(struct net_device *net_dev)
1197 {
1198 	struct efx_nic *efx = efx_netdev_priv(net_dev);
1199 
1200 	return efx->type->rx_hash_key_size;
1201 }
1202 
1203 int efx_ethtool_get_rxfh(struct net_device *net_dev, u32 *indir, u8 *key,
1204 			 u8 *hfunc)
1205 {
1206 	struct efx_nic *efx = efx_netdev_priv(net_dev);
1207 	int rc;
1208 
1209 	rc = efx->type->rx_pull_rss_config(efx);
1210 	if (rc)
1211 		return rc;
1212 
1213 	if (hfunc)
1214 		*hfunc = ETH_RSS_HASH_TOP;
1215 	if (indir)
1216 		memcpy(indir, efx->rss_context.rx_indir_table,
1217 		       sizeof(efx->rss_context.rx_indir_table));
1218 	if (key)
1219 		memcpy(key, efx->rss_context.rx_hash_key,
1220 		       efx->type->rx_hash_key_size);
1221 	return 0;
1222 }
1223 
1224 int efx_ethtool_set_rxfh(struct net_device *net_dev, const u32 *indir,
1225 			 const u8 *key, const u8 hfunc)
1226 {
1227 	struct efx_nic *efx = efx_netdev_priv(net_dev);
1228 
1229 	/* Hash function is Toeplitz, cannot be changed */
1230 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1231 		return -EOPNOTSUPP;
1232 	if (!indir && !key)
1233 		return 0;
1234 
1235 	if (!key)
1236 		key = efx->rss_context.rx_hash_key;
1237 	if (!indir)
1238 		indir = efx->rss_context.rx_indir_table;
1239 
1240 	return efx->type->rx_push_rss_config(efx, true, indir, key);
1241 }
1242 
1243 int efx_ethtool_get_rxfh_context(struct net_device *net_dev, u32 *indir,
1244 				 u8 *key, u8 *hfunc, u32 rss_context)
1245 {
1246 	struct efx_nic *efx = efx_netdev_priv(net_dev);
1247 	struct efx_rss_context *ctx;
1248 	int rc = 0;
1249 
1250 	if (!efx->type->rx_pull_rss_context_config)
1251 		return -EOPNOTSUPP;
1252 
1253 	mutex_lock(&efx->rss_lock);
1254 	ctx = efx_find_rss_context_entry(efx, rss_context);
1255 	if (!ctx) {
1256 		rc = -ENOENT;
1257 		goto out_unlock;
1258 	}
1259 	rc = efx->type->rx_pull_rss_context_config(efx, ctx);
1260 	if (rc)
1261 		goto out_unlock;
1262 
1263 	if (hfunc)
1264 		*hfunc = ETH_RSS_HASH_TOP;
1265 	if (indir)
1266 		memcpy(indir, ctx->rx_indir_table, sizeof(ctx->rx_indir_table));
1267 	if (key)
1268 		memcpy(key, ctx->rx_hash_key, efx->type->rx_hash_key_size);
1269 out_unlock:
1270 	mutex_unlock(&efx->rss_lock);
1271 	return rc;
1272 }
1273 
1274 int efx_ethtool_set_rxfh_context(struct net_device *net_dev,
1275 				 const u32 *indir, const u8 *key,
1276 				 const u8 hfunc, u32 *rss_context,
1277 				 bool delete)
1278 {
1279 	struct efx_nic *efx = efx_netdev_priv(net_dev);
1280 	struct efx_rss_context *ctx;
1281 	bool allocated = false;
1282 	int rc;
1283 
1284 	if (!efx->type->rx_push_rss_context_config)
1285 		return -EOPNOTSUPP;
1286 	/* Hash function is Toeplitz, cannot be changed */
1287 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1288 		return -EOPNOTSUPP;
1289 
1290 	mutex_lock(&efx->rss_lock);
1291 
1292 	if (*rss_context == ETH_RXFH_CONTEXT_ALLOC) {
1293 		if (delete) {
1294 			/* alloc + delete == Nothing to do */
1295 			rc = -EINVAL;
1296 			goto out_unlock;
1297 		}
1298 		ctx = efx_alloc_rss_context_entry(efx);
1299 		if (!ctx) {
1300 			rc = -ENOMEM;
1301 			goto out_unlock;
1302 		}
1303 		ctx->context_id = EFX_MCDI_RSS_CONTEXT_INVALID;
1304 		/* Initialise indir table and key to defaults */
1305 		efx_set_default_rx_indir_table(efx, ctx);
1306 		netdev_rss_key_fill(ctx->rx_hash_key, sizeof(ctx->rx_hash_key));
1307 		allocated = true;
1308 	} else {
1309 		ctx = efx_find_rss_context_entry(efx, *rss_context);
1310 		if (!ctx) {
1311 			rc = -ENOENT;
1312 			goto out_unlock;
1313 		}
1314 	}
1315 
1316 	if (delete) {
1317 		/* delete this context */
1318 		rc = efx->type->rx_push_rss_context_config(efx, ctx, NULL, NULL);
1319 		if (!rc)
1320 			efx_free_rss_context_entry(ctx);
1321 		goto out_unlock;
1322 	}
1323 
1324 	if (!key)
1325 		key = ctx->rx_hash_key;
1326 	if (!indir)
1327 		indir = ctx->rx_indir_table;
1328 
1329 	rc = efx->type->rx_push_rss_context_config(efx, ctx, indir, key);
1330 	if (rc && allocated)
1331 		efx_free_rss_context_entry(ctx);
1332 	else
1333 		*rss_context = ctx->user_id;
1334 out_unlock:
1335 	mutex_unlock(&efx->rss_lock);
1336 	return rc;
1337 }
1338 
1339 int efx_ethtool_reset(struct net_device *net_dev, u32 *flags)
1340 {
1341 	struct efx_nic *efx = efx_netdev_priv(net_dev);
1342 	int rc;
1343 
1344 	rc = efx->type->map_reset_flags(flags);
1345 	if (rc < 0)
1346 		return rc;
1347 
1348 	return efx_reset(efx, rc);
1349 }
1350 
1351 int efx_ethtool_get_module_eeprom(struct net_device *net_dev,
1352 				  struct ethtool_eeprom *ee,
1353 				  u8 *data)
1354 {
1355 	struct efx_nic *efx = efx_netdev_priv(net_dev);
1356 	int ret;
1357 
1358 	mutex_lock(&efx->mac_lock);
1359 	ret = efx_mcdi_phy_get_module_eeprom(efx, ee, data);
1360 	mutex_unlock(&efx->mac_lock);
1361 
1362 	return ret;
1363 }
1364 
1365 int efx_ethtool_get_module_info(struct net_device *net_dev,
1366 				struct ethtool_modinfo *modinfo)
1367 {
1368 	struct efx_nic *efx = efx_netdev_priv(net_dev);
1369 	int ret;
1370 
1371 	mutex_lock(&efx->mac_lock);
1372 	ret = efx_mcdi_phy_get_module_info(efx, modinfo);
1373 	mutex_unlock(&efx->mac_lock);
1374 
1375 	return ret;
1376 }
1377