xref: /openbmc/qemu/hw/scsi/vhost-scsi.c (revision fa3673e4)
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     VHOST_INVALID_FEATURE_BIT
42 };
43 
44 static int vhost_scsi_set_endpoint(VHostSCSI *s)
45 {
46     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
47     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
48     const VhostOps *vhost_ops = vsc->dev.vhost_ops;
49     struct vhost_scsi_target backend;
50     int ret;
51 
52     memset(&backend, 0, sizeof(backend));
53     pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
54     ret = vhost_ops->vhost_scsi_set_endpoint(&vsc->dev, &backend);
55     if (ret < 0) {
56         return -errno;
57     }
58     return 0;
59 }
60 
61 static void vhost_scsi_clear_endpoint(VHostSCSI *s)
62 {
63     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
64     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
65     struct vhost_scsi_target backend;
66     const VhostOps *vhost_ops = vsc->dev.vhost_ops;
67 
68     memset(&backend, 0, sizeof(backend));
69     pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
70     vhost_ops->vhost_scsi_clear_endpoint(&vsc->dev, &backend);
71 }
72 
73 static int vhost_scsi_start(VHostSCSI *s)
74 {
75     int ret, abi_version;
76     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
77     const VhostOps *vhost_ops = vsc->dev.vhost_ops;
78     Error *local_err = NULL;
79 
80     ret = vhost_ops->vhost_scsi_get_abi_version(&vsc->dev, &abi_version);
81     if (ret < 0) {
82         return -errno;
83     }
84     if (abi_version > VHOST_SCSI_ABI_VERSION) {
85         error_report("vhost-scsi: The running tcm_vhost kernel abi_version:"
86                      " %d is greater than vhost_scsi userspace supports: %d,"
87                      " please upgrade your version of QEMU", abi_version,
88                      VHOST_SCSI_ABI_VERSION);
89         return -ENOSYS;
90     }
91 
92     ret = vhost_scsi_common_start(vsc, &local_err);
93     if (ret < 0) {
94         error_reportf_err(local_err, "Error starting vhost-scsi");
95         return ret;
96     }
97 
98     ret = vhost_scsi_set_endpoint(s);
99     if (ret < 0) {
100         error_reportf_err(local_err, "Error setting vhost-scsi endpoint");
101         vhost_scsi_common_stop(vsc);
102     }
103 
104     return ret;
105 }
106 
107 static void vhost_scsi_stop(VHostSCSI *s)
108 {
109     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
110 
111     vhost_scsi_clear_endpoint(s);
112     vhost_scsi_common_stop(vsc);
113 }
114 
115 static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val)
116 {
117     VHostSCSI *s = VHOST_SCSI(vdev);
118     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
119     bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK);
120 
121     if (!vdev->vm_running) {
122         start = false;
123     }
124 
125     if (vhost_dev_is_started(&vsc->dev) == start) {
126         return;
127     }
128 
129     if (start) {
130         int ret;
131 
132         ret = vhost_scsi_start(s);
133         if (ret < 0) {
134             error_report("unable to start vhost-scsi: %s", strerror(-ret));
135             exit(1);
136         }
137     } else {
138         vhost_scsi_stop(s);
139     }
140 }
141 
142 static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
143 {
144 }
145 
146 static int vhost_scsi_pre_save(void *opaque)
147 {
148     VHostSCSICommon *vsc = opaque;
149 
150     /* At this point, backend must be stopped, otherwise
151      * it might keep writing to memory. */
152     assert(!vhost_dev_is_started(&vsc->dev));
153 
154     return 0;
155 }
156 
157 static const VMStateDescription vmstate_virtio_vhost_scsi = {
158     .name = "virtio-vhost_scsi",
159     .minimum_version_id = 1,
160     .version_id = 1,
161     .fields = (VMStateField[]) {
162         VMSTATE_VIRTIO_DEVICE,
163         VMSTATE_END_OF_LIST()
164     },
165     .pre_save = vhost_scsi_pre_save,
166 };
167 
168 static void vhost_scsi_realize(DeviceState *dev, Error **errp)
169 {
170     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
171     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
172     Error *err = NULL;
173     int vhostfd = -1;
174     int ret;
175     struct vhost_virtqueue *vqs = NULL;
176 
177     if (!vs->conf.wwpn) {
178         error_setg(errp, "vhost-scsi: missing wwpn");
179         return;
180     }
181 
182     if (vs->conf.vhostfd) {
183         vhostfd = monitor_fd_param(monitor_cur(), vs->conf.vhostfd, errp);
184         if (vhostfd == -1) {
185             error_prepend(errp, "vhost-scsi: unable to parse vhostfd: ");
186             return;
187         }
188     } else {
189         vhostfd = open("/dev/vhost-scsi", O_RDWR);
190         if (vhostfd < 0) {
191             error_setg(errp, "vhost-scsi: open vhost char device failed: %s",
192                        strerror(errno));
193             return;
194         }
195     }
196 
197     virtio_scsi_common_realize(dev,
198                                vhost_dummy_handle_output,
199                                vhost_dummy_handle_output,
200                                vhost_dummy_handle_output,
201                                &err);
202     if (err != NULL) {
203         error_propagate(errp, err);
204         goto close_fd;
205     }
206 
207     if (!vsc->migratable) {
208         error_setg(&vsc->migration_blocker,
209                 "vhost-scsi does not support migration in all cases. "
210                 "When external environment supports it (Orchestrator migrates "
211                 "target SCSI device state or use shared storage over network), "
212                 "set 'migratable' property to true to enable migration.");
213         if (migrate_add_blocker(&vsc->migration_blocker, errp) < 0) {
214             goto free_virtio;
215         }
216     }
217 
218     vsc->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
219     vqs = g_new0(struct vhost_virtqueue, vsc->dev.nvqs);
220     vsc->dev.vqs = vqs;
221     vsc->dev.vq_index = 0;
222     vsc->dev.backend_features = 0;
223 
224     ret = vhost_dev_init(&vsc->dev, (void *)(uintptr_t)vhostfd,
225                          VHOST_BACKEND_TYPE_KERNEL, 0, errp);
226     if (ret < 0) {
227         /*
228          * vhost_dev_init calls vhost_dev_cleanup on error, which closes
229          * vhostfd, don't double close it.
230          */
231         vhostfd = -1;
232         goto free_vqs;
233     }
234 
235     /* At present, channel and lun both are 0 for bootable vhost-scsi disk */
236     vsc->channel = 0;
237     vsc->lun = 0;
238     /* Note: we can also get the minimum tpgt from kernel */
239     vsc->target = vs->conf.boot_tpgt;
240 
241     return;
242 
243  free_vqs:
244     g_free(vqs);
245     if (!vsc->migratable) {
246         migrate_del_blocker(&vsc->migration_blocker);
247     }
248  free_virtio:
249     virtio_scsi_common_unrealize(dev);
250  close_fd:
251     if (vhostfd >= 0) {
252         close(vhostfd);
253     }
254     return;
255 }
256 
257 static void vhost_scsi_unrealize(DeviceState *dev)
258 {
259     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
260     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
261     struct vhost_virtqueue *vqs = vsc->dev.vqs;
262 
263     if (!vsc->migratable) {
264         migrate_del_blocker(&vsc->migration_blocker);
265     }
266 
267     /* This will stop vhost backend. */
268     vhost_scsi_set_status(vdev, 0);
269 
270     vhost_dev_cleanup(&vsc->dev);
271     g_free(vqs);
272 
273     virtio_scsi_common_unrealize(dev);
274 }
275 
276 static struct vhost_dev *vhost_scsi_get_vhost(VirtIODevice *vdev)
277 {
278     VHostSCSI *s = VHOST_SCSI(vdev);
279     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
280     return &vsc->dev;
281 }
282 
283 static Property vhost_scsi_properties[] = {
284     DEFINE_PROP_STRING("vhostfd", VirtIOSCSICommon, conf.vhostfd),
285     DEFINE_PROP_STRING("wwpn", VirtIOSCSICommon, conf.wwpn),
286     DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0),
287     DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues,
288                        VIRTIO_SCSI_AUTO_NUM_QUEUES),
289     DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size,
290                        128),
291     DEFINE_PROP_BOOL("seg_max_adjust", VirtIOSCSICommon, conf.seg_max_adjust,
292                       true),
293     DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors,
294                        0xFFFF),
295     DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128),
296     DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features,
297                                                  VIRTIO_SCSI_F_T10_PI,
298                                                  false),
299     DEFINE_PROP_BOOL("migratable", VHostSCSICommon, migratable, false),
300     DEFINE_PROP_END_OF_LIST(),
301 };
302 
303 static void vhost_scsi_class_init(ObjectClass *klass, void *data)
304 {
305     DeviceClass *dc = DEVICE_CLASS(klass);
306     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
307     FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
308 
309     device_class_set_props(dc, vhost_scsi_properties);
310     dc->vmsd = &vmstate_virtio_vhost_scsi;
311     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
312     vdc->realize = vhost_scsi_realize;
313     vdc->unrealize = vhost_scsi_unrealize;
314     vdc->get_features = vhost_scsi_common_get_features;
315     vdc->set_config = vhost_scsi_common_set_config;
316     vdc->set_status = vhost_scsi_set_status;
317     vdc->get_vhost = vhost_scsi_get_vhost;
318     fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
319 }
320 
321 static void vhost_scsi_instance_init(Object *obj)
322 {
323     VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj);
324 
325     vsc->feature_bits = kernel_feature_bits;
326 
327     device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL,
328                                   DEVICE(vsc));
329 }
330 
331 static const TypeInfo vhost_scsi_info = {
332     .name = TYPE_VHOST_SCSI,
333     .parent = TYPE_VHOST_SCSI_COMMON,
334     .instance_size = sizeof(VHostSCSI),
335     .class_init = vhost_scsi_class_init,
336     .instance_init = vhost_scsi_instance_init,
337     .interfaces = (InterfaceInfo[]) {
338         { TYPE_FW_PATH_PROVIDER },
339         { }
340     },
341 };
342 
343 static void virtio_register_types(void)
344 {
345     type_register_static(&vhost_scsi_info);
346 }
347 
348 type_init(virtio_register_types)
349