of: net: add a helper for loading netdev->dev_addr
authorJakub Kicinski <kuba@kernel.org>
Thu, 7 Oct 2021 01:06:55 +0000 (18:06 -0700)
committerDavid S. Miller <davem@davemloft.net>
Thu, 7 Oct 2021 12:39:51 +0000 (13:39 +0100)
Commit 406f42fa0d3c ("net-next: When a bond have a massive amount
of VLANs...") introduced a rbtree for faster Ethernet address look
up. To maintain netdev->dev_addr in this tree we need to make all
the writes to it got through appropriate helpers.

There are roughly 40 places where netdev->dev_addr is passed
as the destination to a of_get_mac_address() call. Add a helper
which takes a dev pointer instead, so it can call an appropriate
helper.

Note that of_get_mac_address() already assumes the address is
6 bytes long (ETH_ALEN) so use eth_hw_addr_set().

Signed-off-by: Jakub Kicinski <kuba@kernel.org>
Signed-off-by: David S. Miller <davem@davemloft.net>
include/linux/of_net.h
net/core/of_net.c

index cf31188..0797e2e 100644 (file)
@@ -14,6 +14,7 @@
 struct net_device;
 extern int of_get_phy_mode(struct device_node *np, phy_interface_t *interface);
 extern int of_get_mac_address(struct device_node *np, u8 *mac);
+int of_get_ethdev_address(struct device_node *np, struct net_device *dev);
 extern struct net_device *of_find_net_device_by_node(struct device_node *np);
 #else
 static inline int of_get_phy_mode(struct device_node *np,
@@ -27,6 +28,11 @@ static inline int of_get_mac_address(struct device_node *np, u8 *mac)
        return -ENODEV;
 }
 
+static inline int of_get_ethdev_address(struct device_node *np, struct net_device *dev)
+{
+       return -ENODEV;
+}
+
 static inline struct net_device *of_find_net_device_by_node(struct device_node *np)
 {
        return NULL;
index dbac3a1..f1a9bf7 100644 (file)
@@ -143,3 +143,28 @@ int of_get_mac_address(struct device_node *np, u8 *addr)
        return of_get_mac_addr_nvmem(np, addr);
 }
 EXPORT_SYMBOL(of_get_mac_address);
+
+/**
+ * of_get_ethdev_address()
+ * @np:                Caller's Device Node
+ * @dev:       Pointer to netdevice which address will be updated
+ *
+ * Search the device tree for the best MAC address to use.
+ * If found set @dev->dev_addr to that address.
+ *
+ * See documentation of of_get_mac_address() for more information on how
+ * the best address is determined.
+ *
+ * Return: 0 on success and errno in case of error.
+ */
+int of_get_ethdev_address(struct device_node *np, struct net_device *dev)
+{
+       u8 addr[ETH_ALEN];
+       int ret;
+
+       ret = of_get_mac_address(np, addr);
+       if (!ret)
+               eth_hw_addr_set(dev, addr);
+       return ret;
+}
+EXPORT_SYMBOL(of_get_ethdev_address);