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