xref: /openbmc/linux/drivers/nvdimm/virtio_pmem.c (revision 545094d9)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * virtio_pmem.c: Virtio pmem Driver
4  *
5  * Discovers persistent memory range information
6  * from host and registers the virtual pmem device
7  * with libnvdimm core.
8  */
9 #include "virtio_pmem.h"
10 #include "nd.h"
11 
12 static struct virtio_device_id id_table[] = {
13 	{ VIRTIO_ID_PMEM, VIRTIO_DEV_ANY_ID },
14 	{ 0 },
15 };
16 
17  /* Initialize virt queue */
18 static int init_vq(struct virtio_pmem *vpmem)
19 {
20 	/* single vq */
21 	vpmem->req_vq = virtio_find_single_vq(vpmem->vdev,
22 					virtio_pmem_host_ack, "flush_queue");
23 	if (IS_ERR(vpmem->req_vq))
24 		return PTR_ERR(vpmem->req_vq);
25 
26 	spin_lock_init(&vpmem->pmem_lock);
27 	INIT_LIST_HEAD(&vpmem->req_list);
28 
29 	return 0;
30 };
31 
32 static int virtio_pmem_probe(struct virtio_device *vdev)
33 {
34 	struct nd_region_desc ndr_desc = {};
35 	int nid = dev_to_node(&vdev->dev);
36 	struct nd_region *nd_region;
37 	struct virtio_pmem *vpmem;
38 	struct resource res;
39 	int err = 0;
40 
41 	if (!vdev->config->get) {
42 		dev_err(&vdev->dev, "%s failure: config access disabled\n",
43 			__func__);
44 		return -EINVAL;
45 	}
46 
47 	vpmem = devm_kzalloc(&vdev->dev, sizeof(*vpmem), GFP_KERNEL);
48 	if (!vpmem) {
49 		err = -ENOMEM;
50 		goto out_err;
51 	}
52 
53 	vpmem->vdev = vdev;
54 	vdev->priv = vpmem;
55 	err = init_vq(vpmem);
56 	if (err) {
57 		dev_err(&vdev->dev, "failed to initialize virtio pmem vq's\n");
58 		goto out_err;
59 	}
60 
61 	virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
62 			start, &vpmem->start);
63 	virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
64 			size, &vpmem->size);
65 
66 	res.start = vpmem->start;
67 	res.end   = vpmem->start + vpmem->size - 1;
68 	vpmem->nd_desc.provider_name = "virtio-pmem";
69 	vpmem->nd_desc.module = THIS_MODULE;
70 
71 	vpmem->nvdimm_bus = nvdimm_bus_register(&vdev->dev,
72 						&vpmem->nd_desc);
73 	if (!vpmem->nvdimm_bus) {
74 		dev_err(&vdev->dev, "failed to register device with nvdimm_bus\n");
75 		err = -ENXIO;
76 		goto out_vq;
77 	}
78 
79 	dev_set_drvdata(&vdev->dev, vpmem->nvdimm_bus);
80 
81 	ndr_desc.res = &res;
82 	ndr_desc.numa_node = nid;
83 	ndr_desc.flush = async_pmem_flush;
84 	ndr_desc.provider_data = vdev;
85 	set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
86 	set_bit(ND_REGION_ASYNC, &ndr_desc.flags);
87 	/*
88 	 * The NVDIMM region could be available before the
89 	 * virtio_device_ready() that is called by
90 	 * virtio_dev_probe(), so we set device ready here.
91 	 */
92 	virtio_device_ready(vdev);
93 	nd_region = nvdimm_pmem_region_create(vpmem->nvdimm_bus, &ndr_desc);
94 	if (!nd_region) {
95 		dev_err(&vdev->dev, "failed to create nvdimm region\n");
96 		err = -ENXIO;
97 		goto out_nd;
98 	}
99 	return 0;
100 out_nd:
101 	virtio_reset_device(vdev);
102 	nvdimm_bus_unregister(vpmem->nvdimm_bus);
103 out_vq:
104 	vdev->config->del_vqs(vdev);
105 out_err:
106 	return err;
107 }
108 
109 static void virtio_pmem_remove(struct virtio_device *vdev)
110 {
111 	struct nvdimm_bus *nvdimm_bus = dev_get_drvdata(&vdev->dev);
112 
113 	nvdimm_bus_unregister(nvdimm_bus);
114 	vdev->config->del_vqs(vdev);
115 	virtio_reset_device(vdev);
116 }
117 
118 static struct virtio_driver virtio_pmem_driver = {
119 	.driver.name		= KBUILD_MODNAME,
120 	.driver.owner		= THIS_MODULE,
121 	.id_table		= id_table,
122 	.probe			= virtio_pmem_probe,
123 	.remove			= virtio_pmem_remove,
124 };
125 
126 module_virtio_driver(virtio_pmem_driver);
127 MODULE_DEVICE_TABLE(virtio, id_table);
128 MODULE_DESCRIPTION("Virtio pmem driver");
129 MODULE_LICENSE("GPL");
130