blob: 431645152dea5a6dd18496a249784e290a84f402 [file] [log] [blame]
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +05301#include <chrono>
2#include <phosphor-logging/log.hpp>
3#include <phosphor-logging/elog-errors.hpp>
4#include <xyz/openbmc_project/Common/error.hpp>
Matt Spinler15309ef2018-06-27 13:01:25 -05005#include <xyz/openbmc_project/State/Host/server.hpp>
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +05306#include <systemintfcmds.h>
7#include <utils.hpp>
8#include <config.h>
9#include <host-cmd-manager.hpp>
Vishwanatha Subbanna6e8979d2017-07-13 16:48:20 +053010#include <timer.hpp>
11
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053012namespace phosphor
13{
14namespace host
15{
16namespace command
17{
18
19constexpr auto MAPPER_BUSNAME = "xyz.openbmc_project.ObjectMapper";
20constexpr auto MAPPER_PATH = "/xyz/openbmc_project/object_mapper";
21constexpr auto MAPPER_INTERFACE = "xyz.openbmc_project.ObjectMapper";
Matt Spinler15309ef2018-06-27 13:01:25 -050022constexpr auto HOST_STATE_PATH = "/xyz/openbmc_project/state/host0";
23constexpr auto HOST_STATE_INTERFACE = "xyz.openbmc_project.State.Host";
24constexpr auto HOST_TRANS_PROP = "RequestedHostTransition";
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053025
26// For throwing exceptions
27using namespace phosphor::logging;
28using InternalFailure = sdbusplus::xyz::openbmc_project::Common::
29 Error::InternalFailure;
30
Matt Spinler15309ef2018-06-27 13:01:25 -050031namespace sdbusRule = sdbusplus::bus::match::rules;
32
Vishwanatha Subbanna6e8979d2017-07-13 16:48:20 +053033Manager::Manager(sdbusplus::bus::bus& bus, sd_event* event) :
34 bus(bus),
Matt Spinler15309ef2018-06-27 13:01:25 -050035 timer(event, std::bind(&Manager::hostTimeout, this)),
36 hostTransitionMatch(bus,
37 sdbusRule::propertiesChanged(
38 HOST_STATE_PATH,
39 HOST_STATE_INTERFACE),
40 std::bind(&Manager::clearQueueOnPowerOn, this,
41 std::placeholders::_1))
Vishwanatha Subbanna6e8979d2017-07-13 16:48:20 +053042{
43 // Nothing to do here.
44}
45
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053046// Called as part of READ_MSG_DATA command
47IpmiCmdData Manager::getNextCommand()
48{
49 // Stop the timer. Don't have to Err failure doing so.
50 auto r = timer.setTimer(SD_EVENT_OFF);
51 if (r < 0)
52 {
53 log<level::ERR>("Failure to STOP the timer",
54 entry("ERROR=%s", strerror(-r)));
55 }
56
57 if(this->workQueue.empty())
58 {
59 // Just return a heartbeat in this case. A spurious SMS_ATN was
60 // asserted for the host (probably from a previous boot).
Aditya Saripalli5fb14602017-11-09 14:46:27 +053061 log<level::DEBUG>("Control Host work queue is empty!");
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053062
63 return std::make_pair(CMD_HEARTBEAT, 0x00);
64 }
65
66 // Pop the processed entry off the queue
67 auto command = this->workQueue.front();
68 this->workQueue.pop();
69
70 // IPMI command is the first element in pair
71 auto ipmiCmdData = std::get<0>(command);
72
73 // Now, call the user registered functions so that
74 // implementation specific CommandComplete signals
75 // can be sent. `true` indicating Success.
76 std::get<CallBack>(command)(ipmiCmdData, true);
77
78 // Check for another entry in the queue and kick it off
79 this->checkQueueAndAlertHost();
80
81 // Tuple of command and data
82 return ipmiCmdData;
83}
84
85// Called when initial timer goes off post sending SMS_ATN
86void Manager::hostTimeout()
87{
88 log<level::ERR>("Host control timeout hit!");
89
Matt Spinler15309ef2018-06-27 13:01:25 -050090 clearQueue();
91}
92
93void Manager::clearQueue()
94{
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +053095 // Dequeue all entries and send fail signal
96 while(!this->workQueue.empty())
97 {
98 auto command = this->workQueue.front();
99 this->workQueue.pop();
100
101 // IPMI command is the first element in pair
102 auto ipmiCmdData = std::get<0>(command);
103
104 // Call the implementation specific Command Failure.
105 // `false` indicating Failure
106 std::get<CallBack>(command)(ipmiCmdData, false);
107 }
108}
109
110// Called for alerting the host
111void Manager::checkQueueAndAlertHost()
112{
113 if (this->workQueue.size() >= 1)
114 {
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530115 log<level::DEBUG>("Asserting SMS Attention");
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530116
117 std::string IPMI_PATH("/org/openbmc/HostIpmi/1");
118 std::string IPMI_INTERFACE("org.openbmc.HostIpmi");
119
120 auto host = ::ipmi::getService(this->bus,IPMI_INTERFACE,IPMI_PATH);
121
122 // Start the timer for this transaction
123 auto time = std::chrono::duration_cast<std::chrono::microseconds>(
124 std::chrono::seconds(IPMI_SMS_ATN_ACK_TIMEOUT_SECS));
125
126 auto r = timer.startTimer(time);
127 if (r < 0)
128 {
129 log<level::ERR>("Error starting timer for control host");
130 return;
131 }
132
133 auto method = this->bus.new_method_call(host.c_str(),
134 IPMI_PATH.c_str(),
135 IPMI_INTERFACE.c_str(),
136 "setAttention");
137 auto reply = this->bus.call(method);
138
139 if (reply.is_method_error())
140 {
141 log<level::ERR>("Error in setting SMS attention");
142 elog<InternalFailure>();
143 }
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530144 log<level::DEBUG>("SMS Attention asserted");
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530145 }
146}
147
148// Called by specific implementations that provide commands
149void Manager::execute(CommandHandler command)
150{
Aditya Saripalli5fb14602017-11-09 14:46:27 +0530151 log<level::DEBUG>("Pushing cmd on to queue",
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530152 entry("COMMAND=%d", std::get<0>(command).first));
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
Matt Spinler15309ef2018-06-27 13:01:25 -0500170void Manager::clearQueueOnPowerOn(sdbusplus::message::message& msg)
171{
172 namespace server = sdbusplus::xyz::openbmc_project::State::server;
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
184 auto& requestedState = properties.at(HOST_TRANS_PROP).get<std::string>();
185
186 if (server::Host::convertTransitionFromString(requestedState) ==
187 server::Host::Transition::On)
188 {
189 clearQueue();
190 }
191}
192
Vishwanatha Subbannaac149a92017-07-11 18:16:50 +0530193} // namespace command
194} // namespace host
195} // namepsace phosphor