2 * vfio based subchannel assignment support
4 * Copyright 2017 IBM Corp.
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;
38 static void vfio_ccw_compute_needs_reset(VFIODevice *vdev)
40 vdev->needs_reset = false;
44 * We don't need vfio_hot_reset_multi and vfio_eoi operations for
45 * vfio_ccw device now.
47 struct VFIODeviceOps vfio_ccw_ops = {
48 .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset,
51 static IOInstEnding vfio_ccw_handle_request(SubchDev *sch)
53 S390CCWDevice *cdev = sch->driver_data;
54 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
55 struct ccw_io_region *region = vcdev->io_region;
58 QEMU_BUILD_BUG_ON(sizeof(region->orb_area) != sizeof(ORB));
59 QEMU_BUILD_BUG_ON(sizeof(region->scsw_area) != sizeof(SCSW));
60 QEMU_BUILD_BUG_ON(sizeof(region->irb_area) != sizeof(IRB));
62 memset(region, 0, sizeof(*region));
64 memcpy(region->orb_area, &sch->orb, sizeof(ORB));
65 memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW));
68 ret = pwrite(vcdev->vdev.fd, region,
69 vcdev->io_region_size, vcdev->io_region_offset);
70 if (ret != vcdev->io_region_size) {
71 if (errno == EAGAIN) {
74 error_report("vfio-ccw: wirte I/O region failed with errno=%d", errno);
77 ret = region->ret_code;
81 return IOINST_CC_EXPECTED;
83 return IOINST_CC_BUSY;
86 return IOINST_CC_NOT_OPERATIONAL;
89 sch_gen_unit_exception(sch);
90 css_inject_io_interrupt(sch);
91 return IOINST_CC_EXPECTED;
95 static void vfio_ccw_reset(DeviceState *dev)
97 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
98 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
99 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
101 ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET);
104 static void vfio_ccw_io_notifier_handler(void *opaque)
106 VFIOCCWDevice *vcdev = opaque;
107 struct ccw_io_region *region = vcdev->io_region;
108 S390CCWDevice *cdev = S390_CCW_DEVICE(vcdev);
109 CcwDevice *ccw_dev = CCW_DEVICE(cdev);
110 SubchDev *sch = ccw_dev->sch;
111 SCSW *s = &sch->curr_status.scsw;
112 PMCW *p = &sch->curr_status.pmcw;
116 if (!event_notifier_test_and_clear(&vcdev->io_notifier)) {
120 size = pread(vcdev->vdev.fd, region, vcdev->io_region_size,
121 vcdev->io_region_offset);
125 /* Generate a deferred cc 3 condition. */
126 s->flags |= SCSW_FLAGS_MASK_CC;
127 s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
128 s->ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND);
131 /* Memory problem, generate channel data check. */
132 s->ctrl &= ~SCSW_ACTL_START_PEND;
133 s->cstat = SCSW_CSTAT_DATA_CHECK;
134 s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
135 s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
136 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
139 /* Error, generate channel program check. */
140 s->ctrl &= ~SCSW_ACTL_START_PEND;
141 s->cstat = SCSW_CSTAT_PROG_CHECK;
142 s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
143 s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
144 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
147 } else if (size != vcdev->io_region_size) {
148 /* Information transfer error, generate channel-control check. */
149 s->ctrl &= ~SCSW_ACTL_START_PEND;
150 s->cstat = SCSW_CSTAT_CHN_CTRL_CHK;
151 s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
152 s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
153 SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
157 memcpy(&irb, region->irb_area, sizeof(IRB));
159 /* Update control block via irb. */
160 copy_scsw_to_guest(s, &irb.scsw);
162 /* If a uint check is pending, copy sense data. */
163 if ((s->dstat & SCSW_DSTAT_UNIT_CHECK) &&
164 (p->chars & PMCW_CHARS_MASK_CSENSE)) {
165 memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw));
169 css_inject_io_interrupt(sch);
172 static void vfio_ccw_register_io_notifier(VFIOCCWDevice *vcdev, Error **errp)
174 VFIODevice *vdev = &vcdev->vdev;
175 struct vfio_irq_info *irq_info;
176 struct vfio_irq_set *irq_set;
180 if (vdev->num_irqs < VFIO_CCW_IO_IRQ_INDEX + 1) {
181 error_setg(errp, "vfio: unexpected number of io irqs %u",
186 argsz = sizeof(*irq_info);
187 irq_info = g_malloc0(argsz);
188 irq_info->index = VFIO_CCW_IO_IRQ_INDEX;
189 irq_info->argsz = argsz;
190 if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO,
191 irq_info) < 0 || irq_info->count < 1) {
192 error_setg_errno(errp, errno, "vfio: Error getting irq info");
196 if (event_notifier_init(&vcdev->io_notifier, 0)) {
197 error_setg_errno(errp, errno,
198 "vfio: Unable to init event notifier for IO");
202 argsz = sizeof(*irq_set) + sizeof(*pfd);
203 irq_set = g_malloc0(argsz);
204 irq_set->argsz = argsz;
205 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
206 VFIO_IRQ_SET_ACTION_TRIGGER;
207 irq_set->index = VFIO_CCW_IO_IRQ_INDEX;
210 pfd = (int32_t *) &irq_set->data;
212 *pfd = event_notifier_get_fd(&vcdev->io_notifier);
213 qemu_set_fd_handler(*pfd, vfio_ccw_io_notifier_handler, NULL, vcdev);
214 if (ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
215 error_setg(errp, "vfio: Failed to set up io notification");
216 qemu_set_fd_handler(*pfd, NULL, NULL, vcdev);
217 event_notifier_cleanup(&vcdev->io_notifier);
226 static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice *vcdev)
228 struct vfio_irq_set *irq_set;
232 argsz = sizeof(*irq_set) + sizeof(*pfd);
233 irq_set = g_malloc0(argsz);
234 irq_set->argsz = argsz;
235 irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
236 VFIO_IRQ_SET_ACTION_TRIGGER;
237 irq_set->index = VFIO_CCW_IO_IRQ_INDEX;
240 pfd = (int32_t *) &irq_set->data;
243 if (ioctl(vcdev->vdev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
244 error_report("vfio: Failed to de-assign device io fd: %m");
247 qemu_set_fd_handler(event_notifier_get_fd(&vcdev->io_notifier),
249 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",
318 if (vfio_get_device(group, vcdev->cdev.mdevid, &vcdev->vdev, errp)) {
322 vcdev->vdev.ops = &vfio_ccw_ops;
323 vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW;
324 vcdev->vdev.name = name;
325 vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj;
333 static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp)
335 char *tmp, group_path[PATH_MAX];
339 tmp = g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group",
340 cdev->hostid.cssid, cdev->hostid.ssid,
341 cdev->hostid.devid, cdev->mdevid);
342 len = readlink(tmp, group_path, sizeof(group_path));
345 if (len <= 0 || len >= sizeof(group_path)) {
346 error_setg(errp, "vfio: no iommu_group found");
352 if (sscanf(basename(group_path), "%d", &groupid) != 1) {
353 error_setg(errp, "vfio: failed to read %s", group_path);
357 return vfio_get_group(groupid, &address_space_memory, errp);
360 static void vfio_ccw_realize(DeviceState *dev, Error **errp)
363 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
364 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
365 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
366 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
369 /* Call the class init function for subchannel. */
371 cdc->realize(cdev, vcdev->vdev.sysfsdev, &err);
373 goto out_err_propagate;
377 group = vfio_ccw_get_group(cdev, &err);
382 vfio_ccw_get_device(group, vcdev, &err);
387 vfio_ccw_get_region(vcdev, &err);
392 vfio_ccw_register_io_notifier(vcdev, &err);
394 goto out_notifier_err;
400 vfio_ccw_put_region(vcdev);
402 vfio_ccw_put_device(vcdev);
404 vfio_put_group(group);
406 if (cdc->unrealize) {
407 cdc->unrealize(cdev, NULL);
410 error_propagate(errp, err);
413 static void vfio_ccw_unrealize(DeviceState *dev, Error **errp)
415 CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
416 S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
417 VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
418 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
419 VFIOGroup *group = vcdev->vdev.group;
421 vfio_ccw_unregister_io_notifier(vcdev);
422 vfio_ccw_put_region(vcdev);
423 vfio_ccw_put_device(vcdev);
424 vfio_put_group(group);
426 if (cdc->unrealize) {
427 cdc->unrealize(cdev, errp);
431 static Property vfio_ccw_properties[] = {
432 DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev),
433 DEFINE_PROP_END_OF_LIST(),
436 static const VMStateDescription vfio_ccw_vmstate = {
437 .name = TYPE_VFIO_CCW,
441 static void vfio_ccw_class_init(ObjectClass *klass, void *data)
443 DeviceClass *dc = DEVICE_CLASS(klass);
444 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
446 dc->props = vfio_ccw_properties;
447 dc->vmsd = &vfio_ccw_vmstate;
448 dc->desc = "VFIO-based subchannel assignment";
449 set_bit(DEVICE_CATEGORY_MISC, dc->categories);
450 dc->realize = vfio_ccw_realize;
451 dc->unrealize = vfio_ccw_unrealize;
452 dc->reset = vfio_ccw_reset;
454 cdc->handle_request = vfio_ccw_handle_request;
457 static const TypeInfo vfio_ccw_info = {
458 .name = TYPE_VFIO_CCW,
459 .parent = TYPE_S390_CCW,
460 .instance_size = sizeof(VFIOCCWDevice),
461 .class_init = vfio_ccw_class_init,
464 static void register_vfio_ccw_type(void)
466 type_register_static(&vfio_ccw_info);
469 type_init(register_vfio_ccw_type)