[PATCH 3/3] net: marvell: prestera: force good base mac

Elad Nachman posted 3 patches 1 year, 11 months ago
There is a newer version of this series
[PATCH 3/3] net: marvell: prestera: force good base mac
Posted by Elad Nachman 1 year, 11 months ago
From: Elad Nachman <enachman@marvell.com>

Since each switchport MAC address uses the switch base mac address
and adds the physical port number to it,
Force the last byte of the switch base mac address to be at
least 128, so when adding to it, we will not wrap around the
previous (more significant) mac address byte, resulting in a
warning message.

Signed-off-by: Elad Nachman <enachman@marvell.com>
---
 drivers/net/ethernet/marvell/prestera/prestera_main.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/drivers/net/ethernet/marvell/prestera/prestera_main.c b/drivers/net/ethernet/marvell/prestera/prestera_main.c
index bcaa8ea27b49..e17b1a24fe18 100644
--- a/drivers/net/ethernet/marvell/prestera/prestera_main.c
+++ b/drivers/net/ethernet/marvell/prestera/prestera_main.c
@@ -859,7 +859,9 @@ static int prestera_switch_set_base_mac_addr(struct prestera_switch *sw)
 	if (sw->np)
 		ret = of_get_mac_address(sw->np, sw->base_mac);
 	if (!is_valid_ether_addr(sw->base_mac) || ret) {
-		eth_random_addr(sw->base_mac);
+		do {
+			eth_random_addr(sw->base_mac);
+		} while (sw->base_mac[5] > 0x80);
 		dev_info(prestera_dev(sw), "using random base mac address\n");
 	}
 
-- 
2.25.1
Re: [PATCH 3/3] net: marvell: prestera: force good base mac
Posted by Andrew Lunn 1 year, 11 months ago
On Mon, Mar 11, 2024 at 03:51:12PM +0200, Elad Nachman wrote:
> From: Elad Nachman <enachman@marvell.com>
> 
> Since each switchport MAC address uses the switch base mac address
> and adds the physical port number to it,
> Force the last byte of the switch base mac address to be at
> least 128, so when adding to it, we will not wrap around the
> previous (more significant) mac address byte, resulting in a
> warning message.

It is not clear to me what the real problem is here. Does the hardware
require that the MAC addresses always have the same upper 5 bytes?

	Andrew
Re: [PATCH 3/3] net: marvell: prestera: force good base mac
Posted by Przemek Kitszel 1 year, 11 months ago
On 3/11/24 14:51, Elad Nachman wrote:
> From: Elad Nachman <enachman@marvell.com>
> 
> Since each switchport MAC address uses the switch base mac address
> and adds the physical port number to it,
> Force the last byte of the switch base mac address to be at
> least 128, so when adding to it, we will not wrap around the

to be at *most* 128

> previous (more significant) mac address byte, resulting in a
> warning message.
> 
> Signed-off-by: Elad Nachman <enachman@marvell.com>
> ---
>   drivers/net/ethernet/marvell/prestera/prestera_main.c | 4 +++-
>   1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/drivers/net/ethernet/marvell/prestera/prestera_main.c b/drivers/net/ethernet/marvell/prestera/prestera_main.c
> index bcaa8ea27b49..e17b1a24fe18 100644
> --- a/drivers/net/ethernet/marvell/prestera/prestera_main.c
> +++ b/drivers/net/ethernet/marvell/prestera/prestera_main.c
> @@ -859,7 +859,9 @@ static int prestera_switch_set_base_mac_addr(struct prestera_switch *sw)
>   	if (sw->np)
>   		ret = of_get_mac_address(sw->np, sw->base_mac);
>   	if (!is_valid_ether_addr(sw->base_mac) || ret) {
> -		eth_random_addr(sw->base_mac);
> +		do {
> +			eth_random_addr(sw->base_mac);
> +		} while (sw->base_mac[5] > 0x80);

Instead of this loop, that uses 6 bytes of random data at each step,
I would just fix the last byte.

Either by calling get_random_u8() in a loop, or perhaps better, just
toggle of MSB unconditionally:
sw->base_mac[5] &= ~0x80; // or '&= 127'

what would change your condition in commit message to "to be at most
127", but I think that should be fine, granted simpler code.

>   		dev_info(prestera_dev(sw), "using random base mac address\n");
>   	}
>