net: dsa: microchip: add MIB counters support
v2 cover net: dsa: microchip: add MIB counters support
From: Tristram Ha <[email protected]>
This series of patches is to modify the KSZ9477 DSA driver to read MIB counters periodically to avoid overflow.
The MIB counters should be read only when there is link. Otherwise it is a waste of time as hardware never increases the counters.
Functions are added to check the port link status so that MIB counters read call is used efficiently.
v2
- Create macro similar to readx_poll_timeout to use with switch
- Create ksz_port_cleanup function so that variables like on_ports and live_ports can be updated inside it.
v1
- Use readx_poll_timeout
- Do not clear MIB counters when port is enabled
- Do not advertise 1000 half-duplex mode when port is enabled
- Do not use freeze function as MIB counters may miss counts
Tristram Ha (4):
net: dsa: microchip: prepare PHY for proper advertisement
net: dsa: microchip: add MIB counter reading support
net: dsa: microchip: get port link status
net: dsa: microchip: remove unnecessary include headers
drivers/net/dsa/microchip/ksz9477.c | 137 +++++++++++++++++---------- drivers/net/dsa/microchip/ksz_common.c | 163 ++++++++++++++++++++++++++++++++- drivers/net/dsa/microchip/ksz_common.h | 27 +++++- drivers/net/dsa/microchip/ksz_priv.h | 14 +-- 4 files changed, 284 insertions(+), 57 deletions(-)
Signature · 1 line
1.9.1
PATCH v2 1/4 net: dsa: microchip: prepare PHY for proper advertisement
From: Tristram Ha <[email protected]>
Prepare PHY for proper advertisement as sometimes the PHY in the switch has its own problems even though it may share the PHY id from regular PHY but the fixes in the PHY driver do not apply.
--- drivers/net/dsa/microchip/ksz9477.c | 13 ++++++++++++- drivers/net/dsa/microchip/ksz_common.c | 3 ++- drivers/net/dsa/microchip/ksz_priv.h | 4 +++- 3 files changed, 17 insertions(+), 3 deletions(-)
drivers/net/dsa/microchip/ksz9477.c +12 -1
@@ -2,7 +2,7 @@/* * Microchip KSZ9477 switch driver main logic * -* Copyright (C) 2017-2018 Microchip Technology Inc. +* Copyright (C) 2017-2019 Microchip Technology Inc. */ #include <linux/delay.h>
@@ -966,6 +966,16 @@ static void ksz9477_port_mirror_del(struct dsa_switch *ds, int port, PORT_MIRROR_SNIFFER, false); } +static void ksz9477_phy_setup (struct ksz_device *dev,int port, +struct phy_device *phy) +{ +if (port < dev->phy_port_cnt) { +/* The MAC actually cannot run in 1000 half-duplex mode. */ +phy_remove_link_mode (phy, + ETHTOOL_LINK_MODE_1000baseT_Half_BIT); + } +} +static void ksz9477_port_setup (struct ksz_device *dev,int port,bool cpu_port) {u8 data8;
@@ -1299,6 +1309,7 @@ static void ksz9477_switch_exit(struct ksz_device *dev) .get_port_addr = ksz9477_get_port_addr, .cfg_port_member = ksz9477_cfg_port_member, .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table, + .phy_setup = ksz9477_phy_setup, .port_setup = ksz9477_port_setup, .shutdown = ksz9477_reset_switch, .detect = ksz9477_switch_detect,
drivers/net/dsa/microchip/ksz_common.c +2 -1
@@ -2,7 +2,7 @@/* * Microchip switch driver main logic * -* Copyright (C) 2017-2018 Microchip Technology Inc. +* Copyright (C) 2017-2019 Microchip Technology Inc. */ #include <linux/delay.h>
@@ -238,6 +238,7 @@ int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy)/* setup slave port */ dev->dev_ops->port_setup (dev, port, false); + dev->dev_ops->phy_setup (dev, port, phy);/* port_stp_state_set() will be called after to enable the port so * there is no need to do anything.
drivers/net/dsa/microchip/ksz_priv.h +3 -1
@@ -2,7 +2,7 @@* * Microchip KSZ series switch common definitions * -* Copyright (C) 2017-2018 Microchip Technology Inc. +* Copyright (C) 2017-2019 Microchip Technology Inc. */ #ifndef __KSZ_PRIV_H
@@ -137,6 +137,8 @@ struct ksz_dev_ops {u32 (*get_port_addr)(int port,int offset);void (*cfg_port_member)(struct ksz_device *dev,int port,u8 member);void (*flush_dyn_mac_table)(struct ksz_device *dev,int port); +void (*phy_setup)(struct ksz_device *dev,int port, +struct phy_device *phy);void (*port_setup)(struct ksz_device *dev,int port,bool cpu_port);void (*r_phy)(struct ksz_device *dev,u16 phy,u16 reg,u16 *val);void (*w_phy)(struct ksz_device *dev,u16 phy,u16 reg,u16 val);
Signature · 1 line
1.9.1
PATCH v2 2/4 net: dsa: microchip: add MIB counter reading support
In reply to [email protected]From: Tristram Ha <[email protected]>
Add background MIB counter reading support.
Port MIB counters should only be read when there is link. Otherwise it is a waste of time as hardware never increases those counters. There are exceptions as some switches keep track of dropped counts no matter waht.
--- drivers/net/dsa/microchip/ksz9477.c | 118 ++++++++++++++++++++------------ drivers/net/dsa/microchip/ksz_common.c | 121 +++++++++++++++++++++++++++++++++ drivers/net/dsa/microchip/ksz_common.h | 24 ++++++- drivers/net/dsa/microchip/ksz_priv.h | 9 ++- 4 files changed, 224 insertions(+), 48 deletions(-)
drivers/net/dsa/microchip/ksz9477.c +76 -42
@@ -18,8 +18,8 @@#include <net/switchdev.h> #include "ksz_priv.h" -#include "ksz_common.h" #include "ksz9477_reg.h" +#include "ksz_common.h" static const struct { int index;
@@ -259,6 +259,71 @@ static int ksz9477_reset_switch(struct ksz_device *dev)return 0 ; } +static void ksz9477_r_mib_cnt (struct ksz_device *dev,int port,u16 addr, +u64 *cnt) +{ +struct ksz_port *p = &dev->ports[port]; +u32 data; +int ret; + +/* retain the flush/freeze bit */ + data = p->freeze ?MIB_COUNTER_FLUSH_FREEZE :0 ; + data |=MIB_COUNTER_READ ; + data |= (addr <<MIB_COUNTER_INDEX_S ); +ksz_pwrite32 (dev, port,REG_PORT_MIB_CTRL_STAT__4 , data); + + ret =ksz_pread_poll_timeout (ksz_pread32, dev, port, +REG_PORT_MIB_CTRL_STAT__4 , data, + !(data &MIB_COUNTER_READ ),10 ,1000 ); + +/* failed to read MIB. get out of loop */ +if (ret <0 ) { +dev_dbg (dev->dev,"Failed to get MIB\n" ); +return ; + } + +/* count resets upon read */ +ksz_pread32 (dev, port,REG_PORT_MIB_DATA , &data); + *cnt += data; +} + +static void ksz9477_r_mib_pkt (struct ksz_device *dev,int port,u16 addr, +u64 *dropped,u64 *cnt) +{ + addr = ksz9477_mib_names[addr].index; +ksz9477_r_mib_cnt (dev, port, addr, cnt); +} + +static void ksz9477_freeze_mib (struct ksz_device *dev,int port,bool freeze) +{ +u32 val = freeze ?MIB_COUNTER_FLUSH_FREEZE :0 ; +struct ksz_port *p = &dev->ports[port]; + +/* enable/disable the port for flush/freeze function */ +mutex_lock (&p->mib.cnt_mutex); +ksz_pwrite32 (dev, port,REG_PORT_MIB_CTRL_STAT__4 , val); + +/* used by MIB counter reading code to know freeze is enabled */ + p->freeze = freeze; +mutex_unlock (&p->mib.cnt_mutex); +} + +static void ksz9477_port_init_cnt (struct ksz_device *dev,int port) +{ +struct ksz_port_mib *mib = &dev->ports[port].mib; + +/* flush all enabled port MIB counters */ +mutex_lock (&mib->cnt_mutex); +ksz_pwrite32 (dev, port,REG_PORT_MIB_CTRL_STAT__4 , +MIB_COUNTER_FLUSH_FREEZE ); +ksz_write8 (dev,REG_SW_MAC_CTRL_6 ,SW_MIB_COUNTER_FLUSH ); +ksz_pwrite32 (dev, port,REG_PORT_MIB_CTRL_STAT__4 ,0 ); +mutex_unlock (&mib->cnt_mutex); + + mib->cnt_ptr =0 ; +memset (mib->counters,0 , dev->mib_cnt *sizeof (u64)); +} +static enum dsa_tag_protocol ksz9477_get_tag_protocol (struct dsa_switch *ds,int port) {
@@ -342,47 +407,6 @@ static void ksz9477_get_strings(struct dsa_switch *ds, int port, } } -static void ksz_get_ethtool_stats (struct dsa_switch *ds,int port, -uint64_t *buf) -{ -struct ksz_device *dev = ds->priv; -int i; -u32 data; -int timeout; - -mutex_lock (&dev->stats_mutex); - -for (i =0 ; i <TOTAL_SWITCH_COUNTER_NUM ; i++) { - data =MIB_COUNTER_READ ; - data |= ((ksz9477_mib_names[i].index &0xFF ) << -MIB_COUNTER_INDEX_S ); -ksz_pwrite32 (dev, port,REG_PORT_MIB_CTRL_STAT__4 , data); - - timeout =1000 ; -do { -ksz_pread32 (dev, port,REG_PORT_MIB_CTRL_STAT__4 , - &data); -usleep_range (1 ,10 ); -if (!(data &MIB_COUNTER_READ )) -break ; - }while (timeout-- >0 ); - -/* failed to read MIB. get out of loop */ -if (!timeout) { -dev_dbg (dev->dev,"Failed to get MIB\n" ); -break ; - } - -/* count resets upon read */ -ksz_pread32 (dev, port,REG_PORT_MIB_DATA , &data); - - dev->mib_value[i] += (uint64_t )data; - buf[i] = dev->mib_value[i]; - } - -mutex_unlock (&dev->stats_mutex); -} -static void ksz9477_cfg_port_member (struct ksz_device *dev,int port,u8 member) {
@@ -1151,9 +1175,14 @@ static int ksz9477_setup(struct dsa_switch *ds)/* queue based egress rate limit */ ksz_cfg (dev,REG_SW_MAC_CTRL_5 ,SW_OUT_RATE_LIMIT_QUEUE_BASED , true); +/* enable global MIB counter freeze function */ +ksz_cfg (dev,REG_SW_MAC_CTRL_6 ,SW_MIB_COUNTER_FREEZE , true); +/* start switch */ ksz_cfg (dev,REG_SW_OPERATION ,SW_START , true); +ksz_init_mib_timer (dev); +return 0 ; }
@@ -1287,6 +1316,7 @@ static int ksz9477_switch_init(struct ksz_device *dev)if (!dev->ports)return -ENOMEM ;for (i =0 ; i < dev->mib_port_cnt; i++) { +mutex_init (&dev->ports[i].mib.cnt_mutex); dev->ports[i].mib.counters = devm_kzalloc(dev->dev, sizeof(u64) *
@@ -1311,6 +1341,10 @@ static void ksz9477_switch_exit(struct ksz_device *dev) .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table, .phy_setup = ksz9477_phy_setup, .port_setup = ksz9477_port_setup, + .r_mib_cnt = ksz9477_r_mib_cnt, + .r_mib_pkt = ksz9477_r_mib_pkt, + .freeze_mib = ksz9477_freeze_mib, + .port_init_cnt = ksz9477_port_init_cnt, .shutdown = ksz9477_reset_switch, .detect = ksz9477_switch_detect, .init = ksz9477_switch_init,
drivers/net/dsa/microchip/ksz_common.c +121 -0
139 lines not on this page · raw message
drivers/net/dsa/microchip/ksz_common.h +23 -1
@@ -1,19 +1,21 @@/* SPDX-License-Identifier: GPL-2.0 * Microchip switch driver common header * -* Copyright (C) 2017-2018 Microchip Technology Inc. +* Copyright (C) 2017-2019 Microchip Technology Inc. */ #ifndef __KSZ_COMMON_H#define __KSZ_COMMON_Hvoid ksz_update_port_member (struct ksz_device *dev,int port); +void ksz_init_mib_timer (struct ksz_device *dev);/* Common DSA access functions */ int ksz_phy_read16 (struct dsa_switch *ds,int addr,int reg);int ksz_phy_write16 (struct dsa_switch *ds,int addr,int reg,u16 val);int ksz_sset_count (struct dsa_switch *ds,int port,int sset); +void ksz_get_ethtool_stats (struct dsa_switch *ds,int port,uint64_t *buf);int ksz_port_bridge_join (struct dsa_switch *ds,int port,struct net_device *br); void ksz_port_bridge_leave(struct dsa_switch *ds, int port,
@@ -211,4 +213,24 @@ static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits, ksz_write8(dev, addr, data); } +/* Modify from readx_poll_timeout in iopoll.h. */ +#define ksz_pread_poll_timeout (op, dev, p, addr, val, cond, sleep_us, \ + timeout_us) \ +({ \ + ktime_t timeout = ktime_add_us(ktime_get(), timeout_us); \ + might_sleep_if(sleep_us); \ + for (;;) { \ + op(dev, p, addr, &(val)); \ + if (cond) \ + break; \ + if (timeout_us && ktime_compare(ktime_get(), timeout) > 0) { \ + op(dev, p, addr, &(val)); \ + break; \ + } \ + if (sleep_us) \ + usleep_range((sleep_us >> 2) + 1, sleep_us); \ + } \ + (cond) ? 0 : -ETIMEDOUT; \ +}) +#endif
drivers/net/dsa/microchip/ksz_priv.h +4 -5
@@ -14,8 +14,6 @@#include <linux/etherdevice.h> #include <net/dsa.h> -#include "ksz9477_reg.h" -struct ksz_io_ops ;struct vlan_table {
@@ -23,6 +21,7 @@ struct vlan_table { };struct ksz_port_mib { +struct mutex cnt_mutex;/* structure access */ u8 cnt_ptr;u64 *counters; };
@@ -38,7 +37,8 @@ struct ksz_port { u32 fiber:1; /* port is fiber */ u32 sgmii:1; /* port is SGMII */ u32 force:1; - u32 link_just_down:1; /* link just goes down */ + u32 read:1; /* read MIB counters in background */ + u32 freeze:1; /* MIB counter freeze is enabled */struct ksz_port_mib mib; };
@@ -79,8 +79,6 @@ struct ksz_device {struct vlan_table *vlan_cache; -u64 mib_value[TOTAL_SWITCH_COUNTER_NUM ]; -u8 *txbuf;struct ksz_port *ports;
@@ -153,6 +151,7 @@ struct ksz_dev_ops { u64 *cnt);void (*r_mib_pkt)(struct ksz_device *dev,int port,u16 addr,u64 *dropped,u64 *cnt); +void (*freeze_mib)(struct ksz_device *dev,int port,bool freeze);void (*port_init_cnt)(struct ksz_device *dev,int port);int (*shutdown)(struct ksz_device *dev);int (*detect)(struct ksz_device *dev);
Signature · 1 line
1.9.1
Andrew Lunn
maintainerRe: [PATCH v2 net-next 2/4] net: dsa: microchip: add MIB counter reading support
Quoted 20 lines
drivers/net/dsa/microchip/ksz_common.c +20
-0+static void mib_monitor (struct timer_list *t) +{ +struct ksz_device *dev =from_timer (dev, t, mib_read_timer); +const struct dsa_port *dp; +struct net_device *netdev; +struct ksz_port_mib *mib; +struct ksz_port *p; +int i; + +mod_timer (&dev->mib_read_timer, jiffies + dev->mib_read_interval); + +/* Check which port needs to read MIB counters. */ +for (i =0 ; i < dev->mib_port_cnt; i++) { + p = &dev->ports[i]; +if (!p->on) +continue ; + dp =dsa_to_port (dev->ds, i); + netdev = dp->slave; + + mib = &p->mib;
drivers/net/dsa/microchip/ksz_common.c void ksz_update_port_member(struct ksz_device *dev, int port) · line 117
Quoted 10 lines
drivers/net/dsa/microchip/ksz_common.c +10
-0+mutex_lock (&mib->cnt_mutex); + +/* Read only dropped counters when link is not up. */ +if (netdev && netdev->phydev && !netdev->phydev->link) + mib->cnt_ptr = dev->reg_mib_cnt; +mutex_unlock (&mib->cnt_mutex); + p->read = true; + } +schedule_work (&dev->mib_read); +}
Hi Tristram
This is much easier to understand. Thanks for making the change.
However, i suspect Florian was suggesting you use netif_carrier_ok(netdev), not poke around inside the phydev structure.
Andrew
Re: [PATCH v2 net-next 2/4] net: dsa: microchip: add MIB counter reading support
Quoted 33 lines · Andrew Lunn
Quoted 20 lines
drivers/net/dsa/microchip/ksz_common.c +20
-0+static void mib_monitor (struct timer_list *t) +{ +struct ksz_device *dev =from_timer (dev, t, mib_read_timer); +const struct dsa_port *dp; +struct net_device *netdev; +struct ksz_port_mib *mib; +struct ksz_port *p; +int i; + +mod_timer (&dev->mib_read_timer, jiffies + dev->mib_read_interval); + +/* Check which port needs to read MIB counters. */ +for (i =0 ; i < dev->mib_port_cnt; i++) { + p = &dev->ports[i]; +if (!p->on) +continue ; + dp =dsa_to_port (dev->ds, i); + netdev = dp->slave; + + mib = &p->mib;drivers/net/dsa/microchip/ksz_common.c void ksz_update_port_member(struct ksz_device *dev, int port) · line 117
Quoted 10 lines
drivers/net/dsa/microchip/ksz_common.c +10
-0+mutex_lock (&mib->cnt_mutex); + +/* Read only dropped counters when link is not up. */ +if (netdev && netdev->phydev && !netdev->phydev->link) + mib->cnt_ptr = dev->reg_mib_cnt; +mutex_unlock (&mib->cnt_mutex); + p->read = true; + } +schedule_work (&dev->mib_read); +}Hi Tristram
Quoted 4 lines
This is much easier to understand. Thanks for making the change.
However, i suspect Florian was suggesting you use
netif_carrier_ok(netdev), not poke around inside the phydev structure.
Yes indeed.
Signature · 1 line
Florian
PATCH v2 3/4 net: dsa: microchip: get port link status
In reply to [email protected]From: Tristram Ha <[email protected]>
Get port link status to know whether to read MIB counters when the link is going down. Add port_cleanup function to read MIB counters the last time as after the port is disabled the PHY is also powered down.
--- drivers/net/dsa/microchip/ksz9477.c | 2 ++ drivers/net/dsa/microchip/ksz_common.c | 39 ++++++++++++++++++++++++++++++++-- drivers/net/dsa/microchip/ksz_common.h | 3 +++ drivers/net/dsa/microchip/ksz_priv.h | 1 + 4 files changed, 43 insertions(+), 2 deletions(-)
drivers/net/dsa/microchip/ksz9477.c +2 -0
@@ -1191,6 +1191,7 @@ static int ksz9477_setup(struct dsa_switch *ds) .setup = ksz9477_setup, .phy_read = ksz9477_phy_read16, .phy_write = ksz9477_phy_write16, + .adjust_link = ksz_adjust_link, .port_enable = ksz_enable_port, .port_disable = ksz_disable_port, .get_strings = ksz9477_get_strings,
@@ -1340,6 +1341,7 @@ static void ksz9477_switch_exit(struct ksz_device *dev) .cfg_port_member = ksz9477_cfg_port_member, .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table, .phy_setup = ksz9477_phy_setup, + .port_cleanup = ksz_port_cleanup, .port_setup = ksz9477_port_setup, .r_mib_cnt = ksz9477_r_mib_cnt, .r_mib_pkt = ksz9477_r_mib_pkt,
drivers/net/dsa/microchip/ksz_common.c +37 -2
@@ -20,6 +20,22 @@#include "ksz_priv.h" +void ksz_port_cleanup (struct ksz_device *dev,int port) +{ +/* Read all MIB counters when the link is going down. */ +if (dev->live_ports & (1 << port)) { +struct ksz_port *p = &dev->ports[port]; + + p->read = true; +schedule_work (&dev->mib_read); + } + +/* Common code for port cleanup. */ + dev->on_ports &= ~(1 << port); + dev->live_ports &= ~(1 << port); +} +EXPORT_SYMBOL_GPL (ksz_port_cleanup); +void ksz_update_port_member (struct ksz_device *dev,int port) {struct ksz_port *p;
@@ -156,6 +172,26 @@ int ksz_phy_write16(struct dsa_switch *ds, int addr, int reg, u16 val) }EXPORT_SYMBOL_GPL (ksz_phy_write16); +void ksz_adjust_link (struct dsa_switch *ds,int port, +struct phy_device *phydev) +{ +struct ksz_device *dev = ds->priv; +struct ksz_port *p = &dev->ports[port]; + +if (!phydev->link) { +/* Read all MIB counters when the link is going down. */ +if (dev->live_ports & (1 << port)) { + p->read = true; +schedule_work (&dev->mib_read); + } + dev->live_ports &= ~(1 << port); + }else { +/* Remember which port is connected and active. */ + dev->live_ports |= (1 << port) & dev->on_ports; + } +} +EXPORT_SYMBOL_GPL (ksz_adjust_link); +int ksz_sset_count (struct dsa_switch *ds,int port,int sset) {struct ksz_device *dev = ds->priv;
@@ -367,8 +403,7 @@ void ksz_disable_port(struct dsa_switch *ds, int port, struct phy_device *phy) {struct ksz_device *dev = ds->priv; - dev->on_ports &= ~(1 << port); - dev->live_ports &= ~(1 << port); + dev->dev_ops->port_cleanup (dev, port);/* port_stp_state_set() will be called after to disable the port so * there is no need to do anything.
drivers/net/dsa/microchip/ksz_common.h +3 -0
@@ -7,6 +7,7 @@#ifndef __KSZ_COMMON_H#define __KSZ_COMMON_H +void ksz_port_cleanup (struct ksz_device *dev,int port);void ksz_update_port_member (struct ksz_device *dev,int port);void ksz_init_mib_timer (struct ksz_device *dev);
@@ -14,6 +15,8 @@int ksz_phy_read16 (struct dsa_switch *ds,int addr,int reg);int ksz_phy_write16 (struct dsa_switch *ds,int addr,int reg,u16 val); +void ksz_adjust_link (struct dsa_switch *ds,int port, +struct phy_device *phydev);int ksz_sset_count (struct dsa_switch *ds,int port,int sset);void ksz_get_ethtool_stats (struct dsa_switch *ds,int port,uint64_t *buf); int ksz_port_bridge_join(struct dsa_switch *ds, int port,
drivers/net/dsa/microchip/ksz_priv.h +1 -0
@@ -137,6 +137,7 @@ struct ksz_dev_ops {void (*flush_dyn_mac_table)(struct ksz_device *dev,int port);void (*phy_setup)(struct ksz_device *dev,int port,struct phy_device *phy); +void (*port_cleanup)(struct ksz_device *dev,int port);void (*port_setup)(struct ksz_device *dev,int port,bool cpu_port);void (*r_phy)(struct ksz_device *dev,u16 phy,u16 reg,u16 *val);void (*w_phy)(struct ksz_device *dev,u16 phy,u16 reg,u16 val);
Signature · 1 line
1.9.1
PATCH v2 4/4 net: dsa: microchip: remove unnecessary include headers
In reply to [email protected]From: Tristram Ha <[email protected]>
Remove unnecessary header include.
--- drivers/net/dsa/microchip/ksz9477.c | 4 ---- 1 file changed, 4 deletions(-)
drivers/net/dsa/microchip/ksz9477.c +0 -4
@@ -5,14 +5,10 @@* Copyright (C) 2017-2019 Microchip Technology Inc. */ -#include <linux/delay.h> -#include <linux/export.h> -#include <linux/gpio.h> #include <linux/kernel.h> #include <linux/module.h> #include <linux/platform_data/microchip-ksz.h> #include <linux/phy.h> -#include <linux/etherdevice.h> #include <linux/if_bridge.h> #include <net/dsa.h> #include <net/switchdev.h>
Signature · 1 line
1.9.1