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