[PATCH v2] mtd: core: Add nand_id sysfs attribute for NAND devices
jayxu1990 at gmail.com
jayxu1990 at gmail.com
Tue Oct 14 12:24:55 PDT 2025
From: Jay Xu <jayxu1990 at gmail.com>
[Problem]
Currently, NAND devices do not expose their NAND ID through sysfs,
making it difficult for userspace applications to identify the specific
NAND flash chip in use. For supply management reasons, electronics
products are typically manufactured with multiple storage device
suppliers, creating a need to identify which storage device is used
on a particular product. The NAND ID is a semi-unique identifier that can
be used to determine chip-specific characteristics such as maximum P/E
cycles, which is essential for NAND health monitoring and wear leveling
algorithms.
[Solution]
This patch adds a new 'nand_id' sysfs attribute that:
1. Exposes the full NAND ID (typically 5-8 bytes) in hexadecimal format
2. Only appears on physical NAND devices (MTD_NANDFLASH/MTD_MLCNANDFLASH)
3. Is hidden on virtual MTD devices
4. Reads from the master device to ensure consistent ID across partitions
5. Handles on-demand ID reading if not already populated during probe
The implementation uses a separate attribute group with visibility control
to avoid affecting existing MTD sysfs attributes. All NAND partitions
from the same physical chip will show the same ID, as expected.
The NAND-specific code is conditionally compiled with CONFIG_MTD_RAW_NAND
to ensure clean builds when raw NAND support is not enabled.
This enables userspace tools to reliably identify NAND chips for
health monitoring, bad block management, and device-specific
optimizations.
Reported-by: kernel test robot <lkp at intel.com>
Closes: https://lore.kernel.org/oe-kbuild-all/202510120356.STGKDkA5-lkp@intel.com/
Signed-off-by: Jay Xu <jayxu1990 at gmail.com>
---
drivers/mtd/mtdcore.c | 66 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 65 insertions(+), 1 deletion(-)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 5ba9a741f5ac..215e316194b4 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -34,6 +34,9 @@
#include <linux/mtd/mtd.h>
#include <linux/mtd/partitions.h>
+#ifdef CONFIG_MTD_RAW_NAND
+#include <linux/mtd/rawnand.h>
+#endif
#include "mtdcore.h"
@@ -339,6 +342,56 @@ static ssize_t mtd_bbt_blocks_show(struct device *dev,
}
MTD_DEVICE_ATTR_RO(bbt_blocks);
+#ifdef CONFIG_MTD_RAW_NAND
+static ssize_t mtd_nand_id_show(struct device *dev,
+ struct device_attribute *attr, char *buf)
+{
+ struct mtd_info *mtd = dev_get_drvdata(dev);
+ struct mtd_info *master = mtd_get_master(mtd);
+ struct nand_chip *chip;
+ int ret;
+
+ /* Ensure this is actually a NAND device */
+ if (master->type != MTD_NANDFLASH && master->type != MTD_MLCNANDFLASH)
+ return -ENODEV;
+
+ chip = mtd_to_nand(master);
+
+ /* If ID not populated, try to read it now */
+ if (!chip->id.len) {
+ ret = nand_readid_op(chip, 0, chip->id.data, NAND_MAX_ID_LEN);
+ if (ret)
+ return sysfs_emit(buf, "read-error\n");
+ chip->id.len = strnlen(chip->id.data, NAND_MAX_ID_LEN);
+ }
+
+ return sysfs_emit(buf, "%*phN\n", chip->id.len, chip->id.data);
+}
+MTD_DEVICE_ATTR_RO(nand_id);
+
+static umode_t mtd_nand_id_visible(struct kobject *kobj, struct attribute *attr, int n)
+{
+ struct device *dev = kobj_to_dev(kobj);
+ struct mtd_info *mtd = dev_get_drvdata(dev);
+
+ /* Only show on NAND devices (excludes UBI volumes which have type 'ubi') */
+ if (mtd->type != MTD_NANDFLASH && mtd->type != MTD_MLCNANDFLASH)
+ return 0;
+
+ return attr->mode;
+}
+
+static struct attribute *mtd_nand_attrs[] = {
+ &dev_attr_nand_id.attr,
+ NULL,
+};
+
+static const struct attribute_group mtd_nand_group = {
+ .attrs = mtd_nand_attrs,
+ .is_visible = mtd_nand_id_visible,
+};
+#endif /* CONFIG_MTD_RAW_NAND */
+
static struct attribute *mtd_attrs[] = {
&dev_attr_type.attr,
&dev_attr_flags.attr,
@@ -359,7 +412,18 @@ static struct attribute *mtd_attrs[] = {
&dev_attr_bitflip_threshold.attr,
NULL,
};
-ATTRIBUTE_GROUPS(mtd);
+
+static const struct attribute_group mtd_group = {
+ .attrs = mtd_attrs,
+};
+
+static const struct attribute_group *mtd_groups[] = {
+ &mtd_group,
+#ifdef CONFIG_MTD_RAW_NAND
+ &mtd_nand_group,
+#endif
+ NULL,
+};
static const struct device_type mtd_devtype = {
.name = "mtd",
--
2.47.3
More information about the linux-mtd
mailing list