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