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 "qemu/osdep.h"
18 #include "qapi/error.h"
19 #include <sys/ioctl.h>
20 #include "qemu/error-report.h"
21 #include "qemu/queue.h"
22 #include "monitor/monitor.h"
23 #include "migration/migration.h"
24 #include "hw/virtio/vhost-scsi.h"
25 #include "hw/virtio/vhost.h"
26 #include "hw/virtio/virtio-scsi.h"
27 #include "hw/virtio/virtio-bus.h"
28 #include "hw/virtio/virtio-access.h"
29 #include "hw/fw-path-provider.h"
30 #include "linux/vhost.h"
32 /* Features supported by host kernel. */
33 static const int kernel_feature_bits
[] = {
34 VIRTIO_F_NOTIFY_ON_EMPTY
,
35 VIRTIO_RING_F_INDIRECT_DESC
,
36 VIRTIO_RING_F_EVENT_IDX
,
37 VIRTIO_SCSI_F_HOTPLUG
,
38 VHOST_INVALID_FEATURE_BIT
41 static int vhost_scsi_set_endpoint(VHostSCSI
*s
)
43 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
44 const VhostOps
*vhost_ops
= s
->dev
.vhost_ops
;
45 struct vhost_scsi_target backend
;
48 memset(&backend
, 0, sizeof(backend
));
49 pstrcpy(backend
.vhost_wwpn
, sizeof(backend
.vhost_wwpn
), vs
->conf
.wwpn
);
50 ret
= vhost_ops
->vhost_scsi_set_endpoint(&s
->dev
, &backend
);
57 static void vhost_scsi_clear_endpoint(VHostSCSI
*s
)
59 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(s
);
60 struct vhost_scsi_target backend
;
61 const VhostOps
*vhost_ops
= s
->dev
.vhost_ops
;
63 memset(&backend
, 0, sizeof(backend
));
64 pstrcpy(backend
.vhost_wwpn
, sizeof(backend
.vhost_wwpn
), vs
->conf
.wwpn
);
65 vhost_ops
->vhost_scsi_clear_endpoint(&s
->dev
, &backend
);
68 static int vhost_scsi_start(VHostSCSI
*s
)
70 int ret
, abi_version
, i
;
71 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
72 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
73 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
74 const VhostOps
*vhost_ops
= s
->dev
.vhost_ops
;
76 if (!k
->set_guest_notifiers
) {
77 error_report("binding does not support guest notifiers");
81 ret
= vhost_ops
->vhost_scsi_get_abi_version(&s
->dev
, &abi_version
);
85 if (abi_version
> VHOST_SCSI_ABI_VERSION
) {
86 error_report("vhost-scsi: The running tcm_vhost kernel abi_version:"
87 " %d is greater than vhost_scsi userspace supports: %d, please"
88 " upgrade your version of QEMU", abi_version
,
89 VHOST_SCSI_ABI_VERSION
);
93 ret
= vhost_dev_enable_notifiers(&s
->dev
, vdev
);
98 s
->dev
.acked_features
= vdev
->guest_features
;
99 ret
= vhost_dev_start(&s
->dev
, vdev
);
101 error_report("Error start vhost dev");
105 ret
= vhost_scsi_set_endpoint(s
);
107 error_report("Error set vhost-scsi endpoint");
111 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, true);
113 error_report("Error binding guest notifier");
117 /* guest_notifier_mask/pending not used yet, so just unmask
118 * everything here. virtio-pci will do the right thing by
119 * enabling/disabling irqfd.
121 for (i
= 0; i
< s
->dev
.nvqs
; i
++) {
122 vhost_virtqueue_mask(&s
->dev
, vdev
, s
->dev
.vq_index
+ i
, false);
128 vhost_scsi_clear_endpoint(s
);
130 vhost_dev_stop(&s
->dev
, vdev
);
132 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
136 static void vhost_scsi_stop(VHostSCSI
*s
)
138 VirtIODevice
*vdev
= VIRTIO_DEVICE(s
);
139 BusState
*qbus
= BUS(qdev_get_parent_bus(DEVICE(vdev
)));
140 VirtioBusClass
*k
= VIRTIO_BUS_GET_CLASS(qbus
);
143 if (k
->set_guest_notifiers
) {
144 ret
= k
->set_guest_notifiers(qbus
->parent
, s
->dev
.nvqs
, false);
146 error_report("vhost guest notifier cleanup failed: %d", ret
);
151 vhost_scsi_clear_endpoint(s
);
152 vhost_dev_stop(&s
->dev
, vdev
);
153 vhost_dev_disable_notifiers(&s
->dev
, vdev
);
156 static uint64_t vhost_scsi_get_features(VirtIODevice
*vdev
,
160 VHostSCSI
*s
= VHOST_SCSI(vdev
);
162 return vhost_get_features(&s
->dev
, kernel_feature_bits
, features
);
165 static void vhost_scsi_set_config(VirtIODevice
*vdev
,
166 const uint8_t *config
)
168 VirtIOSCSIConfig
*scsiconf
= (VirtIOSCSIConfig
*)config
;
169 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(vdev
);
171 if ((uint32_t) virtio_ldl_p(vdev
, &scsiconf
->sense_size
) != vs
->sense_size
||
172 (uint32_t) virtio_ldl_p(vdev
, &scsiconf
->cdb_size
) != vs
->cdb_size
) {
173 error_report("vhost-scsi does not support changing the sense data and CDB sizes");
178 static void vhost_scsi_set_status(VirtIODevice
*vdev
, uint8_t val
)
180 VHostSCSI
*s
= (VHostSCSI
*)vdev
;
181 bool start
= (val
& VIRTIO_CONFIG_S_DRIVER_OK
);
183 if (s
->dev
.started
== start
) {
190 ret
= vhost_scsi_start(s
);
192 error_report("virtio-scsi: unable to start vhost: %s",
195 /* There is no userspace virtio-scsi fallback so exit */
203 static void vhost_dummy_handle_output(VirtIODevice
*vdev
, VirtQueue
*vq
)
207 static void vhost_scsi_realize(DeviceState
*dev
, Error
**errp
)
209 VirtIOSCSICommon
*vs
= VIRTIO_SCSI_COMMON(dev
);
210 VHostSCSI
*s
= VHOST_SCSI(dev
);
215 if (!vs
->conf
.wwpn
) {
216 error_setg(errp
, "vhost-scsi: missing wwpn");
220 if (vs
->conf
.vhostfd
) {
221 vhostfd
= monitor_fd_param(cur_mon
, vs
->conf
.vhostfd
, errp
);
223 error_prepend(errp
, "vhost-scsi: unable to parse vhostfd: ");
227 vhostfd
= open("/dev/vhost-scsi", O_RDWR
);
229 error_setg(errp
, "vhost-scsi: open vhost char device failed: %s",
235 virtio_scsi_common_realize(dev
, &err
, vhost_dummy_handle_output
,
236 vhost_dummy_handle_output
,
237 vhost_dummy_handle_output
);
239 error_propagate(errp
, err
);
244 s
->dev
.nvqs
= VHOST_SCSI_VQ_NUM_FIXED
+ vs
->conf
.num_queues
;
245 s
->dev
.vqs
= g_new(struct vhost_virtqueue
, s
->dev
.nvqs
);
247 s
->dev
.backend_features
= 0;
249 ret
= vhost_dev_init(&s
->dev
, (void *)(uintptr_t)vhostfd
,
250 VHOST_BACKEND_TYPE_KERNEL
);
252 error_setg(errp
, "vhost-scsi: vhost initialization failed: %s",
257 /* At present, channel and lun both are 0 for bootable vhost-scsi disk */
260 /* Note: we can also get the minimum tpgt from kernel */
261 s
->target
= vs
->conf
.boot_tpgt
;
263 error_setg(&s
->migration_blocker
,
264 "vhost-scsi does not support migration");
265 migrate_add_blocker(s
->migration_blocker
);
268 static void vhost_scsi_unrealize(DeviceState
*dev
, Error
**errp
)
270 VirtIODevice
*vdev
= VIRTIO_DEVICE(dev
);
271 VHostSCSI
*s
= VHOST_SCSI(dev
);
273 migrate_del_blocker(s
->migration_blocker
);
274 error_free(s
->migration_blocker
);
276 /* This will stop vhost backend. */
277 vhost_scsi_set_status(vdev
, 0);
279 vhost_dev_cleanup(&s
->dev
);
282 virtio_scsi_common_unrealize(dev
, errp
);
286 * Implementation of an interface to adjust firmware path
287 * for the bootindex property handling.
289 static char *vhost_scsi_get_fw_dev_path(FWPathProvider
*p
, BusState
*bus
,
292 VHostSCSI
*s
= VHOST_SCSI(dev
);
293 /* format: channel@channel/vhost-scsi@target,lun */
294 return g_strdup_printf("/channel@%x/%s@%x,%x", s
->channel
,
295 qdev_fw_name(dev
), s
->target
, s
->lun
);
298 static Property vhost_scsi_properties
[] = {
299 DEFINE_PROP_STRING("vhostfd", VHostSCSI
, parent_obj
.conf
.vhostfd
),
300 DEFINE_PROP_STRING("wwpn", VHostSCSI
, parent_obj
.conf
.wwpn
),
301 DEFINE_PROP_UINT32("boot_tpgt", VHostSCSI
, parent_obj
.conf
.boot_tpgt
, 0),
302 DEFINE_PROP_UINT32("num_queues", VHostSCSI
, parent_obj
.conf
.num_queues
, 1),
303 DEFINE_PROP_UINT32("max_sectors", VHostSCSI
, parent_obj
.conf
.max_sectors
,
305 DEFINE_PROP_UINT32("cmd_per_lun", VHostSCSI
, parent_obj
.conf
.cmd_per_lun
,
307 DEFINE_PROP_END_OF_LIST(),
310 static void vhost_scsi_class_init(ObjectClass
*klass
, void *data
)
312 DeviceClass
*dc
= DEVICE_CLASS(klass
);
313 VirtioDeviceClass
*vdc
= VIRTIO_DEVICE_CLASS(klass
);
314 FWPathProviderClass
*fwc
= FW_PATH_PROVIDER_CLASS(klass
);
316 dc
->props
= vhost_scsi_properties
;
317 set_bit(DEVICE_CATEGORY_STORAGE
, dc
->categories
);
318 vdc
->realize
= vhost_scsi_realize
;
319 vdc
->unrealize
= vhost_scsi_unrealize
;
320 vdc
->get_features
= vhost_scsi_get_features
;
321 vdc
->set_config
= vhost_scsi_set_config
;
322 vdc
->set_status
= vhost_scsi_set_status
;
323 fwc
->get_dev_path
= vhost_scsi_get_fw_dev_path
;
326 static void vhost_scsi_instance_init(Object
*obj
)
328 VHostSCSI
*dev
= VHOST_SCSI(obj
);
330 device_add_bootindex_property(obj
, &dev
->bootindex
, "bootindex", NULL
,
334 static const TypeInfo vhost_scsi_info
= {
335 .name
= TYPE_VHOST_SCSI
,
336 .parent
= TYPE_VIRTIO_SCSI_COMMON
,
337 .instance_size
= sizeof(VHostSCSI
),
338 .class_init
= vhost_scsi_class_init
,
339 .instance_init
= vhost_scsi_instance_init
,
340 .interfaces
= (InterfaceInfo
[]) {
341 { TYPE_FW_PATH_PROVIDER
},
346 static void virtio_register_types(void)
348 type_register_static(&vhost_scsi_info
);
351 type_init(virtio_register_types
)