public inbox for u-boot@lists.denx.de
 help / color / mirror / Atom feed
From: Detlev Casanova <detlev.casanova@collabora.com>
To: u-boot@lists.denx.de
Cc: Joe Hershberger <joe.hershberger@ni.com>,
	Ramon Fried <rfried.dev@gmail.com>, Tom Rini <trini@konsulko.com>,
	Philipp Tomsich <philipp.tomsich@vrull.eu>,
	Kever Yang <kever.yang@rock-chips.com>,
	Marek Vasut <marek.vasut+renesas@mailbox.org>,
	Jonas Karlman <jonas@kwiboo.se>, Simon Glass <sjg@chromium.org>,
	Fabio Estevam <festevam@denx.de>,
	Detlev Casanova <detlev.casanova@collabora.com>
Subject: [PATCH 1/3] net: Add a CONFIG_NET_BOARD_ETHADDR
Date: Thu, 14 Mar 2024 10:43:46 -0400	[thread overview]
Message-ID: <20240314144403.491850-2-detlev.casanova@collabora.com> (raw)
In-Reply-To: <20240314144403.491850-1-detlev.casanova@collabora.com>

On some boards, a MAC address is set based on the CPU ID or other
information. This is usually done in the misc_init_r() function.

This becomes a problem for net devices that are probed after the call to
misc_init_r(), for example, when the ethernet is on a PCI port, which
needs to be enumerated.

In this case, misc_init_r() will set the ethaddr variable, then, when
the ethernet device is probed, if it has a ROM address, u-boot will warn
about a MAC address mismatch and use the misc_init_r() address instead
of the one in ROM.

The operating system later will most likely use the ROM MAC address,
which can be confusing.

To avoid that, this commit introduces a CONFIG_NET_BOARD_ETHADDR that
allows board files to implement a function to set an ethaddr in the
environment, that will only be called when necessary. The logic is now:

- If there is there an ethaddr env var, use it.
- If not, if there is a DT MAC address, use it.
- If not, if there is a ROM MAC address, use it.
- If not, if CONFIG_NET_BOARD_ETHADDR, call board_gen_ethaddr() and use
  it.
- If not, if CONFIG_NET_RANDOM_ETHADDR, generate random MAC
- If not, fail with No valid MAC address found

Signed-off-by: Detlev Casanova <detlev.casanova@collabora.com>
---
 net/Kconfig      |  7 +++++++
 net/eth-uclass.c | 17 +++++++++++++++--
 2 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/net/Kconfig b/net/Kconfig
index 5dff6336293..6dd333ddb9e 100644
--- a/net/Kconfig
+++ b/net/Kconfig
@@ -54,6 +54,13 @@ config NET_RANDOM_ETHADDR
 	  generated. It will be saved to the appropriate environment variable,
 	  too.
 
+config NET_BOARD_ETHADDR
+	bool "Board specific ethaddr if unset"
+	help
+	  Allow a board function to set a specific Ethernet address that can
+	  be, e.g., based on the CPU ID. If set, this will be tried before
+	  setting a random address (if set).
+
 config NETCONSOLE
 	bool "NetConsole support"
 	help
diff --git a/net/eth-uclass.c b/net/eth-uclass.c
index 3d0ec91dfa4..f194df8512a 100644
--- a/net/eth-uclass.c
+++ b/net/eth-uclass.c
@@ -56,6 +56,12 @@ __weak int board_interface_eth_init(struct udevice *dev,
 	return 0;
 }
 
+/* board-specific MAC Address generation. */
+__weak int board_gen_ethaddr(int dev_num, u8 *mac_addr)
+{
+	return 0;
+}
+
 static struct eth_uclass_priv *eth_get_uclass_priv(void)
 {
 	struct uclass *uc;
@@ -563,13 +569,20 @@ static int eth_post_probe(struct udevice *dev)
 	if (!eth_dev_get_mac_address(dev, pdata->enetaddr) ||
 	    !is_valid_ethaddr(pdata->enetaddr)) {
 		/* Check if the device has a MAC address in ROM */
+		int ret = -1;
 		if (eth_get_ops(dev)->read_rom_hwaddr) {
-			int ret;
-
 			ret = eth_get_ops(dev)->read_rom_hwaddr(dev);
 			if (!ret)
 				source = "ROM";
 		}
+		if (IS_ENABLED(CONFIG_NET_BOARD_ETHADDR) && ret) {
+			board_gen_ethaddr(dev_seq(dev), pdata->enetaddr);
+
+			if (!is_zero_ethaddr(pdata->enetaddr) &&
+			    is_valid_ethaddr(pdata->enetaddr)) {
+				source = "board";
+			}
+		}
 	}
 
 	eth_env_get_enetaddr_by_index("eth", dev_seq(dev), env_enetaddr);
-- 
2.43.2


  reply	other threads:[~2024-03-14 14:44 UTC|newest]

Thread overview: 8+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-03-14 14:43 [PATCH 0/3] net: Add a CONFIG_NET_BOARD_ETHADDR Detlev Casanova
2024-03-14 14:43 ` Detlev Casanova [this message]
2024-03-14 17:36   ` [PATCH 1/3] " Marek Vasut
2024-03-14 14:43 ` [PATCH 2/3] rockchip: Add a board_gen_ethaddr() function Detlev Casanova
2024-03-14 17:37   ` Marek Vasut
2024-03-14 18:38   ` Jonas Karlman
2024-04-02 15:38     ` Detlev Casanova
2024-03-14 14:43 ` [PATCH 3/3] net: eth-uclass: Add driver source possibility Detlev Casanova

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=20240314144403.491850-2-detlev.casanova@collabora.com \
    --to=detlev.casanova@collabora.com \
    --cc=festevam@denx.de \
    --cc=joe.hershberger@ni.com \
    --cc=jonas@kwiboo.se \
    --cc=kever.yang@rock-chips.com \
    --cc=marek.vasut+renesas@mailbox.org \
    --cc=philipp.tomsich@vrull.eu \
    --cc=rfried.dev@gmail.com \
    --cc=sjg@chromium.org \
    --cc=trini@konsulko.com \
    --cc=u-boot@lists.denx.de \
    /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