Skip to contentSkip to conversation

mtd: nand: sunxi: fix OOB handling in ->write_xxx() functions

PATCH Superseded Boris Brezillon 3 participants 7 messages Sep 14, 2015 – Sep 21, 2015

Subsystems (4)
lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 mtd: nand: sunxi: fix OOB handling in ->write_xxx() functions

The USER_DATA register cannot be accessed using byte accessors on A13 SoCs, thus triggering a bug when using memcpy_toio on this register. Declare an helper macros to convert an OOB buffer into a suitable USER_DATA value and vice-versa.

This patch also fixes an error in the oob_required logic (some OOB data are not written even if the user required it) by removing the oob_required condition, which is perfectly valid since the core already fill ->oob_poi with FFs when oob_required is false.

Cc <[email protected]> # 3.19+
Fixes 1fef62c1423b ("mtd: nand: add sunxi NAND flash controller support")

---
Changes since v3:
- drop the NFC_USER_DATA_TO_BUF() macro

Changes since v2:
- add the NFC_USER_DATA_TO_BUF() macro and rename NFC_USER_DATA() into NFC_BUF_TO_USER_DATA()
- rework the commit message

Changes since v1:
- drop the !oob_required conditional path
- replace endianness conversions by a macro relying on byte shifting operations

---
 drivers/mtd/nand/sunxi_nand.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/mtd/nand/sunxi_nand.c +9 -17
index f97a58d..279cafd 100644
@@ -147,6 +147,10 @@
 #define NFC_ECC_MODE		GENMASK(15, 12)
 #define NFC_RANDOM_SEED		GENMASK(30, 16)
 
+/* NFC_USER_DATA helper macros */
+#define NFC_BUF_TO_USER_DATA(buf)	((buf)[0] | ((buf)[1] << 8) | \
+					((buf)[2] << 16) | ((buf)[3] << 24))
+
 #define NFC_DEFAULT_TIMEOUT_MS	1000
 
 #define NFC_SRAM_SIZE		1024
@@ -646,15 +650,9 @@ static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd,
 		offset = layout->eccpos[i * ecc->bytes] - 4 + mtd->writesize;
 
 		/* Fill OOB data in */
-		if (oob_required) {
-			tmp = 0xffffffff;
-			memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
-				    4);
-		} else {
-			memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE,
-				    chip->oob_poi + offset - mtd->writesize,
-				    4);
-		}
+		writel(NFC_BUF_TO_USER_DATA(chip->oob_poi +
+					    layout->oobfree[i].offset),
+		       nfc->regs + NFC_REG_USER_DATA_BASE);
 
 		chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
 
@@ -784,14 +782,8 @@ static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info *mtd,
 		offset += ecc->size;
 
 		/* Fill OOB data in */
-		if (oob_required) {
-			tmp = 0xffffffff;
-			memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
-				    4);
-		} else {
-			memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, oob,
-				    4);
-		}
+		writel(NFC_BUF_TO_USER_DATA(oob),
+		       nfc->regs + NFC_REG_USER_DATA_BASE);
 
 		tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ACCESS_DIR |
 		      (1 << 30);
Signature · 1 line
1.9.1
#1 lore raw

PATCH v4 1/1 mtd: nand: sunxi: fix OOB handling in ->write_xxx() functions

drivers/mtd/nand/sunxi_nand.c static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd, · line 655

Quoted 13 lines · On Monday 14 September 2015 10:41:03 Boris Brezillon wrote:
                /* Fill OOB data in */
drivers/mtd/nand/sunxi_nand.c +3 -9
-               if (oob_required) {
-                       tmp = 0xffffffff;
-                       memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
-                                   4);
-               } else {
-                       memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE,
-                                   chip->oob_poi + offset - mtd->writesize,
-                                   4);
-               }
+               writel(NFC_BUF_TO_USER_DATA(chip->oob_poi +
+                                           layout->oobfree[i].offset),
+                      nfc->regs + NFC_REG_USER_DATA_BASE);

This looks like you are changing the endianess of the data that gets written. Is that intentional?

memcpy_toio() uses the same endianess for source and destination, while writel() assumes that the destination is a little-endian register, and that could break if the kernel is built to run as big-endian. I also see that sunxi_nfc_write_buf() uses memcpy_toio() for writing the actual data, and you are not changing that.

If all hardware can do 32-bit accesses here and the size is guaranteed to be a multiple of four bytes, you can probably improve performance by using a __raw_writel() loop there. Using __raw_writel() in general is almost always a bug, but here it actually makes sense. See also the powerpc implementation of _memcpy_toio().

Arnd

#2 lore raw

PATCH v4 1/1 mtd: nand: sunxi: fix OOB handling in ->write_xxx() functions

