Skip to contentSkip to conversation

nvmem: brcm_nvram: store a copy of NVRAM content

PATCH Superseded Rafał Miłecki 5 participants 8 messages Sep 14, 2023 – Sep 15, 2023

Subsystems (2)

v1 of 3 Superseded Latest is v3 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 nvmem: brcm_nvram: store a copy of NVRAM content

From: Rafał Miłecki <[email protected]>

This driver uses MMIO access for reading NVRAM from a flash device. Underneath there is a flash controller that reads data and provides mapping window.

Using MMIO interface affects controller configuration and may break real controller driver. It was reported by multiple users of devices with NVRAM stored on NAND.

Modify driver to read & cache all NVRAM content during init and use that copy to provide NVMEM data when requested.

---
 drivers/nvmem/brcm_nvram.c | 77 +++++++++++++++++++++-----------------
 1 file changed, 43 insertions(+), 34 deletions(-)
drivers/nvmem/brcm_nvram.c +43 -34
index 9737104f3b76..cfc1c485e20a 100644
@@ -19,7 +19,8 @@
 
 struct brcm_nvram {
 	struct device *dev;
-	void __iomem *base;
+	uint8_t *data;
+	size_t size;
 	struct nvmem_cell_info *cells;
 	int ncells;
 };
@@ -36,10 +37,8 @@ static int brcm_nvram_read(void *context, unsigned int offset, void *val,
 			   size_t bytes)
 {
 	struct brcm_nvram *priv = context;
-	u8 *dst = val;
 
-	while (bytes--)
-		*dst++ = readb(priv->base + offset++);
+	memcpy(val, priv->data + offset, bytes);
 
 	return 0;
 }
@@ -68,7 +67,12 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data,
 {
 	struct device *dev = priv->dev;
 	char *var, *value, *eq;
+	uint8_t tmp;
 	int idx;
+	int err = 0;
+
+	tmp = priv->data[len - 1];
+	priv->data[len - 1] = '\0';
 
 	priv->ncells = 0;
 	for (var = data + sizeof(struct brcm_nvram_header);
@@ -78,8 +82,10 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data,
 	}
 
 	priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
-	if (!priv->cells)
-		return -ENOMEM;
+	if (!priv->cells) {
+		err = -ENOMEM;
+		goto out;
+	}
 
 	for (var = data + sizeof(struct brcm_nvram_header), idx = 0;
 	     var < (char *)data + len && *var;
@@ -91,8 +97,10 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data,
 		value = eq + 1;
 
 		priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
-		if (!priv->cells[idx].name)
-			return -ENOMEM;
+		if (!priv->cells[idx].name) {
+			err = -ENOMEM;
+			goto out;
+		}
 		priv->cells[idx].offset = value - (char *)data;
 		priv->cells[idx].bytes = strlen(value);
 		priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
@@ -105,40 +113,32 @@ static int brcm_nvram_add_cells(struct brcm_nvram *priv, uint8_t *data,
 		}
 	}
 
-	return 0;
+out:
+	priv->data[len - 1] = tmp;
+	return err;
 }
 
 static int brcm_nvram_parse(struct brcm_nvram *priv)
 {
+	struct brcm_nvram_header *header = (struct brcm_nvram_header *)priv->data;
 	struct device *dev = priv->dev;
-	struct brcm_nvram_header header;
-	uint8_t *data;
 	size_t len;
 	int err;
 
-	memcpy_fromio(&header, priv->base, sizeof(header));
-
-	if (memcmp(header.magic, NVRAM_MAGIC, 4)) {
+	if (memcmp(header->magic, NVRAM_MAGIC, 4)) {
 		dev_err(dev, "Invalid NVRAM magic\n");
 		return -EINVAL;
 	}
 
-	len = le32_to_cpu(header.len);
-
-	data = kzalloc(len, GFP_KERNEL);
-	if (!data)
-		return -ENOMEM;
-
-	memcpy_fromio(data, priv->base, len);
-	data[len - 1] = '\0';
-
-	err = brcm_nvram_add_cells(priv, data, len);
-	if (err) {
-		dev_err(dev, "Failed to add cells: %d\n", err);
-		return err;
+	len = le32_to_cpu(header->len);
+	if (len > priv->size) {
+		dev_err(dev, "NVRAM length (%zd) exceeds mapped size (%zd)\n", len, priv->size);
+		return -EINVAL;
 	}
 
-	kfree(data);
+	err = brcm_nvram_add_cells(priv, priv->data, len);
+	if (err)
+		dev_err(dev, "Failed to add cells: %d\n", err);
 
 	return 0;
 }
@@ -150,8 +150,9 @@ static int brcm_nvram_probe(struct platform_device *pdev)
 		.reg_read = brcm_nvram_read,
 	};
 	struct device *dev = &pdev->dev;
-	struct resource *res;
 	struct brcm_nvram *priv;
+	struct resource *res;
+	void __iomem *base;
 	int err;
 
 	priv = devm_kzalloc(dev, sizeof(*priv), GFP_KERNEL);
@@ -159,21 +160,29 @@ static int brcm_nvram_probe(struct platform_device *pdev)
 		return -ENOMEM;
 	priv->dev = dev;
 
-	priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
-	if (IS_ERR(priv->base))
-		return PTR_ERR(priv->base);
+	base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
+	if (IS_ERR(base))
+		return PTR_ERR(base);
+
+	priv->size = resource_size(res);
+
+	priv->data = devm_kzalloc(dev, priv->size, GFP_KERNEL);
+	if (!priv->data)
+		return -ENOMEM;
+
+	memcpy_fromio(priv->data, base, priv->size);
 
 	err = brcm_nvram_parse(priv);
 	if (err)
 		return err;
 
-	bcm47xx_nvram_init_from_iomem(priv->base, resource_size(res));
+	bcm47xx_nvram_init_from_iomem(base, priv->size);
 
 	config.dev = dev;
 	config.cells = priv->cells;
 	config.ncells = priv->ncells;
 	config.priv = priv;
-	config.size = resource_size(res);
+	config.size = priv->size;
 
 	return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
 }
