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