xref: /openbmc/qemu/hw/ipmi/ipmi.c (revision d341d9f3)
1 /*
2  * QEMU IPMI emulation
3  *
4  * Copyright (c) 2015 Corey Minyard, MontaVista Software, LLC
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a copy
7  * of this software and associated documentation files (the "Software"), to deal
8  * in the Software without restriction, including without limitation the rights
9  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10  * copies of the Software, and to permit persons to whom the Software is
11  * furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included in
14  * all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22  * THE SOFTWARE.
23  */
24 
25 #include "hw/hw.h"
26 #include "hw/ipmi/ipmi.h"
27 #include "sysemu/sysemu.h"
28 #include "qmp-commands.h"
29 #include "qom/object_interfaces.h"
30 #include "qapi/visitor.h"
31 
32 static int ipmi_do_hw_op(IPMIInterface *s, enum ipmi_op op, int checkonly)
33 {
34     switch (op) {
35     case IPMI_RESET_CHASSIS:
36         if (checkonly) {
37             return 0;
38         }
39         qemu_system_reset_request();
40         return 0;
41 
42     case IPMI_POWEROFF_CHASSIS:
43         if (checkonly) {
44             return 0;
45         }
46         qemu_system_powerdown_request();
47         return 0;
48 
49     case IPMI_SEND_NMI:
50         if (checkonly) {
51             return 0;
52         }
53         qemu_mutex_lock_iothread();
54         qmp_inject_nmi(NULL);
55         qemu_mutex_unlock_iothread();
56         return 0;
57 
58     case IPMI_POWERCYCLE_CHASSIS:
59     case IPMI_PULSE_DIAG_IRQ:
60     case IPMI_SHUTDOWN_VIA_ACPI_OVERTEMP:
61     case IPMI_POWERON_CHASSIS:
62     default:
63         return IPMI_CC_COMMAND_NOT_SUPPORTED;
64     }
65 }
66 
67 static void ipmi_interface_class_init(ObjectClass *class, void *data)
68 {
69     IPMIInterfaceClass *ik = IPMI_INTERFACE_CLASS(class);
70 
71     ik->do_hw_op = ipmi_do_hw_op;
72 }
73 
74 static TypeInfo ipmi_interface_type_info = {
75     .name = TYPE_IPMI_INTERFACE,
76     .parent = TYPE_INTERFACE,
77     .class_size = sizeof(IPMIInterfaceClass),
78     .class_init = ipmi_interface_class_init,
79 };
80 
81 static void isa_ipmi_bmc_check(Object *obj, const char *name,
82                                Object *val, Error **errp)
83 {
84     IPMIBmc *bmc = IPMI_BMC(val);
85 
86     if (bmc->intf)
87         error_setg(errp, "BMC object is already in use");
88 }
89 
90 void ipmi_bmc_find_and_link(Object *obj, Object **bmc)
91 {
92     object_property_add_link(obj, "bmc", TYPE_IPMI_BMC, bmc,
93                              isa_ipmi_bmc_check,
94                              OBJ_PROP_LINK_UNREF_ON_RELEASE,
95                              &error_abort);
96 }
97 
98 static Property ipmi_bmc_properties[] = {
99     DEFINE_PROP_UINT8("slave_addr",  IPMIBmc, slave_addr, 0x20),
100     DEFINE_PROP_END_OF_LIST(),
101 };
102 
103 static void bmc_class_init(ObjectClass *oc, void *data)
104 {
105     DeviceClass *dc = DEVICE_CLASS(oc);
106 
107     dc->props = ipmi_bmc_properties;
108 }
109 
110 static TypeInfo ipmi_bmc_type_info = {
111     .name = TYPE_IPMI_BMC,
112     .parent = TYPE_DEVICE,
113     .instance_size = sizeof(IPMIBmc),
114     .abstract = true,
115     .class_size = sizeof(IPMIBmcClass),
116     .class_init = bmc_class_init,
117 };
118 
119 static void ipmi_register_types(void)
120 {
121     type_register_static(&ipmi_interface_type_info);
122     type_register_static(&ipmi_bmc_type_info);
123 }
124 
125 type_init(ipmi_register_types)
126 
127 static IPMIFwInfo *ipmi_fw_info;
128 static unsigned int ipmi_fw_info_len;
129 
130 static uint32_t current_uuid = 1;
131 
132 void ipmi_add_fwinfo(IPMIFwInfo *info, Error **errp)
133 {
134     info->uuid = current_uuid++;
135     ipmi_fw_info = g_realloc(ipmi_fw_info,
136                              sizeof(*ipmi_fw_info) * (ipmi_fw_info_len + 1));
137     ipmi_fw_info[ipmi_fw_info_len] = *info;
138 }
139 
140 IPMIFwInfo *ipmi_first_fwinfo(void)
141 {
142     return ipmi_fw_info;
143 }
144 
145 IPMIFwInfo *ipmi_next_fwinfo(IPMIFwInfo *current)
146 {
147     current++;
148     if (current >= &ipmi_fw_info[ipmi_fw_info_len]) {
149         return NULL;
150     }
151     return current;
152 }
153