2 * vfio based subchannel assignment support
4 * Copyright 2017 IBM Corp.
5 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
6 * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
7 * Pierre Morel <pmorel@linux.vnet.ibm.com>
9 * This work is licensed under the terms of the GNU GPL, version 2 or (at
10 * your option) any later version. See the COPYING file in the top-level
14 #include "qemu/osdep.h"
15 #include <linux/vfio.h>
16 #include <linux/vfio_ccw.h>
17 #include <sys/ioctl.h>
19 #include "qapi/error.h"
20 #include "hw/sysbus.h"
21 #include "hw/vfio/vfio.h"
22 #include "hw/vfio/vfio-common.h"
23 #include "hw/s390x/s390-ccw.h"
24 #include "hw/s390x/vfio-ccw.h"
25 #include "hw/s390x/ccw-device.h"
26 #include "exec/address-spaces.h"
27 #include "qemu/error-report.h"
28 #include "qemu/module.h"
30 struct VFIOCCWDevice
{
33 uint64_t io_region_size
;
34 uint64_t io_region_offset
;
35 struct ccw_io_region
*io_region
;
36 EventNotifier io_notifier
;
41 static inline void warn_once_pfch(VFIOCCWDevice
*vcdev
, SubchDev
*sch
,
44 warn_report_once_cond(&vcdev
->warned_orb_pfch
,
45 "vfio-ccw (devno %x.%x.%04x): %s",
46 sch
->cssid
, sch
->ssid
, sch
->devno
, msg
);
49 static void vfio_ccw_compute_needs_reset(VFIODevice
*vdev
)
51 vdev
->needs_reset
= false;
55 * We don't need vfio_hot_reset_multi and vfio_eoi operations for
56 * vfio_ccw device now.
58 struct VFIODeviceOps vfio_ccw_ops
= {
59 .vfio_compute_needs_reset
= vfio_ccw_compute_needs_reset
,
62 static IOInstEnding
vfio_ccw_handle_request(SubchDev
*sch
)
64 S390CCWDevice
*cdev
= sch
->driver_data
;
65 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
66 struct ccw_io_region
*region
= vcdev
->io_region
;
69 if (!(sch
->orb
.ctrl0
& ORB_CTRL0_MASK_PFCH
)) {
70 if (!(vcdev
->force_orb_pfch
)) {
71 warn_once_pfch(vcdev
, sch
, "requires PFCH flag set");
72 sch_gen_unit_exception(sch
);
73 css_inject_io_interrupt(sch
);
74 return IOINST_CC_EXPECTED
;
76 sch
->orb
.ctrl0
|= ORB_CTRL0_MASK_PFCH
;
77 warn_once_pfch(vcdev
, sch
, "PFCH flag forced");
81 QEMU_BUILD_BUG_ON(sizeof(region
->orb_area
) != sizeof(ORB
));
82 QEMU_BUILD_BUG_ON(sizeof(region
->scsw_area
) != sizeof(SCSW
));
83 QEMU_BUILD_BUG_ON(sizeof(region
->irb_area
) != sizeof(IRB
));
85 memset(region
, 0, sizeof(*region
));
87 memcpy(region
->orb_area
, &sch
->orb
, sizeof(ORB
));
88 memcpy(region
->scsw_area
, &sch
->curr_status
.scsw
, sizeof(SCSW
));
91 ret
= pwrite(vcdev
->vdev
.fd
, region
,
92 vcdev
->io_region_size
, vcdev
->io_region_offset
);
93 if (ret
!= vcdev
->io_region_size
) {
94 if (errno
== EAGAIN
) {
97 error_report("vfio-ccw: wirte I/O region failed with errno=%d", errno
);
100 ret
= region
->ret_code
;
104 return IOINST_CC_EXPECTED
;
106 return IOINST_CC_BUSY
;
109 return IOINST_CC_NOT_OPERATIONAL
;
112 sch_gen_unit_exception(sch
);
113 css_inject_io_interrupt(sch
);
114 return IOINST_CC_EXPECTED
;
118 static void vfio_ccw_reset(DeviceState
*dev
)
120 CcwDevice
*ccw_dev
= DO_UPCAST(CcwDevice
, parent_obj
, dev
);
121 S390CCWDevice
*cdev
= DO_UPCAST(S390CCWDevice
, parent_obj
, ccw_dev
);
122 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
124 ioctl(vcdev
->vdev
.fd
, VFIO_DEVICE_RESET
);
127 static void vfio_ccw_io_notifier_handler(void *opaque
)
129 VFIOCCWDevice
*vcdev
= opaque
;
130 struct ccw_io_region
*region
= vcdev
->io_region
;
131 S390CCWDevice
*cdev
= S390_CCW_DEVICE(vcdev
);
132 CcwDevice
*ccw_dev
= CCW_DEVICE(cdev
);
133 SubchDev
*sch
= ccw_dev
->sch
;
134 SCHIB
*schib
= &sch
->curr_status
;
139 if (!event_notifier_test_and_clear(&vcdev
->io_notifier
)) {
143 size
= pread(vcdev
->vdev
.fd
, region
, vcdev
->io_region_size
,
144 vcdev
->io_region_offset
);
148 /* Generate a deferred cc 3 condition. */
149 schib
->scsw
.flags
|= SCSW_FLAGS_MASK_CC
;
150 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
151 schib
->scsw
.ctrl
|= (SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
);
154 /* Memory problem, generate channel data check. */
155 schib
->scsw
.ctrl
&= ~SCSW_ACTL_START_PEND
;
156 schib
->scsw
.cstat
= SCSW_CSTAT_DATA_CHECK
;
157 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
158 schib
->scsw
.ctrl
|= SCSW_STCTL_PRIMARY
| SCSW_STCTL_SECONDARY
|
159 SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
;
162 /* Error, generate channel program check. */
163 schib
->scsw
.ctrl
&= ~SCSW_ACTL_START_PEND
;
164 schib
->scsw
.cstat
= SCSW_CSTAT_PROG_CHECK
;
165 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
166 schib
->scsw
.ctrl
|= SCSW_STCTL_PRIMARY
| SCSW_STCTL_SECONDARY
|
167 SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
;
170 } else if (size
!= vcdev
->io_region_size
) {
171 /* Information transfer error, generate channel-control check. */
172 schib
->scsw
.ctrl
&= ~SCSW_ACTL_START_PEND
;
173 schib
->scsw
.cstat
= SCSW_CSTAT_CHN_CTRL_CHK
;
174 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
175 schib
->scsw
.ctrl
|= SCSW_STCTL_PRIMARY
| SCSW_STCTL_SECONDARY
|
176 SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
;
180 memcpy(&irb
, region
->irb_area
, sizeof(IRB
));
182 /* Update control block via irb. */
184 copy_scsw_to_guest(&s
, &irb
.scsw
);
187 /* If a uint check is pending, copy sense data. */
188 if ((schib
->scsw
.dstat
& SCSW_DSTAT_UNIT_CHECK
) &&
189 (schib
->pmcw
.chars
& PMCW_CHARS_MASK_CSENSE
)) {
190 memcpy(sch
->sense_data
, irb
.ecw
, sizeof(irb
.ecw
));
194 css_inject_io_interrupt(sch
);
197 static void vfio_ccw_register_io_notifier(VFIOCCWDevice
*vcdev
, Error
**errp
)
199 VFIODevice
*vdev
= &vcdev
->vdev
;
200 struct vfio_irq_info
*irq_info
;
204 if (vdev
->num_irqs
< VFIO_CCW_IO_IRQ_INDEX
+ 1) {
205 error_setg(errp
, "vfio: unexpected number of io irqs %u",
210 argsz
= sizeof(*irq_info
);
211 irq_info
= g_malloc0(argsz
);
212 irq_info
->index
= VFIO_CCW_IO_IRQ_INDEX
;
213 irq_info
->argsz
= argsz
;
214 if (ioctl(vdev
->fd
, VFIO_DEVICE_GET_IRQ_INFO
,
215 irq_info
) < 0 || irq_info
->count
< 1) {
216 error_setg_errno(errp
, errno
, "vfio: Error getting irq info");
220 if (event_notifier_init(&vcdev
->io_notifier
, 0)) {
221 error_setg_errno(errp
, errno
,
222 "vfio: Unable to init event notifier for IO");
226 fd
= event_notifier_get_fd(&vcdev
->io_notifier
);
227 qemu_set_fd_handler(fd
, vfio_ccw_io_notifier_handler
, NULL
, vcdev
);
229 if (vfio_set_irq_signaling(vdev
, VFIO_CCW_IO_IRQ_INDEX
, 0,
230 VFIO_IRQ_SET_ACTION_TRIGGER
, fd
, errp
)) {
231 qemu_set_fd_handler(fd
, NULL
, NULL
, vcdev
);
232 event_notifier_cleanup(&vcdev
->io_notifier
);
239 static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice
*vcdev
)
243 vfio_set_irq_signaling(&vcdev
->vdev
, VFIO_CCW_IO_IRQ_INDEX
, 0,
244 VFIO_IRQ_SET_ACTION_TRIGGER
, -1, &err
);
246 error_reportf_err(err
, VFIO_MSG_PREFIX
, vcdev
->vdev
.name
);
249 qemu_set_fd_handler(event_notifier_get_fd(&vcdev
->io_notifier
),
251 event_notifier_cleanup(&vcdev
->io_notifier
);
254 static void vfio_ccw_get_region(VFIOCCWDevice
*vcdev
, Error
**errp
)
256 VFIODevice
*vdev
= &vcdev
->vdev
;
257 struct vfio_region_info
*info
;
260 /* Sanity check device */
261 if (!(vdev
->flags
& VFIO_DEVICE_FLAGS_CCW
)) {
262 error_setg(errp
, "vfio: Um, this isn't a vfio-ccw device");
266 if (vdev
->num_regions
< VFIO_CCW_CONFIG_REGION_INDEX
+ 1) {
267 error_setg(errp
, "vfio: Unexpected number of the I/O region %u",
272 ret
= vfio_get_region_info(vdev
, VFIO_CCW_CONFIG_REGION_INDEX
, &info
);
274 error_setg_errno(errp
, -ret
, "vfio: Error getting config info");
278 vcdev
->io_region_size
= info
->size
;
279 if (sizeof(*vcdev
->io_region
) != vcdev
->io_region_size
) {
280 error_setg(errp
, "vfio: Unexpected size of the I/O region");
285 vcdev
->io_region_offset
= info
->offset
;
286 vcdev
->io_region
= g_malloc0(info
->size
);
291 static void vfio_ccw_put_region(VFIOCCWDevice
*vcdev
)
293 g_free(vcdev
->io_region
);
296 static void vfio_ccw_put_device(VFIOCCWDevice
*vcdev
)
298 g_free(vcdev
->vdev
.name
);
299 vfio_put_base_device(&vcdev
->vdev
);
302 static void vfio_ccw_get_device(VFIOGroup
*group
, VFIOCCWDevice
*vcdev
,
305 char *name
= g_strdup_printf("%x.%x.%04x", vcdev
->cdev
.hostid
.cssid
,
306 vcdev
->cdev
.hostid
.ssid
,
307 vcdev
->cdev
.hostid
.devid
);
308 VFIODevice
*vbasedev
;
310 QLIST_FOREACH(vbasedev
, &group
->device_list
, next
) {
311 if (strcmp(vbasedev
->name
, name
) == 0) {
312 error_setg(errp
, "vfio: subchannel %s has already been attached",
319 * All vfio-ccw devices are believed to operate in a way compatible with
320 * memory ballooning, ie. pages pinned in the host are in the current
321 * working set of the guest driver and therefore never overlap with pages
322 * available to the guest balloon driver. This needs to be set before
323 * vfio_get_device() for vfio common to handle the balloon inhibitor.
325 vcdev
->vdev
.balloon_allowed
= true;
327 if (vfio_get_device(group
, vcdev
->cdev
.mdevid
, &vcdev
->vdev
, errp
)) {
331 vcdev
->vdev
.ops
= &vfio_ccw_ops
;
332 vcdev
->vdev
.type
= VFIO_DEVICE_TYPE_CCW
;
333 vcdev
->vdev
.name
= name
;
334 vcdev
->vdev
.dev
= &vcdev
->cdev
.parent_obj
.parent_obj
;
342 static VFIOGroup
*vfio_ccw_get_group(S390CCWDevice
*cdev
, Error
**errp
)
344 char *tmp
, group_path
[PATH_MAX
];
348 tmp
= g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group",
349 cdev
->hostid
.cssid
, cdev
->hostid
.ssid
,
350 cdev
->hostid
.devid
, cdev
->mdevid
);
351 len
= readlink(tmp
, group_path
, sizeof(group_path
));
354 if (len
<= 0 || len
>= sizeof(group_path
)) {
355 error_setg(errp
, "vfio: no iommu_group found");
361 if (sscanf(basename(group_path
), "%d", &groupid
) != 1) {
362 error_setg(errp
, "vfio: failed to read %s", group_path
);
366 return vfio_get_group(groupid
, &address_space_memory
, errp
);
369 static void vfio_ccw_realize(DeviceState
*dev
, Error
**errp
)
372 CcwDevice
*ccw_dev
= DO_UPCAST(CcwDevice
, parent_obj
, dev
);
373 S390CCWDevice
*cdev
= DO_UPCAST(S390CCWDevice
, parent_obj
, ccw_dev
);
374 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
375 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_GET_CLASS(cdev
);
378 /* Call the class init function for subchannel. */
380 cdc
->realize(cdev
, vcdev
->vdev
.sysfsdev
, &err
);
382 goto out_err_propagate
;
386 group
= vfio_ccw_get_group(cdev
, &err
);
391 vfio_ccw_get_device(group
, vcdev
, &err
);
396 vfio_ccw_get_region(vcdev
, &err
);
401 vfio_ccw_register_io_notifier(vcdev
, &err
);
403 goto out_notifier_err
;
409 vfio_ccw_put_region(vcdev
);
411 vfio_ccw_put_device(vcdev
);
413 vfio_put_group(group
);
415 if (cdc
->unrealize
) {
416 cdc
->unrealize(cdev
, NULL
);
419 error_propagate(errp
, err
);
422 static void vfio_ccw_unrealize(DeviceState
*dev
, Error
**errp
)
424 CcwDevice
*ccw_dev
= DO_UPCAST(CcwDevice
, parent_obj
, dev
);
425 S390CCWDevice
*cdev
= DO_UPCAST(S390CCWDevice
, parent_obj
, ccw_dev
);
426 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
427 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_GET_CLASS(cdev
);
428 VFIOGroup
*group
= vcdev
->vdev
.group
;
430 vfio_ccw_unregister_io_notifier(vcdev
);
431 vfio_ccw_put_region(vcdev
);
432 vfio_ccw_put_device(vcdev
);
433 vfio_put_group(group
);
435 if (cdc
->unrealize
) {
436 cdc
->unrealize(cdev
, errp
);
440 static Property vfio_ccw_properties
[] = {
441 DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice
, vdev
.sysfsdev
),
442 DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice
, force_orb_pfch
, false),
443 DEFINE_PROP_END_OF_LIST(),
446 static const VMStateDescription vfio_ccw_vmstate
= {
451 static void vfio_ccw_class_init(ObjectClass
*klass
, void *data
)
453 DeviceClass
*dc
= DEVICE_CLASS(klass
);
454 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_CLASS(klass
);
456 dc
->props
= vfio_ccw_properties
;
457 dc
->vmsd
= &vfio_ccw_vmstate
;
458 dc
->desc
= "VFIO-based subchannel assignment";
459 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
460 dc
->realize
= vfio_ccw_realize
;
461 dc
->unrealize
= vfio_ccw_unrealize
;
462 dc
->reset
= vfio_ccw_reset
;
464 cdc
->handle_request
= vfio_ccw_handle_request
;
467 static const TypeInfo vfio_ccw_info
= {
468 .name
= TYPE_VFIO_CCW
,
469 .parent
= TYPE_S390_CCW
,
470 .instance_size
= sizeof(VFIOCCWDevice
),
471 .class_init
= vfio_ccw_class_init
,
474 static void register_vfio_ccw_type(void)
476 type_register_static(&vfio_ccw_info
);
479 type_init(register_vfio_ccw_type
)