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 --- a/drivers/mtd/nand/core.c
28 +++ b/drivers/mtd/nand/core.c
29 @@ -232,7 +232,9 @@ static int nanddev_get_ecc_engine(struct
30 nand->ecc.engine = nand_ecc_get_on_die_hw_engine(nand);
32 case NAND_ECC_ENGINE_TYPE_ON_HOST:
33 - pr_err("On-host hardware ECC engines not supported yet\n");
34 + nand->ecc.engine = nand_ecc_get_on_host_hw_engine(nand);
35 + if (PTR_ERR(nand->ecc.engine) == -EPROBE_DEFER)
36 + return -EPROBE_DEFER;
39 pr_err("Missing ECC engine type\n");
40 @@ -252,7 +254,7 @@ static int nanddev_put_ecc_engine(struct
42 switch (nand->ecc.ctx.conf.engine_type) {
43 case NAND_ECC_ENGINE_TYPE_ON_HOST:
44 - pr_err("On-host hardware ECC engines not supported yet\n");
45 + nand_ecc_put_on_host_hw_engine(nand);
47 case NAND_ECC_ENGINE_TYPE_NONE:
48 case NAND_ECC_ENGINE_TYPE_SOFT:
49 @@ -297,7 +299,9 @@ int nanddev_ecc_engine_init(struct nand_
50 /* Look for the ECC engine to use */
51 ret = nanddev_get_ecc_engine(nand);
53 - pr_err("No ECC engine found\n");
54 + if (ret != -EPROBE_DEFER)
55 + pr_err("No ECC engine found\n");
60 --- a/drivers/mtd/nand/ecc.c
61 +++ b/drivers/mtd/nand/ecc.c
63 #include <linux/module.h>
64 #include <linux/mtd/nand.h>
65 #include <linux/slab.h>
66 +#include <linux/of.h>
67 +#include <linux/of_device.h>
68 +#include <linux/of_platform.h>
70 +static LIST_HEAD(on_host_hw_engines);
71 +static DEFINE_MUTEX(on_host_hw_engines_mutex);
74 * nand_ecc_init_ctx - Init the ECC engine context
75 @@ -611,6 +617,88 @@ struct nand_ecc_engine *nand_ecc_get_on_
77 EXPORT_SYMBOL(nand_ecc_get_on_die_hw_engine);
79 +int nand_ecc_register_on_host_hw_engine(struct nand_ecc_engine *engine)
81 + struct nand_ecc_engine *item;
86 + /* Prevent multiple registrations of one engine */
87 + list_for_each_entry(item, &on_host_hw_engines, node)
91 + mutex_lock(&on_host_hw_engines_mutex);
92 + list_add_tail(&engine->node, &on_host_hw_engines);
93 + mutex_unlock(&on_host_hw_engines_mutex);
97 +EXPORT_SYMBOL(nand_ecc_register_on_host_hw_engine);
99 +int nand_ecc_unregister_on_host_hw_engine(struct nand_ecc_engine *engine)
104 + mutex_lock(&on_host_hw_engines_mutex);
105 + list_del(&engine->node);
106 + mutex_unlock(&on_host_hw_engines_mutex);
110 +EXPORT_SYMBOL(nand_ecc_unregister_on_host_hw_engine);
112 +static struct nand_ecc_engine *nand_ecc_match_on_host_hw_engine(struct device *dev)
114 + struct nand_ecc_engine *item;
116 + list_for_each_entry(item, &on_host_hw_engines, node)
117 + if (item->dev == dev)
123 +struct nand_ecc_engine *nand_ecc_get_on_host_hw_engine(struct nand_device *nand)
125 + struct nand_ecc_engine *engine = NULL;
126 + struct device *dev = &nand->mtd.dev;
127 + struct platform_device *pdev;
128 + struct device_node *np;
130 + if (list_empty(&on_host_hw_engines))
133 + /* Check for an explicit nand-ecc-engine property */
134 + np = of_parse_phandle(dev->of_node, "nand-ecc-engine", 0);
136 + pdev = of_find_device_by_node(np);
138 + return ERR_PTR(-EPROBE_DEFER);
140 + engine = nand_ecc_match_on_host_hw_engine(&pdev->dev);
141 + platform_device_put(pdev);
145 + return ERR_PTR(-EPROBE_DEFER);
149 + get_device(engine->dev);
153 +EXPORT_SYMBOL(nand_ecc_get_on_host_hw_engine);
155 +void nand_ecc_put_on_host_hw_engine(struct nand_device *nand)
157 + put_device(nand->ecc.engine->dev);
159 +EXPORT_SYMBOL(nand_ecc_put_on_host_hw_engine);
161 MODULE_LICENSE("GPL");
162 MODULE_AUTHOR("Miquel Raynal <miquel.raynal@bootlin.com>");
163 MODULE_DESCRIPTION("Generic ECC engine");
164 --- a/include/linux/mtd/nand.h
165 +++ b/include/linux/mtd/nand.h
166 @@ -264,11 +264,35 @@ struct nand_ecc_engine_ops {
170 + * enum nand_ecc_engine_integration - How the NAND ECC engine is integrated
171 + * @NAND_ECC_ENGINE_INTEGRATION_INVALID: Invalid value
172 + * @NAND_ECC_ENGINE_INTEGRATION_PIPELINED: Pipelined engine, performs on-the-fly
173 + * correction, does not need to copy
175 + * @NAND_ECC_ENGINE_INTEGRATION_EXTERNAL: External engine, needs to bring the
176 + * data into its own area before use
178 +enum nand_ecc_engine_integration {
179 + NAND_ECC_ENGINE_INTEGRATION_INVALID,
180 + NAND_ECC_ENGINE_INTEGRATION_PIPELINED,
181 + NAND_ECC_ENGINE_INTEGRATION_EXTERNAL,
185 * struct nand_ecc_engine - ECC engine abstraction for NAND devices
186 + * @dev: Host device
187 + * @node: Private field for registration time
188 * @ops: ECC engine operations
189 + * @integration: How the engine is integrated with the host
190 + * (only relevant on %NAND_ECC_ENGINE_TYPE_ON_HOST engines)
191 + * @priv: Private data
193 struct nand_ecc_engine {
194 + struct device *dev;
195 + struct list_head node;
196 struct nand_ecc_engine_ops *ops;
197 + enum nand_ecc_engine_integration integration;
201 void of_get_nand_ecc_user_config(struct nand_device *nand);
202 @@ -279,8 +303,12 @@ int nand_ecc_prepare_io_req(struct nand_
203 int nand_ecc_finish_io_req(struct nand_device *nand,
204 struct nand_page_io_req *req);
205 bool nand_ecc_is_strong_enough(struct nand_device *nand);
206 +int nand_ecc_register_on_host_hw_engine(struct nand_ecc_engine *engine);
207 +int nand_ecc_unregister_on_host_hw_engine(struct nand_ecc_engine *engine);
208 struct nand_ecc_engine *nand_ecc_get_sw_engine(struct nand_device *nand);
209 struct nand_ecc_engine *nand_ecc_get_on_die_hw_engine(struct nand_device *nand);
210 +struct nand_ecc_engine *nand_ecc_get_on_host_hw_engine(struct nand_device *nand);
211 +void nand_ecc_put_on_host_hw_engine(struct nand_device *nand);
213 #if IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING)
214 struct nand_ecc_engine *nand_ecc_sw_hamming_get_engine(void);