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