| 1 | /* |
| 2 | * vhost-user-blk host device |
| 3 | * |
| 4 | * Copyright(C) 2017 Intel Corporation. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Changpeng Liu <changpeng.liu@intel.com> |
| 8 | * |
| 9 | * Largely based on the "vhost-user-scsi.c" and "vhost-scsi.c" implemented by: |
| 10 | * Felipe Franciosi <felipe@nutanix.com> |
| 11 | * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> |
| 12 | * Nicholas Bellinger <nab@risingtidesystems.com> |
| 13 | * |
| 14 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. |
| 15 | * See the COPYING.LIB file in the top-level directory. |
| 16 | * |
| 17 | */ |
| 18 | |
| 19 | #include "qemu/osdep.h" |
| 20 | #include "qapi/error.h" |
| 21 | #include "qemu/error-report.h" |
| 22 | #include "qemu/cutils.h" |
| 23 | #include "hw/core/qdev.h" |
| 24 | #include "hw/core/qdev-properties.h" |
| 25 | #include "hw/core/qdev-properties-system.h" |
| 26 | #include "hw/virtio/virtio-blk-common.h" |
| 27 | #include "hw/virtio/vhost.h" |
| 28 | #include "hw/virtio/vhost-user-blk.h" |
| 29 | #include "hw/virtio/virtio.h" |
| 30 | #include "hw/virtio/virtio-bus.h" |
| 31 | #include "hw/virtio/virtio-access.h" |
| 32 | #include "system/system.h" |
| 33 | #include "system/runstate.h" |
| 34 | #include "trace.h" |
| 35 | |
| 36 | static const int user_feature_bits[] = { |
| 37 | VIRTIO_BLK_F_SIZE_MAX, |
| 38 | VIRTIO_BLK_F_SEG_MAX, |
| 39 | VIRTIO_BLK_F_GEOMETRY, |
| 40 | VIRTIO_BLK_F_BLK_SIZE, |
| 41 | VIRTIO_BLK_F_TOPOLOGY, |
| 42 | VIRTIO_BLK_F_MQ, |
| 43 | VIRTIO_BLK_F_RO, |
| 44 | VIRTIO_BLK_F_FLUSH, |
| 45 | VIRTIO_BLK_F_CONFIG_WCE, |
| 46 | VIRTIO_BLK_F_DISCARD, |
| 47 | VIRTIO_BLK_F_WRITE_ZEROES, |
| 48 | VIRTIO_F_VERSION_1, |
| 49 | VIRTIO_RING_F_INDIRECT_DESC, |
| 50 | VIRTIO_RING_F_EVENT_IDX, |
| 51 | VIRTIO_F_NOTIFY_ON_EMPTY, |
| 52 | VIRTIO_F_RING_PACKED, |
| 53 | VIRTIO_F_IOMMU_PLATFORM, |
| 54 | VIRTIO_F_RING_RESET, |
| 55 | VIRTIO_F_IN_ORDER, |
| 56 | VIRTIO_F_NOTIFICATION_DATA, |
| 57 | VHOST_INVALID_FEATURE_BIT |
| 58 | }; |
| 59 | |
| 60 | static void vhost_user_blk_event(void *opaque, QEMUChrEvent event); |
| 61 | |
| 62 | static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config) |
| 63 | { |
| 64 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 65 | |
| 66 | /* Our num_queues overrides the device backend */ |
| 67 | virtio_stw_p(vdev, &s->blkcfg.num_queues, s->num_queues); |
| 68 | |
| 69 | if (s->seg_max_adjust) { |
| 70 | uint32_t seg_max = MIN(s->blkcfg.seg_max, s->queue_size - 2); |
| 71 | |
| 72 | virtio_stl_p(vdev, &s->blkcfg.seg_max, seg_max); |
| 73 | } |
| 74 | |
| 75 | memcpy(config, &s->blkcfg, vdev->config_len); |
| 76 | } |
| 77 | |
| 78 | static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config) |
| 79 | { |
| 80 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 81 | struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config; |
| 82 | int ret; |
| 83 | |
| 84 | if (blkcfg->wce == s->blkcfg.wce) { |
| 85 | return; |
| 86 | } |
| 87 | |
| 88 | ret = vhost_dev_set_config(&s->dev, &blkcfg->wce, |
| 89 | offsetof(struct virtio_blk_config, wce), |
| 90 | sizeof(blkcfg->wce), |
| 91 | VHOST_SET_CONFIG_TYPE_FRONTEND); |
| 92 | if (ret) { |
| 93 | error_report("set device config space failed"); |
| 94 | return; |
| 95 | } |
| 96 | |
| 97 | s->blkcfg.wce = blkcfg->wce; |
| 98 | } |
| 99 | |
| 100 | static int vhost_user_blk_sync_config(DeviceState *dev, Error **errp) |
| 101 | { |
| 102 | int ret; |
| 103 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 104 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 105 | |
| 106 | ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg, |
| 107 | vdev->config_len, errp); |
| 108 | if (ret < 0) { |
| 109 | return ret; |
| 110 | } |
| 111 | |
| 112 | memcpy(vdev->config, &s->blkcfg, vdev->config_len); |
| 113 | virtio_notify_config(vdev); |
| 114 | |
| 115 | return 0; |
| 116 | } |
| 117 | |
| 118 | static int vhost_user_blk_handle_config_change(struct vhost_dev *dev) |
| 119 | { |
| 120 | int ret; |
| 121 | Error *local_err = NULL; |
| 122 | |
| 123 | if (!dev->started) { |
| 124 | return 0; |
| 125 | } |
| 126 | |
| 127 | ret = vhost_user_blk_sync_config(DEVICE(dev->vdev), &local_err); |
| 128 | if (ret < 0) { |
| 129 | error_report_err(local_err); |
| 130 | return ret; |
| 131 | } |
| 132 | |
| 133 | return 0; |
| 134 | } |
| 135 | |
| 136 | const VhostDevConfigOps blk_ops = { |
| 137 | .vhost_dev_config_notifier = vhost_user_blk_handle_config_change, |
| 138 | }; |
| 139 | |
| 140 | static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) |
| 141 | { |
| 142 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 143 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
| 144 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
| 145 | int i, ret; |
| 146 | |
| 147 | trace_vhost_user_blk_start_in(vdev); |
| 148 | |
| 149 | if (!k->set_guest_notifiers) { |
| 150 | error_setg(errp, "binding does not support guest notifiers"); |
| 151 | return -ENOSYS; |
| 152 | } |
| 153 | |
| 154 | ret = vhost_dev_enable_notifiers(&s->dev, vdev); |
| 155 | if (ret < 0) { |
| 156 | error_setg_errno(errp, -ret, "Error enabling host notifiers"); |
| 157 | return ret; |
| 158 | } |
| 159 | |
| 160 | ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true); |
| 161 | if (ret < 0) { |
| 162 | error_setg_errno(errp, -ret, "Error binding guest notifier"); |
| 163 | goto err_host_notifiers; |
| 164 | } |
| 165 | |
| 166 | s->dev.acked_features = vdev->guest_features; |
| 167 | |
| 168 | ret = vhost_dev_prepare_inflight(&s->dev, vdev); |
| 169 | if (ret < 0) { |
| 170 | error_setg_errno(errp, -ret, "Error setting inflight format"); |
| 171 | goto err_guest_notifiers; |
| 172 | } |
| 173 | |
| 174 | if (!s->inflight->addr) { |
| 175 | ret = vhost_dev_get_inflight(&s->dev, s->queue_size, s->inflight); |
| 176 | if (ret < 0) { |
| 177 | error_setg_errno(errp, -ret, "Error getting inflight"); |
| 178 | goto err_guest_notifiers; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | ret = vhost_dev_set_inflight(&s->dev, s->inflight); |
| 183 | if (ret < 0) { |
| 184 | error_setg_errno(errp, -ret, "Error setting inflight"); |
| 185 | goto err_guest_notifiers; |
| 186 | } |
| 187 | |
| 188 | /* guest_notifier_mask/pending not used yet, so just unmask |
| 189 | * everything here. virtio-pci will do the right thing by |
| 190 | * enabling/disabling irqfd. |
| 191 | */ |
| 192 | for (i = 0; i < s->dev.nvqs; i++) { |
| 193 | vhost_virtqueue_mask(&s->dev, vdev, i, false); |
| 194 | } |
| 195 | |
| 196 | s->dev.vq_index_end = s->dev.nvqs; |
| 197 | ret = vhost_dev_start(&s->dev, vdev, true); |
| 198 | if (ret < 0) { |
| 199 | error_setg_errno(errp, -ret, "Error starting vhost"); |
| 200 | goto err_guest_notifiers; |
| 201 | } |
| 202 | s->started_vu = true; |
| 203 | |
| 204 | trace_vhost_user_blk_start_out(vdev); |
| 205 | |
| 206 | return ret; |
| 207 | |
| 208 | err_guest_notifiers: |
| 209 | for (i = 0; i < s->dev.nvqs; i++) { |
| 210 | vhost_virtqueue_mask(&s->dev, vdev, i, true); |
| 211 | } |
| 212 | k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); |
| 213 | err_host_notifiers: |
| 214 | vhost_dev_disable_notifiers(&s->dev, vdev); |
| 215 | return ret; |
| 216 | } |
| 217 | |
| 218 | static int vhost_user_blk_stop(VirtIODevice *vdev) |
| 219 | { |
| 220 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 221 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
| 222 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
| 223 | int ret, err; |
| 224 | bool force_stop = false; |
| 225 | |
| 226 | trace_vhost_user_blk_stop_in(vdev); |
| 227 | |
| 228 | if (!s->started_vu) { |
| 229 | return 0; |
| 230 | } |
| 231 | s->started_vu = false; |
| 232 | |
| 233 | if (!k->set_guest_notifiers) { |
| 234 | return 0; |
| 235 | } |
| 236 | |
| 237 | force_stop = s->skip_get_vring_base_on_force_shutdown && |
| 238 | qemu_force_shutdown_requested(); |
| 239 | |
| 240 | ret = force_stop ? vhost_dev_force_stop(&s->dev, vdev, true) : |
| 241 | vhost_dev_stop(&s->dev, vdev, true); |
| 242 | |
| 243 | err = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); |
| 244 | if (err < 0) { |
| 245 | error_report("vhost guest notifier cleanup failed: %d", err); |
| 246 | return err; |
| 247 | } |
| 248 | |
| 249 | vhost_dev_disable_notifiers(&s->dev, vdev); |
| 250 | |
| 251 | trace_vhost_user_blk_stop_out(vdev); |
| 252 | |
| 253 | return ret; |
| 254 | } |
| 255 | |
| 256 | static int vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status) |
| 257 | { |
| 258 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 259 | bool should_start = virtio_device_should_start(vdev, status); |
| 260 | Error *local_err = NULL; |
| 261 | int ret; |
| 262 | |
| 263 | if (!s->connected) { |
| 264 | return -1; |
| 265 | } |
| 266 | |
| 267 | if (vhost_dev_is_started(&s->dev) == should_start) { |
| 268 | return 0; |
| 269 | } |
| 270 | |
| 271 | if (should_start) { |
| 272 | ret = vhost_user_blk_start(vdev, &local_err); |
| 273 | if (ret < 0) { |
| 274 | error_reportf_err(local_err, "vhost-user-blk: vhost start failed: "); |
| 275 | qemu_chr_fe_disconnect(&s->chardev); |
| 276 | } |
| 277 | } else { |
| 278 | ret = vhost_user_blk_stop(vdev); |
| 279 | if (ret < 0) { |
| 280 | return ret; |
| 281 | } |
| 282 | } |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev, |
| 287 | uint64_t features, |
| 288 | Error **errp) |
| 289 | { |
| 290 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 291 | |
| 292 | /* Turn on pre-defined features */ |
| 293 | virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX); |
| 294 | virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY); |
| 295 | virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY); |
| 296 | virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE); |
| 297 | virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH); |
| 298 | virtio_add_feature(&features, VIRTIO_BLK_F_RO); |
| 299 | |
| 300 | if (s->num_queues > 1) { |
| 301 | virtio_add_feature(&features, VIRTIO_BLK_F_MQ); |
| 302 | } |
| 303 | |
| 304 | return vhost_get_features(&s->dev, user_feature_bits, features); |
| 305 | } |
| 306 | |
| 307 | static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
| 308 | { |
| 309 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 310 | Error *local_err = NULL; |
| 311 | int i, ret; |
| 312 | |
| 313 | if (!vdev->start_on_kick) { |
| 314 | return; |
| 315 | } |
| 316 | |
| 317 | if (!s->connected) { |
| 318 | return; |
| 319 | } |
| 320 | |
| 321 | if (vhost_dev_is_started(&s->dev)) { |
| 322 | return; |
| 323 | } |
| 324 | |
| 325 | /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start |
| 326 | * vhost here instead of waiting for .set_status(). |
| 327 | */ |
| 328 | ret = vhost_user_blk_start(vdev, &local_err); |
| 329 | if (ret < 0) { |
| 330 | error_reportf_err(local_err, "vhost-user-blk: vhost start failed: "); |
| 331 | qemu_chr_fe_disconnect(&s->chardev); |
| 332 | return; |
| 333 | } |
| 334 | |
| 335 | /* Kick right away to begin processing requests already in vring */ |
| 336 | for (i = 0; i < s->dev.nvqs; i++) { |
| 337 | VirtQueue *kick_vq = virtio_get_queue(vdev, i); |
| 338 | |
| 339 | if (!virtio_queue_get_desc_addr(vdev, i)) { |
| 340 | continue; |
| 341 | } |
| 342 | event_notifier_set(virtio_queue_get_host_notifier(kick_vq)); |
| 343 | } |
| 344 | } |
| 345 | |
| 346 | static void vhost_user_blk_reset(VirtIODevice *vdev) |
| 347 | { |
| 348 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 349 | |
| 350 | vhost_dev_free_inflight(s->inflight); |
| 351 | } |
| 352 | |
| 353 | static int vhost_user_blk_connect(DeviceState *dev, Error **errp) |
| 354 | { |
| 355 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 356 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 357 | int ret = 0; |
| 358 | |
| 359 | trace_vhost_user_blk_connect_in(vdev); |
| 360 | |
| 361 | if (s->connected) { |
| 362 | return 0; |
| 363 | } |
| 364 | |
| 365 | s->dev.num_queues = s->num_queues; |
| 366 | s->dev.nvqs = s->num_queues; |
| 367 | s->dev.vqs = s->vhost_vqs; |
| 368 | s->dev.vq_index = 0; |
| 369 | |
| 370 | vhost_dev_set_config_notifier(&s->dev, &blk_ops); |
| 371 | |
| 372 | s->vhost_user.supports_config = true; |
| 373 | s->vhost_user.supports_inflight_migration = s->inflight_migration; |
| 374 | ret = vhost_dev_init(&s->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0, |
| 375 | errp); |
| 376 | if (ret < 0) { |
| 377 | return ret; |
| 378 | } |
| 379 | |
| 380 | s->connected = true; |
| 381 | |
| 382 | /* restore vhost state */ |
| 383 | if (virtio_device_started(vdev, vdev->status)) { |
| 384 | ret = vhost_user_blk_start(vdev, errp); |
| 385 | } |
| 386 | |
| 387 | trace_vhost_user_blk_connect_out(vdev); |
| 388 | |
| 389 | return ret; |
| 390 | } |
| 391 | |
| 392 | static void vhost_user_blk_disconnect(DeviceState *dev) |
| 393 | { |
| 394 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 395 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 396 | |
| 397 | if (!s->connected) { |
| 398 | goto done; |
| 399 | } |
| 400 | s->connected = false; |
| 401 | |
| 402 | vhost_user_blk_stop(vdev); |
| 403 | |
| 404 | vhost_dev_cleanup(&s->dev); |
| 405 | |
| 406 | done: |
| 407 | /* Re-instate the event handler for new connections */ |
| 408 | qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event, |
| 409 | NULL, dev, NULL, true); |
| 410 | } |
| 411 | |
| 412 | static void vhost_user_blk_event(void *opaque, QEMUChrEvent event) |
| 413 | { |
| 414 | DeviceState *dev = opaque; |
| 415 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 416 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 417 | Error *local_err = NULL; |
| 418 | |
| 419 | switch (event) { |
| 420 | case CHR_EVENT_OPENED: |
| 421 | if (vhost_user_blk_connect(dev, &local_err) < 0) { |
| 422 | error_report_err(local_err); |
| 423 | qemu_chr_fe_disconnect(&s->chardev); |
| 424 | return; |
| 425 | } |
| 426 | break; |
| 427 | case CHR_EVENT_CLOSED: |
| 428 | /* defer close until later to avoid circular close */ |
| 429 | vhost_user_async_close(dev, &s->chardev, &s->dev, |
| 430 | vhost_user_blk_disconnect); |
| 431 | break; |
| 432 | case CHR_EVENT_BREAK: |
| 433 | case CHR_EVENT_MUX_IN: |
| 434 | case CHR_EVENT_MUX_OUT: |
| 435 | /* Ignore */ |
| 436 | break; |
| 437 | } |
| 438 | } |
| 439 | |
| 440 | static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp) |
| 441 | { |
| 442 | DeviceState *dev = DEVICE(s); |
| 443 | int ret; |
| 444 | |
| 445 | s->connected = false; |
| 446 | |
| 447 | ret = qemu_chr_fe_wait_connected(&s->chardev, errp); |
| 448 | if (ret < 0) { |
| 449 | return ret; |
| 450 | } |
| 451 | |
| 452 | ret = vhost_user_blk_connect(dev, errp); |
| 453 | if (ret < 0) { |
| 454 | qemu_chr_fe_disconnect(&s->chardev); |
| 455 | return ret; |
| 456 | } |
| 457 | assert(s->connected); |
| 458 | |
| 459 | ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg, |
| 460 | VIRTIO_DEVICE(s)->config_len, errp); |
| 461 | if (ret < 0) { |
| 462 | qemu_chr_fe_disconnect(&s->chardev); |
| 463 | vhost_dev_cleanup(&s->dev); |
| 464 | return ret; |
| 465 | } |
| 466 | |
| 467 | return 0; |
| 468 | } |
| 469 | |
| 470 | static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) |
| 471 | { |
| 472 | ERRP_GUARD(); |
| 473 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 474 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 475 | size_t config_size; |
| 476 | int retries; |
| 477 | int i, ret; |
| 478 | |
| 479 | trace_vhost_user_blk_device_realize_in(vdev); |
| 480 | |
| 481 | if (!s->chardev.chr) { |
| 482 | error_setg(errp, "chardev is mandatory"); |
| 483 | return; |
| 484 | } |
| 485 | |
| 486 | if (s->num_queues == VHOST_USER_BLK_AUTO_NUM_QUEUES) { |
| 487 | s->num_queues = 1; |
| 488 | } |
| 489 | if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) { |
| 490 | error_setg(errp, "invalid number of IO queues"); |
| 491 | return; |
| 492 | } |
| 493 | |
| 494 | if (!s->queue_size) { |
| 495 | error_setg(errp, "queue size must be non-zero"); |
| 496 | return; |
| 497 | } |
| 498 | if (s->queue_size < 4 && s->seg_max_adjust) { |
| 499 | error_setg(errp, "queue size must be >= 4 when seg-max-adjust is set"); |
| 500 | return; |
| 501 | } |
| 502 | if (s->queue_size > VIRTQUEUE_MAX_SIZE) { |
| 503 | error_setg(errp, "queue size must not exceed %d", |
| 504 | VIRTQUEUE_MAX_SIZE); |
| 505 | return; |
| 506 | } |
| 507 | |
| 508 | if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) { |
| 509 | return; |
| 510 | } |
| 511 | |
| 512 | config_size = virtio_get_config_size(&virtio_blk_cfg_size_params, |
| 513 | vdev->host_features); |
| 514 | virtio_init(vdev, VIRTIO_ID_BLOCK, config_size); |
| 515 | |
| 516 | s->virtqs = g_new(VirtQueue *, s->num_queues); |
| 517 | for (i = 0; i < s->num_queues; i++) { |
| 518 | s->virtqs[i] = virtio_add_queue(vdev, s->queue_size, |
| 519 | vhost_user_blk_handle_output); |
| 520 | } |
| 521 | |
| 522 | s->inflight = g_new0(struct vhost_inflight, 1); |
| 523 | s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues); |
| 524 | |
| 525 | retries = VU_REALIZE_CONN_RETRIES; |
| 526 | assert(!*errp); |
| 527 | do { |
| 528 | if (*errp) { |
| 529 | error_prepend(errp, "Reconnecting after error: "); |
| 530 | error_report_err(*errp); |
| 531 | *errp = NULL; |
| 532 | } |
| 533 | ret = vhost_user_blk_realize_connect(s, errp); |
| 534 | } while (ret < 0 && retries--); |
| 535 | |
| 536 | if (ret < 0) { |
| 537 | goto virtio_err; |
| 538 | } |
| 539 | |
| 540 | /* we're fully initialized, now we can operate, so add the handler */ |
| 541 | qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, |
| 542 | vhost_user_blk_event, NULL, (void *)dev, |
| 543 | NULL, true); |
| 544 | |
| 545 | trace_vhost_user_blk_device_realize_out(vdev); |
| 546 | |
| 547 | return; |
| 548 | |
| 549 | virtio_err: |
| 550 | g_free(s->vhost_vqs); |
| 551 | s->vhost_vqs = NULL; |
| 552 | g_free(s->inflight); |
| 553 | s->inflight = NULL; |
| 554 | for (i = 0; i < s->num_queues; i++) { |
| 555 | virtio_delete_queue(s->virtqs[i]); |
| 556 | } |
| 557 | g_free(s->virtqs); |
| 558 | virtio_cleanup(vdev); |
| 559 | vhost_user_cleanup(&s->vhost_user); |
| 560 | } |
| 561 | |
| 562 | static void vhost_user_blk_device_unrealize(DeviceState *dev) |
| 563 | { |
| 564 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 565 | VHostUserBlk *s = VHOST_USER_BLK(dev); |
| 566 | int i; |
| 567 | |
| 568 | virtio_set_status(vdev, 0); |
| 569 | qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL, |
| 570 | NULL, NULL, NULL, false); |
| 571 | vhost_dev_cleanup(&s->dev); |
| 572 | vhost_dev_free_inflight(s->inflight); |
| 573 | g_free(s->vhost_vqs); |
| 574 | s->vhost_vqs = NULL; |
| 575 | g_free(s->inflight); |
| 576 | s->inflight = NULL; |
| 577 | |
| 578 | for (i = 0; i < s->num_queues; i++) { |
| 579 | virtio_delete_queue(s->virtqs[i]); |
| 580 | } |
| 581 | g_free(s->virtqs); |
| 582 | virtio_cleanup(vdev); |
| 583 | vhost_user_cleanup(&s->vhost_user); |
| 584 | } |
| 585 | |
| 586 | static void vhost_user_blk_instance_init(Object *obj) |
| 587 | { |
| 588 | VHostUserBlk *s = VHOST_USER_BLK(obj); |
| 589 | |
| 590 | device_add_bootindex_property(obj, &s->bootindex, "bootindex", |
| 591 | "/disk@0,0", DEVICE(obj)); |
| 592 | } |
| 593 | |
| 594 | static struct vhost_dev *vhost_user_blk_get_vhost(VirtIODevice *vdev) |
| 595 | { |
| 596 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 597 | return &s->dev; |
| 598 | } |
| 599 | |
| 600 | static bool vhost_user_blk_inflight_needed(void *opaque) |
| 601 | { |
| 602 | struct VHostUserBlk *s = opaque; |
| 603 | |
| 604 | return vhost_user_has_protocol_feature( |
| 605 | &s->dev, VHOST_USER_PROTOCOL_F_GET_VRING_BASE_INFLIGHT); |
| 606 | } |
| 607 | |
| 608 | static const VMStateDescription vmstate_vhost_user_blk_inflight = { |
| 609 | .name = "vhost-user-blk/inflight", |
| 610 | .version_id = 1, |
| 611 | .needed = vhost_user_blk_inflight_needed, |
| 612 | .fields = (const VMStateField[]) { |
| 613 | VMSTATE_VHOST_INFLIGHT_REGION(inflight, VHostUserBlk), |
| 614 | VMSTATE_END_OF_LIST() |
| 615 | }, |
| 616 | }; |
| 617 | |
| 618 | static const VMStateDescription vmstate_vhost_user_blk = { |
| 619 | .name = "vhost-user-blk", |
| 620 | .minimum_version_id = 1, |
| 621 | .version_id = 1, |
| 622 | .fields = (const VMStateField[]) { |
| 623 | VMSTATE_VIRTIO_DEVICE, |
| 624 | VMSTATE_END_OF_LIST() |
| 625 | }, |
| 626 | .subsections = (const VMStateDescription * const []) { |
| 627 | &vmstate_vhost_user_blk_inflight, |
| 628 | NULL |
| 629 | } |
| 630 | }; |
| 631 | |
| 632 | static const Property vhost_user_blk_properties[] = { |
| 633 | DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev), |
| 634 | DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues, |
| 635 | VHOST_USER_BLK_AUTO_NUM_QUEUES), |
| 636 | DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128), |
| 637 | DEFINE_PROP_BOOL("seg-max-adjust", VHostUserBlk, seg_max_adjust, |
| 638 | false), |
| 639 | DEFINE_PROP_BIT64("config-wce", VHostUserBlk, parent_obj.host_features, |
| 640 | VIRTIO_BLK_F_CONFIG_WCE, true), |
| 641 | DEFINE_PROP_BIT64("discard", VHostUserBlk, parent_obj.host_features, |
| 642 | VIRTIO_BLK_F_DISCARD, true), |
| 643 | DEFINE_PROP_BIT64("write-zeroes", VHostUserBlk, parent_obj.host_features, |
| 644 | VIRTIO_BLK_F_WRITE_ZEROES, true), |
| 645 | DEFINE_PROP_BOOL("skip-get-vring-base-on-force-shutdown", VHostUserBlk, |
| 646 | skip_get_vring_base_on_force_shutdown, false), |
| 647 | DEFINE_PROP_BOOL("inflight-migration", VHostUserBlk, |
| 648 | inflight_migration, false), |
| 649 | }; |
| 650 | |
| 651 | static void vhost_user_blk_class_init(ObjectClass *klass, const void *data) |
| 652 | { |
| 653 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 654 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); |
| 655 | |
| 656 | device_class_set_props(dc, vhost_user_blk_properties); |
| 657 | dc->vmsd = &vmstate_vhost_user_blk; |
| 658 | dc->sync_config = vhost_user_blk_sync_config; |
| 659 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
| 660 | vdc->realize = vhost_user_blk_device_realize; |
| 661 | vdc->unrealize = vhost_user_blk_device_unrealize; |
| 662 | vdc->get_config = vhost_user_blk_update_config; |
| 663 | vdc->set_config = vhost_user_blk_set_config; |
| 664 | vdc->get_features = vhost_user_blk_get_features; |
| 665 | vdc->set_status = vhost_user_blk_set_status; |
| 666 | vdc->reset = vhost_user_blk_reset; |
| 667 | vdc->get_vhost = vhost_user_blk_get_vhost; |
| 668 | } |
| 669 | |
| 670 | static const TypeInfo vhost_user_blk_info = { |
| 671 | .name = TYPE_VHOST_USER_BLK, |
| 672 | .parent = TYPE_VIRTIO_DEVICE, |
| 673 | .instance_size = sizeof(VHostUserBlk), |
| 674 | .instance_init = vhost_user_blk_instance_init, |
| 675 | .class_init = vhost_user_blk_class_init, |
| 676 | }; |
| 677 | |
| 678 | static void virtio_register_types(void) |
| 679 | { |
| 680 | type_register_static(&vhost_user_blk_info); |
| 681 | } |
| 682 | |
| 683 | type_init(virtio_register_types) |