Skip to content

Commit e2414f4

Browse files
computersforpeaceDavid Woodhouse
authored andcommitted
mtd: nand: write BBM to OOB even with flash-based BBT
Currently, the flash-based BBT implementation writes bad block data only to its flash-based table and not to the OOB marker area. Then, as new bad blocks are marked over time, the OOB markers become incomplete and the flash-based table becomes the only source of current bad block information. This becomes an obvious problem when, for example: * bootloader cannot read the flash-based BBT format * BBT is corrupted and the flash must be rescanned for bad blocks; we want to remember bad blocks that were marked from Linux So to keep the bad block markers in sync with the flash-based BBT, this patch changes the default so that we write bad block markers to the proper OOB area on each block in addition to flash-based BBT. Comments are updated, expanded, and/or relocated as necessary. The new flash-based BBT procedure for marking bad blocks: (1) erase the affected block, to allow OOB marker to be written cleanly (2) update in-memory BBT (3) write bad block marker to OOB area of affected block (4) update flash-based BBT Note that we retain the first error encountered in (3) or (4), finish the procedures, and dump the error in the end. This should handle power cuts gracefully enough. (1) and (2) are mostly harmless (note that (1) will not erase an already-recognized bad block). The OOB and BBT may be "out of sync" if we experience power loss bewteen (3) and (4), but we can reasonably expect that on next boot, subsequent I/O operations will discover that the block should be marked bad again, thus re-syncing the OOB and BBT. Note that this is a change from the previous default flash-based BBT behavior. If your system cannot support writing bad block markers to OOB, use the new NAND_BBT_NO_OOB_BBM option (in combination with NAND_BBT_USE_FLASH and NAND_BBT_NO_OOB). Signed-off-by: Brian Norris <computersforpeace@gmail.com> Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
1 parent 050c0c1 commit e2414f4

2 files changed

Lines changed: 36 additions & 15 deletions

File tree

‎drivers/mtd/nand/nand_base.c‎

Lines changed: 31 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -392,15 +392,23 @@ static int nand_block_bad(struct mtd_info *mtd, loff_t ofs, int getchip)
392392
* @ofs: offset from device start
393393
*
394394
* This is the default implementation, which can be overridden by a hardware
395-
* specific driver.
395+
* specific driver. We try operations in the following order, according to our
396+
* bbt_options (NAND_BBT_NO_OOB_BBM and NAND_BBT_USE_FLASH):
397+
* (1) erase the affected block, to allow OOB marker to be written cleanly
398+
* (2) update in-memory BBT
399+
* (3) write bad block marker to OOB area of affected block
400+
* (4) update flash-based BBT
401+
* Note that we retain the first error encountered in (3) or (4), finish the
402+
* procedures, and dump the error in the end.
396403
*/
397404
static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
398405
{
399406
struct nand_chip *chip = mtd->priv;
400407
uint8_t buf[2] = { 0, 0 };
401-
int block, ret, i = 0;
408+
int block, res, ret = 0, i = 0;
409+
int write_oob = !(chip->bbt_options & NAND_BBT_NO_OOB_BBM);
402410

403-
if (!(chip->bbt_options & NAND_BBT_USE_FLASH)) {
411+
if (write_oob) {
404412
struct erase_info einfo;
405413

406414
/* Attempt erase before marking OOB */
@@ -413,23 +421,17 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
413421

414422
/* Get block number */
415423
block = (int)(ofs >> chip->bbt_erase_shift);
424+
/* Mark block bad in memory-based BBT */
416425
if (chip->bbt)
417426
chip->bbt[block >> 2] |= 0x01 << ((block & 0x03) << 1);
418427

419-
/* Do we have a flash based bad block table? */
420-
if (chip->bbt_options & NAND_BBT_USE_FLASH)
421-
ret = nand_update_bbt(mtd, ofs);
422-
else {
428+
/* Write bad block marker to OOB */
429+
if (write_oob) {
423430
struct mtd_oob_ops ops;
424431
loff_t wr_ofs = ofs;
425432

426433
nand_get_device(chip, mtd, FL_WRITING);
427434

428-
/*
429-
* Write to first/last page(s) if necessary. If we write to more
430-
* than one location, the first error encountered quits the
431-
* procedure.
432-
*/
433435
ops.datbuf = NULL;
434436
ops.oobbuf = buf;
435437
ops.ooboffs = chip->badblockpos;
@@ -441,18 +443,28 @@ static int nand_default_block_markbad(struct mtd_info *mtd, loff_t ofs)
441443
}
442444
ops.mode = MTD_OPS_PLACE_OOB;
443445

446+
/* Write to first/last page(s) if necessary */
444447
if (chip->bbt_options & NAND_BBT_SCANLASTPAGE)
445448
wr_ofs += mtd->erasesize - mtd->writesize;
446449
do {
447-
ret = nand_do_write_oob(mtd, wr_ofs, &ops);
450+
res = nand_do_write_oob(mtd, wr_ofs, &ops);
451+
if (!ret)
452+
ret = res;
448453

449454
i++;
450455
wr_ofs += mtd->writesize;
451-
} while (!ret && (chip->bbt_options & NAND_BBT_SCAN2NDPAGE) &&
452-
i < 2);
456+
} while ((chip->bbt_options & NAND_BBT_SCAN2NDPAGE) && i < 2);
453457

454458
nand_release_device(mtd);
455459
}
460+
461+
/* Update flash-based bad block table */
462+
if (chip->bbt_options & NAND_BBT_USE_FLASH) {
463+
res = nand_update_bbt(mtd, ofs);
464+
if (!ret)
465+
ret = res;
466+
}
467+
456468
if (!ret)
457469
mtd->ecc_stats.badblocks++;
458470

@@ -3260,6 +3272,10 @@ int nand_scan_tail(struct mtd_info *mtd)
32603272
int i;
32613273
struct nand_chip *chip = mtd->priv;
32623274

3275+
/* New bad blocks should be marked in OOB, flash-based BBT, or both */
3276+
BUG_ON((chip->bbt_options & NAND_BBT_NO_OOB_BBM) &&
3277+
!(chip->bbt_options & NAND_BBT_USE_FLASH));
3278+
32633279
if (!(chip->options & NAND_OWN_BUFFERS))
32643280
chip->buffers = kmalloc(sizeof(*chip->buffers), GFP_KERNEL);
32653281
if (!chip->buffers)

‎include/linux/mtd/bbm.h‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,11 @@ struct nand_bbt_descr {
112112
#define NAND_BBT_USE_FLASH 0x00020000
113113
/* Do not store flash based bad block table in OOB area; store it in-band */
114114
#define NAND_BBT_NO_OOB 0x00040000
115+
/*
116+
* Do not write new bad block markers to OOB; useful, e.g., when ECC covers
117+
* entire spare area. Must be used with NAND_BBT_USE_FLASH.
118+
*/
119+
#define NAND_BBT_NO_OOB_BBM 0x00080000
115120

116121
/*
117122
* Flag set by nand_create_default_bbt_descr(), marking that the nand_bbt_descr

0 commit comments

Comments
 (0)