xref: /openbmc/qemu/hw/scsi/vhost-user-scsi.c (revision bef6a77f)
1 /*
2  * vhost-user-scsi host device
3  *
4  * Copyright (c) 2016 Nutanix Inc. All rights reserved.
5  *
6  * Author:
7  *  Felipe Franciosi <felipe@nutanix.com>
8  *
9  * This work is largely based on the "vhost-scsi" implementation by:
10  *  Stefan Hajnoczi    <stefanha@linux.vnet.ibm.com>
11  *  Nicholas Bellinger <nab@risingtidesystems.com>
12  *
13  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
14  * See the COPYING.LIB file in the top-level directory.
15  *
16  */
17 
18 #include "qemu/osdep.h"
19 #include "qapi/error.h"
20 #include "qemu/error-report.h"
21 #include "hw/fw-path-provider.h"
22 #include "hw/qdev-core.h"
23 #include "hw/qdev-properties.h"
24 #include "hw/qdev-properties-system.h"
25 #include "hw/virtio/vhost.h"
26 #include "hw/virtio/vhost-backend.h"
27 #include "hw/virtio/vhost-user-scsi.h"
28 #include "hw/virtio/virtio.h"
29 #include "chardev/char-fe.h"
30 #include "sysemu/sysemu.h"
31 
32 /* Features supported by the host application */
33 static const int user_feature_bits[] = {
34     VIRTIO_F_NOTIFY_ON_EMPTY,
35     VIRTIO_RING_F_INDIRECT_DESC,
36     VIRTIO_RING_F_EVENT_IDX,
37     VIRTIO_SCSI_F_HOTPLUG,
38     VIRTIO_F_RING_RESET,
39     VIRTIO_F_NOTIFICATION_DATA,
40     VHOST_INVALID_FEATURE_BIT
41 };
42 
43 static int vhost_user_scsi_start(VHostUserSCSI *s, Error **errp)
44 {
45     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
46     int ret;
47 
48     ret = vhost_scsi_common_start(vsc, errp);
49     s->started_vu = !(ret < 0);
50 
51     return ret;
52 }
53 
54 static void vhost_user_scsi_stop(VHostUserSCSI *s)
55 {
56     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
57 
58     if (!s->started_vu) {
59         return;
60     }
61     s->started_vu = false;
62 
63     vhost_scsi_common_stop(vsc);
64 }
65 
66 static void vhost_user_scsi_set_status(VirtIODevice *vdev, uint8_t status)
67 {
68     VHostUserSCSI *s = (VHostUserSCSI *)vdev;
69     DeviceState *dev = DEVICE(vdev);
70     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
71     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
72     bool should_start = virtio_device_should_start(vdev, status);
73     Error *local_err = NULL;
74     int ret;
75 
76     if (!s->connected) {
77         return;
78     }
79 
80     if (vhost_dev_is_started(&vsc->dev) == should_start) {
81         return;
82     }
83 
84     if (should_start) {
85         ret = vhost_user_scsi_start(s, &local_err);
86         if (ret < 0) {
87             error_reportf_err(local_err,
88                               "unable to start vhost-user-scsi: %s: ",
89                               strerror(-ret));
90             qemu_chr_fe_disconnect(&vs->conf.chardev);
91         }
92     } else {
93         vhost_user_scsi_stop(s);
94     }
95 }
96 
97 static void vhost_user_scsi_handle_output(VirtIODevice *vdev, VirtQueue *vq)
98 {
99     VHostUserSCSI *s = (VHostUserSCSI *)vdev;
100     DeviceState *dev = DEVICE(vdev);
101     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
102     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
103 
104     Error *local_err = NULL;
105     int i, ret;
106 
107     if (!vdev->start_on_kick) {
108         return;
109     }
110 
111     if (!s->connected) {
112         return;
113     }
114 
115     if (vhost_dev_is_started(&vsc->dev)) {
116         return;
117     }
118 
119     /*
120      * Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start
121      * vhost here instead of waiting for .set_status().
122      */
123     ret = vhost_user_scsi_start(s, &local_err);
124     if (ret < 0) {
125         error_reportf_err(local_err, "vhost-user-scsi: vhost start failed: ");
126         qemu_chr_fe_disconnect(&vs->conf.chardev);
127         return;
128     }
129 
130     /* Kick right away to begin processing requests already in vring */
131     for (i = 0; i < vsc->dev.nvqs; i++) {
132         VirtQueue *kick_vq = virtio_get_queue(vdev, i);
133 
134         if (!virtio_queue_get_desc_addr(vdev, i)) {
135             continue;
136         }
137         event_notifier_set(virtio_queue_get_host_notifier(kick_vq));
138     }
139 }
140 
141 static int vhost_user_scsi_connect(DeviceState *dev, Error **errp)
142 {
143     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
144     VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
145     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
146     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
147     int ret = 0;
148 
149     if (s->connected) {
150         return 0;
151     }
152 
153     vsc->dev.num_queues = vs->conf.num_queues;
154     vsc->dev.nvqs = VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
155     vsc->dev.vqs = s->vhost_vqs;
156     vsc->dev.vq_index = 0;
157     vsc->dev.backend_features = 0;
158 
159     ret = vhost_dev_init(&vsc->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0,
160                          errp);
161     if (ret < 0) {
162         return ret;
163     }
164 
165     s->connected = true;
166 
167     /* restore vhost state */
168     if (virtio_device_started(vdev, vdev->status)) {
169         ret = vhost_user_scsi_start(s, errp);
170     }
171 
172     return ret;
173 }
174 
175 static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event);
176 
177 static void vhost_user_scsi_disconnect(DeviceState *dev)
178 {
179     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
180     VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
181     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
182     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
183 
184     if (!s->connected) {
185         goto done;
186     }
187     s->connected = false;
188 
189     vhost_user_scsi_stop(s);
190 
191     vhost_dev_cleanup(&vsc->dev);
192 
193 done:
194     /* Re-instate the event handler for new connections */
195     qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL,
196                              vhost_user_scsi_event, NULL, dev, NULL, true);
197 }
198 
199 static void vhost_user_scsi_event(void *opaque, QEMUChrEvent event)
200 {
201     DeviceState *dev = opaque;
202     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
203     VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
204     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
205     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
206     Error *local_err = NULL;
207 
208     switch (event) {
209     case CHR_EVENT_OPENED:
210         if (vhost_user_scsi_connect(dev, &local_err) < 0) {
211             error_report_err(local_err);
212             qemu_chr_fe_disconnect(&vs->conf.chardev);
213             return;
214         }
215         break;
216     case CHR_EVENT_CLOSED:
217         /* defer close until later to avoid circular close */
218         vhost_user_async_close(dev, &vs->conf.chardev, &vsc->dev,
219                                vhost_user_scsi_disconnect);
220         break;
221     case CHR_EVENT_BREAK:
222     case CHR_EVENT_MUX_IN:
223     case CHR_EVENT_MUX_OUT:
224         /* Ignore */
225         break;
226     }
227 }
228 
229 static int vhost_user_scsi_realize_connect(VHostUserSCSI *s, Error **errp)
230 {
231     DeviceState *dev = DEVICE(s);
232     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
233     int ret;
234 
235     s->connected = false;
236 
237     ret = qemu_chr_fe_wait_connected(&vs->conf.chardev, errp);
238     if (ret < 0) {
239         return ret;
240     }
241 
242     ret = vhost_user_scsi_connect(dev, errp);
243     if (ret < 0) {
244         qemu_chr_fe_disconnect(&vs->conf.chardev);
245         return ret;
246     }
247     assert(s->connected);
248 
249     return 0;
250 }
251 
252 static void vhost_user_scsi_realize(DeviceState *dev, Error **errp)
253 {
254     ERRP_GUARD();
255     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
256     VHostUserSCSI *s = VHOST_USER_SCSI(dev);
257     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
258     Error *err = NULL;
259     int ret;
260     int retries = VU_REALIZE_CONN_RETRIES;
261 
262     if (!vs->conf.chardev.chr) {
263         error_setg(errp, "vhost-user-scsi: missing chardev");
264         return;
265     }
266 
267     virtio_scsi_common_realize(dev, vhost_user_scsi_handle_output,
268                                vhost_user_scsi_handle_output,
269                                vhost_user_scsi_handle_output, &err);
270     if (err != NULL) {
271         error_propagate(errp, err);
272         return;
273     }
274 
275     if (!vhost_user_init(&s->vhost_user, &vs->conf.chardev, errp)) {
276         goto free_virtio;
277     }
278 
279     vsc->inflight = g_new0(struct vhost_inflight, 1);
280     s->vhost_vqs = g_new0(struct vhost_virtqueue,
281                           VIRTIO_SCSI_VQ_NUM_FIXED + vs->conf.num_queues);
282 
283     assert(!*errp);
284     do {
285         if (*errp) {
286             error_prepend(errp, "Reconnecting after error: ");
287             error_report_err(*errp);
288             *errp = NULL;
289         }
290         ret = vhost_user_scsi_realize_connect(s, errp);
291     } while (ret < 0 && retries--);
292 
293     if (ret < 0) {
294         goto free_vhost;
295     }
296 
297     /* we're fully initialized, now we can operate, so add the handler */
298     qemu_chr_fe_set_handlers(&vs->conf.chardev,  NULL, NULL,
299                              vhost_user_scsi_event, NULL, (void *)dev,
300                              NULL, true);
301     /* Channel and lun both are 0 for bootable vhost-user-scsi disk */
302     vsc->channel = 0;
303     vsc->lun = 0;
304     vsc->target = vs->conf.boot_tpgt;
305 
306     return;
307 
308 free_vhost:
309     g_free(s->vhost_vqs);
310     s->vhost_vqs = NULL;
311     g_free(vsc->inflight);
312     vsc->inflight = NULL;
313     vhost_user_cleanup(&s->vhost_user);
314 
315 free_virtio:
316     virtio_scsi_common_unrealize(dev);
317 }
318 
319 static void vhost_user_scsi_unrealize(DeviceState *dev)
320 {
321     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
322     VHostUserSCSI *s = VHOST_USER_SCSI(dev);
323     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
324     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
325 
326     /* This will stop the vhost backend. */
327     vhost_user_scsi_set_status(vdev, 0);
328     qemu_chr_fe_set_handlers(&vs->conf.chardev, NULL, NULL, NULL, NULL, NULL,
329                              NULL, false);
330 
331     vhost_dev_cleanup(&vsc->dev);
332     g_free(s->vhost_vqs);
333     s->vhost_vqs = NULL;
334 
335     vhost_dev_free_inflight(vsc->inflight);
336     g_free(vsc->inflight);
337     vsc->inflight = NULL;
338 
339     vhost_user_cleanup(&s->vhost_user);
340     virtio_scsi_common_unrealize(dev);
341 }
342 
343 static Property vhost_user_scsi_properties[] = {
344     DEFINE_PROP_CHR("chardev", VirtIOSCSICommon, conf.chardev),
345     DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0),
346     DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues,
347                        VIRTIO_SCSI_AUTO_NUM_QUEUES),
348     DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size,
349                        128),
350     DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors,
351                        0xFFFF),
352     DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128),
353     DEFINE_PROP_BIT64("hotplug", VHostSCSICommon, host_features,
354                                                   VIRTIO_SCSI_F_HOTPLUG,
355                                                   true),
356     DEFINE_PROP_BIT64("param_change", VHostSCSICommon, host_features,
357                                                        VIRTIO_SCSI_F_CHANGE,
358                                                        true),
359     DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features,
360                                                  VIRTIO_SCSI_F_T10_PI,
361                                                  false),
362     DEFINE_PROP_END_OF_LIST(),
363 };
364 
365 static void vhost_user_scsi_reset(VirtIODevice *vdev)
366 {
367     VHostUserSCSI *s = VHOST_USER_SCSI(vdev);
368     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
369 
370     vhost_dev_free_inflight(vsc->inflight);
371 }
372 
373 static struct vhost_dev *vhost_user_scsi_get_vhost(VirtIODevice *vdev)
374 {
375     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(vdev);
376     return &vsc->dev;
377 }
378 
379 static const VMStateDescription vmstate_vhost_scsi = {
380     .name = "virtio-scsi",
381     .minimum_version_id = 1,
382     .version_id = 1,
383     .fields = (const VMStateField[]) {
384         VMSTATE_VIRTIO_DEVICE,
385         VMSTATE_END_OF_LIST()
386     },
387 };
388 
389 static void vhost_user_scsi_class_init(ObjectClass *klass, void *data)
390 {
391     DeviceClass *dc = DEVICE_CLASS(klass);
392     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
393     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
394 
395     device_class_set_props(dc, vhost_user_scsi_properties);
396     dc->vmsd = &vmstate_vhost_scsi;
397     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
398     vdc->realize = vhost_user_scsi_realize;
399     vdc->unrealize = vhost_user_scsi_unrealize;
400     vdc->get_features = vhost_scsi_common_get_features;
401     vdc->set_config = vhost_scsi_common_set_config;
402     vdc->set_status = vhost_user_scsi_set_status;
403     fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
404     vdc->reset = vhost_user_scsi_reset;
405     vdc->get_vhost = vhost_user_scsi_get_vhost;
406 }
407 
408 static void vhost_user_scsi_instance_init(Object *obj)
409 {
410     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj);
411 
412     vsc->feature_bits = user_feature_bits;
413 
414     /* Add the bootindex property for this object */
415     device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL,
416                                   DEVICE(vsc));
417 }
418 
419 static const TypeInfo vhost_user_scsi_info = {
420     .name = TYPE_VHOST_USER_SCSI,
421     .parent = TYPE_VHOST_SCSI_COMMON,
422     .instance_size = sizeof(VHostUserSCSI),
423     .class_init = vhost_user_scsi_class_init,
424     .instance_init = vhost_user_scsi_instance_init,
425     .interfaces = (InterfaceInfo[]) {
426         { TYPE_FW_PATH_PROVIDER },
427         { }
428     },
429 };
430 
431 static void virtio_register_types(void)
432 {
433     type_register_static(&vhost_user_scsi_info);
434 }
435 
436 type_init(virtio_register_types)
437