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