xref: /openbmc/qemu/hw/ipmi/ipmi.c (revision 0430891ce162b986c6e02a7729a942ecd2a32ca4)
123076bb3SCorey Minyard /*
223076bb3SCorey Minyard  * QEMU IPMI emulation
323076bb3SCorey Minyard  *
423076bb3SCorey Minyard  * Copyright (c) 2015 Corey Minyard, MontaVista Software, LLC
523076bb3SCorey Minyard  *
623076bb3SCorey Minyard  * Permission is hereby granted, free of charge, to any person obtaining a copy
723076bb3SCorey Minyard  * of this software and associated documentation files (the "Software"), to deal
823076bb3SCorey Minyard  * in the Software without restriction, including without limitation the rights
923076bb3SCorey Minyard  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
1023076bb3SCorey Minyard  * copies of the Software, and to permit persons to whom the Software is
1123076bb3SCorey Minyard  * furnished to do so, subject to the following conditions:
1223076bb3SCorey Minyard  *
1323076bb3SCorey Minyard  * The above copyright notice and this permission notice shall be included in
1423076bb3SCorey Minyard  * all copies or substantial portions of the Software.
1523076bb3SCorey Minyard  *
1623076bb3SCorey Minyard  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
1723076bb3SCorey Minyard  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
1823076bb3SCorey Minyard  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
1923076bb3SCorey Minyard  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
2023076bb3SCorey Minyard  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
2123076bb3SCorey Minyard  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
2223076bb3SCorey Minyard  * THE SOFTWARE.
2323076bb3SCorey Minyard  */
2423076bb3SCorey Minyard 
25*0430891cSPeter Maydell #include "qemu/osdep.h"
2623076bb3SCorey Minyard #include "hw/hw.h"
2723076bb3SCorey Minyard #include "hw/ipmi/ipmi.h"
2823076bb3SCorey Minyard #include "sysemu/sysemu.h"
2923076bb3SCorey Minyard #include "qmp-commands.h"
3023076bb3SCorey Minyard #include "qom/object_interfaces.h"
3123076bb3SCorey Minyard #include "qapi/visitor.h"
3223076bb3SCorey Minyard 
3323076bb3SCorey Minyard static int ipmi_do_hw_op(IPMIInterface *s, enum ipmi_op op, int checkonly)
3423076bb3SCorey Minyard {
3523076bb3SCorey Minyard     switch (op) {
3623076bb3SCorey Minyard     case IPMI_RESET_CHASSIS:
3723076bb3SCorey Minyard         if (checkonly) {
3823076bb3SCorey Minyard             return 0;
3923076bb3SCorey Minyard         }
4023076bb3SCorey Minyard         qemu_system_reset_request();
4123076bb3SCorey Minyard         return 0;
4223076bb3SCorey Minyard 
4323076bb3SCorey Minyard     case IPMI_POWEROFF_CHASSIS:
4423076bb3SCorey Minyard         if (checkonly) {
4523076bb3SCorey Minyard             return 0;
4623076bb3SCorey Minyard         }
4723076bb3SCorey Minyard         qemu_system_powerdown_request();
4823076bb3SCorey Minyard         return 0;
4923076bb3SCorey Minyard 
5023076bb3SCorey Minyard     case IPMI_SEND_NMI:
5123076bb3SCorey Minyard         if (checkonly) {
5223076bb3SCorey Minyard             return 0;
5323076bb3SCorey Minyard         }
5423076bb3SCorey Minyard         qemu_mutex_lock_iothread();
5523076bb3SCorey Minyard         qmp_inject_nmi(NULL);
5623076bb3SCorey Minyard         qemu_mutex_unlock_iothread();
5723076bb3SCorey Minyard         return 0;
5823076bb3SCorey Minyard 
5923076bb3SCorey Minyard     case IPMI_POWERCYCLE_CHASSIS:
6023076bb3SCorey Minyard     case IPMI_PULSE_DIAG_IRQ:
6123076bb3SCorey Minyard     case IPMI_SHUTDOWN_VIA_ACPI_OVERTEMP:
6223076bb3SCorey Minyard     case IPMI_POWERON_CHASSIS:
6323076bb3SCorey Minyard     default:
6423076bb3SCorey Minyard         return IPMI_CC_COMMAND_NOT_SUPPORTED;
6523076bb3SCorey Minyard     }
6623076bb3SCorey Minyard }
6723076bb3SCorey Minyard 
6823076bb3SCorey Minyard static void ipmi_interface_class_init(ObjectClass *class, void *data)
6923076bb3SCorey Minyard {
7023076bb3SCorey Minyard     IPMIInterfaceClass *ik = IPMI_INTERFACE_CLASS(class);
7123076bb3SCorey Minyard 
7223076bb3SCorey Minyard     ik->do_hw_op = ipmi_do_hw_op;
7323076bb3SCorey Minyard }
7423076bb3SCorey Minyard 
7523076bb3SCorey Minyard static TypeInfo ipmi_interface_type_info = {
7623076bb3SCorey Minyard     .name = TYPE_IPMI_INTERFACE,
7723076bb3SCorey Minyard     .parent = TYPE_INTERFACE,
7823076bb3SCorey Minyard     .class_size = sizeof(IPMIInterfaceClass),
7923076bb3SCorey Minyard     .class_init = ipmi_interface_class_init,
8023076bb3SCorey Minyard };
8123076bb3SCorey Minyard 
8223076bb3SCorey Minyard static void isa_ipmi_bmc_check(Object *obj, const char *name,
8323076bb3SCorey Minyard                                Object *val, Error **errp)
8423076bb3SCorey Minyard {
8523076bb3SCorey Minyard     IPMIBmc *bmc = IPMI_BMC(val);
8623076bb3SCorey Minyard 
8723076bb3SCorey Minyard     if (bmc->intf)
8823076bb3SCorey Minyard         error_setg(errp, "BMC object is already in use");
8923076bb3SCorey Minyard }
9023076bb3SCorey Minyard 
9123076bb3SCorey Minyard void ipmi_bmc_find_and_link(Object *obj, Object **bmc)
9223076bb3SCorey Minyard {
9323076bb3SCorey Minyard     object_property_add_link(obj, "bmc", TYPE_IPMI_BMC, bmc,
9423076bb3SCorey Minyard                              isa_ipmi_bmc_check,
9523076bb3SCorey Minyard                              OBJ_PROP_LINK_UNREF_ON_RELEASE,
9623076bb3SCorey Minyard                              &error_abort);
9723076bb3SCorey Minyard }
9823076bb3SCorey Minyard 
9923076bb3SCorey Minyard static Property ipmi_bmc_properties[] = {
10023076bb3SCorey Minyard     DEFINE_PROP_UINT8("slave_addr",  IPMIBmc, slave_addr, 0x20),
10123076bb3SCorey Minyard     DEFINE_PROP_END_OF_LIST(),
10223076bb3SCorey Minyard };
10323076bb3SCorey Minyard 
10423076bb3SCorey Minyard static void bmc_class_init(ObjectClass *oc, void *data)
10523076bb3SCorey Minyard {
10623076bb3SCorey Minyard     DeviceClass *dc = DEVICE_CLASS(oc);
10723076bb3SCorey Minyard 
10823076bb3SCorey Minyard     dc->props = ipmi_bmc_properties;
10923076bb3SCorey Minyard }
11023076bb3SCorey Minyard 
11123076bb3SCorey Minyard static TypeInfo ipmi_bmc_type_info = {
11223076bb3SCorey Minyard     .name = TYPE_IPMI_BMC,
11323076bb3SCorey Minyard     .parent = TYPE_DEVICE,
11423076bb3SCorey Minyard     .instance_size = sizeof(IPMIBmc),
11523076bb3SCorey Minyard     .abstract = true,
11623076bb3SCorey Minyard     .class_size = sizeof(IPMIBmcClass),
11723076bb3SCorey Minyard     .class_init = bmc_class_init,
11823076bb3SCorey Minyard };
11923076bb3SCorey Minyard 
12023076bb3SCorey Minyard static void ipmi_register_types(void)
12123076bb3SCorey Minyard {
12223076bb3SCorey Minyard     type_register_static(&ipmi_interface_type_info);
12323076bb3SCorey Minyard     type_register_static(&ipmi_bmc_type_info);
12423076bb3SCorey Minyard }
12523076bb3SCorey Minyard 
12623076bb3SCorey Minyard type_init(ipmi_register_types)
12790b61805SCorey Minyard 
12890b61805SCorey Minyard static IPMIFwInfo *ipmi_fw_info;
12990b61805SCorey Minyard static unsigned int ipmi_fw_info_len;
13090b61805SCorey Minyard 
13190b61805SCorey Minyard static uint32_t current_uuid = 1;
13290b61805SCorey Minyard 
13390b61805SCorey Minyard void ipmi_add_fwinfo(IPMIFwInfo *info, Error **errp)
13490b61805SCorey Minyard {
13590b61805SCorey Minyard     info->uuid = current_uuid++;
13690b61805SCorey Minyard     ipmi_fw_info = g_realloc(ipmi_fw_info,
13790b61805SCorey Minyard                              sizeof(*ipmi_fw_info) * (ipmi_fw_info_len + 1));
13890b61805SCorey Minyard     ipmi_fw_info[ipmi_fw_info_len] = *info;
13990b61805SCorey Minyard }
14090b61805SCorey Minyard 
14190b61805SCorey Minyard IPMIFwInfo *ipmi_first_fwinfo(void)
14290b61805SCorey Minyard {
14390b61805SCorey Minyard     return ipmi_fw_info;
14490b61805SCorey Minyard }
14590b61805SCorey Minyard 
14690b61805SCorey Minyard IPMIFwInfo *ipmi_next_fwinfo(IPMIFwInfo *current)
14790b61805SCorey Minyard {
14890b61805SCorey Minyard     current++;
14990b61805SCorey Minyard     if (current >= &ipmi_fw_info[ipmi_fw_info_len]) {
15090b61805SCorey Minyard         return NULL;
15190b61805SCorey Minyard     }
15290b61805SCorey Minyard     return current;
15390b61805SCorey Minyard }
154