1 // SPDX-License-Identifier: GPL-2.0 2 // 3 // Copyright (c) 2023, 2024 Pengutronix, 4 // Marc Kleine-Budde <kernel@pengutronix.de> 5 // 6 7 #include <linux/ethtool.h> 8 9 #include "rockchip_canfd.h" 10 11 enum rkcanfd_stats_type { 12 RKCANFD_STATS_TYPE_RX_FIFO_EMPTY_ERRORS, 13 RKCANFD_STATS_TYPE_TX_EXTENDED_AS_STANDARD_ERRORS, 14 }; 15 16 static const char rkcanfd_stats_strings[][ETH_GSTRING_LEN] = { 17 [RKCANFD_STATS_TYPE_RX_FIFO_EMPTY_ERRORS] = "rx_fifo_empty_errors", 18 [RKCANFD_STATS_TYPE_TX_EXTENDED_AS_STANDARD_ERRORS] = "tx_extended_as_standard_errors", 19 }; 20 21 static void 22 rkcanfd_ethtool_get_strings(struct net_device *ndev, u32 stringset, u8 *buf) 23 { 24 switch (stringset) { 25 case ETH_SS_STATS: 26 memcpy(buf, rkcanfd_stats_strings, 27 sizeof(rkcanfd_stats_strings)); 28 } 29 } 30 31 static int rkcanfd_ethtool_get_sset_count(struct net_device *netdev, int sset) 32 { 33 switch (sset) { 34 case ETH_SS_STATS: 35 return ARRAY_SIZE(rkcanfd_stats_strings); 36 default: 37 return -EOPNOTSUPP; 38 } 39 } 40 41 static void 42 rkcanfd_ethtool_get_ethtool_stats(struct net_device *ndev, 43 struct ethtool_stats *stats, u64 *data) 44 { 45 struct rkcanfd_priv *priv = netdev_priv(ndev); 46 struct rkcanfd_stats *rkcanfd_stats; 47 unsigned int start; 48 49 rkcanfd_stats = &priv->stats; 50 51 do { 52 start = u64_stats_fetch_begin(&rkcanfd_stats->syncp); 53 54 data[RKCANFD_STATS_TYPE_RX_FIFO_EMPTY_ERRORS] = 55 u64_stats_read(&rkcanfd_stats->rx_fifo_empty_errors); 56 data[RKCANFD_STATS_TYPE_TX_EXTENDED_AS_STANDARD_ERRORS] = 57 u64_stats_read(&rkcanfd_stats->tx_extended_as_standard_errors); 58 } while (u64_stats_fetch_retry(&rkcanfd_stats->syncp, start)); 59 } 60 61 static const struct ethtool_ops rkcanfd_ethtool_ops = { 62 .get_ts_info = can_ethtool_op_get_ts_info_hwts, 63 .get_strings = rkcanfd_ethtool_get_strings, 64 .get_sset_count = rkcanfd_ethtool_get_sset_count, 65 .get_ethtool_stats = rkcanfd_ethtool_get_ethtool_stats, 66 }; 67 68 void rkcanfd_ethtool_init(struct rkcanfd_priv *priv) 69 { 70 priv->ndev->ethtool_ops = &rkcanfd_ethtool_ops; 71 72 u64_stats_init(&priv->stats.syncp); 73 } 74