ipmi.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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. #include "qemu/osdep.h"
  25. #include "hw/ipmi/ipmi.h"
  26. #include "hw/qdev-properties.h"
  27. #include "qom/object_interfaces.h"
  28. #include "sysemu/runstate.h"
  29. #include "qapi/error.h"
  30. #include "qemu/module.h"
  31. #include "hw/nmi.h"
  32. static uint32_t ipmi_current_uuid = 1;
  33. uint32_t ipmi_next_uuid(void)
  34. {
  35. return ipmi_current_uuid++;
  36. }
  37. static int ipmi_do_hw_op(IPMIInterface *s, enum ipmi_op op, int checkonly)
  38. {
  39. switch (op) {
  40. case IPMI_RESET_CHASSIS:
  41. if (checkonly) {
  42. return 0;
  43. }
  44. qemu_system_reset_request(SHUTDOWN_CAUSE_GUEST_RESET);
  45. return 0;
  46. case IPMI_POWEROFF_CHASSIS:
  47. if (checkonly) {
  48. return 0;
  49. }
  50. qemu_system_shutdown_request(SHUTDOWN_CAUSE_GUEST_SHUTDOWN);
  51. return 0;
  52. case IPMI_SEND_NMI:
  53. if (checkonly) {
  54. return 0;
  55. }
  56. /* We don't care what CPU we use. */
  57. nmi_monitor_handle(0, NULL);
  58. return 0;
  59. case IPMI_SHUTDOWN_VIA_ACPI_OVERTEMP:
  60. if (checkonly) {
  61. return 0;
  62. }
  63. qemu_system_powerdown_request();
  64. return 0;
  65. case IPMI_POWERCYCLE_CHASSIS:
  66. case IPMI_PULSE_DIAG_IRQ:
  67. case IPMI_POWERON_CHASSIS:
  68. default:
  69. return IPMI_CC_COMMAND_NOT_SUPPORTED;
  70. }
  71. }
  72. static void ipmi_interface_class_init(ObjectClass *class, void *data)
  73. {
  74. IPMIInterfaceClass *ik = IPMI_INTERFACE_CLASS(class);
  75. ik->do_hw_op = ipmi_do_hw_op;
  76. }
  77. static TypeInfo ipmi_interface_type_info = {
  78. .name = TYPE_IPMI_INTERFACE,
  79. .parent = TYPE_INTERFACE,
  80. .class_size = sizeof(IPMIInterfaceClass),
  81. .class_init = ipmi_interface_class_init,
  82. };
  83. static void isa_ipmi_bmc_check(const Object *obj, const char *name,
  84. Object *val, Error **errp)
  85. {
  86. IPMIBmc *bmc = IPMI_BMC(val);
  87. if (bmc->intf)
  88. error_setg(errp, "BMC object is already in use");
  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_STRONG);
  95. }
  96. static Property ipmi_bmc_properties[] = {
  97. DEFINE_PROP_UINT8("slave_addr", IPMIBmc, slave_addr, 0x20),
  98. DEFINE_PROP_END_OF_LIST(),
  99. };
  100. static void bmc_class_init(ObjectClass *oc, void *data)
  101. {
  102. DeviceClass *dc = DEVICE_CLASS(oc);
  103. device_class_set_props(dc, ipmi_bmc_properties);
  104. }
  105. static TypeInfo ipmi_bmc_type_info = {
  106. .name = TYPE_IPMI_BMC,
  107. .parent = TYPE_DEVICE,
  108. .instance_size = sizeof(IPMIBmc),
  109. .abstract = true,
  110. .class_size = sizeof(IPMIBmcClass),
  111. .class_init = bmc_class_init,
  112. };
  113. static void ipmi_register_types(void)
  114. {
  115. type_register_static(&ipmi_interface_type_info);
  116. type_register_static(&ipmi_bmc_type_info);
  117. }
  118. type_init(ipmi_register_types)