Skip to contentSkip to conversation

mtd: rawnand: meson: fix bitmask for length in command word

PATCH Applied Arseniy Krasnov 3 participants 6 messages Mar 29, 2023 – Apr 3, 2023

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

PATCH v2 1/1 mtd: rawnand: meson: fix bitmask for length in command word

Valid mask is 0x3FFF, without this patch the following problems were found:

1) [ 0.938914] Could not find a valid ONFI parameter page, trying
bit-wise majority to recover it

   [    0.947384] ONFI parameter recovery failed, aborting

2) Read with disabled ECC mode was broken.

Fixes 8fae856c5350 ("mtd: rawnand: meson: add support for Amlogic NAND flash controller")
---
 drivers/mtd/nand/raw/meson_nand.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mtd/nand/raw/meson_nand.c +3 -3
index a28574c00900..074e14225c06 100644
@@ -280,7 +280,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir,
 
 	if (raw) {
 		len = mtd->writesize + mtd->oobsize;
-		cmd = (len & GENMASK(5, 0)) | scrambler | DMA_DIR(dir);
+		cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir);
 		writel(cmd, nfc->reg_base + NFC_REG_CMD);
 		return;
 	}
@@ -544,7 +544,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len)
 	if (ret)
 		goto out;
 
-	cmd = NFC_CMD_N2M | (len & GENMASK(5, 0));
+	cmd = NFC_CMD_N2M | (len & GENMASK(13, 0));
 	writel(cmd, nfc->reg_base + NFC_REG_CMD);
 
 	meson_nfc_drain_cmd(nfc);
@@ -568,7 +568,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len)
 	if (ret)
 		return ret;
 
-	cmd = NFC_CMD_M2N | (len & GENMASK(5, 0));
+	cmd = NFC_CMD_M2N | (len & GENMASK(13, 0));
 	writel(cmd, nfc->reg_base + NFC_REG_CMD);
 
 	meson_nfc_drain_cmd(nfc);
Signature · 6 lines
2.35.0

_______________________________________________
linux-amlogic mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-amlogic
#1 lore raw
Quoted 21 lines · Arseniy Krasnov

Valid mask is 0x3FFF, without this patch the following problems were found:

1) [ 0.938914] Could not find a valid ONFI parameter page, trying
bit-wise majority to recover it

   [    0.947384] ONFI parameter recovery failed, aborting

2) Read with disabled ECC mode was broken.

Fixes 8fae856c5350 ("mtd: rawnand: meson: add support for Amlogic NAND flash controller")
---
 drivers/mtd/nand/raw/meson_nand.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mtd/nand/raw/meson_nand.c +0 -0
index a28574c00900..074e14225c06 100644

drivers/mtd/nand/raw/meson_nand.c static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, · line 283

Quoted 5 lines
drivers/mtd/nand/raw/meson_nand.c +1 -1
@@ -280,7 +280,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir,
 
 	if (raw) {
 		len = mtd->writesize + mtd->oobsize;
-		cmd = (len & GENMASK(5, 0)) | scrambler | DMA_DIR(dir);
+		cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir);

What happens when len > GENMASK(13, 0)? Do you check this somewhere?

Please introduce a macro/field for GENMASK(13, 0), having such mask scattered along the code looks hackish and doesn't help readability. You'll get to use FIELD_PREP as well.

Quoted 21 lines
drivers/mtd/nand/raw/meson_nand.c +2 -2
 		writel(cmd, nfc->reg_base + NFC_REG_CMD);
 		return;
 	}
@@ -544,7 +544,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len)
 	if (ret)
 		goto out;
 
-	cmd = NFC_CMD_N2M | (len & GENMASK(5, 0));
+	cmd = NFC_CMD_N2M | (len & GENMASK(13, 0));
 	writel(cmd, nfc->reg_base + NFC_REG_CMD);
 
 	meson_nfc_drain_cmd(nfc);
@@ -568,7 +568,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len)
 	if (ret)
 		return ret;
 
-	cmd = NFC_CMD_M2N | (len & GENMASK(5, 0));
+	cmd = NFC_CMD_M2N | (len & GENMASK(13, 0));
 	writel(cmd, nfc->reg_base + NFC_REG_CMD);
 
 	meson_nfc_drain_cmd(nfc);
#2 lore raw
Quoted 25 lines · Tudor Ambarus
Quoted 22 lines · Arseniy Krasnov

Valid mask is 0x3FFF, without this patch the following problems were found:

1) [ 0.938914] Could not find a valid ONFI parameter page, trying
bit-wise majority to recover it

   [    0.947384] ONFI parameter recovery failed, aborting

2) Read with disabled ECC mode was broken.

Fixes 8fae856c5350 ("mtd: rawnand: meson: add support for Amlogic NAND flash controller")
---
 drivers/mtd/nand/raw/meson_nand.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mtd/nand/raw/meson_nand.c +0 -0
index a28574c00900..074e14225c06 100644
@@ -280,7 +280,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir,
 
Quoted 6 lines

drivers/mtd/nand/raw/meson_nand.c static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, · line 283

