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