Skip to content

Commit edbc454

Browse files
mike-dunnDavid Woodhouse
authored andcommitted
mtd: driver _read() returns max_bitflips; mtd_read() returns -EUCLEAN
The drivers' _read() method, absent an error, returns a non-negative integer indicating the maximum number of bit errors that were corrected in any one region comprising an ecc step. MTD returns -EUCLEAN if this is >= bitflip_threshold, 0 otherwise. If bitflip_threshold is zero, the comparison is not made since these devices lack ECC and always return zero in the non-error case (thanks Brian)¹. Note that this is a subtle change to the driver interface. This and the preceding patches in this set were tested with ubi on top of the nandsim and docg4 devices, running the ubi test io_basic from mtd-utils. ¹ http://lists.infradead.org/pipermail/linux-mtd/2012-March/040468.html Signed-off-by: Mike Dunn <mikedunn@newsguy.com> Acked-by: Robert Jarzmik <robert.jarzmik@free.fr> Acked-by: Brian Norris <computersforpeace@gmail.com> Ivan Djelic <ivan.djelic@parrot.com> Signed-off-by: Artem Bityutskiy <artem.bityutskiy@linux.intel.com> Signed-off-by: David Woodhouse <David.Woodhouse@intel.com>
1 parent e2788c9 commit edbc454

8 files changed

Lines changed: 51 additions & 19 deletions

File tree

‎Documentation/ABI/testing/sysfs-class-mtd‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,10 @@ Description:
167167
block degradation, but high enough to avoid the consequences of
168168
a persistent return value of -EUCLEAN on devices where sticky
169169
bitflips occur. Note that if bitflip_threshold exceeds
170-
ecc_strength, -EUCLEAN is never returned by the read functions.
170+
ecc_strength, -EUCLEAN is never returned by mtd_read().
171+
Conversely, if bitflip_threshold is zero, -EUCLEAN is always
172+
returned, absent a hard error.
171173

172174
This is generally applicable only to NAND flash devices with ECC
173-
capability. It is ignored on devices lacking ECC capability.
175+
capability. It is ignored on devices lacking ECC capability;
176+
i.e., devices for which ecc_strength is zero.

‎drivers/mtd/devices/docg3.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -850,6 +850,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
850850
u8 *buf = ops->datbuf;
851851
size_t len, ooblen, nbdata, nboob;
852852
u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
853+
int max_bitflips = 0;
853854

854855
if (buf)
855856
len = ops->len;
@@ -876,7 +877,7 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
876877
ret = 0;
877878
skip = from % DOC_LAYOUT_PAGE_SIZE;
878879
mutex_lock(&docg3->cascade->lock);
879-
while (!ret && (len > 0 || ooblen > 0)) {
880+
while (ret >= 0 && (len > 0 || ooblen > 0)) {
880881
calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
881882
docg3->reliable);
882883
nbdata = min_t(size_t, len, DOC_LAYOUT_PAGE_SIZE - skip);
@@ -936,7 +937,8 @@ static int doc_read_oob(struct mtd_info *mtd, loff_t from,
936937
}
937938
if (ret > 0) {
938939
mtd->ecc_stats.corrected += ret;
939-
ret = -EUCLEAN;
940+
max_bitflips = max(max_bitflips, ret);
941+
ret = max_bitflips;
940942
}
941943
}
942944

‎drivers/mtd/mtdcore.c‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -800,12 +800,24 @@ EXPORT_SYMBOL_GPL(mtd_get_unmapped_area);
800800
int mtd_read(struct mtd_info *mtd, loff_t from, size_t len, size_t *retlen,
801801
u_char *buf)
802802
{
803+
int ret_code;
803804
*retlen = 0;
804805
if (from < 0 || from > mtd->size || len > mtd->size - from)
805806
return -EINVAL;
806807
if (!len)
807808
return 0;
808-
return mtd->_read(mtd, from, len, retlen, buf);
809+
810+
/*
811+
* In the absence of an error, drivers return a non-negative integer
812+
* representing the maximum number of bitflips that were corrected on
813+
* any one ecc region (if applicable; zero otherwise).
814+
*/
815+
ret_code = mtd->_read(mtd, from, len, retlen, buf);
816+
if (unlikely(ret_code < 0))
817+
return ret_code;
818+
if (mtd->ecc_strength == 0)
819+
return 0; /* device lacks ecc */
820+
return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
809821
}
810822
EXPORT_SYMBOL_GPL(mtd_read);
811823

‎drivers/mtd/mtdpart.c‎

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,12 +67,12 @@ static int part_read(struct mtd_info *mtd, loff_t from, size_t len,
6767
stats = part->master->ecc_stats;
6868
res = part->master->_read(part->master, from + part->offset, len,
6969
retlen, buf);
70-
if (unlikely(res)) {
71-
if (mtd_is_bitflip(res))
72-
mtd->ecc_stats.corrected += part->master->ecc_stats.corrected - stats.corrected;
73-
if (mtd_is_eccerr(res))
74-
mtd->ecc_stats.failed += part->master->ecc_stats.failed - stats.failed;
75-
}
70+
if (unlikely(mtd_is_eccerr(res)))
71+
mtd->ecc_stats.failed +=
72+
part->master->ecc_stats.failed - stats.failed;
73+
else
74+
mtd->ecc_stats.corrected +=
75+
part->master->ecc_stats.corrected - stats.corrected;
7676
return res;
7777
}
7878

