Merge tag 'v9.0.0-rc3'
[qemu/ar7.git] / hw / scsi / vhost-scsi.c
blobae26bc19a4577fa374474667cd88067899334beb
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 "qemu/osdep.h"
18 #include <linux/vhost.h>
19 #include <sys/ioctl.h>
20 #include "qapi/error.h"
21 #include "qemu/error-report.h"
22 #include "qemu/module.h"
23 #include "monitor/monitor.h"
24 #include "migration/blocker.h"
25 #include "hw/virtio/vhost-scsi.h"
26 #include "hw/virtio/vhost.h"
27 #include "hw/virtio/virtio-scsi.h"
28 #include "hw/virtio/virtio-bus.h"
29 #include "hw/fw-path-provider.h"
30 #include "hw/qdev-properties.h"
31 #include "qemu/cutils.h"
32 #include "sysemu/sysemu.h"
34 /* Features supported by host kernel. */
35 static const int kernel_feature_bits[] = {
36 VIRTIO_F_NOTIFY_ON_EMPTY,
37 VIRTIO_RING_F_INDIRECT_DESC,
38 VIRTIO_RING_F_EVENT_IDX,
39 VIRTIO_SCSI_F_HOTPLUG,
40 VIRTIO_F_RING_RESET,
41 VHOST_INVALID_FEATURE_BIT
44 static int vhost_scsi_set_endpoint(VHostSCSI *s)
46 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
47 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
48 const VhostOps *vhost_ops = vsc->dev.vhost_ops;
49 struct vhost_scsi_target backend;
50 int ret;
52 memset(&backend, 0, sizeof(backend));
53 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
54 ret = vhost_ops->vhost_scsi_set_endpoint(&vsc->dev, &backend);
55 if (ret < 0) {
56 return -errno;
58 return 0;
61 static void vhost_scsi_clear_endpoint(VHostSCSI *s)
63 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
64 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
65 struct vhost_scsi_target backend;
66 const VhostOps *vhost_ops = vsc->dev.vhost_ops;
68 memset(&backend, 0, sizeof(backend));
69 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
70 vhost_ops->vhost_scsi_clear_endpoint(&vsc->dev, &backend);
73 static int vhost_scsi_start(VHostSCSI *s)
75 int ret, abi_version;
76 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
77 const VhostOps *vhost_ops = vsc->dev.vhost_ops;
78 Error *local_err = NULL;
80 ret = vhost_ops->vhost_scsi_get_abi_version(&vsc->dev, &abi_version);
81 if (ret < 0) {
82 return -errno;
84 if (abi_version > VHOST_SCSI_ABI_VERSION) {
85 error_report("vhost-scsi: The running tcm_vhost kernel abi_version:"
86 " %d is greater than vhost_scsi userspace supports: %d,"
87 " please upgrade your version of QEMU", abi_version,
88 VHOST_SCSI_ABI_VERSION);
89 return -ENOSYS;
92 ret = vhost_scsi_common_start(vsc, &local_err);
93 if (ret < 0) {
94 error_reportf_err(local_err, "Error starting vhost-scsi: ");
95 return ret;
98 ret = vhost_scsi_set_endpoint(s);
99 if (ret < 0) {
100 error_report("Error setting vhost-scsi endpoint");
101 vhost_scsi_common_stop(vsc);
104 return ret;
107 static void vhost_scsi_stop(VHostSCSI *s)
109 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
111 vhost_scsi_clear_endpoint(s);
112 vhost_scsi_common_stop(vsc);
115 static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val)
117 VHostSCSI *s = VHOST_SCSI(vdev);
118 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
119 bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK);
121 if (!vdev->vm_running) {
122 start = false;
125 if (vhost_dev_is_started(&vsc->dev) == start) {
126 return;
129 if (start) {
130 int ret;
132 ret = vhost_scsi_start(s);
133 if (ret < 0) {
134 error_report("unable to start vhost-scsi: %s", strerror(-ret));
135 exit(1);
137 } else {
138 vhost_scsi_stop(s);
142 static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
146 static int vhost_scsi_pre_save(void *opaque)
148 VHostSCSICommon *vsc = opaque;
150 /* At this point, backend must be stopped, otherwise
151 * it might keep writing to memory. */
152 assert(!vhost_dev_is_started(&vsc->dev));
154 return 0;
157 static const VMStateDescription vmstate_virtio_vhost_scsi = {
158 .name = "virtio-vhost_scsi",
159 .minimum_version_id = 1,
160 .version_id = 1,
161 .fields = (const VMStateField[]) {
162 VMSTATE_VIRTIO_DEVICE,
163 VMSTATE_END_OF_LIST()
165 .pre_save = vhost_scsi_pre_save,
168 static int vhost_scsi_set_workers(VHostSCSICommon *vsc, bool per_virtqueue)
170 struct vhost_dev *dev = &vsc->dev;
171 struct vhost_vring_worker vq_worker;
172 struct vhost_worker_state worker;
173 int i, ret;
175 /* Use default worker */
176 if (!per_virtqueue || dev->nvqs == VHOST_SCSI_VQ_NUM_FIXED + 1) {
177 return 0;
181 * ctl/evt share the first worker since it will be rare for them
182 * to send cmds while IO is running.
184 for (i = VHOST_SCSI_VQ_NUM_FIXED + 1; i < dev->nvqs; i++) {
185 memset(&worker, 0, sizeof(worker));
187 ret = dev->vhost_ops->vhost_new_worker(dev, &worker);
188 if (ret == -ENOTTY) {
190 * worker ioctls are not implemented so just ignore and
191 * and continue device setup.
193 warn_report("vhost-scsi: Backend supports a single worker. "
194 "Ignoring worker_per_virtqueue=true setting.");
195 ret = 0;
196 break;
197 } else if (ret) {
198 break;
201 memset(&vq_worker, 0, sizeof(vq_worker));
202 vq_worker.worker_id = worker.worker_id;
203 vq_worker.index = i;
205 ret = dev->vhost_ops->vhost_attach_vring_worker(dev, &vq_worker);
206 if (ret == -ENOTTY) {
208 * It's a bug for the kernel to have supported the worker creation
209 * ioctl but not attach.
211 dev->vhost_ops->vhost_free_worker(dev, &worker);
212 break;
213 } else if (ret) {
214 break;
218 return ret;
221 static void vhost_scsi_realize(DeviceState *dev, Error **errp)
223 ERRP_GUARD();
224 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
225 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
226 Error *err = NULL;
227 int vhostfd = -1;
228 int ret;
229 struct vhost_virtqueue *vqs = NULL;
231 if (!vs->conf.wwpn) {
232 error_setg(errp, "vhost-scsi: missing wwpn");
233 return;
236 if (vs->conf.vhostfd) {
237 vhostfd = monitor_fd_param(monitor_cur(), vs->conf.vhostfd, errp);
238 if (vhostfd == -1) {
239 error_prepend(errp, "vhost-scsi: unable to parse vhostfd: ");
240 return;
242 } else {
243 vhostfd = open("/dev/vhost-scsi", O_RDWR);
244 if (vhostfd < 0) {
245 error_setg(errp, "vhost-scsi: open vhost char device failed: %s",
246 strerror(errno));
247 return;
251 virtio_scsi_common_realize(dev,
252 vhost_dummy_handle_output,
253 vhost_dummy_handle_output,
254 vhost_dummy_handle_output,
255 &err);
256 if (err != NULL) {
257 error_propagate(errp, err);
258 goto close_fd;
261 if (!vsc->migratable) {
262 error_setg(&vsc->migration_blocker,
263 "vhost-scsi does not support migration in all cases. "
264 "When external environment supports it (Orchestrator migrates "
265 "target SCSI device state or use shared storage over network), "
266 "set 'migratable' property to true to enable migration.");
267 if (migrate_add_blocker_normal(&vsc->migration_blocker, errp) < 0) {
268 goto free_virtio;
272 vsc->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
273 vqs = g_new0(struct vhost_virtqueue, vsc->dev.nvqs);
274 vsc->dev.vqs = vqs;
275 vsc->dev.vq_index = 0;
276 vsc->dev.backend_features = 0;
278 ret = vhost_dev_init(&vsc->dev, (void *)(uintptr_t)vhostfd,
279 VHOST_BACKEND_TYPE_KERNEL, 0, errp);
280 if (ret < 0) {
282 * vhost_dev_init calls vhost_dev_cleanup on error, which closes
283 * vhostfd, don't double close it.
285 vhostfd = -1;
286 goto free_vqs;
289 ret = vhost_scsi_set_workers(vsc, vs->conf.worker_per_virtqueue);
290 if (ret < 0) {
291 error_setg(errp, "vhost-scsi: vhost worker setup failed: %s",
292 strerror(-ret));
293 goto free_vqs;
296 /* At present, channel and lun both are 0 for bootable vhost-scsi disk */
297 vsc->channel = 0;
298 vsc->lun = 0;
299 /* Note: we can also get the minimum tpgt from kernel */
300 vsc->target = vs->conf.boot_tpgt;
302 return;
304 free_vqs:
305 g_free(vqs);
306 if (!vsc->migratable) {
307 migrate_del_blocker(&vsc->migration_blocker);
309 free_virtio:
310 virtio_scsi_common_unrealize(dev);
311 close_fd:
312 if (vhostfd >= 0) {
313 close(vhostfd);
315 return;
318 static void vhost_scsi_unrealize(DeviceState *dev)
320 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
321 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(dev);
322 struct vhost_virtqueue *vqs = vsc->dev.vqs;
324 if (!vsc->migratable) {
325 migrate_del_blocker(&vsc->migration_blocker);
328 /* This will stop vhost backend. */
329 vhost_scsi_set_status(vdev, 0);
331 vhost_dev_cleanup(&vsc->dev);
332 g_free(vqs);
334 virtio_scsi_common_unrealize(dev);
337 static struct vhost_dev *vhost_scsi_get_vhost(VirtIODevice *vdev)
339 VHostSCSI *s = VHOST_SCSI(vdev);
340 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(s);
341 return &vsc->dev;
344 static Property vhost_scsi_properties[] = {
345 DEFINE_PROP_STRING("vhostfd", VirtIOSCSICommon, conf.vhostfd),
346 DEFINE_PROP_STRING("wwpn", VirtIOSCSICommon, conf.wwpn),
347 DEFINE_PROP_UINT32("boot_tpgt", VirtIOSCSICommon, conf.boot_tpgt, 0),
348 DEFINE_PROP_UINT32("num_queues", VirtIOSCSICommon, conf.num_queues,
349 VIRTIO_SCSI_AUTO_NUM_QUEUES),
350 DEFINE_PROP_UINT32("virtqueue_size", VirtIOSCSICommon, conf.virtqueue_size,
351 128),
352 DEFINE_PROP_BOOL("seg_max_adjust", VirtIOSCSICommon, conf.seg_max_adjust,
353 true),
354 DEFINE_PROP_UINT32("max_sectors", VirtIOSCSICommon, conf.max_sectors,
355 0xFFFF),
356 DEFINE_PROP_UINT32("cmd_per_lun", VirtIOSCSICommon, conf.cmd_per_lun, 128),
357 DEFINE_PROP_BIT64("t10_pi", VHostSCSICommon, host_features,
358 VIRTIO_SCSI_F_T10_PI,
359 false),
360 DEFINE_PROP_BOOL("migratable", VHostSCSICommon, migratable, false),
361 DEFINE_PROP_BOOL("worker_per_virtqueue", VirtIOSCSICommon,
362 conf.worker_per_virtqueue, false),
363 DEFINE_PROP_END_OF_LIST(),
366 static void vhost_scsi_class_init(ObjectClass *klass, void *data)
368 DeviceClass *dc = DEVICE_CLASS(klass);
369 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
370 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
372 device_class_set_props(dc, vhost_scsi_properties);
373 dc->vmsd = &vmstate_virtio_vhost_scsi;
374 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
375 vdc->realize = vhost_scsi_realize;
376 vdc->unrealize = vhost_scsi_unrealize;
377 vdc->get_features = vhost_scsi_common_get_features;
378 vdc->set_config = vhost_scsi_common_set_config;
379 vdc->set_status = vhost_scsi_set_status;
380 vdc->get_vhost = vhost_scsi_get_vhost;
381 fwc->get_dev_path = vhost_scsi_common_get_fw_dev_path;
384 static void vhost_scsi_instance_init(Object *obj)
386 VHostSCSICommon *vsc = VHOST_SCSI_COMMON(obj);
388 vsc->feature_bits = kernel_feature_bits;
390 device_add_bootindex_property(obj, &vsc->bootindex, "bootindex", NULL,
391 DEVICE(vsc));
394 static const TypeInfo vhost_scsi_info = {
395 .name = TYPE_VHOST_SCSI,
396 .parent = TYPE_VHOST_SCSI_COMMON,
397 .instance_size = sizeof(VHostSCSI),
398 .class_init = vhost_scsi_class_init,
399 .instance_init = vhost_scsi_instance_init,
400 .interfaces = (InterfaceInfo[]) {
401 { TYPE_FW_PATH_PROVIDER },
406 static void virtio_register_types(void)
408 type_register_static(&vhost_scsi_info);
411 type_init(virtio_register_types)