xref: /openbmc/qemu/hw/vfio/ccw.c (revision d6cd66311f527ee29c1d7b0988059cda00ad92fa)
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"
28a27bd6c7SMarkus 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;
4446ea3841SFarhan Ali     uint64_t schib_region_size;
4546ea3841SFarhan Ali     uint64_t schib_region_offset;
4646ea3841SFarhan Ali     struct ccw_schib_region *schib_region;
47f030532fSFarhan Ali     uint64_t crw_region_size;
48f030532fSFarhan Ali     uint64_t crw_region_offset;
49f030532fSFarhan Ali     struct ccw_crw_region *crw_region;
504886b3e9SDong Jia Shi     EventNotifier io_notifier;
51f030532fSFarhan Ali     EventNotifier crw_notifier;
52b2f96f9eSEric Farman     EventNotifier req_notifier;
539a51c9eeSHalil Pasic     bool force_orb_pfch;
549a51c9eeSHalil Pasic     bool warned_orb_pfch;
5544445d86SJason J. Herne };
561dcac3e1SXiao Feng Ren 
579a51c9eeSHalil Pasic static inline void warn_once_pfch(VFIOCCWDevice *vcdev, SubchDev *sch,
589a51c9eeSHalil Pasic                                   const char *msg)
599a51c9eeSHalil Pasic {
60c55510b7SCornelia Huck     warn_report_once_cond(&vcdev->warned_orb_pfch,
61c55510b7SCornelia Huck                           "vfio-ccw (devno %x.%x.%04x): %s",
629a51c9eeSHalil Pasic                           sch->cssid, sch->ssid, sch->devno, msg);
639a51c9eeSHalil Pasic }
649a51c9eeSHalil Pasic 
651dcac3e1SXiao Feng Ren static void vfio_ccw_compute_needs_reset(VFIODevice *vdev)
661dcac3e1SXiao Feng Ren {
671dcac3e1SXiao Feng Ren     vdev->needs_reset = false;
681dcac3e1SXiao Feng Ren }
691dcac3e1SXiao Feng Ren 
701dcac3e1SXiao Feng Ren /*
711dcac3e1SXiao Feng Ren  * We don't need vfio_hot_reset_multi and vfio_eoi operations for
721dcac3e1SXiao Feng Ren  * vfio_ccw device now.
731dcac3e1SXiao Feng Ren  */
741dcac3e1SXiao Feng Ren struct VFIODeviceOps vfio_ccw_ops = {
751dcac3e1SXiao Feng Ren     .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset,
761dcac3e1SXiao Feng Ren };
771dcac3e1SXiao Feng Ren 
7866dc50f7SHalil Pasic static IOInstEnding vfio_ccw_handle_request(SubchDev *sch)
798ca2b376SXiao Feng Ren {
8066dc50f7SHalil Pasic     S390CCWDevice *cdev = sch->driver_data;
818ca2b376SXiao Feng Ren     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
828ca2b376SXiao Feng Ren     struct ccw_io_region *region = vcdev->io_region;
838ca2b376SXiao Feng Ren     int ret;
848ca2b376SXiao Feng Ren 
8524e58a7bSJared Rossi     if (!(sch->orb.ctrl0 & ORB_CTRL0_MASK_PFCH) && vcdev->force_orb_pfch) {
869a51c9eeSHalil Pasic         sch->orb.ctrl0 |= ORB_CTRL0_MASK_PFCH;
879a51c9eeSHalil Pasic         warn_once_pfch(vcdev, sch, "PFCH flag forced");
889a51c9eeSHalil Pasic     }
899a51c9eeSHalil Pasic 
908ca2b376SXiao Feng Ren     QEMU_BUILD_BUG_ON(sizeof(region->orb_area) != sizeof(ORB));
918ca2b376SXiao Feng Ren     QEMU_BUILD_BUG_ON(sizeof(region->scsw_area) != sizeof(SCSW));
928ca2b376SXiao Feng Ren     QEMU_BUILD_BUG_ON(sizeof(region->irb_area) != sizeof(IRB));
938ca2b376SXiao Feng Ren 
948ca2b376SXiao Feng Ren     memset(region, 0, sizeof(*region));
958ca2b376SXiao Feng Ren 
9666dc50f7SHalil Pasic     memcpy(region->orb_area, &sch->orb, sizeof(ORB));
9766dc50f7SHalil Pasic     memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW));
988ca2b376SXiao Feng Ren 
998ca2b376SXiao Feng Ren again:
1008ca2b376SXiao Feng Ren     ret = pwrite(vcdev->vdev.fd, region,
1018ca2b376SXiao Feng Ren                  vcdev->io_region_size, vcdev->io_region_offset);
1028ca2b376SXiao Feng Ren     if (ret != vcdev->io_region_size) {
1038ca2b376SXiao Feng Ren         if (errno == EAGAIN) {
1048ca2b376SXiao Feng Ren             goto again;
1058ca2b376SXiao Feng Ren         }
10691f751dcSBoris Fiuczynski         error_report("vfio-ccw: write I/O region failed with errno=%d", errno);
107*d6cd6631SEric Farman         ret = errno ? -errno : -EFAULT;
10866dc50f7SHalil Pasic     } else {
109*d6cd6631SEric Farman         ret = 0;
1108ca2b376SXiao Feng Ren     }
11166dc50f7SHalil Pasic     switch (ret) {
11266dc50f7SHalil Pasic     case 0:
11366dc50f7SHalil Pasic         return IOINST_CC_EXPECTED;
11466dc50f7SHalil Pasic     case -EBUSY:
11566dc50f7SHalil Pasic         return IOINST_CC_BUSY;
11666dc50f7SHalil Pasic     case -ENODEV:
11766dc50f7SHalil Pasic     case -EACCES:
11866dc50f7SHalil Pasic         return IOINST_CC_NOT_OPERATIONAL;
11966dc50f7SHalil Pasic     case -EFAULT:
12066dc50f7SHalil Pasic     default:
12166dc50f7SHalil Pasic         sch_gen_unit_exception(sch);
12266dc50f7SHalil Pasic         css_inject_io_interrupt(sch);
12366dc50f7SHalil Pasic         return IOINST_CC_EXPECTED;
12466dc50f7SHalil Pasic     }
1258ca2b376SXiao Feng Ren }
1268ca2b376SXiao Feng Ren 
12746ea3841SFarhan Ali static IOInstEnding vfio_ccw_handle_store(SubchDev *sch)
12846ea3841SFarhan Ali {
12946ea3841SFarhan Ali     S390CCWDevice *cdev = sch->driver_data;
13046ea3841SFarhan Ali     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
13146ea3841SFarhan Ali     SCHIB *schib = &sch->curr_status;
13246ea3841SFarhan Ali     struct ccw_schib_region *region = vcdev->schib_region;
13346ea3841SFarhan Ali     SCHIB *s;
13446ea3841SFarhan Ali     int ret;
13546ea3841SFarhan Ali 
13646ea3841SFarhan Ali     /* schib region not available so nothing else to do */
13746ea3841SFarhan Ali     if (!region) {
13846ea3841SFarhan Ali         return IOINST_CC_EXPECTED;
13946ea3841SFarhan Ali     }
14046ea3841SFarhan Ali 
14146ea3841SFarhan Ali     memset(region, 0, sizeof(*region));
14246ea3841SFarhan Ali     ret = pread(vcdev->vdev.fd, region, vcdev->schib_region_size,
14346ea3841SFarhan Ali                 vcdev->schib_region_offset);
14446ea3841SFarhan Ali 
14546ea3841SFarhan Ali     if (ret == -1) {
14646ea3841SFarhan Ali         /*
14746ea3841SFarhan Ali          * Device is probably damaged, but store subchannel does not
14846ea3841SFarhan Ali          * have a nonzero cc defined for this scenario.  Log an error,
14946ea3841SFarhan Ali          * and presume things are otherwise fine.
15046ea3841SFarhan Ali          */
15146ea3841SFarhan Ali         error_report("vfio-ccw: store region read failed with errno=%d", errno);
15246ea3841SFarhan Ali         return IOINST_CC_EXPECTED;
15346ea3841SFarhan Ali     }
15446ea3841SFarhan Ali 
15546ea3841SFarhan Ali     /*
15646ea3841SFarhan Ali      * Selectively copy path-related bits of the SCHIB,
15746ea3841SFarhan Ali      * rather than copying the entire struct.
15846ea3841SFarhan Ali      */
15946ea3841SFarhan Ali     s = (SCHIB *)region->schib_area;
16046ea3841SFarhan Ali     schib->pmcw.pnom = s->pmcw.pnom;
16146ea3841SFarhan Ali     schib->pmcw.lpum = s->pmcw.lpum;
16246ea3841SFarhan Ali     schib->pmcw.pam = s->pmcw.pam;
16346ea3841SFarhan Ali     schib->pmcw.pom = s->pmcw.pom;
16446ea3841SFarhan Ali 
16546ea3841SFarhan Ali     if (s->scsw.flags & SCSW_FLAGS_MASK_PNO) {
16646ea3841SFarhan Ali         schib->scsw.flags |= SCSW_FLAGS_MASK_PNO;
16746ea3841SFarhan Ali     }
16846ea3841SFarhan Ali 
16946ea3841SFarhan Ali     return IOINST_CC_EXPECTED;
17046ea3841SFarhan Ali }
17146ea3841SFarhan Ali 
1728fadea24SCornelia Huck static int vfio_ccw_handle_clear(SubchDev *sch)
1738fadea24SCornelia Huck {
1748fadea24SCornelia Huck     S390CCWDevice *cdev = sch->driver_data;
1758fadea24SCornelia Huck     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
1768fadea24SCornelia Huck     struct ccw_cmd_region *region = vcdev->async_cmd_region;
1778fadea24SCornelia Huck     int ret;
1788fadea24SCornelia Huck 
1798fadea24SCornelia Huck     if (!vcdev->async_cmd_region) {
1808fadea24SCornelia Huck         /* Async command region not available, fall back to emulation */
1818fadea24SCornelia Huck         return -ENOSYS;
1828fadea24SCornelia Huck     }
1838fadea24SCornelia Huck 
1848fadea24SCornelia Huck     memset(region, 0, sizeof(*region));
1858fadea24SCornelia Huck     region->command = VFIO_CCW_ASYNC_CMD_CSCH;
1868fadea24SCornelia Huck 
1878fadea24SCornelia Huck again:
1888fadea24SCornelia Huck     ret = pwrite(vcdev->vdev.fd, region,
1898fadea24SCornelia Huck                  vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset);
1908fadea24SCornelia Huck     if (ret != vcdev->async_cmd_region_size) {
1918fadea24SCornelia Huck         if (errno == EAGAIN) {
1928fadea24SCornelia Huck             goto again;
1938fadea24SCornelia Huck         }
1948fadea24SCornelia Huck         error_report("vfio-ccw: write cmd region failed with errno=%d", errno);
195*d6cd6631SEric Farman         ret = errno ? -errno : -EFAULT;
1968fadea24SCornelia Huck     } else {
197*d6cd6631SEric Farman         ret = 0;
1988fadea24SCornelia Huck     }
1998fadea24SCornelia Huck     switch (ret) {
2008fadea24SCornelia Huck     case 0:
2018fadea24SCornelia Huck     case -ENODEV:
2028fadea24SCornelia Huck     case -EACCES:
2038fadea24SCornelia Huck         return 0;
2048fadea24SCornelia Huck     case -EFAULT:
2058fadea24SCornelia Huck     default:
2068fadea24SCornelia Huck         sch_gen_unit_exception(sch);
2078fadea24SCornelia Huck         css_inject_io_interrupt(sch);
2088fadea24SCornelia Huck         return 0;
2098fadea24SCornelia Huck     }
2108fadea24SCornelia Huck }
2118fadea24SCornelia Huck 
2128fadea24SCornelia Huck static int vfio_ccw_handle_halt(SubchDev *sch)
2138fadea24SCornelia Huck {
2148fadea24SCornelia Huck     S390CCWDevice *cdev = sch->driver_data;
2158fadea24SCornelia Huck     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
2168fadea24SCornelia Huck     struct ccw_cmd_region *region = vcdev->async_cmd_region;
2178fadea24SCornelia Huck     int ret;
2188fadea24SCornelia Huck 
2198fadea24SCornelia Huck     if (!vcdev->async_cmd_region) {
2208fadea24SCornelia Huck         /* Async command region not available, fall back to emulation */
2218fadea24SCornelia Huck         return -ENOSYS;
2228fadea24SCornelia Huck     }
2238fadea24SCornelia Huck 
2248fadea24SCornelia Huck     memset(region, 0, sizeof(*region));
2258fadea24SCornelia Huck     region->command = VFIO_CCW_ASYNC_CMD_HSCH;
2268fadea24SCornelia Huck 
2278fadea24SCornelia Huck again:
2288fadea24SCornelia Huck     ret = pwrite(vcdev->vdev.fd, region,
2298fadea24SCornelia Huck                  vcdev->async_cmd_region_size, vcdev->async_cmd_region_offset);
2308fadea24SCornelia Huck     if (ret != vcdev->async_cmd_region_size) {
2318fadea24SCornelia Huck         if (errno == EAGAIN) {
2328fadea24SCornelia Huck             goto again;
2338fadea24SCornelia Huck         }
2348fadea24SCornelia Huck         error_report("vfio-ccw: write cmd region failed with errno=%d", errno);
235*d6cd6631SEric Farman         ret = errno ? -errno : -EFAULT;
2368fadea24SCornelia Huck     } else {
237*d6cd6631SEric Farman         ret = 0;
2388fadea24SCornelia Huck     }
2398fadea24SCornelia Huck     switch (ret) {
2408fadea24SCornelia Huck     case 0:
2418fadea24SCornelia Huck     case -EBUSY:
2428fadea24SCornelia Huck     case -ENODEV:
2438fadea24SCornelia Huck     case -EACCES:
2448fadea24SCornelia Huck         return 0;
2458fadea24SCornelia Huck     case -EFAULT:
2468fadea24SCornelia Huck     default:
2478fadea24SCornelia Huck         sch_gen_unit_exception(sch);
2488fadea24SCornelia Huck         css_inject_io_interrupt(sch);
2498fadea24SCornelia Huck         return 0;
2508fadea24SCornelia Huck     }
2518fadea24SCornelia Huck }
2528fadea24SCornelia Huck 
2531dcac3e1SXiao Feng Ren static void vfio_ccw_reset(DeviceState *dev)
2541dcac3e1SXiao Feng Ren {
2551dcac3e1SXiao Feng Ren     CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
2561dcac3e1SXiao Feng Ren     S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
2571dcac3e1SXiao Feng Ren     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
2581dcac3e1SXiao Feng Ren 
2591dcac3e1SXiao Feng Ren     ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET);
2601dcac3e1SXiao Feng Ren }
2611dcac3e1SXiao Feng Ren 
262f030532fSFarhan Ali static void vfio_ccw_crw_read(VFIOCCWDevice *vcdev)
263f030532fSFarhan Ali {
264f030532fSFarhan Ali     struct ccw_crw_region *region = vcdev->crw_region;
265f030532fSFarhan Ali     CRW crw;
266f030532fSFarhan Ali     int size;
267f030532fSFarhan Ali 
268f030532fSFarhan Ali     /* Keep reading CRWs as long as data is returned */
269f030532fSFarhan Ali     do {
270f030532fSFarhan Ali         memset(region, 0, sizeof(*region));
271f030532fSFarhan Ali         size = pread(vcdev->vdev.fd, region, vcdev->crw_region_size,
272f030532fSFarhan Ali                      vcdev->crw_region_offset);
273f030532fSFarhan Ali 
274f030532fSFarhan Ali         if (size == -1) {
275f030532fSFarhan Ali             error_report("vfio-ccw: Read crw region failed with errno=%d",
276f030532fSFarhan Ali                          errno);
277f030532fSFarhan Ali             break;
278f030532fSFarhan Ali         }
279f030532fSFarhan Ali 
280f030532fSFarhan Ali         if (region->crw == 0) {
281f030532fSFarhan Ali             /* No more CRWs to queue */
282f030532fSFarhan Ali             break;
283f030532fSFarhan Ali         }
284f030532fSFarhan Ali 
285f030532fSFarhan Ali         memcpy(&crw, &region->crw, sizeof(CRW));
286f030532fSFarhan Ali 
287f030532fSFarhan Ali         css_crw_add_to_queue(crw);
288f030532fSFarhan Ali     } while (1);
289f030532fSFarhan Ali }
290f030532fSFarhan Ali 
291b2f96f9eSEric Farman static void vfio_ccw_req_notifier_handler(void *opaque)
292b2f96f9eSEric Farman {
293b2f96f9eSEric Farman     VFIOCCWDevice *vcdev = opaque;
294b2f96f9eSEric Farman     Error *err = NULL;
295b2f96f9eSEric Farman 
296b2f96f9eSEric Farman     if (!event_notifier_test_and_clear(&vcdev->req_notifier)) {
297b2f96f9eSEric Farman         return;
298b2f96f9eSEric Farman     }
299b2f96f9eSEric Farman 
300b2f96f9eSEric Farman     qdev_unplug(DEVICE(vcdev), &err);
301b2f96f9eSEric Farman     if (err) {
302b2f96f9eSEric Farman         warn_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name);
303b2f96f9eSEric Farman     }
304b2f96f9eSEric Farman }
305b2f96f9eSEric Farman 
306f030532fSFarhan Ali static void vfio_ccw_crw_notifier_handler(void *opaque)
307f030532fSFarhan Ali {
308f030532fSFarhan Ali     VFIOCCWDevice *vcdev = opaque;
309f030532fSFarhan Ali 
310f030532fSFarhan Ali     while (event_notifier_test_and_clear(&vcdev->crw_notifier)) {
311f030532fSFarhan Ali         vfio_ccw_crw_read(vcdev);
312f030532fSFarhan Ali     }
313f030532fSFarhan Ali }
314f030532fSFarhan Ali 
3154886b3e9SDong Jia Shi static void vfio_ccw_io_notifier_handler(void *opaque)
3164886b3e9SDong Jia Shi {
3174886b3e9SDong Jia Shi     VFIOCCWDevice *vcdev = opaque;
3188ca2b376SXiao Feng Ren     struct ccw_io_region *region = vcdev->io_region;
3198ca2b376SXiao Feng Ren     S390CCWDevice *cdev = S390_CCW_DEVICE(vcdev);
3208ca2b376SXiao Feng Ren     CcwDevice *ccw_dev = CCW_DEVICE(cdev);
3218ca2b376SXiao Feng Ren     SubchDev *sch = ccw_dev->sch;
322e1d0b372SDaniel P. Berrangé     SCHIB *schib = &sch->curr_status;
323e1d0b372SDaniel P. Berrangé     SCSW s;
3248ca2b376SXiao Feng Ren     IRB irb;
3258ca2b376SXiao Feng Ren     int size;
3264886b3e9SDong Jia Shi 
3274886b3e9SDong Jia Shi     if (!event_notifier_test_and_clear(&vcdev->io_notifier)) {
3284886b3e9SDong Jia Shi         return;
3294886b3e9SDong Jia Shi     }
3308ca2b376SXiao Feng Ren 
3318ca2b376SXiao Feng Ren     size = pread(vcdev->vdev.fd, region, vcdev->io_region_size,
3328ca2b376SXiao Feng Ren                  vcdev->io_region_offset);
3338ca2b376SXiao Feng Ren     if (size == -1) {
3348ca2b376SXiao Feng Ren         switch (errno) {
3358ca2b376SXiao Feng Ren         case ENODEV:
3368ca2b376SXiao Feng Ren             /* Generate a deferred cc 3 condition. */
337e1d0b372SDaniel P. Berrangé             schib->scsw.flags |= SCSW_FLAGS_MASK_CC;
338e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
339e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND);
3408ca2b376SXiao Feng Ren             goto read_err;
3418ca2b376SXiao Feng Ren         case EFAULT:
3428ca2b376SXiao Feng Ren             /* Memory problem, generate channel data check. */
343e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
344e1d0b372SDaniel P. Berrangé             schib->scsw.cstat = SCSW_CSTAT_DATA_CHECK;
345e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
346e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
3478ca2b376SXiao Feng Ren                        SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
3488ca2b376SXiao Feng Ren             goto read_err;
3498ca2b376SXiao Feng Ren         default:
3508ca2b376SXiao Feng Ren             /* Error, generate channel program check. */
351e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
352e1d0b372SDaniel P. Berrangé             schib->scsw.cstat = SCSW_CSTAT_PROG_CHECK;
353e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
354e1d0b372SDaniel P. Berrangé             schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
3558ca2b376SXiao Feng Ren                        SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
3568ca2b376SXiao Feng Ren             goto read_err;
3578ca2b376SXiao Feng Ren         }
3588ca2b376SXiao Feng Ren     } else if (size != vcdev->io_region_size) {
3598ca2b376SXiao Feng Ren         /* Information transfer error, generate channel-control check. */
360e1d0b372SDaniel P. Berrangé         schib->scsw.ctrl &= ~SCSW_ACTL_START_PEND;
361e1d0b372SDaniel P. Berrangé         schib->scsw.cstat = SCSW_CSTAT_CHN_CTRL_CHK;
362e1d0b372SDaniel P. Berrangé         schib->scsw.ctrl &= ~SCSW_CTRL_MASK_STCTL;
363e1d0b372SDaniel P. Berrangé         schib->scsw.ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
3648ca2b376SXiao Feng Ren                    SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
3658ca2b376SXiao Feng Ren         goto read_err;
3668ca2b376SXiao Feng Ren     }
3678ca2b376SXiao Feng Ren 
3688ca2b376SXiao Feng Ren     memcpy(&irb, region->irb_area, sizeof(IRB));
3698ca2b376SXiao Feng Ren 
3708ca2b376SXiao Feng Ren     /* Update control block via irb. */
371e1d0b372SDaniel P. Berrangé     s = schib->scsw;
372e1d0b372SDaniel P. Berrangé     copy_scsw_to_guest(&s, &irb.scsw);
373e1d0b372SDaniel P. Berrangé     schib->scsw = s;
3748ca2b376SXiao Feng Ren 
375334e7685SDong Jia Shi     /* If a uint check is pending, copy sense data. */
376e1d0b372SDaniel P. Berrangé     if ((schib->scsw.dstat & SCSW_DSTAT_UNIT_CHECK) &&
377e1d0b372SDaniel P. Berrangé         (schib->pmcw.chars & PMCW_CHARS_MASK_CSENSE)) {
378334e7685SDong Jia Shi         memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw));
379334e7685SDong Jia Shi     }
380334e7685SDong Jia Shi 
3818ca2b376SXiao Feng Ren read_err:
3828ca2b376SXiao Feng Ren     css_inject_io_interrupt(sch);
3834886b3e9SDong Jia Shi }
3844886b3e9SDong Jia Shi 
385690e29b9SEric Farman static void vfio_ccw_register_irq_notifier(VFIOCCWDevice *vcdev,
386690e29b9SEric Farman                                            unsigned int irq,
387690e29b9SEric Farman                                            Error **errp)
3884886b3e9SDong Jia Shi {
3894886b3e9SDong Jia Shi     VFIODevice *vdev = &vcdev->vdev;
3904886b3e9SDong Jia Shi     struct vfio_irq_info *irq_info;
3914886b3e9SDong Jia Shi     size_t argsz;
392b5e89f04SCornelia Huck     int fd;
393690e29b9SEric Farman     EventNotifier *notifier;
394690e29b9SEric Farman     IOHandler *fd_read;
3954886b3e9SDong Jia Shi 
396690e29b9SEric Farman     switch (irq) {
397690e29b9SEric Farman     case VFIO_CCW_IO_IRQ_INDEX:
398690e29b9SEric Farman         notifier = &vcdev->io_notifier;
399690e29b9SEric Farman         fd_read = vfio_ccw_io_notifier_handler;
400690e29b9SEric Farman         break;
401f030532fSFarhan Ali     case VFIO_CCW_CRW_IRQ_INDEX:
402f030532fSFarhan Ali         notifier = &vcdev->crw_notifier;
403f030532fSFarhan Ali         fd_read = vfio_ccw_crw_notifier_handler;
404f030532fSFarhan Ali         break;
405b2f96f9eSEric Farman     case VFIO_CCW_REQ_IRQ_INDEX:
406b2f96f9eSEric Farman         notifier = &vcdev->req_notifier;
407b2f96f9eSEric Farman         fd_read = vfio_ccw_req_notifier_handler;
408b2f96f9eSEric Farman         break;
409690e29b9SEric Farman     default:
410690e29b9SEric Farman         error_setg(errp, "vfio: Unsupported device irq(%d)", irq);
411690e29b9SEric Farman         return;
412690e29b9SEric Farman     }
413690e29b9SEric Farman 
414690e29b9SEric Farman     if (vdev->num_irqs < irq + 1) {
415690e29b9SEric Farman         error_setg(errp, "vfio: unexpected number of irqs %u",
4164886b3e9SDong Jia Shi                    vdev->num_irqs);
4174886b3e9SDong Jia Shi         return;
4184886b3e9SDong Jia Shi     }
4194886b3e9SDong Jia Shi 
42028e22d4bSJing Zhang     argsz = sizeof(*irq_info);
4214886b3e9SDong Jia Shi     irq_info = g_malloc0(argsz);
422690e29b9SEric Farman     irq_info->index = irq;
4234886b3e9SDong Jia Shi     irq_info->argsz = argsz;
4244886b3e9SDong Jia Shi     if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO,
4254886b3e9SDong Jia Shi               irq_info) < 0 || irq_info->count < 1) {
4264886b3e9SDong Jia Shi         error_setg_errno(errp, errno, "vfio: Error getting irq info");
4274886b3e9SDong Jia Shi         goto out_free_info;
4284886b3e9SDong Jia Shi     }
4294886b3e9SDong Jia Shi 
430690e29b9SEric Farman     if (event_notifier_init(notifier, 0)) {
4314886b3e9SDong Jia Shi         error_setg_errno(errp, errno,
432690e29b9SEric Farman                          "vfio: Unable to init event notifier for irq (%d)",
433690e29b9SEric Farman                          irq);
4344886b3e9SDong Jia Shi         goto out_free_info;
4354886b3e9SDong Jia Shi     }
4364886b3e9SDong Jia Shi 
437690e29b9SEric Farman     fd = event_notifier_get_fd(notifier);
438690e29b9SEric Farman     qemu_set_fd_handler(fd, fd_read, NULL, vcdev);
4394886b3e9SDong Jia Shi 
440690e29b9SEric Farman     if (vfio_set_irq_signaling(vdev, irq, 0,
441b5e89f04SCornelia Huck                                VFIO_IRQ_SET_ACTION_TRIGGER, fd, errp)) {
442b5e89f04SCornelia Huck         qemu_set_fd_handler(fd, NULL, NULL, vcdev);
443690e29b9SEric Farman         event_notifier_cleanup(notifier);
4444886b3e9SDong Jia Shi     }
4454886b3e9SDong Jia Shi 
4464886b3e9SDong Jia Shi out_free_info:
4474886b3e9SDong Jia Shi     g_free(irq_info);
4484886b3e9SDong Jia Shi }
4494886b3e9SDong Jia Shi 
450690e29b9SEric Farman static void vfio_ccw_unregister_irq_notifier(VFIOCCWDevice *vcdev,
451690e29b9SEric Farman                                              unsigned int irq)
4524886b3e9SDong Jia Shi {
453b5e89f04SCornelia Huck     Error *err = NULL;
454690e29b9SEric Farman     EventNotifier *notifier;
4554886b3e9SDong Jia Shi 
456690e29b9SEric Farman     switch (irq) {
457690e29b9SEric Farman     case VFIO_CCW_IO_IRQ_INDEX:
458690e29b9SEric Farman         notifier = &vcdev->io_notifier;
459690e29b9SEric Farman         break;
460f030532fSFarhan Ali     case VFIO_CCW_CRW_IRQ_INDEX:
461f030532fSFarhan Ali         notifier = &vcdev->crw_notifier;
462f030532fSFarhan Ali         break;
463b2f96f9eSEric Farman     case VFIO_CCW_REQ_IRQ_INDEX:
464b2f96f9eSEric Farman         notifier = &vcdev->req_notifier;
465b2f96f9eSEric Farman         break;
466690e29b9SEric Farman     default:
467690e29b9SEric Farman         error_report("vfio: Unsupported device irq(%d)", irq);
468690e29b9SEric Farman         return;
469690e29b9SEric Farman     }
470690e29b9SEric Farman 
471690e29b9SEric Farman     if (vfio_set_irq_signaling(&vcdev->vdev, irq, 0,
472f5cf94cdSAlex Williamson                                VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err)) {
473b5e89f04SCornelia Huck         error_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name);
4744886b3e9SDong Jia Shi     }
4754886b3e9SDong Jia Shi 
476690e29b9SEric Farman     qemu_set_fd_handler(event_notifier_get_fd(notifier),
4774886b3e9SDong Jia Shi                         NULL, NULL, vcdev);
478690e29b9SEric Farman     event_notifier_cleanup(notifier);
4794886b3e9SDong Jia Shi }
4804886b3e9SDong Jia Shi 
481c14e706cSDong Jia Shi static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp)
482c14e706cSDong Jia Shi {
483c14e706cSDong Jia Shi     VFIODevice *vdev = &vcdev->vdev;
484c14e706cSDong Jia Shi     struct vfio_region_info *info;
485c14e706cSDong Jia Shi     int ret;
486c14e706cSDong Jia Shi 
487c14e706cSDong Jia Shi     /* Sanity check device */
488c14e706cSDong Jia Shi     if (!(vdev->flags & VFIO_DEVICE_FLAGS_CCW)) {
489c14e706cSDong Jia Shi         error_setg(errp, "vfio: Um, this isn't a vfio-ccw device");
490c14e706cSDong Jia Shi         return;
491c14e706cSDong Jia Shi     }
492c14e706cSDong Jia Shi 
4938fadea24SCornelia Huck     /*
4948fadea24SCornelia Huck      * We always expect at least the I/O region to be present. We also
4958fadea24SCornelia Huck      * may have a variable number of regions governed by capabilities.
4968fadea24SCornelia Huck      */
497c14e706cSDong Jia Shi     if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) {
4988fadea24SCornelia Huck         error_setg(errp, "vfio: too few regions (%u), expected at least %u",
4998fadea24SCornelia Huck                    vdev->num_regions, VFIO_CCW_CONFIG_REGION_INDEX + 1);
500c14e706cSDong Jia Shi         return;
501c14e706cSDong Jia Shi     }
502c14e706cSDong Jia Shi 
503c14e706cSDong Jia Shi     ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info);
504c14e706cSDong Jia Shi     if (ret) {
505c14e706cSDong Jia Shi         error_setg_errno(errp, -ret, "vfio: Error getting config info");
506c14e706cSDong Jia Shi         return;
507c14e706cSDong Jia Shi     }
508c14e706cSDong Jia Shi 
509c14e706cSDong Jia Shi     vcdev->io_region_size = info->size;
510c14e706cSDong Jia Shi     if (sizeof(*vcdev->io_region) != vcdev->io_region_size) {
511c14e706cSDong Jia Shi         error_setg(errp, "vfio: Unexpected size of the I/O region");
5122a3b9cbaSEric Farman         goto out_err;
513c14e706cSDong Jia Shi     }
514c14e706cSDong Jia Shi 
515c14e706cSDong Jia Shi     vcdev->io_region_offset = info->offset;
516c14e706cSDong Jia Shi     vcdev->io_region = g_malloc0(info->size);
517c8726f7bSCornelia Huck     g_free(info);
518c14e706cSDong Jia Shi 
5198fadea24SCornelia Huck     /* check for the optional async command region */
5208fadea24SCornelia Huck     ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW,
5218fadea24SCornelia Huck                                    VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD, &info);
5228fadea24SCornelia Huck     if (!ret) {
5238fadea24SCornelia Huck         vcdev->async_cmd_region_size = info->size;
5248fadea24SCornelia Huck         if (sizeof(*vcdev->async_cmd_region) != vcdev->async_cmd_region_size) {
5258fadea24SCornelia Huck             error_setg(errp, "vfio: Unexpected size of the async cmd region");
5262a3b9cbaSEric Farman             goto out_err;
5278fadea24SCornelia Huck         }
5288fadea24SCornelia Huck         vcdev->async_cmd_region_offset = info->offset;
5298fadea24SCornelia Huck         vcdev->async_cmd_region = g_malloc0(info->size);
530c8726f7bSCornelia Huck         g_free(info);
5318fadea24SCornelia Huck     }
5328fadea24SCornelia Huck 
53346ea3841SFarhan Ali     ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW,
53446ea3841SFarhan Ali                                    VFIO_REGION_SUBTYPE_CCW_SCHIB, &info);
53546ea3841SFarhan Ali     if (!ret) {
53646ea3841SFarhan Ali         vcdev->schib_region_size = info->size;
53746ea3841SFarhan Ali         if (sizeof(*vcdev->schib_region) != vcdev->schib_region_size) {
53846ea3841SFarhan Ali             error_setg(errp, "vfio: Unexpected size of the schib region");
53946ea3841SFarhan Ali             goto out_err;
54046ea3841SFarhan Ali         }
54146ea3841SFarhan Ali         vcdev->schib_region_offset = info->offset;
54246ea3841SFarhan Ali         vcdev->schib_region = g_malloc(info->size);
543c8726f7bSCornelia Huck         g_free(info);
54446ea3841SFarhan Ali     }
54546ea3841SFarhan Ali 
546f030532fSFarhan Ali     ret = vfio_get_dev_region_info(vdev, VFIO_REGION_TYPE_CCW,
547f030532fSFarhan Ali                                    VFIO_REGION_SUBTYPE_CCW_CRW, &info);
548f030532fSFarhan Ali 
549f030532fSFarhan Ali     if (!ret) {
550f030532fSFarhan Ali         vcdev->crw_region_size = info->size;
551f030532fSFarhan Ali         if (sizeof(*vcdev->crw_region) != vcdev->crw_region_size) {
552f030532fSFarhan Ali             error_setg(errp, "vfio: Unexpected size of the CRW region");
553f030532fSFarhan Ali             goto out_err;
554f030532fSFarhan Ali         }
555f030532fSFarhan Ali         vcdev->crw_region_offset = info->offset;
556f030532fSFarhan Ali         vcdev->crw_region = g_malloc(info->size);
557c8726f7bSCornelia Huck         g_free(info);
558f030532fSFarhan Ali     }
559f030532fSFarhan Ali 
5602a3b9cbaSEric Farman     return;
5612a3b9cbaSEric Farman 
5622a3b9cbaSEric Farman out_err:
563f030532fSFarhan Ali     g_free(vcdev->crw_region);
56446ea3841SFarhan Ali     g_free(vcdev->schib_region);
5652a3b9cbaSEric Farman     g_free(vcdev->async_cmd_region);
5662a3b9cbaSEric Farman     g_free(vcdev->io_region);
5672a3b9cbaSEric Farman     g_free(info);
5682a3b9cbaSEric Farman     return;
569c14e706cSDong Jia Shi }
570c14e706cSDong Jia Shi 
571c14e706cSDong Jia Shi static void vfio_ccw_put_region(VFIOCCWDevice *vcdev)
572c14e706cSDong Jia Shi {
573f030532fSFarhan Ali     g_free(vcdev->crw_region);
57446ea3841SFarhan Ali     g_free(vcdev->schib_region);
5758fadea24SCornelia Huck     g_free(vcdev->async_cmd_region);
576c14e706cSDong Jia Shi     g_free(vcdev->io_region);
577c14e706cSDong Jia Shi }
578c14e706cSDong Jia Shi 
579c96f2c2aSGreg Kurz static void vfio_ccw_put_device(VFIOCCWDevice *vcdev)
5801dcac3e1SXiao Feng Ren {
5811dcac3e1SXiao Feng Ren     g_free(vcdev->vdev.name);
5821dcac3e1SXiao Feng Ren     vfio_put_base_device(&vcdev->vdev);
5831dcac3e1SXiao Feng Ren }
5841dcac3e1SXiao Feng Ren 
585c96f2c2aSGreg Kurz static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
586c96f2c2aSGreg Kurz                                 Error **errp)
587c96f2c2aSGreg Kurz {
588c96f2c2aSGreg Kurz     char *name = g_strdup_printf("%x.%x.%04x", vcdev->cdev.hostid.cssid,
589c96f2c2aSGreg Kurz                                  vcdev->cdev.hostid.ssid,
590c96f2c2aSGreg Kurz                                  vcdev->cdev.hostid.devid);
591c96f2c2aSGreg Kurz     VFIODevice *vbasedev;
592c96f2c2aSGreg Kurz 
593c96f2c2aSGreg Kurz     QLIST_FOREACH(vbasedev, &group->device_list, next) {
594c96f2c2aSGreg Kurz         if (strcmp(vbasedev->name, name) == 0) {
595c96f2c2aSGreg Kurz             error_setg(errp, "vfio: subchannel %s has already been attached",
596c96f2c2aSGreg Kurz                        name);
597c96f2c2aSGreg Kurz             goto out_err;
598c96f2c2aSGreg Kurz         }
599c96f2c2aSGreg Kurz     }
600c96f2c2aSGreg Kurz 
601238e9172SAlex Williamson     /*
602238e9172SAlex Williamson      * All vfio-ccw devices are believed to operate in a way compatible with
603aff92b82SDavid Hildenbrand      * discarding of memory in RAM blocks, ie. pages pinned in the host are
604aff92b82SDavid Hildenbrand      * in the current working set of the guest driver and therefore never
605aff92b82SDavid Hildenbrand      * overlap e.g., with pages available to the guest balloon driver.  This
606aff92b82SDavid Hildenbrand      * needs to be set before vfio_get_device() for vfio common to handle
607aff92b82SDavid Hildenbrand      * ram_block_discard_disable().
608238e9172SAlex Williamson      */
609aff92b82SDavid Hildenbrand     vcdev->vdev.ram_block_discard_allowed = true;
610238e9172SAlex Williamson 
611c96f2c2aSGreg Kurz     if (vfio_get_device(group, vcdev->cdev.mdevid, &vcdev->vdev, errp)) {
612c96f2c2aSGreg Kurz         goto out_err;
613c96f2c2aSGreg Kurz     }
614c96f2c2aSGreg Kurz 
615c96f2c2aSGreg Kurz     vcdev->vdev.ops = &vfio_ccw_ops;
616c96f2c2aSGreg Kurz     vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW;
617c96f2c2aSGreg Kurz     vcdev->vdev.name = name;
618c96f2c2aSGreg Kurz     vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj;
619c96f2c2aSGreg Kurz 
620c96f2c2aSGreg Kurz     return;
621c96f2c2aSGreg Kurz 
622c96f2c2aSGreg Kurz out_err:
623c96f2c2aSGreg Kurz     g_free(name);
624c96f2c2aSGreg Kurz }
625c96f2c2aSGreg Kurz 
6261dcac3e1SXiao Feng Ren static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp)
6271dcac3e1SXiao Feng Ren {
6281dcac3e1SXiao Feng Ren     char *tmp, group_path[PATH_MAX];
6291dcac3e1SXiao Feng Ren     ssize_t len;
6301dcac3e1SXiao Feng Ren     int groupid;
6311dcac3e1SXiao Feng Ren 
6321dcac3e1SXiao Feng Ren     tmp = g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group",
6331dcac3e1SXiao Feng Ren                           cdev->hostid.cssid, cdev->hostid.ssid,
6341dcac3e1SXiao Feng Ren                           cdev->hostid.devid, cdev->mdevid);
6351dcac3e1SXiao Feng Ren     len = readlink(tmp, group_path, sizeof(group_path));
6361dcac3e1SXiao Feng Ren     g_free(tmp);
6371dcac3e1SXiao Feng Ren 
6381dcac3e1SXiao Feng Ren     if (len <= 0 || len >= sizeof(group_path)) {
6391dcac3e1SXiao Feng Ren         error_setg(errp, "vfio: no iommu_group found");
6401dcac3e1SXiao Feng Ren         return NULL;
6411dcac3e1SXiao Feng Ren     }
6421dcac3e1SXiao Feng Ren 
6431dcac3e1SXiao Feng Ren     group_path[len] = 0;
6441dcac3e1SXiao Feng Ren 
6451dcac3e1SXiao Feng Ren     if (sscanf(basename(group_path), "%d", &groupid) != 1) {
6461dcac3e1SXiao Feng Ren         error_setg(errp, "vfio: failed to read %s", group_path);
6471dcac3e1SXiao Feng Ren         return NULL;
6481dcac3e1SXiao Feng Ren     }
6491dcac3e1SXiao Feng Ren 
6501dcac3e1SXiao Feng Ren     return vfio_get_group(groupid, &address_space_memory, errp);
6511dcac3e1SXiao Feng Ren }
6521dcac3e1SXiao Feng Ren 
6531dcac3e1SXiao Feng Ren static void vfio_ccw_realize(DeviceState *dev, Error **errp)
6541dcac3e1SXiao Feng Ren {
6551dcac3e1SXiao Feng Ren     VFIOGroup *group;
6561dcac3e1SXiao Feng Ren     CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
6571dcac3e1SXiao Feng Ren     S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
6581dcac3e1SXiao Feng Ren     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
6591dcac3e1SXiao Feng Ren     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
6601dcac3e1SXiao Feng Ren     Error *err = NULL;
6611dcac3e1SXiao Feng Ren 
6621dcac3e1SXiao Feng Ren     /* Call the class init function for subchannel. */
6631dcac3e1SXiao Feng Ren     if (cdc->realize) {
6641dcac3e1SXiao Feng Ren         cdc->realize(cdev, vcdev->vdev.sysfsdev, &err);
6651dcac3e1SXiao Feng Ren         if (err) {
6661dcac3e1SXiao Feng Ren             goto out_err_propagate;
6671dcac3e1SXiao Feng Ren         }
6681dcac3e1SXiao Feng Ren     }
6691dcac3e1SXiao Feng Ren 
6701dcac3e1SXiao Feng Ren     group = vfio_ccw_get_group(cdev, &err);
6711dcac3e1SXiao Feng Ren     if (!group) {
6721dcac3e1SXiao Feng Ren         goto out_group_err;
6731dcac3e1SXiao Feng Ren     }
6741dcac3e1SXiao Feng Ren 
675c96f2c2aSGreg Kurz     vfio_ccw_get_device(group, vcdev, &err);
676c96f2c2aSGreg Kurz     if (err) {
6771dcac3e1SXiao Feng Ren         goto out_device_err;
6781dcac3e1SXiao Feng Ren     }
6791dcac3e1SXiao Feng Ren 
680c14e706cSDong Jia Shi     vfio_ccw_get_region(vcdev, &err);
681c14e706cSDong Jia Shi     if (err) {
682c14e706cSDong Jia Shi         goto out_region_err;
683c14e706cSDong Jia Shi     }
684c14e706cSDong Jia Shi 
685690e29b9SEric Farman     vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX, &err);
6864886b3e9SDong Jia Shi     if (err) {
687b2f96f9eSEric Farman         goto out_io_notifier_err;
6884886b3e9SDong Jia Shi     }
6894886b3e9SDong Jia Shi 
690f030532fSFarhan Ali     if (vcdev->crw_region) {
691f030532fSFarhan Ali         vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX, &err);
692f030532fSFarhan Ali         if (err) {
693b2f96f9eSEric Farman             goto out_crw_notifier_err;
694f030532fSFarhan Ali         }
695f030532fSFarhan Ali     }
696f030532fSFarhan Ali 
697b2f96f9eSEric Farman     vfio_ccw_register_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX, &err);
698b2f96f9eSEric Farman     if (err) {
699b2f96f9eSEric Farman         goto out_req_notifier_err;
700b2f96f9eSEric Farman     }
701b2f96f9eSEric Farman 
7021dcac3e1SXiao Feng Ren     return;
7031dcac3e1SXiao Feng Ren 
704b2f96f9eSEric Farman out_req_notifier_err:
705b2f96f9eSEric Farman     vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX);
706b2f96f9eSEric Farman out_crw_notifier_err:
707b2f96f9eSEric Farman     vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX);
708b2f96f9eSEric Farman out_io_notifier_err:
7094886b3e9SDong Jia Shi     vfio_ccw_put_region(vcdev);
710c14e706cSDong Jia Shi out_region_err:
711c96f2c2aSGreg Kurz     vfio_ccw_put_device(vcdev);
7121dcac3e1SXiao Feng Ren out_device_err:
7131dcac3e1SXiao Feng Ren     vfio_put_group(group);
7141dcac3e1SXiao Feng Ren out_group_err:
7151dcac3e1SXiao Feng Ren     if (cdc->unrealize) {
716b69c3c21SMarkus Armbruster         cdc->unrealize(cdev);
7171dcac3e1SXiao Feng Ren     }
7181dcac3e1SXiao Feng Ren out_err_propagate:
7191dcac3e1SXiao Feng Ren     error_propagate(errp, err);
7201dcac3e1SXiao Feng Ren }
7211dcac3e1SXiao Feng Ren 
722b69c3c21SMarkus Armbruster static void vfio_ccw_unrealize(DeviceState *dev)
7231dcac3e1SXiao Feng Ren {
7241dcac3e1SXiao Feng Ren     CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
7251dcac3e1SXiao Feng Ren     S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
7261dcac3e1SXiao Feng Ren     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
7271dcac3e1SXiao Feng Ren     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
7281dcac3e1SXiao Feng Ren     VFIOGroup *group = vcdev->vdev.group;
7291dcac3e1SXiao Feng Ren 
730b2f96f9eSEric Farman     vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_REQ_IRQ_INDEX);
731f030532fSFarhan Ali     vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_CRW_IRQ_INDEX);
732690e29b9SEric Farman     vfio_ccw_unregister_irq_notifier(vcdev, VFIO_CCW_IO_IRQ_INDEX);
733c14e706cSDong Jia Shi     vfio_ccw_put_region(vcdev);
734c96f2c2aSGreg Kurz     vfio_ccw_put_device(vcdev);
7351dcac3e1SXiao Feng Ren     vfio_put_group(group);
7361dcac3e1SXiao Feng Ren 
7371dcac3e1SXiao Feng Ren     if (cdc->unrealize) {
738b69c3c21SMarkus Armbruster         cdc->unrealize(cdev);
7391dcac3e1SXiao Feng Ren     }
7401dcac3e1SXiao Feng Ren }
7411dcac3e1SXiao Feng Ren 
7421dcac3e1SXiao Feng Ren static Property vfio_ccw_properties[] = {
7431dcac3e1SXiao Feng Ren     DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev),
7449a51c9eeSHalil Pasic     DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice, force_orb_pfch, false),
7451dcac3e1SXiao Feng Ren     DEFINE_PROP_END_OF_LIST(),
7461dcac3e1SXiao Feng Ren };
7471dcac3e1SXiao Feng Ren 
7481dcac3e1SXiao Feng Ren static const VMStateDescription vfio_ccw_vmstate = {
749da56e330SLi Qiang     .name = "vfio-ccw",
7501dcac3e1SXiao Feng Ren     .unmigratable = 1,
7511dcac3e1SXiao Feng Ren };
7521dcac3e1SXiao Feng Ren 
7531dcac3e1SXiao Feng Ren static void vfio_ccw_class_init(ObjectClass *klass, void *data)
7541dcac3e1SXiao Feng Ren {
7551dcac3e1SXiao Feng Ren     DeviceClass *dc = DEVICE_CLASS(klass);
7568ca2b376SXiao Feng Ren     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
7571dcac3e1SXiao Feng Ren 
7584f67d30bSMarc-André Lureau     device_class_set_props(dc, vfio_ccw_properties);
7591dcac3e1SXiao Feng Ren     dc->vmsd = &vfio_ccw_vmstate;
7601dcac3e1SXiao Feng Ren     dc->desc = "VFIO-based subchannel assignment";
761bd2aef10SCornelia Huck     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
7621dcac3e1SXiao Feng Ren     dc->realize = vfio_ccw_realize;
7631dcac3e1SXiao Feng Ren     dc->unrealize = vfio_ccw_unrealize;
7641dcac3e1SXiao Feng Ren     dc->reset = vfio_ccw_reset;
7658ca2b376SXiao Feng Ren 
7668ca2b376SXiao Feng Ren     cdc->handle_request = vfio_ccw_handle_request;
7678fadea24SCornelia Huck     cdc->handle_halt = vfio_ccw_handle_halt;
7688fadea24SCornelia Huck     cdc->handle_clear = vfio_ccw_handle_clear;
76946ea3841SFarhan Ali     cdc->handle_store = vfio_ccw_handle_store;
7701dcac3e1SXiao Feng Ren }
7711dcac3e1SXiao Feng Ren 
7721dcac3e1SXiao Feng Ren static const TypeInfo vfio_ccw_info = {
7731dcac3e1SXiao Feng Ren     .name = TYPE_VFIO_CCW,
7741dcac3e1SXiao Feng Ren     .parent = TYPE_S390_CCW,
7751dcac3e1SXiao Feng Ren     .instance_size = sizeof(VFIOCCWDevice),
7761dcac3e1SXiao Feng Ren     .class_init = vfio_ccw_class_init,
7771dcac3e1SXiao Feng Ren };
7781dcac3e1SXiao Feng Ren 
7791dcac3e1SXiao Feng Ren static void register_vfio_ccw_type(void)
7801dcac3e1SXiao Feng Ren {
7811dcac3e1SXiao Feng Ren     type_register_static(&vfio_ccw_info);
7821dcac3e1SXiao Feng Ren }
7831dcac3e1SXiao Feng Ren 
7841dcac3e1SXiao Feng Ren type_init(register_vfio_ccw_type)
785