netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Kedareswara rao Appana <appana.durga.rao@xilinx.com>
To: <michal.simek@xilinx.com>, <soren.brinkmann@xilinx.com>,
	<appanad@xilinx.com>, <f.fainelli@gmail.com>, <andrew@lunn.ch>
Cc: <linux-arm-kernel@lists.infradead.org>,
	<linux-kernel@vger.kernel.org>, <netdev@vger.kernel.org>
Subject: [PATCH 2/2] net: phy: Fix race conditions in the driver
Date: Fri, 19 Aug 2016 18:18:11 +0530	[thread overview]
Message-ID: <1471610891-13353-2-git-send-email-appanad@xilinx.com> (raw)
In-Reply-To: <1471610891-13353-1-git-send-email-appanad@xilinx.com>

This patch fixes the below race conditions in the driver.
---> Fix Opps after unload the driver as a module
---> Use spin locks where relevant.
---> Take reference on the external phy to prevent issues
when phy driver is unloaded.

Reported-by: Andrew Lunn <andrew@lunn.ch>
Signed-off-by: Kedareswara rao Appana <appanad@xilinx.com>
---
 drivers/net/phy/xilinx_gmii2rgmii.c | 32 +++++++++++++++++++++++++++++++-
 1 file changed, 31 insertions(+), 1 deletion(-)

diff --git a/drivers/net/phy/xilinx_gmii2rgmii.c b/drivers/net/phy/xilinx_gmii2rgmii.c
index 7336fd0..cdd9d95 100644
--- a/drivers/net/phy/xilinx_gmii2rgmii.c
+++ b/drivers/net/phy/xilinx_gmii2rgmii.c
@@ -34,6 +34,7 @@ struct gmii2rgmii {
 	struct phy_driver *phy_drv;
 	struct phy_driver conv_phy_drv;
 	int addr;
+	spinlock_t phy_lock;
 };
 
 static int xgmiitorgmii_read_status(struct phy_device *phydev)
@@ -55,7 +56,7 @@ static int xgmiitorgmii_read_status(struct phy_device *phydev)
 		val |= BMCR_SPEED1000;
 	else if (phydev->speed == SPEED_100)
 		val |= BMCR_SPEED100;
-	else
+	else if (phydev->speed == SPEED_10)
 		val |= BMCR_SPEED10;
 
 	err = mdiobus_write(phydev->mdio.bus, priv->addr, XILINX_GMII2RGMII_REG,
@@ -71,6 +72,8 @@ int xgmiitorgmii_probe(struct mdio_device *mdiodev)
 	struct device *dev = &mdiodev->dev;
 	struct device_node *np = dev->of_node, *phy_node;
 	struct gmii2rgmii *priv;
+	struct mii_bus *bus;
+	unsigned long flags;
 
 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
 	if (!priv)
@@ -88,17 +91,43 @@ int xgmiitorgmii_probe(struct mdio_device *mdiodev)
 		return -EPROBE_DEFER;
 	}
 
+	spin_lock_init(&priv->phy_lock);
+
+	bus = priv->phy_dev->mdio.bus;
+	if (!try_module_get(bus->owner)) {
+		dev_err(dev, "failed to get the bus module\n");
+		return -EIO;
+	}
+
+	get_device(&priv->phy_dev->mdio.dev);
+
 	priv->addr = mdiodev->addr;
 	priv->phy_drv = priv->phy_dev->drv;
 	memcpy(&priv->conv_phy_drv, priv->phy_dev->drv,
 	       sizeof(struct phy_driver));
 	priv->conv_phy_drv.read_status = xgmiitorgmii_read_status;
 	priv->phy_dev->priv = priv;
+	spin_lock_irqsave(&priv->phy_lock, flags);
 	priv->phy_dev->drv = &priv->conv_phy_drv;
+	spin_unlock_irqrestore(&priv->phy_lock, flags);
+
+	dev_set_drvdata(dev, priv);
 
 	return 0;
 }
 
+static void xgmiitorgmii_remove(struct mdio_device *mdiodev)
+{
+	struct gmii2rgmii *priv = dev_get_drvdata(&mdiodev->dev);
+	struct mii_bus *bus;
+
+	bus = priv->phy_dev->mdio.bus;
+
+	put_device(&priv->phy_dev->mdio.dev);
+	module_put(bus->owner);
+	phy_disconnect(priv->phy_dev);
+}
+
 static const struct of_device_id xgmiitorgmii_of_match[] = {
 	{ .compatible = "xlnx,gmii-to-rgmii-1.0" },
 	{},
@@ -107,6 +136,7 @@ MODULE_DEVICE_TABLE(of, xgmiitorgmii_of_match);
 
 static struct mdio_driver xgmiitorgmii_driver = {
 	.probe	= xgmiitorgmii_probe,
+	.remove	= xgmiitorgmii_remove,
 	.mdiodrv.driver = {
 		.name = "xgmiitorgmii",
 		.of_match_table = xgmiitorgmii_of_match,
-- 
2.1.2

  reply	other threads:[~2016-08-19 12:49 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2016-08-19 12:48 [PATCH 1/2] net: phy: Add error checks in the driver Kedareswara rao Appana
2016-08-19 12:48 ` Kedareswara rao Appana [this message]
2016-08-19 13:24   ` [PATCH 2/2] net: phy: Fix race conditions " Andrew Lunn
2016-08-19 13:15 ` [PATCH 1/2] net: phy: Add error checks " Andrew Lunn
2016-08-19 13:18   ` Appana Durga Kedareswara Rao

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=1471610891-13353-2-git-send-email-appanad@xilinx.com \
    --to=appana.durga.rao@xilinx.com \
    --cc=andrew@lunn.ch \
    --cc=appanad@xilinx.com \
    --cc=f.fainelli@gmail.com \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=michal.simek@xilinx.com \
    --cc=netdev@vger.kernel.org \
    --cc=soren.brinkmann@xilinx.com \
    /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;
as well as URLs for NNTP newsgroup(s).