[PATCH v8 net-next 07/10] net: dsa: microchip: add support for ethtool port counters

From: Prasanna Vengateshan
Date: Mon Feb 07 2022 - 12:34:37 EST


Added support for get_eth_**_stats() (phy/mac/ctrl) and
get_stats64()

Reused the KSZ common APIs for get_ethtool_stats() & get_sset_count()
along with relevant lan937x hooks for KSZ common layer and added
support for get_strings()

Signed-off-by: Prasanna Vengateshan <prasanna.vengateshan@xxxxxxxxxxxxx>
---
drivers/net/dsa/microchip/lan937x_main.c | 164 +++++++++++++++++++++++
1 file changed, 164 insertions(+)

diff --git a/drivers/net/dsa/microchip/lan937x_main.c b/drivers/net/dsa/microchip/lan937x_main.c
index bddb9ce41136..dfeb728bf737 100644
--- a/drivers/net/dsa/microchip/lan937x_main.c
+++ b/drivers/net/dsa/microchip/lan937x_main.c
@@ -45,6 +45,20 @@ static int lan937x_phy_write16(struct dsa_switch *ds, int addr, int reg,
return lan937x_internal_phy_write(dev, addr, reg, val);
}

+static void lan937x_get_strings(struct dsa_switch *ds, int port, u32 stringset,
+ u8 *buf)
+{
+ struct ksz_device *dev = ds->priv;
+ int i;
+
+ if (stringset != ETH_SS_STATS)
+ return;
+
+ for (i = 0; i < dev->mib_cnt; i++)
+ memcpy(buf + i * ETH_GSTRING_LEN, lan937x_mib_names[i].string,
+ ETH_GSTRING_LEN);
+}
+
static void lan937x_port_stp_state_set(struct dsa_switch *ds, int port,
u8 state)
{
@@ -334,12 +348,162 @@ static void lan937x_phylink_get_caps(struct dsa_switch *ds, int port,
}
}

