2 * s390 CCW Assignment Support
4 * Copyright 2017 IBM Corp
9 * This work is licensed under the terms of the GNU GPL, version 2
10 * or (at your option) any later version. See the COPYING file in the
11 * top-level directory.
14 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "hw/sysbus.h"
18 #include "hw/s390x/css.h"
19 #include "hw/s390x/css-bridge.h"
20 #include "hw/s390x/s390-ccw.h"
22 IOInstEnding s390_ccw_cmd_request(SubchDev *sch)
24 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(sch->driver_data);
26 if (!cdc->handle_request) {
27 return IOINST_CC_STATUS_PRESENT;
29 return cdc->handle_request(sch);
32 static void s390_ccw_get_dev_info(S390CCWDevice *cdev,
36 unsigned int cssid, ssid, devid;
37 char dev_path[PATH_MAX] = {0}, *tmp;
40 error_setg(errp, "No host device provided");
41 error_append_hint(errp,
42 "Use -device vfio-ccw,sysfsdev=PATH_TO_DEVICE\n");
46 if (!realpath(sysfsdev, dev_path)) {
47 error_setg_errno(errp, errno, "Host device '%s' not found", sysfsdev);
51 cdev->mdevid = g_strdup(basename(dev_path));
53 tmp = basename(dirname(dev_path));
54 if (sscanf(tmp, "%2x.%1x.%4x", &cssid, &ssid, &devid) != 3) {
55 error_setg_errno(errp, errno, "Failed to read %s", tmp);
59 cdev->hostid.cssid = cssid;
60 cdev->hostid.ssid = ssid;
61 cdev->hostid.devid = devid;
62 cdev->hostid.valid = true;
65 static void s390_ccw_realize(S390CCWDevice *cdev, char *sysfsdev, Error **errp)
67 CcwDevice *ccw_dev = CCW_DEVICE(cdev);
68 CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
69 DeviceState *parent = DEVICE(ccw_dev);
70 BusState *qbus = qdev_get_parent_bus(parent);
71 VirtualCssBus *cbus = VIRTUAL_CSS_BUS(qbus);
76 s390_ccw_get_dev_info(cdev, sysfsdev, &err);
78 goto out_err_propagate;
81 sch = css_create_sch(ccw_dev->devno, cbus->squash_mcss, &err);
85 sch->driver_data = cdev;
86 sch->do_subchannel_work = do_subchannel_work_passthrough;
89 ret = css_sch_build_schib(sch, &cdev->hostid);
91 error_setg_errno(&err, -ret, "%s: Failed to build initial schib",
96 ck->realize(ccw_dev, &err);
101 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
102 parent->hotplugged, 1);
106 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
110 g_free(cdev->mdevid);
112 error_propagate(errp, err);
115 static void s390_ccw_unrealize(S390CCWDevice *cdev, Error **errp)
117 CcwDevice *ccw_dev = CCW_DEVICE(cdev);
118 SubchDev *sch = ccw_dev->sch;
121 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
126 g_free(cdev->mdevid);
129 static void s390_ccw_class_init(ObjectClass *klass, void *data)
131 DeviceClass *dc = DEVICE_CLASS(klass);
132 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
134 dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
135 cdc->realize = s390_ccw_realize;
136 cdc->unrealize = s390_ccw_unrealize;
139 static const TypeInfo s390_ccw_info = {
140 .name = TYPE_S390_CCW,
141 .parent = TYPE_CCW_DEVICE,
142 .instance_size = sizeof(S390CCWDevice),
143 .class_size = sizeof(S390CCWDeviceClass),
144 .class_init = s390_ccw_class_init,
148 static void register_s390_ccw_type(void)
150 type_register_static(&s390_ccw_info);
153 type_init(register_s390_ccw_type)