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