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