1
2
3
4
5
6
7
8
9
10
11
12
13#include "qemu/osdep.h"
14#include "qapi/error.h"
15#include "hw/sysbus.h"
16#include "libgen.h"
17#include "hw/s390x/css.h"
18#include "hw/s390x/css-bridge.h"
19#include "hw/s390x/s390-ccw.h"
20
21IOInstEnding s390_ccw_cmd_request(SubchDev *sch)
22{
23 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(sch->driver_data);
24
25 if (!cdc->handle_request) {
26 return IOINST_CC_STATUS_PRESENT;
27 }
28 return cdc->handle_request(sch);
29}
30
31static void s390_ccw_get_dev_info(S390CCWDevice *cdev,
32 char *sysfsdev,
33 Error **errp)
34{
35 unsigned int cssid, ssid, devid;
36 char dev_path[PATH_MAX] = {0}, *tmp;
37
38 if (!sysfsdev) {
39 error_setg(errp, "No host device provided");
40 error_append_hint(errp,
41 "Use -device vfio-ccw,sysfsdev=PATH_TO_DEVICE\n");
42 return;
43 }
44
45 if (!realpath(sysfsdev, dev_path)) {
46 error_setg_errno(errp, errno, "Host device '%s' not found", sysfsdev);
47 return;
48 }
49
50 cdev->mdevid = g_strdup(basename(dev_path));
51
52 tmp = basename(dirname(dev_path));
53 if (sscanf(tmp, "%2x.%1x.%4x", &cssid, &ssid, &devid) != 3) {
54 error_setg_errno(errp, errno, "Failed to read %s", tmp);
55 return;
56 }
57
58 cdev->hostid.cssid = cssid;
59 cdev->hostid.ssid = ssid;
60 cdev->hostid.devid = devid;
61 cdev->hostid.valid = true;
62}
63
64static void s390_ccw_realize(S390CCWDevice *cdev, char *sysfsdev, Error **errp)
65{
66 CcwDevice *ccw_dev = CCW_DEVICE(cdev);
67 CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
68 DeviceState *parent = DEVICE(ccw_dev);
69 BusState *qbus = qdev_get_parent_bus(parent);
70 VirtualCssBus *cbus = VIRTUAL_CSS_BUS(qbus);
71 SubchDev *sch;
72 int ret;
73 Error *err = NULL;
74
75 s390_ccw_get_dev_info(cdev, sysfsdev, &err);
76 if (err) {
77 goto out_err_propagate;
78 }
79
80 sch = css_create_sch(ccw_dev->devno, false, cbus->squash_mcss, &err);
81 if (!sch) {
82 goto out_mdevid_free;
83 }
84 sch->driver_data = cdev;
85 sch->do_subchannel_work = do_subchannel_work_passthrough;
86
87 ccw_dev->sch = sch;
88 ret = css_sch_build_schib(sch, &cdev->hostid);
89 if (ret) {
90 error_setg_errno(&err, -ret, "%s: Failed to build initial schib",
91 __func__);
92 goto out_err;
93 }
94
95 ck->realize(ccw_dev, &err);
96 if (err) {
97 goto out_err;
98 }
99
100 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
101 parent->hotplugged, 1);
102 return;
103
104out_err:
105 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
106 ccw_dev->sch = NULL;
107 g_free(sch);
108out_mdevid_free:
109 g_free(cdev->mdevid);
110out_err_propagate:
111 error_propagate(errp, err);
112}
113
114static void s390_ccw_unrealize(S390CCWDevice *cdev, Error **errp)
115{
116 CcwDevice *ccw_dev = CCW_DEVICE(cdev);
117 SubchDev *sch = ccw_dev->sch;
118
119 if (sch) {
120 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
121 g_free(sch);
122 ccw_dev->sch = NULL;
123 }
124
125 g_free(cdev->mdevid);
126}
127
128static void s390_ccw_class_init(ObjectClass *klass, void *data)
129{
130 DeviceClass *dc = DEVICE_CLASS(klass);
131 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
132
133 dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
134 cdc->realize = s390_ccw_realize;
135 cdc->unrealize = s390_ccw_unrealize;
136}
137
138static const TypeInfo s390_ccw_info = {
139 .name = TYPE_S390_CCW,
140 .parent = TYPE_CCW_DEVICE,
141 .instance_size = sizeof(S390CCWDevice),
142 .class_size = sizeof(S390CCWDeviceClass),
143 .class_init = s390_ccw_class_init,
144 .abstract = true,
145};
146
147static void register_s390_ccw_type(void)
148{
149 type_register_static(&s390_ccw_info);
150}
151
152type_init(register_s390_ccw_type)
153