On Wed, Nov 30, 2022 at 06:58:58PM +0530, Rakesh Sankaranarayanan wrote:
> diff --git a/drivers/net/dsa/microchip/ksz_ethtool.c b/drivers/net/dsa/microchip/ksz_ethtool.c
> new file mode 100644
> index 000000000000..7e1f1b4d1e98
> --- /dev/null
> +++ b/drivers/net/dsa/microchip/ksz_ethtool.c
> @@ -0,0 +1,178 @@
> +// SPDX-License-Identifier: GPL-2.0-or-later
> +/*
> + * Microchip KSZ series ethtool statistics
> + *
> + * Copyright (C) 2022 Microchip Technology Inc.
> + */
> +
> +#include "ksz_common.h"
> +#include "ksz_ethtool.h"
> +
> +enum ksz8_mib_entry {
> + ksz8_rx,
> + ksz8_rx_hi,
> + ksz8_rx_undersize,
> + ksz8_rx_fragments,
> + ksz8_rx_oversize,
> + ksz8_rx_jabbers,
> + ksz8_rx_symbol_err,
> + ksz8_rx_crc_err,
> + ksz8_rx_align_err,
> + ksz8_rx_mac_ctrl,
> + ksz8_rx_pause,
> + ksz8_rx_bcast,
> + ksz8_rx_mcast,
> + ksz8_rx_ucast,
> + ksz8_rx_64_or_less,
> + ksz8_rx_65_127,
> + ksz8_rx_128_255,
> + ksz8_rx_256_511,
> + ksz8_rx_512_1023,
> + ksz8_rx_1024_1522,
> + ksz8_tx,
> + ksz8_tx_hi,
> + ksz8_tx_late_col,
> + ksz8_tx_pause,
> + ksz8_tx_bcast,
> + ksz8_tx_mcast,
> + ksz8_tx_ucast,
> + ksz8_tx_deferred,
> + ksz8_tx_total_col,
> + ksz8_tx_exc_col,
> + ksz8_tx_single_col,
> + ksz8_tx_mult_col,
> + ksz8_rx_discards = 0x100,
> + ksz8_tx_discards,
> +};
> +
> +enum ksz9477_mib_entry {
> + ksz9477_rx_hi,
> + ksz9477_rx_undersize,
> + ksz9477_rx_fragments,
> + ksz9477_rx_oversize,
> + ksz9477_rx_jabbers,
> + ksz9477_rx_symbol_err,
> + ksz9477_rx_crc_err,
> + ksz9477_rx_align_err,
> + ksz9477_rx_mac_ctrl,
> + ksz9477_rx_pause,
> + ksz9477_rx_bcast,
> + ksz9477_rx_mcast,
> + ksz9477_rx_ucast,
> + ksz9477_rx_64_or_less,
> + ksz9477_rx_65_127,
> + ksz9477_rx_128_255,
> + ksz9477_rx_256_511,
> + ksz9477_rx_512_1023,
> + ksz9477_rx_1024_1522,
> + ksz9477_rx_1523_2000,
> + ksz9477_rx_2001,
> + ksz9477_tx_hi,
> + ksz9477_tx_late_col,
> + ksz9477_tx_pause,
> + ksz9477_tx_bcast,
> + ksz9477_tx_mcast,
> + ksz9477_tx_ucast,
> + ksz9477_tx_deferred,
> + ksz9477_tx_total_col,
> + ksz9477_tx_exc_col,
> + ksz9477_tx_single_col,
> + ksz9477_tx_mult_col,
> + ksz9477_rx_total = 0x80,
> + ksz9477_tx_total,
> + ksz9477_rx_discards,
> + ksz9477_tx_discards,
> +};
We usually name constants using all capitals.
Can you do something to reuse the ksz_mib_names structures?
@@ -1,6 +1,7 @@
# SPDX-License-Identifier: GPL-2.0-only
obj-$(CONFIG_NET_DSA_MICROCHIP_KSZ_COMMON) += ksz_switch.o
ksz_switch-objs := ksz_common.o
+ksz_switch-objs += ksz_ethtool.o
ksz_switch-objs += ksz9477.o
ksz_switch-objs += ksz8795.o
ksz_switch-objs += lan937x_main.o
@@ -24,6 +24,7 @@
#include <net/switchdev.h>
#include "ksz_common.h"
+#include "ksz_ethtool.h"
#include "ksz8.h"
#include "ksz9477.h"
#include "lan937x.h"
@@ -157,6 +158,7 @@ static const struct ksz_dev_ops ksz8_dev_ops = {
.r_mib_pkt = ksz8_r_mib_pkt,
.freeze_mib = ksz8_freeze_mib,
.port_init_cnt = ksz8_port_init_cnt,
+ .get_rmon_stats = ksz8_get_rmon_stats,
.fdb_dump = ksz8_fdb_dump,
.mdb_add = ksz8_mdb_add,
.mdb_del = ksz8_mdb_del,
@@ -194,6 +196,7 @@ static const struct ksz_dev_ops ksz9477_dev_ops = {
.r_mib_stat64 = ksz_r_mib_stats64,
.freeze_mib = ksz9477_freeze_mib,
.port_init_cnt = ksz9477_port_init_cnt,
+ .get_rmon_stats = ksz9477_get_rmon_stats,
.vlan_filtering = ksz9477_port_vlan_filtering,
.vlan_add = ksz9477_port_vlan_add,
.vlan_del = ksz9477_port_vlan_del,
@@ -230,6 +233,7 @@ static const struct ksz_dev_ops lan937x_dev_ops = {
.r_mib_stat64 = ksz_r_mib_stats64,
.freeze_mib = ksz9477_freeze_mib,
.port_init_cnt = ksz9477_port_init_cnt,
+ .get_rmon_stats = ksz9477_get_rmon_stats,
.vlan_filtering = ksz9477_port_vlan_filtering,
.vlan_add = ksz9477_port_vlan_add,
.vlan_del = ksz9477_port_vlan_del,
@@ -1609,6 +1613,16 @@ static void ksz_get_pause_stats(struct dsa_switch *ds, int port,
spin_unlock(&mib->stats64_lock);
}
+static void ksz_get_rmon_stats(struct dsa_switch *ds, int port,
+ struct ethtool_rmon_stats *rmon_stats,
+ const struct ethtool_rmon_hist_range **ranges)
+{
+ struct ksz_device *dev = ds->priv;
+
+ if (dev->dev_ops->get_rmon_stats)
+ dev->dev_ops->get_rmon_stats(dev, port, rmon_stats, ranges);
+}
+
static void ksz_get_strings(struct dsa_switch *ds, int port,
u32 stringset, uint8_t *buf)
{
@@ -2859,6 +2873,7 @@ static const struct dsa_switch_ops ksz_switch_ops = {
.port_mirror_del = ksz_port_mirror_del,
.get_stats64 = ksz_get_stats64,
.get_pause_stats = ksz_get_pause_stats,
+ .get_rmon_stats = ksz_get_rmon_stats,
.port_change_mtu = ksz_change_mtu,
.port_max_mtu = ksz_max_mtu,
};
@@ -339,6 +339,9 @@ struct ksz_dev_ops {
int (*reset)(struct ksz_device *dev);
int (*init)(struct ksz_device *dev);
void (*exit)(struct ksz_device *dev);
+ void (*get_rmon_stats)(struct ksz_device *dev, int port,
+ struct ethtool_rmon_stats *rmon_stats,
+ const struct ethtool_rmon_hist_range **ranges);
};
struct ksz_device *ksz_switch_alloc(struct device *base, void *priv);
new file mode 100644
@@ -0,0 +1,178 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Microchip KSZ series ethtool statistics
+ *
+ * Copyright (C) 2022 Microchip Technology Inc.
+ */
+
+#include "ksz_common.h"
+#include "ksz_ethtool.h"
+
+enum ksz8_mib_entry {
+ ksz8_rx,
+ ksz8_rx_hi,
+ ksz8_rx_undersize,
+ ksz8_rx_fragments,
+ ksz8_rx_oversize,
+ ksz8_rx_jabbers,
+ ksz8_rx_symbol_err,
+ ksz8_rx_crc_err,
+ ksz8_rx_align_err,
+ ksz8_rx_mac_ctrl,
+ ksz8_rx_pause,
+ ksz8_rx_bcast,
+ ksz8_rx_mcast,
+ ksz8_rx_ucast,
+ ksz8_rx_64_or_less,
+ ksz8_rx_65_127,
+ ksz8_rx_128_255,
+ ksz8_rx_256_511,
+ ksz8_rx_512_1023,
+ ksz8_rx_1024_1522,
+ ksz8_tx,
+ ksz8_tx_hi,
+ ksz8_tx_late_col,
+ ksz8_tx_pause,
+ ksz8_tx_bcast,
+ ksz8_tx_mcast,
+ ksz8_tx_ucast,
+ ksz8_tx_deferred,
+ ksz8_tx_total_col,
+ ksz8_tx_exc_col,
+ ksz8_tx_single_col,
+ ksz8_tx_mult_col,
+ ksz8_rx_discards = 0x100,
+ ksz8_tx_discards,
+};
+
+enum ksz9477_mib_entry {
+ ksz9477_rx_hi,
+ ksz9477_rx_undersize,
+ ksz9477_rx_fragments,
+ ksz9477_rx_oversize,
+ ksz9477_rx_jabbers,
+ ksz9477_rx_symbol_err,
+ ksz9477_rx_crc_err,
+ ksz9477_rx_align_err,
+ ksz9477_rx_mac_ctrl,
+ ksz9477_rx_pause,
+ ksz9477_rx_bcast,
+ ksz9477_rx_mcast,
+ ksz9477_rx_ucast,
+ ksz9477_rx_64_or_less,
+ ksz9477_rx_65_127,
+ ksz9477_rx_128_255,
+ ksz9477_rx_256_511,
+ ksz9477_rx_512_1023,
+ ksz9477_rx_1024_1522,
+ ksz9477_rx_1523_2000,
+ ksz9477_rx_2001,
+ ksz9477_tx_hi,
+ ksz9477_tx_late_col,
+ ksz9477_tx_pause,
+ ksz9477_tx_bcast,
+ ksz9477_tx_mcast,
+ ksz9477_tx_ucast,
+ ksz9477_tx_deferred,
+ ksz9477_tx_total_col,
+ ksz9477_tx_exc_col,
+ ksz9477_tx_single_col,
+ ksz9477_tx_mult_col,
+ ksz9477_rx_total = 0x80,
+ ksz9477_tx_total,
+ ksz9477_rx_discards,
+ ksz9477_tx_discards,
+};
+
+static const struct ethtool_rmon_hist_range ksz_rmon_ranges[] = {
+ { 0, 64 },
+ { 65, 127 },
+ { 128, 255 },
+ { 256, 511 },
+ { 512, 1023 },
+ { 1024, 1522 },
+ { 1523, 2000 },
+ { 2001, 9000 },
+ {}
+};
+
+#define KSZ8_HIST_LEN 6
+#define KSZ9477_HIST_LEN 8
+
+void ksz8_get_rmon_stats(struct ksz_device *dev, int port,
+ struct ethtool_rmon_stats *rmon_stats,
+ const struct ethtool_rmon_hist_range **ranges)
+{
+ struct ksz_port_mib *mib;
+ u64 *cnt;
+ u8 i;
+
+ mib = &dev->ports[port].mib;
+
+ mutex_lock(&mib->cnt_mutex);
+
+ cnt = &mib->counters[ksz8_rx_undersize];
+ dev->dev_ops->r_mib_pkt(dev, port, ksz8_rx_undersize, NULL, cnt);
+ rmon_stats->undersize_pkts = *cnt;
+
+ cnt = &mib->counters[ksz8_rx_oversize];
+ dev->dev_ops->r_mib_pkt(dev, port, ksz8_rx_oversize, NULL, cnt);
+ rmon_stats->oversize_pkts = *cnt;
+
+ cnt = &mib->counters[ksz8_rx_fragments];
+ dev->dev_ops->r_mib_pkt(dev, port, ksz8_rx_fragments, NULL, cnt);
+ rmon_stats->fragments = *cnt;
+
+ cnt = &mib->counters[ksz8_rx_jabbers];
+ dev->dev_ops->r_mib_pkt(dev, port, ksz8_rx_jabbers, NULL, cnt);
+ rmon_stats->jabbers = *cnt;
+
+ for (i = 0; i < KSZ8_HIST_LEN; i++) {
+ cnt = &mib->counters[ksz8_rx_64_or_less + i];
+ dev->dev_ops->r_mib_pkt(dev, port, (ksz8_rx_64_or_less + i), NULL, cnt);
+ rmon_stats->hist[i] = *cnt;
+ }
+
+ mutex_unlock(&mib->cnt_mutex);
+
+ *ranges = ksz_rmon_ranges;
+}
+
+void ksz9477_get_rmon_stats(struct ksz_device *dev, int port,
+ struct ethtool_rmon_stats *rmon_stats,
+ const struct ethtool_rmon_hist_range **ranges)
+{
+ struct ksz_port_mib *mib;
+ u64 *cnt;
+ u8 i;
+
+ mib = &dev->ports[port].mib;
+
+ mutex_lock(&mib->cnt_mutex);
+
+ cnt = &mib->counters[ksz9477_rx_undersize];
+ dev->dev_ops->r_mib_pkt(dev, port, ksz9477_rx_undersize, NULL, cnt);
+ rmon_stats->undersize_pkts = *cnt;
+
+ cnt = &mib->counters[ksz9477_rx_oversize];
+ dev->dev_ops->r_mib_pkt(dev, port, ksz9477_rx_oversize, NULL, cnt);
+ rmon_stats->oversize_pkts = *cnt;
+
+ cnt = &mib->counters[ksz9477_rx_fragments];
+ dev->dev_ops->r_mib_pkt(dev, port, ksz9477_rx_fragments, NULL, cnt);
+ rmon_stats->fragments = *cnt;
+
+ cnt = &mib->counters[ksz9477_rx_jabbers];
+ dev->dev_ops->r_mib_pkt(dev, port, ksz9477_rx_jabbers, NULL, cnt);
+ rmon_stats->jabbers = *cnt;
+
+ for (i = 0; i < KSZ9477_HIST_LEN; i++) {
+ cnt = &mib->counters[ksz9477_rx_64_or_less + i];
+ dev->dev_ops->r_mib_pkt(dev, port, (ksz9477_rx_64_or_less + i), NULL, cnt);
+ rmon_stats->hist[i] = *cnt;
+ }
+
+ mutex_unlock(&mib->cnt_mutex);
+
+ *ranges = ksz_rmon_ranges;
+}
new file mode 100644
@@ -0,0 +1,18 @@
+/* SPDX-License-Identifier: GPL-2.0-or-later */
+/*
+ * Microchip KSZ series ethtool statistics
+ *
+ * Copyright (C) 2022 Microchip Technology Inc.
+ */
+
+#ifndef __KSZ_ETHTOOL_H
+#define __KSZ_ETHTOOL_H
+
+void ksz8_get_rmon_stats(struct ksz_device *dev, int port,
+ struct ethtool_rmon_stats *rmon_stats,
+ const struct ethtool_rmon_hist_range **ranges);
+
+void ksz9477_get_rmon_stats(struct ksz_device *dev, int port,
+ struct ethtool_rmon_stats *rmon_stats,
+ const struct ethtool_rmon_hist_range **ranges);
+#endif