Quoted 4 lines
drivers/mtd/nand/raw/meson_nand.c +1 -1
 	if (raw) {
 		len = mtd->writesize + mtd->oobsize;
-		cmd = (len & GENMASK(5, 0)) | scrambler | DMA_DIR(dir);
+		cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir);

What happens when len > GENMASK(13, 0)? Do you check this somewhere?

'len' will be trimmed. I'm not sure that this case is possible here, because GENMASK(13, 0) is hardware limit for this NAND controller, so 'writesize' and 'oobsize' will be initialized to fit this value. Moreover GENMASK(13, 0) is 16Kb - i think it is big enough for single read. Also i'm not sure that it is good approach to check 'len' here - we are in the middle of NAND read processing.

Quoted 4 lines

Please introduce a macro/field for GENMASK(13, 0), having such mask scattered along the code looks hackish and doesn't help readability. You'll get to use FIELD_PREP as well.

Ack, i'll do it in v3

Thanks, Arseniy

Quoted 22 lines
Quoted 22 lines
drivers/mtd/nand/raw/meson_nand.c +2 -2
 		writel(cmd, nfc->reg_base + NFC_REG_CMD);
 		return;
 	}
@@ -544,7 +544,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len)
 	if (ret)
 		goto out;
 
-	cmd = NFC_CMD_N2M | (len & GENMASK(5, 0));
+	cmd = NFC_CMD_N2M | (len & GENMASK(13, 0));
 	writel(cmd, nfc->reg_base + NFC_REG_CMD);
 
 	meson_nfc_drain_cmd(nfc);
@@ -568,7 +568,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len)
 	if (ret)
 		return ret;
 
-	cmd = NFC_CMD_M2N | (len & GENMASK(5, 0));
+	cmd = NFC_CMD_M2N | (len & GENMASK(13, 0));
 	writel(cmd, nfc->reg_base + NFC_REG_CMD);
 
 	meson_nfc_drain_cmd(nfc);
maintainer
#3 lore raw

Hi Arseniy,

[email protected] wrote on Wed, 29 Mar 2023 11:33:38 +0300:

Quoted 33 lines · Tudor Ambarus
Quoted 25 lines · Arseniy Krasnov
Quoted 22 lines

Valid mask is 0x3FFF, without this patch the following problems were found:

1) [ 0.938914] Could not find a valid ONFI parameter page, trying
bit-wise majority to recover it

   [    0.947384] ONFI parameter recovery failed, aborting

2) Read with disabled ECC mode was broken.

Fixes 8fae856c5350 ("mtd: rawnand: meson: add support for Amlogic NAND flash controller")
---
 drivers/mtd/nand/raw/meson_nand.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mtd/nand/raw/meson_nand.c +0 -0
index a28574c00900..074e14225c06 100644
@@ -280,7 +280,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir,
 
Quoted 6 lines

drivers/mtd/nand/raw/meson_nand.c static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, · line 283

Quoted 4 lines
drivers/mtd/nand/raw/meson_nand.c +1 -1
 	if (raw) {
 		len = mtd->writesize + mtd->oobsize;
-		cmd = (len & GENMASK(5, 0)) | scrambler | DMA_DIR(dir);
+		cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir);  

What happens when len > GENMASK(13, 0)? Do you check this somewhere?

Quoted 5 lines

'len' will be trimmed. I'm not sure that this case is possible here, because GENMASK(13, 0) is hardware limit for this NAND controller, so 'writesize' and 'oobsize' will be initialized to fit this value. Moreover GENMASK(13, 0) is 16Kb - i think it is big enough for single read. Also i'm not sure that it is good approach to check 'len' here - we are in the middle of NAND read processing.

No, you should check the page size will not exceed this limit in the attach hook, likely.

You should also refuse exec_op operations with a data length bigger than 16k (either with a manual check in your own parser or just by providing the max size to the parser table, depending on what's used).

Quoted 27 lines
Quoted 5 lines

Please introduce a macro/field for GENMASK(13, 0), having such mask scattered along the code looks hackish and doesn't help readability. You'll get to use FIELD_PREP as well.

Ack, i'll do it in v3

Thanks, Arseniy

Quoted 17 lines
Quoted 17 lines
drivers/mtd/nand/raw/meson_nand.c +1 -1
 		writel(cmd, nfc->reg_base + NFC_REG_CMD);
 		return;
 	}
@@ -544,7 +544,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len)
 	if (ret)
 		goto out;
 
-	cmd = NFC_CMD_N2M | (len & GENMASK(5, 0));
+	cmd = NFC_CMD_N2M | (len & GENMASK(13, 0));
 	writel(cmd, nfc->reg_base + NFC_REG_CMD);
 
 	meson_nfc_drain_cmd(nfc);
@@ -568,7 +568,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len)
 	if (ret)
 		return ret;
 
Quoted 5 lines
Quoted 5 lines

drivers/mtd/nand/raw/meson_nand.c static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) · line 574

