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 "hw/hw.h" 16 #include "sysemu/block-backend.h" 17 #include "sysemu/blockdev.h" 18 #include "sysemu/sysemu.h" 19 #include "sysemu/kvm.h" 20 #include "net/net.h" 21 #include "hw/virtio/virtio.h" 22 #include "hw/virtio/virtio-serial.h" 23 #include "hw/virtio/virtio-net.h" 24 #include "hw/sysbus.h" 25 #include "qemu/bitops.h" 26 #include "qemu/error-report.h" 27 #include "hw/virtio/virtio-access.h" 28 #include "hw/virtio/virtio-bus.h" 29 #include "hw/s390x/adapter.h" 30 #include "hw/s390x/s390_flic.h" 31 32 #include "hw/s390x/ioinst.h" 33 #include "hw/s390x/css.h" 34 #include "virtio-ccw.h" 35 #include "trace.h" 36 #include "hw/s390x/css-bridge.h" 37 38 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size, 39 VirtioCcwDevice *dev); 40 41 VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch) 42 { 43 VirtIODevice *vdev = NULL; 44 VirtioCcwDevice *dev = sch->driver_data; 45 46 if (dev) { 47 vdev = virtio_bus_get_device(&dev->bus); 48 } 49 return vdev; 50 } 51 52 static void virtio_ccw_start_ioeventfd(VirtioCcwDevice *dev) 53 { 54 virtio_bus_start_ioeventfd(&dev->bus); 55 } 56 57 static void virtio_ccw_stop_ioeventfd(VirtioCcwDevice *dev) 58 { 59 virtio_bus_stop_ioeventfd(&dev->bus); 60 } 61 62 static bool virtio_ccw_ioeventfd_enabled(DeviceState *d) 63 { 64 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 65 66 return (dev->flags & VIRTIO_CCW_FLAG_USE_IOEVENTFD) != 0; 67 } 68 69 static int virtio_ccw_ioeventfd_assign(DeviceState *d, EventNotifier *notifier, 70 int n, bool assign) 71 { 72 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 73 CcwDevice *ccw_dev = CCW_DEVICE(dev); 74 SubchDev *sch = ccw_dev->sch; 75 uint32_t sch_id = (css_build_subchannel_id(sch) << 16) | sch->schid; 76 77 return s390_assign_subch_ioeventfd(notifier, sch_id, n, assign); 78 } 79 80 /* Communication blocks used by several channel commands. */ 81 typedef struct VqInfoBlockLegacy { 82 uint64_t queue; 83 uint32_t align; 84 uint16_t index; 85 uint16_t num; 86 } QEMU_PACKED VqInfoBlockLegacy; 87 88 typedef struct VqInfoBlock { 89 uint64_t desc; 90 uint32_t res0; 91 uint16_t index; 92 uint16_t num; 93 uint64_t avail; 94 uint64_t used; 95 } QEMU_PACKED VqInfoBlock; 96 97 typedef struct VqConfigBlock { 98 uint16_t index; 99 uint16_t num_max; 100 } QEMU_PACKED VqConfigBlock; 101 102 typedef struct VirtioFeatDesc { 103 uint32_t features; 104 uint8_t index; 105 } QEMU_PACKED VirtioFeatDesc; 106 107 typedef struct VirtioThinintInfo { 108 hwaddr summary_indicator; 109 hwaddr device_indicator; 110 uint64_t ind_bit; 111 uint8_t isc; 112 } QEMU_PACKED VirtioThinintInfo; 113 114 typedef struct VirtioRevInfo { 115 uint16_t revision; 116 uint16_t length; 117 uint8_t data[0]; 118 } QEMU_PACKED VirtioRevInfo; 119 120 /* Specify where the virtqueues for the subchannel are in guest memory. */ 121 static int virtio_ccw_set_vqs(SubchDev *sch, VqInfoBlock *info, 122 VqInfoBlockLegacy *linfo) 123 { 124 VirtIODevice *vdev = virtio_ccw_get_vdev(sch); 125 uint16_t index = info ? info->index : linfo->index; 126 uint16_t num = info ? info->num : linfo->num; 127 uint64_t desc = info ? info->desc : linfo->queue; 128 129 if (index >= VIRTIO_CCW_QUEUE_MAX) { 130 return -EINVAL; 131 } 132 133 /* Current code in virtio.c relies on 4K alignment. */ 134 if (linfo && desc && (linfo->align != 4096)) { 135 return -EINVAL; 136 } 137 138 if (!vdev) { 139 return -EINVAL; 140 } 141 142 if (info) { 143 virtio_queue_set_rings(vdev, index, desc, info->avail, info->used); 144 } else { 145 virtio_queue_set_addr(vdev, index, desc); 146 } 147 if (!desc) { 148 virtio_queue_set_vector(vdev, index, VIRTIO_NO_VECTOR); 149 } else { 150 if (info) { 151 /* virtio-1 allows changing the ring size. */ 152 if (virtio_queue_get_num(vdev, index) < num) { 153 /* Fail if we exceed the maximum number. */ 154 return -EINVAL; 155 } 156 virtio_queue_set_num(vdev, index, num); 157 } else if (virtio_queue_get_num(vdev, index) > num) { 158 /* Fail if we don't have a big enough queue. */ 159 return -EINVAL; 160 } 161 /* We ignore possible increased num for legacy for compatibility. */ 162 virtio_queue_set_vector(vdev, index, index); 163 } 164 /* tell notify handler in case of config change */ 165 vdev->config_vector = VIRTIO_CCW_QUEUE_MAX; 166 return 0; 167 } 168 169 static void virtio_ccw_reset_virtio(VirtioCcwDevice *dev, VirtIODevice *vdev) 170 { 171 CcwDevice *ccw_dev = CCW_DEVICE(dev); 172 173 virtio_ccw_stop_ioeventfd(dev); 174 virtio_reset(vdev); 175 if (dev->indicators) { 176 release_indicator(&dev->routes.adapter, dev->indicators); 177 dev->indicators = NULL; 178 } 179 if (dev->indicators2) { 180 release_indicator(&dev->routes.adapter, dev->indicators2); 181 dev->indicators2 = NULL; 182 } 183 if (dev->summary_indicator) { 184 release_indicator(&dev->routes.adapter, dev->summary_indicator); 185 dev->summary_indicator = NULL; 186 } 187 ccw_dev->sch->thinint_active = false; 188 } 189 190 static int virtio_ccw_handle_set_vq(SubchDev *sch, CCW1 ccw, bool check_len, 191 bool is_legacy) 192 { 193 int ret; 194 VqInfoBlock info; 195 VqInfoBlockLegacy linfo; 196 size_t info_len = is_legacy ? sizeof(linfo) : sizeof(info); 197 198 if (check_len) { 199 if (ccw.count != info_len) { 200 return -EINVAL; 201 } 202 } else if (ccw.count < info_len) { 203 /* Can't execute command. */ 204 return -EINVAL; 205 } 206 if (!ccw.cda) { 207 return -EFAULT; 208 } 209 if (is_legacy) { 210 linfo.queue = address_space_ldq_be(&address_space_memory, ccw.cda, 211 MEMTXATTRS_UNSPECIFIED, NULL); 212 linfo.align = address_space_ldl_be(&address_space_memory, 213 ccw.cda + sizeof(linfo.queue), 214 MEMTXATTRS_UNSPECIFIED, 215 NULL); 216 linfo.index = address_space_lduw_be(&address_space_memory, 217 ccw.cda + sizeof(linfo.queue) 218 + sizeof(linfo.align), 219 MEMTXATTRS_UNSPECIFIED, 220 NULL); 221 linfo.num = address_space_lduw_be(&address_space_memory, 222 ccw.cda + sizeof(linfo.queue) 223 + sizeof(linfo.align) 224 + sizeof(linfo.index), 225 MEMTXATTRS_UNSPECIFIED, 226 NULL); 227 ret = virtio_ccw_set_vqs(sch, NULL, &linfo); 228 } else { 229 info.desc = address_space_ldq_be(&address_space_memory, ccw.cda, 230 MEMTXATTRS_UNSPECIFIED, NULL); 231 info.index = address_space_lduw_be(&address_space_memory, 232 ccw.cda + sizeof(info.desc) 233 + sizeof(info.res0), 234 MEMTXATTRS_UNSPECIFIED, NULL); 235 info.num = address_space_lduw_be(&address_space_memory, 236 ccw.cda + sizeof(info.desc) 237 + sizeof(info.res0) 238 + sizeof(info.index), 239 MEMTXATTRS_UNSPECIFIED, NULL); 240 info.avail = address_space_ldq_be(&address_space_memory, 241 ccw.cda + sizeof(info.desc) 242 + sizeof(info.res0) 243 + sizeof(info.index) 244 + sizeof(info.num), 245 MEMTXATTRS_UNSPECIFIED, NULL); 246 info.used = address_space_ldq_be(&address_space_memory, 247 ccw.cda + sizeof(info.desc) 248 + sizeof(info.res0) 249 + sizeof(info.index) 250 + sizeof(info.num) 251 + sizeof(info.avail), 252 MEMTXATTRS_UNSPECIFIED, NULL); 253 ret = virtio_ccw_set_vqs(sch, &info, NULL); 254 } 255 sch->curr_status.scsw.count = 0; 256 return ret; 257 } 258 259 static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw) 260 { 261 int ret; 262 VirtioRevInfo revinfo; 263 uint8_t status; 264 VirtioFeatDesc features; 265 void *config; 266 hwaddr indicators; 267 VqConfigBlock vq_config; 268 VirtioCcwDevice *dev = sch->driver_data; 269 VirtIODevice *vdev = virtio_ccw_get_vdev(sch); 270 bool check_len; 271 int len; 272 hwaddr hw_len; 273 VirtioThinintInfo *thinint; 274 275 if (!dev) { 276 return -EINVAL; 277 } 278 279 trace_virtio_ccw_interpret_ccw(sch->cssid, sch->ssid, sch->schid, 280 ccw.cmd_code); 281 check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC)); 282 283 /* Look at the command. */ 284 switch (ccw.cmd_code) { 285 case CCW_CMD_SET_VQ: 286 ret = virtio_ccw_handle_set_vq(sch, ccw, check_len, dev->revision < 1); 287 break; 288 case CCW_CMD_VDEV_RESET: 289 virtio_ccw_reset_virtio(dev, vdev); 290 ret = 0; 291 break; 292 case CCW_CMD_READ_FEAT: 293 if (check_len) { 294 if (ccw.count != sizeof(features)) { 295 ret = -EINVAL; 296 break; 297 } 298 } else if (ccw.count < sizeof(features)) { 299 /* Can't execute command. */ 300 ret = -EINVAL; 301 break; 302 } 303 if (!ccw.cda) { 304 ret = -EFAULT; 305 } else { 306 VirtioDeviceClass *vdc = VIRTIO_DEVICE_GET_CLASS(vdev); 307 308 features.index = address_space_ldub(&address_space_memory, 309 ccw.cda 310 + sizeof(features.features), 311 MEMTXATTRS_UNSPECIFIED, 312 NULL); 313 if (features.index == 0) { 314 if (dev->revision >= 1) { 315 /* Don't offer legacy features for modern devices. */ 316 features.features = (uint32_t) 317 (vdev->host_features & ~vdc->legacy_features); 318 } else { 319 features.features = (uint32_t)vdev->host_features; 320 } 321 } else if ((features.index == 1) && (dev->revision >= 1)) { 322 /* 323 * Only offer feature bits beyond 31 if the guest has 324 * negotiated at least revision 1. 325 */ 326 features.features = (uint32_t)(vdev->host_features >> 32); 327 } else { 328 /* Return zeroes if the guest supports more feature bits. */ 329 features.features = 0; 330 } 331 address_space_stl_le(&address_space_memory, ccw.cda, 332 features.features, MEMTXATTRS_UNSPECIFIED, 333 NULL); 334 sch->curr_status.scsw.count = ccw.count - sizeof(features); 335 ret = 0; 336 } 337 break; 338 case CCW_CMD_WRITE_FEAT: 339 if (check_len) { 340 if (ccw.count != sizeof(features)) { 341 ret = -EINVAL; 342 break; 343 } 344 } else if (ccw.count < sizeof(features)) { 345 /* Can't execute command. */ 346 ret = -EINVAL; 347 break; 348 } 349 if (!ccw.cda) { 350 ret = -EFAULT; 351 } else { 352 features.index = address_space_ldub(&address_space_memory, 353 ccw.cda 354 + sizeof(features.features), 355 MEMTXATTRS_UNSPECIFIED, 356 NULL); 357 features.features = address_space_ldl_le(&address_space_memory, 358 ccw.cda, 359 MEMTXATTRS_UNSPECIFIED, 360 NULL); 361 if (features.index == 0) { 362 virtio_set_features(vdev, 363 (vdev->guest_features & 0xffffffff00000000ULL) | 364 features.features); 365 } else if ((features.index == 1) && (dev->revision >= 1)) { 366 /* 367 * If the guest did not negotiate at least revision 1, 368 * we did not offer it any feature bits beyond 31. Such a 369 * guest passing us any bit here is therefore buggy. 370 */ 371 virtio_set_features(vdev, 372 (vdev->guest_features & 0x00000000ffffffffULL) | 373 ((uint64_t)features.features << 32)); 374 } else { 375 /* 376 * If the guest supports more feature bits, assert that it 377 * passes us zeroes for those we don't support. 378 */ 379 if (features.features) { 380 fprintf(stderr, "Guest bug: features[%i]=%x (expected 0)\n", 381 features.index, features.features); 382 /* XXX: do a unit check here? */ 383 } 384 } 385 sch->curr_status.scsw.count = ccw.count - sizeof(features); 386 ret = 0; 387 } 388 break; 389 case CCW_CMD_READ_CONF: 390 if (check_len) { 391 if (ccw.count > vdev->config_len) { 392 ret = -EINVAL; 393 break; 394 } 395 } 396 len = MIN(ccw.count, vdev->config_len); 397 if (!ccw.cda) { 398 ret = -EFAULT; 399 } else { 400 virtio_bus_get_vdev_config(&dev->bus, vdev->config); 401 /* XXX config space endianness */ 402 cpu_physical_memory_write(ccw.cda, vdev->config, len); 403 sch->curr_status.scsw.count = ccw.count - len; 404 ret = 0; 405 } 406 break; 407 case CCW_CMD_WRITE_CONF: 408 if (check_len) { 409 if (ccw.count > vdev->config_len) { 410 ret = -EINVAL; 411 break; 412 } 413 } 414 len = MIN(ccw.count, vdev->config_len); 415 hw_len = len; 416 if (!ccw.cda) { 417 ret = -EFAULT; 418 } else { 419 config = cpu_physical_memory_map(ccw.cda, &hw_len, 0); 420 if (!config) { 421 ret = -EFAULT; 422 } else { 423 len = hw_len; 424 /* XXX config space endianness */ 425 memcpy(vdev->config, config, len); 426 cpu_physical_memory_unmap(config, hw_len, 0, hw_len); 427 virtio_bus_set_vdev_config(&dev->bus, vdev->config); 428 sch->curr_status.scsw.count = ccw.count - len; 429 ret = 0; 430 } 431 } 432 break; 433 case CCW_CMD_READ_STATUS: 434 if (check_len) { 435 if (ccw.count != sizeof(status)) { 436 ret = -EINVAL; 437 break; 438 } 439 } else if (ccw.count < sizeof(status)) { 440 /* Can't execute command. */ 441 ret = -EINVAL; 442 break; 443 } 444 if (!ccw.cda) { 445 ret = -EFAULT; 446 } else { 447 address_space_stb(&address_space_memory, ccw.cda, vdev->status, 448 MEMTXATTRS_UNSPECIFIED, NULL); 449 sch->curr_status.scsw.count = ccw.count - sizeof(vdev->status);; 450 ret = 0; 451 } 452 break; 453 case CCW_CMD_WRITE_STATUS: 454 if (check_len) { 455 if (ccw.count != sizeof(status)) { 456 ret = -EINVAL; 457 break; 458 } 459 } else if (ccw.count < sizeof(status)) { 460 /* Can't execute command. */ 461 ret = -EINVAL; 462 break; 463 } 464 if (!ccw.cda) { 465 ret = -EFAULT; 466 } else { 467 status = address_space_ldub(&address_space_memory, ccw.cda, 468 MEMTXATTRS_UNSPECIFIED, NULL); 469 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) { 470 virtio_ccw_stop_ioeventfd(dev); 471 } 472 if (virtio_set_status(vdev, status) == 0) { 473 if (vdev->status == 0) { 474 virtio_ccw_reset_virtio(dev, vdev); 475 } 476 if (status & VIRTIO_CONFIG_S_DRIVER_OK) { 477 virtio_ccw_start_ioeventfd(dev); 478 } 479 sch->curr_status.scsw.count = ccw.count - sizeof(status); 480 ret = 0; 481 } else { 482 /* Trigger a command reject. */ 483 ret = -ENOSYS; 484 } 485 } 486 break; 487 case CCW_CMD_SET_IND: 488 if (check_len) { 489 if (ccw.count != sizeof(indicators)) { 490 ret = -EINVAL; 491 break; 492 } 493 } else if (ccw.count < sizeof(indicators)) { 494 /* Can't execute command. */ 495 ret = -EINVAL; 496 break; 497 } 498 if (sch->thinint_active) { 499 /* Trigger a command reject. */ 500 ret = -ENOSYS; 501 break; 502 } 503 if (!ccw.cda) { 504 ret = -EFAULT; 505 } else { 506 indicators = address_space_ldq_be(&address_space_memory, ccw.cda, 507 MEMTXATTRS_UNSPECIFIED, NULL); 508 dev->indicators = get_indicator(indicators, sizeof(uint64_t)); 509 sch->curr_status.scsw.count = ccw.count - sizeof(indicators); 510 ret = 0; 511 } 512 break; 513 case CCW_CMD_SET_CONF_IND: 514 if (check_len) { 515 if (ccw.count != sizeof(indicators)) { 516 ret = -EINVAL; 517 break; 518 } 519 } else if (ccw.count < sizeof(indicators)) { 520 /* Can't execute command. */ 521 ret = -EINVAL; 522 break; 523 } 524 if (!ccw.cda) { 525 ret = -EFAULT; 526 } else { 527 indicators = address_space_ldq_be(&address_space_memory, ccw.cda, 528 MEMTXATTRS_UNSPECIFIED, NULL); 529 dev->indicators2 = get_indicator(indicators, sizeof(uint64_t)); 530 sch->curr_status.scsw.count = ccw.count - sizeof(indicators); 531 ret = 0; 532 } 533 break; 534 case CCW_CMD_READ_VQ_CONF: 535 if (check_len) { 536 if (ccw.count != sizeof(vq_config)) { 537 ret = -EINVAL; 538 break; 539 } 540 } else if (ccw.count < sizeof(vq_config)) { 541 /* Can't execute command. */ 542 ret = -EINVAL; 543 break; 544 } 545 if (!ccw.cda) { 546 ret = -EFAULT; 547 } else { 548 vq_config.index = address_space_lduw_be(&address_space_memory, 549 ccw.cda, 550 MEMTXATTRS_UNSPECIFIED, 551 NULL); 552 if (vq_config.index >= VIRTIO_CCW_QUEUE_MAX) { 553 ret = -EINVAL; 554 break; 555 } 556 vq_config.num_max = virtio_queue_get_num(vdev, 557 vq_config.index); 558 address_space_stw_be(&address_space_memory, 559 ccw.cda + sizeof(vq_config.index), 560 vq_config.num_max, 561 MEMTXATTRS_UNSPECIFIED, 562 NULL); 563 sch->curr_status.scsw.count = ccw.count - sizeof(vq_config); 564 ret = 0; 565 } 566 break; 567 case CCW_CMD_SET_IND_ADAPTER: 568 if (check_len) { 569 if (ccw.count != sizeof(*thinint)) { 570 ret = -EINVAL; 571 break; 572 } 573 } else if (ccw.count < sizeof(*thinint)) { 574 /* Can't execute command. */ 575 ret = -EINVAL; 576 break; 577 } 578 len = sizeof(*thinint); 579 hw_len = len; 580 if (!ccw.cda) { 581 ret = -EFAULT; 582 } else if (dev->indicators && !sch->thinint_active) { 583 /* Trigger a command reject. */ 584 ret = -ENOSYS; 585 } else { 586 thinint = cpu_physical_memory_map(ccw.cda, &hw_len, 0); 587 if (!thinint) { 588 ret = -EFAULT; 589 } else { 590 uint64_t ind_bit = ldq_be_p(&thinint->ind_bit); 591 592 len = hw_len; 593 dev->summary_indicator = 594 get_indicator(ldq_be_p(&thinint->summary_indicator), 595 sizeof(uint8_t)); 596 dev->indicators = 597 get_indicator(ldq_be_p(&thinint->device_indicator), 598 ind_bit / 8 + 1); 599 dev->thinint_isc = thinint->isc; 600 dev->routes.adapter.ind_offset = ind_bit; 601 dev->routes.adapter.summary_offset = 7; 602 cpu_physical_memory_unmap(thinint, hw_len, 0, hw_len); 603 ret = css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO, 604 dev->thinint_isc, true, false, 605 &dev->routes.adapter.adapter_id); 606 assert(ret == 0); 607 sch->thinint_active = ((dev->indicators != NULL) && 608 (dev->summary_indicator != NULL)); 609 sch->curr_status.scsw.count = ccw.count - len; 610 ret = 0; 611 } 612 } 613 break; 614 case CCW_CMD_SET_VIRTIO_REV: 615 len = sizeof(revinfo); 616 if (ccw.count < len) { 617 ret = -EINVAL; 618 break; 619 } 620 if (!ccw.cda) { 621 ret = -EFAULT; 622 break; 623 } 624 revinfo.revision = 625 address_space_lduw_be(&address_space_memory, ccw.cda, 626 MEMTXATTRS_UNSPECIFIED, NULL); 627 revinfo.length = 628 address_space_lduw_be(&address_space_memory, 629 ccw.cda + sizeof(revinfo.revision), 630 MEMTXATTRS_UNSPECIFIED, NULL); 631 if (ccw.count < len + revinfo.length || 632 (check_len && ccw.count > len + revinfo.length)) { 633 ret = -EINVAL; 634 break; 635 } 636 /* 637 * Once we start to support revisions with additional data, we'll 638 * need to fetch it here. Nothing to do for now, though. 639 */ 640 if (dev->revision >= 0 || 641 revinfo.revision > virtio_ccw_rev_max(dev)) { 642 ret = -ENOSYS; 643 break; 644 } 645 ret = 0; 646 dev->revision = revinfo.revision; 647 break; 648 default: 649 ret = -ENOSYS; 650 break; 651 } 652 return ret; 653 } 654 655 static void virtio_sch_disable_cb(SubchDev *sch) 656 { 657 VirtioCcwDevice *dev = sch->driver_data; 658 659 dev->revision = -1; 660 } 661 662 static void virtio_ccw_device_realize(VirtioCcwDevice *dev, Error **errp) 663 { 664 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_GET_CLASS(dev); 665 CcwDevice *ccw_dev = CCW_DEVICE(dev); 666 SubchDev *sch = css_create_virtual_sch(ccw_dev->bus_id, errp); 667 Error *err = NULL; 668 669 if (!sch) { 670 return; 671 } 672 673 sch->driver_data = dev; 674 sch->ccw_cb = virtio_ccw_cb; 675 sch->disable_cb = virtio_sch_disable_cb; 676 sch->id.reserved = 0xff; 677 sch->id.cu_type = VIRTIO_CCW_CU_TYPE; 678 ccw_dev->sch = sch; 679 dev->indicators = NULL; 680 dev->revision = -1; 681 css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE); 682 683 trace_virtio_ccw_new_device( 684 sch->cssid, sch->ssid, sch->schid, sch->devno, 685 ccw_dev->bus_id.valid ? "user-configured" : "auto-configured"); 686 687 if (!kvm_eventfds_enabled()) { 688 dev->flags &= ~VIRTIO_CCW_FLAG_USE_IOEVENTFD; 689 } 690 691 if (k->realize) { 692 k->realize(dev, &err); 693 } 694 if (err) { 695 error_propagate(errp, err); 696 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); 697 ccw_dev->sch = NULL; 698 g_free(sch); 699 } 700 } 701 702 static int virtio_ccw_exit(VirtioCcwDevice *dev) 703 { 704 CcwDevice *ccw_dev = CCW_DEVICE(dev); 705 SubchDev *sch = ccw_dev->sch; 706 707 if (sch) { 708 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); 709 g_free(sch); 710 } 711 if (dev->indicators) { 712 release_indicator(&dev->routes.adapter, dev->indicators); 713 dev->indicators = NULL; 714 } 715 return 0; 716 } 717 718 static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp) 719 { 720 DeviceState *qdev = DEVICE(ccw_dev); 721 VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev); 722 DeviceState *vdev = DEVICE(&dev->vdev); 723 724 virtio_net_set_netclient_name(&dev->vdev, qdev->id, 725 object_get_typename(OBJECT(qdev))); 726 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 727 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 728 } 729 730 static void virtio_ccw_net_instance_init(Object *obj) 731 { 732 VirtIONetCcw *dev = VIRTIO_NET_CCW(obj); 733 734 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 735 TYPE_VIRTIO_NET); 736 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 737 "bootindex", &error_abort); 738 } 739 740 static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp) 741 { 742 VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev); 743 DeviceState *vdev = DEVICE(&dev->vdev); 744 745 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 746 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 747 } 748 749 static void virtio_ccw_blk_instance_init(Object *obj) 750 { 751 VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj); 752 753 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 754 TYPE_VIRTIO_BLK); 755 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread", 756 &error_abort); 757 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 758 "bootindex", &error_abort); 759 } 760 761 static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp) 762 { 763 VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev); 764 DeviceState *vdev = DEVICE(&dev->vdev); 765 DeviceState *proxy = DEVICE(ccw_dev); 766 char *bus_name; 767 768 /* 769 * For command line compatibility, this sets the virtio-serial-device bus 770 * name as before. 771 */ 772 if (proxy->id) { 773 bus_name = g_strdup_printf("%s.0", proxy->id); 774 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 775 g_free(bus_name); 776 } 777 778 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 779 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 780 } 781 782 783 static void virtio_ccw_serial_instance_init(Object *obj) 784 { 785 VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj); 786 787 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 788 TYPE_VIRTIO_SERIAL); 789 } 790 791 static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp) 792 { 793 VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev); 794 DeviceState *vdev = DEVICE(&dev->vdev); 795 796 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 797 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 798 } 799 800 static void virtio_ccw_balloon_instance_init(Object *obj) 801 { 802 VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj); 803 804 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 805 TYPE_VIRTIO_BALLOON); 806 object_property_add_alias(obj, "guest-stats", OBJECT(&dev->vdev), 807 "guest-stats", &error_abort); 808 object_property_add_alias(obj, "guest-stats-polling-interval", 809 OBJECT(&dev->vdev), 810 "guest-stats-polling-interval", &error_abort); 811 } 812 813 static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp) 814 { 815 VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev); 816 DeviceState *vdev = DEVICE(&dev->vdev); 817 DeviceState *qdev = DEVICE(ccw_dev); 818 char *bus_name; 819 820 /* 821 * For command line compatibility, this sets the virtio-scsi-device bus 822 * name as before. 823 */ 824 if (qdev->id) { 825 bus_name = g_strdup_printf("%s.0", qdev->id); 826 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 827 g_free(bus_name); 828 } 829 830 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 831 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 832 } 833 834 static void virtio_ccw_scsi_instance_init(Object *obj) 835 { 836 VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj); 837 838 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 839 TYPE_VIRTIO_SCSI); 840 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread", 841 &error_abort); 842 } 843 844 #ifdef CONFIG_VHOST_SCSI 845 static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp) 846 { 847 VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev); 848 DeviceState *vdev = DEVICE(&dev->vdev); 849 850 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 851 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 852 } 853 854 static void vhost_ccw_scsi_instance_init(Object *obj) 855 { 856 VHostSCSICcw *dev = VHOST_SCSI_CCW(obj); 857 858 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 859 TYPE_VHOST_SCSI); 860 } 861 #endif 862 863 static void virtio_ccw_rng_realize(VirtioCcwDevice *ccw_dev, Error **errp) 864 { 865 VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev); 866 DeviceState *vdev = DEVICE(&dev->vdev); 867 Error *err = NULL; 868 869 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 870 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 871 if (err) { 872 error_propagate(errp, err); 873 return; 874 } 875 876 object_property_set_link(OBJECT(dev), 877 OBJECT(dev->vdev.conf.rng), "rng", 878 NULL); 879 } 880 881 /* DeviceState to VirtioCcwDevice. Note: used on datapath, 882 * be careful and test performance if you change this. 883 */ 884 static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d) 885 { 886 CcwDevice *ccw_dev = to_ccw_dev_fast(d); 887 888 return container_of(ccw_dev, VirtioCcwDevice, parent_obj); 889 } 890 891 static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc, 892 uint8_t to_be_set) 893 { 894 uint8_t ind_old, ind_new; 895 hwaddr len = 1; 896 uint8_t *ind_addr; 897 898 ind_addr = cpu_physical_memory_map(ind_loc, &len, 1); 899 if (!ind_addr) { 900 error_report("%s(%x.%x.%04x): unable to access indicator", 901 __func__, sch->cssid, sch->ssid, sch->schid); 902 return -1; 903 } 904 do { 905 ind_old = *ind_addr; 906 ind_new = ind_old | to_be_set; 907 } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old); 908 trace_virtio_ccw_set_ind(ind_loc, ind_old, ind_new); 909 cpu_physical_memory_unmap(ind_addr, len, 1, len); 910 911 return ind_old; 912 } 913 914 static void virtio_ccw_notify(DeviceState *d, uint16_t vector) 915 { 916 VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d); 917 CcwDevice *ccw_dev = to_ccw_dev_fast(d); 918 SubchDev *sch = ccw_dev->sch; 919 uint64_t indicators; 920 921 /* queue indicators + secondary indicators */ 922 if (vector >= VIRTIO_CCW_QUEUE_MAX + 64) { 923 return; 924 } 925 926 if (vector < VIRTIO_CCW_QUEUE_MAX) { 927 if (!dev->indicators) { 928 return; 929 } 930 if (sch->thinint_active) { 931 /* 932 * In the adapter interrupt case, indicators points to a 933 * memory area that may be (way) larger than 64 bit and 934 * ind_bit indicates the start of the indicators in a big 935 * endian notation. 936 */ 937 uint64_t ind_bit = dev->routes.adapter.ind_offset; 938 939 virtio_set_ind_atomic(sch, dev->indicators->addr + 940 (ind_bit + vector) / 8, 941 0x80 >> ((ind_bit + vector) % 8)); 942 if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr, 943 0x01)) { 944 css_adapter_interrupt(dev->thinint_isc); 945 } 946 } else { 947 indicators = address_space_ldq(&address_space_memory, 948 dev->indicators->addr, 949 MEMTXATTRS_UNSPECIFIED, 950 NULL); 951 indicators |= 1ULL << vector; 952 address_space_stq(&address_space_memory, dev->indicators->addr, 953 indicators, MEMTXATTRS_UNSPECIFIED, NULL); 954 css_conditional_io_interrupt(sch); 955 } 956 } else { 957 if (!dev->indicators2) { 958 return; 959 } 960 vector = 0; 961 indicators = address_space_ldq(&address_space_memory, 962 dev->indicators2->addr, 963 MEMTXATTRS_UNSPECIFIED, 964 NULL); 965 indicators |= 1ULL << vector; 966 address_space_stq(&address_space_memory, dev->indicators2->addr, 967 indicators, MEMTXATTRS_UNSPECIFIED, NULL); 968 css_conditional_io_interrupt(sch); 969 } 970 } 971 972 static void virtio_ccw_reset(DeviceState *d) 973 { 974 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 975 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 976 CcwDevice *ccw_dev = CCW_DEVICE(d); 977 978 virtio_ccw_reset_virtio(dev, vdev); 979 css_reset_sch(ccw_dev->sch); 980 } 981 982 static void virtio_ccw_vmstate_change(DeviceState *d, bool running) 983 { 984 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 985 986 if (running) { 987 virtio_ccw_start_ioeventfd(dev); 988 } else { 989 virtio_ccw_stop_ioeventfd(dev); 990 } 991 } 992 993 static bool virtio_ccw_query_guest_notifiers(DeviceState *d) 994 { 995 CcwDevice *dev = CCW_DEVICE(d); 996 997 return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA); 998 } 999 1000 static int virtio_ccw_get_mappings(VirtioCcwDevice *dev) 1001 { 1002 int r; 1003 CcwDevice *ccw_dev = CCW_DEVICE(dev); 1004 1005 if (!ccw_dev->sch->thinint_active) { 1006 return -EINVAL; 1007 } 1008 1009 r = map_indicator(&dev->routes.adapter, dev->summary_indicator); 1010 if (r) { 1011 return r; 1012 } 1013 r = map_indicator(&dev->routes.adapter, dev->indicators); 1014 if (r) { 1015 return r; 1016 } 1017 dev->routes.adapter.summary_addr = dev->summary_indicator->map; 1018 dev->routes.adapter.ind_addr = dev->indicators->map; 1019 1020 return 0; 1021 } 1022 1023 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs) 1024 { 1025 int i; 1026 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1027 int ret; 1028 S390FLICState *fs = s390_get_flic(); 1029 S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs); 1030 1031 ret = virtio_ccw_get_mappings(dev); 1032 if (ret) { 1033 return ret; 1034 } 1035 for (i = 0; i < nvqs; i++) { 1036 if (!virtio_queue_get_num(vdev, i)) { 1037 break; 1038 } 1039 } 1040 dev->routes.num_routes = i; 1041 return fsc->add_adapter_routes(fs, &dev->routes); 1042 } 1043 1044 static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs) 1045 { 1046 S390FLICState *fs = s390_get_flic(); 1047 S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs); 1048 1049 fsc->release_adapter_routes(fs, &dev->routes); 1050 } 1051 1052 static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n) 1053 { 1054 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1055 VirtQueue *vq = virtio_get_queue(vdev, n); 1056 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); 1057 1058 return kvm_irqchip_add_irqfd_notifier_gsi(kvm_state, notifier, NULL, 1059 dev->routes.gsi[n]); 1060 } 1061 1062 static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n) 1063 { 1064 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1065 VirtQueue *vq = virtio_get_queue(vdev, n); 1066 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); 1067 int ret; 1068 1069 ret = kvm_irqchip_remove_irqfd_notifier_gsi(kvm_state, notifier, 1070 dev->routes.gsi[n]); 1071 assert(ret == 0); 1072 } 1073 1074 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n, 1075 bool assign, bool with_irqfd) 1076 { 1077 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1078 VirtQueue *vq = virtio_get_queue(vdev, n); 1079 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); 1080 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 1081 1082 if (assign) { 1083 int r = event_notifier_init(notifier, 0); 1084 1085 if (r < 0) { 1086 return r; 1087 } 1088 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); 1089 if (with_irqfd) { 1090 r = virtio_ccw_add_irqfd(dev, n); 1091 if (r) { 1092 virtio_queue_set_guest_notifier_fd_handler(vq, false, 1093 with_irqfd); 1094 return r; 1095 } 1096 } 1097 /* 1098 * We do not support individual masking for channel devices, so we 1099 * need to manually trigger any guest masking callbacks here. 1100 */ 1101 if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) { 1102 k->guest_notifier_mask(vdev, n, false); 1103 } 1104 /* get lost events and re-inject */ 1105 if (k->guest_notifier_pending && 1106 k->guest_notifier_pending(vdev, n)) { 1107 event_notifier_set(notifier); 1108 } 1109 } else { 1110 if (k->guest_notifier_mask && vdev->use_guest_notifier_mask) { 1111 k->guest_notifier_mask(vdev, n, true); 1112 } 1113 if (with_irqfd) { 1114 virtio_ccw_remove_irqfd(dev, n); 1115 } 1116 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); 1117 event_notifier_cleanup(notifier); 1118 } 1119 return 0; 1120 } 1121 1122 static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs, 1123 bool assigned) 1124 { 1125 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1126 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1127 CcwDevice *ccw_dev = CCW_DEVICE(d); 1128 bool with_irqfd = ccw_dev->sch->thinint_active && kvm_irqfds_enabled(); 1129 int r, n; 1130 1131 if (with_irqfd && assigned) { 1132 /* irq routes need to be set up before assigning irqfds */ 1133 r = virtio_ccw_setup_irqroutes(dev, nvqs); 1134 if (r < 0) { 1135 goto irqroute_error; 1136 } 1137 } 1138 for (n = 0; n < nvqs; n++) { 1139 if (!virtio_queue_get_num(vdev, n)) { 1140 break; 1141 } 1142 r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd); 1143 if (r < 0) { 1144 goto assign_error; 1145 } 1146 } 1147 if (with_irqfd && !assigned) { 1148 /* release irq routes after irqfds have been released */ 1149 virtio_ccw_release_irqroutes(dev, nvqs); 1150 } 1151 return 0; 1152 1153 assign_error: 1154 while (--n >= 0) { 1155 virtio_ccw_set_guest_notifier(dev, n, !assigned, false); 1156 } 1157 irqroute_error: 1158 if (with_irqfd && assigned) { 1159 virtio_ccw_release_irqroutes(dev, nvqs); 1160 } 1161 return r; 1162 } 1163 1164 static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f) 1165 { 1166 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1167 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1168 1169 qemu_put_be16(f, virtio_queue_vector(vdev, n)); 1170 } 1171 1172 static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f) 1173 { 1174 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1175 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1176 uint16_t vector; 1177 1178 qemu_get_be16s(f, &vector); 1179 virtio_queue_set_vector(vdev, n , vector); 1180 1181 return 0; 1182 } 1183 1184 static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f) 1185 { 1186 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1187 CcwDevice *ccw_dev = CCW_DEVICE(d); 1188 SubchDev *s = ccw_dev->sch; 1189 VirtIODevice *vdev = virtio_ccw_get_vdev(s); 1190 1191 subch_device_save(s, f); 1192 if (dev->indicators != NULL) { 1193 qemu_put_be32(f, dev->indicators->len); 1194 qemu_put_be64(f, dev->indicators->addr); 1195 } else { 1196 qemu_put_be32(f, 0); 1197 qemu_put_be64(f, 0UL); 1198 } 1199 if (dev->indicators2 != NULL) { 1200 qemu_put_be32(f, dev->indicators2->len); 1201 qemu_put_be64(f, dev->indicators2->addr); 1202 } else { 1203 qemu_put_be32(f, 0); 1204 qemu_put_be64(f, 0UL); 1205 } 1206 if (dev->summary_indicator != NULL) { 1207 qemu_put_be32(f, dev->summary_indicator->len); 1208 qemu_put_be64(f, dev->summary_indicator->addr); 1209 } else { 1210 qemu_put_be32(f, 0); 1211 qemu_put_be64(f, 0UL); 1212 } 1213 qemu_put_be16(f, vdev->config_vector); 1214 qemu_put_be64(f, dev->routes.adapter.ind_offset); 1215 qemu_put_byte(f, dev->thinint_isc); 1216 qemu_put_be32(f, dev->revision); 1217 } 1218 1219 static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f) 1220 { 1221 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1222 CcwDevice *ccw_dev = CCW_DEVICE(d); 1223 SubchDev *s = ccw_dev->sch; 1224 VirtIODevice *vdev = virtio_ccw_get_vdev(s); 1225 int len; 1226 1227 s->driver_data = dev; 1228 subch_device_load(s, f); 1229 len = qemu_get_be32(f); 1230 if (len != 0) { 1231 dev->indicators = get_indicator(qemu_get_be64(f), len); 1232 } else { 1233 qemu_get_be64(f); 1234 dev->indicators = NULL; 1235 } 1236 len = qemu_get_be32(f); 1237 if (len != 0) { 1238 dev->indicators2 = get_indicator(qemu_get_be64(f), len); 1239 } else { 1240 qemu_get_be64(f); 1241 dev->indicators2 = NULL; 1242 } 1243 len = qemu_get_be32(f); 1244 if (len != 0) { 1245 dev->summary_indicator = get_indicator(qemu_get_be64(f), len); 1246 } else { 1247 qemu_get_be64(f); 1248 dev->summary_indicator = NULL; 1249 } 1250 qemu_get_be16s(f, &vdev->config_vector); 1251 dev->routes.adapter.ind_offset = qemu_get_be64(f); 1252 dev->thinint_isc = qemu_get_byte(f); 1253 dev->revision = qemu_get_be32(f); 1254 if (s->thinint_active) { 1255 return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO, 1256 dev->thinint_isc, true, false, 1257 &dev->routes.adapter.adapter_id); 1258 } 1259 1260 return 0; 1261 } 1262 1263 static void virtio_ccw_pre_plugged(DeviceState *d, Error **errp) 1264 { 1265 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1266 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1267 1268 if (dev->max_rev >= 1) { 1269 virtio_add_feature(&vdev->host_features, VIRTIO_F_VERSION_1); 1270 } 1271 } 1272 1273 /* This is called by virtio-bus just after the device is plugged. */ 1274 static void virtio_ccw_device_plugged(DeviceState *d, Error **errp) 1275 { 1276 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1277 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1278 CcwDevice *ccw_dev = CCW_DEVICE(d); 1279 SubchDev *sch = ccw_dev->sch; 1280 int n = virtio_get_num_queues(vdev); 1281 1282 if (!virtio_has_feature(vdev->host_features, VIRTIO_F_VERSION_1)) { 1283 dev->max_rev = 0; 1284 } 1285 1286 if (virtio_get_num_queues(vdev) > VIRTIO_CCW_QUEUE_MAX) { 1287 error_setg(errp, "The number of virtqueues %d " 1288 "exceeds ccw limit %d", n, 1289 VIRTIO_CCW_QUEUE_MAX); 1290 return; 1291 } 1292 1293 sch->id.cu_model = virtio_bus_get_vdev_id(&dev->bus); 1294 1295 1296 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1297 d->hotplugged, 1); 1298 } 1299 1300 static void virtio_ccw_device_unplugged(DeviceState *d) 1301 { 1302 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1303 1304 virtio_ccw_stop_ioeventfd(dev); 1305 } 1306 /**************** Virtio-ccw Bus Device Descriptions *******************/ 1307 1308 static Property virtio_ccw_net_properties[] = { 1309 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id), 1310 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1311 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1312 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev, 1313 VIRTIO_CCW_MAX_REV), 1314 DEFINE_PROP_END_OF_LIST(), 1315 }; 1316 1317 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data) 1318 { 1319 DeviceClass *dc = DEVICE_CLASS(klass); 1320 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1321 1322 k->realize = virtio_ccw_net_realize; 1323 k->exit = virtio_ccw_exit; 1324 dc->reset = virtio_ccw_reset; 1325 dc->props = virtio_ccw_net_properties; 1326 set_bit(DEVICE_CATEGORY_NETWORK, dc->categories); 1327 } 1328 1329 static const TypeInfo virtio_ccw_net = { 1330 .name = TYPE_VIRTIO_NET_CCW, 1331 .parent = TYPE_VIRTIO_CCW_DEVICE, 1332 .instance_size = sizeof(VirtIONetCcw), 1333 .instance_init = virtio_ccw_net_instance_init, 1334 .class_init = virtio_ccw_net_class_init, 1335 }; 1336 1337 static Property virtio_ccw_blk_properties[] = { 1338 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id), 1339 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1340 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1341 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev, 1342 VIRTIO_CCW_MAX_REV), 1343 DEFINE_PROP_END_OF_LIST(), 1344 }; 1345 1346 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data) 1347 { 1348 DeviceClass *dc = DEVICE_CLASS(klass); 1349 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1350 1351 k->realize = virtio_ccw_blk_realize; 1352 k->exit = virtio_ccw_exit; 1353 dc->reset = virtio_ccw_reset; 1354 dc->props = virtio_ccw_blk_properties; 1355 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1356 } 1357 1358 static const TypeInfo virtio_ccw_blk = { 1359 .name = TYPE_VIRTIO_BLK_CCW, 1360 .parent = TYPE_VIRTIO_CCW_DEVICE, 1361 .instance_size = sizeof(VirtIOBlkCcw), 1362 .instance_init = virtio_ccw_blk_instance_init, 1363 .class_init = virtio_ccw_blk_class_init, 1364 }; 1365 1366 static Property virtio_ccw_serial_properties[] = { 1367 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id), 1368 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1369 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1370 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev, 1371 VIRTIO_CCW_MAX_REV), 1372 DEFINE_PROP_END_OF_LIST(), 1373 }; 1374 1375 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data) 1376 { 1377 DeviceClass *dc = DEVICE_CLASS(klass); 1378 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1379 1380 k->realize = virtio_ccw_serial_realize; 1381 k->exit = virtio_ccw_exit; 1382 dc->reset = virtio_ccw_reset; 1383 dc->props = virtio_ccw_serial_properties; 1384 set_bit(DEVICE_CATEGORY_INPUT, dc->categories); 1385 } 1386 1387 static const TypeInfo virtio_ccw_serial = { 1388 .name = TYPE_VIRTIO_SERIAL_CCW, 1389 .parent = TYPE_VIRTIO_CCW_DEVICE, 1390 .instance_size = sizeof(VirtioSerialCcw), 1391 .instance_init = virtio_ccw_serial_instance_init, 1392 .class_init = virtio_ccw_serial_class_init, 1393 }; 1394 1395 static Property virtio_ccw_balloon_properties[] = { 1396 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id), 1397 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1398 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1399 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev, 1400 VIRTIO_CCW_MAX_REV), 1401 DEFINE_PROP_END_OF_LIST(), 1402 }; 1403 1404 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data) 1405 { 1406 DeviceClass *dc = DEVICE_CLASS(klass); 1407 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1408 1409 k->realize = virtio_ccw_balloon_realize; 1410 k->exit = virtio_ccw_exit; 1411 dc->reset = virtio_ccw_reset; 1412 dc->props = virtio_ccw_balloon_properties; 1413 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1414 } 1415 1416 static const TypeInfo virtio_ccw_balloon = { 1417 .name = TYPE_VIRTIO_BALLOON_CCW, 1418 .parent = TYPE_VIRTIO_CCW_DEVICE, 1419 .instance_size = sizeof(VirtIOBalloonCcw), 1420 .instance_init = virtio_ccw_balloon_instance_init, 1421 .class_init = virtio_ccw_balloon_class_init, 1422 }; 1423 1424 static Property virtio_ccw_scsi_properties[] = { 1425 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id), 1426 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1427 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1428 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev, 1429 VIRTIO_CCW_MAX_REV), 1430 DEFINE_PROP_END_OF_LIST(), 1431 }; 1432 1433 static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data) 1434 { 1435 DeviceClass *dc = DEVICE_CLASS(klass); 1436 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1437 1438 k->realize = virtio_ccw_scsi_realize; 1439 k->exit = virtio_ccw_exit; 1440 dc->reset = virtio_ccw_reset; 1441 dc->props = virtio_ccw_scsi_properties; 1442 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1443 } 1444 1445 static const TypeInfo virtio_ccw_scsi = { 1446 .name = TYPE_VIRTIO_SCSI_CCW, 1447 .parent = TYPE_VIRTIO_CCW_DEVICE, 1448 .instance_size = sizeof(VirtIOSCSICcw), 1449 .instance_init = virtio_ccw_scsi_instance_init, 1450 .class_init = virtio_ccw_scsi_class_init, 1451 }; 1452 1453 #ifdef CONFIG_VHOST_SCSI 1454 static Property vhost_ccw_scsi_properties[] = { 1455 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id), 1456 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev, 1457 VIRTIO_CCW_MAX_REV), 1458 DEFINE_PROP_END_OF_LIST(), 1459 }; 1460 1461 static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data) 1462 { 1463 DeviceClass *dc = DEVICE_CLASS(klass); 1464 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1465 1466 k->realize = vhost_ccw_scsi_realize; 1467 k->exit = virtio_ccw_exit; 1468 dc->reset = virtio_ccw_reset; 1469 dc->props = vhost_ccw_scsi_properties; 1470 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1471 } 1472 1473 static const TypeInfo vhost_ccw_scsi = { 1474 .name = TYPE_VHOST_SCSI_CCW, 1475 .parent = TYPE_VIRTIO_CCW_DEVICE, 1476 .instance_size = sizeof(VHostSCSICcw), 1477 .instance_init = vhost_ccw_scsi_instance_init, 1478 .class_init = vhost_ccw_scsi_class_init, 1479 }; 1480 #endif 1481 1482 static void virtio_ccw_rng_instance_init(Object *obj) 1483 { 1484 VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj); 1485 1486 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1487 TYPE_VIRTIO_RNG); 1488 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), 1489 "rng", &error_abort); 1490 } 1491 1492 static Property virtio_ccw_rng_properties[] = { 1493 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id), 1494 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1495 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1496 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev, 1497 VIRTIO_CCW_MAX_REV), 1498 DEFINE_PROP_END_OF_LIST(), 1499 }; 1500 1501 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data) 1502 { 1503 DeviceClass *dc = DEVICE_CLASS(klass); 1504 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1505 1506 k->realize = virtio_ccw_rng_realize; 1507 k->exit = virtio_ccw_exit; 1508 dc->reset = virtio_ccw_reset; 1509 dc->props = virtio_ccw_rng_properties; 1510 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1511 } 1512 1513 static const TypeInfo virtio_ccw_rng = { 1514 .name = TYPE_VIRTIO_RNG_CCW, 1515 .parent = TYPE_VIRTIO_CCW_DEVICE, 1516 .instance_size = sizeof(VirtIORNGCcw), 1517 .instance_init = virtio_ccw_rng_instance_init, 1518 .class_init = virtio_ccw_rng_class_init, 1519 }; 1520 1521 static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp) 1522 { 1523 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; 1524 1525 virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev); 1526 virtio_ccw_device_realize(_dev, errp); 1527 } 1528 1529 static int virtio_ccw_busdev_exit(DeviceState *dev) 1530 { 1531 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; 1532 VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev); 1533 1534 return _info->exit(_dev); 1535 } 1536 1537 static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev, 1538 DeviceState *dev, Error **errp) 1539 { 1540 VirtioCcwDevice *_dev = to_virtio_ccw_dev_fast(dev); 1541 1542 virtio_ccw_stop_ioeventfd(_dev); 1543 } 1544 1545 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data) 1546 { 1547 DeviceClass *dc = DEVICE_CLASS(klass); 1548 CCWDeviceClass *k = CCW_DEVICE_CLASS(dc); 1549 1550 k->unplug = virtio_ccw_busdev_unplug; 1551 dc->realize = virtio_ccw_busdev_realize; 1552 dc->exit = virtio_ccw_busdev_exit; 1553 dc->bus_type = TYPE_VIRTUAL_CSS_BUS; 1554 } 1555 1556 static const TypeInfo virtio_ccw_device_info = { 1557 .name = TYPE_VIRTIO_CCW_DEVICE, 1558 .parent = TYPE_CCW_DEVICE, 1559 .instance_size = sizeof(VirtioCcwDevice), 1560 .class_init = virtio_ccw_device_class_init, 1561 .class_size = sizeof(VirtIOCCWDeviceClass), 1562 .abstract = true, 1563 }; 1564 1565 /* virtio-ccw-bus */ 1566 1567 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size, 1568 VirtioCcwDevice *dev) 1569 { 1570 DeviceState *qdev = DEVICE(dev); 1571 char virtio_bus_name[] = "virtio-bus"; 1572 1573 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS, 1574 qdev, virtio_bus_name); 1575 } 1576 1577 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data) 1578 { 1579 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); 1580 BusClass *bus_class = BUS_CLASS(klass); 1581 1582 bus_class->max_dev = 1; 1583 k->notify = virtio_ccw_notify; 1584 k->vmstate_change = virtio_ccw_vmstate_change; 1585 k->query_guest_notifiers = virtio_ccw_query_guest_notifiers; 1586 k->set_guest_notifiers = virtio_ccw_set_guest_notifiers; 1587 k->save_queue = virtio_ccw_save_queue; 1588 k->load_queue = virtio_ccw_load_queue; 1589 k->save_config = virtio_ccw_save_config; 1590 k->load_config = virtio_ccw_load_config; 1591 k->pre_plugged = virtio_ccw_pre_plugged; 1592 k->device_plugged = virtio_ccw_device_plugged; 1593 k->device_unplugged = virtio_ccw_device_unplugged; 1594 k->ioeventfd_enabled = virtio_ccw_ioeventfd_enabled; 1595 k->ioeventfd_assign = virtio_ccw_ioeventfd_assign; 1596 } 1597 1598 static const TypeInfo virtio_ccw_bus_info = { 1599 .name = TYPE_VIRTIO_CCW_BUS, 1600 .parent = TYPE_VIRTIO_BUS, 1601 .instance_size = sizeof(VirtioCcwBusState), 1602 .class_init = virtio_ccw_bus_class_init, 1603 }; 1604 1605 #ifdef CONFIG_VIRTFS 1606 static Property virtio_ccw_9p_properties[] = { 1607 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id), 1608 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1609 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1610 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev, 1611 VIRTIO_CCW_MAX_REV), 1612 DEFINE_PROP_END_OF_LIST(), 1613 }; 1614 1615 static void virtio_ccw_9p_realize(VirtioCcwDevice *ccw_dev, Error **errp) 1616 { 1617 V9fsCCWState *dev = VIRTIO_9P_CCW(ccw_dev); 1618 DeviceState *vdev = DEVICE(&dev->vdev); 1619 1620 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 1621 object_property_set_bool(OBJECT(vdev), true, "realized", errp); 1622 } 1623 1624 static void virtio_ccw_9p_class_init(ObjectClass *klass, void *data) 1625 { 1626 DeviceClass *dc = DEVICE_CLASS(klass); 1627 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1628 1629 k->exit = virtio_ccw_exit; 1630 k->realize = virtio_ccw_9p_realize; 1631 dc->reset = virtio_ccw_reset; 1632 dc->props = virtio_ccw_9p_properties; 1633 set_bit(DEVICE_CATEGORY_STORAGE, dc->categories); 1634 } 1635 1636 static void virtio_ccw_9p_instance_init(Object *obj) 1637 { 1638 V9fsCCWState *dev = VIRTIO_9P_CCW(obj); 1639 1640 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1641 TYPE_VIRTIO_9P); 1642 } 1643 1644 static const TypeInfo virtio_ccw_9p_info = { 1645 .name = TYPE_VIRTIO_9P_CCW, 1646 .parent = TYPE_VIRTIO_CCW_DEVICE, 1647 .instance_size = sizeof(V9fsCCWState), 1648 .instance_init = virtio_ccw_9p_instance_init, 1649 .class_init = virtio_ccw_9p_class_init, 1650 }; 1651 #endif 1652 1653 #ifdef CONFIG_VHOST_VSOCK 1654 1655 static Property vhost_vsock_ccw_properties[] = { 1656 DEFINE_PROP_CSS_DEV_ID("devno", VirtioCcwDevice, parent_obj.bus_id), 1657 DEFINE_PROP_UINT32("max_revision", VirtioCcwDevice, max_rev, 1658 VIRTIO_CCW_MAX_REV), 1659 DEFINE_PROP_END_OF_LIST(), 1660 }; 1661 1662 static void vhost_vsock_ccw_realize(VirtioCcwDevice *ccw_dev, Error **errp) 1663 { 1664 VHostVSockCCWState *dev = VHOST_VSOCK_CCW(ccw_dev); 1665 DeviceState *vdev = DEVICE(&dev->vdev); 1666 Error *err = NULL; 1667 1668 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 1669 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 1670 if (err) { 1671 error_propagate(errp, err); 1672 } 1673 } 1674 1675 static void vhost_vsock_ccw_class_init(ObjectClass *klass, void *data) 1676 { 1677 DeviceClass *dc = DEVICE_CLASS(klass); 1678 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1679 1680 k->realize = vhost_vsock_ccw_realize; 1681 k->exit = virtio_ccw_exit; 1682 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 1683 dc->props = vhost_vsock_ccw_properties; 1684 dc->reset = virtio_ccw_reset; 1685 } 1686 1687 static void vhost_vsock_ccw_instance_init(Object *obj) 1688 { 1689 VHostVSockCCWState *dev = VHOST_VSOCK_CCW(obj); 1690 1691 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1692 TYPE_VHOST_VSOCK); 1693 } 1694 1695 static const TypeInfo vhost_vsock_ccw_info = { 1696 .name = TYPE_VHOST_VSOCK_CCW, 1697 .parent = TYPE_VIRTIO_CCW_DEVICE, 1698 .instance_size = sizeof(VHostVSockCCWState), 1699 .instance_init = vhost_vsock_ccw_instance_init, 1700 .class_init = vhost_vsock_ccw_class_init, 1701 }; 1702 #endif 1703 1704 static void virtio_ccw_register(void) 1705 { 1706 type_register_static(&virtio_ccw_bus_info); 1707 type_register_static(&virtio_ccw_device_info); 1708 type_register_static(&virtio_ccw_serial); 1709 type_register_static(&virtio_ccw_blk); 1710 type_register_static(&virtio_ccw_net); 1711 type_register_static(&virtio_ccw_balloon); 1712 type_register_static(&virtio_ccw_scsi); 1713 #ifdef CONFIG_VHOST_SCSI 1714 type_register_static(&vhost_ccw_scsi); 1715 #endif 1716 type_register_static(&virtio_ccw_rng); 1717 #ifdef CONFIG_VIRTFS 1718 type_register_static(&virtio_ccw_9p_info); 1719 #endif 1720 #ifdef CONFIG_VHOST_VSOCK 1721 type_register_static(&vhost_vsock_ccw_info); 1722 #endif 1723 } 1724 1725 type_init(virtio_ccw_register) 1726