xref: /openbmc/qemu/hw/block/vhost-user-blk.c (revision a89f364a)
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/typedefs.h"
23 #include "qemu/cutils.h"
24 #include "qom/object.h"
25 #include "hw/qdev-core.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 
32 static const int user_feature_bits[] = {
33     VIRTIO_BLK_F_SIZE_MAX,
34     VIRTIO_BLK_F_SEG_MAX,
35     VIRTIO_BLK_F_GEOMETRY,
36     VIRTIO_BLK_F_BLK_SIZE,
37     VIRTIO_BLK_F_TOPOLOGY,
38     VIRTIO_BLK_F_MQ,
39     VIRTIO_BLK_F_RO,
40     VIRTIO_BLK_F_FLUSH,
41     VIRTIO_BLK_F_CONFIG_WCE,
42     VIRTIO_F_VERSION_1,
43     VIRTIO_RING_F_INDIRECT_DESC,
44     VIRTIO_RING_F_EVENT_IDX,
45     VIRTIO_F_NOTIFY_ON_EMPTY,
46     VHOST_INVALID_FEATURE_BIT
47 };
48 
49 static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config)
50 {
51     VHostUserBlk *s = VHOST_USER_BLK(vdev);
52 
53     memcpy(config, &s->blkcfg, sizeof(struct virtio_blk_config));
54 }
55 
56 static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config)
57 {
58     VHostUserBlk *s = VHOST_USER_BLK(vdev);
59     struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config;
60     int ret;
61 
62     if (blkcfg->wce == s->blkcfg.wce) {
63         return;
64     }
65 
66     ret = vhost_dev_set_config(&s->dev, &blkcfg->wce,
67                                offsetof(struct virtio_blk_config, wce),
68                                sizeof(blkcfg->wce),
69                                VHOST_SET_CONFIG_TYPE_MASTER);
70     if (ret) {
71         error_report("set device config space failed");
72         return;
73     }
74 
75     s->blkcfg.wce = blkcfg->wce;
76 }
77 
78 static int vhost_user_blk_handle_config_change(struct vhost_dev *dev)
79 {
80     int ret;
81     struct virtio_blk_config blkcfg;
82     VHostUserBlk *s = VHOST_USER_BLK(dev->vdev);
83 
84     ret = vhost_dev_get_config(dev, (uint8_t *)&blkcfg,
85                                sizeof(struct virtio_blk_config));
86     if (ret < 0) {
87         error_report("get config space failed");
88         return -1;
89     }
90 
91     /* valid for resize only */
92     if (blkcfg.capacity != s->blkcfg.capacity) {
93         s->blkcfg.capacity = blkcfg.capacity;
94         memcpy(dev->vdev->config, &s->blkcfg, sizeof(struct virtio_blk_config));
95         virtio_notify_config(dev->vdev);
96     }
97 
98     return 0;
99 }
100 
101 const VhostDevConfigOps blk_ops = {
102     .vhost_dev_config_notifier = vhost_user_blk_handle_config_change,
103 };
104 
105 static void vhost_user_blk_start(VirtIODevice *vdev)
106 {
107     VHostUserBlk *s = VHOST_USER_BLK(vdev);
108     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
109     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
110     int i, ret;
111 
112     if (!k->set_guest_notifiers) {
113         error_report("binding does not support guest notifiers");
114         return;
115     }
116 
117     ret = vhost_dev_enable_notifiers(&s->dev, vdev);
118     if (ret < 0) {
119         error_report("Error enabling host notifiers: %d", -ret);
120         return;
121     }
122 
123     ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
124     if (ret < 0) {
125         error_report("Error binding guest notifier: %d", -ret);
126         goto err_host_notifiers;
127     }
128 
129     s->dev.acked_features = vdev->guest_features;
130     ret = vhost_dev_start(&s->dev, vdev);
131     if (ret < 0) {
132         error_report("Error starting vhost: %d", -ret);
133         goto err_guest_notifiers;
134     }
135 
136     /* guest_notifier_mask/pending not used yet, so just unmask
137      * everything here. virtio-pci will do the right thing by
138      * enabling/disabling irqfd.
139      */
140     for (i = 0; i < s->dev.nvqs; i++) {
141         vhost_virtqueue_mask(&s->dev, vdev, i, false);
142     }
143 
144     return;
145 
146 err_guest_notifiers:
147     k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
148 err_host_notifiers:
149     vhost_dev_disable_notifiers(&s->dev, vdev);
150 }
151 
152 static void vhost_user_blk_stop(VirtIODevice *vdev)
153 {
154     VHostUserBlk *s = VHOST_USER_BLK(vdev);
155     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
156     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
157     int ret;
158 
159     if (!k->set_guest_notifiers) {
160         return;
161     }
162 
163     vhost_dev_stop(&s->dev, vdev);
164 
165     ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
166     if (ret < 0) {
167         error_report("vhost guest notifier cleanup failed: %d", ret);
168         return;
169     }
170 
171     vhost_dev_disable_notifiers(&s->dev, vdev);
172 }
173 
174 static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status)
175 {
176     VHostUserBlk *s = VHOST_USER_BLK(vdev);
177     bool should_start = status & VIRTIO_CONFIG_S_DRIVER_OK;
178 
179     if (!vdev->vm_running) {
180         should_start = false;
181     }
182 
183     if (s->dev.started == should_start) {
184         return;
185     }
186 
187     if (should_start) {
188         vhost_user_blk_start(vdev);
189     } else {
190         vhost_user_blk_stop(vdev);
191     }
192 
193 }
194 
195 static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev,
196                                             uint64_t features,
197                                             Error **errp)
198 {
199     VHostUserBlk *s = VHOST_USER_BLK(vdev);
200     uint64_t get_features;
201 
202     /* Turn on pre-defined features */
203     virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX);
204     virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY);
205     virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY);
206     virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE);
207     virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH);
208 
209     if (s->config_wce) {
210         virtio_add_feature(&features, VIRTIO_BLK_F_CONFIG_WCE);
211     }
212     if (s->config_ro) {
213         virtio_add_feature(&features, VIRTIO_BLK_F_RO);
214     }
215     if (s->num_queues > 1) {
216         virtio_add_feature(&features, VIRTIO_BLK_F_MQ);
217     }
218 
219     get_features = vhost_get_features(&s->dev, user_feature_bits, features);
220 
221     return get_features;
222 }
223 
224 static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq)
225 {
226 
227 }
228 
229 static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp)
230 {
231     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
232     VHostUserBlk *s = VHOST_USER_BLK(vdev);
233     int i, ret;
234 
235     if (!s->chardev.chr) {
236         error_setg(errp, "vhost-user-blk: chardev is mandatory");
237         return;
238     }
239 
240     if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) {
241         error_setg(errp, "vhost-user-blk: invalid number of IO queues");
242         return;
243     }
244 
245     if (!s->queue_size) {
246         error_setg(errp, "vhost-user-blk: queue size must be non-zero");
247         return;
248     }
249 
250     virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK,
251                 sizeof(struct virtio_blk_config));
252 
253     for (i = 0; i < s->num_queues; i++) {
254         virtio_add_queue(vdev, s->queue_size,
255                          vhost_user_blk_handle_output);
256     }
257 
258     s->dev.nvqs = s->num_queues;
259     s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs);
260     s->dev.vq_index = 0;
261     s->dev.backend_features = 0;
262 
263     ret = vhost_dev_init(&s->dev, &s->chardev, VHOST_BACKEND_TYPE_USER, 0);
264     if (ret < 0) {
265         error_setg(errp, "vhost-user-blk: vhost initialization failed: %s",
266                    strerror(-ret));
267         goto virtio_err;
268     }
269 
270     ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg,
271                               sizeof(struct virtio_blk_config));
272     if (ret < 0) {
273         error_setg(errp, "vhost-user-blk: get block config failed");
274         goto vhost_err;
275     }
276 
277     if (s->blkcfg.num_queues != s->num_queues) {
278         s->blkcfg.num_queues = s->num_queues;
279     }
280 
281     vhost_dev_set_config_notifier(&s->dev, &blk_ops);
282 
283     return;
284 
285 vhost_err:
286     vhost_dev_cleanup(&s->dev);
287 virtio_err:
288     g_free(s->dev.vqs);
289     virtio_cleanup(vdev);
290 }
291 
292 static void vhost_user_blk_device_unrealize(DeviceState *dev, Error **errp)
293 {
294     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
295     VHostUserBlk *s = VHOST_USER_BLK(dev);
296 
297     vhost_user_blk_set_status(vdev, 0);
298     vhost_dev_cleanup(&s->dev);
299     g_free(s->dev.vqs);
300     virtio_cleanup(vdev);
301 }
302 
303 static void vhost_user_blk_instance_init(Object *obj)
304 {
305     VHostUserBlk *s = VHOST_USER_BLK(obj);
306 
307     device_add_bootindex_property(obj, &s->bootindex, "bootindex",
308                                   "/disk@0,0", DEVICE(obj), NULL);
309 }
310 
311 static const VMStateDescription vmstate_vhost_user_blk = {
312     .name = "vhost-user-blk",
313     .minimum_version_id = 1,
314     .version_id = 1,
315     .fields = (VMStateField[]) {
316         VMSTATE_VIRTIO_DEVICE,
317         VMSTATE_END_OF_LIST()
318     },
319 };
320 
321 static Property vhost_user_blk_properties[] = {
322     DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev),
323     DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues, 1),
324     DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128),
325     DEFINE_PROP_BIT("config-wce", VHostUserBlk, config_wce, 0, true),
326     DEFINE_PROP_BIT("config-ro", VHostUserBlk, config_ro, 0, false),
327     DEFINE_PROP_END_OF_LIST(),
328 };
329 
330 static void vhost_user_blk_class_init(ObjectClass *klass, void *data)
331 {
332     DeviceClass *dc = DEVICE_CLASS(klass);
333     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
334 
335     dc->props = vhost_user_blk_properties;
336     dc->vmsd = &vmstate_vhost_user_blk;
337     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
338     vdc->realize = vhost_user_blk_device_realize;
339     vdc->unrealize = vhost_user_blk_device_unrealize;
340     vdc->get_config = vhost_user_blk_update_config;
341     vdc->set_config = vhost_user_blk_set_config;
342     vdc->get_features = vhost_user_blk_get_features;
343     vdc->set_status = vhost_user_blk_set_status;
344 }
345 
346 static const TypeInfo vhost_user_blk_info = {
347     .name = TYPE_VHOST_USER_BLK,
348     .parent = TYPE_VIRTIO_DEVICE,
349     .instance_size = sizeof(VHostUserBlk),
350     .instance_init = vhost_user_blk_instance_init,
351     .class_init = vhost_user_blk_class_init,
352 };
353 
354 static void virtio_register_types(void)
355 {
356     type_register_static(&vhost_user_blk_info);
357 }
358 
359 type_init(virtio_register_types)
360