| 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-device.h" |
| 24 | #include "system/iommufd.h" |
| 25 | #include "hw/s390x/s390-ccw.h" |
| 26 | #include "hw/s390x/vfio-ccw.h" |
| 27 | #include "hw/core/qdev-properties.h" |
| 28 | #include "hw/s390x/ccw-device.h" |
| 29 | #include "system/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 | }; |
| 54 | |
| 55 | static void vfio_ccw_compute_needs_reset(VFIODevice *vdev) |
| 56 | { |
| 57 | vdev->needs_reset = false; |
| 58 | } |
| 59 | |
| 60 | /* |
| 61 | * We don't need vfio_hot_reset_multi and vfio_eoi operations for |
| 62 | * vfio_ccw device now. |
| 63 | */ |
| 64 | struct VFIODeviceOps vfio_ccw_ops = { |
| 65 | .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset, |
| 66 | }; |
| 67 | |
| 68 | static IOInstEnding vfio_ccw_handle_request(SubchDev *sch) |
| 69 | { |
| 70 | VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data); |
| 71 | struct ccw_io_region *region = vcdev->io_region; |
| 72 | int ret; |
| 73 | |
| 74 | if (!(sch->orb.ctrl0 & ORB_CTRL0_MASK_PFCH) && vcdev->force_orb_pfch) { |
| 75 | sch->orb.ctrl0 |= ORB_CTRL0_MASK_PFCH; |
| 76 | warn_report_once("vfio-ccw (devno %x.%x.%04x): PFCH flag forced", |
| 77 | sch->cssid, sch->ssid, sch->devno); |
| 78 | } |
| 79 | |
| 80 | QEMU_BUILD_BUG_ON(sizeof(region->orb_area) != sizeof(ORB)); |
| 81 | QEMU_BUILD_BUG_ON(sizeof(region->scsw_area) != sizeof(SCSW)); |
| 82 | QEMU_BUILD_BUG_ON(sizeof(region->irb_area) != sizeof(IRB)); |
| 83 | |
| 84 | memset(region, 0, sizeof(*region)); |
| 85 | |
| 86 | memcpy(region->orb_area, &sch->orb, sizeof(ORB)); |
| 87 | memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW)); |
| 88 | |
| 89 | again: |
| 90 | ret = pwrite(vcdev->vdev.fd, region, |
| 91 | vcdev->io_region_size, vcdev->io_region_offset); |
| 92 | if (ret != vcdev->io_region_size) { |
| 93 | if (errno == EAGAIN) { |
| 94 | goto again; |
| 95 | } |
| 96 | error_report("vfio-ccw: write I/O region failed with errno=%d", errno); |
| 97 | ret = errno ? -errno : -EFAULT; |
| 98 | } else { |
| 99 | ret = 0; |
| 100 | } |
| 101 | switch (ret) { |
| 102 | case 0: |
| 103 | return IOINST_CC_EXPECTED; |
| 104 | case -EBUSY: |
| 105 | return IOINST_CC_BUSY; |
| 106 | case -ENODEV: |
| 107 | case -EACCES: |
| 108 | return IOINST_CC_NOT_OPERATIONAL; |
| 109 | case -EFAULT: |
| 110 | default: |
| 111 | sch_gen_unit_exception(sch); |
| 112 | css_inject_io_interrupt(sch); |
| 113 | return IOINST_CC_EXPECTED; |
| 114 | } |
| 115 | } |
| 116 | |
| 117 | static IOInstEnding vfio_ccw_handle_store(SubchDev *sch) |
| 118 | { |
| 119 | VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data); |
| 120 | SCHIB *schib = &sch->curr_status; |
| 121 | struct ccw_schib_region *region = vcdev->schib_region; |
| 122 | SCHIB *s; |
| 123 | int ret; |
| 124 | |
| 125 | /* schib region not available so nothing else to do */ |
| 126 | if (!region) { |
| 127 | return IOINST_CC_EXPECTED; |
| 128 | } |
| 129 | |
| 130 | memset(region, 0, sizeof(*region)); |
| 131 | ret = pread(vcdev->vdev.fd, region, vcdev->schib_region_size, |
| 132 | vcdev->schib_region_offset); |
| 133 | |
| 134 | if (ret == -1) { |
| 135 | /* |
| 136 | * Device is probably damaged, but store subchannel does not |
| 137 | * have a nonzero cc defined for this scenario. Log an error, |
| 138 | * and presume things are otherwise fine. |
| 139 | */ |
| 140 | error_report("vfio-ccw: store region read failed with errno=%d", errno); |
| 141 | return IOINST_CC_EXPECTED; |
| 142 | } |
| 143 | |
| 144 | /* |
| 145 | * Selectively copy path-related bits of the SCHIB, |
| 146 | * rather than copying the entire struct. |
| 147 | */ |
| 148 | s = (SCHIB *)region->schib_area; |
| 149 | schib->pmcw.pnom = s->pmcw.pnom; |
| 150 | schib->pmcw.lpum = s->pmcw.lpum; |
| 151 | schib->pmcw.pam = s->pmcw.pam; |
| 152 | schib->pmcw.pom = s->pmcw.pom; |
| 153 | |
| 154 | if (s->scsw.flags & SCSW_FLAGS_MASK_PNO) { |
| 155 | schib->scsw.flags |= SCSW_FLAGS_MASK_PNO; |
| 156 | } |
| 157 | |
| 158 | return IOINST_CC_EXPECTED; |
| 159 | } |
| 160 | |
| 161 | static int vfio_ccw_handle_clear(SubchDev *sch) |
| 162 | { |
| 163 | VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data); |
| 164 | struct ccw_cmd_region *region = vcdev->async_cmd_region; |
| 165 | int ret; |
| 166 | |
| 167 | if (!vcdev->async_cmd_region) { |
| 168 | /* Async command region not available, fall back to emulation */ |
| 169 | return -ENOSYS; |
| 170 | } |
| 171 | |
| 172 | memset(region, 0, sizeof(*region)); |
| 173 | region->command = VFIO_CCW_ASYNC_CMD_CSCH; |
| 174 | |
| 175 | again: |
| 176 | ret = pwrite(vcdev->vdev.fd, region, |
| 177 | vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset); |
| 178 | if (ret != vcdev->async_cmd_region_size) { |
| 179 | if (errno == EAGAIN) { |
| 180 | goto again; |
| 181 | } |
| 182 | error_report("vfio-ccw: write cmd region failed with errno=%d", errno); |
| 183 | ret = errno ? -errno : -EFAULT; |
| 184 | } else { |
| 185 | ret = 0; |
| 186 | } |
| 187 | switch (ret) { |
| 188 | case 0: |
| 189 | case -ENODEV: |
| 190 | case -EACCES: |
| 191 | return ret; |
| 192 | case -EFAULT: |
| 193 | default: |
| 194 | sch_gen_unit_exception(sch); |
| 195 | css_inject_io_interrupt(sch); |
| 196 | return 0; |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | static int vfio_ccw_handle_halt(SubchDev *sch) |
| 201 | { |
| 202 | VFIOCCWDevice *vcdev = VFIO_CCW(sch->driver_data); |
| 203 | struct ccw_cmd_region *region = vcdev->async_cmd_region; |
| 204 | int ret; |
| 205 | |
| 206 | if (!vcdev->async_cmd_region) { |
| 207 | /* Async command region not available, fall back to emulation */ |
| 208 | return -ENOSYS; |
| 209 | } |
| 210 | |
| 211 | memset(region, 0, sizeof(*region)); |
| 212 | region->command = VFIO_CCW_ASYNC_CMD_HSCH; |
| 213 | |
| 214 | again: |
| 215 | ret = pwrite(vcdev->vdev.fd, region, |
| 216 | vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset); |
| 217 | if (ret != vcdev->async_cmd_region_size) { |
| 218 | if (errno == EAGAIN) { |
| 219 | goto again; |
| 220 | } |
| 221 | error_report("vfio-ccw: write cmd region failed with errno=%d", errno); |
| 222 | ret = errno ? -errno : -EFAULT; |
| 223 | } else { |
| 224 | ret = 0; |
| 225 | } |
| 226 | switch (ret) { |
| 227 | case 0: |
| 228 | case -EBUSY: |
| 229 | case -ENODEV: |
| 230 | case -EACCES: |
| 231 | return ret; |
| 232 | case -EFAULT: |
| 233 | default: |
| 234 | sch_gen_unit_exception(sch); |
| 235 | css_inject_io_interrupt(sch); |
| 236 | return 0; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | static void vfio_ccw_reset(DeviceState *dev) |
| 241 | { |
| 242 | VFIOCCWDevice *vcdev = VFIO_CCW(dev); |
| 243 | |
| 244 | ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET); |
| 245 | } |
| 246 | |
| 247 | static void vfio_ccw_crw_read(VFIOCCWDevice *vcdev) |
| 248 | { |
| 249 | struct ccw_crw_region *region = vcdev->crw_region; |
| 250 | CRW crw; |
| 251 | int size; |
| 252 | |
| 253 | /* Keep reading CRWs as long as data is returned */ |
| 254 | do { |
| 255 | memset(region, 0, sizeof(*region)); |
| 256 | size = pread(vcdev->vdev.fd, region, vcdev->crw_region_size, |
| 257 | vcdev->crw_region_offset); |
| 258 | |
| 259 | if (size == -1) { |
| 260 | error_report("vfio-ccw: Read crw region failed with errno=%d", |
| 261 | errno); |
| 262 | break; |
| 263 | } |
| 264 | |
| 265 | if (region->crw == 0) { |
| 266 | /* No more CRWs to queue */ |
| 267 | break; |
| 268 | } |
| 269 | |
| 270 | memcpy(&crw, ®ion->crw, sizeof(CRW)); |
| 271 | |
| 272 | css_crw_add_to_queue(crw); |
| 273 | } while (1); |
| 274 | } |
| 275 | |
| 276 | static void vfio_ccw_req_notifier_handler(void *opaque) |
| 277 | { |
| 278 | VFIOCCWDevice *vcdev = opaque; |
| 279 | Error *err = NULL; |
| 280 | |
| 281 | if (!event_notifier_test_and_clear(&vcdev->req_notifier)) { |
| 282 | return; |
| 283 | } |
| 284 | |
| 285 | qdev_unplug(DEVICE(vcdev), &err); |
| 286 | if (err) { |
| 287 | warn_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name); |
| 288 | } |
| 289 | } |
| 290 | |
| 291 | static void vfio_ccw_crw_notifier_handler(void *opaque) |
| 292 | { |
| 293 | VFIOCCWDevice *vcdev = opaque; |
| 294 | |
| 295 | while (event_notifier_test_and_clear(&vcdev->crw_notifier)) { |
| 296 | vfio_ccw_crw_read(vcdev); |
| 297 | } |
| 298 | } |
| 299 | |
| 300 | static void vfio_ccw_io_notifier_handler(void *opaque) |
| 301 | { |
| 302 | VFIOCCWDevice *vcdev = opaque; |
| 303 | struct ccw_io_region *region = vcdev->io_region; |
| 304 | CcwDevice *ccw_dev = CCW_DEVICE(vcdev); |
| 305 | SubchDev *sch = ccw_dev->sch; |
| 306 | SCHIB *schib = &sch->curr_status; |
| 307 | SCSW s; |
| 308 | IRB irb; |
| 309 | ESW esw; |
| 310 | int size; |
| 311 | |
| 312 | if (!event_notifier_test_and_clear(&vcdev->io_notifier)) { |
| 313 | return; |
| 314 | } |
| 315 | |
| 316 | size = pread(vcdev->vdev.fd, region, vcdev->io_region_size, |
| 317 | vcdev->io_region_offset); |
| 318 | if (size == -1) { |
| 319 | switch (errno) { |
| 320 | case ENODEV: |
| 321 | /* Generate a deferred cc 3 condition. */ |
| 322 | schib->scsw.flags |= SCSW_FLAGS_MASK_CC; |
| 323 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; |
| 324 | schib->scsw.ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND); |
| 325 | goto read_err; |
| 326 | case EFAULT: |
| 327 | /* Memory problem, generate channel data check. */ |
| 328 | schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; |
| 329 | schib->scsw.cstat = SCSW_CSTAT_DATA_CHECK; |
| 330 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; |
| 331 | schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | |
| 332 | SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; |
| 333 | goto read_err; |
| 334 | default: |
| 335 | /* Error, generate channel program check. */ |
| 336 | schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; |
| 337 | schib->scsw.cstat = SCSW_CSTAT_PROG_CHECK; |
| 338 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; |
| 339 | schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | |
| 340 | SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; |
| 341 | goto read_err; |
| 342 | } |
| 343 | } else if (size != vcdev->io_region_size) { |
| 344 | /* Information transfer error, generate channel-control check. */ |
| 345 | schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND; |
| 346 | schib->scsw.cstat = SCSW_CSTAT_CHN_CTRL_CHK; |
| 347 | schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL; |
| 348 | schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY | |
| 349 | SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND; |
| 350 | goto read_err; |
| 351 | } |
| 352 | |
| 353 | memcpy(&irb, region->irb_area, sizeof(IRB)); |
| 354 | |
| 355 | /* Update control block via irb. */ |
| 356 | s = schib->scsw; |
| 357 | copy_scsw_to_guest(&s, &irb.scsw); |
| 358 | schib->scsw = s; |
| 359 | |
| 360 | copy_esw_to_guest(&esw, &irb.esw); |
| 361 | sch->esw = esw; |
| 362 | |
| 363 | /* If a uint check is pending, copy sense data. */ |
| 364 | if ((schib->scsw.dstat & SCSW_DSTAT_UNIT_CHECK) && |
| 365 | (schib->pmcw.chars & PMCW_CHARS_MASK_CSENSE)) { |
| 366 | memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw)); |
| 367 | } |
| 368 | |
| 369 | read_err: |
| 370 | css_inject_io_interrupt(sch); |
| 371 | } |
| 372 | |
| 373 | static bool vfio_ccw_register_irq_notifier(VFIOCCWDevice *vcdev, |
| 374 | unsigned int irq, |
| 375 | Error **errp) |
| 376 | { |
| 377 | VFIODevice *vdev = &vcdev->vdev; |
| 378 | struct vfio_irq_info irq_info; |
| 379 | int ret; |
| 380 | int fd; |
| 381 | EventNotifier *notifier; |
| 382 | IOHandler *fd_read; |
| 383 | |
| 384 | switch (irq) { |
| 385 | case VFIO_CCW_IO_IRQ_INDEX: |
| 386 | notifier = &vcdev->io_notifier; |
| 387 | fd_read = vfio_ccw_io_notifier_handler; |
| 388 | break; |
| 389 | case VFIO_CCW_CRW_IRQ_INDEX: |
| 390 | notifier = &vcdev->crw_notifier; |
| 391 | fd_read = vfio_ccw_crw_notifier_handler; |
| 392 | break; |
| 393 | case VFIO_CCW_REQ_IRQ_INDEX: |
| 394 | notifier = &vcdev->req_notifier; |
| 395 | fd_read = vfio_ccw_req_notifier_handler; |
| 396 | break; |
| 397 | default: |
| 398 | error_setg(errp, "vfio: Unsupported device irq(%d)", irq); |
| 399 | return false; |
| 400 | } |
| 401 | |
| 402 | if (vdev->num_irqs < irq + 1) { |
| 403 | error_setg(errp, "vfio: IRQ %u not available (number of irqs %u)", |
| 404 | irq, vdev->num_irqs); |
| 405 | return false; |
| 406 | } |
| 407 | |
| 408 | ret = vfio_device_get_irq_info(vdev, irq, &irq_info); |
| 409 | |
| 410 | if (ret < 0) { |
| 411 | error_setg_errno(errp, -ret, "vfio: Error getting irq info"); |
| 412 | return false; |
| 413 | } |
| 414 | |
| 415 | if (irq_info.count < 1) { |
| 416 | error_setg(errp, "vfio: Error getting irq info, count=0"); |
| 417 | return false; |
| 418 | } |
| 419 | |
| 420 | if (event_notifier_init(notifier, 0) < 0) { |
| 421 | error_setg_errno(errp, errno, |
| 422 | "vfio: Unable to init event notifier for irq (%d)", |
| 423 | irq); |
| 424 | return false; |
| 425 | } |
| 426 | |
| 427 | fd = event_notifier_get_fd(notifier); |
| 428 | qemu_set_fd_handler(fd, fd_read, NULL, vcdev); |
| 429 | |
| 430 | if (!vfio_device_irq_set_signaling(vdev, irq, 0, |
| 431 | VFIO_IRQ_SET_ACTION_TRIGGER, fd, errp)) { |
| 432 | qemu_set_fd_handler(fd, NULL, NULL, vcdev); |
| 433 | event_notifier_cleanup(notifier); |
| 434 | return false; |
| 435 | } |
| 436 | |
| 437 | return true; |
| 438 | } |
| 439 | |
| 440 | static void vfio_ccw_unregister_irq_notifier(VFIOCCWDevice *vcdev, |
| 441 | unsigned int irq) |
| 442 | { |
| 443 | Error *err = NULL; |
| 444 | EventNotifier *notifier; |
| 445 | |
| 446 | switch (irq) { |
| 447 | case VFIO_CCW_IO_IRQ_INDEX: |
| 448 | notifier = &vcdev->io_notifier; |
| 449 | break; |
| 450 | case VFIO_CCW_CRW_IRQ_INDEX: |
| 451 | notifier = &vcdev->crw_notifier; |
| 452 | break; |
| 453 | case VFIO_CCW_REQ_IRQ_INDEX: |
| 454 | notifier = &vcdev->req_notifier; |
| 455 | break; |
| 456 | default: |
| 457 | error_report("vfio: Unsupported device irq(%d)", irq); |
| 458 | return; |
| 459 | } |
| 460 | |
| 461 | if (!vfio_device_irq_set_signaling(&vcdev->vdev, irq, 0, |
| 462 | VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) { |
| 463 | warn_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name); |
| 464 | } |
| 465 | |
| 466 | qemu_set_fd_handler(event_notifier_get_fd(notifier), |
| 467 | NULL, NULL, vcdev); |
| 468 | event_notifier_cleanup(notifier); |
| 469 | } |
| 470 | |
| 471 | static bool vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp) |
| 472 | { |
| 473 | VFIODevice *vdev = &vcdev->vdev; |
| 474 | struct vfio_region_info *info; |
| 475 | int ret; |
| 476 | |
| 477 | /* Sanity check device */ |
| 478 | if (!(vdev->flags & VFIO_DEVICE_FLAGS_CCW)) { |
| 479 | error_setg(errp, "vfio: Um, this isn't a vfio-ccw device"); |
| 480 | return false; |
| 481 | } |
| 482 | |
| 483 | /* |
| 484 | * We always expect at least the I/O region to be present. We also |
| 485 | * may have a variable number of regions governed by capabilities. |
| 486 | */ |
| 487 | if (vdev->num_initial_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) { |
| 488 | error_setg(errp, "vfio: too few regions (%u), expected at least %u", |
| 489 | vdev->num_initial_regions, VFIO_CCW_CONFIG_REGION_INDEX + 1); |
| 490 | return false; |
| 491 | } |
| 492 | |
| 493 | ret = vfio_device_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info); |
| 494 | if (ret) { |
| 495 | error_setg_errno(errp, -ret, "vfio: Error getting config info"); |
| 496 | return false; |
| 497 | } |
| 498 | |
| 499 | vcdev->io_region_size = info->size; |
| 500 | if (sizeof(*vcdev->io_region) != vcdev->io_region_size) { |
| 501 | error_setg(errp, "vfio: Unexpected size of the I/O region"); |
| 502 | goto out_err; |
| 503 | } |
| 504 | |
| 505 | vcdev->io_region_offset = info->offset; |
| 506 | vcdev->io_region = g_malloc0(info->size); |
| 507 | |
| 508 | /* check for the optional async command region */ |
| 509 | ret = vfio_device_get_region_info_type(vdev, VFIO_REGION_TYPE_CCW, |
| 510 | VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD, &info); |
| 511 | if (!ret) { |
| 512 | vcdev->async_cmd_region_size = info->size; |
| 513 | if (sizeof(*vcdev->async_cmd_region) != vcdev->async_cmd_region_size) { |
| 514 | error_setg(errp, "vfio: Unexpected size of the async cmd region"); |
| 515 | goto out_err; |
| 516 | } |
| 517 | vcdev->async_cmd_region_offset = info->offset; |
| 518 | vcdev->async_cmd_region = g_malloc0(info->size); |
| 519 | } |
| 520 | |
| 521 | ret = vfio_device_get_region_info_type(vdev, VFIO_REGION_TYPE_CCW, |
| 522 | VFIO_REGION_SUBTYPE_CCW_SCHIB, &info); |
| 523 | if (!ret) { |
| 524 | vcdev->schib_region_size = info->size; |
| 525 | if (sizeof(*vcdev->schib_region) != vcdev->schib_region_size) { |
| 526 | error_setg(errp, "vfio: Unexpected size of the schib region"); |
| 527 | goto out_err; |
| 528 | } |
| 529 | vcdev->schib_region_offset = info->offset; |
| 530 | vcdev->schib_region = g_malloc(info->size); |
| 531 | } |
| 532 | |
| 533 | ret = vfio_device_get_region_info_type(vdev, VFIO_REGION_TYPE_CCW, |
| 534 | VFIO_REGION_SUBTYPE_CCW_CRW, &info); |
| 535 | |
| 536 | if (!ret) { |
| 537 | vcdev->crw_region_size = info->size; |
| 538 | if (sizeof(*vcdev->crw_region) != vcdev->crw_region_size) { |
| 539 | error_setg(errp, "vfio: Unexpected size of the CRW region"); |
| 540 | goto out_err; |
| 541 | } |
| 542 | vcdev->crw_region_offset = info->offset; |
| 543 | vcdev->crw_region = g_malloc(info->size); |
| 544 | } |
| 545 | |
| 546 | return true; |
| 547 | |
| 548 | out_err: |
| 549 | g_free(vcdev->crw_region); |
| 550 | g_free(vcdev->schib_region); |
| 551 | g_free(vcdev->async_cmd_region); |
| 552 | g_free(vcdev->io_region); |
| 553 | return false; |
| 554 | } |
| 555 | |
| 556 | static void vfio_ccw_put_region(VFIOCCWDevice *vcdev) |
| 557 | { |
| 558 | g_free(vcdev->crw_region); |
| 559 | g_free(vcdev->schib_region); |
| 560 | g_free(vcdev->async_cmd_region); |
| 561 | g_free(vcdev->io_region); |
| 562 | } |
| 563 | |
| 564 | static void vfio_ccw_realize(DeviceState *dev, Error **errp) |
| 565 | { |
| 566 | S390CCWDevice *cdev = S390_CCW_DEVICE(dev); |
| 567 | VFIOCCWDevice *vcdev = VFIO_CCW(cdev); |
| 568 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); |
| 569 | VFIODevice *vbasedev = &vcdev->vdev; |
| 570 | Error *err = NULL; |
| 571 | |
| 572 | /* Call the class init function for subchannel. */ |
| 573 | if (cdc->realize) { |
| 574 | if (!cdc->realize(cdev, vcdev->vdev.sysfsdev, errp)) { |
| 575 | return; |
| 576 | } |
| 577 | } |
| 578 | |
| 579 | if (!vfio_device_get_name(vbasedev, errp)) { |
| 580 | goto out_unrealize; |
| 581 | } |
| 582 | |
| 583 | if (!vfio_device_attach(cdev->mdevid, vbasedev, |
| 584 | &address_space_memory, errp)) { |
| 585 | goto out_attach_dev_err; |
| 586 | } |
| 587 | |
| 588 | if (!vfio_ccw_get_region(vcdev, errp)) { |
| 589 | goto out_region_err; |
| 590 | } |
| 591 | |
| 592 | if (!vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX, errp)) { |
| 593 | goto out_io_notifier_err; |
| 594 | } |
| 595 | |
| 596 | if (vcdev->crw_region) { |
| 597 | if (!vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX, |
| 598 | errp)) { |
| 599 | goto out_irq_notifier_err; |
| 600 | } |
| 601 | } |
| 602 | |
| 603 | if (!vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX, &err)) { |
| 604 | /* |
| 605 | * Report this error, but do not make it a failing condition. |
| 606 | * Lack of this IRQ in the host does not prevent normal operation. |
| 607 | */ |
| 608 | warn_report_err(err); |
| 609 | } |
| 610 | |
| 611 | return; |
| 612 | |
| 613 | out_irq_notifier_err: |
| 614 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX); |
| 615 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX); |
| 616 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX); |
| 617 | out_io_notifier_err: |
| 618 | vfio_ccw_put_region(vcdev); |
| 619 | out_region_err: |
| 620 | vfio_device_detach(vbasedev); |
| 621 | out_attach_dev_err: |
| 622 | vfio_device_free_name(vbasedev); |
| 623 | out_unrealize: |
| 624 | if (cdc->unrealize) { |
| 625 | cdc->unrealize(cdev); |
| 626 | } |
| 627 | } |
| 628 | |
| 629 | static void vfio_ccw_unrealize(DeviceState *dev) |
| 630 | { |
| 631 | S390CCWDevice *cdev = S390_CCW_DEVICE(dev); |
| 632 | VFIOCCWDevice *vcdev = VFIO_CCW(cdev); |
| 633 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev); |
| 634 | |
| 635 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX); |
| 636 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX); |
| 637 | vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX); |
| 638 | vfio_ccw_put_region(vcdev); |
| 639 | vfio_device_detach(&vcdev->vdev); |
| 640 | vfio_device_free_name(&vcdev->vdev); |
| 641 | |
| 642 | if (cdc->unrealize) { |
| 643 | cdc->unrealize(cdev); |
| 644 | } |
| 645 | } |
| 646 | |
| 647 | static const Property vfio_ccw_properties[] = { |
| 648 | DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev), |
| 649 | DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice, force_orb_pfch, false), |
| 650 | DEFINE_PROP_LINK("iommufd", VFIOCCWDevice, vdev.iommufd, |
| 651 | TYPE_IOMMUFD_BACKEND, IOMMUFDBackend *), |
| 652 | DEFINE_PROP_CCW_LOADPARM("loadparm", CcwDevice, loadparm), |
| 653 | }; |
| 654 | |
| 655 | static const VMStateDescription vfio_ccw_vmstate = { |
| 656 | .name = "vfio-ccw", |
| 657 | .unmigratable = 1, |
| 658 | }; |
| 659 | |
| 660 | static void vfio_ccw_instance_init(Object *obj) |
| 661 | { |
| 662 | VFIOCCWDevice *vcdev = VFIO_CCW(obj); |
| 663 | VFIODevice *vbasedev = &vcdev->vdev; |
| 664 | |
| 665 | /* CCW device is mdev type device */ |
| 666 | vbasedev->mdev = true; |
| 667 | |
| 668 | /* |
| 669 | * All vfio-ccw devices are believed to operate in a way compatible with |
| 670 | * discarding of memory in RAM blocks, ie. pages pinned in the host are |
| 671 | * in the current working set of the guest driver and therefore never |
| 672 | * overlap e.g., with pages available to the guest balloon driver. This |
| 673 | * needs to be set before vfio_get_device() for vfio common to handle |
| 674 | * ram_block_discard_disable(). |
| 675 | */ |
| 676 | vfio_device_init(vbasedev, VFIO_DEVICE_TYPE_CCW, &vfio_ccw_ops, |
| 677 | DEVICE(vcdev), true); |
| 678 | } |
| 679 | |
| 680 | static void vfio_ccw_set_fd(Object *obj, const char *str, Error **errp) |
| 681 | { |
| 682 | vfio_device_set_fd(&VFIO_CCW(obj)->vdev, str, errp); |
| 683 | } |
| 684 | |
| 685 | static void vfio_ccw_class_init(ObjectClass *klass, const void *data) |
| 686 | { |
| 687 | DeviceClass *dc = DEVICE_CLASS(klass); |
| 688 | S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass); |
| 689 | |
| 690 | device_class_set_props(dc, vfio_ccw_properties); |
| 691 | object_class_property_add_str(klass, "fd", NULL, vfio_ccw_set_fd); |
| 692 | dc->vmsd = &vfio_ccw_vmstate; |
| 693 | dc->desc = "VFIO-based subchannel assignment"; |
| 694 | set_bit(DEVICE_CATEGORY_MISC, dc->categories); |
| 695 | dc->realize = vfio_ccw_realize; |
| 696 | dc->unrealize = vfio_ccw_unrealize; |
| 697 | device_class_set_legacy_reset(dc, vfio_ccw_reset); |
| 698 | |
| 699 | cdc->handle_request = vfio_ccw_handle_request; |
| 700 | cdc->handle_halt = vfio_ccw_handle_halt; |
| 701 | cdc->handle_clear = vfio_ccw_handle_clear; |
| 702 | cdc->handle_store = vfio_ccw_handle_store; |
| 703 | |
| 704 | object_class_property_set_description(klass, /* 2.10 */ |
| 705 | "sysfsdev", |
| 706 | "Host sysfs path of assigned device"); |
| 707 | object_class_property_set_description(klass, /* 3.0 */ |
| 708 | "force-orb-pfch", |
| 709 | "Force unlimited prefetch"); |
| 710 | object_class_property_set_description(klass, /* 9.0 */ |
| 711 | "iommufd", |
| 712 | "Set host IOMMUFD backend device"); |
| 713 | object_class_property_set_description(klass, /* 9.2 */ |
| 714 | "loadparm", |
| 715 | "Define which devices that can be used for booting"); |
| 716 | } |
| 717 | |
| 718 | static const TypeInfo vfio_ccw_info = { |
| 719 | .name = TYPE_VFIO_CCW, |
| 720 | .parent = TYPE_S390_CCW, |
| 721 | .instance_size = sizeof(VFIOCCWDevice), |
| 722 | .instance_init = vfio_ccw_instance_init, |
| 723 | .class_init = vfio_ccw_class_init, |
| 724 | }; |
| 725 | |
| 726 | static void register_vfio_ccw_type(void) |
| 727 | { |
| 728 | type_register_static(&vfio_ccw_info); |
| 729 | } |
| 730 | |
| 731 | type_init(register_vfio_ccw_type) |