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