-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathocc_errors.cpp
184 lines (163 loc) · 5.12 KB
/
occ_errors.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
#include "occ_errors.hpp"
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <org/open_power/OCC/Device/error.hpp>
#include <phosphor-logging/elog-errors.hpp>
#include <phosphor-logging/elog.hpp>
#include <phosphor-logging/lg2.hpp>
#include <phosphor-logging/log.hpp>
#include <xyz/openbmc_project/Common/error.hpp>
namespace open_power
{
namespace occ
{
using namespace phosphor::logging;
using namespace sdbusplus::org::open_power::OCC::Device::Error;
using InternalFailure =
sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
// Populate the file descriptor on the error file
void Error::openFile()
{
using namespace phosphor::logging;
fd = open(file.c_str(), O_RDONLY | O_NONBLOCK);
const int open_errno = errno;
if (fd < 0)
{
lg2::error("Error::openFile: open of {FILE} failed (errno={ERR})",
"FILE", file.c_str(), "ERR", open_errno);
elog<OpenFailure>(phosphor::logging::org::open_power::OCC::Device::
OpenFailure::CALLOUT_ERRNO(open_errno),
phosphor::logging::org::open_power::OCC::Device::
OpenFailure::CALLOUT_DEVICE_PATH(file.c_str()));
}
}
// Attaches the FD to event loop and registers the callback handler
void Error::registerCallBack()
{
decltype(eventSource.get()) sourcePtr = nullptr;
auto r = sd_event_add_io(event.get(), &sourcePtr, fd, EPOLLPRI | EPOLLERR,
processEvents, this);
eventSource.reset(sourcePtr);
if (r < 0)
{
lg2::error("Failed to register callback handler: error={ERR}", "ERR",
strerror(-r));
elog<InternalFailure>();
}
}
// Starts to watch for errors
void Error::addWatch(bool poll)
{
if (!watching)
{
// Open the file
openFile();
if (poll)
{
// register the callback handler
registerCallBack();
}
// Set we are watching the error
watching = true;
}
}
// Stops watching for errors
void Error::removeWatch()
{
if (watching)
{
// Close the file
if (fd >= 0)
{
close(fd);
}
// Reduce the reference count. Since there is only one instances
// of add_io, this will result empty loop
eventSource.reset();
// We are no more watching the error
watching = false;
}
}
// Callback handler when there is an activity on the FD
int Error::processEvents(sd_event_source* /*es*/, int /*fd*/,
uint32_t /*revents*/, void* userData)
{
auto error = static_cast<Error*>(userData);
error->analyzeEvent();
return 0;
}
// Reads the error file and analyzes the data
void Error::analyzeEvent()
{
// Get the number of bytes to read
int err = 0;
int len = -1;
auto r = ioctl(fd, FIONREAD, &len);
if (r < 0)
{
elog<ConfigFailure>(
phosphor::logging::org::open_power::OCC::Device::ConfigFailure::
CALLOUT_ERRNO(errno),
phosphor::logging::org::open_power::OCC::Device::ConfigFailure::
CALLOUT_DEVICE_PATH(file.c_str()));
}
// A non-zero data indicates an error condition
// Let the caller take appropriate action on this
auto data = readFile(len);
if (!data.empty())
err = std::stoi(data, nullptr, 0);
if (callBack)
{
callBack(err);
}
return;
}
// Reads so many bytes as passed in
std::string Error::readFile(int len) const
{
auto data = std::make_unique<char[]>(len + 1);
auto retries = 3;
auto delay = std::chrono::milliseconds{100};
// OCC / FSI have intermittent issues so retry all reads
while (true)
{
// This file get created soon after binding. A value of 0 is
// deemed success and anything else is a Failure
// Since all the sysfs files would have size of 4096, if we read 0
// bytes -or- value '0', then it just means we are fine
auto r = read(fd, data.get(), len);
if (r < 0)
{
retries--;
if (retries == 0)
{
elog<ReadFailure>(
phosphor::logging::org::open_power::OCC::Device::
ReadFailure::CALLOUT_ERRNO(errno),
phosphor::logging::org::open_power::OCC::Device::
ReadFailure::CALLOUT_DEVICE_PATH(file.c_str()));
break;
}
std::this_thread::sleep_for(delay);
continue;
}
break;
}
// Need to seek to START, else the poll returns immediately telling
// there is data to be read
auto r = lseek(fd, 0, SEEK_SET);
if (r < 0)
{
lg2::error("Failure seeking error file to START");
elog<ConfigFailure>(
phosphor::logging::org::open_power::OCC::Device::ConfigFailure::
CALLOUT_ERRNO(errno),
phosphor::logging::org::open_power::OCC::Device::ConfigFailure::
CALLOUT_DEVICE_PATH(file.c_str()));
}
return std::string(data.get());
}
} // namespace occ
} // namespace open_power