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