d49a20599dc2b08cd021f3cfb44bd3357d3d9ac9
[openwrt/staging/981213.git] /
1 From 1e37bf84afacd5ba17b7a13a18ca2bc78aff05c0 Mon Sep 17 00:00:00 2001
2 From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <rafal@milecki.pl>
3 Date: Fri, 15 Dec 2023 11:13:58 +0000
4 Subject: [PATCH] nvmem: brcm_nvram: store a copy of NVRAM content
5 MIME-Version: 1.0
6 Content-Type: text/plain; charset=UTF-8
7 Content-Transfer-Encoding: 8bit
8
9 This driver uses MMIO access for reading NVRAM from a flash device.
10 Underneath there is a flash controller that reads data and provides
11 mapping window.
12
13 Using MMIO interface affects controller configuration and may break real
14 controller driver. It was reported by multiple users of devices with
15 NVRAM stored on NAND.
16
17 Modify driver to read & cache NVRAM content during init and use that
18 copy to provide NVMEM data when requested. On NAND flashes due to their
19 alignment NVRAM partitions can be quite big (1 MiB and more) while
20 actual NVRAM content stays quite small (usually 16 to 32 KiB). To avoid
21 allocating so much memory check for actual data length.
22
23 Link: https://lore.kernel.org/linux-mtd/CACna6rwf3_9QVjYcM+847biTX=K0EoWXuXcSMkJO1Vy_5vmVqA@mail.gmail.com/
24 Fixes: 3fef9ed0627a ("nvmem: brcm_nvram: new driver exposing Broadcom's NVRAM")
25 Cc: <Stable@vger.kernel.org>
26 Cc: Arınç ÜNAL <arinc.unal@arinc9.com>
27 Cc: Florian Fainelli <florian.fainelli@broadcom.com>
28 Cc: Scott Branden <scott.branden@broadcom.com>
29 Signed-off-by: Rafał Miłecki <rafal@milecki.pl>
30 Acked-by: Arınç ÜNAL <arinc.unal@arinc9.com>
31 Signed-off-by: Srinivas Kandagatla <srinivas.kandagatla@linaro.org>
32 Link: https://lore.kernel.org/r/20231215111358.316727-3-srinivas.kandagatla@linaro.org
33 Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
34 ---
35 drivers/nvmem/brcm_nvram.c | 134 ++++++++++++++++++++++++++-----------
36 1 file changed, 94 insertions(+), 40 deletions(-)
37
38 --- a/drivers/nvmem/brcm_nvram.c
39 +++ b/drivers/nvmem/brcm_nvram.c
40 @@ -17,9 +17,23 @@
41
42 #define NVRAM_MAGIC "FLSH"
43
44 +/**
45 + * struct brcm_nvram - driver state internal struct
46 + *
47 + * @dev: NVMEM device pointer
48 + * @nvmem_size: Size of the whole space available for NVRAM
49 + * @data: NVRAM data copy stored to avoid poking underlaying flash controller
50 + * @data_len: NVRAM data size
51 + * @padding_byte: Padding value used to fill remaining space
52 + * @cells: Array of discovered NVMEM cells
53 + * @ncells: Number of elements in cells
54 + */
55 struct brcm_nvram {
56 struct device *dev;
57 - void __iomem *base;
58 + size_t nvmem_size;
59 + uint8_t *data;
60 + size_t data_len;
61 + uint8_t padding_byte;
62 struct nvmem_cell_info *cells;
63 int ncells;
64 };
65 @@ -36,10 +50,47 @@ static int brcm_nvram_read(void *context
66 size_t bytes)
67 {
68 struct brcm_nvram *priv = context;
69 - u8 *dst = val;
70 + size_t to_copy;
71 +
72 + if (offset + bytes > priv->data_len)
73 + to_copy = max_t(ssize_t, (ssize_t)priv->data_len - offset, 0);
74 + else
75 + to_copy = bytes;
76 +
77 + memcpy(val, priv->data + offset, to_copy);
78 +
79 + memset((uint8_t *)val + to_copy, priv->padding_byte, bytes - to_copy);
80 +
81 + return 0;
82 +}
83 +
84 +static int brcm_nvram_copy_data(struct brcm_nvram *priv, struct platform_device *pdev)
85 +{
86 + struct resource *res;
87 + void __iomem *base;
88 +
89 + base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
90 + if (IS_ERR(base))
91 + return PTR_ERR(base);
92 +
93 + priv->nvmem_size = resource_size(res);
94 +
95 + priv->padding_byte = readb(base + priv->nvmem_size - 1);
96 + for (priv->data_len = priv->nvmem_size;
97 + priv->data_len;
98 + priv->data_len--) {
99 + if (readb(base + priv->data_len - 1) != priv->padding_byte)
100 + break;
101 + }
102 + WARN(priv->data_len > SZ_128K, "Unexpected (big) NVRAM size: %zu B\n", priv->data_len);
103
104 - while (bytes--)
105 - *dst++ = readb(priv->base + offset++);
106 + priv->data = devm_kzalloc(priv->dev, priv->data_len, GFP_KERNEL);
107 + if (!priv->data)
108 + return -ENOMEM;
109 +
110 + memcpy_fromio(priv->data, base, priv->data_len);
111 +
112 + bcm47xx_nvram_init_from_iomem(base, priv->data_len);
113
114 return 0;
115 }
116 @@ -67,8 +118,13 @@ static int brcm_nvram_add_cells(struct b
117 size_t len)
118 {
119 struct device *dev = priv->dev;
120 - char *var, *value, *eq;
121 + char *var, *value;
122 + uint8_t tmp;
123 int idx;
124 + int err = 0;
125 +
126 + tmp = priv->data[len - 1];
127 + priv->data[len - 1] = '\0';
128
129 priv->ncells = 0;
130 for (var = data + sizeof(struct brcm_nvram_header);
131 @@ -78,67 +134,68 @@ static int brcm_nvram_add_cells(struct b
132 }
133
134 priv->cells = devm_kcalloc(dev, priv->ncells, sizeof(*priv->cells), GFP_KERNEL);
135 - if (!priv->cells)
136 - return -ENOMEM;
137 + if (!priv->cells) {
138 + err = -ENOMEM;
139 + goto out;
140 + }
141
142 for (var = data + sizeof(struct brcm_nvram_header), idx = 0;
143 var < (char *)data + len && *var;
144 var = value + strlen(value) + 1, idx++) {
145 + char *eq, *name;
146 +
147 eq = strchr(var, '=');
148 if (!eq)
149 break;
150 *eq = '\0';
151 + name = devm_kstrdup(dev, var, GFP_KERNEL);
152 + *eq = '=';
153 + if (!name) {
154 + err = -ENOMEM;
155 + goto out;
156 + }
157 value = eq + 1;
158
159 - priv->cells[idx].name = devm_kstrdup(dev, var, GFP_KERNEL);
160 - if (!priv->cells[idx].name)
161 - return -ENOMEM;
162 + priv->cells[idx].name = name;
163 priv->cells[idx].offset = value - (char *)data;
164 priv->cells[idx].bytes = strlen(value);
165 priv->cells[idx].np = of_get_child_by_name(dev->of_node, priv->cells[idx].name);
166 - if (!strcmp(var, "et0macaddr") ||
167 - !strcmp(var, "et1macaddr") ||
168 - !strcmp(var, "et2macaddr")) {
169 + if (!strcmp(name, "et0macaddr") ||
170 + !strcmp(name, "et1macaddr") ||
171 + !strcmp(name, "et2macaddr")) {
172 priv->cells[idx].raw_len = strlen(value);
173 priv->cells[idx].bytes = ETH_ALEN;
174 priv->cells[idx].read_post_process = brcm_nvram_read_post_process_macaddr;
175 }
176 }
177
178 - return 0;
179 +out:
180 + priv->data[len - 1] = tmp;
181 + return err;
182 }
183
184 static int brcm_nvram_parse(struct brcm_nvram *priv)
185 {
186 + struct brcm_nvram_header *header = (struct brcm_nvram_header *)priv->data;
187 struct device *dev = priv->dev;
188 - struct brcm_nvram_header header;
189 - uint8_t *data;
190 size_t len;
191 int err;
192
193 - memcpy_fromio(&header, priv->base, sizeof(header));
194 -
195 - if (memcmp(header.magic, NVRAM_MAGIC, 4)) {
196 + if (memcmp(header->magic, NVRAM_MAGIC, 4)) {
197 dev_err(dev, "Invalid NVRAM magic\n");
198 return -EINVAL;
199 }
200
201 - len = le32_to_cpu(header.len);
202 -
203 - data = kzalloc(len, GFP_KERNEL);
204 - if (!data)
205 - return -ENOMEM;
206 -
207 - memcpy_fromio(data, priv->base, len);
208 - data[len - 1] = '\0';
209 -
210 - err = brcm_nvram_add_cells(priv, data, len);
211 - if (err) {
212 - dev_err(dev, "Failed to add cells: %d\n", err);
213 - return err;
214 + len = le32_to_cpu(header->len);
215 + if (len > priv->nvmem_size) {
216 + dev_err(dev, "NVRAM length (%zd) exceeds mapped size (%zd)\n", len,
217 + priv->nvmem_size);
218 + return -EINVAL;
219 }
220
221 - kfree(data);
222 + err = brcm_nvram_add_cells(priv, priv->data, len);
223 + if (err)
224 + dev_err(dev, "Failed to add cells: %d\n", err);
225
226 return 0;
227 }
228 @@ -150,7 +207,6 @@ static int brcm_nvram_probe(struct platf
229 .reg_read = brcm_nvram_read,
230 };
231 struct device *dev = &pdev->dev;
232 - struct resource *res;
233 struct brcm_nvram *priv;
234 int err;
235
236 @@ -159,21 +215,19 @@ static int brcm_nvram_probe(struct platf
237 return -ENOMEM;
238 priv->dev = dev;
239
240 - priv->base = devm_platform_get_and_ioremap_resource(pdev, 0, &res);
241 - if (IS_ERR(priv->base))
242 - return PTR_ERR(priv->base);
243 + err = brcm_nvram_copy_data(priv, pdev);
244 + if (err)
245 + return err;
246
247 err = brcm_nvram_parse(priv);
248 if (err)
249 return err;
250
251 - bcm47xx_nvram_init_from_iomem(priv->base, resource_size(res));
252 -
253 config.dev = dev;
254 config.cells = priv->cells;
255 config.ncells = priv->ncells;
256 config.priv = priv;
257 - config.size = resource_size(res);
258 + config.size = priv->nvmem_size;
259
260 return PTR_ERR_OR_ZERO(devm_nvmem_register(dev, &config));
261 }