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