xref: /openbmc/linux/drivers/net/ethernet/sfc/ethtool.c (revision cce8e04c)
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, NULL);
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[0]) {
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[0];
736 	old_fc = efx->wanted_fc;
737 	efx_link_set_wanted_fc(efx, wanted_fc);
738 	if (efx->link_advertising[0] != 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 				      u32 *rss_context)
813 {
814 	struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
815 	struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
816 	struct ethtool_usrip4_spec *uip_entry = &rule->h_u.usr_ip4_spec;
817 	struct ethtool_usrip4_spec *uip_mask = &rule->m_u.usr_ip4_spec;
818 	struct ethtool_tcpip6_spec *ip6_entry = &rule->h_u.tcp_ip6_spec;
819 	struct ethtool_tcpip6_spec *ip6_mask = &rule->m_u.tcp_ip6_spec;
820 	struct ethtool_usrip6_spec *uip6_entry = &rule->h_u.usr_ip6_spec;
821 	struct ethtool_usrip6_spec *uip6_mask = &rule->m_u.usr_ip6_spec;
822 	struct ethhdr *mac_entry = &rule->h_u.ether_spec;
823 	struct ethhdr *mac_mask = &rule->m_u.ether_spec;
824 	struct efx_filter_spec spec;
825 	int rc;
826 
827 	rc = efx_filter_get_filter_safe(efx, EFX_FILTER_PRI_MANUAL,
828 					rule->location, &spec);
829 	if (rc)
830 		return rc;
831 
832 	if (spec.dmaq_id == EFX_FILTER_RX_DMAQ_ID_DROP)
833 		rule->ring_cookie = RX_CLS_FLOW_DISC;
834 	else
835 		rule->ring_cookie = spec.dmaq_id;
836 
837 	if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) &&
838 	    spec.ether_type == htons(ETH_P_IP) &&
839 	    (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) &&
840 	    (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) &&
841 	    !(spec.match_flags &
842 	      ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
843 		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
844 		EFX_FILTER_MATCH_IP_PROTO |
845 		EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) {
846 		rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ?
847 				   TCP_V4_FLOW : UDP_V4_FLOW);
848 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
849 			ip_entry->ip4dst = spec.loc_host[0];
850 			ip_mask->ip4dst = IP4_ADDR_FULL_MASK;
851 		}
852 		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
853 			ip_entry->ip4src = spec.rem_host[0];
854 			ip_mask->ip4src = IP4_ADDR_FULL_MASK;
855 		}
856 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) {
857 			ip_entry->pdst = spec.loc_port;
858 			ip_mask->pdst = PORT_FULL_MASK;
859 		}
860 		if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) {
861 			ip_entry->psrc = spec.rem_port;
862 			ip_mask->psrc = PORT_FULL_MASK;
863 		}
864 	} else if ((spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) &&
865 	    spec.ether_type == htons(ETH_P_IPV6) &&
866 	    (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) &&
867 	    (spec.ip_proto == IPPROTO_TCP || spec.ip_proto == IPPROTO_UDP) &&
868 	    !(spec.match_flags &
869 	      ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
870 		EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
871 		EFX_FILTER_MATCH_IP_PROTO |
872 		EFX_FILTER_MATCH_LOC_PORT | EFX_FILTER_MATCH_REM_PORT))) {
873 		rule->flow_type = ((spec.ip_proto == IPPROTO_TCP) ?
874 				   TCP_V6_FLOW : UDP_V6_FLOW);
875 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
876 			memcpy(ip6_entry->ip6dst, spec.loc_host,
877 			       sizeof(ip6_entry->ip6dst));
878 			ip6_fill_mask(ip6_mask->ip6dst);
879 		}
880 		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
881 			memcpy(ip6_entry->ip6src, spec.rem_host,
882 			       sizeof(ip6_entry->ip6src));
883 			ip6_fill_mask(ip6_mask->ip6src);
884 		}
885 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_PORT) {
886 			ip6_entry->pdst = spec.loc_port;
887 			ip6_mask->pdst = PORT_FULL_MASK;
888 		}
889 		if (spec.match_flags & EFX_FILTER_MATCH_REM_PORT) {
890 			ip6_entry->psrc = spec.rem_port;
891 			ip6_mask->psrc = PORT_FULL_MASK;
892 		}
893 	} else if (!(spec.match_flags &
894 		     ~(EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG |
895 		       EFX_FILTER_MATCH_REM_MAC | EFX_FILTER_MATCH_ETHER_TYPE |
896 		       EFX_FILTER_MATCH_OUTER_VID))) {
897 		rule->flow_type = ETHER_FLOW;
898 		if (spec.match_flags &
899 		    (EFX_FILTER_MATCH_LOC_MAC | EFX_FILTER_MATCH_LOC_MAC_IG)) {
900 			ether_addr_copy(mac_entry->h_dest, spec.loc_mac);
901 			if (spec.match_flags & EFX_FILTER_MATCH_LOC_MAC)
902 				eth_broadcast_addr(mac_mask->h_dest);
903 			else
904 				ether_addr_copy(mac_mask->h_dest,
905 						mac_addr_ig_mask);
906 		}
907 		if (spec.match_flags & EFX_FILTER_MATCH_REM_MAC) {
908 			ether_addr_copy(mac_entry->h_source, spec.rem_mac);
909 			eth_broadcast_addr(mac_mask->h_source);
910 		}
911 		if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE) {
912 			mac_entry->h_proto = spec.ether_type;
913 			mac_mask->h_proto = ETHER_TYPE_FULL_MASK;
914 		}
915 	} else if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE &&
916 		   spec.ether_type == htons(ETH_P_IP) &&
917 		   !(spec.match_flags &
918 		     ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
919 		       EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
920 		       EFX_FILTER_MATCH_IP_PROTO))) {
921 		rule->flow_type = IPV4_USER_FLOW;
922 		uip_entry->ip_ver = ETH_RX_NFC_IP4;
923 		if (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) {
924 			uip_mask->proto = IP_PROTO_FULL_MASK;
925 			uip_entry->proto = spec.ip_proto;
926 		}
927 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
928 			uip_entry->ip4dst = spec.loc_host[0];
929 			uip_mask->ip4dst = IP4_ADDR_FULL_MASK;
930 		}
931 		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
932 			uip_entry->ip4src = spec.rem_host[0];
933 			uip_mask->ip4src = IP4_ADDR_FULL_MASK;
934 		}
935 	} else if (spec.match_flags & EFX_FILTER_MATCH_ETHER_TYPE &&
936 		   spec.ether_type == htons(ETH_P_IPV6) &&
937 		   !(spec.match_flags &
938 		     ~(EFX_FILTER_MATCH_ETHER_TYPE | EFX_FILTER_MATCH_OUTER_VID |
939 		       EFX_FILTER_MATCH_LOC_HOST | EFX_FILTER_MATCH_REM_HOST |
940 		       EFX_FILTER_MATCH_IP_PROTO))) {
941 		rule->flow_type = IPV6_USER_FLOW;
942 		if (spec.match_flags & EFX_FILTER_MATCH_IP_PROTO) {
943 			uip6_mask->l4_proto = IP_PROTO_FULL_MASK;
944 			uip6_entry->l4_proto = spec.ip_proto;
945 		}
946 		if (spec.match_flags & EFX_FILTER_MATCH_LOC_HOST) {
947 			memcpy(uip6_entry->ip6dst, spec.loc_host,
948 			       sizeof(uip6_entry->ip6dst));
949 			ip6_fill_mask(uip6_mask->ip6dst);
950 		}
951 		if (spec.match_flags & EFX_FILTER_MATCH_REM_HOST) {
952 			memcpy(uip6_entry->ip6src, spec.rem_host,
953 			       sizeof(uip6_entry->ip6src));
954 			ip6_fill_mask(uip6_mask->ip6src);
955 		}
956 	} else {
957 		/* The above should handle all filters that we insert */
958 		WARN_ON(1);
959 		return -EINVAL;
960 	}
961 
962 	if (spec.match_flags & EFX_FILTER_MATCH_OUTER_VID) {
963 		rule->flow_type |= FLOW_EXT;
964 		rule->h_ext.vlan_tci = spec.outer_vid;
965 		rule->m_ext.vlan_tci = htons(0xfff);
966 	}
967 
968 	if (spec.flags & EFX_FILTER_FLAG_RX_RSS) {
969 		rule->flow_type |= FLOW_RSS;
970 		*rss_context = spec.rss_context;
971 	}
972 
973 	return rc;
974 }
975 
976 static int
977 efx_ethtool_get_rxnfc(struct net_device *net_dev,
978 		      struct ethtool_rxnfc *info, u32 *rule_locs)
979 {
980 	struct efx_nic *efx = netdev_priv(net_dev);
981 	u32 rss_context = 0;
982 	s32 rc = 0;
983 
984 	switch (info->cmd) {
985 	case ETHTOOL_GRXRINGS:
986 		info->data = efx->n_rx_channels;
987 		return 0;
988 
989 	case ETHTOOL_GRXFH: {
990 		struct efx_rss_context *ctx = &efx->rss_context;
991 
992 		mutex_lock(&efx->rss_lock);
993 		if (info->flow_type & FLOW_RSS && info->rss_context) {
994 			ctx = efx_find_rss_context_entry(efx, info->rss_context);
995 			if (!ctx) {
996 				rc = -ENOENT;
997 				goto out_unlock;
998 			}
999 		}
1000 		info->data = 0;
1001 		if (!efx_rss_active(ctx)) /* No RSS */
1002 			goto out_unlock;
1003 		switch (info->flow_type & ~FLOW_RSS) {
1004 		case UDP_V4_FLOW:
1005 			if (ctx->rx_hash_udp_4tuple)
1006 				/* fall through */
1007 		case TCP_V4_FLOW:
1008 				info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1009 			/* fall through */
1010 		case SCTP_V4_FLOW:
1011 		case AH_ESP_V4_FLOW:
1012 		case IPV4_FLOW:
1013 			info->data |= RXH_IP_SRC | RXH_IP_DST;
1014 			break;
1015 		case UDP_V6_FLOW:
1016 			if (ctx->rx_hash_udp_4tuple)
1017 				/* fall through */
1018 		case TCP_V6_FLOW:
1019 				info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
1020 			/* fall through */
1021 		case SCTP_V6_FLOW:
1022 		case AH_ESP_V6_FLOW:
1023 		case IPV6_FLOW:
1024 			info->data |= RXH_IP_SRC | RXH_IP_DST;
1025 			break;
1026 		default:
1027 			break;
1028 		}
1029 out_unlock:
1030 		mutex_unlock(&efx->rss_lock);
1031 		return rc;
1032 	}
1033 
1034 	case ETHTOOL_GRXCLSRLCNT:
1035 		info->data = efx_filter_get_rx_id_limit(efx);
1036 		if (info->data == 0)
1037 			return -EOPNOTSUPP;
1038 		info->data |= RX_CLS_LOC_SPECIAL;
1039 		info->rule_cnt =
1040 			efx_filter_count_rx_used(efx, EFX_FILTER_PRI_MANUAL);
1041 		return 0;
1042 
1043 	case ETHTOOL_GRXCLSRULE:
1044 		if (efx_filter_get_rx_id_limit(efx) == 0)
1045 			return -EOPNOTSUPP;
1046 		rc = efx_ethtool_get_class_rule(efx, &info->fs, &rss_context);
1047 		if (rc < 0)
1048 			return rc;
1049 		if (info->fs.flow_type & FLOW_RSS)
1050 			info->rss_context = rss_context;
1051 		return 0;
1052 
1053 	case ETHTOOL_GRXCLSRLALL:
1054 		info->data = efx_filter_get_rx_id_limit(efx);
1055 		if (info->data == 0)
1056 			return -EOPNOTSUPP;
1057 		rc = efx_filter_get_rx_ids(efx, EFX_FILTER_PRI_MANUAL,
1058 					   rule_locs, info->rule_cnt);
1059 		if (rc < 0)
1060 			return rc;
1061 		info->rule_cnt = rc;
1062 		return 0;
1063 
1064 	default:
1065 		return -EOPNOTSUPP;
1066 	}
1067 }
1068 
1069 static inline bool ip6_mask_is_full(__be32 mask[4])
1070 {
1071 	return !~(mask[0] & mask[1] & mask[2] & mask[3]);
1072 }
1073 
1074 static inline bool ip6_mask_is_empty(__be32 mask[4])
1075 {
1076 	return !(mask[0] | mask[1] | mask[2] | mask[3]);
1077 }
1078 
1079 static int efx_ethtool_set_class_rule(struct efx_nic *efx,
1080 				      struct ethtool_rx_flow_spec *rule,
1081 				      u32 rss_context)
1082 {
1083 	struct ethtool_tcpip4_spec *ip_entry = &rule->h_u.tcp_ip4_spec;
1084 	struct ethtool_tcpip4_spec *ip_mask = &rule->m_u.tcp_ip4_spec;
1085 	struct ethtool_usrip4_spec *uip_entry = &rule->h_u.usr_ip4_spec;
1086 	struct ethtool_usrip4_spec *uip_mask = &rule->m_u.usr_ip4_spec;
1087 	struct ethtool_tcpip6_spec *ip6_entry = &rule->h_u.tcp_ip6_spec;
1088 	struct ethtool_tcpip6_spec *ip6_mask = &rule->m_u.tcp_ip6_spec;
1089 	struct ethtool_usrip6_spec *uip6_entry = &rule->h_u.usr_ip6_spec;
1090 	struct ethtool_usrip6_spec *uip6_mask = &rule->m_u.usr_ip6_spec;
1091 	u32 flow_type = rule->flow_type & ~(FLOW_EXT | FLOW_RSS);
1092 	struct ethhdr *mac_entry = &rule->h_u.ether_spec;
1093 	struct ethhdr *mac_mask = &rule->m_u.ether_spec;
1094 	enum efx_filter_flags flags = 0;
1095 	struct efx_filter_spec spec;
1096 	int rc;
1097 
1098 	/* Check that user wants us to choose the location */
1099 	if (rule->location != RX_CLS_LOC_ANY)
1100 		return -EINVAL;
1101 
1102 	/* Range-check ring_cookie */
1103 	if (rule->ring_cookie >= efx->n_rx_channels &&
1104 	    rule->ring_cookie != RX_CLS_FLOW_DISC)
1105 		return -EINVAL;
1106 
1107 	/* Check for unsupported extensions */
1108 	if ((rule->flow_type & FLOW_EXT) &&
1109 	    (rule->m_ext.vlan_etype || rule->m_ext.data[0] ||
1110 	     rule->m_ext.data[1]))
1111 		return -EINVAL;
1112 
1113 	if (efx->rx_scatter)
1114 		flags |= EFX_FILTER_FLAG_RX_SCATTER;
1115 	if (rule->flow_type & FLOW_RSS)
1116 		flags |= EFX_FILTER_FLAG_RX_RSS;
1117 
1118 	efx_filter_init_rx(&spec, EFX_FILTER_PRI_MANUAL, flags,
1119 			   (rule->ring_cookie == RX_CLS_FLOW_DISC) ?
1120 			   EFX_FILTER_RX_DMAQ_ID_DROP : rule->ring_cookie);
1121 
1122 	if (rule->flow_type & FLOW_RSS)
1123 		spec.rss_context = rss_context;
1124 
1125 	switch (flow_type) {
1126 	case TCP_V4_FLOW:
1127 	case UDP_V4_FLOW:
1128 		spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE |
1129 				    EFX_FILTER_MATCH_IP_PROTO);
1130 		spec.ether_type = htons(ETH_P_IP);
1131 		spec.ip_proto = flow_type == TCP_V4_FLOW ? IPPROTO_TCP
1132 							 : IPPROTO_UDP;
1133 		if (ip_mask->ip4dst) {
1134 			if (ip_mask->ip4dst != IP4_ADDR_FULL_MASK)
1135 				return -EINVAL;
1136 			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1137 			spec.loc_host[0] = ip_entry->ip4dst;
1138 		}
1139 		if (ip_mask->ip4src) {
1140 			if (ip_mask->ip4src != IP4_ADDR_FULL_MASK)
1141 				return -EINVAL;
1142 			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1143 			spec.rem_host[0] = ip_entry->ip4src;
1144 		}
1145 		if (ip_mask->pdst) {
1146 			if (ip_mask->pdst != PORT_FULL_MASK)
1147 				return -EINVAL;
1148 			spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT;
1149 			spec.loc_port = ip_entry->pdst;
1150 		}
1151 		if (ip_mask->psrc) {
1152 			if (ip_mask->psrc != PORT_FULL_MASK)
1153 				return -EINVAL;
1154 			spec.match_flags |= EFX_FILTER_MATCH_REM_PORT;
1155 			spec.rem_port = ip_entry->psrc;
1156 		}
1157 		if (ip_mask->tos)
1158 			return -EINVAL;
1159 		break;
1160 
1161 	case TCP_V6_FLOW:
1162 	case UDP_V6_FLOW:
1163 		spec.match_flags = (EFX_FILTER_MATCH_ETHER_TYPE |
1164 				    EFX_FILTER_MATCH_IP_PROTO);
1165 		spec.ether_type = htons(ETH_P_IPV6);
1166 		spec.ip_proto = flow_type == TCP_V6_FLOW ? IPPROTO_TCP
1167 							 : IPPROTO_UDP;
1168 		if (!ip6_mask_is_empty(ip6_mask->ip6dst)) {
1169 			if (!ip6_mask_is_full(ip6_mask->ip6dst))
1170 				return -EINVAL;
1171 			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1172 			memcpy(spec.loc_host, ip6_entry->ip6dst, sizeof(spec.loc_host));
1173 		}
1174 		if (!ip6_mask_is_empty(ip6_mask->ip6src)) {
1175 			if (!ip6_mask_is_full(ip6_mask->ip6src))
1176 				return -EINVAL;
1177 			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1178 			memcpy(spec.rem_host, ip6_entry->ip6src, sizeof(spec.rem_host));
1179 		}
1180 		if (ip6_mask->pdst) {
1181 			if (ip6_mask->pdst != PORT_FULL_MASK)
1182 				return -EINVAL;
1183 			spec.match_flags |= EFX_FILTER_MATCH_LOC_PORT;
1184 			spec.loc_port = ip6_entry->pdst;
1185 		}
1186 		if (ip6_mask->psrc) {
1187 			if (ip6_mask->psrc != PORT_FULL_MASK)
1188 				return -EINVAL;
1189 			spec.match_flags |= EFX_FILTER_MATCH_REM_PORT;
1190 			spec.rem_port = ip6_entry->psrc;
1191 		}
1192 		if (ip6_mask->tclass)
1193 			return -EINVAL;
1194 		break;
1195 
1196 	case IPV4_USER_FLOW:
1197 		if (uip_mask->l4_4_bytes || uip_mask->tos || uip_mask->ip_ver ||
1198 		    uip_entry->ip_ver != ETH_RX_NFC_IP4)
1199 			return -EINVAL;
1200 		spec.match_flags = EFX_FILTER_MATCH_ETHER_TYPE;
1201 		spec.ether_type = htons(ETH_P_IP);
1202 		if (uip_mask->ip4dst) {
1203 			if (uip_mask->ip4dst != IP4_ADDR_FULL_MASK)
1204 				return -EINVAL;
1205 			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1206 			spec.loc_host[0] = uip_entry->ip4dst;
1207 		}
1208 		if (uip_mask->ip4src) {
1209 			if (uip_mask->ip4src != IP4_ADDR_FULL_MASK)
1210 				return -EINVAL;
1211 			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1212 			spec.rem_host[0] = uip_entry->ip4src;
1213 		}
1214 		if (uip_mask->proto) {
1215 			if (uip_mask->proto != IP_PROTO_FULL_MASK)
1216 				return -EINVAL;
1217 			spec.match_flags |= EFX_FILTER_MATCH_IP_PROTO;
1218 			spec.ip_proto = uip_entry->proto;
1219 		}
1220 		break;
1221 
1222 	case IPV6_USER_FLOW:
1223 		if (uip6_mask->l4_4_bytes || uip6_mask->tclass)
1224 			return -EINVAL;
1225 		spec.match_flags = EFX_FILTER_MATCH_ETHER_TYPE;
1226 		spec.ether_type = htons(ETH_P_IPV6);
1227 		if (!ip6_mask_is_empty(uip6_mask->ip6dst)) {
1228 			if (!ip6_mask_is_full(uip6_mask->ip6dst))
1229 				return -EINVAL;
1230 			spec.match_flags |= EFX_FILTER_MATCH_LOC_HOST;
1231 			memcpy(spec.loc_host, uip6_entry->ip6dst, sizeof(spec.loc_host));
1232 		}
1233 		if (!ip6_mask_is_empty(uip6_mask->ip6src)) {
1234 			if (!ip6_mask_is_full(uip6_mask->ip6src))
1235 				return -EINVAL;
1236 			spec.match_flags |= EFX_FILTER_MATCH_REM_HOST;
1237 			memcpy(spec.rem_host, uip6_entry->ip6src, sizeof(spec.rem_host));
1238 		}
1239 		if (uip6_mask->l4_proto) {
1240 			if (uip6_mask->l4_proto != IP_PROTO_FULL_MASK)
1241 				return -EINVAL;
1242 			spec.match_flags |= EFX_FILTER_MATCH_IP_PROTO;
1243 			spec.ip_proto = uip6_entry->l4_proto;
1244 		}
1245 		break;
1246 
1247 	case ETHER_FLOW:
1248 		if (!is_zero_ether_addr(mac_mask->h_dest)) {
1249 			if (ether_addr_equal(mac_mask->h_dest,
1250 					     mac_addr_ig_mask))
1251 				spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC_IG;
1252 			else if (is_broadcast_ether_addr(mac_mask->h_dest))
1253 				spec.match_flags |= EFX_FILTER_MATCH_LOC_MAC;
1254 			else
1255 				return -EINVAL;
1256 			ether_addr_copy(spec.loc_mac, mac_entry->h_dest);
1257 		}
1258 		if (!is_zero_ether_addr(mac_mask->h_source)) {
1259 			if (!is_broadcast_ether_addr(mac_mask->h_source))
1260 				return -EINVAL;
1261 			spec.match_flags |= EFX_FILTER_MATCH_REM_MAC;
1262 			ether_addr_copy(spec.rem_mac, mac_entry->h_source);
1263 		}
1264 		if (mac_mask->h_proto) {
1265 			if (mac_mask->h_proto != ETHER_TYPE_FULL_MASK)
1266 				return -EINVAL;
1267 			spec.match_flags |= EFX_FILTER_MATCH_ETHER_TYPE;
1268 			spec.ether_type = mac_entry->h_proto;
1269 		}
1270 		break;
1271 
1272 	default:
1273 		return -EINVAL;
1274 	}
1275 
1276 	if ((rule->flow_type & FLOW_EXT) && rule->m_ext.vlan_tci) {
1277 		if (rule->m_ext.vlan_tci != htons(0xfff))
1278 			return -EINVAL;
1279 		spec.match_flags |= EFX_FILTER_MATCH_OUTER_VID;
1280 		spec.outer_vid = rule->h_ext.vlan_tci;
1281 	}
1282 
1283 	rc = efx_filter_insert_filter(efx, &spec, true);
1284 	if (rc < 0)
1285 		return rc;
1286 
1287 	rule->location = rc;
1288 	return 0;
1289 }
1290 
1291 static int efx_ethtool_set_rxnfc(struct net_device *net_dev,
1292 				 struct ethtool_rxnfc *info)
1293 {
1294 	struct efx_nic *efx = netdev_priv(net_dev);
1295 
1296 	if (efx_filter_get_rx_id_limit(efx) == 0)
1297 		return -EOPNOTSUPP;
1298 
1299 	switch (info->cmd) {
1300 	case ETHTOOL_SRXCLSRLINS:
1301 		return efx_ethtool_set_class_rule(efx, &info->fs,
1302 						  info->rss_context);
1303 
1304 	case ETHTOOL_SRXCLSRLDEL:
1305 		return efx_filter_remove_id_safe(efx, EFX_FILTER_PRI_MANUAL,
1306 						 info->fs.location);
1307 
1308 	default:
1309 		return -EOPNOTSUPP;
1310 	}
1311 }
1312 
1313 static u32 efx_ethtool_get_rxfh_indir_size(struct net_device *net_dev)
1314 {
1315 	struct efx_nic *efx = netdev_priv(net_dev);
1316 
1317 	if (efx->n_rx_channels == 1)
1318 		return 0;
1319 	return ARRAY_SIZE(efx->rss_context.rx_indir_table);
1320 }
1321 
1322 static u32 efx_ethtool_get_rxfh_key_size(struct net_device *net_dev)
1323 {
1324 	struct efx_nic *efx = netdev_priv(net_dev);
1325 
1326 	return efx->type->rx_hash_key_size;
1327 }
1328 
1329 static int efx_ethtool_get_rxfh(struct net_device *net_dev, u32 *indir, u8 *key,
1330 				u8 *hfunc)
1331 {
1332 	struct efx_nic *efx = netdev_priv(net_dev);
1333 	int rc;
1334 
1335 	rc = efx->type->rx_pull_rss_config(efx);
1336 	if (rc)
1337 		return rc;
1338 
1339 	if (hfunc)
1340 		*hfunc = ETH_RSS_HASH_TOP;
1341 	if (indir)
1342 		memcpy(indir, efx->rss_context.rx_indir_table,
1343 		       sizeof(efx->rss_context.rx_indir_table));
1344 	if (key)
1345 		memcpy(key, efx->rss_context.rx_hash_key,
1346 		       efx->type->rx_hash_key_size);
1347 	return 0;
1348 }
1349 
1350 static int efx_ethtool_set_rxfh(struct net_device *net_dev, const u32 *indir,
1351 				const u8 *key, const u8 hfunc)
1352 {
1353 	struct efx_nic *efx = netdev_priv(net_dev);
1354 
1355 	/* Hash function is Toeplitz, cannot be changed */
1356 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1357 		return -EOPNOTSUPP;
1358 	if (!indir && !key)
1359 		return 0;
1360 
1361 	if (!key)
1362 		key = efx->rss_context.rx_hash_key;
1363 	if (!indir)
1364 		indir = efx->rss_context.rx_indir_table;
1365 
1366 	return efx->type->rx_push_rss_config(efx, true, indir, key);
1367 }
1368 
1369 static int efx_ethtool_get_rxfh_context(struct net_device *net_dev, u32 *indir,
1370 					u8 *key, u8 *hfunc, u32 rss_context)
1371 {
1372 	struct efx_nic *efx = netdev_priv(net_dev);
1373 	struct efx_rss_context *ctx;
1374 	int rc = 0;
1375 
1376 	if (!efx->type->rx_pull_rss_context_config)
1377 		return -EOPNOTSUPP;
1378 
1379 	mutex_lock(&efx->rss_lock);
1380 	ctx = efx_find_rss_context_entry(efx, rss_context);
1381 	if (!ctx) {
1382 		rc = -ENOENT;
1383 		goto out_unlock;
1384 	}
1385 	rc = efx->type->rx_pull_rss_context_config(efx, ctx);
1386 	if (rc)
1387 		goto out_unlock;
1388 
1389 	if (hfunc)
1390 		*hfunc = ETH_RSS_HASH_TOP;
1391 	if (indir)
1392 		memcpy(indir, ctx->rx_indir_table, sizeof(ctx->rx_indir_table));
1393 	if (key)
1394 		memcpy(key, ctx->rx_hash_key, efx->type->rx_hash_key_size);
1395 out_unlock:
1396 	mutex_unlock(&efx->rss_lock);
1397 	return rc;
1398 }
1399 
1400 static int efx_ethtool_set_rxfh_context(struct net_device *net_dev,
1401 					const u32 *indir, const u8 *key,
1402 					const u8 hfunc, u32 *rss_context,
1403 					bool delete)
1404 {
1405 	struct efx_nic *efx = netdev_priv(net_dev);
1406 	struct efx_rss_context *ctx;
1407 	bool allocated = false;
1408 	int rc;
1409 
1410 	if (!efx->type->rx_push_rss_context_config)
1411 		return -EOPNOTSUPP;
1412 	/* Hash function is Toeplitz, cannot be changed */
1413 	if (hfunc != ETH_RSS_HASH_NO_CHANGE && hfunc != ETH_RSS_HASH_TOP)
1414 		return -EOPNOTSUPP;
1415 
1416 	mutex_lock(&efx->rss_lock);
1417 
1418 	if (*rss_context == ETH_RXFH_CONTEXT_ALLOC) {
1419 		if (delete) {
1420 			/* alloc + delete == Nothing to do */
1421 			rc = -EINVAL;
1422 			goto out_unlock;
1423 		}
1424 		ctx = efx_alloc_rss_context_entry(efx);
1425 		if (!ctx) {
1426 			rc = -ENOMEM;
1427 			goto out_unlock;
1428 		}
1429 		ctx->context_id = EFX_EF10_RSS_CONTEXT_INVALID;
1430 		/* Initialise indir table and key to defaults */
1431 		efx_set_default_rx_indir_table(efx, ctx);
1432 		netdev_rss_key_fill(ctx->rx_hash_key, sizeof(ctx->rx_hash_key));
1433 		allocated = true;
1434 	} else {
1435 		ctx = efx_find_rss_context_entry(efx, *rss_context);
1436 		if (!ctx) {
1437 			rc = -ENOENT;
1438 			goto out_unlock;
1439 		}
1440 	}
1441 
1442 	if (delete) {
1443 		/* delete this context */
1444 		rc = efx->type->rx_push_rss_context_config(efx, ctx, NULL, NULL);
1445 		if (!rc)
1446 			efx_free_rss_context_entry(ctx);
1447 		goto out_unlock;
1448 	}
1449 
1450 	if (!key)
1451 		key = ctx->rx_hash_key;
1452 	if (!indir)
1453 		indir = ctx->rx_indir_table;
1454 
1455 	rc = efx->type->rx_push_rss_context_config(efx, ctx, indir, key);
1456 	if (rc && allocated)
1457 		efx_free_rss_context_entry(ctx);
1458 	else
1459 		*rss_context = ctx->user_id;
1460 out_unlock:
1461 	mutex_unlock(&efx->rss_lock);
1462 	return rc;
1463 }
1464 
1465 static int efx_ethtool_get_ts_info(struct net_device *net_dev,
1466 				   struct ethtool_ts_info *ts_info)
1467 {
1468 	struct efx_nic *efx = netdev_priv(net_dev);
1469 
1470 	/* Software capabilities */
1471 	ts_info->so_timestamping = (SOF_TIMESTAMPING_RX_SOFTWARE |
1472 				    SOF_TIMESTAMPING_SOFTWARE);
1473 	ts_info->phc_index = -1;
1474 
1475 	efx_ptp_get_ts_info(efx, ts_info);
1476 	return 0;
1477 }
1478 
1479 static int efx_ethtool_get_module_eeprom(struct net_device *net_dev,
1480 					 struct ethtool_eeprom *ee,
1481 					 u8 *data)
1482 {
1483 	struct efx_nic *efx = netdev_priv(net_dev);
1484 	int ret;
1485 
1486 	if (!efx->phy_op || !efx->phy_op->get_module_eeprom)
1487 		return -EOPNOTSUPP;
1488 
1489 	mutex_lock(&efx->mac_lock);
1490 	ret = efx->phy_op->get_module_eeprom(efx, ee, data);
1491 	mutex_unlock(&efx->mac_lock);
1492 
1493 	return ret;
1494 }
1495 
1496 static int efx_ethtool_get_module_info(struct net_device *net_dev,
1497 				       struct ethtool_modinfo *modinfo)
1498 {
1499 	struct efx_nic *efx = netdev_priv(net_dev);
1500 	int ret;
1501 
1502 	if (!efx->phy_op || !efx->phy_op->get_module_info)
1503 		return -EOPNOTSUPP;
1504 
1505 	mutex_lock(&efx->mac_lock);
1506 	ret = efx->phy_op->get_module_info(efx, modinfo);
1507 	mutex_unlock(&efx->mac_lock);
1508 
1509 	return ret;
1510 }
1511 
1512 static int efx_ethtool_get_fecparam(struct net_device *net_dev,
1513 				    struct ethtool_fecparam *fecparam)
1514 {
1515 	struct efx_nic *efx = netdev_priv(net_dev);
1516 	int rc;
1517 
1518 	if (!efx->phy_op || !efx->phy_op->get_fecparam)
1519 		return -EOPNOTSUPP;
1520 	mutex_lock(&efx->mac_lock);
1521 	rc = efx->phy_op->get_fecparam(efx, fecparam);
1522 	mutex_unlock(&efx->mac_lock);
1523 
1524 	return rc;
1525 }
1526 
1527 static int efx_ethtool_set_fecparam(struct net_device *net_dev,
1528 				    struct ethtool_fecparam *fecparam)
1529 {
1530 	struct efx_nic *efx = netdev_priv(net_dev);
1531 	int rc;
1532 
1533 	if (!efx->phy_op || !efx->phy_op->get_fecparam)
1534 		return -EOPNOTSUPP;
1535 	mutex_lock(&efx->mac_lock);
1536 	rc = efx->phy_op->set_fecparam(efx, fecparam);
1537 	mutex_unlock(&efx->mac_lock);
1538 
1539 	return rc;
1540 }
1541 
1542 const struct ethtool_ops efx_ethtool_ops = {
1543 	.get_drvinfo		= efx_ethtool_get_drvinfo,
1544 	.get_regs_len		= efx_ethtool_get_regs_len,
1545 	.get_regs		= efx_ethtool_get_regs,
1546 	.get_msglevel		= efx_ethtool_get_msglevel,
1547 	.set_msglevel		= efx_ethtool_set_msglevel,
1548 	.nway_reset		= efx_ethtool_nway_reset,
1549 	.get_link		= ethtool_op_get_link,
1550 	.get_coalesce		= efx_ethtool_get_coalesce,
1551 	.set_coalesce		= efx_ethtool_set_coalesce,
1552 	.get_ringparam		= efx_ethtool_get_ringparam,
1553 	.set_ringparam		= efx_ethtool_set_ringparam,
1554 	.get_pauseparam         = efx_ethtool_get_pauseparam,
1555 	.set_pauseparam         = efx_ethtool_set_pauseparam,
1556 	.get_sset_count		= efx_ethtool_get_sset_count,
1557 	.self_test		= efx_ethtool_self_test,
1558 	.get_strings		= efx_ethtool_get_strings,
1559 	.set_phys_id		= efx_ethtool_phys_id,
1560 	.get_ethtool_stats	= efx_ethtool_get_stats,
1561 	.get_wol                = efx_ethtool_get_wol,
1562 	.set_wol                = efx_ethtool_set_wol,
1563 	.reset			= efx_ethtool_reset,
1564 	.get_rxnfc		= efx_ethtool_get_rxnfc,
1565 	.set_rxnfc		= efx_ethtool_set_rxnfc,
1566 	.get_rxfh_indir_size	= efx_ethtool_get_rxfh_indir_size,
1567 	.get_rxfh_key_size	= efx_ethtool_get_rxfh_key_size,
1568 	.get_rxfh		= efx_ethtool_get_rxfh,
1569 	.set_rxfh		= efx_ethtool_set_rxfh,
1570 	.get_rxfh_context	= efx_ethtool_get_rxfh_context,
1571 	.set_rxfh_context	= efx_ethtool_set_rxfh_context,
1572 	.get_ts_info		= efx_ethtool_get_ts_info,
1573 	.get_module_info	= efx_ethtool_get_module_info,
1574 	.get_module_eeprom	= efx_ethtool_get_module_eeprom,
1575 	.get_link_ksettings	= efx_ethtool_get_link_ksettings,
1576 	.set_link_ksettings	= efx_ethtool_set_link_ksettings,
1577 	.get_fecparam		= efx_ethtool_get_fecparam,
1578 	.set_fecparam		= efx_ethtool_set_fecparam,
1579 };
1580