‎drivers/mtd/nand/alauda.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -414,7 +414,7 @@ static int alauda_bounce_read(struct mtd_info *mtd, loff_t from, size_t len,
414414
}
415415
err = 0;
416416
if (corrected)
417-
err = -EUCLEAN;
417+
err = 1; /* return max_bitflips per ecc step */
418418
if (uncorrected)
419419
err = -EBADMSG;
420420
out:
@@ -446,7 +446,7 @@ static int alauda_read(struct mtd_info *mtd, loff_t from, size_t len,
446446
}
447447
err = 0;
448448
if (corrected)
449-
err = -EUCLEAN;
449+
err = 1; /* return max_bitflips per ecc step */
450450
if (uncorrected)
451451
err = -EBADMSG;
452452
return err;

‎drivers/mtd/nand/nand_base.c‎

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1486,6 +1486,7 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
14861486
mtd->oobavail : mtd->oobsize;
14871487

14881488
uint8_t *bufpoi, *oob, *buf;
1489+
unsigned int max_bitflips = 0;
14891490

14901491
stats = mtd->ecc_stats;
14911492

@@ -1513,7 +1514,10 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
15131514
sndcmd = 0;
15141515
}
15151516

1516-
/* Now read the page into the buffer */
1517+
/*
1518+
* Now read the page into the buffer. Absent an error,
1519+
* the read methods return max bitflips per ecc step.
1520+
*/
15171521
if (unlikely(ops->mode == MTD_OPS_RAW))
15181522
ret = chip->ecc.read_page_raw(mtd, chip,
15191523
bufpoi, page);
@@ -1530,15 +1534,19 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
15301534
break;
15311535
}
15321536

1537+
max_bitflips = max_t(unsigned int, max_bitflips, ret);
1538+
15331539
/* Transfer not aligned data */
15341540
if (!aligned) {
15351541
if (!NAND_SUBPAGE_READ(chip) && !oob &&
15361542
!(mtd->ecc_stats.failed - stats.failed) &&
1537-
(ops->mode != MTD_OPS_RAW))
1543+
(ops->mode != MTD_OPS_RAW)) {
15381544
chip->pagebuf = realpage;
1539-
else
1545+
chip->pagebuf_bitflips = ret;
1546+
} else {
15401547
/* Invalidate page cache */
15411548
chip->pagebuf = -1;
1549+
}
15421550
memcpy(buf, chip->buffers->databuf + col, bytes);
15431551
}
15441552

@@ -1571,6 +1579,8 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
15711579
} else {
15721580
memcpy(buf, chip->buffers->databuf + col, bytes);
15731581
buf += bytes;
1582+
max_bitflips = max_t(unsigned int, max_bitflips,
1583+
chip->pagebuf_bitflips);
15741584
}
15751585

15761586
readlen -= bytes;
@@ -1609,7 +1619,7 @@ static int nand_do_read_ops(struct mtd_info *mtd, loff_t from,
16091619
if (mtd->ecc_stats.failed - stats.failed)
16101620
return -EBADMSG;
16111621

1612-
return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1622+
return max_bitflips;
16131623
}
16141624

16151625
/**

‎drivers/mtd/onenand/onenand_base.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1201,7 +1201,8 @@ static int onenand_mlc_read_ops_nolock(struct mtd_info *mtd, loff_t from,
12011201
if (mtd->ecc_stats.failed - stats.failed)
12021202
return -EBADMSG;
12031203

1204-
return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1204+
/* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1205+
return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
12051206
}
12061207

12071208
/**
@@ -1333,7 +1334,8 @@ static int onenand_read_ops_nolock(struct mtd_info *mtd, loff_t from,
13331334
if (mtd->ecc_stats.failed - stats.failed)
13341335
return -EBADMSG;
13351336

1336-
return mtd->ecc_stats.corrected - stats.corrected ? -EUCLEAN : 0;
1337+
/* return max bitflips per ecc step; ONENANDs correct 1 bit only */
1338+
return mtd->ecc_stats.corrected != stats.corrected ? 1 : 0;
13371339
}
13381340

13391341
/**

‎include/linux/mtd/nand.h‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,8 @@ struct nand_buffers {
459459
* @pagemask: [INTERN] page number mask = number of (pages / chip) - 1
460460
* @pagebuf: [INTERN] holds the pagenumber which is currently in
461461
* data_buf.
462+
* @pagebuf_bitflips: [INTERN] holds the bitflip count for the page which is
463+
* currently in data_buf.
462464
* @subpagesize: [INTERN] holds the subpagesize
463465
* @onfi_version: [INTERN] holds the chip ONFI version (BCD encoded),
464466
* non 0 if ONFI supported.
@@ -519,6 +521,7 @@ struct nand_chip {
519521
uint64_t chipsize;
520522
int pagemask;
521523
int pagebuf;
524+
unsigned int pagebuf_bitflips;
522525
int subpagesize;
523526
uint8_t cellinfo;
524527
int badblockpos;

0 commit comments

Comments
 (0)