xref: /linux/drivers/net/ethernet/sfc/ethtool_common.c (revision 37744feebc086908fd89760650f458ab19071750)
1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3  * Driver for Solarflare network controllers and boards
4  * Copyright 2019 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 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include "net_driver.h"
13 #include "mcdi.h"
14 #include "nic.h"
15 #include "selftest.h"
16 #include "ethtool_common.h"
17 
18 struct efx_sw_stat_desc {
19 	const char *name;
20 	enum {
21 		EFX_ETHTOOL_STAT_SOURCE_nic,
22 		EFX_ETHTOOL_STAT_SOURCE_channel,
23 		EFX_ETHTOOL_STAT_SOURCE_tx_queue
24 	} source;
25 	unsigned int offset;
26 	u64 (*get_stat)(void *field); /* Reader function */
27 };
28 
29 /* Initialiser for a struct efx_sw_stat_desc with type-checking */
30 #define EFX_ETHTOOL_STAT(stat_name, source_name, field, field_type, \
31 				get_stat_function) {			\
32 	.name = #stat_name,						\
33 	.source = EFX_ETHTOOL_STAT_SOURCE_##source_name,		\
34 	.offset = ((((field_type *) 0) ==				\
35 		      &((struct efx_##source_name *)0)->field) ?	\
36 		    offsetof(struct efx_##source_name, field) :		\
37 		    offsetof(struct efx_##source_name, field)),		\
38 	.get_stat = get_stat_function,					\
39 }
40 
41 static u64 efx_get_uint_stat(void *field)
42 {
43 	return *(unsigned int *)field;
44 }
45 
46 static u64 efx_get_atomic_stat(void *field)
47 {
48 	return atomic_read((atomic_t *) field);
49 }
50 
51 #define EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(field)		\
52 	EFX_ETHTOOL_STAT(field, nic, field,			\
53 			 atomic_t, efx_get_atomic_stat)
54 
55 #define EFX_ETHTOOL_UINT_CHANNEL_STAT(field)			\
56 	EFX_ETHTOOL_STAT(field, channel, n_##field,		\
57 			 unsigned int, efx_get_uint_stat)
58 #define EFX_ETHTOOL_UINT_CHANNEL_STAT_NO_N(field)		\
59 	EFX_ETHTOOL_STAT(field, channel, field,			\
60 			 unsigned int, efx_get_uint_stat)
61 
62 #define EFX_ETHTOOL_UINT_TXQ_STAT(field)			\
63 	EFX_ETHTOOL_STAT(tx_##field, tx_queue, field,		\
64 			 unsigned int, efx_get_uint_stat)
65 
66 static const struct efx_sw_stat_desc efx_sw_stat_desc[] = {
67 	EFX_ETHTOOL_UINT_TXQ_STAT(merge_events),
68 	EFX_ETHTOOL_UINT_TXQ_STAT(tso_bursts),
69 	EFX_ETHTOOL_UINT_TXQ_STAT(tso_long_headers),
70 	EFX_ETHTOOL_UINT_TXQ_STAT(tso_packets),
71 	EFX_ETHTOOL_UINT_TXQ_STAT(tso_fallbacks),
72 	EFX_ETHTOOL_UINT_TXQ_STAT(pushes),
73 	EFX_ETHTOOL_UINT_TXQ_STAT(pio_packets),
74 	EFX_ETHTOOL_UINT_TXQ_STAT(cb_packets),
75 	EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(rx_reset),
76 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tobe_disc),
77 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_ip_hdr_chksum_err),
78 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_tcp_udp_chksum_err),
79 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_inner_ip_hdr_chksum_err),
80 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_inner_tcp_udp_chksum_err),
81 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_outer_ip_hdr_chksum_err),
82 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_outer_tcp_udp_chksum_err),
83 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_eth_crc_err),
84 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_mcast_mismatch),
85 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_frm_trunc),
86 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_events),
87 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_merge_packets),
88 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_drops),
89 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_bad_drops),
90 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_tx),
91 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rx_xdp_redirect),
92 #ifdef CONFIG_RFS_ACCEL
93 	EFX_ETHTOOL_UINT_CHANNEL_STAT_NO_N(rfs_filter_count),
94 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rfs_succeeded),
95 	EFX_ETHTOOL_UINT_CHANNEL_STAT(rfs_failed),
96 #endif
97 };
98 
99 #define EFX_ETHTOOL_SW_STAT_COUNT ARRAY_SIZE(efx_sw_stat_desc)
100 
101 void efx_ethtool_get_drvinfo(struct net_device *net_dev,
102 			     struct ethtool_drvinfo *info)
103 {
104 	struct efx_nic *efx = netdev_priv(net_dev);
105 
106 	strlcpy(info->driver, KBUILD_MODNAME, sizeof(info->driver));
107 	strlcpy(info->version, EFX_DRIVER_VERSION, sizeof(info->version));
108 	efx_mcdi_print_fwver(efx, info->fw_version,
109 			     sizeof(info->fw_version));
110 	strlcpy(info->bus_info, pci_name(efx->pci_dev), sizeof(info->bus_info));
111 }
112 
113 u32 efx_ethtool_get_msglevel(struct net_device *net_dev)
114 {
115 	struct efx_nic *efx = netdev_priv(net_dev);
116 
117 	return efx->msg_enable;
118 }
119 
120 void efx_ethtool_set_msglevel(struct net_device *net_dev, u32 msg_enable)
121 {
122 	struct efx_nic *efx = netdev_priv(net_dev);
123 
124 	efx->msg_enable = msg_enable;
125 }
126 
127 void efx_ethtool_get_pauseparam(struct net_device *net_dev,
128 				struct ethtool_pauseparam *pause)
129 {
130 	struct efx_nic *efx = netdev_priv(net_dev);
131 
132 	pause->rx_pause = !!(efx->wanted_fc & EFX_FC_RX);
133 	pause->tx_pause = !!(efx->wanted_fc & EFX_FC_TX);
134 	pause->autoneg = !!(efx->wanted_fc & EFX_FC_AUTO);
135 }
136 
137 /**
138  * efx_fill_test - fill in an individual self-test entry
139  * @test_index:		Index of the test
140  * @strings:		Ethtool strings, or %NULL
141  * @data:		Ethtool test results, or %NULL
142  * @test:		Pointer to test result (used only if data != %NULL)
143  * @unit_format:	Unit name format (e.g. "chan\%d")
144  * @unit_id:		Unit id (e.g. 0 for "chan0")
145  * @test_format:	Test name format (e.g. "loopback.\%s.tx.sent")
146  * @test_id:		Test id (e.g. "PHYXS" for "loopback.PHYXS.tx_sent")
147  *
148  * Fill in an individual self-test entry.
149  */
150 static void efx_fill_test(unsigned int test_index, u8 *strings, u64 *data,
151 			  int *test, const char *unit_format, int unit_id,
152 			  const char *test_format, const char *test_id)
153 {
154 	char unit_str[ETH_GSTRING_LEN], test_str[ETH_GSTRING_LEN];
155 
156 	/* Fill data value, if applicable */
157 	if (data)
158 		data[test_index] = *test;
159 
160 	/* Fill string, if applicable */
161 	if (strings) {
162 		if (strchr(unit_format, '%'))
163 			snprintf(unit_str, sizeof(unit_str),
164 				 unit_format, unit_id);
165 		else
166 			strcpy(unit_str, unit_format);
167 		snprintf(test_str, sizeof(test_str), test_format, test_id);
168 		snprintf(strings + test_index * ETH_GSTRING_LEN,
169 			 ETH_GSTRING_LEN,
170 			 "%-6s %-24s", unit_str, test_str);
171 	}
172 }
173 
174 #define EFX_CHANNEL_NAME(_channel) "chan%d", _channel->channel
175 #define EFX_TX_QUEUE_NAME(_tx_queue) "txq%d", _tx_queue->queue
176 #define EFX_RX_QUEUE_NAME(_rx_queue) "rxq%d", _rx_queue->queue
177 #define EFX_LOOPBACK_NAME(_mode, _counter)			\
178 	"loopback.%s." _counter, STRING_TABLE_LOOKUP(_mode, efx_loopback_mode)
179 
180 /**
181  * efx_fill_loopback_test - fill in a block of loopback self-test entries
182  * @efx:		Efx NIC
183  * @lb_tests:		Efx loopback self-test results structure
184  * @mode:		Loopback test mode
185  * @test_index:		Starting index of the test
186  * @strings:		Ethtool strings, or %NULL
187  * @data:		Ethtool test results, or %NULL
188  *
189  * Fill in a block of loopback self-test entries.  Return new test
190  * index.
191  */
192 static int efx_fill_loopback_test(struct efx_nic *efx,
193 				  struct efx_loopback_self_tests *lb_tests,
194 				  enum efx_loopback_mode mode,
195 				  unsigned int test_index,
196 				  u8 *strings, u64 *data)
197 {
198 	struct efx_channel *channel =
199 		efx_get_channel(efx, efx->tx_channel_offset);
200 	struct efx_tx_queue *tx_queue;
201 
202 	efx_for_each_channel_tx_queue(tx_queue, channel) {
203 		efx_fill_test(test_index++, strings, data,
204 			      &lb_tests->tx_sent[tx_queue->queue],
205 			      EFX_TX_QUEUE_NAME(tx_queue),
206 			      EFX_LOOPBACK_NAME(mode, "tx_sent"));
207 		efx_fill_test(test_index++, strings, data,
208 			      &lb_tests->tx_done[tx_queue->queue],
209 			      EFX_TX_QUEUE_NAME(tx_queue),
210 			      EFX_LOOPBACK_NAME(mode, "tx_done"));
211 	}
212 	efx_fill_test(test_index++, strings, data,
213 		      &lb_tests->rx_good,
214 		      "rx", 0,
215 		      EFX_LOOPBACK_NAME(mode, "rx_good"));
216 	efx_fill_test(test_index++, strings, data,
217 		      &lb_tests->rx_bad,
218 		      "rx", 0,
219 		      EFX_LOOPBACK_NAME(mode, "rx_bad"));
220 
221 	return test_index;
222 }
223 
224 /**
225  * efx_ethtool_fill_self_tests - get self-test details
226  * @efx:		Efx NIC
227  * @tests:		Efx self-test results structure, or %NULL
228  * @strings:		Ethtool strings, or %NULL
229  * @data:		Ethtool test results, or %NULL
230  *
231  * Get self-test number of strings, strings, and/or test results.
232  * Return number of strings (== number of test results).
233  *
234  * The reason for merging these three functions is to make sure that
235  * they can never be inconsistent.
236  */
237 int efx_ethtool_fill_self_tests(struct efx_nic *efx,
238 				struct efx_self_tests *tests,
239 				u8 *strings, u64 *data)
240 {
241 	struct efx_channel *channel;
242 	unsigned int n = 0, i;
243 	enum efx_loopback_mode mode;
244 
245 	efx_fill_test(n++, strings, data, &tests->phy_alive,
246 		      "phy", 0, "alive", NULL);
247 	efx_fill_test(n++, strings, data, &tests->nvram,
248 		      "core", 0, "nvram", NULL);
249 	efx_fill_test(n++, strings, data, &tests->interrupt,
250 		      "core", 0, "interrupt", NULL);
251 
252 	/* Event queues */
253 	efx_for_each_channel(channel, efx) {
254 		efx_fill_test(n++, strings, data,
255 			      &tests->eventq_dma[channel->channel],
256 			      EFX_CHANNEL_NAME(channel),
257 			      "eventq.dma", NULL);
258 		efx_fill_test(n++, strings, data,
259 			      &tests->eventq_int[channel->channel],
260 			      EFX_CHANNEL_NAME(channel),
261 			      "eventq.int", NULL);
262 	}
263 
264 	efx_fill_test(n++, strings, data, &tests->memory,
265 		      "core", 0, "memory", NULL);
266 	efx_fill_test(n++, strings, data, &tests->registers,
267 		      "core", 0, "registers", NULL);
268 
269 	if (efx->phy_op->run_tests != NULL) {
270 		EFX_WARN_ON_PARANOID(efx->phy_op->test_name == NULL);
271 
272 		for (i = 0; true; ++i) {
273 			const char *name;
274 
275 			EFX_WARN_ON_PARANOID(i >= EFX_MAX_PHY_TESTS);
276 			name = efx->phy_op->test_name(efx, i);
277 			if (name == NULL)
278 				break;
279 
280 			efx_fill_test(n++, strings, data, &tests->phy_ext[i],
281 				      "phy", 0, name, NULL);
282 		}
283 	}
284 
285 	/* Loopback tests */
286 	for (mode = LOOPBACK_NONE; mode <= LOOPBACK_TEST_MAX; mode++) {
287 		if (!(efx->loopback_modes & (1 << mode)))
288 			continue;
289 		n = efx_fill_loopback_test(efx,
290 					   &tests->loopback[mode], mode, n,
291 					   strings, data);
292 	}
293 
294 	return n;
295 }
296 
297 static size_t efx_describe_per_queue_stats(struct efx_nic *efx, u8 *strings)
298 {
299 	size_t n_stats = 0;
300 	struct efx_channel *channel;
301 
302 	efx_for_each_channel(channel, efx) {
303 		if (efx_channel_has_tx_queues(channel)) {
304 			n_stats++;
305 			if (strings != NULL) {
306 				snprintf(strings, ETH_GSTRING_LEN,
307 					 "tx-%u.tx_packets",
308 					 channel->tx_queue[0].queue /
309 					 EFX_TXQ_TYPES);
310 
311 				strings += ETH_GSTRING_LEN;
312 			}
313 		}
314 	}
315 	efx_for_each_channel(channel, efx) {
316 		if (efx_channel_has_rx_queue(channel)) {
317 			n_stats++;
318 			if (strings != NULL) {
319 				snprintf(strings, ETH_GSTRING_LEN,
320 					 "rx-%d.rx_packets", channel->channel);
321 				strings += ETH_GSTRING_LEN;
322 			}
323 		}
324 	}
325 	if (efx->xdp_tx_queue_count && efx->xdp_tx_queues) {
326 		unsigned short xdp;
327 
328 		for (xdp = 0; xdp < efx->xdp_tx_queue_count; xdp++) {
329 			n_stats++;
330 			if (strings) {
331 				snprintf(strings, ETH_GSTRING_LEN,
332 					 "tx-xdp-cpu-%hu.tx_packets", xdp);
333 				strings += ETH_GSTRING_LEN;
334 			}
335 		}
336 	}
337 
338 	return n_stats;
339 }
340 
341 int efx_ethtool_get_sset_count(struct net_device *net_dev, int string_set)
342 {
343 	struct efx_nic *efx = netdev_priv(net_dev);
344 
345 	switch (string_set) {
346 	case ETH_SS_STATS:
347 		return efx->type->describe_stats(efx, NULL) +
348 		       EFX_ETHTOOL_SW_STAT_COUNT +
349 		       efx_describe_per_queue_stats(efx, NULL) +
350 		       efx_ptp_describe_stats(efx, NULL);
351 	case ETH_SS_TEST:
352 		return efx_ethtool_fill_self_tests(efx, NULL, NULL, NULL);
353 	default:
354 		return -EINVAL;
355 	}
356 }
357 
358 void efx_ethtool_get_strings(struct net_device *net_dev,
359 			     u32 string_set, u8 *strings)
360 {
361 	struct efx_nic *efx = netdev_priv(net_dev);
362 	int i;
363 
364 	switch (string_set) {
365 	case ETH_SS_STATS:
366 		strings += (efx->type->describe_stats(efx, strings) *
367 			    ETH_GSTRING_LEN);
368 		for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++)
369 			strlcpy(strings + i * ETH_GSTRING_LEN,
370 				efx_sw_stat_desc[i].name, ETH_GSTRING_LEN);
371 		strings += EFX_ETHTOOL_SW_STAT_COUNT * ETH_GSTRING_LEN;
372 		strings += (efx_describe_per_queue_stats(efx, strings) *
373 			    ETH_GSTRING_LEN);
374 		efx_ptp_describe_stats(efx, strings);
375 		break;
376 	case ETH_SS_TEST:
377 		efx_ethtool_fill_self_tests(efx, NULL, strings, NULL);
378 		break;
379 	default:
380 		/* No other string sets */
381 		break;
382 	}
383 }
384 
385 void efx_ethtool_get_stats(struct net_device *net_dev,
386 			   struct ethtool_stats *stats,
387 			   u64 *data)
388 {
389 	struct efx_nic *efx = netdev_priv(net_dev);
390 	const struct efx_sw_stat_desc *stat;
391 	struct efx_channel *channel;
392 	struct efx_tx_queue *tx_queue;
393 	struct efx_rx_queue *rx_queue;
394 	int i;
395 
396 	spin_lock_bh(&efx->stats_lock);
397 
398 	/* Get NIC statistics */
399 	data += efx->type->update_stats(efx, data, NULL);
400 
401 	/* Get software statistics */
402 	for (i = 0; i < EFX_ETHTOOL_SW_STAT_COUNT; i++) {
403 		stat = &efx_sw_stat_desc[i];
404 		switch (stat->source) {
405 		case EFX_ETHTOOL_STAT_SOURCE_nic:
406 			data[i] = stat->get_stat((void *)efx + stat->offset);
407 			break;
408 		case EFX_ETHTOOL_STAT_SOURCE_channel:
409 			data[i] = 0;
410 			efx_for_each_channel(channel, efx)
411 				data[i] += stat->get_stat((void *)channel +
412 							  stat->offset);
413 			break;
414 		case EFX_ETHTOOL_STAT_SOURCE_tx_queue:
415 			data[i] = 0;
416 			efx_for_each_channel(channel, efx) {
417 				efx_for_each_channel_tx_queue(tx_queue, channel)
418 					data[i] +=
419 						stat->get_stat((void *)tx_queue
420 							       + stat->offset);
421 			}
422 			break;
423 		}
424 	}
425 	data += EFX_ETHTOOL_SW_STAT_COUNT;
426 
427 	spin_unlock_bh(&efx->stats_lock);
428 
429 	efx_for_each_channel(channel, efx) {
430 		if (efx_channel_has_tx_queues(channel)) {
431 			*data = 0;
432 			efx_for_each_channel_tx_queue(tx_queue, channel) {
433 				*data += tx_queue->tx_packets;
434 			}
435 			data++;
436 		}
437 	}
438 	efx_for_each_channel(channel, efx) {
439 		if (efx_channel_has_rx_queue(channel)) {
440 			*data = 0;
441 			efx_for_each_channel_rx_queue(rx_queue, channel) {
442 				*data += rx_queue->rx_packets;
443 			}
444 			data++;
445 		}
446 	}
447 	if (efx->xdp_tx_queue_count && efx->xdp_tx_queues) {
448 		int xdp;
449 
450 		for (xdp = 0; xdp < efx->xdp_tx_queue_count; xdp++) {
451 			data[0] = efx->xdp_tx_queues[xdp]->tx_packets;
452 			data++;
453 		}
454 	}
455 
456 	efx_ptp_update_stats(efx, data);
457 }
458