blob: e52c9bb754308bf4a02affb034a225d101ffd08c [file] [log] [blame]
Patrick Venture46470a32018-09-07 19:26:25 -07001#include "config.h"
2
3#include "host-cmd-manager.hpp"
4
5#include "systemintfcmds.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07006
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +05307#include <chrono>
Vernon Mauery6a98fe72019-03-11 15:57:48 -07008#include <ipmid/utils.hpp>
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +05309#include <phosphor-logging/elog-errors.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070010#include <phosphor-logging/log.hpp>
William A. Kennington III4c008022018-10-12 17:18:14 -070011#include <sdbusplus/message/types.hpp>
Vernon Mauery1181af72018-10-08 12:05:00 -070012#include <sdbusplus/timer.hpp>
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053013#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler15309ef2018-06-27 13:01:25 -050014#include <xyz/openbmc_project/State/Host/server.hpp>
Vishwanatha Subbanna6e8979d2017-07-13 16:48:20 +053015
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053016namespace phosphor
17{
18namespace host
19{
20namespace command
21{
22
Matt Spinler15309ef2018-06-27 13:01:25 -050023constexpr auto HOST_STATE_PATH = "/xyz/openbmc_project/state/host0";
24constexpr auto HOST_STATE_INTERFACE = "xyz.openbmc_project.State.Host";
25constexpr auto HOST_TRANS_PROP = "RequestedHostTransition";
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053026
27// For throwing exceptions
28using namespace phosphor::logging;
Patrick Venture0b02be92018-08-31 11:55:55 -070029using InternalFailure =
30 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053031
Matt Spinler15309ef2018-06-27 13:01:25 -050032namespace sdbusRule = sdbusplus::bus::match::rules;
33
Vernon Mauery316f23d2018-10-29 13:34:26 -070034Manager::Manager(sdbusplus::bus::bus& bus) :
35 bus(bus), timer(std::bind(&Manager::hostTimeout, this)),
Patrick Venture0b02be92018-08-31 11:55:55 -070036 hostTransitionMatch(
37 bus,
38 sdbusRule::propertiesChanged(HOST_STATE_PATH, HOST_STATE_INTERFACE),
39 std::bind(&Manager::clearQueueOnPowerOn, this, std::placeholders::_1))
Vishwanatha Subbanna6e8979d2017-07-13 16:48:20 +053040{
41 // Nothing to do here.
42}
43
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053044// Called as part of READ_MSG_DATA command
45IpmiCmdData Manager::getNextCommand()
46{
47 // Stop the timer. Don't have to Err failure doing so.
Vernon Mauery1181af72018-10-08 12:05:00 -070048 auto r = timer.stop();
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053049 if (r < 0)
50 {
51 log<level::ERR>("Failure to STOP the timer",
Patrick Venture0b02be92018-08-31 11:55:55 -070052 entry("ERROR=%s", strerror(-r)));
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053053 }
54
Patrick Venture0b02be92018-08-31 11:55:55 -070055 if (this->workQueue.empty())
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053056 {
57 // Just return a heartbeat in this case. A spurious SMS_ATN was
58 // asserted for the host (probably from a previous boot).
Aditya Saripalli5fb14602017-11-09 14:46:27 +053059 log<level::DEBUG>("Control Host work queue is empty!");
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053060
61 return std::make_pair(CMD_HEARTBEAT, 0x00);
62 }
63
64 // Pop the processed entry off the queue
65 auto command = this->workQueue.front();
66 this->workQueue.pop();
67
68 // IPMI command is the first element in pair
69 auto ipmiCmdData = std::get<0>(command);
70
71 // Now, call the user registered functions so that
72 // implementation specific CommandComplete signals
73 // can be sent. `true` indicating Success.
74 std::get<CallBack>(command)(ipmiCmdData, true);
75
76 // Check for another entry in the queue and kick it off
77 this->checkQueueAndAlertHost();
78
79 // Tuple of command and data
80 return ipmiCmdData;
81}
82
83// Called when initial timer goes off post sending SMS_ATN
84void Manager::hostTimeout()
85{
86 log<level::ERR>("Host control timeout hit!");
87
Matt Spinler15309ef2018-06-27 13:01:25 -050088 clearQueue();
89}
90
91void Manager::clearQueue()
92{
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053093 // Dequeue all entries and send fail signal
Patrick Venture0b02be92018-08-31 11:55:55 -070094 while (!this->workQueue.empty())
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053095 {
96 auto command = this->workQueue.front();
97 this->workQueue.pop();
98
99 // IPMI command is the first element in pair
100 auto ipmiCmdData = std::get<0>(command);
101
102 // Call the implementation specific Command Failure.
103 // `false` indicating Failure
104 std::get<CallBack>(command)(ipmiCmdData, false);
105 }
106}
107
108// Called for alerting the host
109void Manager::checkQueueAndAlertHost()
110{
111 if (this->workQueue.size() >= 1)
112 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530113 log<level::DEBUG>("Asserting SMS Attention");
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530114
Andrew Geisslerb18c8bc2020-08-05 15:54:44 -0500115 std::string HOST_IPMI_SVC("org.openbmc.HostIpmi");
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530116 std::string IPMI_PATH("/org/openbmc/HostIpmi/1");
117 std::string IPMI_INTERFACE("org.openbmc.HostIpmi");
118
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530119 // Start the timer for this transaction
120 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
Patrick Venture0b02be92018-08-31 11:55:55 -0700121 std::chrono::seconds(IPMI_SMS_ATN_ACK_TIMEOUT_SECS));
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530122
Vernon Mauery1181af72018-10-08 12:05:00 -0700123 auto r = timer.start(time);
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530124 if (r < 0)
125 {
126 log<level::ERR>("Error starting timer for control host");
127 return;
128 }
129
Patrick Venture0b02be92018-08-31 11:55:55 -0700130 auto method =
Andrew Geisslerb18c8bc2020-08-05 15:54:44 -0500131 this->bus.new_method_call(HOST_IPMI_SVC.c_str(), IPMI_PATH.c_str(),
Patrick Venture0b02be92018-08-31 11:55:55 -0700132 IPMI_INTERFACE.c_str(), "setAttention");
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530133 auto reply = this->bus.call(method);
134
135 if (reply.is_method_error())
136 {
137 log<level::ERR>("Error in setting SMS attention");
138 elog<InternalFailure>();
139 }
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530140 log<level::DEBUG>("SMS Attention asserted");
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530141 }
142}
143
144// Called by specific implementations that provide commands
145void Manager::execute(CommandHandler command)
146{
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530147 log<level::DEBUG>("Pushing cmd on to queue",
Patrick Venture0b02be92018-08-31 11:55:55 -0700148 entry("COMMAND=%d", std::get<0>(command).first));
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530149
150 this->workQueue.emplace(command);
151
152 // Alert host if this is only command in queue otherwise host will
153 // be notified of next message after processing the current one
154 if (this->workQueue.size() == 1)
155 {
156 this->checkQueueAndAlertHost();
157 }
158 else
159 {
160 log<level::INFO>("Command in process, no attention");
161 }
162
163 return;
164}
165
Matt Spinler15309ef2018-06-27 13:01:25 -0500166void Manager::clearQueueOnPowerOn(sdbusplus::message::message& msg)
167{
168 namespace server = sdbusplus::xyz::openbmc_project::State::server;
169
170 ::ipmi::DbusInterface interface;
171 ::ipmi::PropertyMap properties;
172
173 msg.read(interface, properties);
174
175 if (properties.find(HOST_TRANS_PROP) == properties.end())
176 {
177 return;
178 }
179
William A. Kennington III4c008022018-10-12 17:18:14 -0700180 auto& requestedState =
Vernon Maueryf442e112019-04-09 11:44:36 -0700181 std::get<std::string>(properties.at(HOST_TRANS_PROP));
Matt Spinler15309ef2018-06-27 13:01:25 -0500182
183 if (server::Host::convertTransitionFromString(requestedState) ==
Patrick Venture0b02be92018-08-31 11:55:55 -0700184 server::Host::Transition::On)
Matt Spinler15309ef2018-06-27 13:01:25 -0500185 {
186 clearQueue();
187 }
188}
189
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530190} // namespace command
191} // namespace host
Patrick Venture0b02be92018-08-31 11:55:55 -0700192} // namespace phosphor