[PATCH v7 5/9] block: implement NVMEM provider
From: Loic Poulain <loic.poulain@oss.qualcomm.com>
Date: 2026-07-01 16:00:49
Also in:
linux-arm-msm, linux-block, linux-bluetooth, linux-devicetree, linux-mmc, linux-wireless, lkml
Subsystem:
block layer, the rest · Maintainers:
Jens Axboe, Linus Torvalds
From: Daniel Golle <daniel@makrotopia.org> On embedded devices using an eMMC it is common that one or more partitions on the eMMC are used to store MAC addresses and Wi-Fi calibration EEPROM data. Allow referencing the partition in device tree for the kernel and Wi-Fi drivers accessing it via the NVMEM layer. NVMEM is registered for a block device whose OF node describes an NVMEM layout, either via an "nvmem-layout" child or by being a "fixed-layout" node itself (e.g. an eMMC boot partition associated through its mmc-card node). Signed-off-by: Daniel Golle <daniel@makrotopia.org> Co-developed-by: Loic Poulain <loic.poulain@oss.qualcomm.com> Signed-off-by: Loic Poulain <loic.poulain@oss.qualcomm.com> Reviewed-by: Bartosz Golaszewski <redacted> --- block/Kconfig | 9 ++++ block/Makefile | 1 + block/blk-nvmem.c | 109 ++++++++++++++++++++++++++++++++++++++++++++++ block/blk.h | 8 ++++ block/genhd.c | 4 ++ include/linux/blk_types.h | 4 ++ 6 files changed, 135 insertions(+)
diff --git a/block/Kconfig b/block/Kconfig
index 15027963472d7b40e27b9097a5993c457b5b3054..0b33747e16dc33473683706f75c92bdf8b648f7c 100644
--- a/block/Kconfig
+++ b/block/Kconfig@@ -209,6 +209,15 @@ config BLK_INLINE_ENCRYPTION_FALLBACK by falling back to the kernel crypto API when inline encryption hardware is not present. +config BLK_NVMEM + bool "Block device NVMEM provider" + depends on OF + depends on NVMEM + help + Allow block devices (or partitions) to act as NVMEM providers, + typically used with eMMC to store MAC addresses or Wi-Fi + calibration data on embedded devices. + source "block/partitions/Kconfig" config BLK_PM
diff --git a/block/Makefile b/block/Makefile
index 7dce2e44276c4274c11a0a61121c83d9c43d6e0c..d7ac389e71902bc091a8800ea266190a43b3e63d 100644
--- a/block/Makefile
+++ b/block/Makefile@@ -36,3 +36,4 @@ obj-$(CONFIG_BLK_INLINE_ENCRYPTION) += blk-crypto.o blk-crypto-profile.o \ blk-crypto-sysfs.o obj-$(CONFIG_BLK_INLINE_ENCRYPTION_FALLBACK) += blk-crypto-fallback.o obj-$(CONFIG_BLOCK_HOLDER_DEPRECATED) += holder.o +obj-$(CONFIG_BLK_NVMEM) += blk-nvmem.o
diff --git a/block/blk-nvmem.c b/block/blk-nvmem.c
new file mode 100644
index 0000000000000000000000000000000000000000..49e9b5d4410d5e935c4ad9674c6909453fe027ff
--- /dev/null
+++ b/block/blk-nvmem.c@@ -0,0 +1,109 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* + * block device NVMEM provider + * + * Copyright (c) 2024 Daniel Golle <daniel@makrotopia.org> + * Copyright (c) Qualcomm Technologies, Inc. and/or its subsidiaries. + * + * Useful on devices using a partition on an eMMC for MAC addresses or + * Wi-Fi calibration EEPROM data. + */ + +#include <linux/cleanup.h> +#include <linux/device.h> +#include <linux/file.h> +#include <linux/nvmem-provider.h> +#include <linux/nvmem-consumer.h> +#include <linux/of.h> +#include <linux/pagemap.h> +#include <linux/property.h> + +#include "blk.h" + +static int blk_nvmem_reg_read(void *priv, unsigned int from, void *val, size_t bytes) +{ + dev_t devt = (dev_t)(uintptr_t)priv; + size_t bytes_left = bytes; + loff_t pos = from; + int ret = 0; + + struct file *bdev_file __free(fput) = + bdev_file_open_by_dev(devt, BLK_OPEN_READ, NULL, NULL); + if (IS_ERR(bdev_file)) + return PTR_ERR(bdev_file); + + while (bytes_left) { + pgoff_t f_index = pos >> PAGE_SHIFT; + struct folio *folio; + size_t folio_off; + size_t to_read; + + folio = read_mapping_folio(bdev_file->f_mapping, f_index, NULL); + if (IS_ERR(folio)) { + ret = PTR_ERR(folio); + break; + } + + folio_off = offset_in_folio(folio, pos); + to_read = min(bytes_left, folio_size(folio) - folio_off); + memcpy_from_folio(val, folio, folio_off, to_read); + pos += to_read; + bytes_left -= to_read; + val += to_read; + folio_put(folio); + } + + return ret; +} + +int blk_nvmem_add(struct block_device *bdev) +{ + struct device *dev = &bdev->bd_device; + struct nvmem_config config = {}; + + /* skip devices which do not have a device tree node */ + if (!dev_of_node(dev)) + return 0; + + /* skip devices without an nvmem layout defined */ + struct device_node *child __free(device_node) = + of_get_child_by_name(dev_of_node(dev), "nvmem-layout"); + if (!child && !of_device_is_compatible(dev_of_node(dev), "fixed-layout")) + return 0; + + /* + * skip block device too large to be represented as NVMEM devices, + * nvmem_config.size is a signed int + */ + if (bdev_nr_bytes(bdev) > INT_MAX) { + dev_warn(dev, "block device too large to be an NVMEM provider\n"); + return 0; + } + + config.id = NVMEM_DEVID_NONE; + config.dev = dev; + config.name = dev_name(dev); + config.owner = THIS_MODULE; + config.priv = (void *)(uintptr_t)dev->devt; + config.reg_read = blk_nvmem_reg_read; + config.size = bdev_nr_bytes(bdev); + config.word_size = 1; + config.stride = 1; + config.read_only = true; + config.root_only = true; + config.ignore_wp = true; + config.of_node = to_of_node(dev->fwnode); + + bdev->bd_nvmem = nvmem_register(&config); + if (IS_ERR(bdev->bd_nvmem)) + return dev_err_probe(dev, PTR_ERR(bdev->bd_nvmem), + "Failed to register NVMEM device\n"); + + return 0; +} + +void blk_nvmem_del(struct block_device *bdev) +{ + nvmem_unregister(bdev->bd_nvmem); + bdev->bd_nvmem = NULL; +}
diff --git a/block/blk.h b/block/blk.h
index ec4674cdf2ead4fd259ff5fc42401f591e684ee9..ed0c10168ba7be10855509637f824a9cea2b9ccb 100644
--- a/block/blk.h
+++ b/block/blk.h@@ -757,4 +757,12 @@ static inline void blk_debugfs_unlock(struct request_queue *q, memalloc_noio_restore(memflags); } +#ifdef CONFIG_BLK_NVMEM +int blk_nvmem_add(struct block_device *bdev); +void blk_nvmem_del(struct block_device *bdev); +#else +static inline int blk_nvmem_add(struct block_device *bdev) { return 0; } +static inline void blk_nvmem_del(struct block_device *bdev) {} +#endif + #endif /* BLK_INTERNAL_H */
diff --git a/block/genhd.c b/block/genhd.c
index 7d6854fd28e95ae9134309679a7c6a937f5b7db8..1b2382de6fb30c1e5f60f45c04dc03ed3bf5d5f2 100644
--- a/block/genhd.c
+++ b/block/genhd.c@@ -421,6 +421,8 @@ static void add_disk_final(struct gendisk *disk) */ dev_set_uevent_suppress(ddev, 0); disk_uevent(disk, KOBJ_ADD); + + blk_nvmem_add(disk->part0); } blk_apply_bdi_limits(disk->bdi, &disk->queue->limits);
@@ -704,6 +706,8 @@ static void __del_gendisk(struct gendisk *disk) disk_del_events(disk); + blk_nvmem_del(disk->part0); + /* * Prevent new openers by unlinked the bdev inode. */
diff --git a/include/linux/blk_types.h b/include/linux/blk_types.h
index 8808ee76e73c09e0ceaac41ba59e86fb0c4efc64..6ed173c649025b95cce9253b27f68f2c7dbab8eb 100644
--- a/include/linux/blk_types.h
+++ b/include/linux/blk_types.h@@ -15,6 +15,7 @@ struct bio_set; struct bio; struct bio_integrity_payload; +struct nvmem_device; struct page; struct io_context; struct cgroup_subsys_state;
@@ -73,6 +74,9 @@ struct block_device { int bd_writers; #ifdef CONFIG_SECURITY void *bd_security; +#endif +#ifdef CONFIG_BLK_NVMEM + struct nvmem_device *bd_nvmem; #endif /* * keep this out-of-line as it's both big and not needed in the fast
--
2.34.1