Patrick Venture | 46470a3 | 2018-09-07 19:26:25 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
| 3 | #include "host-cmd-manager.hpp" |
| 4 | |
| 5 | #include "systemintfcmds.hpp" |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 6 | |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 7 | #include <ipmid/utils.hpp> |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 9 | #include <phosphor-logging/log.hpp> |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 10 | #include <sdbusplus/message/types.hpp> |
Vernon Mauery | 1181af7 | 2018-10-08 12:05:00 -0700 | [diff] [blame] | 11 | #include <sdbusplus/timer.hpp> |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 12 | #include <xyz/openbmc_project/Common/error.hpp> |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 13 | #include <xyz/openbmc_project/State/Host/server.hpp> |
Vishwanatha Subbanna | 6e8979d | 2017-07-13 16:48:20 +0530 | [diff] [blame] | 14 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 15 | #include <chrono> |
| 16 | |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 17 | namespace phosphor |
| 18 | { |
| 19 | namespace host |
| 20 | { |
| 21 | namespace command |
| 22 | { |
| 23 | |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 24 | constexpr auto HOST_STATE_PATH = "/xyz/openbmc_project/state/host0"; |
| 25 | constexpr auto HOST_STATE_INTERFACE = "xyz.openbmc_project.State.Host"; |
| 26 | constexpr auto HOST_TRANS_PROP = "RequestedHostTransition"; |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 27 | |
| 28 | // For throwing exceptions |
| 29 | using namespace phosphor::logging; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 30 | using InternalFailure = |
Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 31 | sdbusplus::error::xyz::openbmc_project::common::InternalFailure; |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 32 | |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 33 | namespace sdbusRule = sdbusplus::bus::match::rules; |
| 34 | |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 35 | Manager::Manager(sdbusplus::bus_t& bus) : |
Vernon Mauery | 316f23d | 2018-10-29 13:34:26 -0700 | [diff] [blame] | 36 | bus(bus), timer(std::bind(&Manager::hostTimeout, this)), |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 37 | hostTransitionMatch( |
| 38 | bus, |
| 39 | sdbusRule::propertiesChanged(HOST_STATE_PATH, HOST_STATE_INTERFACE), |
| 40 | std::bind(&Manager::clearQueueOnPowerOn, this, std::placeholders::_1)) |
Vishwanatha Subbanna | 6e8979d | 2017-07-13 16:48:20 +0530 | [diff] [blame] | 41 | { |
| 42 | // Nothing to do here. |
| 43 | } |
| 44 | |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 45 | // Called as part of READ_MSG_DATA command |
| 46 | IpmiCmdData Manager::getNextCommand() |
| 47 | { |
| 48 | // Stop the timer. Don't have to Err failure doing so. |
Vernon Mauery | 1181af7 | 2018-10-08 12:05:00 -0700 | [diff] [blame] | 49 | auto r = timer.stop(); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 50 | if (r < 0) |
| 51 | { |
| 52 | log<level::ERR>("Failure to STOP the timer", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 53 | entry("ERROR=%s", strerror(-r))); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 54 | } |
| 55 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 56 | if (this->workQueue.empty()) |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 57 | { |
| 58 | // Just return a heartbeat in this case. A spurious SMS_ATN was |
| 59 | // asserted for the host (probably from a previous boot). |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 60 | log<level::DEBUG>("Control Host work queue is empty!"); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 61 | |
| 62 | return std::make_pair(CMD_HEARTBEAT, 0x00); |
| 63 | } |
| 64 | |
| 65 | // Pop the processed entry off the queue |
| 66 | auto command = this->workQueue.front(); |
| 67 | this->workQueue.pop(); |
| 68 | |
| 69 | // IPMI command is the first element in pair |
| 70 | auto ipmiCmdData = std::get<0>(command); |
| 71 | |
| 72 | // Now, call the user registered functions so that |
| 73 | // implementation specific CommandComplete signals |
| 74 | // can be sent. `true` indicating Success. |
| 75 | std::get<CallBack>(command)(ipmiCmdData, true); |
| 76 | |
| 77 | // Check for another entry in the queue and kick it off |
| 78 | this->checkQueueAndAlertHost(); |
| 79 | |
| 80 | // Tuple of command and data |
| 81 | return ipmiCmdData; |
| 82 | } |
| 83 | |
| 84 | // Called when initial timer goes off post sending SMS_ATN |
| 85 | void Manager::hostTimeout() |
| 86 | { |
| 87 | log<level::ERR>("Host control timeout hit!"); |
| 88 | |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 89 | clearQueue(); |
| 90 | } |
| 91 | |
| 92 | void Manager::clearQueue() |
| 93 | { |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 94 | // Dequeue all entries and send fail signal |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 95 | while (!this->workQueue.empty()) |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 96 | { |
| 97 | auto command = this->workQueue.front(); |
| 98 | this->workQueue.pop(); |
| 99 | |
| 100 | // IPMI command is the first element in pair |
| 101 | auto ipmiCmdData = std::get<0>(command); |
| 102 | |
| 103 | // Call the implementation specific Command Failure. |
| 104 | // `false` indicating Failure |
| 105 | std::get<CallBack>(command)(ipmiCmdData, false); |
| 106 | } |
| 107 | } |
| 108 | |
| 109 | // Called for alerting the host |
| 110 | void Manager::checkQueueAndAlertHost() |
| 111 | { |
| 112 | if (this->workQueue.size() >= 1) |
| 113 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 114 | log<level::DEBUG>("Asserting SMS Attention"); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 115 | |
Andrew Geissler | b18c8bc | 2020-08-05 15:54:44 -0500 | [diff] [blame] | 116 | std::string HOST_IPMI_SVC("org.openbmc.HostIpmi"); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 117 | std::string IPMI_PATH("/org/openbmc/HostIpmi/1"); |
| 118 | std::string IPMI_INTERFACE("org.openbmc.HostIpmi"); |
| 119 | |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 120 | // Start the timer for this transaction |
| 121 | auto time = std::chrono::duration_cast<std::chrono::microseconds>( |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 122 | std::chrono::seconds(IPMI_SMS_ATN_ACK_TIMEOUT_SECS)); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 123 | |
Vernon Mauery | 1181af7 | 2018-10-08 12:05:00 -0700 | [diff] [blame] | 124 | auto r = timer.start(time); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 125 | if (r < 0) |
| 126 | { |
| 127 | log<level::ERR>("Error starting timer for control host"); |
| 128 | return; |
| 129 | } |
| 130 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 131 | auto method = |
Andrew Geissler | b18c8bc | 2020-08-05 15:54:44 -0500 | [diff] [blame] | 132 | this->bus.new_method_call(HOST_IPMI_SVC.c_str(), IPMI_PATH.c_str(), |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 133 | IPMI_INTERFACE.c_str(), "setAttention"); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 134 | |
Thang Tran | 897ae72 | 2022-03-06 10:26:18 +0700 | [diff] [blame] | 135 | try |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 136 | { |
Thang Tran | 897ae72 | 2022-03-06 10:26:18 +0700 | [diff] [blame] | 137 | auto reply = this->bus.call(method); |
| 138 | |
| 139 | log<level::DEBUG>("SMS Attention asserted"); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 140 | } |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 141 | catch (sdbusplus::exception_t& e) |
Thang Tran | 897ae72 | 2022-03-06 10:26:18 +0700 | [diff] [blame] | 142 | { |
| 143 | log<level::ERR>("Error when call setAttention method"); |
| 144 | } |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 145 | } |
| 146 | } |
| 147 | |
| 148 | // Called by specific implementations that provide commands |
| 149 | void Manager::execute(CommandHandler command) |
| 150 | { |
Aditya Saripalli | 5fb1460 | 2017-11-09 14:46:27 +0530 | [diff] [blame] | 151 | log<level::DEBUG>("Pushing cmd on to queue", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 152 | entry("COMMAND=%d", std::get<0>(command).first)); |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 153 | |
| 154 | this->workQueue.emplace(command); |
| 155 | |
| 156 | // Alert host if this is only command in queue otherwise host will |
| 157 | // be notified of next message after processing the current one |
| 158 | if (this->workQueue.size() == 1) |
| 159 | { |
| 160 | this->checkQueueAndAlertHost(); |
| 161 | } |
| 162 | else |
| 163 | { |
| 164 | log<level::INFO>("Command in process, no attention"); |
| 165 | } |
| 166 | |
| 167 | return; |
| 168 | } |
| 169 | |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 170 | void Manager::clearQueueOnPowerOn(sdbusplus::message_t& msg) |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 171 | { |
Willy Tu | 523e2d1 | 2023-09-05 11:36:48 -0700 | [diff] [blame] | 172 | namespace server = sdbusplus::server::xyz::openbmc_project::state; |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 173 | |
| 174 | ::ipmi::DbusInterface interface; |
| 175 | ::ipmi::PropertyMap properties; |
| 176 | |
| 177 | msg.read(interface, properties); |
| 178 | |
| 179 | if (properties.find(HOST_TRANS_PROP) == properties.end()) |
| 180 | { |
| 181 | return; |
| 182 | } |
| 183 | |
William A. Kennington III | 4c00802 | 2018-10-12 17:18:14 -0700 | [diff] [blame] | 184 | auto& requestedState = |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 185 | std::get<std::string>(properties.at(HOST_TRANS_PROP)); |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 186 | |
| 187 | if (server::Host::convertTransitionFromString(requestedState) == |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 188 | server::Host::Transition::On) |
Matt Spinler | 15309ef | 2018-06-27 13:01:25 -0500 | [diff] [blame] | 189 | { |
| 190 | clearQueue(); |
| 191 | } |
| 192 | } |
| 193 | |
Vishwanatha Subbanna | ac149a9 | 2017-07-11 18:16:50 +0530 | [diff] [blame] | 194 | } // namespace command |
| 195 | } // namespace host |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 196 | } // namespace phosphor |