xref: /openbmc/qemu/hw/s390x/s390-ccw.c (revision 06d4c71f)
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 static void s390_ccw_get_dev_info(S390CCWDevice *cdev,
55                                   char *sysfsdev,
56                                   Error **errp)
57 {
58     unsigned int cssid, ssid, devid;
59     char dev_path[PATH_MAX] = {0}, *tmp;
60 
61     if (!sysfsdev) {
62         error_setg(errp, "No host device provided");
63         error_append_hint(errp,
64                           "Use -device vfio-ccw,sysfsdev=PATH_TO_DEVICE\n");
65         return;
66     }
67 
68     if (!realpath(sysfsdev, dev_path)) {
69         error_setg_errno(errp, errno, "Host device '%s' not found", sysfsdev);
70         return;
71     }
72 
73     cdev->mdevid = g_path_get_basename(dev_path);
74 
75     tmp = basename(dirname(dev_path));
76     if (sscanf(tmp, "%2x.%1x.%4x", &cssid, &ssid, &devid) != 3) {
77         error_setg_errno(errp, errno, "Failed to read %s", tmp);
78         return;
79     }
80 
81     cdev->hostid.cssid = cssid;
82     cdev->hostid.ssid = ssid;
83     cdev->hostid.devid = devid;
84     cdev->hostid.valid = true;
85 }
86 
87 static void s390_ccw_realize(S390CCWDevice *cdev, char *sysfsdev, Error **errp)
88 {
89     CcwDevice *ccw_dev = CCW_DEVICE(cdev);
90     CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
91     DeviceState *parent = DEVICE(ccw_dev);
92     SubchDev *sch;
93     int ret;
94     Error *err = NULL;
95 
96     s390_ccw_get_dev_info(cdev, sysfsdev, &err);
97     if (err) {
98         goto out_err_propagate;
99     }
100 
101     sch = css_create_sch(ccw_dev->devno, &err);
102     if (!sch) {
103         goto out_mdevid_free;
104     }
105     sch->driver_data = cdev;
106     sch->do_subchannel_work = do_subchannel_work_passthrough;
107 
108     ccw_dev->sch = sch;
109     ret = css_sch_build_schib(sch, &cdev->hostid);
110     if (ret) {
111         error_setg_errno(&err, -ret, "%s: Failed to build initial schib",
112                          __func__);
113         goto out_err;
114     }
115 
116     ck->realize(ccw_dev, &err);
117     if (err) {
118         goto out_err;
119     }
120 
121     css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
122                           parent->hotplugged, 1);
123     return;
124 
125 out_err:
126     css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
127     ccw_dev->sch = NULL;
128     g_free(sch);
129 out_mdevid_free:
130     g_free(cdev->mdevid);
131 out_err_propagate:
132     error_propagate(errp, err);
133 }
134 
135 static void s390_ccw_unrealize(S390CCWDevice *cdev)
136 {
137     CcwDevice *ccw_dev = CCW_DEVICE(cdev);
138     SubchDev *sch = ccw_dev->sch;
139 
140     if (sch) {
141         css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
142         g_free(sch);
143         ccw_dev->sch = NULL;
144     }
145 
146     g_free(cdev->mdevid);
147 }
148 
149 static void s390_ccw_instance_init(Object *obj)
150 {
151     S390CCWDevice *dev = S390_CCW_DEVICE(obj);
152 
153     device_add_bootindex_property(obj, &dev->bootindex, "bootindex",
154                                   "/disk@0,0", DEVICE(obj));
155 }
156 
157 static void s390_ccw_class_init(ObjectClass *klass, void *data)
158 {
159     DeviceClass *dc = DEVICE_CLASS(klass);
160     S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
161 
162     dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
163     cdc->realize = s390_ccw_realize;
164     cdc->unrealize = s390_ccw_unrealize;
165 }
166 
167 static const TypeInfo s390_ccw_info = {
168     .name          = TYPE_S390_CCW,
169     .parent        = TYPE_CCW_DEVICE,
170     .instance_init = s390_ccw_instance_init,
171     .instance_size = sizeof(S390CCWDevice),
172     .class_size    = sizeof(S390CCWDeviceClass),
173     .class_init    = s390_ccw_class_init,
174     .abstract      = true,
175 };
176 
177 static void register_s390_ccw_type(void)
178 {
179     type_register_static(&s390_ccw_info);
180 }
181 
182 type_init(register_s390_ccw_type)
183