1 /* 2 * vfio based subchannel assignment support 3 * 4 * Copyright 2017 IBM Corp. 5 * Copyright 2019 Red Hat, Inc. 6 * 7 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com> 8 * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com> 9 * Pierre Morel <pmorel@linux.vnet.ibm.com> 10 * Cornelia Huck <cohuck@redhat.com> 11 * 12 * This work is licensed under the terms of the GNU GPL, version 2 or (at 13 * your option) any later version. See the COPYING file in the top-level 14 * directory. 15 */ 16 17 #include "qemu/osdep.h" 18 #include <linux/vfio.h> 19 #include <linux/vfio_ccw.h> 20 #include <sys/ioctl.h> 21 22 #include "qapi/error.h" 23 #include "hw/vfio/vfio.h" 24 #include "hw/vfio/vfio-common.h" 25 #include "hw/s390x/s390-ccw.h" 26 #include "hw/s390x/vfio-ccw.h" 27 #include "hw/qdev-properties.h" 28 #include "hw/s390x/ccw-device.h" 29 #include "exec/address-spaces.h" 30 #include "qemu/error-report.h" 31 #include "qemu/main-loop.h" 32 #include "qemu/module.h" 33 34 struct VFIOCCWDevice { 35 S390CCWDevice cdev; 36 VFIODevice vdev; 37 uint64_t io_region_size; 38 uint64_t io_region_offset; 39 struct ccw_io_region *io_region; 40 uint64_t async_cmd_region_size; 41 uint64_t async_cmd_region_offset; 42 struct ccw_cmd_region *async_cmd_region; 43 uint64_t schib_region_size; 44 uint64_t schib_region_offset; 45 struct ccw_schib_region *schib_region; 46 uint64_t crw_region_size; 47 uint64_t crw_region_offset; 48 struct ccw_crw_region *crw_region; 49 EventNotifier io_notifier; 50 EventNotifier crw_notifier; 51 EventNotifier req_notifier; 52 bool force_orb_pfch; 53 bool warned_orb_pfch; 54 }; 55 56 static inline void warn_once_pfch(VFIOCCWDevice *vcdev, SubchDev *sch, 57 const char *msg) 58 { 59 warn_report_once_cond(&vcdev->warned_orb_pfch, 60 "vfio-ccw (devno %x.%x.%04x): %s", 61 sch->cssid, sch->ssid, sch->devno, msg); 62 } 63 64 static void vfio_ccw_compute_needs_reset(VFIODevice *vdev) 65 { 66 vdev->needs_reset = false; 67 } 68 69 /* 70 * We don't need vfio_hot_reset_multi and vfio_eoi operations for 71 * vfio_ccw device now. 72 */ 73 struct VFIODeviceOps vfio_ccw_ops = { 74 .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset, 75 }; 76 77 static IOInstEnding vfio_ccw_handle_request(SubchDev *sch) 78 { 79 S390CCWDevice *cdev = sch->driver_data; 80 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); 81 struct ccw_io_region *region = vcdev->io_region; 82 int ret; 83 84 if (!(sch->orb.ctrl0 & ORB_CTRL0_MASK_PFCH) && vcdev->force_orb_pfch) { 85 sch->orb.ctrl0 |= ORB_CTRL0_MASK_PFCH; 86 warn_once_pfch(vcdev, sch, "PFCH flag forced"); 87 } 88 89 QEMU_BUILD_BUG_ON(sizeof(region->orb_area) != sizeof(ORB)); 90 QEMU_BUILD_BUG_ON(sizeof(region->scsw_area) != sizeof(SCSW)); 91 QEMU_BUILD_BUG_ON(sizeof(region->irb_area) != sizeof(IRB)); 92 93 memset(region, 0, sizeof(*region)); 94 95 memcpy(region->orb_area, &sch->orb, sizeof(ORB)); 96 memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW)); 97 98 again: 99 ret = pwrite(vcdev->vdev.fd, region, 100 vcdev->io_region_size, vcdev->io_region_offset); 101 if (ret != vcdev->io_region_size) { 102 if (errno == EAGAIN) { 103 goto again; 104 } 105 error_report("vfio-ccw: write I/O region failed with errno=%d", errno); 106 ret = errno ? -errno : -EFAULT; 107 } else { 108 ret = 0; 109 } 110 switch (ret) { 111 case 0: 112 return IOINST_CC_EXPECTED; 113 case -EBUSY: 114 return IOINST_CC_BUSY; 115 case -ENODEV: 116 case -EACCES: 117 return IOINST_CC_NOT_OPERATIONAL; 118 case -EFAULT: 119 default: 120 sch_gen_unit_exception(sch); 121 css_inject_io_interrupt(sch); 122 return IOINST_CC_EXPECTED; 123 } 124 } 125 126 static IOInstEnding vfio_ccw_handle_store(SubchDev *sch) 127 { 128 S390CCWDevice *cdev = sch->driver_data; 129 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); 130 SCHIB *schib = &sch->curr_status; 131 struct ccw_schib_region *region = vcdev->schib_region; 132 SCHIB *s; 133 int ret; 134 135 /* schib region not available so nothing else to do */ 136 if (!region) { 137 return IOINST_CC_EXPECTED; 138 } 139 140 memset(region, 0, sizeof(*region)); 141 ret = pread(vcdev->vdev.fd, region, vcdev->schib_region_size, 142 vcdev->schib_region_offset); 143 144 if (ret == -1) { 145 /* 146 * Device is probably damaged, but store subchannel does not 147 * have a nonzero cc defined for this scenario. Log an error, 148 * and presume things are otherwise fine. 149 */ 150 error_report("vfio-ccw: store region read failed with errno=%d", errno); 151 return IOINST_CC_EXPECTED; 152 } 153 154 /* 155 * Selectively copy path-related bits of the SCHIB, 156 * rather than copying the entire struct. 157 */ 158 s = (SCHIB *)region->schib_area; 159 schib->pmcw.pnom = s->pmcw.pnom; 160 schib->pmcw.lpum = s->pmcw.lpum; 161 schib->pmcw.pam = s->pmcw.pam; 162 schib->pmcw.pom = s->pmcw.pom; 163 164 if (s->scsw.flags & SCSW_FLAGS_MASK_PNO) { 165 schib->scsw.flags |= SCSW_FLAGS_MASK_PNO; 166 } 167 168 return IOINST_CC_EXPECTED; 169 } 170 171 static int vfio_ccw_handle_clear(SubchDev *sch) 172 { 173 S390CCWDevice *cdev = sch->driver_data; 174 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); 175 struct ccw_cmd_region *region = vcdev->async_cmd_region; 176 int ret; 177 178 if (!vcdev->async_cmd_region) { 179 /* Async command region not available, fall back to emulation */ 180 return -ENOSYS; 181 } 182 183 memset(region, 0, sizeof(*region)); 184 region->command = VFIO_CCW_ASYNC_CMD_CSCH; 185 186 again: 187 ret = pwrite(vcdev->vdev.fd, region, 188 vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset); 189 if (ret != vcdev->async_cmd_region_size) { 190 if (errno == EAGAIN) { 191 goto again; 192 } 193 error_report("vfio-ccw: write cmd region failed with errno=%d", errno); 194 ret = errno ? -errno : -EFAULT; 195 } else { 196 ret = 0; 197 } 198 switch (ret) { 199 case 0: 200 case -ENODEV: 201 case -EACCES: 202 return 0; 203 case -EFAULT: 204 default: 205 sch_gen_unit_exception(sch); 206 css_inject_io_interrupt(sch); 207 return 0; 208 } 209 } 210 211 static int vfio_ccw_handle_halt(SubchDev *sch) 212 { 213 S390CCWDevice *cdev = sch->driver_data; 214 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); 215 struct ccw_cmd_region *region = vcdev->async_cmd_region; 216 int ret; 217 218 if (!vcdev->async_cmd_region) { 219 /* Async command region not available, fall back to emulation */ 220 return -ENOSYS; 221 } 222 223 memset(region, 0, sizeof(*region)); 224 region->command = VFIO_CCW_ASYNC_CMD_HSCH; 225 226 again: 227 ret = pwrite(vcdev->vdev.fd, region, 228 vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset); 229 if (ret != vcdev->async_cmd_region_size) { 230 if (errno == EAGAIN) { 231 goto again; 232 } 233 error_report("vfio-ccw: write cmd region failed with errno=%d", errno); 234 ret = errno ? -errno : -EFAULT; 235 } else { 236 ret = 0; 237 } 238 switch (ret) { 239 case 0: 240 case -EBUSY: 241 case -ENODEV: 242 case -EACCES: 243 return 0; 244 case -EFAULT: 245 default: 246 sch_gen_unit_exception(sch); 247 css_inject_io_interrupt(sch); 248 return 0; 249 } 250 } 251 252 static void vfio_ccw_reset(DeviceState *dev) 253 { 254 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev); 255 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev); 256 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); 257 258 ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET); 259 } 260 261 static void vfio_ccw_crw_read(VFIOCCWDevice *vcdev) 262 { 263 struct ccw_crw_region *region = vcdev->crw_region; 264 CRW crw; 265 int size; 266 267 /* Keep reading CRWs as long as data is returned */ 268 do { 269 memset(region, 0, sizeof(*region)); 270 size = pread(vcdev->vdev.fd, region, vcdev->crw_region_size, 271 vcdev->crw_region_offset); 272 273 if (size == -1) { 274 error_report("vfio-ccw: Read crw region failed with errno=%d", 275 errno); 276 break; 277 } 278 279 if (region->crw == 0) { 280 /* No more CRWs to queue */ 281 break; 282 } 283 284 memcpy(&crw, ®ion->crw, sizeof(CRW)); 285 286 css_crw_add_to_queue(crw); 287 } while (1); 288 } 289 290 static void vfio_ccw_req_notifier_handler(void *opaque) 291 { 292 VFIOCCWDevice *vcdev = opaque; 293 Error *err = NULL; 294 295 if (!event_notifier_test_and_clear(&vcdev->req_notifier)) { 296 return; 297 } 298 299 qdev_unplug(DEVICE(vcdev), &err); 300 if (err) { 301 warn_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name); 302 } 303 } 304 305 static void vfio_ccw_crw_notifier_handler(void *opaque) 306 { 307 VFIOCCWDevice *vcdev = opaque; 308 309 while (event_notifier_test_and_clear(&vcdev->crw_notifier)) { 310 vfio_ccw_crw_read(vcdev); 311 } 312 } 313 314 static void vfio_ccw_io_notifier_handler(void *opaque) 315 { 316 VFIOCCWDevice *vcdev = opaque; 317 struct ccw_io_region *region = vcdev->io_region; 318 S390CCWDevice *cdev = S390_CCW_DEVICE(vcdev); 319 CcwDevice *ccw_dev = CCW_DEVICE(cdev); 320 SubchDev *sch = ccw_dev->sch; 321 SCHIB *schib = &sch->curr_status; 322 SCSW s; 323 IRB irb; 324 int size; 325 326 if (!event_notifier_test_and_clear(&vcdev->io_notifier)) { 327 return; 328 } 329 330 size = pread(vcdev->vdev.fd, region, vcdev->io_region_size, 331 vcdev->io_region_offset); 332 if (size == -1) { 333 switch (errno) { 334 case ENODEV: 335 /* Generate a deferred cc 3 condition. */ 336 schib->scsw.flags |= SCSW_FLAGS_MASK_CC; 337 schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; 338 schib->scsw.ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND); 339 goto read_err; 340 case EFAULT: 341 /* Memory problem, generate channel data check. */ 342 schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; 343 schib->scsw.cstat = SCSW_CSTAT_DATA_CHECK; 344 schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; 345 schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | 346 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; 347 goto read_err; 348 default: 349 /* Error, generate channel program check. */ 350 schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; 351 schib->scsw.cstat = SCSW_CSTAT_PROG_CHECK; 352 schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; 353 schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | 354 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; 355 goto read_err; 356 } 357 } else if (size != vcdev->io_region_size) { 358 /* Information transfer error, generate channel-control check. */ 359 schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; 360 schib->scsw.cstat = SCSW_CSTAT_CHN_CTRL_CHK; 361 schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; 362 schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | 363 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; 364 goto read_err; 365 } 366 367 memcpy(&irb, region->irb_area, sizeof(IRB)); 368 369 /* Update control block via irb. */ 370 s = schib->scsw; 371 copy_scsw_to_guest(&s, &irb.scsw); 372 schib->scsw = s; 373 374 /* If a uint check is pending, copy sense data. */ 375 if ((schib->scsw.dstat & SCSW_DSTAT_UNIT_CHECK) && 376 (schib->pmcw.chars & PMCW_CHARS_MASK_CSENSE)) { 377 memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw)); 378 } 379 380 read_err: 381 css_inject_io_interrupt(sch); 382 } 383 384 static void vfio_ccw_register_irq_notifier(VFIOCCWDevice *vcdev, 385 unsigned int irq, 386 Error **errp) 387 { 388 VFIODevice *vdev = &vcdev->vdev; 389 struct vfio_irq_info *irq_info; 390 size_t argsz; 391 int fd; 392 EventNotifier *notifier; 393 IOHandler *fd_read; 394 395 switch (irq) { 396 case VFIO_CCW_IO_IRQ_INDEX: 397 notifier = &vcdev->io_notifier; 398 fd_read = vfio_ccw_io_notifier_handler; 399 break; 400 case VFIO_CCW_CRW_IRQ_INDEX: 401 notifier = &vcdev->crw_notifier; 402 fd_read = vfio_ccw_crw_notifier_handler; 403 break; 404 case VFIO_CCW_REQ_IRQ_INDEX: 405 notifier = &vcdev->req_notifier; 406 fd_read = vfio_ccw_req_notifier_handler; 407 break; 408 default: 409 error_setg(errp, "vfio: Unsupported device irq(%d)", irq); 410 return; 411 } 412 413 if (vdev->num_irqs < irq + 1) { 414 error_setg(errp, "vfio: unexpected number of irqs %u", 415 vdev->num_irqs); 416 return; 417 } 418 419 argsz = sizeof(*irq_info); 420 irq_info = g_malloc0(argsz); 421 irq_info->index = irq; 422 irq_info->argsz = argsz; 423 if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO, 424 irq_info) < 0 || irq_info->count < 1) { 425 error_setg_errno(errp, errno, "vfio: Error getting irq info"); 426 goto out_free_info; 427 } 428 429 if (event_notifier_init(notifier, 0)) { 430 error_setg_errno(errp, errno, 431 "vfio: Unable to init event notifier for irq (%d)", 432 irq); 433 goto out_free_info; 434 } 435 436 fd = event_notifier_get_fd(notifier); 437 qemu_set_fd_handler(fd, fd_read, NULL, vcdev); 438 439 if (vfio_set_irq_signaling(vdev, irq, 0, 440 VFIO_IRQ_SET_ACTION_TRIGGER, fd, errp)) { 441 qemu_set_fd_handler(fd, NULL, NULL, vcdev); 442 event_notifier_cleanup(notifier); 443 } 444 445 out_free_info: 446 g_free(irq_info); 447 } 448 449 static void vfio_ccw_unregister_irq_notifier(VFIOCCWDevice *vcdev, 450 unsigned int irq) 451 { 452 Error *err = NULL; 453 EventNotifier *notifier; 454 455 switch (irq) { 456 case VFIO_CCW_IO_IRQ_INDEX: 457 notifier = &vcdev->io_notifier; 458 break; 459 case VFIO_CCW_CRW_IRQ_INDEX: 460 notifier = &vcdev->crw_notifier; 461 break; 462 case VFIO_CCW_REQ_IRQ_INDEX: 463 notifier = &vcdev->req_notifier; 464 break; 465 default: 466 error_report("vfio: Unsupported device irq(%d)", irq); 467 return; 468 } 469 470 if (vfio_set_irq_signaling(&vcdev->vdev, irq, 0, 471 VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) { 472 error_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name); 473 } 474 475 qemu_set_fd_handler(event_notifier_get_fd(notifier), 476 NULL, NULL, vcdev); 477 event_notifier_cleanup(notifier); 478 } 479 480 static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp) 481 { 482 VFIODevice *vdev = &vcdev->vdev; 483 struct vfio_region_info *info; 484 int ret; 485 486 /* Sanity check device */ 487 if (!(vdev->flags & VFIO_DEVICE_FLAGS_CCW)) { 488 error_setg(errp, "vfio: Um, this isn't a vfio-ccw device"); 489 return; 490 } 491 492 /* 493 * We always expect at least the I/O region to be present. We also 494 * may have a variable number of regions governed by capabilities. 495 */ 496 if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) { 497 error_setg(errp, "vfio: too few regions (%u), expected at least %u", 498 vdev->num_regions, VFIO_CCW_CONFIG_REGION_INDEX + 1); 499 return; 500 } 501 502 ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info); 503 if (ret) { 504 error_setg_errno(errp, -ret, "vfio: Error getting config info"); 505 return; 506 } 507 508 vcdev->io_region_size = info->size; 509 if (sizeof(*vcdev->io_region) != vcdev->io_region_size) { 510 error_setg(errp, "vfio: Unexpected size of the I/O region"); 511 goto out_err; 512 } 513 514 vcdev->io_region_offset = info->offset; 515 vcdev->io_region = g_malloc0(info->size); 516 g_free(info); 517 518 /* check for the optional async command region */ 519 ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, 520 VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD, &info); 521 if (!ret) { 522 vcdev->async_cmd_region_size = info->size; 523 if (sizeof(*vcdev->async_cmd_region) != vcdev->async_cmd_region_size) { 524 error_setg(errp, "vfio: Unexpected size of the async cmd region"); 525 goto out_err; 526 } 527 vcdev->async_cmd_region_offset = info->offset; 528 vcdev->async_cmd_region = g_malloc0(info->size); 529 g_free(info); 530 } 531 532 ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, 533 VFIO_REGION_SUBTYPE_CCW_SCHIB, &info); 534 if (!ret) { 535 vcdev->schib_region_size = info->size; 536 if (sizeof(*vcdev->schib_region) != vcdev->schib_region_size) { 537 error_setg(errp, "vfio: Unexpected size of the schib region"); 538 goto out_err; 539 } 540 vcdev->schib_region_offset = info->offset; 541 vcdev->schib_region = g_malloc(info->size); 542 g_free(info); 543 } 544 545 ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW, 546 VFIO_REGION_SUBTYPE_CCW_CRW, &info); 547 548 if (!ret) { 549 vcdev->crw_region_size = info->size; 550 if (sizeof(*vcdev->crw_region) != vcdev->crw_region_size) { 551 error_setg(errp, "vfio: Unexpected size of the CRW region"); 552 goto out_err; 553 } 554 vcdev->crw_region_offset = info->offset; 555 vcdev->crw_region = g_malloc(info->size); 556 g_free(info); 557 } 558 559 return; 560 561 out_err: 562 g_free(vcdev->crw_region); 563 g_free(vcdev->schib_region); 564 g_free(vcdev->async_cmd_region); 565 g_free(vcdev->io_region); 566 g_free(info); 567 return; 568 } 569 570 static void vfio_ccw_put_region(VFIOCCWDevice *vcdev) 571 { 572 g_free(vcdev->crw_region); 573 g_free(vcdev->schib_region); 574 g_free(vcdev->async_cmd_region); 575 g_free(vcdev->io_region); 576 } 577 578 static void vfio_ccw_put_device(VFIOCCWDevice *vcdev) 579 { 580 g_free(vcdev->vdev.name); 581 vfio_put_base_device(&vcdev->vdev); 582 } 583 584 static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev, 585 Error **errp) 586 { 587 char *name = g_strdup_printf("%x.%x.%04x", vcdev->cdev.hostid.cssid, 588 vcdev->cdev.hostid.ssid, 589 vcdev->cdev.hostid.devid); 590 VFIODevice *vbasedev; 591 592 QLIST_FOREACH(vbasedev, &group->device_list, next) { 593 if (strcmp(vbasedev->name, name) == 0) { 594 error_setg(errp, "vfio: subchannel %s has already been attached", 595 name); 596 goto out_err; 597 } 598 } 599 600 /* 601 * All vfio-ccw devices are believed to operate in a way compatible with 602 * discarding of memory in RAM blocks, ie. pages pinned in the host are 603 * in the current working set of the guest driver and therefore never 604 * overlap e.g., with pages available to the guest balloon driver. This 605 * needs to be set before vfio_get_device() for vfio common to handle 606 * ram_block_discard_disable(). 607 */ 608 vcdev->vdev.ram_block_discard_allowed = true; 609 610 if (vfio_get_device(group, vcdev->cdev.mdevid, &vcdev->vdev, errp)) { 611 goto out_err; 612 } 613 614 vcdev->vdev.ops = &vfio_ccw_ops; 615 vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW; 616 vcdev->vdev.name = name; 617 vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj; 618 619 return; 620 621 out_err: 622 g_free(name); 623 } 624 625 static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp) 626 { 627 char *tmp, group_path[PATH_MAX]; 628 ssize_t len; 629 int groupid; 630 631 tmp = g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group", 632 cdev->hostid.cssid, cdev->hostid.ssid, 633 cdev->hostid.devid, cdev->mdevid); 634 len = readlink(tmp, group_path, sizeof(group_path)); 635 g_free(tmp); 636 637 if (len <= 0 || len >= sizeof(group_path)) { 638 error_setg(errp, "vfio: no iommu_group found"); 639 return NULL; 640 } 641 642 group_path[len] = 0; 643 644 if (sscanf(basename(group_path), "%d", &groupid) != 1) { 645 error_setg(errp, "vfio: failed to read %s", group_path); 646 return NULL; 647 } 648 649 return vfio_get_group(groupid, &address_space_memory, errp); 650 } 651 652 static void vfio_ccw_realize(DeviceState *dev, Error **errp) 653 { 654 VFIOGroup *group; 655 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev); 656 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev); 657 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); 658 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); 659 Error *err = NULL; 660 661 /* Call the class init function for subchannel. */ 662 if (cdc->realize) { 663 cdc->realize(cdev, vcdev->vdev.sysfsdev, &err); 664 if (err) { 665 goto out_err_propagate; 666 } 667 } 668 669 group = vfio_ccw_get_group(cdev, &err); 670 if (!group) { 671 goto out_group_err; 672 } 673 674 vfio_ccw_get_device(group, vcdev, &err); 675 if (err) { 676 goto out_device_err; 677 } 678 679 vfio_ccw_get_region(vcdev, &err); 680 if (err) { 681 goto out_region_err; 682 } 683 684 vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX, &err); 685 if (err) { 686 goto out_io_notifier_err; 687 } 688 689 if (vcdev->crw_region) { 690 vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX, &err); 691 if (err) { 692 goto out_crw_notifier_err; 693 } 694 } 695 696 vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX, &err); 697 if (err) { 698 goto out_req_notifier_err; 699 } 700 701 return; 702 703 out_req_notifier_err: 704 vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX); 705 out_crw_notifier_err: 706 vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX); 707 out_io_notifier_err: 708 vfio_ccw_put_region(vcdev); 709 out_region_err: 710 vfio_ccw_put_device(vcdev); 711 out_device_err: 712 vfio_put_group(group); 713 out_group_err: 714 if (cdc->unrealize) { 715 cdc->unrealize(cdev); 716 } 717 out_err_propagate: 718 error_propagate(errp, err); 719 } 720 721 static void vfio_ccw_unrealize(DeviceState *dev) 722 { 723 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev); 724 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev); 725 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev); 726 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); 727 VFIOGroup *group = vcdev->vdev.group; 728 729 vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX); 730 vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX); 731 vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX); 732 vfio_ccw_put_region(vcdev); 733 vfio_ccw_put_device(vcdev); 734 vfio_put_group(group); 735 736 if (cdc->unrealize) { 737 cdc->unrealize(cdev); 738 } 739 } 740 741 static Property vfio_ccw_properties[] = { 742 DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev), 743 DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice, force_orb_pfch, false), 744 DEFINE_PROP_END_OF_LIST(), 745 }; 746 747 static const VMStateDescription vfio_ccw_vmstate = { 748 .name = "vfio-ccw", 749 .unmigratable = 1, 750 }; 751 752 static void vfio_ccw_class_init(ObjectClass *klass, void *data) 753 { 754 DeviceClass *dc = DEVICE_CLASS(klass); 755 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass); 756 757 device_class_set_props(dc, vfio_ccw_properties); 758 dc->vmsd = &vfio_ccw_vmstate; 759 dc->desc = "VFIO-based subchannel assignment"; 760 set_bit(DEVICE_CATEGORY_MISC, dc->categories); 761 dc->realize = vfio_ccw_realize; 762 dc->unrealize = vfio_ccw_unrealize; 763 dc->reset = vfio_ccw_reset; 764 765 cdc->handle_request = vfio_ccw_handle_request; 766 cdc->handle_halt = vfio_ccw_handle_halt; 767 cdc->handle_clear = vfio_ccw_handle_clear; 768 cdc->handle_store = vfio_ccw_handle_store; 769 } 770 771 static const TypeInfo vfio_ccw_info = { 772 .name = TYPE_VFIO_CCW, 773 .parent = TYPE_S390_CCW, 774 .instance_size = sizeof(VFIOCCWDevice), 775 .class_init = vfio_ccw_class_init, 776 }; 777 778 static void register_vfio_ccw_type(void) 779 { 780 type_register_static(&vfio_ccw_info); 781 } 782 783 type_init(register_vfio_ccw_type) 784