xref: /openbmc/qemu/hw/s390x/ccw-device.c (revision e50a24ea)
1 /*
2  * Common device infrastructure for devices in the virtual css
3  *
4  * Copyright 2016 IBM Corp.
5  * Author(s): Jing Liu <liujbjl@linux.vnet.ibm.com>
6  *
7  * This work is licensed under the terms of the GNU GPL, version 2 or (at
8  * your option) any later version. See the COPYING file in the top-level
9  * directory.
10  */
11 
12 #include "qemu/osdep.h"
13 #include "ccw-device.h"
14 #include "hw/qdev-properties.h"
15 #include "qemu/module.h"
16 #include "ipl.h"
17 #include "qapi/visitor.h"
18 #include "qemu/ctype.h"
19 #include "qapi/error.h"
20 
21 static void ccw_device_refill_ids(CcwDevice *dev)
22 {
23     SubchDev *sch = dev->sch;
24 
25     assert(sch);
26 
27     dev->dev_id.cssid = sch->cssid;
28     dev->dev_id.ssid = sch->ssid;
29     dev->dev_id.devid = sch->devno;
30     dev->dev_id.valid = true;
31 
32     dev->subch_id.cssid = sch->cssid;
33     dev->subch_id.ssid = sch->ssid;
34     dev->subch_id.devid = sch->schid;
35     dev->subch_id.valid = true;
36 }
37 
38 static bool ccw_device_realize(CcwDevice *dev, Error **errp)
39 {
40     ccw_device_refill_ids(dev);
41     return true;
42 }
43 
44 static void ccw_device_get_loadparm(Object *obj, Visitor *v,
45                                  const char *name, void *opaque,
46                                  Error **errp)
47 {
48     CcwDevice *dev = CCW_DEVICE(obj);
49     char *str = g_strndup((char *) dev->loadparm, sizeof(dev->loadparm));
50 
51     visit_type_str(v, name, &str, errp);
52     g_free(str);
53 }
54 
55 static void ccw_device_set_loadparm(Object *obj, Visitor *v,
56                                  const char *name, void *opaque,
57                                  Error **errp)
58 {
59     CcwDevice *dev = CCW_DEVICE(obj);
60     char *val;
61     int index;
62 
63     index = object_property_get_int(obj, "bootindex", NULL);
64 
65     if (index < 0) {
66         error_setg(errp, "LOADPARM is only valid for boot devices!");
67     }
68 
69     if (!visit_type_str(v, name, &val, errp)) {
70         return;
71     }
72 
73     s390_ipl_fmt_loadparm(dev->loadparm, val, errp);
74 }
75 
76 const PropertyInfo ccw_loadparm = {
77     .name  = "ccw_loadparm",
78     .description = "Up to 8 chars in set of [A-Za-z0-9. ] to pass"
79             " to the guest loader/kernel",
80     .get = ccw_device_get_loadparm,
81     .set = ccw_device_set_loadparm,
82 };
83 
84 static Property ccw_device_properties[] = {
85     DEFINE_PROP_CSS_DEV_ID("devno", CcwDevice, devno),
86     DEFINE_PROP_CSS_DEV_ID_RO("dev_id", CcwDevice, dev_id),
87     DEFINE_PROP_CSS_DEV_ID_RO("subch_id", CcwDevice, subch_id),
88     DEFINE_PROP_END_OF_LIST(),
89 };
90 
91 static void ccw_device_reset_hold(Object *obj, ResetType type)
92 {
93     CcwDevice *ccw_dev = CCW_DEVICE(obj);
94 
95     css_reset_sch(ccw_dev->sch);
96 }
97 
98 static void ccw_device_class_init(ObjectClass *klass, void *data)
99 {
100     DeviceClass *dc = DEVICE_CLASS(klass);
101     CCWDeviceClass *k = CCW_DEVICE_CLASS(klass);
102     ResettableClass *rc = RESETTABLE_CLASS(klass);
103 
104     k->realize = ccw_device_realize;
105     k->refill_ids = ccw_device_refill_ids;
106     device_class_set_props(dc, ccw_device_properties);
107     rc->phases.hold = ccw_device_reset_hold;
108     dc->bus_type = TYPE_VIRTUAL_CSS_BUS;
109 }
110 
111 const VMStateDescription vmstate_ccw_dev = {
112     .name = "s390_ccw_dev",
113     .version_id = 1,
114     .minimum_version_id = 1,
115     .fields = (const VMStateField[]) {
116         VMSTATE_STRUCT_POINTER(sch, CcwDevice, vmstate_subch_dev, SubchDev),
117         VMSTATE_END_OF_LIST()
118     }
119 };
120 
121 static const TypeInfo ccw_device_info = {
122     .name = TYPE_CCW_DEVICE,
123     .parent = TYPE_DEVICE,
124     .instance_size = sizeof(CcwDevice),
125     .class_size = sizeof(CCWDeviceClass),
126     .class_init = ccw_device_class_init,
127     .abstract = true,
128 };
129 
130 static void ccw_device_register(void)
131 {
132     type_register_static(&ccw_device_info);
133 }
134 
135 type_init(ccw_device_register)
136