mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: Oleksij Rempel <o.rempel@pengutronix.de>, barebox@lists.infradead.org
Subject: Re: [PATCH v2 10/10] ARM: i.MX8MP: skov: assign Ethernet addresses to all ports
Date: Thu, 5 Oct 2023 08:52:10 +0200	[thread overview]
Message-ID: <281cf0d5-084d-4f64-6a00-e48e5d7e7b48@pengutronix.de> (raw)
In-Reply-To: <20231002101654.2373000-11-o.rempel@pengutronix.de>

Hello Oleksij,

On 02.10.23 12:16, Oleksij Rempel wrote:
> Assign Ethernet address to all ports based on the master address stored
> in the NVMEM. In case nothing nvmem is clean, generate address based on
> vendor OUI with part of i.MX8MP unique ID.
> 
> Signed-off-by: Oleksij Rempel <o.rempel@pengutronix.de>
> ---
>  arch/arm/boards/skov-imx8mp/board.c | 97 +++++++++++++++++++++++++++++
>  1 file changed, 97 insertions(+)
> 
> diff --git a/arch/arm/boards/skov-imx8mp/board.c b/arch/arm/boards/skov-imx8mp/board.c
> index 3b6eb7b080..6de9ab47ab 100644
> --- a/arch/arm/boards/skov-imx8mp/board.c
> +++ b/arch/arm/boards/skov-imx8mp/board.c
> @@ -12,13 +12,18 @@
>  #include <io.h>
>  #include <mach/imx/bbu.h>
>  #include <mach/imx/generic.h>
> +#include <mach/imx/imx8mq.h>
>  #include <mach/imx/iomux-mx8mp.h>
> +#include <net.h>
> +#include <of_net.h>
>  
>  struct skov_imx8mp_priv {
>  	struct device *dev;
>  	int variant_id;
>  };
>  
> +#define SKOV_OUI  {0x00, 0x0e, 0xcd}
> +
>  static struct skov_imx8mp_priv *skov_imx8mp_priv;
>  
>  #define GPIO_HW_VARIANT  {\
> @@ -85,6 +90,98 @@ static const struct board_description imx8mp_variants[] = {
>  	},
>  };
>  
> +static struct eth_device *
> +skov_imx8mp_init_master_edev(struct skov_imx8mp_priv *priv,
> +			     struct device_node *np)
> +{
> +	u8 oui_mac[ETH_ALEN] = SKOV_OUI;
> +	struct eth_device *edev;
> +
> +	edev = of_find_eth_device_by_node(np);
> +	if (!edev) {
> +		dev_err(priv->dev, "Failed to find master eth device\n");
> +		return NULL;
> +	}
> +
> +	if (is_valid_ether_addr(edev->ethaddr))
> +		return edev;
> +
> +	if (!edev->parent || of_get_mac_addr_nvmem(edev->parent->of_node,
> +						   oui_mac)) {
> +		char str[sizeof("xx:xx:xx:xx:xx:xx")];
> +		u64 unique_id;
> +
> +		unique_id = imx8m_uid();
> +		if (!unique_id)
> +			dev_err(priv->dev, "Failed to get i.MX8MP unique ID\n");
> +
> +		/* Generate MAC address based on i.MX8MP unique ID */
> +		oui_mac[3] = (unique_id >> 56) & 0xff;
> +		oui_mac[4] = (unique_id >> 48) & 0xff;
> +		oui_mac[5] = (unique_id >> 40) & 0xff;
> +		ethaddr_to_string(oui_mac, str);
> +		dev_warn(priv->dev, "Failed to get master eth addr. Generating based on i.MX8MP unique ID: %s\n",
> +			 str);

I attempted to solve this generically in [1]. I'd prefer to get that in instead of
board-specific measures. I also think that it might be a bad idea to expose the
unique ID like that as this allows easy brute forcing of the machine ID that's
generated by default out of the UID.

[1]: https://lore.barebox.org/barebox/20230911155927.3786335-1-a.fatoum@pengutronix.de/

> +	}
> +
> +	of_eth_register_ethaddr(np, oui_mac);
> +
> +	return edev;
> +}
> +
> +static void skov_imx8mp_register_ethaddr(struct skov_imx8mp_priv *priv,
> +					 struct device_node *root)
> +{
> +	struct eth_device *master_edev;
> +	struct eth_node_info {
> +		const char *alias;
> +		struct device_node *np;
> +		u32 inc;
> +	} eth_nodes[] = {
> +		{ "ethernet0", NULL, 0 },
> +		/* Addresses are assigned in the reverse order
> +		 * LAN2 addr < LAN1 addr
> +		 */
> +		{ "ethernet1", NULL, 1 },
> +		{ "ethernet2", NULL, 0 },
> +	};
> +	int i;
> +
> +	for (i = 0; i < ARRAY_SIZE(eth_nodes); i++) {
> +		eth_nodes[i].np = of_find_node_by_alias(root,
> +							eth_nodes[i].alias);
> +		if (!eth_nodes[i].np) {
> +			dev_err(priv->dev, "Failed to find %s alias\n",
> +				eth_nodes[i].alias);
> +			return;
> +		}
> +	}
> +
> +	master_edev = skov_imx8mp_init_master_edev(priv, eth_nodes[0].np);
> +	if (!master_edev) {
> +		dev_err(priv->dev, "Failed to init master edev\n");
> +		return;
> +	}
> +
> +	/* Set ETH addresses for all but master edev */
> +	for (i = 1; i < ARRAY_SIZE(eth_nodes); i++) {
> +		u8 ethaddr[ETH_ALEN];
> +
> +		ether_addr_inc(ethaddr, master_edev->ethaddr, eth_nodes[i].inc);
> +		of_eth_register_ethaddr(eth_nodes[i].np, ethaddr);
> +	}
> +}
> +
> +static int skov_imx8mp_populate_ethaddr(void)
> +{
> +	struct skov_imx8mp_priv *priv = skov_imx8mp_priv;
> +
> +	skov_imx8mp_register_ethaddr(priv, NULL);
> +
> +	return 0;
> +}
> +postenvironment_initcall(skov_imx8mp_populate_ethaddr);
> +
>  static int skov_imx8mp_fixup(struct device_node *root, void *data)
>  {
>  	struct device_node *chosen = of_create_node(root, "/chosen");

-- 
Pengutronix e.K.                           |                             |
Steuerwalder Str. 21                       | http://www.pengutronix.de/  |
31137 Hildesheim, Germany                  | Phone: +49-5121-206917-0    |
Amtsgericht Hildesheim, HRA 2686           | Fax:   +49-5121-206917-5555 |




      reply	other threads:[~2023-10-05  6:53 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-02 10:16 [PATCH v2 00/10] SKOV maintenance Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 01/10] arm: dts: imx8mp-skov: Add reserved-memory for ramoops pstore Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 02/10] arm: dts: imx8mp-skov: Add pins for hardware variant detection Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 03/10] arm: dts: imx8mp-skov: Switch to GPT for eMMC partitioning Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 04/10] arm: dts: imx8mp-skov: Switch to GPT for SD partitioning Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 05/10] arm: dts: imx8mp-skov: Add barebox state backend in DTS Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 06/10] ARM: i.MX8MP: skov: refactor bootsource and BBU handlers Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 07/10] ARM: i.MX8MP: skov: Add hardware variant support Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 08/10] ARM: i.MX8MP: skov: fixup skov,imx8mp-board-version DT property for the kernel Oleksij Rempel
2023-10-02 10:16 ` [PATCH v2 09/10] net: lib: add ether_addr_inc() helper Oleksij Rempel
2023-10-04  6:41   ` Sascha Hauer
2023-10-05  6:48     ` Ahmad Fatoum
2023-10-05  7:07       ` Oleksij Rempel
2023-10-06 11:44         ` Sascha Hauer
2023-10-02 10:16 ` [PATCH v2 10/10] ARM: i.MX8MP: skov: assign Ethernet addresses to all ports Oleksij Rempel
2023-10-05  6:52   ` Ahmad Fatoum [this message]

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=281cf0d5-084d-4f64-6a00-e48e5d7e7b48@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=o.rempel@pengutronix.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