Hi Arnd,

Quoted 15 lines · On Mon, 14 Sep 2015 10:59:02 +0200 Arnd Bergmann <[email protected]> wrote:

drivers/mtd/nand/sunxi_nand.c static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd, · line 655

Quoted 13 lines · On Monday 14 September 2015 10:41:03 Boris Brezillon wrote:
                /* Fill OOB data in */
drivers/mtd/nand/sunxi_nand.c +3 -9
-               if (oob_required) {
-                       tmp = 0xffffffff;
-                       memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
-                                   4);
-               } else {
-                       memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE,
-                                   chip->oob_poi + offset - mtd->writesize,
-                                   4);
-               }
+               writel(NFC_BUF_TO_USER_DATA(chip->oob_poi +
+                                           layout->oobfree[i].offset),
+                      nfc->regs + NFC_REG_USER_DATA_BASE);
Quoted 2 lines

This looks like you are changing the endianess of the data that gets written. Is that intentional?

Hm, the real goal of this patch was to avoid accessing the NFC_REG_USER_DATA_BASE register using byte accessors (writeb()). The first version of this series was directly copying data from the buffer into a temporary u32 variable, thus forcing the data to be stored in little endian (tell me if I'm wrong), and then changing endianness using le32_to_cpu().
Brian suggested to use __raw_writel() (as you seem to suggest too), but I was worried about the missing mem barrier in this function. That's why I made my own macro doing the little endian to CPU conversion manually, but still using the standard writel() accessor (which will do the conversion in reverse order).
Maybe I should use __raw_writel() and add an explicit memory barrier.

Quoted 5 lines

memcpy_toio() uses the same endianess for source and destination, while writel() assumes that the destination is a little-endian register, and that could break if the kernel is built to run as big-endian. I also see that sunxi_nfc_write_buf() uses memcpy_toio() for writing the actual data, and you are not changing that.

AFAIU the peripheral is always in little endian, and only the CPU can be switched to big endian, right?
Are you saying that memcpy_toio() uses writel? Because according to this implementation [2] it uses writeb, which should be safe (accessing the internal SRAM using byte accessors is authorized).

Quoted 6 lines

If all hardware can do 32-bit accesses here and the size is guaranteed to be a multiple of four bytes, you can probably improve performance by using a __raw_writel() loop there. Using __raw_writel() in general is almost always a bug, but here it actually makes sense. See also the powerpc implementation of _memcpy_toio().

AFAICT, buffer passed to ->write_bu() are not necessarily aligned on 32bits, so using writel here might require copying data in temporary buffers :-/.

Don't hesitate to point where I'm wrong ;-).

Best Regards,

Boris

[1]https://patchwork.ozlabs.org/patch/502041/ [2]http://lxr.free-electrons.com/source/arch/arm/kernel/io.c#L56

Signature · 3 lines
Boris Brezillon, Free Electrons
Embedded Linux and Kernel engineering
http://free-electrons.com
#3 lore raw

PATCH v4 1/1 mtd: nand: sunxi: fix OOB handling in ->write_xxx() functions

Quoted 23 lines · On Monday 14 September 2015 11:41:13 Boris Brezillon wrote:

Hi Arnd,

Quoted 15 lines · On Mon, 14 Sep 2015 10:59:02 +0200 Arnd Bergmann <[email protected]> wrote:

drivers/mtd/nand/sunxi_nand.c static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd, · line 655

Quoted 13 lines · On Monday 14 September 2015 10:41:03 Boris Brezillon wrote:
                /* Fill OOB data in */
drivers/mtd/nand/sunxi_nand.c +3 -9
-               if (oob_required) {
-                       tmp = 0xffffffff;
-                       memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
-                                   4);
-               } else {
-                       memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE,
-                                   chip->oob_poi + offset - mtd->writesize,
-                                   4);
-               }
+               writel(NFC_BUF_TO_USER_DATA(chip->oob_poi +
+                                           layout->oobfree[i].offset),
+                      nfc->regs + NFC_REG_USER_DATA_BASE);
Quoted 2 lines

This looks like you are changing the endianess of the data that gets written. Is that intentional?

Quoted 11 lines

