xref: /openbmc/qemu/hw/scsi/vhost-scsi.c (revision 07a32d6b)
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 <sys/ioctl.h>
18 #include "config.h"
19 #include "qemu/queue.h"
20 #include "monitor/monitor.h"
21 #include "migration/migration.h"
22 #include "hw/virtio/vhost-scsi.h"
23 #include "hw/virtio/vhost.h"
24 #include "hw/virtio/virtio-scsi.h"
25 #include "hw/virtio/virtio-bus.h"
26 
27 /* Features supported by host kernel. */
28 static const int kernel_feature_bits[] = {
29     VIRTIO_F_NOTIFY_ON_EMPTY,
30     VIRTIO_RING_F_INDIRECT_DESC,
31     VIRTIO_RING_F_EVENT_IDX,
32     VIRTIO_SCSI_F_HOTPLUG,
33     VHOST_INVALID_FEATURE_BIT
34 };
35 
36 static int vhost_scsi_set_endpoint(VHostSCSI *s)
37 {
38     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
39     const VhostOps *vhost_ops = s->dev.vhost_ops;
40     struct vhost_scsi_target backend;
41     int ret;
42 
43     memset(&backend, 0, sizeof(backend));
44     pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
45     ret = vhost_ops->vhost_call(&s->dev, VHOST_SCSI_SET_ENDPOINT, &backend);
46     if (ret < 0) {
47         return -errno;
48     }
49     return 0;
50 }
51 
52 static void vhost_scsi_clear_endpoint(VHostSCSI *s)
53 {
54     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
55     struct vhost_scsi_target backend;
56     const VhostOps *vhost_ops = s->dev.vhost_ops;
57 
58     memset(&backend, 0, sizeof(backend));
59     pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
60     vhost_ops->vhost_call(&s->dev, VHOST_SCSI_CLEAR_ENDPOINT, &backend);
61 }
62 
63 static int vhost_scsi_start(VHostSCSI *s)
64 {
65     int ret, abi_version, i;
66     VirtIODevice *vdev = VIRTIO_DEVICE(s);
67     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
68     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
69     const VhostOps *vhost_ops = s->dev.vhost_ops;
70 
71     if (!k->set_guest_notifiers) {
72         error_report("binding does not support guest notifiers");
73         return -ENOSYS;
74     }
75 
76     ret = vhost_ops->vhost_call(&s->dev,
77                                 VHOST_SCSI_GET_ABI_VERSION, &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, please"
84                      " upgrade your version of QEMU\n", abi_version,
85                      VHOST_SCSI_ABI_VERSION);
86         return -ENOSYS;
87     }
88 
89     ret = vhost_dev_enable_notifiers(&s->dev, vdev);
90     if (ret < 0) {
91         return ret;
92     }
93 
94     s->dev.acked_features = vdev->guest_features;
95     ret = vhost_dev_start(&s->dev, vdev);
96     if (ret < 0) {
97         error_report("Error start vhost dev");
98         goto err_notifiers;
99     }
100 
101     ret = vhost_scsi_set_endpoint(s);
102     if (ret < 0) {
103         error_report("Error set vhost-scsi endpoint");
104         goto err_vhost_stop;
105     }
106 
107     ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
108     if (ret < 0) {
109         error_report("Error binding guest notifier");
110         goto err_endpoint;
111     }
112 
113     /* guest_notifier_mask/pending not used yet, so just unmask
114      * everything here.  virtio-pci will do the right thing by
115      * enabling/disabling irqfd.
116      */
117     for (i = 0; i < s->dev.nvqs; i++) {
118         vhost_virtqueue_mask(&s->dev, vdev, i, false);
119     }
120 
121     return ret;
122 
123 err_endpoint:
124     vhost_scsi_clear_endpoint(s);
125 err_vhost_stop:
126     vhost_dev_stop(&s->dev, vdev);
127 err_notifiers:
128     vhost_dev_disable_notifiers(&s->dev, vdev);
129     return ret;
130 }
131 
132 static void vhost_scsi_stop(VHostSCSI *s)
133 {
134     VirtIODevice *vdev = VIRTIO_DEVICE(s);
135     BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
136     VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
137     int ret = 0;
138 
139     if (k->set_guest_notifiers) {
140         ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
141         if (ret < 0) {
142                 error_report("vhost guest notifier cleanup failed: %d\n", ret);
143         }
144     }
145     assert(ret >= 0);
146 
147     vhost_scsi_clear_endpoint(s);
148     vhost_dev_stop(&s->dev, vdev);
149     vhost_dev_disable_notifiers(&s->dev, vdev);
150 }
151 
152 static uint32_t vhost_scsi_get_features(VirtIODevice *vdev,
153                                         uint32_t features)
154 {
155     VHostSCSI *s = VHOST_SCSI(vdev);
156 
157     return vhost_get_features(&s->dev, kernel_feature_bits, features);
158 }
159 
160 static void vhost_scsi_set_config(VirtIODevice *vdev,
161                                   const uint8_t *config)
162 {
163     VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
164     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
165 
166     if ((uint32_t) ldl_p(&scsiconf->sense_size) != vs->sense_size ||
167         (uint32_t) ldl_p(&scsiconf->cdb_size) != vs->cdb_size) {
168         error_report("vhost-scsi does not support changing the sense data and CDB sizes");
169         exit(1);
170     }
171 }
172 
173 static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val)
174 {
175     VHostSCSI *s = (VHostSCSI *)vdev;
176     bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK);
177 
178     if (s->dev.started == start) {
179         return;
180     }
181 
182     if (start) {
183         int ret;
184 
185         ret = vhost_scsi_start(s);
186         if (ret < 0) {
187             error_report("virtio-scsi: unable to start vhost: %s\n",
188                          strerror(-ret));
189 
190             /* There is no userspace virtio-scsi fallback so exit */
191             exit(1);
192         }
193     } else {
194         vhost_scsi_stop(s);
195     }
196 }
197 
198 static void vhost_scsi_realize(DeviceState *dev, Error **errp)
199 {
200     VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
201     VHostSCSI *s = VHOST_SCSI(dev);
202     Error *err = NULL;
203     int vhostfd = -1;
204     int ret;
205 
206     if (!vs->conf.wwpn) {
207         error_setg(errp, "vhost-scsi: missing wwpn");
208         return;
209     }
210 
211     if (vs->conf.vhostfd) {
212         vhostfd = monitor_handle_fd_param(cur_mon, vs->conf.vhostfd);
213         if (vhostfd == -1) {
214             error_setg(errp, "vhost-scsi: unable to parse vhostfd");
215             return;
216         }
217     } else {
218         vhostfd = open("/dev/vhost-scsi", O_RDWR);
219         if (vhostfd < 0) {
220             error_setg(errp, "vhost-scsi: open vhost char device failed: %s",
221                        strerror(errno));
222             return;
223         }
224     }
225 
226     virtio_scsi_common_realize(dev, &err);
227     if (err != NULL) {
228         error_propagate(errp, err);
229         return;
230     }
231 
232     s->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
233     s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs);
234     s->dev.vq_index = 0;
235 
236     ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd,
237                          VHOST_BACKEND_TYPE_KERNEL, true);
238     if (ret < 0) {
239         error_setg(errp, "vhost-scsi: vhost initialization failed: %s",
240                    strerror(-ret));
241         return;
242     }
243     s->dev.backend_features = 0;
244 
245     error_setg(&s->migration_blocker,
246             "vhost-scsi does not support migration");
247     migrate_add_blocker(s->migration_blocker);
248 }
249 
250 static void vhost_scsi_unrealize(DeviceState *dev, Error **errp)
251 {
252     VirtIODevice *vdev = VIRTIO_DEVICE(dev);
253     VHostSCSI *s = VHOST_SCSI(dev);
254 
255     migrate_del_blocker(s->migration_blocker);
256     error_free(s->migration_blocker);
257 
258     /* This will stop vhost backend. */
259     vhost_scsi_set_status(vdev, 0);
260 
261     g_free(s->dev.vqs);
262 
263     virtio_scsi_common_unrealize(dev, errp);
264 }
265 
266 static Property vhost_scsi_properties[] = {
267     DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSI, parent_obj.conf),
268     DEFINE_PROP_END_OF_LIST(),
269 };
270 
271 static void vhost_scsi_class_init(ObjectClass *klass, void *data)
272 {
273     DeviceClass *dc = DEVICE_CLASS(klass);
274     VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
275 
276     dc->props = vhost_scsi_properties;
277     set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
278     vdc->realize = vhost_scsi_realize;
279     vdc->unrealize = vhost_scsi_unrealize;
280     vdc->get_features = vhost_scsi_get_features;
281     vdc->set_config = vhost_scsi_set_config;
282     vdc->set_status = vhost_scsi_set_status;
283 }
284 
285 static const TypeInfo vhost_scsi_info = {
286     .name = TYPE_VHOST_SCSI,
287     .parent = TYPE_VIRTIO_SCSI_COMMON,
288     .instance_size = sizeof(VHostSCSI),
289     .class_init = vhost_scsi_class_init,
290 };
291 
292 static void virtio_register_types(void)
293 {
294     type_register_static(&vhost_scsi_info);
295 }
296 
297 type_init(virtio_register_types)
298