mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
From: Pawel Dembicki <paweldembicki@gmail.com>
To: netdev@vger.kernel.org
Cc: Pawel Dembicki <paweldembicki@gmail.com>,
	Andrew Lunn <andrew@lunn.ch>,
	Florian Fainelli <f.fainelli@gmail.com>,
	Vladimir Oltean <olteanv@gmail.com>,
	"David S. Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Heiner Kallweit <hkallweit1@gmail.com>,
	Russell King <linux@armlinux.org.uk>,
	"Russell King (Oracle)" <rmk+kernel@armlinux.org.uk>,
	Linus Walleij <linus.walleij@linaro.org>,
	linux-kernel@vger.kernel.org
Subject: [PATCH net-next 5/9] net: dsa: vsc73xx: use mutex to mdio operations
Date: Mon, 29 Jul 2024 23:06:11 +0200	[thread overview]
Message-ID: <20240729210615.279952-6-paweldembicki@gmail.com> (raw)
In-Reply-To: <20240729210615.279952-1-paweldembicki@gmail.com>

vsc73xx needs mutex during mdio bus access to avoid races. Without it,
phys are misconfigured and bus operations aren't work as expected.

Signed-off-by: Pawel Dembicki <paweldembicki@gmail.com>
---
 drivers/net/dsa/vitesse-vsc73xx-core.c | 59 ++++++++++++++++++++------
 drivers/net/dsa/vitesse-vsc73xx.h      |  2 +
 2 files changed, 47 insertions(+), 14 deletions(-)

diff --git a/drivers/net/dsa/vitesse-vsc73xx-core.c b/drivers/net/dsa/vitesse-vsc73xx-core.c
index 5eb37dee2261..40c64ef7e729 100644
--- a/drivers/net/dsa/vitesse-vsc73xx-core.c
+++ b/drivers/net/dsa/vitesse-vsc73xx-core.c
@@ -545,6 +545,18 @@ static int vsc73xx_detect(struct vsc73xx *vsc)
 	return 0;
 }
 
+static int vsc73xx_mdio_busy_check(struct vsc73xx *vsc)
+{
+	int val, ret;
+
+	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MII, VSC73XX_BLOCK_MII_INTERNAL,
+			   VSC73XX_MII_STAT, &val);
+	if (ret)
+		return ret;
+
+	return (val & VSC73XX_MII_STAT_BUSY) ? -EBUSY : 0;
+}
+
 static int vsc73xx_phy_read(struct dsa_switch *ds, int phy, int regnum)
 {
 	struct vsc73xx *vsc = ds->priv;
@@ -552,6 +564,12 @@ static int vsc73xx_phy_read(struct dsa_switch *ds, int phy, int regnum)
 	u32 val;
 	int ret;
 
+	mutex_lock(&vsc->mdio_lock);
+
+	ret = vsc73xx_mdio_busy_check(vsc);
+	if (ret)
+		goto err;
+
 	/* Setting bit 26 means "read" */
 	cmd = VSC73XX_MII_CMD_OPERATION |
 	      FIELD_PREP(VSC73XX_MII_CMD_PHY_ADDR, phy) |
@@ -559,23 +577,27 @@ static int vsc73xx_phy_read(struct dsa_switch *ds, int phy, int regnum)
 	ret = vsc73xx_write(vsc, VSC73XX_BLOCK_MII, VSC73XX_BLOCK_MII_INTERNAL,
 			    VSC73XX_MII_CMD, cmd);
 	if (ret)
-		return ret;
-	msleep(2);
+		goto err;
+	usleep_range(100, 200);
 	ret = vsc73xx_read(vsc, VSC73XX_BLOCK_MII, VSC73XX_BLOCK_MII_INTERNAL,
 			   VSC73XX_MII_DATA, &val);
 	if (ret)
-		return ret;
+		goto err;
+
 	if (val & VSC73XX_MII_DATA_FAILURE) {
 		dev_err(vsc->dev, "reading reg %02x from phy%d failed\n",
 			regnum, phy);
-		return -EIO;
+		ret = -EIO;
+		goto err;
 	}
-	val &= VSC73XX_MII_DATA_READ_DATA;
+	ret = val & VSC73XX_MII_DATA_READ_DATA;
 
 	dev_dbg(vsc->dev, "read reg %02x from phy%d = %04x\n",
-		regnum, phy, val);
+		regnum, phy, ret);
 
