xref: /openbmc/qemu/hw/scsi/vhost-scsi.c (revision 523e45ac)
1 /*
2  * vhost_scsi host device
3  *
4  * Copyright IBM, Corp. 2011
5  *
6  * Authors:
7  *  Stefan Hajnoczi   <stefanha@linux.vnet.ibm.com>
8  *
9  * Changes for QEMU mainline + tcm_vhost kernel upstream:
10  *  Nicholas Bellinger <nab@risingtidesystems.com>
11  *
12  * This work is licensed under the terms of the GNU LGPL, version 2 or later.
13  * See the COPYING.LIB file in the top-level directory.
14  *
15  */
16 
17 #include "qemu/osdep.h"
18 #include <linux/vhost.h>
19 #include <sys/ioctl.h>
20 #include "qapi/error.h"
21 #include "qemu/error-report.h"
22 #include "qemu/module.h"
23 #include "monitor/monitor.h"
24 #include "migration/blocker.h"
25 #include "hw/virtio/vhost-scsi.h"
26 #include "hw/virtio/vhost.h"
27 #include "hw/virtio/virtio-scsi.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "hw/fw-path-provider.h"
30 #include "hw/qdev-properties.h"
31 #include "qemu/cutils.h"
32 #include "sysemu/sysemu.h"
33 
34 /* Features supported by host kernel. */
35 static const int kernel_feature_bits[] = {
36     VIRTIO_F_NOTIFY_ON_EMPTY,
37     VIRTIO_RING_F_INDIRECT_DESC,
38     VIRTIO_RING_F_EVENT_IDX,
39     VIRTIO_SCSI_F_HOTPLUG,
40     VIRTIO_F_RING_RESET,
41     VIRTIO_F_IN_ORDER,
42     VIRTIO_F_NOTIFICATION_DATA,
43     VHOST_INVALID_FEATURE_BIT
44 };
45 
46 static int vhost_scsi_set_endpoint(VHostSCSI *s)
47 {
48     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
49     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
50     const VhostOps *vhost_ops = vsc->dev.vhost_ops;
51     struct vhost_scsi_target backend;
52     int ret;
53 
54     memset(&backend, 0, sizeof(backend));
55     pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
56     ret = vhost_ops->vhost_scsi_set_endpoint(&vsc->dev, &backend);
57     if (ret < 0) {
58         return -errno;
59     }
60     return 0;
61 }
62 
63 static void vhost_scsi_clear_endpoint(VHostSCSI *s)
64 {
65     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
66     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
67     struct vhost_scsi_target backend;
68     const VhostOps *vhost_ops = vsc->dev.vhost_ops;
69 
70     memset(&backend, 0, sizeof(backend));
71     pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
72     vhost_ops->vhost_scsi_clear_endpoint(&vsc->dev, &backend);
73 }
74 
75 static int vhost_scsi_start(VHostSCSI *s)
76 {
77     int ret, abi_version;
78     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
79     const VhostOps *vhost_ops = vsc->dev.vhost_ops;
80     Error *local_err = NULL;
81 
82     ret = vhost_ops->vhost_scsi_get_abi_version(&vsc->dev, &abi_version);
83     if (ret < 0) {
84         return -errno;
85     }
86     if (abi_version > VHOST_SCSI_ABI_VERSION) {
87         error_report("vhost-scsi: The running tcm_vhost kernel abi_version:"
88                      " %d is greater than vhost_scsi userspace supports: %d,"
89                      " please upgrade your version of QEMU", abi_version,
90                      VHOST_SCSI_ABI_VERSION);
91         return -ENOSYS;
92     }
93 
94     ret = vhost_scsi_common_start(vsc, &local_err);
95     if (ret < 0) {
96         error_reportf_err(local_err, "Error starting vhost-scsi: ");
97         return ret;
98     }
99 
100     ret = vhost_scsi_set_endpoint(s);
101     if (ret < 0) {
102         error_report("Error setting vhost-scsi endpoint");
103         vhost_scsi_common_stop(vsc);
104     }
105 
106     return ret;
107 }
108 
109 static void vhost_scsi_stop(VHostSCSI *s)
110 {
111     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
112 
113     vhost_scsi_clear_endpoint(s);
114     vhost_scsi_common_stop(vsc);
115 }
116 
117 static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val)
118 {
119     VHostSCSI *s = VHOST_SCSI(vdev);
120     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
121     bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK);
122 
123     if (!vdev->vm_running) {
124         start = false;
125     }
126 
127     if (vhost_dev_is_started(&vsc->dev) == start) {
128         return;
129     }
130 
131     if (start) {
132         int ret;
133 
134         ret = vhost_scsi_start(s);
135         if (ret < 0) {
136             error_report("unable to start vhost-scsi: %s", strerror(-ret));
137             exit(1);
138         }
139     } else {
140         vhost_scsi_stop(s);
141     }
142 }
143 
144 static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
145 {
146 }
147 
148 static int vhost_scsi_pre_save(void *opaque)
149 {
150     VHostSCSICommon *vsc = opaque;
151 
152     /* At this point, backend must be stopped, otherwise
153      * it might keep writing to memory. */
154     assert(!vhost_dev_is_started(&vsc->dev));
155 
156     return 0;
157 }
158 
159 static const VMStateDescription vmstate_virtio_vhost_scsi = {
160     .name = "virtio-vhost_scsi",
161     .minimum_version_id = 1,
162     .version_id = 1,
163     .fields = (const VMStateField[]) {
164         VMSTATE_VIRTIO_DEVICE,
165         VMSTATE_END_OF_LIST()
166     },
167     .pre_save = vhost_scsi_pre_save,
168 };
169 
170 static int vhost_scsi_set_workers(VHostSCSICommon *vsc, bool per_virtqueue)
171 {
172     struct vhost_dev *dev = &vsc->dev;
173     struct vhost_vring_worker vq_worker;
174     struct vhost_worker_state worker;
175     int i, ret;
176 
177     /* Use default worker */
178     if (!per_virtqueue || dev->nvqs == VHOST_SCSI_VQ_NUM_FIXED + 1) {
179         return 0;
180     }
181 
182     /*
183      * ctl/evt share the first worker since it will be rare for them
184      * to send cmds while IO is running.
185      */
186     for (i = VHOST_SCSI_VQ_NUM_FIXED + 1; i < dev->nvqs; i++) {
187         memset(&worker, 0, sizeof(worker));
188 
189         ret = dev->vhost_ops->vhost_new_worker(dev, &worker);
190         if (ret == -ENOTTY) {
191             /*
192              * worker ioctls are not implemented so just ignore and
193              * and continue device setup.
194              */
195             warn_report("vhost-scsi: Backend supports a single worker. "
196                         "Ignoring worker_per_virtqueue=true setting.");
197             ret = 0;
198             break;
199         } else if (ret) {
200             break;
201         }
202 
203         memset(&vq_worker, 0, sizeof(vq_worker));
204         vq_worker.worker_id = worker.worker_id;
205         vq_worker.index = i;
206 
207         ret = dev->vhost_ops->vhost_attach_vring_worker(dev, &vq_worker);
208         if (ret == -ENOTTY) {
209             /*
210              * It's a bug for the kernel to have supported the worker creation
211              * ioctl but not attach.
212              */
213             dev->vhost_ops->vhost_free_worker(dev, &worker);
214             break;
215         } else if (ret) {
216             break;
217         }
218     }
219 
220     return ret;
221 }
222 
223 static void vhost_scsi_realize(DeviceState *dev, Error **errp)
224 {
225     ERRP_GUARD();
226     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
227     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
228     Error *err = NULL;
229     int vhostfd = -1;
230     int ret;
231     struct vhost_virtqueue *vqs = NULL;
232 
233     if (!vs->conf.wwpn) {
234         error_setg(errp, "vhost-scsi: missing wwpn");
235         return;
236     }
237 
238     if (vs->conf.vhostfd) {
239         vhostfd = monitor_fd_param(monitor_cur(), vs->conf.vhostfd, errp);
240         if (vhostfd == -1) {
241             error_prepend(errp, "vhost-scsi: unable to parse vhostfd: ");
242             return;
243         }
244     } else {
245         vhostfd = open("/dev/vhost-scsi", O_RDWR);
246         if (vhostfd < 0) {
247             error_setg(errp, "vhost-scsi: open vhost char device failed: %s",
248                        strerror(errno));
249             return;
250         }
251     }
252 
253     virtio_scsi_common_realize(dev,
254                                vhost_dummy_handle_output,
255                                vhost_dummy_handle_output,
256                                vhost_dummy_handle_output,
257                                &err);
258     if (err != NULL) {
259         error_propagate(errp, err);
260         goto close_fd;
261     }
262 
263     if (!vsc->migratable) {
264         error_setg(&vsc->migration_blocker,
265                 "vhost-scsi does not support migration in all cases. "
266                 "When external environment supports it (Orchestrator migrates "
267                 "target SCSI device state or use shared storage over network), "
268                 "set 'migratable' property to true to enable migration.");
269         if (migrate_add_blocker_normal(&vsc->migration_blocker, errp) < 0) {
270             goto free_virtio;
271         }
272     }
273 
274     vsc->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
275     vqs = g_new0(struct vhost_virtqueue, vsc->dev.nvqs);
276     vsc->dev.vqs = vqs;
277     vsc->dev.vq_index = 0;
278     vsc->dev.backend_features = 0;
279 
280     ret = vhost_dev_init(&vsc->dev, (void *)(uintptr_t)vhostfd,
281                          VHOST_BACKEND_TYPE_KERNEL, 0, errp);
282     if (ret < 0) {
283         /*
284          * vhost_dev_init calls vhost_dev_cleanup on error, which closes
285          * vhostfd, don't double close it.
286          */
287         vhostfd = -1;
288         goto free_vqs;
289     }
290 
291     ret = vhost_scsi_set_workers(vsc, vs->conf.worker_per_virtqueue);
292     if (ret < 0) {
293         error_setg(errp, "vhost-scsi: vhost worker setup failed: %s",
294                    strerror(-ret));
295         goto free_vqs;
296     }
297 
298     /* At present, channel and lun both are 0 for bootable vhost-scsi disk */
299     vsc->channel = 0;
300     vsc->lun = 0;
301     /* Note: we can also get the minimum tpgt from kernel */
302     vsc->target = vs->conf.boot_tpgt;
303 
304     return;
305 
306  free_vqs:
307     g_free(vqs);
308     if (!vsc->migratable) {
309         migrate_del_blocker(&vsc->migration_blocker);
310     }
311  free_virtio:
312     virtio_scsi_common_unrealize(dev);
313  close_fd:
314     if (vhostfd >= 0) {
315         close(vhostfd);
316     }
317     return;
318 }
319 
320 static void vhost_scsi_unrealize(DeviceState *dev)
321 {
322     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
323     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
324     struct vhost_virtqueue *vqs = vsc->dev.vqs;
325 
326     if (!vsc->migratable) {
327         migrate_del_blocker(&vsc->migration_blocker);
328     }
329 
330     /* This will stop vhost backend. */
331     vhost_scsi_set_status(vdev, 0);
332 
333     vhost_dev_cleanup(&vsc->dev);
334     g_free(vqs);
335 
336     virtio_scsi_common_unrealize(dev);
337 }
338 
339 static struct vhost_dev *vhost_scsi_get_vhost(VirtIODevice *vdev)
340 {
341     VHostSCSI *s = VHOST_SCSI(vdev);
342     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
343     return &vsc->dev;
344 }
345 
346 static Property vhost_scsi_properties[] = {
347     DEFINE_PROP_STRING("vhostfd", VirtIOSCSICommon, conf.vhostfd),
348     DEFINE_PROP_STRING("wwpn", VirtIOSCSICommon, conf.wwpn),
349     DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0),
350     DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues,
351                        VIRTIO_SCSI_AUTO_NUM_QUEUES),
352     DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size,
353                        128),
354     DEFINE_PROP_BOOL("seg_max_adjust", VirtIOSCSICommon, conf.seg_max_adjust,
355                       true),
356     DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors,
357                        0xFFFF),
358     DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128),
359     DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features,
360                                                  VIRTIO_SCSI_F_T10_PI,
361                                                  false),
362     DEFINE_PROP_BOOL("migratable", VHostSCSICommon, migratable, false),
363     DEFINE_PROP_BOOL("worker_per_virtqueue", VirtIOSCSICommon,
364                      conf.worker_per_virtqueue, false),
365     DEFINE_PROP_END_OF_LIST(),
366 };
367 
368 static void vhost_scsi_class_init(ObjectClass *klass, void *data)
369 {
370     DeviceClass *dc = DEVICE_CLASS(klass);
371     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
372     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
373 
374     device_class_set_props(dc, vhost_scsi_properties);
375     dc->vmsd = &vmstate_virtio_vhost_scsi;
376     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
377     vdc->realize = vhost_scsi_realize;
378     vdc->unrealize = vhost_scsi_unrealize;
379     vdc->get_features = vhost_scsi_common_get_features;
380     vdc->set_config = vhost_scsi_common_set_config;
381     vdc->set_status = vhost_scsi_set_status;
382     vdc->get_vhost = vhost_scsi_get_vhost;
383     fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
384 }
385 
386 static void vhost_scsi_instance_init(Object *obj)
387 {
388     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj);
389 
390     vsc->feature_bits = kernel_feature_bits;
391 
392     device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL,
393                                   DEVICE(vsc));
394 }
395 
396 static const TypeInfo vhost_scsi_info = {
397     .name = TYPE_VHOST_SCSI,
398     .parent = TYPE_VHOST_SCSI_COMMON,
399     .instance_size = sizeof(VHostSCSI),
400     .class_init = vhost_scsi_class_init,
401     .instance_init = vhost_scsi_instance_init,
402     .interfaces = (InterfaceInfo[]) {
403         { TYPE_FW_PATH_PROVIDER },
404         { }
405     },
406 };
407 
408 static void virtio_register_types(void)
409 {
410     type_register_static(&vhost_scsi_info);
411 }
412 
413 type_init(virtio_register_types)
414