xref: /qemu/hw/vfio/ccw.c (revision dc03272d)
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 "qemu/error-report.h"
26 
27 #define TYPE_VFIO_CCW "vfio-ccw"
28 typedef struct VFIOCCWDevice {
29     S390CCWDevice cdev;
30     VFIODevice vdev;
31     uint64_t io_region_size;
32     uint64_t io_region_offset;
33     struct ccw_io_region *io_region;
34     EventNotifier io_notifier;
35 } VFIOCCWDevice;
36 
37 static void vfio_ccw_compute_needs_reset(VFIODevice *vdev)
38 {
39     vdev->needs_reset = false;
40 }
41 
42 /*
43  * We don't need vfio_hot_reset_multi and vfio_eoi operations for
44  * vfio_ccw device now.
45  */
46 struct VFIODeviceOps vfio_ccw_ops = {
47     .vfio_compute_needs_reset = vfio_ccw_compute_needs_reset,
48 };
49 
50 static IOInstEnding vfio_ccw_handle_request(SubchDev *sch)
51 {
52     S390CCWDevice *cdev = sch->driver_data;
53     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
54     struct ccw_io_region *region = vcdev->io_region;
55     int ret;
56 
57     QEMU_BUILD_BUG_ON(sizeof(region->orb_area) != sizeof(ORB));
58     QEMU_BUILD_BUG_ON(sizeof(region->scsw_area) != sizeof(SCSW));
59     QEMU_BUILD_BUG_ON(sizeof(region->irb_area) != sizeof(IRB));
60 
61     memset(region, 0, sizeof(*region));
62 
63     memcpy(region->orb_area, &sch->orb, sizeof(ORB));
64     memcpy(region->scsw_area, &sch->curr_status.scsw, sizeof(SCSW));
65 
66 again:
67     ret = pwrite(vcdev->vdev.fd, region,
68                  vcdev->io_region_size, vcdev->io_region_offset);
69     if (ret != vcdev->io_region_size) {
70         if (errno == EAGAIN) {
71             goto again;
72         }
73         error_report("vfio-ccw: wirte I/O region failed with errno=%d", errno);
74         ret = -errno;
75     } else {
76         ret = region->ret_code;
77     }
78     switch (ret) {
79     case 0:
80         return IOINST_CC_EXPECTED;
81     case -EBUSY:
82         return IOINST_CC_BUSY;
83     case -ENODEV:
84     case -EACCES:
85         return IOINST_CC_NOT_OPERATIONAL;
86     case -EFAULT:
87     default:
88         sch_gen_unit_exception(sch);
89         css_inject_io_interrupt(sch);
90         return IOINST_CC_EXPECTED;
91     }
92 }
93 
94 static void vfio_ccw_reset(DeviceState *dev)
95 {
96     CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
97     S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
98     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
99 
100     ioctl(vcdev->vdev.fd, VFIO_DEVICE_RESET);
101 }
102 
103 static void vfio_ccw_io_notifier_handler(void *opaque)
104 {
105     VFIOCCWDevice *vcdev = opaque;
106     struct ccw_io_region *region = vcdev->io_region;
107     S390CCWDevice *cdev = S390_CCW_DEVICE(vcdev);
108     CcwDevice *ccw_dev = CCW_DEVICE(cdev);
109     SubchDev *sch = ccw_dev->sch;
110     SCSW *s = &sch->curr_status.scsw;
111     PMCW *p = &sch->curr_status.pmcw;
112     IRB irb;
113     int size;
114 
115     if (!event_notifier_test_and_clear(&vcdev->io_notifier)) {
116         return;
117     }
118 
119     size = pread(vcdev->vdev.fd, region, vcdev->io_region_size,
120                  vcdev->io_region_offset);
121     if (size == -1) {
122         switch (errno) {
123         case ENODEV:
124             /* Generate a deferred cc 3 condition. */
125             s->flags |= SCSW_FLAGS_MASK_CC;
126             s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
127             s->ctrl |= (SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND);
128             goto read_err;
129         case EFAULT:
130             /* Memory problem, generate channel data check. */
131             s->ctrl &= ~SCSW_ACTL_START_PEND;
132             s->cstat = SCSW_CSTAT_DATA_CHECK;
133             s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
134             s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
135                        SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
136             goto read_err;
137         default:
138             /* Error, generate channel program check. */
139             s->ctrl &= ~SCSW_ACTL_START_PEND;
140             s->cstat = SCSW_CSTAT_PROG_CHECK;
141             s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
142             s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
143                        SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
144             goto read_err;
145         }
146     } else if (size != vcdev->io_region_size) {
147         /* Information transfer error, generate channel-control check. */
148         s->ctrl &= ~SCSW_ACTL_START_PEND;
149         s->cstat = SCSW_CSTAT_CHN_CTRL_CHK;
150         s->ctrl &= ~SCSW_CTRL_MASK_STCTL;
151         s->ctrl |= SCSW_STCTL_PRIMARY | SCSW_STCTL_SECONDARY |
152                    SCSW_STCTL_ALERT | SCSW_STCTL_STATUS_PEND;
153         goto read_err;
154     }
155 
156     memcpy(&irb, region->irb_area, sizeof(IRB));
157 
158     /* Update control block via irb. */
159     copy_scsw_to_guest(s, &irb.scsw);
160 
161     /* If a uint check is pending, copy sense data. */
162     if ((s->dstat & SCSW_DSTAT_UNIT_CHECK) &&
163         (p->chars & PMCW_CHARS_MASK_CSENSE)) {
164         memcpy(sch->sense_data, irb.ecw, sizeof(irb.ecw));
165     }
166 
167 read_err:
168     css_inject_io_interrupt(sch);
169 }
170 
171 static void vfio_ccw_register_io_notifier(VFIOCCWDevice *vcdev, Error **errp)
172 {
173     VFIODevice *vdev = &vcdev->vdev;
174     struct vfio_irq_info *irq_info;
175     struct vfio_irq_set *irq_set;
176     size_t argsz;
177     int32_t *pfd;
178 
179     if (vdev->num_irqs < VFIO_CCW_IO_IRQ_INDEX + 1) {
180         error_setg(errp, "vfio: unexpected number of io irqs %u",
181                    vdev->num_irqs);
182         return;
183     }
184 
185     argsz = sizeof(*irq_info);
186     irq_info = g_malloc0(argsz);
187     irq_info->index = VFIO_CCW_IO_IRQ_INDEX;
188     irq_info->argsz = argsz;
189     if (ioctl(vdev->fd, VFIO_DEVICE_GET_IRQ_INFO,
190               irq_info) < 0 || irq_info->count < 1) {
191         error_setg_errno(errp, errno, "vfio: Error getting irq info");
192         goto out_free_info;
193     }
194 
195     if (event_notifier_init(&vcdev->io_notifier, 0)) {
196         error_setg_errno(errp, errno,
197                          "vfio: Unable to init event notifier for IO");
198         goto out_free_info;
199     }
200 
201     argsz = sizeof(*irq_set) + sizeof(*pfd);
202     irq_set = g_malloc0(argsz);
203     irq_set->argsz = argsz;
204     irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
205                      VFIO_IRQ_SET_ACTION_TRIGGER;
206     irq_set->index = VFIO_CCW_IO_IRQ_INDEX;
207     irq_set->start = 0;
208     irq_set->count = 1;
209     pfd = (int32_t *) &irq_set->data;
210 
211     *pfd = event_notifier_get_fd(&vcdev->io_notifier);
212     qemu_set_fd_handler(*pfd, vfio_ccw_io_notifier_handler, NULL, vcdev);
213     if (ioctl(vdev->fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
214         error_setg(errp, "vfio: Failed to set up io notification");
215         qemu_set_fd_handler(*pfd, NULL, NULL, vcdev);
216         event_notifier_cleanup(&vcdev->io_notifier);
217     }
218 
219     g_free(irq_set);
220 
221 out_free_info:
222     g_free(irq_info);
223 }
224 
225 static void vfio_ccw_unregister_io_notifier(VFIOCCWDevice *vcdev)
226 {
227     struct vfio_irq_set *irq_set;
228     size_t argsz;
229     int32_t *pfd;
230 
231     argsz = sizeof(*irq_set) + sizeof(*pfd);
232     irq_set = g_malloc0(argsz);
233     irq_set->argsz = argsz;
234     irq_set->flags = VFIO_IRQ_SET_DATA_EVENTFD |
235                      VFIO_IRQ_SET_ACTION_TRIGGER;
236     irq_set->index = VFIO_CCW_IO_IRQ_INDEX;
237     irq_set->start = 0;
238     irq_set->count = 1;
239     pfd = (int32_t *) &irq_set->data;
240     *pfd = -1;
241 
242     if (ioctl(vcdev->vdev.fd, VFIO_DEVICE_SET_IRQS, irq_set)) {
243         error_report("vfio: Failed to de-assign device io fd: %m");
244     }
245 
246     qemu_set_fd_handler(event_notifier_get_fd(&vcdev->io_notifier),
247                         NULL, NULL, vcdev);
248     event_notifier_cleanup(&vcdev->io_notifier);
249 
250     g_free(irq_set);
251 }
252 
253 static void vfio_ccw_get_region(VFIOCCWDevice *vcdev, Error **errp)
254 {
255     VFIODevice *vdev = &vcdev->vdev;
256     struct vfio_region_info *info;
257     int ret;
258 
259     /* Sanity check device */
260     if (!(vdev->flags & VFIO_DEVICE_FLAGS_CCW)) {
261         error_setg(errp, "vfio: Um, this isn't a vfio-ccw device");
262         return;
263     }
264 
265     if (vdev->num_regions < VFIO_CCW_CONFIG_REGION_INDEX + 1) {
266         error_setg(errp, "vfio: Unexpected number of the I/O region %u",
267                    vdev->num_regions);
268         return;
269     }
270 
271     ret = vfio_get_region_info(vdev, VFIO_CCW_CONFIG_REGION_INDEX, &info);
272     if (ret) {
273         error_setg_errno(errp, -ret, "vfio: Error getting config info");
274         return;
275     }
276 
277     vcdev->io_region_size = info->size;
278     if (sizeof(*vcdev->io_region) != vcdev->io_region_size) {
279         error_setg(errp, "vfio: Unexpected size of the I/O region");
280         g_free(info);
281         return;
282     }
283 
284     vcdev->io_region_offset = info->offset;
285     vcdev->io_region = g_malloc0(info->size);
286 
287     g_free(info);
288 }
289 
290 static void vfio_ccw_put_region(VFIOCCWDevice *vcdev)
291 {
292     g_free(vcdev->io_region);
293 }
294 
295 static void vfio_ccw_put_device(VFIOCCWDevice *vcdev)
296 {
297     g_free(vcdev->vdev.name);
298     vfio_put_base_device(&vcdev->vdev);
299 }
300 
301 static void vfio_ccw_get_device(VFIOGroup *group, VFIOCCWDevice *vcdev,
302                                 Error **errp)
303 {
304     char *name = g_strdup_printf("%x.%x.%04x", vcdev->cdev.hostid.cssid,
305                                  vcdev->cdev.hostid.ssid,
306                                  vcdev->cdev.hostid.devid);
307     VFIODevice *vbasedev;
308 
309     QLIST_FOREACH(vbasedev, &group->device_list, next) {
310         if (strcmp(vbasedev->name, name) == 0) {
311             error_setg(errp, "vfio: subchannel %s has already been attached",
312                        name);
313             goto out_err;
314         }
315     }
316 
317     if (vfio_get_device(group, vcdev->cdev.mdevid, &vcdev->vdev, errp)) {
318         goto out_err;
319     }
320 
321     vcdev->vdev.ops = &vfio_ccw_ops;
322     vcdev->vdev.type = VFIO_DEVICE_TYPE_CCW;
323     vcdev->vdev.name = name;
324     vcdev->vdev.dev = &vcdev->cdev.parent_obj.parent_obj;
325 
326     return;
327 
328 out_err:
329     g_free(name);
330 }
331 
332 static VFIOGroup *vfio_ccw_get_group(S390CCWDevice *cdev, Error **errp)
333 {
334     char *tmp, group_path[PATH_MAX];
335     ssize_t len;
336     int groupid;
337 
338     tmp = g_strdup_printf("/sys/bus/css/devices/%x.%x.%04x/%s/iommu_group",
339                           cdev->hostid.cssid, cdev->hostid.ssid,
340                           cdev->hostid.devid, cdev->mdevid);
341     len = readlink(tmp, group_path, sizeof(group_path));
342     g_free(tmp);
343 
344     if (len <= 0 || len >= sizeof(group_path)) {
345         error_setg(errp, "vfio: no iommu_group found");
346         return NULL;
347     }
348 
349     group_path[len] = 0;
350 
351     if (sscanf(basename(group_path), "%d", &groupid) != 1) {
352         error_setg(errp, "vfio: failed to read %s", group_path);
353         return NULL;
354     }
355 
356     return vfio_get_group(groupid, &address_space_memory, errp);
357 }
358 
359 static void vfio_ccw_realize(DeviceState *dev, Error **errp)
360 {
361     VFIOGroup *group;
362     CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
363     S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
364     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
365     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
366     Error *err = NULL;
367 
368     /* Call the class init function for subchannel. */
369     if (cdc->realize) {
370         cdc->realize(cdev, vcdev->vdev.sysfsdev, &err);
371         if (err) {
372             goto out_err_propagate;
373         }
374     }
375 
376     group = vfio_ccw_get_group(cdev, &err);
377     if (!group) {
378         goto out_group_err;
379     }
380 
381     vfio_ccw_get_device(group, vcdev, &err);
382     if (err) {
383         goto out_device_err;
384     }
385 
386     vfio_ccw_get_region(vcdev, &err);
387     if (err) {
388         goto out_region_err;
389     }
390 
391     vfio_ccw_register_io_notifier(vcdev, &err);
392     if (err) {
393         goto out_notifier_err;
394     }
395 
396     return;
397 
398 out_notifier_err:
399     vfio_ccw_put_region(vcdev);
400 out_region_err:
401     vfio_ccw_put_device(vcdev);
402 out_device_err:
403     vfio_put_group(group);
404 out_group_err:
405     if (cdc->unrealize) {
406         cdc->unrealize(cdev, NULL);
407     }
408 out_err_propagate:
409     error_propagate(errp, err);
410 }
411 
412 static void vfio_ccw_unrealize(DeviceState *dev, Error **errp)
413 {
414     CcwDevice *ccw_dev = DO_UPCAST(CcwDevice, parent_obj, dev);
415     S390CCWDevice *cdev = DO_UPCAST(S390CCWDevice, parent_obj, ccw_dev);
416     VFIOCCWDevice *vcdev = DO_UPCAST(VFIOCCWDevice, cdev, cdev);
417     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(cdev);
418     VFIOGroup *group = vcdev->vdev.group;
419 
420     vfio_ccw_unregister_io_notifier(vcdev);
421     vfio_ccw_put_region(vcdev);
422     vfio_ccw_put_device(vcdev);
423     vfio_put_group(group);
424 
425     if (cdc->unrealize) {
426         cdc->unrealize(cdev, errp);
427     }
428 }
429 
430 static Property vfio_ccw_properties[] = {
431     DEFINE_PROP_STRING("sysfsdev", VFIOCCWDevice, vdev.sysfsdev),
432     DEFINE_PROP_END_OF_LIST(),
433 };
434 
435 static const VMStateDescription vfio_ccw_vmstate = {
436     .name = TYPE_VFIO_CCW,
437     .unmigratable = 1,
438 };
439 
440 static void vfio_ccw_class_init(ObjectClass *klass, void *data)
441 {
442     DeviceClass *dc = DEVICE_CLASS(klass);
443     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
444 
445     dc->props = vfio_ccw_properties;
446     dc->vmsd = &vfio_ccw_vmstate;
447     dc->desc = "VFIO-based subchannel assignment";
448     set_bit(DEVICE_CATEGORY_MISC, dc->categories);
449     dc->realize = vfio_ccw_realize;
450     dc->unrealize = vfio_ccw_unrealize;
451     dc->reset = vfio_ccw_reset;
452 
453     cdc->handle_request = vfio_ccw_handle_request;
454 }
455 
456 static const TypeInfo vfio_ccw_info = {
457     .name = TYPE_VFIO_CCW,
458     .parent = TYPE_S390_CCW,
459     .instance_size = sizeof(VFIOCCWDevice),
460     .class_init = vfio_ccw_class_init,
461 };
462 
463 static void register_vfio_ccw_type(void)
464 {
465     type_register_static(&vfio_ccw_info);
466 }
467 
468 type_init(register_vfio_ccw_type)
469