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 struct vhost_scsi_target backend
;
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
);
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");
73 ret
= ioctl(s
->dev
.control
, VHOST_SCSI_GET_ABI_VERSION
, &abi_version
);
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
);
85 ret
= vhost_dev_enable_notifiers(&s
->dev
, vdev
);
90 s
->dev
.acked_features
= vdev
->guest_features
;
91 ret
= vhost_dev_start(&s
->dev
, vdev
);
93 error_report("Error start vhost dev");
97 ret
= vhost_scsi_set_endpoint(s
);
99 error_report("Error set vhost-scsi endpoint");
103 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, true);
105 error_report("Error binding guest notifier");
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);
120 vhost_scsi_clear_endpoint(s
);
122 vhost_dev_stop(&s
->dev
, vdev
);
124 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
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
);
135 if (k
->set_guest_notifiers
) {
136 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
138 error_report("vhost guest notifier cleanup failed: %d\n", ret
);
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
,
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");
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
) {
181 ret
= vhost_scsi_start(s
);
183 error_report("virtio-scsi: unable to start vhost: %s\n",
186 /* There is no userspace virtio-scsi fallback so exit */
194 static void vhost_scsi_realize(DeviceState
*dev
, Error
**errp
)
196 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
197 VHostSCSI
*s
= VHOST_SCSI(dev
);
202 if (!vs
->conf
.wwpn
) {
203 error_setg(errp
, "vhost-scsi: missing wwpn");
207 if (vs
->conf
.vhostfd
) {
208 vhostfd
= monitor_handle_fd_param(cur_mon
, vs
->conf
.vhostfd
);
210 error_setg(errp
, "vhost-scsi: unable to parse vhostfd");
214 vhostfd
= open("/dev/vhost-scsi", O_RDWR
);
216 error_setg(errp
, "vhost-scsi: open vhost char device failed: %s",
222 virtio_scsi_common_realize(dev
, &err
);
224 error_propagate(errp
, err
);
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
);
232 ret
= vhost_dev_init(&s
->dev
, (void *)(uintptr_t)vhostfd
,
235 error_setg(errp
, "vhost-scsi: vhost initialization failed: %s",
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);
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
)