Hm, the real goal of this patch was to avoid accessing the NFC_REG_USER_DATA_BASE register using byte accessors (writeb()). The first version of this series was directly copying data from the buffer into a temporary u32 variable, thus forcing the data to be stored in little endian (tell me if I'm wrong), and then changing endianness using le32_to_cpu().
Brian suggested to use __raw_writel() (as you seem to suggest too), but I was worried about the missing mem barrier in this function. That's why I made my own macro doing the little endian to CPU conversion manually, but still using the standard writel() accessor (which will do the conversion in reverse order).

D'oh, I totally missed your open-coded le32_to_cpu macro. So your code does look correct to me, it's just a little inefficient on big-endian machines because you end up swapping twice.

Quoted 1 line

Maybe I should use __raw_writel() and add an explicit memory barrier.

That would work, and avoid the double swapping, yes. Or you could use writesl() with a length of one, which should have all the necessary barriers but no byteswap.

I don't think you need a barrier on ARM here (no DMA that can interfere), but it's better write the code architecture independent (as you did above).

The memcpy_toio() is definitely overkill as it has a barrier after every single byte, where you need at most one for this kind of driver.

Quoted 7 lines
Quoted 4 lines

memcpy_toio() uses the same endianess for source and destination, while writel() assumes that the destination is a little-endian register, and that could break if the kernel is built to run as big-endian. I also see that sunxi_nfc_write_buf() uses memcpy_toio() for writing the actual data, and you are not changing that.

AFAIU the peripheral is always in little endian, and only the CPU can be switched to big endian, right?

Correct.

Quoted 3 lines

Are you saying that memcpy_toio() uses writel? Because according to this implementation [2] it uses writeb, which should be safe (accessing the internal SRAM using byte accessors is authorized).

No, what I meant is that you are replacing some memcpy_toio() with writel(), but don't replace some of the others that should/could also be replaced.

As mentioned, I was under the impression that you changed the endianess for the OOB data but did not change the endianess for the user data, which would be inconsistent.

Quoted 11 lines
Quoted 5 lines

If all hardware can do 32-bit accesses here and the size is guaranteed to be a multiple of four bytes, you can probably improve performance by using a __raw_writel() loop there. Using __raw_writel() in general is almost always a bug, but here it actually makes sense. See also the powerpc implementation of _memcpy_toio().

AFAICT, buffer passed to ->write_bu() are not necessarily aligned on 32bits, so using writel here might require copying data in temporary buffers :-/.

Don't hesitate to point where I'm wrong ;-).

Brian or Dwmw2 should be able to know for sure. I think it's definitely worth trying as the potential performance gains could be huge, if you replace

	for (p = start; p < start + length; data++, p++) {
		writeb(*data, p);
		wmb();
	}

with

	for (p = start; p < start + length; data++, p+=4) {
		writel(*data, p);
	};
	wmb();

	Arnd
#4 lore raw

PATCH v4 1/1 mtd: nand: sunxi: fix OOB handling in ->write_xxx() functions

Quoted 21 lines · On Monday 14 September 2015 13:49:12 Arnd Bergmann wrote:
Quoted 11 lines
Quoted 5 lines

If all hardware can do 32-bit accesses here and the size is guaranteed to be a multiple of four bytes, you can probably improve performance by using a __raw_writel() loop there. Using __raw_writel() in general is almost always a bug, but here it actually makes sense. See also the powerpc implementation of _memcpy_toio().

AFAICT, buffer passed to ->write_bu() are not necessarily aligned on 32bits, so using writel here might require copying data in temporary buffers :-/.

Don't hesitate to point where I'm wrong ;-).

Brian or Dwmw2 should be able to know for sure. I think it's definitely worth trying as the potential performance gains could be huge, if you replace

        for (p = start; p < start + length; data++, p++) {
                writeb(*data, p);
                wmb();
        }
Quoted 7 lines

with

        for (p = start; p < start + length; data++, p+=4) {
                writel(*data, p);
        };
        wmb();

As Boris pointed out on IRC, we have an optimized version of memcpy_toio on little-endian, which already does this. I'm not completely sure why we don't use it for big-endian architectures as well.

Powerpc uses the same method on big-endian, but it's possible that it does not do the right thing on one of the older platforms using BE32 mode, or one that has a weird bus mode.

Arnd

#5 lore raw

PATCH v4 1/1 mtd: nand: sunxi: fix OOB handling in ->write_xxx() functions

In reply to Boris Brezillon
Quoted 10 lines · Boris Brezillon

The USER_DATA register cannot be accessed using byte accessors on A13 SoCs, thus triggering a bug when using memcpy_toio on this register. Declare an helper macros to convert an OOB buffer into a suitable USER_DATA value and vice-versa.

This patch also fixes an error in the oob_required logic (some OOB data are not written even if the user required it) by removing the oob_required condition, which is perfectly valid since the core already fill ->oob_poi with FFs when oob_required is false.

Quoted 7 lines
Cc <[email protected]> # 3.19+
Fixes 1fef62c1423b ("mtd: nand: add sunxi NAND flash controller support")

---
Changes since v3:
- drop the NFC_USER_DATA_TO_BUF() macro

