xref: /openbmc/qemu/hw/s390x/s390-ccw.c (revision 2ba60ec1)
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 #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 
21 IOInstEnding 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 
31 static 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 
64 static 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, 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 
104 out_err:
105     css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
106     ccw_dev->sch = NULL;
107     g_free(sch);
108 out_mdevid_free:
109     g_free(cdev->mdevid);
110 out_err_propagate:
111     error_propagate(errp, err);
112 }
113 
114 static 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 
128 static 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 
138 static 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 
147 static void register_s390_ccw_type(void)
148 {
149     type_register_static(&s390_ccw_info);
150 }
151 
152 type_init(register_s390_ccw_type)
153