[PATCHv6 net-next 6/7] net: ibm: emac: generate random MAC if not found

Rosen Penev posted 7 patches 1 month, 2 weeks ago
There is a newer version of this series
[PATCHv6 net-next 6/7] net: ibm: emac: generate random MAC if not found
Posted by Rosen Penev 1 month, 2 weeks ago
On this Cisco MX60W, u-boot sets the local-mac-address property.
Unfortunately by default, the MAC is wrong and is actually located on a
UBI partition. Which means nvmem needs to be used to grab it.

In the case where that fails, EMAC fails to initialize instead of
generating a random MAC as many other drivers do.

Match behavior with other drivers to have a working ethernet interface.

Signed-off-by: Rosen Penev <rosenp@gmail.com>
---
 drivers/net/ethernet/ibm/emac/core.c | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/drivers/net/ethernet/ibm/emac/core.c b/drivers/net/ethernet/ibm/emac/core.c
index b9ccaae61c48..faa483790b29 100644
--- a/drivers/net/ethernet/ibm/emac/core.c
+++ b/drivers/net/ethernet/ibm/emac/core.c
@@ -2937,9 +2937,12 @@ static int emac_init_config(struct emac_instance *dev)
 
 	/* Read MAC-address */
 	err = of_get_ethdev_address(np, dev->ndev);
