From: Ajit Kumar <[email protected]>

A port's first I2C access to its SFP cage can fail right after
axgbe_phy_init(), even alone on the bus: a quick NACK (-ENOTCONN) or
a full stall until axgbe_i2c_xfer()'s own 1s timeout (-ETIMEDOUT).
This is separate from the cross-port race fixed by
axgbe_i2c_mdio_bus_mutex; it shows up on a single port with no
sibling active, so serializing the bus doesn't help.

axgbe_phy_i2c_read() only retried -EAGAIN. Retry -ENOTCONN and
-ETIMEDOUT too, with a short settle delay, but only until the port's
bus has one successful transfer since phy_init(). After that, a
repeat is a real fault, not a settling device, and shouldn't cost
every later poll an extra second while holding the shared mutex.

Validated on AMD CRB V3C48 and Advantech FWA-1081: 0 I2C errors
across 30 iterations of the cross-port repro plus a solo-access
repro that now links up cleanly on both; both failed every time
without this change.

Depends on axgbe_i2c_mdio_bus_mutex from "net/axgbe: serialize PHY
and link status access" for the cross-port case; otherwise applies
independently.

Fixes: a5c7273771e8 ("net/axgbe: add phy programming APIs")
Cc: [email protected]

Signed-off-by: Ajit Kumar <[email protected]>
---
v2:
 - the extended retry wasn't actually limited to the first access as
   claimed; nothing tracked that state, so a persistent fault paid
   the cost on every poll. Added a per-port settled flag to gate it
   correctly (per DPDK AI code-review bot).
 - switched the delay to rte_delay_us_sleep(), a real sleep, since it
   can now be held more than once.
 - cut the in-code comment, which just repeated this commit log
   (per Stephen Hemminger's review).
---
 drivers/net/axgbe/axgbe_phy_impl.c | 38 ++++++++++++++++++++++++++----
 1 file changed, 34 insertions(+), 4 deletions(-)

diff --git a/drivers/net/axgbe/axgbe_phy_impl.c 
b/drivers/net/axgbe/axgbe_phy_impl.c
index 55d45bfb7b..57bd1f64d4 100644
--- a/drivers/net/axgbe/axgbe_phy_impl.c
+++ b/drivers/net/axgbe/axgbe_phy_impl.c
@@ -223,6 +223,7 @@ struct axgbe_phy_data {
        unsigned int sfp_tx_fault;
        unsigned int sfp_mod_absent;
        unsigned int sfp_changed;
+       unsigned int sfp_i2c_settled;
        unsigned int sfp_phy_avail;
        unsigned int sfp_cable_len;
        enum axgbe_sfp_base sfp_base;
@@ -531,10 +532,29 @@ static int axgbe_phy_redrv_write(struct axgbe_port 
*pdata, unsigned int reg,
        return ret;
 }
 
+/*
+ * The SFP cage mux/GPIO expander may not respond to the first access
+ * after phy_init(), either NACKing (-ENOTCONN) or stalling until the
+ * transfer times out (-ETIMEDOUT). Retry those as well as
+ * arbitration loss (-EAGAIN) after a short delay, but only until the
+ * first successful transfer; after that it's a real fault, not a
+ * settling device.
+ */
+static inline bool axgbe_i2c_retryable(int ret, bool settled)
+{
+       if (ret == -EAGAIN)
+               return true;
+
+       return !settled && (ret == -ENOTCONN || ret == -ETIMEDOUT);
+}
+
+#define AXGBE_I2C_RETRY_DELAY_US       2000
+
 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)
 {
+       struct axgbe_phy_data *phy_data = pdata->phy_data;
        struct axgbe_i2c_op i2c_op;
        int retry, ret;
 
@@ -547,8 +567,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, phy_data->sfp_i2c_settled) && 
retry--) {
+                       rte_delay_us_sleep(AXGBE_I2C_RETRY_DELAY_US);
                        goto again1;
+               }
 
                return ret;
        }
@@ -561,10 +583,18 @@ 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--)
-               goto again2;
+       if (ret) {
+               if (axgbe_i2c_retryable(ret, phy_data->sfp_i2c_settled) && 
retry--) {
+                       rte_delay_us_sleep(AXGBE_I2C_RETRY_DELAY_US);
+                       goto again2;
+               }
 
-       return ret;
+               return ret;
+       }
+
+       phy_data->sfp_i2c_settled = 1;
+
+       return 0;
 }
 
 static int axgbe_phy_sfp_put_mux(struct axgbe_port *pdata)
-- 
2.43.0

Reply via email to