netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 1/2] net: phy: Add error checks in the driver
@ 2016-08-19 12:48 Kedareswara rao Appana
  2016-08-19 12:48 ` [PATCH 2/2] net: phy: Fix race conditions " Kedareswara rao Appana
  2016-08-19 13:15 ` [PATCH 1/2] net: phy: Add error checks " Andrew Lunn
  0 siblings, 2 replies; 5+ messages in thread
From: Kedareswara rao Appana @ 2016-08-19 12:48 UTC (permalink / raw)
  To: michal.simek, soren.brinkmann, appanad, f.fainelli, andrew
  Cc: linux-arm-kernel, linux-kernel, netdev

This patch adds the necessary error checks in the driver.

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

diff --git a/drivers/net/phy/xilinx_gmii2rgmii.c b/drivers/net/phy/xilinx_gmii2rgmii.c
index e7a20ec..7336fd0 100644
--- a/drivers/net/phy/xilinx_gmii2rgmii.c
+++ b/drivers/net/phy/xilinx_gmii2rgmii.c
@@ -39,11 +39,16 @@ struct gmii2rgmii {
 static int xgmiitorgmii_read_status(struct phy_device *phydev)
 {
 	struct gmii2rgmii *priv = phydev->priv;
-	u16 val = 0;
+	int err, val = 0;
 
-	priv->phy_drv->read_status(phydev);
+	err = priv->phy_drv->read_status(phydev);
+	if (err < 0)
+		return err;
 
 	val = mdiobus_read(phydev->mdio.bus, priv->addr, XILINX_GMII2RGMII_REG);
+	if (val < 0)
+		return val;
+
 	val &= XILINX_GMII2RGMII_SPEED_MASK;
 
 	if (phydev->speed == SPEED_1000)
@@ -53,7 +58,10 @@ static int xgmiitorgmii_read_status(struct phy_device *phydev)
 	else
 		val |= BMCR_SPEED10;
 
-	mdiobus_write(phydev->mdio.bus, priv->addr, XILINX_GMII2RGMII_REG, val);
+	err = mdiobus_write(phydev->mdio.bus, priv->addr, XILINX_GMII2RGMII_REG,
+			    val);
+	if (err < 0)
+		return err;
 
 	return 0;
 }
-- 
2.1.2

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/2] net: phy: Fix race conditions in the driver
  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
  2016-08-19 13:24   ` Andrew Lunn
  2016-08-19 13:15 ` [PATCH 1/2] net: phy: Add error checks " Andrew Lunn
  1 sibling, 1 reply; 5+ messages in thread
From: Kedareswara rao Appana @ 2016-08-19 12:48 UTC (permalink / raw)
  To: michal.simek, soren.brinkmann, appanad, f.fainelli, andrew
  Cc: linux-arm-kernel, linux-kernel, netdev

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

^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/2] net: phy: Add error checks in the driver
  2016-08-19 12:48 [PATCH 1/2] net: phy: Add error checks in the driver Kedareswara rao Appana
  2016-08-19 12:48 ` [PATCH 2/2] net: phy: Fix race conditions " Kedareswara rao Appana
@ 2016-08-19 13:15 ` Andrew Lunn
  2016-08-19 13:18   ` Appana Durga Kedareswara Rao
  1 sibling, 1 reply; 5+ messages in thread
From: Andrew Lunn @ 2016-08-19 13:15 UTC (permalink / raw)
  To: Kedareswara rao Appana
  Cc: michal.simek, soren.brinkmann, appanad, f.fainelli,
	linux-arm-kernel, linux-kernel, netdev

> -	mdiobus_write(phydev->mdio.bus, priv->addr, XILINX_GMII2RGMII_REG, val);
> +	err = mdiobus_write(phydev->mdio.bus, priv->addr, XILINX_GMII2RGMII_REG,
> +			    val);
> +	if (err < 0)
> +		return err;
>  
>  	return 0;

Do you need to assign err? Why not just

   return mdiobus_write(phydev->mdio.bus, priv->addr, XILINX_GMII2RGMII_REG,
			val);

^ permalink raw reply	[flat|nested] 5+ messages in thread

* RE: [PATCH 1/2] net: phy: Add error checks in the driver
  2016-08-19 13:15 ` [PATCH 1/2] net: phy: Add error checks " Andrew Lunn
@ 2016-08-19 13:18   ` Appana Durga Kedareswara Rao
  0 siblings, 0 replies; 5+ messages in thread
From: Appana Durga Kedareswara Rao @ 2016-08-19 13:18 UTC (permalink / raw)
  To: Andrew Lunn
  Cc: f.fainelli@gmail.com, netdev@vger.kernel.org,
	linux-kernel@vger.kernel.org, Michal Simek, Soren Brinkmann,
	linux-arm-kernel@lists.infradead.org

Hi Andrew,

	Thanks for the review...

> > -	mdiobus_write(phydev->mdio.bus, priv->addr,
> XILINX_GMII2RGMII_REG, val);
> > +	err = mdiobus_write(phydev->mdio.bus, priv->addr,
> XILINX_GMII2RGMII_REG,
> > +			    val);
> > +	if (err < 0)
> > +		return err;
> >
> >  	return 0;
> 
> Do you need to assign err? Why not just

Ok will fix in the next version....

Regards,
Kedar.

> 
>    return mdiobus_write(phydev->mdio.bus, priv->addr,
> XILINX_GMII2RGMII_REG,
> 			val);

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH 2/2] net: phy: Fix race conditions in the driver
  2016-08-19 12:48 ` [PATCH 2/2] net: phy: Fix race conditions " Kedareswara rao Appana
@ 2016-08-19 13:24   ` Andrew Lunn
  0 siblings, 0 replies; 5+ messages in thread
From: Andrew Lunn @ 2016-08-19 13:24 UTC (permalink / raw)
  To: Kedareswara rao Appana
  Cc: michal.simek, soren.brinkmann, appanad, f.fainelli,
	linux-arm-kernel, linux-kernel, netdev

On Fri, Aug 19, 2016 at 06:18:11PM +0530, Kedareswara rao Appana wrote:
> 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.

Each one of these should be an individual patch.

> 
> 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;

The phy already have a 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;
  
I said you want to return an error is the PHY is using a speed your
converter cannot support. I don't see an error being raised here.

>  	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;
> +	}

The comment at the top says you are taking a reference to the phy.
Here you take a reference to the mdio bus driver???

> +
> +	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);

And how is this spinlock protecting anything? 

> +
> +	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);

Why are you disconnecting the phy?

> +}
> +
>  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
> 

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2016-08-19 13:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2016-08-19 12:48 [PATCH 1/2] net: phy: Add error checks in the driver Kedareswara rao Appana
2016-08-19 12:48 ` [PATCH 2/2] net: phy: Fix race conditions " Kedareswara rao Appana
2016-08-19 13:24   ` 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

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).