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