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"
29 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 SCHIB
*schib
= &sch
->curr_status
;
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 schib
->scsw
.flags
|= SCSW_FLAGS_MASK_CC
;
149 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
150 schib
->scsw
.ctrl
|= (SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
);
153 /* Memory problem, generate channel data check. */
154 schib
->scsw
.ctrl
&= ~SCSW_ACTL_START_PEND
;
155 schib
->scsw
.cstat
= SCSW_CSTAT_DATA_CHECK
;
156 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
157 schib
->scsw
.ctrl
|= SCSW_STCTL_PRIMARY
| SCSW_STCTL_SECONDARY
|
158 SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
;
161 /* Error, generate channel program check. */
162 schib
->scsw
.ctrl
&= ~SCSW_ACTL_START_PEND
;
163 schib
->scsw
.cstat
= SCSW_CSTAT_PROG_CHECK
;
164 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
165 schib
->scsw
.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 schib
->scsw
.ctrl
&= ~SCSW_ACTL_START_PEND
;
172 schib
->scsw
.cstat
= SCSW_CSTAT_CHN_CTRL_CHK
;
173 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
174 schib
->scsw
.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. */
183 copy_scsw_to_guest(&s
, &irb
.scsw
);
186 /* If a uint check is pending, copy sense data. */
187 if ((schib
->scsw
.dstat
& SCSW_DSTAT_UNIT_CHECK
) &&
188 (schib
->pmcw
.chars
& PMCW_CHARS_MASK_CSENSE
)) {
189 memcpy(sch
->sense_data
, irb
.ecw
, sizeof(irb
.ecw
));
193 css_inject_io_interrupt(sch
);
196 static void vfio_ccw_register_io_notifier(VFIOCCWDevice
*vcdev
, Error
**errp
)
198 VFIODevice
*vdev
= &vcdev
->vdev
;
199 struct vfio_irq_info
*irq_info
;
200 struct vfio_irq_set
*irq_set
;
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 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
227 irq_set
= g_malloc0(argsz
);
228 irq_set
->argsz
= argsz
;
229 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
230 VFIO_IRQ_SET_ACTION_TRIGGER
;
231 irq_set
->index
= VFIO_CCW_IO_IRQ_INDEX
;
234 pfd
= (int32_t *) &irq_set
->data
;
236 *pfd
= event_notifier_get_fd(&vcdev
->io_notifier
);
237 qemu_set_fd_handler(*pfd
, vfio_ccw_io_notifier_handler
, NULL
, vcdev
);
238 if (ioctl(vdev
->fd
, VFIO_DEVICE_SET_IRQS
, irq_set
)) {
239 error_setg(errp
, "vfio: Failed to set up io notification");
240 qemu_set_fd_handler(*pfd
, NULL
, NULL
, vcdev
);
241 event_notifier_cleanup(&vcdev
->io_notifier
);
250 static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice
*vcdev
)
252 struct vfio_irq_set
*irq_set
;
256 argsz
= sizeof(*irq_set
) + sizeof(*pfd
);
257 irq_set
= g_malloc0(argsz
);
258 irq_set
->argsz
= argsz
;
259 irq_set
->flags
= VFIO_IRQ_SET_DATA_EVENTFD
|
260 VFIO_IRQ_SET_ACTION_TRIGGER
;
261 irq_set
->index
= VFIO_CCW_IO_IRQ_INDEX
;
264 pfd
= (int32_t *) &irq_set
->data
;
267 if (ioctl(vcdev
->vdev
.fd
, VFIO_DEVICE_SET_IRQS
, irq_set
)) {
268 error_report("vfio: Failed to de-assign device io fd: %m");
271 qemu_set_fd_handler(event_notifier_get_fd(&vcdev
->io_notifier
),
273 event_notifier_cleanup(&vcdev
->io_notifier
);
278 static void vfio_ccw_get_region(VFIOCCWDevice
*vcdev
, Error
**errp
)
280 VFIODevice
*vdev
= &vcdev
->vdev
;
281 struct vfio_region_info
*info
;
284 /* Sanity check device */
285 if (!(vdev
->flags
& VFIO_DEVICE_FLAGS_CCW
)) {
286 error_setg(errp
, "vfio: Um, this isn't a vfio-ccw device");
290 if (vdev
->num_regions
< VFIO_CCW_CONFIG_REGION_INDEX
+ 1) {
291 error_setg(errp
, "vfio: Unexpected number of the I/O region %u",
296 ret
= vfio_get_region_info(vdev
, VFIO_CCW_CONFIG_REGION_INDEX
, &info
);
298 error_setg_errno(errp
, -ret
, "vfio: Error getting config info");
302 vcdev
->io_region_size
= info
->size
;
303 if (sizeof(*vcdev
->io_region
) != vcdev
->io_region_size
) {
304 error_setg(errp
, "vfio: Unexpected size of the I/O region");
309 vcdev
->io_region_offset
= info
->offset
;
310 vcdev
->io_region
= g_malloc0(info
->size
);
315 static void vfio_ccw_put_region(VFIOCCWDevice
*vcdev
)
317 g_free(vcdev
->io_region
);
320 static void vfio_ccw_put_device(VFIOCCWDevice
*vcdev
)
322 g_free(vcdev
->vdev
.name
);
323 vfio_put_base_device(&vcdev
->vdev
);
326 static void vfio_ccw_get_device(VFIOGroup
*group
, VFIOCCWDevice
*vcdev
,
329 char *name
= g_strdup_printf("%x.%x.%04x", vcdev
->cdev
.hostid
.cssid
,
330 vcdev
->cdev
.hostid
.ssid
,
331 vcdev
->cdev
.hostid
.devid
);
332 VFIODevice
*vbasedev
;
334 QLIST_FOREACH(vbasedev
, &group
->device_list
, next
) {
335 if (strcmp(vbasedev
->name
, name
) == 0) {
336 error_setg(errp
, "vfio: subchannel %s has already been attached",
343 * All vfio-ccw devices are believed to operate in a way compatible with
344 * memory ballooning, ie. pages pinned in the host are in the current
345 * working set of the guest driver and therefore never overlap with pages
346 * available to the guest balloon driver. This needs to be set before
347 * vfio_get_device() for vfio common to handle the balloon inhibitor.
349 vcdev
->vdev
.balloon_allowed
= true;
351 if (vfio_get_device(group
, vcdev
->cdev
.mdevid
, &vcdev
->vdev
, errp
)) {
355 vcdev
->vdev
.ops
= &vfio_ccw_ops
;
356 vcdev
->vdev
.type
= VFIO_DEVICE_TYPE_CCW
;
357 vcdev
->vdev
.name
= name
;
358 vcdev
->vdev
.dev
= &vcdev
->cdev
.parent_obj
.parent_obj
;
366 static VFIOGroup
*vfio_ccw_get_group(S390CCWDevice
*cdev
, Error
**errp
)
368 char *tmp
, group_path
[PATH_MAX
];
372 tmp
= g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group",
373 cdev
->hostid
.cssid
, cdev
->hostid
.ssid
,
374 cdev
->hostid
.devid
, cdev
->mdevid
);
375 len
= readlink(tmp
, group_path
, sizeof(group_path
));
378 if (len
<= 0 || len
>= sizeof(group_path
)) {
379 error_setg(errp
, "vfio: no iommu_group found");
385 if (sscanf(basename(group_path
), "%d", &groupid
) != 1) {
386 error_setg(errp
, "vfio: failed to read %s", group_path
);
390 return vfio_get_group(groupid
, &address_space_memory
, errp
);
393 static void vfio_ccw_realize(DeviceState
*dev
, Error
**errp
)
396 CcwDevice
*ccw_dev
= DO_UPCAST(CcwDevice
, parent_obj
, dev
);
397 S390CCWDevice
*cdev
= DO_UPCAST(S390CCWDevice
, parent_obj
, ccw_dev
);
398 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
399 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_GET_CLASS(cdev
);
402 /* Call the class init function for subchannel. */
404 cdc
->realize(cdev
, vcdev
->vdev
.sysfsdev
, &err
);
406 goto out_err_propagate
;
410 group
= vfio_ccw_get_group(cdev
, &err
);
415 vfio_ccw_get_device(group
, vcdev
, &err
);
420 vfio_ccw_get_region(vcdev
, &err
);
425 vfio_ccw_register_io_notifier(vcdev
, &err
);
427 goto out_notifier_err
;
433 vfio_ccw_put_region(vcdev
);
435 vfio_ccw_put_device(vcdev
);
437 vfio_put_group(group
);
439 if (cdc
->unrealize
) {
440 cdc
->unrealize(cdev
, NULL
);
443 error_propagate(errp
, err
);
446 static void vfio_ccw_unrealize(DeviceState
*dev
, Error
**errp
)
448 CcwDevice
*ccw_dev
= DO_UPCAST(CcwDevice
, parent_obj
, dev
);
449 S390CCWDevice
*cdev
= DO_UPCAST(S390CCWDevice
, parent_obj
, ccw_dev
);
450 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
451 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_GET_CLASS(cdev
);
452 VFIOGroup
*group
= vcdev
->vdev
.group
;
454 vfio_ccw_unregister_io_notifier(vcdev
);
455 vfio_ccw_put_region(vcdev
);
456 vfio_ccw_put_device(vcdev
);
457 vfio_put_group(group
);
459 if (cdc
->unrealize
) {
460 cdc
->unrealize(cdev
, errp
);
464 static Property vfio_ccw_properties
[] = {
465 DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice
, vdev
.sysfsdev
),
466 DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice
, force_orb_pfch
, false),
467 DEFINE_PROP_END_OF_LIST(),
470 static const VMStateDescription vfio_ccw_vmstate
= {
471 .name
= TYPE_VFIO_CCW
,
475 static void vfio_ccw_class_init(ObjectClass
*klass
, void *data
)
477 DeviceClass
*dc
= DEVICE_CLASS(klass
);
478 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_CLASS(klass
);
480 dc
->props
= vfio_ccw_properties
;
481 dc
->vmsd
= &vfio_ccw_vmstate
;
482 dc
->desc
= "VFIO-based subchannel assignment";
483 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
484 dc
->realize
= vfio_ccw_realize
;
485 dc
->unrealize
= vfio_ccw_unrealize
;
486 dc
->reset
= vfio_ccw_reset
;
488 cdc
->handle_request
= vfio_ccw_handle_request
;
491 static const TypeInfo vfio_ccw_info
= {
492 .name
= TYPE_VFIO_CCW
,
493 .parent
= TYPE_S390_CCW
,
494 .instance_size
= sizeof(VFIOCCWDevice
),
495 .class_init
= vfio_ccw_class_init
,
498 static void register_vfio_ccw_type(void)
500 type_register_static(&vfio_ccw_info
);
503 type_init(register_vfio_ccw_type
)