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