blob: c826983f541b8a2c3c3fae731972305f867e3747 [file] [log] [blame]
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +05301#include "config.h"
Gunnar Mills94df8c92018-09-14 14:50:03 -05002
3#include "occ_pass_through.hpp"
4
5#include "elog-errors.hpp"
6
7#include <errno.h>
8#include <fcntl.h>
Chris Caina8857c52021-01-27 11:53:05 -06009#include <fmt/core.h>
Gunnar Mills94df8c92018-09-14 14:50:03 -050010#include <unistd.h>
11
Gunnar Mills94df8c92018-09-14 14:50:03 -050012#include <org/open_power/OCC/Device/error.hpp>
13#include <phosphor-logging/elog.hpp>
14#include <phosphor-logging/log.hpp>
George Liub5ca1012021-09-10 12:53:11 +080015
16#include <algorithm>
17#include <memory>
Gunnar Mills94df8c92018-09-14 14:50:03 -050018#include <string>
Chris Caina8857c52021-01-27 11:53:05 -060019
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -050020namespace open_power
21{
22namespace occ
23{
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -050024
Chris Cain36f9cde2021-11-22 11:18:21 -060025using namespace phosphor::logging;
26using namespace sdbusplus::org::open_power::OCC::Device::Error;
27
28PassThrough::PassThrough(
29 const char* path
30#ifdef POWER10
31 ,
32 std::unique_ptr<open_power::occ::powermode::PowerMode>& powerModeRef
33#endif
34 ) :
35 Iface(utils::getBus(), path),
36 path(path),
37#ifdef POWER10
38 pmode(powerModeRef),
39#endif
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +053040 devicePath(OCC_DEV_PATH + std::to_string((this->path.back() - '0') + 1)),
Chris Caina8857c52021-01-27 11:53:05 -060041 occInstance(this->path.back() - '0'),
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +053042 activeStatusSignal(
George Liuf3b75142021-06-10 11:22:50 +080043 utils::getBus(),
44 sdbusRule::propertiesChanged(path, "org.open_power.OCC.Status"),
Gunnar Mills94df8c92018-09-14 14:50:03 -050045 std::bind(std::mem_fn(&PassThrough::activeStatusEvent), this,
Chris Caina8857c52021-01-27 11:53:05 -060046 std::placeholders::_1)),
George Liuf3b75142021-06-10 11:22:50 +080047 occCmd(occInstance, path)
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -050048{
Vishwanatha Subbanna38b08d72017-04-14 18:12:14 +053049 // Nothing to do.
50}
51
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -050052std::vector<int32_t> PassThrough::send(std::vector<int32_t> command)
53{
Gunnar Mills94df8c92018-09-14 14:50:03 -050054 std::vector<int32_t> response{};
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053055
Vishwanatha Subbanna7d700e22017-05-19 19:58:01 +053056 // OCC only understands [bytes] so need array of bytes. Doing this
57 // because rest-server currently treats all int* as 32 bit integer.
Chris Caina8857c52021-01-27 11:53:05 -060058 std::vector<uint8_t> cmdInBytes, rsp;
Vishwanatha Subbanna7d700e22017-05-19 19:58:01 +053059 cmdInBytes.resize(command.size());
60
61 // Populate uint8_t version of vector.
62 std::transform(command.begin(), command.end(), cmdInBytes.begin(),
Gunnar Mills94df8c92018-09-14 14:50:03 -050063 [](decltype(cmdInBytes)::value_type x) { return x; });
Vishwanatha Subbanna7d700e22017-05-19 19:58:01 +053064
Chris Caina8857c52021-01-27 11:53:05 -060065 rsp = send(cmdInBytes);
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053066
Chris Caina8857c52021-01-27 11:53:05 -060067 response.resize(rsp.size());
68 std::transform(rsp.begin(), rsp.end(), response.begin(),
69 [](decltype(response)::value_type x) { return x; });
70
71 return response;
72}
73
74std::vector<uint8_t> PassThrough::send(std::vector<uint8_t> command)
75{
Chris Caina8857c52021-01-27 11:53:05 -060076 std::vector<uint8_t> response{};
77
Chris Cain40501a22022-03-14 17:33:27 -050078 log<level::INFO>(
Chris Caina8857c52021-01-27 11:53:05 -060079 fmt::format("PassThrough::send() Sending 0x{:02X} command to OCC{}",
80 command.front(), occInstance)
81 .c_str());
82 CmdStatus status = occCmd.send(command, response);
83 if (status == CmdStatus::SUCCESS)
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053084 {
Chris Caina8857c52021-01-27 11:53:05 -060085 if (response.size() >= 5)
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053086 {
George Liub5ca1012021-09-10 12:53:11 +080087 log<level::DEBUG>(
88 fmt::format("PassThrough::send() response had {} bytes",
89 response.size())
90 .c_str());
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053091 }
92 else
93 {
Chris Caina8857c52021-01-27 11:53:05 -060094 log<level::ERR>("PassThrough::send() Invalid OCC response");
95 dump_hex(response);
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +053096 }
97 }
Chris Caina8857c52021-01-27 11:53:05 -060098 else
99 {
Chris Cainc567dc82022-04-01 15:09:17 -0500100 log<level::ERR>(
101 fmt::format(
102 "PassThrough::send(): OCC command failed with status {}",
103 uint32_t(status))
104 .c_str());
Chris Caina8857c52021-01-27 11:53:05 -0600105 }
Eddie James4f4712d2018-06-21 15:57:02 -0500106
Vishwanatha Subbanna67d50ad2017-04-17 23:21:52 +0530107 return response;
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -0500108}
109
Chris Cain36f9cde2021-11-22 11:18:21 -0600110bool PassThrough::setMode(const uint8_t mode, const uint16_t modeData)
111{
112#ifdef POWER10
113 SysPwrMode newMode = SysPwrMode(mode);
114
115 if ((!VALID_POWER_MODE_SETTING(newMode)) &&
116 (!VALID_OEM_POWER_MODE_SETTING(newMode)))
117 {
118 log<level::ERR>(
119 fmt::format(
120 "PassThrough::setMode() Unsupported mode {} requested (0x{:04X})",
121 newMode, modeData)
122 .c_str());
123 return false;
124 }
125
126 if (((newMode == SysPwrMode::FFO) || (newMode == SysPwrMode::SFP)) &&
127 (modeData == 0))
128 {
129 log<level::ERR>(
130 fmt::format(
131 "PassThrough::setMode() Mode {} requires non-zero frequency point.",
132 newMode)
133 .c_str());
134 return false;
135 }
136
Chris Cain6fa848a2022-01-24 14:54:38 -0600137 if (!pmode)
138 {
139 log<level::ERR>("PassThrough::setMode: PowerMode is not defined!");
140 return false;
141 }
142
Chris Cain36f9cde2021-11-22 11:18:21 -0600143 log<level::INFO>(
144 fmt::format("PassThrough::setMode() Setting Power Mode {} (data: {})",
145 newMode, modeData)
146 .c_str());
147 return pmode->setMode(newMode, modeData);
148#else
149 log<level::DEBUG>(
150 fmt::format(
151 "PassThrough::setMode() No support to setting Power Mode {} (data: {})",
152 mode, modeData)
153 .c_str());
154 return false;
155#endif
156}
157
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +0530158// Called at OCC Status change signal
159void PassThrough::activeStatusEvent(sdbusplus::message::message& msg)
160{
161 std::string statusInterface;
Patrick Williamse0962702020-05-13 17:50:22 -0500162 std::map<std::string, std::variant<bool>> msgData;
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +0530163 msg.read(statusInterface, msgData);
164
165 auto propertyMap = msgData.find("OccActive");
166 if (propertyMap != msgData.end())
167 {
168 // Extract the OccActive property
Patrick Williams305ff8b2020-05-13 11:17:39 -0500169 if (std::get<bool>(propertyMap->second))
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +0530170 {
Eddie James4f4712d2018-06-21 15:57:02 -0500171 occActive = true;
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +0530172 }
173 else
174 {
Eddie James4f4712d2018-06-21 15:57:02 -0500175 occActive = false;
Vishwanatha Subbanna3e5422e2017-08-10 18:25:26 +0530176 }
177 }
178 return;
179}
180
Deepak Kodihalli6b492fb2017-03-18 01:09:28 -0500181} // namespace occ
182} // namespace open_power