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