9e6fbea38cf469841302b657e88eaf2ffaf12e0e
[openwrt/staging/stintel.git] / target / linux / generic / pending-6.1 / 450-07-mtd-ubi-provide-NVMEM-layer-over-UBI-volumes.patch
1 From 7eb6666348f3f2d1f7308c712fa5903cbe189401 Mon Sep 17 00:00:00 2001
2 From: Daniel Golle <daniel@makrotopia.org>
3 Date: Thu, 8 Jun 2023 17:22:04 +0100
4 Subject: [PATCH 07/15] mtd: ubi: provide NVMEM layer over UBI volumes
5
6 In an ideal world we would like UBI to be used where ever possible on a
7 NAND chip. And with UBI support in ARM Trusted Firmware and U-Boot it
8 is possible to achieve an (almost-)all-UBI flash layout. Hence the need
9 for a way to also use UBI volumes to store board-level constants, such
10 as MAC addresses and calibration data of wireless interfaces.
11
12 Add UBI volume NVMEM driver module exposing UBI volumes as NVMEM
13 providers. Allow UBI devices to have a "volumes" firmware subnode with
14 volumes which may be compatible with "nvmem-cells".
15 Access to UBI volumes via the NVMEM interface at this point is
16 read-only, and it is slow, opening and closing the UBI volume for each
17 access due to limitations of the NVMEM provider API.
18
19 Signed-off-by: Daniel Golle <daniel@makrotopia.org>
20 ---
21 drivers/mtd/ubi/Kconfig | 12 +++
22 drivers/mtd/ubi/Makefile | 1 +
23 drivers/mtd/ubi/nvmem.c | 188 +++++++++++++++++++++++++++++++++++++++
24 3 files changed, 201 insertions(+)
25 create mode 100644 drivers/mtd/ubi/nvmem.c
26
27 --- a/drivers/mtd/ubi/Kconfig
28 +++ b/drivers/mtd/ubi/Kconfig
29 @@ -104,4 +104,16 @@ config MTD_UBI_BLOCK
30
31 If in doubt, say "N".
32
33 +config MTD_UBI_NVMEM
34 + tristate "UBI virtual NVMEM"
35 + default n
36 + depends on NVMEM
37 + help
38 + This option enabled an additional driver exposing UBI volumes as NVMEM
39 + providers, intended for platforms where UBI is part of the firmware
40 + specification and used to store also e.g. MAC addresses or board-
41 + specific Wi-Fi calibration data.
42 +
43 + If in doubt, say "N".
44 +
45 endif # MTD_UBI
46 --- a/drivers/mtd/ubi/Makefile
47 +++ b/drivers/mtd/ubi/Makefile
48 @@ -7,3 +7,4 @@ ubi-$(CONFIG_MTD_UBI_FASTMAP) += fastmap
49 ubi-$(CONFIG_MTD_UBI_BLOCK) += block.o
50
51 obj-$(CONFIG_MTD_UBI_GLUEBI) += gluebi.o
52 +obj-$(CONFIG_MTD_UBI_NVMEM) += nvmem.o
53 --- /dev/null
54 +++ b/drivers/mtd/ubi/nvmem.c
55 @@ -0,0 +1,188 @@
56 +// SPDX-License-Identifier: GPL-2.0-or-later
57 +/*
58 + * Copyright (c) 2023 Daniel Golle <daniel@makrotopia.org>
59 + */
60 +
61 +/* UBI NVMEM provider */
62 +#include "ubi.h"
63 +#include <linux/nvmem-provider.h>
64 +#include <asm/div64.h>
65 +
66 +/* List of all NVMEM devices */
67 +static LIST_HEAD(nvmem_devices);
68 +static DEFINE_MUTEX(devices_mutex);
69 +
70 +struct ubi_nvmem {
71 + struct nvmem_device *nvmem;
72 + int ubi_num;
73 + int vol_id;
74 + int usable_leb_size;
75 + struct list_head list;
76 +};
77 +
78 +static int ubi_nvmem_reg_read(void *priv, unsigned int from,
79 + void *val, size_t bytes)
80 +{
81 + int err = 0, lnum = from, offs, bytes_left = bytes, to_read;
82 + struct ubi_nvmem *unv = priv;
83 + struct ubi_volume_desc *desc;
84 +
85 + desc = ubi_open_volume(unv->ubi_num, unv->vol_id, UBI_READONLY);
86 + if (IS_ERR(desc))
87 + return PTR_ERR(desc);
88 +
89 + offs = do_div(lnum, unv->usable_leb_size);
90 + while (bytes_left) {
91 + to_read = unv->usable_leb_size - offs;
92 +
93 + if (to_read > bytes_left)
94 + to_read = bytes_left;
95 +
96 + err = ubi_read(desc, lnum, val, offs, to_read);
97 + if (err)
98 + break;
99 +
100 + lnum += 1;
101 + offs = 0;
102 + bytes_left -= to_read;
103 + val += to_read;
104 + }
105 + ubi_close_volume(desc);
106 +
107 + if (err)
108 + return err;
109 +
110 + return bytes_left == 0 ? 0 : -EIO;
111 +}
112 +
113 +static int ubi_nvmem_add(struct ubi_volume_info *vi)
114 +{
115 + struct device_node *np = dev_of_node(vi->dev);
116 + struct nvmem_config config = {};
117 + struct ubi_nvmem *unv;
118 + int ret;
119 +
120 + if (!np)
121 + return 0;
122 +
123 + if (!of_get_child_by_name(np, "nvmem-layout"))
124 + return 0;
125 +
126 + if (WARN_ON_ONCE(vi->usable_leb_size <= 0) ||
127 + WARN_ON_ONCE(vi->size <= 0))
128 + return -EINVAL;
129 +
130 + unv = kzalloc(sizeof(struct ubi_nvmem), GFP_KERNEL);
131 + if (!unv)
132 + return -ENOMEM;
133 +
134 + config.id = NVMEM_DEVID_NONE;
135 + config.dev = vi->dev;
136 + config.name = dev_name(vi->dev);
137 + config.owner = THIS_MODULE;
138 + config.priv = unv;
139 + config.reg_read = ubi_nvmem_reg_read;
140 + config.size = vi->usable_leb_size * vi->size;
141 + config.word_size = 1;
142 + config.stride = 1;
143 + config.read_only = true;
144 + config.root_only = true;
145 + config.ignore_wp = true;
146 + config.of_node = np;
147 +
148 + unv->ubi_num = vi->ubi_num;
149 + unv->vol_id = vi->vol_id;
150 + unv->usable_leb_size = vi->usable_leb_size;
151 + unv->nvmem = nvmem_register(&config);
152 + if (IS_ERR(unv->nvmem)) {
153 + ret = dev_err_probe(vi->dev, PTR_ERR(unv->nvmem),
154 + "Failed to register NVMEM device\n");
155 + kfree(unv);
156 + return ret;
157 + }
158 +
159 + mutex_lock(&devices_mutex);
160 + list_add_tail(&unv->list, &nvmem_devices);
161 + mutex_unlock(&devices_mutex);
162 +
163 + return 0;
164 +}
165 +
166 +static void ubi_nvmem_remove(struct ubi_volume_info *vi)
167 +{
168 + struct ubi_nvmem *unv_c, *unv = NULL;
169 +
170 + mutex_lock(&devices_mutex);
171 + list_for_each_entry(unv_c, &nvmem_devices, list)
172 + if (unv_c->ubi_num == vi->ubi_num && unv_c->vol_id == vi->vol_id) {
173 + unv = unv_c;
174 + break;
175 + }
176 +
177 + if (!unv) {
178 + mutex_unlock(&devices_mutex);
179 + return;
180 + }
181 +
182 + list_del(&unv->list);
183 + mutex_unlock(&devices_mutex);
184 + nvmem_unregister(unv->nvmem);
185 + kfree(unv);
186 +}
187 +
188 +/**
189 + * nvmem_notify - UBI notification handler.
190 + * @nb: registered notifier block
191 + * @l: notification type
192 + * @ns_ptr: pointer to the &struct ubi_notification object
193 + */
194 +static int nvmem_notify(struct notifier_block *nb, unsigned long l,
195 + void *ns_ptr)
196 +{
197 + struct ubi_notification *nt = ns_ptr;
198 +
199 + switch (l) {
200 + case UBI_VOLUME_RESIZED:
201 + ubi_nvmem_remove(&nt->vi);
202 + fallthrough;
203 + case UBI_VOLUME_ADDED:
204 + ubi_nvmem_add(&nt->vi);
205 + break;
206 + case UBI_VOLUME_SHUTDOWN:
207 + ubi_nvmem_remove(&nt->vi);
208 + break;
209 + default:
210 + break;
211 + }
212 + return NOTIFY_OK;
213 +}
214 +
215 +static struct notifier_block nvmem_notifier = {
216 + .notifier_call = nvmem_notify,
217 +};
218 +
219 +static int __init ubi_nvmem_init(void)
220 +{
221 + return ubi_register_volume_notifier(&nvmem_notifier, 0);
222 +}
223 +
224 +static void __exit ubi_nvmem_exit(void)
225 +{
226 + struct ubi_nvmem *unv, *tmp;
227 +
228 + mutex_lock(&devices_mutex);
229 + list_for_each_entry_safe(unv, tmp, &nvmem_devices, list) {
230 + nvmem_unregister(unv->nvmem);
231 + list_del(&unv->list);
232 + kfree(unv);
233 + }
234 + mutex_unlock(&devices_mutex);
235 +
236 + ubi_unregister_volume_notifier(&nvmem_notifier);
237 +}
238 +
239 +module_init(ubi_nvmem_init);
240 +module_exit(ubi_nvmem_exit);
241 +MODULE_DESCRIPTION("NVMEM layer over UBI volumes");
242 +MODULE_AUTHOR("Daniel Golle");
243 +MODULE_LICENSE("GPL");