-	if (err)
-		return dev_err_probe(&dev->ofdev->dev, err,
-				     "Can't get valid [local-]mac-address from OF !\n");
+	if (err == -EPROBE_DEFER)
+		return err;
+	if (err) {
+		dev_warn(&dev->ofdev->dev, "Can't get valid mac-address. Generating random.");
+		eth_hw_addr_random(dev->ndev);
+	}
 
 	/* IAHT and GAHT filter parameterization */
 	if (emac_has_feature(dev, EMAC_FTR_EMAC4SYNC)) {
-- 
2.47.0
Re: [PATCHv6 net-next 6/7] net: ibm: emac: generate random MAC if not found
Posted by Simon Horman 1 month, 2 weeks ago
On Fri, Oct 11, 2024 at 12:56:21PM -0700, Rosen Penev wrote:
> On this Cisco MX60W, u-boot sets the local-mac-address property.
> Unfortunately by default, the MAC is wrong and is actually located on a
> UBI partition. Which means nvmem needs to be used to grab it.
> 
> In the case where that fails, EMAC fails to initialize instead of
> generating a random MAC as many other drivers do.
> 
> Match behavior with other drivers to have a working ethernet interface.
> 
> Signed-off-by: Rosen Penev <rosenp@gmail.com>
> ---
>  drivers/net/ethernet/ibm/emac/core.c | 9 ++++++---
>  1 file changed, 6 insertions(+), 3 deletions(-)
> 
> diff --git a/drivers/net/ethernet/ibm/emac/core.c b/drivers/net/ethernet/ibm/emac/core.c
> index b9ccaae61c48..faa483790b29 100644
> --- a/drivers/net/ethernet/ibm/emac/core.c
> +++ b/drivers/net/ethernet/ibm/emac/core.c
> @@ -2937,9 +2937,12 @@ static int emac_init_config(struct emac_instance *dev)
>  
>  	/* Read MAC-address */
>  	err = of_get_ethdev_address(np, dev->ndev);
> -	if (err)
> -		return dev_err_probe(&dev->ofdev->dev, err,
> -				     "Can't get valid [local-]mac-address from OF !\n");
> +	if (err == -EPROBE_DEFER)
> +		return err;
> +	if (err) {
> +		dev_warn(&dev->ofdev->dev, "Can't get valid mac-address. Generating random.");
> +		eth_hw_addr_random(dev->ndev);
> +	}

The above seems to take the random path for all errors other than
-EPROBE_DEFER. That seems too broad to me, and perhaps it would
be better to be more specific. Assuming the case that needs
to be covered is -EINVAL (a guess on my part), perhaps something like this
would work? (Completely untested!)

	err = of_get_ethdev_address(np, dev->ndev);
	if (err == -EINVAL) {
		/* An explanation should go here, mentioning Cisco MX60W
		 * Maybe the logic should even be specific to that hw?
		 */
		dev_warn(&dev->ofdev->dev, "Can't get valid mac-address. Generating random.");
		eth_hw_addr_random(dev->ndev);
	} else if (err) {
		return dev_err_probe(&dev->ofdev->dev, err,
				     "Can't get valid [local-]mac-address from OF !\n");
	}

Also, should this be a bug fix with a Fixes tag for net?

>  
>  	/* IAHT and GAHT filter parameterization */
>  	if (emac_has_feature(dev, EMAC_FTR_EMAC4SYNC)) {
> -- 
> 2.47.0
>
Re: [PATCHv6 net-next 6/7] net: ibm: emac: generate random MAC if not found
Posted by Rosen Penev 1 month, 1 week ago
On Sat, Oct 12, 2024 at 6:16 AM Simon Horman <horms@kernel.org> wrote:
>
> On Fri, Oct 11, 2024 at 12:56:21PM -0700, Rosen Penev wrote:
> > On this Cisco MX60W, u-boot sets the local-mac-address property.
> > Unfortunately by default, the MAC is wrong and is actually located on a
> > UBI partition. Which means nvmem needs to be used to grab it.
> >
> > In the case where that fails, EMAC fails to initialize instead of
> > generating a random MAC as many other drivers do.
> >
> > Match behavior with other drivers to have a working ethernet interface.
> >
> > Signed-off-by: Rosen Penev <rosenp@gmail.com>
> > ---
> >  drivers/net/ethernet/ibm/emac/core.c | 9 ++++++---
> >  1 file changed, 6 insertions(+), 3 deletions(-)
> >
> > diff --git a/drivers/net/ethernet/ibm/emac/core.c b/drivers/net/ethernet/ibm/emac/core.c
> > index b9ccaae61c48..faa483790b29 100644
> > --- a/drivers/net/ethernet/ibm/emac/core.c
> > +++ b/drivers/net/ethernet/ibm/emac/core.c
> > @@ -2937,9 +2937,12 @@ static int emac_init_config(struct emac_instance *dev)
> >
> >       /* Read MAC-address */
> >       err = of_get_ethdev_address(np, dev->ndev);
> > -     if (err)
> > -             return dev_err_probe(&dev->ofdev->dev, err,
> > -                                  "Can't get valid [local-]mac-address from OF !\n");
> > +     if (err == -EPROBE_DEFER)
> > +             return err;
> > +     if (err) {
> > +             dev_warn(&dev->ofdev->dev, "Can't get valid mac-address. Generating random.");
> > +             eth_hw_addr_random(dev->ndev);
> > +     }
>
> The above seems to take the random path for all errors other than
> -EPROBE_DEFER. That seems too broad to me, and perhaps it would
> be better to be more specific. Assuming the case that needs
> to be covered is -EINVAL (a guess on my part), perhaps something like this
> would work? (Completely untested!)
>
>         err = of_get_ethdev_address(np, dev->ndev);
>         if (err == -EINVAL) {
>                 /* An explanation should go here, mentioning Cisco MX60W
>                  * Maybe the logic should even be specific to that hw?
>                  */
>                 dev_warn(&dev->ofdev->dev, "Can't get valid mac-address. Generating random.");
>                 eth_hw_addr_random(dev->ndev);
>         } else if (err) {
>                 return dev_err_probe(&dev->ofdev->dev, err,
>                                      "Can't get valid [local-]mac-address from OF !\n");
>         }
That's just yak shaving. besides 0 and EPROBE_DEFER,
of_get_ethdev_address returns ENODEV, EINVAL, and maybe something
else. I don't see a good enough reason to diverge from convention
here. This same pattern is present in other drivers.
>
> Also, should this be a bug fix with a Fixes tag for net?
No. It's more of a feature honestly.
>
> >
> >       /* IAHT and GAHT filter parameterization */
> >       if (emac_has_feature(dev, EMAC_FTR_EMAC4SYNC)) {
> > --
> > 2.47.0
> >
Re: [PATCHv6 net-next 6/7] net: ibm: emac: generate random MAC if not found
Posted by Simon Horman 1 month, 1 week ago
On Tue, Oct 15, 2024 at 12:44:52PM -0700, Rosen Penev wrote:
> On Sat, Oct 12, 2024 at 6:16 AM Simon Horman <horms@kernel.org> wrote:
> >
> > On Fri, Oct 11, 2024 at 12:56:21PM -0700, Rosen Penev wrote:
> > > On this Cisco MX60W, u-boot sets the local-mac-address property.
> > > Unfortunately by default, the MAC is wrong and is actually located on a
> > > UBI partition. Which means nvmem needs to be used to grab it.
> > >
> > > In the case where that fails, EMAC fails to initialize instead of
> > > generating a random MAC as many other drivers do.
> > >
> > > Match behavior with other drivers to have a working ethernet interface.
> > >
> > > Signed-off-by: Rosen Penev <rosenp@gmail.com>
> > > ---
> > >  drivers/net/ethernet/ibm/emac/core.c | 9 ++++++---
> > >  1 file changed, 6 insertions(+), 3 deletions(-)
> > >
> > > diff --git a/drivers/net/ethernet/ibm/emac/core.c b/drivers/net/ethernet/ibm/emac/core.c
> > > index b9ccaae61c48..faa483790b29 100644
> > > --- a/drivers/net/ethernet/ibm/emac/core.c
> > > +++ b/drivers/net/ethernet/ibm/emac/core.c
> > > @@ -2937,9 +2937,12 @@ static int emac_init_config(struct emac_instance *dev)
> > >
> > >       /* Read MAC-address */
> > >       err = of_get_ethdev_address(np, dev->ndev);
> > > -     if (err)
> > > -             return dev_err_probe(&dev->ofdev->dev, err,
> > > -                                  "Can't get valid [local-]mac-address from OF !\n");
> > > +     if (err == -EPROBE_DEFER)
> > > +             return err;
> > > +     if (err) {
> > > +             dev_warn(&dev->ofdev->dev, "Can't get valid mac-address. Generating random.");
> > > +             eth_hw_addr_random(dev->ndev);
> > > +     }
> >
> > The above seems to take the random path for all errors other than
> > -EPROBE_DEFER. That seems too broad to me, and perhaps it would
> > be better to be more specific. Assuming the case that needs
> > to be covered is -EINVAL (a guess on my part), perhaps something like this
> > would work? (Completely untested!)
> >
> >         err = of_get_ethdev_address(np, dev->ndev);
> >         if (err == -EINVAL) {
> >                 /* An explanation should go here, mentioning Cisco MX60W
> >                  * Maybe the logic should even be specific to that hw?
> >                  */
> >                 dev_warn(&dev->ofdev->dev, "Can't get valid mac-address. Generating random.");
> >                 eth_hw_addr_random(dev->ndev);
> >         } else if (err) {
> >                 return dev_err_probe(&dev->ofdev->dev, err,
> >                                      "Can't get valid [local-]mac-address from OF !\n");
> >         }
> That's just yak shaving. besides 0 and EPROBE_DEFER,
> of_get_ethdev_address returns ENODEV, EINVAL, and maybe something
> else. I don't see a good enough reason to diverge from convention
> here. This same pattern is present in other drivers.

I assumed that more specific error detection would be appropriate, because
it seemed to be a rather specific case.  But if the pattern is present in
other drivers, then that is fine.

> >
> > Also, should this be a bug fix with a Fixes tag for net?
> No. It's more of a feature honestly.
> >
> > >
> > >       /* IAHT and GAHT filter parameterization */
> > >       if (emac_has_feature(dev, EMAC_FTR_EMAC4SYNC)) {
> > > --
> > > 2.47.0
> > >
>