Message ID | 20240314144403.491850-2-detlev.casanova@collabora.com |
---|---|
State | Changes Requested |
Delegated to: | Tom Rini |
Headers | show |
Series | net: Add a CONFIG_NET_BOARD_ETHADDR | expand |
On 3/14/24 3:43 PM, Detlev Casanova wrote: > 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); You do need to pass in the udevice *dev directly, otherwise this DM uclass patch would behave like non-DM code. I'd personally just create a __weak board_gen_ethaddr() function here and let boards override it, without new Kconfig symbol.
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);
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(-)