]>
Commit | Line | Data |
---|---|---|
00343e4b CL |
1 | /* |
2 | * vhost-user-blk host device | |
3 | * | |
4 | * Copyright(C) 2017 Intel Corporation. | |
5 | * | |
6 | * Authors: | |
7 | * Changpeng Liu <[email protected]> | |
8 | * | |
9 | * Largely based on the "vhost-user-scsi.c" and "vhost-scsi.c" implemented by: | |
10 | * Felipe Franciosi <[email protected]> | |
11 | * Stefan Hajnoczi <[email protected]> | |
12 | * Nicholas Bellinger <[email protected]> | |
13 | * | |
14 | * This work is licensed under the terms of the GNU LGPL, version 2 or later. | |
15 | * See the COPYING.LIB file in the top-level directory. | |
16 | * | |
17 | */ | |
18 | ||
19 | #include "qemu/osdep.h" | |
20 | #include "qapi/error.h" | |
21 | #include "qemu/error-report.h" | |
00343e4b | 22 | #include "qemu/cutils.h" |
00343e4b | 23 | #include "hw/qdev-core.h" |
a27bd6c7 | 24 | #include "hw/qdev-properties.h" |
ce35e229 | 25 | #include "hw/qdev-properties-system.h" |
8edd5673 | 26 | #include "hw/virtio/virtio-blk-common.h" |
00343e4b CL |
27 | #include "hw/virtio/vhost.h" |
28 | #include "hw/virtio/vhost-user-blk.h" | |
29 | #include "hw/virtio/virtio.h" | |
30 | #include "hw/virtio/virtio-bus.h" | |
31 | #include "hw/virtio/virtio-access.h" | |
2f780b6a | 32 | #include "sysemu/sysemu.h" |
54d31236 | 33 | #include "sysemu/runstate.h" |
00343e4b | 34 | |
a527e312 KW |
35 | #define REALIZE_CONNECTION_RETRIES 3 |
36 | ||
00343e4b CL |
37 | static const int user_feature_bits[] = { |
38 | VIRTIO_BLK_F_SIZE_MAX, | |
39 | VIRTIO_BLK_F_SEG_MAX, | |
40 | VIRTIO_BLK_F_GEOMETRY, | |
41 | VIRTIO_BLK_F_BLK_SIZE, | |
42 | VIRTIO_BLK_F_TOPOLOGY, | |
43 | VIRTIO_BLK_F_MQ, | |
44 | VIRTIO_BLK_F_RO, | |
45 | VIRTIO_BLK_F_FLUSH, | |
46 | VIRTIO_BLK_F_CONFIG_WCE, | |
caa1ee43 CL |
47 | VIRTIO_BLK_F_DISCARD, |
48 | VIRTIO_BLK_F_WRITE_ZEROES, | |
00343e4b CL |
49 | VIRTIO_F_VERSION_1, |
50 | VIRTIO_RING_F_INDIRECT_DESC, | |
51 | VIRTIO_RING_F_EVENT_IDX, | |
52 | VIRTIO_F_NOTIFY_ON_EMPTY, | |
7556a320 KW |
53 | VIRTIO_F_RING_PACKED, |
54 | VIRTIO_F_IOMMU_PLATFORM, | |
562a7d23 | 55 | VIRTIO_F_RING_RESET, |
00343e4b CL |
56 | VHOST_INVALID_FEATURE_BIT |
57 | }; | |
58 | ||
dabefdd6 KW |
59 | static void vhost_user_blk_event(void *opaque, QEMUChrEvent event); |
60 | ||
00343e4b CL |
61 | static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config) |
62 | { | |
63 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
64 | ||
535255b4 SH |
65 | /* Our num_queues overrides the device backend */ |
66 | virtio_stw_p(vdev, &s->blkcfg.num_queues, s->num_queues); | |
67 | ||
8edd5673 | 68 | memcpy(config, &s->blkcfg, vdev->config_len); |
00343e4b CL |
69 | } |
70 | ||
71 | static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config) | |
72 | { | |
73 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
74 | struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config; | |
75 | int ret; | |
76 | ||
77 | if (blkcfg->wce == s->blkcfg.wce) { | |
78 | return; | |
79 | } | |
80 | ||
81 | ret = vhost_dev_set_config(&s->dev, &blkcfg->wce, | |
82 | offsetof(struct virtio_blk_config, wce), | |
83 | sizeof(blkcfg->wce), | |
84 | VHOST_SET_CONFIG_TYPE_MASTER); | |
85 | if (ret) { | |
86 | error_report("set device config space failed"); | |
87 | return; | |
88 | } | |
89 | ||
90 | s->blkcfg.wce = blkcfg->wce; | |
91 | } | |
92 | ||
93 | static int vhost_user_blk_handle_config_change(struct vhost_dev *dev) | |
94 | { | |
95 | int ret; | |
96 | struct virtio_blk_config blkcfg; | |
8edd5673 | 97 | VirtIODevice *vdev = dev->vdev; |
00343e4b | 98 | VHostUserBlk *s = VHOST_USER_BLK(dev->vdev); |
50de5138 | 99 | Error *local_err = NULL; |
00343e4b | 100 | |
ab6075d8 LF |
101 | if (!dev->started) { |
102 | return 0; | |
103 | } | |
104 | ||
00343e4b | 105 | ret = vhost_dev_get_config(dev, (uint8_t *)&blkcfg, |
8edd5673 | 106 | vdev->config_len, &local_err); |
00343e4b | 107 | if (ret < 0) { |
50de5138 | 108 | error_report_err(local_err); |
fb767859 | 109 | return ret; |
00343e4b CL |
110 | } |
111 | ||
112 | /* valid for resize only */ | |
113 | if (blkcfg.capacity != s->blkcfg.capacity) { | |
114 | s->blkcfg.capacity = blkcfg.capacity; | |
8edd5673 | 115 | memcpy(dev->vdev->config, &s->blkcfg, vdev->config_len); |
00343e4b CL |
116 | virtio_notify_config(dev->vdev); |
117 | } | |
118 | ||
119 | return 0; | |
120 | } | |
121 | ||
122 | const VhostDevConfigOps blk_ops = { | |
123 | .vhost_dev_config_notifier = vhost_user_blk_handle_config_change, | |
124 | }; | |
125 | ||
b8da6568 | 126 | static int vhost_user_blk_start(VirtIODevice *vdev, Error **errp) |
00343e4b CL |
127 | { |
128 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
129 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); | |
130 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
131 | int i, ret; | |
132 | ||
133 | if (!k->set_guest_notifiers) { | |
b8da6568 | 134 | error_setg(errp, "binding does not support guest notifiers"); |
a57f0091 | 135 | return -ENOSYS; |
00343e4b CL |
136 | } |
137 | ||
138 | ret = vhost_dev_enable_notifiers(&s->dev, vdev); | |
139 | if (ret < 0) { | |
b8da6568 | 140 | error_setg_errno(errp, -ret, "Error enabling host notifiers"); |
a57f0091 | 141 | return ret; |
00343e4b CL |
142 | } |
143 | ||
144 | ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true); | |
145 | if (ret < 0) { | |
b8da6568 | 146 | error_setg_errno(errp, -ret, "Error binding guest notifier"); |
00343e4b CL |
147 | goto err_host_notifiers; |
148 | } | |
149 | ||
150 | s->dev.acked_features = vdev->guest_features; | |
a1fe0b8f | 151 | |
1b0063b3 JY |
152 | ret = vhost_dev_prepare_inflight(&s->dev, vdev); |
153 | if (ret < 0) { | |
b8da6568 | 154 | error_setg_errno(errp, -ret, "Error setting inflight format"); |
1b0063b3 JY |
155 | goto err_guest_notifiers; |
156 | } | |
157 | ||
a1fe0b8f XY |
158 | if (!s->inflight->addr) { |
159 | ret = vhost_dev_get_inflight(&s->dev, s->queue_size, s->inflight); | |
160 | if (ret < 0) { | |
b8da6568 | 161 | error_setg_errno(errp, -ret, "Error getting inflight"); |
a1fe0b8f XY |
162 | goto err_guest_notifiers; |
163 | } | |
164 | } | |
165 | ||
166 | ret = vhost_dev_set_inflight(&s->dev, s->inflight); | |
167 | if (ret < 0) { | |
b8da6568 | 168 | error_setg_errno(errp, -ret, "Error setting inflight"); |
a1fe0b8f XY |
169 | goto err_guest_notifiers; |
170 | } | |
171 | ||
00343e4b CL |
172 | /* guest_notifier_mask/pending not used yet, so just unmask |
173 | * everything here. virtio-pci will do the right thing by | |
174 | * enabling/disabling irqfd. | |
175 | */ | |
176 | for (i = 0; i < s->dev.nvqs; i++) { | |
177 | vhost_virtqueue_mask(&s->dev, vdev, i, false); | |
178 | } | |
179 | ||
8b67fe00 | 180 | s->dev.vq_index_end = s->dev.nvqs; |
4daa5054 | 181 | ret = vhost_dev_start(&s->dev, vdev, true); |
8b67fe00 YW |
182 | if (ret < 0) { |
183 | error_setg_errno(errp, -ret, "Error starting vhost"); | |
184 | goto err_guest_notifiers; | |
185 | } | |
186 | s->started_vu = true; | |
187 | ||
a57f0091 | 188 | return ret; |
00343e4b CL |
189 | |
190 | err_guest_notifiers: | |
8b67fe00 YW |
191 | for (i = 0; i < s->dev.nvqs; i++) { |
192 | vhost_virtqueue_mask(&s->dev, vdev, i, true); | |
193 | } | |
00343e4b CL |
194 | k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); |
195 | err_host_notifiers: | |
196 | vhost_dev_disable_notifiers(&s->dev, vdev); | |
a57f0091 | 197 | return ret; |
00343e4b CL |
198 | } |
199 | ||
200 | static void vhost_user_blk_stop(VirtIODevice *vdev) | |
201 | { | |
202 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
203 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); | |
204 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); | |
205 | int ret; | |
206 | ||
f5b22d06 DS |
207 | if (!s->started_vu) { |
208 | return; | |
209 | } | |
210 | s->started_vu = false; | |
211 | ||
00343e4b CL |
212 | if (!k->set_guest_notifiers) { |
213 | return; | |
214 | } | |
215 | ||
4daa5054 | 216 | vhost_dev_stop(&s->dev, vdev, true); |
00343e4b CL |
217 | |
218 | ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); | |
219 | if (ret < 0) { | |
220 | error_report("vhost guest notifier cleanup failed: %d", ret); | |
221 | return; | |
222 | } | |
223 | ||
224 | vhost_dev_disable_notifiers(&s->dev, vdev); | |
225 | } | |
226 | ||
227 | static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status) | |
228 | { | |
229 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
259d69c0 | 230 | bool should_start = virtio_device_should_start(vdev, status); |
b8da6568 | 231 | Error *local_err = NULL; |
77542d43 | 232 | int ret; |
00343e4b | 233 | |
77542d43 XY |
234 | if (!s->connected) { |
235 | return; | |
236 | } | |
237 | ||
b8f3e6a1 | 238 | if (vhost_dev_is_started(&s->dev) == should_start) { |
00343e4b CL |
239 | return; |
240 | } | |
241 | ||
242 | if (should_start) { | |
b8da6568 | 243 | ret = vhost_user_blk_start(vdev, &local_err); |
77542d43 | 244 | if (ret < 0) { |
b8da6568 | 245 | error_reportf_err(local_err, "vhost-user-blk: vhost start failed: "); |
77542d43 XY |
246 | qemu_chr_fe_disconnect(&s->chardev); |
247 | } | |
00343e4b CL |
248 | } else { |
249 | vhost_user_blk_stop(vdev); | |
250 | } | |
251 | ||
252 | } | |
253 | ||
254 | static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev, | |
255 | uint64_t features, | |
256 | Error **errp) | |
257 | { | |
258 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
00343e4b CL |
259 | |
260 | /* Turn on pre-defined features */ | |
0a963af3 | 261 | virtio_add_feature(&features, VIRTIO_BLK_F_SIZE_MAX); |
00343e4b CL |
262 | virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX); |
263 | virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY); | |
264 | virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY); | |
265 | virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE); | |
266 | virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH); | |
25b1d45a | 267 | virtio_add_feature(&features, VIRTIO_BLK_F_RO); |
00343e4b | 268 | |
00343e4b CL |
269 | if (s->num_queues > 1) { |
270 | virtio_add_feature(&features, VIRTIO_BLK_F_MQ); | |
271 | } | |
272 | ||
4a4ff4c5 | 273 | return vhost_get_features(&s->dev, user_feature_bits, features); |
00343e4b CL |
274 | } |
275 | ||
276 | static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) | |
277 | { | |
110b9463 | 278 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
b8da6568 | 279 | Error *local_err = NULL; |
77542d43 | 280 | int i, ret; |
110b9463 | 281 | |
f3facbe6 | 282 | if (!vdev->start_on_kick) { |
110b9463 YX |
283 | return; |
284 | } | |
285 | ||
77542d43 XY |
286 | if (!s->connected) { |
287 | return; | |
288 | } | |
289 | ||
b8f3e6a1 | 290 | if (vhost_dev_is_started(&s->dev)) { |
110b9463 YX |
291 | return; |
292 | } | |
293 | ||
294 | /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start | |
295 | * vhost here instead of waiting for .set_status(). | |
296 | */ | |
b8da6568 | 297 | ret = vhost_user_blk_start(vdev, &local_err); |
77542d43 | 298 | if (ret < 0) { |
b8da6568 | 299 | error_reportf_err(local_err, "vhost-user-blk: vhost start failed: "); |
77542d43 XY |
300 | qemu_chr_fe_disconnect(&s->chardev); |
301 | return; | |
302 | } | |
00343e4b | 303 | |
110b9463 YX |
304 | /* Kick right away to begin processing requests already in vring */ |
305 | for (i = 0; i < s->dev.nvqs; i++) { | |
306 | VirtQueue *kick_vq = virtio_get_queue(vdev, i); | |
307 | ||
308 | if (!virtio_queue_get_desc_addr(vdev, i)) { | |
309 | continue; | |
310 | } | |
311 | event_notifier_set(virtio_queue_get_host_notifier(kick_vq)); | |
312 | } | |
00343e4b CL |
313 | } |
314 | ||
a1fe0b8f XY |
315 | static void vhost_user_blk_reset(VirtIODevice *vdev) |
316 | { | |
317 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
318 | ||
319 | vhost_dev_free_inflight(s->inflight); | |
320 | } | |
321 | ||
5b9243d2 | 322 | static int vhost_user_blk_connect(DeviceState *dev, Error **errp) |
77542d43 XY |
323 | { |
324 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
325 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
326 | int ret = 0; | |
327 | ||
328 | if (s->connected) { | |
329 | return 0; | |
330 | } | |
331 | s->connected = true; | |
332 | ||
c90bd505 | 333 | s->dev.num_queues = s->num_queues; |
77542d43 | 334 | s->dev.nvqs = s->num_queues; |
38e245a4 | 335 | s->dev.vqs = s->vhost_vqs; |
77542d43 XY |
336 | s->dev.vq_index = 0; |
337 | s->dev.backend_features = 0; | |
338 | ||
339 | vhost_dev_set_config_notifier(&s->dev, &blk_ops); | |
340 | ||
06cb5c82 | 341 | s->vhost_user.supports_config = true; |
a6945f22 KW |
342 | ret = vhost_dev_init(&s->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0, |
343 | errp); | |
77542d43 | 344 | if (ret < 0) { |
77542d43 XY |
345 | return ret; |
346 | } | |
347 | ||
348 | /* restore vhost state */ | |
e57f2c31 | 349 | if (virtio_device_started(vdev, vdev->status)) { |
b8da6568 | 350 | ret = vhost_user_blk_start(vdev, errp); |
77542d43 | 351 | if (ret < 0) { |
77542d43 XY |
352 | return ret; |
353 | } | |
354 | } | |
355 | ||
356 | return 0; | |
357 | } | |
358 | ||
359 | static void vhost_user_blk_disconnect(DeviceState *dev) | |
360 | { | |
361 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
362 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
363 | ||
364 | if (!s->connected) { | |
365 | return; | |
366 | } | |
367 | s->connected = false; | |
368 | ||
f5b22d06 | 369 | vhost_user_blk_stop(vdev); |
77542d43 XY |
370 | |
371 | vhost_dev_cleanup(&s->dev); | |
77542d43 | 372 | |
71e076a0 | 373 | /* Re-instate the event handler for new connections */ |
dabefdd6 | 374 | qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event, |
71e076a0 | 375 | NULL, dev, NULL, true); |
4bcad76f DS |
376 | } |
377 | ||
dabefdd6 | 378 | static void vhost_user_blk_event(void *opaque, QEMUChrEvent event) |
77542d43 XY |
379 | { |
380 | DeviceState *dev = opaque; | |
381 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
382 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
5b9243d2 | 383 | Error *local_err = NULL; |
77542d43 XY |
384 | |
385 | switch (event) { | |
386 | case CHR_EVENT_OPENED: | |
5b9243d2 KW |
387 | if (vhost_user_blk_connect(dev, &local_err) < 0) { |
388 | error_report_err(local_err); | |
77542d43 XY |
389 | qemu_chr_fe_disconnect(&s->chardev); |
390 | return; | |
391 | } | |
77542d43 XY |
392 | break; |
393 | case CHR_EVENT_CLOSED: | |
71e076a0 AB |
394 | /* defer close until later to avoid circular close */ |
395 | vhost_user_async_close(dev, &s->chardev, &s->dev, | |
396 | vhost_user_blk_disconnect); | |
77542d43 | 397 | break; |
669457f3 PMD |
398 | case CHR_EVENT_BREAK: |
399 | case CHR_EVENT_MUX_IN: | |
400 | case CHR_EVENT_MUX_OUT: | |
401 | /* Ignore */ | |
402 | break; | |
77542d43 XY |
403 | } |
404 | } | |
405 | ||
415fc294 KW |
406 | static int vhost_user_blk_realize_connect(VHostUserBlk *s, Error **errp) |
407 | { | |
408 | DeviceState *dev = &s->parent_obj.parent_obj; | |
409 | int ret; | |
410 | ||
411 | s->connected = false; | |
412 | ||
413 | ret = qemu_chr_fe_wait_connected(&s->chardev, errp); | |
414 | if (ret < 0) { | |
415 | return ret; | |
416 | } | |
417 | ||
418 | ret = vhost_user_blk_connect(dev, errp); | |
419 | if (ret < 0) { | |
420 | qemu_chr_fe_disconnect(&s->chardev); | |
421 | return ret; | |
422 | } | |
423 | assert(s->connected); | |
424 | ||
425 | ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg, | |
8edd5673 | 426 | s->parent_obj.config_len, errp); |
415fc294 KW |
427 | if (ret < 0) { |
428 | qemu_chr_fe_disconnect(&s->chardev); | |
429 | vhost_dev_cleanup(&s->dev); | |
430 | return ret; | |
431 | } | |
432 | ||
433 | return 0; | |
434 | } | |
435 | ||
00343e4b CL |
436 | static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) |
437 | { | |
a527e312 | 438 | ERRP_GUARD(); |
00343e4b CL |
439 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
440 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
8edd5673 | 441 | size_t config_size; |
a527e312 | 442 | int retries; |
00343e4b CL |
443 | int i, ret; |
444 | ||
445 | if (!s->chardev.chr) { | |
5b9243d2 | 446 | error_setg(errp, "chardev is mandatory"); |
00343e4b CL |
447 | return; |
448 | } | |
449 | ||
a4eef071 SH |
450 | if (s->num_queues == VHOST_USER_BLK_AUTO_NUM_QUEUES) { |
451 | s->num_queues = 1; | |
452 | } | |
00343e4b | 453 | if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) { |
5b9243d2 | 454 | error_setg(errp, "invalid number of IO queues"); |
00343e4b CL |
455 | return; |
456 | } | |
457 | ||
458 | if (!s->queue_size) { | |
5b9243d2 | 459 | error_setg(errp, "queue size must be non-zero"); |
00343e4b CL |
460 | return; |
461 | } | |
68bf7336 | 462 | if (s->queue_size > VIRTQUEUE_MAX_SIZE) { |
5b9243d2 | 463 | error_setg(errp, "queue size must not exceed %d", |
68bf7336 KW |
464 | VIRTQUEUE_MAX_SIZE); |
465 | return; | |
466 | } | |
00343e4b | 467 | |
0b99f224 | 468 | if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) { |
4d0cf552 TB |
469 | return; |
470 | } | |
471 | ||
8edd5673 DT |
472 | config_size = virtio_get_config_size(&virtio_blk_cfg_size_params, |
473 | vdev->host_features); | |
474 | virtio_init(vdev, VIRTIO_ID_BLOCK, config_size); | |
00343e4b | 475 | |
38e245a4 | 476 | s->virtqs = g_new(VirtQueue *, s->num_queues); |
00343e4b | 477 | for (i = 0; i < s->num_queues; i++) { |
38e245a4 PN |
478 | s->virtqs[i] = virtio_add_queue(vdev, s->queue_size, |
479 | vhost_user_blk_handle_output); | |
00343e4b CL |
480 | } |
481 | ||
a1fe0b8f | 482 | s->inflight = g_new0(struct vhost_inflight, 1); |
38e245a4 | 483 | s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues); |
00343e4b | 484 | |
a527e312 KW |
485 | retries = REALIZE_CONNECTION_RETRIES; |
486 | assert(!*errp); | |
487 | do { | |
488 | if (*errp) { | |
489 | error_prepend(errp, "Reconnecting after error: "); | |
490 | error_report_err(*errp); | |
491 | *errp = NULL; | |
492 | } | |
493 | ret = vhost_user_blk_realize_connect(s, errp); | |
b7107e75 | 494 | } while (ret < 0 && retries--); |
a527e312 | 495 | |
00343e4b | 496 | if (ret < 0) { |
415fc294 | 497 | goto virtio_err; |
00343e4b CL |
498 | } |
499 | ||
dabefdd6 | 500 | /* we're fully initialized, now we can operate, so add the handler */ |
0c99d722 | 501 | qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, |
dabefdd6 | 502 | vhost_user_blk_event, NULL, (void *)dev, |
0c99d722 | 503 | NULL, true); |
00343e4b CL |
504 | return; |
505 | ||
00343e4b | 506 | virtio_err: |
38e245a4 | 507 | g_free(s->vhost_vqs); |
0ac2e635 | 508 | s->vhost_vqs = NULL; |
a1fe0b8f | 509 | g_free(s->inflight); |
0ac2e635 | 510 | s->inflight = NULL; |
13e54681 | 511 | for (i = 0; i < s->num_queues; i++) { |
38e245a4 | 512 | virtio_delete_queue(s->virtqs[i]); |
13e54681 | 513 | } |
38e245a4 | 514 | g_free(s->virtqs); |
00343e4b | 515 | virtio_cleanup(vdev); |
0b99f224 | 516 | vhost_user_cleanup(&s->vhost_user); |
00343e4b CL |
517 | } |
518 | ||
b69c3c21 | 519 | static void vhost_user_blk_device_unrealize(DeviceState *dev) |
00343e4b CL |
520 | { |
521 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); | |
522 | VHostUserBlk *s = VHOST_USER_BLK(dev); | |
13e54681 | 523 | int i; |
00343e4b | 524 | |
96cb5498 | 525 | virtio_set_status(vdev, 0); |
77542d43 XY |
526 | qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL, |
527 | NULL, NULL, NULL, false); | |
00343e4b | 528 | vhost_dev_cleanup(&s->dev); |
a1fe0b8f | 529 | vhost_dev_free_inflight(s->inflight); |
38e245a4 | 530 | g_free(s->vhost_vqs); |
0ac2e635 | 531 | s->vhost_vqs = NULL; |
a1fe0b8f | 532 | g_free(s->inflight); |
0ac2e635 | 533 | s->inflight = NULL; |
13e54681 PN |
534 | |
535 | for (i = 0; i < s->num_queues; i++) { | |
38e245a4 | 536 | virtio_delete_queue(s->virtqs[i]); |
13e54681 | 537 | } |
38e245a4 | 538 | g_free(s->virtqs); |
00343e4b | 539 | virtio_cleanup(vdev); |
0b99f224 | 540 | vhost_user_cleanup(&s->vhost_user); |
00343e4b CL |
541 | } |
542 | ||
543 | static void vhost_user_blk_instance_init(Object *obj) | |
544 | { | |
545 | VHostUserBlk *s = VHOST_USER_BLK(obj); | |
546 | ||
547 | device_add_bootindex_property(obj, &s->bootindex, "bootindex", | |
40c2281c | 548 | "/disk@0,0", DEVICE(obj)); |
00343e4b CL |
549 | } |
550 | ||
c255488d JP |
551 | static struct vhost_dev *vhost_user_blk_get_vhost(VirtIODevice *vdev) |
552 | { | |
553 | VHostUserBlk *s = VHOST_USER_BLK(vdev); | |
554 | return &s->dev; | |
555 | } | |
556 | ||
00343e4b CL |
557 | static const VMStateDescription vmstate_vhost_user_blk = { |
558 | .name = "vhost-user-blk", | |
559 | .minimum_version_id = 1, | |
560 | .version_id = 1, | |
561 | .fields = (VMStateField[]) { | |
562 | VMSTATE_VIRTIO_DEVICE, | |
563 | VMSTATE_END_OF_LIST() | |
564 | }, | |
565 | }; | |
566 | ||
567 | static Property vhost_user_blk_properties[] = { | |
568 | DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev), | |
a4eef071 SH |
569 | DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues, |
570 | VHOST_USER_BLK_AUTO_NUM_QUEUES), | |
00343e4b | 571 | DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128), |
f1c25f29 DT |
572 | DEFINE_PROP_BIT64("config-wce", VHostUserBlk, parent_obj.host_features, |
573 | VIRTIO_BLK_F_CONFIG_WCE, true), | |
246db16d DT |
574 | DEFINE_PROP_BIT64("discard", VHostUserBlk, parent_obj.host_features, |
575 | VIRTIO_BLK_F_DISCARD, true), | |
576 | DEFINE_PROP_BIT64("write-zeroes", VHostUserBlk, parent_obj.host_features, | |
577 | VIRTIO_BLK_F_WRITE_ZEROES, true), | |
00343e4b CL |
578 | DEFINE_PROP_END_OF_LIST(), |
579 | }; | |
580 | ||
581 | static void vhost_user_blk_class_init(ObjectClass *klass, void *data) | |
582 | { | |
583 | DeviceClass *dc = DEVICE_CLASS(klass); | |
584 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); | |
585 | ||
4f67d30b | 586 | device_class_set_props(dc, vhost_user_blk_properties); |
00343e4b CL |
587 | dc->vmsd = &vmstate_vhost_user_blk; |
588 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); | |
589 | vdc->realize = vhost_user_blk_device_realize; | |
590 | vdc->unrealize = vhost_user_blk_device_unrealize; | |
591 | vdc->get_config = vhost_user_blk_update_config; | |
592 | vdc->set_config = vhost_user_blk_set_config; | |
593 | vdc->get_features = vhost_user_blk_get_features; | |
594 | vdc->set_status = vhost_user_blk_set_status; | |
a1fe0b8f | 595 | vdc->reset = vhost_user_blk_reset; |
c255488d | 596 | vdc->get_vhost = vhost_user_blk_get_vhost; |
00343e4b CL |
597 | } |
598 | ||
599 | static const TypeInfo vhost_user_blk_info = { | |
600 | .name = TYPE_VHOST_USER_BLK, | |
601 | .parent = TYPE_VIRTIO_DEVICE, | |
602 | .instance_size = sizeof(VHostUserBlk), | |
603 | .instance_init = vhost_user_blk_instance_init, | |
604 | .class_init = vhost_user_blk_class_init, | |
605 | }; | |
606 | ||
607 | static void virtio_register_types(void) | |
608 | { | |
609 | type_register_static(&vhost_user_blk_info); | |
610 | } | |
611 | ||
612 | type_init(virtio_register_types) |