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