Re: [PATCH] ethernet: aquantia: Try MAC address from device tree

From: Heiner Kallweit
Date: Mon Nov 29 2021 - 17:34:12 EST


On 28.11.2021 03:37, Tianhao Chai wrote:
> Apple M1 Mac minis (2020) with 10GE NICs do not have MAC address in the
> card, but instead need to obtain MAC addresses from the device tree. In
> this case the hardware will report an invalid MAC.
>
> Currently atlantic driver does not query the DT for MAC address and will
> randomly assign a MAC if the NIC doesn't have a permanent MAC burnt in.
> This patch causes the driver to perfer a valid MAC address from OF (if
> present) over HW self-reported MAC and only fall back to a random MAC
> address when neither of them is valid.
>
> Signed-off-by: Tianhao Chai <cth451@xxxxxxxxx>
> ---
> .../net/ethernet/aquantia/atlantic/aq_nic.c | 28 ++++++++++++-------
> 1 file changed, 18 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/net/ethernet/aquantia/atlantic/aq_nic.c b/drivers/net/ethernet/aquantia/atlantic/aq_nic.c
> index 1acf544afeb4..ae6c4a044390 100644
> --- a/drivers/net/ethernet/aquantia/atlantic/aq_nic.c
> +++ b/drivers/net/ethernet/aquantia/atlantic/aq_nic.c
> @@ -316,18 +316,26 @@ int aq_nic_ndev_register(struct aq_nic_s *self)
> aq_macsec_init(self);
> #endif
>
> - mutex_lock(&self->fwreq_mutex);
> - err = self->aq_fw_ops->get_mac_permanent(self->aq_hw, addr);
> - mutex_unlock(&self->fwreq_mutex);
> - if (err)
> - goto err_exit;
> + if (eth_platform_get_mac_address(&self->pdev->dev, addr) == 0 &&
> + is_valid_ether_addr(addr)) {

Calling is_valid_ether_addr() shouldn't be needed here. of_get_mac_addr()
does this check already. If you should decide to keep this check:
Kernel doc of is_valid_ether_addr() states that argument must be
word-aligned. So you may need to add a __align(2) to the address char
array definition.

> + // DT supplied a valid MAC address
> + eth_hw_addr_set(self->ndev, addr);
> + } else {
> + // If DT has none or an invalid one, ask device for MAC address
> + mutex_lock(&self->fwreq_mutex);
> + err = self->aq_fw_ops->get_mac_permanent(self->aq_hw, addr);
> + mutex_unlock(&self->fwreq_mutex);
>
> - eth_hw_addr_set(self->ndev, addr);
> + if (err)
> + goto err_exit;
>
> - if (!is_valid_ether_addr(self->ndev->dev_addr) ||
> - !aq_nic_is_valid_ether_addr(self->ndev->dev_addr)) {
> - netdev_warn(self->ndev, "MAC is invalid, will use random.");
> - eth_hw_addr_random(self->ndev);
> + if (is_valid_ether_addr(addr) &&
> + aq_nic_is_valid_ether_addr(addr)) {
> + eth_hw_addr_set(self->ndev, addr);
> + } else {
> + netdev_warn(self->ndev, "MAC is invalid, will use random.");
> + eth_hw_addr_random(self->ndev);
> + }
> }
>
> #if defined(AQ_CFG_MAC_ADDR_PERMANENT)
>