2 * vhost-user-blk host device
4 * Copyright(C) 2017 Intel Corporation.
9 * Largely based on the "vhost-user-scsi.c" and "vhost-scsi.c" implemented by:
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 "qom/object.h"
24 #include "hw/qdev-core.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"
31 static const int user_feature_bits[] = {
32 VIRTIO_BLK_F_SIZE_MAX,
34 VIRTIO_BLK_F_GEOMETRY,
35 VIRTIO_BLK_F_BLK_SIZE,
36 VIRTIO_BLK_F_TOPOLOGY,
40 VIRTIO_BLK_F_CONFIG_WCE,
42 VIRTIO_RING_F_INDIRECT_DESC,
43 VIRTIO_RING_F_EVENT_IDX,
44 VIRTIO_F_NOTIFY_ON_EMPTY,
45 VHOST_INVALID_FEATURE_BIT
48 static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config)
50 VHostUserBlk *s = VHOST_USER_BLK(vdev);
52 memcpy(config, &s->blkcfg, sizeof(struct virtio_blk_config));
55 static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
57 VHostUserBlk *s = VHOST_USER_BLK(vdev);
58 struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config;
61 if (blkcfg->wce == s->blkcfg.wce) {
65 ret = vhost_dev_set_config(&s->dev, &blkcfg->wce,
66 offsetof(struct virtio_blk_config, wce),
68 VHOST_SET_CONFIG_TYPE_MASTER);
70 error_report("set device config space failed");
74 s->blkcfg.wce = blkcfg->wce;
77 static int vhost_user_blk_handle_config_change(struct vhost_dev *dev)
80 struct virtio_blk_config blkcfg;
81 VHostUserBlk *s = VHOST_USER_BLK(dev->vdev);
83 ret = vhost_dev_get_config(dev, (uint8_t *)&blkcfg,
84 sizeof(struct virtio_blk_config));
86 error_report("get config space failed");
90 /* valid for resize only */
91 if (blkcfg.capacity != s->blkcfg.capacity) {
92 s->blkcfg.capacity = blkcfg.capacity;
93 memcpy(dev->vdev->config, &s->blkcfg, sizeof(struct virtio_blk_config));
94 virtio_notify_config(dev->vdev);
100 const VhostDevConfigOps blk_ops = {
101 .vhost_dev_config_notifier = vhost_user_blk_handle_config_change,
104 static void vhost_user_blk_start(VirtIODevice *vdev)
106 VHostUserBlk *s = VHOST_USER_BLK(vdev);
107 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
108 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
111 if (!k->set_guest_notifiers) {
112 error_report("binding does not support guest notifiers");
116 ret = vhost_dev_enable_notifiers(&s->dev, vdev);
118 error_report("Error enabling host notifiers: %d", -ret);
122 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
124 error_report("Error binding guest notifier: %d", -ret);
125 goto err_host_notifiers;
128 s->dev.acked_features = vdev->guest_features;
129 ret = vhost_dev_start(&s->dev, vdev);
131 error_report("Error starting vhost: %d", -ret);
132 goto err_guest_notifiers;
135 /* guest_notifier_mask/pending not used yet, so just unmask
136 * everything here. virtio-pci will do the right thing by
137 * enabling/disabling irqfd.
139 for (i = 0; i < s->dev.nvqs; i++) {
140 vhost_virtqueue_mask(&s->dev, vdev, i, false);
146 k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
148 vhost_dev_disable_notifiers(&s->dev, vdev);
151 static void vhost_user_blk_stop(VirtIODevice *vdev)
153 VHostUserBlk *s = VHOST_USER_BLK(vdev);
154 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
155 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
158 if (!k->set_guest_notifiers) {
162 vhost_dev_stop(&s->dev, vdev);
164 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
166 error_report("vhost guest notifier cleanup failed: %d", ret);
170 vhost_dev_disable_notifiers(&s->dev, vdev);
173 static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status)
175 VHostUserBlk *s = VHOST_USER_BLK(vdev);
176 bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK;
178 if (!vdev->vm_running) {
179 should_start = false;
182 if (s->dev.started == should_start) {
187 vhost_user_blk_start(vdev);
189 vhost_user_blk_stop(vdev);
194 static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev,
198 VHostUserBlk *s = VHOST_USER_BLK(vdev);
199 uint64_t get_features;
201 /* Turn on pre-defined features */
202 virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
203 virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
204 virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
205 virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
206 virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH);
209 virtio_add_feature(&features, VIRTIO_BLK_F_CONFIG_WCE);
212 virtio_add_feature(&features, VIRTIO_BLK_F_RO);
214 if (s->num_queues > 1) {
215 virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
218 get_features = vhost_get_features(&s->dev, user_feature_bits, features);
223 static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
228 static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
230 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
231 VHostUserBlk *s = VHOST_USER_BLK(vdev);
234 if (!s->chardev.chr) {
235 error_setg(errp, "vhost-user-blk: chardev is mandatory");
239 if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) {
240 error_setg(errp, "vhost-user-blk: invalid number of IO queues");
244 if (!s->queue_size) {
245 error_setg(errp, "vhost-user-blk: queue size must be non-zero");
249 virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
250 sizeof(struct virtio_blk_config));
252 for (i = 0; i < s->num_queues; i++) {
253 virtio_add_queue(vdev, s->queue_size,
254 vhost_user_blk_handle_output);
257 s->dev.nvqs = s->num_queues;
258 s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs);
260 s->dev.backend_features = 0;
262 ret = vhost_dev_init(&s->dev, &s->chardev, VHOST_BACKEND_TYPE_USER, 0);
264 error_setg(errp, "vhost-user-blk: vhost initialization failed: %s",
269 ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
270 sizeof(struct virtio_blk_config));
272 error_setg(errp, "vhost-user-blk: get block config failed");
276 if (s->blkcfg.num_queues != s->num_queues) {
277 s->blkcfg.num_queues = s->num_queues;
280 vhost_dev_set_config_notifier(&s->dev, &blk_ops);
285 vhost_dev_cleanup(&s->dev);
288 virtio_cleanup(vdev);
291 static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
293 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
294 VHostUserBlk *s = VHOST_USER_BLK(dev);
296 vhost_user_blk_set_status(vdev, 0);
297 vhost_dev_cleanup(&s->dev);
299 virtio_cleanup(vdev);
302 static void vhost_user_blk_instance_init(Object *obj)
304 VHostUserBlk *s = VHOST_USER_BLK(obj);
306 device_add_bootindex_property(obj, &s->bootindex, "bootindex",
307 "/disk@0,0", DEVICE(obj), NULL);
310 static const VMStateDescription vmstate_vhost_user_blk = {
311 .name = "vhost-user-blk",
312 .minimum_version_id = 1,
314 .fields = (VMStateField[]) {
315 VMSTATE_VIRTIO_DEVICE,
316 VMSTATE_END_OF_LIST()
320 static Property vhost_user_blk_properties[] = {
321 DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev),
322 DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues, 1),
323 DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128),
324 DEFINE_PROP_BIT("config-wce", VHostUserBlk, config_wce, 0, true),
325 DEFINE_PROP_BIT("config-ro", VHostUserBlk, config_ro, 0, false),
326 DEFINE_PROP_END_OF_LIST(),
329 static void vhost_user_blk_class_init(ObjectClass *klass, void *data)
331 DeviceClass *dc = DEVICE_CLASS(klass);
332 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
334 dc->props = vhost_user_blk_properties;
335 dc->vmsd = &vmstate_vhost_user_blk;
336 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
337 vdc->realize = vhost_user_blk_device_realize;
338 vdc->unrealize = vhost_user_blk_device_unrealize;
339 vdc->get_config = vhost_user_blk_update_config;
340 vdc->set_config = vhost_user_blk_set_config;
341 vdc->get_features = vhost_user_blk_get_features;
342 vdc->set_status = vhost_user_blk_set_status;
345 static const TypeInfo vhost_user_blk_info = {
346 .name = TYPE_VHOST_USER_BLK,
347 .parent = TYPE_VIRTIO_DEVICE,
348 .instance_size = sizeof(VHostUserBlk),
349 .instance_init = vhost_user_blk_instance_init,
350 .class_init = vhost_user_blk_class_init,
353 static void virtio_register_types(void)
355 type_register_static(&vhost_user_blk_info);
358 type_init(virtio_register_types)