net: dsa: ksz: added the generic port_stp_state_set function
RFC PATCH 1/1 net: dsa: ksz: added the generic port_stp_state_set function
The ksz8795 and ksz9477 uses the same algorithm for the port_stp_state_set function except the register address is different. So moved the algorithm to the ksz_common.c and used the dev_ops for register read and write. This function can also used for the lan937x part. Hence making it generic for all the parts.
--- drivers/net/dsa/microchip/ksz8795.c | 37 ++++--------------------- drivers/net/dsa/microchip/ksz8795_reg.h | 3 -- drivers/net/dsa/microchip/ksz9477.c | 36 ++++-------------------- drivers/net/dsa/microchip/ksz9477_reg.h | 4 --- drivers/net/dsa/microchip/ksz_common.c | 37 +++++++++++++++++++++++++ drivers/net/dsa/microchip/ksz_common.h | 8 ++++++ 6 files changed, 55 insertions(+), 70 deletions(-)
drivers/net/dsa/microchip/ksz8795.c +5 -32
@@ -1025,42 +1025,14 @@ static void ksz8_cfg_port_member(struct ksz_device *dev, int port, u8 member)ksz_pwrite8 (dev, port,P_MIRROR_CTRL , data); } -static void ksz8_port_stp_state_set (struct dsa_switch *ds,int port,u8 state) +static void ksz8_cfg_stp_state (struct ksz_device *dev,int port,u8 val) { -struct ksz_device *dev = ds->priv; -struct ksz_port *p;u8 data;ksz_pread8 (dev, port,P_STP_CTRL , &data); data &= ~(PORT_TX_ENABLE |PORT_RX_ENABLE |PORT_LEARN_DISABLE ); - -switch (state) { -case BR_STATE_DISABLED : - data |=PORT_LEARN_DISABLE ; -break ; -case BR_STATE_LISTENING : - data |= (PORT_RX_ENABLE |PORT_LEARN_DISABLE ); -break ; -case BR_STATE_LEARNING : - data |=PORT_RX_ENABLE ; -break ; -case BR_STATE_FORWARDING : - data |= (PORT_TX_ENABLE |PORT_RX_ENABLE ); -break ; -case BR_STATE_BLOCKING : - data |=PORT_LEARN_DISABLE ; -break ; -default : -dev_err (ds->dev,"invalid STP state: %d\n" , state); -return ; - } - + data |= val;ksz_pwrite8 (dev, port,P_STP_CTRL , data); - - p = &dev->ports[port]; - p->stp_state = state; - -ksz_update_port_member (dev, port); }static void ksz8_flush_dyn_mac_table (struct ksz_device *dev,int port)
@@ -1385,7 +1357,7 @@ static void ksz8_config_cpu_port(struct dsa_switch *ds)for (i =0 ; i < dev->phy_port_cnt; i++) { p = &dev->ports[i]; -ksz8_port_stp_state_set (ds, i,BR_STATE_DISABLED ); +ksz_port_stp_state_set (ds, i,BR_STATE_DISABLED );/* Last port may be disabled. */ if (i == dev->phy_port_cnt)
@@ -1542,7 +1514,7 @@ static const struct dsa_switch_ops ksz8_switch_ops = { .get_sset_count = ksz_sset_count, .port_bridge_join = ksz_port_bridge_join, .port_bridge_leave = ksz_port_bridge_leave, - .port_stp_state_set = ksz8_port_stp_state_set, + .port_stp_state_set = ksz_port_stp_state_set, .port_fast_age = ksz_port_fast_age, .port_vlan_filtering = ksz8_port_vlan_filtering, .port_vlan_add = ksz8_port_vlan_add,
@@ -1761,6 +1733,7 @@ static const struct ksz_dev_ops ksz8_dev_ops = { .cfg_port_member = ksz8_cfg_port_member, .flush_dyn_mac_table = ksz8_flush_dyn_mac_table, .port_setup = ksz8_port_setup, + .cfg_stp_state = ksz8_cfg_stp_state, .r_phy = ksz8_r_phy, .w_phy = ksz8_w_phy, .r_dyn_mac_table = ksz8_r_dyn_mac_table,
drivers/net/dsa/microchip/ksz8795_reg.h +0 -3
@@ -160,9 +160,6 @@#define PORT_DISCARD_NON_VID BIT(5)#define PORT_FORCE_FLOW_CTRL BIT(4)#define PORT_BACK_PRESSURE BIT(3) -#define PORT_TX_ENABLE BIT(2) -#define PORT_RX_ENABLE BIT(1) -#define PORT_LEARN_DISABLE BIT(0)#define REG_PORT_1_CTRL_3 0x13#define REG_PORT_2_CTRL_3 0x23
drivers/net/dsa/microchip/ksz9477.c +5 -31
@@ -514,41 +514,14 @@ static void ksz9477_cfg_port_member(struct ksz_device *dev, int port,ksz_pwrite32 (dev, port,REG_PORT_VLAN_MEMBERSHIP__4 , member); } -static void ksz9477_port_stp_state_set (struct dsa_switch *ds,int port, -u8 state) +static void ksz9477_cfg_stp_state (struct ksz_device *dev,int port,u8 val) { -struct ksz_device *dev = ds->priv; -struct ksz_port *p = &dev->ports[port];u8 data;ksz_pread8 (dev, port,P_STP_CTRL , &data); data &= ~(PORT_TX_ENABLE |PORT_RX_ENABLE |PORT_LEARN_DISABLE ); - -switch (state) { -case BR_STATE_DISABLED : - data |=PORT_LEARN_DISABLE ; -break ; -case BR_STATE_LISTENING : - data |= (PORT_RX_ENABLE |PORT_LEARN_DISABLE ); -break ; -case BR_STATE_LEARNING : - data |=PORT_RX_ENABLE ; -break ; -case BR_STATE_FORWARDING : - data |= (PORT_TX_ENABLE |PORT_RX_ENABLE ); -break ; -case BR_STATE_BLOCKING : - data |=PORT_LEARN_DISABLE ; -break ; -default : -dev_err (ds->dev,"invalid STP state: %d\n" , state); -return ; - } - + data |= val;ksz_pwrite8 (dev, port,P_STP_CTRL , data); - p->stp_state = state; - -ksz_update_port_member (dev, port); }static void ksz9477_flush_dyn_mac_table (struct ksz_device *dev,int port)
@@ -1404,7 +1377,7 @@ static void ksz9477_config_cpu_port(struct dsa_switch *ds)continue ; p = &dev->ports[i]; -ksz9477_port_stp_state_set (ds, i,BR_STATE_DISABLED ); +ksz_port_stp_state_set (ds, i,BR_STATE_DISABLED ); p->on =1 ;if (i < dev->phy_port_cnt) p->phy =1 ;
@@ -1481,7 +1454,7 @@ static const struct dsa_switch_ops ksz9477_switch_ops = { .get_sset_count = ksz_sset_count, .port_bridge_join = ksz_port_bridge_join, .port_bridge_leave = ksz_port_bridge_leave, - .port_stp_state_set = ksz9477_port_stp_state_set, + .port_stp_state_set = ksz_port_stp_state_set, .port_fast_age = ksz_port_fast_age, .port_vlan_filtering = ksz9477_port_vlan_filtering, .port_vlan_add = ksz9477_port_vlan_add,
@@ -1682,6 +1655,7 @@ static const struct ksz_dev_ops ksz9477_dev_ops = { .cfg_port_member = ksz9477_cfg_port_member, .flush_dyn_mac_table = ksz9477_flush_dyn_mac_table, .port_setup = ksz9477_port_setup, + .cfg_stp_state = ksz9477_cfg_stp_state, .r_mib_cnt = ksz9477_r_mib_cnt, .r_mib_pkt = ksz9477_r_mib_pkt, .r_mib_stat64 = ksz9477_r_mib_stats64,
drivers/net/dsa/microchip/ksz9477_reg.h +0 -4
@@ -1586,10 +1586,6 @@#define REG_PORT_LUE_MSTP_STATE 0x0B04 -#define PORT_TX_ENABLE BIT(2) -#define PORT_RX_ENABLE BIT(1) -#define PORT_LEARN_DISABLE BIT(0) -/* C - PTP */ #define REG_PTP_PORT_RX_DELAY__2 0x0C00
drivers/net/dsa/microchip/ksz_common.c +37 -0
@@ -372,6 +372,43 @@ int ksz_enable_port(struct dsa_switch *ds, int port, struct phy_device *phy) }EXPORT_SYMBOL_GPL (ksz_enable_port); +void ksz_port_stp_state_set (struct dsa_switch *ds,int port, +u8 state) +{ +struct ksz_device *dev = ds->priv; +struct ksz_port *p; +u8 data; + +switch (state) { +case BR_STATE_DISABLED : + data =PORT_LEARN_DISABLE ; +break ; +case BR_STATE_LISTENING : + data = (PORT_RX_ENABLE |PORT_LEARN_DISABLE ); +break ; +case BR_STATE_LEARNING : + data =PORT_RX_ENABLE ; +break ; +case BR_STATE_FORWARDING : + data = (PORT_TX_ENABLE |PORT_RX_ENABLE ); +break ; +case BR_STATE_BLOCKING : + data =PORT_LEARN_DISABLE ; +break ; +default : +dev_err (ds->dev,"invalid STP state: %d\n" , state); +return ; + } + + dev->dev_ops->cfg_stp_state (dev, port, data); + + p = &dev->ports[port]; + p->stp_state = state; + +ksz_update_port_member (dev, port); +} +EXPORT_SYMBOL_GPL (ksz_port_stp_state_set); +struct ksz_device *ksz_switch_alloc (struct device *base,void *priv) {struct dsa_switch *ds;
drivers/net/dsa/microchip/ksz_common.h +8 -0
@@ -119,6 +119,7 @@ struct ksz_dev_ops {void (*flush_dyn_mac_table)(struct ksz_device *dev,int port);void (*port_cleanup)(struct ksz_device *dev,int port);void (*port_setup)(struct ksz_device *dev,int port,bool cpu_port); +void (*cfg_stp_state)(struct ksz_device *dev,int port,u8 state);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); int (*r_dyn_mac_table)(struct ksz_device *dev, u16 addr, u8 *mac_addr,
@@ -165,6 +166,8 @@ int ksz_port_bridge_join(struct dsa_switch *ds, int port, struct netlink_ext_ack *extack);void ksz_port_bridge_leave (struct dsa_switch *ds,int port,struct dsa_bridge bridge); +void ksz_port_stp_state_set (struct dsa_switch *ds,int port, +u8 state);void ksz_port_fast_age (struct dsa_switch *ds,int port);int ksz_port_fdb_dump (struct dsa_switch *ds,int port,dsa_fdb_dump_cb_t *cb,void *data);
@@ -292,6 +295,11 @@ static inline void ksz_regmap_unlock(void *__mtx)mutex_unlock (mtx); } +/* STP State Defines */ +#define PORT_TX_ENABLE BIT(2) +#define PORT_RX_ENABLE BIT(1) +#define PORT_LEARN_DISABLE BIT(0) +/* Regmap tables generation */ #define KSZ_SPI_OP_RD 3#define KSZ_SPI_OP_WR 2
base-commit: cc4bdef26ecd56de16a04bc6d99aa10ff9076498
Signature · 1 line
2.33.0
Vladimir Oltean
maintainerQuoted 8 lines · Arun Ramadoss
The ksz8795 and ksz9477 uses the same algorithm for the
port_stp_state_setfunction except the register address is different. So moved the algorithm to theksz_common.cand used thedev_opsfor register read and write. This function can also used for the lan937x part. Hence making it generic for all the parts.Signed-off-by Arun Ramadoss <[email protected]>---
If the entire port STP state change procedure is the same, just a register offset is different, can you not create a common STP state procedure that takes the register offset as argument, and gets called with different offset arguments from ksz8795.c and from ksz9477.c?
On Fri, 2022-04-22 at 20:01 +0300, Vladimir Oltean wrote:
Hi Vladimir,
Quoted 12 lines
EXTERNAL EMAIL: Do not click links or open attachments unless you know the content is safe
Quoted 8 lines · Arun Ramadoss
The ksz8795 and ksz9477 uses the same algorithm for the
port_stp_state_setfunction except the register address is different. So
moved the algorithm to theksz_common.cand used thedev_opsfor register read and write. This function can also used for the lan937x
part. Hence making it generic for all the parts.
Quoted 7 lines
Quoted 2 lines
Signed-off-by Arun Ramadoss <[email protected]>---
If the entire port STP state change procedure is the same, just a register offset is different, can you not create a common STP state procedure that takes the register offset as argument, and gets called with different offset arguments from
ksz8795.cand fromksz9477.c?
Thanks for comment.
I will update the Patch by adding the register address as function argument and repost.