2 * vfio based subchannel assignment support
4 * Copyright 2017 IBM Corp.
5 * Copyright 2019 Red Hat, Inc.
7 * Author(s): Dong Jia Shi <bjsdjshi@linux.vnet.ibm.com>
8 * Xiao Feng Ren <renxiaof@linux.vnet.ibm.com>
9 * Pierre Morel <pmorel@linux.vnet.ibm.com>
10 * Cornelia Huck <cohuck@redhat.com>
12 * This work is licensed under the terms of the GNU GPL, version 2 or (at
13 * your option) any later version. See the COPYING file in the top-level
17 #include "qemu/osdep.h"
18 #include <linux/vfio.h>
19 #include <linux/vfio_ccw.h>
20 #include <sys/ioctl.h>
22 #include "qapi/error.h"
23 #include "hw/sysbus.h"
24 #include "hw/vfio/vfio.h"
25 #include "hw/vfio/vfio-common.h"
26 #include "hw/s390x/s390-ccw.h"
27 #include "hw/s390x/vfio-ccw.h"
28 #include "hw/s390x/ccw-device.h"
29 #include "exec/address-spaces.h"
30 #include "qemu/error-report.h"
31 #include "qemu/module.h"
33 struct VFIOCCWDevice
{
36 uint64_t io_region_size
;
37 uint64_t io_region_offset
;
38 struct ccw_io_region
*io_region
;
39 uint64_t async_cmd_region_size
;
40 uint64_t async_cmd_region_offset
;
41 struct ccw_cmd_region
*async_cmd_region
;
42 EventNotifier io_notifier
;
47 static inline void warn_once_pfch(VFIOCCWDevice
*vcdev
, SubchDev
*sch
,
50 warn_report_once_cond(&vcdev
->warned_orb_pfch
,
51 "vfio-ccw (devno %x.%x.%04x): %s",
52 sch
->cssid
, sch
->ssid
, sch
->devno
, msg
);
55 static void vfio_ccw_compute_needs_reset(VFIODevice
*vdev
)
57 vdev
->needs_reset
= false;
61 * We don't need vfio_hot_reset_multi and vfio_eoi operations for
62 * vfio_ccw device now.
64 struct VFIODeviceOps vfio_ccw_ops
= {
65 .vfio_compute_needs_reset
= vfio_ccw_compute_needs_reset
,
68 static IOInstEnding
vfio_ccw_handle_request(SubchDev
*sch
)
70 S390CCWDevice
*cdev
= sch
->driver_data
;
71 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
72 struct ccw_io_region
*region
= vcdev
->io_region
;
75 if (!(sch
->orb
.ctrl0
& ORB_CTRL0_MASK_PFCH
)) {
76 if (!(vcdev
->force_orb_pfch
)) {
77 warn_once_pfch(vcdev
, sch
, "requires PFCH flag set");
78 sch_gen_unit_exception(sch
);
79 css_inject_io_interrupt(sch
);
80 return IOINST_CC_EXPECTED
;
82 sch
->orb
.ctrl0
|= ORB_CTRL0_MASK_PFCH
;
83 warn_once_pfch(vcdev
, sch
, "PFCH flag forced");
87 QEMU_BUILD_BUG_ON(sizeof(region
->orb_area
) != sizeof(ORB
));
88 QEMU_BUILD_BUG_ON(sizeof(region
->scsw_area
) != sizeof(SCSW
));
89 QEMU_BUILD_BUG_ON(sizeof(region
->irb_area
) != sizeof(IRB
));
91 memset(region
, 0, sizeof(*region
));
93 memcpy(region
->orb_area
, &sch
->orb
, sizeof(ORB
));
94 memcpy(region
->scsw_area
, &sch
->curr_status
.scsw
, sizeof(SCSW
));
97 ret
= pwrite(vcdev
->vdev
.fd
, region
,
98 vcdev
->io_region_size
, vcdev
->io_region_offset
);
99 if (ret
!= vcdev
->io_region_size
) {
100 if (errno
== EAGAIN
) {
103 error_report("vfio-ccw: wirte I/O region failed with errno=%d", errno
);
106 ret
= region
->ret_code
;
110 return IOINST_CC_EXPECTED
;
112 return IOINST_CC_BUSY
;
115 return IOINST_CC_NOT_OPERATIONAL
;
118 sch_gen_unit_exception(sch
);
119 css_inject_io_interrupt(sch
);
120 return IOINST_CC_EXPECTED
;
124 static int vfio_ccw_handle_clear(SubchDev
*sch
)
126 S390CCWDevice
*cdev
= sch
->driver_data
;
127 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
128 struct ccw_cmd_region
*region
= vcdev
->async_cmd_region
;
131 if (!vcdev
->async_cmd_region
) {
132 /* Async command region not available, fall back to emulation */
136 memset(region
, 0, sizeof(*region
));
137 region
->command
= VFIO_CCW_ASYNC_CMD_CSCH
;
140 ret
= pwrite(vcdev
->vdev
.fd
, region
,
141 vcdev
->async_cmd_region_size
, vcdev
->async_cmd_region_offset
);
142 if (ret
!= vcdev
->async_cmd_region_size
) {
143 if (errno
== EAGAIN
) {
146 error_report("vfio-ccw: write cmd region failed with errno=%d", errno
);
149 ret
= region
->ret_code
;
158 sch_gen_unit_exception(sch
);
159 css_inject_io_interrupt(sch
);
164 static int vfio_ccw_handle_halt(SubchDev
*sch
)
166 S390CCWDevice
*cdev
= sch
->driver_data
;
167 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
168 struct ccw_cmd_region
*region
= vcdev
->async_cmd_region
;
171 if (!vcdev
->async_cmd_region
) {
172 /* Async command region not available, fall back to emulation */
176 memset(region
, 0, sizeof(*region
));
177 region
->command
= VFIO_CCW_ASYNC_CMD_HSCH
;
180 ret
= pwrite(vcdev
->vdev
.fd
, region
,
181 vcdev
->async_cmd_region_size
, vcdev
->async_cmd_region_offset
);
182 if (ret
!= vcdev
->async_cmd_region_size
) {
183 if (errno
== EAGAIN
) {
186 error_report("vfio-ccw: write cmd region failed with errno=%d", errno
);
189 ret
= region
->ret_code
;
199 sch_gen_unit_exception(sch
);
200 css_inject_io_interrupt(sch
);
205 static void vfio_ccw_reset(DeviceState
*dev
)
207 CcwDevice
*ccw_dev
= DO_UPCAST(CcwDevice
, parent_obj
, dev
);
208 S390CCWDevice
*cdev
= DO_UPCAST(S390CCWDevice
, parent_obj
, ccw_dev
);
209 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
211 ioctl(vcdev
->vdev
.fd
, VFIO_DEVICE_RESET
);
214 static void vfio_ccw_io_notifier_handler(void *opaque
)
216 VFIOCCWDevice
*vcdev
= opaque
;
217 struct ccw_io_region
*region
= vcdev
->io_region
;
218 S390CCWDevice
*cdev
= S390_CCW_DEVICE(vcdev
);
219 CcwDevice
*ccw_dev
= CCW_DEVICE(cdev
);
220 SubchDev
*sch
= ccw_dev
->sch
;
221 SCHIB
*schib
= &sch
->curr_status
;
226 if (!event_notifier_test_and_clear(&vcdev
->io_notifier
)) {
230 size
= pread(vcdev
->vdev
.fd
, region
, vcdev
->io_region_size
,
231 vcdev
->io_region_offset
);
235 /* Generate a deferred cc 3 condition. */
236 schib
->scsw
.flags
|= SCSW_FLAGS_MASK_CC
;
237 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
238 schib
->scsw
.ctrl
|= (SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
);
241 /* Memory problem, generate channel data check. */
242 schib
->scsw
.ctrl
&= ~SCSW_ACTL_START_PEND
;
243 schib
->scsw
.cstat
= SCSW_CSTAT_DATA_CHECK
;
244 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
245 schib
->scsw
.ctrl
|= SCSW_STCTL_PRIMARY
| SCSW_STCTL_SECONDARY
|
246 SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
;
249 /* Error, generate channel program check. */
250 schib
->scsw
.ctrl
&= ~SCSW_ACTL_START_PEND
;
251 schib
->scsw
.cstat
= SCSW_CSTAT_PROG_CHECK
;
252 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
253 schib
->scsw
.ctrl
|= SCSW_STCTL_PRIMARY
| SCSW_STCTL_SECONDARY
|
254 SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
;
257 } else if (size
!= vcdev
->io_region_size
) {
258 /* Information transfer error, generate channel-control check. */
259 schib
->scsw
.ctrl
&= ~SCSW_ACTL_START_PEND
;
260 schib
->scsw
.cstat
= SCSW_CSTAT_CHN_CTRL_CHK
;
261 schib
->scsw
.ctrl
&= ~SCSW_CTRL_MASK_STCTL
;
262 schib
->scsw
.ctrl
|= SCSW_STCTL_PRIMARY
| SCSW_STCTL_SECONDARY
|
263 SCSW_STCTL_ALERT
| SCSW_STCTL_STATUS_PEND
;
267 memcpy(&irb
, region
->irb_area
, sizeof(IRB
));
269 /* Update control block via irb. */
271 copy_scsw_to_guest(&s
, &irb
.scsw
);
274 /* If a uint check is pending, copy sense data. */
275 if ((schib
->scsw
.dstat
& SCSW_DSTAT_UNIT_CHECK
) &&
276 (schib
->pmcw
.chars
& PMCW_CHARS_MASK_CSENSE
)) {
277 memcpy(sch
->sense_data
, irb
.ecw
, sizeof(irb
.ecw
));
281 css_inject_io_interrupt(sch
);
284 static void vfio_ccw_register_io_notifier(VFIOCCWDevice
*vcdev
, Error
**errp
)
286 VFIODevice
*vdev
= &vcdev
->vdev
;
287 struct vfio_irq_info
*irq_info
;
291 if (vdev
->num_irqs
< VFIO_CCW_IO_IRQ_INDEX
+ 1) {
292 error_setg(errp
, "vfio: unexpected number of io irqs %u",
297 argsz
= sizeof(*irq_info
);
298 irq_info
= g_malloc0(argsz
);
299 irq_info
->index
= VFIO_CCW_IO_IRQ_INDEX
;
300 irq_info
->argsz
= argsz
;
301 if (ioctl(vdev
->fd
, VFIO_DEVICE_GET_IRQ_INFO
,
302 irq_info
) < 0 || irq_info
->count
< 1) {
303 error_setg_errno(errp
, errno
, "vfio: Error getting irq info");
307 if (event_notifier_init(&vcdev
->io_notifier
, 0)) {
308 error_setg_errno(errp
, errno
,
309 "vfio: Unable to init event notifier for IO");
313 fd
= event_notifier_get_fd(&vcdev
->io_notifier
);
314 qemu_set_fd_handler(fd
, vfio_ccw_io_notifier_handler
, NULL
, vcdev
);
316 if (vfio_set_irq_signaling(vdev
, VFIO_CCW_IO_IRQ_INDEX
, 0,
317 VFIO_IRQ_SET_ACTION_TRIGGER
, fd
, errp
)) {
318 qemu_set_fd_handler(fd
, NULL
, NULL
, vcdev
);
319 event_notifier_cleanup(&vcdev
->io_notifier
);
326 static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice
*vcdev
)
330 if (vfio_set_irq_signaling(&vcdev
->vdev
, VFIO_CCW_IO_IRQ_INDEX
, 0,
331 VFIO_IRQ_SET_ACTION_TRIGGER
, -1, &err
)) {
332 error_reportf_err(err
, VFIO_MSG_PREFIX
, vcdev
->vdev
.name
);
335 qemu_set_fd_handler(event_notifier_get_fd(&vcdev
->io_notifier
),
337 event_notifier_cleanup(&vcdev
->io_notifier
);
340 static void vfio_ccw_get_region(VFIOCCWDevice
*vcdev
, Error
**errp
)
342 VFIODevice
*vdev
= &vcdev
->vdev
;
343 struct vfio_region_info
*info
;
346 /* Sanity check device */
347 if (!(vdev
->flags
& VFIO_DEVICE_FLAGS_CCW
)) {
348 error_setg(errp
, "vfio: Um, this isn't a vfio-ccw device");
353 * We always expect at least the I/O region to be present. We also
354 * may have a variable number of regions governed by capabilities.
356 if (vdev
->num_regions
< VFIO_CCW_CONFIG_REGION_INDEX
+ 1) {
357 error_setg(errp
, "vfio: too few regions (%u), expected at least %u",
358 vdev
->num_regions
, VFIO_CCW_CONFIG_REGION_INDEX
+ 1);
362 ret
= vfio_get_region_info(vdev
, VFIO_CCW_CONFIG_REGION_INDEX
, &info
);
364 error_setg_errno(errp
, -ret
, "vfio: Error getting config info");
368 vcdev
->io_region_size
= info
->size
;
369 if (sizeof(*vcdev
->io_region
) != vcdev
->io_region_size
) {
370 error_setg(errp
, "vfio: Unexpected size of the I/O region");
375 vcdev
->io_region_offset
= info
->offset
;
376 vcdev
->io_region
= g_malloc0(info
->size
);
378 /* check for the optional async command region */
379 ret
= vfio_get_dev_region_info(vdev
, VFIO_REGION_TYPE_CCW
,
380 VFIO_REGION_SUBTYPE_CCW_ASYNC_CMD
, &info
);
382 vcdev
->async_cmd_region_size
= info
->size
;
383 if (sizeof(*vcdev
->async_cmd_region
) != vcdev
->async_cmd_region_size
) {
384 error_setg(errp
, "vfio: Unexpected size of the async cmd region");
385 g_free(vcdev
->io_region
);
389 vcdev
->async_cmd_region_offset
= info
->offset
;
390 vcdev
->async_cmd_region
= g_malloc0(info
->size
);
396 static void vfio_ccw_put_region(VFIOCCWDevice
*vcdev
)
398 g_free(vcdev
->async_cmd_region
);
399 g_free(vcdev
->io_region
);
402 static void vfio_ccw_put_device(VFIOCCWDevice
*vcdev
)
404 g_free(vcdev
->vdev
.name
);
405 vfio_put_base_device(&vcdev
->vdev
);
408 static void vfio_ccw_get_device(VFIOGroup
*group
, VFIOCCWDevice
*vcdev
,
411 char *name
= g_strdup_printf("%x.%x.%04x", vcdev
->cdev
.hostid
.cssid
,
412 vcdev
->cdev
.hostid
.ssid
,
413 vcdev
->cdev
.hostid
.devid
);
414 VFIODevice
*vbasedev
;
416 QLIST_FOREACH(vbasedev
, &group
->device_list
, next
) {
417 if (strcmp(vbasedev
->name
, name
) == 0) {
418 error_setg(errp
, "vfio: subchannel %s has already been attached",
425 * All vfio-ccw devices are believed to operate in a way compatible with
426 * memory ballooning, ie. pages pinned in the host are in the current
427 * working set of the guest driver and therefore never overlap with pages
428 * available to the guest balloon driver. This needs to be set before
429 * vfio_get_device() for vfio common to handle the balloon inhibitor.
431 vcdev
->vdev
.balloon_allowed
= true;
433 if (vfio_get_device(group
, vcdev
->cdev
.mdevid
, &vcdev
->vdev
, errp
)) {
437 vcdev
->vdev
.ops
= &vfio_ccw_ops
;
438 vcdev
->vdev
.type
= VFIO_DEVICE_TYPE_CCW
;
439 vcdev
->vdev
.name
= name
;
440 vcdev
->vdev
.dev
= &vcdev
->cdev
.parent_obj
.parent_obj
;
448 static VFIOGroup
*vfio_ccw_get_group(S390CCWDevice
*cdev
, Error
**errp
)
450 char *tmp
, group_path
[PATH_MAX
];
454 tmp
= g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group",
455 cdev
->hostid
.cssid
, cdev
->hostid
.ssid
,
456 cdev
->hostid
.devid
, cdev
->mdevid
);
457 len
= readlink(tmp
, group_path
, sizeof(group_path
));
460 if (len
<= 0 || len
>= sizeof(group_path
)) {
461 error_setg(errp
, "vfio: no iommu_group found");
467 if (sscanf(basename(group_path
), "%d", &groupid
) != 1) {
468 error_setg(errp
, "vfio: failed to read %s", group_path
);
472 return vfio_get_group(groupid
, &address_space_memory
, errp
);
475 static void vfio_ccw_realize(DeviceState
*dev
, Error
**errp
)
478 CcwDevice
*ccw_dev
= DO_UPCAST(CcwDevice
, parent_obj
, dev
);
479 S390CCWDevice
*cdev
= DO_UPCAST(S390CCWDevice
, parent_obj
, ccw_dev
);
480 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
481 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_GET_CLASS(cdev
);
484 /* Call the class init function for subchannel. */
486 cdc
->realize(cdev
, vcdev
->vdev
.sysfsdev
, &err
);
488 goto out_err_propagate
;
492 group
= vfio_ccw_get_group(cdev
, &err
);
497 vfio_ccw_get_device(group
, vcdev
, &err
);
502 vfio_ccw_get_region(vcdev
, &err
);
507 vfio_ccw_register_io_notifier(vcdev
, &err
);
509 goto out_notifier_err
;
515 vfio_ccw_put_region(vcdev
);
517 vfio_ccw_put_device(vcdev
);
519 vfio_put_group(group
);
521 if (cdc
->unrealize
) {
522 cdc
->unrealize(cdev
, NULL
);
525 error_propagate(errp
, err
);
528 static void vfio_ccw_unrealize(DeviceState
*dev
, Error
**errp
)
530 CcwDevice
*ccw_dev
= DO_UPCAST(CcwDevice
, parent_obj
, dev
);
531 S390CCWDevice
*cdev
= DO_UPCAST(S390CCWDevice
, parent_obj
, ccw_dev
);
532 VFIOCCWDevice
*vcdev
= DO_UPCAST(VFIOCCWDevice
, cdev
, cdev
);
533 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_GET_CLASS(cdev
);
534 VFIOGroup
*group
= vcdev
->vdev
.group
;
536 vfio_ccw_unregister_io_notifier(vcdev
);
537 vfio_ccw_put_region(vcdev
);
538 vfio_ccw_put_device(vcdev
);
539 vfio_put_group(group
);
541 if (cdc
->unrealize
) {
542 cdc
->unrealize(cdev
, errp
);
546 static Property vfio_ccw_properties
[] = {
547 DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice
, vdev
.sysfsdev
),
548 DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice
, force_orb_pfch
, false),
549 DEFINE_PROP_END_OF_LIST(),
552 static const VMStateDescription vfio_ccw_vmstate
= {
557 static void vfio_ccw_class_init(ObjectClass
*klass
, void *data
)
559 DeviceClass
*dc
= DEVICE_CLASS(klass
);
560 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_CLASS(klass
);
562 dc
->props
= vfio_ccw_properties
;
563 dc
->vmsd
= &vfio_ccw_vmstate
;
564 dc
->desc
= "VFIO-based subchannel assignment";
565 set_bit(DEVICE_CATEGORY_MISC
, dc
->categories
);
566 dc
->realize
= vfio_ccw_realize
;
567 dc
->unrealize
= vfio_ccw_unrealize
;
568 dc
->reset
= vfio_ccw_reset
;
570 cdc
->handle_request
= vfio_ccw_handle_request
;
571 cdc
->handle_halt
= vfio_ccw_handle_halt
;
572 cdc
->handle_clear
= vfio_ccw_handle_clear
;
575 static const TypeInfo vfio_ccw_info
= {
576 .name
= TYPE_VFIO_CCW
,
577 .parent
= TYPE_S390_CCW
,
578 .instance_size
= sizeof(VFIOCCWDevice
),
579 .class_init
= vfio_ccw_class_init
,
582 static void register_vfio_ccw_type(void)
584 type_register_static(&vfio_ccw_info
);
587 type_init(register_vfio_ccw_type
)