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