1 /* 2 * virtio ccw target implementation 3 * 4 * Copyright 2012 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 "block/block.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 25 #include "ioinst.h" 26 #include "css.h" 27 #include "virtio-ccw.h" 28 #include "trace.h" 29 30 static void virtio_ccw_bus_new(VirtioBusState *bus, VirtioCcwDevice *dev); 31 32 static int virtual_css_bus_reset(BusState *qbus) 33 { 34 /* This should actually be modelled via the generic css */ 35 css_reset(); 36 37 /* we dont traverse ourself, return 0 */ 38 return 0; 39 } 40 41 42 static void virtual_css_bus_class_init(ObjectClass *klass, void *data) 43 { 44 BusClass *k = BUS_CLASS(klass); 45 46 k->reset = virtual_css_bus_reset; 47 } 48 49 static const TypeInfo virtual_css_bus_info = { 50 .name = TYPE_VIRTUAL_CSS_BUS, 51 .parent = TYPE_BUS, 52 .instance_size = sizeof(VirtualCssBus), 53 .class_init = virtual_css_bus_class_init, 54 }; 55 56 VirtIODevice *virtio_ccw_get_vdev(SubchDev *sch) 57 { 58 VirtIODevice *vdev = NULL; 59 60 if (sch->driver_data) { 61 vdev = ((VirtioCcwDevice *)sch->driver_data)->vdev; 62 } 63 return vdev; 64 } 65 66 VirtualCssBus *virtual_css_bus_init(void) 67 { 68 VirtualCssBus *cbus; 69 BusState *bus; 70 DeviceState *dev; 71 72 /* Create bridge device */ 73 dev = qdev_create(NULL, "virtual-css-bridge"); 74 qdev_init_nofail(dev); 75 76 /* Create bus on bridge device */ 77 bus = qbus_create(TYPE_VIRTUAL_CSS_BUS, dev, "virtual-css"); 78 cbus = VIRTUAL_CSS_BUS(bus); 79 80 /* Enable hotplugging */ 81 bus->allow_hotplug = 1; 82 83 return cbus; 84 } 85 86 /* Communication blocks used by several channel commands. */ 87 typedef struct VqInfoBlock { 88 uint64_t queue; 89 uint32_t align; 90 uint16_t index; 91 uint16_t num; 92 } QEMU_PACKED VqInfoBlock; 93 94 typedef struct VqConfigBlock { 95 uint16_t index; 96 uint16_t num_max; 97 } QEMU_PACKED VqConfigBlock; 98 99 typedef struct VirtioFeatDesc { 100 uint32_t features; 101 uint8_t index; 102 } QEMU_PACKED VirtioFeatDesc; 103 104 /* Specify where the virtqueues for the subchannel are in guest memory. */ 105 static int virtio_ccw_set_vqs(SubchDev *sch, uint64_t addr, uint32_t align, 106 uint16_t index, uint16_t num) 107 { 108 VirtioCcwDevice *dev = sch->driver_data; 109 110 if (index > VIRTIO_PCI_QUEUE_MAX) { 111 return -EINVAL; 112 } 113 114 /* Current code in virtio.c relies on 4K alignment. */ 115 if (addr && (align != 4096)) { 116 return -EINVAL; 117 } 118 119 if (!dev) { 120 return -EINVAL; 121 } 122 123 virtio_queue_set_addr(dev->vdev, index, addr); 124 if (!addr) { 125 virtio_queue_set_vector(dev->vdev, index, 0); 126 } else { 127 /* Fail if we don't have a big enough queue. */ 128 /* TODO: Add interface to handle vring.num changing */ 129 if (virtio_queue_get_num(dev->vdev, index) > num) { 130 return -EINVAL; 131 } 132 virtio_queue_set_vector(dev->vdev, index, index); 133 } 134 /* tell notify handler in case of config change */ 135 dev->vdev->config_vector = VIRTIO_PCI_QUEUE_MAX; 136 return 0; 137 } 138 139 static int virtio_ccw_cb(SubchDev *sch, CCW1 ccw) 140 { 141 int ret; 142 VqInfoBlock info; 143 uint8_t status; 144 VirtioFeatDesc features; 145 void *config; 146 hwaddr indicators; 147 VqConfigBlock vq_config; 148 VirtioCcwDevice *dev = sch->driver_data; 149 bool check_len; 150 int len; 151 hwaddr hw_len; 152 153 if (!dev) { 154 return -EINVAL; 155 } 156 157 trace_virtio_ccw_interpret_ccw(sch->cssid, sch->ssid, sch->schid, 158 ccw.cmd_code); 159 check_len = !((ccw.flags & CCW_FLAG_SLI) && !(ccw.flags & CCW_FLAG_DC)); 160 161 /* Look at the command. */ 162 switch (ccw.cmd_code) { 163 case CCW_CMD_SET_VQ: 164 if (check_len) { 165 if (ccw.count != sizeof(info)) { 166 ret = -EINVAL; 167 break; 168 } 169 } else if (ccw.count < sizeof(info)) { 170 /* Can't execute command. */ 171 ret = -EINVAL; 172 break; 173 } 174 if (!ccw.cda) { 175 ret = -EFAULT; 176 } else { 177 info.queue = ldq_phys(ccw.cda); 178 info.align = ldl_phys(ccw.cda + sizeof(info.queue)); 179 info.index = lduw_phys(ccw.cda + sizeof(info.queue) 180 + sizeof(info.align)); 181 info.num = lduw_phys(ccw.cda + sizeof(info.queue) 182 + sizeof(info.align) 183 + sizeof(info.index)); 184 ret = virtio_ccw_set_vqs(sch, info.queue, info.align, info.index, 185 info.num); 186 sch->curr_status.scsw.count = 0; 187 } 188 break; 189 case CCW_CMD_VDEV_RESET: 190 virtio_reset(dev->vdev); 191 ret = 0; 192 break; 193 case CCW_CMD_READ_FEAT: 194 if (check_len) { 195 if (ccw.count != sizeof(features)) { 196 ret = -EINVAL; 197 break; 198 } 199 } else if (ccw.count < sizeof(features)) { 200 /* Can't execute command. */ 201 ret = -EINVAL; 202 break; 203 } 204 if (!ccw.cda) { 205 ret = -EFAULT; 206 } else { 207 features.index = ldub_phys(ccw.cda + sizeof(features.features)); 208 if (features.index < ARRAY_SIZE(dev->host_features)) { 209 features.features = dev->host_features[features.index]; 210 } else { 211 /* Return zeroes if the guest supports more feature bits. */ 212 features.features = 0; 213 } 214 stl_le_phys(ccw.cda, features.features); 215 sch->curr_status.scsw.count = ccw.count - sizeof(features); 216 ret = 0; 217 } 218 break; 219 case CCW_CMD_WRITE_FEAT: 220 if (check_len) { 221 if (ccw.count != sizeof(features)) { 222 ret = -EINVAL; 223 break; 224 } 225 } else if (ccw.count < sizeof(features)) { 226 /* Can't execute command. */ 227 ret = -EINVAL; 228 break; 229 } 230 if (!ccw.cda) { 231 ret = -EFAULT; 232 } else { 233 features.index = ldub_phys(ccw.cda + sizeof(features.features)); 234 features.features = ldl_le_phys(ccw.cda); 235 if (features.index < ARRAY_SIZE(dev->host_features)) { 236 virtio_bus_set_vdev_features(&dev->bus, features.features); 237 dev->vdev->guest_features = features.features; 238 } else { 239 /* 240 * If the guest supports more feature bits, assert that it 241 * passes us zeroes for those we don't support. 242 */ 243 if (features.features) { 244 fprintf(stderr, "Guest bug: features[%i]=%x (expected 0)\n", 245 features.index, features.features); 246 /* XXX: do a unit check here? */ 247 } 248 } 249 sch->curr_status.scsw.count = ccw.count - sizeof(features); 250 ret = 0; 251 } 252 break; 253 case CCW_CMD_READ_CONF: 254 if (check_len) { 255 if (ccw.count > dev->vdev->config_len) { 256 ret = -EINVAL; 257 break; 258 } 259 } 260 len = MIN(ccw.count, dev->vdev->config_len); 261 if (!ccw.cda) { 262 ret = -EFAULT; 263 } else { 264 virtio_bus_get_vdev_config(&dev->bus, dev->vdev->config); 265 /* XXX config space endianness */ 266 cpu_physical_memory_write(ccw.cda, dev->vdev->config, len); 267 sch->curr_status.scsw.count = ccw.count - len; 268 ret = 0; 269 } 270 break; 271 case CCW_CMD_WRITE_CONF: 272 if (check_len) { 273 if (ccw.count > dev->vdev->config_len) { 274 ret = -EINVAL; 275 break; 276 } 277 } 278 len = MIN(ccw.count, dev->vdev->config_len); 279 hw_len = len; 280 if (!ccw.cda) { 281 ret = -EFAULT; 282 } else { 283 config = cpu_physical_memory_map(ccw.cda, &hw_len, 0); 284 if (!config) { 285 ret = -EFAULT; 286 } else { 287 len = hw_len; 288 /* XXX config space endianness */ 289 memcpy(dev->vdev->config, config, len); 290 cpu_physical_memory_unmap(config, hw_len, 0, hw_len); 291 virtio_bus_set_vdev_config(&dev->bus, dev->vdev->config); 292 sch->curr_status.scsw.count = ccw.count - len; 293 ret = 0; 294 } 295 } 296 break; 297 case CCW_CMD_WRITE_STATUS: 298 if (check_len) { 299 if (ccw.count != sizeof(status)) { 300 ret = -EINVAL; 301 break; 302 } 303 } else if (ccw.count < sizeof(status)) { 304 /* Can't execute command. */ 305 ret = -EINVAL; 306 break; 307 } 308 if (!ccw.cda) { 309 ret = -EFAULT; 310 } else { 311 status = ldub_phys(ccw.cda); 312 virtio_set_status(dev->vdev, status); 313 if (dev->vdev->status == 0) { 314 virtio_reset(dev->vdev); 315 } 316 sch->curr_status.scsw.count = ccw.count - sizeof(status); 317 ret = 0; 318 } 319 break; 320 case CCW_CMD_SET_IND: 321 if (check_len) { 322 if (ccw.count != sizeof(indicators)) { 323 ret = -EINVAL; 324 break; 325 } 326 } else if (ccw.count < sizeof(indicators)) { 327 /* Can't execute command. */ 328 ret = -EINVAL; 329 break; 330 } 331 indicators = ldq_phys(ccw.cda); 332 if (!indicators) { 333 ret = -EFAULT; 334 } else { 335 dev->indicators = indicators; 336 sch->curr_status.scsw.count = ccw.count - sizeof(indicators); 337 ret = 0; 338 } 339 break; 340 case CCW_CMD_SET_CONF_IND: 341 if (check_len) { 342 if (ccw.count != sizeof(indicators)) { 343 ret = -EINVAL; 344 break; 345 } 346 } else if (ccw.count < sizeof(indicators)) { 347 /* Can't execute command. */ 348 ret = -EINVAL; 349 break; 350 } 351 indicators = ldq_phys(ccw.cda); 352 if (!indicators) { 353 ret = -EFAULT; 354 } else { 355 dev->indicators2 = indicators; 356 sch->curr_status.scsw.count = ccw.count - sizeof(indicators); 357 ret = 0; 358 } 359 break; 360 case CCW_CMD_READ_VQ_CONF: 361 if (check_len) { 362 if (ccw.count != sizeof(vq_config)) { 363 ret = -EINVAL; 364 break; 365 } 366 } else if (ccw.count < sizeof(vq_config)) { 367 /* Can't execute command. */ 368 ret = -EINVAL; 369 break; 370 } 371 if (!ccw.cda) { 372 ret = -EFAULT; 373 } else { 374 vq_config.index = lduw_phys(ccw.cda); 375 vq_config.num_max = virtio_queue_get_num(dev->vdev, 376 vq_config.index); 377 stw_phys(ccw.cda + sizeof(vq_config.index), vq_config.num_max); 378 sch->curr_status.scsw.count = ccw.count - sizeof(vq_config); 379 ret = 0; 380 } 381 break; 382 default: 383 ret = -ENOSYS; 384 break; 385 } 386 return ret; 387 } 388 389 static int virtio_ccw_device_init(VirtioCcwDevice *dev, VirtIODevice *vdev) 390 { 391 unsigned int cssid = 0; 392 unsigned int ssid = 0; 393 unsigned int schid; 394 unsigned int devno; 395 bool have_devno = false; 396 bool found = false; 397 SubchDev *sch; 398 int ret; 399 int num; 400 DeviceState *parent = DEVICE(dev); 401 402 sch = g_malloc0(sizeof(SubchDev)); 403 404 sch->driver_data = dev; 405 dev->sch = sch; 406 407 dev->vdev = vdev; 408 dev->indicators = 0; 409 410 /* Initialize subchannel structure. */ 411 sch->channel_prog = 0x0; 412 sch->last_cmd_valid = false; 413 sch->orb = NULL; 414 /* 415 * Use a device number if provided. Otherwise, fall back to subchannel 416 * number. 417 */ 418 if (dev->bus_id) { 419 num = sscanf(dev->bus_id, "%x.%x.%04x", &cssid, &ssid, &devno); 420 if (num == 3) { 421 if ((cssid > MAX_CSSID) || (ssid > MAX_SSID)) { 422 ret = -EINVAL; 423 error_report("Invalid cssid or ssid: cssid %x, ssid %x", 424 cssid, ssid); 425 goto out_err; 426 } 427 /* Enforce use of virtual cssid. */ 428 if (cssid != VIRTUAL_CSSID) { 429 ret = -EINVAL; 430 error_report("cssid %x not valid for virtio devices", cssid); 431 goto out_err; 432 } 433 if (css_devno_used(cssid, ssid, devno)) { 434 ret = -EEXIST; 435 error_report("Device %x.%x.%04x already exists", cssid, ssid, 436 devno); 437 goto out_err; 438 } 439 sch->cssid = cssid; 440 sch->ssid = ssid; 441 sch->devno = devno; 442 have_devno = true; 443 } else { 444 ret = -EINVAL; 445 error_report("Malformed devno parameter '%s'", dev->bus_id); 446 goto out_err; 447 } 448 } 449 450 /* Find the next free id. */ 451 if (have_devno) { 452 for (schid = 0; schid <= MAX_SCHID; schid++) { 453 if (!css_find_subch(1, cssid, ssid, schid)) { 454 sch->schid = schid; 455 css_subch_assign(cssid, ssid, schid, devno, sch); 456 found = true; 457 break; 458 } 459 } 460 if (!found) { 461 ret = -ENODEV; 462 error_report("No free subchannel found for %x.%x.%04x", cssid, ssid, 463 devno); 464 goto out_err; 465 } 466 trace_virtio_ccw_new_device(cssid, ssid, schid, devno, 467 "user-configured"); 468 } else { 469 cssid = VIRTUAL_CSSID; 470 for (ssid = 0; ssid <= MAX_SSID; ssid++) { 471 for (schid = 0; schid <= MAX_SCHID; schid++) { 472 if (!css_find_subch(1, cssid, ssid, schid)) { 473 sch->cssid = cssid; 474 sch->ssid = ssid; 475 sch->schid = schid; 476 devno = schid; 477 /* 478 * If the devno is already taken, look further in this 479 * subchannel set. 480 */ 481 while (css_devno_used(cssid, ssid, devno)) { 482 if (devno == MAX_SCHID) { 483 devno = 0; 484 } else if (devno == schid - 1) { 485 ret = -ENODEV; 486 error_report("No free devno found"); 487 goto out_err; 488 } else { 489 devno++; 490 } 491 } 492 sch->devno = devno; 493 css_subch_assign(cssid, ssid, schid, devno, sch); 494 found = true; 495 break; 496 } 497 } 498 if (found) { 499 break; 500 } 501 } 502 if (!found) { 503 ret = -ENODEV; 504 error_report("Virtual channel subsystem is full!"); 505 goto out_err; 506 } 507 trace_virtio_ccw_new_device(cssid, ssid, schid, devno, 508 "auto-configured"); 509 } 510 511 /* Build initial schib. */ 512 css_sch_build_virtual_schib(sch, 0, VIRTIO_CCW_CHPID_TYPE); 513 514 sch->ccw_cb = virtio_ccw_cb; 515 516 /* Build senseid data. */ 517 memset(&sch->id, 0, sizeof(SenseId)); 518 sch->id.reserved = 0xff; 519 sch->id.cu_type = VIRTIO_CCW_CU_TYPE; 520 sch->id.cu_model = dev->vdev->device_id; 521 522 /* Only the first 32 feature bits are used. */ 523 dev->host_features[0] = virtio_bus_get_vdev_features(&dev->bus, 524 dev->host_features[0]); 525 526 dev->host_features[0] |= 0x1 << VIRTIO_F_NOTIFY_ON_EMPTY; 527 dev->host_features[0] |= 0x1 << VIRTIO_F_BAD_FEATURE; 528 529 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 530 parent->hotplugged, 1); 531 return 0; 532 533 out_err: 534 dev->sch = NULL; 535 g_free(sch); 536 return ret; 537 } 538 539 static int virtio_ccw_exit(VirtioCcwDevice *dev) 540 { 541 SubchDev *sch = dev->sch; 542 543 if (sch) { 544 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL); 545 g_free(sch); 546 } 547 dev->indicators = 0; 548 return 0; 549 } 550 551 static int virtio_ccw_net_init(VirtioCcwDevice *ccw_dev) 552 { 553 VirtIONetCcw *dev = VIRTIO_NET_CCW(ccw_dev); 554 DeviceState *vdev = DEVICE(&dev->vdev); 555 556 virtio_net_set_config_size(&dev->vdev, ccw_dev->host_features[0]); 557 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 558 if (qdev_init(vdev) < 0) { 559 return -1; 560 } 561 562 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev)); 563 } 564 565 static void virtio_ccw_net_instance_init(Object *obj) 566 { 567 VirtIONetCcw *dev = VIRTIO_NET_CCW(obj); 568 object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_NET); 569 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 570 } 571 572 static int virtio_ccw_blk_init(VirtioCcwDevice *ccw_dev) 573 { 574 VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(ccw_dev); 575 DeviceState *vdev = DEVICE(&dev->vdev); 576 virtio_blk_set_conf(vdev, &(dev->blk)); 577 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 578 if (qdev_init(vdev) < 0) { 579 return -1; 580 } 581 582 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev)); 583 } 584 585 static void virtio_ccw_blk_instance_init(Object *obj) 586 { 587 VirtIOBlkCcw *dev = VIRTIO_BLK_CCW(obj); 588 object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BLK); 589 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 590 } 591 592 static int virtio_ccw_serial_init(VirtioCcwDevice *ccw_dev) 593 { 594 VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(ccw_dev); 595 DeviceState *vdev = DEVICE(&dev->vdev); 596 597 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 598 if (qdev_init(vdev) < 0) { 599 return -1; 600 } 601 602 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev)); 603 } 604 605 606 static void virtio_ccw_serial_instance_init(Object *obj) 607 { 608 VirtioSerialCcw *dev = VIRTIO_SERIAL_CCW(obj); 609 object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_SERIAL); 610 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 611 } 612 613 static int virtio_ccw_balloon_init(VirtioCcwDevice *ccw_dev) 614 { 615 VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(ccw_dev); 616 DeviceState *vdev = DEVICE(&dev->vdev); 617 618 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 619 if (qdev_init(vdev) < 0) { 620 return -1; 621 } 622 623 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev)); 624 } 625 626 static void balloon_ccw_stats_get_all(Object *obj, struct Visitor *v, 627 void *opaque, const char *name, 628 Error **errp) 629 { 630 VirtIOBalloonCcw *dev = opaque; 631 object_property_get(OBJECT(&dev->vdev), v, "guest-stats", errp); 632 } 633 634 static void balloon_ccw_stats_get_poll_interval(Object *obj, struct Visitor *v, 635 void *opaque, const char *name, 636 Error **errp) 637 { 638 VirtIOBalloonCcw *dev = opaque; 639 object_property_get(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", 640 errp); 641 } 642 643 static void balloon_ccw_stats_set_poll_interval(Object *obj, struct Visitor *v, 644 void *opaque, const char *name, 645 Error **errp) 646 { 647 VirtIOBalloonCcw *dev = opaque; 648 object_property_set(OBJECT(&dev->vdev), v, "guest-stats-polling-interval", 649 errp); 650 } 651 652 static void virtio_ccw_balloon_instance_init(Object *obj) 653 { 654 VirtIOBalloonCcw *dev = VIRTIO_BALLOON_CCW(obj); 655 object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_BALLOON); 656 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 657 658 object_property_add(obj, "guest-stats", "guest statistics", 659 balloon_ccw_stats_get_all, NULL, NULL, dev, NULL); 660 661 object_property_add(obj, "guest-stats-polling-interval", "int", 662 balloon_ccw_stats_get_poll_interval, 663 balloon_ccw_stats_set_poll_interval, 664 NULL, dev, NULL); 665 } 666 667 static int virtio_ccw_scsi_init(VirtioCcwDevice *ccw_dev) 668 { 669 VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(ccw_dev); 670 DeviceState *vdev = DEVICE(&dev->vdev); 671 672 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 673 if (qdev_init(vdev) < 0) { 674 return -1; 675 } 676 677 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev)); 678 } 679 680 static void virtio_ccw_scsi_instance_init(Object *obj) 681 { 682 VirtIOSCSICcw *dev = VIRTIO_SCSI_CCW(obj); 683 object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_SCSI); 684 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 685 } 686 687 #ifdef CONFIG_VHOST_SCSI 688 static int vhost_ccw_scsi_init(VirtioCcwDevice *ccw_dev) 689 { 690 VHostSCSICcw *dev = VHOST_SCSI_CCW(ccw_dev); 691 DeviceState *vdev = DEVICE(&dev->vdev); 692 693 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 694 if (qdev_init(vdev) < 0) { 695 return -1; 696 } 697 698 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev)); 699 } 700 701 static void vhost_ccw_scsi_instance_init(Object *obj) 702 { 703 VHostSCSICcw *dev = VHOST_SCSI_CCW(obj); 704 object_initialize(OBJECT(&dev->vdev), TYPE_VHOST_SCSI); 705 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 706 } 707 #endif 708 709 static int virtio_ccw_rng_init(VirtioCcwDevice *ccw_dev) 710 { 711 VirtIORNGCcw *dev = VIRTIO_RNG_CCW(ccw_dev); 712 DeviceState *vdev = DEVICE(&dev->vdev); 713 714 qdev_set_parent_bus(vdev, BUS(&ccw_dev->bus)); 715 if (qdev_init(vdev) < 0) { 716 return -1; 717 } 718 719 object_property_set_link(OBJECT(dev), 720 OBJECT(dev->vdev.conf.default_backend), "rng", 721 NULL); 722 723 return virtio_ccw_device_init(ccw_dev, VIRTIO_DEVICE(vdev)); 724 } 725 726 /* DeviceState to VirtioCcwDevice. Note: used on datapath, 727 * be careful and test performance if you change this. 728 */ 729 static inline VirtioCcwDevice *to_virtio_ccw_dev_fast(DeviceState *d) 730 { 731 return container_of(d, VirtioCcwDevice, parent_obj); 732 } 733 734 static void virtio_ccw_notify(DeviceState *d, uint16_t vector) 735 { 736 VirtioCcwDevice *dev = to_virtio_ccw_dev_fast(d); 737 SubchDev *sch = dev->sch; 738 uint64_t indicators; 739 740 if (vector >= 128) { 741 return; 742 } 743 744 if (vector < VIRTIO_PCI_QUEUE_MAX) { 745 if (!dev->indicators) { 746 return; 747 } 748 indicators = ldq_phys(dev->indicators); 749 indicators |= 1ULL << vector; 750 stq_phys(dev->indicators, indicators); 751 } else { 752 if (!dev->indicators2) { 753 return; 754 } 755 vector = 0; 756 indicators = ldq_phys(dev->indicators2); 757 indicators |= 1ULL << vector; 758 stq_phys(dev->indicators2, indicators); 759 } 760 761 css_conditional_io_interrupt(sch); 762 763 } 764 765 static unsigned virtio_ccw_get_features(DeviceState *d) 766 { 767 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 768 769 /* Only the first 32 feature bits are used. */ 770 return dev->host_features[0]; 771 } 772 773 static void virtio_ccw_reset(DeviceState *d) 774 { 775 VirtioCcwDevice *dev = VIRTIO_CCW_DEVICE(d); 776 777 virtio_reset(dev->vdev); 778 css_reset_sch(dev->sch); 779 } 780 781 /**************** Virtio-ccw Bus Device Descriptions *******************/ 782 783 static Property virtio_ccw_net_properties[] = { 784 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 785 DEFINE_VIRTIO_NET_FEATURES(VirtioCcwDevice, host_features[0]), 786 DEFINE_VIRTIO_NET_PROPERTIES(VirtIONetCcw, vdev.net_conf), 787 DEFINE_NIC_PROPERTIES(VirtIONetCcw, vdev.nic_conf), 788 DEFINE_PROP_END_OF_LIST(), 789 }; 790 791 static void virtio_ccw_net_class_init(ObjectClass *klass, void *data) 792 { 793 DeviceClass *dc = DEVICE_CLASS(klass); 794 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 795 796 k->init = virtio_ccw_net_init; 797 k->exit = virtio_ccw_exit; 798 dc->reset = virtio_ccw_reset; 799 dc->props = virtio_ccw_net_properties; 800 } 801 802 static const TypeInfo virtio_ccw_net = { 803 .name = TYPE_VIRTIO_NET_CCW, 804 .parent = TYPE_VIRTIO_CCW_DEVICE, 805 .instance_size = sizeof(VirtIONetCcw), 806 .instance_init = virtio_ccw_net_instance_init, 807 .class_init = virtio_ccw_net_class_init, 808 }; 809 810 static Property virtio_ccw_blk_properties[] = { 811 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 812 DEFINE_VIRTIO_BLK_FEATURES(VirtioCcwDevice, host_features[0]), 813 DEFINE_VIRTIO_BLK_PROPERTIES(VirtIOBlkCcw, blk), 814 DEFINE_PROP_END_OF_LIST(), 815 }; 816 817 static void virtio_ccw_blk_class_init(ObjectClass *klass, void *data) 818 { 819 DeviceClass *dc = DEVICE_CLASS(klass); 820 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 821 822 k->init = virtio_ccw_blk_init; 823 k->exit = virtio_ccw_exit; 824 dc->reset = virtio_ccw_reset; 825 dc->props = virtio_ccw_blk_properties; 826 } 827 828 static const TypeInfo virtio_ccw_blk = { 829 .name = TYPE_VIRTIO_BLK_CCW, 830 .parent = TYPE_VIRTIO_CCW_DEVICE, 831 .instance_size = sizeof(VirtIOBlkCcw), 832 .instance_init = virtio_ccw_blk_instance_init, 833 .class_init = virtio_ccw_blk_class_init, 834 }; 835 836 static Property virtio_ccw_serial_properties[] = { 837 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 838 DEFINE_VIRTIO_SERIAL_PROPERTIES(VirtioSerialCcw, vdev.serial), 839 DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]), 840 DEFINE_PROP_END_OF_LIST(), 841 }; 842 843 static void virtio_ccw_serial_class_init(ObjectClass *klass, void *data) 844 { 845 DeviceClass *dc = DEVICE_CLASS(klass); 846 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 847 848 k->init = virtio_ccw_serial_init; 849 k->exit = virtio_ccw_exit; 850 dc->reset = virtio_ccw_reset; 851 dc->props = virtio_ccw_serial_properties; 852 } 853 854 static const TypeInfo virtio_ccw_serial = { 855 .name = TYPE_VIRTIO_SERIAL_CCW, 856 .parent = TYPE_VIRTIO_CCW_DEVICE, 857 .instance_size = sizeof(VirtioSerialCcw), 858 .instance_init = virtio_ccw_serial_instance_init, 859 .class_init = virtio_ccw_serial_class_init, 860 }; 861 862 static Property virtio_ccw_balloon_properties[] = { 863 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 864 DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]), 865 DEFINE_PROP_END_OF_LIST(), 866 }; 867 868 static void virtio_ccw_balloon_class_init(ObjectClass *klass, void *data) 869 { 870 DeviceClass *dc = DEVICE_CLASS(klass); 871 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 872 873 k->init = virtio_ccw_balloon_init; 874 k->exit = virtio_ccw_exit; 875 dc->reset = virtio_ccw_reset; 876 dc->props = virtio_ccw_balloon_properties; 877 } 878 879 static const TypeInfo virtio_ccw_balloon = { 880 .name = TYPE_VIRTIO_BALLOON_CCW, 881 .parent = TYPE_VIRTIO_CCW_DEVICE, 882 .instance_size = sizeof(VirtIOBalloonCcw), 883 .instance_init = virtio_ccw_balloon_instance_init, 884 .class_init = virtio_ccw_balloon_class_init, 885 }; 886 887 static Property virtio_ccw_scsi_properties[] = { 888 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 889 DEFINE_VIRTIO_SCSI_PROPERTIES(VirtIOSCSICcw, vdev.parent_obj.conf), 890 DEFINE_VIRTIO_SCSI_FEATURES(VirtioCcwDevice, host_features[0]), 891 DEFINE_PROP_END_OF_LIST(), 892 }; 893 894 static void virtio_ccw_scsi_class_init(ObjectClass *klass, void *data) 895 { 896 DeviceClass *dc = DEVICE_CLASS(klass); 897 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 898 899 k->init = virtio_ccw_scsi_init; 900 k->exit = virtio_ccw_exit; 901 dc->reset = virtio_ccw_reset; 902 dc->props = virtio_ccw_scsi_properties; 903 } 904 905 static const TypeInfo virtio_ccw_scsi = { 906 .name = TYPE_VIRTIO_SCSI_CCW, 907 .parent = TYPE_VIRTIO_CCW_DEVICE, 908 .instance_size = sizeof(VirtIOSCSICcw), 909 .instance_init = virtio_ccw_scsi_instance_init, 910 .class_init = virtio_ccw_scsi_class_init, 911 }; 912 913 #ifdef CONFIG_VHOST_SCSI 914 static Property vhost_ccw_scsi_properties[] = { 915 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 916 DEFINE_VHOST_SCSI_PROPERTIES(VirtIOSCSICcw, vdev.parent_obj.conf), 917 DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]), 918 DEFINE_PROP_END_OF_LIST(), 919 }; 920 921 static void vhost_ccw_scsi_class_init(ObjectClass *klass, void *data) 922 { 923 DeviceClass *dc = DEVICE_CLASS(klass); 924 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 925 926 k->init = vhost_ccw_scsi_init; 927 k->exit = virtio_ccw_exit; 928 dc->reset = virtio_ccw_reset; 929 dc->props = vhost_ccw_scsi_properties; 930 } 931 932 static const TypeInfo vhost_ccw_scsi = { 933 .name = TYPE_VHOST_SCSI_CCW, 934 .parent = TYPE_VIRTIO_CCW_DEVICE, 935 .instance_size = sizeof(VirtIOSCSICcw), 936 .instance_init = vhost_ccw_scsi_instance_init, 937 .class_init = vhost_ccw_scsi_class_init, 938 }; 939 #endif 940 941 static void virtio_ccw_rng_instance_init(Object *obj) 942 { 943 VirtIORNGCcw *dev = VIRTIO_RNG_CCW(obj); 944 object_initialize(OBJECT(&dev->vdev), TYPE_VIRTIO_RNG); 945 object_property_add_child(obj, "virtio-backend", OBJECT(&dev->vdev), NULL); 946 object_property_add_link(obj, "rng", TYPE_RNG_BACKEND, 947 (Object **)&dev->vdev.conf.rng, NULL); 948 } 949 950 static Property virtio_ccw_rng_properties[] = { 951 DEFINE_PROP_STRING("devno", VirtioCcwDevice, bus_id), 952 DEFINE_VIRTIO_COMMON_FEATURES(VirtioCcwDevice, host_features[0]), 953 DEFINE_VIRTIO_RNG_PROPERTIES(VirtIORNGCcw, vdev.conf), 954 DEFINE_PROP_END_OF_LIST(), 955 }; 956 957 static void virtio_ccw_rng_class_init(ObjectClass *klass, void *data) 958 { 959 DeviceClass *dc = DEVICE_CLASS(klass); 960 VirtIOCCWDeviceClass *k = VIRTIO_CCW_DEVICE_CLASS(klass); 961 962 k->init = virtio_ccw_rng_init; 963 k->exit = virtio_ccw_exit; 964 dc->reset = virtio_ccw_reset; 965 dc->props = virtio_ccw_rng_properties; 966 } 967 968 static const TypeInfo virtio_ccw_rng = { 969 .name = TYPE_VIRTIO_RNG_CCW, 970 .parent = TYPE_VIRTIO_CCW_DEVICE, 971 .instance_size = sizeof(VirtIORNGCcw), 972 .instance_init = virtio_ccw_rng_instance_init, 973 .class_init = virtio_ccw_rng_class_init, 974 }; 975 976 static int virtio_ccw_busdev_init(DeviceState *dev) 977 { 978 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; 979 VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev); 980 981 virtio_ccw_bus_new(&_dev->bus, _dev); 982 983 return _info->init(_dev); 984 } 985 986 static int virtio_ccw_busdev_exit(DeviceState *dev) 987 { 988 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; 989 VirtIOCCWDeviceClass *_info = VIRTIO_CCW_DEVICE_GET_CLASS(dev); 990 991 return _info->exit(_dev); 992 } 993 994 static int virtio_ccw_busdev_unplug(DeviceState *dev) 995 { 996 VirtioCcwDevice *_dev = (VirtioCcwDevice *)dev; 997 SubchDev *sch = _dev->sch; 998 999 /* 1000 * We should arrive here only for device_del, since we don't support 1001 * direct hot(un)plug of channels, but only through virtio. 1002 */ 1003 assert(sch != NULL); 1004 /* Subchannel is now disabled and no longer valid. */ 1005 sch->curr_status.pmcw.flags &= ~(PMCW_FLAGS_MASK_ENA | 1006 PMCW_FLAGS_MASK_DNV); 1007 1008 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid, 1, 0); 1009 1010 qdev_free(dev); 1011 return 0; 1012 } 1013 1014 static void virtio_ccw_device_class_init(ObjectClass *klass, void *data) 1015 { 1016 DeviceClass *dc = DEVICE_CLASS(klass); 1017 1018 dc->init = virtio_ccw_busdev_init; 1019 dc->exit = virtio_ccw_busdev_exit; 1020 dc->unplug = virtio_ccw_busdev_unplug; 1021 dc->bus_type = TYPE_VIRTUAL_CSS_BUS; 1022 1023 } 1024 1025 static const TypeInfo virtio_ccw_device_info = { 1026 .name = TYPE_VIRTIO_CCW_DEVICE, 1027 .parent = TYPE_DEVICE, 1028 .instance_size = sizeof(VirtioCcwDevice), 1029 .class_init = virtio_ccw_device_class_init, 1030 .class_size = sizeof(VirtIOCCWDeviceClass), 1031 .abstract = true, 1032 }; 1033 1034 /***************** Virtual-css Bus Bridge Device ********************/ 1035 /* Only required to have the virtio bus as child in the system bus */ 1036 1037 static int virtual_css_bridge_init(SysBusDevice *dev) 1038 { 1039 /* nothing */ 1040 return 0; 1041 } 1042 1043 static void virtual_css_bridge_class_init(ObjectClass *klass, void *data) 1044 { 1045 DeviceClass *dc = DEVICE_CLASS(klass); 1046 SysBusDeviceClass *k = SYS_BUS_DEVICE_CLASS(klass); 1047 1048 k->init = virtual_css_bridge_init; 1049 dc->no_user = 1; 1050 } 1051 1052 static const TypeInfo virtual_css_bridge_info = { 1053 .name = "virtual-css-bridge", 1054 .parent = TYPE_SYS_BUS_DEVICE, 1055 .instance_size = sizeof(SysBusDevice), 1056 .class_init = virtual_css_bridge_class_init, 1057 }; 1058 1059 /* virtio-ccw-bus */ 1060 1061 static void virtio_ccw_bus_new(VirtioBusState *bus, VirtioCcwDevice *dev) 1062 { 1063 DeviceState *qdev = DEVICE(dev); 1064 BusState *qbus; 1065 1066 qbus_create_inplace((BusState *)bus, TYPE_VIRTIO_CCW_BUS, qdev, NULL); 1067 qbus = BUS(bus); 1068 qbus->allow_hotplug = 1; 1069 } 1070 1071 static void virtio_ccw_bus_class_init(ObjectClass *klass, void *data) 1072 { 1073 VirtioBusClass *k = VIRTIO_BUS_CLASS(klass); 1074 BusClass *bus_class = BUS_CLASS(klass); 1075 1076 bus_class->max_dev = 1; 1077 k->notify = virtio_ccw_notify; 1078 k->get_features = virtio_ccw_get_features; 1079 } 1080 1081 static const TypeInfo virtio_ccw_bus_info = { 1082 .name = TYPE_VIRTIO_CCW_BUS, 1083 .parent = TYPE_VIRTIO_BUS, 1084 .instance_size = sizeof(VirtioCcwBusState), 1085 .class_init = virtio_ccw_bus_class_init, 1086 }; 1087 1088 static void virtio_ccw_register(void) 1089 { 1090 type_register_static(&virtio_ccw_bus_info); 1091 type_register_static(&virtual_css_bus_info); 1092 type_register_static(&virtio_ccw_device_info); 1093 type_register_static(&virtio_ccw_serial); 1094 type_register_static(&virtio_ccw_blk); 1095 type_register_static(&virtio_ccw_net); 1096 type_register_static(&virtio_ccw_balloon); 1097 type_register_static(&virtio_ccw_scsi); 1098 #ifdef CONFIG_VHOST_SCSI 1099 type_register_static(&vhost_ccw_scsi); 1100 #endif 1101 type_register_static(&virtio_ccw_rng); 1102 type_register_static(&virtual_css_bridge_info); 1103 } 1104 1105 type_init(virtio_ccw_register) 1106