106737cd0SDan Williams // SPDX-License-Identifier: GPL-2.0-only
206737cd0SDan Williams /* Copyright(c) 2020 Intel Corporation. */
306737cd0SDan Williams #include <linux/device.h>
406737cd0SDan Williams #include <linux/slab.h>
52e52b625SDan Williams #include <linux/idr.h>
606737cd0SDan Williams #include <cxlmem.h>
706737cd0SDan Williams #include <cxl.h>
806737cd0SDan Williams #include "core.h"
906737cd0SDan Williams
10a01da6caSDan Williams /**
11a01da6caSDan Williams * DOC: cxl pmem
12a01da6caSDan Williams *
13a01da6caSDan Williams * The core CXL PMEM infrastructure supports persistent memory
14a01da6caSDan Williams * provisioning and serves as a bridge to the LIBNVDIMM subsystem. A CXL
15a01da6caSDan Williams * 'bridge' device is added at the root of a CXL device topology if
16a01da6caSDan Williams * platform firmware advertises at least one persistent memory capable
17a01da6caSDan Williams * CXL window. That root-level bridge corresponds to a LIBNVDIMM 'bus'
18a01da6caSDan Williams * device. Then for each cxl_memdev in the CXL device topology a bridge
19a01da6caSDan Williams * device is added to host a LIBNVDIMM dimm object. When these bridges
20a01da6caSDan Williams * are registered native LIBNVDIMM uapis are translated to CXL
21a01da6caSDan Williams * operations, for example, namespace label access commands.
22a01da6caSDan Williams */
23a01da6caSDan Williams
242e52b625SDan Williams static DEFINE_IDA(cxl_nvdimm_bridge_ida);
252e52b625SDan Williams
cxl_nvdimm_bridge_release(struct device * dev)2606737cd0SDan Williams static void cxl_nvdimm_bridge_release(struct device *dev)
2706737cd0SDan Williams {
2806737cd0SDan Williams struct cxl_nvdimm_bridge *cxl_nvb = to_cxl_nvdimm_bridge(dev);
2906737cd0SDan Williams
302e52b625SDan Williams ida_free(&cxl_nvdimm_bridge_ida, cxl_nvb->id);
3106737cd0SDan Williams kfree(cxl_nvb);
3206737cd0SDan Williams }
3306737cd0SDan Williams
3406737cd0SDan Williams static const struct attribute_group *cxl_nvdimm_bridge_attribute_groups[] = {
3506737cd0SDan Williams &cxl_base_attribute_group,
3606737cd0SDan Williams NULL,
3706737cd0SDan Williams };
3806737cd0SDan Williams
3906737cd0SDan Williams const struct device_type cxl_nvdimm_bridge_type = {
4006737cd0SDan Williams .name = "cxl_nvdimm_bridge",
4106737cd0SDan Williams .release = cxl_nvdimm_bridge_release,
4206737cd0SDan Williams .groups = cxl_nvdimm_bridge_attribute_groups,
4306737cd0SDan Williams };
4406737cd0SDan Williams
to_cxl_nvdimm_bridge(struct device * dev)4506737cd0SDan Williams struct cxl_nvdimm_bridge *to_cxl_nvdimm_bridge(struct device *dev)
4606737cd0SDan Williams {
4706737cd0SDan Williams if (dev_WARN_ONCE(dev, dev->type != &cxl_nvdimm_bridge_type,
4806737cd0SDan Williams "not a cxl_nvdimm_bridge device\n"))
4906737cd0SDan Williams return NULL;
5006737cd0SDan Williams return container_of(dev, struct cxl_nvdimm_bridge, dev);
5106737cd0SDan Williams }
52affec782SDan Williams EXPORT_SYMBOL_NS_GPL(to_cxl_nvdimm_bridge, CXL);
5306737cd0SDan Williams
is_cxl_nvdimm_bridge(struct device * dev)5453989fadSDan Williams bool is_cxl_nvdimm_bridge(struct device *dev)
552e52b625SDan Williams {
562e52b625SDan Williams return dev->type == &cxl_nvdimm_bridge_type;
572e52b625SDan Williams }
5853989fadSDan Williams EXPORT_SYMBOL_NS_GPL(is_cxl_nvdimm_bridge, CXL);
5953989fadSDan Williams
match_nvdimm_bridge(struct device * dev,void * data)60a46cfc0fSDan Williams static int match_nvdimm_bridge(struct device *dev, void *data)
6153989fadSDan Williams {
6253989fadSDan Williams return is_cxl_nvdimm_bridge(dev);
6353989fadSDan Williams }
642e52b625SDan Williams
65*84ec9859SLi Ming /**
66*84ec9859SLi Ming * cxl_find_nvdimm_bridge() - find a bridge device relative to a port
67*84ec9859SLi Ming * @port: any descendant port of an nvdimm-bridge associated
68*84ec9859SLi Ming * root-cxl-port
69*84ec9859SLi Ming */
cxl_find_nvdimm_bridge(struct cxl_port * port)70*84ec9859SLi Ming struct cxl_nvdimm_bridge *cxl_find_nvdimm_bridge(struct cxl_port *port)
712e52b625SDan Williams {
72*84ec9859SLi Ming struct cxl_root *cxl_root __free(put_cxl_root) = find_cxl_root(port);
732e52b625SDan Williams struct device *dev;
742e52b625SDan Williams
7544cd71efSDave Jiang if (!cxl_root)
76a46cfc0fSDan Williams return NULL;
77a46cfc0fSDan Williams
7866f11890SDave Jiang dev = device_find_child(&cxl_root->port.dev, NULL, match_nvdimm_bridge);
79a46cfc0fSDan Williams
802e52b625SDan Williams if (!dev)
812e52b625SDan Williams return NULL;
82a46cfc0fSDan Williams
832e52b625SDan Williams return to_cxl_nvdimm_bridge(dev);
842e52b625SDan Williams }
85affec782SDan Williams EXPORT_SYMBOL_NS_GPL(cxl_find_nvdimm_bridge, CXL);
862e52b625SDan Williams
873750d013SDan Williams static struct lock_class_key cxl_nvdimm_bridge_key;
883750d013SDan Williams
cxl_nvdimm_bridge_alloc(struct cxl_port * port)89a46cfc0fSDan Williams static struct cxl_nvdimm_bridge *cxl_nvdimm_bridge_alloc(struct cxl_port *port)
9006737cd0SDan Williams {
9106737cd0SDan Williams struct cxl_nvdimm_bridge *cxl_nvb;
9206737cd0SDan Williams struct device *dev;
932e52b625SDan Williams int rc;
9406737cd0SDan Williams
9506737cd0SDan Williams cxl_nvb = kzalloc(sizeof(*cxl_nvb), GFP_KERNEL);
9606737cd0SDan Williams if (!cxl_nvb)
9706737cd0SDan Williams return ERR_PTR(-ENOMEM);
9806737cd0SDan Williams
992e52b625SDan Williams rc = ida_alloc(&cxl_nvdimm_bridge_ida, GFP_KERNEL);
1002e52b625SDan Williams if (rc < 0)
1012e52b625SDan Williams goto err;
1022e52b625SDan Williams cxl_nvb->id = rc;
1032e52b625SDan Williams
10406737cd0SDan Williams dev = &cxl_nvb->dev;
10506737cd0SDan Williams cxl_nvb->port = port;
10606737cd0SDan Williams device_initialize(dev);
1073750d013SDan Williams lockdep_set_class(&dev->mutex, &cxl_nvdimm_bridge_key);
10806737cd0SDan Williams device_set_pm_not_required(dev);
10906737cd0SDan Williams dev->parent = &port->dev;
11006737cd0SDan Williams dev->bus = &cxl_bus_type;
11106737cd0SDan Williams dev->type = &cxl_nvdimm_bridge_type;
11206737cd0SDan Williams
11306737cd0SDan Williams return cxl_nvb;
1142e52b625SDan Williams
1152e52b625SDan Williams err:
1162e52b625SDan Williams kfree(cxl_nvb);
1172e52b625SDan Williams return ERR_PTR(rc);
11806737cd0SDan Williams }
11906737cd0SDan Williams
unregister_nvb(void * _cxl_nvb)12006737cd0SDan Williams static void unregister_nvb(void *_cxl_nvb)
12106737cd0SDan Williams {
12206737cd0SDan Williams struct cxl_nvdimm_bridge *cxl_nvb = _cxl_nvb;
12306737cd0SDan Williams
12406737cd0SDan Williams device_unregister(&cxl_nvb->dev);
12506737cd0SDan Williams }
12606737cd0SDan Williams
127a01da6caSDan Williams /**
128a01da6caSDan Williams * devm_cxl_add_nvdimm_bridge() - add the root of a LIBNVDIMM topology
129a01da6caSDan Williams * @host: platform firmware root device
130a01da6caSDan Williams * @port: CXL port at the root of a CXL topology
131a01da6caSDan Williams *
132a01da6caSDan Williams * Return: bridge device that can host cxl_nvdimm objects
133a01da6caSDan Williams */
devm_cxl_add_nvdimm_bridge(struct device * host,struct cxl_port * port)13406737cd0SDan Williams struct cxl_nvdimm_bridge *devm_cxl_add_nvdimm_bridge(struct device *host,
13506737cd0SDan Williams struct cxl_port *port)
13606737cd0SDan Williams {
13706737cd0SDan Williams struct cxl_nvdimm_bridge *cxl_nvb;
13806737cd0SDan Williams struct device *dev;
13906737cd0SDan Williams int rc;
14006737cd0SDan Williams
14106737cd0SDan Williams if (!IS_ENABLED(CONFIG_CXL_PMEM))
14206737cd0SDan Williams return ERR_PTR(-ENXIO);
14306737cd0SDan Williams
14406737cd0SDan Williams cxl_nvb = cxl_nvdimm_bridge_alloc(port);
14506737cd0SDan Williams if (IS_ERR(cxl_nvb))
14606737cd0SDan Williams return cxl_nvb;
14706737cd0SDan Williams
14806737cd0SDan Williams dev = &cxl_nvb->dev;
1492e52b625SDan Williams rc = dev_set_name(dev, "nvdimm-bridge%d", cxl_nvb->id);
15006737cd0SDan Williams if (rc)
15106737cd0SDan Williams goto err;
15206737cd0SDan Williams
15306737cd0SDan Williams rc = device_add(dev);
15406737cd0SDan Williams if (rc)
15506737cd0SDan Williams goto err;
15606737cd0SDan Williams
15706737cd0SDan Williams rc = devm_add_action_or_reset(host, unregister_nvb, cxl_nvb);
15806737cd0SDan Williams if (rc)
15906737cd0SDan Williams return ERR_PTR(rc);
16006737cd0SDan Williams
16106737cd0SDan Williams return cxl_nvb;
16206737cd0SDan Williams
16306737cd0SDan Williams err:
16406737cd0SDan Williams put_device(dev);
16506737cd0SDan Williams return ERR_PTR(rc);
16606737cd0SDan Williams }
167affec782SDan Williams EXPORT_SYMBOL_NS_GPL(devm_cxl_add_nvdimm_bridge, CXL);
16806737cd0SDan Williams
cxl_nvdimm_release(struct device * dev)16906737cd0SDan Williams static void cxl_nvdimm_release(struct device *dev)
17006737cd0SDan Williams {
17106737cd0SDan Williams struct cxl_nvdimm *cxl_nvd = to_cxl_nvdimm(dev);
17206737cd0SDan Williams
17306737cd0SDan Williams kfree(cxl_nvd);
17406737cd0SDan Williams }
17506737cd0SDan Williams
17606737cd0SDan Williams static const struct attribute_group *cxl_nvdimm_attribute_groups[] = {
17706737cd0SDan Williams &cxl_base_attribute_group,
17806737cd0SDan Williams NULL,
17906737cd0SDan Williams };
18006737cd0SDan Williams
18106737cd0SDan Williams const struct device_type cxl_nvdimm_type = {
18206737cd0SDan Williams .name = "cxl_nvdimm",
18306737cd0SDan Williams .release = cxl_nvdimm_release,
18406737cd0SDan Williams .groups = cxl_nvdimm_attribute_groups,
18506737cd0SDan Williams };
18606737cd0SDan Williams
is_cxl_nvdimm(struct device * dev)18706737cd0SDan Williams bool is_cxl_nvdimm(struct device *dev)
18806737cd0SDan Williams {
18906737cd0SDan Williams return dev->type == &cxl_nvdimm_type;
19006737cd0SDan Williams }
191affec782SDan Williams EXPORT_SYMBOL_NS_GPL(is_cxl_nvdimm, CXL);
19206737cd0SDan Williams
to_cxl_nvdimm(struct device * dev)19306737cd0SDan Williams struct cxl_nvdimm *to_cxl_nvdimm(struct device *dev)
19406737cd0SDan Williams {
19506737cd0SDan Williams if (dev_WARN_ONCE(dev, !is_cxl_nvdimm(dev),
19606737cd0SDan Williams "not a cxl_nvdimm device\n"))
19706737cd0SDan Williams return NULL;
19806737cd0SDan Williams return container_of(dev, struct cxl_nvdimm, dev);
19906737cd0SDan Williams }
200affec782SDan Williams EXPORT_SYMBOL_NS_GPL(to_cxl_nvdimm, CXL);
20106737cd0SDan Williams
2023750d013SDan Williams static struct lock_class_key cxl_nvdimm_key;
2033750d013SDan Williams
cxl_nvdimm_alloc(struct cxl_nvdimm_bridge * cxl_nvb,struct cxl_memdev * cxlmd)204f17b558dSDan Williams static struct cxl_nvdimm *cxl_nvdimm_alloc(struct cxl_nvdimm_bridge *cxl_nvb,
205f17b558dSDan Williams struct cxl_memdev *cxlmd)
20606737cd0SDan Williams {
20706737cd0SDan Williams struct cxl_nvdimm *cxl_nvd;
20806737cd0SDan Williams struct device *dev;
20906737cd0SDan Williams
21006737cd0SDan Williams cxl_nvd = kzalloc(sizeof(*cxl_nvd), GFP_KERNEL);
21106737cd0SDan Williams if (!cxl_nvd)
21206737cd0SDan Williams return ERR_PTR(-ENOMEM);
21306737cd0SDan Williams
21406737cd0SDan Williams dev = &cxl_nvd->dev;
21506737cd0SDan Williams cxl_nvd->cxlmd = cxlmd;
216f17b558dSDan Williams cxlmd->cxl_nvd = cxl_nvd;
21706737cd0SDan Williams device_initialize(dev);
2183750d013SDan Williams lockdep_set_class(&dev->mutex, &cxl_nvdimm_key);
21906737cd0SDan Williams device_set_pm_not_required(dev);
22006737cd0SDan Williams dev->parent = &cxlmd->dev;
22106737cd0SDan Williams dev->bus = &cxl_bus_type;
22206737cd0SDan Williams dev->type = &cxl_nvdimm_type;
223b5807c80SDave Jiang /*
224b5807c80SDave Jiang * A "%llx" string is 17-bytes vs dimm_id that is max
225b5807c80SDave Jiang * NVDIMM_KEY_DESC_LEN
226b5807c80SDave Jiang */
227b5807c80SDave Jiang BUILD_BUG_ON(sizeof(cxl_nvd->dev_id) < 17 ||
228b5807c80SDave Jiang sizeof(cxl_nvd->dev_id) > NVDIMM_KEY_DESC_LEN);
229b5807c80SDave Jiang sprintf(cxl_nvd->dev_id, "%llx", cxlmd->cxlds->serial);
23006737cd0SDan Williams
23106737cd0SDan Williams return cxl_nvd;
23206737cd0SDan Williams }
23306737cd0SDan Williams
cxlmd_release_nvdimm(void * _cxlmd)234f17b558dSDan Williams static void cxlmd_release_nvdimm(void *_cxlmd)
235f17b558dSDan Williams {
236f17b558dSDan Williams struct cxl_memdev *cxlmd = _cxlmd;
23719398821SDan Williams struct cxl_nvdimm *cxl_nvd = cxlmd->cxl_nvd;
238f17b558dSDan Williams struct cxl_nvdimm_bridge *cxl_nvb = cxlmd->cxl_nvb;
239f17b558dSDan Williams
24019398821SDan Williams cxl_nvd->cxlmd = NULL;
24119398821SDan Williams cxlmd->cxl_nvd = NULL;
24219398821SDan Williams cxlmd->cxl_nvb = NULL;
24319398821SDan Williams device_unregister(&cxl_nvd->dev);
244f17b558dSDan Williams put_device(&cxl_nvb->dev);
24548667f67SDan Williams }
24648667f67SDan Williams
247a01da6caSDan Williams /**
248a01da6caSDan Williams * devm_cxl_add_nvdimm() - add a bridge between a cxl_memdev and an nvdimm
249*84ec9859SLi Ming * @parent_port: parent port for the (to be added) @cxlmd endpoint port
250a01da6caSDan Williams * @cxlmd: cxl_memdev instance that will perform LIBNVDIMM operations
251a01da6caSDan Williams *
252a01da6caSDan Williams * Return: 0 on success negative error code on failure.
253a01da6caSDan Williams */
devm_cxl_add_nvdimm(struct cxl_port * parent_port,struct cxl_memdev * cxlmd)254*84ec9859SLi Ming int devm_cxl_add_nvdimm(struct cxl_port *parent_port,
255*84ec9859SLi Ming struct cxl_memdev *cxlmd)
25606737cd0SDan Williams {
257f17b558dSDan Williams struct cxl_nvdimm_bridge *cxl_nvb;
25806737cd0SDan Williams struct cxl_nvdimm *cxl_nvd;
25906737cd0SDan Williams struct device *dev;
26006737cd0SDan Williams int rc;
26106737cd0SDan Williams
262*84ec9859SLi Ming cxl_nvb = cxl_find_nvdimm_bridge(parent_port);
263f17b558dSDan Williams if (!cxl_nvb)
264f17b558dSDan Williams return -ENODEV;
265f17b558dSDan Williams
266f17b558dSDan Williams cxl_nvd = cxl_nvdimm_alloc(cxl_nvb, cxlmd);
267f17b558dSDan Williams if (IS_ERR(cxl_nvd)) {
268f17b558dSDan Williams rc = PTR_ERR(cxl_nvd);
269f17b558dSDan Williams goto err_alloc;
270f17b558dSDan Williams }
271f17b558dSDan Williams cxlmd->cxl_nvb = cxl_nvb;
27206737cd0SDan Williams
27306737cd0SDan Williams dev = &cxl_nvd->dev;
27406737cd0SDan Williams rc = dev_set_name(dev, "pmem%d", cxlmd->id);
27506737cd0SDan Williams if (rc)
27606737cd0SDan Williams goto err;
27706737cd0SDan Williams
27806737cd0SDan Williams rc = device_add(dev);
27906737cd0SDan Williams if (rc)
28006737cd0SDan Williams goto err;
28106737cd0SDan Williams
282f17b558dSDan Williams dev_dbg(&cxlmd->dev, "register %s\n", dev_name(dev));
28306737cd0SDan Williams
284f17b558dSDan Williams /* @cxlmd carries a reference on @cxl_nvb until cxlmd_release_nvdimm */
285f17b558dSDan Williams return devm_add_action_or_reset(&cxlmd->dev, cxlmd_release_nvdimm, cxlmd);
28606737cd0SDan Williams
28706737cd0SDan Williams err:
28806737cd0SDan Williams put_device(dev);
289f17b558dSDan Williams err_alloc:
290f17b558dSDan Williams cxlmd->cxl_nvb = NULL;
291f17b558dSDan Williams cxlmd->cxl_nvd = NULL;
292f17b558dSDan Williams put_device(&cxl_nvb->dev);
293f17b558dSDan Williams
29406737cd0SDan Williams return rc;
29506737cd0SDan Williams }
296affec782SDan Williams EXPORT_SYMBOL_NS_GPL(devm_cxl_add_nvdimm, CXL);
297