vhost: enable vhost without without MSI-X
[qemu.git] / hw / scsi / vhost-scsi.c
blob1c389c4d7efca5662a280d7a34410d2ae6a85973
1 /*
2 * vhost_scsi host device
4 * Copyright IBM, Corp. 2011
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
9 * Changes for QEMU mainline + tcm_vhost kernel upstream:
10 * Nicholas Bellinger <nab@risingtidesystems.com>
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.
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 #include "hw/virtio/virtio-access.h"
27 #include "hw/fw-path-provider.h"
29 /* Features supported by host kernel. */
30 static const int kernel_feature_bits[] = {
31 VIRTIO_F_NOTIFY_ON_EMPTY,
32 VIRTIO_RING_F_INDIRECT_DESC,
33 VIRTIO_RING_F_EVENT_IDX,
34 VIRTIO_SCSI_F_HOTPLUG,
35 VHOST_INVALID_FEATURE_BIT
38 static int vhost_scsi_set_endpoint(VHostSCSI *s)
40 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
41 const VhostOps *vhost_ops = s->dev.vhost_ops;
42 struct vhost_scsi_target backend;
43 int ret;
45 memset(&backend, 0, sizeof(backend));
46 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
47 ret = vhost_ops->vhost_call(&s->dev, VHOST_SCSI_SET_ENDPOINT, &backend);
48 if (ret < 0) {
49 return -errno;
51 return 0;
54 static void vhost_scsi_clear_endpoint(VHostSCSI *s)
56 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
57 struct vhost_scsi_target backend;
58 const VhostOps *vhost_ops = s->dev.vhost_ops;
60 memset(&backend, 0, sizeof(backend));
61 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
62 vhost_ops->vhost_call(&s->dev, VHOST_SCSI_CLEAR_ENDPOINT, &backend);
65 static int vhost_scsi_start(VHostSCSI *s)
67 int ret, abi_version, i;
68 VirtIODevice *vdev = VIRTIO_DEVICE(s);
69 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
70 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
71 const VhostOps *vhost_ops = s->dev.vhost_ops;
73 if (!k->set_guest_notifiers) {
74 error_report("binding does not support guest notifiers");
75 return -ENOSYS;
78 ret = vhost_ops->vhost_call(&s->dev,
79 VHOST_SCSI_GET_ABI_VERSION, &abi_version);
80 if (ret < 0) {
81 return -errno;
83 if (abi_version > VHOST_SCSI_ABI_VERSION) {
84 error_report("vhost-scsi: The running tcm_vhost kernel abi_version:"
85 " %d is greater than vhost_scsi userspace supports: %d, please"
86 " upgrade your version of QEMU", abi_version,
87 VHOST_SCSI_ABI_VERSION);
88 return -ENOSYS;
91 ret = vhost_dev_enable_notifiers(&s->dev, vdev);
92 if (ret < 0) {
93 return ret;
96 s->dev.acked_features = vdev->guest_features;
97 ret = vhost_dev_start(&s->dev, vdev);
98 if (ret < 0) {
99 error_report("Error start vhost dev");
100 goto err_notifiers;
103 ret = vhost_scsi_set_endpoint(s);
104 if (ret < 0) {
105 error_report("Error set vhost-scsi endpoint");
106 goto err_vhost_stop;
109 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
110 if (ret < 0) {
111 error_report("Error binding guest notifier");
112 goto err_endpoint;
115 /* guest_notifier_mask/pending not used yet, so just unmask
116 * everything here. virtio-pci will do the right thing by
117 * enabling/disabling irqfd.
119 for (i = 0; i < s->dev.nvqs; i++) {
120 vhost_virtqueue_mask(&s->dev, vdev, i, false);
123 return ret;
125 err_endpoint:
126 vhost_scsi_clear_endpoint(s);
127 err_vhost_stop:
128 vhost_dev_stop(&s->dev, vdev);
129 err_notifiers:
130 vhost_dev_disable_notifiers(&s->dev, vdev);
131 return ret;
134 static void vhost_scsi_stop(VHostSCSI *s)
136 VirtIODevice *vdev = VIRTIO_DEVICE(s);
137 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
138 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
139 int ret = 0;
141 if (k->set_guest_notifiers) {
142 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
143 if (ret < 0) {
144 error_report("vhost guest notifier cleanup failed: %d", ret);
147 assert(ret >= 0);
149 vhost_scsi_clear_endpoint(s);
150 vhost_dev_stop(&s->dev, vdev);
151 vhost_dev_disable_notifiers(&s->dev, vdev);
154 static uint64_t vhost_scsi_get_features(VirtIODevice *vdev,
155 uint64_t features)
157 VHostSCSI *s = VHOST_SCSI(vdev);
159 return vhost_get_features(&s->dev, kernel_feature_bits, features);
162 static void vhost_scsi_set_config(VirtIODevice *vdev,
163 const uint8_t *config)
165 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
166 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
168 if ((uint32_t) virtio_ldl_p(vdev, &scsiconf->sense_size) != vs->sense_size ||
169 (uint32_t) virtio_ldl_p(vdev, &scsiconf->cdb_size) != vs->cdb_size) {
170 error_report("vhost-scsi does not support changing the sense data and CDB sizes");
171 exit(1);
175 static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val)
177 VHostSCSI *s = (VHostSCSI *)vdev;
178 bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK);
180 if (s->dev.started == start) {
181 return;
184 if (start) {
185 int ret;
187 ret = vhost_scsi_start(s);
188 if (ret < 0) {
189 error_report("virtio-scsi: unable to start vhost: %s",
190 strerror(-ret));
192 /* There is no userspace virtio-scsi fallback so exit */
193 exit(1);
195 } else {
196 vhost_scsi_stop(s);
200 static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
204 static void vhost_scsi_realize(DeviceState *dev, Error **errp)
206 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
207 VHostSCSI *s = VHOST_SCSI(dev);
208 Error *err = NULL;
209 int vhostfd = -1;
210 int ret;
212 if (!vs->conf.wwpn) {
213 error_setg(errp, "vhost-scsi: missing wwpn");
214 return;
217 if (vs->conf.vhostfd) {
218 vhostfd = monitor_fd_param(cur_mon, vs->conf.vhostfd, &err);
219 if (vhostfd == -1) {
220 error_setg(errp, "vhost-scsi: unable to parse vhostfd: %s",
221 error_get_pretty(err));
222 error_free(err);
223 return;
225 } else {
226 vhostfd = open("/dev/vhost-scsi", O_RDWR);
227 if (vhostfd < 0) {
228 error_setg(errp, "vhost-scsi: open vhost char device failed: %s",
229 strerror(errno));
230 return;
234 virtio_scsi_common_realize(dev, &err, vhost_dummy_handle_output,
235 vhost_dummy_handle_output,
236 vhost_dummy_handle_output);
237 if (err != NULL) {
238 error_propagate(errp, err);
239 close(vhostfd);
240 return;
243 s->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
244 s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs);
245 s->dev.vq_index = 0;
246 s->dev.backend_features = 0;
248 ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd,
249 VHOST_BACKEND_TYPE_KERNEL);
250 if (ret < 0) {
251 error_setg(errp, "vhost-scsi: vhost initialization failed: %s",
252 strerror(-ret));
253 return;
256 /* At present, channel and lun both are 0 for bootable vhost-scsi disk */
257 s->channel = 0;
258 s->lun = 0;
259 /* Note: we can also get the minimum tpgt from kernel */
260 s->target = vs->conf.boot_tpgt;
262 error_setg(&s->migration_blocker,
263 "vhost-scsi does not support migration");
264 migrate_add_blocker(s->migration_blocker);
267 static void vhost_scsi_unrealize(DeviceState *dev, Error **errp)
269 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
270 VHostSCSI *s = VHOST_SCSI(dev);
272 migrate_del_blocker(s->migration_blocker);
273 error_free(s->migration_blocker);
275 /* This will stop vhost backend. */
276 vhost_scsi_set_status(vdev, 0);
278 g_free(s->dev.vqs);
280 virtio_scsi_common_unrealize(dev, errp);
284 * Implementation of an interface to adjust firmware path
285 * for the bootindex property handling.
287 static char *vhost_scsi_get_fw_dev_path(FWPathProvider *p, BusState *bus,
288 DeviceState *dev)
290 VHostSCSI *s = VHOST_SCSI(dev);
291 /* format: channel@channel/vhost-scsi@target,lun */
292 return g_strdup_printf("channel@%x/%s@%x,%x", s->channel,
293 qdev_fw_name(dev), s->target, s->lun);
296 static Property vhost_scsi_properties[] = {
297 DEFINE_PROP_STRING("vhostfd", VHostSCSI, parent_obj.conf.vhostfd),
298 DEFINE_PROP_STRING("wwpn", VHostSCSI, parent_obj.conf.wwpn),
299 DEFINE_PROP_UINT32("boot_tpgt", VHostSCSI, parent_obj.conf.boot_tpgt, 0),
300 DEFINE_PROP_UINT32("num_queues", VHostSCSI, parent_obj.conf.num_queues, 1),
301 DEFINE_PROP_UINT32("max_sectors", VHostSCSI, parent_obj.conf.max_sectors,
302 0xFFFF),
303 DEFINE_PROP_UINT32("cmd_per_lun", VHostSCSI, parent_obj.conf.cmd_per_lun,
304 128),
305 DEFINE_PROP_END_OF_LIST(),
308 static void vhost_scsi_class_init(ObjectClass *klass, void *data)
310 DeviceClass *dc = DEVICE_CLASS(klass);
311 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
312 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
314 dc->props = vhost_scsi_properties;
315 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
316 vdc->realize = vhost_scsi_realize;
317 vdc->unrealize = vhost_scsi_unrealize;
318 vdc->get_features = vhost_scsi_get_features;
319 vdc->set_config = vhost_scsi_set_config;
320 vdc->set_status = vhost_scsi_set_status;
321 fwc->get_dev_path = vhost_scsi_get_fw_dev_path;
324 static void vhost_scsi_instance_init(Object *obj)
326 VHostSCSI *dev = VHOST_SCSI(obj);
328 device_add_bootindex_property(obj, &dev->bootindex, "bootindex", NULL,
329 DEVICE(dev), NULL);
332 static const TypeInfo vhost_scsi_info = {
333 .name = TYPE_VHOST_SCSI,
334 .parent = TYPE_VIRTIO_SCSI_COMMON,
335 .instance_size = sizeof(VHostSCSI),
336 .class_init = vhost_scsi_class_init,
337 .instance_init = vhost_scsi_instance_init,
338 .interfaces = (InterfaceInfo[]) {
339 { TYPE_FW_PATH_PROVIDER },
344 static void virtio_register_types(void)
346 type_register_static(&vhost_scsi_info);
349 type_init(virtio_register_types)