1 /* 2 * Virtio PMEM device 3 * 4 * Copyright (C) 2018-2019 Red Hat, Inc. 5 * 6 * Authors: 7 * Pankaj Gupta <pagupta@redhat.com> 8 * David Hildenbrand <david@redhat.com> 9 * 10 * This work is licensed under the terms of the GNU GPL, version 2. 11 * See the COPYING file in the top-level directory. 12 */ 13 14 #include "qemu/osdep.h" 15 #include "qapi/error.h" 16 #include "qemu/error-report.h" 17 #include "qemu/main-loop.h" 18 #include "hw/virtio/virtio-pmem.h" 19 #include "hw/qdev-properties.h" 20 #include "hw/virtio/virtio-access.h" 21 #include "standard-headers/linux/virtio_ids.h" 22 #include "standard-headers/linux/virtio_pmem.h" 23 #include "sysemu/hostmem.h" 24 #include "block/aio.h" 25 #include "block/thread-pool.h" 26 #include "trace.h" 27 28 typedef struct VirtIODeviceRequest { 29 VirtQueueElement elem; 30 int fd; 31 VirtIOPMEM *pmem; 32 VirtIODevice *vdev; 33 struct virtio_pmem_req req; 34 struct virtio_pmem_resp resp; 35 } VirtIODeviceRequest; 36 37 static int worker_cb(void *opaque) 38 { 39 VirtIODeviceRequest *req_data = opaque; 40 int err = 0; 41 42 /* flush raw backing image */ 43 err = fsync(req_data->fd); 44 trace_virtio_pmem_flush_done(err); 45 if (err != 0) { 46 err = 1; 47 } 48 49 virtio_stl_p(req_data->vdev, &req_data->resp.ret, err); 50 51 return 0; 52 } 53 54 static void done_cb(void *opaque, int ret) 55 { 56 VirtIODeviceRequest *req_data = opaque; 57 int len = iov_from_buf(req_data->elem.in_sg, req_data->elem.in_num, 0, 58 &req_data->resp, sizeof(struct virtio_pmem_resp)); 59 60 /* Callbacks are serialized, so no need to use atomic ops. */ 61 virtqueue_push(req_data->pmem->rq_vq, &req_data->elem, len); 62 virtio_notify((VirtIODevice *)req_data->pmem, req_data->pmem->rq_vq); 63 trace_virtio_pmem_response(); 64 g_free(req_data); 65 } 66 67 static void virtio_pmem_flush(VirtIODevice *vdev, VirtQueue *vq) 68 { 69 VirtIODeviceRequest *req_data; 70 VirtIOPMEM *pmem = VIRTIO_PMEM(vdev); 71 HostMemoryBackend *backend = MEMORY_BACKEND(pmem->memdev); 72 ThreadPool *pool = aio_get_thread_pool(qemu_get_aio_context()); 73 74 trace_virtio_pmem_flush_request(); 75 req_data = virtqueue_pop(vq, sizeof(VirtIODeviceRequest)); 76 if (!req_data) { 77 virtio_error(vdev, "virtio-pmem missing request data"); 78 return; 79 } 80 81 if (req_data->elem.out_num < 1 || req_data->elem.in_num < 1) { 82 virtio_error(vdev, "virtio-pmem request not proper"); 83 virtqueue_detach_element(vq, (VirtQueueElement *)req_data, 0); 84 g_free(req_data); 85 return; 86 } 87 req_data->fd = memory_region_get_fd(&backend->mr); 88 req_data->pmem = pmem; 89 req_data->vdev = vdev; 90 thread_pool_submit_aio(pool, worker_cb, req_data, done_cb, req_data); 91 } 92 93 static void virtio_pmem_get_config(VirtIODevice *vdev, uint8_t *config) 94 { 95 VirtIOPMEM *pmem = VIRTIO_PMEM(vdev); 96 struct virtio_pmem_config *pmemcfg = (struct virtio_pmem_config *) config; 97 98 virtio_stq_p(vdev, &pmemcfg->start, pmem->start); 99 virtio_stq_p(vdev, &pmemcfg->size, memory_region_size(&pmem->memdev->mr)); 100 } 101 102 static uint64_t virtio_pmem_get_features(VirtIODevice *vdev, uint64_t features, 103 Error **errp) 104 { 105 return features; 106 } 107 108 static void virtio_pmem_realize(DeviceState *dev, Error **errp) 109 { 110 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 111 VirtIOPMEM *pmem = VIRTIO_PMEM(dev); 112 113 if (!pmem->memdev) { 114 error_setg(errp, "virtio-pmem memdev not set"); 115 return; 116 } 117 118 if (host_memory_backend_is_mapped(pmem->memdev)) { 119 error_setg(errp, "can't use already busy memdev: %s", 120 object_get_canonical_path_component(OBJECT(pmem->memdev))); 121 return; 122 } 123 124 host_memory_backend_set_mapped(pmem->memdev, true); 125 virtio_init(vdev, VIRTIO_ID_PMEM, sizeof(struct virtio_pmem_config)); 126 pmem->rq_vq = virtio_add_queue(vdev, 128, virtio_pmem_flush); 127 } 128 129 static void virtio_pmem_unrealize(DeviceState *dev) 130 { 131 VirtIODevice *vdev = VIRTIO_DEVICE(dev); 132 VirtIOPMEM *pmem = VIRTIO_PMEM(dev); 133 134 host_memory_backend_set_mapped(pmem->memdev, false); 135 virtio_delete_queue(pmem->rq_vq); 136 virtio_cleanup(vdev); 137 } 138 139 static void virtio_pmem_fill_device_info(const VirtIOPMEM *pmem, 140 VirtioPMEMDeviceInfo *vi) 141 { 142 vi->memaddr = pmem->start; 143 vi->size = memory_region_size(&pmem->memdev->mr); 144 vi->memdev = object_get_canonical_path(OBJECT(pmem->memdev)); 145 } 146 147 static MemoryRegion *virtio_pmem_get_memory_region(VirtIOPMEM *pmem, 148 Error **errp) 149 { 150 if (!pmem->memdev) { 151 error_setg(errp, "'%s' property must be set", VIRTIO_PMEM_MEMDEV_PROP); 152 return NULL; 153 } 154 155 return &pmem->memdev->mr; 156 } 157 158 static Property virtio_pmem_properties[] = { 159 DEFINE_PROP_UINT64(VIRTIO_PMEM_ADDR_PROP, VirtIOPMEM, start, 0), 160 DEFINE_PROP_LINK(VIRTIO_PMEM_MEMDEV_PROP, VirtIOPMEM, memdev, 161 TYPE_MEMORY_BACKEND, HostMemoryBackend *), 162 DEFINE_PROP_END_OF_LIST(), 163 }; 164 165 static void virtio_pmem_class_init(ObjectClass *klass, void *data) 166 { 167 DeviceClass *dc = DEVICE_CLASS(klass); 168 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); 169 VirtIOPMEMClass *vpc = VIRTIO_PMEM_CLASS(klass); 170 171 device_class_set_props(dc, virtio_pmem_properties); 172 173 vdc->realize = virtio_pmem_realize; 174 vdc->unrealize = virtio_pmem_unrealize; 175 vdc->get_config = virtio_pmem_get_config; 176 vdc->get_features = virtio_pmem_get_features; 177 178 vpc->fill_device_info = virtio_pmem_fill_device_info; 179 vpc->get_memory_region = virtio_pmem_get_memory_region; 180 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 181 } 182 183 static const TypeInfo virtio_pmem_info = { 184 .name = TYPE_VIRTIO_PMEM, 185 .parent = TYPE_VIRTIO_DEVICE, 186 .class_size = sizeof(VirtIOPMEMClass), 187 .class_init = virtio_pmem_class_init, 188 .instance_size = sizeof(VirtIOPMEM), 189 }; 190 191 static void virtio_register_types(void) 192 { 193 type_register_static(&virtio_pmem_info); 194 } 195 196 type_init(virtio_register_types) 197