cpu: Move CPUClass::get_memory_mapping to SysemuCPUOps
[qemu/ar7.git] / hw / block / vhost-user-blk.c
blobc6210fad0c1359039638f4f084f9e20b2bf25bb3
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 VIRTIO_F_RING_PACKED,
51 VIRTIO_F_IOMMU_PLATFORM,
52 VHOST_INVALID_FEATURE_BIT
55 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event);
57 static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config)
59 VHostUserBlk *s = VHOST_USER_BLK(vdev);
61 /* Our num_queues overrides the device backend */
62 virtio_stw_p(vdev, &s->blkcfg.num_queues, s->num_queues);
64 memcpy(config, &s->blkcfg, sizeof(struct virtio_blk_config));
67 static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
69 VHostUserBlk *s = VHOST_USER_BLK(vdev);
70 struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config;
71 int ret;
73 if (blkcfg->wce == s->blkcfg.wce) {
74 return;
77 ret = vhost_dev_set_config(&s->dev, &blkcfg->wce,
78 offsetof(struct virtio_blk_config, wce),
79 sizeof(blkcfg->wce),
80 VHOST_SET_CONFIG_TYPE_MASTER);
81 if (ret) {
82 error_report("set device config space failed");
83 return;
86 s->blkcfg.wce = blkcfg->wce;
89 static int vhost_user_blk_handle_config_change(struct vhost_dev *dev)
91 int ret;
92 struct virtio_blk_config blkcfg;
93 VHostUserBlk *s = VHOST_USER_BLK(dev->vdev);
95 ret = vhost_dev_get_config(dev, (uint8_t *)&blkcfg,
96 sizeof(struct virtio_blk_config));
97 if (ret < 0) {
98 error_report("get config space failed");
99 return -1;
102 /* valid for resize only */
103 if (blkcfg.capacity != s->blkcfg.capacity) {
104 s->blkcfg.capacity = blkcfg.capacity;
105 memcpy(dev->vdev->config, &s->blkcfg, sizeof(struct virtio_blk_config));
106 virtio_notify_config(dev->vdev);
109 return 0;
112 const VhostDevConfigOps blk_ops = {
113 .vhost_dev_config_notifier = vhost_user_blk_handle_config_change,
116 static int vhost_user_blk_start(VirtIODevice *vdev)
118 VHostUserBlk *s = VHOST_USER_BLK(vdev);
119 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
120 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
121 int i, ret;
123 if (!k->set_guest_notifiers) {
124 error_report("binding does not support guest notifiers");
125 return -ENOSYS;
128 ret = vhost_dev_enable_notifiers(&s->dev, vdev);
129 if (ret < 0) {
130 error_report("Error enabling host notifiers: %d", -ret);
131 return ret;
134 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
135 if (ret < 0) {
136 error_report("Error binding guest notifier: %d", -ret);
137 goto err_host_notifiers;
140 s->dev.acked_features = vdev->guest_features;
142 ret = vhost_dev_prepare_inflight(&s->dev, vdev);
143 if (ret < 0) {
144 error_report("Error set inflight format: %d", -ret);
145 goto err_guest_notifiers;
148 if (!s->inflight->addr) {
149 ret = vhost_dev_get_inflight(&s->dev, s->queue_size, s->inflight);
150 if (ret < 0) {
151 error_report("Error get inflight: %d", -ret);
152 goto err_guest_notifiers;
156 ret = vhost_dev_set_inflight(&s->dev, s->inflight);
157 if (ret < 0) {
158 error_report("Error set inflight: %d", -ret);
159 goto err_guest_notifiers;
162 ret = vhost_dev_start(&s->dev, vdev);
163 if (ret < 0) {
164 error_report("Error starting vhost: %d", -ret);
165 goto err_guest_notifiers;
167 s->started_vu = true;
169 /* guest_notifier_mask/pending not used yet, so just unmask
170 * everything here. virtio-pci will do the right thing by
171 * enabling/disabling irqfd.
173 for (i = 0; i < s->dev.nvqs; i++) {
174 vhost_virtqueue_mask(&s->dev, vdev, i, false);
177 return ret;
179 err_guest_notifiers:
180 k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
181 err_host_notifiers:
182 vhost_dev_disable_notifiers(&s->dev, vdev);
183 return ret;
186 static void vhost_user_blk_stop(VirtIODevice *vdev)
188 VHostUserBlk *s = VHOST_USER_BLK(vdev);
189 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
190 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
191 int ret;
193 if (!s->started_vu) {
194 return;
196 s->started_vu = false;
198 if (!k->set_guest_notifiers) {
199 return;
202 vhost_dev_stop(&s->dev, vdev);
204 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
205 if (ret < 0) {
206 error_report("vhost guest notifier cleanup failed: %d", ret);
207 return;
210 vhost_dev_disable_notifiers(&s->dev, vdev);
213 static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status)
215 VHostUserBlk *s = VHOST_USER_BLK(vdev);
216 bool should_start = virtio_device_started(vdev, status);
217 int ret;
219 if (!vdev->vm_running) {
220 should_start = false;
223 if (!s->connected) {
224 return;
227 if (s->dev.started == should_start) {
228 return;
231 if (should_start) {
232 ret = vhost_user_blk_start(vdev);
233 if (ret < 0) {
234 error_report("vhost-user-blk: vhost start failed: %s",
235 strerror(-ret));
236 qemu_chr_fe_disconnect(&s->chardev);
238 } else {
239 vhost_user_blk_stop(vdev);
244 static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev,
245 uint64_t features,
246 Error **errp)
248 VHostUserBlk *s = VHOST_USER_BLK(vdev);
250 /* Turn on pre-defined features */
251 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
252 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
253 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
254 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
255 virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH);
256 virtio_add_feature(&features, VIRTIO_BLK_F_RO);
257 virtio_add_feature(&features, VIRTIO_BLK_F_DISCARD);
258 virtio_add_feature(&features, VIRTIO_BLK_F_WRITE_ZEROES);
260 if (s->config_wce) {
261 virtio_add_feature(&features, VIRTIO_BLK_F_CONFIG_WCE);
263 if (s->num_queues > 1) {
264 virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
267 return vhost_get_features(&s->dev, user_feature_bits, features);
270 static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
272 VHostUserBlk *s = VHOST_USER_BLK(vdev);
273 int i, ret;
275 if (!vdev->start_on_kick) {
276 return;
279 if (!s->connected) {
280 return;
283 if (s->dev.started) {
284 return;
287 /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
288 * vhost here instead of waiting for .set_status().
290 ret = vhost_user_blk_start(vdev);
291 if (ret < 0) {
292 error_report("vhost-user-blk: vhost start failed: %s",
293 strerror(-ret));
294 qemu_chr_fe_disconnect(&s->chardev);
295 return;
298 /* Kick right away to begin processing requests already in vring */
299 for (i = 0; i < s->dev.nvqs; i++) {
300 VirtQueue *kick_vq = virtio_get_queue(vdev, i);
302 if (!virtio_queue_get_desc_addr(vdev, i)) {
303 continue;
305 event_notifier_set(virtio_queue_get_host_notifier(kick_vq));
309 static void vhost_user_blk_reset(VirtIODevice *vdev)
311 VHostUserBlk *s = VHOST_USER_BLK(vdev);
313 vhost_dev_free_inflight(s->inflight);
316 static int vhost_user_blk_connect(DeviceState *dev, Error **errp)
318 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
319 VHostUserBlk *s = VHOST_USER_BLK(vdev);
320 int ret = 0;
322 if (s->connected) {
323 return 0;
325 s->connected = true;
327 s->dev.num_queues = s->num_queues;
328 s->dev.nvqs = s->num_queues;
329 s->dev.vqs = s->vhost_vqs;
330 s->dev.vq_index = 0;
331 s->dev.backend_features = 0;
333 vhost_dev_set_config_notifier(&s->dev, &blk_ops);
335 ret = vhost_dev_init(&s->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0);
336 if (ret < 0) {
337 error_setg_errno(errp, -ret, "vhost initialization failed");
338 return ret;
341 /* restore vhost state */
342 if (virtio_device_started(vdev, vdev->status)) {
343 ret = vhost_user_blk_start(vdev);
344 if (ret < 0) {
345 error_setg_errno(errp, -ret, "vhost start failed");
346 return ret;
350 return 0;
353 static void vhost_user_blk_disconnect(DeviceState *dev)
355 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
356 VHostUserBlk *s = VHOST_USER_BLK(vdev);
358 if (!s->connected) {
359 return;
361 s->connected = false;
363 vhost_user_blk_stop(vdev);
365 vhost_dev_cleanup(&s->dev);
368 static void vhost_user_blk_chr_closed_bh(void *opaque)
370 DeviceState *dev = opaque;
371 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
372 VHostUserBlk *s = VHOST_USER_BLK(vdev);
374 vhost_user_blk_disconnect(dev);
375 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event,
376 NULL, opaque, NULL, true);
379 static void vhost_user_blk_event(void *opaque, QEMUChrEvent event)
381 DeviceState *dev = opaque;
382 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
383 VHostUserBlk *s = VHOST_USER_BLK(vdev);
384 Error *local_err = NULL;
386 switch (event) {
387 case CHR_EVENT_OPENED:
388 if (vhost_user_blk_connect(dev, &local_err) < 0) {
389 error_report_err(local_err);
390 qemu_chr_fe_disconnect(&s->chardev);
391 return;
393 break;
394 case CHR_EVENT_CLOSED:
395 if (!runstate_check(RUN_STATE_SHUTDOWN)) {
397 * A close event may happen during a read/write, but vhost
398 * code assumes the vhost_dev remains setup, so delay the
399 * stop & clear.
401 AioContext *ctx = qemu_get_current_aio_context();
403 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL, NULL,
404 NULL, NULL, false);
405 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;
416 break;
417 case CHR_EVENT_BREAK:
418 case CHR_EVENT_MUX_IN:
419 case CHR_EVENT_MUX_OUT:
420 /* Ignore */
421 break;
425 static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
427 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
428 VHostUserBlk *s = VHOST_USER_BLK(vdev);
429 int i, ret;
431 if (!s->chardev.chr) {
432 error_setg(errp, "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, "invalid number of IO queues");
441 return;
444 if (!s->queue_size) {
445 error_setg(errp, "queue size must be non-zero");
446 return;
448 if (s->queue_size > VIRTQUEUE_MAX_SIZE) {
449 error_setg(errp, "queue size must not exceed %d",
450 VIRTQUEUE_MAX_SIZE);
451 return;
454 if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) {
455 return;
458 virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
459 sizeof(struct virtio_blk_config));
461 s->virtqs = g_new(VirtQueue *, s->num_queues);
462 for (i = 0; i < s->num_queues; i++) {
463 s->virtqs[i] = virtio_add_queue(vdev, s->queue_size,
464 vhost_user_blk_handle_output);
467 s->inflight = g_new0(struct vhost_inflight, 1);
468 s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues);
469 s->connected = false;
471 if (qemu_chr_fe_wait_connected(&s->chardev, errp) < 0) {
472 goto virtio_err;
475 if (vhost_user_blk_connect(dev, errp) < 0) {
476 qemu_chr_fe_disconnect(&s->chardev);
477 goto virtio_err;
479 assert(s->connected);
481 ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
482 sizeof(struct virtio_blk_config));
483 if (ret < 0) {
484 error_setg(errp, "vhost-user-blk: get block config failed");
485 goto vhost_err;
488 /* we're fully initialized, now we can operate, so add the handler */
489 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL,
490 vhost_user_blk_event, NULL, (void *)dev,
491 NULL, true);
492 return;
494 vhost_err:
495 vhost_dev_cleanup(&s->dev);
496 virtio_err:
497 g_free(s->vhost_vqs);
498 s->vhost_vqs = NULL;
499 g_free(s->inflight);
500 s->inflight = NULL;
501 for (i = 0; i < s->num_queues; i++) {
502 virtio_delete_queue(s->virtqs[i]);
504 g_free(s->virtqs);
505 virtio_cleanup(vdev);
506 vhost_user_cleanup(&s->vhost_user);
509 static void vhost_user_blk_device_unrealize(DeviceState *dev)
511 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
512 VHostUserBlk *s = VHOST_USER_BLK(dev);
513 int i;
515 virtio_set_status(vdev, 0);
516 qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL,
517 NULL, NULL, NULL, false);
518 vhost_dev_cleanup(&s->dev);
519 vhost_dev_free_inflight(s->inflight);
520 g_free(s->vhost_vqs);
521 s->vhost_vqs = NULL;
522 g_free(s->inflight);
523 s->inflight = NULL;
525 for (i = 0; i < s->num_queues; i++) {
526 virtio_delete_queue(s->virtqs[i]);
528 g_free(s->virtqs);
529 virtio_cleanup(vdev);
530 vhost_user_cleanup(&s->vhost_user);
533 static void vhost_user_blk_instance_init(Object *obj)
535 VHostUserBlk *s = VHOST_USER_BLK(obj);
537 device_add_bootindex_property(obj, &s->bootindex, "bootindex",
538 "/disk@0,0", DEVICE(obj));
541 static const VMStateDescription vmstate_vhost_user_blk = {
542 .name = "vhost-user-blk",
543 .minimum_version_id = 1,
544 .version_id = 1,
545 .fields = (VMStateField[]) {
546 VMSTATE_VIRTIO_DEVICE,
547 VMSTATE_END_OF_LIST()
551 static Property vhost_user_blk_properties[] = {
552 DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev),
553 DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues,
554 VHOST_USER_BLK_AUTO_NUM_QUEUES),
555 DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128),
556 DEFINE_PROP_BIT("config-wce", VHostUserBlk, config_wce, 0, true),
557 DEFINE_PROP_END_OF_LIST(),
560 static void vhost_user_blk_class_init(ObjectClass *klass, void *data)
562 DeviceClass *dc = DEVICE_CLASS(klass);
563 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
565 device_class_set_props(dc, vhost_user_blk_properties);
566 dc->vmsd = &vmstate_vhost_user_blk;
567 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
568 vdc->realize = vhost_user_blk_device_realize;
569 vdc->unrealize = vhost_user_blk_device_unrealize;
570 vdc->get_config = vhost_user_blk_update_config;
571 vdc->set_config = vhost_user_blk_set_config;
572 vdc->get_features = vhost_user_blk_get_features;
573 vdc->set_status = vhost_user_blk_set_status;
574 vdc->reset = vhost_user_blk_reset;
577 static const TypeInfo vhost_user_blk_info = {
578 .name = TYPE_VHOST_USER_BLK,
579 .parent = TYPE_VIRTIO_DEVICE,
580 .instance_size = sizeof(VHostUserBlk),
581 .instance_init = vhost_user_blk_instance_init,
582 .class_init = vhost_user_blk_class_init,
585 static void virtio_register_types(void)
587 type_register_static(&vhost_user_blk_info);
590 type_init(virtio_register_types)