Skip to contentSkip to conversation

mtd: nand: erase block before marking bad

PATCH In review Brian Norris 2 participants 6 messages Jan 14, 2012 – Jan 16, 2012

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

PATCH 1/4 mtd: nand: erase block before marking bad

Many NAND flash systems (especially those with MLC NAND) cannot be reliably written twice in a row. For instance, when marking a bad block, the block may already have data written to it, and so we should attempt to erase the block before writing a bad block marker to its OOB region.

We can ignore erase failures, since the block may be bad such that it cannot be erased properly; we still attempt to write zeros to its spare area.

---
 drivers/mtd/nand/nand_base.c |    8 ++++++++
 1 files changed, 8 insertions(+), 0 deletions(-)
drivers/mtd/nand/nand_base.c +8 -0
index 8a393f9..f0e768f 100644
@@ -407,6 +407,14 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
 		ret = nand_update_bbt(mtd, ofs);
 	else {
 		struct mtd_oob_ops ops;
+		struct erase_info einfo;
+
+		/* Attempt erase before marking OOB */
+		memset(&einfo, 0, sizeof(einfo));
+		einfo.mtd = mtd;
+		einfo.addr = ofs;
+		einfo.len = 1 << chip->phys_erase_shift;
+		nand_erase_nand(mtd, &einfo, 0);
 
 		nand_get_device(chip, mtd, FL_WRITING);
 
Signature · 1 line
1.7.5.4
#1 lore raw

PATCH 2/4 mtd: nand: fix SCAN2NDPAGE check for BBM

nand_block_bad() doesn't check the correct pages when NAND_BBT_SCAN2NDPAGE is enabled. It should scan both the OOB region of both the 1st and 2nd page of each block.

---
 drivers/mtd/nand/nand_base.c |   40 +++++++++++++++++++++++-----------------
 1 files changed, 23 insertions(+), 17 deletions(-)
drivers/mtd/nand/nand_base.c +23 -17
index f0e768f..c1ec4fb 100644
@@ -338,7 +338,7 @@ static int nand_verify_buf16(struct mtd_info *mtd, const uint8_t *buf, int len)
  */
 static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
 {
-	int page, chipnr, res = 0;
+	int page, chipnr, res = 0, i = 0;
 	struct nand_chip *chip = mtd->priv;
 	u16 bad;
 
@@ -356,23 +356,29 @@ static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
 		chip->select_chip(mtd, chipnr);
 	}
 
-	if (chip->options & NAND_BUSWIDTH_16) {
-		chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos & 0xFE,
-			      page);
-		bad = cpu_to_le16(chip->read_word(mtd));
-		if (chip->badblockpos & 0x1)
-			bad >>= 8;
-		else
-			bad &= 0xFF;
-	} else {
-		chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos, page);
-		bad = chip->read_byte(mtd);
-	}
+	do {
+		if (chip->options & NAND_BUSWIDTH_16) {
+			chip->cmdfunc(mtd, NAND_CMD_READOOB,
+					chip->badblockpos & 0xFE, page);
+			bad = cpu_to_le16(chip->read_word(mtd));
+			if (chip->badblockpos & 0x1)
+				bad >>= 8;
+			else
+				bad &= 0xFF;
+		} else {
+			chip->cmdfunc(mtd, NAND_CMD_READOOB, chip->badblockpos,
+					page);
+			bad = chip->read_byte(mtd);
+		}
 
-	if (likely(chip->badblockbits == 8))
-		res = bad != 0xFF;
-	else
-		res = hweight8(bad) < chip->badblockbits;
+		if (likely(chip->badblockbits == 8))
+			res = bad != 0xFF;
+		else
+			res = hweight8(bad) < chip->badblockbits;
+		ofs += mtd->writesize;
+		page = (int)(ofs >> chip->page_shift) & chip->pagemask;
+		i++;
+	} while (!res && i < 2 && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE));
 
 	if (getchip)
 		nand_release_device(mtd);
Signature · 1 line
1.7.5.4
#2 lore raw

PATCH 3/4 mtd: nand: differentiate 1- vs. 2-byte writes when marking bad blocks

In reply to Brian Norris

