1 // SPDX-License-Identifier: GPL-2.0
3 * virtio_pmem.c: Virtio pmem Driver
5 * Discovers persistent memory range information
6 * from host and registers the virtual pmem device
9 #include "virtio_pmem.h"
12 static struct virtio_device_id id_table[] = {
13 { VIRTIO_ID_PMEM, VIRTIO_DEV_ANY_ID },
17 /* Initialize virt queue */
18 static int init_vq(struct virtio_pmem *vpmem)
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);
26 spin_lock_init(&vpmem->pmem_lock);
27 INIT_LIST_HEAD(&vpmem->req_list);
32 static int virtio_pmem_probe(struct virtio_device *vdev)
34 struct nd_region_desc ndr_desc = {};
35 struct nd_region *nd_region;
36 struct virtio_pmem *vpmem;
40 if (!vdev->config->get) {
41 dev_err(&vdev->dev, "%s failure: config access disabled\n",
46 vpmem = devm_kzalloc(&vdev->dev, sizeof(*vpmem), GFP_KERNEL);
56 dev_err(&vdev->dev, "failed to initialize virtio pmem vq's\n");
60 virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
61 start, &vpmem->start);
62 virtio_cread_le(vpmem->vdev, struct virtio_pmem_config,
65 res.start = vpmem->start;
66 res.end = vpmem->start + vpmem->size - 1;
67 vpmem->nd_desc.provider_name = "virtio-pmem";
68 vpmem->nd_desc.module = THIS_MODULE;
70 vpmem->nvdimm_bus = nvdimm_bus_register(&vdev->dev,
72 if (!vpmem->nvdimm_bus) {
73 dev_err(&vdev->dev, "failed to register device with nvdimm_bus\n");
78 dev_set_drvdata(&vdev->dev, vpmem->nvdimm_bus);
82 ndr_desc.numa_node = memory_add_physaddr_to_nid(res.start);
83 ndr_desc.target_node = phys_to_target_node(res.start);
84 if (ndr_desc.target_node == NUMA_NO_NODE) {
85 ndr_desc.target_node = ndr_desc.numa_node;
86 dev_dbg(&vdev->dev, "changing target node from %d to %d",
87 NUMA_NO_NODE, ndr_desc.target_node);
90 ndr_desc.flush = async_pmem_flush;
91 ndr_desc.provider_data = vdev;
92 set_bit(ND_REGION_PAGEMAP, &ndr_desc.flags);
93 set_bit(ND_REGION_ASYNC, &ndr_desc.flags);
95 * The NVDIMM region could be available before the
96 * virtio_device_ready() that is called by
97 * virtio_dev_probe(), so we set device ready here.
99 virtio_device_ready(vdev);
100 nd_region = nvdimm_pmem_region_create(vpmem->nvdimm_bus, &ndr_desc);
102 dev_err(&vdev->dev, "failed to create nvdimm region\n");
108 virtio_reset_device(vdev);
109 nvdimm_bus_unregister(vpmem->nvdimm_bus);
111 vdev->config->del_vqs(vdev);
116 static void virtio_pmem_remove(struct virtio_device *vdev)
118 struct nvdimm_bus *nvdimm_bus = dev_get_drvdata(&vdev->dev);
120 nvdimm_bus_unregister(nvdimm_bus);
121 vdev->config->del_vqs(vdev);
122 virtio_reset_device(vdev);
125 static struct virtio_driver virtio_pmem_driver = {
126 .driver.name = KBUILD_MODNAME,
127 .driver.owner = THIS_MODULE,
128 .id_table = id_table,
129 .probe = virtio_pmem_probe,
130 .remove = virtio_pmem_remove,
133 module_virtio_driver(virtio_pmem_driver);
134 MODULE_DEVICE_TABLE(virtio, id_table);
135 MODULE_DESCRIPTION("Virtio pmem driver");
136 MODULE_LICENSE("GPL");