1 // SPDX-License-Identifier: GPL-2.0-only
2 /****************************************************************************
3 * Driver for Solarflare network controllers and boards
4 * Copyright 2019 Solarflare Communications Inc.
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.
10 #include <linux/module.h>
11 #include <linux/netdevice.h>
12 #include "net_driver.h"
16 #include "ethtool_common.h"
18 struct efx_sw_stat_desc
{
21 EFX_ETHTOOL_STAT_SOURCE_nic
,
22 EFX_ETHTOOL_STAT_SOURCE_channel
,
23 EFX_ETHTOOL_STAT_SOURCE_tx_queue
26 u64 (*get_stat
)(void *field
); /* Reader function */
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) { \
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, \
41 static u64
efx_get_uint_stat(void *field
)
43 return *(unsigned int *)field
;
46 static u64
efx_get_atomic_stat(void *field
)
48 return atomic_read((atomic_t
*) field
);
51 #define EFX_ETHTOOL_ATOMIC_NIC_ERROR_STAT(field) \
52 EFX_ETHTOOL_STAT(field, nic, field, \
53 atomic_t, efx_get_atomic_stat)
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)
62 #define EFX_ETHTOOL_UINT_TXQ_STAT(field) \
63 EFX_ETHTOOL_STAT(tx_##field, tx_queue, field, \
64 unsigned int, efx_get_uint_stat)
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
),
99 #define EFX_ETHTOOL_SW_STAT_COUNT ARRAY_SIZE(efx_sw_stat_desc)
101 void efx_ethtool_get_drvinfo(struct net_device
*net_dev
,
102 struct ethtool_drvinfo
*info
)
104 struct efx_nic
*efx
= netdev_priv(net_dev
);
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
));
113 u32
efx_ethtool_get_msglevel(struct net_device
*net_dev
)
115 struct efx_nic
*efx
= netdev_priv(net_dev
);
117 return efx
->msg_enable
;
120 void efx_ethtool_set_msglevel(struct net_device
*net_dev
, u32 msg_enable
)
122 struct efx_nic
*efx
= netdev_priv(net_dev
);
124 efx
->msg_enable
= msg_enable
;
127 void efx_ethtool_get_pauseparam(struct net_device
*net_dev
,
128 struct ethtool_pauseparam
*pause
)
130 struct efx_nic
*efx
= netdev_priv(net_dev
);
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
);
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")
148 * Fill in an individual self-test entry.
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
)
154 char unit_str
[ETH_GSTRING_LEN
], test_str
[ETH_GSTRING_LEN
];
156 /* Fill data value, if applicable */
158 data
[test_index
] = *test
;
160 /* Fill string, if applicable */
162 if (strchr(unit_format
, '%'))
163 snprintf(unit_str
, sizeof(unit_str
),
164 unit_format
, unit_id
);
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
,
170 "%-6s %-24s", unit_str
, test_str
);
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)
181 * efx_fill_loopback_test - fill in a block of loopback self-test entries
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
189 * Fill in a block of loopback self-test entries. Return new test
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
)
198 struct efx_channel
*channel
=
199 efx_get_channel(efx
, efx
->tx_channel_offset
);
200 struct efx_tx_queue
*tx_queue
;
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"));
212 efx_fill_test(test_index
++, strings
, data
,
215 EFX_LOOPBACK_NAME(mode
, "rx_good"));
216 efx_fill_test(test_index
++, strings
, data
,
219 EFX_LOOPBACK_NAME(mode
, "rx_bad"));
225 * efx_ethtool_fill_self_tests - get self-test details
227 * @tests: Efx self-test results structure, or %NULL
228 * @strings: Ethtool strings, or %NULL
229 * @data: Ethtool test results, or %NULL
231 * Get self-test number of strings, strings, and/or test results.
232 * Return number of strings (== number of test results).
234 * The reason for merging these three functions is to make sure that
235 * they can never be inconsistent.
237 int efx_ethtool_fill_self_tests(struct efx_nic
*efx
,
238 struct efx_self_tests
*tests
,
239 u8
*strings
, u64
*data
)
241 struct efx_channel
*channel
;
242 unsigned int n
= 0, i
;
243 enum efx_loopback_mode mode
;
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
);
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
),
258 efx_fill_test(n
++, strings
, data
,
259 &tests
->eventq_int
[channel
->channel
],
260 EFX_CHANNEL_NAME(channel
),
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
);
269 if (efx
->phy_op
->run_tests
!= NULL
) {
270 EFX_WARN_ON_PARANOID(efx
->phy_op
->test_name
== NULL
);
272 for (i
= 0; true; ++i
) {
275 EFX_WARN_ON_PARANOID(i
>= EFX_MAX_PHY_TESTS
);
276 name
= efx
->phy_op
->test_name(efx
, i
);
280 efx_fill_test(n
++, strings
, data
, &tests
->phy_ext
[i
],
281 "phy", 0, name
, NULL
);
286 for (mode
= LOOPBACK_NONE
; mode
<= LOOPBACK_TEST_MAX
; mode
++) {
287 if (!(efx
->loopback_modes
& (1 << mode
)))
289 n
= efx_fill_loopback_test(efx
,
290 &tests
->loopback
[mode
], mode
, n
,
297 static size_t efx_describe_per_queue_stats(struct efx_nic
*efx
, u8
*strings
)
300 struct efx_channel
*channel
;
302 efx_for_each_channel(channel
, efx
) {
303 if (efx_channel_has_tx_queues(channel
)) {
305 if (strings
!= NULL
) {
306 snprintf(strings
, ETH_GSTRING_LEN
,
308 channel
->tx_queue
[0].queue
/
311 strings
+= ETH_GSTRING_LEN
;
315 efx_for_each_channel(channel
, efx
) {
316 if (efx_channel_has_rx_queue(channel
)) {
318 if (strings
!= NULL
) {
319 snprintf(strings
, ETH_GSTRING_LEN
,
320 "rx-%d.rx_packets", channel
->channel
);
321 strings
+= ETH_GSTRING_LEN
;
325 if (efx
->xdp_tx_queue_count
&& efx
->xdp_tx_queues
) {
328 for (xdp
= 0; xdp
< efx
->xdp_tx_queue_count
; xdp
++) {
331 snprintf(strings
, ETH_GSTRING_LEN
,
332 "tx-xdp-cpu-%hu.tx_packets", xdp
);
333 strings
+= ETH_GSTRING_LEN
;
341 int efx_ethtool_get_sset_count(struct net_device
*net_dev
, int string_set
)
343 struct efx_nic
*efx
= netdev_priv(net_dev
);
345 switch (string_set
) {
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
);
352 return efx_ethtool_fill_self_tests(efx
, NULL
, NULL
, NULL
);
358 void efx_ethtool_get_strings(struct net_device
*net_dev
,
359 u32 string_set
, u8
*strings
)
361 struct efx_nic
*efx
= netdev_priv(net_dev
);
364 switch (string_set
) {
366 strings
+= (efx
->type
->describe_stats(efx
, strings
) *
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
) *
374 efx_ptp_describe_stats(efx
, strings
);
377 efx_ethtool_fill_self_tests(efx
, NULL
, strings
, NULL
);
380 /* No other string sets */
385 void efx_ethtool_get_stats(struct net_device
*net_dev
,
386 struct ethtool_stats
*stats
,
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
;
396 spin_lock_bh(&efx
->stats_lock
);
398 /* Get NIC statistics */
399 data
+= efx
->type
->update_stats(efx
, data
, NULL
);
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
);
408 case EFX_ETHTOOL_STAT_SOURCE_channel
:
410 efx_for_each_channel(channel
, efx
)
411 data
[i
] += stat
->get_stat((void *)channel
+
414 case EFX_ETHTOOL_STAT_SOURCE_tx_queue
:
416 efx_for_each_channel(channel
, efx
) {
417 efx_for_each_channel_tx_queue(tx_queue
, channel
)
419 stat
->get_stat((void *)tx_queue
425 data
+= EFX_ETHTOOL_SW_STAT_COUNT
;
427 spin_unlock_bh(&efx
->stats_lock
);
429 efx_for_each_channel(channel
, efx
) {
430 if (efx_channel_has_tx_queues(channel
)) {
432 efx_for_each_channel_tx_queue(tx_queue
, channel
) {
433 *data
+= tx_queue
->tx_packets
;
438 efx_for_each_channel(channel
, efx
) {
439 if (efx_channel_has_rx_queue(channel
)) {
441 efx_for_each_channel_rx_queue(rx_queue
, channel
) {
442 *data
+= rx_queue
->rx_packets
;
447 if (efx
->xdp_tx_queue_count
&& efx
->xdp_tx_queues
) {
450 for (xdp
= 0; xdp
< efx
->xdp_tx_queue_count
; xdp
++) {
451 data
[0] = efx
->xdp_tx_queues
[xdp
]->tx_packets
;
456 efx_ptp_update_stats(efx
, data
);