-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathocc_pass_through.cpp
196 lines (171 loc) · 5.24 KB
/
occ_pass_through.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
#include "config.h"
#include "occ_pass_through.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 <memory>
#include <string>
namespace open_power
{
namespace occ
{
using namespace phosphor::logging;
using namespace sdbusplus::org::open_power::OCC::Device::Error;
PassThrough::PassThrough(
const char* path
#ifdef POWER10
,
std::unique_ptr<open_power::occ::powermode::PowerMode>& powerModeRef
#endif
) :
Iface(utils::getBus(), path), path(path),
#ifdef POWER10
pmode(powerModeRef),
#endif
devicePath(OCC_DEV_PATH + std::to_string((this->path.back() - '0') + 1)),
occInstance(this->path.back() - '0'),
activeStatusSignal(
utils::getBus(),
sdbusRule::propertiesChanged(path, "org.open_power.OCC.Status"),
std::bind(std::mem_fn(&PassThrough::activeStatusEvent), this,
std::placeholders::_1)),
occCmd(occInstance, path)
{
// Nothing to do.
}
std::vector<int32_t> PassThrough::send(std::vector<int32_t> command)
{
std::vector<int32_t> response{};
// OCC only understands [bytes] so need array of bytes. Doing this
// because rest-server currently treats all int* as 32 bit integer.
std::vector<uint8_t> cmdInBytes, rsp;
cmdInBytes.resize(command.size());
// Populate uint8_t version of vector.
std::transform(command.begin(), command.end(), cmdInBytes.begin(),
[](decltype(cmdInBytes)::value_type x) { return x; });
rsp = send(cmdInBytes);
response.resize(rsp.size());
std::transform(rsp.begin(), rsp.end(), response.begin(),
[](decltype(response)::value_type x) { return x; });
return response;
}
std::vector<uint8_t> PassThrough::send(std::vector<uint8_t> command)
{
std::vector<uint8_t> response{};
if (!occActive)
{
lg2::error(
"PassThrough::send() - OCC{INST} not active, command not sent",
"INST", occInstance);
return response;
}
if (command.size() >= 3)
{
const uint16_t dataLen = command[1] << 8 | command[2];
std::string dataString = "";
if (command.size() > 3)
{
// Trace first 4 bytes of command data
size_t index = 3;
dataString = "0x";
for (; (index < 7) && (index < command.size()); ++index)
{
dataString += std::format("{:02X}", command[index]);
}
if (index < command.size())
{
dataString += "...";
}
}
lg2::info(
"PassThrough::send() Sending {CMD} command to OCC{INST} (data len={LEN}, data={DATA})",
"CMD", lg2::hex, command.front(), "INST", occInstance, "LEN",
dataLen, "DATA", dataString);
}
else
{
lg2::info("PassThrough::send() Sending {CMD} command to OCC{INST}",
"CMD", command.front(), "INST", occInstance);
}
CmdStatus status = occCmd.send(command, response);
if (status == CmdStatus::SUCCESS)
{
if (response.size() >= 5)
{
lg2::debug("PassThrough::send() response had {LEN} bytes", "LEN",
response.size());
}
else
{
lg2::error("PassThrough::send() Invalid OCC response");
dump_hex(response);
}
}
else
{
lg2::error(
"PassThrough::send(): OCC command failed with status {STATUS}",
"STATUS", status);
}
return response;
}
bool PassThrough::setMode(const uint8_t mode, const uint16_t modeData)
{
#ifdef POWER10
SysPwrMode newMode = SysPwrMode(mode);
if (!pmode)
{
lg2::error("PassThrough::setMode: PowerMode is not defined!");
return false;
}
if (!pmode->isValidMode(SysPwrMode(mode)))
{
lg2::error(
"PassThrough::setMode() Unsupported mode {MODE} requested ({DATA})",
"MODE", newMode, "DATA", modeData);
return false;
}
if (((newMode == SysPwrMode::FFO) || (newMode == SysPwrMode::SFP)) &&
(modeData == 0))
{
lg2::error(
"PassThrough::setMode() Mode {MODE} requires non-zero frequency point.",
"MODE", newMode);
return false;
}
lg2::info("PassThrough::setMode() Setting Power Mode {MODE} (data: {DATA})",
"MODE", uint8_t(newMode), "DATA", modeData);
return pmode->setMode(newMode, modeData);
#else
lg2::debug(
"PassThrough::setMode() No support to setting Power Mode {MODE} (data: {DATA})",
"MODE", mode, "DATA", modeData);
return false;
#endif
}
// Called at OCC Status change signal
void PassThrough::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;
}
}
return;
}
} // namespace occ
} // namespace open_power