-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathocc_command.cpp
284 lines (253 loc) · 8.15 KB
/
occ_command.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
#include "config.h"
#include "occ_command.hpp"
#include <errno.h>
#include <fcntl.h>
#include <unistd.h>
#include <org/open_power/OCC/Device/error.hpp>
#include <phosphor-logging/lg2.hpp>
#include <algorithm>
#include <format>
#include <string>
// #define TRACE_PACKETS
namespace open_power
{
namespace occ
{
// Trace block of data in hex
void dump_hex(const std::vector<std::uint8_t>& data,
const unsigned int data_len)
{
unsigned int dump_length = data.size();
if ((data_len > 0) && (data_len < dump_length))
{
dump_length = data_len;
}
std::string s;
for (uint32_t i = 0; i < dump_length; i++)
{
if (i % 16 == 0)
{
s += std::format("{:04X}: ", i);
}
else if (i % 4 == 0)
{
s += " ";
}
s += std::format("{:02X}", data.at(i));
if ((i % 16 == 15) || (i == (dump_length - 1)))
{
lg2::info("{STRING}", "STRING", s);
s.clear();
}
}
}
OccCommand::OccCommand(uint8_t instance, const char* path) :
occInstance(instance), path(path),
devicePath(OCC_DEV_PATH + std::to_string((this->path.back() - '0') + 1)),
activeStatusSignal(
utils::getBus(),
sdbusRule::propertiesChanged(path, "org.open_power.OCC.Status"),
std::bind(std::mem_fn(&OccCommand::activeStatusEvent), this,
std::placeholders::_1))
{
lg2::debug("OccCommand::OccCommand(path={PATH}, devicePath={DEVICE}",
"PATH", this->path, "DEVICE", devicePath);
}
void OccCommand::openDevice()
{
using namespace sdbusplus::org::open_power::OCC::Device::Error;
lg2::debug("OccCommand::openDevice: calling open {PATH}", "PATH",
devicePath);
fd = open(devicePath.c_str(), O_RDWR | O_NONBLOCK);
if (fd < 0)
{
const int openErrno = errno;
lg2::error(
"OccCommand::openDevice: open failed (errno={ERR}, path={PATH})",
"ERR", openErrno, "PATH", devicePath);
}
else
{
lg2::debug("OccCommand::openDevice: open success");
}
return;
}
void OccCommand::closeDevice()
{
if (fd >= 0)
{
lg2::debug("OccCommand::closeDevice: calling close()");
close(fd);
fd = -1;
}
}
CmdStatus OccCommand::send(const std::vector<uint8_t>& command,
std::vector<uint8_t>& response)
{
using namespace sdbusplus::org::open_power::OCC::Device::Error;
CmdStatus status = CmdStatus::FAILURE;
response.clear();
lg2::debug("OccCommand::send: calling openDevice()");
openDevice();
if (fd < 0)
{
// OCC is inactive; empty response
return CmdStatus::COMM_FAILURE;
}
const uint8_t cmd_type = command[0];
#ifdef TRACE_PACKETS
lg2::info("OCC{INST}: Sending {CMD} command (length={LEN}, {PATH})", "INST",
occInstance, "CMD", lg2::hex, cmd_type, "LEN", command.size(),
"PATH", devicePath);
dump_hex(command);
#else
lg2::debug("OccCommand::send: calling write()");
#endif
int retries = 1; // Allow a retry if a command fails to get valid response
do
{
auto rc = write(fd, command.data(), command.size());
const int writeErrno = errno;
if ((rc < 0) || (rc != (int)command.size()))
{
lg2::error(
"OccCommand::send: write(OCC{INST}, command:{CMD}) failed with errno={ERR} (retries={RETRIES})",
"INST", occInstance, "CMD", lg2::hex, cmd_type, "ERR",
writeErrno, "RETRIES", retries);
status = CmdStatus::COMM_FAILURE;
// retry if available
continue;
}
else
{
lg2::debug("OccCommand::send: write succeeded");
}
// Now read the response. This would be the content of occ-sram
while (1)
{
uint8_t data{};
auto len = read(fd, &data, sizeof(data));
const int readErrno = errno;
if (len > 0)
{
response.emplace_back(data);
}
else if (len < 0 && readErrno == EAGAIN)
{
// We may have data coming still.
// This driver does not need a sleep for a retry.
continue;
}
else if (len == 0)
{
lg2::debug("OccCommand::send: read completed");
// We have read all that we can.
status = CmdStatus::SUCCESS;
break;
}
else
{
lg2::error(
"OccCommand::send: read(OCC{INST}, command:{CMD) failed with errno={ERR} (rspSize={LEN}, retries={RETRIES})",
"INST", occInstance, "CMD", lg2::hex, cmd_type, "ERR",
readErrno, "LEN", response.size(), "RETRIES", retries);
status = CmdStatus::COMM_FAILURE;
break;
}
}
if (status != CmdStatus::SUCCESS)
{
// retry if available
continue;
}
if (response.size() > 2)
{
#ifdef TRACE_PACKETS
lg2::info(
"OCC{INST}: Received {CMD} response (length={LEN} w/checksum)",
"INST", occInstance, "CMD", lg2::hex, cmd_type, "LEN",
response.size());
dump_hex(response, 64);
#endif
// Validate checksum (last 2 bytes of response)
const unsigned int csumIndex = response.size() - 2;
const uint32_t rspChecksum =
(response[csumIndex] << 8) + response[csumIndex + 1];
// OCC checksum is the 2 byte sum of data (ignoring overflow)
uint16_t calcChecksum = 0;
for (unsigned int index = 0; index < csumIndex; ++index)
{
calcChecksum += response[index];
}
if (calcChecksum != rspChecksum)
{
lg2::error("OCC{INST}: Checksum Mismatch: response "
"{RCVD}, calculated {EXPECT}",
"INST", occInstance, "RCVD", rspChecksum, "EXPECT",
calcChecksum);
dump_hex(response);
status = CmdStatus::COMM_FAILURE;
}
else
{
// Validate response was for the specified command
if (command[0] == response[1])
{
// Valid response received
// Strip off 2 byte checksum
response.pop_back();
response.pop_back();
break;
}
else
{
lg2::error("OccCommand::send: Response command mismatch "
"(sent: "
"{CMD}, rsp: {STATUS}, rsp seq#: {SEQ}",
"CMD", lg2::hex, command[0], "STATUS", lg2::hex,
response[1], "SEQ", lg2::hex, response[0]);
dump_hex(response, 64);
}
}
}
else
{
lg2::error(
"OccCommand::send: Invalid OCC{INST} response length: {LEN}",
"INST", occInstance, "LEN", response.size());
status = CmdStatus::FAILURE;
dump_hex(response);
}
if (retries > 0)
{
lg2::error("OccCommand::send: Command will be retried");
response.clear();
}
} while (retries-- > 0);
closeDevice();
return status;
}
// Called at OCC Status change signal
void OccCommand::activeStatusEvent(sdbusplus::message_t& msg)
{
std::string statusInterface;
std::map<std::string, std::variant<bool>> msgData;
msg.read(statusInterface, msgData);
auto propertyMap = msgData.find("OccActive");
if (propertyMap != msgData.end())
{
// Extract the OccActive property
if (std::get<bool>(propertyMap->second))
{
occActive = true;
}
else
{
occActive = false;
this->closeDevice();
}
}
return;
}
} // namespace occ
} // namespace open_power