Merge remote-tracking branch 'qemu/master'
[qemu/ar7.git] / hw / scsi / vhost-scsi.c
blobddfe76aed01b0a2ed5fa709ade3badba753ac854
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 const VhostOps *vhost_ops = s->dev.vhost_ops;
40 struct vhost_scsi_target backend;
41 int ret;
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;
49 return 0;
52 static void vhost_scsi_clear_endpoint(VHostSCSI *s)
54 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
55 struct vhost_scsi_target backend;
56 const VhostOps *vhost_ops = s->dev.vhost_ops;
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);
63 static int vhost_scsi_start(VHostSCSI *s)
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;
71 if (!k->set_guest_notifiers) {
72 error_report("binding does not support guest notifiers");
73 return -ENOSYS;
76 ret = vhost_ops->vhost_call(&s->dev,
77 VHOST_SCSI_GET_ABI_VERSION, &abi_version);
78 if (ret < 0) {
79 return -errno;
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;
89 ret = vhost_dev_enable_notifiers(&s->dev, vdev);
90 if (ret < 0) {
91 return ret;
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;
101 ret = vhost_scsi_set_endpoint(s);
102 if (ret < 0) {
103 error_report("Error set vhost-scsi endpoint");
104 goto err_vhost_stop;
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;
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.
117 for (i = 0; i < s->dev.nvqs; i++) {
118 vhost_virtqueue_mask(&s->dev, vdev, i, false);
121 return ret;
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;
132 static void vhost_scsi_stop(VHostSCSI *s)
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;
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);
145 assert(ret >= 0);
147 vhost_scsi_clear_endpoint(s);
148 vhost_dev_stop(&s->dev, vdev);
149 vhost_dev_disable_notifiers(&s->dev, vdev);
152 static uint32_t vhost_scsi_get_features(VirtIODevice *vdev,
153 uint32_t features)
155 VHostSCSI *s = VHOST_SCSI(vdev);
157 return vhost_get_features(&s->dev, kernel_feature_bits, features);
160 static void vhost_scsi_set_config(VirtIODevice *vdev,
161 const uint8_t *config)
163 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
164 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
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);
173 static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val)
175 VHostSCSI *s = (VHostSCSI *)vdev;
176 bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK);
178 if (s->dev.started == start) {
179 return;
182 if (start) {
183 int ret;
185 ret = vhost_scsi_start(s);
186 if (ret < 0) {
187 error_report("virtio-scsi: unable to start vhost: %s\n",
188 strerror(-ret));
190 /* There is no userspace virtio-scsi fallback so exit */
191 exit(1);
193 } else {
194 vhost_scsi_stop(s);
198 static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
202 static void vhost_scsi_realize(DeviceState *dev, Error **errp)
204 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
205 VHostSCSI *s = VHOST_SCSI(dev);
206 Error *err = NULL;
207 int vhostfd = -1;
208 int ret;
210 if (!vs->conf.wwpn) {
211 error_setg(errp, "vhost-scsi: missing wwpn");
212 return;
215 if (vs->conf.vhostfd) {
216 vhostfd = monitor_handle_fd_param(cur_mon, vs->conf.vhostfd);
217 if (vhostfd == -1) {
218 error_setg(errp, "vhost-scsi: unable to parse vhostfd");
219 return;
221 } else {
222 vhostfd = open("/dev/vhost-scsi", O_RDWR);
223 if (vhostfd < 0) {
224 error_setg(errp, "vhost-scsi: open vhost char device failed: %s",
225 strerror(errno));
226 return;
230 virtio_scsi_common_realize(dev, &err, vhost_dummy_handle_output,
231 vhost_dummy_handle_output,
232 vhost_dummy_handle_output);
233 if (err != NULL) {
234 error_propagate(errp, err);
235 return;
238 s->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
239 s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs);
240 s->dev.vq_index = 0;
242 ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd,
243 VHOST_BACKEND_TYPE_KERNEL, true);
244 if (ret < 0) {
245 error_setg(errp, "vhost-scsi: vhost initialization failed: %s",
246 strerror(-ret));
247 return;
249 s->dev.backend_features = 0;
251 error_setg(&s->migration_blocker,
252 "vhost-scsi does not support migration");
253 migrate_add_blocker(s->migration_blocker);
256 static void vhost_scsi_unrealize(DeviceState *dev, Error **errp)
258 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
259 VHostSCSI *s = VHOST_SCSI(dev);
261 migrate_del_blocker(s->migration_blocker);
262 error_free(s->migration_blocker);
264 /* This will stop vhost backend. */
265 vhost_scsi_set_status(vdev, 0);
267 g_free(s->dev.vqs);
269 virtio_scsi_common_unrealize(dev, errp);
272 static Property vhost_scsi_properties[] = {
273 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSI, parent_obj.conf),
274 DEFINE_PROP_END_OF_LIST(),
277 static void vhost_scsi_class_init(ObjectClass *klass, void *data)
279 DeviceClass *dc = DEVICE_CLASS(klass);
280 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
282 dc->props = vhost_scsi_properties;
283 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
284 vdc->realize = vhost_scsi_realize;
285 vdc->unrealize = vhost_scsi_unrealize;
286 vdc->get_features = vhost_scsi_get_features;
287 vdc->set_config = vhost_scsi_set_config;
288 vdc->set_status = vhost_scsi_set_status;
291 static const TypeInfo vhost_scsi_info = {
292 .name = TYPE_VHOST_SCSI,
293 .parent = TYPE_VIRTIO_SCSI_COMMON,
294 .instance_size = sizeof(VHostSCSI),
295 .class_init = vhost_scsi_class_init,
298 static void virtio_register_types(void)
300 type_register_static(&vhost_scsi_info);
303 type_init(virtio_register_types)