Skip to contentSkip to conversation

mtd: map_rom: Support UBI on ROM

PATCH Applied Aaron Sierra 3 participants 6 messages Sep 25, 2014 – Jan 10, 2015 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

#1 lore raw

PATCH 2/2 mtd: map_rom: Support UBI on ROM

UBI needs to know the physical erase block size, even on read-only devices, since it defines the on-device layout. Use a device-tree provided value to support previously written UBI on read-only NOR.

UBI also needs a non-zero writebufsize, so we set it to one.

Note: This was implemented because hardware write-protected CFI
NOR cannot be probed for the physical erase block size.

---
 Documentation/devicetree/bindings/mtd/mtd-physmap.txt |  5 +++++
 drivers/mtd/chips/map_rom.c                           | 13 ++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/mtd/mtd-physmap.txt +5 -0
index 6b9f680..8ab16df 100644
@@ -36,6 +36,11 @@ are defined:
  - vendor-id : Contains the flash chip's vendor id (1 byte).
  - device-id : Contains the flash chip's device id (1 byte).
 
+For ROM compatible devices (and ROM fallback from cfi-flash), the following
+additional property is defined:
+
+ - erase-size : The chip's physical erase block size in bytes.
+
 The device tree may optionally contain sub-nodes describing partitions of the
 address space. See partition.txt for more detail.
 
drivers/mtd/chips/map_rom.c +12 -1
index 47a43cf..a831265 100644
@@ -11,6 +11,7 @@
 #include <linux/errno.h>
 #include <linux/slab.h>
 #include <linux/init.h>
+#include <linux/of.h>
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/map.h>
 
@@ -28,6 +29,15 @@ static struct mtd_chip_driver maprom_chipdrv = {
 	.module	= THIS_MODULE
 };
 
