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