819-v6.8-0006-nvmem-core-Expose-cells-through-sysfs.patch 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  1. From 0331c611949fffdf486652450901a4dc52bc5cca Mon Sep 17 00:00:00 2001
  2. From: Miquel Raynal <[email protected]>
  3. Date: Fri, 15 Dec 2023 11:15:34 +0000
  4. Subject: [PATCH] nvmem: core: Expose cells through sysfs
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. The binary content of nvmem devices is available to the user so in the
  9. easiest cases, finding the content of a cell is rather easy as it is
  10. just a matter of looking at a known and fixed offset. However, nvmem
  11. layouts have been recently introduced to cope with more advanced
  12. situations, where the offset and size of the cells is not known in
  13. advance or is dynamic. When using layouts, more advanced parsers are
  14. used by the kernel in order to give direct access to the content of each
  15. cell, regardless of its position/size in the underlying
  16. device. Unfortunately, these information are not accessible by users,
  17. unless by fully re-implementing the parser logic in userland.
  18. Let's expose the cells and their content through sysfs to avoid these
  19. situations. Of course the relevant NVMEM sysfs Kconfig option must be
  20. enabled for this support to be available.
  21. Not all nvmem devices expose cells. Indeed, the .bin_attrs attribute
  22. group member will be filled at runtime only when relevant and will
  23. remain empty otherwise. In this case, as the cells attribute group will
  24. be empty, it will not lead to any additional folder/file creation.
  25. Exposed cells are read-only. There is, in practice, everything in the
  26. core to support a write path, but as I don't see any need for that, I
  27. prefer to keep the interface simple (and probably safer). The interface
  28. is documented as being in the "testing" state which means we can later
  29. add a write attribute if though relevant.
  30. Signed-off-by: Miquel Raynal <[email protected]>
  31. Tested-by: Rafał Miłecki <[email protected]>
  32. Tested-by: Chen-Yu Tsai <[email protected]>
  33. Signed-off-by: Srinivas Kandagatla <[email protected]>
  34. Link: https://lore.kernel.org/r/[email protected]
  35. Signed-off-by: Greg Kroah-Hartman <[email protected]>
  36. ---
  37. drivers/nvmem/core.c | 135 +++++++++++++++++++++++++++++++++++++-
  38. drivers/nvmem/internals.h | 1 +
  39. 2 files changed, 135 insertions(+), 1 deletion(-)
  40. --- a/drivers/nvmem/core.c
  41. +++ b/drivers/nvmem/core.c
  42. @@ -299,6 +299,43 @@ static umode_t nvmem_bin_attr_is_visible
  43. return nvmem_bin_attr_get_umode(nvmem);
  44. }
  45. +static struct nvmem_cell *nvmem_create_cell(struct nvmem_cell_entry *entry,
  46. + const char *id, int index);
  47. +
  48. +static ssize_t nvmem_cell_attr_read(struct file *filp, struct kobject *kobj,
  49. + struct bin_attribute *attr, char *buf,
  50. + loff_t pos, size_t count)
  51. +{
  52. + struct nvmem_cell_entry *entry;
  53. + struct nvmem_cell *cell = NULL;
  54. + size_t cell_sz, read_len;
  55. + void *content;
  56. +
  57. + entry = attr->private;
  58. + cell = nvmem_create_cell(entry, entry->name, 0);
  59. + if (IS_ERR(cell))
  60. + return PTR_ERR(cell);
  61. +
  62. + if (!cell)
  63. + return -EINVAL;
  64. +
  65. + content = nvmem_cell_read(cell, &cell_sz);
  66. + if (IS_ERR(content)) {
  67. + read_len = PTR_ERR(content);
  68. + goto destroy_cell;
  69. + }
  70. +
  71. + read_len = min_t(unsigned int, cell_sz - pos, count);
  72. + memcpy(buf, content + pos, read_len);
  73. + kfree(content);
  74. +
  75. +destroy_cell:
  76. + kfree_const(cell->id);
  77. + kfree(cell);
  78. +
  79. + return read_len;
  80. +}
  81. +
  82. /* default read/write permissions */
  83. static struct bin_attribute bin_attr_rw_nvmem = {
  84. .attr = {
  85. @@ -320,11 +357,21 @@ static const struct attribute_group nvme
  86. .is_bin_visible = nvmem_bin_attr_is_visible,
  87. };
  88. +/* Cell attributes will be dynamically allocated */
  89. +static struct attribute_group nvmem_cells_group = {
  90. + .name = "cells",
  91. +};
  92. +
  93. static const struct attribute_group *nvmem_dev_groups[] = {
  94. &nvmem_bin_group,
  95. NULL,
  96. };
  97. +static const struct attribute_group *nvmem_cells_groups[] = {
  98. + &nvmem_cells_group,
  99. + NULL,
  100. +};
  101. +
  102. static struct bin_attribute bin_attr_nvmem_eeprom_compat = {
  103. .attr = {
  104. .name = "eeprom",
  105. @@ -380,6 +427,68 @@ static void nvmem_sysfs_remove_compat(st
  106. device_remove_bin_file(nvmem->base_dev, &nvmem->eeprom);
  107. }
  108. +static int nvmem_populate_sysfs_cells(struct nvmem_device *nvmem)
  109. +{
  110. + struct bin_attribute **cells_attrs, *attrs;
  111. + struct nvmem_cell_entry *entry;
  112. + unsigned int ncells = 0, i = 0;
  113. + int ret = 0;
  114. +
  115. + mutex_lock(&nvmem_mutex);
  116. +
  117. + if (list_empty(&nvmem->cells) || nvmem->sysfs_cells_populated) {
  118. + nvmem_cells_group.bin_attrs = NULL;
  119. + goto unlock_mutex;
  120. + }
  121. +
  122. + /* Allocate an array of attributes with a sentinel */
  123. + ncells = list_count_nodes(&nvmem->cells);
  124. + cells_attrs = devm_kcalloc(&nvmem->dev, ncells + 1,
  125. + sizeof(struct bin_attribute *), GFP_KERNEL);
  126. + if (!cells_attrs) {
  127. + ret = -ENOMEM;
  128. + goto unlock_mutex;
  129. + }
  130. +
  131. + attrs = devm_kcalloc(&nvmem->dev, ncells, sizeof(struct bin_attribute), GFP_KERNEL);
  132. + if (!attrs) {
  133. + ret = -ENOMEM;
  134. + goto unlock_mutex;
  135. + }
  136. +
  137. + /* Initialize each attribute to take the name and size of the cell */
  138. + list_for_each_entry(entry, &nvmem->cells, node) {
  139. + sysfs_bin_attr_init(&attrs[i]);
  140. + attrs[i].attr.name = devm_kasprintf(&nvmem->dev, GFP_KERNEL,
  141. + "%s@%x", entry->name,
  142. + entry->offset);
  143. + attrs[i].attr.mode = 0444;
  144. + attrs[i].size = entry->bytes;
  145. + attrs[i].read = &nvmem_cell_attr_read;
  146. + attrs[i].private = entry;
  147. + if (!attrs[i].attr.name) {
  148. + ret = -ENOMEM;
  149. + goto unlock_mutex;
  150. + }
  151. +
  152. + cells_attrs[i] = &attrs[i];
  153. + i++;
  154. + }
  155. +
  156. + nvmem_cells_group.bin_attrs = cells_attrs;
  157. +
  158. + ret = devm_device_add_groups(&nvmem->dev, nvmem_cells_groups);
  159. + if (ret)
  160. + goto unlock_mutex;
  161. +
  162. + nvmem->sysfs_cells_populated = true;
  163. +
  164. +unlock_mutex:
  165. + mutex_unlock(&nvmem_mutex);
  166. +
  167. + return ret;
  168. +}
  169. +
  170. #else /* CONFIG_NVMEM_SYSFS */
  171. static int nvmem_sysfs_setup_compat(struct nvmem_device *nvmem,
  172. @@ -739,11 +848,25 @@ static int nvmem_add_cells_from_fixed_la
  173. int nvmem_layout_register(struct nvmem_layout *layout)
  174. {
  175. + int ret;
  176. +
  177. if (!layout->add_cells)
  178. return -EINVAL;
  179. /* Populate the cells */
  180. - return layout->add_cells(&layout->nvmem->dev, layout->nvmem);
  181. + ret = layout->add_cells(&layout->nvmem->dev, layout->nvmem);
  182. + if (ret)
  183. + return ret;
  184. +
  185. +#ifdef CONFIG_NVMEM_SYSFS
  186. + ret = nvmem_populate_sysfs_cells(layout->nvmem);
  187. + if (ret) {
  188. + nvmem_device_remove_all_cells(layout->nvmem);
  189. + return ret;
  190. + }
  191. +#endif
  192. +
  193. + return 0;
  194. }
  195. EXPORT_SYMBOL_GPL(nvmem_layout_register);
  196. @@ -902,10 +1025,20 @@ struct nvmem_device *nvmem_register(cons
  197. if (rval)
  198. goto err_remove_dev;
  199. +#ifdef CONFIG_NVMEM_SYSFS
  200. + rval = nvmem_populate_sysfs_cells(nvmem);
  201. + if (rval)
  202. + goto err_destroy_layout;
  203. +#endif
  204. +
  205. blocking_notifier_call_chain(&nvmem_notifier, NVMEM_ADD, nvmem);
  206. return nvmem;
  207. +#ifdef CONFIG_NVMEM_SYSFS
  208. +err_destroy_layout:
  209. + nvmem_destroy_layout(nvmem);
  210. +#endif
  211. err_remove_dev:
  212. device_del(&nvmem->dev);
  213. err_remove_cells:
  214. --- a/drivers/nvmem/internals.h
  215. +++ b/drivers/nvmem/internals.h
  216. @@ -32,6 +32,7 @@ struct nvmem_device {
  217. struct gpio_desc *wp_gpio;
  218. struct nvmem_layout *layout;
  219. void *priv;
  220. + bool sysfs_cells_populated;
  221. };
  222. #if IS_ENABLED(CONFIG_OF)