| 1 | /* |
| 2 | * virtio ccw target implementation |
| 3 | * |
| 4 | * Copyright 2012,2015 IBM Corp. |
| 5 | * Author(s): Cornelia Huck <cornelia.huck@de.ibm.com> |
| 6 | * Pierre Morel <pmorel@linux.vnet.ibm.com> |
| 7 | * |
| 8 | * This work is licensed under the terms of the GNU GPL, version 2 or (at |
| 9 | * your option) any later version. See the COPYING file in the top-level |
| 10 | * directory. |
| 11 | */ |
| 12 | |
| 13 | #include "qemu/osdep.h" |
| 14 | #include "qapi/error.h" |
| 15 | #include "system/address-spaces.h" |
| 16 | #include "system/kvm.h" |
| 17 | #include "system/physmem.h" |
| 18 | #include "net/net.h" |
| 19 | #include "hw/virtio/virtio.h" |
| 20 | #include "migration/qemu-file-types.h" |
| 21 | #include "hw/virtio/virtio-net.h" |
| 22 | #include "qemu/bitops.h" |
| 23 | #include "qemu/error-report.h" |
| 24 | #include "qemu/log.h" |
| 25 | #include "qemu/module.h" |
| 26 | #include "hw/virtio/virtio-bus.h" |
| 27 | #include "hw/s390x/adapter.h" |
| 28 | #include "hw/s390x/s390_flic.h" |
| 29 | |
| 30 | #include "hw/s390x/ioinst.h" |
| 31 | #include "hw/s390x/css.h" |
| 32 | #include "virtio-ccw.h" |
| 33 | #include "trace.h" |
| 34 | #include "hw/s390x/css-bridge.h" |
| 35 | #include "hw/s390x/s390-virtio-ccw.h" |
| 36 | #include "exec/cpu-common.h" |
| 37 | #include "system/replay.h" |
| 38 | |
| 39 | #define NR_CLASSIC_INDICATOR_BITS 64 |
| 40 | |
| 41 | bool have_virtio_ccw = true; |
| 42 | |
| 43 | static int virtio_ccw_dev_post_load(void *opaque, int version_id) |
| 44 | { |
| 45 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(opaque); |
| 46 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
| 47 | CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev); |
| 48 | |
| 49 | ccw_dev->sch->driver_data = dev; |
| 50 | if (ccw_dev->sch->thinint_active) { |
| 51 | dev->routes.adapter.adapter_id = css_get_adapter_id( |
| 52 | CSS_IO_ADAPTER_VIRTIO, |
| 53 | dev->thinint_isc); |
| 54 | } |
| 55 | /* Re-fill subch_id after loading the subchannel states.*/ |
| 56 | if (ck->refill_ids) { |
| 57 | ck->refill_ids(ccw_dev); |
| 58 | } |
| 59 | return 0; |
| 60 | } |
| 61 | |
| 62 | typedef struct VirtioCcwDeviceTmp { |
| 63 | VirtioCcwDevice *parent; |
| 64 | uint16_t config_vector; |
| 65 | } VirtioCcwDeviceTmp; |
| 66 | |
| 67 | static int virtio_ccw_dev_tmp_pre_save(void *opaque) |
| 68 | { |
| 69 | VirtioCcwDeviceTmp *tmp = opaque; |
| 70 | VirtioCcwDevice *dev = tmp->parent; |
| 71 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 72 | |
| 73 | tmp->config_vector = vdev->config_vector; |
| 74 | |
| 75 | return 0; |
| 76 | } |
| 77 | |
| 78 | static int virtio_ccw_dev_tmp_post_load(void *opaque, int version_id) |
| 79 | { |
| 80 | VirtioCcwDeviceTmp *tmp = opaque; |
| 81 | VirtioCcwDevice *dev = tmp->parent; |
| 82 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 83 | |
| 84 | vdev->config_vector = tmp->config_vector; |
| 85 | return 0; |
| 86 | } |
| 87 | |
| 88 | const VMStateDescription vmstate_virtio_ccw_dev_tmp = { |
| 89 | .name = "s390_virtio_ccw_dev_tmp", |
| 90 | .pre_save = virtio_ccw_dev_tmp_pre_save, |
| 91 | .post_load = virtio_ccw_dev_tmp_post_load, |
| 92 | .fields = (const VMStateField[]) { |
| 93 | VMSTATE_UINT16(config_vector, VirtioCcwDeviceTmp), |
| 94 | VMSTATE_END_OF_LIST() |
| 95 | } |
| 96 | }; |
| 97 | |
| 98 | const VMStateDescription vmstate_virtio_ccw_dev = { |
| 99 | .name = "s390_virtio_ccw_dev", |
| 100 | .version_id = 1, |
| 101 | .minimum_version_id = 1, |
| 102 | .post_load = virtio_ccw_dev_post_load, |
| 103 | .fields = (const VMStateField[]) { |
| 104 | VMSTATE_CCW_DEVICE(parent_obj, VirtioCcwDevice), |
| 105 | VMSTATE_PTR_TO_IND_ADDR(indicators, VirtioCcwDevice), |
| 106 | VMSTATE_PTR_TO_IND_ADDR(indicators2, VirtioCcwDevice), |
| 107 | VMSTATE_PTR_TO_IND_ADDR(summary_indicator, VirtioCcwDevice), |
| 108 | /* |
| 109 | * Ugly hack because VirtIODevice does not migrate itself. |
| 110 | * This also makes legacy via vmstate_save_state possible. |
| 111 | */ |
| 112 | VMSTATE_WITH_TMP(VirtioCcwDevice, VirtioCcwDeviceTmp, |
| 113 | vmstate_virtio_ccw_dev_tmp), |
| 114 | VMSTATE_STRUCT(routes, VirtioCcwDevice, 1, vmstate_adapter_routes, |
| 115 | AdapterRoutes), |
| 116 | VMSTATE_UINT8(thinint_isc, VirtioCcwDevice), |
| 117 | VMSTATE_INT32(revision, VirtioCcwDevice), |
| 118 | VMSTATE_END_OF_LIST() |
| 119 | } |
| 120 | }; |
| 121 | |
| 122 | static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size, |
| 123 | VirtioCcwDevice *dev); |
| 124 | |
| 125 | VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch) |
| 126 | { |
| 127 | VirtIODevice *vdev = NULL; |
| 128 | VirtioCcwDevice *dev = sch->driver_data; |
| 129 | |
| 130 | if (dev) { |
| 131 | vdev = virtio_bus_get_device(&dev->bus); |
| 132 | } |
| 133 | return vdev; |
| 134 | } |
| 135 | |
| 136 | static void virtio_ccw_start_ioeventfd(VirtioCcwDevice *dev) |
| 137 | { |
| 138 | virtio_bus_start_ioeventfd(&dev->bus); |
| 139 | } |
| 140 | |
| 141 | static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice *dev) |
| 142 | { |
| 143 | virtio_bus_stop_ioeventfd(&dev->bus); |
| 144 | } |
| 145 | |
| 146 | static bool virtio_ccw_ioeventfd_enabled(DeviceState *d) |
| 147 | { |
| 148 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 149 | |
| 150 | return (dev->flags & VIRTIO_CCW_FLAG_USE_IOEVENTFD) != 0; |
| 151 | } |
| 152 | |
| 153 | static int virtio_ccw_ioeventfd_assign(DeviceState *d, EventNotifier *notifier, |
| 154 | int n, bool assign) |
| 155 | { |
| 156 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 157 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
| 158 | SubchDev *sch = ccw_dev->sch; |
| 159 | uint32_t sch_id = (css_build_subchannel_id(sch) << 16) | sch->schid; |
| 160 | |
| 161 | return s390_assign_subch_ioeventfd(notifier, sch_id, n, assign); |
| 162 | } |
| 163 | |
| 164 | /* Communication blocks used by several channel commands. */ |
| 165 | typedef struct VqInfoBlockLegacy { |
| 166 | uint64_t queue; |
| 167 | uint32_t align; |
| 168 | uint16_t index; |
| 169 | uint16_t num; |
| 170 | } QEMU_PACKED VqInfoBlockLegacy; |
| 171 | |
| 172 | typedef struct VqInfoBlock { |
| 173 | uint64_t desc; |
| 174 | uint32_t res0; |
| 175 | uint16_t index; |
| 176 | uint16_t num; |
| 177 | uint64_t avail; |
| 178 | uint64_t used; |
| 179 | } QEMU_PACKED VqInfoBlock; |
| 180 | |
| 181 | typedef struct VqConfigBlock { |
| 182 | uint16_t index; |
| 183 | uint16_t num_max; |
| 184 | } QEMU_PACKED VqConfigBlock; |
| 185 | |
| 186 | typedef struct VirtioFeatDesc { |
| 187 | uint32_t features; |
| 188 | uint8_t index; |
| 189 | } QEMU_PACKED VirtioFeatDesc; |
| 190 | |
| 191 | typedef struct VirtioThinintInfo { |
| 192 | hwaddr summary_indicator; |
| 193 | hwaddr device_indicator; |
| 194 | uint64_t ind_bit; |
| 195 | uint8_t isc; |
| 196 | } QEMU_PACKED VirtioThinintInfo; |
| 197 | |
| 198 | typedef struct VirtioRevInfo { |
| 199 | uint16_t revision; |
| 200 | uint16_t length; |
| 201 | uint8_t data[]; |
| 202 | } QEMU_PACKED VirtioRevInfo; |
| 203 | |
| 204 | /* Specify where the virtqueues for the subchannel are in guest memory. */ |
| 205 | static int virtio_ccw_set_vqs(SubchDev *sch, VqInfoBlock *info, |
| 206 | VqInfoBlockLegacy *linfo) |
| 207 | { |
| 208 | VirtIODevice *vdev = virtio_ccw_get_vdev(sch); |
| 209 | uint16_t index = info ? info->index : linfo->index; |
| 210 | uint16_t num = info ? info->num : linfo->num; |
| 211 | uint64_t desc = info ? info->desc : linfo->queue; |
| 212 | |
| 213 | if (index >= VIRTIO_QUEUE_MAX) { |
| 214 | return -EINVAL; |
| 215 | } |
| 216 | |
| 217 | /* Current code in virtio.c relies on 4K alignment. */ |
| 218 | if (linfo && desc && (linfo->align != 4096)) { |
| 219 | return -EINVAL; |
| 220 | } |
| 221 | |
| 222 | if (!vdev) { |
| 223 | return -EINVAL; |
| 224 | } |
| 225 | |
| 226 | if (info) { |
| 227 | virtio_queue_set_rings(vdev, index, desc, info->avail, info->used); |
| 228 | } else { |
| 229 | virtio_queue_set_addr(vdev, index, desc); |
| 230 | } |
| 231 | if (!desc) { |
| 232 | virtio_queue_set_vector(vdev, index, VIRTIO_NO_VECTOR); |
| 233 | } else { |
| 234 | if (info) { |
| 235 | /* virtio-1 allows changing the ring size. */ |
| 236 | if (virtio_queue_get_max_num(vdev, index) < num) { |
| 237 | /* Fail if we exceed the maximum number. */ |
| 238 | return -EINVAL; |
| 239 | } |
| 240 | virtio_queue_set_num(vdev, index, num); |
| 241 | virtio_init_region_cache(vdev, index); |
| 242 | } else if (virtio_queue_get_num(vdev, index) > num) { |
| 243 | /* Fail if we don't have a big enough queue. */ |
| 244 | return -EINVAL; |
| 245 | } |
| 246 | /* We ignore possible increased num for legacy for compatibility. */ |
| 247 | virtio_queue_set_vector(vdev, index, index); |
| 248 | } |
| 249 | /* tell notify handler in case of config change */ |
| 250 | vdev->config_vector = VIRTIO_QUEUE_MAX; |
| 251 | return 0; |
| 252 | } |
| 253 | |
| 254 | static void virtio_ccw_reset_virtio(VirtioCcwDevice *dev) |
| 255 | { |
| 256 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
| 257 | |
| 258 | virtio_bus_reset(&dev->bus); |
| 259 | if (dev->indicators) { |
| 260 | release_indicator(&dev->routes.adapter, dev->indicators); |
| 261 | dev->indicators = NULL; |
| 262 | } |
| 263 | if (dev->indicators2) { |
| 264 | release_indicator(&dev->routes.adapter, dev->indicators2); |
| 265 | dev->indicators2 = NULL; |
| 266 | } |
| 267 | if (dev->summary_indicator) { |
| 268 | release_indicator(&dev->routes.adapter, dev->summary_indicator); |
| 269 | dev->summary_indicator = NULL; |
| 270 | } |
| 271 | ccw_dev->sch->thinint_active = false; |
| 272 | } |
| 273 | |
| 274 | static int virtio_ccw_handle_set_vq(SubchDev *sch, CCW1 ccw, bool check_len, |
| 275 | bool is_legacy) |
| 276 | { |
| 277 | int ret; |
| 278 | VqInfoBlock info; |
| 279 | VqInfoBlockLegacy linfo; |
| 280 | size_t info_len = is_legacy ? sizeof(linfo) : sizeof(info); |
| 281 | |
| 282 | if (check_len) { |
| 283 | if (ccw.count != info_len) { |
| 284 | return -EINVAL; |
| 285 | } |
| 286 | } else if (ccw.count < info_len) { |
| 287 | /* Can't execute command. */ |
| 288 | return -EINVAL; |
| 289 | } |
| 290 | if (!ccw.cda) { |
| 291 | return -EFAULT; |
| 292 | } |
| 293 | if (is_legacy) { |
| 294 | ret = ccw_dstream_read(&sch->cds, linfo); |
| 295 | if (ret) { |
| 296 | return ret; |
| 297 | } |
| 298 | linfo.queue = be64_to_cpu(linfo.queue); |
| 299 | linfo.align = be32_to_cpu(linfo.align); |
| 300 | linfo.index = be16_to_cpu(linfo.index); |
| 301 | linfo.num = be16_to_cpu(linfo.num); |
| 302 | ret = virtio_ccw_set_vqs(sch, NULL, &linfo); |
| 303 | } else { |
| 304 | ret = ccw_dstream_read(&sch->cds, info); |
| 305 | if (ret) { |
| 306 | return ret; |
| 307 | } |
| 308 | info.desc = be64_to_cpu(info.desc); |
| 309 | info.index = be16_to_cpu(info.index); |
| 310 | info.num = be16_to_cpu(info.num); |
| 311 | info.avail = be64_to_cpu(info.avail); |
| 312 | info.used = be64_to_cpu(info.used); |
| 313 | ret = virtio_ccw_set_vqs(sch, &info, NULL); |
| 314 | } |
| 315 | sch->curr_status.scsw.count = 0; |
| 316 | return ret; |
| 317 | } |
| 318 | |
| 319 | static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw) |
| 320 | { |
| 321 | int ret; |
| 322 | VirtioRevInfo revinfo; |
| 323 | uint8_t status; |
| 324 | VirtioFeatDesc features; |
| 325 | hwaddr indicators; |
| 326 | VqConfigBlock vq_config; |
| 327 | VirtioCcwDevice *dev = sch->driver_data; |
| 328 | VirtIODevice *vdev = virtio_ccw_get_vdev(sch); |
| 329 | bool check_len; |
| 330 | int len; |
| 331 | VirtioThinintInfo thinint; |
| 332 | |
| 333 | if (!dev) { |
| 334 | return -EINVAL; |
| 335 | } |
| 336 | |
| 337 | trace_virtio_ccw_interpret_ccw(sch->cssid, sch->ssid, sch->schid, |
| 338 | ccw.cmd_code); |
| 339 | check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC)); |
| 340 | |
| 341 | if (dev->revision < 0 && ccw.cmd_code != CCW_CMD_SET_VIRTIO_REV) { |
| 342 | if (dev->force_revision_1) { |
| 343 | /* |
| 344 | * virtio-1 drivers must start with negotiating to a revision >= 1, |
| 345 | * so post a command reject for all other commands |
| 346 | */ |
| 347 | return -ENOSYS; |
| 348 | } else { |
| 349 | /* |
| 350 | * If the driver issues any command that is not SET_VIRTIO_REV, |
| 351 | * we'll have to operate the device in legacy mode. |
| 352 | */ |
| 353 | dev->revision = 0; |
| 354 | } |
| 355 | } |
| 356 | |
| 357 | /* Look at the command. */ |
| 358 | switch (ccw.cmd_code) { |
| 359 | case CCW_CMD_SET_VQ: |
| 360 | ret = virtio_ccw_handle_set_vq(sch, ccw, check_len, dev->revision < 1); |
| 361 | break; |
| 362 | case CCW_CMD_VDEV_RESET: |
| 363 | virtio_ccw_reset_virtio(dev); |
| 364 | ret = 0; |
| 365 | break; |
| 366 | case CCW_CMD_READ_FEAT: |
| 367 | if (check_len) { |
| 368 | if (ccw.count != sizeof(features)) { |
| 369 | ret = -EINVAL; |
| 370 | break; |
| 371 | } |
| 372 | } else if (ccw.count < sizeof(features)) { |
| 373 | /* Can't execute command. */ |
| 374 | ret = -EINVAL; |
| 375 | break; |
| 376 | } |
| 377 | if (!ccw.cda) { |
| 378 | ret = -EFAULT; |
| 379 | } else { |
| 380 | VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); |
| 381 | |
| 382 | ccw_dstream_advance(&sch->cds, sizeof(features.features)); |
| 383 | ret = ccw_dstream_read(&sch->cds, features.index); |
| 384 | if (ret) { |
| 385 | break; |
| 386 | } |
| 387 | if (features.index == 0) { |
| 388 | if (dev->revision >= 1) { |
| 389 | /* Don't offer legacy features for modern devices. */ |
| 390 | features.features = (uint32_t) |
| 391 | (vdev->host_features & ~vdc->legacy_features); |
| 392 | } else { |
| 393 | features.features = (uint32_t)vdev->host_features; |
| 394 | } |
| 395 | } else if ((features.index == 1) && (dev->revision >= 1)) { |
| 396 | /* |
| 397 | * Only offer feature bits beyond 31 if the guest has |
| 398 | * negotiated at least revision 1. |
| 399 | */ |
| 400 | features.features = (uint32_t)(vdev->host_features >> 32); |
| 401 | } else { |
| 402 | /* Return zeroes if the guest supports more feature bits. */ |
| 403 | features.features = 0; |
| 404 | } |
| 405 | ccw_dstream_rewind(&sch->cds); |
| 406 | features.features = cpu_to_le32(features.features); |
| 407 | ret = ccw_dstream_write(&sch->cds, features.features); |
| 408 | if (!ret) { |
| 409 | sch->curr_status.scsw.count = ccw.count - sizeof(features); |
| 410 | } |
| 411 | } |
| 412 | break; |
| 413 | case CCW_CMD_WRITE_FEAT: |
| 414 | if (check_len) { |
| 415 | if (ccw.count != sizeof(features)) { |
| 416 | ret = -EINVAL; |
| 417 | break; |
| 418 | } |
| 419 | } else if (ccw.count < sizeof(features)) { |
| 420 | /* Can't execute command. */ |
| 421 | ret = -EINVAL; |
| 422 | break; |
| 423 | } |
| 424 | if (!ccw.cda) { |
| 425 | ret = -EFAULT; |
| 426 | } else { |
| 427 | ret = ccw_dstream_read(&sch->cds, features); |
| 428 | if (ret) { |
| 429 | break; |
| 430 | } |
| 431 | features.features = le32_to_cpu(features.features); |
| 432 | if (features.index == 0) { |
| 433 | virtio_set_features(vdev, |
| 434 | (vdev->guest_features & 0xffffffff00000000ULL) | |
| 435 | features.features); |
| 436 | } else if ((features.index == 1) && (dev->revision >= 1)) { |
| 437 | /* |
| 438 | * If the guest did not negotiate at least revision 1, |
| 439 | * we did not offer it any feature bits beyond 31. Such a |
| 440 | * guest passing us any bit here is therefore buggy. |
| 441 | */ |
| 442 | virtio_set_features(vdev, |
| 443 | (vdev->guest_features & 0x00000000ffffffffULL) | |
| 444 | ((uint64_t)features.features << 32)); |
| 445 | } else { |
| 446 | /* |
| 447 | * If the guest supports more feature bits, assert that it |
| 448 | * passes us zeroes for those we don't support. |
| 449 | */ |
| 450 | if (features.features) { |
| 451 | qemu_log_mask(LOG_GUEST_ERROR, |
| 452 | "Guest bug: features[%i]=%x (expected 0)", |
| 453 | features.index, features.features); |
| 454 | /* XXX: do a unit check here? */ |
| 455 | } |
| 456 | } |
| 457 | sch->curr_status.scsw.count = ccw.count - sizeof(features); |
| 458 | ret = 0; |
| 459 | } |
| 460 | break; |
| 461 | case CCW_CMD_READ_CONF: |
| 462 | if (check_len) { |
| 463 | if (ccw.count > vdev->config_len) { |
| 464 | ret = -EINVAL; |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | len = MIN(ccw.count, vdev->config_len); |
| 469 | if (!ccw.cda) { |
| 470 | ret = -EFAULT; |
| 471 | } else { |
| 472 | virtio_bus_get_vdev_config(&dev->bus, vdev->config); |
| 473 | ret = ccw_dstream_write_buf(&sch->cds, vdev->config, len); |
| 474 | if (ret) { |
| 475 | sch->curr_status.scsw.count = ccw.count - len; |
| 476 | } |
| 477 | } |
| 478 | break; |
| 479 | case CCW_CMD_WRITE_CONF: |
| 480 | if (check_len) { |
| 481 | if (ccw.count > vdev->config_len) { |
| 482 | ret = -EINVAL; |
| 483 | break; |
| 484 | } |
| 485 | } |
| 486 | len = MIN(ccw.count, vdev->config_len); |
| 487 | if (!ccw.cda) { |
| 488 | ret = -EFAULT; |
| 489 | } else { |
| 490 | ret = ccw_dstream_read_buf(&sch->cds, vdev->config, len); |
| 491 | if (!ret) { |
| 492 | virtio_bus_set_vdev_config(&dev->bus, vdev->config); |
| 493 | sch->curr_status.scsw.count = ccw.count - len; |
| 494 | } |
| 495 | } |
| 496 | break; |
| 497 | case CCW_CMD_READ_STATUS: |
| 498 | if (check_len) { |
| 499 | if (ccw.count != sizeof(status)) { |
| 500 | ret = -EINVAL; |
| 501 | break; |
| 502 | } |
| 503 | } else if (ccw.count < sizeof(status)) { |
| 504 | /* Can't execute command. */ |
| 505 | ret = -EINVAL; |
| 506 | break; |
| 507 | } |
| 508 | if (!ccw.cda) { |
| 509 | ret = -EFAULT; |
| 510 | } else { |
| 511 | address_space_stb(&address_space_memory, ccw.cda, vdev->status, |
| 512 | MEMTXATTRS_UNSPECIFIED, NULL); |
| 513 | sch->curr_status.scsw.count = ccw.count - sizeof(vdev->status); |
| 514 | ret = 0; |
| 515 | } |
| 516 | break; |
| 517 | case CCW_CMD_WRITE_STATUS: |
| 518 | if (check_len) { |
| 519 | if (ccw.count != sizeof(status)) { |
| 520 | ret = -EINVAL; |
| 521 | break; |
| 522 | } |
| 523 | } else if (ccw.count < sizeof(status)) { |
| 524 | /* Can't execute command. */ |
| 525 | ret = -EINVAL; |
| 526 | break; |
| 527 | } |
| 528 | if (!ccw.cda) { |
| 529 | ret = -EFAULT; |
| 530 | } else { |
| 531 | ret = ccw_dstream_read(&sch->cds, status); |
| 532 | if (ret) { |
| 533 | break; |
| 534 | } |
| 535 | if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) { |
| 536 | virtio_ccw_stop_ioeventfd(dev); |
| 537 | } |
| 538 | if (virtio_set_status(vdev, status) == 0) { |
| 539 | if (vdev->status == 0) { |
| 540 | virtio_ccw_reset_virtio(dev); |
| 541 | } |
| 542 | if (status & VIRTIO_CONFIG_S_DRIVER_OK) { |
| 543 | virtio_ccw_start_ioeventfd(dev); |
| 544 | } |
| 545 | sch->curr_status.scsw.count = ccw.count - sizeof(status); |
| 546 | ret = 0; |
| 547 | } else { |
| 548 | /* Trigger a command reject. */ |
| 549 | ret = -ENOSYS; |
| 550 | } |
| 551 | } |
| 552 | break; |
| 553 | case CCW_CMD_SET_IND: |
| 554 | if (check_len) { |
| 555 | if (ccw.count != sizeof(indicators)) { |
| 556 | ret = -EINVAL; |
| 557 | break; |
| 558 | } |
| 559 | } else if (ccw.count < sizeof(indicators)) { |
| 560 | /* Can't execute command. */ |
| 561 | ret = -EINVAL; |
| 562 | break; |
| 563 | } |
| 564 | if (sch->thinint_active) { |
| 565 | /* Trigger a command reject. */ |
| 566 | ret = -ENOSYS; |
| 567 | break; |
| 568 | } |
| 569 | if (virtio_get_num_queues(vdev) > NR_CLASSIC_INDICATOR_BITS) { |
| 570 | /* More queues than indicator bits --> trigger a reject */ |
| 571 | ret = -ENOSYS; |
| 572 | break; |
| 573 | } |
| 574 | if (!ccw.cda) { |
| 575 | ret = -EFAULT; |
| 576 | } else { |
| 577 | ret = ccw_dstream_read(&sch->cds, indicators); |
| 578 | if (ret) { |
| 579 | break; |
| 580 | } |
| 581 | indicators = be64_to_cpu(indicators); |
| 582 | dev->indicators = get_indicator(indicators, sizeof(uint64_t)); |
| 583 | sch->curr_status.scsw.count = ccw.count - sizeof(indicators); |
| 584 | ret = 0; |
| 585 | } |
| 586 | break; |
| 587 | case CCW_CMD_SET_CONF_IND: |
| 588 | if (check_len) { |
| 589 | if (ccw.count != sizeof(indicators)) { |
| 590 | ret = -EINVAL; |
| 591 | break; |
| 592 | } |
| 593 | } else if (ccw.count < sizeof(indicators)) { |
| 594 | /* Can't execute command. */ |
| 595 | ret = -EINVAL; |
| 596 | break; |
| 597 | } |
| 598 | if (!ccw.cda) { |
| 599 | ret = -EFAULT; |
| 600 | } else { |
| 601 | ret = ccw_dstream_read(&sch->cds, indicators); |
| 602 | if (ret) { |
| 603 | break; |
| 604 | } |
| 605 | indicators = be64_to_cpu(indicators); |
| 606 | dev->indicators2 = get_indicator(indicators, sizeof(uint64_t)); |
| 607 | sch->curr_status.scsw.count = ccw.count - sizeof(indicators); |
| 608 | ret = 0; |
| 609 | } |
| 610 | break; |
| 611 | case CCW_CMD_READ_VQ_CONF: |
| 612 | if (check_len) { |
| 613 | if (ccw.count != sizeof(vq_config)) { |
| 614 | ret = -EINVAL; |
| 615 | break; |
| 616 | } |
| 617 | } else if (ccw.count < sizeof(vq_config)) { |
| 618 | /* Can't execute command. */ |
| 619 | ret = -EINVAL; |
| 620 | break; |
| 621 | } |
| 622 | if (!ccw.cda) { |
| 623 | ret = -EFAULT; |
| 624 | } else { |
| 625 | ret = ccw_dstream_read(&sch->cds, vq_config.index); |
| 626 | if (ret) { |
| 627 | break; |
| 628 | } |
| 629 | vq_config.index = be16_to_cpu(vq_config.index); |
| 630 | if (vq_config.index >= VIRTIO_QUEUE_MAX) { |
| 631 | ret = -EINVAL; |
| 632 | break; |
| 633 | } |
| 634 | vq_config.num_max = virtio_queue_get_num(vdev, |
| 635 | vq_config.index); |
| 636 | vq_config.num_max = cpu_to_be16(vq_config.num_max); |
| 637 | ret = ccw_dstream_write(&sch->cds, vq_config.num_max); |
| 638 | if (!ret) { |
| 639 | sch->curr_status.scsw.count = ccw.count - sizeof(vq_config); |
| 640 | } |
| 641 | } |
| 642 | break; |
| 643 | case CCW_CMD_SET_IND_ADAPTER: |
| 644 | if (check_len) { |
| 645 | if (ccw.count != sizeof(thinint)) { |
| 646 | ret = -EINVAL; |
| 647 | break; |
| 648 | } |
| 649 | } else if (ccw.count < sizeof(thinint)) { |
| 650 | /* Can't execute command. */ |
| 651 | ret = -EINVAL; |
| 652 | break; |
| 653 | } |
| 654 | if (!ccw.cda) { |
| 655 | ret = -EFAULT; |
| 656 | } else if (dev->indicators && !sch->thinint_active) { |
| 657 | /* Trigger a command reject. */ |
| 658 | ret = -ENOSYS; |
| 659 | } else { |
| 660 | if (ccw_dstream_read(&sch->cds, thinint)) { |
| 661 | ret = -EFAULT; |
| 662 | } else { |
| 663 | thinint.ind_bit = be64_to_cpu(thinint.ind_bit); |
| 664 | thinint.summary_indicator = |
| 665 | be64_to_cpu(thinint.summary_indicator); |
| 666 | thinint.device_indicator = |
| 667 | be64_to_cpu(thinint.device_indicator); |
| 668 | |
| 669 | dev->summary_indicator = |
| 670 | get_indicator(thinint.summary_indicator, sizeof(uint8_t)); |
| 671 | dev->indicators = |
| 672 | get_indicator(thinint.device_indicator, |
| 673 | thinint.ind_bit / 8 + 1); |
| 674 | dev->thinint_isc = thinint.isc; |
| 675 | dev->routes.adapter.ind_offset = thinint.ind_bit; |
| 676 | dev->routes.adapter.summary_offset = 7; |
| 677 | dev->routes.adapter.adapter_id = css_get_adapter_id( |
| 678 | CSS_IO_ADAPTER_VIRTIO, |
| 679 | dev->thinint_isc); |
| 680 | sch->thinint_active = ((dev->indicators != NULL) && |
| 681 | (dev->summary_indicator != NULL)); |
| 682 | sch->curr_status.scsw.count = ccw.count - sizeof(thinint); |
| 683 | ret = 0; |
| 684 | } |
| 685 | } |
| 686 | break; |
| 687 | case CCW_CMD_SET_VIRTIO_REV: |
| 688 | len = sizeof(revinfo); |
| 689 | if (ccw.count < len) { |
| 690 | ret = -EINVAL; |
| 691 | break; |
| 692 | } |
| 693 | if (!ccw.cda) { |
| 694 | ret = -EFAULT; |
| 695 | break; |
| 696 | } |
| 697 | ret = ccw_dstream_read_buf(&sch->cds, &revinfo, 4); |
| 698 | if (ret < 0) { |
| 699 | break; |
| 700 | } |
| 701 | revinfo.revision = be16_to_cpu(revinfo.revision); |
| 702 | revinfo.length = be16_to_cpu(revinfo.length); |
| 703 | if (ccw.count < len + revinfo.length || |
| 704 | (check_len && ccw.count > len + revinfo.length)) { |
| 705 | ret = -EINVAL; |
| 706 | break; |
| 707 | } |
| 708 | /* |
| 709 | * Once we start to support revisions with additional data, we'll |
| 710 | * need to fetch it here. Nothing to do for now, though. |
| 711 | */ |
| 712 | if (dev->revision >= 0 || |
| 713 | revinfo.revision > virtio_ccw_rev_max(dev) || |
| 714 | (dev->force_revision_1 && !revinfo.revision)) { |
| 715 | ret = -ENOSYS; |
| 716 | break; |
| 717 | } |
| 718 | ret = 0; |
| 719 | dev->revision = revinfo.revision; |
| 720 | break; |
| 721 | default: |
| 722 | ret = -ENOSYS; |
| 723 | break; |
| 724 | } |
| 725 | return ret; |
| 726 | } |
| 727 | |
| 728 | static void virtio_sch_disable_cb(SubchDev *sch) |
| 729 | { |
| 730 | VirtioCcwDevice *dev = sch->driver_data; |
| 731 | |
| 732 | dev->revision = -1; |
| 733 | } |
| 734 | |
| 735 | static void virtio_ccw_device_realize(VirtioCcwDevice *dev, Error **errp) |
| 736 | { |
| 737 | VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(dev); |
| 738 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
| 739 | CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev); |
| 740 | SubchDev *sch; |
| 741 | Error *err = NULL; |
| 742 | int i; |
| 743 | |
| 744 | sch = css_create_sch(ccw_dev->devno, errp); |
| 745 | if (!sch) { |
| 746 | return; |
| 747 | } |
| 748 | if (!virtio_ccw_rev_max(dev) && dev->force_revision_1) { |
| 749 | error_setg(&err, "Invalid value of property max_rev " |
| 750 | "(is %d expected >= 1)", virtio_ccw_rev_max(dev)); |
| 751 | goto out_err; |
| 752 | } |
| 753 | |
| 754 | sch->driver_data = dev; |
| 755 | sch->ccw_cb = virtio_ccw_cb; |
| 756 | sch->disable_cb = virtio_sch_disable_cb; |
| 757 | sch->id.reserved = 0xff; |
| 758 | sch->id.cu_type = VIRTIO_CCW_CU_TYPE; |
| 759 | sch->do_subchannel_work = do_subchannel_work_virtual; |
| 760 | sch->irb_cb = build_irb_virtual; |
| 761 | ccw_dev->sch = sch; |
| 762 | dev->indicators = NULL; |
| 763 | dev->revision = -1; |
| 764 | for (i = 0; i < ADAPTER_ROUTES_MAX_GSI; i++) { |
| 765 | dev->routes.gsi[i] = -1; |
| 766 | } |
| 767 | css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE); |
| 768 | |
| 769 | trace_virtio_ccw_new_device( |
| 770 | sch->cssid, sch->ssid, sch->schid, sch->devno, |
| 771 | ccw_dev->devno.valid ? "user-configured" : "auto-configured"); |
| 772 | |
| 773 | /* fd-based ioevents can't be synchronized in record/replay */ |
| 774 | if (replay_mode != REPLAY_MODE_NONE) { |
| 775 | dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD; |
| 776 | } |
| 777 | |
| 778 | if (k->realize) { |
| 779 | k->realize(dev, &err); |
| 780 | if (err) { |
| 781 | goto out_err; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | ck->realize(ccw_dev, &err); |
| 786 | if (err) { |
| 787 | goto out_err; |
| 788 | } |
| 789 | |
| 790 | return; |
| 791 | |
| 792 | out_err: |
| 793 | error_propagate(errp, err); |
| 794 | css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); |
| 795 | ccw_dev->sch = NULL; |
| 796 | g_free(sch); |
| 797 | } |
| 798 | |
| 799 | static void virtio_ccw_device_unrealize(VirtioCcwDevice *dev) |
| 800 | { |
| 801 | VirtIOCCWDeviceClass *dc = VIRTIO_CCW_DEVICE_GET_CLASS(dev); |
| 802 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
| 803 | SubchDev *sch = ccw_dev->sch; |
| 804 | |
| 805 | if (dc->unrealize) { |
| 806 | dc->unrealize(dev); |
| 807 | } |
| 808 | |
| 809 | if (sch) { |
| 810 | css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); |
| 811 | g_free(sch); |
| 812 | ccw_dev->sch = NULL; |
| 813 | } |
| 814 | if (dev->indicators) { |
| 815 | release_indicator(&dev->routes.adapter, dev->indicators); |
| 816 | dev->indicators = NULL; |
| 817 | } |
| 818 | } |
| 819 | |
| 820 | /* DeviceState to VirtioCcwDevice. Note: used on datapath, |
| 821 | * be careful and test performance if you change this. |
| 822 | */ |
| 823 | static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d) |
| 824 | { |
| 825 | CcwDevice *ccw_dev = to_ccw_dev_fast(d); |
| 826 | |
| 827 | return container_of(ccw_dev, VirtioCcwDevice, parent_obj); |
| 828 | } |
| 829 | |
| 830 | static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc, |
| 831 | uint8_t to_be_set) |
| 832 | { |
| 833 | uint8_t expected, actual; |
| 834 | hwaddr len = 1; |
| 835 | /* avoid multiple fetches */ |
| 836 | uint8_t volatile *ind_addr; |
| 837 | |
| 838 | ind_addr = physical_memory_map(ind_loc, &len, true); |
| 839 | if (!ind_addr) { |
| 840 | error_report("%s(%x.%x.%04x): unable to access indicator", |
| 841 | __func__, sch->cssid, sch->ssid, sch->schid); |
| 842 | return -1; |
| 843 | } |
| 844 | actual = *ind_addr; |
| 845 | do { |
| 846 | expected = actual; |
| 847 | actual = qatomic_cmpxchg(ind_addr, expected, expected | to_be_set); |
| 848 | } while (actual != expected); |
| 849 | trace_virtio_ccw_set_ind(ind_loc, actual, actual | to_be_set); |
| 850 | physical_memory_unmap((void *)ind_addr, len, 1, len); |
| 851 | |
| 852 | return actual; |
| 853 | } |
| 854 | |
| 855 | static void virtio_ccw_notify(DeviceState *d, uint16_t vector) |
| 856 | { |
| 857 | VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d); |
| 858 | CcwDevice *ccw_dev = to_ccw_dev_fast(d); |
| 859 | SubchDev *sch = ccw_dev->sch; |
| 860 | uint64_t indicators; |
| 861 | |
| 862 | if (vector == VIRTIO_NO_VECTOR) { |
| 863 | return; |
| 864 | } |
| 865 | /* |
| 866 | * vector < VIRTIO_QUEUE_MAX: notification for a virtqueue |
| 867 | * vector == VIRTIO_QUEUE_MAX: configuration change notification |
| 868 | * bits beyond that are unused and should never be notified for |
| 869 | */ |
| 870 | assert(vector <= VIRTIO_QUEUE_MAX); |
| 871 | |
| 872 | if (vector < VIRTIO_QUEUE_MAX) { |
| 873 | if (!dev->indicators) { |
| 874 | return; |
| 875 | } |
| 876 | if (sch->thinint_active) { |
| 877 | /* |
| 878 | * In the adapter interrupt case, indicators points to a |
| 879 | * memory area that may be (way) larger than 64 bit and |
| 880 | * ind_bit indicates the start of the indicators in a big |
| 881 | * endian notation. |
| 882 | */ |
| 883 | uint64_t ind_bit = dev->routes.adapter.ind_offset; |
| 884 | |
| 885 | virtio_set_ind_atomic(sch, dev->indicators->addr + |
| 886 | (ind_bit + vector) / 8, |
| 887 | 0x80 >> ((ind_bit + vector) % 8)); |
| 888 | if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr, |
| 889 | 0x01)) { |
| 890 | css_adapter_interrupt(CSS_IO_ADAPTER_VIRTIO, dev->thinint_isc); |
| 891 | } |
| 892 | } else { |
| 893 | assert(vector < NR_CLASSIC_INDICATOR_BITS); |
| 894 | indicators = address_space_ldq_be(&address_space_memory, |
| 895 | dev->indicators->addr, |
| 896 | MEMTXATTRS_UNSPECIFIED, |
| 897 | NULL); |
| 898 | indicators |= 1ULL << vector; |
| 899 | address_space_stq_be(&address_space_memory, dev->indicators->addr, |
| 900 | indicators, MEMTXATTRS_UNSPECIFIED, NULL); |
| 901 | css_conditional_io_interrupt(sch); |
| 902 | } |
| 903 | } else { |
| 904 | if (!dev->indicators2) { |
| 905 | return; |
| 906 | } |
| 907 | indicators = address_space_ldq_be(&address_space_memory, |
| 908 | dev->indicators2->addr, |
| 909 | MEMTXATTRS_UNSPECIFIED, |
| 910 | NULL); |
| 911 | indicators |= 1ULL; |
| 912 | address_space_stq_be(&address_space_memory, dev->indicators2->addr, |
| 913 | indicators, MEMTXATTRS_UNSPECIFIED, NULL); |
| 914 | css_conditional_io_interrupt(sch); |
| 915 | } |
| 916 | } |
| 917 | |
| 918 | static void virtio_ccw_reset_hold(Object *obj, ResetType type) |
| 919 | { |
| 920 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(obj); |
| 921 | VirtIOCCWDeviceClass *vdc = VIRTIO_CCW_DEVICE_GET_CLASS(dev); |
| 922 | |
| 923 | virtio_ccw_reset_virtio(dev); |
| 924 | |
| 925 | if (vdc->parent_phases.hold) { |
| 926 | vdc->parent_phases.hold(obj, type); |
| 927 | } |
| 928 | } |
| 929 | |
| 930 | static void virtio_ccw_vmstate_change(DeviceState *d, bool running) |
| 931 | { |
| 932 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 933 | |
| 934 | if (running) { |
| 935 | virtio_ccw_start_ioeventfd(dev); |
| 936 | } else { |
| 937 | virtio_ccw_stop_ioeventfd(dev); |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | static bool virtio_ccw_query_guest_notifiers(DeviceState *d) |
| 942 | { |
| 943 | CcwDevice *dev = CCW_DEVICE(d); |
| 944 | |
| 945 | return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA); |
| 946 | } |
| 947 | |
| 948 | static int virtio_ccw_get_mappings(VirtioCcwDevice *dev) |
| 949 | { |
| 950 | int r; |
| 951 | CcwDevice *ccw_dev = CCW_DEVICE(dev); |
| 952 | |
| 953 | if (!ccw_dev->sch->thinint_active) { |
| 954 | return -EINVAL; |
| 955 | } |
| 956 | |
| 957 | r = map_indicator(&dev->routes.adapter, dev->summary_indicator); |
| 958 | if (r) { |
| 959 | return r; |
| 960 | } |
| 961 | r = map_indicator(&dev->routes.adapter, dev->indicators); |
| 962 | if (r) { |
| 963 | return r; |
| 964 | } |
| 965 | dev->routes.adapter.summary_addr = dev->summary_indicator->map; |
| 966 | dev->routes.adapter.ind_addr = dev->indicators->map; |
| 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs) |
| 972 | { |
| 973 | int i; |
| 974 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 975 | int ret; |
| 976 | S390FLICState *fs = s390_get_flic(); |
| 977 | S390FLICStateClass *fsc = s390_get_flic_class(fs); |
| 978 | |
| 979 | ret = virtio_ccw_get_mappings(dev); |
| 980 | if (ret) { |
| 981 | return ret; |
| 982 | } |
| 983 | for (i = 0; i < nvqs; i++) { |
| 984 | if (!virtio_queue_get_num(vdev, i)) { |
| 985 | break; |
| 986 | } |
| 987 | } |
| 988 | dev->routes.num_routes = i; |
| 989 | return fsc->add_adapter_routes(fs, &dev->routes); |
| 990 | } |
| 991 | |
| 992 | static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs) |
| 993 | { |
| 994 | S390FLICState *fs = s390_get_flic(); |
| 995 | S390FLICStateClass *fsc = s390_get_flic_class(fs); |
| 996 | |
| 997 | fsc->release_adapter_routes(fs, &dev->routes); |
| 998 | } |
| 999 | |
| 1000 | static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n) |
| 1001 | { |
| 1002 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1003 | VirtQueue *vq = virtio_get_queue(vdev, n); |
| 1004 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); |
| 1005 | |
| 1006 | return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL, |
| 1007 | dev->routes.gsi[n]); |
| 1008 | } |
| 1009 | |
| 1010 | static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n) |
| 1011 | { |
| 1012 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1013 | VirtQueue *vq = virtio_get_queue(vdev, n); |
| 1014 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); |
| 1015 | int ret; |
| 1016 | |
| 1017 | ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier, |
| 1018 | dev->routes.gsi[n]); |
| 1019 | assert(ret == 0); |
| 1020 | } |
| 1021 | |
| 1022 | static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n, |
| 1023 | bool assign, bool with_irqfd) |
| 1024 | { |
| 1025 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1026 | VirtQueue *vq = virtio_get_queue(vdev, n); |
| 1027 | EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); |
| 1028 | VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); |
| 1029 | int r; |
| 1030 | |
| 1031 | if (!assign) { |
| 1032 | if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) { |
| 1033 | k->guest_notifier_mask(vdev, n, true); |
| 1034 | } |
| 1035 | if (with_irqfd) { |
| 1036 | virtio_ccw_remove_irqfd(dev, n); |
| 1037 | } |
| 1038 | } |
| 1039 | |
| 1040 | r = virtio_set_guest_notifier(vdev, n, assign, with_irqfd); |
| 1041 | if (r < 0) { |
| 1042 | return r; |
| 1043 | } |
| 1044 | |
| 1045 | if (assign) { |
| 1046 | if (with_irqfd) { |
| 1047 | r = virtio_ccw_add_irqfd(dev, n); |
| 1048 | if (r) { |
| 1049 | virtio_set_guest_notifier(vdev, n, false, with_irqfd); |
| 1050 | return r; |
| 1051 | } |
| 1052 | } |
| 1053 | /* |
| 1054 | * We do not support individual masking for channel devices, so we |
| 1055 | * need to manually trigger any guest masking callbacks here. |
| 1056 | */ |
| 1057 | if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) { |
| 1058 | k->guest_notifier_mask(vdev, n, false); |
| 1059 | } |
| 1060 | /* get lost events and re-inject */ |
| 1061 | if (k->guest_notifier_pending && |
| 1062 | k->guest_notifier_pending(vdev, n)) { |
| 1063 | event_notifier_set(notifier); |
| 1064 | } |
| 1065 | } |
| 1066 | |
| 1067 | return 0; |
| 1068 | } |
| 1069 | |
| 1070 | static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs, |
| 1071 | bool assigned) |
| 1072 | { |
| 1073 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1074 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1075 | CcwDevice *ccw_dev = CCW_DEVICE(d); |
| 1076 | bool with_irqfd = ccw_dev->sch->thinint_active && kvm_irqfds_enabled(); |
| 1077 | int r, n; |
| 1078 | |
| 1079 | if (with_irqfd && assigned) { |
| 1080 | /* irq routes need to be set up before assigning irqfds */ |
| 1081 | r = virtio_ccw_setup_irqroutes(dev, nvqs); |
| 1082 | if (r < 0) { |
| 1083 | goto irqroute_error; |
| 1084 | } |
| 1085 | } |
| 1086 | for (n = 0; n < nvqs; n++) { |
| 1087 | if (!virtio_queue_get_num(vdev, n)) { |
| 1088 | break; |
| 1089 | } |
| 1090 | r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd); |
| 1091 | if (r < 0) { |
| 1092 | goto assign_error; |
| 1093 | } |
| 1094 | } |
| 1095 | if (with_irqfd && !assigned) { |
| 1096 | /* release irq routes after irqfds have been released */ |
| 1097 | virtio_ccw_release_irqroutes(dev, nvqs); |
| 1098 | } |
| 1099 | return 0; |
| 1100 | |
| 1101 | assign_error: |
| 1102 | while (--n >= 0) { |
| 1103 | virtio_ccw_set_guest_notifier(dev, n, !assigned, false); |
| 1104 | } |
| 1105 | irqroute_error: |
| 1106 | if (with_irqfd && assigned) { |
| 1107 | virtio_ccw_release_irqroutes(dev, nvqs); |
| 1108 | } |
| 1109 | return r; |
| 1110 | } |
| 1111 | |
| 1112 | static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f) |
| 1113 | { |
| 1114 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1115 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1116 | |
| 1117 | qemu_put_be16(f, virtio_queue_vector(vdev, n)); |
| 1118 | } |
| 1119 | |
| 1120 | static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f) |
| 1121 | { |
| 1122 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1123 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1124 | uint16_t vector; |
| 1125 | |
| 1126 | qemu_get_be16s(f, &vector); |
| 1127 | virtio_queue_set_vector(vdev, n , vector); |
| 1128 | |
| 1129 | return 0; |
| 1130 | } |
| 1131 | |
| 1132 | static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f) |
| 1133 | { |
| 1134 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1135 | Error *local_err = NULL; |
| 1136 | int ret; |
| 1137 | |
| 1138 | ret = vmstate_save_state(f, &vmstate_virtio_ccw_dev, dev, NULL, &local_err); |
| 1139 | if (ret < 0) { |
| 1140 | error_report_err(local_err); |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f) |
| 1145 | { |
| 1146 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1147 | Error *local_err = NULL; |
| 1148 | int ret; |
| 1149 | |
| 1150 | ret = vmstate_load_state(f, &vmstate_virtio_ccw_dev, dev, 1, &local_err); |
| 1151 | if (ret < 0) { |
| 1152 | error_report_err(local_err); |
| 1153 | } |
| 1154 | return ret; |
| 1155 | } |
| 1156 | |
| 1157 | static void virtio_ccw_pre_plugged(DeviceState *d, Error **errp) |
| 1158 | { |
| 1159 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1160 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1161 | |
| 1162 | if (dev->max_rev >= 1) { |
| 1163 | virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1); |
| 1164 | } |
| 1165 | } |
| 1166 | |
| 1167 | /* This is called by virtio-bus just after the device is plugged. */ |
| 1168 | static void virtio_ccw_device_plugged(DeviceState *d, Error **errp) |
| 1169 | { |
| 1170 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1171 | VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); |
| 1172 | CcwDevice *ccw_dev = CCW_DEVICE(d); |
| 1173 | SubchDev *sch = ccw_dev->sch; |
| 1174 | int n = virtio_get_num_queues(vdev); |
| 1175 | |
| 1176 | if (!virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) { |
| 1177 | dev->max_rev = 0; |
| 1178 | } |
| 1179 | |
| 1180 | if (!virtio_ccw_rev_max(dev) && !virtio_legacy_allowed(vdev)) { |
| 1181 | /* |
| 1182 | * To avoid migration issues, we allow legacy mode when legacy |
| 1183 | * check is disabled in the old machine types (< 5.1). |
| 1184 | */ |
| 1185 | if (virtio_legacy_check_disabled(vdev)) { |
| 1186 | warn_report("device requires revision >= 1, but for backward " |
| 1187 | "compatibility max_revision=0 is allowed"); |
| 1188 | } else { |
| 1189 | error_setg(errp, "Invalid value of property max_rev " |
| 1190 | "(is %d expected >= 1)", virtio_ccw_rev_max(dev)); |
| 1191 | return; |
| 1192 | } |
| 1193 | } |
| 1194 | |
| 1195 | if (virtio_get_num_queues(vdev) > VIRTIO_QUEUE_MAX) { |
| 1196 | error_setg(errp, "The number of virtqueues %d " |
| 1197 | "exceeds virtio limit %d", n, |
| 1198 | VIRTIO_QUEUE_MAX); |
| 1199 | return; |
| 1200 | } |
| 1201 | if (virtio_get_num_queues(vdev) > ADAPTER_ROUTES_MAX_GSI) { |
| 1202 | error_setg(errp, "The number of virtqueues %d " |
| 1203 | "exceeds flic adapter route limit %d", n, |
| 1204 | ADAPTER_ROUTES_MAX_GSI); |
| 1205 | return; |
| 1206 | } |
| 1207 | |
| 1208 | sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus); |
| 1209 | |
| 1210 | |
| 1211 | css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, |
| 1212 | d->hotplugged, 1); |
| 1213 | } |
| 1214 | |
| 1215 | static void virtio_ccw_device_unplugged(DeviceState *d) |
| 1216 | { |
| 1217 | VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); |
| 1218 | |
| 1219 | virtio_ccw_stop_ioeventfd(dev); |
| 1220 | } |
| 1221 | /**************** Virtio-ccw Bus Device Descriptions *******************/ |
| 1222 | |
| 1223 | static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp) |
| 1224 | { |
| 1225 | VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; |
| 1226 | |
| 1227 | virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev); |
| 1228 | virtio_ccw_device_realize(_dev, errp); |
| 1229 | } |
| 1230 | |
| 1231 | static void virtio_ccw_busdev_unrealize(DeviceState *dev) |
| 1232 | { |
| 1233 | VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; |
| 1234 | |
| 1235 | virtio_ccw_device_unrealize(_dev); |
| 1236 | } |
| 1237 | |
| 1238 | static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev, |
| 1239 | DeviceState *dev, Error **errp) |
| 1240 | { |
| 1241 | VirtioCcwDevice *_dev = to_virtio_ccw_dev_fast(dev); |
| 1242 | |
| 1243 | virtio_ccw_stop_ioeventfd(_dev); |
| 1244 | } |
| 1245 | |
| 1246 | static void virtio_ccw_device_class_init(ObjectClass *klass, const void *data) |
| 1247 | { |
| 1248 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 1249 | CCWDeviceClass *k = CCW_DEVICE_CLASS(dc); |
| 1250 | VirtIOCCWDeviceClass *vdc = VIRTIO_CCW_DEVICE_CLASS(klass); |
| 1251 | ResettableClass *rc = RESETTABLE_CLASS(klass); |
| 1252 | |
| 1253 | k->unplug = virtio_ccw_busdev_unplug; |
| 1254 | dc->realize = virtio_ccw_busdev_realize; |
| 1255 | dc->unrealize = virtio_ccw_busdev_unrealize; |
| 1256 | resettable_class_set_parent_phases(rc, NULL, virtio_ccw_reset_hold, NULL, |
| 1257 | &vdc->parent_phases); |
| 1258 | } |
| 1259 | |
| 1260 | static const TypeInfo virtio_ccw_device_info = { |
| 1261 | .name = TYPE_VIRTIO_CCW_DEVICE, |
| 1262 | .parent = TYPE_CCW_DEVICE, |
| 1263 | .instance_size = sizeof(VirtioCcwDevice), |
| 1264 | .class_init = virtio_ccw_device_class_init, |
| 1265 | .class_size = sizeof(VirtIOCCWDeviceClass), |
| 1266 | .abstract = true, |
| 1267 | }; |
| 1268 | |
| 1269 | /* virtio-ccw-bus */ |
| 1270 | |
| 1271 | static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size, |
| 1272 | VirtioCcwDevice *dev) |
| 1273 | { |
| 1274 | DeviceState *qdev = DEVICE(dev); |
| 1275 | char virtio_bus_name[] = "virtio-bus"; |
| 1276 | |
| 1277 | qbus_init(bus, bus_size, TYPE_VIRTIO_CCW_BUS, qdev, virtio_bus_name); |
| 1278 | } |
| 1279 | |
| 1280 | static void virtio_ccw_bus_class_init(ObjectClass *klass, const void *data) |
| 1281 | { |
| 1282 | VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); |
| 1283 | BusClass *bus_class = BUS_CLASS(klass); |
| 1284 | |
| 1285 | bus_class->max_dev = 1; |
| 1286 | k->notify = virtio_ccw_notify; |
| 1287 | k->vmstate_change = virtio_ccw_vmstate_change; |
| 1288 | k->query_guest_notifiers = virtio_ccw_query_guest_notifiers; |
| 1289 | k->set_guest_notifiers = virtio_ccw_set_guest_notifiers; |
| 1290 | k->save_queue = virtio_ccw_save_queue; |
| 1291 | k->load_queue = virtio_ccw_load_queue; |
| 1292 | k->save_config = virtio_ccw_save_config; |
| 1293 | k->load_config = virtio_ccw_load_config; |
| 1294 | k->pre_plugged = virtio_ccw_pre_plugged; |
| 1295 | k->device_plugged = virtio_ccw_device_plugged; |
| 1296 | k->device_unplugged = virtio_ccw_device_unplugged; |
| 1297 | k->ioeventfd_enabled = virtio_ccw_ioeventfd_enabled; |
| 1298 | k->ioeventfd_assign = virtio_ccw_ioeventfd_assign; |
| 1299 | } |
| 1300 | |
| 1301 | static const TypeInfo virtio_ccw_bus_info = { |
| 1302 | .name = TYPE_VIRTIO_CCW_BUS, |
| 1303 | .parent = TYPE_VIRTIO_BUS, |
| 1304 | .instance_size = sizeof(VirtioCcwBusState), |
| 1305 | .class_size = sizeof(VirtioCcwBusClass), |
| 1306 | .class_init = virtio_ccw_bus_class_init, |
| 1307 | }; |
| 1308 | |
| 1309 | static void virtio_ccw_register(void) |
| 1310 | { |
| 1311 | type_register_static(&virtio_ccw_bus_info); |
| 1312 | type_register_static(&virtio_ccw_device_info); |
| 1313 | } |
| 1314 | |
| 1315 | type_init(virtio_ccw_register) |