vhost_net_init will use VhostNetOptions to get all its arguments
[qemu.git] / hw / scsi / vhost-scsi.c
blobe4c98b494d25060afd9f99bbea5d364811b5a9cd
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"
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
36 static int vhost_scsi_set_endpoint(VHostSCSI *s)
38 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
39 struct vhost_scsi_target backend;
40 int ret;
42 memset(&backend, 0, sizeof(backend));
43 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
44 ret = ioctl(s->dev.control, VHOST_SCSI_SET_ENDPOINT, &backend);
45 if (ret < 0) {
46 return -errno;
48 return 0;
51 static void vhost_scsi_clear_endpoint(VHostSCSI *s)
53 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
54 struct vhost_scsi_target backend;
56 memset(&backend, 0, sizeof(backend));
57 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
58 ioctl(s->dev.control, VHOST_SCSI_CLEAR_ENDPOINT, &backend);
61 static int vhost_scsi_start(VHostSCSI *s)
63 int ret, abi_version, i;
64 VirtIODevice *vdev = VIRTIO_DEVICE(s);
65 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
66 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
68 if (!k->set_guest_notifiers) {
69 error_report("binding does not support guest notifiers");
70 return -ENOSYS;
73 ret = ioctl(s->dev.control, VHOST_SCSI_GET_ABI_VERSION, &abi_version);
74 if (ret < 0) {
75 return -errno;
77 if (abi_version > VHOST_SCSI_ABI_VERSION) {
78 error_report("vhost-scsi: The running tcm_vhost kernel abi_version:"
79 " %d is greater than vhost_scsi userspace supports: %d, please"
80 " upgrade your version of QEMU\n", abi_version,
81 VHOST_SCSI_ABI_VERSION);
82 return -ENOSYS;
85 ret = vhost_dev_enable_notifiers(&s->dev, vdev);
86 if (ret < 0) {
87 return ret;
90 s->dev.acked_features = vdev->guest_features;
91 ret = vhost_dev_start(&s->dev, vdev);
92 if (ret < 0) {
93 error_report("Error start vhost dev");
94 goto err_notifiers;
97 ret = vhost_scsi_set_endpoint(s);
98 if (ret < 0) {
99 error_report("Error set vhost-scsi endpoint");
100 goto err_vhost_stop;
103 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
104 if (ret < 0) {
105 error_report("Error binding guest notifier");
106 goto err_endpoint;
109 /* guest_notifier_mask/pending not used yet, so just unmask
110 * everything here. virtio-pci will do the right thing by
111 * enabling/disabling irqfd.
113 for (i = 0; i < s->dev.nvqs; i++) {
114 vhost_virtqueue_mask(&s->dev, vdev, i, false);
117 return ret;
119 err_endpoint:
120 vhost_scsi_clear_endpoint(s);
121 err_vhost_stop:
122 vhost_dev_stop(&s->dev, vdev);
123 err_notifiers:
124 vhost_dev_disable_notifiers(&s->dev, vdev);
125 return ret;
128 static void vhost_scsi_stop(VHostSCSI *s)
130 VirtIODevice *vdev = VIRTIO_DEVICE(s);
131 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
132 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
133 int ret = 0;
135 if (k->set_guest_notifiers) {
136 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
137 if (ret < 0) {
138 error_report("vhost guest notifier cleanup failed: %d\n", ret);
141 assert(ret >= 0);
143 vhost_scsi_clear_endpoint(s);
144 vhost_dev_stop(&s->dev, vdev);
145 vhost_dev_disable_notifiers(&s->dev, vdev);
148 static uint32_t vhost_scsi_get_features(VirtIODevice *vdev,
149 uint32_t features)
151 VHostSCSI *s = VHOST_SCSI(vdev);
153 return vhost_get_features(&s->dev, kernel_feature_bits, features);
156 static void vhost_scsi_set_config(VirtIODevice *vdev,
157 const uint8_t *config)
159 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
160 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
162 if ((uint32_t) ldl_p(&scsiconf->sense_size) != vs->sense_size ||
163 (uint32_t) ldl_p(&scsiconf->cdb_size) != vs->cdb_size) {
164 error_report("vhost-scsi does not support changing the sense data and CDB sizes");
165 exit(1);
169 static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val)
171 VHostSCSI *s = (VHostSCSI *)vdev;
172 bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK);
174 if (s->dev.started == start) {
175 return;
178 if (start) {
179 int ret;
181 ret = vhost_scsi_start(s);
182 if (ret < 0) {
183 error_report("virtio-scsi: unable to start vhost: %s\n",
184 strerror(-ret));
186 /* There is no userspace virtio-scsi fallback so exit */
187 exit(1);
189 } else {
190 vhost_scsi_stop(s);
194 static void vhost_scsi_realize(DeviceState *dev, Error **errp)
196 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
197 VHostSCSI *s = VHOST_SCSI(dev);
198 Error *err = NULL;
199 int vhostfd = -1;
200 int ret;
202 if (!vs->conf.wwpn) {
203 error_setg(errp, "vhost-scsi: missing wwpn");
204 return;
207 if (vs->conf.vhostfd) {
208 vhostfd = monitor_handle_fd_param(cur_mon, vs->conf.vhostfd);
209 if (vhostfd == -1) {
210 error_setg(errp, "vhost-scsi: unable to parse vhostfd");
211 return;
213 } else {
214 vhostfd = open("/dev/vhost-scsi", O_RDWR);
215 if (vhostfd < 0) {
216 error_setg(errp, "vhost-scsi: open vhost char device failed: %s",
217 strerror(errno));
218 return;
222 virtio_scsi_common_realize(dev, &err);
223 if (err != NULL) {
224 error_propagate(errp, err);
225 return;
228 s->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
229 s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs);
230 s->dev.vq_index = 0;
232 ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd,
233 true);
234 if (ret < 0) {
235 error_setg(errp, "vhost-scsi: vhost initialization failed: %s",
236 strerror(-ret));
237 return;
239 s->dev.backend_features = 0;
241 error_setg(&s->migration_blocker,
242 "vhost-scsi does not support migration");
243 migrate_add_blocker(s->migration_blocker);
246 static void vhost_scsi_unrealize(DeviceState *dev, Error **errp)
248 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
249 VHostSCSI *s = VHOST_SCSI(dev);
251 migrate_del_blocker(s->migration_blocker);
252 error_free(s->migration_blocker);
254 /* This will stop vhost backend. */
255 vhost_scsi_set_status(vdev, 0);
257 g_free(s->dev.vqs);
259 virtio_scsi_common_unrealize(dev, errp);
262 static Property vhost_scsi_properties[] = {
263 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSI, parent_obj.conf),
264 DEFINE_PROP_END_OF_LIST(),
267 static void vhost_scsi_class_init(ObjectClass *klass, void *data)
269 DeviceClass *dc = DEVICE_CLASS(klass);
270 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
272 dc->props = vhost_scsi_properties;
273 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
274 vdc->realize = vhost_scsi_realize;
275 vdc->unrealize = vhost_scsi_unrealize;
276 vdc->get_features = vhost_scsi_get_features;
277 vdc->set_config = vhost_scsi_set_config;
278 vdc->set_status = vhost_scsi_set_status;
281 static const TypeInfo vhost_scsi_info = {
282 .name = TYPE_VHOST_SCSI,
283 .parent = TYPE_VIRTIO_SCSI_COMMON,
284 .instance_size = sizeof(VHostSCSI),
285 .class_init = vhost_scsi_class_init,
288 static void virtio_register_types(void)
290 type_register_static(&vhost_scsi_info);
293 type_init(virtio_register_types)