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