xref: /linux/drivers/net/ethernet/sfc/ethtool.c (revision f2ee442115c9b6219083c019939a9cc0c9abb2f8)
1 /****************************************************************************
2  * Driver for Solarflare Solarstorm network controllers and boards
3  * Copyright 2005-2006 Fen Systems Ltd.
4  * Copyright 2006-2010 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 ethtool_string {
23 	char name[ETH_GSTRING_LEN];
24 };
25 
26 struct efx_ethtool_stat {
27 	const char *name;
28 	enum {
29 		EFX_ETHTOOL_STAT_SOURCE_mac_stats,
30 		EFX_ETHTOOL_STAT_SOURCE_nic,
31 		EFX_ETHTOOL_STAT_SOURCE_channel,
32 		EFX_ETHTOOL_STAT_SOURCE_tx_queue
33 	} source;
34 	unsigned offset;
35 	u64(*get_stat) (void *field); /* Reader function */
36 };
37 
38 /* Initialiser for a struct #efx_ethtool_stat with type-checking */
39 #define EFX_ETHTOOL_STAT(stat_name, source_name, field, field_type, \
40 				get_stat_function) {			\
41 	.name = #stat_name,						\
42 	.source = EFX_ETHTOOL_STAT_SOURCE_##source_name,		\
43 	.offset = ((((field_type *) 0) ==				\
44 		      &((struct efx_##source_name *)0)->field) ?	\
45 		    offsetof(struct efx_##source_name, field) :		\
46 		    offsetof(struct efx_##source_name, field)),		\
47 	.get_stat = get_stat_function,					\
48 }
49 
50 static u64 efx_get_uint_stat(void *field)
51 {
52 	return *(unsigned int *)field;
53 }
54 
55 static u64 efx_get_ulong_stat(void *field)
56 {
57 	return *(unsigned long *)field;
58 }
59 
60 static u64 efx_get_u64_stat(void *field)
61 {
62 	return *(u64 *) field;
63 }
64 
65 static u64 efx_get_atomic_stat(void *field)
66 {
67 	return atomic_read((atomic_t *) field);
68 }
69 
70 #define EFX_ETHTOOL_ULONG_MAC_STAT(field)			\
71 	EFX_ETHTOOL_STAT(field, mac_stats, field, 		\
72 			  unsigned long, efx_get_ulong_stat)
73 
74 #define EFX_ETHTOOL_U64_MAC_STAT(field)				\
75 	EFX_ETHTOOL_STAT(field, mac_stats, field, 		\
76 			  u64, efx_get_u64_stat)
77 
78 #define EFX_ETHTOOL_UINT_NIC_STAT(name)				\
79 	EFX_ETHTOOL_STAT(name, nic, n_##name,			\
80 			 unsigned int, efx_get_uint_stat)
81 
82 #define EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(field)		\
83 	EFX_ETHTOOL_STAT(field, nic, field,			\
84 			 atomic_t, efx_get_atomic_stat)
85 
86 #define EFX_ETHTOOL_UINT_CHANNEL_STAT(field)			\
87 	EFX_ETHTOOL_STAT(field, channel, n_##field,		\
88 			 unsigned int, efx_get_uint_stat)
89 
90 #define EFX_ETHTOOL_UINT_TXQ_STAT(field)			\
91 	EFX_ETHTOOL_STAT(tx_##field, tx_queue, field,		\
92 			 unsigned int, efx_get_uint_stat)
93 
94 static struct efx_ethtool_stat efx_ethtool_stats[] = {
95 	EFX_ETHTOOL_U64_MAC_STAT(tx_bytes),
96 	EFX_ETHTOOL_U64_MAC_STAT(tx_good_bytes),
97 	EFX_ETHTOOL_U64_MAC_STAT(tx_bad_bytes),
98 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_packets),
99 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_bad),
100 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_pause),
101 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_control),
102 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_unicast),
103 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_multicast),
104 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_broadcast),
105 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_lt64),
106 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_64),
107 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_65_to_127),
108 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_128_to_255),
109 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_256_to_511),
110 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_512_to_1023),
111 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_1024_to_15xx),
112 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_15xx_to_jumbo),
113 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_gtjumbo),
114 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_collision),
115 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_single_collision),
116 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_multiple_collision),
117 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_excessive_collision),
118 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_deferred),
119 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_late_collision),
120 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_excessive_deferred),
121 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_non_tcpudp),
122 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_mac_src_error),
123 	EFX_ETHTOOL_ULONG_MAC_STAT(tx_ip_src_error),
124 	EFX_ETHTOOL_UINT_TXQ_STAT(tso_bursts),
125 	EFX_ETHTOOL_UINT_TXQ_STAT(tso_long_headers),
126 	EFX_ETHTOOL_UINT_TXQ_STAT(tso_packets),
127 	EFX_ETHTOOL_UINT_TXQ_STAT(pushes),
128 	EFX_ETHTOOL_U64_MAC_STAT(rx_bytes),
129 	EFX_ETHTOOL_U64_MAC_STAT(rx_good_bytes),
130 	EFX_ETHTOOL_U64_MAC_STAT(rx_bad_bytes),
131 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_packets),
132 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_good),
133 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad),
134 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_pause),
135 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_control),
136 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_unicast),
137 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_multicast),
138 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_broadcast),
139 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_lt64),
140 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_64),
141 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_65_to_127),
142 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_128_to_255),
143 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_256_to_511),
144 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_512_to_1023),
145 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_1024_to_15xx),
146 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_15xx_to_jumbo),
147 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_gtjumbo),
148 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_lt64),
149 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_64_to_15xx),
150 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_15xx_to_jumbo),
151 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_bad_gtjumbo),
152 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_overflow),
153 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_missed),
154 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_false_carrier),
155 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_symbol_error),
156 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_align_error),
157 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_length_error),
158 	EFX_ETHTOOL_ULONG_MAC_STAT(rx_internal_error),
159 	EFX_ETHTOOL_UINT_NIC_STAT(rx_nodesc_drop_cnt),
160 	EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(rx_reset),
161 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tobe_disc),
162 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_ip_hdr_chksum_err),
163 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tcp_udp_chksum_err),
164 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_mcast_mismatch),
165 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_frm_trunc),
166 };
167 
168 /* Number of ethtool statistics */
169 #define EFX_ETHTOOL_NUM_STATS ARRAY_SIZE(efx_ethtool_stats)
170 
171 #define EFX_ETHTOOL_EEPROM_MAGIC 0xEFAB
172 
173 /**************************************************************************
174  *
175  * Ethtool operations
176  *
177  **************************************************************************
178  */
179 
180 /* Identify device by flashing LEDs */
181 static int efx_ethtool_phys_id(struct net_device *net_dev,
182 			       enum ethtool_phys_id_state state)
183 {
184 	struct efx_nic *efx = netdev_priv(net_dev);
185 	enum efx_led_mode mode = EFX_LED_DEFAULT;
186 
187 	switch (state) {
188 	case ETHTOOL_ID_ON:
189 		mode = EFX_LED_ON;
190 		break;
191 	case ETHTOOL_ID_OFF:
192 		mode = EFX_LED_OFF;
193 		break;
194 	case ETHTOOL_ID_INACTIVE:
195 		mode = EFX_LED_DEFAULT;
196 		break;
197 	case ETHTOOL_ID_ACTIVE:
198 		return 1;	/* cycle on/off once per second */
199 	}
200 
201 	efx->type->set_id_led(efx, mode);
202 	return 0;
203 }
204 
205 /* This must be called with rtnl_lock held. */
206 static int efx_ethtool_get_settings(struct net_device *net_dev,
207 				    struct ethtool_cmd *ecmd)
208 {
209 	struct efx_nic *efx = netdev_priv(net_dev);
210 	struct efx_link_state *link_state = &efx->link_state;
211 
212 	mutex_lock(&efx->mac_lock);
213 	efx->phy_op->get_settings(efx, ecmd);
214 	mutex_unlock(&efx->mac_lock);
215 
216 	/* GMAC does not support 1000Mbps HD */
217 	ecmd->supported &= ~SUPPORTED_1000baseT_Half;
218 	/* Both MACs support pause frames (bidirectional and respond-only) */
219 	ecmd->supported |= SUPPORTED_Pause | SUPPORTED_Asym_Pause;
220 
221 	if (LOOPBACK_INTERNAL(efx)) {
222 		ethtool_cmd_speed_set(ecmd, link_state->speed);
223 		ecmd->duplex = link_state->fd ? DUPLEX_FULL : DUPLEX_HALF;
224 	}
225 
226 	return 0;
227 }
228 
229 /* This must be called with rtnl_lock held. */
230 static int efx_ethtool_set_settings(struct net_device *net_dev,
231 				    struct ethtool_cmd *ecmd)
232 {
233 	struct efx_nic *efx = netdev_priv(net_dev);
234 	int rc;
235 
236 	/* GMAC does not support 1000Mbps HD */
237 	if ((ethtool_cmd_speed(ecmd) == SPEED_1000) &&
238 	    (ecmd->duplex != DUPLEX_FULL)) {
239 		netif_dbg(efx, drv, efx->net_dev,
240 			  "rejecting unsupported 1000Mbps HD setting\n");
241 		return -EINVAL;
242 	}
243 
244 	mutex_lock(&efx->mac_lock);
245 	rc = efx->phy_op->set_settings(efx, ecmd);
246 	mutex_unlock(&efx->mac_lock);
247 	return rc;
248 }
249 
250 static void efx_ethtool_get_drvinfo(struct net_device *net_dev,
251 				    struct ethtool_drvinfo *info)
252 {
253 	struct efx_nic *efx = netdev_priv(net_dev);
254 
255 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
256 	strlcpy(info->version, EFX_DRIVER_VERSION, sizeof(info->version));
257 	if (efx_nic_rev(efx) >= EFX_REV_SIENA_A0)
258 		efx_mcdi_print_fwver(efx, info->fw_version,
259 				     sizeof(info->fw_version));
260 	strlcpy(info->bus_info, pci_name(efx->pci_dev), sizeof(info->bus_info));
261 }
262 
263 static int efx_ethtool_get_regs_len(struct net_device *net_dev)
264 {
265 	return efx_nic_get_regs_len(netdev_priv(net_dev));
266 }
267 
268 static void efx_ethtool_get_regs(struct net_device *net_dev,
269 				 struct ethtool_regs *regs, void *buf)
270 {
271 	struct efx_nic *efx = netdev_priv(net_dev);
272 
273 	regs->version = efx->type->revision;
274 	efx_nic_get_regs(efx, buf);
275 }
276 
277 static u32 efx_ethtool_get_msglevel(struct net_device *net_dev)
278 {
279 	struct efx_nic *efx = netdev_priv(net_dev);
280 	return efx->msg_enable;
281 }
282 
283 static void efx_ethtool_set_msglevel(struct net_device *net_dev, u32 msg_enable)
284 {
285 	struct efx_nic *efx = netdev_priv(net_dev);
286 	efx->msg_enable = msg_enable;
287 }
288 
289 /**
290  * efx_fill_test - fill in an individual self-test entry
291  * @test_index:		Index of the test
292  * @strings:		Ethtool strings, or %NULL
293  * @data:		Ethtool test results, or %NULL
294  * @test:		Pointer to test result (used only if data != %NULL)
295  * @unit_format:	Unit name format (e.g. "chan\%d")
296  * @unit_id:		Unit id (e.g. 0 for "chan0")
297  * @test_format:	Test name format (e.g. "loopback.\%s.tx.sent")
298  * @test_id:		Test id (e.g. "PHYXS" for "loopback.PHYXS.tx_sent")
299  *
300  * Fill in an individual self-test entry.
301  */
302 static void efx_fill_test(unsigned int test_index,
303 			  struct ethtool_string *strings, u64 *data,
304 			  int *test, const char *unit_format, int unit_id,
305 			  const char *test_format, const char *test_id)
306 {
307 	struct ethtool_string unit_str, test_str;
308 
309 	/* Fill data value, if applicable */
310 	if (data)
311 		data[test_index] = *test;
312 
313 	/* Fill string, if applicable */
314 	if (strings) {
315 		if (strchr(unit_format, '%'))
316 			snprintf(unit_str.name, sizeof(unit_str.name),
317 				 unit_format, unit_id);
318 		else
319 			strcpy(unit_str.name, unit_format);
320 		snprintf(test_str.name, sizeof(test_str.name),
321 			 test_format, test_id);
322 		snprintf(strings[test_index].name,
323 			 sizeof(strings[test_index].name),
324 			 "%-6s %-24s", unit_str.name, test_str.name);
325 	}
326 }
327 
328 #define EFX_CHANNEL_NAME(_channel) "chan%d", _channel->channel
329 #define EFX_TX_QUEUE_NAME(_tx_queue) "txq%d", _tx_queue->queue
330 #define EFX_RX_QUEUE_NAME(_rx_queue) "rxq%d", _rx_queue->queue
331 #define EFX_LOOPBACK_NAME(_mode, _counter)			\
332 	"loopback.%s." _counter, STRING_TABLE_LOOKUP(_mode, efx_loopback_mode)
333 
334 /**
335  * efx_fill_loopback_test - fill in a block of loopback self-test entries
336  * @efx:		Efx NIC
337  * @lb_tests:		Efx loopback self-test results structure
338  * @mode:		Loopback test mode
339  * @test_index:		Starting index of the test
340  * @strings:		Ethtool strings, or %NULL
341  * @data:		Ethtool test results, or %NULL
342  */
343 static int efx_fill_loopback_test(struct efx_nic *efx,
344 				  struct efx_loopback_self_tests *lb_tests,
345 				  enum efx_loopback_mode mode,
346 				  unsigned int test_index,
347 				  struct ethtool_string *strings, u64 *data)
348 {
349 	struct efx_channel *channel = efx_get_channel(efx, 0);
350 	struct efx_tx_queue *tx_queue;
351 
352 	efx_for_each_channel_tx_queue(tx_queue, channel) {
353 		efx_fill_test(test_index++, strings, data,
354 			      &lb_tests->tx_sent[tx_queue->queue],
355 			      EFX_TX_QUEUE_NAME(tx_queue),
356 			      EFX_LOOPBACK_NAME(mode, "tx_sent"));
357 		efx_fill_test(test_index++, strings, data,
358 			      &lb_tests->tx_done[tx_queue->queue],
359 			      EFX_TX_QUEUE_NAME(tx_queue),
360 			      EFX_LOOPBACK_NAME(mode, "tx_done"));
361 	}
362 	efx_fill_test(test_index++, strings, data,
363 		      &lb_tests->rx_good,
364 		      "rx", 0,
365 		      EFX_LOOPBACK_NAME(mode, "rx_good"));
366 	efx_fill_test(test_index++, strings, data,
367 		      &lb_tests->rx_bad,
368 		      "rx", 0,
369 		      EFX_LOOPBACK_NAME(mode, "rx_bad"));
370 
371 	return test_index;
372 }
373 
374 /**
375  * efx_ethtool_fill_self_tests - get self-test details
376  * @efx:		Efx NIC
377  * @tests:		Efx self-test results structure, or %NULL
378  * @strings:		Ethtool strings, or %NULL
379  * @data:		Ethtool test results, or %NULL
380  */
381 static int efx_ethtool_fill_self_tests(struct efx_nic *efx,
382 				       struct efx_self_tests *tests,
383 				       struct ethtool_string *strings,
384 				       u64 *data)
385 {
386 	struct efx_channel *channel;
387 	unsigned int n = 0, i;
388 	enum efx_loopback_mode mode;
389 
390 	efx_fill_test(n++, strings, data, &tests->phy_alive,
391 		      "phy", 0, "alive", NULL);
392 	efx_fill_test(n++, strings, data, &tests->nvram,
393 		      "core", 0, "nvram", NULL);
394 	efx_fill_test(n++, strings, data, &tests->interrupt,
395 		      "core", 0, "interrupt", NULL);
396 
397 	/* Event queues */
398 	efx_for_each_channel(channel, efx) {
399 		efx_fill_test(n++, strings, data,
400 			      &tests->eventq_dma[channel->channel],
401 			      EFX_CHANNEL_NAME(channel),
402 			      "eventq.dma", NULL);
403 		efx_fill_test(n++, strings, data,
404 			      &tests->eventq_int[channel->channel],
405 			      EFX_CHANNEL_NAME(channel),
406 			      "eventq.int", NULL);
407 		efx_fill_test(n++, strings, data,
408 			      &tests->eventq_poll[channel->channel],
409 			      EFX_CHANNEL_NAME(channel),
410 			      "eventq.poll", NULL);
411 	}
412 
413 	efx_fill_test(n++, strings, data, &tests->registers,
414 		      "core", 0, "registers", NULL);
415 
416 	if (efx->phy_op->run_tests != NULL) {
417 		EFX_BUG_ON_PARANOID(efx->phy_op->test_name == NULL);
418 
419 		for (i = 0; true; ++i) {
420 			const char *name;
421 
422 			EFX_BUG_ON_PARANOID(i >= EFX_MAX_PHY_TESTS);
423 			name = efx->phy_op->test_name(efx, i);
424 			if (name == NULL)
425 				break;
426 
427 			efx_fill_test(n++, strings, data, &tests->phy_ext[i],
428 				      "phy", 0, name, NULL);
429 		}
430 	}
431 
432 	/* Loopback tests */
433 	for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
434 		if (!(efx->loopback_modes & (1 << mode)))
435 			continue;
436 		n = efx_fill_loopback_test(efx,
437 					   &tests->loopback[mode], mode, n,
438 					   strings, data);
439 	}
440 
441 	return n;
442 }
443 
444 static int efx_ethtool_get_sset_count(struct net_device *net_dev,
445 				      int string_set)
446 {
447 	switch (string_set) {
448 	case ETH_SS_STATS:
449 		return EFX_ETHTOOL_NUM_STATS;
450 	case ETH_SS_TEST:
451 		return efx_ethtool_fill_self_tests(netdev_priv(net_dev),
452 						   NULL, NULL, NULL);
453 	default:
454 		return -EINVAL;
455 	}
456 }
457 
458 static void efx_ethtool_get_strings(struct net_device *net_dev,
459 				    u32 string_set, u8 *strings)
460 {
461 	struct efx_nic *efx = netdev_priv(net_dev);
462 	struct ethtool_string *ethtool_strings =
463 		(struct ethtool_string *)strings;
464 	int i;
465 
466 	switch (string_set) {
467 	case ETH_SS_STATS:
468 		for (i = 0; i < EFX_ETHTOOL_NUM_STATS; i++)
469 			strncpy(ethtool_strings[i].name,
470 				efx_ethtool_stats[i].name,
471 				sizeof(ethtool_strings[i].name));
472 		break;
473 	case ETH_SS_TEST:
474 		efx_ethtool_fill_self_tests(efx, NULL,
475 					    ethtool_strings, NULL);
476 		break;
477 	default:
478 		/* No other string sets */
479 		break;
480 	}
481 }
482 
483 static void efx_ethtool_get_stats(struct net_device *net_dev,
484 				  struct ethtool_stats *stats,
485 				  u64 *data)
486 {
487 	struct efx_nic *efx = netdev_priv(net_dev);
488 	struct efx_mac_stats *mac_stats = &efx->mac_stats;
489 	struct efx_ethtool_stat *stat;
490 	struct efx_channel *channel;
491 	struct efx_tx_queue *tx_queue;
492 	struct rtnl_link_stats64 temp;
493 	int i;
494 
495 	EFX_BUG_ON_PARANOID(stats->n_stats != EFX_ETHTOOL_NUM_STATS);
496 
497 	/* Update MAC and NIC statistics */
498 	dev_get_stats(net_dev, &temp);
499 
500 	/* Fill detailed statistics buffer */
501 	for (i = 0; i < EFX_ETHTOOL_NUM_STATS; i++) {
502 		stat = &efx_ethtool_stats[i];
503 		switch (stat->source) {
504 		case EFX_ETHTOOL_STAT_SOURCE_mac_stats:
505 			data[i] = stat->get_stat((void *)mac_stats +
506 						 stat->offset);
507 			break;
508 		case EFX_ETHTOOL_STAT_SOURCE_nic:
509 			data[i] = stat->get_stat((void *)efx + stat->offset);
510 			break;
511 		case EFX_ETHTOOL_STAT_SOURCE_channel:
512 			data[i] = 0;
513 			efx_for_each_channel(channel, efx)
514 				data[i] += stat->get_stat((void *)channel +
515 							  stat->offset);
516 			break;
517 		case EFX_ETHTOOL_STAT_SOURCE_tx_queue:
518 			data[i] = 0;
519 			efx_for_each_channel(channel, efx) {
520 				efx_for_each_channel_tx_queue(tx_queue, channel)
521 					data[i] +=
522 						stat->get_stat((void *)tx_queue
523 							       + stat->offset);
524 			}
525 			break;
526 		}
527 	}
528 }
529 
530 static void efx_ethtool_self_test(struct net_device *net_dev,
531 				  struct ethtool_test *test, u64 *data)
532 {
533 	struct efx_nic *efx = netdev_priv(net_dev);
534 	struct efx_self_tests *efx_tests;
535 	int already_up;
536 	int rc = -ENOMEM;
537 
538 	efx_tests = kzalloc(sizeof(*efx_tests), GFP_KERNEL);
539 	if (!efx_tests)
540 		goto fail;
541 
542 
543 	ASSERT_RTNL();
544 	if (efx->state != STATE_RUNNING) {
545 		rc = -EIO;
546 		goto fail1;
547 	}
548 
549 	netif_info(efx, drv, efx->net_dev, "starting %sline testing\n",
550 		   (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
551 
552 	/* We need rx buffers and interrupts. */
553 	already_up = (efx->net_dev->flags & IFF_UP);
554 	if (!already_up) {
555 		rc = dev_open(efx->net_dev);
556 		if (rc) {
557 			netif_err(efx, drv, efx->net_dev,
558 				  "failed opening device.\n");
559 			goto fail1;
560 		}
561 	}
562 
563 	rc = efx_selftest(efx, efx_tests, test->flags);
564 
565 	if (!already_up)
566 		dev_close(efx->net_dev);
567 
568 	netif_info(efx, drv, efx->net_dev, "%s %sline self-tests\n",
569 		   rc == 0 ? "passed" : "failed",
570 		   (test->flags & ETH_TEST_FL_OFFLINE) ? "off" : "on");
571 
572 fail1:
573 	/* Fill ethtool results structures */
574 	efx_ethtool_fill_self_tests(efx, efx_tests, NULL, data);
575 	kfree(efx_tests);
576 fail:
577 	if (rc)
578 		test->flags |= ETH_TEST_FL_FAILED;
579 }
580 
581 /* Restart autonegotiation */
582 static int efx_ethtool_nway_reset(struct net_device *net_dev)
583 {
584 	struct efx_nic *efx = netdev_priv(net_dev);
585 
586 	return mdio45_nway_restart(&efx->mdio);
587 }
588 
589 /*
590  * Each channel has a single IRQ and moderation timer, started by any
591  * completion (or other event).  Unless the module parameter
592  * separate_tx_channels is set, IRQs and moderation are therefore
593  * shared between RX and TX completions.  In this case, when RX IRQ
594  * moderation is explicitly changed then TX IRQ moderation is
595  * automatically changed too, but otherwise we fail if the two values
596  * are requested to be different.
597  *
598  * The hardware does not support a limit on the number of completions
599  * before an IRQ, so we do not use the max_frames fields.  We should
600  * report and require that max_frames == (usecs != 0), but this would
601  * invalidate existing user documentation.
602  *
603  * The hardware does not have distinct settings for interrupt
604  * moderation while the previous IRQ is being handled, so we should
605  * not use the 'irq' fields.  However, an earlier developer
606  * misunderstood the meaning of the 'irq' fields and the driver did
607  * not support the standard fields.  To avoid invalidating existing
608  * user documentation, we report and accept changes through either the
609  * standard or 'irq' fields.  If both are changed at the same time, we
610  * prefer the standard field.
611  *
612  * We implement adaptive IRQ moderation, but use a different algorithm
613  * from that assumed in the definition of struct ethtool_coalesce.
614  * Therefore we do not use any of the adaptive moderation parameters
615  * in it.
616  */
617 
618 static int efx_ethtool_get_coalesce(struct net_device *net_dev,
619 				    struct ethtool_coalesce *coalesce)
620 {
621 	struct efx_nic *efx = netdev_priv(net_dev);
622 	unsigned int tx_usecs, rx_usecs;
623 	bool rx_adaptive;
624 
625 	efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &rx_adaptive);
626 
627 	coalesce->tx_coalesce_usecs = tx_usecs;
628 	coalesce->tx_coalesce_usecs_irq = tx_usecs;
629 	coalesce->rx_coalesce_usecs = rx_usecs;
630 	coalesce->rx_coalesce_usecs_irq = rx_usecs;
631 	coalesce->use_adaptive_rx_coalesce = rx_adaptive;
632 
633 	return 0;
634 }
635 
636 static int efx_ethtool_set_coalesce(struct net_device *net_dev,
637 				    struct ethtool_coalesce *coalesce)
638 {
639 	struct efx_nic *efx = netdev_priv(net_dev);
640 	struct efx_channel *channel;
641 	unsigned int tx_usecs, rx_usecs;
642 	bool adaptive, rx_may_override_tx;
643 	int rc;
644 
645 	if (coalesce->use_adaptive_tx_coalesce)
646 		return -EINVAL;
647 
648 	efx_get_irq_moderation(efx, &tx_usecs, &rx_usecs, &adaptive);
649 
650 	if (coalesce->rx_coalesce_usecs != rx_usecs)
651 		rx_usecs = coalesce->rx_coalesce_usecs;
652 	else
653 		rx_usecs = coalesce->rx_coalesce_usecs_irq;
654 
655 	adaptive = coalesce->use_adaptive_rx_coalesce;
656 
657 	/* If channels are shared, TX IRQ moderation can be quietly
658 	 * overridden unless it is changed from its old value.
659 	 */
660 	rx_may_override_tx = (coalesce->tx_coalesce_usecs == tx_usecs &&
661 			      coalesce->tx_coalesce_usecs_irq == tx_usecs);
662 	if (coalesce->tx_coalesce_usecs != tx_usecs)
663 		tx_usecs = coalesce->tx_coalesce_usecs;
664 	else
665 		tx_usecs = coalesce->tx_coalesce_usecs_irq;
666 
667 	rc = efx_init_irq_moderation(efx, tx_usecs, rx_usecs, adaptive,
668 				     rx_may_override_tx);
669 	if (rc != 0)
670 		return rc;
671 
672 	efx_for_each_channel(channel, efx)
673 		efx->type->push_irq_moderation(channel);
674 
675 	return 0;
676 }
677 
678 static void efx_ethtool_get_ringparam(struct net_device *net_dev,
679 				      struct ethtool_ringparam *ring)
680 {
681 	struct efx_nic *efx = netdev_priv(net_dev);
682 
683 	ring->rx_max_pending = EFX_MAX_DMAQ_SIZE;
684 	ring->tx_max_pending = EFX_MAX_DMAQ_SIZE;
685 	ring->rx_pending = efx->rxq_entries;
686 	ring->tx_pending = efx->txq_entries;
687 }
688 
689 static int efx_ethtool_set_ringparam(struct net_device *net_dev,
690 				     struct ethtool_ringparam *ring)
691 {
692 	struct efx_nic *efx = netdev_priv(net_dev);
693 
694 	if (ring->rx_mini_pending || ring->rx_jumbo_pending ||
695 	    ring->rx_pending > EFX_MAX_DMAQ_SIZE ||
696 	    ring->tx_pending > EFX_MAX_DMAQ_SIZE)
697 		return -EINVAL;
698 
699 	if (ring->rx_pending < EFX_MIN_RING_SIZE ||
700 	    ring->tx_pending < EFX_MIN_RING_SIZE) {
701 		netif_err(efx, drv, efx->net_dev,
702 			  "TX and RX queues cannot be smaller than %ld\n",
703 			  EFX_MIN_RING_SIZE);
704 		return -EINVAL;
705 	}
706 
707 	return efx_realloc_channels(efx, ring->rx_pending, ring->tx_pending);
708 }
709 
710 static int efx_ethtool_set_pauseparam(struct net_device *net_dev,
711 				      struct ethtool_pauseparam *pause)
712 {
713 	struct efx_nic *efx = netdev_priv(net_dev);
714 	u8 wanted_fc, old_fc;
715 	u32 old_adv;
716 	bool reset;
717 	int rc = 0;
718 
719 	mutex_lock(&efx->mac_lock);
720 
721 	wanted_fc = ((pause->rx_pause ? EFX_FC_RX : 0) |
722 		     (pause->tx_pause ? EFX_FC_TX : 0) |
723 		     (pause->autoneg ? EFX_FC_AUTO : 0));
724 
725 	if ((wanted_fc & EFX_FC_TX) && !(wanted_fc & EFX_FC_RX)) {
726 		netif_dbg(efx, drv, efx->net_dev,
727 			  "Flow control unsupported: tx ON rx OFF\n");
728 		rc = -EINVAL;
729 		goto out;
730 	}
731 
732 	if ((wanted_fc & EFX_FC_AUTO) && !efx->link_advertising) {
733 		netif_dbg(efx, drv, efx->net_dev,
734 			  "Autonegotiation is disabled\n");
735 		rc = -EINVAL;
736 		goto out;
737 	}
738 
739 	/* TX flow control may automatically turn itself off if the
740 	 * link partner (intermittently) stops responding to pause
741 	 * frames. There isn't any indication that this has happened,
742 	 * so the best we do is leave it up to the user to spot this
743 	 * and fix it be cycling transmit flow control on this end. */
744 	reset = (wanted_fc & EFX_FC_TX) && !(efx->wanted_fc & EFX_FC_TX);
745 	if (EFX_WORKAROUND_11482(efx) && reset) {
746 		if (efx_nic_rev(efx) == EFX_REV_FALCON_B0) {
747 			/* Recover by resetting the EM block */
748 			falcon_stop_nic_stats(efx);
749 			falcon_drain_tx_fifo(efx);
750 			efx->mac_op->reconfigure(efx);
751 			falcon_start_nic_stats(efx);
752 		} else {
753 			/* Schedule a reset to recover */
754 			efx_schedule_reset(efx, RESET_TYPE_INVISIBLE);
755 		}
756 	}
757 
758 	old_adv = efx->link_advertising;
759 	old_fc = efx->wanted_fc;
760 	efx_link_set_wanted_fc(efx, wanted_fc);
761 	if (efx->link_advertising != old_adv ||
762 	    (efx->wanted_fc ^ old_fc) & EFX_FC_AUTO) {
763 		rc = efx->phy_op->reconfigure(efx);
764 		if (rc) {
765 			netif_err(efx, drv, efx->net_dev,
766 				  "Unable to advertise requested flow "
767 				  "control setting\n");
768 			goto out;
769 		}
770 	}
771 
772 	/* Reconfigure the MAC. The PHY *may* generate a link state change event
773 	 * if the user just changed the advertised capabilities, but there's no
774 	 * harm doing this twice */
775 	efx->mac_op->reconfigure(efx);
776 
777 out:
778 	mutex_unlock(&efx->mac_lock);
779 
780 	return rc;
781 }
782 
783 static void efx_ethtool_get_pauseparam(struct net_device *net_dev,
784 				       struct ethtool_pauseparam *pause)
785 {
786 	struct efx_nic *efx = netdev_priv(net_dev);
787 
788 	pause->rx_pause = !!(efx->wanted_fc & EFX_FC_RX);
789 	pause->tx_pause = !!(efx->wanted_fc & EFX_FC_TX);
790 	pause->autoneg = !!(efx->wanted_fc & EFX_FC_AUTO);
791 }
792 
793 
794 static void efx_ethtool_get_wol(struct net_device *net_dev,
795 				struct ethtool_wolinfo *wol)
796 {
797 	struct efx_nic *efx = netdev_priv(net_dev);
798 	return efx->type->get_wol(efx, wol);
799 }
800 
801 
802 static int efx_ethtool_set_wol(struct net_device *net_dev,
803 			       struct ethtool_wolinfo *wol)
804 {
805 	struct efx_nic *efx = netdev_priv(net_dev);
806 	return efx->type->set_wol(efx, wol->wolopts);
807 }
808 
809 static int efx_ethtool_reset(struct net_device *net_dev, u32 *flags)
810 {
811 	struct efx_nic *efx = netdev_priv(net_dev);
812 	int rc;
813 
814 	rc = efx->type->map_reset_flags(flags);
815 	if (rc < 0)
816 		return rc;
817 
818 	return efx_reset(efx, rc);
819 }
820 
821 static int
822 efx_ethtool_get_rxnfc(struct net_device *net_dev,
823 		      struct ethtool_rxnfc *info, u32 *rules __always_unused)
824 {
825 	struct efx_nic *efx = netdev_priv(net_dev);
826 
827 	switch (info->cmd) {
828 	case ETHTOOL_GRXRINGS:
829 		info->data = efx->n_rx_channels;
830 		return 0;
831 
832 	case ETHTOOL_GRXFH: {
833 		unsigned min_revision = 0;
834 
835 		info->data = 0;
836 		switch (info->flow_type) {
837 		case TCP_V4_FLOW:
838 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
839 			/* fall through */
840 		case UDP_V4_FLOW:
841 		case SCTP_V4_FLOW:
842 		case AH_ESP_V4_FLOW:
843 		case IPV4_FLOW:
844 			info->data |= RXH_IP_SRC | RXH_IP_DST;
845 			min_revision = EFX_REV_FALCON_B0;
846 			break;
847 		case TCP_V6_FLOW:
848 			info->data |= RXH_L4_B_0_1 | RXH_L4_B_2_3;
849 			/* fall through */
850 		case UDP_V6_FLOW:
851 		case SCTP_V6_FLOW:
852 		case AH_ESP_V6_FLOW:
853 		case IPV6_FLOW:
854 			info->data |= RXH_IP_SRC | RXH_IP_DST;
855 			min_revision = EFX_REV_SIENA_A0;
856 			break;
857 		default:
858 			break;
859 		}
860 		if (efx_nic_rev(efx) < min_revision)
861 			info->data = 0;
862 		return 0;
863 	}
864 
865 	default:
866 		return -EOPNOTSUPP;
867 	}
868 }
869 
870 static int efx_ethtool_set_rx_ntuple(struct net_device *net_dev,
871 				     struct ethtool_rx_ntuple *ntuple)
872 {
873 	struct efx_nic *efx = netdev_priv(net_dev);
874 	struct ethtool_tcpip4_spec *ip_entry = &ntuple->fs.h_u.tcp_ip4_spec;
875 	struct ethtool_tcpip4_spec *ip_mask = &ntuple->fs.m_u.tcp_ip4_spec;
876 	struct ethhdr *mac_entry = &ntuple->fs.h_u.ether_spec;
877 	struct ethhdr *mac_mask = &ntuple->fs.m_u.ether_spec;
878 	struct efx_filter_spec filter;
879 	int rc;
880 
881 	/* Range-check action */
882 	if (ntuple->fs.action < ETHTOOL_RXNTUPLE_ACTION_CLEAR ||
883 	    ntuple->fs.action >= (s32)efx->n_rx_channels)
884 		return -EINVAL;
885 
886 	if (~ntuple->fs.data_mask)
887 		return -EINVAL;
888 
889 	efx_filter_init_rx(&filter, EFX_FILTER_PRI_MANUAL, 0,
890 			   (ntuple->fs.action == ETHTOOL_RXNTUPLE_ACTION_DROP) ?
891 			   0xfff : ntuple->fs.action);
892 
893 	switch (ntuple->fs.flow_type) {
894 	case TCP_V4_FLOW:
895 	case UDP_V4_FLOW: {
896 		u8 proto = (ntuple->fs.flow_type == TCP_V4_FLOW ?
897 			    IPPROTO_TCP : IPPROTO_UDP);
898 
899 		/* Must match all of destination, */
900 		if (ip_mask->ip4dst | ip_mask->pdst)
901 			return -EINVAL;
902 		/* all or none of source, */
903 		if ((ip_mask->ip4src | ip_mask->psrc) &&
904 		    ((__force u32)~ip_mask->ip4src |
905 		     (__force u16)~ip_mask->psrc))
906 			return -EINVAL;
907 		/* and nothing else */
908 		if ((u8)~ip_mask->tos | (u16)~ntuple->fs.vlan_tag_mask)
909 			return -EINVAL;
910 
911 		if (!ip_mask->ip4src)
912 			rc = efx_filter_set_ipv4_full(&filter, proto,
913 						      ip_entry->ip4dst,
914 						      ip_entry->pdst,
915 						      ip_entry->ip4src,
916 						      ip_entry->psrc);
917 		else
918 			rc = efx_filter_set_ipv4_local(&filter, proto,
919 						       ip_entry->ip4dst,
920 						       ip_entry->pdst);
921 		if (rc)
922 			return rc;
923 		break;
924 	}
925 
926 	case ETHER_FLOW:
927 		/* Must match all of destination, */
928 		if (!is_zero_ether_addr(mac_mask->h_dest))
929 			return -EINVAL;
930 		/* all or none of VID, */
931 		if (ntuple->fs.vlan_tag_mask != 0xf000 &&
932 		    ntuple->fs.vlan_tag_mask != 0xffff)
933 			return -EINVAL;
934 		/* and nothing else */
935 		if (!is_broadcast_ether_addr(mac_mask->h_source) ||
936 		    mac_mask->h_proto != htons(0xffff))
937 			return -EINVAL;
938 
939 		rc = efx_filter_set_eth_local(
940 			&filter,
941 			(ntuple->fs.vlan_tag_mask == 0xf000) ?
942 			ntuple->fs.vlan_tag : EFX_FILTER_VID_UNSPEC,
943 			mac_entry->h_dest);
944 		if (rc)
945 			return rc;
946 		break;
947 
948 	default:
949 		return -EINVAL;
950 	}
951 
952 	if (ntuple->fs.action == ETHTOOL_RXNTUPLE_ACTION_CLEAR)
953 		return efx_filter_remove_filter(efx, &filter);
954 
955 	rc = efx_filter_insert_filter(efx, &filter, true);
956 	return rc < 0 ? rc : 0;
957 }
958 
959 static int efx_ethtool_get_rxfh_indir(struct net_device *net_dev,
960 				      struct ethtool_rxfh_indir *indir)
961 {
962 	struct efx_nic *efx = netdev_priv(net_dev);
963 	size_t copy_size =
964 		min_t(size_t, indir->size, ARRAY_SIZE(efx->rx_indir_table));
965 
966 	if (efx_nic_rev(efx) < EFX_REV_FALCON_B0)
967 		return -EOPNOTSUPP;
968 
969 	indir->size = ARRAY_SIZE(efx->rx_indir_table);
970 	memcpy(indir->ring_index, efx->rx_indir_table,
971 	       copy_size * sizeof(indir->ring_index[0]));
972 	return 0;
973 }
974 
975 static int efx_ethtool_set_rxfh_indir(struct net_device *net_dev,
976 				      const struct ethtool_rxfh_indir *indir)
977 {
978 	struct efx_nic *efx = netdev_priv(net_dev);
979 	size_t i;
980 
981 	if (efx_nic_rev(efx) < EFX_REV_FALCON_B0)
982 		return -EOPNOTSUPP;
983 
984 	/* Validate size and indices */
985 	if (indir->size != ARRAY_SIZE(efx->rx_indir_table))
986 		return -EINVAL;
987 	for (i = 0; i < ARRAY_SIZE(efx->rx_indir_table); i++)
988 		if (indir->ring_index[i] >= efx->n_rx_channels)
989 			return -EINVAL;
990 
991 	memcpy(efx->rx_indir_table, indir->ring_index,
992 	       sizeof(efx->rx_indir_table));
993 	efx_nic_push_rx_indir_table(efx);
994 	return 0;
995 }
996 
997 const struct ethtool_ops efx_ethtool_ops = {
998 	.get_settings		= efx_ethtool_get_settings,
999 	.set_settings		= efx_ethtool_set_settings,
1000 	.get_drvinfo		= efx_ethtool_get_drvinfo,
1001 	.get_regs_len		= efx_ethtool_get_regs_len,
1002 	.get_regs		= efx_ethtool_get_regs,
1003 	.get_msglevel		= efx_ethtool_get_msglevel,
1004 	.set_msglevel		= efx_ethtool_set_msglevel,
1005 	.nway_reset		= efx_ethtool_nway_reset,
1006 	.get_link		= ethtool_op_get_link,
1007 	.get_coalesce		= efx_ethtool_get_coalesce,
1008 	.set_coalesce		= efx_ethtool_set_coalesce,
1009 	.get_ringparam		= efx_ethtool_get_ringparam,
1010 	.set_ringparam		= efx_ethtool_set_ringparam,
1011 	.get_pauseparam         = efx_ethtool_get_pauseparam,
1012 	.set_pauseparam         = efx_ethtool_set_pauseparam,
1013 	.get_sset_count		= efx_ethtool_get_sset_count,
1014 	.self_test		= efx_ethtool_self_test,
1015 	.get_strings		= efx_ethtool_get_strings,
1016 	.set_phys_id		= efx_ethtool_phys_id,
1017 	.get_ethtool_stats	= efx_ethtool_get_stats,
1018 	.get_wol                = efx_ethtool_get_wol,
1019 	.set_wol                = efx_ethtool_set_wol,
1020 	.reset			= efx_ethtool_reset,
1021 	.get_rxnfc		= efx_ethtool_get_rxnfc,
1022 	.set_rx_ntuple		= efx_ethtool_set_rx_ntuple,
1023 	.get_rxfh_indir		= efx_ethtool_get_rxfh_indir,
1024 	.set_rxfh_indir		= efx_ethtool_set_rxfh_indir,
1025 };
1026