From: Ajit Kumar <[email protected]>

Concurrent link status polling and PHY access can result in
simultaneous I2C/MDIO bus transactions. On systems with multiple
AXGBE ports this may lead to I2C transfer failures such as
-EAGAIN, -ENOTCONN, or -ETIMEDOUT and can cause incorrect link
status reporting.

Serialize access to shared PHY communication resources by
protecting PHY status updates and introducing a mutex around
I2C/MDIO ownership acquisition. Also add retry delays for
recoverable I2C transfer failures.

Signed-off-by: Ajit Kumar <[email protected]>
---
 drivers/net/axgbe/axgbe_ethdev.c   |  3 +++
 drivers/net/axgbe/axgbe_ethdev.h   |  1 +
 drivers/net/axgbe/axgbe_phy_impl.c | 18 ++++++++++++++++--
 3 files changed, 20 insertions(+), 2 deletions(-)

diff --git a/drivers/net/axgbe/axgbe_ethdev.c b/drivers/net/axgbe/axgbe_ethdev.c
index 2411e74d97..9e36186eca 100644
--- a/drivers/net/axgbe/axgbe_ethdev.c
+++ b/drivers/net/axgbe/axgbe_ethdev.c
@@ -760,7 +760,9 @@ axgbe_dev_link_update(struct rte_eth_dev *dev,
        PMD_INIT_FUNC_TRACE();
        rte_delay_ms(800);
 
+       pthread_mutex_lock(&pdata->link_status_mutex);
        pdata->phy_if.phy_status(pdata);
+       pthread_mutex_unlock(&pdata->link_status_mutex);
 
        memset(&link, 0, sizeof(struct rte_eth_link));
        link.link_duplex = pdata->phy.duplex;
@@ -2406,6 +2408,7 @@ eth_axgbe_dev_init(struct rte_eth_dev *eth_dev)
        rte_thread_mutex_init_shared(&pdata->i2c_mutex);
        rte_thread_mutex_init_shared(&pdata->an_mutex);
        rte_thread_mutex_init_shared(&pdata->phy_mutex);
+       rte_thread_mutex_init_shared(&pdata->link_status_mutex);
 
        ret = pdata->phy_if.phy_init(pdata);
        if (ret) {
diff --git a/drivers/net/axgbe/axgbe_ethdev.h b/drivers/net/axgbe/axgbe_ethdev.h
index 77c21469a8..5bbda1b684 100644
--- a/drivers/net/axgbe/axgbe_ethdev.h
+++ b/drivers/net/axgbe/axgbe_ethdev.h
@@ -654,6 +654,7 @@ struct axgbe_port {
        pthread_mutex_t i2c_mutex;
        pthread_mutex_t an_mutex;
        pthread_mutex_t phy_mutex;
+       pthread_mutex_t link_status_mutex;
 
        /* Flow control settings */
        unsigned int pause_autoneg;
diff --git a/drivers/net/axgbe/axgbe_phy_impl.c 
b/drivers/net/axgbe/axgbe_phy_impl.c
index 4abf5fdea9..43a258984f 100644
--- a/drivers/net/axgbe/axgbe_phy_impl.c
+++ b/drivers/net/axgbe/axgbe_phy_impl.c
@@ -531,6 +531,10 @@ static int axgbe_phy_redrv_write(struct axgbe_port *pdata, 
unsigned int reg,
        return ret;
 }
 
+#define AXGBE_I2C_RETRYABLE(_ret) \
+       ((_ret) == -EAGAIN || (_ret) == -ENOTCONN || (_ret) == -ETIMEDOUT)
+#define AXGBE_I2C_RETRY_DELAY_MS       2
+
 static int axgbe_phy_i2c_read(struct axgbe_port *pdata, unsigned int target,
                              void *reg, unsigned int reg_len,
                              void *val, unsigned int val_len)
@@ -547,8 +551,10 @@ static int axgbe_phy_i2c_read(struct axgbe_port *pdata, 
unsigned int target,
        i2c_op.buf = reg;
        ret = axgbe_phy_i2c_xfer(pdata, &i2c_op);
        if (ret) {
-               if ((ret == -EAGAIN) && retry--)
+               if (AXGBE_I2C_RETRYABLE(ret) && retry--) {
+                       rte_delay_ms(AXGBE_I2C_RETRY_DELAY_MS);
                        goto again1;
+       }
 
                return ret;
        }
@@ -561,8 +567,10 @@ static int axgbe_phy_i2c_read(struct axgbe_port *pdata, 
unsigned int target,
        i2c_op.len = val_len;
        i2c_op.buf = val;
        ret = axgbe_phy_i2c_xfer(pdata, &i2c_op);
-       if ((ret == -EAGAIN) && retry--)
+       if (ret && AXGBE_I2C_RETRYABLE(ret) && retry--) {
+               rte_delay_ms(AXGBE_I2C_RETRY_DELAY_MS);
                goto again2;
+       }
 
        return ret;
 }
@@ -605,9 +613,12 @@ static int axgbe_phy_sfp_get_mux(struct axgbe_port *pdata)
        return axgbe_phy_i2c_xfer(pdata, &i2c_op);
 }
 
+static pthread_mutex_t axgbe_i2c_mdio_bus_mutex = PTHREAD_MUTEX_INITIALIZER;
+
 static void axgbe_phy_put_comm_ownership(struct axgbe_port *pdata)
 {
        pthread_mutex_unlock(&pdata->phy_mutex);
+       pthread_mutex_unlock(&axgbe_i2c_mdio_bus_mutex);
 }
 
 static int axgbe_phy_get_comm_ownership(struct axgbe_port *pdata)
@@ -616,6 +627,8 @@ static int axgbe_phy_get_comm_ownership(struct axgbe_port 
*pdata)
        uint64_t timeout;
        unsigned int mutex_id;
 
+       pthread_mutex_lock(&axgbe_i2c_mdio_bus_mutex);
+
        /* The I2C and MDIO/GPIO bus is multiplexed between multiple devices,
         * the driver needs to take the software mutex and then the hardware
         * mutexes before being able to use the busses.
@@ -648,6 +661,7 @@ static int axgbe_phy_get_comm_ownership(struct axgbe_port 
*pdata)
        }
 
        pthread_mutex_unlock(&pdata->phy_mutex);
+       pthread_mutex_unlock(&axgbe_i2c_mdio_bus_mutex);
 
        PMD_DRV_LOG_LINE(ERR, "unable to obtain hardware mutexes");
 
-- 
2.43.0

Reply via email to