net: dsa: microchip: Add control bus error handling
RFC PATCH 1/1 net: dsa: microchip: Add control bus error handling
This patch adds error handling for ksz_{read,write}{8,16,32} functions, which was completely missing from the original driver. While a failure of the control SPI/I2C bus during operation is unlikely, it does happen and thus is good to handle it instead of doing something undefined.
This patch only fixes part of the problem for the aforementioned functions, it does not fix ksz_cfg() calls and it does not fix ksz_p{read,write} either, these are fixes in subsequent patches. This is because the amount of changes in the driver in this patch is already huge, hence the split.
Signed-off-by Marek Vasut <[email protected]>
Cc Andrew Lunn <[email protected]>
Cc David S. Miller <[email protected]>
Cc Florian Fainelli <[email protected]>
Cc George McCollister <[email protected]>
Cc Tristram Ha <[email protected]>
Cc Woojung Huh <[email protected]>
--- drivers/net/dsa/microchip/ksz8795.c | 142 ++++++++++---- drivers/net/dsa/microchip/ksz9477.c | 260 ++++++++++++++++++------- drivers/net/dsa/microchip/ksz_common.h | 12 +- 3 files changed, 304 insertions(+), 110 deletions(-)
drivers/net/dsa/microchip/ksz8795.c +105 -37
@@ -76,12 +76,15 @@ static void ksz_port_cfg(struct ksz_device *dev, int port, int offset, u8 bits,static int ksz8795_reset_switch (struct ksz_device *dev) { +int ret; +/* reset switch */ -ksz_write8 (dev,REG_POWER_MANAGEMENT_1 , -SW_SOFTWARE_POWER_DOWN <<SW_POWER_MANAGEMENT_MODE_S ); -ksz_write8 (dev,REG_POWER_MANAGEMENT_1 ,0 ); + ret =ksz_write8 (dev,REG_POWER_MANAGEMENT_1 , +SW_SOFTWARE_POWER_DOWN <<SW_POWER_MANAGEMENT_MODE_S ); +if (ret) +return ret; -return 0 ; +return ksz_write8 (dev,REG_POWER_MANAGEMENT_1 ,0 ); }static void ksz8795_set_prio_queue (struct ksz_device *dev,int port,int queue)
@@ -117,44 +120,55 @@ static void ksz8795_set_prio_queue(struct ksz_device *dev, int port, int queue) true); } -static void ksz8795_r_mib_cnt (struct ksz_device *dev,int port,u16 addr, -u64 *cnt) +static int ksz8795_r_mib_cnt (struct ksz_device *dev,int port,u16 addr, +u64 *cnt) {u16 ctrl_addr;u32 data;u8 check; -int loop; +int ret, loop; ctrl_addr = addr +SWITCH_COUNTER_NUM * port; ctrl_addr |=IND_ACC_TABLE (TABLE_MIB |TABLE_READ );mutex_lock (&dev->alu_mutex); -ksz_write16 (dev,REG_IND_CTRL_0 , ctrl_addr); + ret =ksz_write16 (dev,REG_IND_CTRL_0 , ctrl_addr); +if (ret) + goto exit;/* It is almost guaranteed to always read the valid bit because of * slow SPI speed. */ for (loop =2 ; loop >0 ; loop--) { -ksz_read8 (dev,REG_IND_MIB_CHECK , &check); + ret =ksz_read8 (dev,REG_IND_MIB_CHECK , &check); +if (ret) + goto exit;if (check &MIB_COUNTER_VALID ) { -ksz_read32 (dev,REG_IND_DATA_LO , &data); + ret =ksz_read32 (dev,REG_IND_DATA_LO , &data); +if (ret) + goto exit; +if (check &MIB_COUNTER_OVERFLOW ) *cnt +=MIB_COUNTER_VALUE +1 ; *cnt += data &MIB_COUNTER_VALUE ;break ; } } + +exit:mutex_unlock (&dev->alu_mutex); + +return ret; } -static void ksz8795_r_mib_pkt (struct ksz_device *dev,int port,u16 addr, -u64 *dropped,u64 *cnt) +static int ksz8795_r_mib_pkt (struct ksz_device *dev,int port,u16 addr, +u64 *dropped,u64 *cnt) { +int ret, loop;u16 ctrl_addr;u32 data;u8 check; -int loop; addr -=SWITCH_COUNTER_NUM ; ctrl_addr = (KS_MIB_TOTAL_RX_1 -KS_MIB_TOTAL_RX_0 ) * port;
@@ -162,16 +176,23 @@ static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr, ctrl_addr |=IND_ACC_TABLE (TABLE_MIB |TABLE_READ );mutex_lock (&dev->alu_mutex); -ksz_write16 (dev,REG_IND_CTRL_0 , ctrl_addr); + ret =ksz_write16 (dev,REG_IND_CTRL_0 , ctrl_addr); +if (ret) + goto exit;/* It is almost guaranteed to always read the valid bit because of * slow SPI speed. */ for (loop =2 ; loop >0 ; loop--) { -ksz_read8 (dev,REG_IND_MIB_CHECK , &check); + ret =ksz_read8 (dev,REG_IND_MIB_CHECK , &check); +if (ret) + goto exit;if (check &MIB_COUNTER_VALID ) { -ksz_read32 (dev,REG_IND_DATA_LO , &data); + ret =ksz_read32 (dev,REG_IND_DATA_LO , &data); +if (ret) + goto exit; +if (addr <2 ) {u64 total;
@@ -192,7 +213,10 @@ static void ksz8795_r_mib_pkt(struct ksz_device *dev, int port, u16 addr,break ; } } + +exit:mutex_unlock (&dev->alu_mutex); +return ret; }static void ksz8795_freeze_mib (struct ksz_device *dev,int port,bool freeze)
@@ -207,7 +231,7 @@ static void ksz8795_freeze_mib(struct ksz_device *dev, int port, bool freeze)ksz_cfg (dev,REG_SW_CTRL_6 ,BIT (port), false); } -static void ksz8795_port_init_cnt (struct ksz_device *dev,int port) +static int ksz8795_port_init_cnt (struct ksz_device *dev,int port) {struct ksz_port_mib *mib = &dev->ports[port].mib;
@@ -233,40 +257,56 @@ static void ksz8795_port_init_cnt(struct ksz_device *dev, int port) } mib->cnt_ptr =0 ;memset (mib->counters,0 , dev->mib_cnt *sizeof (u64)); + +return 0 ; } -static void ksz8795_r_table (struct ksz_device *dev,int table,u16 addr, -u64 *data) +static int ksz8795_r_table (struct ksz_device *dev,int table,u16 addr, +u64 *data) {u16 ctrl_addr; +int ret; ctrl_addr =IND_ACC_TABLE (table |TABLE_READ ) | addr;mutex_lock (&dev->alu_mutex); -ksz_write16 (dev,REG_IND_CTRL_0 , ctrl_addr); -ksz_read64 (dev,REG_IND_DATA_HI , data); + ret =ksz_write16 (dev,REG_IND_CTRL_0 , ctrl_addr); +if (!ret) + ret =ksz_read64 (dev,REG_IND_DATA_HI , data); +mutex_unlock (&dev->alu_mutex); + +return ret; } -static void ksz8795_w_table (struct ksz_device *dev,int table,u16 addr, -u64 data) +static int ksz8795_w_table (struct ksz_device *dev,int table,u16 addr, +u64 data) {u16 ctrl_addr; +int ret; ctrl_addr =IND_ACC_TABLE (table) | addr;mutex_lock (&dev->alu_mutex); -ksz_write64 (dev,REG_IND_DATA_HI , data); -ksz_write16 (dev,REG_IND_CTRL_0 , ctrl_addr); + ret =ksz_write64 (dev,REG_IND_DATA_HI , data); +if (!ret) + ret =ksz_write16 (dev,REG_IND_CTRL_0 , ctrl_addr); +mutex_unlock (&dev->alu_mutex); + +return ret; }static int ksz8795_valid_dyn_entry (struct ksz_device *dev,u8 *data) {int timeout =100 ; +int ret;do { -ksz_read8 (dev,REG_IND_DATA_CHECK , data); + ret =ksz_read8 (dev,REG_IND_DATA_CHECK , data); +if (ret) +return ret; + timeout--; }while ((*data &DYNAMIC_MAC_TABLE_NOT_READY ) && timeout);
@@ -275,7 +315,9 @@ static int ksz8795_valid_dyn_entry(struct ksz_device *dev, u8 *data)return -EAGAIN ;/* Entry is ready for accessing. */ }else { -ksz_read8 (dev,REG_IND_DATA_8 , data); + ret =ksz_read8 (dev,REG_IND_DATA_8 , data); +if (ret) +return ret;/* There is no valid entry in the table. */ if (*data &DYNAMIC_MAC_TABLE_MAC_EMPTY )
@@ -296,7 +338,9 @@ static int ksz8795_r_dyn_mac_table(struct ksz_device *dev, u16 addr, ctrl_addr =IND_ACC_TABLE (TABLE_DYNAMIC_MAC |TABLE_READ ) | addr;mutex_lock (&dev->alu_mutex); -ksz_write16 (dev,REG_IND_CTRL_0 , ctrl_addr); + rc =ksz_write16 (dev,REG_IND_CTRL_0 , ctrl_addr); +if (rc) + goto exit; rc =ksz8795_valid_dyn_entry (dev, &data);if (rc == -EAGAIN ) {
@@ -309,7 +353,10 @@ static int ksz8795_r_dyn_mac_table(struct ksz_device *dev, u16 addr,u64 buf =0 ;int cnt; -ksz_read64 (dev,REG_IND_DATA_HI , &buf); + rc =ksz_read64 (dev,REG_IND_DATA_HI , &buf); +if (rc) + goto exit; + data_hi = (u32)(buf >>32 ); data_lo = (u32 )buf;
@@ -336,6 +383,8 @@ static int ksz8795_r_dyn_mac_table(struct ksz_device *dev, u16 addr, mac_addr[0 ] = (u8)(data_hi >>8 ); rc =0 ; } + +exit:mutex_unlock (&dev->alu_mutex);return rc;
@@ -920,10 +969,11 @@ static void ksz8795_port_mirror_del(struct dsa_switch *ds, int port, PORT_MIRROR_SNIFFER, false); } -static void ksz8795_port_setup (struct ksz_device *dev,int port,bool cpu_port) +static int ksz8795_port_setup (struct ksz_device *dev,int port,bool cpu_port) {struct ksz_port *p = &dev->ports[port];u8 data8, member; +int ret;/* enable broadcast storm limit */ ksz_port_cfg (dev, port,P_BCAST_STORM_CTRL ,PORT_BROADCAST_STORM , true);
@@ -941,7 +991,10 @@ static void ksz8795_port_setup(struct ksz_device *dev, int port, bool cpu_port)if (cpu_port) {/* Configure MII interface for proper network communication. */ -ksz_read8 (dev,REG_PORT_5_CTRL_6 , &data8); + ret =ksz_read8 (dev,REG_PORT_5_CTRL_6 , &data8); +if (ret) +return ret; + data8 &= ~PORT_INTERFACE_TYPE ; data8 &= ~PORT_GMII_1GPS_MODE ;switch (dev->interface) {
@@ -971,7 +1024,10 @@ static void ksz8795_port_setup(struct ksz_device *dev, int port, bool cpu_port) p->phydev.speed =SPEED_1000 ;break ; } -ksz_write8 (dev,REG_PORT_5_CTRL_6 , data8); + ret =ksz_write8 (dev,REG_PORT_5_CTRL_6 , data8); +if (ret) +return ret; + p->phydev.duplex =1 ; member = dev->port_mask;
@@ -986,14 +1042,16 @@ static void ksz8795_port_setup(struct ksz_device *dev, int port, bool cpu_port) dev->live_ports |=BIT (port); }ksz8795_cfg_port_member (dev, port, member); + +return 0 ; } -static void ksz8795_config_cpu_port (struct dsa_switch *ds) +static int ksz8795_config_cpu_port (struct dsa_switch *ds) {struct ksz_device *dev = ds->priv;struct ksz_port *p; +int i, ret;u8 remote; -int i; ds->num_ports = dev->port_cnt +1 ;
@@ -1005,7 +1063,10 @@ static void ksz8795_config_cpu_port(struct dsa_switch *ds) p->vid_member = dev->port_mask; p->on =1 ; -ksz8795_port_setup (dev, dev->cpu_port, true); + ret =ksz8795_port_setup (dev, dev->cpu_port, true); +if (ret) +return ret; + dev->member = dev->host_mask;for (i =0 ; i <SWITCH_PORT_NUM ; i++) {
@@ -1038,6 +1099,8 @@ static void ksz8795_config_cpu_port(struct dsa_switch *ds)ksz_port_cfg (dev, i,P_STP_CTRL ,PORT_FORCE_FLOW_CTRL , false); } + +return 0 ; }static int ksz8795_setup (struct dsa_switch *ds)
@@ -1074,7 +1137,9 @@ static int ksz8795_setup(struct dsa_switch *ds)UNICAST_VLAN_BOUNDARY |NO_EXC_COLLISION_DROP , UNICAST_VLAN_BOUNDARY | NO_EXC_COLLISION_DROP); -ksz8795_config_cpu_port (ds); + ret =ksz8795_config_cpu_port (ds); +if (ret) +return ret;ksz_cfg (dev,REG_SW_CTRL_2 ,MULTICAST_STORM_DISABLE , true);
@@ -1162,7 +1227,10 @@ static int ksz8795_switch_detect(struct ksz_device *dev)u8 val; id2 =0x95 ; -ksz_read8 (dev,REG_PORT_1_STATUS_0 , &val); + ret =ksz_read8 (dev,REG_PORT_1_STATUS_0 , &val); +if (ret) +return ret; +if (val &PORT_FIBER_MODE ) id2 =0x65 ; }else if (id2 ==CHIP_ID_94 ) {
drivers/net/dsa/microchip/ksz9477.c +193 -67
483 lines not on this page · raw message
drivers/net/dsa/microchip/ksz_common.h +6 -6
@@ -123,7 +123,7 @@ struct ksz_dev_ops {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); +int (*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); int (*r_dyn_mac_table)(struct ksz_device *dev, u16 addr, u8 *mac_addr,
@@ -133,12 +133,12 @@ struct ksz_dev_ops { struct alu_struct *alu);void (*w_sta_mac_table)(struct ksz_device *dev,u16 addr,struct alu_struct *alu); -void (*r_mib_cnt)(struct ksz_device *dev,int port,u16 addr, -u64 *cnt); -void (*r_mib_pkt)(struct ksz_device *dev,int port,u16 addr, -u64 *dropped,u64 *cnt); +int (*r_mib_cnt)(struct ksz_device *dev,int port,u16 addr, +u64 *cnt); +int (*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 (*port_init_cnt)(struct ksz_device *dev,int port);int (*shutdown)(struct ksz_device *dev);int (*detect)(struct ksz_device *dev);int (*init)(struct ksz_device *dev);
Signature · 1 line
2.23.0