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