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
20struct 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
43static u64 efx_get_uint_stat(void *field)
44{
45	return *(unsigned int *)field;
46}
47
48static 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
68static 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
103void efx_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	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
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
114u32 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
121void 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
128void 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_net_active(efx->state)) {
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
168out:
169	efx_ethtool_fill_self_tests(efx, efx_tests, NULL, data);
170	kfree(efx_tests);
171fail:
172	if (rc)
173		test->flags |= ETH_TEST_FL_FAILED;
174}
175
176void efx_ethtool_get_pauseparam(struct net_device *net_dev,
177				struct ethtool_pauseparam *pause)
178{
179	struct efx_nic *efx = netdev_priv(net_dev);
180
181	pause->rx_pause = !!(efx->wanted_fc & EFX_FC_RX);
182	pause->tx_pause = !!(efx->wanted_fc & EFX_FC_TX);
183	pause->autoneg = !!(efx->wanted_fc & EFX_FC_AUTO);
184}
185
186int efx_ethtool_set_pauseparam(struct net_device *net_dev,
187			       struct ethtool_pauseparam *pause)
188{
189	struct efx_nic *efx = netdev_priv(net_dev);
190	u8 wanted_fc, old_fc;
191	u32 old_adv;
192	int rc = 0;
193
194	mutex_lock(&efx->mac_lock);
195
196	wanted_fc = ((pause->rx_pause ? EFX_FC_RX : 0) |
197		     (pause->tx_pause ? EFX_FC_TX : 0) |
198		     (pause->autoneg ? EFX_FC_AUTO : 0));
199
200	if ((wanted_fc & EFX_FC_TX) && !(wanted_fc & EFX_FC_RX)) {
201		netif_dbg(efx, drv, efx->net_dev,
202			  "Flow control unsupported: tx ON rx OFF\n");
203		rc = -EINVAL;
204		goto out;
205	}
206
207	if ((wanted_fc & EFX_FC_AUTO) && !efx->link_advertising[0]) {
208		netif_dbg(efx, drv, efx->net_dev,
209			  "Autonegotiation is disabled\n");
210		rc = -EINVAL;
211		goto out;
212	}
213
214	/* Hook for Falcon bug 11482 workaround */
215	if (efx->type->prepare_enable_fc_tx &&
216	    (wanted_fc & EFX_FC_TX) && !(efx->wanted_fc & EFX_FC_TX))
217		efx->type->prepare_enable_fc_tx(efx);
218
219	old_adv = efx->link_advertising[0];
220	old_fc = efx->wanted_fc;
221	efx_link_set_wanted_fc(efx, wanted_fc);
222	if (efx->link_advertising[0] != old_adv ||
223	    (efx->wanted_fc ^ old_fc) & EFX_FC_AUTO) {
224		rc = efx_mcdi_port_reconfigure(efx);
225		if (rc) {
226			netif_err(efx, drv, efx->net_dev,
227				  "Unable to advertise requested flow "
228				  "control setting\n");
229			goto out;
230		}
231	}
232
233	/* Reconfigure the MAC. The PHY *may* generate a link state change event
234	 * if the user just changed the advertised capabilities, but there's no
235	 * harm doing this twice */
236	efx_mac_reconfigure(efx, false);
237
238out:
239	mutex_unlock(&efx->mac_lock);
240
241	return rc;
242}
243
244/**
245 * efx_fill_test - fill in an individual self-test entry
246 * @test_index:		Index of the test
247 * @strings:		Ethtool strings, or %NULL
248 * @data:		Ethtool test results, or %NULL
249 * @test:		Pointer to test result (used only if data != %NULL)
250 * @unit_format:	Unit name format (e.g. "chan\%d")
251 * @unit_id:		Unit id (e.g. 0 for "chan0")
252 * @test_format:	Test name format (e.g. "loopback.\%s.tx.sent")
253 * @test_id:		Test id (e.g. "PHYXS" for "loopback.PHYXS.tx_sent")
254 *
255 * Fill in an individual self-test entry.
256 */
257static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
258			  int *test, const char *unit_format, int unit_id,
259			  const char *test_format, const char *test_id)
260{
261	char unit_str[ETH_GSTRING_LEN], test_str[ETH_GSTRING_LEN];
262
263	/* Fill data value, if applicable */
264	if (data)
265		data[test_index] = *test;
266
267	/* Fill string, if applicable */
268	if (strings) {
269		if (strchr(unit_format, '%'))
270			snprintf(unit_str, sizeof(unit_str),
271				 unit_format, unit_id);
272		else
273			strcpy(unit_str, unit_format);
274		snprintf(test_str, sizeof(test_str), test_format, test_id);
275		snprintf(strings + test_index * ETH_GSTRING_LEN,
276			 ETH_GSTRING_LEN,
277			 "%-6s %-24s", unit_str, test_str);
278	}
279}
280
281#define EFX_CHANNEL_NAME(_channel) "chan%d", _channel->channel
282#define EFX_TX_QUEUE_NAME(_tx_queue) "txq%d", _tx_queue->label
283#define EFX_LOOPBACK_NAME(_mode, _counter)			\
284	"loopback.%s." _counter, STRING_TABLE_LOOKUP(_mode, efx_loopback_mode)
285
286/**
287 * efx_fill_loopback_test - fill in a block of loopback self-test entries
288 * @efx:		Efx NIC
289 * @lb_tests:		Efx loopback self-test results structure
290 * @mode:		Loopback test mode
291 * @test_index:		Starting index of the test
292 * @strings:		Ethtool strings, or %NULL
293 * @data:		Ethtool test results, or %NULL
294 *
295 * Fill in a block of loopback self-test entries.  Return new test
296 * index.
297 */
298static int efx_fill_loopback_test(struct efx_nic *efx,
299				  struct efx_loopback_self_tests *lb_tests,
300				  enum efx_loopback_mode mode,
301				  unsigned int test_index,
302				  u8 *strings, u64 *data)
303{
304	struct efx_channel *channel =
305		efx_get_channel(efx, efx->tx_channel_offset);
306	struct efx_tx_queue *tx_queue;
307
308	efx_for_each_channel_tx_queue(tx_queue, channel) {
309		efx_fill_test(test_index++, strings, data,
310			      &lb_tests->tx_sent[tx_queue->label],
311			      EFX_TX_QUEUE_NAME(tx_queue),
312			      EFX_LOOPBACK_NAME(mode, "tx_sent"));
313		efx_fill_test(test_index++, strings, data,
314			      &lb_tests->tx_done[tx_queue->label],
315			      EFX_TX_QUEUE_NAME(tx_queue),
316			      EFX_LOOPBACK_NAME(mode, "tx_done"));
317	}
318	efx_fill_test(test_index++, strings, data,
319		      &lb_tests->rx_good,
320		      "rx", 0,
321		      EFX_LOOPBACK_NAME(mode, "rx_good"));
322	efx_fill_test(test_index++, strings, data,
323		      &lb_tests->rx_bad,
324		      "rx", 0,
325		      EFX_LOOPBACK_NAME(mode, "rx_bad"));
326
327	return test_index;
328}
329
330/**
331 * efx_ethtool_fill_self_tests - get self-test details
332 * @efx:		Efx NIC
333 * @tests:		Efx self-test results structure, or %NULL
334 * @strings:		Ethtool strings, or %NULL
335 * @data:		Ethtool test results, or %NULL
336 *
337 * Get self-test number of strings, strings, and/or test results.
338 * Return number of strings (== number of test results).
339 *
340 * The reason for merging these three functions is to make sure that
341 * they can never be inconsistent.
342 */
343int efx_ethtool_fill_self_tests(struct efx_nic *efx,
344				struct efx_self_tests *tests,
345				u8 *strings, u64 *data)
346{
347	struct efx_channel *channel;
348	unsigned int n = 0, i;
349	enum efx_loopback_mode mode;
350
351	efx_fill_test(n++, strings, data, &tests->phy_alive,
352		      "phy", 0, "alive", NULL);
353	efx_fill_test(n++, strings, data, &tests->nvram,
354		      "core", 0, "nvram", NULL);
355	efx_fill_test(n++, strings, data, &tests->interrupt,
356		      "core", 0, "interrupt", NULL);
357
358	/* Event queues */
359	efx_for_each_channel(channel, efx) {
360		efx_fill_test(n++, strings, data,
361			      &tests->eventq_dma[channel->channel],
362			      EFX_CHANNEL_NAME(channel),
363			      "eventq.dma", NULL);
364		efx_fill_test(n++, strings, data,
365			      &tests->eventq_int[channel->channel],
366			      EFX_CHANNEL_NAME(channel),
367			      "eventq.int", NULL);
368	}
369
370	efx_fill_test(n++, strings, data, &tests->memory,
371		      "core", 0, "memory", NULL);
372	efx_fill_test(n++, strings, data, &tests->registers,
373		      "core", 0, "registers", NULL);
374
375	for (i = 0; true; ++i) {
376		const char *name;
377
378		EFX_WARN_ON_PARANOID(i >= EFX_MAX_PHY_TESTS);
379		name = efx_mcdi_phy_test_name(efx, i);
380		if (name == NULL)
381			break;
382
383		efx_fill_test(n++, strings, data, &tests->phy_ext[i], "phy", 0, name, NULL);
384	}
385
386	/* Loopback tests */
387	for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
388		if (!(efx->loopback_modes & (1 << mode)))
389			continue;
390		n = efx_fill_loopback_test(efx,
391					   &tests->loopback[mode], mode, n,
392					   strings, data);
393	}
394
395	return n;
396}
397
398static 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
442int efx_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_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
459void efx_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			strlcpy(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_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
486void efx_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_ptp_update_stats(efx, data);
558}
559
560/* This must be called with rtnl_lock held. */
561int efx_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_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. */
584int efx_ethtool_set_link_ksettings(struct net_device *net_dev,
585				   const struct ethtool_link_ksettings *cmd)
586{
587	struct efx_nic *efx = netdev_priv(net_dev);
588	int rc;
589
590	/* GMAC does not support 1000Mbps HD */
591	if ((cmd->base.speed == SPEED_1000) &&
592	    (cmd->base.duplex != DUPLEX_FULL)) {
593		netif_dbg(efx, drv, efx->net_dev,
594			  "rejecting unsupported 1000Mbps HD setting\n");
595		return -EINVAL;
596	}
597
598	mutex_lock(&efx->mac_lock);
599	rc = efx_mcdi_phy_set_link_ksettings(efx, cmd);
600	mutex_unlock(&efx->mac_lock);
601	return rc;
602}
603
604int efx_ethtool_get_fecparam(struct net_device *net_dev,
605			     struct ethtool_fecparam *fecparam)
606{
607	struct efx_nic *efx = netdev_priv(net_dev);
608	int rc;
609
610	mutex_lock(&efx->mac_lock);
611	rc = efx_mcdi_phy_get_fecparam(efx, fecparam);
612	mutex_unlock(&efx->mac_lock);
613
614	return rc;
615}
616
617int efx_ethtool_set_fecparam(struct net_device *net_dev,
618			     struct ethtool_fecparam *fecparam)
619{
620	struct efx_nic *efx = netdev_priv(net_dev);
621	int rc;
622
623	mutex_lock(&efx->mac_lock);
624	rc = efx_mcdi_phy_set_fecparam(efx, fecparam);
625	mutex_unlock(&efx->mac_lock);
626
627	return rc;
628}
629
630/* MAC address mask including only I/G bit */
631static const u8 mac_addr_ig_mask[ETH_ALEN] __aligned(2) = {0x01, 0, 0, 0, 0, 0};
632
633#define IP4_ADDR_FULL_MASK	((__force __be32)~0)
634#define IP_PROTO_FULL_MASK	0xFF
635#define PORT_FULL_MASK		((__force __be16)~0)
636#define ETHER_TYPE_FULL_MASK	((__force __be16)~0)
637
638static inline void ip6_fill_mask(__be32 *mask)
639{
640	mask[0] = mask[1] = mask[2] = mask[3] = ~(__be32)0;
641}
642
643static int efx_ethtool_get_class_rule(struct efx_nic *efx,
644				      struct ethtool_rx_flow_spec *rule,
645				      u32 *rss_context)
646{
647	struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
648	struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
649	struct ethtool_usrip4_spec *uip_entry = &rule->h_u.usr_ip4_spec;
650	struct ethtool_usrip4_spec *uip_mask = &rule->m_u.usr_ip4_spec;
651	struct ethtool_tcpip6_spec *ip6_entry = &rule->h_u.tcp_ip6_spec;
652	struct ethtool_tcpip6_spec *ip6_mask = &rule->m_u.tcp_ip6_spec;
653	struct ethtool_usrip6_spec *uip6_entry = &rule->h_u.usr_ip6_spec;
654	struct ethtool_usrip6_spec *uip6_mask = &rule->m_u.usr_ip6_spec;
655	struct ethhdr *mac_entry = &rule->h_u.ether_spec;
656	struct ethhdr *mac_mask = &rule->m_u.ether_spec;
657	struct efx_filter_spec spec;
658	int rc;
659
660	rc = efx_filter_get_filter_safe(efx, EFX_FILTER_PRI_MANUAL,
661					rule->location, &spec);
662	if (rc)
663		return rc;
664
665	if (spec.dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
666		rule->ring_cookie = RX_CLS_FLOW_DISC;
667	else
668		rule->ring_cookie = spec.dmaq_id;
669
670	if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) &&
671	    spec.ether_type == htons(ETH_P_IP) &&
672	    (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) &&
673	    (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) &&
674	    !(spec.match_flags &
675	      ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
676		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
677		EFX_FILTER_MATCH_IP_PROTO |
678		EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) {
679		rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ?
680				   TCP_V4_FLOW : UDP_V4_FLOW);
681		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
682			ip_entry->ip4dst = spec.loc_host[0];
683			ip_mask->ip4dst = IP4_ADDR_FULL_MASK;
684		}
685		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
686			ip_entry->ip4src = spec.rem_host[0];
687			ip_mask->ip4src = IP4_ADDR_FULL_MASK;
688		}
689		if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) {
690			ip_entry->pdst = spec.loc_port;
691			ip_mask->pdst = PORT_FULL_MASK;
692		}
693		if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) {
694			ip_entry->psrc = spec.rem_port;
695			ip_mask->psrc = PORT_FULL_MASK;
696		}
697	} else if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) &&
698	    spec.ether_type == htons(ETH_P_IPV6) &&
699	    (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) &&
700	    (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) &&
701	    !(spec.match_flags &
702	      ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
703		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
704		EFX_FILTER_MATCH_IP_PROTO |
705		EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) {
706		rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ?
707				   TCP_V6_FLOW : UDP_V6_FLOW);
708		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
709			memcpy(ip6_entry->ip6dst, spec.loc_host,
710			       sizeof(ip6_entry->ip6dst));
711			ip6_fill_mask(ip6_mask->ip6dst);
712		}
713		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
714			memcpy(ip6_entry->ip6src, spec.rem_host,
715			       sizeof(ip6_entry->ip6src));
716			ip6_fill_mask(ip6_mask->ip6src);
717		}
718		if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) {
719			ip6_entry->pdst = spec.loc_port;
720			ip6_mask->pdst = PORT_FULL_MASK;
721		}
722		if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) {
723			ip6_entry->psrc = spec.rem_port;
724			ip6_mask->psrc = PORT_FULL_MASK;
725		}
726	} else if (!(spec.match_flags &
727		     ~(EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG |
728		       EFX_FILTER_MATCH_REM_MAC | EFX_FILTER_MATCH_ETHER_TYPE |
729		       EFX_FILTER_MATCH_OUTER_VID))) {
730		rule->flow_type = ETHER_FLOW;
731		if (spec.match_flags &
732		    (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG)) {
733			ether_addr_copy(mac_entry->h_dest, spec.loc_mac);
734			if (spec.match_flags & EFX_FILTER_MATCH_LOC_MAC)
735				eth_broadcast_addr(mac_mask->h_dest);
736			else
737				ether_addr_copy(mac_mask->h_dest,
738						mac_addr_ig_mask);
739		}
740		if (spec.match_flags & EFX_FILTER_MATCH_REM_MAC) {
741			ether_addr_copy(mac_entry->h_source, spec.rem_mac);
742			eth_broadcast_addr(mac_mask->h_source);
743		}
744		if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) {
745			mac_entry->h_proto = spec.ether_type;
746			mac_mask->h_proto = ETHER_TYPE_FULL_MASK;
747		}
748	} else if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE &&
749		   spec.ether_type == htons(ETH_P_IP) &&
750		   !(spec.match_flags &
751		     ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
752		       EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
753		       EFX_FILTER_MATCH_IP_PROTO))) {
754		rule->flow_type = IPV4_USER_FLOW;
755		uip_entry->ip_ver = ETH_RX_NFC_IP4;
756		if (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) {
757			uip_mask->proto = IP_PROTO_FULL_MASK;
758			uip_entry->proto = spec.ip_proto;
759		}
760		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
761			uip_entry->ip4dst = spec.loc_host[0];
762			uip_mask->ip4dst = IP4_ADDR_FULL_MASK;
763		}
764		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
765			uip_entry->ip4src = spec.rem_host[0];
766			uip_mask->ip4src = IP4_ADDR_FULL_MASK;
767		}
768	} else if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE &&
769		   spec.ether_type == htons(ETH_P_IPV6) &&
770		   !(spec.match_flags &
771		     ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
772		       EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
773		       EFX_FILTER_MATCH_IP_PROTO))) {
774		rule->flow_type = IPV6_USER_FLOW;
775		if (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) {
776			uip6_mask->l4_proto = IP_PROTO_FULL_MASK;
777			uip6_entry->l4_proto = spec.ip_proto;
778		}
779		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
780			memcpy(uip6_entry->ip6dst, spec.loc_host,
781			       sizeof(uip6_entry->ip6dst));
782			ip6_fill_mask(uip6_mask->ip6dst);
783		}
784		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
785			memcpy(uip6_entry->ip6src, spec.rem_host,
786			       sizeof(uip6_entry->ip6src));
787			ip6_fill_mask(uip6_mask->ip6src);
788		}
789	} else {
790		/* The above should handle all filters that we insert */
791		WARN_ON(1);
792		return -EINVAL;
793	}
794
795	if (spec.match_flags & EFX_FILTER_MATCH_OUTER_VID) {
796		rule->flow_type |= FLOW_EXT;
797		rule->h_ext.vlan_tci = spec.outer_vid;
798		rule->m_ext.vlan_tci = htons(0xfff);
799	}
800
801	if (spec.flags & EFX_FILTER_FLAG_RX_RSS) {
802		rule->flow_type |= FLOW_RSS;
803		*rss_context = spec.rss_context;
804	}
805
806	return rc;
807}
808
809int efx_ethtool_get_rxnfc(struct net_device *net_dev,
810			  struct ethtool_rxnfc *info, u32 *rule_locs)
811{
812	struct efx_nic *efx = netdev_priv(net_dev);
813	u32 rss_context = 0;
814	s32 rc = 0;
815
816	switch (info->cmd) {
817	case ETHTOOL_GRXRINGS:
818		info->data = efx->n_rx_channels;
819		return 0;
820
821	case ETHTOOL_GRXFH: {
822		struct efx_rss_context *ctx = &efx->rss_context;
823		__u64 data;
824
825		mutex_lock(&efx->rss_lock);
826		if (info->flow_type & FLOW_RSS && info->rss_context) {
827			ctx = efx_find_rss_context_entry(efx, info->rss_context);
828			if (!ctx) {
829				rc = -ENOENT;
830				goto out_unlock;
831			}
832		}
833
834		data = 0;
835		if (!efx_rss_active(ctx)) /* No RSS */
836			goto out_setdata_unlock;
837
838		switch (info->flow_type & ~FLOW_RSS) {
839		case UDP_V4_FLOW:
840		case UDP_V6_FLOW:
841			if (ctx->rx_hash_udp_4tuple)
842				data = (RXH_L4_B_0_1 | RXH_L4_B_2_3 |
843					RXH_IP_SRC | RXH_IP_DST);
844			else
845				data = RXH_IP_SRC | RXH_IP_DST;
846			break;
847		case TCP_V4_FLOW:
848		case TCP_V6_FLOW:
849			data = (RXH_L4_B_0_1 | RXH_L4_B_2_3 |
850				RXH_IP_SRC | RXH_IP_DST);
851			break;
852		case SCTP_V4_FLOW:
853		case SCTP_V6_FLOW:
854		case AH_ESP_V4_FLOW:
855		case AH_ESP_V6_FLOW:
856		case IPV4_FLOW:
857		case IPV6_FLOW:
858			data = RXH_IP_SRC | RXH_IP_DST;
859			break;
860		default:
861			break;
862		}
863out_setdata_unlock:
864		info->data = data;
865out_unlock:
866		mutex_unlock(&efx->rss_lock);
867		return rc;
868	}
869
870	case ETHTOOL_GRXCLSRLCNT:
871		info->data = efx_filter_get_rx_id_limit(efx);
872		if (info->data == 0)
873			return -EOPNOTSUPP;
874		info->data |= RX_CLS_LOC_SPECIAL;
875		info->rule_cnt =
876			efx_filter_count_rx_used(efx, EFX_FILTER_PRI_MANUAL);
877		return 0;
878
879	case ETHTOOL_GRXCLSRULE:
880		if (efx_filter_get_rx_id_limit(efx) == 0)
881			return -EOPNOTSUPP;
882		rc = efx_ethtool_get_class_rule(efx, &info->fs, &rss_context);
883		if (rc < 0)
884			return rc;
885		if (info->fs.flow_type & FLOW_RSS)
886			info->rss_context = rss_context;
887		return 0;
888
889	case ETHTOOL_GRXCLSRLALL:
890		info->data = efx_filter_get_rx_id_limit(efx);
891		if (info->data == 0)
892			return -EOPNOTSUPP;
893		rc = efx_filter_get_rx_ids(efx, EFX_FILTER_PRI_MANUAL,
894					   rule_locs, info->rule_cnt);
895		if (rc < 0)
896			return rc;
897		info->rule_cnt = rc;
898		return 0;
899
900	default:
901		return -EOPNOTSUPP;
902	}
903}
904
905static inline bool ip6_mask_is_full(__be32 mask[4])
906{
907	return !~(mask[0] & mask[1] & mask[2] & mask[3]);
908}
909
910static inline bool ip6_mask_is_empty(__be32 mask[4])
911{
912	return !(mask[0] | mask[1] | mask[2] | mask[3]);
913}
914
915static int efx_ethtool_set_class_rule(struct efx_nic *efx,
916				      struct ethtool_rx_flow_spec *rule,
917				      u32 rss_context)
918{
919	struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
920	struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
921	struct ethtool_usrip4_spec *uip_entry = &rule->h_u.usr_ip4_spec;
922	struct ethtool_usrip4_spec *uip_mask = &rule->m_u.usr_ip4_spec;
923	struct ethtool_tcpip6_spec *ip6_entry = &rule->h_u.tcp_ip6_spec;
924	struct ethtool_tcpip6_spec *ip6_mask = &rule->m_u.tcp_ip6_spec;
925	struct ethtool_usrip6_spec *uip6_entry = &rule->h_u.usr_ip6_spec;
926	struct ethtool_usrip6_spec *uip6_mask = &rule->m_u.usr_ip6_spec;
927	u32 flow_type = rule->flow_type & ~(FLOW_EXT | FLOW_RSS);
928	struct ethhdr *mac_entry = &rule->h_u.ether_spec;
929	struct ethhdr *mac_mask = &rule->m_u.ether_spec;
930	enum efx_filter_flags flags = 0;
931	struct efx_filter_spec spec;
932	int rc;
933
934	/* Check that user wants us to choose the location */
935	if (rule->location != RX_CLS_LOC_ANY)
936		return -EINVAL;
937
938	/* Range-check ring_cookie */
939	if (rule->ring_cookie >= efx->n_rx_channels &&
940	    rule->ring_cookie != RX_CLS_FLOW_DISC)
941		return -EINVAL;
942
943	/* Check for unsupported extensions */
944	if ((rule->flow_type & FLOW_EXT) &&
945	    (rule->m_ext.vlan_etype || rule->m_ext.data[0] ||
946	     rule->m_ext.data[1]))
947		return -EINVAL;
948
949	if (efx->rx_scatter)
950		flags |= EFX_FILTER_FLAG_RX_SCATTER;
951	if (rule->flow_type & FLOW_RSS)
952		flags |= EFX_FILTER_FLAG_RX_RSS;
953
954	efx_filter_init_rx(&spec, EFX_FILTER_PRI_MANUAL, flags,
955			   (rule->ring_cookie == RX_CLS_FLOW_DISC) ?
956			   EFX_FILTER_RX_DMAQ_ID_DROP : rule->ring_cookie);
957
958	if (rule->flow_type & FLOW_RSS)
959		spec.rss_context = rss_context;
960
961	switch (flow_type) {
962	case TCP_V4_FLOW:
963	case UDP_V4_FLOW:
964		spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE |
965				    EFX_FILTER_MATCH_IP_PROTO);
966		spec.ether_type = htons(ETH_P_IP);
967		spec.ip_proto = flow_type == TCP_V4_FLOW ? IPPROTO_TCP
968							 : IPPROTO_UDP;
969		if (ip_mask->ip4dst) {
970			if (ip_mask->ip4dst != IP4_ADDR_FULL_MASK)
971				return -EINVAL;
972			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
973			spec.loc_host[0] = ip_entry->ip4dst;
974		}
975		if (ip_mask->ip4src) {
976			if (ip_mask->ip4src != IP4_ADDR_FULL_MASK)
977				return -EINVAL;
978			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
979			spec.rem_host[0] = ip_entry->ip4src;
980		}
981		if (ip_mask->pdst) {
982			if (ip_mask->pdst != PORT_FULL_MASK)
983				return -EINVAL;
984			spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT;
985			spec.loc_port = ip_entry->pdst;
986		}
987		if (ip_mask->psrc) {
988			if (ip_mask->psrc != PORT_FULL_MASK)
989				return -EINVAL;
990			spec.match_flags |= EFX_FILTER_MATCH_REM_PORT;
991			spec.rem_port = ip_entry->psrc;
992		}
993		if (ip_mask->tos)
994			return -EINVAL;
995		break;
996
997	case TCP_V6_FLOW:
998	case UDP_V6_FLOW:
999		spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE |
1000				    EFX_FILTER_MATCH_IP_PROTO);
1001		spec.ether_type = htons(ETH_P_IPV6);
1002		spec.ip_proto = flow_type == TCP_V6_FLOW ? IPPROTO_TCP
1003							 : IPPROTO_UDP;
1004		if (!ip6_mask_is_empty(ip6_mask->ip6dst)) {
1005			if (!ip6_mask_is_full(ip6_mask->ip6dst))
1006				return -EINVAL;
1007			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1008			memcpy(spec.loc_host, ip6_entry->ip6dst, sizeof(spec.loc_host));
1009		}
1010		if (!ip6_mask_is_empty(ip6_mask->ip6src)) {
1011			if (!ip6_mask_is_full(ip6_mask->ip6src))
1012				return -EINVAL;
1013			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1014			memcpy(spec.rem_host, ip6_entry->ip6src, sizeof(spec.rem_host));
1015		}
1016		if (ip6_mask->pdst) {
1017			if (ip6_mask->pdst != PORT_FULL_MASK)
1018				return -EINVAL;
1019			spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT;
1020			spec.loc_port = ip6_entry->pdst;
1021		}
1022		if (ip6_mask->psrc) {
1023			if (ip6_mask->psrc != PORT_FULL_MASK)
1024				return -EINVAL;
1025			spec.match_flags |= EFX_FILTER_MATCH_REM_PORT;
1026			spec.rem_port = ip6_entry->psrc;
1027		}
1028		if (ip6_mask->tclass)
1029			return -EINVAL;
1030		break;
1031
1032	case IPV4_USER_FLOW:
1033		if (uip_mask->l4_4_bytes || uip_mask->tos || uip_mask->ip_ver ||
1034		    uip_entry->ip_ver != ETH_RX_NFC_IP4)
1035			return -EINVAL;
1036		spec.match_flags = EFX_FILTER_MATCH_ETHER_TYPE;
1037		spec.ether_type = htons(ETH_P_IP);
1038		if (uip_mask->ip4dst) {
1039			if (uip_mask->ip4dst != IP4_ADDR_FULL_MASK)
1040				return -EINVAL;
1041			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1042			spec.loc_host[0] = uip_entry->ip4dst;
1043		}
1044		if (uip_mask->ip4src) {
1045			if (uip_mask->ip4src != IP4_ADDR_FULL_MASK)
1046				return -EINVAL;
1047			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1048			spec.rem_host[0] = uip_entry->ip4src;
1049		}
1050		if (uip_mask->proto) {
1051			if (uip_mask->proto != IP_PROTO_FULL_MASK)
1052				return -EINVAL;
1053			spec.match_flags |= EFX_FILTER_MATCH_IP_PROTO;
1054			spec.ip_proto = uip_entry->proto;
1055		}
1056		break;
1057
1058	case IPV6_USER_FLOW:
1059		if (uip6_mask->l4_4_bytes || uip6_mask->tclass)
1060			return -EINVAL;
1061		spec.match_flags = EFX_FILTER_MATCH_ETHER_TYPE;
1062		spec.ether_type = htons(ETH_P_IPV6);
1063		if (!ip6_mask_is_empty(uip6_mask->ip6dst)) {
1064			if (!ip6_mask_is_full(uip6_mask->ip6dst))
1065				return -EINVAL;
1066			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1067			memcpy(spec.loc_host, uip6_entry->ip6dst, sizeof(spec.loc_host));
1068		}
1069		if (!ip6_mask_is_empty(uip6_mask->ip6src)) {
1070			if (!ip6_mask_is_full(uip6_mask->ip6src))
1071				return -EINVAL;
1072			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1073			memcpy(spec.rem_host, uip6_entry->ip6src, sizeof(spec.rem_host));
1074		}
1075		if (uip6_mask->l4_proto) {
1076			if (uip6_mask->l4_proto != IP_PROTO_FULL_MASK)
1077				return -EINVAL;
1078			spec.match_flags |= EFX_FILTER_MATCH_IP_PROTO;
1079			spec.ip_proto = uip6_entry->l4_proto;
1080		}
1081		break;
1082
1083	case ETHER_FLOW:
1084		if (!is_zero_ether_addr(mac_mask->h_dest)) {
1085			if (ether_addr_equal(mac_mask->h_dest,
1086					     mac_addr_ig_mask))
1087				spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC_IG;
1088			else if (is_broadcast_ether_addr(mac_mask->h_dest))
1089				spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC;
1090			else
1091				return -EINVAL;
1092			ether_addr_copy(spec.loc_mac, mac_entry->h_dest);
1093		}
1094		if (!is_zero_ether_addr(mac_mask->h_source)) {
1095			if (!is_broadcast_ether_addr(mac_mask->h_source))
1096				return -EINVAL;
1097			spec.match_flags |= EFX_FILTER_MATCH_REM_MAC;
1098			ether_addr_copy(spec.rem_mac, mac_entry->h_source);
1099		}
1100		if (mac_mask->h_proto) {
1101			if (mac_mask->h_proto != ETHER_TYPE_FULL_MASK)
1102				return -EINVAL;
1103			spec.match_flags |= EFX_FILTER_MATCH_ETHER_TYPE;
1104			spec.ether_type = mac_entry->h_proto;
1105		}
1106		break;
1107
1108	default:
1109		return -EINVAL;
1110	}
1111
1112	if ((rule->flow_type & FLOW_EXT) && rule->m_ext.vlan_tci) {
1113		if (rule->m_ext.vlan_tci != htons(0xfff))
1114			return -EINVAL;
1115		spec.match_flags |= EFX_FILTER_MATCH_OUTER_VID;
1116		spec.outer_vid = rule->h_ext.vlan_tci;
1117	}
1118
1119	rc = efx_filter_insert_filter(efx, &spec, true);
1120	if (rc < 0)
1121		return rc;
1122
1123	rule->location = rc;
1124	return 0;
1125}
1126
1127int efx_ethtool_set_rxnfc(struct net_device *net_dev,
1128			  struct ethtool_rxnfc *info)
1129{
1130	struct efx_nic *efx = netdev_priv(net_dev);
1131
1132	if (efx_filter_get_rx_id_limit(efx) == 0)
1133		return -EOPNOTSUPP;
1134
1135	switch (info->cmd) {
1136	case ETHTOOL_SRXCLSRLINS:
1137		return efx_ethtool_set_class_rule(efx, &info->fs,
1138						  info->rss_context);
1139
1140	case ETHTOOL_SRXCLSRLDEL:
1141		return efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_MANUAL,
1142						 info->fs.location);
1143
1144	default:
1145		return -EOPNOTSUPP;
1146	}
1147}
1148
1149u32 efx_ethtool_get_rxfh_indir_size(struct net_device *net_dev)
1150{
1151	struct efx_nic *efx = netdev_priv(net_dev);
1152
1153	if (efx->n_rx_channels == 1)
1154		return 0;
1155	return ARRAY_SIZE(efx->rss_context.rx_indir_table);
1156}
1157
1158u32 efx_ethtool_get_rxfh_key_size(struct net_device *net_dev)
1159{
1160	struct efx_nic *efx = netdev_priv(net_dev);
1161
1162	return efx->type->rx_hash_key_size;
1163}
1164
1165int efx_ethtool_get_rxfh(struct net_device *net_dev, u32 *indir, u8 *key,
1166			 u8 *hfunc)
1167{
1168	struct efx_nic *efx = netdev_priv(net_dev);
1169	int rc;
1170
1171	rc = efx->type->rx_pull_rss_config(efx);
1172	if (rc)
1173		return rc;
1174
1175	if (hfunc)
1176		*hfunc = ETH_RSS_HASH_TOP;
1177	if (indir)
1178		memcpy(indir, efx->rss_context.rx_indir_table,
1179		       sizeof(efx->rss_context.rx_indir_table));
1180	if (key)
1181		memcpy(key, efx->rss_context.rx_hash_key,
1182		       efx->type->rx_hash_key_size);
1183	return 0;
1184}
1185
1186int efx_ethtool_set_rxfh(struct net_device *net_dev, const u32 *indir,
1187			 const u8 *key, const u8 hfunc)
1188{
1189	struct efx_nic *efx = netdev_priv(net_dev);
1190
1191	/* Hash function is Toeplitz, cannot be changed */
1192	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1193		return -EOPNOTSUPP;
1194	if (!indir && !key)
1195		return 0;
1196
1197	if (!key)
1198		key = efx->rss_context.rx_hash_key;
1199	if (!indir)
1200		indir = efx->rss_context.rx_indir_table;
1201
1202	return efx->type->rx_push_rss_config(efx, true, indir, key);
1203}
1204
1205int efx_ethtool_get_rxfh_context(struct net_device *net_dev, u32 *indir,
1206				 u8 *key, u8 *hfunc, u32 rss_context)
1207{
1208	struct efx_nic *efx = netdev_priv(net_dev);
1209	struct efx_rss_context *ctx;
1210	int rc = 0;
1211
1212	if (!efx->type->rx_pull_rss_context_config)
1213		return -EOPNOTSUPP;
1214
1215	mutex_lock(&efx->rss_lock);
1216	ctx = efx_find_rss_context_entry(efx, rss_context);
1217	if (!ctx) {
1218		rc = -ENOENT;
1219		goto out_unlock;
1220	}
1221	rc = efx->type->rx_pull_rss_context_config(efx, ctx);
1222	if (rc)
1223		goto out_unlock;
1224
1225	if (hfunc)
1226		*hfunc = ETH_RSS_HASH_TOP;
1227	if (indir)
1228		memcpy(indir, ctx->rx_indir_table, sizeof(ctx->rx_indir_table));
1229	if (key)
1230		memcpy(key, ctx->rx_hash_key, efx->type->rx_hash_key_size);
1231out_unlock:
1232	mutex_unlock(&efx->rss_lock);
1233	return rc;
1234}
1235
1236int efx_ethtool_set_rxfh_context(struct net_device *net_dev,
1237				 const u32 *indir, const u8 *key,
1238				 const u8 hfunc, u32 *rss_context,
1239				 bool delete)
1240{
1241	struct efx_nic *efx = netdev_priv(net_dev);
1242	struct efx_rss_context *ctx;
1243	bool allocated = false;
1244	int rc;
1245
1246	if (!efx->type->rx_push_rss_context_config)
1247		return -EOPNOTSUPP;
1248	/* Hash function is Toeplitz, cannot be changed */
1249	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1250		return -EOPNOTSUPP;
1251
1252	mutex_lock(&efx->rss_lock);
1253
1254	if (*rss_context == ETH_RXFH_CONTEXT_ALLOC) {
1255		if (delete) {
1256			/* alloc + delete == Nothing to do */
1257			rc = -EINVAL;
1258			goto out_unlock;
1259		}
1260		ctx = efx_alloc_rss_context_entry(efx);
1261		if (!ctx) {
1262			rc = -ENOMEM;
1263			goto out_unlock;
1264		}
1265		ctx->context_id = EFX_MCDI_RSS_CONTEXT_INVALID;
1266		/* Initialise indir table and key to defaults */
1267		efx_set_default_rx_indir_table(efx, ctx);
1268		netdev_rss_key_fill(ctx->rx_hash_key, sizeof(ctx->rx_hash_key));
1269		allocated = true;
1270	} else {
1271		ctx = efx_find_rss_context_entry(efx, *rss_context);
1272		if (!ctx) {
1273			rc = -ENOENT;
1274			goto out_unlock;
1275		}
1276	}
1277
1278	if (delete) {
1279		/* delete this context */
1280		rc = efx->type->rx_push_rss_context_config(efx, ctx, NULL, NULL);
1281		if (!rc)
1282			efx_free_rss_context_entry(ctx);
1283		goto out_unlock;
1284	}
1285
1286	if (!key)
1287		key = ctx->rx_hash_key;
1288	if (!indir)
1289		indir = ctx->rx_indir_table;
1290
1291	rc = efx->type->rx_push_rss_context_config(efx, ctx, indir, key);
1292	if (rc && allocated)
1293		efx_free_rss_context_entry(ctx);
1294	else
1295		*rss_context = ctx->user_id;
1296out_unlock:
1297	mutex_unlock(&efx->rss_lock);
1298	return rc;
1299}
1300
1301int efx_ethtool_reset(struct net_device *net_dev, u32 *flags)
1302{
1303	struct efx_nic *efx = netdev_priv(net_dev);
1304	int rc;
1305
1306	rc = efx->type->map_reset_flags(flags);
1307	if (rc < 0)
1308		return rc;
1309
1310	return efx_reset(efx, rc);
1311}
1312
1313int efx_ethtool_get_module_eeprom(struct net_device *net_dev,
1314				  struct ethtool_eeprom *ee,
1315				  u8 *data)
1316{
1317	struct efx_nic *efx = netdev_priv(net_dev);
1318	int ret;
1319
1320	mutex_lock(&efx->mac_lock);
1321	ret = efx_mcdi_phy_get_module_eeprom(efx, ee, data);
1322	mutex_unlock(&efx->mac_lock);
1323
1324	return ret;
1325}
1326
1327int efx_ethtool_get_module_info(struct net_device *net_dev,
1328				struct ethtool_modinfo *modinfo)
1329{
1330	struct efx_nic *efx = netdev_priv(net_dev);
1331	int ret;
1332
1333	mutex_lock(&efx->mac_lock);
1334	ret = efx_mcdi_phy_get_module_info(efx, modinfo);
1335	mutex_unlock(&efx->mac_lock);
1336
1337	return ret;
1338}
1339