[PATCH 6/8] nvmem: transformations: ethernet address offset support
From: Michael Walle
Date: Tue Dec 28 2021 - 09:26:32 EST
An nvmem cell might just contain a base MAC address. To generate a
address of a specific interface, add a transformation to add an offset
to this base address.
Add a generic implementation and the first user of it, namely the sl28
vpd storage.
Signed-off-by: Michael Walle <michael@xxxxxxxx>
---
drivers/nvmem/transformations.c | 45 +++++++++++++++++++++++++++++++++
1 file changed, 45 insertions(+)
diff --git a/drivers/nvmem/transformations.c b/drivers/nvmem/transformations.c
index 61642a9feefb..15cd26da1f83 100644
--- a/drivers/nvmem/transformations.c
+++ b/drivers/nvmem/transformations.c
@@ -12,7 +12,52 @@ struct nvmem_transformations {
nvmem_cell_post_process_t pp;
};
+/**
+ * nvmem_transform_mac_address_offset() - Add an offset to a mac address cell
+ *
+ * A simple transformation which treats the index argument as an offset and add
+ * it to a mac address. This is useful, if the nvmem cell stores a base
+ * ethernet address.
+ *
+ * @index: nvmem cell index
+ * @data: nvmem data
+ * @bytes: length of the data
+ *
+ * Return: 0 or negative error code on failure.
+ */
+static int nvmem_transform_mac_address_offset(int index, unsigned int offset,
+ void *data, size_t bytes)
+{
+ if (bytes != ETH_ALEN)
+ return -EINVAL;
+
+ if (index < 0)
+ return -EINVAL;
+
+ if (!is_valid_ether_addr(data))
+ return -EINVAL;
+
+ eth_addr_add(data, index);
+
+ return 0;
+}
+
+static int nvmem_kontron_sl28_vpd_pp(void *priv, const char *id, int index,
+ unsigned int offset, void *data,
+ size_t bytes)
+{
+ if (!id)
+ return 0;
+
+ if (!strcmp(id, "mac-address"))
+ return nvmem_transform_mac_address_offset(index, offset, data,
+ bytes);
+
+ return 0;
+}
+
static const struct nvmem_transformations nvmem_transformations[] = {
+ { .compatible = "kontron,sl28-vpd", .pp = nvmem_kontron_sl28_vpd_pp },
{}
};
--
2.30.2