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