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.
14 #include "qemu/osdep.h"
16 #include "qapi/error.h"
17 #include "qemu/module.h"
18 #include "hw/s390x/css.h"
19 #include "hw/s390x/css-bridge.h"
20 #include "hw/s390x/s390-ccw.h"
21 #include "sysemu/sysemu.h"
23 IOInstEnding
s390_ccw_cmd_request(SubchDev
*sch
)
25 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_GET_CLASS(sch
->driver_data
);
27 if (!cdc
->handle_request
) {
28 return IOINST_CC_STATUS_PRESENT
;
30 return cdc
->handle_request(sch
);
33 int s390_ccw_halt(SubchDev
*sch
)
35 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_GET_CLASS(sch
->driver_data
);
37 if (!cdc
->handle_halt
) {
40 return cdc
->handle_halt(sch
);
43 int s390_ccw_clear(SubchDev
*sch
)
45 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_GET_CLASS(sch
->driver_data
);
47 if (!cdc
->handle_clear
) {
50 return cdc
->handle_clear(sch
);
53 IOInstEnding
s390_ccw_store(SubchDev
*sch
)
55 S390CCWDeviceClass
*cdc
= NULL
;
56 int ret
= IOINST_CC_EXPECTED
;
59 * This code is called for both virtual and passthrough devices,
60 * but only applies to the latter. This ugly check makes that
63 if (object_dynamic_cast(OBJECT(sch
->driver_data
), TYPE_S390_CCW
)) {
64 cdc
= S390_CCW_DEVICE_GET_CLASS(sch
->driver_data
);
67 if (cdc
&& cdc
->handle_store
) {
68 ret
= cdc
->handle_store(sch
);
74 static void s390_ccw_get_dev_info(S390CCWDevice
*cdev
,
78 unsigned int cssid
, ssid
, devid
;
79 char dev_path
[PATH_MAX
] = {0};
80 g_autofree
char *tmp_dir
= NULL
;
81 g_autofree
char *tmp
= NULL
;
84 error_setg(errp
, "No host device provided");
85 error_append_hint(errp
,
86 "Use -device vfio-ccw,sysfsdev=PATH_TO_DEVICE\n");
90 if (!realpath(sysfsdev
, dev_path
)) {
91 error_setg_errno(errp
, errno
, "Host device '%s' not found", sysfsdev
);
95 cdev
->mdevid
= g_path_get_basename(dev_path
);
97 tmp_dir
= g_path_get_dirname(dev_path
);
98 tmp
= g_path_get_basename(tmp_dir
);
99 if (sscanf(tmp
, "%2x.%1x.%4x", &cssid
, &ssid
, &devid
) != 3) {
100 error_setg_errno(errp
, errno
, "Failed to read %s", tmp
);
104 cdev
->hostid
.cssid
= cssid
;
105 cdev
->hostid
.ssid
= ssid
;
106 cdev
->hostid
.devid
= devid
;
107 cdev
->hostid
.valid
= true;
110 static void s390_ccw_realize(S390CCWDevice
*cdev
, char *sysfsdev
, Error
**errp
)
112 CcwDevice
*ccw_dev
= CCW_DEVICE(cdev
);
113 CCWDeviceClass
*ck
= CCW_DEVICE_GET_CLASS(ccw_dev
);
114 DeviceState
*parent
= DEVICE(ccw_dev
);
119 s390_ccw_get_dev_info(cdev
, sysfsdev
, &err
);
121 goto out_err_propagate
;
124 sch
= css_create_sch(ccw_dev
->devno
, &err
);
126 goto out_mdevid_free
;
128 sch
->driver_data
= cdev
;
129 sch
->do_subchannel_work
= do_subchannel_work_passthrough
;
130 sch
->irb_cb
= build_irb_passthrough
;
133 ret
= css_sch_build_schib(sch
, &cdev
->hostid
);
135 error_setg_errno(&err
, -ret
, "%s: Failed to build initial schib",
140 ck
->realize(ccw_dev
, &err
);
145 css_generate_sch_crws(sch
->cssid
, sch
->ssid
, sch
->schid
,
146 parent
->hotplugged
, 1);
150 css_subch_assign(sch
->cssid
, sch
->ssid
, sch
->schid
, sch
->devno
, NULL
);
154 g_free(cdev
->mdevid
);
156 error_propagate(errp
, err
);
159 static void s390_ccw_unrealize(S390CCWDevice
*cdev
)
161 CcwDevice
*ccw_dev
= CCW_DEVICE(cdev
);
162 SubchDev
*sch
= ccw_dev
->sch
;
165 css_subch_assign(sch
->cssid
, sch
->ssid
, sch
->schid
, sch
->devno
, NULL
);
170 g_free(cdev
->mdevid
);
173 static void s390_ccw_instance_init(Object
*obj
)
175 S390CCWDevice
*dev
= S390_CCW_DEVICE(obj
);
177 device_add_bootindex_property(obj
, &dev
->bootindex
, "bootindex",
178 "/disk@0,0", DEVICE(obj
));
181 static void s390_ccw_class_init(ObjectClass
*klass
, void *data
)
183 S390CCWDeviceClass
*cdc
= S390_CCW_DEVICE_CLASS(klass
);
185 cdc
->realize
= s390_ccw_realize
;
186 cdc
->unrealize
= s390_ccw_unrealize
;
189 static const TypeInfo s390_ccw_info
= {
190 .name
= TYPE_S390_CCW
,
191 .parent
= TYPE_CCW_DEVICE
,
192 .instance_init
= s390_ccw_instance_init
,
193 .instance_size
= sizeof(S390CCWDevice
),
194 .class_size
= sizeof(S390CCWDeviceClass
),
195 .class_init
= s390_ccw_class_init
,
199 static void register_s390_ccw_type(void)
201 type_register_static(&s390_ccw_info
);
204 type_init(register_s390_ccw_type
)