]> git.proxmox.com Git - mirror_qemu.git/blame - hw/scsi/vhost-scsi.c
hw/virtio: move vhd->started check into helper and add FIXME
[mirror_qemu.git] / hw / scsi / vhost-scsi.c
CommitLineData
5e9be92d
NB
1/*
2 * vhost_scsi host device
3 *
4 * Copyright IBM, Corp. 2011
5 *
6 * Authors:
7 * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com>
8 *
9 * Changes for QEMU mainline + tcm_vhost kernel upstream:
10 * Nicholas Bellinger <nab@risingtidesystems.com>
11 *
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.
14 *
15 */
16
a4ab4792 17#include "qemu/osdep.h"
a9c94277 18#include <linux/vhost.h>
5e9be92d 19#include <sys/ioctl.h>
a9c94277 20#include "qapi/error.h"
d49b6836 21#include "qemu/error-report.h"
0b8fa32f 22#include "qemu/module.h"
5e9be92d 23#include "monitor/monitor.h"
795c40b8 24#include "migration/blocker.h"
5e9be92d
NB
25#include "hw/virtio/vhost-scsi.h"
26#include "hw/virtio/vhost.h"
27#include "hw/virtio/virtio-scsi.h"
1c819449 28#include "hw/virtio/virtio-bus.h"
7ce04255 29#include "hw/virtio/virtio-access.h"
1956cf6f 30#include "hw/fw-path-provider.h"
a27bd6c7 31#include "hw/qdev-properties.h"
f348b6d1 32#include "qemu/cutils.h"
2f780b6a 33#include "sysemu/sysemu.h"
5e9be92d 34
2e6d46d7
NN
35/* Features supported by host kernel. */
36static const int kernel_feature_bits[] = {
37 VIRTIO_F_NOTIFY_ON_EMPTY,
38 VIRTIO_RING_F_INDIRECT_DESC,
39 VIRTIO_RING_F_EVENT_IDX,
40 VIRTIO_SCSI_F_HOTPLUG,
41 VHOST_INVALID_FEATURE_BIT
42};
43
5e9be92d
NB
44static int vhost_scsi_set_endpoint(VHostSCSI *s)
45{
46 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
95615ce5
FF
47 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
48 const VhostOps *vhost_ops = vsc->dev.vhost_ops;
5e9be92d
NB
49 struct vhost_scsi_target backend;
50 int ret;
51
52 memset(&backend, 0, sizeof(backend));
53 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
95615ce5 54 ret = vhost_ops->vhost_scsi_set_endpoint(&vsc->dev, &backend);
5e9be92d
NB
55 if (ret < 0) {
56 return -errno;
57 }
58 return 0;
59}
60
61static void vhost_scsi_clear_endpoint(VHostSCSI *s)
62{
63 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
95615ce5 64 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
5e9be92d 65 struct vhost_scsi_target backend;
95615ce5 66 const VhostOps *vhost_ops = vsc->dev.vhost_ops;
5e9be92d
NB
67
68 memset(&backend, 0, sizeof(backend));
69 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
95615ce5 70 vhost_ops->vhost_scsi_clear_endpoint(&vsc->dev, &backend);
5e9be92d
NB
71}
72
73static int vhost_scsi_start(VHostSCSI *s)
74{
95615ce5
FF
75 int ret, abi_version;
76 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
77 const VhostOps *vhost_ops = vsc->dev.vhost_ops;
5e9be92d 78
95615ce5 79 ret = vhost_ops->vhost_scsi_get_abi_version(&vsc->dev, &abi_version);
5e9be92d
NB
80 if (ret < 0) {
81 return -errno;
82 }
83 if (abi_version > VHOST_SCSI_ABI_VERSION) {
84 error_report("vhost-scsi: The running tcm_vhost kernel abi_version:"
95615ce5
FF
85 " %d is greater than vhost_scsi userspace supports: %d,"
86 " please upgrade your version of QEMU", abi_version,
5e9be92d
NB
87 VHOST_SCSI_ABI_VERSION);
88 return -ENOSYS;
89 }
90
95615ce5 91 ret = vhost_scsi_common_start(vsc);
5e9be92d
NB
92 if (ret < 0) {
93 return ret;
94 }
95
5e9be92d
NB
96 ret = vhost_scsi_set_endpoint(s);
97 if (ret < 0) {
95615ce5
FF
98 error_report("Error setting vhost-scsi endpoint");
99 vhost_scsi_common_stop(vsc);
5e9be92d
NB
100 }
101
102 return ret;
5e9be92d
NB
103}
104
105static void vhost_scsi_stop(VHostSCSI *s)
106{
95615ce5 107 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
5e9be92d
NB
108
109 vhost_scsi_clear_endpoint(s);
95615ce5 110 vhost_scsi_common_stop(vsc);
5e9be92d
NB
111}
112
113static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val)
114{
95615ce5
FF
115 VHostSCSI *s = VHOST_SCSI(vdev);
116 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
5e9be92d
NB
117 bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK);
118
c6d369fd
NW
119 if (!vdev->vm_running) {
120 start = false;
121 }
122
b8f3e6a1 123 if (vhost_dev_is_started(&vsc->dev) == start) {
5e9be92d
NB
124 return;
125 }
126
127 if (start) {
128 int ret;
129
130 ret = vhost_scsi_start(s);
131 if (ret < 0) {
95615ce5 132 error_report("unable to start vhost-scsi: %s", strerror(-ret));
5e9be92d
NB
133 exit(1);
134 }
135 } else {
136 vhost_scsi_stop(s);
137 }
138}
139
91d670fb
ML
140static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
141{
142}
143
4ea57425
NW
144static int vhost_scsi_pre_save(void *opaque)
145{
146 VHostSCSICommon *vsc = opaque;
147
148 /* At this point, backend must be stopped, otherwise
149 * it might keep writing to memory. */
b8f3e6a1 150 assert(!vhost_dev_is_started(&vsc->dev));
4ea57425
NW
151
152 return 0;
153}
154
155static const VMStateDescription vmstate_virtio_vhost_scsi = {
156 .name = "virtio-vhost_scsi",
157 .minimum_version_id = 1,
158 .version_id = 1,
159 .fields = (VMStateField[]) {
160 VMSTATE_VIRTIO_DEVICE,
161 VMSTATE_END_OF_LIST()
162 },
163 .pre_save = vhost_scsi_pre_save,
164};
165
71a6520b 166static void vhost_scsi_realize(DeviceState *dev, Error **errp)
5e9be92d 167{
71a6520b 168 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
95615ce5 169 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
71a6520b 170 Error *err = NULL;
5e9be92d
NB
171 int vhostfd = -1;
172 int ret;
b259772a 173 struct vhost_virtqueue *vqs = NULL;
5e9be92d
NB
174
175 if (!vs->conf.wwpn) {
71a6520b
AF
176 error_setg(errp, "vhost-scsi: missing wwpn");
177 return;
5e9be92d
NB
178 }
179
180 if (vs->conf.vhostfd) {
947e4744 181 vhostfd = monitor_fd_param(monitor_cur(), vs->conf.vhostfd, errp);
5e9be92d 182 if (vhostfd == -1) {
e43bfd9c 183 error_prepend(errp, "vhost-scsi: unable to parse vhostfd: ");
71a6520b 184 return;
5e9be92d 185 }
81647a65
NN
186 } else {
187 vhostfd = open("/dev/vhost-scsi", O_RDWR);
188 if (vhostfd < 0) {
189 error_setg(errp, "vhost-scsi: open vhost char device failed: %s",
190 strerror(errno));
191 return;
192 }
5e9be92d
NB
193 }
194
bf46e67d 195 virtio_scsi_common_realize(dev,
91d670fb 196 vhost_dummy_handle_output,
bf46e67d
FZ
197 vhost_dummy_handle_output,
198 vhost_dummy_handle_output,
199 &err);
71a6520b
AF
200 if (err != NULL) {
201 error_propagate(errp, err);
fe44dc91
AA
202 goto close_fd;
203 }
204
b3e89c94
LA
205 if (!vsc->migratable) {
206 error_setg(&vsc->migration_blocker,
207 "vhost-scsi does not support migration in all cases. "
208 "When external environment supports it (Orchestrator migrates "
209 "target SCSI device state or use shared storage over network), "
210 "set 'migratable' property to true to enable migration.");
386f6c07 211 if (migrate_add_blocker(vsc->migration_blocker, errp) < 0) {
934443c3 212 goto free_virtio;
b3e89c94 213 }
5e9be92d
NB
214 }
215
95615ce5 216 vsc->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
b259772a
DT
217 vqs = g_new0(struct vhost_virtqueue, vsc->dev.nvqs);
218 vsc->dev.vqs = vqs;
95615ce5
FF
219 vsc->dev.vq_index = 0;
220 vsc->dev.backend_features = 0;
5e9be92d 221
95615ce5 222 ret = vhost_dev_init(&vsc->dev, (void *)(uintptr_t)vhostfd,
a6945f22 223 VHOST_BACKEND_TYPE_KERNEL, 0, errp);
5e9be92d 224 if (ret < 0) {
539ba1ac
DT
225 /*
226 * vhost_dev_init calls vhost_dev_cleanup on error, which closes
227 * vhostfd, don't double close it.
228 */
229 vhostfd = -1;
fe44dc91 230 goto free_vqs;
5e9be92d 231 }
5e9be92d 232
444c7e0d 233 /* At present, channel and lun both are 0 for bootable vhost-scsi disk */
95615ce5
FF
234 vsc->channel = 0;
235 vsc->lun = 0;
444c7e0d 236 /* Note: we can also get the minimum tpgt from kernel */
95615ce5 237 vsc->target = vs->conf.boot_tpgt;
444c7e0d 238
fe44dc91
AA
239 return;
240
241 free_vqs:
b259772a 242 g_free(vqs);
b3e89c94
LA
243 if (!vsc->migratable) {
244 migrate_del_blocker(vsc->migration_blocker);
245 }
934443c3 246 free_virtio:
aa6f7448 247 error_free(vsc->migration_blocker);
934443c3 248 virtio_scsi_common_unrealize(dev);
fe44dc91 249 close_fd:
539ba1ac
DT
250 if (vhostfd >= 0) {
251 close(vhostfd);
252 }
fe44dc91 253 return;
5e9be92d
NB
254}
255
b69c3c21 256static void vhost_scsi_unrealize(DeviceState *dev)
5e9be92d 257{
306ec6c3 258 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
95615ce5 259 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
a5390d93 260 struct vhost_virtqueue *vqs = vsc->dev.vqs;
5e9be92d 261
b3e89c94
LA
262 if (!vsc->migratable) {
263 migrate_del_blocker(vsc->migration_blocker);
264 error_free(vsc->migration_blocker);
265 }
5e9be92d
NB
266
267 /* This will stop vhost backend. */
268 vhost_scsi_set_status(vdev, 0);
269
95615ce5 270 vhost_dev_cleanup(&vsc->dev);
a5390d93 271 g_free(vqs);
306ec6c3 272
12e1dc49 273 virtio_scsi_common_unrealize(dev);
5e9be92d
NB
274}
275
c255488d
JP
276static struct vhost_dev *vhost_scsi_get_vhost(VirtIODevice *vdev)
277{
278 VHostSCSI *s = VHOST_SCSI(vdev);
279 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
280 return &vsc->dev;
281}
282
5e9be92d 283static Property vhost_scsi_properties[] = {
95615ce5
FF
284 DEFINE_PROP_STRING("vhostfd", VirtIOSCSICommon, conf.vhostfd),
285 DEFINE_PROP_STRING("wwpn", VirtIOSCSICommon, conf.wwpn),
286 DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0),
6a558822
SH
287 DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues,
288 VIRTIO_SCSI_AUTO_NUM_QUEUES),
2994cb2e
EF
289 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size,
290 128),
1bf8a989
DP
291 DEFINE_PROP_BOOL("seg_max_adjust", VirtIOSCSICommon, conf.seg_max_adjust,
292 true),
95615ce5
FF
293 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors,
294 0xFFFF),
295 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128),
f287fdd9
GE
296 DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features,
297 VIRTIO_SCSI_F_T10_PI,
298 false),
b3e89c94 299 DEFINE_PROP_BOOL("migratable", VHostSCSICommon, migratable, false),
5e9be92d
NB
300 DEFINE_PROP_END_OF_LIST(),
301};
302
303static void vhost_scsi_class_init(ObjectClass *klass, void *data)
304{
305 DeviceClass *dc = DEVICE_CLASS(klass);
306 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1956cf6f 307 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
71a6520b 308
4f67d30b 309 device_class_set_props(dc, vhost_scsi_properties);
4ea57425 310 dc->vmsd = &vmstate_virtio_vhost_scsi;
125ee0ed 311 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
71a6520b 312 vdc->realize = vhost_scsi_realize;
306ec6c3 313 vdc->unrealize = vhost_scsi_unrealize;
95615ce5
FF
314 vdc->get_features = vhost_scsi_common_get_features;
315 vdc->set_config = vhost_scsi_common_set_config;
5e9be92d 316 vdc->set_status = vhost_scsi_set_status;
c255488d 317 vdc->get_vhost = vhost_scsi_get_vhost;
95615ce5 318 fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
5e9be92d
NB
319}
320
d4433f32
GA
321static void vhost_scsi_instance_init(Object *obj)
322{
95615ce5
FF
323 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj);
324
325 vsc->feature_bits = kernel_feature_bits;
d4433f32 326
95615ce5 327 device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL,
40c2281c 328 DEVICE(vsc));
d4433f32
GA
329}
330
5e9be92d
NB
331static const TypeInfo vhost_scsi_info = {
332 .name = TYPE_VHOST_SCSI,
95615ce5 333 .parent = TYPE_VHOST_SCSI_COMMON,
5e9be92d
NB
334 .instance_size = sizeof(VHostSCSI),
335 .class_init = vhost_scsi_class_init,
d4433f32 336 .instance_init = vhost_scsi_instance_init,
1956cf6f
GA
337 .interfaces = (InterfaceInfo[]) {
338 { TYPE_FW_PATH_PROVIDER },
339 { }
340 },
5e9be92d
NB
341};
342
343static void virtio_register_types(void)
344{
345 type_register_static(&vhost_scsi_info);
346}
347
348type_init(virtio_register_types)