Signature · 1 line
2.35.3
#1 lore raw
Quoted 10 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

This driver uses MMIO access for reading NVRAM from a flash device. Underneath there is a flash controller that reads data and provides mapping window.

Using MMIO interface affects controller configuration and may break real controller driver. It was reported by multiple users of devices with NVRAM stored on NAND.

Quoted 8 lines

Modify driver to read & cache all NVRAM content during init and use that copy to provide NVMEM data when requested.

This is a bug fix, could you add a "Fixes" tag so it will be backported to stable releases?

Arınç

#2 lore raw
Quoted 14 lines · Arınç ÜNAL
Quoted 13 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

This driver uses MMIO access for reading NVRAM from a flash device. Underneath there is a flash controller that reads data and provides mapping window.

Using MMIO interface affects controller configuration and may break real controller driver. It was reported by multiple users of devices with NVRAM stored on NAND.

Modify driver to read & cache all NVRAM content during init and use that copy to provide NVMEM data when requested.

Quoted 8 lines
Quoted 5 lines

This is a bug fix, could you add a "Fixes" tag so it will be backported to stable releases?

I suppose we could try:

Fixes 3fef9ed0627a ("nvmem: brcm_nvram: new driver exposing Broadcom's NVRAM")

Srini: can you append it while applying if you find it OK?

Signature · 5 lines
Rafał Miłecki

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/
#3 lore raw
In reply to Rafał Miłecki
Quoted 19 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

This driver uses MMIO access for reading NVRAM from a flash device. Underneath there is a flash controller that reads data and provides mapping window.

Using MMIO interface affects controller configuration and may break real controller driver. It was reported by multiple users of devices with NVRAM stored on NAND.

Modify driver to read & cache all NVRAM content during init and use that copy to provide NVMEM data when requested.

---

[snip]

drivers/nvmem/brcm_nvram.c static int brcm_nvram_probe(struct platform_device *pdev) · line 169

Quoted 10 lines
drivers/nvmem/brcm_nvram.c +7 -3
-	priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
-	if (IS_ERR(priv->base))
-		return PTR_ERR(priv->base);
+	base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
+	if (IS_ERR(base))
+		return PTR_ERR(base);
+
+	priv->size = resource_size(res);
+
+	priv->data = devm_kzalloc(dev, priv->size, GFP_KERNEL);

These can conceivably quite big data structures, how about using kvmalloc()?

Signature · 1 line
Florian
#4 lore raw
Quoted 22 lines · Florian Fainelli
Quoted 19 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

This driver uses MMIO access for reading NVRAM from a flash device. Underneath there is a flash controller that reads data and provides mapping window.

Using MMIO interface affects controller configuration and may break real controller driver. It was reported by multiple users of devices with NVRAM stored on NAND.

Modify driver to read & cache all NVRAM content during init and use that copy to provide NVMEM data when requested.

---

[snip]

Quoted 12 lines

drivers/nvmem/brcm_nvram.c static int brcm_nvram_probe(struct platform_device *pdev) · line 169

Quoted 10 lines
drivers/nvmem/brcm_nvram.c +7 -3
-     priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
-     if (IS_ERR(priv->base))
-             return PTR_ERR(priv->base);
+     base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
+     if (IS_ERR(base))
+             return PTR_ERR(base);
+
+     priv->size = resource_size(res);
+
+     priv->data = devm_kzalloc(dev, priv->size, GFP_KERNEL);

These can conceivably quite big data structures, how about using kvmalloc()?

