Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 1 | /* |
| 2 | * vhost-user-blk host device |
| 3 | * |
| 4 | * Copyright(C) 2017 Intel Corporation. |
| 5 | * |
| 6 | * Authors: |
| 7 | * Changpeng Liu <changpeng.liu@intel.com> |
| 8 | * |
| 9 | * Largely based on the "vhost-user-scsi.c" and "vhost-scsi.c" implemented by: |
| 10 | * Felipe Franciosi <felipe@nutanix.com> |
| 11 | * Stefan Hajnoczi <stefanha@linux.vnet.ibm.com> |
| 12 | * Nicholas Bellinger <nab@risingtidesystems.com> |
| 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" |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 22 | #include "qemu/cutils.h" |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 23 | #include "hw/qdev-core.h" |
Markus Armbruster | a27bd6c | 2019-08-12 07:23:51 +0200 | [diff] [blame] | 24 | #include "hw/qdev-properties.h" |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 25 | #include "hw/virtio/vhost.h" |
| 26 | #include "hw/virtio/vhost-user-blk.h" |
| 27 | #include "hw/virtio/virtio.h" |
| 28 | #include "hw/virtio/virtio-bus.h" |
| 29 | #include "hw/virtio/virtio-access.h" |
Markus Armbruster | 2f780b6 | 2019-08-12 07:23:58 +0200 | [diff] [blame] | 30 | #include "sysemu/sysemu.h" |
Markus Armbruster | 54d3123 | 2019-08-12 07:23:59 +0200 | [diff] [blame] | 31 | #include "sysemu/runstate.h" |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 32 | |
| 33 | static const int user_feature_bits[] = { |
| 34 | VIRTIO_BLK_F_SIZE_MAX, |
| 35 | VIRTIO_BLK_F_SEG_MAX, |
| 36 | VIRTIO_BLK_F_GEOMETRY, |
| 37 | VIRTIO_BLK_F_BLK_SIZE, |
| 38 | VIRTIO_BLK_F_TOPOLOGY, |
| 39 | VIRTIO_BLK_F_MQ, |
| 40 | VIRTIO_BLK_F_RO, |
| 41 | VIRTIO_BLK_F_FLUSH, |
| 42 | VIRTIO_BLK_F_CONFIG_WCE, |
Changpeng Liu | caa1ee4 | 2019-01-16 13:19:30 +0800 | [diff] [blame] | 43 | VIRTIO_BLK_F_DISCARD, |
| 44 | VIRTIO_BLK_F_WRITE_ZEROES, |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 45 | VIRTIO_F_VERSION_1, |
| 46 | VIRTIO_RING_F_INDIRECT_DESC, |
| 47 | VIRTIO_RING_F_EVENT_IDX, |
| 48 | VIRTIO_F_NOTIFY_ON_EMPTY, |
| 49 | VHOST_INVALID_FEATURE_BIT |
| 50 | }; |
| 51 | |
| 52 | static void vhost_user_blk_update_config(VirtIODevice *vdev, uint8_t *config) |
| 53 | { |
| 54 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 55 | |
| 56 | memcpy(config, &s->blkcfg, sizeof(struct virtio_blk_config)); |
| 57 | } |
| 58 | |
| 59 | static void vhost_user_blk_set_config(VirtIODevice *vdev, const uint8_t *config) |
| 60 | { |
| 61 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 62 | struct virtio_blk_config *blkcfg = (struct virtio_blk_config *)config; |
| 63 | int ret; |
| 64 | |
| 65 | if (blkcfg->wce == s->blkcfg.wce) { |
| 66 | return; |
| 67 | } |
| 68 | |
| 69 | ret = vhost_dev_set_config(&s->dev, &blkcfg->wce, |
| 70 | offsetof(struct virtio_blk_config, wce), |
| 71 | sizeof(blkcfg->wce), |
| 72 | VHOST_SET_CONFIG_TYPE_MASTER); |
| 73 | if (ret) { |
| 74 | error_report("set device config space failed"); |
| 75 | return; |
| 76 | } |
| 77 | |
| 78 | s->blkcfg.wce = blkcfg->wce; |
| 79 | } |
| 80 | |
| 81 | static int vhost_user_blk_handle_config_change(struct vhost_dev *dev) |
| 82 | { |
| 83 | int ret; |
| 84 | struct virtio_blk_config blkcfg; |
| 85 | VHostUserBlk *s = VHOST_USER_BLK(dev->vdev); |
| 86 | |
| 87 | ret = vhost_dev_get_config(dev, (uint8_t *)&blkcfg, |
| 88 | sizeof(struct virtio_blk_config)); |
| 89 | if (ret < 0) { |
| 90 | error_report("get config space failed"); |
| 91 | return -1; |
| 92 | } |
| 93 | |
| 94 | /* valid for resize only */ |
| 95 | if (blkcfg.capacity != s->blkcfg.capacity) { |
| 96 | s->blkcfg.capacity = blkcfg.capacity; |
| 97 | memcpy(dev->vdev->config, &s->blkcfg, sizeof(struct virtio_blk_config)); |
| 98 | virtio_notify_config(dev->vdev); |
| 99 | } |
| 100 | |
| 101 | return 0; |
| 102 | } |
| 103 | |
| 104 | const VhostDevConfigOps blk_ops = { |
| 105 | .vhost_dev_config_notifier = vhost_user_blk_handle_config_change, |
| 106 | }; |
| 107 | |
Xie Yongji | a57f009 | 2019-03-20 19:26:44 +0800 | [diff] [blame] | 108 | static int vhost_user_blk_start(VirtIODevice *vdev) |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 109 | { |
| 110 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 111 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
| 112 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
| 113 | int i, ret; |
| 114 | |
| 115 | if (!k->set_guest_notifiers) { |
| 116 | error_report("binding does not support guest notifiers"); |
Xie Yongji | a57f009 | 2019-03-20 19:26:44 +0800 | [diff] [blame] | 117 | return -ENOSYS; |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | ret = vhost_dev_enable_notifiers(&s->dev, vdev); |
| 121 | if (ret < 0) { |
| 122 | error_report("Error enabling host notifiers: %d", -ret); |
Xie Yongji | a57f009 | 2019-03-20 19:26:44 +0800 | [diff] [blame] | 123 | return ret; |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, true); |
| 127 | if (ret < 0) { |
| 128 | error_report("Error binding guest notifier: %d", -ret); |
| 129 | goto err_host_notifiers; |
| 130 | } |
| 131 | |
| 132 | s->dev.acked_features = vdev->guest_features; |
Xie Yongji | a1fe0b8 | 2019-02-28 16:53:53 +0800 | [diff] [blame] | 133 | |
| 134 | if (!s->inflight->addr) { |
| 135 | ret = vhost_dev_get_inflight(&s->dev, s->queue_size, s->inflight); |
| 136 | if (ret < 0) { |
| 137 | error_report("Error get inflight: %d", -ret); |
| 138 | goto err_guest_notifiers; |
| 139 | } |
| 140 | } |
| 141 | |
| 142 | ret = vhost_dev_set_inflight(&s->dev, s->inflight); |
| 143 | if (ret < 0) { |
| 144 | error_report("Error set inflight: %d", -ret); |
| 145 | goto err_guest_notifiers; |
| 146 | } |
| 147 | |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 148 | ret = vhost_dev_start(&s->dev, vdev); |
| 149 | if (ret < 0) { |
| 150 | error_report("Error starting vhost: %d", -ret); |
| 151 | goto err_guest_notifiers; |
| 152 | } |
| 153 | |
| 154 | /* guest_notifier_mask/pending not used yet, so just unmask |
| 155 | * everything here. virtio-pci will do the right thing by |
| 156 | * enabling/disabling irqfd. |
| 157 | */ |
| 158 | for (i = 0; i < s->dev.nvqs; i++) { |
| 159 | vhost_virtqueue_mask(&s->dev, vdev, i, false); |
| 160 | } |
| 161 | |
Xie Yongji | a57f009 | 2019-03-20 19:26:44 +0800 | [diff] [blame] | 162 | return ret; |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 163 | |
| 164 | err_guest_notifiers: |
| 165 | k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); |
| 166 | err_host_notifiers: |
| 167 | vhost_dev_disable_notifiers(&s->dev, vdev); |
Xie Yongji | a57f009 | 2019-03-20 19:26:44 +0800 | [diff] [blame] | 168 | return ret; |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | static void vhost_user_blk_stop(VirtIODevice *vdev) |
| 172 | { |
| 173 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 174 | BusState *qbus = BUS(qdev_get_parent_bus(DEVICE(vdev))); |
| 175 | VirtioBusClass *k = VIRTIO_BUS_GET_CLASS(qbus); |
| 176 | int ret; |
| 177 | |
| 178 | if (!k->set_guest_notifiers) { |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | vhost_dev_stop(&s->dev, vdev); |
| 183 | |
| 184 | ret = k->set_guest_notifiers(qbus->parent, s->dev.nvqs, false); |
| 185 | if (ret < 0) { |
| 186 | error_report("vhost guest notifier cleanup failed: %d", ret); |
| 187 | return; |
| 188 | } |
| 189 | |
| 190 | vhost_dev_disable_notifiers(&s->dev, vdev); |
| 191 | } |
| 192 | |
| 193 | static void vhost_user_blk_set_status(VirtIODevice *vdev, uint8_t status) |
| 194 | { |
| 195 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
Xie Yongji | e57f2c3 | 2019-06-26 10:31:26 +0800 | [diff] [blame] | 196 | bool should_start = virtio_device_started(vdev, status); |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 197 | int ret; |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 198 | |
| 199 | if (!vdev->vm_running) { |
| 200 | should_start = false; |
| 201 | } |
| 202 | |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 203 | if (!s->connected) { |
| 204 | return; |
| 205 | } |
| 206 | |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 207 | if (s->dev.started == should_start) { |
| 208 | return; |
| 209 | } |
| 210 | |
| 211 | if (should_start) { |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 212 | ret = vhost_user_blk_start(vdev); |
| 213 | if (ret < 0) { |
| 214 | error_report("vhost-user-blk: vhost start failed: %s", |
| 215 | strerror(-ret)); |
| 216 | qemu_chr_fe_disconnect(&s->chardev); |
| 217 | } |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 218 | } else { |
| 219 | vhost_user_blk_stop(vdev); |
| 220 | } |
| 221 | |
| 222 | } |
| 223 | |
| 224 | static uint64_t vhost_user_blk_get_features(VirtIODevice *vdev, |
| 225 | uint64_t features, |
| 226 | Error **errp) |
| 227 | { |
| 228 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 229 | |
| 230 | /* Turn on pre-defined features */ |
| 231 | virtio_add_feature(&features, VIRTIO_BLK_F_SEG_MAX); |
| 232 | virtio_add_feature(&features, VIRTIO_BLK_F_GEOMETRY); |
| 233 | virtio_add_feature(&features, VIRTIO_BLK_F_TOPOLOGY); |
| 234 | virtio_add_feature(&features, VIRTIO_BLK_F_BLK_SIZE); |
| 235 | virtio_add_feature(&features, VIRTIO_BLK_F_FLUSH); |
Changpeng Liu | 25b1d45 | 2018-05-29 09:24:35 +0800 | [diff] [blame] | 236 | virtio_add_feature(&features, VIRTIO_BLK_F_RO); |
Changpeng Liu | caa1ee4 | 2019-01-16 13:19:30 +0800 | [diff] [blame] | 237 | virtio_add_feature(&features, VIRTIO_BLK_F_DISCARD); |
| 238 | virtio_add_feature(&features, VIRTIO_BLK_F_WRITE_ZEROES); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 239 | |
| 240 | if (s->config_wce) { |
| 241 | virtio_add_feature(&features, VIRTIO_BLK_F_CONFIG_WCE); |
| 242 | } |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 243 | if (s->num_queues > 1) { |
| 244 | virtio_add_feature(&features, VIRTIO_BLK_F_MQ); |
| 245 | } |
| 246 | |
Laurent Vivier | 4a4ff4c | 2018-03-23 15:32:02 +0100 | [diff] [blame] | 247 | return vhost_get_features(&s->dev, user_feature_bits, features); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 248 | } |
| 249 | |
| 250 | static void vhost_user_blk_handle_output(VirtIODevice *vdev, VirtQueue *vq) |
| 251 | { |
Yongji Xie | 110b946 | 2018-06-06 21:24:48 +0800 | [diff] [blame] | 252 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 253 | int i, ret; |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 254 | |
Xie Yongji | f3facbe | 2019-03-20 19:26:43 +0800 | [diff] [blame] | 255 | if (!vdev->start_on_kick) { |
Yongji Xie | 110b946 | 2018-06-06 21:24:48 +0800 | [diff] [blame] | 256 | return; |
| 257 | } |
| 258 | |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 259 | if (!s->connected) { |
| 260 | return; |
| 261 | } |
| 262 | |
Yongji Xie | 110b946 | 2018-06-06 21:24:48 +0800 | [diff] [blame] | 263 | if (s->dev.started) { |
| 264 | return; |
| 265 | } |
| 266 | |
| 267 | /* Some guests kick before setting VIRTIO_CONFIG_S_DRIVER_OK so start |
| 268 | * vhost here instead of waiting for .set_status(). |
| 269 | */ |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 270 | ret = vhost_user_blk_start(vdev); |
| 271 | if (ret < 0) { |
| 272 | error_report("vhost-user-blk: vhost start failed: %s", |
| 273 | strerror(-ret)); |
| 274 | qemu_chr_fe_disconnect(&s->chardev); |
| 275 | return; |
| 276 | } |
Yongji Xie | 110b946 | 2018-06-06 21:24:48 +0800 | [diff] [blame] | 277 | |
| 278 | /* Kick right away to begin processing requests already in vring */ |
| 279 | for (i = 0; i < s->dev.nvqs; i++) { |
| 280 | VirtQueue *kick_vq = virtio_get_queue(vdev, i); |
| 281 | |
| 282 | if (!virtio_queue_get_desc_addr(vdev, i)) { |
| 283 | continue; |
| 284 | } |
| 285 | event_notifier_set(virtio_queue_get_host_notifier(kick_vq)); |
| 286 | } |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 287 | } |
| 288 | |
Xie Yongji | a1fe0b8 | 2019-02-28 16:53:53 +0800 | [diff] [blame] | 289 | static void vhost_user_blk_reset(VirtIODevice *vdev) |
| 290 | { |
| 291 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 292 | |
| 293 | vhost_dev_free_inflight(s->inflight); |
| 294 | } |
| 295 | |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 296 | static int vhost_user_blk_connect(DeviceState *dev) |
| 297 | { |
| 298 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 299 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 300 | int ret = 0; |
| 301 | |
| 302 | if (s->connected) { |
| 303 | return 0; |
| 304 | } |
| 305 | s->connected = true; |
| 306 | |
| 307 | s->dev.nvqs = s->num_queues; |
Pan Nengyuan | 38e245a | 2020-02-24 12:13:36 +0800 | [diff] [blame] | 308 | s->dev.vqs = s->vhost_vqs; |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 309 | s->dev.vq_index = 0; |
| 310 | s->dev.backend_features = 0; |
| 311 | |
| 312 | vhost_dev_set_config_notifier(&s->dev, &blk_ops); |
| 313 | |
| 314 | ret = vhost_dev_init(&s->dev, &s->vhost_user, VHOST_BACKEND_TYPE_USER, 0); |
| 315 | if (ret < 0) { |
| 316 | error_report("vhost-user-blk: vhost initialization failed: %s", |
| 317 | strerror(-ret)); |
| 318 | return ret; |
| 319 | } |
| 320 | |
| 321 | /* restore vhost state */ |
Xie Yongji | e57f2c3 | 2019-06-26 10:31:26 +0800 | [diff] [blame] | 322 | if (virtio_device_started(vdev, vdev->status)) { |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 323 | ret = vhost_user_blk_start(vdev); |
| 324 | if (ret < 0) { |
| 325 | error_report("vhost-user-blk: vhost start failed: %s", |
| 326 | strerror(-ret)); |
| 327 | return ret; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | return 0; |
| 332 | } |
| 333 | |
| 334 | static void vhost_user_blk_disconnect(DeviceState *dev) |
| 335 | { |
| 336 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 337 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 338 | |
| 339 | if (!s->connected) { |
| 340 | return; |
| 341 | } |
| 342 | s->connected = false; |
| 343 | |
| 344 | if (s->dev.started) { |
| 345 | vhost_user_blk_stop(vdev); |
| 346 | } |
| 347 | |
| 348 | vhost_dev_cleanup(&s->dev); |
| 349 | } |
| 350 | |
Dima Stepanov | 4bcad76 | 2020-05-28 12:11:19 +0300 | [diff] [blame] | 351 | static void vhost_user_blk_event(void *opaque, QEMUChrEvent event); |
| 352 | |
| 353 | static void vhost_user_blk_chr_closed_bh(void *opaque) |
| 354 | { |
| 355 | DeviceState *dev = opaque; |
| 356 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 357 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 358 | |
| 359 | vhost_user_blk_disconnect(dev); |
| 360 | qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event, |
| 361 | NULL, opaque, NULL, true); |
| 362 | } |
| 363 | |
Philippe Mathieu-Daudé | 083b266 | 2019-12-18 18:20:09 +0100 | [diff] [blame] | 364 | static void vhost_user_blk_event(void *opaque, QEMUChrEvent event) |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 365 | { |
| 366 | DeviceState *dev = opaque; |
| 367 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 368 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
| 369 | |
| 370 | switch (event) { |
| 371 | case CHR_EVENT_OPENED: |
| 372 | if (vhost_user_blk_connect(dev) < 0) { |
| 373 | qemu_chr_fe_disconnect(&s->chardev); |
| 374 | return; |
| 375 | } |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 376 | break; |
| 377 | case CHR_EVENT_CLOSED: |
Dima Stepanov | 4bcad76 | 2020-05-28 12:11:19 +0300 | [diff] [blame] | 378 | /* |
| 379 | * A close event may happen during a read/write, but vhost |
| 380 | * code assumes the vhost_dev remains setup, so delay the |
| 381 | * stop & clear. There are two possible paths to hit this |
| 382 | * disconnect event: |
| 383 | * 1. When VM is in the RUN_STATE_PRELAUNCH state. The |
| 384 | * vhost_user_blk_device_realize() is a caller. |
| 385 | * 2. In tha main loop phase after VM start. |
| 386 | * |
| 387 | * For p2 the disconnect event will be delayed. We can't |
| 388 | * do the same for p1, because we are not running the loop |
| 389 | * at this moment. So just skip this step and perform |
| 390 | * disconnect in the caller function. |
| 391 | * |
| 392 | * TODO: maybe it is a good idea to make the same fix |
| 393 | * for other vhost-user devices. |
| 394 | */ |
| 395 | if (runstate_is_running()) { |
| 396 | AioContext *ctx = qemu_get_current_aio_context(); |
| 397 | |
| 398 | qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL, NULL, |
| 399 | NULL, NULL, false); |
| 400 | aio_bh_schedule_oneshot(ctx, vhost_user_blk_chr_closed_bh, opaque); |
| 401 | } |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 402 | break; |
Philippe Mathieu-Daudé | 669457f | 2019-12-18 18:20:04 +0100 | [diff] [blame] | 403 | case CHR_EVENT_BREAK: |
| 404 | case CHR_EVENT_MUX_IN: |
| 405 | case CHR_EVENT_MUX_OUT: |
| 406 | /* Ignore */ |
| 407 | break; |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 408 | } |
| 409 | } |
| 410 | |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 411 | static void vhost_user_blk_device_realize(DeviceState *dev, Error **errp) |
| 412 | { |
| 413 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 414 | VHostUserBlk *s = VHOST_USER_BLK(vdev); |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 415 | Error *err = NULL; |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 416 | int i, ret; |
| 417 | |
| 418 | if (!s->chardev.chr) { |
| 419 | error_setg(errp, "vhost-user-blk: chardev is mandatory"); |
| 420 | return; |
| 421 | } |
| 422 | |
Stefan Hajnoczi | a4eef07 | 2020-08-18 15:33:48 +0100 | [diff] [blame] | 423 | if (s->num_queues == VHOST_USER_BLK_AUTO_NUM_QUEUES) { |
| 424 | s->num_queues = 1; |
| 425 | } |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 426 | if (!s->num_queues || s->num_queues > VIRTIO_QUEUE_MAX) { |
| 427 | error_setg(errp, "vhost-user-blk: invalid number of IO queues"); |
| 428 | return; |
| 429 | } |
| 430 | |
| 431 | if (!s->queue_size) { |
| 432 | error_setg(errp, "vhost-user-blk: queue size must be non-zero"); |
| 433 | return; |
| 434 | } |
| 435 | |
Marc-André Lureau | 0b99f22 | 2019-03-08 15:04:45 +0100 | [diff] [blame] | 436 | if (!vhost_user_init(&s->vhost_user, &s->chardev, errp)) { |
Tiwei Bie | 4d0cf55 | 2018-05-24 18:33:33 +0800 | [diff] [blame] | 437 | return; |
| 438 | } |
| 439 | |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 440 | virtio_init(vdev, "virtio-blk", VIRTIO_ID_BLOCK, |
| 441 | sizeof(struct virtio_blk_config)); |
| 442 | |
Pan Nengyuan | 38e245a | 2020-02-24 12:13:36 +0800 | [diff] [blame] | 443 | s->virtqs = g_new(VirtQueue *, s->num_queues); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 444 | for (i = 0; i < s->num_queues; i++) { |
Pan Nengyuan | 38e245a | 2020-02-24 12:13:36 +0800 | [diff] [blame] | 445 | s->virtqs[i] = virtio_add_queue(vdev, s->queue_size, |
| 446 | vhost_user_blk_handle_output); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 447 | } |
| 448 | |
Xie Yongji | a1fe0b8 | 2019-02-28 16:53:53 +0800 | [diff] [blame] | 449 | s->inflight = g_new0(struct vhost_inflight, 1); |
Pan Nengyuan | 38e245a | 2020-02-24 12:13:36 +0800 | [diff] [blame] | 450 | s->vhost_vqs = g_new0(struct vhost_virtqueue, s->num_queues); |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 451 | s->connected = false; |
Xie Yongji | a1fe0b8 | 2019-02-28 16:53:53 +0800 | [diff] [blame] | 452 | |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 453 | qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, vhost_user_blk_event, |
| 454 | NULL, (void *)dev, NULL, true); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 455 | |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 456 | reconnect: |
| 457 | if (qemu_chr_fe_wait_connected(&s->chardev, &err) < 0) { |
| 458 | error_report_err(err); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 459 | goto virtio_err; |
| 460 | } |
| 461 | |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 462 | /* check whether vhost_user_blk_connect() failed or not */ |
| 463 | if (!s->connected) { |
| 464 | goto reconnect; |
| 465 | } |
| 466 | |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 467 | ret = vhost_dev_get_config(&s->dev, (uint8_t *)&s->blkcfg, |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 468 | sizeof(struct virtio_blk_config)); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 469 | if (ret < 0) { |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 470 | error_report("vhost-user-blk: get block config failed"); |
| 471 | goto reconnect; |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 472 | } |
| 473 | |
| 474 | if (s->blkcfg.num_queues != s->num_queues) { |
| 475 | s->blkcfg.num_queues = s->num_queues; |
| 476 | } |
| 477 | |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 478 | return; |
| 479 | |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 480 | virtio_err: |
Pan Nengyuan | 38e245a | 2020-02-24 12:13:36 +0800 | [diff] [blame] | 481 | g_free(s->vhost_vqs); |
Li Feng | 0ac2e63 | 2020-04-17 18:17:07 +0800 | [diff] [blame] | 482 | s->vhost_vqs = NULL; |
Xie Yongji | a1fe0b8 | 2019-02-28 16:53:53 +0800 | [diff] [blame] | 483 | g_free(s->inflight); |
Li Feng | 0ac2e63 | 2020-04-17 18:17:07 +0800 | [diff] [blame] | 484 | s->inflight = NULL; |
Pan Nengyuan | 13e5468 | 2020-02-24 12:13:35 +0800 | [diff] [blame] | 485 | for (i = 0; i < s->num_queues; i++) { |
Pan Nengyuan | 38e245a | 2020-02-24 12:13:36 +0800 | [diff] [blame] | 486 | virtio_delete_queue(s->virtqs[i]); |
Pan Nengyuan | 13e5468 | 2020-02-24 12:13:35 +0800 | [diff] [blame] | 487 | } |
Pan Nengyuan | 38e245a | 2020-02-24 12:13:36 +0800 | [diff] [blame] | 488 | g_free(s->virtqs); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 489 | virtio_cleanup(vdev); |
Marc-André Lureau | 0b99f22 | 2019-03-08 15:04:45 +0100 | [diff] [blame] | 490 | vhost_user_cleanup(&s->vhost_user); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 491 | } |
| 492 | |
Markus Armbruster | b69c3c2 | 2020-05-05 17:29:24 +0200 | [diff] [blame] | 493 | static void vhost_user_blk_device_unrealize(DeviceState *dev) |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 494 | { |
| 495 | VirtIODevice *vdev = VIRTIO_DEVICE(dev); |
| 496 | VHostUserBlk *s = VHOST_USER_BLK(dev); |
Pan Nengyuan | 13e5468 | 2020-02-24 12:13:35 +0800 | [diff] [blame] | 497 | int i; |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 498 | |
Xie Yongji | 96cb549 | 2019-03-20 19:26:42 +0800 | [diff] [blame] | 499 | virtio_set_status(vdev, 0); |
Xie Yongji | 77542d4 | 2019-03-20 19:26:45 +0800 | [diff] [blame] | 500 | qemu_chr_fe_set_handlers(&s->chardev, NULL, NULL, NULL, |
| 501 | NULL, NULL, NULL, false); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 502 | vhost_dev_cleanup(&s->dev); |
Xie Yongji | a1fe0b8 | 2019-02-28 16:53:53 +0800 | [diff] [blame] | 503 | vhost_dev_free_inflight(s->inflight); |
Pan Nengyuan | 38e245a | 2020-02-24 12:13:36 +0800 | [diff] [blame] | 504 | g_free(s->vhost_vqs); |
Li Feng | 0ac2e63 | 2020-04-17 18:17:07 +0800 | [diff] [blame] | 505 | s->vhost_vqs = NULL; |
Xie Yongji | a1fe0b8 | 2019-02-28 16:53:53 +0800 | [diff] [blame] | 506 | g_free(s->inflight); |
Li Feng | 0ac2e63 | 2020-04-17 18:17:07 +0800 | [diff] [blame] | 507 | s->inflight = NULL; |
Pan Nengyuan | 13e5468 | 2020-02-24 12:13:35 +0800 | [diff] [blame] | 508 | |
| 509 | for (i = 0; i < s->num_queues; i++) { |
Pan Nengyuan | 38e245a | 2020-02-24 12:13:36 +0800 | [diff] [blame] | 510 | virtio_delete_queue(s->virtqs[i]); |
Pan Nengyuan | 13e5468 | 2020-02-24 12:13:35 +0800 | [diff] [blame] | 511 | } |
Pan Nengyuan | 38e245a | 2020-02-24 12:13:36 +0800 | [diff] [blame] | 512 | g_free(s->virtqs); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 513 | virtio_cleanup(vdev); |
Marc-André Lureau | 0b99f22 | 2019-03-08 15:04:45 +0100 | [diff] [blame] | 514 | vhost_user_cleanup(&s->vhost_user); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | static void vhost_user_blk_instance_init(Object *obj) |
| 518 | { |
| 519 | VHostUserBlk *s = VHOST_USER_BLK(obj); |
| 520 | |
| 521 | device_add_bootindex_property(obj, &s->bootindex, "bootindex", |
Markus Armbruster | 40c2281 | 2020-05-05 17:29:23 +0200 | [diff] [blame] | 522 | "/disk@0,0", DEVICE(obj)); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 523 | } |
| 524 | |
| 525 | static const VMStateDescription vmstate_vhost_user_blk = { |
| 526 | .name = "vhost-user-blk", |
| 527 | .minimum_version_id = 1, |
| 528 | .version_id = 1, |
| 529 | .fields = (VMStateField[]) { |
| 530 | VMSTATE_VIRTIO_DEVICE, |
| 531 | VMSTATE_END_OF_LIST() |
| 532 | }, |
| 533 | }; |
| 534 | |
| 535 | static Property vhost_user_blk_properties[] = { |
| 536 | DEFINE_PROP_CHR("chardev", VHostUserBlk, chardev), |
Stefan Hajnoczi | a4eef07 | 2020-08-18 15:33:48 +0100 | [diff] [blame] | 537 | DEFINE_PROP_UINT16("num-queues", VHostUserBlk, num_queues, |
| 538 | VHOST_USER_BLK_AUTO_NUM_QUEUES), |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 539 | DEFINE_PROP_UINT32("queue-size", VHostUserBlk, queue_size, 128), |
| 540 | DEFINE_PROP_BIT("config-wce", VHostUserBlk, config_wce, 0, true), |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 541 | DEFINE_PROP_END_OF_LIST(), |
| 542 | }; |
| 543 | |
| 544 | static void vhost_user_blk_class_init(ObjectClass *klass, void *data) |
| 545 | { |
| 546 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 547 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_CLASS(klass); |
| 548 | |
Marc-André Lureau | 4f67d30 | 2020-01-10 19:30:32 +0400 | [diff] [blame] | 549 | device_class_set_props(dc, vhost_user_blk_properties); |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 550 | dc->vmsd = &vmstate_vhost_user_blk; |
| 551 | set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); |
| 552 | vdc->realize = vhost_user_blk_device_realize; |
| 553 | vdc->unrealize = vhost_user_blk_device_unrealize; |
| 554 | vdc->get_config = vhost_user_blk_update_config; |
| 555 | vdc->set_config = vhost_user_blk_set_config; |
| 556 | vdc->get_features = vhost_user_blk_get_features; |
| 557 | vdc->set_status = vhost_user_blk_set_status; |
Xie Yongji | a1fe0b8 | 2019-02-28 16:53:53 +0800 | [diff] [blame] | 558 | vdc->reset = vhost_user_blk_reset; |
Changpeng Liu | 00343e4 | 2018-01-04 09:53:32 +0800 | [diff] [blame] | 559 | } |
| 560 | |
| 561 | static const TypeInfo vhost_user_blk_info = { |
| 562 | .name = TYPE_VHOST_USER_BLK, |
| 563 | .parent = TYPE_VIRTIO_DEVICE, |
| 564 | .instance_size = sizeof(VHostUserBlk), |
| 565 | .instance_init = vhost_user_blk_instance_init, |
| 566 | .class_init = vhost_user_blk_class_init, |
| 567 | }; |
| 568 | |
| 569 | static void virtio_register_types(void) |
| 570 | { |
| 571 | type_register_static(&vhost_user_blk_info); |
| 572 | } |
| 573 | |
| 574 | type_init(virtio_register_types) |