f2f45eb7bc7ad99ba107a4de85dd52d58510e37d
[openwrt/staging/blogic.git] /
1 From 2ed18d818d1f7492172f8dd5904344c7d367e8ed Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Micha=C5=82=20K=C4=99pie=C5=84?= <kernel@kempniu.pl>
3 Date: Wed, 29 Jun 2022 14:57:36 +0200
4 Subject: [PATCH 3/4] mtd: add ECC error accounting for each read request
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 Extend struct mtd_req_stats with two new fields holding the number of
10 corrected bitflips and uncorrectable errors detected during a read
11 operation. This is a prerequisite for ultimately passing those counters
12 to user space, where they can be useful to applications for making
13 better-informed choices about moving data around.
14
15 Unlike 'max_bitflips' (which is set - in a common code path - to the
16 return value of a function called while the MTD device's mutex is held),
17 these counters have to be maintained in each MTD driver which defines
18 the '_read_oob' callback because the statistics need to be calculated
19 while the MTD device's mutex is held.
20
21 Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
22 Signed-off-by: Michał Kępień <kernel@kempniu.pl>
23 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
24 Link: https://lore.kernel.org/linux-mtd/20220629125737.14418-4-kernel@kempniu.pl
25 ---
26 drivers/mtd/devices/docg3.c | 8 ++++++++
27 drivers/mtd/nand/onenand/onenand_base.c | 12 ++++++++++++
28 drivers/mtd/nand/raw/nand_base.c | 10 ++++++++++
29 drivers/mtd/nand/spi/core.c | 10 ++++++++++
30 include/linux/mtd/mtd.h | 2 ++
31 5 files changed, 42 insertions(+)
32
33 --- a/drivers/mtd/devices/docg3.c
34 +++ b/drivers/mtd/devices/docg3.c
35 @@ -871,6 +871,7 @@ static int doc_read_oob(struct mtd_info
36 u8 *buf = ops->datbuf;
37 size_t len, ooblen, nbdata, nboob;
38 u8 hwecc[DOC_ECC_BCH_SIZE], eccconf1;
39 + struct mtd_ecc_stats old_stats;
40 int max_bitflips = 0;
41
42 if (buf)
43 @@ -895,6 +896,7 @@ static int doc_read_oob(struct mtd_info
44 ret = 0;
45 skip = from % DOC_LAYOUT_PAGE_SIZE;
46 mutex_lock(&docg3->cascade->lock);
47 + old_stats = mtd->ecc_stats;
48 while (ret >= 0 && (len > 0 || ooblen > 0)) {
49 calc_block_sector(from - skip, &block0, &block1, &page, &ofs,
50 docg3->reliable);
51 @@ -966,6 +968,12 @@ static int doc_read_oob(struct mtd_info
52 }
53
54 out:
55 + if (ops->stats) {
56 + ops->stats->uncorrectable_errors +=
57 + mtd->ecc_stats.failed - old_stats.failed;
58 + ops->stats->corrected_bitflips +=
59 + mtd->ecc_stats.corrected - old_stats.corrected;
60 + }
61 mutex_unlock(&docg3->cascade->lock);
62 return ret;
63 err_in_read:
64 --- a/drivers/mtd/nand/onenand/onenand_base.c
65 +++ b/drivers/mtd/nand/onenand/onenand_base.c
66 @@ -1440,6 +1440,7 @@ static int onenand_read_oob(struct mtd_i
67 struct mtd_oob_ops *ops)
68 {
69 struct onenand_chip *this = mtd->priv;
70 + struct mtd_ecc_stats old_stats;
71 int ret;
72
73 switch (ops->mode) {
74 @@ -1453,12 +1454,23 @@ static int onenand_read_oob(struct mtd_i
75 }
76
77 onenand_get_device(mtd, FL_READING);
78 +
79 + old_stats = mtd->ecc_stats;
80 +
81 if (ops->datbuf)
82 ret = ONENAND_IS_4KB_PAGE(this) ?
83 onenand_mlc_read_ops_nolock(mtd, from, ops) :
84 onenand_read_ops_nolock(mtd, from, ops);
85 else
86 ret = onenand_read_oob_nolock(mtd, from, ops);
87 +
88 + if (ops->stats) {
89 + ops->stats->uncorrectable_errors +=
90 + mtd->ecc_stats.failed - old_stats.failed;
91 + ops->stats->corrected_bitflips +=
92 + mtd->ecc_stats.corrected - old_stats.corrected;
93 + }
94 +
95 onenand_release_device(mtd);
96
97 return ret;
98 --- a/drivers/mtd/nand/raw/nand_base.c
99 +++ b/drivers/mtd/nand/raw/nand_base.c
100 @@ -3815,6 +3815,7 @@ static int nand_read_oob(struct mtd_info
101 struct mtd_oob_ops *ops)
102 {
103 struct nand_chip *chip = mtd_to_nand(mtd);
104 + struct mtd_ecc_stats old_stats;
105 int ret;
106
107 ops->retlen = 0;
108 @@ -3826,11 +3827,20 @@ static int nand_read_oob(struct mtd_info
109
110 nand_get_device(chip);
111
112 + old_stats = mtd->ecc_stats;
113 +
114 if (!ops->datbuf)
115 ret = nand_do_read_oob(chip, from, ops);
116 else
117 ret = nand_do_read_ops(chip, from, ops);
118
119 + if (ops->stats) {
120 + ops->stats->uncorrectable_errors +=
121 + mtd->ecc_stats.failed - old_stats.failed;
122 + ops->stats->corrected_bitflips +=
123 + mtd->ecc_stats.corrected - old_stats.corrected;
124 + }
125 +
126 nand_release_device(chip);
127 return ret;
128 }
129 --- a/drivers/mtd/nand/spi/core.c
130 +++ b/drivers/mtd/nand/spi/core.c
131 @@ -629,6 +629,7 @@ static int spinand_mtd_read(struct mtd_i
132 {
133 struct spinand_device *spinand = mtd_to_spinand(mtd);
134 struct nand_device *nand = mtd_to_nanddev(mtd);
135 + struct mtd_ecc_stats old_stats;
136 unsigned int max_bitflips = 0;
137 struct nand_io_iter iter;
138 bool disable_ecc = false;
139 @@ -640,6 +641,8 @@ static int spinand_mtd_read(struct mtd_i
140
141 mutex_lock(&spinand->lock);
142
143 + old_stats = mtd->ecc_stats;
144 +
145 nanddev_io_for_each_page(nand, NAND_PAGE_READ, from, ops, &iter) {
146 if (disable_ecc)
147 iter.req.mode = MTD_OPS_RAW;
148 @@ -662,6 +665,13 @@ static int spinand_mtd_read(struct mtd_i
149 ops->oobretlen += iter.req.ooblen;
150 }
151
152 + if (ops->stats) {
153 + ops->stats->uncorrectable_errors +=
154 + mtd->ecc_stats.failed - old_stats.failed;
155 + ops->stats->corrected_bitflips +=
156 + mtd->ecc_stats.corrected - old_stats.corrected;
157 + }
158 +
159 mutex_unlock(&spinand->lock);
160
161 if (ecc_failed && !ret)
162 --- a/include/linux/mtd/mtd.h
163 +++ b/include/linux/mtd/mtd.h
164 @@ -41,6 +41,8 @@ struct mtd_erase_region_info {
165 };
166
167 struct mtd_req_stats {
168 + unsigned int uncorrectable_errors;
169 + unsigned int corrected_bitflips;
170 unsigned int max_bitflips;
171 };
172