vfio-ccw: use vfio_set_irq_signaling
[qemu/ar7.git] / hw / vfio / ccw.c
bloba18bbd33b05588818665dc26cba904cc97744cec
1 /*
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
11 * directory.
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 {
31 S390CCWDevice cdev;
32 VFIODevice vdev;
33 uint64_t io_region_size;
34 uint64_t io_region_offset;
35 struct ccw_io_region *io_region;
36 EventNotifier io_notifier;
37 bool force_orb_pfch;
38 bool warned_orb_pfch;
41 static inline void warn_once_pfch(VFIOCCWDevice *vcdev, SubchDev *sch,
42 const char *msg)
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;
67 int ret;
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;
75 } else {
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));
90 again:
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) {
95 goto again;
97 error_report("vfio-ccw: wirte I/O region failed with errno=%d", errno);
98 ret = -errno;
99 } else {
100 ret = region->ret_code;
102 switch (ret) {
103 case 0:
104 return IOINST_CC_EXPECTED;
105 case -EBUSY:
106 return IOINST_CC_BUSY;
107 case -ENODEV:
108 case -EACCES:
109 return IOINST_CC_NOT_OPERATIONAL;
110 case -EFAULT:
111 default:
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;
135 SCSW s;
136 IRB irb;
137 int size;
139 if (!event_notifier_test_and_clear(&vcdev->io_notifier)) {
140 return;
143 size = pread(vcdev->vdev.fd, region, vcdev->io_region_size,
144 vcdev->io_region_offset);
145 if (size == -1) {
146 switch (errno) {
147 case ENODEV:
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);
152 goto read_err;
153 case EFAULT:
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;
160 goto read_err;
161 default:
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;
168 goto read_err;
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;
177 goto read_err;
180 memcpy(&irb, region->irb_area, sizeof(IRB));
182 /* Update control block via irb. */
183 s = schib->scsw;
184 copy_scsw_to_guest(&s, &irb.scsw);
185 schib->scsw = s;
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));
193 read_err:
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;
201 size_t argsz;
202 int fd;
204 if (vdev->num_irqs < VFIO_CCW_IO_IRQ_INDEX + 1) {
205 error_setg(errp, "vfio: unexpected number of io irqs %u",
206 vdev->num_irqs);
207 return;
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");
217 goto out_free_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");
223 goto out_free_info;
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);
235 out_free_info:
236 g_free(irq_info);
239 static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice *vcdev)
241 Error *err = NULL;
243 vfio_set_irq_signaling(&vcdev->vdev, VFIO_CCW_IO_IRQ_INDEX, 0,
244 VFIO_IRQ_SET_ACTION_TRIGGER, -1, &err);
245 if (err) {
246 error_reportf_err(err, VFIO_MSG_PREFIX, vcdev->vdev.name);
249 qemu_set_fd_handler(event_notifier_get_fd(&vcdev->io_notifier),
250 NULL, NULL, vcdev);
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;
258 int ret;
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");
263 return;
266 if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) {
267 error_setg(errp, "vfio: Unexpected number of the I/O region %u",
268 vdev->num_regions);
269 return;
272 ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info);
273 if (ret) {
274 error_setg_errno(errp, -ret, "vfio: Error getting config info");
275 return;
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");
281 g_free(info);
282 return;
285 vcdev->io_region_offset = info->offset;
286 vcdev->io_region = g_malloc0(info->size);
288 g_free(info);
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,
303 Error **errp)
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",
313 name);
314 goto out_err;
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)) {
328 goto out_err;
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;
336 return;
338 out_err:
339 g_free(name);
342 static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp)
344 char *tmp, group_path[PATH_MAX];
345 ssize_t len;
346 int groupid;
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));
352 g_free(tmp);
354 if (len <= 0 || len >= sizeof(group_path)) {
355 error_setg(errp, "vfio: no iommu_group found");
356 return NULL;
359 group_path[len] = 0;
361 if (sscanf(basename(group_path), "%d", &groupid) != 1) {
362 error_setg(errp, "vfio: failed to read %s", group_path);
363 return NULL;
366 return vfio_get_group(groupid, &address_space_memory, errp);
369 static void vfio_ccw_realize(DeviceState *dev, Error **errp)
371 VFIOGroup *group;
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);
376 Error *err = NULL;
378 /* Call the class init function for subchannel. */
379 if (cdc->realize) {
380 cdc->realize(cdev, vcdev->vdev.sysfsdev, &err);
381 if (err) {
382 goto out_err_propagate;
386 group = vfio_ccw_get_group(cdev, &err);
387 if (!group) {
388 goto out_group_err;
391 vfio_ccw_get_device(group, vcdev, &err);
392 if (err) {
393 goto out_device_err;
396 vfio_ccw_get_region(vcdev, &err);
397 if (err) {
398 goto out_region_err;
401 vfio_ccw_register_io_notifier(vcdev, &err);
402 if (err) {
403 goto out_notifier_err;
406 return;
408 out_notifier_err:
409 vfio_ccw_put_region(vcdev);
410 out_region_err:
411 vfio_ccw_put_device(vcdev);
412 out_device_err:
413 vfio_put_group(group);
414 out_group_err:
415 if (cdc->unrealize) {
416 cdc->unrealize(cdev, NULL);
418 out_err_propagate:
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 = {
447 .name = "vfio-ccw",
448 .unmigratable = 1,
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)