linux-omap.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Bartosz Golaszewski <brgl@bgdev.pl>
To: Dan Carpenter <dan.carpenter@oracle.com>
Cc: Bartosz Golaszewski <bgolaszewski@baylibre.com>,
	Sekhar Nori <nsekhar@ti.com>, Kevin Hilman <khilman@kernel.org>,
	Russell King <linux@armlinux.org.uk>,
	Grygorii Strashko <grygorii.strashko@ti.com>,
	"David S . Miller" <davem@davemloft.net>,
	Srinivas Kandagatla <srinivas.kandagatla@linaro.org>,
	Lukas Wunner <lukas@wunner.de>, Rob Herring <robh@kernel.org>,
	Florian Fainelli <f.fainelli@gmail.com>,
	Ivan Khoronzhuk <ivan.khoronzhuk@linaro.org>,
	David Lechner <david@lechnology.com>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Andrew Lunn <andrew@lunn.ch>,
	arm-soc <linux-arm-kernel@lists.infradead.org>,
	LKML <linux-kernel@vger.kernel.org>,
	Linux-OMAP <linux-omap@vger.kernel.org>,
	netdev@vger.kernel.org
Subject: Re: [PATCH v2 2/3] net: add support for nvmem to eth_platform_get_mac_address()
Date: Thu, 19 Jul 2018 17:35:40 +0200	[thread overview]
Message-ID: <CAMRc=MeuoHcnRKc4uP8F5MEripV_w3gT9Eg1LPMRZc479vUHBQ@mail.gmail.com> (raw)
In-Reply-To: <20180719123722.sk5fcih6pufy4wpd@mwanda>

2018-07-19 14:37 GMT+02:00 Dan Carpenter <dan.carpenter@oracle.com>:
> On Thu, Jul 19, 2018 at 12:06:51PM +0200, Bartosz Golaszewski wrote:
>> It looks simpler as long as you don't add all the new routines
>> resulting from this approach. I've just tried to quickly implement
>> this solution and it resulted in much bigger and duplicated code
>> (checking the validity of mac_addr, copying it etc.). I would prefer
>> the current approach and would like to read someone else's opinion on
>> that.
>
> It's not too bad.  There is two extra ether_addr_copy() calls and one
> extra is_valid_ether_addr().  There are two extra alen declarations and
> one extra addr declaration.  The functions don't share *that* much code.
>
> regards,
> dan carpenter
>
> diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c
> index fd8faa0dfa61..8ab7289a5069 100644
> --- a/net/ethernet/eth.c
> +++ b/net/ethernet/eth.c
> @@ -54,6 +54,8 @@
>  #include <linux/if_ether.h>
>  #include <linux/of_net.h>
>  #include <linux/pci.h>
> +#include <linux/nvmem-consumer.h>
> +#include <linux/mtd/mtd.h>
>  #include <net/dst.h>
>  #include <net/arp.h>
>  #include <net/sock.h>
> @@ -525,7 +527,7 @@ unsigned char * __weak arch_get_platform_mac_address(void)
>         return NULL;
>  }
>
> -int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr)
> +static int of_eth_get_mac_address(struct device *dev, u8 *mac_addr)
>  {
>         const unsigned char *addr;
>         struct device_node *dp;
> @@ -547,4 +549,82 @@ int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr)
>         ether_addr_copy(mac_addr, addr);
>         return 0;
>  }
> +
> +static int nvmem_eth_get_mac_address(struct device *dev, u8 *mac_addr)
> +{
> +       struct nvmem_cell *nvmem;
> +       unsigned char *addr;
> +       size_t alen;
> +       int ret = 0;
> +
> +       nvmem = nvmem_cell_get(dev, "mac-address");
> +       if (PTR_ERR(nvmem) == -EPROBE_DEFER)
> +               /* We may have a lookup registered for MAC address but the
> +                * corresponding nvmem provider hasn't been registered yet.
> +                */
> +               return -EPROBE_DEFER;
> +       if (IS_ERR(nvmem))
> +               return PTR_ERR(nvmem);
> +       addr = nvmem_cell_read(nvmem, &alen);
> +       if (IS_ERR(addr)) {
> +               ret = PTR_ERR(addr);
> +               goto put_nvmem;
> +       }
> +       if (alen != ETH_ALEN || !is_valid_ether_addr(addr)) {
> +               ret = -EINVAL;
> +               goto free_addr;
> +       }
> +       ether_addr_copy(mac_addr, addr);
> +free_addr:
> +       kfree(addr);
> +put_nvmem:
> +       nvmem_cell_put(nvmem);
> +       return ret;
> +}
> +
> +static int mtd_eth_get_mac_address(struct device *dev, u8 *mac_addr)
> +{
> +       struct mtd_info *mtd;
> +       u8 addrbuf[ETH_ALEN];
> +       size_t alen;
> +       int ret;
> +
> +       /* This function should go away as soon as MTD gets nvmem support. */
> +       if (!IS_ENABLED(CONFIG_MTD))
> +               return -ENODEV;
> +
> +       mtd = get_mtd_device_nm("MAC-Address");
> +       if (IS_ERR(mtd))
> +               return PTR_ERR(mtd);
> +       ret = mtd_read(mtd, 0, ETH_ALEN, &alen, addrbuf);
> +       if (ret)
> +               goto put_mtd;
> +       if (!is_valid_ether_addr(addrbuf)) {
> +               ret = -EINVAL;
> +               goto put_mtd;
> +       }
> +       ether_addr_copy(mac_addr, addrbuf);
> +put_mtd:
> +       put_mtd_device(mtd);
> +       return ret;
> +}
> +
> +int eth_platform_get_mac_address(struct device *dev, u8 *mac_addr)
> +{
> +       int ret;
> +
> +       ret = of_eth_get_mac_address(dev, mac_addr);
> +       if (!ret)
> +               return 0;
> +       ret = nvmem_eth_get_mac_address(dev, mac_addr);
> +       if (ret == -EPROBE_DEFER)
> +               return -EPROBE_DEFER;
> +       if (!ret)
> +               return 0;
> +       ret = mtd_eth_get_mac_address(dev, mac_addr);
> +       if (!ret)
> +               return 0;
> +
> +       return -ENODEV;
> +}
>  EXPORT_SYMBOL(eth_platform_get_mac_address);

