All of lore.kernel.org
 help / color / mirror / Atom feed
From: Andy Duan <fugang.duan@nxp.com>
To: "davem@davemloft.net" <davem@davemloft.net>
Cc: "netdev@vger.kernel.org" <netdev@vger.kernel.org>,
	"ynezz@true.cz" <ynezz@true.cz>,
	"john@phrozen.org" <john@phrozen.org>,
	"bgolaszewski@baylibre.com" <bgolaszewski@baylibre.com>,
	Andy Duan <fugang.duan@nxp.com>
Subject: [PATCH net 1/3] net: ethernet: add property "nvmem_macaddr_swap" to swap macaddr bytes order
Date: Fri, 10 May 2019 08:24:00 +0000	[thread overview]
Message-ID: <1557476567-17397-2-git-send-email-fugang.duan@nxp.com> (raw)
In-Reply-To: <1557476567-17397-1-git-send-email-fugang.duan@nxp.com>

ethernet controller driver call .of_get_mac_address() to get
the mac address from devictree tree, if these properties are
not present, then try to read from nvmem.

For example, read MAC address from nvmem:
of_get_mac_address()
	of_get_mac_addr_nvmem()
		nvmem_get_mac_address()

i.MX6x/7D/8MQ/8MM platforms ethernet MAC address read from
nvmem ocotp eFuses, but it requires to swap the six bytes
order.

The patch add optional property "nvmem_macaddr_swap" to swap
macaddr bytes order.

Signed-off-by: Fugang Duan <fugang.duan@nxp.com>
---
 net/ethernet/eth.c | 25 ++++++++++++++++++++-----
 1 file changed, 20 insertions(+), 5 deletions(-)

diff --git a/net/ethernet/eth.c b/net/ethernet/eth.c
index 4b2b222..0a0986b 100644
--- a/net/ethernet/eth.c
+++ b/net/ethernet/eth.c
@@ -583,8 +583,10 @@ EXPORT_SYMBOL(eth_platform_get_mac_address);
 int nvmem_get_mac_address(struct device *dev, void *addrbuf)
 {
 	struct nvmem_cell *cell;
-	const void *mac;
+	const unsigned char *mac;
+	unsigned char macaddr[ETH_ALEN];
 	size_t len;
+	int i = 0;
 
 	cell = nvmem_cell_get(dev, "mac-address");
 	if (IS_ERR(cell))
@@ -596,14 +598,27 @@ int nvmem_get_mac_address(struct device *dev, void *addrbuf)
 	if (IS_ERR(mac))
 		return PTR_ERR(mac);
 
-	if (len != ETH_ALEN || !is_valid_ether_addr(mac)) {
-		kfree(mac);
-		return -EINVAL;
+	if (len != ETH_ALEN)
+		goto invalid_addr;
+
+	if (dev->of_node &&
+	    of_property_read_bool(dev->of_node, "nvmem_macaddr_swap")) {
+		for (i = 0; i < ETH_ALEN; i++)
+			macaddr[i] = mac[ETH_ALEN - i - 1];
+	} else {
+		ether_addr_copy(macaddr, mac);
 	}
 
-	ether_addr_copy(addrbuf, mac);
+	if (!is_valid_ether_addr(macaddr))
+		goto invalid_addr;
+
+	ether_addr_copy(addrbuf, macaddr);
 	kfree(mac);
 
 	return 0;
+
+invalid_addr:
+	kfree(mac);
+	return -EINVAL;
 }
 EXPORT_SYMBOL(nvmem_get_mac_address);
-- 
2.7.4


  reply	other threads:[~2019-05-10  8:24 UTC|newest]

Thread overview: 28+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-05-10  8:23 [PATCH net 0/3] add property "nvmem_macaddr_swap" to swap macaddr bytes order Andy Duan
2019-05-10  8:24 ` Andy Duan [this message]
2019-05-10 18:16   ` [PATCH net 1/3] net: ethernet: " Andrew Lunn
2019-05-13  3:10     ` [EXT] " Andy Duan
2019-05-10  8:24 ` [PATCH net 2/3] of_net: add property "nvmem-mac-address" for of_get_mac_addr() Andy Duan
2019-05-10 18:17   ` Andrew Lunn
2019-05-13  3:31     ` [EXT] " Andy Duan
2019-05-13  8:00       ` Petr Štetiar
2019-05-13  8:47         ` Andy Duan
2019-05-10  8:24 ` [PATCH net 3/3] dt-bindings: doc: add new properties for of_get_mac_address from nvmem Andy Duan
2019-05-10 11:28 ` [PATCH net 0/3] add property "nvmem_macaddr_swap" to swap macaddr bytes order Petr Štetiar
2019-05-10 11:31   ` Maxime Ripard
2019-05-10 11:31     ` Maxime Ripard
2019-05-11 14:44     ` NVMEM address DT post processing [Was: Re: [PATCH net 0/3] add property "nvmem_macaddr_swap" to swap macaddr bytes order] Petr Štetiar
2019-05-12 12:19       ` Maxime Ripard
2019-05-12 12:19         ` Maxime Ripard
2019-05-13  9:28         ` Petr Štetiar
2019-05-13  8:25       ` Srinivas Kandagatla
2019-05-13  9:07         ` Petr Štetiar
2019-05-13 10:06           ` Srinivas Kandagatla
2019-05-13 11:16             ` Petr Štetiar
2019-05-14 15:13               ` Srinivas Kandagatla
2019-05-14 17:44                 ` Petr Štetiar
2019-05-15 17:12                   ` Srinivas Kandagatla
2019-05-20 14:28               ` Rob Herring
2019-05-13  3:38     ` [EXT] Re: [PATCH net 0/3] add property "nvmem_macaddr_swap" to swap macaddr bytes order Andy Duan
2019-05-13  9:35       ` ynezz
2019-05-13  3:06   ` [EXT] " Andy Duan

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=1557476567-17397-2-git-send-email-fugang.duan@nxp.com \
    --to=fugang.duan@nxp.com \
    --cc=bgolaszewski@baylibre.com \
    --cc=davem@davemloft.net \
    --cc=john@phrozen.org \
    --cc=netdev@vger.kernel.org \
    --cc=ynezz@true.cz \
    /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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.