Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 3 | #include "occ_manager.hpp" |
| 4 | |
| 5 | #include "i2c_occ.hpp" |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 6 | #include "occ_dbus.hpp" |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 7 | #include "utils.hpp" |
| 8 | |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 9 | #include <phosphor-logging/elog-errors.hpp> |
| 10 | #include <phosphor-logging/log.hpp> |
| 11 | #include <xyz/openbmc_project/Common/error.hpp> |
| 12 | |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame] | 13 | #include <chrono> |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 14 | #include <cmath> |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 15 | #include <filesystem> |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 16 | #include <fstream> |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 17 | #include <regex> |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 18 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 19 | namespace open_power |
| 20 | { |
| 21 | namespace occ |
| 22 | { |
| 23 | |
Matt Spinler | 8b8abee | 2021-08-25 15:18:21 -0500 | [diff] [blame] | 24 | constexpr uint32_t fruTypeNotAvailable = 0xFF; |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 25 | constexpr auto fruTypeSuffix = "fru_type"; |
| 26 | constexpr auto faultSuffix = "fault"; |
| 27 | constexpr auto inputSuffix = "input"; |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame] | 28 | constexpr auto maxSuffix = "max"; |
Matt Spinler | 8b8abee | 2021-08-25 15:18:21 -0500 | [diff] [blame] | 29 | |
Chris Cain | 1718fd8 | 2022-02-16 16:39:50 -0600 | [diff] [blame] | 30 | const auto HOST_ON_FILE = "/run/openbmc/host@0-on"; |
| 31 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 32 | using namespace phosphor::logging; |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 33 | using namespace std::literals::chrono_literals; |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 34 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 35 | template <typename T> |
| 36 | T readFile(const std::string& path) |
| 37 | { |
| 38 | std::ifstream ifs; |
| 39 | ifs.exceptions(std::ifstream::failbit | std::ifstream::badbit | |
| 40 | std::ifstream::eofbit); |
| 41 | T data; |
| 42 | |
| 43 | try |
| 44 | { |
| 45 | ifs.open(path); |
| 46 | ifs >> data; |
| 47 | ifs.close(); |
| 48 | } |
| 49 | catch (const std::exception& e) |
| 50 | { |
| 51 | auto err = errno; |
| 52 | throw std::system_error(err, std::generic_category()); |
| 53 | } |
| 54 | |
| 55 | return data; |
| 56 | } |
| 57 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 58 | void Manager::findAndCreateObjects() |
| 59 | { |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame] | 60 | #ifndef POWER10 |
Deepak Kodihalli | 370f06b | 2017-10-25 04:26:07 -0500 | [diff] [blame] | 61 | for (auto id = 0; id < MAX_CPUS; ++id) |
| 62 | { |
Deepak Kodihalli | 30417a1 | 2017-12-04 00:54:01 -0600 | [diff] [blame] | 63 | // Create one occ per cpu |
| 64 | auto occ = std::string(OCC_NAME) + std::to_string(id); |
| 65 | createObjects(occ); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 66 | } |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame] | 67 | #else |
Chris Cain | 1718fd8 | 2022-02-16 16:39:50 -0600 | [diff] [blame] | 68 | if (!fs::exists(HOST_ON_FILE)) |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame] | 69 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 70 | static bool statusObjCreated = false; |
| 71 | if (!statusObjCreated) |
Chris Cain | 1718fd8 | 2022-02-16 16:39:50 -0600 | [diff] [blame] | 72 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 73 | // Create the OCCs based on on the /dev/occX devices |
| 74 | auto occs = findOCCsInDev(); |
Chris Cain | 1718fd8 | 2022-02-16 16:39:50 -0600 | [diff] [blame] | 75 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 76 | if (occs.empty() || (prevOCCSearch.size() != occs.size())) |
Chris Cain | 1718fd8 | 2022-02-16 16:39:50 -0600 | [diff] [blame] | 77 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 78 | // Something changed or no OCCs yet, try again in 10s. |
| 79 | // Note on the first pass prevOCCSearch will be empty, |
| 80 | // so there will be at least one delay to give things |
| 81 | // a chance to settle. |
| 82 | prevOCCSearch = occs; |
| 83 | |
| 84 | log<level::INFO>( |
| 85 | fmt::format( |
| 86 | "Manager::findAndCreateObjects(): Waiting for OCCs (currently {})", |
| 87 | occs.size()) |
| 88 | .c_str()); |
| 89 | |
| 90 | discoverTimer->restartOnce(10s); |
| 91 | } |
| 92 | else |
| 93 | { |
| 94 | // All OCCs appear to be available, create status objects |
| 95 | |
| 96 | // createObjects requires OCC0 first. |
| 97 | std::sort(occs.begin(), occs.end()); |
| 98 | |
| 99 | log<level::INFO>( |
| 100 | fmt::format( |
| 101 | "Manager::findAndCreateObjects(): Creating {} OCC Status Objects", |
| 102 | occs.size()) |
| 103 | .c_str()); |
| 104 | for (auto id : occs) |
| 105 | { |
| 106 | createObjects(std::string(OCC_NAME) + std::to_string(id)); |
| 107 | } |
| 108 | statusObjCreated = true; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | if (statusObjCreated) |
| 113 | { |
| 114 | static bool tracedHostWait = false; |
| 115 | if (utils::isHostRunning()) |
| 116 | { |
| 117 | if (tracedHostWait) |
| 118 | { |
| 119 | log<level::INFO>( |
| 120 | "Manager::findAndCreateObjects(): Host is running"); |
| 121 | tracedHostWait = false; |
| 122 | } |
| 123 | waitingForAllOccActiveSensors = true; |
| 124 | checkAllActiveSensors(); |
| 125 | } |
| 126 | else |
| 127 | { |
| 128 | if (!tracedHostWait) |
| 129 | { |
| 130 | log<level::INFO>( |
| 131 | "Manager::findAndCreateObjects(): Waiting for host to start"); |
| 132 | tracedHostWait = true; |
| 133 | } |
| 134 | discoverTimer->restartOnce(30s); |
Chris Cain | 1718fd8 | 2022-02-16 16:39:50 -0600 | [diff] [blame] | 135 | } |
| 136 | } |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame] | 137 | } |
| 138 | else |
| 139 | { |
Chris Cain | 1718fd8 | 2022-02-16 16:39:50 -0600 | [diff] [blame] | 140 | log<level::INFO>( |
| 141 | fmt::format( |
| 142 | "Manager::findAndCreateObjects(): Waiting for {} to complete...", |
| 143 | HOST_ON_FILE) |
| 144 | .c_str()); |
| 145 | discoverTimer->restartOnce(10s); |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame] | 146 | } |
| 147 | #endif |
| 148 | } |
| 149 | |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 150 | #ifdef POWER10 |
| 151 | // Check if all occActive sensors are available |
| 152 | void Manager::checkAllActiveSensors() |
| 153 | { |
| 154 | static bool allActiveSensorAvailable = false; |
| 155 | static bool tracedSensorWait = false; |
| 156 | |
| 157 | // Start with the assumption that all are available |
| 158 | allActiveSensorAvailable = true; |
| 159 | for (auto& obj : statusObjects) |
| 160 | { |
| 161 | // If active sensor is already true, then no need to query sensor |
| 162 | if (!obj->occActive()) |
| 163 | { |
| 164 | allActiveSensorAvailable = false; |
| 165 | if (!tracedSensorWait) |
| 166 | { |
| 167 | log<level::INFO>( |
| 168 | fmt::format( |
| 169 | "Manager::checkAllActiveSensors(): Waiting on OCC{} Active sensor", |
| 170 | obj->getOccInstanceID()) |
| 171 | .c_str()); |
| 172 | tracedSensorWait = true; |
| 173 | } |
| 174 | pldmHandle->checkActiveSensor(obj->getOccInstanceID()); |
| 175 | break; |
| 176 | } |
| 177 | } |
| 178 | |
| 179 | if (allActiveSensorAvailable) |
| 180 | { |
| 181 | // All sensors were found, disable the discovery timer |
| 182 | discoverTimer.reset(); |
| 183 | waitingForAllOccActiveSensors = false; |
| 184 | |
| 185 | log<level::INFO>( |
| 186 | "Manager::checkAllActiveSensors(): OCC Active sensors are available"); |
| 187 | tracedSensorWait = false; |
| 188 | } |
| 189 | else |
| 190 | { |
| 191 | // Not all sensors were available, so keep waiting |
| 192 | if (!tracedSensorWait) |
| 193 | { |
| 194 | log<level::INFO>( |
| 195 | "Manager::checkAllActiveSensors(): Waiting for OCC Active sensors to become available"); |
| 196 | tracedSensorWait = true; |
| 197 | } |
| 198 | discoverTimer->restartOnce(30s); |
| 199 | } |
| 200 | } |
| 201 | #endif |
| 202 | |
Matt Spinler | d267cec | 2021-09-01 14:49:19 -0500 | [diff] [blame] | 203 | std::vector<int> Manager::findOCCsInDev() |
| 204 | { |
| 205 | std::vector<int> occs; |
| 206 | std::regex expr{R"(occ(\d+)$)"}; |
| 207 | |
| 208 | for (auto& file : fs::directory_iterator("/dev")) |
| 209 | { |
| 210 | std::smatch match; |
| 211 | std::string path{file.path().string()}; |
| 212 | if (std::regex_search(path, match, expr)) |
| 213 | { |
| 214 | auto num = std::stoi(match[1].str()); |
| 215 | |
| 216 | // /dev numbering starts at 1, ours starts at 0. |
| 217 | occs.push_back(num - 1); |
| 218 | } |
| 219 | } |
| 220 | |
| 221 | return occs; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | int Manager::cpuCreated(sdbusplus::message::message& msg) |
| 225 | { |
George Liu | bcef3b4 | 2021-09-10 12:39:02 +0800 | [diff] [blame] | 226 | namespace fs = std::filesystem; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 227 | |
| 228 | sdbusplus::message::object_path o; |
| 229 | msg.read(o); |
| 230 | fs::path cpuPath(std::string(std::move(o))); |
| 231 | |
| 232 | auto name = cpuPath.filename().string(); |
| 233 | auto index = name.find(CPU_NAME); |
| 234 | name.replace(index, std::strlen(CPU_NAME), OCC_NAME); |
| 235 | |
| 236 | createObjects(name); |
| 237 | |
| 238 | return 0; |
| 239 | } |
| 240 | |
| 241 | void Manager::createObjects(const std::string& occ) |
| 242 | { |
| 243 | auto path = fs::path(OCC_CONTROL_ROOT) / occ; |
| 244 | |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 245 | #ifdef POWER10 |
| 246 | if (!pmode) |
| 247 | { |
Chris Cain | 1be4337 | 2021-12-09 19:29:37 -0600 | [diff] [blame] | 248 | // Create the power mode object |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 249 | pmode = std::make_unique<powermode::PowerMode>( |
Sheldon Bailey | ea2b22e | 2022-04-04 12:24:46 -0500 | [diff] [blame] | 250 | *this, powermode::PMODE_PATH, powermode::PIPS_PATH |
| 251 | #ifdef POWER10 |
| 252 | , |
| 253 | event |
| 254 | #endif |
| 255 | ); |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 256 | } |
| 257 | #endif |
| 258 | |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 259 | statusObjects.emplace_back(std::make_unique<Status>( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 260 | event, path.c_str(), *this, |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 261 | #ifdef POWER10 |
| 262 | pmode, |
| 263 | #endif |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 264 | std::bind(std::mem_fn(&Manager::statusCallBack), this, |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 265 | std::placeholders::_1, std::placeholders::_2) |
Tom Joseph | 0032523 | 2020-07-29 17:51:48 +0530 | [diff] [blame] | 266 | #ifdef PLDM |
| 267 | , |
| 268 | std::bind(std::mem_fn(&pldm::Interface::resetOCC), pldmHandle.get(), |
| 269 | std::placeholders::_1) |
| 270 | #endif |
| 271 | )); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 272 | |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 273 | // Create the power cap monitor object |
| 274 | if (!pcap) |
| 275 | { |
| 276 | pcap = std::make_unique<open_power::occ::powercap::PowerCap>( |
| 277 | *statusObjects.back()); |
| 278 | } |
| 279 | |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 280 | if (statusObjects.back()->isMasterOcc()) |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 281 | { |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 282 | log<level::INFO>( |
| 283 | fmt::format("Manager::createObjects(): OCC{} is the master", |
| 284 | statusObjects.back()->getOccInstanceID()) |
| 285 | .c_str()); |
| 286 | _pollTimer->setEnabled(false); |
| 287 | |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 288 | #ifdef POWER10 |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 289 | // Set the master OCC on the PowerMode object |
| 290 | pmode->setMasterOcc(path); |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 291 | #endif |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | passThroughObjects.emplace_back(std::make_unique<PassThrough>(path.c_str() |
| 295 | #ifdef POWER10 |
| 296 | , |
| 297 | pmode |
| 298 | #endif |
| 299 | )); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 300 | } |
| 301 | |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 302 | void Manager::statusCallBack(instanceID instance, bool status) |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 303 | { |
Gunnar Mills | 94df8c9 | 2018-09-14 14:50:03 -0500 | [diff] [blame] | 304 | using InternalFailure = |
| 305 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 306 | |
| 307 | // At this time, it won't happen but keeping it |
| 308 | // here just in case something changes in the future |
| 309 | if ((activeCount == 0) && (!status)) |
| 310 | { |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 311 | log<level::ERR>( |
| 312 | fmt::format("Invalid update on OCCActive with OCC{}", instance) |
| 313 | .c_str()); |
| 314 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 315 | elog<InternalFailure>(); |
| 316 | } |
| 317 | |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 318 | if (status == true) |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 319 | { |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 320 | // OCC went active |
| 321 | ++activeCount; |
| 322 | |
| 323 | #ifdef POWER10 |
| 324 | if (activeCount == 1) |
Eddie James | dae2d94 | 2017-12-20 10:50:03 -0600 | [diff] [blame] | 325 | { |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 326 | // First OCC went active (allow some time for all OCCs to go active) |
| 327 | waitForAllOccsTimer->restartOnce(30s); |
Matt Spinler | 53f6814 | 2021-08-25 15:47:31 -0500 | [diff] [blame] | 328 | } |
| 329 | #endif |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 330 | |
| 331 | if (activeCount == statusObjects.size()) |
| 332 | { |
| 333 | #ifdef POWER10 |
| 334 | // All OCCs are now running |
| 335 | if (waitForAllOccsTimer->isEnabled()) |
| 336 | { |
| 337 | // stop occ wait timer |
| 338 | waitForAllOccsTimer->setEnabled(false); |
| 339 | } |
| 340 | #endif |
| 341 | |
| 342 | // Verify master OCC and start presence monitor |
| 343 | validateOccMaster(); |
| 344 | } |
| 345 | |
| 346 | // Start poll timer if not already started |
| 347 | if (!_pollTimer->isEnabled()) |
| 348 | { |
| 349 | log<level::INFO>( |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 350 | fmt::format("Manager: OCCs will be polled every {} seconds", |
| 351 | pollInterval) |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 352 | .c_str()); |
| 353 | |
| 354 | // Send poll and start OCC poll timer |
| 355 | pollerTimerExpired(); |
| 356 | } |
| 357 | } |
| 358 | else |
| 359 | { |
| 360 | // OCC went away |
| 361 | --activeCount; |
| 362 | |
| 363 | if (activeCount == 0) |
| 364 | { |
| 365 | // No OCCs are running |
| 366 | |
| 367 | // Stop OCC poll timer |
| 368 | if (_pollTimer->isEnabled()) |
| 369 | { |
| 370 | log<level::INFO>( |
| 371 | "Manager::statusCallBack(): OCCs are not running, stopping poll timer"); |
| 372 | _pollTimer->setEnabled(false); |
| 373 | } |
| 374 | |
| 375 | #ifdef POWER10 |
| 376 | // stop wait timer |
| 377 | if (waitForAllOccsTimer->isEnabled()) |
| 378 | { |
| 379 | waitForAllOccsTimer->setEnabled(false); |
| 380 | } |
| 381 | #endif |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 382 | } |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 383 | #ifdef READ_OCC_SENSORS |
| 384 | // Clear OCC sensors |
| 385 | setSensorValueToNonFunctional(instance); |
| 386 | #endif |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 387 | } |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 388 | |
| 389 | #ifdef POWER10 |
| 390 | if (waitingForAllOccActiveSensors) |
| 391 | { |
| 392 | checkAllActiveSensors(); |
| 393 | } |
| 394 | #endif |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | #ifdef I2C_OCC |
| 398 | void Manager::initStatusObjects() |
| 399 | { |
| 400 | // Make sure we have a valid path string |
| 401 | static_assert(sizeof(DEV_PATH) != 0); |
| 402 | |
| 403 | auto deviceNames = i2c_occ::getOccHwmonDevices(DEV_PATH); |
| 404 | for (auto& name : deviceNames) |
| 405 | { |
| 406 | i2c_occ::i2cToDbus(name); |
Lei YU | b5259a1 | 2017-09-01 16:22:40 +0800 | [diff] [blame] | 407 | name = std::string(OCC_NAME) + '_' + name; |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 408 | auto path = fs::path(OCC_CONTROL_ROOT) / name; |
| 409 | statusObjects.emplace_back( |
George Liu | f3b7514 | 2021-06-10 11:22:50 +0800 | [diff] [blame] | 410 | std::make_unique<Status>(event, path.c_str(), *this)); |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 411 | } |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 412 | // The first device is master occ |
| 413 | pcap = std::make_unique<open_power::occ::powercap::PowerCap>( |
| 414 | *statusObjects.front()); |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 415 | #ifdef POWER10 |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 416 | pmode = std::make_unique<powermode::PowerMode>(*this, powermode::PMODE_PATH, |
| 417 | powermode::PIPS_PATH); |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 418 | // Set the master OCC on the PowerMode object |
| 419 | pmode->setMasterOcc(path); |
Chris Cain | 78e8601 | 2021-03-04 16:15:31 -0600 | [diff] [blame] | 420 | #endif |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 421 | } |
| 422 | #endif |
| 423 | |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 424 | #ifdef PLDM |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 425 | void Manager::sbeTimeout(unsigned int instance) |
| 426 | { |
Eddie James | 2a751d7 | 2022-03-04 09:16:12 -0600 | [diff] [blame] | 427 | auto obj = std::find_if(statusObjects.begin(), statusObjects.end(), |
| 428 | [instance](const auto& obj) { |
| 429 | return instance == obj->getOccInstanceID(); |
| 430 | }); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 431 | |
Eddie James | cb018da | 2022-03-05 11:49:37 -0600 | [diff] [blame] | 432 | if (obj != statusObjects.end() && (*obj)->occActive()) |
Eddie James | 2a751d7 | 2022-03-04 09:16:12 -0600 | [diff] [blame] | 433 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 434 | log<level::INFO>( |
| 435 | fmt::format("SBE timeout, requesting HRESET (OCC{})", instance) |
| 436 | .c_str()); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 437 | |
Eddie James | 2a751d7 | 2022-03-04 09:16:12 -0600 | [diff] [blame] | 438 | setSBEState(instance, SBE_STATE_NOT_USABLE); |
| 439 | |
| 440 | pldmHandle->sendHRESET(instance); |
| 441 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 442 | } |
| 443 | |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 444 | bool Manager::updateOCCActive(instanceID instance, bool status) |
| 445 | { |
Chris Cain | 7e374fb | 2022-04-07 09:47:23 -0500 | [diff] [blame] | 446 | auto obj = std::find_if(statusObjects.begin(), statusObjects.end(), |
| 447 | [instance](const auto& obj) { |
| 448 | return instance == obj->getOccInstanceID(); |
| 449 | }); |
| 450 | |
| 451 | if (obj != statusObjects.end()) |
| 452 | { |
| 453 | return (*obj)->occActive(status); |
| 454 | } |
| 455 | else |
| 456 | { |
| 457 | log<level::WARNING>( |
| 458 | fmt::format( |
| 459 | "Manager::updateOCCActive: No status object to update for OCC{} (active={})", |
| 460 | instance, status) |
| 461 | .c_str()); |
| 462 | return false; |
| 463 | } |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 464 | } |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 465 | |
| 466 | void Manager::sbeHRESETResult(instanceID instance, bool success) |
| 467 | { |
| 468 | if (success) |
| 469 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 470 | log<level::INFO>( |
| 471 | fmt::format("HRESET succeeded (OCC{})", instance).c_str()); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 472 | |
| 473 | setSBEState(instance, SBE_STATE_BOOTED); |
| 474 | |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | setSBEState(instance, SBE_STATE_FAILED); |
| 479 | |
| 480 | if (sbeCanDump(instance)) |
| 481 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 482 | log<level::INFO>( |
| 483 | fmt::format("HRESET failed (OCC{}), triggering SBE dump", instance) |
| 484 | .c_str()); |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 485 | |
| 486 | auto& bus = utils::getBus(); |
| 487 | uint32_t src6 = instance << 16; |
| 488 | uint32_t logId = |
| 489 | FFDC::createPEL("org.open_power.Processor.Error.SbeChipOpTimeout", |
| 490 | src6, "SBE command timeout"); |
| 491 | |
| 492 | try |
| 493 | { |
George Liu | f3a4a69 | 2021-12-28 13:59:51 +0800 | [diff] [blame] | 494 | constexpr auto path = "/org/openpower/dump"; |
| 495 | constexpr auto interface = "xyz.openbmc_project.Dump.Create"; |
| 496 | constexpr auto function = "CreateDump"; |
| 497 | |
Eddie James | cbad219 | 2021-10-07 09:39:39 -0500 | [diff] [blame] | 498 | std::string service = utils::getService(path, interface); |
| 499 | auto method = |
| 500 | bus.new_method_call(service.c_str(), path, interface, function); |
| 501 | |
| 502 | std::map<std::string, std::variant<std::string, uint64_t>> |
| 503 | createParams{ |
| 504 | {"com.ibm.Dump.Create.CreateParameters.ErrorLogId", |
| 505 | uint64_t(logId)}, |
| 506 | {"com.ibm.Dump.Create.CreateParameters.DumpType", |
| 507 | "com.ibm.Dump.Create.DumpType.SBE"}, |
| 508 | {"com.ibm.Dump.Create.CreateParameters.FailingUnitId", |
| 509 | uint64_t(instance)}, |
| 510 | }; |
| 511 | |
| 512 | method.append(createParams); |
| 513 | |
| 514 | auto response = bus.call(method); |
| 515 | } |
| 516 | catch (const sdbusplus::exception::exception& e) |
| 517 | { |
| 518 | constexpr auto ERROR_DUMP_DISABLED = |
| 519 | "xyz.openbmc_project.Dump.Create.Error.Disabled"; |
| 520 | if (e.name() == ERROR_DUMP_DISABLED) |
| 521 | { |
| 522 | log<level::INFO>("Dump is disabled, skipping"); |
| 523 | } |
| 524 | else |
| 525 | { |
| 526 | log<level::ERR>("Dump failed"); |
| 527 | } |
| 528 | } |
| 529 | } |
| 530 | } |
| 531 | |
| 532 | bool Manager::sbeCanDump(unsigned int instance) |
| 533 | { |
| 534 | struct pdbg_target* proc = getPdbgTarget(instance); |
| 535 | |
| 536 | if (!proc) |
| 537 | { |
| 538 | // allow the dump in the error case |
| 539 | return true; |
| 540 | } |
| 541 | |
| 542 | try |
| 543 | { |
| 544 | if (!openpower::phal::sbe::isDumpAllowed(proc)) |
| 545 | { |
| 546 | return false; |
| 547 | } |
| 548 | |
| 549 | if (openpower::phal::pdbg::isSbeVitalAttnActive(proc)) |
| 550 | { |
| 551 | return false; |
| 552 | } |
| 553 | } |
| 554 | catch (openpower::phal::exception::SbeError& e) |
| 555 | { |
| 556 | log<level::INFO>("Failed to query SBE state"); |
| 557 | } |
| 558 | |
| 559 | // allow the dump in the error case |
| 560 | return true; |
| 561 | } |
| 562 | |
| 563 | void Manager::setSBEState(unsigned int instance, enum sbe_state state) |
| 564 | { |
| 565 | struct pdbg_target* proc = getPdbgTarget(instance); |
| 566 | |
| 567 | if (!proc) |
| 568 | { |
| 569 | return; |
| 570 | } |
| 571 | |
| 572 | try |
| 573 | { |
| 574 | openpower::phal::sbe::setState(proc, state); |
| 575 | } |
| 576 | catch (const openpower::phal::exception::SbeError& e) |
| 577 | { |
| 578 | log<level::ERR>("Failed to set SBE state"); |
| 579 | } |
| 580 | } |
| 581 | |
| 582 | struct pdbg_target* Manager::getPdbgTarget(unsigned int instance) |
| 583 | { |
| 584 | if (!pdbgInitialized) |
| 585 | { |
| 586 | try |
| 587 | { |
| 588 | openpower::phal::pdbg::init(); |
| 589 | pdbgInitialized = true; |
| 590 | } |
| 591 | catch (const openpower::phal::exception::PdbgError& e) |
| 592 | { |
| 593 | log<level::ERR>("pdbg initialization failed"); |
| 594 | return nullptr; |
| 595 | } |
| 596 | } |
| 597 | |
| 598 | struct pdbg_target* proc = nullptr; |
| 599 | pdbg_for_each_class_target("proc", proc) |
| 600 | { |
| 601 | if (pdbg_target_index(proc) == instance) |
| 602 | { |
| 603 | return proc; |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | log<level::ERR>("Failed to get pdbg target"); |
| 608 | return nullptr; |
| 609 | } |
Tom Joseph | 815f9f5 | 2020-07-27 12:12:13 +0530 | [diff] [blame] | 610 | #endif |
| 611 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 612 | void Manager::pollerTimerExpired() |
| 613 | { |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 614 | if (!_pollTimer) |
| 615 | { |
| 616 | log<level::ERR>( |
| 617 | "Manager::pollerTimerExpired() ERROR: Timer not defined"); |
| 618 | return; |
| 619 | } |
| 620 | |
| 621 | for (auto& obj : statusObjects) |
| 622 | { |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 623 | if (!obj->occActive()) |
| 624 | { |
| 625 | // OCC is not running yet |
| 626 | #ifdef READ_OCC_SENSORS |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 627 | auto id = obj->getOccInstanceID(); |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 628 | setSensorValueToNonFunctional(id); |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 629 | #endif |
| 630 | continue; |
| 631 | } |
| 632 | |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 633 | // Read sysfs to force kernel to poll OCC |
| 634 | obj->readOccState(); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 635 | |
| 636 | #ifdef READ_OCC_SENSORS |
| 637 | // Read occ sensor values |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 638 | getSensorValues(obj); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 639 | #endif |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 640 | } |
| 641 | |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 642 | if (activeCount > 0) |
| 643 | { |
| 644 | // Restart OCC poll timer |
| 645 | _pollTimer->restartOnce(std::chrono::seconds(pollInterval)); |
| 646 | } |
| 647 | else |
| 648 | { |
| 649 | // No OCCs running, so poll timer will not be restarted |
| 650 | log<level::INFO>( |
| 651 | fmt::format( |
| 652 | "Manager::pollerTimerExpired: poll timer will not be restarted") |
| 653 | .c_str()); |
| 654 | } |
Chris Cain | a8857c5 | 2021-01-27 11:53:05 -0600 | [diff] [blame] | 655 | } |
| 656 | |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 657 | #ifdef READ_OCC_SENSORS |
| 658 | void Manager::readTempSensors(const fs::path& path, uint32_t id) |
| 659 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 660 | std::regex expr{"temp\\d+_label$"}; // Example: temp5_label |
| 661 | for (auto& file : fs::directory_iterator(path)) |
| 662 | { |
| 663 | if (!std::regex_search(file.path().string(), expr)) |
| 664 | { |
| 665 | continue; |
| 666 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 667 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 668 | uint32_t labelValue{0}; |
| 669 | |
| 670 | try |
| 671 | { |
| 672 | labelValue = readFile<uint32_t>(file.path()); |
| 673 | } |
| 674 | catch (const std::system_error& e) |
| 675 | { |
| 676 | log<level::DEBUG>( |
| 677 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 678 | file.path().string(), e.code().value()) |
| 679 | .c_str()); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 680 | continue; |
| 681 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 682 | |
| 683 | const std::string& tempLabel = "label"; |
| 684 | const std::string filePathString = file.path().string().substr( |
| 685 | 0, file.path().string().length() - tempLabel.length()); |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 686 | |
| 687 | uint32_t fruTypeValue{0}; |
| 688 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 689 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 690 | fruTypeValue = readFile<uint32_t>(filePathString + fruTypeSuffix); |
| 691 | } |
| 692 | catch (const std::system_error& e) |
| 693 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 694 | log<level::DEBUG>( |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 695 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 696 | filePathString + fruTypeSuffix, e.code().value()) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 697 | .c_str()); |
| 698 | continue; |
| 699 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 700 | |
| 701 | std::string sensorPath = |
| 702 | OCC_SENSORS_ROOT + std::string("/temperature/"); |
| 703 | |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame] | 704 | std::string dvfsTempPath; |
| 705 | |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 706 | if (fruTypeValue == VRMVdd) |
| 707 | { |
| 708 | sensorPath.append("vrm_vdd" + std::to_string(id) + "_temp"); |
| 709 | } |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame] | 710 | else if (fruTypeValue == processorIoRing) |
| 711 | { |
| 712 | sensorPath.append("proc" + std::to_string(id) + "_ioring_temp"); |
| 713 | dvfsTempPath = std::string{OCC_SENSORS_ROOT} + "/temperature/proc" + |
| 714 | std::to_string(id) + "_ioring_dvfs_temp"; |
| 715 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 716 | else |
| 717 | { |
Matt Spinler | 14d1402 | 2021-08-25 15:38:29 -0500 | [diff] [blame] | 718 | uint16_t type = (labelValue & 0xFF000000) >> 24; |
| 719 | uint16_t instanceID = labelValue & 0x0000FFFF; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 720 | |
| 721 | if (type == OCC_DIMM_TEMP_SENSOR_TYPE) |
| 722 | { |
Matt Spinler | 8b8abee | 2021-08-25 15:18:21 -0500 | [diff] [blame] | 723 | if (fruTypeValue == fruTypeNotAvailable) |
| 724 | { |
| 725 | // Not all DIMM related temps are available to read |
| 726 | // (no _input file in this case) |
| 727 | continue; |
| 728 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 729 | auto iter = dimmTempSensorName.find(fruTypeValue); |
| 730 | if (iter == dimmTempSensorName.end()) |
| 731 | { |
George Liu | b5ca101 | 2021-09-10 12:53:11 +0800 | [diff] [blame] | 732 | log<level::ERR>( |
| 733 | fmt::format( |
| 734 | "readTempSensors: Fru type error! fruTypeValue = {}) ", |
| 735 | fruTypeValue) |
| 736 | .c_str()); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 737 | continue; |
| 738 | } |
| 739 | |
| 740 | sensorPath.append("dimm" + std::to_string(instanceID) + |
| 741 | iter->second); |
| 742 | } |
| 743 | else if (type == OCC_CPU_TEMP_SENSOR_TYPE) |
| 744 | { |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame] | 745 | if (fruTypeValue == processorCore) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 746 | { |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame] | 747 | // The OCC reports small core temps, of which there are |
| 748 | // two per big core. All current P10 systems are in big |
| 749 | // core mode, so use a big core name. |
| 750 | uint16_t coreNum = instanceID / 2; |
| 751 | uint16_t tempNum = instanceID % 2; |
| 752 | sensorPath.append("proc" + std::to_string(id) + "_core" + |
| 753 | std::to_string(coreNum) + "_" + |
| 754 | std::to_string(tempNum) + "_temp"); |
| 755 | |
| 756 | dvfsTempPath = std::string{OCC_SENSORS_ROOT} + |
| 757 | "/temperature/proc" + std::to_string(id) + |
| 758 | "_core_dvfs_temp"; |
| 759 | } |
| 760 | else |
| 761 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 762 | continue; |
| 763 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 764 | } |
| 765 | else |
| 766 | { |
| 767 | continue; |
| 768 | } |
| 769 | } |
| 770 | |
Matt Spinler | ace67d8 | 2021-10-18 13:41:57 -0500 | [diff] [blame] | 771 | // The dvfs temp file only needs to be read once per chip per type. |
| 772 | if (!dvfsTempPath.empty() && |
| 773 | !dbus::OccDBusSensors::getOccDBus().hasDvfsTemp(dvfsTempPath)) |
| 774 | { |
| 775 | try |
| 776 | { |
| 777 | auto dvfsValue = readFile<double>(filePathString + maxSuffix); |
| 778 | |
| 779 | dbus::OccDBusSensors::getOccDBus().setDvfsTemp( |
| 780 | dvfsTempPath, dvfsValue * std::pow(10, -3)); |
| 781 | } |
| 782 | catch (const std::system_error& e) |
| 783 | { |
| 784 | log<level::DEBUG>( |
| 785 | fmt::format( |
| 786 | "readTempSensors: Failed reading {}, errno = {}", |
| 787 | filePathString + maxSuffix, e.code().value()) |
| 788 | .c_str()); |
| 789 | } |
| 790 | } |
| 791 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 792 | uint32_t faultValue{0}; |
| 793 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 794 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 795 | faultValue = readFile<uint32_t>(filePathString + faultSuffix); |
| 796 | } |
| 797 | catch (const std::system_error& e) |
| 798 | { |
| 799 | log<level::DEBUG>( |
| 800 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 801 | filePathString + faultSuffix, e.code().value()) |
| 802 | .c_str()); |
| 803 | continue; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 804 | } |
| 805 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 806 | if (faultValue != 0) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 807 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 808 | dbus::OccDBusSensors::getOccDBus().setValue( |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 809 | sensorPath, std::numeric_limits<double>::quiet_NaN()); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 810 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 811 | dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath, |
| 812 | false); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 813 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 814 | continue; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 815 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 816 | |
| 817 | double tempValue{0}; |
| 818 | |
| 819 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 820 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 821 | tempValue = readFile<double>(filePathString + inputSuffix); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 822 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 823 | catch (const std::system_error& e) |
| 824 | { |
| 825 | log<level::DEBUG>( |
| 826 | fmt::format("readTempSensors: Failed reading {}, errno = {}", |
| 827 | filePathString + inputSuffix, e.code().value()) |
| 828 | .c_str()); |
| 829 | continue; |
| 830 | } |
| 831 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 832 | dbus::OccDBusSensors::getOccDBus().setValue( |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 833 | sensorPath, tempValue * std::pow(10, -3)); |
| 834 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 835 | dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath, |
| 836 | true); |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 837 | |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 838 | // At this point, the sensor will be created for sure. |
| 839 | if (existingSensors.find(sensorPath) == existingSensors.end()) |
| 840 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 841 | dbus::OccDBusSensors::getOccDBus().setChassisAssociation( |
| 842 | sensorPath); |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 843 | } |
| 844 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 845 | existingSensors[sensorPath] = id; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 846 | } |
| 847 | return; |
| 848 | } |
| 849 | |
| 850 | std::optional<std::string> |
| 851 | Manager::getPowerLabelFunctionID(const std::string& value) |
| 852 | { |
| 853 | // If the value is "system", then the FunctionID is "system". |
| 854 | if (value == "system") |
| 855 | { |
| 856 | return value; |
| 857 | } |
| 858 | |
| 859 | // If the value is not "system", then the label value have 3 numbers, of |
| 860 | // which we only care about the middle one: |
| 861 | // <sensor id>_<function id>_<apss channel> |
| 862 | // eg: The value is "0_10_5" , then the FunctionID is "10". |
| 863 | if (value.find("_") == std::string::npos) |
| 864 | { |
| 865 | return std::nullopt; |
| 866 | } |
| 867 | |
| 868 | auto powerLabelValue = value.substr((value.find("_") + 1)); |
| 869 | |
| 870 | if (powerLabelValue.find("_") == std::string::npos) |
| 871 | { |
| 872 | return std::nullopt; |
| 873 | } |
| 874 | |
| 875 | return powerLabelValue.substr(0, powerLabelValue.find("_")); |
| 876 | } |
| 877 | |
| 878 | void Manager::readPowerSensors(const fs::path& path, uint32_t id) |
| 879 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 880 | std::regex expr{"power\\d+_label$"}; // Example: power5_label |
| 881 | for (auto& file : fs::directory_iterator(path)) |
| 882 | { |
| 883 | if (!std::regex_search(file.path().string(), expr)) |
| 884 | { |
| 885 | continue; |
| 886 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 887 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 888 | std::string labelValue; |
| 889 | try |
| 890 | { |
| 891 | labelValue = readFile<std::string>(file.path()); |
| 892 | } |
| 893 | catch (const std::system_error& e) |
| 894 | { |
| 895 | log<level::DEBUG>( |
| 896 | fmt::format("readPowerSensors: Failed reading {}, errno = {}", |
| 897 | file.path().string(), e.code().value()) |
| 898 | .c_str()); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 899 | continue; |
| 900 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 901 | |
| 902 | auto functionID = getPowerLabelFunctionID(labelValue); |
| 903 | if (functionID == std::nullopt) |
| 904 | { |
| 905 | continue; |
| 906 | } |
| 907 | |
| 908 | const std::string& tempLabel = "label"; |
| 909 | const std::string filePathString = file.path().string().substr( |
| 910 | 0, file.path().string().length() - tempLabel.length()); |
| 911 | |
| 912 | std::string sensorPath = OCC_SENSORS_ROOT + std::string("/power/"); |
| 913 | |
| 914 | auto iter = powerSensorName.find(*functionID); |
| 915 | if (iter == powerSensorName.end()) |
| 916 | { |
| 917 | continue; |
| 918 | } |
| 919 | sensorPath.append(iter->second); |
| 920 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 921 | double tempValue{0}; |
| 922 | |
| 923 | try |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 924 | { |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 925 | tempValue = readFile<double>(filePathString + inputSuffix); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 926 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 927 | catch (const std::system_error& e) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 928 | { |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 929 | log<level::DEBUG>( |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 930 | fmt::format("readPowerSensors: Failed reading {}, errno = {}", |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 931 | filePathString + inputSuffix, e.code().value()) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 932 | .c_str()); |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 933 | continue; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 934 | } |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 935 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 936 | dbus::OccDBusSensors::getOccDBus().setUnit( |
Chris Cain | d84a833 | 2022-01-13 08:58:45 -0600 | [diff] [blame] | 937 | sensorPath, "xyz.openbmc_project.Sensor.Value.Unit.Watts"); |
| 938 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 939 | dbus::OccDBusSensors::getOccDBus().setValue( |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 940 | sensorPath, tempValue * std::pow(10, -3) * std::pow(10, -3)); |
| 941 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 942 | dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath, |
| 943 | true); |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 944 | |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 945 | if (existingSensors.find(sensorPath) == existingSensors.end()) |
| 946 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 947 | dbus::OccDBusSensors::getOccDBus().setChassisAssociation( |
| 948 | sensorPath); |
Matt Spinler | 5901abd | 2021-09-23 13:50:03 -0500 | [diff] [blame] | 949 | } |
| 950 | |
Matt Spinler | a26f152 | 2021-08-25 15:50:20 -0500 | [diff] [blame] | 951 | existingSensors[sensorPath] = id; |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 952 | } |
| 953 | return; |
| 954 | } |
| 955 | |
| 956 | void Manager::setSensorValueToNaN(uint32_t id) |
| 957 | { |
| 958 | for (const auto& [sensorPath, occId] : existingSensors) |
| 959 | { |
| 960 | if (occId == id) |
| 961 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 962 | dbus::OccDBusSensors::getOccDBus().setValue( |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 963 | sensorPath, std::numeric_limits<double>::quiet_NaN()); |
| 964 | } |
| 965 | } |
| 966 | return; |
| 967 | } |
| 968 | |
Sheldon Bailey | 373af75 | 2022-02-21 15:14:00 -0600 | [diff] [blame] | 969 | void Manager::setSensorValueToNonFunctional(uint32_t id) const |
| 970 | { |
| 971 | for (const auto& [sensorPath, occId] : existingSensors) |
| 972 | { |
| 973 | if (occId == id) |
| 974 | { |
| 975 | dbus::OccDBusSensors::getOccDBus().setValue( |
| 976 | sensorPath, std::numeric_limits<double>::quiet_NaN()); |
| 977 | |
| 978 | dbus::OccDBusSensors::getOccDBus().setOperationalStatus(sensorPath, |
| 979 | false); |
| 980 | } |
| 981 | } |
| 982 | return; |
| 983 | } |
| 984 | |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 985 | void Manager::getSensorValues(std::unique_ptr<Status>& occ) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 986 | { |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 987 | static bool tracedError[8] = {0}; |
| 988 | const fs::path sensorPath = occ->getHwmonPath(); |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 989 | const uint32_t id = occ->getOccInstanceID(); |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 990 | |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 991 | if (fs::exists(sensorPath)) |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 992 | { |
Chris Cain | e2d0a43 | 2022-03-28 11:08:49 -0500 | [diff] [blame] | 993 | // Read temperature sensors |
| 994 | readTempSensors(sensorPath, id); |
| 995 | |
| 996 | if (occ->isMasterOcc()) |
| 997 | { |
| 998 | // Read power sensors |
| 999 | readPowerSensors(sensorPath, id); |
| 1000 | } |
| 1001 | tracedError[id] = false; |
| 1002 | } |
| 1003 | else |
| 1004 | { |
| 1005 | if (!tracedError[id]) |
| 1006 | { |
| 1007 | log<level::ERR>( |
| 1008 | fmt::format( |
| 1009 | "Manager::getSensorValues: OCC{} sensor path missing: {}", |
| 1010 | id, sensorPath.c_str()) |
| 1011 | .c_str()); |
| 1012 | tracedError[id] = true; |
| 1013 | } |
Chicago Duan | bb895cb | 2021-06-18 19:37:16 +0800 | [diff] [blame] | 1014 | } |
| 1015 | |
| 1016 | return; |
| 1017 | } |
| 1018 | #endif |
Chris Cain | 1725767 | 2021-10-22 13:41:03 -0500 | [diff] [blame] | 1019 | |
| 1020 | // Read the altitude from DBus |
| 1021 | void Manager::readAltitude() |
| 1022 | { |
| 1023 | static bool traceAltitudeErr = true; |
| 1024 | |
| 1025 | utils::PropertyValue altitudeProperty{}; |
| 1026 | try |
| 1027 | { |
| 1028 | altitudeProperty = utils::getProperty(ALTITUDE_PATH, ALTITUDE_INTERFACE, |
| 1029 | ALTITUDE_PROP); |
| 1030 | auto sensorVal = std::get<double>(altitudeProperty); |
| 1031 | if (sensorVal < 0xFFFF) |
| 1032 | { |
| 1033 | if (sensorVal < 0) |
| 1034 | { |
| 1035 | altitude = 0; |
| 1036 | } |
| 1037 | else |
| 1038 | { |
| 1039 | // Round to nearest meter |
| 1040 | altitude = uint16_t(sensorVal + 0.5); |
| 1041 | } |
| 1042 | log<level::DEBUG>(fmt::format("readAltitude: sensor={} ({}m)", |
| 1043 | sensorVal, altitude) |
| 1044 | .c_str()); |
| 1045 | traceAltitudeErr = true; |
| 1046 | } |
| 1047 | else |
| 1048 | { |
| 1049 | if (traceAltitudeErr) |
| 1050 | { |
| 1051 | traceAltitudeErr = false; |
| 1052 | log<level::DEBUG>( |
| 1053 | fmt::format("Invalid altitude value: {}", sensorVal) |
| 1054 | .c_str()); |
| 1055 | } |
| 1056 | } |
| 1057 | } |
| 1058 | catch (const sdbusplus::exception::exception& e) |
| 1059 | { |
| 1060 | if (traceAltitudeErr) |
| 1061 | { |
| 1062 | traceAltitudeErr = false; |
| 1063 | log<level::INFO>( |
| 1064 | fmt::format("Unable to read Altitude: {}", e.what()).c_str()); |
| 1065 | } |
| 1066 | altitude = 0xFFFF; // not available |
| 1067 | } |
| 1068 | } |
| 1069 | |
| 1070 | // Callback function when ambient temperature changes |
| 1071 | void Manager::ambientCallback(sdbusplus::message::message& msg) |
| 1072 | { |
| 1073 | double currentTemp = 0; |
| 1074 | uint8_t truncatedTemp = 0xFF; |
| 1075 | std::string msgSensor; |
| 1076 | std::map<std::string, std::variant<double>> msgData; |
| 1077 | msg.read(msgSensor, msgData); |
| 1078 | |
| 1079 | auto valPropMap = msgData.find(AMBIENT_PROP); |
| 1080 | if (valPropMap == msgData.end()) |
| 1081 | { |
| 1082 | log<level::DEBUG>("ambientCallback: Unknown ambient property changed"); |
| 1083 | return; |
| 1084 | } |
| 1085 | currentTemp = std::get<double>(valPropMap->second); |
| 1086 | if (std::isnan(currentTemp)) |
| 1087 | { |
| 1088 | truncatedTemp = 0xFF; |
| 1089 | } |
| 1090 | else |
| 1091 | { |
| 1092 | if (currentTemp < 0) |
| 1093 | { |
| 1094 | truncatedTemp = 0; |
| 1095 | } |
| 1096 | else |
| 1097 | { |
| 1098 | // Round to nearest degree C |
| 1099 | truncatedTemp = uint8_t(currentTemp + 0.5); |
| 1100 | } |
| 1101 | } |
| 1102 | |
| 1103 | // If ambient changes, notify OCCs |
| 1104 | if (truncatedTemp != ambient) |
| 1105 | { |
| 1106 | log<level::DEBUG>( |
| 1107 | fmt::format("ambientCallback: Ambient change from {} to {}C", |
| 1108 | ambient, currentTemp) |
| 1109 | .c_str()); |
| 1110 | |
| 1111 | ambient = truncatedTemp; |
| 1112 | if (altitude == 0xFFFF) |
| 1113 | { |
| 1114 | // No altitude yet, try reading again |
| 1115 | readAltitude(); |
| 1116 | } |
| 1117 | |
| 1118 | log<level::DEBUG>( |
| 1119 | fmt::format("ambientCallback: Ambient: {}C, altitude: {}m", ambient, |
| 1120 | altitude) |
| 1121 | .c_str()); |
| 1122 | #ifdef POWER10 |
| 1123 | // Send ambient and altitude to all OCCs |
| 1124 | for (auto& obj : statusObjects) |
| 1125 | { |
| 1126 | if (obj->occActive()) |
| 1127 | { |
| 1128 | obj->sendAmbient(ambient, altitude); |
| 1129 | } |
| 1130 | } |
| 1131 | #endif // POWER10 |
| 1132 | } |
| 1133 | } |
| 1134 | |
| 1135 | // return the current ambient and altitude readings |
| 1136 | void Manager::getAmbientData(bool& ambientValid, uint8_t& ambientTemp, |
| 1137 | uint16_t& altitudeValue) const |
| 1138 | { |
| 1139 | ambientValid = true; |
| 1140 | ambientTemp = ambient; |
| 1141 | altitudeValue = altitude; |
| 1142 | |
| 1143 | if (ambient == 0xFF) |
| 1144 | { |
| 1145 | ambientValid = false; |
| 1146 | } |
| 1147 | } |
| 1148 | |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 1149 | #ifdef POWER10 |
| 1150 | void Manager::occsNotAllRunning() |
| 1151 | { |
Chris Cain | 6fa848a | 2022-01-24 14:54:38 -0600 | [diff] [blame] | 1152 | // Function will also gets called when occ-control app gets |
| 1153 | // restarted. (occ active sensors do not change, so the Status |
| 1154 | // object does not call Manager back for all OCCs) |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 1155 | |
| 1156 | if (activeCount != statusObjects.size()) |
| 1157 | { |
| 1158 | // Not all OCCs went active |
| 1159 | log<level::WARNING>( |
| 1160 | fmt::format( |
| 1161 | "occsNotAllRunning: Active OCC count ({}) does not match expected count ({})", |
| 1162 | activeCount, statusObjects.size()) |
| 1163 | .c_str()); |
| 1164 | // Procs may be garded, so may not need reset. |
| 1165 | } |
| 1166 | |
| 1167 | validateOccMaster(); |
| 1168 | } |
| 1169 | #endif // POWER10 |
| 1170 | |
| 1171 | // Verify single master OCC and start presence monitor |
| 1172 | void Manager::validateOccMaster() |
| 1173 | { |
| 1174 | int masterInstance = -1; |
| 1175 | for (auto& obj : statusObjects) |
| 1176 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 1177 | #ifdef POWER10 |
| 1178 | if (!obj->occActive()) |
| 1179 | { |
| 1180 | if (utils::isHostRunning()) |
| 1181 | { |
| 1182 | // OCC does not appear to be active yet, check active sensor |
| 1183 | pldmHandle->checkActiveSensor(obj->getOccInstanceID()); |
| 1184 | if (obj->occActive()) |
| 1185 | { |
| 1186 | log<level::INFO>( |
| 1187 | fmt::format( |
| 1188 | "validateOccMaster: OCC{} is ACTIVE after reading sensor", |
| 1189 | obj->getOccInstanceID()) |
| 1190 | .c_str()); |
| 1191 | } |
| 1192 | } |
| 1193 | else |
| 1194 | { |
| 1195 | log<level::WARNING>( |
| 1196 | fmt::format( |
| 1197 | "validateOccMaster: HOST is not running (OCC{})", |
| 1198 | obj->getOccInstanceID()) |
| 1199 | .c_str()); |
| 1200 | return; |
| 1201 | } |
| 1202 | } |
| 1203 | #endif // POWER10 |
| 1204 | |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 1205 | if (obj->isMasterOcc()) |
| 1206 | { |
Chris Cain | 5d66a0a | 2022-02-09 08:52:10 -0600 | [diff] [blame] | 1207 | obj->addPresenceWatchMaster(); |
| 1208 | |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 1209 | if (masterInstance == -1) |
| 1210 | { |
| 1211 | masterInstance = obj->getOccInstanceID(); |
| 1212 | } |
| 1213 | else |
| 1214 | { |
| 1215 | log<level::ERR>( |
| 1216 | fmt::format( |
| 1217 | "validateOccMaster: Multiple OCC masters! ({} and {})", |
| 1218 | masterInstance, obj->getOccInstanceID()) |
| 1219 | .c_str()); |
| 1220 | // request reset |
| 1221 | obj->deviceError(); |
| 1222 | } |
| 1223 | } |
| 1224 | } |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 1225 | |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 1226 | if (masterInstance < 0) |
| 1227 | { |
Chris Cain | bae4d07 | 2022-02-28 09:46:50 -0600 | [diff] [blame] | 1228 | log<level::ERR>( |
| 1229 | fmt::format("validateOccMaster: Master OCC not found! (of {} OCCs)", |
| 1230 | statusObjects.size()) |
| 1231 | .c_str()); |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 1232 | // request reset |
| 1233 | statusObjects.front()->deviceError(); |
| 1234 | } |
| 1235 | else |
| 1236 | { |
| 1237 | log<level::INFO>( |
Chris Cain | 36f9cde | 2021-11-22 11:18:21 -0600 | [diff] [blame] | 1238 | fmt::format("validateOccMaster: OCC{} is master of {} OCCs", |
| 1239 | masterInstance, activeCount) |
Chris Cain | a7b74dc | 2021-11-10 17:03:43 -0600 | [diff] [blame] | 1240 | .c_str()); |
| 1241 | } |
| 1242 | } |
| 1243 | |
Chris Cain | 40501a2 | 2022-03-14 17:33:27 -0500 | [diff] [blame] | 1244 | void Manager::updatePcapBounds() const |
| 1245 | { |
| 1246 | if (pcap) |
| 1247 | { |
| 1248 | pcap->updatePcapBounds(); |
| 1249 | } |
| 1250 | } |
| 1251 | |
Vishwanatha Subbanna | dfc7ec7 | 2017-09-07 18:18:01 +0530 | [diff] [blame] | 1252 | } // namespace occ |
| 1253 | } // namespace open_power |