xref: /openbmc/qemu/hw/vfio/ccw.c (revision 520e210c)
1 /*
2  * vfio based subchannel assignment support
3  *
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>
8  *
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.
12  */
13 
14 #include "qemu/osdep.h"
15 #include <linux/vfio.h>
16 #include <linux/vfio_ccw.h>
17 #include <sys/ioctl.h>
18 
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"
27 
28 #define TYPE_VFIO_CCW "vfio-ccw"
29 typedef struct VFIOCCWDevice {
30     S390CCWDevice cdev;
31     VFIODevice vdev;
32     uint64_t io_region_size;
33     uint64_t io_region_offset;
34     struct ccw_io_region *io_region;
35     EventNotifier io_notifier;
36     bool force_orb_pfch;
37     bool warned_orb_pfch;
38 } VFIOCCWDevice;
39 
40 static inline void warn_once_pfch(VFIOCCWDevice *vcdev, SubchDev *sch,
41                                   const char *msg)
42 {
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);
46 }
47 
48 static void vfio_ccw_compute_needs_reset(VFIODevice *vdev)
49 {
50     vdev->needs_reset = false;
51 }
52 
53 /*
54  * We don't need vfio_hot_reset_multi and vfio_eoi operations for
55  * vfio_ccw device now.
56  */
57 struct VFIODeviceOps vfio_ccw_ops = {
58     .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset,
59 };
60 
61 static IOInstEnding vfio_ccw_handle_request(SubchDev *sch)
62 {
63     S390CCWDevice *cdev = sch->driver_data;
64     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
65     struct ccw_io_region *region = vcdev->io_region;
66     int ret;
67 
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;
74         } else {
75             sch->orb.ctrl0 |= ORB_CTRL0_MASK_PFCH;
76             warn_once_pfch(vcdev, sch, "PFCH flag forced");
77         }
78     }
79 
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));
83 
84     memset(region, 0, sizeof(*region));
85 
86     memcpy(region->orb_area, &sch->orb, sizeof(ORB));
87     memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW));
88 
89 again:
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) {
94             goto again;
95         }
96         error_report("vfio-ccw: wirte I/O region failed with errno=%d", errno);
97         ret = -errno;
98     } else {
99         ret = region->ret_code;
100     }
101     switch (ret) {
102     case 0:
103         return IOINST_CC_EXPECTED;
104     case -EBUSY:
105         return IOINST_CC_BUSY;
106     case -ENODEV:
107     case -EACCES:
108         return IOINST_CC_NOT_OPERATIONAL;
109     case -EFAULT:
110     default:
111         sch_gen_unit_exception(sch);
112         css_inject_io_interrupt(sch);
113         return IOINST_CC_EXPECTED;
114     }
115 }
116 
117 static void vfio_ccw_reset(DeviceState *dev)
118 {
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);
122 
123     ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET);
124 }
125 
126 static void vfio_ccw_io_notifier_handler(void *opaque)
127 {
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;
135     IRB irb;
136     int size;
137 
138     if (!event_notifier_test_and_clear(&vcdev->io_notifier)) {
139         return;
140     }
141 
142     size = pread(vcdev->vdev.fd, region, vcdev->io_region_size,
143                  vcdev->io_region_offset);
144     if (size == -1) {
145         switch (errno) {
146         case ENODEV:
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);
151             goto read_err;
152         case EFAULT:
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;
159             goto read_err;
160         default:
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;
167             goto read_err;
168         }
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;
176         goto read_err;
177     }
178 
179     memcpy(&irb, region->irb_area, sizeof(IRB));
180 
181     /* Update control block via irb. */
182     copy_scsw_to_guest(s, &irb.scsw);
183 
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));
188     }
189 
190 read_err:
191     css_inject_io_interrupt(sch);
192 }
193 
194 static void vfio_ccw_register_io_notifier(VFIOCCWDevice *vcdev, Error **errp)
195 {
196     VFIODevice *vdev = &vcdev->vdev;
197     struct vfio_irq_info *irq_info;
198     struct vfio_irq_set *irq_set;
199     size_t argsz;
200     int32_t *pfd;
201 
202     if (vdev->num_irqs < VFIO_CCW_IO_IRQ_INDEX + 1) {
203         error_setg(errp, "vfio: unexpected number of io irqs %u",
204                    vdev->num_irqs);
205         return;
206     }
207 
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");
215         goto out_free_info;
216     }
217 
218     if (event_notifier_init(&vcdev->io_notifier, 0)) {
219         error_setg_errno(errp, errno,
220                          "vfio: Unable to init event notifier for IO");
221         goto out_free_info;
222     }
223 
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;
230     irq_set->start = 0;
231     irq_set->count = 1;
232     pfd = (int32_t *) &irq_set->data;
233 
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);
240     }
241 
242     g_free(irq_set);
243 
244 out_free_info:
245     g_free(irq_info);
246 }
247 
248 static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice *vcdev)
249 {
250     struct vfio_irq_set *irq_set;
251     size_t argsz;
252     int32_t *pfd;
253 
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;
260     irq_set->start = 0;
261     irq_set->count = 1;
262     pfd = (int32_t *) &irq_set->data;
263     *pfd = -1;
264 
265     if (ioctl(vcdev->vdev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
266         error_report("vfio: Failed to de-assign device io fd: %m");
267     }
268 
269     qemu_set_fd_handler(event_notifier_get_fd(&vcdev->io_notifier),
270                         NULL, NULL, vcdev);
271     event_notifier_cleanup(&vcdev->io_notifier);
272 
273     g_free(irq_set);
274 }
275 
276 static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp)
277 {
278     VFIODevice *vdev = &vcdev->vdev;
279     struct vfio_region_info *info;
280     int ret;
281 
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");
285         return;
286     }
287 
288     if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) {
289         error_setg(errp, "vfio: Unexpected number of the I/O region %u",
290                    vdev->num_regions);
291         return;
292     }
293 
294     ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info);
295     if (ret) {
296         error_setg_errno(errp, -ret, "vfio: Error getting config info");
297         return;
298     }
299 
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");
303         g_free(info);
304         return;
305     }
306 
307     vcdev->io_region_offset = info->offset;
308     vcdev->io_region = g_malloc0(info->size);
309 
310     g_free(info);
311 }
312 
313 static void vfio_ccw_put_region(VFIOCCWDevice *vcdev)
314 {
315     g_free(vcdev->io_region);
316 }
317 
318 static void vfio_ccw_put_device(VFIOCCWDevice *vcdev)
319 {
320     g_free(vcdev->vdev.name);
321     vfio_put_base_device(&vcdev->vdev);
322 }
323 
324 static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
325                                 Error **errp)
326 {
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;
331 
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",
335                        name);
336             goto out_err;
337         }
338     }
339 
340     /*
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.
346      */
347     vcdev->vdev.balloon_allowed = true;
348 
349     if (vfio_get_device(group, vcdev->cdev.mdevid, &vcdev->vdev, errp)) {
350         goto out_err;
351     }
352 
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;
357 
358     return;
359 
360 out_err:
361     g_free(name);
362 }
363 
364 static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp)
365 {
366     char *tmp, group_path[PATH_MAX];
367     ssize_t len;
368     int groupid;
369 
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));
374     g_free(tmp);
375 
376     if (len <= 0 || len >= sizeof(group_path)) {
377         error_setg(errp, "vfio: no iommu_group found");
378         return NULL;
379     }
380 
381     group_path[len] = 0;
382 
383     if (sscanf(basename(group_path), "%d", &groupid) != 1) {
384         error_setg(errp, "vfio: failed to read %s", group_path);
385         return NULL;
386     }
387 
388     return vfio_get_group(groupid, &address_space_memory, errp);
389 }
390 
391 static void vfio_ccw_realize(DeviceState *dev, Error **errp)
392 {
393     VFIOGroup *group;
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);
398     Error *err = NULL;
399 
400     /* Call the class init function for subchannel. */
401     if (cdc->realize) {
402         cdc->realize(cdev, vcdev->vdev.sysfsdev, &err);
403         if (err) {
404             goto out_err_propagate;
405         }
406     }
407 
408     group = vfio_ccw_get_group(cdev, &err);
409     if (!group) {
410         goto out_group_err;
411     }
412 
413     vfio_ccw_get_device(group, vcdev, &err);
414     if (err) {
415         goto out_device_err;
416     }
417 
418     vfio_ccw_get_region(vcdev, &err);
419     if (err) {
420         goto out_region_err;
421     }
422 
423     vfio_ccw_register_io_notifier(vcdev, &err);
424     if (err) {
425         goto out_notifier_err;
426     }
427 
428     return;
429 
430 out_notifier_err:
431     vfio_ccw_put_region(vcdev);
432 out_region_err:
433     vfio_ccw_put_device(vcdev);
434 out_device_err:
435     vfio_put_group(group);
436 out_group_err:
437     if (cdc->unrealize) {
438         cdc->unrealize(cdev, NULL);
439     }
440 out_err_propagate:
441     error_propagate(errp, err);
442 }
443 
444 static void vfio_ccw_unrealize(DeviceState *dev, Error **errp)
445 {
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;
451 
452     vfio_ccw_unregister_io_notifier(vcdev);
453     vfio_ccw_put_region(vcdev);
454     vfio_ccw_put_device(vcdev);
455     vfio_put_group(group);
456 
457     if (cdc->unrealize) {
458         cdc->unrealize(cdev, errp);
459     }
460 }
461 
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(),
466 };
467 
468 static const VMStateDescription vfio_ccw_vmstate = {
469     .name = TYPE_VFIO_CCW,
470     .unmigratable = 1,
471 };
472 
473 static void vfio_ccw_class_init(ObjectClass *klass, void *data)
474 {
475     DeviceClass *dc = DEVICE_CLASS(klass);
476     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
477 
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;
485 
486     cdc->handle_request = vfio_ccw_handle_request;
487 }
488 
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,
494 };
495 
496 static void register_vfio_ccw_type(void)
497 {
498     type_register_static(&vfio_ccw_info);
499 }
500 
501 type_init(register_vfio_ccw_type)
502