]> Git Repo - qemu.git/blame - hw/scsi/vhost-scsi.c
vhost-user: add a migration blocker
[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 <[email protected]>
8 *
9 * Changes for QEMU mainline + tcm_vhost kernel upstream:
10 * Nicholas Bellinger <[email protected]>
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
17#include <sys/ioctl.h>
18#include "config.h"
d49b6836 19#include "qemu/error-report.h"
5e9be92d
NB
20#include "qemu/queue.h"
21#include "monitor/monitor.h"
22#include "migration/migration.h"
23#include "hw/virtio/vhost-scsi.h"
24#include "hw/virtio/vhost.h"
25#include "hw/virtio/virtio-scsi.h"
1c819449 26#include "hw/virtio/virtio-bus.h"
7ce04255 27#include "hw/virtio/virtio-access.h"
1956cf6f 28#include "hw/fw-path-provider.h"
50088776 29#include "linux/vhost.h"
5e9be92d 30
2e6d46d7
NN
31/* Features supported by host kernel. */
32static const int kernel_feature_bits[] = {
33 VIRTIO_F_NOTIFY_ON_EMPTY,
34 VIRTIO_RING_F_INDIRECT_DESC,
35 VIRTIO_RING_F_EVENT_IDX,
36 VIRTIO_SCSI_F_HOTPLUG,
37 VHOST_INVALID_FEATURE_BIT
38};
39
5e9be92d
NB
40static int vhost_scsi_set_endpoint(VHostSCSI *s)
41{
42 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
24d1eb33 43 const VhostOps *vhost_ops = s->dev.vhost_ops;
5e9be92d
NB
44 struct vhost_scsi_target backend;
45 int ret;
46
47 memset(&backend, 0, sizeof(backend));
48 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
24d1eb33 49 ret = vhost_ops->vhost_call(&s->dev, VHOST_SCSI_SET_ENDPOINT, &backend);
5e9be92d
NB
50 if (ret < 0) {
51 return -errno;
52 }
53 return 0;
54}
55
56static void vhost_scsi_clear_endpoint(VHostSCSI *s)
57{
58 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
59 struct vhost_scsi_target backend;
24d1eb33 60 const VhostOps *vhost_ops = s->dev.vhost_ops;
5e9be92d
NB
61
62 memset(&backend, 0, sizeof(backend));
63 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
24d1eb33 64 vhost_ops->vhost_call(&s->dev, VHOST_SCSI_CLEAR_ENDPOINT, &backend);
5e9be92d
NB
65}
66
67static int vhost_scsi_start(VHostSCSI *s)
68{
69 int ret, abi_version, i;
70 VirtIODevice *vdev = VIRTIO_DEVICE(s);
1c819449
FK
71 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
72 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
24d1eb33 73 const VhostOps *vhost_ops = s->dev.vhost_ops;
5e9be92d 74
1c819449 75 if (!k->set_guest_notifiers) {
5e9be92d
NB
76 error_report("binding does not support guest notifiers");
77 return -ENOSYS;
78 }
79
24d1eb33
NN
80 ret = vhost_ops->vhost_call(&s->dev,
81 VHOST_SCSI_GET_ABI_VERSION, &abi_version);
5e9be92d
NB
82 if (ret < 0) {
83 return -errno;
84 }
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"
f6a16175 88 " upgrade your version of QEMU", abi_version,
5e9be92d
NB
89 VHOST_SCSI_ABI_VERSION);
90 return -ENOSYS;
91 }
92
93 ret = vhost_dev_enable_notifiers(&s->dev, vdev);
94 if (ret < 0) {
95 return ret;
96 }
97
98 s->dev.acked_features = vdev->guest_features;
99 ret = vhost_dev_start(&s->dev, vdev);
100 if (ret < 0) {
101 error_report("Error start vhost dev");
102 goto err_notifiers;
103 }
104
105 ret = vhost_scsi_set_endpoint(s);
106 if (ret < 0) {
107 error_report("Error set vhost-scsi endpoint");
108 goto err_vhost_stop;
109 }
110
1c819449 111 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
5e9be92d
NB
112 if (ret < 0) {
113 error_report("Error binding guest notifier");
114 goto err_endpoint;
115 }
116
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.
120 */
121 for (i = 0; i < s->dev.nvqs; i++) {
fe2d1a81 122 vhost_virtqueue_mask(&s->dev, vdev, s->dev.vq_index + i, false);
5e9be92d
NB
123 }
124
125 return ret;
126
127err_endpoint:
128 vhost_scsi_clear_endpoint(s);
129err_vhost_stop:
130 vhost_dev_stop(&s->dev, vdev);
131err_notifiers:
132 vhost_dev_disable_notifiers(&s->dev, vdev);
133 return ret;
134}
135
136static void vhost_scsi_stop(VHostSCSI *s)
137{
138 VirtIODevice *vdev = VIRTIO_DEVICE(s);
1c819449
FK
139 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
140 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
5e9be92d
NB
141 int ret = 0;
142
0e22a2d1 143 if (k->set_guest_notifiers) {
1c819449 144 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
5e9be92d 145 if (ret < 0) {
f6a16175 146 error_report("vhost guest notifier cleanup failed: %d", ret);
5e9be92d
NB
147 }
148 }
149 assert(ret >= 0);
150
151 vhost_scsi_clear_endpoint(s);
152 vhost_dev_stop(&s->dev, vdev);
153 vhost_dev_disable_notifiers(&s->dev, vdev);
154}
155
019a3edb 156static uint64_t vhost_scsi_get_features(VirtIODevice *vdev,
9d5b731d
JW
157 uint64_t features,
158 Error **errp)
5e9be92d
NB
159{
160 VHostSCSI *s = VHOST_SCSI(vdev);
161
2e6d46d7 162 return vhost_get_features(&s->dev, kernel_feature_bits, features);
5e9be92d
NB
163}
164
165static void vhost_scsi_set_config(VirtIODevice *vdev,
166 const uint8_t *config)
167{
168 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
169 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
170
7ce04255
PB
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) {
5e9be92d
NB
173 error_report("vhost-scsi does not support changing the sense data and CDB sizes");
174 exit(1);
175 }
176}
177
178static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val)
179{
180 VHostSCSI *s = (VHostSCSI *)vdev;
181 bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK);
182
183 if (s->dev.started == start) {
184 return;
185 }
186
187 if (start) {
188 int ret;
189
190 ret = vhost_scsi_start(s);
191 if (ret < 0) {
f6a16175 192 error_report("virtio-scsi: unable to start vhost: %s",
5e9be92d
NB
193 strerror(-ret));
194
195 /* There is no userspace virtio-scsi fallback so exit */
196 exit(1);
197 }
198 } else {
199 vhost_scsi_stop(s);
200 }
201}
202
91d670fb
ML
203static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
204{
205}
206
71a6520b 207static void vhost_scsi_realize(DeviceState *dev, Error **errp)
5e9be92d 208{
71a6520b
AF
209 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
210 VHostSCSI *s = VHOST_SCSI(dev);
211 Error *err = NULL;
5e9be92d
NB
212 int vhostfd = -1;
213 int ret;
214
215 if (!vs->conf.wwpn) {
71a6520b
AF
216 error_setg(errp, "vhost-scsi: missing wwpn");
217 return;
5e9be92d
NB
218 }
219
220 if (vs->conf.vhostfd) {
1677f4c6 221 vhostfd = monitor_fd_param(cur_mon, vs->conf.vhostfd, &err);
5e9be92d 222 if (vhostfd == -1) {
248337e1
MA
223 error_setg(errp, "vhost-scsi: unable to parse vhostfd: %s",
224 error_get_pretty(err));
225 error_free(err);
71a6520b 226 return;
5e9be92d 227 }
81647a65
NN
228 } else {
229 vhostfd = open("/dev/vhost-scsi", O_RDWR);
230 if (vhostfd < 0) {
231 error_setg(errp, "vhost-scsi: open vhost char device failed: %s",
232 strerror(errno));
233 return;
234 }
5e9be92d
NB
235 }
236
91d670fb
ML
237 virtio_scsi_common_realize(dev, &err, vhost_dummy_handle_output,
238 vhost_dummy_handle_output,
239 vhost_dummy_handle_output);
71a6520b
AF
240 if (err != NULL) {
241 error_propagate(errp, err);
b19ca188 242 close(vhostfd);
71a6520b 243 return;
5e9be92d
NB
244 }
245
5e9be92d
NB
246 s->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
247 s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs);
248 s->dev.vq_index = 0;
3a1655fc 249 s->dev.backend_features = 0;
5e9be92d 250
81647a65 251 ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd,
1e7398a1 252 VHOST_BACKEND_TYPE_KERNEL);
5e9be92d 253 if (ret < 0) {
71a6520b
AF
254 error_setg(errp, "vhost-scsi: vhost initialization failed: %s",
255 strerror(-ret));
256 return;
5e9be92d 257 }
5e9be92d 258
444c7e0d
GA
259 /* At present, channel and lun both are 0 for bootable vhost-scsi disk */
260 s->channel = 0;
261 s->lun = 0;
262 /* Note: we can also get the minimum tpgt from kernel */
263 s->target = vs->conf.boot_tpgt;
264
5e9be92d
NB
265 error_setg(&s->migration_blocker,
266 "vhost-scsi does not support migration");
267 migrate_add_blocker(s->migration_blocker);
5e9be92d
NB
268}
269
306ec6c3 270static void vhost_scsi_unrealize(DeviceState *dev, Error **errp)
5e9be92d 271{
306ec6c3
AF
272 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
273 VHostSCSI *s = VHOST_SCSI(dev);
5e9be92d
NB
274
275 migrate_del_blocker(s->migration_blocker);
276 error_free(s->migration_blocker);
277
278 /* This will stop vhost backend. */
279 vhost_scsi_set_status(vdev, 0);
280
af103c93 281 vhost_dev_cleanup(&s->dev);
5e9be92d 282 g_free(s->dev.vqs);
306ec6c3
AF
283
284 virtio_scsi_common_unrealize(dev, errp);
5e9be92d
NB
285}
286
1956cf6f
GA
287/*
288 * Implementation of an interface to adjust firmware path
289 * for the bootindex property handling.
290 */
291static char *vhost_scsi_get_fw_dev_path(FWPathProvider *p, BusState *bus,
292 DeviceState *dev)
293{
294 VHostSCSI *s = VHOST_SCSI(dev);
295 /* format: channel@channel/vhost-scsi@target,lun */
f42bf6a2 296 return g_strdup_printf("/channel@%x/%s@%x,%x", s->channel,
1956cf6f
GA
297 qdev_fw_name(dev), s->target, s->lun);
298}
299
5e9be92d 300static Property vhost_scsi_properties[] = {
21549a46
SZ
301 DEFINE_PROP_STRING("vhostfd", VHostSCSI, parent_obj.conf.vhostfd),
302 DEFINE_PROP_STRING("wwpn", VHostSCSI, parent_obj.conf.wwpn),
303 DEFINE_PROP_UINT32("boot_tpgt", VHostSCSI, parent_obj.conf.boot_tpgt, 0),
304 DEFINE_PROP_UINT32("num_queues", VHostSCSI, parent_obj.conf.num_queues, 1),
305 DEFINE_PROP_UINT32("max_sectors", VHostSCSI, parent_obj.conf.max_sectors,
306 0xFFFF),
307 DEFINE_PROP_UINT32("cmd_per_lun", VHostSCSI, parent_obj.conf.cmd_per_lun,
308 128),
5e9be92d
NB
309 DEFINE_PROP_END_OF_LIST(),
310};
311
312static void vhost_scsi_class_init(ObjectClass *klass, void *data)
313{
314 DeviceClass *dc = DEVICE_CLASS(klass);
315 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
1956cf6f 316 FWPathProviderClass *fwc = FW_PATH_PROVIDER_CLASS(klass);
71a6520b 317
5e9be92d 318 dc->props = vhost_scsi_properties;
125ee0ed 319 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
71a6520b 320 vdc->realize = vhost_scsi_realize;
306ec6c3 321 vdc->unrealize = vhost_scsi_unrealize;
5e9be92d
NB
322 vdc->get_features = vhost_scsi_get_features;
323 vdc->set_config = vhost_scsi_set_config;
324 vdc->set_status = vhost_scsi_set_status;
1956cf6f 325 fwc->get_dev_path = vhost_scsi_get_fw_dev_path;
5e9be92d
NB
326}
327
d4433f32
GA
328static void vhost_scsi_instance_init(Object *obj)
329{
330 VHostSCSI *dev = VHOST_SCSI(obj);
331
332 device_add_bootindex_property(obj, &dev->bootindex, "bootindex", NULL,
333 DEVICE(dev), NULL);
334}
335
5e9be92d
NB
336static const TypeInfo vhost_scsi_info = {
337 .name = TYPE_VHOST_SCSI,
338 .parent = TYPE_VIRTIO_SCSI_COMMON,
339 .instance_size = sizeof(VHostSCSI),
340 .class_init = vhost_scsi_class_init,
d4433f32 341 .instance_init = vhost_scsi_instance_init,
1956cf6f
GA
342 .interfaces = (InterfaceInfo[]) {
343 { TYPE_FW_PATH_PROVIDER },
344 { }
345 },
5e9be92d
NB
346};
347
348static void virtio_register_types(void)
349{
350 type_register_static(&vhost_scsi_info);
351}
352
353type_init(virtio_register_types)
This page took 0.221755 seconds and 4 git commands to generate.