pmem.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /* Copyright(c) 2020 Intel Corporation. */
  3. #include <linux/device.h>
  4. #include <linux/slab.h>
  5. #include <linux/idr.h>
  6. #include <cxlmem.h>
  7. #include <cxl.h>
  8. #include "core.h"
  9. /**
  10. * DOC: cxl pmem
  11. *
  12. * The core CXL PMEM infrastructure supports persistent memory
  13. * provisioning and serves as a bridge to the LIBNVDIMM subsystem. A CXL
  14. * 'bridge' device is added at the root of a CXL device topology if
  15. * platform firmware advertises at least one persistent memory capable
  16. * CXL window. That root-level bridge corresponds to a LIBNVDIMM 'bus'
  17. * device. Then for each cxl_memdev in the CXL device topology a bridge
  18. * device is added to host a LIBNVDIMM dimm object. When these bridges
  19. * are registered native LIBNVDIMM uapis are translated to CXL
  20. * operations, for example, namespace label access commands.
  21. */
  22. static DEFINE_IDA(cxl_nvdimm_bridge_ida);
  23. static void cxl_nvdimm_bridge_release(struct device *dev)
  24. {
  25. struct cxl_nvdimm_bridge *cxl_nvb = to_cxl_nvdimm_bridge(dev);
  26. ida_free(&cxl_nvdimm_bridge_ida, cxl_nvb->id);
  27. kfree(cxl_nvb);
  28. }
  29. static const struct attribute_group *cxl_nvdimm_bridge_attribute_groups[] = {
  30. &cxl_base_attribute_group,
  31. NULL,
  32. };
  33. const struct device_type cxl_nvdimm_bridge_type = {
  34. .name = "cxl_nvdimm_bridge",
  35. .release = cxl_nvdimm_bridge_release,
  36. .groups = cxl_nvdimm_bridge_attribute_groups,
  37. };
  38. struct cxl_nvdimm_bridge *to_cxl_nvdimm_bridge(struct device *dev)
  39. {
  40. if (dev_WARN_ONCE(dev, dev->type != &cxl_nvdimm_bridge_type,
  41. "not a cxl_nvdimm_bridge device\n"))
  42. return NULL;
  43. return container_of(dev, struct cxl_nvdimm_bridge, dev);
  44. }
  45. EXPORT_SYMBOL_NS_GPL(to_cxl_nvdimm_bridge, CXL);
  46. bool is_cxl_nvdimm_bridge(struct device *dev)
  47. {
  48. return dev->type == &cxl_nvdimm_bridge_type;
  49. }
  50. EXPORT_SYMBOL_NS_GPL(is_cxl_nvdimm_bridge, CXL);
  51. static int match_nvdimm_bridge(struct device *dev, void *data)
  52. {
  53. return is_cxl_nvdimm_bridge(dev);
  54. }
  55. /**
  56. * cxl_find_nvdimm_bridge() - find a bridge device relative to a port
  57. * @port: any descendant port of an nvdimm-bridge associated
  58. * root-cxl-port
  59. */
  60. struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct cxl_port *port)
  61. {
  62. struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
  63. struct device *dev;
  64. if (!cxl_root)
  65. return NULL;
  66. dev = device_find_child(&cxl_root->port.dev, NULL, match_nvdimm_bridge);
  67. if (!dev)
  68. return NULL;
  69. return to_cxl_nvdimm_bridge(dev);
  70. }
  71. EXPORT_SYMBOL_NS_GPL(cxl_find_nvdimm_bridge, CXL);
  72. static struct lock_class_key cxl_nvdimm_bridge_key;
  73. static struct cxl_nvdimm_bridge *cxl_nvdimm_bridge_alloc(struct cxl_port *port)
  74. {
  75. struct cxl_nvdimm_bridge *cxl_nvb;
  76. struct device *dev;
  77. int rc;
  78. cxl_nvb = kzalloc(sizeof(*cxl_nvb), GFP_KERNEL);
  79. if (!cxl_nvb)
  80. return ERR_PTR(-ENOMEM);
  81. rc = ida_alloc(&cxl_nvdimm_bridge_ida, GFP_KERNEL);
  82. if (rc < 0)
  83. goto err;
  84. cxl_nvb->id = rc;
  85. dev = &cxl_nvb->dev;
  86. cxl_nvb->port = port;
  87. device_initialize(dev);
  88. lockdep_set_class(&dev->mutex, &cxl_nvdimm_bridge_key);
  89. device_set_pm_not_required(dev);
  90. dev->parent = &port->dev;
  91. dev->bus = &cxl_bus_type;
  92. dev->type = &cxl_nvdimm_bridge_type;
  93. return cxl_nvb;
  94. err:
  95. kfree(cxl_nvb);
  96. return ERR_PTR(rc);
  97. }
  98. static void unregister_nvb(void *_cxl_nvb)
  99. {
  100. struct cxl_nvdimm_bridge *cxl_nvb = _cxl_nvb;
  101. device_unregister(&cxl_nvb->dev);
  102. }
  103. /**
  104. * devm_cxl_add_nvdimm_bridge() - add the root of a LIBNVDIMM topology
  105. * @host: platform firmware root device
  106. * @port: CXL port at the root of a CXL topology
  107. *
  108. * Return: bridge device that can host cxl_nvdimm objects
  109. */
  110. struct cxl_nvdimm_bridge *devm_cxl_add_nvdimm_bridge(struct device *host,
  111. struct cxl_port *port)
  112. {
  113. struct cxl_nvdimm_bridge *cxl_nvb;
  114. struct device *dev;
  115. int rc;
  116. if (!IS_ENABLED(CONFIG_CXL_PMEM))
  117. return ERR_PTR(-ENXIO);
  118. cxl_nvb = cxl_nvdimm_bridge_alloc(port);
  119. if (IS_ERR(cxl_nvb))
  120. return cxl_nvb;
  121. dev = &cxl_nvb->dev;
  122. rc = dev_set_name(dev, "nvdimm-bridge%d", cxl_nvb->id);
  123. if (rc)
  124. goto err;
  125. rc = device_add(dev);
  126. if (rc)
  127. goto err;
  128. rc = devm_add_action_or_reset(host, unregister_nvb, cxl_nvb);
  129. if (rc)
  130. return ERR_PTR(rc);
  131. return cxl_nvb;
  132. err:
  133. put_device(dev);
  134. return ERR_PTR(rc);
  135. }
  136. EXPORT_SYMBOL_NS_GPL(devm_cxl_add_nvdimm_bridge, CXL);
  137. static void cxl_nvdimm_release(struct device *dev)
  138. {
  139. struct cxl_nvdimm *cxl_nvd = to_cxl_nvdimm(dev);
  140. kfree(cxl_nvd);
  141. }
  142. static const struct attribute_group *cxl_nvdimm_attribute_groups[] = {
  143. &cxl_base_attribute_group,
  144. NULL,
  145. };
  146. const struct device_type cxl_nvdimm_type = {
  147. .name = "cxl_nvdimm",
  148. .release = cxl_nvdimm_release,
  149. .groups = cxl_nvdimm_attribute_groups,
  150. };
  151. bool is_cxl_nvdimm(struct device *dev)
  152. {
  153. return dev->type == &cxl_nvdimm_type;
  154. }
  155. EXPORT_SYMBOL_NS_GPL(is_cxl_nvdimm, CXL);
  156. struct cxl_nvdimm *to_cxl_nvdimm(struct device *dev)
  157. {
  158. if (dev_WARN_ONCE(dev, !is_cxl_nvdimm(dev),
  159. "not a cxl_nvdimm device\n"))
  160. return NULL;
  161. return container_of(dev, struct cxl_nvdimm, dev);
  162. }
  163. EXPORT_SYMBOL_NS_GPL(to_cxl_nvdimm, CXL);
  164. static struct lock_class_key cxl_nvdimm_key;
  165. static struct cxl_nvdimm *cxl_nvdimm_alloc(struct cxl_nvdimm_bridge *cxl_nvb,
  166. struct cxl_memdev *cxlmd)
  167. {
  168. struct cxl_nvdimm *cxl_nvd;
  169. struct device *dev;
  170. cxl_nvd = kzalloc(sizeof(*cxl_nvd), GFP_KERNEL);
  171. if (!cxl_nvd)
  172. return ERR_PTR(-ENOMEM);
  173. dev = &cxl_nvd->dev;
  174. cxl_nvd->cxlmd = cxlmd;
  175. cxlmd->cxl_nvd = cxl_nvd;
  176. device_initialize(dev);
  177. lockdep_set_class(&dev->mutex, &cxl_nvdimm_key);
  178. device_set_pm_not_required(dev);
  179. dev->parent = &cxlmd->dev;
  180. dev->bus = &cxl_bus_type;
  181. dev->type = &cxl_nvdimm_type;
  182. /*
  183. * A "%llx" string is 17-bytes vs dimm_id that is max
  184. * NVDIMM_KEY_DESC_LEN
  185. */
  186. BUILD_BUG_ON(sizeof(cxl_nvd->dev_id) < 17 ||
  187. sizeof(cxl_nvd->dev_id) > NVDIMM_KEY_DESC_LEN);
  188. sprintf(cxl_nvd->dev_id, "%llx", cxlmd->cxlds->serial);
  189. return cxl_nvd;
  190. }
  191. static void cxlmd_release_nvdimm(void *_cxlmd)
  192. {
  193. struct cxl_memdev *cxlmd = _cxlmd;
  194. struct cxl_nvdimm *cxl_nvd = cxlmd->cxl_nvd;
  195. struct cxl_nvdimm_bridge *cxl_nvb = cxlmd->cxl_nvb;
  196. cxl_nvd->cxlmd = NULL;
  197. cxlmd->cxl_nvd = NULL;
  198. cxlmd->cxl_nvb = NULL;
  199. device_unregister(&cxl_nvd->dev);
  200. put_device(&cxl_nvb->dev);
  201. }
  202. /**
  203. * devm_cxl_add_nvdimm() - add a bridge between a cxl_memdev and an nvdimm
  204. * @parent_port: parent port for the (to be added) @cxlmd endpoint port
  205. * @cxlmd: cxl_memdev instance that will perform LIBNVDIMM operations
  206. *
  207. * Return: 0 on success negative error code on failure.
  208. */
  209. int devm_cxl_add_nvdimm(struct cxl_port *parent_port,
  210. struct cxl_memdev *cxlmd)
  211. {
  212. struct cxl_nvdimm_bridge *cxl_nvb;
  213. struct cxl_nvdimm *cxl_nvd;
  214. struct device *dev;
  215. int rc;
  216. cxl_nvb = cxl_find_nvdimm_bridge(parent_port);
  217. if (!cxl_nvb)
  218. return -ENODEV;
  219. cxl_nvd = cxl_nvdimm_alloc(cxl_nvb, cxlmd);
  220. if (IS_ERR(cxl_nvd)) {
  221. rc = PTR_ERR(cxl_nvd);
  222. goto err_alloc;
  223. }
  224. cxlmd->cxl_nvb = cxl_nvb;
  225. dev = &cxl_nvd->dev;
  226. rc = dev_set_name(dev, "pmem%d", cxlmd->id);
  227. if (rc)
  228. goto err;
  229. rc = device_add(dev);
  230. if (rc)
  231. goto err;
  232. dev_dbg(&cxlmd->dev, "register %s\n", dev_name(dev));
  233. /* @cxlmd carries a reference on @cxl_nvb until cxlmd_release_nvdimm */
  234. return devm_add_action_or_reset(&cxlmd->dev, cxlmd_release_nvdimm, cxlmd);
  235. err:
  236. put_device(dev);
  237. err_alloc:
  238. cxlmd->cxl_nvb = NULL;
  239. cxlmd->cxl_nvd = NULL;
  240. put_device(&cxl_nvb->dev);
  241. return rc;
  242. }
  243. EXPORT_SYMBOL_NS_GPL(devm_cxl_add_nvdimm, CXL);