It seems that we have developed a bad-block-marking "feature" out of pure laziness:

"We write two bytes per location, so we dont have to mess with 16 bit access."

It's relatively simple to write a 1 byte at a time on x8 devices and 2 bytes at a time on x16 devices, so let's do it.

---
 drivers/mtd/nand/nand_base.c |   12 ++++++++----
 1 files changed, 8 insertions(+), 4 deletions(-)
drivers/mtd/nand/nand_base.c +8 -4
index c1ec4fb..ecc3f52 100644
@@ -427,13 +427,17 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
 		/*
 		 * Write to first two pages if necessary. If we write to more
 		 * than one location, the first error encountered quits the
-		 * procedure. We write two bytes per location, so we dont have
-		 * to mess with 16 bit access.
+		 * procedure.
 		 */
-		ops.len = ops.ooblen = 2;
 		ops.datbuf = NULL;
 		ops.oobbuf = buf;
-		ops.ooboffs = chip->badblockpos & ~0x01;
+		ops.ooboffs = chip->badblockpos;
+		if (chip->options & NAND_BUSWIDTH_16) {
+			ops.ooboffs &= ~0x01;
+			ops.len = ops.ooblen = 2;
+		} else {
+			ops.len = ops.ooblen = 1;
+		}
 		ops.mode = MTD_OPS_PLACE_OOB;
 		do {
 			ret = nand_do_write_oob(mtd, ofs, &ops);
Signature · 1 line
1.7.5.4
#3 lore raw

PATCH 4/4 mtd: nand: correct comment on nand_chip badblockbits

In reply to Brian Norris

The description for badblockbits is incorrect. I think someone just made up a false description on the spot to satisfy some kerneldoc warning.

---
 include/linux/mtd/nand.h |    5 +++--
 1 files changed, 3 insertions(+), 2 deletions(-)
include/linux/mtd/nand.h +3 -2
index 63b5a8b..609868f 100644
@@ -448,8 +448,9 @@ struct nand_buffers {
  *			will be copied to the appropriate nand_bbt_descr's.
  * @badblockpos:	[INTERN] position of the bad block marker in the oob
  *			area.
- * @badblockbits:	[INTERN] number of bits to left-shift the bad block
- *			number
+ * @badblockbits:	[INTERN] minimum number of set bits in a good block's
+ *			bad block marker position; i.e., BBM == 11110111b is
+ *			not bad when badblockbits == 7
  * @cellinfo:		[INTERN] MLC/multichip data from chip ident
  * @numchips:		[INTERN] number of physical chips
  * @chipsize:		[INTERN] the size of one chip for multichip arrays
Signature · 1 line
1.7.5.4
#4 lore raw
In reply to Brian Norris
Quoted 10 lines · Brian Norris

Many NAND flash systems (especially those with MLC NAND) cannot be reliably written twice in a row. For instance, when marking a bad block, the block may already have data written to it, and so we should attempt to erase the block before writing a bad block marker to its OOB region.

We can ignore erase failures, since the block may be bad such that it cannot be erased properly; we still attempt to write zeros to its spare area.

All four look good, pushed to l2-mtd.git, thanks.

What are your plans WRT marking blocks as bad both in the OOB and the table?

Signature · 2 lines
Best Regards,
Artem Bityutskiy
#5 lore raw
Quoted 7 lines · Artem Bityutskiy
Quoted 4 lines · Brian Norris

Many NAND flash systems (especially those with MLC NAND) cannot be reliably written twice in a row. For instance, when marking a bad block, the block may already have data written to it, and so we should attempt to erase the block before writing a bad block marker to its OOB region.

All four look good, pushed to l2-mtd.git, thanks.

Sorry, but actually this "erase block before marking bad" patch is incorrect. I didn't test this particular revision, and so I forgot that the erase fails because the block is already marked as bad:

nand_erase_nand: attempt to erase a bad block at page 0x...

I'll send a fixup patch on top of l2-mtd-2.6.

Quoted 2 lines

What are your plans WRT marking blocks as bad both in the OOB and the table?

I don't have a 100% definite plan, but I've been trying to track the comments/suggestions in the other thread. I'll reply to that thread so we have the right reply-chain and CC list for discussion.

Brian