Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 1 | #include "occ_status.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 2 | |
Chris Cain | 1725767 | 2021-10-22 13:41:03 -0500 | [diff] [blame] | 3 | #include "occ_manager.hpp" |
Vishwanatha Subbanna | 6add0b8 | 2017-07-21 19:02:37 +0530 | [diff] [blame] | 4 | #include "occ_sensor.hpp" |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 5 | #include "powermode.hpp" |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 6 | #include "utils.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 7 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 8 | #include <fmt/core.h> |
| 9 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 10 | #ifdef POWER10 |
| 11 | #include <com/ibm/Host/Target/server.hpp> |
| 12 | #endif |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 13 | #include <phosphor-logging/log.hpp> |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 14 | |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 15 | #include <filesystem> |
| 16 | |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 17 | namespace open_power |
| 18 | { |
| 19 | namespace occ |
| 20 | { |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 21 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 22 | using namespace phosphor::logging; |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 23 | |
| 24 | // Handles updates to occActive property |
| 25 | bool Status::occActive(bool value) |
| 26 | { |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 27 | if (value != this->occActive()) |
| 28 | { |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 29 | log<level::INFO>(fmt::format("Status::occActive OCC{} changed to {}", |
| 30 | instance, value) |
| 31 | .c_str()); |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 32 | if (value) |
| 33 | { |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 34 | // Set the device active |
| 35 | device.setActive(true); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 36 | |
Edward A. James | 9fd2bdc | 2017-11-08 16:18:57 -0600 | [diff] [blame] | 37 | // Start watching for errors |
| 38 | addErrorWatch(); |
| 39 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 40 | // Reset last OCC state |
| 41 | lastState = 0; |
| 42 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 43 | if (device.master()) |
| 44 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 45 | // Update powercap bounds from OCC |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 46 | manager.updatePcapBounds(); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 47 | } |
| 48 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 49 | // Update the OCC active sensor before notifying Manager |
| 50 | Base::Status::occActive(value); |
| 51 | |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 52 | // Call into Manager to let know that we have bound |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 53 | if (this->managerCallBack) |
Vishwanatha Subbanna | 2dc9b1a | 2017-08-18 18:29:41 +0530 | [diff] [blame] | 54 | { |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 55 | this->managerCallBack(instance, value); |
Edward A. James | 9fd2bdc | 2017-11-08 16:18:57 -0600 | [diff] [blame] | 56 | } |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 57 | } |
| 58 | else |
| 59 | { |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 60 | #ifdef POWER10 |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 61 | if (pmode && device.master()) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 62 | { |
| 63 | // Prevent mode changes |
| 64 | pmode->setMasterActive(false); |
| 65 | } |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 66 | if (safeStateDelayTimer.isEnabled()) |
| 67 | { |
| 68 | // stop safe delay timer |
| 69 | safeStateDelayTimer.setEnabled(false); |
| 70 | } |
| 71 | #endif |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 72 | // Call into Manager to let know that we will unbind. |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 73 | if (this->managerCallBack) |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 74 | { |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 75 | this->managerCallBack(instance, value); |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 76 | } |
| 77 | |
Edward A. James | 9fd2bdc | 2017-11-08 16:18:57 -0600 | [diff] [blame] | 78 | // Stop watching for errors |
| 79 | removeErrorWatch(); |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 80 | |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 81 | // Set the device inactive |
| 82 | device.setActive(false); |
Vishwanatha Subbanna | 32e84e9 | 2017-06-28 19:17:28 +0530 | [diff] [blame] | 83 | } |
| 84 | } |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 85 | else if (value && !device.active()) |
Edward A. James | 5e17797 | 2017-10-25 15:50:31 -0500 | [diff] [blame] | 86 | { |
| 87 | // Existing error watch is on a dead file descriptor. |
Edward A. James | 9fd2bdc | 2017-11-08 16:18:57 -0600 | [diff] [blame] | 88 | removeErrorWatch(); |
Edward A. James | 5e17797 | 2017-10-25 15:50:31 -0500 | [diff] [blame] | 89 | |
| 90 | /* |
| 91 | * In it's constructor, Status checks Device::bound() to see if OCC is |
| 92 | * active or not. |
| 93 | * Device::bound() checks for occX-dev0 directory. |
| 94 | * We will lose occX-dev0 directories during FSI rescan. |
| 95 | * So, if we start this application (and construct Status), and then |
| 96 | * later do FSI rescan, we will end up with occActive = true and device |
| 97 | * NOT bound. Lets correct that situation here. |
| 98 | */ |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 99 | device.setActive(true); |
Edward A. James | 5e17797 | 2017-10-25 15:50:31 -0500 | [diff] [blame] | 100 | |
| 101 | // Add error watch again |
Edward A. James | 9fd2bdc | 2017-11-08 16:18:57 -0600 | [diff] [blame] | 102 | addErrorWatch(); |
Edward A. James | 5e17797 | 2017-10-25 15:50:31 -0500 | [diff] [blame] | 103 | } |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 104 | else if (!value && device.active()) |
Eddie James | 6d6d1b3 | 2019-04-22 10:45:08 -0500 | [diff] [blame] | 105 | { |
| 106 | removeErrorWatch(); |
| 107 | |
| 108 | // In the event that the application never receives the active signal |
| 109 | // even though the OCC is active (this can occur if the BMC is rebooted |
| 110 | // with the host on, since the initial OCC driver probe will discover |
| 111 | // the OCCs), this application needs to be able to unbind the device |
| 112 | // when we get the OCC inactive signal. |
Eddie James | aced309 | 2022-04-22 16:19:30 -0500 | [diff] [blame] | 113 | device.setActive(false); |
Eddie James | 6d6d1b3 | 2019-04-22 10:45:08 -0500 | [diff] [blame] | 114 | } |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 115 | return Base::Status::occActive(value); |
| 116 | } |
| 117 | |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 118 | // Callback handler when a device error is reported. |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 119 | void Status::deviceError() |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 120 | { |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 121 | #ifdef POWER10 |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 122 | if (pmode && device.master()) |
| 123 | { |
| 124 | // Prevent mode changes |
| 125 | pmode->setMasterActive(false); |
| 126 | } |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 127 | #endif |
| 128 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 129 | // This would deem OCC inactive |
| 130 | this->occActive(false); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 131 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 132 | // Reset the OCC |
| 133 | this->resetOCC(); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | // Sends message to host control command handler to reset OCC |
| 137 | void Status::resetOCC() |
| 138 | { |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 139 | log<level::INFO>( |
| 140 | fmt::format(">>Status::resetOCC() - requesting reset for OCC{}", |
| 141 | instance) |
| 142 | .c_str()); |
Tom Joseph | 0032523 | 2020-07-29 17:51:48 +0530 | [diff] [blame] | 143 | #ifdef PLDM |
| 144 | if (resetCallBack) |
| 145 | { |
| 146 | this->resetCallBack(instance); |
| 147 | } |
| 148 | #else |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 149 | constexpr auto CONTROL_HOST_PATH = "/org/open_power/control/host0"; |
| 150 | constexpr auto CONTROL_HOST_INTF = "org.open_power.Control.Host"; |
| 151 | |
| 152 | // This will throw exception on failure |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 153 | auto service = utils::getService(CONTROL_HOST_PATH, CONTROL_HOST_INTF); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 154 | |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 155 | auto& bus = utils::getBus(); |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 156 | auto method = bus.new_method_call(service.c_str(), CONTROL_HOST_PATH, |
| 157 | CONTROL_HOST_INTF, "Execute"); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 158 | // OCC Reset control command |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 159 | method.append(convertForMessage(Control::Host::Command::OCCReset).c_str()); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 160 | |
| 161 | // OCC Sensor ID for callout reasons |
Patrick Williams | e096270 | 2020-05-13 17:50:22 -0500 | [diff] [blame] | 162 | method.append(std::variant<uint8_t>(std::get<0>(sensorMap.at(instance)))); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 163 | bus.call_noreply(method); |
| 164 | return; |
Tom Joseph | 0032523 | 2020-07-29 17:51:48 +0530 | [diff] [blame] | 165 | #endif |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | // Handler called by Host control command handler to convey the |
| 169 | // status of the executed command |
| 170 | void Status::hostControlEvent(sdbusplus::message::message& msg) |
| 171 | { |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 172 | std::string cmdCompleted{}; |
| 173 | std::string cmdStatus{}; |
| 174 | |
| 175 | msg.read(cmdCompleted, cmdStatus); |
| 176 | |
| 177 | log<level::DEBUG>("Host control signal values", |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 178 | entry("COMMAND=%s", cmdCompleted.c_str()), |
| 179 | entry("STATUS=%s", cmdStatus.c_str())); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 180 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 181 | if (Control::Host::convertResultFromString(cmdStatus) != |
| 182 | Control::Host::Result::Success) |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 183 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 184 | if (Control::Host::convertCommandFromString(cmdCompleted) == |
| 185 | Control::Host::Command::OCCReset) |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 186 | { |
Gunnar Mills | 85e6520 | 2018-04-08 15:01:54 -0500 | [diff] [blame] | 187 | // Must be a Timeout. Log an Error trace |
Alexander Filippov | 1d69e19 | 2019-03-21 18:12:07 +0300 | [diff] [blame] | 188 | log<level::ERR>( |
| 189 | "Error resetting the OCC.", entry("PATH=%s", path.c_str()), |
| 190 | entry("SENSORID=0x%X", std::get<0>(sensorMap.at(instance)))); |
Vishwanatha Subbanna | 30e329a | 2017-07-24 23:13:14 +0530 | [diff] [blame] | 191 | } |
| 192 | } |
| 193 | return; |
Vishwanatha Subbanna | ee4d83d | 2017-06-29 18:35:00 +0530 | [diff] [blame] | 194 | } |
| 195 | |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 196 | // Called from Manager::pollerTimerExpired() in preperation to POLL OCC. |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 197 | void Status::readOccState() |
| 198 | { |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 199 | currentOccReadRetriesCount = occReadRetries; |
| 200 | occReadStateNow(); |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 201 | } |
| 202 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 203 | #ifdef POWER10 |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 204 | // Special processing that needs to happen once the OCCs change to ACTIVE state |
| 205 | void Status::occsWentActive() |
| 206 | { |
| 207 | CmdStatus status = CmdStatus::SUCCESS; |
| 208 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 209 | status = pmode->sendModeChange(); |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 210 | if (status != CmdStatus::SUCCESS) |
| 211 | { |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 212 | log<level::ERR>( |
| 213 | fmt::format( |
| 214 | "Status::occsWentActive: OCC mode change failed with status {}", |
| 215 | status) |
| 216 | .c_str()); |
Chris Cain | c567dc8 | 2022-04-01 15:09:17 -0500 | [diff] [blame] | 217 | |
| 218 | // Disable and reset to try recovering |
| 219 | deviceError(); |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 220 | } |
| 221 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 222 | status = pmode->sendIpsData(); |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 223 | if (status != CmdStatus::SUCCESS) |
| 224 | { |
| 225 | log<level::ERR>( |
| 226 | fmt::format( |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 227 | "Status::occsWentActive: Sending Idle Power Save Config data failed with status {}", |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 228 | status) |
| 229 | .c_str()); |
Chris Cain | c567dc8 | 2022-04-01 15:09:17 -0500 | [diff] [blame] | 230 | |
| 231 | if (status == CmdStatus::COMM_FAILURE) |
| 232 | { |
| 233 | // Disable and reset to try recovering |
| 234 | deviceError(); |
| 235 | } |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 236 | } |
| 237 | } |
| 238 | |
Chris Cain | 1725767 | 2021-10-22 13:41:03 -0500 | [diff] [blame] | 239 | // Send Ambient and Altitude to the OCC |
| 240 | CmdStatus Status::sendAmbient(const uint8_t inTemp, const uint16_t inAltitude) |
| 241 | { |
| 242 | CmdStatus status = CmdStatus::FAILURE; |
| 243 | bool ambientValid = true; |
| 244 | uint8_t ambientTemp = inTemp; |
| 245 | uint16_t altitude = inAltitude; |
| 246 | |
| 247 | if (ambientTemp == 0xFF) |
| 248 | { |
| 249 | // Get latest readings from manager |
| 250 | manager.getAmbientData(ambientValid, ambientTemp, altitude); |
| 251 | log<level::DEBUG>( |
| 252 | fmt::format("sendAmbient: valid: {}, Ambient: {}C, altitude: {}m", |
| 253 | ambientValid, ambientTemp, altitude) |
| 254 | .c_str()); |
| 255 | } |
| 256 | |
| 257 | std::vector<std::uint8_t> cmd, rsp; |
| 258 | cmd.reserve(11); |
| 259 | cmd.push_back(uint8_t(CmdType::SEND_AMBIENT)); |
| 260 | cmd.push_back(0x00); // Data Length (2 bytes) |
| 261 | cmd.push_back(0x08); // |
| 262 | cmd.push_back(0x00); // Version |
| 263 | cmd.push_back(ambientValid ? 0 : 0xFF); // Ambient Status |
| 264 | cmd.push_back(ambientTemp); // Ambient Temperature |
| 265 | cmd.push_back(altitude >> 8); // Altitude in meters (2 bytes) |
| 266 | cmd.push_back(altitude & 0xFF); // |
| 267 | cmd.push_back(0x00); // Reserved (3 bytes) |
| 268 | cmd.push_back(0x00); |
| 269 | cmd.push_back(0x00); |
| 270 | log<level::DEBUG>(fmt::format("sendAmbient: SEND_AMBIENT " |
| 271 | "command to OCC{} ({} bytes)", |
| 272 | instance, cmd.size()) |
| 273 | .c_str()); |
| 274 | status = occCmd.send(cmd, rsp); |
| 275 | if (status == CmdStatus::SUCCESS) |
| 276 | { |
| 277 | if (rsp.size() == 5) |
| 278 | { |
| 279 | if (RspStatus::SUCCESS != RspStatus(rsp[2])) |
| 280 | { |
| 281 | log<level::ERR>( |
| 282 | fmt::format( |
Chris Cain | c567dc8 | 2022-04-01 15:09:17 -0500 | [diff] [blame] | 283 | "sendAmbient: SEND_AMBIENT failed with rspStatus 0x{:02X}", |
Chris Cain | 1725767 | 2021-10-22 13:41:03 -0500 | [diff] [blame] | 284 | rsp[2]) |
| 285 | .c_str()); |
| 286 | dump_hex(rsp); |
| 287 | status = CmdStatus::FAILURE; |
| 288 | } |
| 289 | } |
| 290 | else |
| 291 | { |
Chris Cain | c567dc8 | 2022-04-01 15:09:17 -0500 | [diff] [blame] | 292 | log<level::ERR>( |
| 293 | fmt::format( |
| 294 | "sendAmbient: INVALID SEND_AMBIENT response length:{}", |
| 295 | rsp.size()) |
| 296 | .c_str()); |
Chris Cain | 1725767 | 2021-10-22 13:41:03 -0500 | [diff] [blame] | 297 | dump_hex(rsp); |
| 298 | status = CmdStatus::FAILURE; |
| 299 | } |
| 300 | } |
| 301 | else |
| 302 | { |
Chris Cain | c567dc8 | 2022-04-01 15:09:17 -0500 | [diff] [blame] | 303 | log<level::ERR>( |
| 304 | fmt::format( |
| 305 | "sendAmbient: SEND_AMBIENT FAILED! with status 0x{:02X}", |
| 306 | status) |
| 307 | .c_str()); |
| 308 | |
| 309 | if (status == CmdStatus::COMM_FAILURE) |
Chris Cain | 1725767 | 2021-10-22 13:41:03 -0500 | [diff] [blame] | 310 | { |
Chris Cain | c567dc8 | 2022-04-01 15:09:17 -0500 | [diff] [blame] | 311 | // Disable and reset to try recovering |
| 312 | deviceError(); |
Chris Cain | 1725767 | 2021-10-22 13:41:03 -0500 | [diff] [blame] | 313 | } |
| 314 | } |
| 315 | |
| 316 | return status; |
| 317 | } |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 318 | |
| 319 | // Called when safe timer expires to determine if OCCs need to be reset |
| 320 | void Status::safeStateDelayExpired() |
| 321 | { |
| 322 | if (this->occActive()) |
| 323 | { |
| 324 | log<level::INFO>( |
| 325 | fmt::format( |
| 326 | "safeStateDelayExpired: OCC{} is in SAFE state, requesting reset", |
| 327 | instance) |
| 328 | .c_str()); |
| 329 | // Disable and reset to try recovering |
| 330 | deviceError(); |
| 331 | } |
| 332 | } |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 333 | #endif // POWER10 |
| 334 | |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 335 | fs::path Status::getHwmonPath() |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 336 | { |
| 337 | using namespace std::literals::string_literals; |
| 338 | |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 339 | if (!fs::exists(hwmonPath)) |
| 340 | { |
| 341 | static bool tracedFail[8] = {0}; |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 342 | |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 343 | if (!hwmonPath.empty()) |
| 344 | { |
| 345 | log<level::ERR>( |
| 346 | fmt::format("Status::getHwmonPath(): path no longer exists: {}", |
| 347 | hwmonPath.c_str()) |
| 348 | .c_str()); |
| 349 | hwmonPath.clear(); |
| 350 | } |
| 351 | |
| 352 | // Build the base HWMON path |
| 353 | fs::path prefixPath = |
| 354 | fs::path{OCC_HWMON_PATH + "occ-hwmon."s + |
| 355 | std::to_string(instance + 1) + "/hwmon/"s}; |
| 356 | |
| 357 | // Get the hwmonXX directory name |
| 358 | try |
| 359 | { |
| 360 | // there should only be one directory |
| 361 | const int numDirs = std::distance( |
| 362 | fs::directory_iterator(prefixPath), fs::directory_iterator{}); |
| 363 | if (numDirs == 1) |
| 364 | { |
| 365 | hwmonPath = *fs::directory_iterator(prefixPath); |
| 366 | tracedFail[instance] = false; |
| 367 | } |
| 368 | else |
| 369 | { |
| 370 | if (!tracedFail[instance]) |
| 371 | { |
| 372 | log<level::ERR>( |
| 373 | fmt::format( |
| 374 | "Status::getHwmonPath(): Found multiple ({}) hwmon paths!", |
| 375 | numDirs) |
| 376 | .c_str()); |
| 377 | tracedFail[instance] = true; |
| 378 | } |
| 379 | } |
| 380 | } |
| 381 | catch (const fs::filesystem_error& e) |
| 382 | { |
| 383 | if (!tracedFail[instance]) |
| 384 | { |
| 385 | log<level::ERR>( |
| 386 | fmt::format( |
| 387 | "Status::getHwmonPath(): error accessing {}: {}", |
| 388 | prefixPath.c_str(), e.what()) |
| 389 | .c_str()); |
| 390 | tracedFail[instance] = true; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
| 395 | return hwmonPath; |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 396 | } |
| 397 | |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 398 | // Called to read state and upon failure to read after occReadStateFailTimer. |
| 399 | void Status::occReadStateNow() |
| 400 | { |
| 401 | unsigned int state; |
| 402 | const fs::path filename = |
| 403 | fs::path(DEV_PATH) / |
| 404 | fs::path(sysfsName + "." + std::to_string(instance + 1)) / "occ_state"; |
| 405 | |
| 406 | std::ifstream file; |
| 407 | bool goodFile = false; |
| 408 | |
| 409 | // open file. |
| 410 | file.open(filename, std::ios::in); |
| 411 | const int openErrno = errno; |
| 412 | |
| 413 | // File is open and state can be used. |
| 414 | if (file.is_open() && file.good()) |
| 415 | { |
| 416 | goodFile = true; |
| 417 | file >> state; |
| 418 | |
| 419 | if (state != lastState) |
| 420 | { |
| 421 | // Trace OCC state changes |
| 422 | log<level::INFO>( |
Chris Cain | bd551de | 2022-04-26 13:41:16 -0500 | [diff] [blame^] | 423 | fmt::format( |
| 424 | "Status::readOccState: OCC{} state 0x{:02X} (lastState: 0x{:02X})", |
| 425 | instance, state, lastState) |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 426 | .c_str()); |
| 427 | lastState = state; |
| 428 | #ifdef POWER10 |
| 429 | if (OccState(state) == OccState::ACTIVE) |
| 430 | { |
| 431 | if (pmode && device.master()) |
| 432 | { |
| 433 | // Set the master OCC on the PowerMode object |
| 434 | pmode->setMasterOcc(path); |
| 435 | // Enable mode changes |
| 436 | pmode->setMasterActive(); |
| 437 | |
| 438 | // Special processing by master OCC when it goes active |
| 439 | occsWentActive(); |
| 440 | } |
| 441 | |
| 442 | CmdStatus status = sendAmbient(); |
| 443 | if (status != CmdStatus::SUCCESS) |
| 444 | { |
| 445 | log<level::ERR>( |
| 446 | fmt::format( |
| 447 | "readOccState: Sending Ambient failed with status {}", |
| 448 | status) |
| 449 | .c_str()); |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | // If OCC in known Good State. |
| 454 | if ((OccState(state) == OccState::ACTIVE) || |
| 455 | (OccState(state) == OccState::CHARACTERIZATION) || |
| 456 | (OccState(state) == OccState::OBSERVATION)) |
| 457 | { |
| 458 | // Good OCC State then sensors valid again |
| 459 | stateValid = true; |
| 460 | |
| 461 | if (safeStateDelayTimer.isEnabled()) |
| 462 | { |
| 463 | // stop safe delay timer (no longer in SAFE state) |
| 464 | safeStateDelayTimer.setEnabled(false); |
| 465 | } |
| 466 | } |
| 467 | // Else not Valid state We would be in SAFE mode. |
| 468 | // This captures both SAFE mode, and 0x00, or other invalid |
| 469 | // state values. |
| 470 | else |
| 471 | { |
| 472 | if (!safeStateDelayTimer.isEnabled()) |
| 473 | { |
| 474 | // start safe delay timer (before requesting reset) |
| 475 | using namespace std::literals::chrono_literals; |
| 476 | safeStateDelayTimer.restartOnce(60s); |
| 477 | } |
| 478 | // Not valid state, update sensors to Nan & not functional. |
| 479 | stateValid = false; |
| 480 | } |
| 481 | #else |
| 482 | // Before P10 state not checked, only used good file open. |
| 483 | stateValid = true; |
| 484 | #endif |
| 485 | } |
| 486 | } |
| 487 | file.close(); |
| 488 | |
| 489 | // if failed to Read a state or not a valid state -> Attempt retry |
| 490 | // after 1 Second delay if allowed. |
| 491 | if ((!goodFile) || (!stateValid)) |
| 492 | { |
| 493 | if (!goodFile) |
| 494 | { |
| 495 | // If not able to read, OCC may be offline |
| 496 | log<level::ERR>( |
| 497 | fmt::format("Status::readOccState: open failed (errno={})", |
| 498 | openErrno) |
| 499 | .c_str()); |
| 500 | } |
| 501 | else |
| 502 | { |
| 503 | // else this failed due to state not valid. |
Chris Cain | bd551de | 2022-04-26 13:41:16 -0500 | [diff] [blame^] | 504 | if (state != lastState) |
| 505 | { |
| 506 | log<level::ERR>( |
| 507 | fmt::format( |
| 508 | "Status::readOccState: OCC{} Invalid state 0x{:02X} (last state: 0x{:02X})", |
| 509 | instance, state, lastState) |
| 510 | .c_str()); |
| 511 | } |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 512 | } |
| 513 | |
| 514 | #ifdef READ_OCC_SENSORS |
| 515 | manager.setSensorValueToNonFunctional(instance); |
| 516 | #endif |
| 517 | |
| 518 | // See occReadRetries for number of retry attempts. |
| 519 | if (currentOccReadRetriesCount > 0) |
| 520 | { |
| 521 | --currentOccReadRetriesCount; |
| 522 | #ifdef POWER10 |
| 523 | using namespace std::chrono_literals; |
| 524 | occReadStateFailTimer.restartOnce(1s); |
| 525 | #endif |
| 526 | } |
| 527 | else |
| 528 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 529 | #ifdef POWER10 |
| 530 | if (!stateValid && occActive()) |
| 531 | { |
| 532 | if (!safeStateDelayTimer.isEnabled()) |
| 533 | { |
| 534 | log<level::ERR>( |
| 535 | "Starting 60 sec delay timer before requesting a reset"); |
| 536 | // start safe delay timer (before requesting reset) |
| 537 | using namespace std::literals::chrono_literals; |
| 538 | safeStateDelayTimer.restartOnce(60s); |
| 539 | } |
| 540 | } |
| 541 | #else |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 542 | // State could not be determined, set it to NO State. |
| 543 | lastState = 0; |
| 544 | |
| 545 | // Disable the ability to send Failed actions until OCC is |
| 546 | // Active again. |
| 547 | stateValid = false; |
| 548 | |
| 549 | // Disable and reset to try recovering |
| 550 | deviceError(); |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 551 | #endif |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 552 | } |
| 553 | } |
| 554 | } |
| 555 | |
Vishwanatha Subbanna | 307d80b | 2017-06-28 15:56:09 +0530 | [diff] [blame] | 556 | } // namespace occ |
| 557 | } // namespace open_power |