450-07-mtd-ubi-provide-NVMEM-layer-over-UBI-volumes.patch 6.1 KB

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