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