Please take a look at v3 - I did it a bit differently but the idea is the same.

Bart

  reply	other threads:[~2018-07-19 15:35 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2018-07-19  8:20 [PATCH v2 0/3] net: extend eth_platform_get_mac_address() Bartosz Golaszewski
2018-07-19  8:20 ` [PATCH v2 1/3] net: fortify eth_platform_get_mac_address() Bartosz Golaszewski
2018-07-19  8:20 ` [PATCH v2 2/3] net: add support for nvmem to eth_platform_get_mac_address() Bartosz Golaszewski
2018-07-19  8:48   ` Dan Carpenter
2018-07-19  8:57     ` Bartosz Golaszewski
2018-07-19  9:09       ` Dan Carpenter
2018-07-19 10:06         ` Bartosz Golaszewski
2018-07-19 12:37           ` Dan Carpenter
2018-07-19 15:35             ` Bartosz Golaszewski [this message]
2018-07-19 12:23     ` Russell King - ARM Linux
2018-07-19  8:20 ` [PATCH v2 3/3] net: add MTD support " Bartosz Golaszewski

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='CAMRc=MeuoHcnRKc4uP8F5MEripV_w3gT9Eg1LPMRZc479vUHBQ@mail.gmail.com' \
    --to=brgl@bgdev.pl \
    --cc=andrew@lunn.ch \
    --cc=bgolaszewski@baylibre.com \
    --cc=dan.carpenter@oracle.com \
    --cc=davem@davemloft.net \
    --cc=david@lechnology.com \
    --cc=f.fainelli@gmail.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=grygorii.strashko@ti.com \
    --cc=ivan.khoronzhuk@linaro.org \
    --cc=khilman@kernel.org \
    --cc=linux-arm-kernel@lists.infradead.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-omap@vger.kernel.org \
    --cc=linux@armlinux.org.uk \
    --cc=lukas@wunner.de \
    --cc=netdev@vger.kernel.org \
    --cc=nsekhar@ti.com \
    --cc=robh@kernel.org \
    --cc=srinivas.kandagatla@linaro.org \
    /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).