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