-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathbmc_state_manager.cpp
333 lines (275 loc) · 9.7 KB
/
bmc_state_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
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#include "bmc_state_manager.hpp"
#include "utils.hpp"
#include "xyz/openbmc_project/Common/error.hpp"
#include <gpiod.h>
#include <sys/sysinfo.h>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/lg2.hpp>
#include <sdbusplus/exception.hpp>
#include <cassert>
#include <filesystem>
#include <fstream>
#include <iostream>
namespace phosphor
{
namespace state
{
namespace manager
{
PHOSPHOR_LOG2_USING;
// When you see server:: you know we're referencing our base class
namespace server = sdbusplus::xyz::openbmc_project::State::server;
using namespace phosphor::logging;
using sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
constexpr auto obmcQuiesceTarget = "[email protected]";
constexpr auto obmcStandbyTarget = "multi-user.target";
constexpr auto signalDone = "done";
constexpr auto activeState = "active";
/* Map a transition to it's systemd target */
const std::map<server::BMC::Transition, const char*> SYSTEMD_TABLE = {
{server::BMC::Transition::Reboot, "reboot.target"}};
constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
constexpr auto SYSTEMD_OBJ_PATH = "/org/freedesktop/systemd1";
constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
constexpr auto SYSTEMD_PRP_INTERFACE = "org.freedesktop.DBus.Properties";
std::string BMC::getUnitState(const std::string& unitToCheck)
{
std::variant<std::string> currentState;
sdbusplus::message::object_path unitTargetPath;
auto method = this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
SYSTEMD_INTERFACE, "GetUnit");
method.append(unitToCheck);
try
{
auto result = this->bus.call(method);
result.read(unitTargetPath);
}
catch (const sdbusplus::exception_t& e)
{
// Not all input units will have been loaded yet so just return an
// empty string if an exception is caught in this path
info("Unit {UNIT} not found: {ERROR}", "UNIT", unitToCheck, "ERROR", e);
return std::string{};
}
method = this->bus.new_method_call(
SYSTEMD_SERVICE,
static_cast<const std::string&>(unitTargetPath).c_str(),
SYSTEMD_PRP_INTERFACE, "Get");
method.append("org.freedesktop.systemd1.Unit", "ActiveState");
try
{
auto result = this->bus.call(method);
// Is input target active or inactive?
result.read(currentState);
}
catch (const sdbusplus::exception_t& e)
{
info("Error in ActiveState Get: {ERROR}", "ERROR", e);
return std::string{};
}
return (std::get<std::string>(currentState));
}
void BMC::discoverInitialState()
{
// First look to see if the BMC quiesce target is active
auto currentStateStr = getUnitState(obmcQuiesceTarget);
if (currentStateStr == activeState)
{
info("Setting the BMCState field to BMC_QUIESCED");
this->currentBMCState(BMCState::Quiesced);
return;
}
// If not quiesced, then check standby target
currentStateStr = getUnitState(obmcStandbyTarget);
if (currentStateStr == activeState)
{
info("Setting the BMCState field to BMC_READY");
this->currentBMCState(BMCState::Ready);
}
else
{
info("Setting the BMCState field to BMC_NOTREADY");
this->currentBMCState(BMCState::NotReady);
}
return;
}
void BMC::executeTransition(const Transition tranReq)
{
// HardReboot does not shutdown any services and immediately transitions
// into the reboot process
if (server::BMC::Transition::HardReboot == tranReq)
{
// Put BMC state not NotReady when issuing a BMC reboot
// and stop monitoring for state changes
this->currentBMCState(BMCState::NotReady);
this->stateSignal.reset();
auto method = this->bus.new_method_call(
SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "Reboot");
try
{
this->bus.call(method);
}
catch (const sdbusplus::exception_t& e)
{
info("Error in HardReboot: {ERROR}", "ERROR", e);
}
}
else
{
// Check to make sure it can be found
auto iter = SYSTEMD_TABLE.find(tranReq);
if (iter == SYSTEMD_TABLE.end())
return;
const auto& sysdUnit = iter->second;
auto method = this->bus.new_method_call(
SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH, SYSTEMD_INTERFACE, "StartUnit");
// The only valid transition is reboot and that
// needs to be irreversible once started
method.append(sysdUnit, "replace-irreversibly");
// Put BMC state not NotReady when issuing a BMC reboot
// and stop monitoring for state changes
this->currentBMCState(BMCState::NotReady);
this->stateSignal.reset();
try
{
this->bus.call(method);
}
catch (const sdbusplus::exception_t& e)
{
info("Error in StartUnit - replace-irreversibly: {ERROR}", "ERROR",
e);
}
}
return;
}
int BMC::bmcStateChange(sdbusplus::message_t& msg)
{
uint32_t newStateID{};
sdbusplus::message::object_path newStateObjPath;
std::string newStateUnit{};
std::string newStateResult{};
// Read the msg and populate each variable
msg.read(newStateID, newStateObjPath, newStateUnit, newStateResult);
if ((newStateUnit == obmcQuiesceTarget) && (newStateResult == signalDone))
{
error("BMC has entered BMC_QUIESCED state");
this->currentBMCState(BMCState::Quiesced);
// There is no getting out of Quiesced once entered (other then BMC
// reboot) so stop watching for signals
auto method =
this->bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_OBJ_PATH,
SYSTEMD_INTERFACE, "Unsubscribe");
try
{
this->bus.call(method);
}
catch (const sdbusplus::exception_t& e)
{
info("Error in Unsubscribe: {ERROR}", "ERROR", e);
}
// disable the system state change object as well
this->stateSignal.reset();
return 0;
}
// Caught the signal that indicates the BMC is now BMC_READY
if ((newStateUnit == obmcStandbyTarget) && (newStateResult == signalDone))
{
info("BMC_READY");
this->currentBMCState(BMCState::Ready);
}
return 0;
}
BMC::Transition BMC::requestedBMCTransition(Transition value)
{
info("Setting the RequestedBMCTransition field to "
"{REQUESTED_BMC_TRANSITION}",
"REQUESTED_BMC_TRANSITION", value);
executeTransition(value);
return server::BMC::requestedBMCTransition(value);
}
BMC::BMCState BMC::currentBMCState(BMCState value)
{
info("Setting the BMCState field to {CURRENT_BMC_STATE}",
"CURRENT_BMC_STATE", value);
return server::BMC::currentBMCState(value);
}
BMC::RebootCause BMC::lastRebootCause(RebootCause value)
{
info("Setting the RebootCause field to {LAST_REBOOT_CAUSE}",
"LAST_REBOOT_CAUSE", value);
return server::BMC::lastRebootCause(value);
}
uint64_t BMC::lastRebootTime() const
{
using namespace std::chrono;
struct sysinfo info;
auto rc = sysinfo(&info);
assert(rc == 0);
// Since uptime is in seconds, also get the current time in seconds.
auto now = time_point_cast<seconds>(system_clock::now());
auto rebootTime = now - seconds(info.uptime);
return duration_cast<milliseconds>(rebootTime.time_since_epoch()).count();
}
void BMC::discoverLastRebootCause()
{
uint64_t bootReason = 0;
std::ifstream file;
auto bootstatusPath = "/sys/class/watchdog/watchdog0/bootstatus";
file.exceptions(std::ifstream::failbit | std::ifstream::badbit |
std::ifstream::eofbit);
try
{
file.open(bootstatusPath);
file >> bootReason;
}
catch (const std::exception& e)
{
auto rc = errno;
error("Failed to read sysfs file {FILE} with errno {ERRNO}", "FILE",
bootstatusPath, "ERRNO", rc);
}
switch (bootReason)
{
case WDIOF_EXTERN1:
this->lastRebootCause(RebootCause::Watchdog);
return;
case WDIOF_CARDRESET:
this->lastRebootCause(RebootCause::POR);
return;
default:
this->lastRebootCause(RebootCause::Unknown);
// Continue below to see if more details can be found
// on reason for reboot
break;
}
// If the above code could not detect a reason, look for a the
// reset-cause-pinhole gpio to see if it is the reason for the reboot
auto gpioval =
phosphor::state::manager::utils::getGpioValue("reset-cause-pinhole");
// A 0 indicates a pinhole reset occurred
if (0 == gpioval)
{
info("The BMC reset was caused by a pinhole reset");
this->lastRebootCause(RebootCause::PinholeReset);
// Generate log telling user a pinhole reset has occurred
const std::string errorMsg = "xyz.openbmc_project.State.PinholeReset";
phosphor::state::manager::utils::createError(
this->bus, errorMsg,
sdbusplus::xyz::openbmc_project::Logging::server::Entry::Level::
Notice);
return;
}
// If we still haven't found a reason, see if we lost AC power
// Note that a pinhole reset will remove AC power to the chassis
// on some systems so we always want to look for the pinhole reset
// first as that would be the main reason AC power was lost.
size_t chassisId = 0;
if (phosphor::state::manager::utils::checkACLoss(chassisId))
{
this->lastRebootCause(RebootCause::POR);
}
return;
}
} // namespace manager
} // namespace state
} // namespace phosphor