kernel: backport MEMREAD ioctl
[openwrt/staging/dedeckeh.git] / target / linux / generic / backport-5.15 / 423-v6.1-0001-mtd-track-maximum-number-of-bitflips-for-each-read-r.patch
1 From e237285113963bd1dd2e925770aa8b3aa8a1894c 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:34 +0200
4 Subject: [PATCH 1/4] mtd: track maximum number of bitflips for each read
5 request
6 MIME-Version: 1.0
7 Content-Type: text/plain; charset=UTF-8
8 Content-Transfer-Encoding: 8bit
9
10 mtd_read_oob() callers are currently oblivious to the details of ECC
11 errors detected during the read operation - they only learn (through the
12 return value) whether any corrected bitflips or uncorrectable errors
13 occurred. More detailed ECC information can be useful to user-space
14 applications for making better-informed choices about moving data
15 around.
16
17 Extend struct mtd_oob_ops with a pointer to a newly-introduced struct
18 mtd_req_stats and set its 'max_bitflips' field to the maximum number of
19 bitflips found in a single ECC step during the read operation performed
20 by mtd_read_oob(). This is a prerequisite for ultimately passing that
21 value back to user space.
22
23 Suggested-by: Boris Brezillon <boris.brezillon@collabora.com>
24 Signed-off-by: Michał Kępień <kernel@kempniu.pl>
25 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
26 Link: https://lore.kernel.org/linux-mtd/20220629125737.14418-2-kernel@kempniu.pl
27 ---
28 drivers/mtd/mtdcore.c | 5 +++++
29 include/linux/mtd/mtd.h | 5 +++++
30 2 files changed, 10 insertions(+)
31
32 --- a/drivers/mtd/mtdcore.c
33 +++ b/drivers/mtd/mtdcore.c
34 @@ -1669,6 +1669,9 @@ int mtd_read_oob(struct mtd_info *mtd, l
35 if (!master->_read_oob && (!master->_read || ops->oobbuf))
36 return -EOPNOTSUPP;
37
38 + if (ops->stats)
39 + memset(ops->stats, 0, sizeof(*ops->stats));
40 +
41 if (mtd->flags & MTD_SLC_ON_MLC_EMULATION)
42 ret_code = mtd_io_emulated_slc(mtd, from, true, ops);
43 else
44 @@ -1686,6 +1689,8 @@ int mtd_read_oob(struct mtd_info *mtd, l
45 return ret_code;
46 if (mtd->ecc_strength == 0)
47 return 0; /* device lacks ecc */
48 + if (ops->stats)
49 + ops->stats->max_bitflips = ret_code;
50 return ret_code >= mtd->bitflip_threshold ? -EUCLEAN : 0;
51 }
52 EXPORT_SYMBOL_GPL(mtd_read_oob);
53 --- a/include/linux/mtd/mtd.h
54 +++ b/include/linux/mtd/mtd.h
55 @@ -40,6 +40,10 @@ struct mtd_erase_region_info {
56 unsigned long *lockmap; /* If keeping bitmap of locks */
57 };
58
59 +struct mtd_req_stats {
60 + unsigned int max_bitflips;
61 +};
62 +
63 /**
64 * struct mtd_oob_ops - oob operation operands
65 * @mode: operation mode
66 @@ -70,6 +74,7 @@ struct mtd_oob_ops {
67 uint32_t ooboffs;
68 uint8_t *datbuf;
69 uint8_t *oobbuf;
70 + struct mtd_req_stats *stats;
71 };
72
73 #define MTD_MAX_OOBFREE_ENTRIES_LARGE 32