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