Andrew Geissler | 758e3af | 2021-04-29 21:38:39 -0500 | [diff] [blame] | 1 | |
Patrick Venture | 46470a3 | 2018-09-07 19:26:25 -0700 | [diff] [blame] | 2 | #include "config.h" |
| 3 | |
| 4 | #include "host-interface.hpp" |
| 5 | |
| 6 | #include "systemintfcmds.hpp" |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 7 | |
| 8 | #include <functional> |
William A. Kennington III | 194375f | 2018-12-14 02:14:33 -0800 | [diff] [blame] | 9 | #include <ipmid-host/cmd-utils.hpp> |
| 10 | #include <ipmid-host/cmd.hpp> |
Andrew Geissler | 758e3af | 2021-04-29 21:38:39 -0500 | [diff] [blame] | 11 | #include <ipmid/api.hpp> |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 12 | #include <ipmid/utils.hpp> |
Andrew Geissler | 758e3af | 2021-04-29 21:38:39 -0500 | [diff] [blame] | 13 | #include <memory> |
| 14 | #include <optional> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 15 | #include <phosphor-logging/log.hpp> |
Patrick Venture | 46470a3 | 2018-09-07 19:26:25 -0700 | [diff] [blame] | 16 | |
Andrew Geissler | dd2c6fd | 2017-03-16 15:53:20 -0500 | [diff] [blame] | 17 | namespace phosphor |
| 18 | { |
| 19 | namespace host |
| 20 | { |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 21 | namespace command |
Andrew Geissler | 8315970 | 2017-04-03 13:31:13 -0500 | [diff] [blame] | 22 | { |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 23 | |
Andrew Geissler | 758e3af | 2021-04-29 21:38:39 -0500 | [diff] [blame] | 24 | using namespace phosphor::logging; |
| 25 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 26 | // When you see Base:: you know we're referencing our base class |
| 27 | namespace Base = sdbusplus::xyz::openbmc_project::Control::server; |
| 28 | |
| 29 | // IPMI OEM command. |
| 30 | // https://github.com/openbmc/openbmc/issues/2082 for handling |
| 31 | // Non-OEM commands that need to send SMS_ATN |
| 32 | using OEMCmd = uint8_t; |
| 33 | |
| 34 | // Map of IPMI OEM command to its equivalent interface command. |
| 35 | // This is needed when invoking the callback handler to indicate |
| 36 | // the status of the executed command. |
| 37 | static const std::map<OEMCmd, Host::Command> intfCommand = { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 38 | {CMD_HEARTBEAT, Base::Host::Command::Heartbeat}, |
| 39 | {CMD_POWER, Base::Host::Command::SoftOff}}; |
Andrew Geissler | 8315970 | 2017-04-03 13:31:13 -0500 | [diff] [blame] | 40 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 41 | // Map of Interface command to its corresponding IPMI OEM command. |
| 42 | // This is needed when pushing IPMI commands to command manager's |
| 43 | // queue. The same pair will be returned when IPMI asks us |
| 44 | // why a SMS_ATN was sent |
| 45 | static const std::map<Host::Command, IpmiCmdData> ipmiCommand = { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 46 | {Base::Host::Command::Heartbeat, std::make_pair(CMD_HEARTBEAT, 0x00)}, |
| 47 | {Base::Host::Command::SoftOff, std::make_pair(CMD_POWER, SOFT_OFF)}}; |
Andrew Geissler | 8315970 | 2017-04-03 13:31:13 -0500 | [diff] [blame] | 48 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 49 | // Called at user request |
| 50 | void Host::execute(Base::Host::Command command) |
Andrew Geissler | 8315970 | 2017-04-03 13:31:13 -0500 | [diff] [blame] | 51 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 52 | log<level::DEBUG>( |
| 53 | "Pushing cmd on to queue", |
| 54 | entry("CONTROL_HOST_CMD=%s", convertForMessage(command).c_str())); |
Andrew Geissler | 0c07c32 | 2017-03-22 14:02:30 -0500 | [diff] [blame] | 55 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 56 | auto cmd = std::make_tuple(ipmiCommand.at(command), |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 57 | std::bind(&Host::commandStatusHandler, this, |
| 58 | std::placeholders::_1, |
| 59 | std::placeholders::_2)); |
Andrew Geissler | 0c07c32 | 2017-03-22 14:02:30 -0500 | [diff] [blame] | 60 | |
Chen,Yugang | 0e862fa | 2019-09-06 11:03:05 +0800 | [diff] [blame] | 61 | ipmid_send_cmd_to_host(std::move(cmd)); |
Andrew Geissler | 62817fa9 | 2017-03-20 14:20:49 -0500 | [diff] [blame] | 62 | } |
| 63 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 64 | // Called into by Command Manager |
| 65 | void Host::commandStatusHandler(IpmiCmdData cmd, bool status) |
| 66 | { |
| 67 | // Need to convert <cmd> to the equivalent one mentioned in spec |
| 68 | auto value = status ? Result::Success : Result::Failure; |
| 69 | |
| 70 | // Fire a signal |
| 71 | this->commandComplete(intfCommand.at(std::get<0>(cmd)), value); |
| 72 | } |
| 73 | |
Andrew Geissler | c7021b8 | 2021-04-28 15:22:35 -0500 | [diff] [blame] | 74 | Host::FirmwareCondition Host::currentFirmwareCondition() const |
| 75 | { |
Andrew Geissler | 758e3af | 2021-04-29 21:38:39 -0500 | [diff] [blame] | 76 | // shared object used to wait for host response |
| 77 | auto hostCondition = |
| 78 | std::make_shared<std::optional<Host::FirmwareCondition>>(); |
| 79 | |
| 80 | // callback for command to host |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 81 | auto hostAckCallback = [hostCondition](IpmiCmdData, bool status) { |
Andrew Geissler | 758e3af | 2021-04-29 21:38:39 -0500 | [diff] [blame] | 82 | auto value = status ? Host::FirmwareCondition::Running |
| 83 | : Host::FirmwareCondition::Off; |
| 84 | |
| 85 | log<level::DEBUG>("currentFirmwareCondition:hostAckCallback fired", |
| 86 | entry("CONTROL_HOST_CMD=%i", value)); |
| 87 | |
| 88 | *(hostCondition.get()) = value; |
| 89 | return; |
| 90 | }; |
| 91 | |
| 92 | auto cmd = phosphor::host::command::CommandHandler( |
| 93 | ipmiCommand.at(Base::Host::Command::Heartbeat), |
| 94 | std::move(hostAckCallback)); |
| 95 | |
| 96 | ipmid_send_cmd_to_host(std::move(cmd)); |
| 97 | |
| 98 | // Timer to ensure this function returns something within a reasonable time |
| 99 | phosphor::Timer hostAckTimer([hostCondition]() { |
| 100 | log<level::DEBUG>("currentFirmwareCondition: timer expired!"); |
| 101 | *(hostCondition.get()) = Host::FirmwareCondition::Off; |
| 102 | }); |
| 103 | |
| 104 | // Wait 1 second past the ATN_ACK timeout to ensure we wait for as |
| 105 | // long as the timeout |
| 106 | hostAckTimer.start(std::chrono::seconds(IPMI_SMS_ATN_ACK_TIMEOUT_SECS + 1)); |
| 107 | |
| 108 | auto io = getIoContext(); |
| 109 | |
| 110 | while (!hostCondition.get()->has_value()) |
| 111 | { |
| 112 | log<level::DEBUG>( |
| 113 | "currentFirmwareCondition: waiting for host response"); |
| 114 | io->run_for(std::chrono::milliseconds(100)); |
| 115 | } |
| 116 | hostAckTimer.stop(); |
| 117 | |
| 118 | log<level::DEBUG>("currentFirmwareCondition: hostCondition is ready!"); |
| 119 | return hostCondition.get()->value(); |
Andrew Geissler | c7021b8 | 2021-04-28 15:22:35 -0500 | [diff] [blame] | 120 | } |
| 121 | |
Vishwanatha Subbanna | 3eb117a | 2017-07-12 16:13:49 +0530 | [diff] [blame] | 122 | } // namespace command |
Andrew Geissler | dd2c6fd | 2017-03-16 15:53:20 -0500 | [diff] [blame] | 123 | } // namespace host |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 124 | } // namespace phosphor |