+static unsigned int default_erasesize(struct map_info *map)
+{
+	const __be32 *erase_size = NULL;
+#ifdef CONFIG_OF
+	erase_size = of_get_property(map->device_node, "erase-size", NULL);
+#endif
+	return !erase_size ? map->size : be32_to_cpu(*erase_size);
+}
+
 static struct mtd_info *map_rom_probe(struct map_info *map)
 {
 	struct mtd_info *mtd;
@@ -47,8 +57,9 @@ static struct mtd_info *map_rom_probe(struct map_info *map)
 	mtd->_sync = maprom_nop;
 	mtd->_erase = maprom_erase;
 	mtd->flags = MTD_CAP_ROM;
-	mtd->erasesize = map->size;
+	mtd->erasesize = default_erasesize(map);
 	mtd->writesize = 1;
+	mtd->writebufsize = 1;
 
 	__module_get(THIS_MODULE);
 	return mtd;
Signature · 1 line
1.9.1
#2 lore raw

Hi Aaron,

I see this has been sitting around way too long...

Quoted 5 lines · Aaron Sierra

UBI needs to know the physical erase block size, even on read-only devices, since it defines the on-device layout. Use a device-tree provided value to support previously written UBI on read-only NOR.

UBI also needs a non-zero writebufsize, so we set it to one.

Hmm, this one is a pretty big bug IIRC (don't you get an OOPS or BUG()?). Do you think it might be worth splitting into a separate patch and sending to stable?

But then I'm curious: how do you get anything useful out of using UBI on a read-only device? You don't need to (and can't) handle anything like read disturb, and there's no write wear that needs to be leveled.

Quoted 40 lines

Note: This was implemented because hardware write-protected CFI
NOR cannot be probed for the physical erase block size.

---
 Documentation/devicetree/bindings/mtd/mtd-physmap.txt |  5 +++++
 drivers/mtd/chips/map_rom.c                           | 13 ++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/mtd/mtd-physmap.txt +5 -0
index 6b9f680..8ab16df 100644
@@ -36,6 +36,11 @@ are defined:
  - vendor-id : Contains the flash chip's vendor id (1 byte).
  - device-id : Contains the flash chip's device id (1 byte).
 
+For ROM compatible devices (and ROM fallback from cfi-flash), the following
+additional property is defined:
+
+ - erase-size : The chip's physical erase block size in bytes.
+
 The device tree may optionally contain sub-nodes describing partitions of the
 address space. See partition.txt for more detail.
 
drivers/mtd/chips/map_rom.c +1 -0
index 47a43cf..a831265 100644
@@ -11,6 +11,7 @@
 #include <linux/errno.h>
 #include <linux/slab.h>
 #include <linux/init.h>
+#include <linux/of.h>
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/map.h>
 

drivers/mtd/chips/map_rom.c static struct mtd_chip_driver maprom_chipdrv = { · line 37

Quoted 9 lines
drivers/mtd/chips/map_rom.c +6 -0
@@ -28,6 +29,15 @@ static struct mtd_chip_driver maprom_chipdrv = {
 	.module	= THIS_MODULE
 };
 
+static unsigned int default_erasesize(struct map_info *map)
+{
+	const __be32 *erase_size = NULL;
+#ifdef CONFIG_OF
+	erase_size = of_get_property(map->device_node, "erase-size", NULL);
+#endif

I think you can just drop the #ifdef and have the same effect, no?

Quoted 7 lines
drivers/mtd/chips/map_rom.c +3 -0
+	return !erase_size ? map->size : be32_to_cpu(*erase_size);
+}
+
 static struct mtd_info *map_rom_probe(struct map_info *map)
 {
 	struct mtd_info *mtd;

drivers/mtd/chips/map_rom.c static struct mtd_info *map_rom_probe(struct map_info *map) · line 65

Quoted 10 lines
drivers/mtd/chips/map_rom.c +2 -1
@@ -47,8 +57,9 @@ static struct mtd_info *map_rom_probe(struct map_info *map)
 	mtd->_sync = maprom_nop;
 	mtd->_erase = maprom_erase;
 	mtd->flags = MTD_CAP_ROM;
-	mtd->erasesize = map->size;
+	mtd->erasesize = default_erasesize(map);
 	mtd->writesize = 1;
+	mtd->writebufsize = 1;
 
 	__module_get(THIS_MODULE);
 	return mtd;

Brian

#3 lore raw
Quoted 3 lines · Brian Norris

But then I'm curious: how do you get anything useful out of using UBI on a read-only device? You don't need to (and can't) handle anything like read disturb, and there's no write wear that needs to be leveled.

Don't know about this particular case, but in some cases it can be an advantage to be able to use the same file system image in both a read-only and a writable environment.

Say you have some form of ROM device with no bit flips, but occasionally (e.g. during development) you want to use the same file system image in a flash. While the same thing can be accomplished by using seperate file systems which share the same content, it can simplify the whole system if only one file system image is needed which can reside in multiple types of media.

/Ricard

Signature · 3 lines
Ricard Wolf Wanderlöf                           ricardw(at)axis.com
Axis Communications AB, Lund, Sweden            www.axis.com
Phone +46 46 272 2016                           Fax +46 46 13 61 30
#4 lore raw

----- Original Message -----

Quoted 13 lines

From: "Ricard Wanderlof" <[email protected]>
Sent: Wednesday, December 17, 2014 3:37:42 AM

Quoted 3 lines · Brian Norris

But then I'm curious: how do you get anything useful out of using UBI on a read-only device? You don't need to (and can't) handle anything like read disturb, and there's no write wear that needs to be leveled.

Don't know about this particular case, but in some cases it can be an advantage to be able to use the same file system image in both a read-only and a writable environment.

Quoted 6 lines

Say you have some form of ROM device with no bit flips, but occasionally (e.g. during development) you want to use the same file system image in a flash. While the same thing can be accomplished by using seperate file systems which share the same content, it can simplify the whole system if only one file system image is needed which can reside in multiple types of media.

Thanks Ricard, you have touched on the scenario that I am concerned about. The environment in our case is a single-board computer that can become read-only based on a hardware jumper or signal from a backplane. It would be tedious to _have_ to prepare a separate image in order to install the SBC into a backplane that forces the NOR into read-only operation. Instead, we find it advantageous to be able to simply freeze the state of the UBI filesytem until the SBC is moved back to a read-write environment for further development.

-Aaron

#5 lore raw
In reply to Brian Norris

Brian, I have responses to your questions inline below.

----- Original Message -----

Quoted 12 lines

From: "Brian Norris" <[email protected]>
Sent: Tuesday, December 16, 2014 8:33:44 PM

Hi Aaron,

I see this has been sitting around way too long...

Quoted 4 lines · Aaron Sierra

UBI needs to know the physical erase block size, even on read-only devices, since it defines the on-device layout. Use a device-tree provided value to support previously written UBI on read-only NOR.

Quoted 5 lines
Quoted 1 line

UBI also needs a non-zero writebufsize, so we set it to one.

Hmm, this one is a pretty big bug IIRC (don't you get an OOPS or BUG()?). Do you think it might be worth splitting into a separate patch and sending to stable?

The following assumes an initially unpatched map_rom.c.

Attempting to ubiattach a ROM partition (/dev/mtd5) with the default map_rom writebufsize set to zero, I get the following error in the kernel log:

UBI: attaching mtd5 to ubi0
UBI error: io_init: bad write buffer size 0 for 1 min. I/O unit
ubiattach: error!: cannot attach "/dev/mtd5"
error 22 (Invalid argument)

After assigning writebufsize to one and recompiling, attempting to ubiattach without specifying an erase-size in the device-tree results in the following error in the kernel log:

UBI: attaching mtd5 to ubi0
UBI: MTD device 5 is write-protected, attach in read-only mode
UBI: scanning is finished
UBI: empty MTD device detected
UBI error: ubi_early_get_peb: no free eraseblocks
UBI error: ubi_attach_mtd_dev: failed to attach mtd5, error -28
ubiattach: error!: cannot attach "/dev/mtd5"
error 28 (No space left on device)

Neither of those errors are horrible, but both result in an inaccessible read-only UBIFS.

Quoted 11 lines
Quoted 11 lines

Note: This was implemented because hardware write-protected CFI
NOR cannot be probed for the physical erase block size.

---
 Documentation/devicetree/bindings/mtd/mtd-physmap.txt |  5 +++++
 drivers/mtd/chips/map_rom.c                           | 13 ++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)

[ snip ]

Quoted 8 lines

drivers/mtd/chips/map_rom.c static struct mtd_chip_driver maprom_chipdrv = { · line 37

Quoted 6 lines
drivers/mtd/chips/map_rom.c +6 -0
+static unsigned int default_erasesize(struct map_info *map)
+{
+	const __be32 *erase_size = NULL;
+#ifdef CONFIG_OF
+	erase_size = of_get_property(map->device_node, "erase-size", NULL);
+#endif

I think you can just drop the #ifdef and have the same effect, no?

Yes, you are correct. I was being too cautious.

-Aaron

Applied
#6 lore raw
In reply to Aaron Sierra
Quoted 11 lines · Aaron Sierra

UBI needs to know the physical erase block size, even on read-only devices, since it defines the on-device layout. Use a device-tree provided value to support previously written UBI on read-only NOR.

UBI also needs a non-zero writebufsize, so we set it to one.

Note: This was implemented because hardware write-protected CFI
NOR cannot be probed for the physical erase block size.

Pushed to l2-mtd.git with two small tweaks:

Quoted 10 lines
---
 Documentation/devicetree/bindings/mtd/mtd-physmap.txt |  5 +++++
 drivers/mtd/chips/map_rom.c                           | 13 ++++++++++++-
 2 files changed, 17 insertions(+), 1 deletion(-)
Documentation/devicetree/bindings/mtd/mtd-physmap.txt +0 -0
index 6b9f680..8ab16df 100644

Documentation/devicetree/bindings/mtd/mtd-physmap.txt are defined: · line 40

Quoted 5 lines
Documentation/devicetree/bindings/mtd/mtd-physmap.txt +2 -0
@@ -36,6 +36,11 @@ are defined:
  - vendor-id : Contains the flash chip's vendor id (1 byte).
  - device-id : Contains the flash chip's device id (1 byte).
 
+For ROM compatible devices (and ROM fallback from cfi-flash), the following
+additional property is defined:

I noted that this property is optional, just to be clear.

Quoted 19 lines
Documentation/devicetree/bindings/mtd/mtd-physmap.txt +3 -0
+
+ - erase-size : The chip's physical erase block size in bytes.
+
 The device tree may optionally contain sub-nodes describing partitions of the
 address space. See partition.txt for more detail.
 
drivers/mtd/chips/map_rom.c +1 -0
index 47a43cf..a831265 100644
@@ -11,6 +11,7 @@
 #include <linux/errno.h>
 #include <linux/slab.h>
 #include <linux/init.h>
+#include <linux/of.h>
 #include <linux/mtd/mtd.h>
 #include <linux/mtd/map.h>
 

drivers/mtd/chips/map_rom.c static struct mtd_chip_driver maprom_chipdrv = { · line 35

Quoted 7 lines
drivers/mtd/chips/map_rom.c +4 -0
@@ -28,6 +29,15 @@ static struct mtd_chip_driver maprom_chipdrv = {
 	.module	= THIS_MODULE
 };
 
+static unsigned int default_erasesize(struct map_info *map)
+{
+	const __be32 *erase_size = NULL;
+#ifdef CONFIG_OF

I dropped this #ifdef.

Quoted 9 lines
drivers/mtd/chips/map_rom.c +5 -0
+	erase_size = of_get_property(map->device_node, "erase-size", NULL);
+#endif
+	return !erase_size ? map->size : be32_to_cpu(*erase_size);
+}
+
 static struct mtd_info *map_rom_probe(struct map_info *map)
 {
 	struct mtd_info *mtd;

drivers/mtd/chips/map_rom.c static struct mtd_info *map_rom_probe(struct map_info *map) · line 65

Quoted 10 lines
drivers/mtd/chips/map_rom.c +2 -1
@@ -47,8 +57,9 @@ static struct mtd_info *map_rom_probe(struct map_info *map)
 	mtd->_sync = maprom_nop;
 	mtd->_erase = maprom_erase;
 	mtd->flags = MTD_CAP_ROM;
-	mtd->erasesize = map->size;
+	mtd->erasesize = default_erasesize(map);
 	mtd->writesize = 1;
+	mtd->writebufsize = 1;
 
 	__module_get(THIS_MODULE);
 	return mtd;

Brian