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/ccw-device.h"
25 #include "exec/address-spaces.h"
26 #include "qemu/error-report.h"
28 #define TYPE_VFIO_CCW "vfio-ccw"
29 typedef struct VFIOCCWDevice
{
32 uint64_t io_region_size
;
33 uint64_t io_region_offset
;
34 struct ccw_io_region
*io_region
;
35 EventNotifier io_notifier
;
40 static inline void warn_once_pfch(VFIOCCWDevice
*vcdev
, SubchDev
*sch
,
43 warn_report_once_cond(&vcdev
->warned_orb_pfch
,
44 "vfio-ccw (devno %x.%x.%04x): %s",
45 sch
->cssid
, sch
->ssid
, sch
->devno
, msg
);
48 static void vfio_ccw_compute_needs_reset(VFIODevice
*vdev
)
50 vdev
->needs_reset
= false;
54 * We don't need vfio_hot_reset_multi and vfio_eoi operations for
55 * vfio_ccw device now.
57 struct VFIODeviceOps vfio_ccw_ops
= {
58 .vfio_compute_needs_reset
= vfio_ccw_compute_needs_reset
,
61 static IOInstEnding
vfio_ccw_handle_request(SubchDev
*sch
)
63 S390CCWDevice
*cdev
= sch
->driver_data
;
64 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
65 struct ccw_io_region
*region
= vcdev
->io_region
;
68 if (!(sch
->orb
.ctrl0
& ORB_CTRL0_MASK_PFCH
)) {
69 if (!(vcdev
->force_orb_pfch
)) {
70 warn_once_pfch(vcdev
, sch
, "requires PFCH flag set");
71 sch_gen_unit_exception(sch
);
72 css_inject_io_interrupt(sch
);
73 return IOINST_CC_EXPECTED
;
75 sch
->orb
.ctrl0
|= ORB_CTRL0_MASK_PFCH
;
76 warn_once_pfch(vcdev
, sch
, "PFCH flag forced");
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
));
84 memset(region
, 0, sizeof(*region
));
86 memcpy(region
->orb_area
, &sch
->orb
, sizeof(ORB
));
87 memcpy(region
->scsw_area
, &sch
->curr_status
.scsw
, sizeof(SCSW
));
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
) {
96 error_report("vfio-ccw: wirte I/O region failed with errno=%d", errno
);
99 ret
= region
->ret_code
;
103 return IOINST_CC_EXPECTED
;
105 return IOINST_CC_BUSY
;
108 return IOINST_CC_NOT_OPERATIONAL
;
111 sch_gen_unit_exception(sch
);
112 css_inject_io_interrupt(sch
);
113 return IOINST_CC_EXPECTED
;
117 static void vfio_ccw_reset(DeviceState
*dev
)
119 CcwDevice
*ccw_dev
= DO_UPCAST(CcwDevice
, parent_obj
, dev
);
120 S390CCWDevice
*cdev
= DO_UPCAST(S390CCWDevice
, parent_obj
, ccw_dev
);
121 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
123 ioctl(vcdev
->vdev
.fd
, VFIO_DEVICE_RESET
);
126 static void vfio_ccw_io_notifier_handler(void *opaque
)
128 VFIOCCWDevice
*vcdev
= opaque
;
129 struct ccw_io_region
*region
= vcdev
->io_region
;
130 S390CCWDevice
*cdev
= S390_CCW_DEVICE(vcdev
);
131 CcwDevice
*ccw_dev
= CCW_DEVICE(cdev
);
132 SubchDev
*sch
= ccw_dev
->sch
;
133 SCSW
*s
= &sch
->curr_status
.scsw
;
134 PMCW
*p
= &sch
->curr_status
.pmcw
;
138 if (!event_notifier_test_and_clear(&vcdev
->io_notifier
)) {
142 size
= pread(vcdev
->vdev
.fd
, region
, vcdev
->io_region_size
,
143 vcdev
->io_region_offset
);
147 /* Generate a deferred cc 3 condition. */
148 s
->flags
|= SCSW_FLAGS_MASK_CC
;
149 s
->ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
150 s
->ctrl
|= (SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
);
153 /* Memory problem, generate channel data check. */
154 s
->ctrl
&= ~SCSW_ACTL_START_PEND
;
155 s
->cstat
= SCSW_CSTAT_DATA_CHECK
;
156 s
->ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
157 s
->ctrl
|= SCSW_STCTL_PRIMARY
| SCSW_STCTL_SECONDARY
|
158 SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
;
161 /* Error, generate channel program check. */
162 s
->ctrl
&= ~SCSW_ACTL_START_PEND
;
163 s
->cstat
= SCSW_CSTAT_PROG_CHECK
;
164 s
->ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
165 s
->ctrl
|= SCSW_STCTL_PRIMARY
| SCSW_STCTL_SECONDARY
|
166 SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
;
169 } else if (size
!= vcdev
->io_region_size
) {
170 /* Information transfer error, generate channel-control check. */
171 s
->ctrl
&= ~SCSW_ACTL_START_PEND
;
172 s
->cstat
= SCSW_CSTAT_CHN_CTRL_CHK
;
173 s
->ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
174 s
->ctrl
|= SCSW_STCTL_PRIMARY
| SCSW_STCTL_SECONDARY
|
175 SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
;
179 memcpy(&irb
, region
->irb_area
, sizeof(IRB
));
181 /* Update control block via irb. */
182 copy_scsw_to_guest(s
, &irb
.scsw
);
184 /* If a uint check is pending, copy sense data. */
185 if ((s
->dstat
& SCSW_DSTAT_UNIT_CHECK
) &&
186 (p
->chars
& PMCW_CHARS_MASK_CSENSE
)) {
187 memcpy(sch
->sense_data
, irb
.ecw
, sizeof(irb
.ecw
));
191 css_inject_io_interrupt(sch
);
194 static void vfio_ccw_register_io_notifier(VFIOCCWDevice
*vcdev
, Error
**errp
)
196 VFIODevice
*vdev
= &vcdev
->vdev
;
197 struct vfio_irq_info
*irq_info
;
198 struct vfio_irq_set
*irq_set
;
202 if (vdev
->num_irqs
< VFIO_CCW_IO_IRQ_INDEX
+ 1) {
203 error_setg(errp
, "vfio: unexpected number of io irqs %u",
208 argsz
= sizeof(*irq_info
);
209 irq_info
= g_malloc0(argsz
);
210 irq_info
->index
= VFIO_CCW_IO_IRQ_INDEX
;
211 irq_info
->argsz
= argsz
;
212 if (ioctl(vdev
->fd
, VFIO_DEVICE_GET_IRQ_INFO
,
213 irq_info
) < 0 || irq_info
->count
< 1) {
214 error_setg_errno(errp
, errno
, "vfio: Error getting irq info");
218 if (event_notifier_init(&vcdev
->io_notifier
, 0)) {
219 error_setg_errno(errp
, errno
,
220 "vfio: Unable to init event notifier for IO");
224 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
225 irq_set
= g_malloc0(argsz
);
226 irq_set
->argsz
= argsz
;
227 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
228 VFIO_IRQ_SET_ACTION_TRIGGER
;
229 irq_set
->index
= VFIO_CCW_IO_IRQ_INDEX
;
232 pfd
= (int32_t *) &irq_set
->data
;
234 *pfd
= event_notifier_get_fd(&vcdev
->io_notifier
);
235 qemu_set_fd_handler(*pfd
, vfio_ccw_io_notifier_handler
, NULL
, vcdev
);
236 if (ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
)) {
237 error_setg(errp
, "vfio: Failed to set up io notification");
238 qemu_set_fd_handler(*pfd
, NULL
, NULL
, vcdev
);
239 event_notifier_cleanup(&vcdev
->io_notifier
);
248 static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice
*vcdev
)
250 struct vfio_irq_set
*irq_set
;
254 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
255 irq_set
= g_malloc0(argsz
);
256 irq_set
->argsz
= argsz
;
257 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
258 VFIO_IRQ_SET_ACTION_TRIGGER
;
259 irq_set
->index
= VFIO_CCW_IO_IRQ_INDEX
;
262 pfd
= (int32_t *) &irq_set
->data
;
265 if (ioctl(vcdev
->vdev
.fd
, VFIO_DEVICE_SET_IRQS
, irq_set
)) {
266 error_report("vfio: Failed to de-assign device io fd: %m");
269 qemu_set_fd_handler(event_notifier_get_fd(&vcdev
->io_notifier
),
271 event_notifier_cleanup(&vcdev
->io_notifier
);
276 static void vfio_ccw_get_region(VFIOCCWDevice
*vcdev
, Error
**errp
)
278 VFIODevice
*vdev
= &vcdev
->vdev
;
279 struct vfio_region_info
*info
;
282 /* Sanity check device */
283 if (!(vdev
->flags
& VFIO_DEVICE_FLAGS_CCW
)) {
284 error_setg(errp
, "vfio: Um, this isn't a vfio-ccw device");
288 if (vdev
->num_regions
< VFIO_CCW_CONFIG_REGION_INDEX
+ 1) {
289 error_setg(errp
, "vfio: Unexpected number of the I/O region %u",
294 ret
= vfio_get_region_info(vdev
, VFIO_CCW_CONFIG_REGION_INDEX
, &info
);
296 error_setg_errno(errp
, -ret
, "vfio: Error getting config info");
300 vcdev
->io_region_size
= info
->size
;
301 if (sizeof(*vcdev
->io_region
) != vcdev
->io_region_size
) {
302 error_setg(errp
, "vfio: Unexpected size of the I/O region");
307 vcdev
->io_region_offset
= info
->offset
;
308 vcdev
->io_region
= g_malloc0(info
->size
);
313 static void vfio_ccw_put_region(VFIOCCWDevice
*vcdev
)
315 g_free(vcdev
->io_region
);
318 static void vfio_ccw_put_device(VFIOCCWDevice
*vcdev
)
320 g_free(vcdev
->vdev
.name
);
321 vfio_put_base_device(&vcdev
->vdev
);
324 static void vfio_ccw_get_device(VFIOGroup
*group
, VFIOCCWDevice
*vcdev
,
327 char *name
= g_strdup_printf("%x.%x.%04x", vcdev
->cdev
.hostid
.cssid
,
328 vcdev
->cdev
.hostid
.ssid
,
329 vcdev
->cdev
.hostid
.devid
);
330 VFIODevice
*vbasedev
;
332 QLIST_FOREACH(vbasedev
, &group
->device_list
, next
) {
333 if (strcmp(vbasedev
->name
, name
) == 0) {
334 error_setg(errp
, "vfio: subchannel %s has already been attached",
341 * All vfio-ccw devices are believed to operate in a way compatible with
342 * memory ballooning, ie. pages pinned in the host are in the current
343 * working set of the guest driver and therefore never overlap with pages
344 * available to the guest balloon driver. This needs to be set before
345 * vfio_get_device() for vfio common to handle the balloon inhibitor.
347 vcdev
->vdev
.balloon_allowed
= true;
349 if (vfio_get_device(group
, vcdev
->cdev
.mdevid
, &vcdev
->vdev
, errp
)) {
353 vcdev
->vdev
.ops
= &vfio_ccw_ops
;
354 vcdev
->vdev
.type
= VFIO_DEVICE_TYPE_CCW
;
355 vcdev
->vdev
.name
= name
;
356 vcdev
->vdev
.dev
= &vcdev
->cdev
.parent_obj
.parent_obj
;
364 static VFIOGroup
*vfio_ccw_get_group(S390CCWDevice
*cdev
, Error
**errp
)
366 char *tmp
, group_path
[PATH_MAX
];
370 tmp
= g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group",
371 cdev
->hostid
.cssid
, cdev
->hostid
.ssid
,
372 cdev
->hostid
.devid
, cdev
->mdevid
);
373 len
= readlink(tmp
, group_path
, sizeof(group_path
));
376 if (len
<= 0 || len
>= sizeof(group_path
)) {
377 error_setg(errp
, "vfio: no iommu_group found");
383 if (sscanf(basename(group_path
), "%d", &groupid
) != 1) {
384 error_setg(errp
, "vfio: failed to read %s", group_path
);
388 return vfio_get_group(groupid
, &address_space_memory
, errp
);
391 static void vfio_ccw_realize(DeviceState
*dev
, Error
**errp
)
394 CcwDevice
*ccw_dev
= DO_UPCAST(CcwDevice
, parent_obj
, dev
);
395 S390CCWDevice
*cdev
= DO_UPCAST(S390CCWDevice
, parent_obj
, ccw_dev
);
396 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
397 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_GET_CLASS(cdev
);
400 /* Call the class init function for subchannel. */
402 cdc
->realize(cdev
, vcdev
->vdev
.sysfsdev
, &err
);
404 goto out_err_propagate
;
408 group
= vfio_ccw_get_group(cdev
, &err
);
413 vfio_ccw_get_device(group
, vcdev
, &err
);
418 vfio_ccw_get_region(vcdev
, &err
);
423 vfio_ccw_register_io_notifier(vcdev
, &err
);
425 goto out_notifier_err
;
431 vfio_ccw_put_region(vcdev
);
433 vfio_ccw_put_device(vcdev
);
435 vfio_put_group(group
);
437 if (cdc
->unrealize
) {
438 cdc
->unrealize(cdev
, NULL
);
441 error_propagate(errp
, err
);
444 static void vfio_ccw_unrealize(DeviceState
*dev
, Error
**errp
)
446 CcwDevice
*ccw_dev
= DO_UPCAST(CcwDevice
, parent_obj
, dev
);
447 S390CCWDevice
*cdev
= DO_UPCAST(S390CCWDevice
, parent_obj
, ccw_dev
);
448 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
449 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_GET_CLASS(cdev
);
450 VFIOGroup
*group
= vcdev
->vdev
.group
;
452 vfio_ccw_unregister_io_notifier(vcdev
);
453 vfio_ccw_put_region(vcdev
);
454 vfio_ccw_put_device(vcdev
);
455 vfio_put_group(group
);
457 if (cdc
->unrealize
) {
458 cdc
->unrealize(cdev
, errp
);
462 static Property vfio_ccw_properties
[] = {
463 DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice
, vdev
.sysfsdev
),
464 DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice
, force_orb_pfch
, false),
465 DEFINE_PROP_END_OF_LIST(),
468 static const VMStateDescription vfio_ccw_vmstate
= {
469 .name
= TYPE_VFIO_CCW
,
473 static void vfio_ccw_class_init(ObjectClass
*klass
, void *data
)
475 DeviceClass
*dc
= DEVICE_CLASS(klass
);
476 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_CLASS(klass
);
478 dc
->props
= vfio_ccw_properties
;
479 dc
->vmsd
= &vfio_ccw_vmstate
;
480 dc
->desc
= "VFIO-based subchannel assignment";
481 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
482 dc
->realize
= vfio_ccw_realize
;
483 dc
->unrealize
= vfio_ccw_unrealize
;
484 dc
->reset
= vfio_ccw_reset
;
486 cdc
->handle_request
= vfio_ccw_handle_request
;
489 static const TypeInfo vfio_ccw_info
= {
490 .name
= TYPE_VFIO_CCW
,
491 .parent
= TYPE_S390_CCW
,
492 .instance_size
= sizeof(VFIOCCWDevice
),
493 .class_init
= vfio_ccw_class_init
,
496 static void register_vfio_ccw_type(void)
498 type_register_static(&vfio_ccw_info
);
501 type_init(register_vfio_ccw_type
)