spapr: Simplify spapr_cpu_core_realize() and spapr_cpu_core_unrealize()
[qemu/ar7.git] / hw / block / vhost-user-blk.c
bloba076b1e54d1936b1eac973c4da3d520484e68960
1 /*
2 * vhost-user-blk host device
4 * Copyright(C) 2017 Intel Corporation.
6 * Authors:
7 * Changpeng Liu <changpeng.liu@intel.com>
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>
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.
19 #include "qemu/osdep.h"
20 #include "qapi/error.h"
21 #include "qemu/error-report.h"
22 #include "qemu/cutils.h"
23 #include "hw/qdev-core.h"
24 #include "hw/qdev-properties.h"
25 #include "hw/virtio/vhost.h"
26 #include "hw/virtio/vhost-user-blk.h"
27 #include "hw/virtio/virtio.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "hw/virtio/virtio-access.h"
30 #include "sysemu/sysemu.h"
31 #include "sysemu/runstate.h"
33 static const int user_feature_bits[] = {
34 VIRTIO_BLK_F_SIZE_MAX,
35 VIRTIO_BLK_F_SEG_MAX,
36 VIRTIO_BLK_F_GEOMETRY,
37 VIRTIO_BLK_F_BLK_SIZE,
38 VIRTIO_BLK_F_TOPOLOGY,
39 VIRTIO_BLK_F_MQ,
40 VIRTIO_BLK_F_RO,
41 VIRTIO_BLK_F_FLUSH,
42 VIRTIO_BLK_F_CONFIG_WCE,
43 VIRTIO_BLK_F_DISCARD,
44 VIRTIO_BLK_F_WRITE_ZEROES,
45 VIRTIO_F_VERSION_1,
46 VIRTIO_RING_F_INDIRECT_DESC,
47 VIRTIO_RING_F_EVENT_IDX,
48 VIRTIO_F_NOTIFY_ON_EMPTY,
49 VHOST_INVALID_FEATURE_BIT
52 static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config)
54 VHostUserBlk *s = VHOST_USER_BLK(vdev);
56 memcpy(config, &s->blkcfg, sizeof(struct virtio_blk_config));
59 static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
61 VHostUserBlk *s = VHOST_USER_BLK(vdev);
62 struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config;
63 int ret;
65 if (blkcfg->wce == s->blkcfg.wce) {
66 return;
69 ret = vhost_dev_set_config(&s->dev, &blkcfg->wce,
70 offsetof(struct virtio_blk_config, wce),
71 sizeof(blkcfg->wce),
72 VHOST_SET_CONFIG_TYPE_MASTER);
73 if (ret) {
74 error_report("set device config space failed");
75 return;
78 s->blkcfg.wce = blkcfg->wce;
81 static int vhost_user_blk_handle_config_change(struct vhost_dev *dev)
83 int ret;
84 struct virtio_blk_config blkcfg;
85 VHostUserBlk *s = VHOST_USER_BLK(dev->vdev);
87 ret = vhost_dev_get_config(dev, (uint8_t *)&blkcfg,
88 sizeof(struct virtio_blk_config));
89 if (ret < 0) {
90 error_report("get config space failed");
91 return -1;
94 /* valid for resize only */
95 if (blkcfg.capacity != s->blkcfg.capacity) {
96 s->blkcfg.capacity = blkcfg.capacity;
97 memcpy(dev->vdev->config, &s->blkcfg, sizeof(struct virtio_blk_config));
98 virtio_notify_config(dev->vdev);
101 return 0;
104 const VhostDevConfigOps blk_ops = {
105 .vhost_dev_config_notifier = vhost_user_blk_handle_config_change,
108 static int vhost_user_blk_start(VirtIODevice *vdev)
110 VHostUserBlk *s = VHOST_USER_BLK(vdev);
111 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
112 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
113 int i, ret;
115 if (!k->set_guest_notifiers) {
116 error_report("binding does not support guest notifiers");
117 return -ENOSYS;
120 ret = vhost_dev_enable_notifiers(&s->dev, vdev);
121 if (ret < 0) {
122 error_report("Error enabling host notifiers: %d", -ret);
123 return ret;
126 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
127 if (ret < 0) {
128 error_report("Error binding guest notifier: %d", -ret);
129 goto err_host_notifiers;
132 s->dev.acked_features = vdev->guest_features;
134 if (!s->inflight->addr) {
135 ret = vhost_dev_get_inflight(&s->dev, s->queue_size, s->inflight);
136 if (ret < 0) {
137 error_report("Error get inflight: %d", -ret);
138 goto err_guest_notifiers;
142 ret = vhost_dev_set_inflight(&s->dev, s->inflight);
143 if (ret < 0) {
144 error_report("Error set inflight: %d", -ret);
145 goto err_guest_notifiers;
148 ret = vhost_dev_start(&s->dev, vdev);
149 if (ret < 0) {
150 error_report("Error starting vhost: %d", -ret);
151 goto err_guest_notifiers;
153 s->started_vu = true;
155 /* guest_notifier_mask/pending not used yet, so just unmask
156 * everything here. virtio-pci will do the right thing by
157 * enabling/disabling irqfd.
159 for (i = 0; i < s->dev.nvqs; i++) {
160 vhost_virtqueue_mask(&s->dev, vdev, i, false);
163 return ret;
165 err_guest_notifiers:
166 k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
167 err_host_notifiers:
168 vhost_dev_disable_notifiers(&s->dev, vdev);
169 return ret;
172 static void vhost_user_blk_stop(VirtIODevice *vdev)
174 VHostUserBlk *s = VHOST_USER_BLK(vdev);
175 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
176 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
177 int ret;
179 if (!s->started_vu) {
180 return;
182 s->started_vu = false;
184 if (!k->set_guest_notifiers) {
185 return;
188 vhost_dev_stop(&s->dev, vdev);
190 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
191 if (ret < 0) {
192 error_report("vhost guest notifier cleanup failed: %d", ret);
193 return;
196 vhost_dev_disable_notifiers(&s->dev, vdev);
199 static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status)
201 VHostUserBlk *s = VHOST_USER_BLK(vdev);
202 bool should_start = virtio_device_started(vdev, status);
203 int ret;
205 if (!vdev->vm_running) {
206 should_start = false;
209 if (!s->connected) {
210 return;
213 if (s->dev.started == should_start) {
214 return;
217 if (should_start) {
218 ret = vhost_user_blk_start(vdev);
219 if (ret < 0) {
220 error_report("vhost-user-blk: vhost start failed: %s",
221 strerror(-ret));
222 qemu_chr_fe_disconnect(&s->chardev);
224 } else {
225 vhost_user_blk_stop(vdev);
230 static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev,
231 uint64_t features,
232 Error **errp)
234 VHostUserBlk *s = VHOST_USER_BLK(vdev);
236 /* Turn on pre-defined features */
237 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
238 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
239 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
240 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
241 virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH);
242 virtio_add_feature(&features, VIRTIO_BLK_F_RO);
243 virtio_add_feature(&features, VIRTIO_BLK_F_DISCARD);
244 virtio_add_feature(&features, VIRTIO_BLK_F_WRITE_ZEROES);
246 if (s->config_wce) {
247 virtio_add_feature(&features, VIRTIO_BLK_F_CONFIG_WCE);
249 if (s->num_queues > 1) {
250 virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
253 return vhost_get_features(&s->dev, user_feature_bits, features);
256 static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
258 VHostUserBlk *s = VHOST_USER_BLK(vdev);
259 int i, ret;
261 if (!vdev->start_on_kick) {
262 return;
265 if (!s->connected) {
266 return;
269 if (s->dev.started) {
270 return;
273 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
274 * vhost here instead of waiting for .set_status().
276 ret = vhost_user_blk_start(vdev);
277 if (ret < 0) {
278 error_report("vhost-user-blk: vhost start failed: %s",
279 strerror(-ret));
280 qemu_chr_fe_disconnect(&s->chardev);
281 return;
284 /* Kick right away to begin processing requests already in vring */
285 for (i = 0; i < s->dev.nvqs; i++) {
286 VirtQueue *kick_vq = virtio_get_queue(vdev, i);
288 if (!virtio_queue_get_desc_addr(vdev, i)) {
289 continue;
291 event_notifier_set(virtio_queue_get_host_notifier(kick_vq));
295 static void vhost_user_blk_reset(VirtIODevice *vdev)
297 VHostUserBlk *s = VHOST_USER_BLK(vdev);
299 vhost_dev_free_inflight(s->inflight);
302 static int vhost_user_blk_connect(DeviceState *dev)
304 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
305 VHostUserBlk *s = VHOST_USER_BLK(vdev);
306 int ret = 0;
308 if (s->connected) {
309 return 0;
311 s->connected = true;
313 s->dev.nvqs = s->num_queues;
314 s->dev.vqs = s->vhost_vqs;
315 s->dev.vq_index = 0;
316 s->dev.backend_features = 0;
318 vhost_dev_set_config_notifier(&s->dev, &blk_ops);
320 ret = vhost_dev_init(&s->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0);
321 if (ret < 0) {
322 error_report("vhost-user-blk: vhost initialization failed: %s",
323 strerror(-ret));
324 return ret;
327 /* restore vhost state */
328 if (virtio_device_started(vdev, vdev->status)) {
329 ret = vhost_user_blk_start(vdev);
330 if (ret < 0) {
331 error_report("vhost-user-blk: vhost start failed: %s",
332 strerror(-ret));
333 return ret;
337 return 0;
340 static void vhost_user_blk_disconnect(DeviceState *dev)
342 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
343 VHostUserBlk *s = VHOST_USER_BLK(vdev);
345 if (!s->connected) {
346 return;
348 s->connected = false;
350 vhost_user_blk_stop(vdev);
352 vhost_dev_cleanup(&s->dev);
355 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event);
357 static void vhost_user_blk_chr_closed_bh(void *opaque)
359 DeviceState *dev = opaque;
360 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
361 VHostUserBlk *s = VHOST_USER_BLK(vdev);
363 vhost_user_blk_disconnect(dev);
364 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event,
365 NULL, opaque, NULL, true);
368 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event)
370 DeviceState *dev = opaque;
371 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
372 VHostUserBlk *s = VHOST_USER_BLK(vdev);
374 switch (event) {
375 case CHR_EVENT_OPENED:
376 if (vhost_user_blk_connect(dev) < 0) {
377 qemu_chr_fe_disconnect(&s->chardev);
378 return;
380 break;
381 case CHR_EVENT_CLOSED:
383 * A close event may happen during a read/write, but vhost
384 * code assumes the vhost_dev remains setup, so delay the
385 * stop & clear. There are two possible paths to hit this
386 * disconnect event:
387 * 1. When VM is in the RUN_STATE_PRELAUNCH state. The
388 * vhost_user_blk_device_realize() is a caller.
389 * 2. In tha main loop phase after VM start.
391 * For p2 the disconnect event will be delayed. We can't
392 * do the same for p1, because we are not running the loop
393 * at this moment. So just skip this step and perform
394 * disconnect in the caller function.
396 * TODO: maybe it is a good idea to make the same fix
397 * for other vhost-user devices.
399 if (runstate_is_running()) {
400 AioContext *ctx = qemu_get_current_aio_context();
402 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL, NULL,
403 NULL, NULL, false);
404 aio_bh_schedule_oneshot(ctx, vhost_user_blk_chr_closed_bh, opaque);
408 * Move vhost device to the stopped state. The vhost-user device
409 * will be clean up and disconnected in BH. This can be useful in
410 * the vhost migration code. If disconnect was caught there is an
411 * option for the general vhost code to get the dev state without
412 * knowing its type (in this case vhost-user).
414 s->dev.started = false;
415 break;
416 case CHR_EVENT_BREAK:
417 case CHR_EVENT_MUX_IN:
418 case CHR_EVENT_MUX_OUT:
419 /* Ignore */
420 break;
424 static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
426 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
427 VHostUserBlk *s = VHOST_USER_BLK(vdev);
428 Error *err = NULL;
429 int i, ret;
431 if (!s->chardev.chr) {
432 error_setg(errp, "vhost-user-blk: chardev is mandatory");
433 return;
436 if (s->num_queues == VHOST_USER_BLK_AUTO_NUM_QUEUES) {
437 s->num_queues = 1;
439 if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) {
440 error_setg(errp, "vhost-user-blk: invalid number of IO queues");
441 return;
444 if (!s->queue_size) {
445 error_setg(errp, "vhost-user-blk: queue size must be non-zero");
446 return;
449 if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) {
450 return;
453 virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
454 sizeof(struct virtio_blk_config));
456 s->virtqs = g_new(VirtQueue *, s->num_queues);
457 for (i = 0; i < s->num_queues; i++) {
458 s->virtqs[i] = virtio_add_queue(vdev, s->queue_size,
459 vhost_user_blk_handle_output);
462 s->inflight = g_new0(struct vhost_inflight, 1);
463 s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues);
464 s->connected = false;
466 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event,
467 NULL, (void *)dev, NULL, true);
469 reconnect:
470 if (qemu_chr_fe_wait_connected(&s->chardev, &err) < 0) {
471 error_report_err(err);
472 goto virtio_err;
475 /* check whether vhost_user_blk_connect() failed or not */
476 if (!s->connected) {
477 goto reconnect;
480 ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
481 sizeof(struct virtio_blk_config));
482 if (ret < 0) {
483 error_report("vhost-user-blk: get block config failed");
484 goto reconnect;
487 if (s->blkcfg.num_queues != s->num_queues) {
488 s->blkcfg.num_queues = s->num_queues;
491 return;
493 virtio_err:
494 g_free(s->vhost_vqs);
495 s->vhost_vqs = NULL;
496 g_free(s->inflight);
497 s->inflight = NULL;
498 for (i = 0; i < s->num_queues; i++) {
499 virtio_delete_queue(s->virtqs[i]);
501 g_free(s->virtqs);
502 virtio_cleanup(vdev);
503 vhost_user_cleanup(&s->vhost_user);
506 static void vhost_user_blk_device_unrealize(DeviceState *dev)
508 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
509 VHostUserBlk *s = VHOST_USER_BLK(dev);
510 int i;
512 virtio_set_status(vdev, 0);
513 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL,
514 NULL, NULL, NULL, false);
515 vhost_dev_cleanup(&s->dev);
516 vhost_dev_free_inflight(s->inflight);
517 g_free(s->vhost_vqs);
518 s->vhost_vqs = NULL;
519 g_free(s->inflight);
520 s->inflight = NULL;
522 for (i = 0; i < s->num_queues; i++) {
523 virtio_delete_queue(s->virtqs[i]);
525 g_free(s->virtqs);
526 virtio_cleanup(vdev);
527 vhost_user_cleanup(&s->vhost_user);
530 static void vhost_user_blk_instance_init(Object *obj)
532 VHostUserBlk *s = VHOST_USER_BLK(obj);
534 device_add_bootindex_property(obj, &s->bootindex, "bootindex",
535 "/disk@0,0", DEVICE(obj));
538 static const VMStateDescription vmstate_vhost_user_blk = {
539 .name = "vhost-user-blk",
540 .minimum_version_id = 1,
541 .version_id = 1,
542 .fields = (VMStateField[]) {
543 VMSTATE_VIRTIO_DEVICE,
544 VMSTATE_END_OF_LIST()
548 static Property vhost_user_blk_properties[] = {
549 DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev),
550 DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues,
551 VHOST_USER_BLK_AUTO_NUM_QUEUES),
552 DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128),
553 DEFINE_PROP_BIT("config-wce", VHostUserBlk, config_wce, 0, true),
554 DEFINE_PROP_END_OF_LIST(),
557 static void vhost_user_blk_class_init(ObjectClass *klass, void *data)
559 DeviceClass *dc = DEVICE_CLASS(klass);
560 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
562 device_class_set_props(dc, vhost_user_blk_properties);
563 dc->vmsd = &vmstate_vhost_user_blk;
564 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
565 vdc->realize = vhost_user_blk_device_realize;
566 vdc->unrealize = vhost_user_blk_device_unrealize;
567 vdc->get_config = vhost_user_blk_update_config;
568 vdc->set_config = vhost_user_blk_set_config;
569 vdc->get_features = vhost_user_blk_get_features;
570 vdc->set_status = vhost_user_blk_set_status;
571 vdc->reset = vhost_user_blk_reset;
574 static const TypeInfo vhost_user_blk_info = {
575 .name = TYPE_VHOST_USER_BLK,
576 .parent = TYPE_VIRTIO_DEVICE,
577 .instance_size = sizeof(VHostUserBlk),
578 .instance_init = vhost_user_blk_instance_init,
579 .class_init = vhost_user_blk_class_init,
582 static void virtio_register_types(void)
584 type_register_static(&vhost_user_blk_info);
587 type_init(virtio_register_types)