meson: convert hw/vfio
[qemu.git] / hw / s390x / s390-ccw.c
1 /*
2 * s390 CCW 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
10 * or (at your option) any later version. See the COPYING file in the
11 * top-level directory.
12 */
13
14 #include "qemu/osdep.h"
15 #include <libgen.h>
16 #include "qapi/error.h"
17 #include "qemu/module.h"
18 #include "hw/sysbus.h"
19 #include "hw/s390x/css.h"
20 #include "hw/s390x/css-bridge.h"
21 #include "hw/s390x/s390-ccw.h"
22 #include "sysemu/sysemu.h"
23
24 IOInstEnding s390_ccw_cmd_request(SubchDev *sch)
25 {
26 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(sch->driver_data);
27
28 if (!cdc->handle_request) {
29 return IOINST_CC_STATUS_PRESENT;
30 }
31 return cdc->handle_request(sch);
32 }
33
34 int s390_ccw_halt(SubchDev *sch)
35 {
36 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(sch->driver_data);
37
38 if (!cdc->handle_halt) {
39 return -ENOSYS;
40 }
41 return cdc->handle_halt(sch);
42 }
43
44 int s390_ccw_clear(SubchDev *sch)
45 {
46 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(sch->driver_data);
47
48 if (!cdc->handle_clear) {
49 return -ENOSYS;
50 }
51 return cdc->handle_clear(sch);
52 }
53
54 IOInstEnding s390_ccw_store(SubchDev *sch)
55 {
56 S390CCWDeviceClass *cdc = NULL;
57 int ret = IOINST_CC_EXPECTED;
58
59 /*
60 * This code is called for both virtual and passthrough devices,
61 * but only applies to to the latter. This ugly check makes that
62 * distinction for us.
63 */
64 if (object_dynamic_cast(OBJECT(sch->driver_data), TYPE_S390_CCW)) {
65 cdc = S390_CCW_DEVICE_GET_CLASS(sch->driver_data);
66 }
67
68 if (cdc && cdc->handle_store) {
69 ret = cdc->handle_store(sch);
70 }
71
72 return ret;
73 }
74
75 static void s390_ccw_get_dev_info(S390CCWDevice *cdev,
76 char *sysfsdev,
77 Error **errp)
78 {
79 unsigned int cssid, ssid, devid;
80 char dev_path[PATH_MAX] = {0}, *tmp;
81
82 if (!sysfsdev) {
83 error_setg(errp, "No host device provided");
84 error_append_hint(errp,
85 "Use -device vfio-ccw,sysfsdev=PATH_TO_DEVICE\n");
86 return;
87 }
88
89 if (!realpath(sysfsdev, dev_path)) {
90 error_setg_errno(errp, errno, "Host device '%s' not found", sysfsdev);
91 return;
92 }
93
94 cdev->mdevid = g_path_get_basename(dev_path);
95
96 tmp = basename(dirname(dev_path));
97 if (sscanf(tmp, "%2x.%1x.%4x", &cssid, &ssid, &devid) != 3) {
98 error_setg_errno(errp, errno, "Failed to read %s", tmp);
99 return;
100 }
101
102 cdev->hostid.cssid = cssid;
103 cdev->hostid.ssid = ssid;
104 cdev->hostid.devid = devid;
105 cdev->hostid.valid = true;
106 }
107
108 static void s390_ccw_realize(S390CCWDevice *cdev, char *sysfsdev, Error **errp)
109 {
110 CcwDevice *ccw_dev = CCW_DEVICE(cdev);
111 CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
112 DeviceState *parent = DEVICE(ccw_dev);
113 SubchDev *sch;
114 int ret;
115 Error *err = NULL;
116
117 s390_ccw_get_dev_info(cdev, sysfsdev, &err);
118 if (err) {
119 goto out_err_propagate;
120 }
121
122 sch = css_create_sch(ccw_dev->devno, &err);
123 if (!sch) {
124 goto out_mdevid_free;
125 }
126 sch->driver_data = cdev;
127 sch->do_subchannel_work = do_subchannel_work_passthrough;
128
129 ccw_dev->sch = sch;
130 ret = css_sch_build_schib(sch, &cdev->hostid);
131 if (ret) {
132 error_setg_errno(&err, -ret, "%s: Failed to build initial schib",
133 __func__);
134 goto out_err;
135 }
136
137 ck->realize(ccw_dev, &err);
138 if (err) {
139 goto out_err;
140 }
141
142 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
143 parent->hotplugged, 1);
144 return;
145
146 out_err:
147 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
148 ccw_dev->sch = NULL;
149 g_free(sch);
150 out_mdevid_free:
151 g_free(cdev->mdevid);
152 out_err_propagate:
153 error_propagate(errp, err);
154 }
155
156 static void s390_ccw_unrealize(S390CCWDevice *cdev)
157 {
158 CcwDevice *ccw_dev = CCW_DEVICE(cdev);
159 SubchDev *sch = ccw_dev->sch;
160
161 if (sch) {
162 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
163 g_free(sch);
164 ccw_dev->sch = NULL;
165 }
166
167 g_free(cdev->mdevid);
168 }
169
170 static void s390_ccw_instance_init(Object *obj)
171 {
172 S390CCWDevice *dev = S390_CCW_DEVICE(obj);
173
174 device_add_bootindex_property(obj, &dev->bootindex, "bootindex",
175 "/disk@0,0", DEVICE(obj));
176 }
177
178 static void s390_ccw_class_init(ObjectClass *klass, void *data)
179 {
180 DeviceClass *dc = DEVICE_CLASS(klass);
181 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
182
183 dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
184 cdc->realize = s390_ccw_realize;
185 cdc->unrealize = s390_ccw_unrealize;
186 }
187
188 static const TypeInfo s390_ccw_info = {
189 .name = TYPE_S390_CCW,
190 .parent = TYPE_CCW_DEVICE,
191 .instance_init = s390_ccw_instance_init,
192 .instance_size = sizeof(S390CCWDevice),
193 .class_size = sizeof(S390CCWDeviceClass),
194 .class_init = s390_ccw_class_init,
195 .abstract = true,
196 };
197
198 static void register_s390_ccw_type(void)
199 {
200 type_register_static(&s390_ccw_info);
201 }
202
203 type_init(register_s390_ccw_type)