xref: /openbmc/qemu/hw/vfio/ccw.c (revision a27bd6c779badb8d76e4430d810ef710a1b98f4e)
11dcac3e1SXiao Feng Ren /*
21dcac3e1SXiao Feng Ren  * vfio based subchannel assignment support
31dcac3e1SXiao Feng Ren  *
41dcac3e1SXiao Feng Ren  * Copyright 2017 IBM Corp.
58fadea24SCornelia Huck  * Copyright 2019 Red Hat, Inc.
68fadea24SCornelia Huck  *
71dcac3e1SXiao Feng Ren  * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
81dcac3e1SXiao Feng Ren  *            Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
91dcac3e1SXiao Feng Ren  *            Pierre Morel <pmorel@linux.vnet.ibm.com>
108fadea24SCornelia Huck  *            Cornelia Huck <cohuck@redhat.com>
111dcac3e1SXiao Feng Ren  *
121dcac3e1SXiao Feng Ren  * This work is licensed under the terms of the GNU GPL, version 2 or (at
1308b824aaSCornelia Huck  * your option) any later version. See the COPYING file in the top-level
141dcac3e1SXiao Feng Ren  * directory.
151dcac3e1SXiao Feng Ren  */
161dcac3e1SXiao Feng Ren 
17e9808d09SPhilippe Mathieu-Daudé #include "qemu/osdep.h"
181dcac3e1SXiao Feng Ren #include <linux/vfio.h>
19c14e706cSDong Jia Shi #include <linux/vfio_ccw.h>
201dcac3e1SXiao Feng Ren #include <sys/ioctl.h>
211dcac3e1SXiao Feng Ren 
221dcac3e1SXiao Feng Ren #include "qapi/error.h"
231dcac3e1SXiao Feng Ren #include "hw/sysbus.h"
241dcac3e1SXiao Feng Ren #include "hw/vfio/vfio.h"
251dcac3e1SXiao Feng Ren #include "hw/vfio/vfio-common.h"
261dcac3e1SXiao Feng Ren #include "hw/s390x/s390-ccw.h"
2744445d86SJason J. Herne #include "hw/s390x/vfio-ccw.h"
28*a27bd6c7SMarkus Armbruster #include "hw/qdev-properties.h"
291dcac3e1SXiao Feng Ren #include "hw/s390x/ccw-device.h"
30d791937fSPhilippe Mathieu-Daudé #include "exec/address-spaces.h"
314886b3e9SDong Jia Shi #include "qemu/error-report.h"
32db725815SMarkus Armbruster #include "qemu/main-loop.h"
330b8fa32fSMarkus Armbruster #include "qemu/module.h"
341dcac3e1SXiao Feng Ren 
3544445d86SJason J. Herne struct VFIOCCWDevice {
361dcac3e1SXiao Feng Ren     S390CCWDevice cdev;
371dcac3e1SXiao Feng Ren     VFIODevice vdev;
38c14e706cSDong Jia Shi     uint64_t io_region_size;
39c14e706cSDong Jia Shi     uint64_t io_region_offset;
40c14e706cSDong Jia Shi     struct ccw_io_region *io_region;
418fadea24SCornelia Huck     uint64_t async_cmd_region_size;
428fadea24SCornelia Huck     uint64_t async_cmd_region_offset;
438fadea24SCornelia Huck     struct ccw_cmd_region *async_cmd_region;
444886b3e9SDong Jia Shi     EventNotifier io_notifier;
459a51c9eeSHalil Pasic     bool force_orb_pfch;
469a51c9eeSHalil Pasic     bool warned_orb_pfch;
4744445d86SJason J. Herne };
481dcac3e1SXiao Feng Ren 
499a51c9eeSHalil Pasic static inline void warn_once_pfch(VFIOCCWDevice *vcdev, SubchDev *sch,
509a51c9eeSHalil Pasic                                   const char *msg)
519a51c9eeSHalil Pasic {
52c55510b7SCornelia Huck     warn_report_once_cond(&vcdev->warned_orb_pfch,
53c55510b7SCornelia Huck                           "vfio-ccw (devno %x.%x.%04x): %s",
549a51c9eeSHalil Pasic                           sch->cssid, sch->ssid, sch->devno, msg);
559a51c9eeSHalil Pasic }
569a51c9eeSHalil Pasic 
571dcac3e1SXiao Feng Ren static void vfio_ccw_compute_needs_reset(VFIODevice *vdev)
581dcac3e1SXiao Feng Ren {
591dcac3e1SXiao Feng Ren     vdev->needs_reset = false;
601dcac3e1SXiao Feng Ren }
611dcac3e1SXiao Feng Ren 
621dcac3e1SXiao Feng Ren /*
631dcac3e1SXiao Feng Ren  * We don't need vfio_hot_reset_multi and vfio_eoi operations for
641dcac3e1SXiao Feng Ren  * vfio_ccw device now.
651dcac3e1SXiao Feng Ren  */
661dcac3e1SXiao Feng Ren struct VFIODeviceOps vfio_ccw_ops = {
671dcac3e1SXiao Feng Ren     .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset,
681dcac3e1SXiao Feng Ren };
691dcac3e1SXiao Feng Ren 
7066dc50f7SHalil Pasic static IOInstEnding vfio_ccw_handle_request(SubchDev *sch)
718ca2b376SXiao Feng Ren {
7266dc50f7SHalil Pasic     S390CCWDevice *cdev = sch->driver_data;
738ca2b376SXiao Feng Ren     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
748ca2b376SXiao Feng Ren     struct ccw_io_region *region = vcdev->io_region;
758ca2b376SXiao Feng Ren     int ret;
768ca2b376SXiao Feng Ren 
779a51c9eeSHalil Pasic     if (!(sch->orb.ctrl0 & ORB_CTRL0_MASK_PFCH)) {
789a51c9eeSHalil Pasic         if (!(vcdev->force_orb_pfch)) {
799a51c9eeSHalil Pasic             warn_once_pfch(vcdev, sch, "requires PFCH flag set");
809a51c9eeSHalil Pasic             sch_gen_unit_exception(sch);
819a51c9eeSHalil Pasic             css_inject_io_interrupt(sch);
829a51c9eeSHalil Pasic             return IOINST_CC_EXPECTED;
839a51c9eeSHalil Pasic         } else {
849a51c9eeSHalil Pasic             sch->orb.ctrl0 |= ORB_CTRL0_MASK_PFCH;
859a51c9eeSHalil Pasic             warn_once_pfch(vcdev, sch, "PFCH flag forced");
869a51c9eeSHalil Pasic         }
879a51c9eeSHalil Pasic     }
889a51c9eeSHalil Pasic 
898ca2b376SXiao Feng Ren     QEMU_BUILD_BUG_ON(sizeof(region->orb_area) != sizeof(ORB));
908ca2b376SXiao Feng Ren     QEMU_BUILD_BUG_ON(sizeof(region->scsw_area) != sizeof(SCSW));
918ca2b376SXiao Feng Ren     QEMU_BUILD_BUG_ON(sizeof(region->irb_area) != sizeof(IRB));
928ca2b376SXiao Feng Ren 
938ca2b376SXiao Feng Ren     memset(region, 0, sizeof(*region));
948ca2b376SXiao Feng Ren 
9566dc50f7SHalil Pasic     memcpy(region->orb_area, &sch->orb, sizeof(ORB));
9666dc50f7SHalil Pasic     memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW));
978ca2b376SXiao Feng Ren 
988ca2b376SXiao Feng Ren again:
998ca2b376SXiao Feng Ren     ret = pwrite(vcdev->vdev.fd, region,
1008ca2b376SXiao Feng Ren                  vcdev->io_region_size, vcdev->io_region_offset);
1018ca2b376SXiao Feng Ren     if (ret != vcdev->io_region_size) {
1028ca2b376SXiao Feng Ren         if (errno == EAGAIN) {
1038ca2b376SXiao Feng Ren             goto again;
1048ca2b376SXiao Feng Ren         }
1058ca2b376SXiao Feng Ren         error_report("vfio-ccw: wirte I/O region failed with errno=%d", errno);
10666dc50f7SHalil Pasic         ret = -errno;
10766dc50f7SHalil Pasic     } else {
10866dc50f7SHalil Pasic         ret = region->ret_code;
1098ca2b376SXiao Feng Ren     }
11066dc50f7SHalil Pasic     switch (ret) {
11166dc50f7SHalil Pasic     case 0:
11266dc50f7SHalil Pasic         return IOINST_CC_EXPECTED;
11366dc50f7SHalil Pasic     case -EBUSY:
11466dc50f7SHalil Pasic         return IOINST_CC_BUSY;
11566dc50f7SHalil Pasic     case -ENODEV:
11666dc50f7SHalil Pasic     case -EACCES:
11766dc50f7SHalil Pasic         return IOINST_CC_NOT_OPERATIONAL;
11866dc50f7SHalil Pasic     case -EFAULT:
11966dc50f7SHalil Pasic     default:
12066dc50f7SHalil Pasic         sch_gen_unit_exception(sch);
12166dc50f7SHalil Pasic         css_inject_io_interrupt(sch);
12266dc50f7SHalil Pasic         return IOINST_CC_EXPECTED;
12366dc50f7SHalil Pasic     }
1248ca2b376SXiao Feng Ren }
1258ca2b376SXiao Feng Ren 
1268fadea24SCornelia Huck static int vfio_ccw_handle_clear(SubchDev *sch)
1278fadea24SCornelia Huck {
1288fadea24SCornelia Huck     S390CCWDevice *cdev = sch->driver_data;
1298fadea24SCornelia Huck     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
1308fadea24SCornelia Huck     struct ccw_cmd_region *region = vcdev->async_cmd_region;
1318fadea24SCornelia Huck     int ret;
1328fadea24SCornelia Huck 
1338fadea24SCornelia Huck     if (!vcdev->async_cmd_region) {
1348fadea24SCornelia Huck         /* Async command region not available, fall back to emulation */
1358fadea24SCornelia Huck         return -ENOSYS;
1368fadea24SCornelia Huck     }
1378fadea24SCornelia Huck 
1388fadea24SCornelia Huck     memset(region, 0, sizeof(*region));
1398fadea24SCornelia Huck     region->command = VFIO_CCW_ASYNC_CMD_CSCH;
1408fadea24SCornelia Huck 
1418fadea24SCornelia Huck again:
1428fadea24SCornelia Huck     ret = pwrite(vcdev->vdev.fd, region,
1438fadea24SCornelia Huck                  vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset);
1448fadea24SCornelia Huck     if (ret != vcdev->async_cmd_region_size) {
1458fadea24SCornelia Huck         if (errno == EAGAIN) {
1468fadea24SCornelia Huck             goto again;
1478fadea24SCornelia Huck         }
1488fadea24SCornelia Huck         error_report("vfio-ccw: write cmd region failed with errno=%d", errno);
1498fadea24SCornelia Huck         ret = -errno;
1508fadea24SCornelia Huck     } else {
1518fadea24SCornelia Huck         ret = region->ret_code;
1528fadea24SCornelia Huck     }
1538fadea24SCornelia Huck     switch (ret) {
1548fadea24SCornelia Huck     case 0:
1558fadea24SCornelia Huck     case -ENODEV:
1568fadea24SCornelia Huck     case -EACCES:
1578fadea24SCornelia Huck         return 0;
1588fadea24SCornelia Huck     case -EFAULT:
1598fadea24SCornelia Huck     default:
1608fadea24SCornelia Huck         sch_gen_unit_exception(sch);
1618fadea24SCornelia Huck         css_inject_io_interrupt(sch);
1628fadea24SCornelia Huck         return 0;
1638fadea24SCornelia Huck     }
1648fadea24SCornelia Huck }
1658fadea24SCornelia Huck 
1668fadea24SCornelia Huck static int vfio_ccw_handle_halt(SubchDev *sch)
1678fadea24SCornelia Huck {
1688fadea24SCornelia Huck     S390CCWDevice *cdev = sch->driver_data;
1698fadea24SCornelia Huck     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
1708fadea24SCornelia Huck     struct ccw_cmd_region *region = vcdev->async_cmd_region;
1718fadea24SCornelia Huck     int ret;
1728fadea24SCornelia Huck 
1738fadea24SCornelia Huck     if (!vcdev->async_cmd_region) {
1748fadea24SCornelia Huck         /* Async command region not available, fall back to emulation */
1758fadea24SCornelia Huck         return -ENOSYS;
1768fadea24SCornelia Huck     }
1778fadea24SCornelia Huck 
1788fadea24SCornelia Huck     memset(region, 0, sizeof(*region));
1798fadea24SCornelia Huck     region->command = VFIO_CCW_ASYNC_CMD_HSCH;
1808fadea24SCornelia Huck 
1818fadea24SCornelia Huck again:
1828fadea24SCornelia Huck     ret = pwrite(vcdev->vdev.fd, region,
1838fadea24SCornelia Huck                  vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset);
1848fadea24SCornelia Huck     if (ret != vcdev->async_cmd_region_size) {
1858fadea24SCornelia Huck         if (errno == EAGAIN) {
1868fadea24SCornelia Huck             goto again;
1878fadea24SCornelia Huck         }
1888fadea24SCornelia Huck         error_report("vfio-ccw: write cmd region failed with errno=%d", errno);
1898fadea24SCornelia Huck         ret = -errno;
1908fadea24SCornelia Huck     } else {
1918fadea24SCornelia Huck         ret = region->ret_code;
1928fadea24SCornelia Huck     }
1938fadea24SCornelia Huck     switch (ret) {
1948fadea24SCornelia Huck     case 0:
1958fadea24SCornelia Huck     case -EBUSY:
1968fadea24SCornelia Huck     case -ENODEV:
1978fadea24SCornelia Huck     case -EACCES:
1988fadea24SCornelia Huck         return 0;
1998fadea24SCornelia Huck     case -EFAULT:
2008fadea24SCornelia Huck     default:
2018fadea24SCornelia Huck         sch_gen_unit_exception(sch);
2028fadea24SCornelia Huck         css_inject_io_interrupt(sch);
2038fadea24SCornelia Huck         return 0;
2048fadea24SCornelia Huck     }
2058fadea24SCornelia Huck }
2068fadea24SCornelia Huck 
2071dcac3e1SXiao Feng Ren static void vfio_ccw_reset(DeviceState *dev)
2081dcac3e1SXiao Feng Ren {
2091dcac3e1SXiao Feng Ren     CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
2101dcac3e1SXiao Feng Ren     S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
2111dcac3e1SXiao Feng Ren     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
2121dcac3e1SXiao Feng Ren 
2131dcac3e1SXiao Feng Ren     ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET);
2141dcac3e1SXiao Feng Ren }
2151dcac3e1SXiao Feng Ren 
2164886b3e9SDong Jia Shi static void vfio_ccw_io_notifier_handler(void *opaque)
2174886b3e9SDong Jia Shi {
2184886b3e9SDong Jia Shi     VFIOCCWDevice *vcdev = opaque;
2198ca2b376SXiao Feng Ren     struct ccw_io_region *region = vcdev->io_region;
2208ca2b376SXiao Feng Ren     S390CCWDevice *cdev = S390_CCW_DEVICE(vcdev);
2218ca2b376SXiao Feng Ren     CcwDevice *ccw_dev = CCW_DEVICE(cdev);
2228ca2b376SXiao Feng Ren     SubchDev *sch = ccw_dev->sch;
223e1d0b372SDaniel P. Berrangé     SCHIB *schib = &sch->curr_status;
224e1d0b372SDaniel P. Berrangé     SCSW s;
2258ca2b376SXiao Feng Ren     IRB irb;
2268ca2b376SXiao Feng Ren     int size;
2274886b3e9SDong Jia Shi 
2284886b3e9SDong Jia Shi     if (!event_notifier_test_and_clear(&vcdev->io_notifier)) {
2294886b3e9SDong Jia Shi         return;
2304886b3e9SDong Jia Shi     }
2318ca2b376SXiao Feng Ren 
2328ca2b376SXiao Feng Ren     size = pread(vcdev->vdev.fd, region, vcdev->io_region_size,
2338ca2b376SXiao Feng Ren                  vcdev->io_region_offset);
2348ca2b376SXiao Feng Ren     if (size == -1) {
2358ca2b376SXiao Feng Ren         switch (errno) {
2368ca2b376SXiao Feng Ren         case ENODEV:
2378ca2b376SXiao Feng Ren             /* Generate a deferred cc 3 condition. */
238e1d0b372SDaniel P. Berrangé             schib->scsw.flags |= SCSW_FLAGS_MASK_CC;
239e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
240e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND);
2418ca2b376SXiao Feng Ren             goto read_err;
2428ca2b376SXiao Feng Ren         case EFAULT:
2438ca2b376SXiao Feng Ren             /* Memory problem, generate channel data check. */
244e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
245e1d0b372SDaniel P. Berrangé             schib->scsw.cstat = SCSW_CSTAT_DATA_CHECK;
246e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
247e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
2488ca2b376SXiao Feng Ren                        SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
2498ca2b376SXiao Feng Ren             goto read_err;
2508ca2b376SXiao Feng Ren         default:
2518ca2b376SXiao Feng Ren             /* Error, generate channel program check. */
252e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
253e1d0b372SDaniel P. Berrangé             schib->scsw.cstat = SCSW_CSTAT_PROG_CHECK;
254e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
255e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
2568ca2b376SXiao Feng Ren                        SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
2578ca2b376SXiao Feng Ren             goto read_err;
2588ca2b376SXiao Feng Ren         }
2598ca2b376SXiao Feng Ren     } else if (size != vcdev->io_region_size) {
2608ca2b376SXiao Feng Ren         /* Information transfer error, generate channel-control check. */
261e1d0b372SDaniel P. Berrangé         schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
262e1d0b372SDaniel P. Berrangé         schib->scsw.cstat = SCSW_CSTAT_CHN_CTRL_CHK;
263e1d0b372SDaniel P. Berrangé         schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
264e1d0b372SDaniel P. Berrangé         schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
2658ca2b376SXiao Feng Ren                    SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
2668ca2b376SXiao Feng Ren         goto read_err;
2678ca2b376SXiao Feng Ren     }
2688ca2b376SXiao Feng Ren 
2698ca2b376SXiao Feng Ren     memcpy(&irb, region->irb_area, sizeof(IRB));
2708ca2b376SXiao Feng Ren 
2718ca2b376SXiao Feng Ren     /* Update control block via irb. */
272e1d0b372SDaniel P. Berrangé     s = schib->scsw;
273e1d0b372SDaniel P. Berrangé     copy_scsw_to_guest(&s, &irb.scsw);
274e1d0b372SDaniel P. Berrangé     schib->scsw = s;
2758ca2b376SXiao Feng Ren 
276334e7685SDong Jia Shi     /* If a uint check is pending, copy sense data. */
277e1d0b372SDaniel P. Berrangé     if ((schib->scsw.dstat & SCSW_DSTAT_UNIT_CHECK) &&
278e1d0b372SDaniel P. Berrangé         (schib->pmcw.chars & PMCW_CHARS_MASK_CSENSE)) {
279334e7685SDong Jia Shi         memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw));
280334e7685SDong Jia Shi     }
281334e7685SDong Jia Shi 
2828ca2b376SXiao Feng Ren read_err:
2838ca2b376SXiao Feng Ren     css_inject_io_interrupt(sch);
2844886b3e9SDong Jia Shi }
2854886b3e9SDong Jia Shi 
2864886b3e9SDong Jia Shi static void vfio_ccw_register_io_notifier(VFIOCCWDevice *vcdev, Error **errp)
2874886b3e9SDong Jia Shi {
2884886b3e9SDong Jia Shi     VFIODevice *vdev = &vcdev->vdev;
2894886b3e9SDong Jia Shi     struct vfio_irq_info *irq_info;
2904886b3e9SDong Jia Shi     size_t argsz;
291b5e89f04SCornelia Huck     int fd;
2924886b3e9SDong Jia Shi 
2934886b3e9SDong Jia Shi     if (vdev->num_irqs < VFIO_CCW_IO_IRQ_INDEX + 1) {
2944886b3e9SDong Jia Shi         error_setg(errp, "vfio: unexpected number of io irqs %u",
2954886b3e9SDong Jia Shi                    vdev->num_irqs);
2964886b3e9SDong Jia Shi         return;
2974886b3e9SDong Jia Shi     }
2984886b3e9SDong Jia Shi 
29928e22d4bSJing Zhang     argsz = sizeof(*irq_info);
3004886b3e9SDong Jia Shi     irq_info = g_malloc0(argsz);
3014886b3e9SDong Jia Shi     irq_info->index = VFIO_CCW_IO_IRQ_INDEX;
3024886b3e9SDong Jia Shi     irq_info->argsz = argsz;
3034886b3e9SDong Jia Shi     if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO,
3044886b3e9SDong Jia Shi               irq_info) < 0 || irq_info->count < 1) {
3054886b3e9SDong Jia Shi         error_setg_errno(errp, errno, "vfio: Error getting irq info");
3064886b3e9SDong Jia Shi         goto out_free_info;
3074886b3e9SDong Jia Shi     }
3084886b3e9SDong Jia Shi 
3094886b3e9SDong Jia Shi     if (event_notifier_init(&vcdev->io_notifier, 0)) {
3104886b3e9SDong Jia Shi         error_setg_errno(errp, errno,
3114886b3e9SDong Jia Shi                          "vfio: Unable to init event notifier for IO");
3124886b3e9SDong Jia Shi         goto out_free_info;
3134886b3e9SDong Jia Shi     }
3144886b3e9SDong Jia Shi 
315b5e89f04SCornelia Huck     fd = event_notifier_get_fd(&vcdev->io_notifier);
316b5e89f04SCornelia Huck     qemu_set_fd_handler(fd, vfio_ccw_io_notifier_handler, NULL, vcdev);
3174886b3e9SDong Jia Shi 
318b5e89f04SCornelia Huck     if (vfio_set_irq_signaling(vdev, VFIO_CCW_IO_IRQ_INDEX, 0,
319b5e89f04SCornelia Huck                                VFIO_IRQ_SET_ACTION_TRIGGER, fd, errp)) {
320b5e89f04SCornelia Huck         qemu_set_fd_handler(fd, NULL, NULL, vcdev);
3214886b3e9SDong Jia Shi         event_notifier_cleanup(&vcdev->io_notifier);
3224886b3e9SDong Jia Shi     }
3234886b3e9SDong Jia Shi 
3244886b3e9SDong Jia Shi out_free_info:
3254886b3e9SDong Jia Shi     g_free(irq_info);
3264886b3e9SDong Jia Shi }
3274886b3e9SDong Jia Shi 
3284886b3e9SDong Jia Shi static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice *vcdev)
3294886b3e9SDong Jia Shi {
330b5e89f04SCornelia Huck     Error *err = NULL;
3314886b3e9SDong Jia Shi 
332f5cf94cdSAlex Williamson     if (vfio_set_irq_signaling(&vcdev->vdev, VFIO_CCW_IO_IRQ_INDEX, 0,
333f5cf94cdSAlex Williamson                                VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) {
334b5e89f04SCornelia Huck         error_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name);
3354886b3e9SDong Jia Shi     }
3364886b3e9SDong Jia Shi 
3374886b3e9SDong Jia Shi     qemu_set_fd_handler(event_notifier_get_fd(&vcdev->io_notifier),
3384886b3e9SDong Jia Shi                         NULL, NULL, vcdev);
3394886b3e9SDong Jia Shi     event_notifier_cleanup(&vcdev->io_notifier);
3404886b3e9SDong Jia Shi }
3414886b3e9SDong Jia Shi 
342c14e706cSDong Jia Shi static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp)
343c14e706cSDong Jia Shi {
344c14e706cSDong Jia Shi     VFIODevice *vdev = &vcdev->vdev;
345c14e706cSDong Jia Shi     struct vfio_region_info *info;
346c14e706cSDong Jia Shi     int ret;
347c14e706cSDong Jia Shi 
348c14e706cSDong Jia Shi     /* Sanity check device */
349c14e706cSDong Jia Shi     if (!(vdev->flags & VFIO_DEVICE_FLAGS_CCW)) {
350c14e706cSDong Jia Shi         error_setg(errp, "vfio: Um, this isn't a vfio-ccw device");
351c14e706cSDong Jia Shi         return;
352c14e706cSDong Jia Shi     }
353c14e706cSDong Jia Shi 
3548fadea24SCornelia Huck     /*
3558fadea24SCornelia Huck      * We always expect at least the I/O region to be present. We also
3568fadea24SCornelia Huck      * may have a variable number of regions governed by capabilities.
3578fadea24SCornelia Huck      */
358c14e706cSDong Jia Shi     if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) {
3598fadea24SCornelia Huck         error_setg(errp, "vfio: too few regions (%u), expected at least %u",
3608fadea24SCornelia Huck                    vdev->num_regions, VFIO_CCW_CONFIG_REGION_INDEX + 1);
361c14e706cSDong Jia Shi         return;
362c14e706cSDong Jia Shi     }
363c14e706cSDong Jia Shi 
364c14e706cSDong Jia Shi     ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info);
365c14e706cSDong Jia Shi     if (ret) {
366c14e706cSDong Jia Shi         error_setg_errno(errp, -ret, "vfio: Error getting config info");
367c14e706cSDong Jia Shi         return;
368c14e706cSDong Jia Shi     }
369c14e706cSDong Jia Shi 
370c14e706cSDong Jia Shi     vcdev->io_region_size = info->size;
371c14e706cSDong Jia Shi     if (sizeof(*vcdev->io_region) != vcdev->io_region_size) {
372c14e706cSDong Jia Shi         error_setg(errp, "vfio: Unexpected size of the I/O region");
373c14e706cSDong Jia Shi         g_free(info);
374c14e706cSDong Jia Shi         return;
375c14e706cSDong Jia Shi     }
376c14e706cSDong Jia Shi 
377c14e706cSDong Jia Shi     vcdev->io_region_offset = info->offset;
378c14e706cSDong Jia Shi     vcdev->io_region = g_malloc0(info->size);
379c14e706cSDong Jia Shi 
3808fadea24SCornelia Huck     /* check for the optional async command region */
3818fadea24SCornelia Huck     ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW,
3828fadea24SCornelia Huck                                    VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD, &info);
3838fadea24SCornelia Huck     if (!ret) {
3848fadea24SCornelia Huck         vcdev->async_cmd_region_size = info->size;
3858fadea24SCornelia Huck         if (sizeof(*vcdev->async_cmd_region) != vcdev->async_cmd_region_size) {
3868fadea24SCornelia Huck             error_setg(errp, "vfio: Unexpected size of the async cmd region");
3878fadea24SCornelia Huck             g_free(vcdev->io_region);
3888fadea24SCornelia Huck             g_free(info);
3898fadea24SCornelia Huck             return;
3908fadea24SCornelia Huck         }
3918fadea24SCornelia Huck         vcdev->async_cmd_region_offset = info->offset;
3928fadea24SCornelia Huck         vcdev->async_cmd_region = g_malloc0(info->size);
3938fadea24SCornelia Huck     }
3948fadea24SCornelia Huck 
395c14e706cSDong Jia Shi     g_free(info);
396c14e706cSDong Jia Shi }
397c14e706cSDong Jia Shi 
398c14e706cSDong Jia Shi static void vfio_ccw_put_region(VFIOCCWDevice *vcdev)
399c14e706cSDong Jia Shi {
4008fadea24SCornelia Huck     g_free(vcdev->async_cmd_region);
401c14e706cSDong Jia Shi     g_free(vcdev->io_region);
402c14e706cSDong Jia Shi }
403c14e706cSDong Jia Shi 
404c96f2c2aSGreg Kurz static void vfio_ccw_put_device(VFIOCCWDevice *vcdev)
4051dcac3e1SXiao Feng Ren {
4061dcac3e1SXiao Feng Ren     g_free(vcdev->vdev.name);
4071dcac3e1SXiao Feng Ren     vfio_put_base_device(&vcdev->vdev);
4081dcac3e1SXiao Feng Ren }
4091dcac3e1SXiao Feng Ren 
410c96f2c2aSGreg Kurz static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
411c96f2c2aSGreg Kurz                                 Error **errp)
412c96f2c2aSGreg Kurz {
413c96f2c2aSGreg Kurz     char *name = g_strdup_printf("%x.%x.%04x", vcdev->cdev.hostid.cssid,
414c96f2c2aSGreg Kurz                                  vcdev->cdev.hostid.ssid,
415c96f2c2aSGreg Kurz                                  vcdev->cdev.hostid.devid);
416c96f2c2aSGreg Kurz     VFIODevice *vbasedev;
417c96f2c2aSGreg Kurz 
418c96f2c2aSGreg Kurz     QLIST_FOREACH(vbasedev, &group->device_list, next) {
419c96f2c2aSGreg Kurz         if (strcmp(vbasedev->name, name) == 0) {
420c96f2c2aSGreg Kurz             error_setg(errp, "vfio: subchannel %s has already been attached",
421c96f2c2aSGreg Kurz                        name);
422c96f2c2aSGreg Kurz             goto out_err;
423c96f2c2aSGreg Kurz         }
424c96f2c2aSGreg Kurz     }
425c96f2c2aSGreg Kurz 
426238e9172SAlex Williamson     /*
427238e9172SAlex Williamson      * All vfio-ccw devices are believed to operate in a way compatible with
428238e9172SAlex Williamson      * memory ballooning, ie. pages pinned in the host are in the current
429238e9172SAlex Williamson      * working set of the guest driver and therefore never overlap with pages
430238e9172SAlex Williamson      * available to the guest balloon driver.  This needs to be set before
431238e9172SAlex Williamson      * vfio_get_device() for vfio common to handle the balloon inhibitor.
432238e9172SAlex Williamson      */
433238e9172SAlex Williamson     vcdev->vdev.balloon_allowed = true;
434238e9172SAlex Williamson 
435c96f2c2aSGreg Kurz     if (vfio_get_device(group, vcdev->cdev.mdevid, &vcdev->vdev, errp)) {
436c96f2c2aSGreg Kurz         goto out_err;
437c96f2c2aSGreg Kurz     }
438c96f2c2aSGreg Kurz 
439c96f2c2aSGreg Kurz     vcdev->vdev.ops = &vfio_ccw_ops;
440c96f2c2aSGreg Kurz     vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW;
441c96f2c2aSGreg Kurz     vcdev->vdev.name = name;
442c96f2c2aSGreg Kurz     vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj;
443c96f2c2aSGreg Kurz 
444c96f2c2aSGreg Kurz     return;
445c96f2c2aSGreg Kurz 
446c96f2c2aSGreg Kurz out_err:
447c96f2c2aSGreg Kurz     g_free(name);
448c96f2c2aSGreg Kurz }
449c96f2c2aSGreg Kurz 
4501dcac3e1SXiao Feng Ren static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp)
4511dcac3e1SXiao Feng Ren {
4521dcac3e1SXiao Feng Ren     char *tmp, group_path[PATH_MAX];
4531dcac3e1SXiao Feng Ren     ssize_t len;
4541dcac3e1SXiao Feng Ren     int groupid;
4551dcac3e1SXiao Feng Ren 
4561dcac3e1SXiao Feng Ren     tmp = g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group",
4571dcac3e1SXiao Feng Ren                           cdev->hostid.cssid, cdev->hostid.ssid,
4581dcac3e1SXiao Feng Ren                           cdev->hostid.devid, cdev->mdevid);
4591dcac3e1SXiao Feng Ren     len = readlink(tmp, group_path, sizeof(group_path));
4601dcac3e1SXiao Feng Ren     g_free(tmp);
4611dcac3e1SXiao Feng Ren 
4621dcac3e1SXiao Feng Ren     if (len <= 0 || len >= sizeof(group_path)) {
4631dcac3e1SXiao Feng Ren         error_setg(errp, "vfio: no iommu_group found");
4641dcac3e1SXiao Feng Ren         return NULL;
4651dcac3e1SXiao Feng Ren     }
4661dcac3e1SXiao Feng Ren 
4671dcac3e1SXiao Feng Ren     group_path[len] = 0;
4681dcac3e1SXiao Feng Ren 
4691dcac3e1SXiao Feng Ren     if (sscanf(basename(group_path), "%d", &groupid) != 1) {
4701dcac3e1SXiao Feng Ren         error_setg(errp, "vfio: failed to read %s", group_path);
4711dcac3e1SXiao Feng Ren         return NULL;
4721dcac3e1SXiao Feng Ren     }
4731dcac3e1SXiao Feng Ren 
4741dcac3e1SXiao Feng Ren     return vfio_get_group(groupid, &address_space_memory, errp);
4751dcac3e1SXiao Feng Ren }
4761dcac3e1SXiao Feng Ren 
4771dcac3e1SXiao Feng Ren static void vfio_ccw_realize(DeviceState *dev, Error **errp)
4781dcac3e1SXiao Feng Ren {
4791dcac3e1SXiao Feng Ren     VFIOGroup *group;
4801dcac3e1SXiao Feng Ren     CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
4811dcac3e1SXiao Feng Ren     S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
4821dcac3e1SXiao Feng Ren     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
4831dcac3e1SXiao Feng Ren     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
4841dcac3e1SXiao Feng Ren     Error *err = NULL;
4851dcac3e1SXiao Feng Ren 
4861dcac3e1SXiao Feng Ren     /* Call the class init function for subchannel. */
4871dcac3e1SXiao Feng Ren     if (cdc->realize) {
4881dcac3e1SXiao Feng Ren         cdc->realize(cdev, vcdev->vdev.sysfsdev, &err);
4891dcac3e1SXiao Feng Ren         if (err) {
4901dcac3e1SXiao Feng Ren             goto out_err_propagate;
4911dcac3e1SXiao Feng Ren         }
4921dcac3e1SXiao Feng Ren     }
4931dcac3e1SXiao Feng Ren 
4941dcac3e1SXiao Feng Ren     group = vfio_ccw_get_group(cdev, &err);
4951dcac3e1SXiao Feng Ren     if (!group) {
4961dcac3e1SXiao Feng Ren         goto out_group_err;
4971dcac3e1SXiao Feng Ren     }
4981dcac3e1SXiao Feng Ren 
499c96f2c2aSGreg Kurz     vfio_ccw_get_device(group, vcdev, &err);
500c96f2c2aSGreg Kurz     if (err) {
5011dcac3e1SXiao Feng Ren         goto out_device_err;
5021dcac3e1SXiao Feng Ren     }
5031dcac3e1SXiao Feng Ren 
504c14e706cSDong Jia Shi     vfio_ccw_get_region(vcdev, &err);
505c14e706cSDong Jia Shi     if (err) {
506c14e706cSDong Jia Shi         goto out_region_err;
507c14e706cSDong Jia Shi     }
508c14e706cSDong Jia Shi 
5094886b3e9SDong Jia Shi     vfio_ccw_register_io_notifier(vcdev, &err);
5104886b3e9SDong Jia Shi     if (err) {
5114886b3e9SDong Jia Shi         goto out_notifier_err;
5124886b3e9SDong Jia Shi     }
5134886b3e9SDong Jia Shi 
5141dcac3e1SXiao Feng Ren     return;
5151dcac3e1SXiao Feng Ren 
5164886b3e9SDong Jia Shi out_notifier_err:
5174886b3e9SDong Jia Shi     vfio_ccw_put_region(vcdev);
518c14e706cSDong Jia Shi out_region_err:
519c96f2c2aSGreg Kurz     vfio_ccw_put_device(vcdev);
5201dcac3e1SXiao Feng Ren out_device_err:
5211dcac3e1SXiao Feng Ren     vfio_put_group(group);
5221dcac3e1SXiao Feng Ren out_group_err:
5231dcac3e1SXiao Feng Ren     if (cdc->unrealize) {
5241dcac3e1SXiao Feng Ren         cdc->unrealize(cdev, NULL);
5251dcac3e1SXiao Feng Ren     }
5261dcac3e1SXiao Feng Ren out_err_propagate:
5271dcac3e1SXiao Feng Ren     error_propagate(errp, err);
5281dcac3e1SXiao Feng Ren }
5291dcac3e1SXiao Feng Ren 
5301dcac3e1SXiao Feng Ren static void vfio_ccw_unrealize(DeviceState *dev, Error **errp)
5311dcac3e1SXiao Feng Ren {
5321dcac3e1SXiao Feng Ren     CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
5331dcac3e1SXiao Feng Ren     S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
5341dcac3e1SXiao Feng Ren     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
5351dcac3e1SXiao Feng Ren     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
5361dcac3e1SXiao Feng Ren     VFIOGroup *group = vcdev->vdev.group;
5371dcac3e1SXiao Feng Ren 
5384886b3e9SDong Jia Shi     vfio_ccw_unregister_io_notifier(vcdev);
539c14e706cSDong Jia Shi     vfio_ccw_put_region(vcdev);
540c96f2c2aSGreg Kurz     vfio_ccw_put_device(vcdev);
5411dcac3e1SXiao Feng Ren     vfio_put_group(group);
5421dcac3e1SXiao Feng Ren 
5431dcac3e1SXiao Feng Ren     if (cdc->unrealize) {
5441dcac3e1SXiao Feng Ren         cdc->unrealize(cdev, errp);
5451dcac3e1SXiao Feng Ren     }
5461dcac3e1SXiao Feng Ren }
5471dcac3e1SXiao Feng Ren 
5481dcac3e1SXiao Feng Ren static Property vfio_ccw_properties[] = {
5491dcac3e1SXiao Feng Ren     DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev),
5509a51c9eeSHalil Pasic     DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice, force_orb_pfch, false),
5511dcac3e1SXiao Feng Ren     DEFINE_PROP_END_OF_LIST(),
5521dcac3e1SXiao Feng Ren };
5531dcac3e1SXiao Feng Ren 
5541dcac3e1SXiao Feng Ren static const VMStateDescription vfio_ccw_vmstate = {
555da56e330SLi Qiang     .name = "vfio-ccw",
5561dcac3e1SXiao Feng Ren     .unmigratable = 1,
5571dcac3e1SXiao Feng Ren };
5581dcac3e1SXiao Feng Ren 
5591dcac3e1SXiao Feng Ren static void vfio_ccw_class_init(ObjectClass *klass, void *data)
5601dcac3e1SXiao Feng Ren {
5611dcac3e1SXiao Feng Ren     DeviceClass *dc = DEVICE_CLASS(klass);
5628ca2b376SXiao Feng Ren     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
5631dcac3e1SXiao Feng Ren 
5641dcac3e1SXiao Feng Ren     dc->props = vfio_ccw_properties;
5651dcac3e1SXiao Feng Ren     dc->vmsd = &vfio_ccw_vmstate;
5661dcac3e1SXiao Feng Ren     dc->desc = "VFIO-based subchannel assignment";
567bd2aef10SCornelia Huck     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
5681dcac3e1SXiao Feng Ren     dc->realize = vfio_ccw_realize;
5691dcac3e1SXiao Feng Ren     dc->unrealize = vfio_ccw_unrealize;
5701dcac3e1SXiao Feng Ren     dc->reset = vfio_ccw_reset;
5718ca2b376SXiao Feng Ren 
5728ca2b376SXiao Feng Ren     cdc->handle_request = vfio_ccw_handle_request;
5738fadea24SCornelia Huck     cdc->handle_halt = vfio_ccw_handle_halt;
5748fadea24SCornelia Huck     cdc->handle_clear = vfio_ccw_handle_clear;
5751dcac3e1SXiao Feng Ren }
5761dcac3e1SXiao Feng Ren 
5771dcac3e1SXiao Feng Ren static const TypeInfo vfio_ccw_info = {
5781dcac3e1SXiao Feng Ren     .name = TYPE_VFIO_CCW,
5791dcac3e1SXiao Feng Ren     .parent = TYPE_S390_CCW,
5801dcac3e1SXiao Feng Ren     .instance_size = sizeof(VFIOCCWDevice),
5811dcac3e1SXiao Feng Ren     .class_init = vfio_ccw_class_init,
5821dcac3e1SXiao Feng Ren };
5831dcac3e1SXiao Feng Ren 
5841dcac3e1SXiao Feng Ren static void register_vfio_ccw_type(void)
5851dcac3e1SXiao Feng Ren {
5861dcac3e1SXiao Feng Ren     type_register_static(&vfio_ccw_info);
5871dcac3e1SXiao Feng Ren }
5881dcac3e1SXiao Feng Ren 
5891dcac3e1SXiao Feng Ren type_init(register_vfio_ccw_type)
590