nvmem.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Copyright (c) 2023 Daniel Golle <daniel@makrotopia.org>
  4. */
  5. /* UBI NVMEM provider */
  6. #include "ubi.h"
  7. #include <linux/nvmem-provider.h>
  8. /* List of all NVMEM devices */
  9. static LIST_HEAD(nvmem_devices);
  10. static DEFINE_MUTEX(devices_mutex);
  11. struct ubi_nvmem {
  12. struct nvmem_device *nvmem;
  13. int ubi_num;
  14. int vol_id;
  15. int usable_leb_size;
  16. struct list_head list;
  17. };
  18. static int ubi_nvmem_reg_read(void *priv, unsigned int from,
  19. void *val, size_t bytes)
  20. {
  21. size_t to_read, bytes_left = bytes;
  22. struct ubi_nvmem *unv = priv;
  23. struct ubi_volume_desc *desc;
  24. uint32_t offs;
  25. uint32_t lnum;
  26. int err = 0;
  27. desc = ubi_open_volume(unv->ubi_num, unv->vol_id, UBI_READONLY);
  28. if (IS_ERR(desc))
  29. return PTR_ERR(desc);
  30. offs = from % unv->usable_leb_size;
  31. lnum = from / unv->usable_leb_size;
  32. while (bytes_left) {
  33. to_read = unv->usable_leb_size - offs;
  34. if (to_read > bytes_left)
  35. to_read = bytes_left;
  36. err = ubi_read(desc, lnum, val, offs, to_read);
  37. if (err)
  38. break;
  39. lnum += 1;
  40. offs = 0;
  41. bytes_left -= to_read;
  42. val += to_read;
  43. }
  44. ubi_close_volume(desc);
  45. if (err)
  46. return err;
  47. return 0;
  48. }
  49. static int ubi_nvmem_add(struct ubi_volume_info *vi)
  50. {
  51. struct device_node *np = dev_of_node(vi->dev);
  52. struct nvmem_config config = {};
  53. struct ubi_nvmem *unv;
  54. int ret;
  55. if (!np)
  56. return 0;
  57. if (!of_get_child_by_name(np, "nvmem-layout"))
  58. return 0;
  59. if (WARN_ON_ONCE(vi->usable_leb_size <= 0) ||
  60. WARN_ON_ONCE(vi->size <= 0))
  61. return -EINVAL;
  62. unv = kzalloc_obj(struct ubi_nvmem);
  63. if (!unv)
  64. return -ENOMEM;
  65. config.id = NVMEM_DEVID_NONE;
  66. config.dev = vi->dev;
  67. config.name = dev_name(vi->dev);
  68. config.owner = THIS_MODULE;
  69. config.priv = unv;
  70. config.reg_read = ubi_nvmem_reg_read;
  71. config.size = vi->usable_leb_size * vi->size;
  72. config.word_size = 1;
  73. config.stride = 1;
  74. config.read_only = true;
  75. config.root_only = true;
  76. config.ignore_wp = true;
  77. config.of_node = np;
  78. unv->ubi_num = vi->ubi_num;
  79. unv->vol_id = vi->vol_id;
  80. unv->usable_leb_size = vi->usable_leb_size;
  81. unv->nvmem = nvmem_register(&config);
  82. if (IS_ERR(unv->nvmem)) {
  83. ret = dev_err_probe(vi->dev, PTR_ERR(unv->nvmem),
  84. "Failed to register NVMEM device\n");
  85. kfree(unv);
  86. return ret;
  87. }
  88. mutex_lock(&devices_mutex);
  89. list_add_tail(&unv->list, &nvmem_devices);
  90. mutex_unlock(&devices_mutex);
  91. return 0;
  92. }
  93. static void ubi_nvmem_remove(struct ubi_volume_info *vi)
  94. {
  95. struct ubi_nvmem *unv_c, *unv = NULL;
  96. mutex_lock(&devices_mutex);
  97. list_for_each_entry(unv_c, &nvmem_devices, list)
  98. if (unv_c->ubi_num == vi->ubi_num && unv_c->vol_id == vi->vol_id) {
  99. unv = unv_c;
  100. break;
  101. }
  102. if (!unv) {
  103. mutex_unlock(&devices_mutex);
  104. return;
  105. }
  106. list_del(&unv->list);
  107. mutex_unlock(&devices_mutex);
  108. nvmem_unregister(unv->nvmem);
  109. kfree(unv);
  110. }
  111. /**
  112. * nvmem_notify - UBI notification handler.
  113. * @nb: registered notifier block
  114. * @l: notification type
  115. * @ns_ptr: pointer to the &struct ubi_notification object
  116. */
  117. static int nvmem_notify(struct notifier_block *nb, unsigned long l,
  118. void *ns_ptr)
  119. {
  120. struct ubi_notification *nt = ns_ptr;
  121. switch (l) {
  122. case UBI_VOLUME_RESIZED:
  123. ubi_nvmem_remove(&nt->vi);
  124. fallthrough;
  125. case UBI_VOLUME_ADDED:
  126. ubi_nvmem_add(&nt->vi);
  127. break;
  128. case UBI_VOLUME_SHUTDOWN:
  129. ubi_nvmem_remove(&nt->vi);
  130. break;
  131. default:
  132. break;
  133. }
  134. return NOTIFY_OK;
  135. }
  136. static struct notifier_block nvmem_notifier = {
  137. .notifier_call = nvmem_notify,
  138. };
  139. static int __init ubi_nvmem_init(void)
  140. {
  141. return ubi_register_volume_notifier(&nvmem_notifier, 0);
  142. }
  143. static void __exit ubi_nvmem_exit(void)
  144. {
  145. struct ubi_nvmem *unv, *tmp;
  146. mutex_lock(&devices_mutex);
  147. list_for_each_entry_safe(unv, tmp, &nvmem_devices, list) {
  148. nvmem_unregister(unv->nvmem);
  149. list_del(&unv->list);
  150. kfree(unv);
  151. }
  152. mutex_unlock(&devices_mutex);
  153. ubi_unregister_volume_notifier(&nvmem_notifier);
  154. }
  155. module_init(ubi_nvmem_init);
  156. module_exit(ubi_nvmem_exit);
  157. MODULE_DESCRIPTION("NVMEM layer over UBI volumes");
  158. MODULE_AUTHOR("Daniel Golle");
  159. MODULE_LICENSE("GPL");