Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 1 | #include "config.h" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 2 | |
| 3 | #include "occ_pass_through.hpp" |
| 4 | |
| 5 | #include "elog-errors.hpp" |
| 6 | |
| 7 | #include <errno.h> |
| 8 | #include <fcntl.h> |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 9 | #include <fmt/core.h> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 10 | #include <unistd.h> |
| 11 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 12 | #include <org/open_power/OCC/Device/error.hpp> |
| 13 | #include <phosphor-logging/elog.hpp> |
| 14 | #include <phosphor-logging/log.hpp> |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 15 | |
| 16 | #include <algorithm> |
| 17 | #include <memory> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 18 | #include <string> |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 19 | |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 20 | namespace open_power |
| 21 | { |
| 22 | namespace occ |
| 23 | { |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 24 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 25 | using namespace phosphor::logging; |
| 26 | using namespace sdbusplus::org::open_power::OCC::Device::Error; |
| 27 | |
| 28 | PassThrough::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 Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 40 | devicePath(OCC_DEV_PATH + std::to_string((this->path.back() - '0') + 1)), |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 41 | occInstance(this->path.back() - '0'), |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 42 | activeStatusSignal( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 43 | utils::getBus(), |
| 44 | sdbusRule::propertiesChanged(path, "org.open_power.OCC.Status"), |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 45 | std::bind(std::mem_fn(&PassThrough::activeStatusEvent), this, |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 46 | std::placeholders::_1)), |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 47 | occCmd(occInstance, path) |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 48 | { |
Vishwanatha Subbanna | 38b08d7 | 2017-04-14 18:12:14 +0530 | [diff] [blame] | 49 | // Nothing to do. |
| 50 | } |
| 51 | |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 52 | std::vector<int32_t> PassThrough::send(std::vector<int32_t> command) |
| 53 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 54 | std::vector<int32_t> response{}; |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 55 | |
Vishwanatha Subbanna | 7d700e2 | 2017-05-19 19:58:01 +0530 | [diff] [blame] | 56 | // OCC only understands [bytes] so need array of bytes. Doing this |
| 57 | // because rest-server currently treats all int* as 32 bit integer. |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 58 | std::vector<uint8_t> cmdInBytes, rsp; |
Vishwanatha Subbanna | 7d700e2 | 2017-05-19 19:58:01 +0530 | [diff] [blame] | 59 | cmdInBytes.resize(command.size()); |
| 60 | |
| 61 | // Populate uint8_t version of vector. |
| 62 | std::transform(command.begin(), command.end(), cmdInBytes.begin(), |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 63 | [](decltype(cmdInBytes)::value_type x) { return x; }); |
Vishwanatha Subbanna | 7d700e2 | 2017-05-19 19:58:01 +0530 | [diff] [blame] | 64 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 65 | rsp = send(cmdInBytes); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 66 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 67 | 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 | |
| 74 | std::vector<uint8_t> PassThrough::send(std::vector<uint8_t> command) |
| 75 | { |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 76 | std::vector<uint8_t> response{}; |
| 77 | |
Chris Cain | d4c19a0 | 2022-04-22 15:46:13 -0500 | [diff] [blame] | 78 | if (!occActive) |
| 79 | { |
| 80 | log<level::ERR>( |
| 81 | fmt::format( |
| 82 | "PassThrough::send() - OCC{} not active, command not sent", |
| 83 | occInstance) |
| 84 | .c_str()); |
| 85 | return response; |
| 86 | } |
| 87 | |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 88 | log<level::INFO>( |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 89 | fmt::format("PassThrough::send() Sending 0x{:02X} command to OCC{}", |
| 90 | command.front(), occInstance) |
| 91 | .c_str()); |
| 92 | CmdStatus status = occCmd.send(command, response); |
| 93 | if (status == CmdStatus::SUCCESS) |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 94 | { |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 95 | if (response.size() >= 5) |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 96 | { |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 97 | log<level::DEBUG>( |
| 98 | fmt::format("PassThrough::send() response had {} bytes", |
| 99 | response.size()) |
| 100 | .c_str()); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 104 | log<level::ERR>("PassThrough::send() Invalid OCC response"); |
| 105 | dump_hex(response); |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 106 | } |
| 107 | } |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 108 | else |
| 109 | { |
Chris Cain | c567dc8 | 2022-04-01 15:09:17 -0500 | [diff] [blame] | 110 | log<level::ERR>( |
| 111 | fmt::format( |
| 112 | "PassThrough::send(): OCC command failed with status {}", |
| 113 | uint32_t(status)) |
| 114 | .c_str()); |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 115 | } |
Eddie James | 4f4712d | 2018-06-21 15:57:02 -0500 | [diff] [blame] | 116 | |
Vishwanatha Subbanna | 67d50ad | 2017-04-17 23:21:52 +0530 | [diff] [blame] | 117 | return response; |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 118 | } |
| 119 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 120 | bool PassThrough::setMode(const uint8_t mode, const uint16_t modeData) |
| 121 | { |
| 122 | #ifdef POWER10 |
| 123 | SysPwrMode newMode = SysPwrMode(mode); |
| 124 | |
| 125 | if ((!VALID_POWER_MODE_SETTING(newMode)) && |
| 126 | (!VALID_OEM_POWER_MODE_SETTING(newMode))) |
| 127 | { |
| 128 | log<level::ERR>( |
| 129 | fmt::format( |
| 130 | "PassThrough::setMode() Unsupported mode {} requested (0x{:04X})", |
| 131 | newMode, modeData) |
| 132 | .c_str()); |
| 133 | return false; |
| 134 | } |
| 135 | |
| 136 | if (((newMode == SysPwrMode::FFO) || (newMode == SysPwrMode::SFP)) && |
| 137 | (modeData == 0)) |
| 138 | { |
| 139 | log<level::ERR>( |
| 140 | fmt::format( |
| 141 | "PassThrough::setMode() Mode {} requires non-zero frequency point.", |
| 142 | newMode) |
| 143 | .c_str()); |
| 144 | return false; |
| 145 | } |
| 146 | |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 147 | if (!pmode) |
| 148 | { |
| 149 | log<level::ERR>("PassThrough::setMode: PowerMode is not defined!"); |
| 150 | return false; |
| 151 | } |
| 152 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 153 | log<level::INFO>( |
| 154 | fmt::format("PassThrough::setMode() Setting Power Mode {} (data: {})", |
| 155 | newMode, modeData) |
| 156 | .c_str()); |
| 157 | return pmode->setMode(newMode, modeData); |
| 158 | #else |
| 159 | log<level::DEBUG>( |
| 160 | fmt::format( |
| 161 | "PassThrough::setMode() No support to setting Power Mode {} (data: {})", |
| 162 | mode, modeData) |
| 163 | .c_str()); |
| 164 | return false; |
| 165 | #endif |
| 166 | } |
| 167 | |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 168 | // Called at OCC Status change signal |
| 169 | void PassThrough::activeStatusEvent(sdbusplus::message::message& msg) |
| 170 | { |
| 171 | std::string statusInterface; |
Patrick Williams | e096270 | 2020-05-13 17:50:22 -0500 | [diff] [blame] | 172 | std::map<std::string, std::variant<bool>> msgData; |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 173 | msg.read(statusInterface, msgData); |
| 174 | |
| 175 | auto propertyMap = msgData.find("OccActive"); |
| 176 | if (propertyMap != msgData.end()) |
| 177 | { |
| 178 | // Extract the OccActive property |
Patrick Williams | 305ff8b | 2020-05-13 11:17:39 -0500 | [diff] [blame] | 179 | if (std::get<bool>(propertyMap->second)) |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 180 | { |
Eddie James | 4f4712d | 2018-06-21 15:57:02 -0500 | [diff] [blame] | 181 | occActive = true; |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 182 | } |
| 183 | else |
| 184 | { |
Eddie James | 4f4712d | 2018-06-21 15:57:02 -0500 | [diff] [blame] | 185 | occActive = false; |
Vishwanatha Subbanna | 3e5422e | 2017-08-10 18:25:26 +0530 | [diff] [blame] | 186 | } |
| 187 | } |
| 188 | return; |
| 189 | } |
| 190 | |
Deepak Kodihalli | 6b492fb | 2017-03-18 01:09:28 -0500 | [diff] [blame] | 191 | } // namespace occ |
| 192 | } // namespace open_power |