Why do we even need to expose MMIO interface to NAND though? Why not always go through the controller driver. I don't see how the MMIO access would be used given bad blocks aren't handled?

Quoted 3 lines

--
Florian

#5 lore raw
Quoted 35 lines · Scott Branden
Quoted 22 lines · Florian Fainelli
Quoted 19 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

This driver uses MMIO access for reading NVRAM from a flash device. Underneath there is a flash controller that reads data and provides mapping window.

Using MMIO interface affects controller configuration and may break real controller driver. It was reported by multiple users of devices with NVRAM stored on NAND.

Modify driver to read & cache all NVRAM content during init and use that copy to provide NVMEM data when requested.

---

[snip]

Quoted 12 lines

drivers/nvmem/brcm_nvram.c static int brcm_nvram_probe(struct platform_device *pdev) · line 169

Quoted 10 lines
drivers/nvmem/brcm_nvram.c +7 -3
-     priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
-     if (IS_ERR(priv->base))
-             return PTR_ERR(priv->base);
+     base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
+     if (IS_ERR(base))
+             return PTR_ERR(base);
+
+     priv->size = resource_size(res);
+
+     priv->data = devm_kzalloc(dev, priv->size, GFP_KERNEL);

These can conceivably quite big data structures, how about using kvmalloc()?

Quoted 3 lines

Why do we even need to expose MMIO interface to NAND though? Why not always go through the controller driver. I don't see how the MMIO access would be used given bad blocks aren't handled?

We need to read NVMRAM *early* for booting purposes. Some vendors store there information about used firmware in case of having main one and fallback one. That info is required for partitioning which happens during mtd initialization before we have NAND driver fully initialized.

Signature · 1 line
Rafał Miłecki
#6 lore raw
Quoted 39 lines · Rafał Miłecki
Quoted 37 lines · Scott Branden
Quoted 35 lines · Florian Fainelli
Quoted 19 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

This driver uses MMIO access for reading NVRAM from a flash device. Underneath there is a flash controller that reads data and provides mapping window.

Using MMIO interface affects controller configuration and may break real controller driver. It was reported by multiple users of devices with NVRAM stored on NAND.

Modify driver to read & cache all NVRAM content during init and use that copy to provide NVMEM data when requested.

---

[snip]

drivers/nvmem/brcm_nvram.c static int brcm_nvram_probe(struct platform_device *pdev) · line 169

Quoted 10 lines
drivers/nvmem/brcm_nvram.c +7 -3
-     priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
-     if (IS_ERR(priv->base))
-             return PTR_ERR(priv->base);
+     base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
+     if (IS_ERR(base))
+             return PTR_ERR(base);
+
+     priv->size = resource_size(res);
+
+     priv->data = devm_kzalloc(dev, priv->size, GFP_KERNEL);

These can conceivably quite big data structures, how about using kvmalloc()?

Quoted 8 lines
Quoted 3 lines

Why do we even need to expose MMIO interface to NAND though? Why not always go through the controller driver. I don't see how the MMIO access would be used given bad blocks aren't handled?

We need to read NVMRAM *early* for booting purposes. Some vendors store there information about used firmware in case of having main one and fallback one. That info is required for partitioning which happens during mtd initialization before we have NAND driver fully initialized.

OK, it would have been nice for the bootloader to sort out and retrieve any information. But if you need to access flash in the kernel before the nand driver is loaded so be it. Thanks for the explanation.

Quoted 3 lines

--
Rafał Miłecki

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

#7 lore raw
In reply to Florian Fainelli
Quoted 21 lines · Florian Fainelli
Quoted 19 lines · Rafał Miłecki

From: Rafał Miłecki <[email protected]>

This driver uses MMIO access for reading NVRAM from a flash device. Underneath there is a flash controller that reads data and provides mapping window.

Using MMIO interface affects controller configuration and may break real controller driver. It was reported by multiple users of devices with NVRAM stored on NAND.

Modify driver to read & cache all NVRAM content during init and use that copy to provide NVMEM data when requested.

---

[snip]

Quoted 12 lines

drivers/nvmem/brcm_nvram.c static int brcm_nvram_probe(struct platform_device *pdev) · line 169

Quoted 10 lines
drivers/nvmem/brcm_nvram.c +7 -3
-	priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
-	if (IS_ERR(priv->base))
-		return PTR_ERR(priv->base);
+	base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
+	if (IS_ERR(base))
+		return PTR_ERR(base);
+
+	priv->size = resource_size(res);
+
+	priv->data = devm_kzalloc(dev, priv->size, GFP_KERNEL);

These can conceivably quite big data structures, how about using kvmalloc()?

Good idea.

Actual NVRAM data usually doesn't take much space, but it reserves quite a lot and we need all of that to full expose NVMEM device content.

Signature · 5 lines
Rafał Miłecki

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/