Merge remote-tracking branch 'remotes/cody/tags/block-pull-request' into staging
[qemu/ar7.git] / hw / s390x / s390-ccw.c
blob8614dda6f8a6376959247dc763b70132cf5eadb3
1 /*
2 * s390 CCW Assignment Support
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>
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.
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"
21 int s390_ccw_cmd_request(ORB *orb, SCSW *scsw, void *data)
23 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_GET_CLASS(data);
25 if (cdc->handle_request) {
26 return cdc->handle_request(orb, scsw, data);
27 } else {
28 return -ENOSYS;
32 static void s390_ccw_get_dev_info(S390CCWDevice *cdev,
33 char *sysfsdev,
34 Error **errp)
36 unsigned int cssid, ssid, devid;
37 char dev_path[PATH_MAX] = {0}, *tmp;
39 if (!sysfsdev) {
40 error_setg(errp, "No host device provided");
41 error_append_hint(errp,
42 "Use -device vfio-ccw,sysfsdev=PATH_TO_DEVICE\n");
43 return;
46 if (!realpath(sysfsdev, dev_path)) {
47 error_setg_errno(errp, errno, "Host device '%s' not found", sysfsdev);
48 return;
51 cdev->mdevid = g_strdup(basename(dev_path));
53 tmp = basename(dirname(dev_path));
54 if (sscanf(tmp, "%2x.%1x.%4x", &cssid, &ssid, &devid) != 3) {
55 error_setg_errno(errp, errno, "Failed to read %s", tmp);
56 return;
59 cdev->hostid.cssid = cssid;
60 cdev->hostid.ssid = ssid;
61 cdev->hostid.devid = devid;
62 cdev->hostid.valid = true;
65 static void s390_ccw_realize(S390CCWDevice *cdev, char *sysfsdev, Error **errp)
67 CcwDevice *ccw_dev = CCW_DEVICE(cdev);
68 CCWDeviceClass *ck = CCW_DEVICE_GET_CLASS(ccw_dev);
69 DeviceState *parent = DEVICE(ccw_dev);
70 BusState *qbus = qdev_get_parent_bus(parent);
71 VirtualCssBus *cbus = VIRTUAL_CSS_BUS(qbus);
72 SubchDev *sch;
73 int ret;
74 Error *err = NULL;
76 s390_ccw_get_dev_info(cdev, sysfsdev, &err);
77 if (err) {
78 goto out_err_propagate;
81 sch = css_create_sch(ccw_dev->devno, false, cbus->squash_mcss, &err);
82 if (!sch) {
83 goto out_mdevid_free;
85 sch->driver_data = cdev;
86 sch->do_subchannel_work = do_subchannel_work_passthrough;
88 ccw_dev->sch = sch;
89 ret = css_sch_build_schib(sch, &cdev->hostid);
90 if (ret) {
91 error_setg_errno(&err, -ret, "%s: Failed to build initial schib",
92 __func__);
93 goto out_err;
96 ck->realize(ccw_dev, &err);
97 if (err) {
98 goto out_err;
101 css_generate_sch_crws(sch->cssid, sch->ssid, sch->schid,
102 parent->hotplugged, 1);
103 return;
105 out_err:
106 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
107 ccw_dev->sch = NULL;
108 g_free(sch);
109 out_mdevid_free:
110 g_free(cdev->mdevid);
111 out_err_propagate:
112 error_propagate(errp, err);
115 static void s390_ccw_unrealize(S390CCWDevice *cdev, Error **errp)
117 CcwDevice *ccw_dev = CCW_DEVICE(cdev);
118 SubchDev *sch = ccw_dev->sch;
120 if (sch) {
121 css_subch_assign(sch->cssid, sch->ssid, sch->schid, sch->devno, NULL);
122 g_free(sch);
123 ccw_dev->sch = NULL;
126 g_free(cdev->mdevid);
129 static void s390_ccw_class_init(ObjectClass *klass, void *data)
131 DeviceClass *dc = DEVICE_CLASS(klass);
132 S390CCWDeviceClass *cdc = S390_CCW_DEVICE_CLASS(klass);
134 dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
135 cdc->realize = s390_ccw_realize;
136 cdc->unrealize = s390_ccw_unrealize;
139 static const TypeInfo s390_ccw_info = {
140 .name = TYPE_S390_CCW,
141 .parent = TYPE_CCW_DEVICE,
142 .instance_size = sizeof(S390CCWDevice),
143 .class_size = sizeof(S390CCWDeviceClass),
144 .class_init = s390_ccw_class_init,
145 .abstract = true,
148 static void register_s390_ccw_type(void)
150 type_register_static(&s390_ccw_info);
153 type_init(register_s390_ccw_type)