+static void lan937x_get_eth_phy_stats(struct dsa_switch *ds, int port,
+ struct ethtool_eth_phy_stats *phy_stats)
+{
+ struct ksz_device *dev = ds->priv;
+ struct ksz_port_mib *mib = &dev->ports[port].mib;
+ u64 *cnt;
+
+ mutex_lock(&mib->cnt_mutex);
+
+ cnt = &mib->counters[lan937x_mib_rx_sym_err];
+ lan937x_r_mib_pkt(dev, port, lan937x_mib_rx_sym_err, NULL, cnt);
+
+ phy_stats->SymbolErrorDuringCarrier = *cnt;
+
+ mutex_unlock(&mib->cnt_mutex);
+}
+
+static void lan937x_get_eth_mac_stats(struct dsa_switch *ds, int port,
+ struct ethtool_eth_mac_stats *mac_stats)
+{
+ struct ksz_device *dev = ds->priv;
+ struct ksz_port_mib *mib = &dev->ports[port].mib;
+ u64 *ctr = mib->counters;
+
+ mutex_lock(&mib->cnt_mutex);
+
+ while (mib->cnt_ptr < dev->mib_cnt) {
+ lan937x_r_mib_pkt(dev, port, mib->cnt_ptr,
+ NULL, &mib->counters[mib->cnt_ptr]);
+ ++mib->cnt_ptr;
+ }
+
+ mac_stats->FramesTransmittedOK = ctr[lan937x_mib_tx_mcast] +
+ ctr[lan937x_mib_tx_bcast] +
+ ctr[lan937x_mib_tx_ucast] +
+ ctr[lan937x_mib_tx_pause];
+
+ mac_stats->SingleCollisionFrames = ctr[lan937x_mib_tx_single_col];
+ mac_stats->MultipleCollisionFrames = ctr[lan937x_mib_tx_mult_col];
+
+ mac_stats->FramesReceivedOK = ctr[lan937x_mib_rx_mcast] +
+ ctr[lan937x_mib_rx_bcast] +
+ ctr[lan937x_mib_rx_ucast] +
+ ctr[lan937x_mib_rx_pause];
+
+ mac_stats->FrameCheckSequenceErrors = ctr[lan937x_mib_rx_crc_err];
+ mac_stats->AlignmentErrors = ctr[lan937x_mib_rx_align_err];
+ mac_stats->OctetsTransmittedOK = ctr[lan937x_mib_tx_total];
+ mac_stats->FramesWithDeferredXmissions = ctr[lan937x_mib_tx_deferred];
+ mac_stats->LateCollisions = ctr[lan937x_mib_tx_late_col];
+ mac_stats->FramesAbortedDueToXSColls = ctr[lan937x_mib_tx_exc_col];
+ mac_stats->FramesLostDueToIntMACXmitError = ctr[lan937x_mib_tx_discard];
+
+ mac_stats->OctetsReceivedOK = ctr[lan937x_mib_rx_total];
+ mac_stats->FramesLostDueToIntMACRcvError = ctr[lan937x_mib_rx_discard];
+ mac_stats->MulticastFramesXmittedOK = ctr[lan937x_mib_tx_mcast];
+ mac_stats->BroadcastFramesXmittedOK = ctr[lan937x_mib_tx_bcast];
+
+ mac_stats->MulticastFramesReceivedOK = ctr[lan937x_mib_rx_mcast];
+ mac_stats->BroadcastFramesReceivedOK = ctr[lan937x_mib_rx_bcast];
+ mac_stats->InRangeLengthErrors = ctr[lan937x_mib_rx_fragments];
+
+ mib->cnt_ptr = 0;
+ mutex_unlock(&mib->cnt_mutex);
+}
+
+static void lan937x_get_eth_ctrl_stats(struct dsa_switch *ds, int port,
+ struct ethtool_eth_ctrl_stats *ctrl_sts)
+{
+ struct ksz_device *dev = ds->priv;
+ struct ksz_port_mib *mib = &dev->ports[port].mib;
+ u64 *cnt;
+
+ mutex_lock(&mib->cnt_mutex);
+
+ cnt = &mib->counters[lan937x_mib_rx_pause];
+ lan937x_r_mib_pkt(dev, port, lan937x_mib_rx_pause, NULL, cnt);
+ ctrl_sts->MACControlFramesReceived = *cnt;
+
+ cnt = &mib->counters[lan937x_mib_tx_pause];
+ lan937x_r_mib_pkt(dev, port, lan937x_mib_tx_pause, NULL, cnt);
+ ctrl_sts->MACControlFramesTransmitted = *cnt;
+
+ mutex_unlock(&mib->cnt_mutex);
+}
+
+static void lan937x_get_stats64(struct dsa_switch *ds, int port,
+ struct rtnl_link_stats64 *s)
+{
+ struct ksz_device *dev = ds->priv;
+ struct ksz_port_mib *mib = &dev->ports[port].mib;
+ u64 *ctr = mib->counters;
+
+ mutex_lock(&mib->cnt_mutex);
+
+ while (mib->cnt_ptr < dev->mib_cnt) {
+ lan937x_r_mib_pkt(dev, port, mib->cnt_ptr,
+ NULL, &mib->counters[mib->cnt_ptr]);
+ ++mib->cnt_ptr;
+ }
+
+ s->rx_packets = ctr[lan937x_mib_rx_mcast] +
+ ctr[lan937x_mib_rx_bcast] +
+ ctr[lan937x_mib_rx_ucast] +
+ ctr[lan937x_mib_rx_pause];
+
+ s->tx_packets = ctr[lan937x_mib_tx_mcast] +
+ ctr[lan937x_mib_tx_bcast] +
+ ctr[lan937x_mib_tx_ucast] +
+ ctr[lan937x_mib_tx_pause];
+
+ s->rx_bytes = ctr[lan937x_mib_rx_total];
+ s->tx_bytes = ctr[lan937x_mib_tx_total];
+
+ s->rx_errors = ctr[lan937x_mib_rx_fragments] +
+ ctr[lan937x_mib_rx_jabbers] +
+ ctr[lan937x_mib_rx_sym_err] +
+ ctr[lan937x_mib_rx_align_err] +
+ ctr[lan937x_mib_rx_crc_err];
+
+ s->tx_errors = ctr[lan937x_mib_tx_exc_col] +
+ ctr[lan937x_mib_tx_late_col];
+
+ s->rx_dropped = ctr[lan937x_mib_rx_discard];
+ s->tx_dropped = ctr[lan937x_mib_tx_discard];
+ s->multicast = ctr[lan937x_mib_rx_mcast];
+
+ s->collisions = ctr[lan937x_mib_tx_late_col] +
+ ctr[lan937x_mib_tx_single_col] +
+ ctr[lan937x_mib_tx_mult_col];
+
+ s->rx_length_errors = ctr[lan937x_mib_rx_fragments] +
+ ctr[lan937x_mib_rx_jabbers];
+
+ s->rx_crc_errors = ctr[lan937x_mib_rx_crc_err];
+ s->rx_frame_errors = ctr[lan937x_mib_rx_align_err];
+ s->tx_aborted_errors = ctr[lan937x_mib_tx_exc_col];
+ s->tx_window_errors = ctr[lan937x_mib_tx_late_col];
+
+ mib->cnt_ptr = 0;
+ mutex_unlock(&mib->cnt_mutex);
+}
+
const struct dsa_switch_ops lan937x_switch_ops = {
.get_tag_protocol = lan937x_get_tag_protocol,
.setup = lan937x_setup,
.phy_read = lan937x_phy_read16,
.phy_write = lan937x_phy_write16,
.port_enable = ksz_enable_port,
+ .get_strings = lan937x_get_strings,
+ .get_ethtool_stats = ksz_get_ethtool_stats,
+ .get_sset_count = ksz_sset_count,
+ .get_eth_ctrl_stats = lan937x_get_eth_ctrl_stats,
+ .get_eth_mac_stats = lan937x_get_eth_mac_stats,
+ .get_eth_phy_stats = lan937x_get_eth_phy_stats,
+ .get_stats64 = lan937x_get_stats64,
.port_bridge_join = ksz_port_bridge_join,
.port_bridge_leave = ksz_port_bridge_leave,
.port_stp_state_set = lan937x_port_stp_state_set,
--
2.30.2