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