xref: /qemu/hw/vfio/ccw.c (revision bf913672)
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/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"
29 
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;
39 };
40 
41 static inline void warn_once_pfch(VFIOCCWDevice *vcdev, SubchDev *sch,
42                                   const char *msg)
43 {
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);
47 }
48 
49 static void vfio_ccw_compute_needs_reset(VFIODevice *vdev)
50 {
51     vdev->needs_reset = false;
52 }
53 
54 /*
55  * We don't need vfio_hot_reset_multi and vfio_eoi operations for
56  * vfio_ccw device now.
57  */
58 struct VFIODeviceOps vfio_ccw_ops = {
59     .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset,
60 };
61 
62 static IOInstEnding vfio_ccw_handle_request(SubchDev *sch)
63 {
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;
68 
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");
78         }
79     }
80 
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));
84 
85     memset(region, 0, sizeof(*region));
86 
87     memcpy(region->orb_area, &sch->orb, sizeof(ORB));
88     memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW));
89 
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;
96         }
97         error_report("vfio-ccw: wirte I/O region failed with errno=%d", errno);
98         ret = -errno;
99     } else {
100         ret = region->ret_code;
101     }
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;
115     }
116 }
117 
118 static void vfio_ccw_reset(DeviceState *dev)
119 {
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);
123 
124     ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET);
125 }
126 
127 static void vfio_ccw_io_notifier_handler(void *opaque)
128 {
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;
138 
139     if (!event_notifier_test_and_clear(&vcdev->io_notifier)) {
140         return;
141     }
142 
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;
169         }
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;
178     }
179 
180     memcpy(&irb, region->irb_area, sizeof(IRB));
181 
182     /* Update control block via irb. */
183     s = schib->scsw;
184     copy_scsw_to_guest(&s, &irb.scsw);
185     schib->scsw = s;
186 
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));
191     }
192 
193 read_err:
194     css_inject_io_interrupt(sch);
195 }
196 
197 static void vfio_ccw_register_io_notifier(VFIOCCWDevice *vcdev, Error **errp)
198 {
199     VFIODevice *vdev = &vcdev->vdev;
200     struct vfio_irq_info *irq_info;
201     struct vfio_irq_set *irq_set;
202     size_t argsz;
203     int32_t *pfd;
204 
205     if (vdev->num_irqs < VFIO_CCW_IO_IRQ_INDEX + 1) {
206         error_setg(errp, "vfio: unexpected number of io irqs %u",
207                    vdev->num_irqs);
208         return;
209     }
210 
211     argsz = sizeof(*irq_info);
212     irq_info = g_malloc0(argsz);
213     irq_info->index = VFIO_CCW_IO_IRQ_INDEX;
214     irq_info->argsz = argsz;
215     if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO,
216               irq_info) < 0 || irq_info->count < 1) {
217         error_setg_errno(errp, errno, "vfio: Error getting irq info");
218         goto out_free_info;
219     }
220 
221     if (event_notifier_init(&vcdev->io_notifier, 0)) {
222         error_setg_errno(errp, errno,
223                          "vfio: Unable to init event notifier for IO");
224         goto out_free_info;
225     }
226 
227     argsz = sizeof(*irq_set) + sizeof(*pfd);
228     irq_set = g_malloc0(argsz);
229     irq_set->argsz = argsz;
230     irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
231                      VFIO_IRQ_SET_ACTION_TRIGGER;
232     irq_set->index = VFIO_CCW_IO_IRQ_INDEX;
233     irq_set->start = 0;
234     irq_set->count = 1;
235     pfd = (int32_t *) &irq_set->data;
236 
237     *pfd = event_notifier_get_fd(&vcdev->io_notifier);
238     qemu_set_fd_handler(*pfd, vfio_ccw_io_notifier_handler, NULL, vcdev);
239     if (ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
240         error_setg(errp, "vfio: Failed to set up io notification");
241         qemu_set_fd_handler(*pfd, NULL, NULL, vcdev);
242         event_notifier_cleanup(&vcdev->io_notifier);
243     }
244 
245     g_free(irq_set);
246 
247 out_free_info:
248     g_free(irq_info);
249 }
250 
251 static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice *vcdev)
252 {
253     struct vfio_irq_set *irq_set;
254     size_t argsz;
255     int32_t *pfd;
256 
257     argsz = sizeof(*irq_set) + sizeof(*pfd);
258     irq_set = g_malloc0(argsz);
259     irq_set->argsz = argsz;
260     irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
261                      VFIO_IRQ_SET_ACTION_TRIGGER;
262     irq_set->index = VFIO_CCW_IO_IRQ_INDEX;
263     irq_set->start = 0;
264     irq_set->count = 1;
265     pfd = (int32_t *) &irq_set->data;
266     *pfd = -1;
267 
268     if (ioctl(vcdev->vdev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
269         error_report("vfio: Failed to de-assign device io fd: %m");
270     }
271 
272     qemu_set_fd_handler(event_notifier_get_fd(&vcdev->io_notifier),
273                         NULL, NULL, vcdev);
274     event_notifier_cleanup(&vcdev->io_notifier);
275 
276     g_free(irq_set);
277 }
278 
279 static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp)
280 {
281     VFIODevice *vdev = &vcdev->vdev;
282     struct vfio_region_info *info;
283     int ret;
284 
285     /* Sanity check device */
286     if (!(vdev->flags & VFIO_DEVICE_FLAGS_CCW)) {
287         error_setg(errp, "vfio: Um, this isn't a vfio-ccw device");
288         return;
289     }
290 
291     if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) {
292         error_setg(errp, "vfio: Unexpected number of the I/O region %u",
293                    vdev->num_regions);
294         return;
295     }
296 
297     ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info);
298     if (ret) {
299         error_setg_errno(errp, -ret, "vfio: Error getting config info");
300         return;
301     }
302 
303     vcdev->io_region_size = info->size;
304     if (sizeof(*vcdev->io_region) != vcdev->io_region_size) {
305         error_setg(errp, "vfio: Unexpected size of the I/O region");
306         g_free(info);
307         return;
308     }
309 
310     vcdev->io_region_offset = info->offset;
311     vcdev->io_region = g_malloc0(info->size);
312 
313     g_free(info);
314 }
315 
316 static void vfio_ccw_put_region(VFIOCCWDevice *vcdev)
317 {
318     g_free(vcdev->io_region);
319 }
320 
321 static void vfio_ccw_put_device(VFIOCCWDevice *vcdev)
322 {
323     g_free(vcdev->vdev.name);
324     vfio_put_base_device(&vcdev->vdev);
325 }
326 
327 static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
328                                 Error **errp)
329 {
330     char *name = g_strdup_printf("%x.%x.%04x", vcdev->cdev.hostid.cssid,
331                                  vcdev->cdev.hostid.ssid,
332                                  vcdev->cdev.hostid.devid);
333     VFIODevice *vbasedev;
334 
335     QLIST_FOREACH(vbasedev, &group->device_list, next) {
336         if (strcmp(vbasedev->name, name) == 0) {
337             error_setg(errp, "vfio: subchannel %s has already been attached",
338                        name);
339             goto out_err;
340         }
341     }
342 
343     /*
344      * All vfio-ccw devices are believed to operate in a way compatible with
345      * memory ballooning, ie. pages pinned in the host are in the current
346      * working set of the guest driver and therefore never overlap with pages
347      * available to the guest balloon driver.  This needs to be set before
348      * vfio_get_device() for vfio common to handle the balloon inhibitor.
349      */
350     vcdev->vdev.balloon_allowed = true;
351 
352     if (vfio_get_device(group, vcdev->cdev.mdevid, &vcdev->vdev, errp)) {
353         goto out_err;
354     }
355 
356     vcdev->vdev.ops = &vfio_ccw_ops;
357     vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW;
358     vcdev->vdev.name = name;
359     vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj;
360 
361     return;
362 
363 out_err:
364     g_free(name);
365 }
366 
367 static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp)
368 {
369     char *tmp, group_path[PATH_MAX];
370     ssize_t len;
371     int groupid;
372 
373     tmp = g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group",
374                           cdev->hostid.cssid, cdev->hostid.ssid,
375                           cdev->hostid.devid, cdev->mdevid);
376     len = readlink(tmp, group_path, sizeof(group_path));
377     g_free(tmp);
378 
379     if (len <= 0 || len >= sizeof(group_path)) {
380         error_setg(errp, "vfio: no iommu_group found");
381         return NULL;
382     }
383 
384     group_path[len] = 0;
385 
386     if (sscanf(basename(group_path), "%d", &groupid) != 1) {
387         error_setg(errp, "vfio: failed to read %s", group_path);
388         return NULL;
389     }
390 
391     return vfio_get_group(groupid, &address_space_memory, errp);
392 }
393 
394 static void vfio_ccw_realize(DeviceState *dev, Error **errp)
395 {
396     VFIOGroup *group;
397     CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
398     S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
399     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
400     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
401     Error *err = NULL;
402 
403     /* Call the class init function for subchannel. */
404     if (cdc->realize) {
405         cdc->realize(cdev, vcdev->vdev.sysfsdev, &err);
406         if (err) {
407             goto out_err_propagate;
408         }
409     }
410 
411     group = vfio_ccw_get_group(cdev, &err);
412     if (!group) {
413         goto out_group_err;
414     }
415 
416     vfio_ccw_get_device(group, vcdev, &err);
417     if (err) {
418         goto out_device_err;
419     }
420 
421     vfio_ccw_get_region(vcdev, &err);
422     if (err) {
423         goto out_region_err;
424     }
425 
426     vfio_ccw_register_io_notifier(vcdev, &err);
427     if (err) {
428         goto out_notifier_err;
429     }
430 
431     return;
432 
433 out_notifier_err:
434     vfio_ccw_put_region(vcdev);
435 out_region_err:
436     vfio_ccw_put_device(vcdev);
437 out_device_err:
438     vfio_put_group(group);
439 out_group_err:
440     if (cdc->unrealize) {
441         cdc->unrealize(cdev, NULL);
442     }
443 out_err_propagate:
444     error_propagate(errp, err);
445 }
446 
447 static void vfio_ccw_unrealize(DeviceState *dev, Error **errp)
448 {
449     CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
450     S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
451     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
452     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
453     VFIOGroup *group = vcdev->vdev.group;
454 
455     vfio_ccw_unregister_io_notifier(vcdev);
456     vfio_ccw_put_region(vcdev);
457     vfio_ccw_put_device(vcdev);
458     vfio_put_group(group);
459 
460     if (cdc->unrealize) {
461         cdc->unrealize(cdev, errp);
462     }
463 }
464 
465 static Property vfio_ccw_properties[] = {
466     DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev),
467     DEFINE_PROP_BOOL("force-orb-pfch", VFIOCCWDevice, force_orb_pfch, false),
468     DEFINE_PROP_END_OF_LIST(),
469 };
470 
471 static const VMStateDescription vfio_ccw_vmstate = {
472     .name = "vfio-ccw",
473     .unmigratable = 1,
474 };
475 
476 static void vfio_ccw_class_init(ObjectClass *klass, void *data)
477 {
478     DeviceClass *dc = DEVICE_CLASS(klass);
479     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
480 
481     dc->props = vfio_ccw_properties;
482     dc->vmsd = &vfio_ccw_vmstate;
483     dc->desc = "VFIO-based subchannel assignment";
484     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
485     dc->realize = vfio_ccw_realize;
486     dc->unrealize = vfio_ccw_unrealize;
487     dc->reset = vfio_ccw_reset;
488 
489     cdc->handle_request = vfio_ccw_handle_request;
490 }
491 
492 static const TypeInfo vfio_ccw_info = {
493     .name = TYPE_VFIO_CCW,
494     .parent = TYPE_S390_CCW,
495     .instance_size = sizeof(VFIOCCWDevice),
496     .class_init = vfio_ccw_class_init,
497 };
498 
499 static void register_vfio_ccw_type(void)
500 {
501     type_register_static(&vfio_ccw_info);
502 }
503 
504 type_init(register_vfio_ccw_type)
505