I don't have any real objections to this version, and I think some IRC discussion helped clear up a few questions. I'll take this for 4.3 soon, unless I see any objections.

Quoted 34 lines

Changes since v2:
- add the NFC_USER_DATA_TO_BUF() macro and rename NFC_USER_DATA() into NFC_BUF_TO_USER_DATA()
- rework the commit message

Changes since v1:
- drop the !oob_required conditional path
- replace endianness conversions by a macro relying on byte shifting operations

---
 drivers/mtd/nand/sunxi_nand.c | 26 +++++++++-----------------
 1 file changed, 9 insertions(+), 17 deletions(-)
drivers/mtd/nand/sunxi_nand.c +4 -2
index f97a58d..279cafd 100644
@@ -147,6 +147,10 @@
 #define NFC_ECC_MODE		GENMASK(15, 12)
 #define NFC_RANDOM_SEED		GENMASK(30, 16)
 
+/* NFC_USER_DATA helper macros */
+#define NFC_BUF_TO_USER_DATA(buf)	((buf)[0] | ((buf)[1] << 8) | \
+					((buf)[2] << 16) | ((buf)[3] << 24))
+
 #define NFC_DEFAULT_TIMEOUT_MS	1000
 
 #define NFC_SRAM_SIZE		1024
@@ -646,15 +650,9 @@ static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd,
 		offset = layout->eccpos[i * ecc->bytes] - 4 + mtd->writesize;
 
 		/* Fill OOB data in */
-		if (oob_required) {
-			tmp = 0xffffffff;

drivers/mtd/nand/sunxi_nand.c static int sunxi_nfc_hw_ecc_write_page(struct mtd_info *mtd, · line 655

Quoted 10 lines
drivers/mtd/nand/sunxi_nand.c +3 -7
-			memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
-				    4);
-		} else {
-			memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE,
-				    chip->oob_poi + offset - mtd->writesize,
-				    4);
-		}
+		writel(NFC_BUF_TO_USER_DATA(chip->oob_poi +
+					    layout->oobfree[i].offset),
+		       nfc->regs + NFC_REG_USER_DATA_BASE);

I believe the few remaining questionable points were:

* you don't actually need to add the barrier, thus __raw_writel() would suffice
* you're still doing a double swap for the (likely theoretical) big endian case

Neither point matters much to me, so as noted above, LGTM.

Quoted 7 lines
drivers/mtd/nand/sunxi_nand.c +0 -0
 		chip->cmdfunc(mtd, NAND_CMD_RNDIN, offset, -1);
 
@@ -784,14 +782,8 @@ static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info *mtd,
 		offset += ecc->size;
 
 		/* Fill OOB data in */

drivers/mtd/nand/sunxi_nand.c static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info *mtd, · line 786

Quoted 10 lines
drivers/mtd/nand/sunxi_nand.c +2 -8
-		if (oob_required) {
-			tmp = 0xffffffff;
-			memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, &tmp,
-				    4);
-		} else {
-			memcpy_toio(nfc->regs + NFC_REG_USER_DATA_BASE, oob,
-				    4);
-		}
+		writel(NFC_BUF_TO_USER_DATA(oob),
+		       nfc->regs + NFC_REG_USER_DATA_BASE);

Same here.

drivers/mtd/nand/sunxi_nand.c static int sunxi_nfc_hw_syndrome_ecc_write_page(struct mtd_info *mtd, · line 789

Quoted 3 lines
drivers/mtd/nand/sunxi_nand.c +0 -0
 		tmp = NFC_DATA_TRANS | NFC_DATA_SWAP_METHOD | NFC_ACCESS_DIR |
 		      (1 << 30);

Brian

Applied
#6 lore raw

PATCH v4 1/1 mtd: nand: sunxi: fix OOB handling in ->write_xxx() functions

Quoted 15 lines · Brian Norris
Quoted 14 lines · Boris Brezillon

The USER_DATA register cannot be accessed using byte accessors on A13 SoCs, thus triggering a bug when using memcpy_toio on this register. Declare an helper macros to convert an OOB buffer into a suitable USER_DATA value and vice-versa.

This patch also fixes an error in the oob_required logic (some OOB data are not written even if the user required it) by removing the oob_required condition, which is perfectly valid since the core already fill ->oob_poi with FFs when oob_required is false.

Cc <[email protected]> # 3.19+
Fixes 1fef62c1423b ("mtd: nand: add sunxi NAND flash controller support")
Quoted 7 lines
Quoted 3 lines

---
Changes since v3:
- drop the NFC_USER_DATA_TO_BUF() macro

I don't have any real objections to this version, and I think some IRC discussion helped clear up a few questions. I'll take this for 4.3 soon, unless I see any objections.

Pushed to linux-mtd.git.