1 From ad4944aa0b02cb043afe20bc2a018c161e65c992 Mon Sep 17 00:00:00 2001
2 From: Miquel Raynal <miquel.raynal@bootlin.com>
3 Date: Thu, 16 Dec 2021 12:16:38 +0100
4 Subject: [PATCH 01/15] mtd: nand: ecc: Add infrastructure to support hardware
7 Add the necessary helpers to register/unregister hardware ECC engines
8 that will be called from ECC engine drivers.
10 Also add helpers to get the right engine from the user
11 perspective. Keep a reference of the in use ECC engine in order to
12 prevent modules to be unloaded. Put the reference when the engine gets
15 A static list of hardware (only) ECC engines is setup to keep track of
16 the registered engines.
18 Signed-off-by: Miquel Raynal <miquel.raynal@bootlin.com>
19 Link: https://lore.kernel.org/linux-mtd/20211216111654.238086-13-miquel.raynal@bootlin.com
20 (cherry picked from commit 96489c1c0b53131b0e1ec33e2060538379ad6152)
22 drivers/mtd/nand/core.c | 10 +++--
23 drivers/mtd/nand/ecc.c | 88 ++++++++++++++++++++++++++++++++++++++++
24 include/linux/mtd/nand.h | 28 +++++++++++++
25 3 files changed, 123 insertions(+), 3 deletions(-)
27 diff --git a/drivers/mtd/nand/core.c b/drivers/mtd/nand/core.c
28 index 5e13a03d2b32..b228b4d13b7a 100644
29 --- a/drivers/mtd/nand/core.c
30 +++ b/drivers/mtd/nand/core.c
31 @@ -232,7 +232,9 @@ static int nanddev_get_ecc_engine(struct nand_device *nand)
32 nand->ecc.engine = nand_ecc_get_on_die_hw_engine(nand);
34 case NAND_ECC_ENGINE_TYPE_ON_HOST:
35 - pr_err("On-host hardware ECC engines not supported yet\n");
36 + nand->ecc.engine = nand_ecc_get_on_host_hw_engine(nand);
37 + if (PTR_ERR(nand->ecc.engine) == -EPROBE_DEFER)
38 + return -EPROBE_DEFER;
41 pr_err("Missing ECC engine type\n");
42 @@ -252,7 +254,7 @@ static int nanddev_put_ecc_engine(struct nand_device *nand)
44 switch (nand->ecc.ctx.conf.engine_type) {
45 case NAND_ECC_ENGINE_TYPE_ON_HOST:
46 - pr_err("On-host hardware ECC engines not supported yet\n");
47 + nand_ecc_put_on_host_hw_engine(nand);
49 case NAND_ECC_ENGINE_TYPE_NONE:
50 case NAND_ECC_ENGINE_TYPE_SOFT:
51 @@ -297,7 +299,9 @@ int nanddev_ecc_engine_init(struct nand_device *nand)
52 /* Look for the ECC engine to use */
53 ret = nanddev_get_ecc_engine(nand);
55 - pr_err("No ECC engine found\n");
56 + if (ret != -EPROBE_DEFER)
57 + pr_err("No ECC engine found\n");
62 diff --git a/drivers/mtd/nand/ecc.c b/drivers/mtd/nand/ecc.c
63 index 6c43dfda01d4..078f5ec38de3 100644
64 --- a/drivers/mtd/nand/ecc.c
65 +++ b/drivers/mtd/nand/ecc.c
67 #include <linux/module.h>
68 #include <linux/mtd/nand.h>
69 #include <linux/slab.h>
70 +#include <linux/of.h>
71 +#include <linux/of_device.h>
72 +#include <linux/of_platform.h>
74 +static LIST_HEAD(on_host_hw_engines);
75 +static DEFINE_MUTEX(on_host_hw_engines_mutex);
78 * nand_ecc_init_ctx - Init the ECC engine context
79 @@ -611,6 +617,88 @@ struct nand_ecc_engine *nand_ecc_get_on_die_hw_engine(struct nand_device *nand)
81 EXPORT_SYMBOL(nand_ecc_get_on_die_hw_engine);
83 +int nand_ecc_register_on_host_hw_engine(struct nand_ecc_engine *engine)
85 + struct nand_ecc_engine *item;
90 + /* Prevent multiple registrations of one engine */
91 + list_for_each_entry(item, &on_host_hw_engines, node)
95 + mutex_lock(&on_host_hw_engines_mutex);
96 + list_add_tail(&engine->node, &on_host_hw_engines);
97 + mutex_unlock(&on_host_hw_engines_mutex);
101 +EXPORT_SYMBOL(nand_ecc_register_on_host_hw_engine);
103 +int nand_ecc_unregister_on_host_hw_engine(struct nand_ecc_engine *engine)
108 + mutex_lock(&on_host_hw_engines_mutex);
109 + list_del(&engine->node);
110 + mutex_unlock(&on_host_hw_engines_mutex);
114 +EXPORT_SYMBOL(nand_ecc_unregister_on_host_hw_engine);
116 +static struct nand_ecc_engine *nand_ecc_match_on_host_hw_engine(struct device *dev)
118 + struct nand_ecc_engine *item;
120 + list_for_each_entry(item, &on_host_hw_engines, node)
121 + if (item->dev == dev)
127 +struct nand_ecc_engine *nand_ecc_get_on_host_hw_engine(struct nand_device *nand)
129 + struct nand_ecc_engine *engine = NULL;
130 + struct device *dev = &nand->mtd.dev;
131 + struct platform_device *pdev;
132 + struct device_node *np;
134 + if (list_empty(&on_host_hw_engines))
137 + /* Check for an explicit nand-ecc-engine property */
138 + np = of_parse_phandle(dev->of_node, "nand-ecc-engine", 0);
140 + pdev = of_find_device_by_node(np);
142 + return ERR_PTR(-EPROBE_DEFER);
144 + engine = nand_ecc_match_on_host_hw_engine(&pdev->dev);
145 + platform_device_put(pdev);
149 + return ERR_PTR(-EPROBE_DEFER);
153 + get_device(engine->dev);
157 +EXPORT_SYMBOL(nand_ecc_get_on_host_hw_engine);
159 +void nand_ecc_put_on_host_hw_engine(struct nand_device *nand)
161 + put_device(nand->ecc.engine->dev);
163 +EXPORT_SYMBOL(nand_ecc_put_on_host_hw_engine);
165 MODULE_LICENSE("GPL");
166 MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>");
167 MODULE_DESCRIPTION("Generic ECC engine");
168 diff --git a/include/linux/mtd/nand.h b/include/linux/mtd/nand.h
169 index 32fc7edf65b3..4ddd20fe9c9e 100644
170 --- a/include/linux/mtd/nand.h
171 +++ b/include/linux/mtd/nand.h
172 @@ -263,12 +263,36 @@ struct nand_ecc_engine_ops {
173 struct nand_page_io_req *req);
177 + * enum nand_ecc_engine_integration - How the NAND ECC engine is integrated
178 + * @NAND_ECC_ENGINE_INTEGRATION_INVALID: Invalid value
179 + * @NAND_ECC_ENGINE_INTEGRATION_PIPELINED: Pipelined engine, performs on-the-fly
180 + * correction, does not need to copy
182 + * @NAND_ECC_ENGINE_INTEGRATION_EXTERNAL: External engine, needs to bring the
183 + * data into its own area before use
185 +enum nand_ecc_engine_integration {
186 + NAND_ECC_ENGINE_INTEGRATION_INVALID,
187 + NAND_ECC_ENGINE_INTEGRATION_PIPELINED,
188 + NAND_ECC_ENGINE_INTEGRATION_EXTERNAL,
192 * struct nand_ecc_engine - ECC engine abstraction for NAND devices
193 + * @dev: Host device
194 + * @node: Private field for registration time
195 * @ops: ECC engine operations
196 + * @integration: How the engine is integrated with the host
197 + * (only relevant on %NAND_ECC_ENGINE_TYPE_ON_HOST engines)
198 + * @priv: Private data
200 struct nand_ecc_engine {
201 + struct device *dev;
202 + struct list_head node;
203 struct nand_ecc_engine_ops *ops;
204 + enum nand_ecc_engine_integration integration;
208 void of_get_nand_ecc_user_config(struct nand_device *nand);
209 @@ -279,8 +303,12 @@ int nand_ecc_prepare_io_req(struct nand_device *nand,
210 int nand_ecc_finish_io_req(struct nand_device *nand,
211 struct nand_page_io_req *req);
212 bool nand_ecc_is_strong_enough(struct nand_device *nand);
213 +int nand_ecc_register_on_host_hw_engine(struct nand_ecc_engine *engine);
214 +int nand_ecc_unregister_on_host_hw_engine(struct nand_ecc_engine *engine);
215 struct nand_ecc_engine *nand_ecc_get_sw_engine(struct nand_device *nand);
216 struct nand_ecc_engine *nand_ecc_get_on_die_hw_engine(struct nand_device *nand);
217 +struct nand_ecc_engine *nand_ecc_get_on_host_hw_engine(struct nand_device *nand);
218 +void nand_ecc_put_on_host_hw_engine(struct nand_device *nand);
220 #if IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING)
221 struct nand_ecc_engine *nand_ecc_sw_hamming_get_engine(void);