Quoted 5 lines
drivers/mtd/nand/raw/meson_nand.c +1 -1
-	cmd = NFC_CMD_M2N | (len & GENMASK(5, 0));
+	cmd = NFC_CMD_M2N | (len & GENMASK(13, 0));
 	writel(cmd, nfc->reg_base + NFC_REG_CMD);
 
 	meson_nfc_drain_cmd(nfc);  

Thanks,
Miquèl

_______________________________________________
linux-amlogic mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-amlogic

#4 lore raw
Quoted 43 lines · Miquel Raynal

Hi Arseniy,

[email protected] wrote on Wed, 29 Mar 2023 11:33:38 +0300:

Quoted 33 lines · Tudor Ambarus
Quoted 25 lines · Arseniy Krasnov
Quoted 22 lines

Valid mask is 0x3FFF, without this patch the following problems were found:

1) [ 0.938914] Could not find a valid ONFI parameter page, trying
bit-wise majority to recover it

   [    0.947384] ONFI parameter recovery failed, aborting

2) Read with disabled ECC mode was broken.

Fixes 8fae856c5350 ("mtd: rawnand: meson: add support for Amlogic NAND flash controller")
---
 drivers/mtd/nand/raw/meson_nand.c | 6 +++---
 1 file changed, 3 insertions(+), 3 deletions(-)
drivers/mtd/nand/raw/meson_nand.c +0 -0
index a28574c00900..074e14225c06 100644
@@ -280,7 +280,7 @@ static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir,
 
Quoted 6 lines

drivers/mtd/nand/raw/meson_nand.c static void meson_nfc_cmd_access(struct nand_chip *nand, int raw, bool dir, · line 283

Quoted 4 lines
drivers/mtd/nand/raw/meson_nand.c +1 -1
 	if (raw) {
 		len = mtd->writesize + mtd->oobsize;
-		cmd = (len & GENMASK(5, 0)) | scrambler | DMA_DIR(dir);
+		cmd = (len & GENMASK(13, 0)) | scrambler | DMA_DIR(dir);  

What happens when len > GENMASK(13, 0)? Do you check this somewhere?

Quoted 5 lines

'len' will be trimmed. I'm not sure that this case is possible here, because GENMASK(13, 0) is hardware limit for this NAND controller, so 'writesize' and 'oobsize' will be initialized to fit this value. Moreover GENMASK(13, 0) is 16Kb - i think it is big enough for single read. Also i'm not sure that it is good approach to check 'len' here - we are in the middle of NAND read processing.

Quoted 7 lines

No, you should check the page size will not exceed this limit in the attach hook, likely.

You should also refuse exec_op operations with a data length bigger than 16k (either with a manual check in your own parser or just by providing the max size to the parser table, depending on what's used).

Ok, I see.

Thanks, Arseniy

Quoted 36 lines
Quoted 27 lines
Quoted 5 lines

Please introduce a macro/field for GENMASK(13, 0), having such mask scattered along the code looks hackish and doesn't help readability. You'll get to use FIELD_PREP as well.

Ack, i'll do it in v3

Thanks, Arseniy

Quoted 17 lines
Quoted 17 lines
drivers/mtd/nand/raw/meson_nand.c +1 -1
 		writel(cmd, nfc->reg_base + NFC_REG_CMD);
 		return;
 	}
@@ -544,7 +544,7 @@ static int meson_nfc_read_buf(struct nand_chip *nand, u8 *buf, int len)
 	if (ret)
 		goto out;
 
-	cmd = NFC_CMD_N2M | (len & GENMASK(5, 0));
+	cmd = NFC_CMD_N2M | (len & GENMASK(13, 0));
 	writel(cmd, nfc->reg_base + NFC_REG_CMD);
 
 	meson_nfc_drain_cmd(nfc);
@@ -568,7 +568,7 @@ static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len)
 	if (ret)
 		return ret;
 
Quoted 5 lines
Quoted 5 lines

drivers/mtd/nand/raw/meson_nand.c static int meson_nfc_write_buf(struct nand_chip *nand, u8 *buf, int len) · line 574

Quoted 5 lines
drivers/mtd/nand/raw/meson_nand.c +1 -1
-	cmd = NFC_CMD_M2N | (len & GENMASK(5, 0));
+	cmd = NFC_CMD_M2N | (len & GENMASK(13, 0));
 	writel(cmd, nfc->reg_base + NFC_REG_CMD);
 
 	meson_nfc_drain_cmd(nfc);  

Thanks,
Miquèl

maintainer Applied
#5 lore raw
In reply to Arseniy Krasnov
Quoted 5 lines · Arseniy Krasnov

Valid mask is 0x3FFF, without this patch the following problems were found:

1) [ 0.938914] Could not find a valid ONFI parameter page, trying
bit-wise majority to recover it

Quoted 7 lines
   [    0.947384] ONFI parameter recovery failed, aborting

2) Read with disabled ECC mode was broken.

Fixes 8fae856c5350 ("mtd: rawnand: meson: add support for Amlogic NAND flash controller")

Applied to https://git.kernel.org/pub/scm/linux/kernel/git/mtd/linux.git mtd/fixes, thanks.

Miquel

_______________________________________________
linux-amlogic mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-amlogic