| 1 | /* |
| 2 | * Vhost Vdpa Device |
| 3 | * |
| 4 | * Copyright (c) Huawei Technologies Co., Ltd. 2022. All Rights Reserved. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Longpeng <longpeng2@huawei.com> |
| 8 | * |
| 9 | * Largely based on the "vhost-user-blk-pci.c" and "vhost-user-blk.c" |
| 10 | * implemented by: |
| 11 | * Changpeng Liu <changpeng.liu@intel.com> |
| 12 | * |
| 13 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. |
| 14 | * See the COPYING.LIB file in the top-level directory. |
| 15 | */ |
| 16 | #include "qemu/osdep.h" |
| 17 | #include <sys/ioctl.h> |
| 18 | #include <linux/vhost.h> |
| 19 | #include "qapi/error.h" |
| 20 | #include "qemu/error-report.h" |
| 21 | #include "qemu/cutils.h" |
| 22 | #include "hw/core/qdev.h" |
| 23 | #include "hw/core/qdev-properties.h" |
| 24 | #include "hw/core/qdev-properties-system.h" |
| 25 | #include "hw/virtio/vhost.h" |
| 26 | #include "hw/virtio/virtio.h" |
| 27 | #include "hw/virtio/virtio-bus.h" |
| 28 | #include "hw/virtio/vdpa-dev.h" |
| 29 | #include "system/system.h" |
| 30 | #include "system/runstate.h" |
| 31 | |
| 32 | static void |
| 33 | vhost_vdpa_device_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
| 34 | { |
| 35 | /* Nothing to do */ |
| 36 | } |
| 37 | |
| 38 | static uint32_t |
| 39 | vhost_vdpa_device_get_u32(int fd, unsigned long int cmd, Error **errp) |
| 40 | { |
| 41 | uint32_t val = (uint32_t)-1; |
| 42 | |
| 43 | if (ioctl(fd, cmd, &val) < 0) { |
| 44 | error_setg_errno(errp, errno, "vhost-vdpa-device: cmd 0x%lx failed", |
| 45 | cmd); |
| 46 | } |
| 47 | |
| 48 | return val; |
| 49 | } |
| 50 | |
| 51 | static void vhost_vdpa_device_realize(DeviceState *dev, Error **errp) |
| 52 | { |
| 53 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 54 | VhostVdpaDevice *v = VHOST_VDPA_DEVICE(vdev); |
| 55 | struct vhost_vdpa_iova_range iova_range; |
| 56 | uint16_t max_queue_size; |
| 57 | struct vhost_virtqueue *vqs; |
| 58 | int i, ret; |
| 59 | |
| 60 | if (!v->vhostdev) { |
| 61 | error_setg(errp, "vhost-vdpa-device: vhostdev are missing"); |
| 62 | return; |
| 63 | } |
| 64 | |
| 65 | v->vhostfd = qemu_open(v->vhostdev, O_RDWR, errp); |
| 66 | if (*errp) { |
| 67 | return; |
| 68 | } |
| 69 | |
| 70 | v->vdev_id = vhost_vdpa_device_get_u32(v->vhostfd, |
| 71 | VHOST_VDPA_GET_DEVICE_ID, errp); |
| 72 | if (*errp) { |
| 73 | goto out; |
| 74 | } |
| 75 | |
| 76 | max_queue_size = vhost_vdpa_device_get_u32(v->vhostfd, |
| 77 | VHOST_VDPA_GET_VRING_NUM, errp); |
| 78 | if (*errp) { |
| 79 | goto out; |
| 80 | } |
| 81 | |
| 82 | if (v->queue_size > max_queue_size) { |
| 83 | error_setg(errp, "vhost-vdpa-device: invalid queue_size: %u (max:%u)", |
| 84 | v->queue_size, max_queue_size); |
| 85 | goto out; |
| 86 | } else if (!v->queue_size) { |
| 87 | v->queue_size = max_queue_size; |
| 88 | } |
| 89 | |
| 90 | v->num_queues = vhost_vdpa_device_get_u32(v->vhostfd, |
| 91 | VHOST_VDPA_GET_VQS_COUNT, errp); |
| 92 | if (*errp) { |
| 93 | goto out; |
| 94 | } |
| 95 | |
| 96 | if (!v->num_queues || v->num_queues > VIRTIO_QUEUE_MAX) { |
| 97 | error_setg(errp, "invalid number of virtqueues: %u (max:%u)", |
| 98 | v->num_queues, VIRTIO_QUEUE_MAX); |
| 99 | goto out; |
| 100 | } |
| 101 | |
| 102 | v->dev.nvqs = v->num_queues; |
| 103 | vqs = g_new0(struct vhost_virtqueue, v->dev.nvqs); |
| 104 | v->dev.vqs = vqs; |
| 105 | v->dev.vq_index = 0; |
| 106 | v->dev.vq_index_end = v->dev.nvqs; |
| 107 | v->started = false; |
| 108 | |
| 109 | ret = vhost_vdpa_get_iova_range(v->vhostfd, &iova_range); |
| 110 | if (ret < 0) { |
| 111 | error_setg(errp, "vhost-vdpa-device: get iova range failed: %s", |
| 112 | strerror(-ret)); |
| 113 | goto free_vqs; |
| 114 | } |
| 115 | v->vdpa.shared = g_new0(VhostVDPAShared, 1); |
| 116 | v->vdpa.shared->device_fd = v->vhostfd; |
| 117 | v->vdpa.shared->iova_range = iova_range; |
| 118 | |
| 119 | ret = vhost_dev_init(&v->dev, &v->vdpa, VHOST_BACKEND_TYPE_VDPA, 0, NULL); |
| 120 | if (ret < 0) { |
| 121 | error_setg(errp, "vhost-vdpa-device: vhost initialization failed: %s", |
| 122 | strerror(-ret)); |
| 123 | goto free_vqs; |
| 124 | } |
| 125 | |
| 126 | v->config_size = vhost_vdpa_device_get_u32(v->vhostfd, |
| 127 | VHOST_VDPA_GET_CONFIG_SIZE, |
| 128 | errp); |
| 129 | if (*errp) { |
| 130 | goto vhost_cleanup; |
| 131 | } |
| 132 | |
| 133 | /* |
| 134 | * Invoke .post_init() to initialize the transport-specific fields |
| 135 | * before calling virtio_init(). |
| 136 | */ |
| 137 | if (v->post_init && v->post_init(v, errp) < 0) { |
| 138 | goto vhost_cleanup; |
| 139 | } |
| 140 | |
| 141 | v->config = g_malloc0(v->config_size); |
| 142 | |
| 143 | ret = vhost_dev_get_config(&v->dev, v->config, v->config_size, NULL); |
| 144 | if (ret < 0) { |
| 145 | error_setg(errp, "vhost-vdpa-device: get config failed"); |
| 146 | goto free_config; |
| 147 | } |
| 148 | |
| 149 | virtio_init(vdev, v->vdev_id, v->config_size); |
| 150 | |
| 151 | v->virtqs = g_new0(VirtQueue *, v->dev.nvqs); |
| 152 | for (i = 0; i < v->dev.nvqs; i++) { |
| 153 | v->virtqs[i] = virtio_add_queue(vdev, v->queue_size, |
| 154 | vhost_vdpa_device_dummy_handle_output); |
| 155 | } |
| 156 | |
| 157 | return; |
| 158 | |
| 159 | free_config: |
| 160 | g_free(v->config); |
| 161 | vhost_cleanup: |
| 162 | vhost_dev_cleanup(&v->dev); |
| 163 | free_vqs: |
| 164 | g_free(vqs); |
| 165 | g_free(v->vdpa.shared); |
| 166 | out: |
| 167 | qemu_close(v->vhostfd); |
| 168 | v->vhostfd = -1; |
| 169 | } |
| 170 | |
| 171 | static void vhost_vdpa_device_unrealize(DeviceState *dev) |
| 172 | { |
| 173 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 174 | VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev); |
| 175 | struct vhost_virtqueue *vqs = s->dev.vqs; |
| 176 | int i; |
| 177 | |
| 178 | virtio_set_status(vdev, 0); |
| 179 | |
| 180 | for (i = 0; i < s->num_queues; i++) { |
| 181 | virtio_delete_queue(s->virtqs[i]); |
| 182 | } |
| 183 | g_free(s->virtqs); |
| 184 | virtio_cleanup(vdev); |
| 185 | |
| 186 | g_free(s->config); |
| 187 | vhost_dev_cleanup(&s->dev); |
| 188 | g_free(vqs); |
| 189 | g_free(s->vdpa.shared); |
| 190 | qemu_close(s->vhostfd); |
| 191 | s->vhostfd = -1; |
| 192 | } |
| 193 | |
| 194 | static void |
| 195 | vhost_vdpa_device_get_config(VirtIODevice *vdev, uint8_t *config) |
| 196 | { |
| 197 | VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev); |
| 198 | int ret; |
| 199 | |
| 200 | ret = vhost_dev_get_config(&s->dev, s->config, s->config_size, |
| 201 | NULL); |
| 202 | if (ret < 0) { |
| 203 | error_report("get device config space failed"); |
| 204 | return; |
| 205 | } |
| 206 | memcpy(config, s->config, s->config_size); |
| 207 | } |
| 208 | |
| 209 | static void |
| 210 | vhost_vdpa_device_set_config(VirtIODevice *vdev, const uint8_t *config) |
| 211 | { |
| 212 | VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev); |
| 213 | int ret; |
| 214 | |
| 215 | ret = vhost_dev_set_config(&s->dev, config, 0, s->config_size, |
| 216 | VHOST_SET_CONFIG_TYPE_FRONTEND); |
| 217 | if (ret) { |
| 218 | error_report("set device config space failed"); |
| 219 | return; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | static uint64_t vhost_vdpa_device_get_features(VirtIODevice *vdev, |
| 224 | uint64_t features, |
| 225 | Error **errp) |
| 226 | { |
| 227 | VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev); |
| 228 | uint64_t backend_features = vhost_dev_features(&s->dev); |
| 229 | |
| 230 | if (!virtio_has_feature(features, VIRTIO_F_IOMMU_PLATFORM)) { |
| 231 | virtio_clear_feature(&backend_features, VIRTIO_F_IOMMU_PLATFORM); |
| 232 | } |
| 233 | |
| 234 | return backend_features; |
| 235 | } |
| 236 | |
| 237 | static int vhost_vdpa_device_start(VirtIODevice *vdev, Error **errp) |
| 238 | { |
| 239 | VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev); |
| 240 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
| 241 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
| 242 | int i, ret; |
| 243 | |
| 244 | if (!k->set_guest_notifiers) { |
| 245 | error_setg(errp, "binding does not support guest notifiers"); |
| 246 | return -ENOSYS; |
| 247 | } |
| 248 | |
| 249 | ret = vhost_dev_enable_notifiers(&s->dev, vdev); |
| 250 | if (ret < 0) { |
| 251 | error_setg_errno(errp, -ret, "Error enabling host notifiers"); |
| 252 | return ret; |
| 253 | } |
| 254 | |
| 255 | ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true); |
| 256 | if (ret < 0) { |
| 257 | error_setg_errno(errp, -ret, "Error binding guest notifier"); |
| 258 | goto err_host_notifiers; |
| 259 | } |
| 260 | |
| 261 | s->dev.acked_features = vdev->guest_features; |
| 262 | |
| 263 | ret = vhost_dev_start(&s->dev, vdev, true); |
| 264 | if (ret < 0) { |
| 265 | error_setg_errno(errp, -ret, "Error starting vhost"); |
| 266 | goto err_guest_notifiers; |
| 267 | } |
| 268 | s->started = true; |
| 269 | |
| 270 | /* |
| 271 | * guest_notifier_mask/pending not used yet, so just unmask |
| 272 | * everything here. virtio-pci will do the right thing by |
| 273 | * enabling/disabling irqfd. |
| 274 | */ |
| 275 | for (i = 0; i < s->dev.nvqs; i++) { |
| 276 | vhost_virtqueue_mask(&s->dev, vdev, i, false); |
| 277 | } |
| 278 | |
| 279 | return ret; |
| 280 | |
| 281 | err_guest_notifiers: |
| 282 | k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); |
| 283 | err_host_notifiers: |
| 284 | vhost_dev_disable_notifiers(&s->dev, vdev); |
| 285 | return ret; |
| 286 | } |
| 287 | |
| 288 | static void vhost_vdpa_device_stop(VirtIODevice *vdev) |
| 289 | { |
| 290 | VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev); |
| 291 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
| 292 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
| 293 | int ret; |
| 294 | |
| 295 | if (!s->started) { |
| 296 | return; |
| 297 | } |
| 298 | s->started = false; |
| 299 | |
| 300 | if (!k->set_guest_notifiers) { |
| 301 | return; |
| 302 | } |
| 303 | |
| 304 | vhost_dev_stop(&s->dev, vdev, false); |
| 305 | |
| 306 | ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); |
| 307 | if (ret < 0) { |
| 308 | error_report("vhost guest notifier cleanup failed: %d", ret); |
| 309 | return; |
| 310 | } |
| 311 | |
| 312 | vhost_dev_disable_notifiers(&s->dev, vdev); |
| 313 | } |
| 314 | |
| 315 | static int vhost_vdpa_device_set_status(VirtIODevice *vdev, uint8_t status) |
| 316 | { |
| 317 | VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev); |
| 318 | bool should_start = virtio_device_started(vdev, status); |
| 319 | Error *local_err = NULL; |
| 320 | int ret; |
| 321 | |
| 322 | if (!vdev->vm_running) { |
| 323 | should_start = false; |
| 324 | } |
| 325 | |
| 326 | if (s->started == should_start) { |
| 327 | return 0; |
| 328 | } |
| 329 | |
| 330 | if (should_start) { |
| 331 | ret = vhost_vdpa_device_start(vdev, &local_err); |
| 332 | if (ret < 0) { |
| 333 | error_reportf_err(local_err, "vhost-vdpa-device: start failed: "); |
| 334 | } |
| 335 | } else { |
| 336 | vhost_vdpa_device_stop(vdev); |
| 337 | } |
| 338 | return 0; |
| 339 | } |
| 340 | |
| 341 | static struct vhost_dev *vhost_vdpa_device_get_vhost(VirtIODevice *vdev) |
| 342 | { |
| 343 | VhostVdpaDevice *s = VHOST_VDPA_DEVICE(vdev); |
| 344 | return &s->dev; |
| 345 | } |
| 346 | |
| 347 | static const Property vhost_vdpa_device_properties[] = { |
| 348 | DEFINE_PROP_STRING("vhostdev", VhostVdpaDevice, vhostdev), |
| 349 | DEFINE_PROP_UINT16("queue-size", VhostVdpaDevice, queue_size, 0), |
| 350 | }; |
| 351 | |
| 352 | static const VMStateDescription vmstate_vhost_vdpa_device = { |
| 353 | .name = "vhost-vdpa-device", |
| 354 | .unmigratable = 1, |
| 355 | .minimum_version_id = 1, |
| 356 | .version_id = 1, |
| 357 | .fields = (const VMStateField[]) { |
| 358 | VMSTATE_VIRTIO_DEVICE, |
| 359 | VMSTATE_END_OF_LIST() |
| 360 | }, |
| 361 | }; |
| 362 | |
| 363 | static void vhost_vdpa_device_class_init(ObjectClass *klass, const void *data) |
| 364 | { |
| 365 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 366 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); |
| 367 | |
| 368 | device_class_set_props(dc, vhost_vdpa_device_properties); |
| 369 | dc->desc = "VDPA-based generic device assignment"; |
| 370 | dc->vmsd = &vmstate_vhost_vdpa_device; |
| 371 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
| 372 | vdc->realize = vhost_vdpa_device_realize; |
| 373 | vdc->unrealize = vhost_vdpa_device_unrealize; |
| 374 | vdc->get_config = vhost_vdpa_device_get_config; |
| 375 | vdc->set_config = vhost_vdpa_device_set_config; |
| 376 | vdc->get_features = vhost_vdpa_device_get_features; |
| 377 | vdc->set_status = vhost_vdpa_device_set_status; |
| 378 | vdc->get_vhost = vhost_vdpa_device_get_vhost; |
| 379 | } |
| 380 | |
| 381 | static void vhost_vdpa_device_instance_init(Object *obj) |
| 382 | { |
| 383 | VhostVdpaDevice *s = VHOST_VDPA_DEVICE(obj); |
| 384 | |
| 385 | device_add_bootindex_property(obj, &s->bootindex, "bootindex", |
| 386 | NULL, DEVICE(obj)); |
| 387 | } |
| 388 | |
| 389 | static const TypeInfo vhost_vdpa_device_info = { |
| 390 | .name = TYPE_VHOST_VDPA_DEVICE, |
| 391 | .parent = TYPE_VIRTIO_DEVICE, |
| 392 | .instance_size = sizeof(VhostVdpaDevice), |
| 393 | .class_init = vhost_vdpa_device_class_init, |
| 394 | .instance_init = vhost_vdpa_device_instance_init, |
| 395 | }; |
| 396 | |
| 397 | static void register_vhost_vdpa_device_type(void) |
| 398 | { |
| 399 | type_register_static(&vhost_vdpa_device_info); |
| 400 | } |
| 401 | |
| 402 | type_init(register_vhost_vdpa_device_type); |