7fb62e7403c74129ee10874b2f22c4e0219c6d22
[openwrt/staging/jow.git] /
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
5 engines
6
7 Add the necessary helpers to register/unregister hardware ECC engines
8 that will be called from ECC engine drivers.
9
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
13 retired.
14
15 A static list of hardware (only) ECC engines is setup to keep track of
16 the registered engines.
17
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)
21 ---
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(-)
26
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);
31 break;
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;
37 break;
38 default:
39 pr_err("Missing ECC engine type\n");
40 @@ -252,7 +254,7 @@ static int nanddev_put_ecc_engine(struct
41 {
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);
46 break;
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);
52 if (ret) {
53 - pr_err("No ECC engine found\n");
54 + if (ret != -EPROBE_DEFER)
55 + pr_err("No ECC engine found\n");
56 +
57 return ret;
58 }
59
60 --- a/drivers/mtd/nand/ecc.c
61 +++ b/drivers/mtd/nand/ecc.c
62 @@ -96,6 +96,12 @@
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>
69 +
70 +static LIST_HEAD(on_host_hw_engines);
71 +static DEFINE_MUTEX(on_host_hw_engines_mutex);
72
73 /**
74 * nand_ecc_init_ctx - Init the ECC engine context
75 @@ -611,6 +617,88 @@ struct nand_ecc_engine *nand_ecc_get_on_
76 }
77 EXPORT_SYMBOL(nand_ecc_get_on_die_hw_engine);
78
79 +int nand_ecc_register_on_host_hw_engine(struct nand_ecc_engine *engine)
80 +{
81 + struct nand_ecc_engine *item;
82 +
83 + if (!engine)
84 + return -EINVAL;
85 +
86 + /* Prevent multiple registrations of one engine */
87 + list_for_each_entry(item, &on_host_hw_engines, node)
88 + if (item == engine)
89 + return 0;
90 +
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);
94 +
95 + return 0;
96 +}
97 +EXPORT_SYMBOL(nand_ecc_register_on_host_hw_engine);
98 +
99 +int nand_ecc_unregister_on_host_hw_engine(struct nand_ecc_engine *engine)
100 +{
101 + if (!engine)
102 + return -EINVAL;
103 +
104 + mutex_lock(&on_host_hw_engines_mutex);
105 + list_del(&engine->node);
106 + mutex_unlock(&on_host_hw_engines_mutex);
107 +
108 + return 0;
109 +}
110 +EXPORT_SYMBOL(nand_ecc_unregister_on_host_hw_engine);
111 +
112 +static struct nand_ecc_engine *nand_ecc_match_on_host_hw_engine(struct device *dev)
113 +{
114 + struct nand_ecc_engine *item;
115 +
116 + list_for_each_entry(item, &on_host_hw_engines, node)
117 + if (item->dev == dev)
118 + return item;
119 +
120 + return NULL;
121 +}
122 +
123 +struct nand_ecc_engine *nand_ecc_get_on_host_hw_engine(struct nand_device *nand)
124 +{
125 + struct nand_ecc_engine *engine = NULL;
126 + struct device *dev = &nand->mtd.dev;
127 + struct platform_device *pdev;
128 + struct device_node *np;
129 +
130 + if (list_empty(&on_host_hw_engines))
131 + return NULL;
132 +
133 + /* Check for an explicit nand-ecc-engine property */
134 + np = of_parse_phandle(dev->of_node, "nand-ecc-engine", 0);
135 + if (np) {
136 + pdev = of_find_device_by_node(np);
137 + if (!pdev)
138 + return ERR_PTR(-EPROBE_DEFER);
139 +
140 + engine = nand_ecc_match_on_host_hw_engine(&pdev->dev);
141 + platform_device_put(pdev);
142 + of_node_put(np);
143 +
144 + if (!engine)
145 + return ERR_PTR(-EPROBE_DEFER);
146 + }
147 +
148 + if (engine)
149 + get_device(engine->dev);
150 +
151 + return engine;
152 +}
153 +EXPORT_SYMBOL(nand_ecc_get_on_host_hw_engine);
154 +
155 +void nand_ecc_put_on_host_hw_engine(struct nand_device *nand)
156 +{
157 + put_device(nand->ecc.engine->dev);
158 +}
159 +EXPORT_SYMBOL(nand_ecc_put_on_host_hw_engine);
160 +
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 {
167 };
168
169 /**
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
174 + * data around
175 + * @NAND_ECC_ENGINE_INTEGRATION_EXTERNAL: External engine, needs to bring the
176 + * data into its own area before use
177 + */
178 +enum nand_ecc_engine_integration {
179 + NAND_ECC_ENGINE_INTEGRATION_INVALID,
180 + NAND_ECC_ENGINE_INTEGRATION_PIPELINED,
181 + NAND_ECC_ENGINE_INTEGRATION_EXTERNAL,
182 +};
183 +
184 +/**
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
192 */
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;
198 + void *priv;
199 };
200
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);
212
213 #if IS_ENABLED(CONFIG_MTD_NAND_ECC_SW_HAMMING)
214 struct nand_ecc_engine *nand_ecc_sw_hamming_get_engine(void);