Merge remote-tracking branch 'remotes/bonzini-gitlab/tags/for-upstream' into staging
[qemu/ar7.git] / hw / block / vhost-user-blk.c
blobb870a50e6b201d81e37bc230bf7f0a190e5ffc50
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/qdev-properties-system.h"
26 #include "hw/virtio/vhost.h"
27 #include "hw/virtio/vhost-user-blk.h"
28 #include "hw/virtio/virtio.h"
29 #include "hw/virtio/virtio-bus.h"
30 #include "hw/virtio/virtio-access.h"
31 #include "sysemu/sysemu.h"
32 #include "sysemu/runstate.h"
34 static const int user_feature_bits[] = {
35 VIRTIO_BLK_F_SIZE_MAX,
36 VIRTIO_BLK_F_SEG_MAX,
37 VIRTIO_BLK_F_GEOMETRY,
38 VIRTIO_BLK_F_BLK_SIZE,
39 VIRTIO_BLK_F_TOPOLOGY,
40 VIRTIO_BLK_F_MQ,
41 VIRTIO_BLK_F_RO,
42 VIRTIO_BLK_F_FLUSH,
43 VIRTIO_BLK_F_CONFIG_WCE,
44 VIRTIO_BLK_F_DISCARD,
45 VIRTIO_BLK_F_WRITE_ZEROES,
46 VIRTIO_F_VERSION_1,
47 VIRTIO_RING_F_INDIRECT_DESC,
48 VIRTIO_RING_F_EVENT_IDX,
49 VIRTIO_F_NOTIFY_ON_EMPTY,
50 VHOST_INVALID_FEATURE_BIT
53 static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config)
55 VHostUserBlk *s = VHOST_USER_BLK(vdev);
57 /* Our num_queues overrides the device backend */
58 virtio_stw_p(vdev, &s->blkcfg.num_queues, s->num_queues);
60 memcpy(config, &s->blkcfg, sizeof(struct virtio_blk_config));
63 static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
65 VHostUserBlk *s = VHOST_USER_BLK(vdev);
66 struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config;
67 int ret;
69 if (blkcfg->wce == s->blkcfg.wce) {
70 return;
73 ret = vhost_dev_set_config(&s->dev, &blkcfg->wce,
74 offsetof(struct virtio_blk_config, wce),
75 sizeof(blkcfg->wce),
76 VHOST_SET_CONFIG_TYPE_MASTER);
77 if (ret) {
78 error_report("set device config space failed");
79 return;
82 s->blkcfg.wce = blkcfg->wce;
85 static int vhost_user_blk_handle_config_change(struct vhost_dev *dev)
87 int ret;
88 struct virtio_blk_config blkcfg;
89 VHostUserBlk *s = VHOST_USER_BLK(dev->vdev);
91 ret = vhost_dev_get_config(dev, (uint8_t *)&blkcfg,
92 sizeof(struct virtio_blk_config));
93 if (ret < 0) {
94 error_report("get config space failed");
95 return -1;
98 /* valid for resize only */
99 if (blkcfg.capacity != s->blkcfg.capacity) {
100 s->blkcfg.capacity = blkcfg.capacity;
101 memcpy(dev->vdev->config, &s->blkcfg, sizeof(struct virtio_blk_config));
102 virtio_notify_config(dev->vdev);
105 return 0;
108 const VhostDevConfigOps blk_ops = {
109 .vhost_dev_config_notifier = vhost_user_blk_handle_config_change,
112 static int vhost_user_blk_start(VirtIODevice *vdev)
114 VHostUserBlk *s = VHOST_USER_BLK(vdev);
115 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
116 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
117 int i, ret;
119 if (!k->set_guest_notifiers) {
120 error_report("binding does not support guest notifiers");
121 return -ENOSYS;
124 ret = vhost_dev_enable_notifiers(&s->dev, vdev);
125 if (ret < 0) {
126 error_report("Error enabling host notifiers: %d", -ret);
127 return ret;
130 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
131 if (ret < 0) {
132 error_report("Error binding guest notifier: %d", -ret);
133 goto err_host_notifiers;
136 s->dev.acked_features = vdev->guest_features;
138 ret = vhost_dev_prepare_inflight(&s->dev, vdev);
139 if (ret < 0) {
140 error_report("Error set inflight format: %d", -ret);
141 goto err_guest_notifiers;
144 if (!s->inflight->addr) {
145 ret = vhost_dev_get_inflight(&s->dev, s->queue_size, s->inflight);
146 if (ret < 0) {
147 error_report("Error get inflight: %d", -ret);
148 goto err_guest_notifiers;
152 ret = vhost_dev_set_inflight(&s->dev, s->inflight);
153 if (ret < 0) {
154 error_report("Error set inflight: %d", -ret);
155 goto err_guest_notifiers;
158 ret = vhost_dev_start(&s->dev, vdev);
159 if (ret < 0) {
160 error_report("Error starting vhost: %d", -ret);
161 goto err_guest_notifiers;
163 s->started_vu = true;
165 /* guest_notifier_mask/pending not used yet, so just unmask
166 * everything here. virtio-pci will do the right thing by
167 * enabling/disabling irqfd.
169 for (i = 0; i < s->dev.nvqs; i++) {
170 vhost_virtqueue_mask(&s->dev, vdev, i, false);
173 return ret;
175 err_guest_notifiers:
176 k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
177 err_host_notifiers:
178 vhost_dev_disable_notifiers(&s->dev, vdev);
179 return ret;
182 static void vhost_user_blk_stop(VirtIODevice *vdev)
184 VHostUserBlk *s = VHOST_USER_BLK(vdev);
185 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
186 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
187 int ret;
189 if (!s->started_vu) {
190 return;
192 s->started_vu = false;
194 if (!k->set_guest_notifiers) {
195 return;
198 vhost_dev_stop(&s->dev, vdev);
200 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
201 if (ret < 0) {
202 error_report("vhost guest notifier cleanup failed: %d", ret);
203 return;
206 vhost_dev_disable_notifiers(&s->dev, vdev);
209 static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status)
211 VHostUserBlk *s = VHOST_USER_BLK(vdev);
212 bool should_start = virtio_device_started(vdev, status);
213 int ret;
215 if (!vdev->vm_running) {
216 should_start = false;
219 if (!s->connected) {
220 return;
223 if (s->dev.started == should_start) {
224 return;
227 if (should_start) {
228 ret = vhost_user_blk_start(vdev);
229 if (ret < 0) {
230 error_report("vhost-user-blk: vhost start failed: %s",
231 strerror(-ret));
232 qemu_chr_fe_disconnect(&s->chardev);
234 } else {
235 vhost_user_blk_stop(vdev);
240 static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev,
241 uint64_t features,
242 Error **errp)
244 VHostUserBlk *s = VHOST_USER_BLK(vdev);
246 /* Turn on pre-defined features */
247 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
248 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
249 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
250 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
251 virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH);
252 virtio_add_feature(&features, VIRTIO_BLK_F_RO);
253 virtio_add_feature(&features, VIRTIO_BLK_F_DISCARD);
254 virtio_add_feature(&features, VIRTIO_BLK_F_WRITE_ZEROES);
256 if (s->config_wce) {
257 virtio_add_feature(&features, VIRTIO_BLK_F_CONFIG_WCE);
259 if (s->num_queues > 1) {
260 virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
263 return vhost_get_features(&s->dev, user_feature_bits, features);
266 static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
268 VHostUserBlk *s = VHOST_USER_BLK(vdev);
269 int i, ret;
271 if (!vdev->start_on_kick) {
272 return;
275 if (!s->connected) {
276 return;
279 if (s->dev.started) {
280 return;
283 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
284 * vhost here instead of waiting for .set_status().
286 ret = vhost_user_blk_start(vdev);
287 if (ret < 0) {
288 error_report("vhost-user-blk: vhost start failed: %s",
289 strerror(-ret));
290 qemu_chr_fe_disconnect(&s->chardev);
291 return;
294 /* Kick right away to begin processing requests already in vring */
295 for (i = 0; i < s->dev.nvqs; i++) {
296 VirtQueue *kick_vq = virtio_get_queue(vdev, i);
298 if (!virtio_queue_get_desc_addr(vdev, i)) {
299 continue;
301 event_notifier_set(virtio_queue_get_host_notifier(kick_vq));
305 static void vhost_user_blk_reset(VirtIODevice *vdev)
307 VHostUserBlk *s = VHOST_USER_BLK(vdev);
309 vhost_dev_free_inflight(s->inflight);
312 static int vhost_user_blk_connect(DeviceState *dev)
314 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
315 VHostUserBlk *s = VHOST_USER_BLK(vdev);
316 int ret = 0;
318 if (s->connected) {
319 return 0;
321 s->connected = true;
323 s->dev.nvqs = s->num_queues;
324 s->dev.vqs = s->vhost_vqs;
325 s->dev.vq_index = 0;
326 s->dev.backend_features = 0;
328 vhost_dev_set_config_notifier(&s->dev, &blk_ops);
330 ret = vhost_dev_init(&s->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0);
331 if (ret < 0) {
332 error_report("vhost-user-blk: vhost initialization failed: %s",
333 strerror(-ret));
334 return ret;
337 /* restore vhost state */
338 if (virtio_device_started(vdev, vdev->status)) {
339 ret = vhost_user_blk_start(vdev);
340 if (ret < 0) {
341 error_report("vhost-user-blk: vhost start failed: %s",
342 strerror(-ret));
343 return ret;
347 return 0;
350 static void vhost_user_blk_disconnect(DeviceState *dev)
352 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
353 VHostUserBlk *s = VHOST_USER_BLK(vdev);
355 if (!s->connected) {
356 return;
358 s->connected = false;
360 vhost_user_blk_stop(vdev);
362 vhost_dev_cleanup(&s->dev);
365 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event);
367 static void vhost_user_blk_chr_closed_bh(void *opaque)
369 DeviceState *dev = opaque;
370 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
371 VHostUserBlk *s = VHOST_USER_BLK(vdev);
373 vhost_user_blk_disconnect(dev);
374 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event,
375 NULL, opaque, NULL, true);
378 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event)
380 DeviceState *dev = opaque;
381 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
382 VHostUserBlk *s = VHOST_USER_BLK(vdev);
384 switch (event) {
385 case CHR_EVENT_OPENED:
386 if (vhost_user_blk_connect(dev) < 0) {
387 qemu_chr_fe_disconnect(&s->chardev);
388 return;
390 break;
391 case CHR_EVENT_CLOSED:
393 * A close event may happen during a read/write, but vhost
394 * code assumes the vhost_dev remains setup, so delay the
395 * stop & clear. There are two possible paths to hit this
396 * disconnect event:
397 * 1. When VM is in the RUN_STATE_PRELAUNCH state. The
398 * vhost_user_blk_device_realize() is a caller.
399 * 2. In tha main loop phase after VM start.
401 * For p2 the disconnect event will be delayed. We can't
402 * do the same for p1, because we are not running the loop
403 * at this moment. So just skip this step and perform
404 * disconnect in the caller function.
406 * TODO: maybe it is a good idea to make the same fix
407 * for other vhost-user devices.
409 if (runstate_is_running()) {
410 AioContext *ctx = qemu_get_current_aio_context();
412 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL, NULL,
413 NULL, NULL, false);
414 aio_bh_schedule_oneshot(ctx, vhost_user_blk_chr_closed_bh, opaque);
418 * Move vhost device to the stopped state. The vhost-user device
419 * will be clean up and disconnected in BH. This can be useful in
420 * the vhost migration code. If disconnect was caught there is an
421 * option for the general vhost code to get the dev state without
422 * knowing its type (in this case vhost-user).
424 s->dev.started = false;
425 break;
426 case CHR_EVENT_BREAK:
427 case CHR_EVENT_MUX_IN:
428 case CHR_EVENT_MUX_OUT:
429 /* Ignore */
430 break;
434 static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
436 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
437 VHostUserBlk *s = VHOST_USER_BLK(vdev);
438 Error *err = NULL;
439 int i, ret;
441 if (!s->chardev.chr) {
442 error_setg(errp, "vhost-user-blk: chardev is mandatory");
443 return;
446 if (s->num_queues == VHOST_USER_BLK_AUTO_NUM_QUEUES) {
447 s->num_queues = 1;
449 if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) {
450 error_setg(errp, "vhost-user-blk: invalid number of IO queues");
451 return;
454 if (!s->queue_size) {
455 error_setg(errp, "vhost-user-blk: queue size must be non-zero");
456 return;
459 if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) {
460 return;
463 virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
464 sizeof(struct virtio_blk_config));
466 s->virtqs = g_new(VirtQueue *, s->num_queues);
467 for (i = 0; i < s->num_queues; i++) {
468 s->virtqs[i] = virtio_add_queue(vdev, s->queue_size,
469 vhost_user_blk_handle_output);
472 s->inflight = g_new0(struct vhost_inflight, 1);
473 s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues);
474 s->connected = false;
476 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event,
477 NULL, (void *)dev, NULL, true);
479 reconnect:
480 if (qemu_chr_fe_wait_connected(&s->chardev, &err) < 0) {
481 error_report_err(err);
482 goto virtio_err;
485 /* check whether vhost_user_blk_connect() failed or not */
486 if (!s->connected) {
487 goto reconnect;
490 ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
491 sizeof(struct virtio_blk_config));
492 if (ret < 0) {
493 error_report("vhost-user-blk: get block config failed");
494 goto reconnect;
497 return;
499 virtio_err:
500 g_free(s->vhost_vqs);
501 s->vhost_vqs = NULL;
502 g_free(s->inflight);
503 s->inflight = NULL;
504 for (i = 0; i < s->num_queues; i++) {
505 virtio_delete_queue(s->virtqs[i]);
507 g_free(s->virtqs);
508 virtio_cleanup(vdev);
509 vhost_user_cleanup(&s->vhost_user);
512 static void vhost_user_blk_device_unrealize(DeviceState *dev)
514 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
515 VHostUserBlk *s = VHOST_USER_BLK(dev);
516 int i;
518 virtio_set_status(vdev, 0);
519 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL,
520 NULL, NULL, NULL, false);
521 vhost_dev_cleanup(&s->dev);
522 vhost_dev_free_inflight(s->inflight);
523 g_free(s->vhost_vqs);
524 s->vhost_vqs = NULL;
525 g_free(s->inflight);
526 s->inflight = NULL;
528 for (i = 0; i < s->num_queues; i++) {
529 virtio_delete_queue(s->virtqs[i]);
531 g_free(s->virtqs);
532 virtio_cleanup(vdev);
533 vhost_user_cleanup(&s->vhost_user);
536 static void vhost_user_blk_instance_init(Object *obj)
538 VHostUserBlk *s = VHOST_USER_BLK(obj);
540 device_add_bootindex_property(obj, &s->bootindex, "bootindex",
541 "/disk@0,0", DEVICE(obj));
544 static const VMStateDescription vmstate_vhost_user_blk = {
545 .name = "vhost-user-blk",
546 .minimum_version_id = 1,
547 .version_id = 1,
548 .fields = (VMStateField[]) {
549 VMSTATE_VIRTIO_DEVICE,
550 VMSTATE_END_OF_LIST()
554 static Property vhost_user_blk_properties[] = {
555 DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev),
556 DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues,
557 VHOST_USER_BLK_AUTO_NUM_QUEUES),
558 DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128),
559 DEFINE_PROP_BIT("config-wce", VHostUserBlk, config_wce, 0, true),
560 DEFINE_PROP_END_OF_LIST(),
563 static void vhost_user_blk_class_init(ObjectClass *klass, void *data)
565 DeviceClass *dc = DEVICE_CLASS(klass);
566 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
568 device_class_set_props(dc, vhost_user_blk_properties);
569 dc->vmsd = &vmstate_vhost_user_blk;
570 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
571 vdc->realize = vhost_user_blk_device_realize;
572 vdc->unrealize = vhost_user_blk_device_unrealize;
573 vdc->get_config = vhost_user_blk_update_config;
574 vdc->set_config = vhost_user_blk_set_config;
575 vdc->get_features = vhost_user_blk_get_features;
576 vdc->set_status = vhost_user_blk_set_status;
577 vdc->reset = vhost_user_blk_reset;
580 static const TypeInfo vhost_user_blk_info = {
581 .name = TYPE_VHOST_USER_BLK,
582 .parent = TYPE_VIRTIO_DEVICE,
583 .instance_size = sizeof(VHostUserBlk),
584 .instance_init = vhost_user_blk_instance_init,
585 .class_init = vhost_user_blk_class_init,
588 static void virtio_register_types(void)
590 type_register_static(&vhost_user_blk_info);
593 type_init(virtio_register_types)