-From 639dba857aa554f2a78572adc4cf3c32de9ec2e2 Mon Sep 17 00:00:00 2001
+From 844c273286f328acf0dab5fbd5d864366b4904dc Mon Sep 17 00:00:00 2001
From: Ansuel Smith <ansuelsmth@gmail.com>
Date: Tue, 30 Mar 2021 18:21:14 +0200
-Subject: [PATCH 2/2] of_net: add mac-address-increment support
+Subject: [PATCH] of_net: add mac-address-increment support
Lots of embedded devices use the mac-address of other interface
extracted from nvmem cells and increments it by one or two. Add two
Signed-off-by: Ansuel Smith <ansuelsmth@gmail.com>
---
- drivers/of/of_net.c | 59 ++++++++++++++++++++++++++++++++++-----------
- 1 file changed, 45 insertions(+), 14 deletions(-)
+ drivers/of/of_net.c | 43 +++++++++++++++++++++++++++++++++++++++----
+ 1 file changed, 39 insertions(+), 4 deletions(-)
--- a/drivers/of/of_net.c
+++ b/drivers/of/of_net.c
-@@ -55,31 +55,36 @@ static void *of_get_mac_addr(struct devi
- return NULL;
- }
-
--static const void *of_get_mac_addr_nvmem(struct device_node *np)
-+static void *of_get_mac_addr_nvmem(struct device_node *np, int *err)
- {
- int ret;
-- const void *mac;
-+ void *mac;
- u8 nvmem_mac[ETH_ALEN];
- struct platform_device *pdev = of_find_device_by_node(np);
-
-- if (!pdev)
-- return ERR_PTR(-ENODEV);
-+ if (!pdev) {
-+ *err = -ENODEV;
-+ return NULL;
-+ }
-
- ret = nvmem_get_mac_address(&pdev->dev, &nvmem_mac);
- if (ret) {
- put_device(&pdev->dev);
-- return ERR_PTR(ret);
-+ *err = ret;
-+ return NULL;
- }
-
- mac = devm_kmemdup(&pdev->dev, nvmem_mac, ETH_ALEN, GFP_KERNEL);
- put_device(&pdev->dev);
-- if (!mac)
-- return ERR_PTR(-ENOMEM);
-+ if (!mac) {
-+ *err = -ENOMEM;
-+ return NULL;
-+ }
-
- return mac;
- }
-
--static const void *of_get_mac_address_mtd(struct device_node *np)
-+static void *of_get_mac_address_mtd(struct device_node *np)
- {
- #ifdef CONFIG_MTD
- struct platform_device *pdev = of_find_device_by_node(np);
-@@ -153,28 +158,54 @@ static const void *of_get_mac_address_mt
- * If a mtd-mac-address property exists, try to fetch the MAC address from the
- * specified mtd device.
+@@ -115,27 +115,62 @@ static int of_get_mac_addr_nvmem(struct
+ * this case, the real MAC is in 'local-mac-address', and 'mac-address' exists
+ * but is all zeros.
*
+ * DT can tell the system to increment the mac-address after is extracted by
+ * using:
+ * - mac-address-increment-byte to decide what byte to increase
+ * (if not defined is increased the last byte)
-+ * - mac-address-increment to decide how much to increase. The value will
-+ * not overflow to other bytes if the increment is over 255.
-+ * (example 00:01:02:03:04:ff + 1 == 00:01:02:03:04:00)
++ * - mac-address-increment to decide how much to increase. The value WILL
++ * overflow to other bytes if the increment is over 255 or the total
++ * increment will exceed 255 of the current byte.
++ * (example 00:01:02:03:04:ff + 1 == 00:01:02:03:05:00)
++ * (example 00:01:02:03:04:fe + 5 == 00:01:02:03:05:03)
+ *
- * Return: Will be a valid pointer on success and ERR_PTR in case of error.
+ * Return: 0 on success and errno in case of error.
*/
- const void *of_get_mac_address(struct device_node *np)
+ int of_get_mac_address(struct device_node *np, u8 *addr)
{
-- const void *addr;
-+ u32 inc_idx, mac_inc;
-+ int ret = 0;
-+ u8 *addr;
-+
++ u32 inc_idx, mac_inc, mac_val;
+ int ret;
+
+ /* Check first if the increment byte is present and valid.
+ * If not set assume to increment the last byte if found.
+ */
+ if (of_property_read_u32(np, "mac-address-increment-byte", &inc_idx))
+ inc_idx = 5;
+ if (inc_idx < 3 || inc_idx > 5)
-+ return ERR_PTR(-EINVAL);
-
- addr = of_get_mac_addr(np, "mac-address");
- if (addr)
-- return addr;
-+ goto found;
++ return -EINVAL;
++
+ if (!np)
+ return -ENODEV;
- addr = of_get_mac_addr(np, "local-mac-address");
- if (addr)
-- return addr;
+ ret = of_get_mac_addr(np, "mac-address", addr);
+ if (!ret)
+- return 0;
+ goto found;
- addr = of_get_mac_addr(np, "address");
- if (addr)
-- return addr;
+ ret = of_get_mac_addr(np, "local-mac-address", addr);
+ if (!ret)
+- return 0;
+ goto found;
- addr = of_get_mac_address_mtd(np);
- if (addr)
-- return addr;
+ ret = of_get_mac_addr(np, "address", addr);
+ if (!ret)
+- return 0;
+ goto found;
+
-+ addr = of_get_mac_addr_nvmem(np, &ret);
++ ret = of_get_mac_addr_nvmem(np, addr);
+ if (ret)
-+ return ERR_PTR(ret);
++ return ret;
+
+found:
-+ if (!of_property_read_u32(np, "mac-address-increment", &mac_inc))
-+ addr[inc_idx] += mac_inc;
++ if (!of_property_read_u32(np, "mac-address-increment", &mac_inc)) {
++ /* Convert to a contiguous value */
++ mac_val = (addr[3] << 16) + (addr[4] << 8) + addr[5];
++ mac_val += mac_inc << 8 * (5-inc_idx);
++
++ /* Apply the incremented value handling overflow case */
++ addr[3] = (mac_val >> 16) & 0xff;
++ addr[4] = (mac_val >> 8) & 0xff;
++ addr[5] = (mac_val >> 0) & 0xff;
++ }
-- return of_get_mac_addr_nvmem(np);
-+ return addr;
+- return of_get_mac_addr_nvmem(np, addr);
++ return ret;
}
EXPORT_SYMBOL(of_get_mac_address);