-	return val;
+err:
+	mutex_unlock(&vsc->mdio_lock);
+	return ret;
 }
 
 static int vsc73xx_phy_write(struct dsa_switch *ds, int phy, int regnum,
@@ -583,7 +605,13 @@ static int vsc73xx_phy_write(struct dsa_switch *ds, int phy, int regnum,
 {
 	struct vsc73xx *vsc = ds->priv;
 	u32 cmd;
-	int ret;
+	int ret = 0;
+
+	mutex_lock(&vsc->mdio_lock);
+
+	ret = vsc73xx_mdio_busy_check(vsc);
+	if (ret)
+		goto err;
 
 	/* It was found through tedious experiments that this router
 	 * chip really hates to have it's PHYs reset. They
@@ -601,12 +629,13 @@ static int vsc73xx_phy_write(struct dsa_switch *ds, int phy, int regnum,
 	      FIELD_PREP(VSC73XX_MII_CMD_WRITE_DATA, val);
 	ret = vsc73xx_write(vsc, VSC73XX_BLOCK_MII, VSC73XX_BLOCK_MII_INTERNAL,
 			    VSC73XX_MII_CMD, cmd);
-	if (ret)
-		return ret;
-
-	dev_dbg(vsc->dev, "write %04x to reg %02x in phy%d\n",
-		val, regnum, phy);
-	return 0;
+	if (!ret)
+		dev_dbg(vsc->dev, "write %04x to reg %02x in phy%d\n",
+			val, regnum, phy);
+	usleep_range(100, 200);
+err:
+	mutex_unlock(&vsc->mdio_lock);
+	return ret;
 }
 
 static enum dsa_tag_protocol vsc73xx_get_tag_protocol(struct dsa_switch *ds,
@@ -1973,6 +2002,8 @@ int vsc73xx_probe(struct vsc73xx *vsc)
 		return -ENODEV;
 	}
 
+	mutex_init(&vsc->mdio_lock);
+
 	eth_random_addr(vsc->addr);
 	dev_info(vsc->dev,
 		 "MAC for control frames: %02X:%02X:%02X:%02X:%02X:%02X\n",
diff --git a/drivers/net/dsa/vitesse-vsc73xx.h b/drivers/net/dsa/vitesse-vsc73xx.h
index 3ca579acc798..fc0fa8f5f57c 100644
--- a/drivers/net/dsa/vitesse-vsc73xx.h
+++ b/drivers/net/dsa/vitesse-vsc73xx.h
@@ -45,6 +45,7 @@ struct vsc73xx_portinfo {
  * @vlans: List of configured vlans. Contains port mask and untagged status of
  *	every vlan configured in port vlan operation. It doesn't cover tag_8021q
  *	vlans.
+ * @mdio_lock: Mutex used in mdio operations
  */
 struct vsc73xx {
 	struct device			*dev;
@@ -57,6 +58,7 @@ struct vsc73xx {
 	void				*priv;
 	struct vsc73xx_portinfo		portinfo[VSC73XX_MAX_NUM_PORTS];
 	struct list_head		vlans;
+	struct mutex			mdio_lock;
 };
 
 /**
-- 
2.34.1


  parent reply	other threads:[~2024-07-29 21:06 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-07-29 21:06 [PATCH net-next 0/9] net: dsa: vsc73xx: fix MDIO bus access and PHY operations Pawel Dembicki
2024-07-29 21:06 ` [PATCH net-next 1/9] net: dsa: vsc73xx: fix phylink capabilities Pawel Dembicki
2024-07-29 22:54   ` Andrew Lunn
2024-07-29 23:05   ` Russell King (Oracle)
2024-07-29 21:06 ` [PATCH net-next 2/9] net: dsa: vsc73xx: fix port MAC configuration in full duplex mode Pawel Dembicki
2024-07-29 21:06 ` [PATCH net-next 3/9] net: dsa: vsc73xx: pass value in phy_write operation Pawel Dembicki
2024-07-29 21:06 ` [PATCH net-next 4/9] net: dsa: vsc73xx: use defined values in phy operations Pawel Dembicki
2024-07-29 21:06 ` Pawel Dembicki [this message]
2024-07-29 23:07   ` [PATCH net-next 5/9] net: dsa: vsc73xx: use mutex to mdio operations Andrew Lunn
2024-07-29 21:06 ` [PATCH net-next 6/9] net: dsa: vsc73xx: speed up mdio bus to max allowed value Pawel Dembicki
2024-07-29 23:10   ` Andrew Lunn
2024-07-30 18:55     ` Paweł Dembicki
2024-07-30 20:23       ` Andrew Lunn
2024-07-29 21:06 ` [PATCH net-next 7/9] net: dsa: vsc73xx: allow phy resetting Pawel Dembicki
2024-07-29 21:06 ` [PATCH net-next 8/9] net: phy: vitesse: repair vsc73xx autonegotiation Pawel Dembicki
2024-07-29 21:06 ` [PATCH net-next 9/9] net: phy: vitesse: implement downshift in vsc73xx phys Pawel Dembicki
2024-07-29 23:39   ` Russell King (Oracle)
2024-07-29 22:53 ` [PATCH net-next 0/9] net: dsa: vsc73xx: fix MDIO bus access and PHY operations Andrew Lunn

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240729210615.279952-6-paweldembicki@gmail.com \
    --to=paweldembicki@gmail.com \
    --cc=andrew@lunn.ch \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=f.fainelli@gmail.com \
    --cc=hkallweit1@gmail.com \
    --cc=kuba@kernel.org \
    --cc=linus.walleij@linaro.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=netdev@vger.kernel.org \
    --cc=olteanv@gmail.com \
    --cc=pabeni@redhat.com \
    --cc=rmk+kernel@armlinux.org.uk \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®