]> Git Repo - qemu.git/blame - hw/scsi/vhost-scsi.c
qtest: fix vhost-user-test compilation with old GLib
[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"
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"
1c819449 25#include "hw/virtio/virtio-bus.h"
5e9be92d 26
2e6d46d7
NN
27/* Features supported by host kernel. */
28static 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
34};
35
5e9be92d
NB
36static int vhost_scsi_set_endpoint(VHostSCSI *s)
37{
38 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
24d1eb33 39 const VhostOps *vhost_ops = s->dev.vhost_ops;
5e9be92d
NB
40 struct vhost_scsi_target backend;
41 int ret;
42
43 memset(&backend, 0, sizeof(backend));
44 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
24d1eb33 45 ret = vhost_ops->vhost_call(&s->dev, VHOST_SCSI_SET_ENDPOINT, &backend);
5e9be92d
NB
46 if (ret < 0) {
47 return -errno;
48 }
49 return 0;
50}
51
52static void vhost_scsi_clear_endpoint(VHostSCSI *s)
53{
54 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(s);
55 struct vhost_scsi_target backend;
24d1eb33 56 const VhostOps *vhost_ops = s->dev.vhost_ops;
5e9be92d
NB
57
58 memset(&backend, 0, sizeof(backend));
59 pstrcpy(backend.vhost_wwpn, sizeof(backend.vhost_wwpn), vs->conf.wwpn);
24d1eb33 60 vhost_ops->vhost_call(&s->dev, VHOST_SCSI_CLEAR_ENDPOINT, &backend);
5e9be92d
NB
61}
62
63static int vhost_scsi_start(VHostSCSI *s)
64{
65 int ret, abi_version, i;
66 VirtIODevice *vdev = VIRTIO_DEVICE(s);
1c819449
FK
67 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
68 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
24d1eb33 69 const VhostOps *vhost_ops = s->dev.vhost_ops;
5e9be92d 70
1c819449 71 if (!k->set_guest_notifiers) {
5e9be92d
NB
72 error_report("binding does not support guest notifiers");
73 return -ENOSYS;
74 }
75
24d1eb33
NN
76 ret = vhost_ops->vhost_call(&s->dev,
77 VHOST_SCSI_GET_ABI_VERSION, &abi_version);
5e9be92d
NB
78 if (ret < 0) {
79 return -errno;
80 }
81 if (abi_version > VHOST_SCSI_ABI_VERSION) {
82 error_report("vhost-scsi: The running tcm_vhost kernel abi_version:"
83 " %d is greater than vhost_scsi userspace supports: %d, please"
84 " upgrade your version of QEMU\n", abi_version,
85 VHOST_SCSI_ABI_VERSION);
86 return -ENOSYS;
87 }
88
89 ret = vhost_dev_enable_notifiers(&s->dev, vdev);
90 if (ret < 0) {
91 return ret;
92 }
93
94 s->dev.acked_features = vdev->guest_features;
95 ret = vhost_dev_start(&s->dev, vdev);
96 if (ret < 0) {
97 error_report("Error start vhost dev");
98 goto err_notifiers;
99 }
100
101 ret = vhost_scsi_set_endpoint(s);
102 if (ret < 0) {
103 error_report("Error set vhost-scsi endpoint");
104 goto err_vhost_stop;
105 }
106
1c819449 107 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true);
5e9be92d
NB
108 if (ret < 0) {
109 error_report("Error binding guest notifier");
110 goto err_endpoint;
111 }
112
113 /* guest_notifier_mask/pending not used yet, so just unmask
114 * everything here. virtio-pci will do the right thing by
115 * enabling/disabling irqfd.
116 */
117 for (i = 0; i < s->dev.nvqs; i++) {
118 vhost_virtqueue_mask(&s->dev, vdev, i, false);
119 }
120
121 return ret;
122
123err_endpoint:
124 vhost_scsi_clear_endpoint(s);
125err_vhost_stop:
126 vhost_dev_stop(&s->dev, vdev);
127err_notifiers:
128 vhost_dev_disable_notifiers(&s->dev, vdev);
129 return ret;
130}
131
132static void vhost_scsi_stop(VHostSCSI *s)
133{
134 VirtIODevice *vdev = VIRTIO_DEVICE(s);
1c819449
FK
135 BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev)));
136 VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus);
5e9be92d
NB
137 int ret = 0;
138
0e22a2d1 139 if (k->set_guest_notifiers) {
1c819449 140 ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false);
5e9be92d
NB
141 if (ret < 0) {
142 error_report("vhost guest notifier cleanup failed: %d\n", ret);
143 }
144 }
145 assert(ret >= 0);
146
147 vhost_scsi_clear_endpoint(s);
148 vhost_dev_stop(&s->dev, vdev);
149 vhost_dev_disable_notifiers(&s->dev, vdev);
150}
151
152static uint32_t vhost_scsi_get_features(VirtIODevice *vdev,
153 uint32_t features)
154{
155 VHostSCSI *s = VHOST_SCSI(vdev);
156
2e6d46d7 157 return vhost_get_features(&s->dev, kernel_feature_bits, features);
5e9be92d
NB
158}
159
160static void vhost_scsi_set_config(VirtIODevice *vdev,
161 const uint8_t *config)
162{
163 VirtIOSCSIConfig *scsiconf = (VirtIOSCSIConfig *)config;
164 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(vdev);
165
0983979b
PB
166 if ((uint32_t) ldl_p(&scsiconf->sense_size) != vs->sense_size ||
167 (uint32_t) ldl_p(&scsiconf->cdb_size) != vs->cdb_size) {
5e9be92d
NB
168 error_report("vhost-scsi does not support changing the sense data and CDB sizes");
169 exit(1);
170 }
171}
172
173static void vhost_scsi_set_status(VirtIODevice *vdev, uint8_t val)
174{
175 VHostSCSI *s = (VHostSCSI *)vdev;
176 bool start = (val & VIRTIO_CONFIG_S_DRIVER_OK);
177
178 if (s->dev.started == start) {
179 return;
180 }
181
182 if (start) {
183 int ret;
184
185 ret = vhost_scsi_start(s);
186 if (ret < 0) {
187 error_report("virtio-scsi: unable to start vhost: %s\n",
188 strerror(-ret));
189
190 /* There is no userspace virtio-scsi fallback so exit */
191 exit(1);
192 }
193 } else {
194 vhost_scsi_stop(s);
195 }
196}
197
91d670fb
ML
198static void vhost_dummy_handle_output(VirtIODevice *vdev, VirtQueue *vq)
199{
200}
201
71a6520b 202static void vhost_scsi_realize(DeviceState *dev, Error **errp)
5e9be92d 203{
71a6520b
AF
204 VirtIOSCSICommon *vs = VIRTIO_SCSI_COMMON(dev);
205 VHostSCSI *s = VHOST_SCSI(dev);
206 Error *err = NULL;
5e9be92d
NB
207 int vhostfd = -1;
208 int ret;
209
210 if (!vs->conf.wwpn) {
71a6520b
AF
211 error_setg(errp, "vhost-scsi: missing wwpn");
212 return;
5e9be92d
NB
213 }
214
215 if (vs->conf.vhostfd) {
216 vhostfd = monitor_handle_fd_param(cur_mon, vs->conf.vhostfd);
217 if (vhostfd == -1) {
71a6520b
AF
218 error_setg(errp, "vhost-scsi: unable to parse vhostfd");
219 return;
5e9be92d 220 }
81647a65
NN
221 } else {
222 vhostfd = open("/dev/vhost-scsi", O_RDWR);
223 if (vhostfd < 0) {
224 error_setg(errp, "vhost-scsi: open vhost char device failed: %s",
225 strerror(errno));
226 return;
227 }
5e9be92d
NB
228 }
229
91d670fb
ML
230 virtio_scsi_common_realize(dev, &err, vhost_dummy_handle_output,
231 vhost_dummy_handle_output,
232 vhost_dummy_handle_output);
71a6520b
AF
233 if (err != NULL) {
234 error_propagate(errp, err);
235 return;
5e9be92d
NB
236 }
237
5e9be92d
NB
238 s->dev.nvqs = VHOST_SCSI_VQ_NUM_FIXED + vs->conf.num_queues;
239 s->dev.vqs = g_new(struct vhost_virtqueue, s->dev.nvqs);
240 s->dev.vq_index = 0;
241
81647a65 242 ret = vhost_dev_init(&s->dev, (void *)(uintptr_t)vhostfd,
1a1bfac9 243 VHOST_BACKEND_TYPE_KERNEL, true);
5e9be92d 244 if (ret < 0) {
71a6520b
AF
245 error_setg(errp, "vhost-scsi: vhost initialization failed: %s",
246 strerror(-ret));
247 return;
5e9be92d
NB
248 }
249 s->dev.backend_features = 0;
250
251 error_setg(&s->migration_blocker,
252 "vhost-scsi does not support migration");
253 migrate_add_blocker(s->migration_blocker);
5e9be92d
NB
254}
255
306ec6c3 256static void vhost_scsi_unrealize(DeviceState *dev, Error **errp)
5e9be92d 257{
306ec6c3
AF
258 VirtIODevice *vdev = VIRTIO_DEVICE(dev);
259 VHostSCSI *s = VHOST_SCSI(dev);
5e9be92d
NB
260
261 migrate_del_blocker(s->migration_blocker);
262 error_free(s->migration_blocker);
263
264 /* This will stop vhost backend. */
265 vhost_scsi_set_status(vdev, 0);
266
267 g_free(s->dev.vqs);
306ec6c3
AF
268
269 virtio_scsi_common_unrealize(dev, errp);
5e9be92d
NB
270}
271
272static Property vhost_scsi_properties[] = {
273 DEFINE_VHOST_SCSI_PROPERTIES(VHostSCSI, parent_obj.conf),
274 DEFINE_PROP_END_OF_LIST(),
275};
276
277static void vhost_scsi_class_init(ObjectClass *klass, void *data)
278{
279 DeviceClass *dc = DEVICE_CLASS(klass);
280 VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass);
71a6520b 281
5e9be92d 282 dc->props = vhost_scsi_properties;
125ee0ed 283 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories);
71a6520b 284 vdc->realize = vhost_scsi_realize;
306ec6c3 285 vdc->unrealize = vhost_scsi_unrealize;
5e9be92d
NB
286 vdc->get_features = vhost_scsi_get_features;
287 vdc->set_config = vhost_scsi_set_config;
288 vdc->set_status = vhost_scsi_set_status;
289}
290
291static const TypeInfo vhost_scsi_info = {
292 .name = TYPE_VHOST_SCSI,
293 .parent = TYPE_VIRTIO_SCSI_COMMON,
294 .instance_size = sizeof(VHostSCSI),
295 .class_init = vhost_scsi_class_init,
296};
297
298static void virtio_register_types(void)
299{
300 type_register_static(&vhost_scsi_info);
301}
302
303type_init(virtio_register_types)
This page took 0.157148 seconds and 4 git commands to generate.