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 = ldq_phys(&address_space_memory, ccw.cda); 339 info.align = ldl_phys(&address_space_memory, 340 ccw.cda + sizeof(info.queue)); 341 info.index = lduw_phys(&address_space_memory, 342 ccw.cda + sizeof(info.queue) 343 + sizeof(info.align)); 344 info.num = lduw_phys(&address_space_memory, 345 ccw.cda + sizeof(info.queue) 346 + sizeof(info.align) 347 + sizeof(info.index)); 348 ret = virtio_ccw_set_vqs(sch, info.queue, info.align, info.index, 349 info.num); 350 sch->curr_status.scsw.count = 0; 351 } 352 break; 353 case CCW_CMD_VDEV_RESET: 354 virtio_ccw_stop_ioeventfd(dev); 355 virtio_reset(vdev); 356 ret = 0; 357 break; 358 case CCW_CMD_READ_FEAT: 359 if (check_len) { 360 if (ccw.count != sizeof(features)) { 361 ret = -EINVAL; 362 break; 363 } 364 } else if (ccw.count < sizeof(features)) { 365 /* Can't execute command. */ 366 ret = -EINVAL; 367 break; 368 } 369 if (!ccw.cda) { 370 ret = -EFAULT; 371 } else { 372 features.index = ldub_phys(&address_space_memory, 373 ccw.cda + sizeof(features.features)); 374 if (features.index < ARRAY_SIZE(dev->host_features)) { 375 features.features = dev->host_features[features.index]; 376 } else { 377 /* Return zeroes if the guest supports more feature bits. */ 378 features.features = 0; 379 } 380 stl_le_phys(&address_space_memory, ccw.cda, features.features); 381 sch->curr_status.scsw.count = ccw.count - sizeof(features); 382 ret = 0; 383 } 384 break; 385 case CCW_CMD_WRITE_FEAT: 386 if (check_len) { 387 if (ccw.count != sizeof(features)) { 388 ret = -EINVAL; 389 break; 390 } 391 } else if (ccw.count < sizeof(features)) { 392 /* Can't execute command. */ 393 ret = -EINVAL; 394 break; 395 } 396 if (!ccw.cda) { 397 ret = -EFAULT; 398 } else { 399 features.index = ldub_phys(&address_space_memory, 400 ccw.cda + sizeof(features.features)); 401 features.features = ldl_le_phys(&address_space_memory, ccw.cda); 402 if (features.index < ARRAY_SIZE(dev->host_features)) { 403 virtio_set_features(vdev, features.features); 404 } else { 405 /* 406 * If the guest supports more feature bits, assert that it 407 * passes us zeroes for those we don't support. 408 */ 409 if (features.features) { 410 fprintf(stderr, "Guest bug: features[%i]=%x (expected 0)\n", 411 features.index, features.features); 412 /* XXX: do a unit check here? */ 413 } 414 } 415 sch->curr_status.scsw.count = ccw.count - sizeof(features); 416 ret = 0; 417 } 418 break; 419 case CCW_CMD_READ_CONF: 420 if (check_len) { 421 if (ccw.count > vdev->config_len) { 422 ret = -EINVAL; 423 break; 424 } 425 } 426 len = MIN(ccw.count, vdev->config_len); 427 if (!ccw.cda) { 428 ret = -EFAULT; 429 } else { 430 virtio_bus_get_vdev_config(&dev->bus, vdev->config); 431 /* XXX config space endianness */ 432 cpu_physical_memory_write(ccw.cda, vdev->config, len); 433 sch->curr_status.scsw.count = ccw.count - len; 434 ret = 0; 435 } 436 break; 437 case CCW_CMD_WRITE_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 hw_len = len; 446 if (!ccw.cda) { 447 ret = -EFAULT; 448 } else { 449 config = cpu_physical_memory_map(ccw.cda, &hw_len, 0); 450 if (!config) { 451 ret = -EFAULT; 452 } else { 453 len = hw_len; 454 /* XXX config space endianness */ 455 memcpy(vdev->config, config, len); 456 cpu_physical_memory_unmap(config, hw_len, 0, hw_len); 457 virtio_bus_set_vdev_config(&dev->bus, vdev->config); 458 sch->curr_status.scsw.count = ccw.count - len; 459 ret = 0; 460 } 461 } 462 break; 463 case CCW_CMD_WRITE_STATUS: 464 if (check_len) { 465 if (ccw.count != sizeof(status)) { 466 ret = -EINVAL; 467 break; 468 } 469 } else if (ccw.count < sizeof(status)) { 470 /* Can't execute command. */ 471 ret = -EINVAL; 472 break; 473 } 474 if (!ccw.cda) { 475 ret = -EFAULT; 476 } else { 477 status = ldub_phys(&address_space_memory, ccw.cda); 478 if (!(status & VIRTIO_CONFIG_S_DRIVER_OK)) { 479 virtio_ccw_stop_ioeventfd(dev); 480 } 481 virtio_set_status(vdev, status); 482 if (vdev->status == 0) { 483 virtio_reset(vdev); 484 } 485 if (status & VIRTIO_CONFIG_S_DRIVER_OK) { 486 virtio_ccw_start_ioeventfd(dev); 487 } 488 sch->curr_status.scsw.count = ccw.count - sizeof(status); 489 ret = 0; 490 } 491 break; 492 case CCW_CMD_SET_IND: 493 if (check_len) { 494 if (ccw.count != sizeof(indicators)) { 495 ret = -EINVAL; 496 break; 497 } 498 } else if (ccw.count < sizeof(indicators)) { 499 /* Can't execute command. */ 500 ret = -EINVAL; 501 break; 502 } 503 if (sch->thinint_active) { 504 /* Trigger a command reject. */ 505 ret = -ENOSYS; 506 break; 507 } 508 if (!ccw.cda) { 509 ret = -EFAULT; 510 } else { 511 indicators = ldq_phys(&address_space_memory, ccw.cda); 512 dev->indicators = get_indicator(indicators, sizeof(uint64_t)); 513 sch->curr_status.scsw.count = ccw.count - sizeof(indicators); 514 ret = 0; 515 } 516 break; 517 case CCW_CMD_SET_CONF_IND: 518 if (check_len) { 519 if (ccw.count != sizeof(indicators)) { 520 ret = -EINVAL; 521 break; 522 } 523 } else if (ccw.count < sizeof(indicators)) { 524 /* Can't execute command. */ 525 ret = -EINVAL; 526 break; 527 } 528 if (!ccw.cda) { 529 ret = -EFAULT; 530 } else { 531 indicators = ldq_phys(&address_space_memory, ccw.cda); 532 dev->indicators2 = 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_READ_VQ_CONF: 538 if (check_len) { 539 if (ccw.count != sizeof(vq_config)) { 540 ret = -EINVAL; 541 break; 542 } 543 } else if (ccw.count < sizeof(vq_config)) { 544 /* Can't execute command. */ 545 ret = -EINVAL; 546 break; 547 } 548 if (!ccw.cda) { 549 ret = -EFAULT; 550 } else { 551 vq_config.index = lduw_phys(&address_space_memory, ccw.cda); 552 vq_config.num_max = virtio_queue_get_num(vdev, 553 vq_config.index); 554 stw_phys(&address_space_memory, 555 ccw.cda + sizeof(vq_config.index), vq_config.num_max); 556 sch->curr_status.scsw.count = ccw.count - sizeof(vq_config); 557 ret = 0; 558 } 559 break; 560 case CCW_CMD_SET_IND_ADAPTER: 561 if (check_len) { 562 if (ccw.count != sizeof(*thinint)) { 563 ret = -EINVAL; 564 break; 565 } 566 } else if (ccw.count < sizeof(*thinint)) { 567 /* Can't execute command. */ 568 ret = -EINVAL; 569 break; 570 } 571 len = sizeof(*thinint); 572 hw_len = len; 573 if (!ccw.cda) { 574 ret = -EFAULT; 575 } else if (dev->indicators && !sch->thinint_active) { 576 /* Trigger a command reject. */ 577 ret = -ENOSYS; 578 } else { 579 thinint = cpu_physical_memory_map(ccw.cda, &hw_len, 0); 580 if (!thinint) { 581 ret = -EFAULT; 582 } else { 583 len = hw_len; 584 dev->summary_indicator = 585 get_indicator(thinint->summary_indicator, sizeof(uint8_t)); 586 dev->indicators = get_indicator(thinint->device_indicator, 587 thinint->ind_bit / 8 + 1); 588 dev->thinint_isc = thinint->isc; 589 dev->routes.adapter.ind_offset = thinint->ind_bit; 590 dev->routes.adapter.summary_offset = 7; 591 cpu_physical_memory_unmap(thinint, hw_len, 0, hw_len); 592 ret = css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO, 593 dev->thinint_isc, true, false, 594 &dev->routes.adapter.adapter_id); 595 assert(ret == 0); 596 sch->thinint_active = ((dev->indicators != NULL) && 597 (dev->summary_indicator != NULL)); 598 sch->curr_status.scsw.count = ccw.count - len; 599 ret = 0; 600 } 601 } 602 break; 603 default: 604 ret = -ENOSYS; 605 break; 606 } 607 return ret; 608 } 609 610 static void virtio_ccw_device_realize(VirtioCcwDevice *dev, 611 VirtIODevice *vdev, Error **errp) 612 { 613 unsigned int cssid = 0; 614 unsigned int ssid = 0; 615 unsigned int schid; 616 unsigned int devno; 617 bool have_devno = false; 618 bool found = false; 619 SubchDev *sch; 620 int num; 621 DeviceState *parent = DEVICE(dev); 622 623 sch = g_malloc0(sizeof(SubchDev)); 624 625 sch->driver_data = dev; 626 dev->sch = sch; 627 628 dev->indicators = NULL; 629 630 /* Initialize subchannel structure. */ 631 sch->channel_prog = 0x0; 632 sch->last_cmd_valid = false; 633 sch->thinint_active = false; 634 /* 635 * Use a device number if provided. Otherwise, fall back to subchannel 636 * number. 637 */ 638 if (dev->bus_id) { 639 num = sscanf(dev->bus_id, "%x.%x.%04x", &cssid, &ssid, &devno); 640 if (num == 3) { 641 if ((cssid > MAX_CSSID) || (ssid > MAX_SSID)) { 642 error_setg(errp, "Invalid cssid or ssid: cssid %x, ssid %x", 643 cssid, ssid); 644 goto out_err; 645 } 646 /* Enforce use of virtual cssid. */ 647 if (cssid != VIRTUAL_CSSID) { 648 error_setg(errp, "cssid %x not valid for virtio devices", 649 cssid); 650 goto out_err; 651 } 652 if (css_devno_used(cssid, ssid, devno)) { 653 error_setg(errp, "Device %x.%x.%04x already exists", 654 cssid, ssid, devno); 655 goto out_err; 656 } 657 sch->cssid = cssid; 658 sch->ssid = ssid; 659 sch->devno = devno; 660 have_devno = true; 661 } else { 662 error_setg(errp, "Malformed devno parameter '%s'", dev->bus_id); 663 goto out_err; 664 } 665 } 666 667 /* Find the next free id. */ 668 if (have_devno) { 669 for (schid = 0; schid <= MAX_SCHID; schid++) { 670 if (!css_find_subch(1, cssid, ssid, schid)) { 671 sch->schid = schid; 672 css_subch_assign(cssid, ssid, schid, devno, sch); 673 found = true; 674 break; 675 } 676 } 677 if (!found) { 678 error_setg(errp, "No free subchannel found for %x.%x.%04x", 679 cssid, ssid, devno); 680 goto out_err; 681 } 682 trace_virtio_ccw_new_device(cssid, ssid, schid, devno, 683 "user-configured"); 684 } else { 685 cssid = VIRTUAL_CSSID; 686 for (ssid = 0; ssid <= MAX_SSID; ssid++) { 687 for (schid = 0; schid <= MAX_SCHID; schid++) { 688 if (!css_find_subch(1, cssid, ssid, schid)) { 689 sch->cssid = cssid; 690 sch->ssid = ssid; 691 sch->schid = schid; 692 devno = schid; 693 /* 694 * If the devno is already taken, look further in this 695 * subchannel set. 696 */ 697 while (css_devno_used(cssid, ssid, devno)) { 698 if (devno == MAX_SCHID) { 699 devno = 0; 700 } else if (devno == schid - 1) { 701 error_setg(errp, "No free devno found"); 702 goto out_err; 703 } else { 704 devno++; 705 } 706 } 707 sch->devno = devno; 708 css_subch_assign(cssid, ssid, schid, devno, sch); 709 found = true; 710 break; 711 } 712 } 713 if (found) { 714 break; 715 } 716 } 717 if (!found) { 718 error_setg(errp, "Virtual channel subsystem is full!"); 719 goto out_err; 720 } 721 trace_virtio_ccw_new_device(cssid, ssid, schid, devno, 722 "auto-configured"); 723 } 724 725 /* Build initial schib. */ 726 css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE); 727 728 sch->ccw_cb = virtio_ccw_cb; 729 730 /* Build senseid data. */ 731 memset(&sch->id, 0, sizeof(SenseId)); 732 sch->id.reserved = 0xff; 733 sch->id.cu_type = VIRTIO_CCW_CU_TYPE; 734 sch->id.cu_model = vdev->device_id; 735 736 /* Only the first 32 feature bits are used. */ 737 dev->host_features[0] = virtio_bus_get_vdev_features(&dev->bus, 738 dev->host_features[0]); 739 740 virtio_add_feature(&dev->host_features[0], VIRTIO_F_NOTIFY_ON_EMPTY); 741 virtio_add_feature(&dev->host_features[0], VIRTIO_F_BAD_FEATURE); 742 743 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 744 parent->hotplugged, 1); 745 return; 746 747 out_err: 748 dev->sch = NULL; 749 g_free(sch); 750 } 751 752 static int virtio_ccw_exit(VirtioCcwDevice *dev) 753 { 754 SubchDev *sch = dev->sch; 755 756 if (sch) { 757 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); 758 g_free(sch); 759 } 760 if (dev->indicators) { 761 release_indicator(&dev->routes.adapter, dev->indicators); 762 dev->indicators = NULL; 763 } 764 return 0; 765 } 766 767 static void virtio_ccw_net_realize(VirtioCcwDevice *ccw_dev, Error **errp) 768 { 769 DeviceState *qdev = DEVICE(ccw_dev); 770 VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev); 771 DeviceState *vdev = DEVICE(&dev->vdev); 772 Error *err = NULL; 773 774 virtio_net_set_config_size(&dev->vdev, ccw_dev->host_features[0]); 775 virtio_net_set_netclient_name(&dev->vdev, qdev->id, 776 object_get_typename(OBJECT(qdev))); 777 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 778 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 779 if (err) { 780 error_propagate(errp, err); 781 return; 782 } 783 784 virtio_ccw_device_realize(ccw_dev, VIRTIO_DEVICE(vdev), errp); 785 } 786 787 static void virtio_ccw_net_instance_init(Object *obj) 788 { 789 VirtIONetCcw *dev = VIRTIO_NET_CCW(obj); 790 791 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 792 TYPE_VIRTIO_NET); 793 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 794 "bootindex", &error_abort); 795 } 796 797 static void virtio_ccw_blk_realize(VirtioCcwDevice *ccw_dev, Error **errp) 798 { 799 VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev); 800 DeviceState *vdev = DEVICE(&dev->vdev); 801 Error *err = NULL; 802 803 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 804 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 805 if (err) { 806 error_propagate(errp, err); 807 return; 808 } 809 810 virtio_ccw_device_realize(ccw_dev, VIRTIO_DEVICE(vdev), errp); 811 } 812 813 static void virtio_ccw_blk_instance_init(Object *obj) 814 { 815 VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj); 816 817 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 818 TYPE_VIRTIO_BLK); 819 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev),"iothread", 820 &error_abort); 821 object_property_add_alias(obj, "bootindex", OBJECT(&dev->vdev), 822 "bootindex", &error_abort); 823 } 824 825 static void virtio_ccw_serial_realize(VirtioCcwDevice *ccw_dev, Error **errp) 826 { 827 VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev); 828 DeviceState *vdev = DEVICE(&dev->vdev); 829 DeviceState *proxy = DEVICE(ccw_dev); 830 Error *err = NULL; 831 char *bus_name; 832 833 /* 834 * For command line compatibility, this sets the virtio-serial-device bus 835 * name as before. 836 */ 837 if (proxy->id) { 838 bus_name = g_strdup_printf("%s.0", proxy->id); 839 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 840 g_free(bus_name); 841 } 842 843 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 844 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 845 if (err) { 846 error_propagate(errp, err); 847 return; 848 } 849 850 virtio_ccw_device_realize(ccw_dev, VIRTIO_DEVICE(vdev), errp); 851 } 852 853 854 static void virtio_ccw_serial_instance_init(Object *obj) 855 { 856 VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj); 857 858 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 859 TYPE_VIRTIO_SERIAL); 860 } 861 862 static void virtio_ccw_balloon_realize(VirtioCcwDevice *ccw_dev, Error **errp) 863 { 864 VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev); 865 DeviceState *vdev = DEVICE(&dev->vdev); 866 Error *err = NULL; 867 868 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 869 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 870 if (err) { 871 error_propagate(errp, err); 872 return; 873 } 874 875 virtio_ccw_device_realize(ccw_dev, VIRTIO_DEVICE(vdev), errp); 876 } 877 878 static void balloon_ccw_stats_get_all(Object *obj, struct Visitor *v, 879 void *opaque, const char *name, 880 Error **errp) 881 { 882 VirtIOBalloonCcw *dev = opaque; 883 object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp); 884 } 885 886 static void balloon_ccw_stats_get_poll_interval(Object *obj, struct Visitor *v, 887 void *opaque, const char *name, 888 Error **errp) 889 { 890 VirtIOBalloonCcw *dev = opaque; 891 object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", 892 errp); 893 } 894 895 static void balloon_ccw_stats_set_poll_interval(Object *obj, struct Visitor *v, 896 void *opaque, const char *name, 897 Error **errp) 898 { 899 VirtIOBalloonCcw *dev = opaque; 900 object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", 901 errp); 902 } 903 904 static void virtio_ccw_balloon_instance_init(Object *obj) 905 { 906 VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj); 907 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 908 TYPE_VIRTIO_BALLOON); 909 object_property_add(obj, "guest-stats", "guest statistics", 910 balloon_ccw_stats_get_all, NULL, NULL, dev, NULL); 911 912 object_property_add(obj, "guest-stats-polling-interval", "int", 913 balloon_ccw_stats_get_poll_interval, 914 balloon_ccw_stats_set_poll_interval, 915 NULL, dev, NULL); 916 } 917 918 static void virtio_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp) 919 { 920 VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev); 921 DeviceState *vdev = DEVICE(&dev->vdev); 922 DeviceState *qdev = DEVICE(ccw_dev); 923 Error *err = NULL; 924 char *bus_name; 925 926 /* 927 * For command line compatibility, this sets the virtio-scsi-device bus 928 * name as before. 929 */ 930 if (qdev->id) { 931 bus_name = g_strdup_printf("%s.0", qdev->id); 932 virtio_device_set_child_bus_name(VIRTIO_DEVICE(vdev), bus_name); 933 g_free(bus_name); 934 } 935 936 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 937 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 938 if (err) { 939 error_propagate(errp, err); 940 return; 941 } 942 943 virtio_ccw_device_realize(ccw_dev, VIRTIO_DEVICE(vdev), errp); 944 } 945 946 static void virtio_ccw_scsi_instance_init(Object *obj) 947 { 948 VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj); 949 950 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 951 TYPE_VIRTIO_SCSI); 952 object_property_add_alias(obj, "iothread", OBJECT(&dev->vdev), "iothread", 953 &error_abort); 954 } 955 956 #ifdef CONFIG_VHOST_SCSI 957 static void vhost_ccw_scsi_realize(VirtioCcwDevice *ccw_dev, Error **errp) 958 { 959 VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev); 960 DeviceState *vdev = DEVICE(&dev->vdev); 961 Error *err = NULL; 962 963 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 964 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 965 if (err) { 966 error_propagate(errp, err); 967 return; 968 } 969 970 virtio_ccw_device_realize(ccw_dev, VIRTIO_DEVICE(vdev), errp); 971 } 972 973 static void vhost_ccw_scsi_instance_init(Object *obj) 974 { 975 VHostSCSICcw *dev = VHOST_SCSI_CCW(obj); 976 977 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 978 TYPE_VHOST_SCSI); 979 } 980 #endif 981 982 static void virtio_ccw_rng_realize(VirtioCcwDevice *ccw_dev, Error **errp) 983 { 984 VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev); 985 DeviceState *vdev = DEVICE(&dev->vdev); 986 Error *err = NULL; 987 988 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 989 object_property_set_bool(OBJECT(vdev), true, "realized", &err); 990 if (err) { 991 error_propagate(errp, err); 992 return; 993 } 994 995 object_property_set_link(OBJECT(dev), 996 OBJECT(dev->vdev.conf.rng), "rng", 997 NULL); 998 999 virtio_ccw_device_realize(ccw_dev, VIRTIO_DEVICE(vdev), errp); 1000 } 1001 1002 /* DeviceState to VirtioCcwDevice. Note: used on datapath, 1003 * be careful and test performance if you change this. 1004 */ 1005 static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d) 1006 { 1007 return container_of(d, VirtioCcwDevice, parent_obj); 1008 } 1009 1010 static uint8_t virtio_set_ind_atomic(SubchDev *sch, uint64_t ind_loc, 1011 uint8_t to_be_set) 1012 { 1013 uint8_t ind_old, ind_new; 1014 hwaddr len = 1; 1015 uint8_t *ind_addr; 1016 1017 ind_addr = cpu_physical_memory_map(ind_loc, &len, 1); 1018 if (!ind_addr) { 1019 error_report("%s(%x.%x.%04x): unable to access indicator", 1020 __func__, sch->cssid, sch->ssid, sch->schid); 1021 return -1; 1022 } 1023 do { 1024 ind_old = *ind_addr; 1025 ind_new = ind_old | to_be_set; 1026 } while (atomic_cmpxchg(ind_addr, ind_old, ind_new) != ind_old); 1027 cpu_physical_memory_unmap(ind_addr, len, 1, len); 1028 1029 return ind_old; 1030 } 1031 1032 static void virtio_ccw_notify(DeviceState *d, uint16_t vector) 1033 { 1034 VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d); 1035 SubchDev *sch = dev->sch; 1036 uint64_t indicators; 1037 1038 if (vector >= 128) { 1039 return; 1040 } 1041 1042 if (vector < VIRTIO_PCI_QUEUE_MAX) { 1043 if (!dev->indicators) { 1044 return; 1045 } 1046 if (sch->thinint_active) { 1047 /* 1048 * In the adapter interrupt case, indicators points to a 1049 * memory area that may be (way) larger than 64 bit and 1050 * ind_bit indicates the start of the indicators in a big 1051 * endian notation. 1052 */ 1053 uint64_t ind_bit = dev->routes.adapter.ind_offset; 1054 1055 virtio_set_ind_atomic(sch, dev->indicators->addr + 1056 (ind_bit + vector) / 8, 1057 0x80 >> ((ind_bit + vector) % 8)); 1058 if (!virtio_set_ind_atomic(sch, dev->summary_indicator->addr, 1059 0x01)) { 1060 css_adapter_interrupt(dev->thinint_isc); 1061 } 1062 } else { 1063 indicators = ldq_phys(&address_space_memory, dev->indicators->addr); 1064 indicators |= 1ULL << vector; 1065 stq_phys(&address_space_memory, dev->indicators->addr, indicators); 1066 css_conditional_io_interrupt(sch); 1067 } 1068 } else { 1069 if (!dev->indicators2) { 1070 return; 1071 } 1072 vector = 0; 1073 indicators = ldq_phys(&address_space_memory, dev->indicators2->addr); 1074 indicators |= 1ULL << vector; 1075 stq_phys(&address_space_memory, dev->indicators2->addr, indicators); 1076 css_conditional_io_interrupt(sch); 1077 } 1078 } 1079 1080 static unsigned virtio_ccw_get_features(DeviceState *d) 1081 { 1082 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1083 1084 /* Only the first 32 feature bits are used. */ 1085 return dev->host_features[0]; 1086 } 1087 1088 static void virtio_ccw_reset(DeviceState *d) 1089 { 1090 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1091 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1092 1093 virtio_ccw_stop_ioeventfd(dev); 1094 virtio_reset(vdev); 1095 css_reset_sch(dev->sch); 1096 if (dev->indicators) { 1097 release_indicator(&dev->routes.adapter, dev->indicators); 1098 dev->indicators = NULL; 1099 } 1100 if (dev->indicators2) { 1101 release_indicator(&dev->routes.adapter, dev->indicators2); 1102 dev->indicators2 = NULL; 1103 } 1104 if (dev->summary_indicator) { 1105 release_indicator(&dev->routes.adapter, dev->summary_indicator); 1106 dev->summary_indicator = NULL; 1107 } 1108 } 1109 1110 static void virtio_ccw_vmstate_change(DeviceState *d, bool running) 1111 { 1112 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1113 1114 if (running) { 1115 virtio_ccw_start_ioeventfd(dev); 1116 } else { 1117 virtio_ccw_stop_ioeventfd(dev); 1118 } 1119 } 1120 1121 static bool virtio_ccw_query_guest_notifiers(DeviceState *d) 1122 { 1123 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1124 1125 return !!(dev->sch->curr_status.pmcw.flags & PMCW_FLAGS_MASK_ENA); 1126 } 1127 1128 static int virtio_ccw_set_host_notifier(DeviceState *d, int n, bool assign) 1129 { 1130 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1131 1132 /* Stop using the generic ioeventfd, we are doing eventfd handling 1133 * ourselves below */ 1134 dev->ioeventfd_disabled = assign; 1135 if (assign) { 1136 virtio_ccw_stop_ioeventfd(dev); 1137 } 1138 return virtio_ccw_set_guest2host_notifier(dev, n, assign, false); 1139 } 1140 1141 static int virtio_ccw_get_mappings(VirtioCcwDevice *dev) 1142 { 1143 int r; 1144 1145 if (!dev->sch->thinint_active) { 1146 return -EINVAL; 1147 } 1148 1149 r = map_indicator(&dev->routes.adapter, dev->summary_indicator); 1150 if (r) { 1151 return r; 1152 } 1153 r = map_indicator(&dev->routes.adapter, dev->indicators); 1154 if (r) { 1155 return r; 1156 } 1157 dev->routes.adapter.summary_addr = dev->summary_indicator->map; 1158 dev->routes.adapter.ind_addr = dev->indicators->map; 1159 1160 return 0; 1161 } 1162 1163 static int virtio_ccw_setup_irqroutes(VirtioCcwDevice *dev, int nvqs) 1164 { 1165 int i; 1166 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1167 int ret; 1168 S390FLICState *fs = s390_get_flic(); 1169 S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs); 1170 1171 ret = virtio_ccw_get_mappings(dev); 1172 if (ret) { 1173 return ret; 1174 } 1175 for (i = 0; i < nvqs; i++) { 1176 if (!virtio_queue_get_num(vdev, i)) { 1177 break; 1178 } 1179 } 1180 dev->routes.num_routes = i; 1181 return fsc->add_adapter_routes(fs, &dev->routes); 1182 } 1183 1184 static void virtio_ccw_release_irqroutes(VirtioCcwDevice *dev, int nvqs) 1185 { 1186 S390FLICState *fs = s390_get_flic(); 1187 S390FLICStateClass *fsc = S390_FLIC_COMMON_GET_CLASS(fs); 1188 1189 fsc->release_adapter_routes(fs, &dev->routes); 1190 } 1191 1192 static int virtio_ccw_add_irqfd(VirtioCcwDevice *dev, int n) 1193 { 1194 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1195 VirtQueue *vq = virtio_get_queue(vdev, n); 1196 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); 1197 1198 return kvm_irqchip_add_irqfd_notifier(kvm_state, notifier, NULL, 1199 dev->routes.gsi[n]); 1200 } 1201 1202 static void virtio_ccw_remove_irqfd(VirtioCcwDevice *dev, int n) 1203 { 1204 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1205 VirtQueue *vq = virtio_get_queue(vdev, n); 1206 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); 1207 int ret; 1208 1209 ret = kvm_irqchip_remove_irqfd_notifier(kvm_state, notifier, 1210 dev->routes.gsi[n]); 1211 assert(ret == 0); 1212 } 1213 1214 static int virtio_ccw_set_guest_notifier(VirtioCcwDevice *dev, int n, 1215 bool assign, bool with_irqfd) 1216 { 1217 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1218 VirtQueue *vq = virtio_get_queue(vdev, n); 1219 EventNotifier *notifier = virtio_queue_get_guest_notifier(vq); 1220 VirtioDeviceClass *k = VIRTIO_DEVICE_GET_CLASS(vdev); 1221 1222 if (assign) { 1223 int r = event_notifier_init(notifier, 0); 1224 1225 if (r < 0) { 1226 return r; 1227 } 1228 virtio_queue_set_guest_notifier_fd_handler(vq, true, with_irqfd); 1229 if (with_irqfd) { 1230 r = virtio_ccw_add_irqfd(dev, n); 1231 if (r) { 1232 virtio_queue_set_guest_notifier_fd_handler(vq, false, 1233 with_irqfd); 1234 return r; 1235 } 1236 } 1237 /* 1238 * We do not support individual masking for channel devices, so we 1239 * need to manually trigger any guest masking callbacks here. 1240 */ 1241 if (k->guest_notifier_mask) { 1242 k->guest_notifier_mask(vdev, n, false); 1243 } 1244 /* get lost events and re-inject */ 1245 if (k->guest_notifier_pending && 1246 k->guest_notifier_pending(vdev, n)) { 1247 event_notifier_set(notifier); 1248 } 1249 } else { 1250 if (k->guest_notifier_mask) { 1251 k->guest_notifier_mask(vdev, n, true); 1252 } 1253 if (with_irqfd) { 1254 virtio_ccw_remove_irqfd(dev, n); 1255 } 1256 virtio_queue_set_guest_notifier_fd_handler(vq, false, with_irqfd); 1257 event_notifier_cleanup(notifier); 1258 } 1259 return 0; 1260 } 1261 1262 static int virtio_ccw_set_guest_notifiers(DeviceState *d, int nvqs, 1263 bool assigned) 1264 { 1265 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1266 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1267 bool with_irqfd = dev->sch->thinint_active && kvm_irqfds_enabled(); 1268 int r, n; 1269 1270 if (with_irqfd && assigned) { 1271 /* irq routes need to be set up before assigning irqfds */ 1272 r = virtio_ccw_setup_irqroutes(dev, nvqs); 1273 if (r < 0) { 1274 goto irqroute_error; 1275 } 1276 } 1277 for (n = 0; n < nvqs; n++) { 1278 if (!virtio_queue_get_num(vdev, n)) { 1279 break; 1280 } 1281 r = virtio_ccw_set_guest_notifier(dev, n, assigned, with_irqfd); 1282 if (r < 0) { 1283 goto assign_error; 1284 } 1285 } 1286 if (with_irqfd && !assigned) { 1287 /* release irq routes after irqfds have been released */ 1288 virtio_ccw_release_irqroutes(dev, nvqs); 1289 } 1290 return 0; 1291 1292 assign_error: 1293 while (--n >= 0) { 1294 virtio_ccw_set_guest_notifier(dev, n, !assigned, false); 1295 } 1296 irqroute_error: 1297 if (with_irqfd && assigned) { 1298 virtio_ccw_release_irqroutes(dev, nvqs); 1299 } 1300 return r; 1301 } 1302 1303 static void virtio_ccw_save_queue(DeviceState *d, int n, QEMUFile *f) 1304 { 1305 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1306 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1307 1308 qemu_put_be16(f, virtio_queue_vector(vdev, n)); 1309 } 1310 1311 static int virtio_ccw_load_queue(DeviceState *d, int n, QEMUFile *f) 1312 { 1313 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1314 VirtIODevice *vdev = virtio_bus_get_device(&dev->bus); 1315 uint16_t vector; 1316 1317 qemu_get_be16s(f, &vector); 1318 virtio_queue_set_vector(vdev, n , vector); 1319 1320 return 0; 1321 } 1322 1323 static void virtio_ccw_save_config(DeviceState *d, QEMUFile *f) 1324 { 1325 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1326 SubchDev *s = dev->sch; 1327 1328 subch_device_save(s, f); 1329 if (dev->indicators != NULL) { 1330 qemu_put_be32(f, dev->indicators->len); 1331 qemu_put_be64(f, dev->indicators->addr); 1332 } else { 1333 qemu_put_be32(f, 0); 1334 qemu_put_be64(f, 0UL); 1335 } 1336 if (dev->indicators2 != NULL) { 1337 qemu_put_be32(f, dev->indicators2->len); 1338 qemu_put_be64(f, dev->indicators2->addr); 1339 } else { 1340 qemu_put_be32(f, 0); 1341 qemu_put_be64(f, 0UL); 1342 } 1343 if (dev->summary_indicator != NULL) { 1344 qemu_put_be32(f, dev->summary_indicator->len); 1345 qemu_put_be64(f, dev->summary_indicator->addr); 1346 } else { 1347 qemu_put_be32(f, 0); 1348 qemu_put_be64(f, 0UL); 1349 } 1350 qemu_put_be64(f, dev->routes.adapter.ind_offset); 1351 qemu_put_byte(f, dev->thinint_isc); 1352 } 1353 1354 static int virtio_ccw_load_config(DeviceState *d, QEMUFile *f) 1355 { 1356 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 1357 SubchDev *s = dev->sch; 1358 int len; 1359 1360 s->driver_data = dev; 1361 subch_device_load(s, f); 1362 len = qemu_get_be32(f); 1363 if (len != 0) { 1364 dev->indicators = get_indicator(qemu_get_be64(f), len); 1365 } else { 1366 qemu_get_be64(f); 1367 dev->indicators = NULL; 1368 } 1369 len = qemu_get_be32(f); 1370 if (len != 0) { 1371 dev->indicators2 = get_indicator(qemu_get_be64(f), len); 1372 } else { 1373 qemu_get_be64(f); 1374 dev->indicators2 = NULL; 1375 } 1376 len = qemu_get_be32(f); 1377 if (len != 0) { 1378 dev->summary_indicator = get_indicator(qemu_get_be64(f), len); 1379 } else { 1380 qemu_get_be64(f); 1381 dev->summary_indicator = NULL; 1382 } 1383 dev->routes.adapter.ind_offset = qemu_get_be64(f); 1384 dev->thinint_isc = qemu_get_byte(f); 1385 if (s->thinint_active) { 1386 return css_register_io_adapter(CSS_IO_ADAPTER_VIRTIO, 1387 dev->thinint_isc, true, false, 1388 &dev->routes.adapter.adapter_id); 1389 } 1390 1391 return 0; 1392 } 1393 1394 /**************** Virtio-ccw Bus Device Descriptions *******************/ 1395 1396 static Property virtio_ccw_net_properties[] = { 1397 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 1398 DEFINE_VIRTIO_NET_FEATURES(VirtioCcwDevice, host_features[0]), 1399 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1400 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1401 DEFINE_PROP_END_OF_LIST(), 1402 }; 1403 1404 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data) 1405 { 1406 DeviceClass *dc = DEVICE_CLASS(klass); 1407 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1408 1409 k->realize = virtio_ccw_net_realize; 1410 k->exit = virtio_ccw_exit; 1411 dc->reset = virtio_ccw_reset; 1412 dc->props = virtio_ccw_net_properties; 1413 } 1414 1415 static const TypeInfo virtio_ccw_net = { 1416 .name = TYPE_VIRTIO_NET_CCW, 1417 .parent = TYPE_VIRTIO_CCW_DEVICE, 1418 .instance_size = sizeof(VirtIONetCcw), 1419 .instance_init = virtio_ccw_net_instance_init, 1420 .class_init = virtio_ccw_net_class_init, 1421 }; 1422 1423 static Property virtio_ccw_blk_properties[] = { 1424 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 1425 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1426 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1427 DEFINE_PROP_END_OF_LIST(), 1428 }; 1429 1430 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data) 1431 { 1432 DeviceClass *dc = DEVICE_CLASS(klass); 1433 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1434 1435 k->realize = virtio_ccw_blk_realize; 1436 k->exit = virtio_ccw_exit; 1437 dc->reset = virtio_ccw_reset; 1438 dc->props = virtio_ccw_blk_properties; 1439 } 1440 1441 static const TypeInfo virtio_ccw_blk = { 1442 .name = TYPE_VIRTIO_BLK_CCW, 1443 .parent = TYPE_VIRTIO_CCW_DEVICE, 1444 .instance_size = sizeof(VirtIOBlkCcw), 1445 .instance_init = virtio_ccw_blk_instance_init, 1446 .class_init = virtio_ccw_blk_class_init, 1447 }; 1448 1449 static Property virtio_ccw_serial_properties[] = { 1450 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 1451 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1452 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1453 DEFINE_PROP_END_OF_LIST(), 1454 }; 1455 1456 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data) 1457 { 1458 DeviceClass *dc = DEVICE_CLASS(klass); 1459 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1460 1461 k->realize = virtio_ccw_serial_realize; 1462 k->exit = virtio_ccw_exit; 1463 dc->reset = virtio_ccw_reset; 1464 dc->props = virtio_ccw_serial_properties; 1465 } 1466 1467 static const TypeInfo virtio_ccw_serial = { 1468 .name = TYPE_VIRTIO_SERIAL_CCW, 1469 .parent = TYPE_VIRTIO_CCW_DEVICE, 1470 .instance_size = sizeof(VirtioSerialCcw), 1471 .instance_init = virtio_ccw_serial_instance_init, 1472 .class_init = virtio_ccw_serial_class_init, 1473 }; 1474 1475 static Property virtio_ccw_balloon_properties[] = { 1476 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 1477 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1478 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1479 DEFINE_PROP_END_OF_LIST(), 1480 }; 1481 1482 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data) 1483 { 1484 DeviceClass *dc = DEVICE_CLASS(klass); 1485 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1486 1487 k->realize = virtio_ccw_balloon_realize; 1488 k->exit = virtio_ccw_exit; 1489 dc->reset = virtio_ccw_reset; 1490 dc->props = virtio_ccw_balloon_properties; 1491 } 1492 1493 static const TypeInfo virtio_ccw_balloon = { 1494 .name = TYPE_VIRTIO_BALLOON_CCW, 1495 .parent = TYPE_VIRTIO_CCW_DEVICE, 1496 .instance_size = sizeof(VirtIOBalloonCcw), 1497 .instance_init = virtio_ccw_balloon_instance_init, 1498 .class_init = virtio_ccw_balloon_class_init, 1499 }; 1500 1501 static Property virtio_ccw_scsi_properties[] = { 1502 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 1503 DEFINE_VIRTIO_SCSI_FEATURES(VirtioCcwDevice, host_features[0]), 1504 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1505 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1506 DEFINE_PROP_END_OF_LIST(), 1507 }; 1508 1509 static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data) 1510 { 1511 DeviceClass *dc = DEVICE_CLASS(klass); 1512 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1513 1514 k->realize = virtio_ccw_scsi_realize; 1515 k->exit = virtio_ccw_exit; 1516 dc->reset = virtio_ccw_reset; 1517 dc->props = virtio_ccw_scsi_properties; 1518 } 1519 1520 static const TypeInfo virtio_ccw_scsi = { 1521 .name = TYPE_VIRTIO_SCSI_CCW, 1522 .parent = TYPE_VIRTIO_CCW_DEVICE, 1523 .instance_size = sizeof(VirtIOSCSICcw), 1524 .instance_init = virtio_ccw_scsi_instance_init, 1525 .class_init = virtio_ccw_scsi_class_init, 1526 }; 1527 1528 #ifdef CONFIG_VHOST_SCSI 1529 static Property vhost_ccw_scsi_properties[] = { 1530 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 1531 DEFINE_PROP_END_OF_LIST(), 1532 }; 1533 1534 static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data) 1535 { 1536 DeviceClass *dc = DEVICE_CLASS(klass); 1537 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1538 1539 k->realize = vhost_ccw_scsi_realize; 1540 k->exit = virtio_ccw_exit; 1541 dc->reset = virtio_ccw_reset; 1542 dc->props = vhost_ccw_scsi_properties; 1543 } 1544 1545 static const TypeInfo vhost_ccw_scsi = { 1546 .name = TYPE_VHOST_SCSI_CCW, 1547 .parent = TYPE_VIRTIO_CCW_DEVICE, 1548 .instance_size = sizeof(VHostSCSICcw), 1549 .instance_init = vhost_ccw_scsi_instance_init, 1550 .class_init = vhost_ccw_scsi_class_init, 1551 }; 1552 #endif 1553 1554 static void virtio_ccw_rng_instance_init(Object *obj) 1555 { 1556 VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj); 1557 1558 virtio_instance_init_common(obj, &dev->vdev, sizeof(dev->vdev), 1559 TYPE_VIRTIO_RNG); 1560 object_property_add_alias(obj, "rng", OBJECT(&dev->vdev), 1561 "rng", &error_abort); 1562 } 1563 1564 static Property virtio_ccw_rng_properties[] = { 1565 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 1566 DEFINE_PROP_BIT("ioeventfd", VirtioCcwDevice, flags, 1567 VIRTIO_CCW_FLAG_USE_IOEVENTFD_BIT, true), 1568 DEFINE_PROP_END_OF_LIST(), 1569 }; 1570 1571 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data) 1572 { 1573 DeviceClass *dc = DEVICE_CLASS(klass); 1574 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 1575 1576 k->realize = virtio_ccw_rng_realize; 1577 k->exit = virtio_ccw_exit; 1578 dc->reset = virtio_ccw_reset; 1579 dc->props = virtio_ccw_rng_properties; 1580 } 1581 1582 static const TypeInfo virtio_ccw_rng = { 1583 .name = TYPE_VIRTIO_RNG_CCW, 1584 .parent = TYPE_VIRTIO_CCW_DEVICE, 1585 .instance_size = sizeof(VirtIORNGCcw), 1586 .instance_init = virtio_ccw_rng_instance_init, 1587 .class_init = virtio_ccw_rng_class_init, 1588 }; 1589 1590 static void virtio_ccw_busdev_realize(DeviceState *dev, Error **errp) 1591 { 1592 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; 1593 VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev); 1594 1595 virtio_ccw_bus_new(&_dev->bus, sizeof(_dev->bus), _dev); 1596 _info->realize(_dev, errp); 1597 } 1598 1599 static int virtio_ccw_busdev_exit(DeviceState *dev) 1600 { 1601 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; 1602 VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev); 1603 1604 return _info->exit(_dev); 1605 } 1606 1607 static void virtio_ccw_busdev_unplug(HotplugHandler *hotplug_dev, 1608 DeviceState *dev, Error **errp) 1609 { 1610 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; 1611 SubchDev *sch = _dev->sch; 1612 1613 virtio_ccw_stop_ioeventfd(_dev); 1614 1615 /* 1616 * We should arrive here only for device_del, since we don't support 1617 * direct hot(un)plug of channels, but only through virtio. 1618 */ 1619 assert(sch != NULL); 1620 /* Subchannel is now disabled and no longer valid. */ 1621 sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA | 1622 PMCW_FLAGS_MASK_DNV); 1623 1624 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0); 1625 1626 object_unparent(OBJECT(dev)); 1627 } 1628 1629 static Property virtio_ccw_properties[] = { 1630 DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]), 1631 DEFINE_PROP_END_OF_LIST(), 1632 }; 1633 1634 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data) 1635 { 1636 DeviceClass *dc = DEVICE_CLASS(klass); 1637 1638 dc->props = virtio_ccw_properties; 1639 dc->realize = virtio_ccw_busdev_realize; 1640 dc->exit = virtio_ccw_busdev_exit; 1641 dc->bus_type = TYPE_VIRTUAL_CSS_BUS; 1642 } 1643 1644 static const TypeInfo virtio_ccw_device_info = { 1645 .name = TYPE_VIRTIO_CCW_DEVICE, 1646 .parent = TYPE_DEVICE, 1647 .instance_size = sizeof(VirtioCcwDevice), 1648 .class_init = virtio_ccw_device_class_init, 1649 .class_size = sizeof(VirtIOCCWDeviceClass), 1650 .abstract = true, 1651 }; 1652 1653 /***************** Virtual-css Bus Bridge Device ********************/ 1654 /* Only required to have the virtio bus as child in the system bus */ 1655 1656 static int virtual_css_bridge_init(SysBusDevice *dev) 1657 { 1658 /* nothing */ 1659 return 0; 1660 } 1661 1662 static void virtual_css_bridge_class_init(ObjectClass *klass, void *data) 1663 { 1664 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 1665 HotplugHandlerClass *hc = HOTPLUG_HANDLER_CLASS(klass); 1666 1667 k->init = virtual_css_bridge_init; 1668 hc->unplug = virtio_ccw_busdev_unplug; 1669 } 1670 1671 static const TypeInfo virtual_css_bridge_info = { 1672 .name = "virtual-css-bridge", 1673 .parent = TYPE_SYS_BUS_DEVICE, 1674 .instance_size = sizeof(SysBusDevice), 1675 .class_init = virtual_css_bridge_class_init, 1676 .interfaces = (InterfaceInfo[]) { 1677 { TYPE_HOTPLUG_HANDLER }, 1678 { } 1679 } 1680 }; 1681 1682 /* virtio-ccw-bus */ 1683 1684 static void virtio_ccw_bus_new(VirtioBusState *bus, size_t bus_size, 1685 VirtioCcwDevice *dev) 1686 { 1687 DeviceState *qdev = DEVICE(dev); 1688 char virtio_bus_name[] = "virtio-bus"; 1689 1690 qbus_create_inplace(bus, bus_size, TYPE_VIRTIO_CCW_BUS, 1691 qdev, virtio_bus_name); 1692 } 1693 1694 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data) 1695 { 1696 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); 1697 BusClass *bus_class = BUS_CLASS(klass); 1698 1699 bus_class->max_dev = 1; 1700 k->notify = virtio_ccw_notify; 1701 k->get_features = virtio_ccw_get_features; 1702 k->vmstate_change = virtio_ccw_vmstate_change; 1703 k->query_guest_notifiers = virtio_ccw_query_guest_notifiers; 1704 k->set_host_notifier = virtio_ccw_set_host_notifier; 1705 k->set_guest_notifiers = virtio_ccw_set_guest_notifiers; 1706 k->save_queue = virtio_ccw_save_queue; 1707 k->load_queue = virtio_ccw_load_queue; 1708 k->save_config = virtio_ccw_save_config; 1709 k->load_config = virtio_ccw_load_config; 1710 } 1711 1712 static const TypeInfo virtio_ccw_bus_info = { 1713 .name = TYPE_VIRTIO_CCW_BUS, 1714 .parent = TYPE_VIRTIO_BUS, 1715 .instance_size = sizeof(VirtioCcwBusState), 1716 .class_init = virtio_ccw_bus_class_init, 1717 }; 1718 1719 static void virtio_ccw_register(void) 1720 { 1721 type_register_static(&virtio_ccw_bus_info); 1722 type_register_static(&virtual_css_bus_info); 1723 type_register_static(&virtio_ccw_device_info); 1724 type_register_static(&virtio_ccw_serial); 1725 type_register_static(&virtio_ccw_blk); 1726 type_register_static(&virtio_ccw_net); 1727 type_register_static(&virtio_ccw_balloon); 1728 type_register_static(&virtio_ccw_scsi); 1729 #ifdef CONFIG_VHOST_SCSI 1730 type_register_static(&vhost_ccw_scsi); 1731 #endif 1732 type_register_static(&virtio_ccw_rng); 1733 type_register_static(&virtual_css_bridge_info); 1734 } 1735 1736 type_init(virtio_ccw_register) 1737