2 * vhost_scsi host device
4 * Copyright IBM, Corp. 2011
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>
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
;
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
);
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");
76 ret
= vhost_ops
->vhost_call(&s
->dev
,
77 VHOST_SCSI_GET_ABI_VERSION
, &abi_version
);
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
);
89 ret
= vhost_dev_enable_notifiers(&s
->dev
, vdev
);
94 s
->dev
.acked_features
= vdev
->guest_features
;
95 ret
= vhost_dev_start(&s
->dev
, vdev
);
97 error_report("Error start vhost dev");
101 ret
= vhost_scsi_set_endpoint(s
);
103 error_report("Error set vhost-scsi endpoint");
107 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, true);
109 error_report("Error binding guest notifier");
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);
124 vhost_scsi_clear_endpoint(s
);
126 vhost_dev_stop(&s
->dev
, vdev
);
128 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
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
);
139 if (k
->set_guest_notifiers
) {
140 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
142 error_report("vhost guest notifier cleanup failed: %d\n", ret
);
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
,
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");
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
) {
185 ret
= vhost_scsi_start(s
);
187 error_report("virtio-scsi: unable to start vhost: %s\n",
190 /* There is no userspace virtio-scsi fallback so exit */
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
);
210 if (!vs
->conf
.wwpn
) {
211 error_setg(errp
, "vhost-scsi: missing wwpn");
215 if (vs
->conf
.vhostfd
) {
216 vhostfd
= monitor_handle_fd_param(cur_mon
, vs
->conf
.vhostfd
);
218 error_setg(errp
, "vhost-scsi: unable to parse vhostfd");
222 vhostfd
= open("/dev/vhost-scsi", O_RDWR
);
224 error_setg(errp
, "vhost-scsi: open vhost char device failed: %s",
230 virtio_scsi_common_realize(dev
, &err
, vhost_dummy_handle_output
,
231 vhost_dummy_handle_output
,
232 vhost_dummy_handle_output
);
234 error_propagate(errp
, err
);
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
);
242 ret
= vhost_dev_init(&s
->dev
, (void *)(uintptr_t)vhostfd
,
243 VHOST_BACKEND_TYPE_KERNEL
, true);
245 error_setg(errp
, "vhost-scsi: vhost initialization failed: %s",
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);
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
)