Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 3 | #include "power_supply.hpp" |
| 4 | |
| 5 | #include "types.hpp" |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 6 | #include "util.hpp" |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 7 | |
Brandon Wyman | df13c3a | 2020-12-15 14:25:22 -0600 | [diff] [blame] | 8 | #include <fmt/format.h> |
| 9 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 10 | #include <xyz/openbmc_project/Common/Device/error.hpp> |
| 11 | |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 12 | #include <chrono> // sleep_for() |
| 13 | #include <cstdint> // uint8_t... |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 14 | #include <fstream> |
| 15 | #include <thread> // sleep_for() |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 16 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 17 | namespace phosphor::power::psu |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 18 | { |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 19 | // Amount of time in milliseconds to delay between power supply going from |
| 20 | // missing to present before running the bind command(s). |
| 21 | constexpr auto bindDelay = 1000; |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 22 | |
| 23 | using namespace phosphor::logging; |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 24 | using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error; |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 25 | |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 26 | PowerSupply::PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath, |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 27 | std::uint8_t i2cbus, std::uint16_t i2caddr, |
| 28 | const std::string& gpioLineName) : |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 29 | bus(bus), |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 30 | inventoryPath(invpath), bindPath("/sys/bus/i2c/drivers/ibm-cffps") |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 31 | { |
| 32 | if (inventoryPath.empty()) |
| 33 | { |
| 34 | throw std::invalid_argument{"Invalid empty inventoryPath"}; |
| 35 | } |
| 36 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 37 | if (gpioLineName.empty()) |
| 38 | { |
| 39 | throw std::invalid_argument{"Invalid empty gpioLineName"}; |
| 40 | } |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 41 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 42 | log<level::DEBUG>(fmt::format("gpioLineName: {}", gpioLineName).c_str()); |
| 43 | presenceGPIO = createGPIO(gpioLineName); |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 44 | |
| 45 | std::ostringstream ss; |
| 46 | ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr; |
| 47 | std::string addrStr = ss.str(); |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 48 | std::string busStr = std::to_string(i2cbus); |
| 49 | bindDevice = busStr; |
| 50 | bindDevice.append("-"); |
| 51 | bindDevice.append(addrStr); |
| 52 | |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 53 | pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr); |
| 54 | |
| 55 | // Get the current state of the Present property. |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 56 | try |
| 57 | { |
| 58 | updatePresenceGPIO(); |
| 59 | } |
| 60 | catch (...) |
| 61 | { |
| 62 | // If the above attempt to use the GPIO failed, it likely means that the |
| 63 | // GPIOs are in use by the kernel, meaning it is using gpio-keys. |
| 64 | // So, I should rely on phosphor-gpio-presence to update D-Bus, and |
| 65 | // work that way for power supply presence. |
| 66 | presenceGPIO = nullptr; |
| 67 | // Setup the functions to call when the D-Bus inventory path for the |
| 68 | // Present property changes. |
| 69 | presentMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 70 | bus, |
| 71 | sdbusplus::bus::match::rules::propertiesChanged(inventoryPath, |
| 72 | INVENTORY_IFACE), |
| 73 | [this](auto& msg) { this->inventoryChanged(msg); }); |
| 74 | |
| 75 | presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>( |
| 76 | bus, |
| 77 | sdbusplus::bus::match::rules::interfacesAdded() + |
| 78 | sdbusplus::bus::match::rules::argNpath(0, inventoryPath), |
| 79 | [this](auto& msg) { this->inventoryAdded(msg); }); |
| 80 | |
| 81 | updatePresence(); |
| 82 | updateInventory(); |
| 83 | } |
| 84 | } |
| 85 | |
| 86 | void PowerSupply::bindOrUnbindDriver(bool present) |
| 87 | { |
| 88 | auto action = (present) ? "bind" : "unbind"; |
| 89 | auto path = bindPath / action; |
| 90 | |
| 91 | if (present) |
| 92 | { |
| 93 | log<level::INFO>( |
| 94 | fmt::format("Binding device driver. path: {} device: {}", |
| 95 | path.string(), bindDevice) |
| 96 | .c_str()); |
| 97 | } |
| 98 | else |
| 99 | { |
| 100 | log<level::INFO>( |
| 101 | fmt::format("Unbinding device driver. path: {} device: {}", |
| 102 | path.string(), bindDevice) |
| 103 | .c_str()); |
| 104 | } |
| 105 | |
| 106 | std::ofstream file; |
| 107 | |
| 108 | file.exceptions(std::ofstream::failbit | std::ofstream::badbit | |
| 109 | std::ofstream::eofbit); |
| 110 | |
| 111 | try |
| 112 | { |
| 113 | file.open(path); |
| 114 | file << bindDevice; |
| 115 | file.close(); |
| 116 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 117 | catch (const std::exception& e) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 118 | { |
| 119 | auto err = errno; |
| 120 | |
| 121 | log<level::ERR>( |
| 122 | fmt::format("Failed binding or unbinding device. errno={}", err) |
| 123 | .c_str()); |
| 124 | } |
Brandon Wyman | 510acaa | 2020-11-05 18:32:04 -0600 | [diff] [blame] | 125 | } |
| 126 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 127 | void PowerSupply::updatePresence() |
| 128 | { |
| 129 | try |
| 130 | { |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 131 | present = getPresence(bus, inventoryPath); |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 132 | } |
Patrick Williams | 69f10ad | 2021-09-02 09:46:49 -0500 | [diff] [blame] | 133 | catch (const sdbusplus::exception::exception& e) |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 134 | { |
| 135 | // Relying on property change or interface added to retry. |
| 136 | // Log an informational trace to the journal. |
Brandon Wyman | df13c3a | 2020-12-15 14:25:22 -0600 | [diff] [blame] | 137 | log<level::INFO>( |
| 138 | fmt::format("D-Bus property {} access failure exception", |
| 139 | inventoryPath) |
| 140 | .c_str()); |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 141 | } |
| 142 | } |
| 143 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 144 | void PowerSupply::updatePresenceGPIO() |
| 145 | { |
| 146 | bool presentOld = present; |
| 147 | |
| 148 | try |
| 149 | { |
| 150 | if (presenceGPIO->read() > 0) |
| 151 | { |
| 152 | present = true; |
| 153 | } |
| 154 | else |
| 155 | { |
| 156 | present = false; |
| 157 | } |
| 158 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 159 | catch (const std::exception& e) |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 160 | { |
| 161 | log<level::ERR>( |
| 162 | fmt::format("presenceGPIO read fail: {}", e.what()).c_str()); |
| 163 | throw; |
| 164 | } |
| 165 | |
| 166 | if (presentOld != present) |
| 167 | { |
| 168 | log<level::DEBUG>( |
| 169 | fmt::format("presentOld: {} present: {}", presentOld, present) |
| 170 | .c_str()); |
| 171 | if (present) |
| 172 | { |
| 173 | std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay)); |
| 174 | bindOrUnbindDriver(present); |
| 175 | pmbusIntf->findHwmonDir(); |
| 176 | onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY); |
| 177 | clearFaults(); |
| 178 | } |
| 179 | else |
| 180 | { |
| 181 | bindOrUnbindDriver(present); |
| 182 | } |
| 183 | |
| 184 | auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH)); |
| 185 | auto const lastSlashPos = invpath.find_last_of('/'); |
| 186 | std::string prettyName = invpath.substr(lastSlashPos + 1); |
| 187 | setPresence(bus, invpath, present, prettyName); |
| 188 | updateInventory(); |
| 189 | } |
| 190 | } |
| 191 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 192 | void PowerSupply::analyze() |
| 193 | { |
| 194 | using namespace phosphor::pmbus; |
| 195 | |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 196 | if (presenceGPIO) |
| 197 | { |
| 198 | updatePresenceGPIO(); |
| 199 | } |
| 200 | |
Brandon Wyman | f65c406 | 2020-08-19 13:15:53 -0500 | [diff] [blame] | 201 | if ((present) && (readFail < LOG_LIMIT)) |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 202 | { |
| 203 | try |
| 204 | { |
Brandon Wyman | fed0ba2 | 2020-09-26 20:02:51 -0500 | [diff] [blame] | 205 | statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug); |
Brandon Wyman | f65c406 | 2020-08-19 13:15:53 -0500 | [diff] [blame] | 206 | // Read worked, reset the fail count. |
| 207 | readFail = 0; |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 208 | |
| 209 | if (statusWord) |
| 210 | { |
Brandon Wyman | f07bc79 | 2021-10-12 19:00:35 +0000 | [diff] [blame] | 211 | statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug); |
Jay Meyer | 10d9405 | 2020-11-30 14:41:21 -0600 | [diff] [blame] | 212 | statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug); |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 213 | if (statusWord & status_word::INPUT_FAULT_WARN) |
| 214 | { |
| 215 | if (!inputFault) |
| 216 | { |
Brandon Wyman | c899660 | 2021-10-12 19:28:56 +0000 | [diff] [blame] | 217 | log<level::INFO>( |
Brandon Wyman | f07bc79 | 2021-10-12 19:00:35 +0000 | [diff] [blame] | 218 | fmt::format("INPUT fault: STATUS_WORD = {:#04x}, " |
| 219 | "STATUS_MFR_SPECIFIC = {:#02x}, " |
| 220 | "STATUS_INPUT = {:#02x}", |
| 221 | statusWord, statusMFR, statusInput) |
Brandon Wyman | c899660 | 2021-10-12 19:28:56 +0000 | [diff] [blame] | 222 | .c_str()); |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | faultFound = true; |
| 226 | inputFault = true; |
| 227 | } |
| 228 | |
| 229 | if (statusWord & status_word::MFR_SPECIFIC_FAULT) |
| 230 | { |
| 231 | if (!mfrFault) |
| 232 | { |
Brandon Wyman | c899660 | 2021-10-12 19:28:56 +0000 | [diff] [blame] | 233 | log<level::ERR>( |
| 234 | fmt::format("MFR fault: " |
| 235 | "STATUS_WORD = {:#04x} " |
| 236 | "STATUS_MFR_SPECIFIC = {:#02x}", |
| 237 | statusWord, statusMFR) |
| 238 | .c_str()); |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 239 | } |
| 240 | faultFound = true; |
| 241 | mfrFault = true; |
| 242 | } |
| 243 | |
| 244 | if (statusWord & status_word::VIN_UV_FAULT) |
| 245 | { |
| 246 | if (!vinUVFault) |
| 247 | { |
Brandon Wyman | c899660 | 2021-10-12 19:28:56 +0000 | [diff] [blame] | 248 | log<level::INFO>( |
Brandon Wyman | f07bc79 | 2021-10-12 19:00:35 +0000 | [diff] [blame] | 249 | fmt::format("VIN_UV fault: STATUS_WORD = {:#04x}, " |
| 250 | "STATUS_MFR_SPECIFIC = {:#02x}, " |
| 251 | "STATUS_INPUT = {:#02x}", |
| 252 | statusWord, statusMFR, statusInput) |
Brandon Wyman | c899660 | 2021-10-12 19:28:56 +0000 | [diff] [blame] | 253 | .c_str()); |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | faultFound = true; |
| 257 | vinUVFault = true; |
| 258 | } |
| 259 | } |
| 260 | else |
| 261 | { |
| 262 | faultFound = false; |
| 263 | inputFault = false; |
| 264 | mfrFault = false; |
| 265 | vinUVFault = false; |
| 266 | } |
| 267 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 268 | catch (const ReadFailure& e) |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 269 | { |
Brandon Wyman | f65c406 | 2020-08-19 13:15:53 -0500 | [diff] [blame] | 270 | readFail++; |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 271 | phosphor::logging::commit<ReadFailure>(); |
| 272 | } |
| 273 | } |
| 274 | } |
| 275 | |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 276 | void PowerSupply::onOffConfig(uint8_t data) |
| 277 | { |
| 278 | using namespace phosphor::pmbus; |
| 279 | |
| 280 | if (present) |
| 281 | { |
| 282 | log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data)); |
| 283 | try |
| 284 | { |
| 285 | std::vector<uint8_t> configData{data}; |
| 286 | pmbusIntf->writeBinary(ON_OFF_CONFIG, configData, |
| 287 | Type::HwmonDeviceDebug); |
| 288 | } |
| 289 | catch (...) |
| 290 | { |
| 291 | // The underlying code in writeBinary will log a message to the |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 292 | // journal if the write fails. If the ON_OFF_CONFIG is not setup |
| 293 | // as desired, later fault detection and analysis code should |
| 294 | // catch any of the fall out. We should not need to terminate |
| 295 | // the application if this write fails. |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | } |
| 299 | |
Brandon Wyman | 3c20846 | 2020-05-13 16:25:58 -0500 | [diff] [blame] | 300 | void PowerSupply::clearFaults() |
| 301 | { |
Brandon Wyman | 5474c91 | 2021-02-23 14:39:43 -0600 | [diff] [blame] | 302 | faultLogged = false; |
Brandon Wyman | 3c20846 | 2020-05-13 16:25:58 -0500 | [diff] [blame] | 303 | // The PMBus device driver does not allow for writing CLEAR_FAULTS |
| 304 | // directly. However, the pmbus hwmon device driver code will send a |
| 305 | // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so |
| 306 | // reading in1_input should result in clearing the fault bits in |
| 307 | // STATUS_BYTE/STATUS_WORD. |
| 308 | // I do not care what the return value is. |
Brandon Wyman | 1115153 | 2020-11-10 13:45:57 -0600 | [diff] [blame] | 309 | if (present) |
Brandon Wyman | 3c20846 | 2020-05-13 16:25:58 -0500 | [diff] [blame] | 310 | { |
Brandon Wyman | 9564e94 | 2020-11-10 14:01:42 -0600 | [diff] [blame] | 311 | faultFound = false; |
| 312 | inputFault = false; |
| 313 | mfrFault = false; |
Jay Meyer | 10d9405 | 2020-11-30 14:41:21 -0600 | [diff] [blame] | 314 | statusMFR = 0; |
Brandon Wyman | 9564e94 | 2020-11-10 14:01:42 -0600 | [diff] [blame] | 315 | vinUVFault = false; |
| 316 | readFail = 0; |
Brandon Wyman | 9564e94 | 2020-11-10 14:01:42 -0600 | [diff] [blame] | 317 | |
Brandon Wyman | 1115153 | 2020-11-10 13:45:57 -0600 | [diff] [blame] | 318 | try |
| 319 | { |
| 320 | static_cast<void>( |
| 321 | pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon)); |
| 322 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 323 | catch (const ReadFailure& e) |
Brandon Wyman | 1115153 | 2020-11-10 13:45:57 -0600 | [diff] [blame] | 324 | { |
| 325 | // Since I do not care what the return value is, I really do not |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 326 | // care much if it gets a ReadFailure either. However, this |
| 327 | // should not prevent the application from continuing to run, so |
| 328 | // catching the read failure. |
Brandon Wyman | 1115153 | 2020-11-10 13:45:57 -0600 | [diff] [blame] | 329 | } |
Brandon Wyman | 3c20846 | 2020-05-13 16:25:58 -0500 | [diff] [blame] | 330 | } |
| 331 | } |
| 332 | |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 333 | void PowerSupply::inventoryChanged(sdbusplus::message::message& msg) |
| 334 | { |
| 335 | std::string msgSensor; |
Patrick Williams | abe4941 | 2020-05-13 17:59:47 -0500 | [diff] [blame] | 336 | std::map<std::string, std::variant<uint32_t, bool>> msgData; |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 337 | msg.read(msgSensor, msgData); |
| 338 | |
| 339 | // Check if it was the Present property that changed. |
| 340 | auto valPropMap = msgData.find(PRESENT_PROP); |
| 341 | if (valPropMap != msgData.end()) |
| 342 | { |
| 343 | if (std::get<bool>(valPropMap->second)) |
| 344 | { |
| 345 | present = true; |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 346 | // TODO: Immediately trying to read or write the "files" causes |
| 347 | // read or write failures. |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 348 | using namespace std::chrono_literals; |
| 349 | std::this_thread::sleep_for(20ms); |
Brandon Wyman | 9564e94 | 2020-11-10 14:01:42 -0600 | [diff] [blame] | 350 | pmbusIntf->findHwmonDir(); |
Brandon Wyman | 59a3579 | 2020-06-04 12:37:40 -0500 | [diff] [blame] | 351 | onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY); |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 352 | clearFaults(); |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 353 | updateInventory(); |
Brandon Wyman | aed1f75 | 2019-11-25 18:10:52 -0600 | [diff] [blame] | 354 | } |
| 355 | else |
| 356 | { |
| 357 | present = false; |
| 358 | |
| 359 | // Clear out the now outdated inventory properties |
| 360 | updateInventory(); |
| 361 | } |
| 362 | } |
| 363 | } |
| 364 | |
Brandon Wyman | 9a507db | 2021-02-25 16:15:22 -0600 | [diff] [blame] | 365 | void PowerSupply::inventoryAdded(sdbusplus::message::message& msg) |
| 366 | { |
| 367 | sdbusplus::message::object_path path; |
| 368 | msg.read(path); |
| 369 | // Make sure the signal is for the PSU inventory path |
| 370 | if (path == inventoryPath) |
| 371 | { |
| 372 | std::map<std::string, std::map<std::string, std::variant<bool>>> |
| 373 | interfaces; |
| 374 | // Get map of interfaces and their properties |
| 375 | msg.read(interfaces); |
| 376 | |
| 377 | auto properties = interfaces.find(INVENTORY_IFACE); |
| 378 | if (properties != interfaces.end()) |
| 379 | { |
| 380 | auto property = properties->second.find(PRESENT_PROP); |
| 381 | if (property != properties->second.end()) |
| 382 | { |
| 383 | present = std::get<bool>(property->second); |
| 384 | |
| 385 | log<level::INFO>(fmt::format("Power Supply {} Present {}", |
| 386 | inventoryPath, present) |
| 387 | .c_str()); |
| 388 | |
| 389 | updateInventory(); |
| 390 | } |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 395 | void PowerSupply::updateInventory() |
| 396 | { |
| 397 | using namespace phosphor::pmbus; |
| 398 | |
Chanh Nguyen | c12c53b | 2021-04-06 17:24:47 +0700 | [diff] [blame] | 399 | #if IBM_VPD |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 400 | std::string ccin; |
| 401 | std::string pn; |
| 402 | std::string fn; |
| 403 | std::string header; |
| 404 | std::string sn; |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 405 | using PropertyMap = |
George Liu | 070c1bc | 2020-10-12 11:28:01 +0800 | [diff] [blame] | 406 | std::map<std::string, |
| 407 | std::variant<std::string, std::vector<uint8_t>, bool>>; |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 408 | PropertyMap assetProps; |
George Liu | 070c1bc | 2020-10-12 11:28:01 +0800 | [diff] [blame] | 409 | PropertyMap operProps; |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 410 | PropertyMap versionProps; |
| 411 | PropertyMap ipzvpdDINFProps; |
| 412 | PropertyMap ipzvpdVINIProps; |
| 413 | using InterfaceMap = std::map<std::string, PropertyMap>; |
| 414 | InterfaceMap interfaces; |
| 415 | using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>; |
| 416 | ObjectMap object; |
| 417 | #endif |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 418 | log<level::DEBUG>( |
| 419 | fmt::format("updateInventory() inventoryPath: {}", inventoryPath) |
| 420 | .c_str()); |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 421 | |
| 422 | if (present) |
| 423 | { |
| 424 | // TODO: non-IBM inventory updates? |
| 425 | |
Chanh Nguyen | c12c53b | 2021-04-06 17:24:47 +0700 | [diff] [blame] | 426 | #if IBM_VPD |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 427 | try |
| 428 | { |
| 429 | ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug); |
| 430 | assetProps.emplace(MODEL_PROP, ccin); |
Adriana Kobylak | 572a905 | 2021-03-30 15:58:07 +0000 | [diff] [blame] | 431 | modelName = ccin; |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 432 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 433 | catch (const ReadFailure& e) |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 434 | { |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 435 | // Ignore the read failure, let pmbus code indicate failure, |
| 436 | // path... |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 437 | // TODO - ibm918 |
| 438 | // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md |
| 439 | // The BMC must log errors if any of the VPD cannot be properly |
| 440 | // parsed or fails ECC checks. |
| 441 | } |
| 442 | |
| 443 | try |
| 444 | { |
| 445 | pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug); |
| 446 | assetProps.emplace(PN_PROP, pn); |
| 447 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 448 | catch (const ReadFailure& e) |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 449 | { |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 450 | // Ignore the read failure, let pmbus code indicate failure, |
| 451 | // path... |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 452 | } |
| 453 | |
| 454 | try |
| 455 | { |
| 456 | fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug); |
| 457 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 458 | catch (const ReadFailure& e) |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 459 | { |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 460 | // Ignore the read failure, let pmbus code indicate failure, |
| 461 | // path... |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 462 | } |
| 463 | |
| 464 | try |
| 465 | { |
| 466 | header = |
| 467 | pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug); |
| 468 | sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug); |
| 469 | assetProps.emplace(SN_PROP, sn); |
| 470 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 471 | catch (const ReadFailure& e) |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 472 | { |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 473 | // Ignore the read failure, let pmbus code indicate failure, |
| 474 | // path... |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 475 | } |
| 476 | |
| 477 | try |
| 478 | { |
Brandon Wyman | c9efe41 | 2020-10-09 15:42:50 -0500 | [diff] [blame] | 479 | fwVersion = |
| 480 | pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug); |
| 481 | versionProps.emplace(VERSION_PROP, fwVersion); |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 482 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 483 | catch (const ReadFailure& e) |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 484 | { |
B. J. Wyman | 681b2a3 | 2021-04-20 22:31:22 +0000 | [diff] [blame] | 485 | // Ignore the read failure, let pmbus code indicate failure, |
| 486 | // path... |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 487 | } |
| 488 | |
| 489 | ipzvpdVINIProps.emplace("CC", |
| 490 | std::vector<uint8_t>(ccin.begin(), ccin.end())); |
| 491 | ipzvpdVINIProps.emplace("PN", |
| 492 | std::vector<uint8_t>(pn.begin(), pn.end())); |
| 493 | ipzvpdVINIProps.emplace("FN", |
| 494 | std::vector<uint8_t>(fn.begin(), fn.end())); |
| 495 | std::string header_sn = header + sn + '\0'; |
| 496 | ipzvpdVINIProps.emplace( |
| 497 | "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end())); |
| 498 | std::string description = "IBM PS"; |
| 499 | ipzvpdVINIProps.emplace( |
| 500 | "DR", std::vector<uint8_t>(description.begin(), description.end())); |
| 501 | |
| 502 | // Update the Resource Identifier (RI) keyword |
| 503 | // 2 byte FRC: 0x0003 |
| 504 | // 2 byte RID: 0x1000, 0x1001... |
| 505 | std::uint8_t num = std::stoul( |
| 506 | inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0); |
| 507 | std::vector<uint8_t> ri{0x00, 0x03, 0x10, num}; |
| 508 | ipzvpdDINFProps.emplace("RI", ri); |
| 509 | |
| 510 | // Fill in the FRU Label (FL) keyword. |
| 511 | std::string fl = "E"; |
| 512 | fl.push_back(inventoryPath.back()); |
| 513 | fl.resize(FL_KW_SIZE, ' '); |
| 514 | ipzvpdDINFProps.emplace("FL", |
| 515 | std::vector<uint8_t>(fl.begin(), fl.end())); |
| 516 | |
| 517 | interfaces.emplace(ASSET_IFACE, std::move(assetProps)); |
| 518 | interfaces.emplace(VERSION_IFACE, std::move(versionProps)); |
| 519 | interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps)); |
| 520 | interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps)); |
| 521 | |
George Liu | 070c1bc | 2020-10-12 11:28:01 +0800 | [diff] [blame] | 522 | // Update the Functional |
| 523 | operProps.emplace(FUNCTIONAL_PROP, present); |
| 524 | interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps)); |
| 525 | |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 526 | auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH)); |
| 527 | object.emplace(path, std::move(interfaces)); |
| 528 | |
| 529 | try |
| 530 | { |
| 531 | auto service = |
| 532 | util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus); |
| 533 | |
| 534 | if (service.empty()) |
| 535 | { |
| 536 | log<level::ERR>("Unable to get inventory manager service"); |
| 537 | return; |
| 538 | } |
| 539 | |
| 540 | auto method = |
| 541 | bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH, |
| 542 | INVENTORY_MGR_IFACE, "Notify"); |
| 543 | |
| 544 | method.append(std::move(object)); |
| 545 | |
| 546 | auto reply = bus.call(method); |
| 547 | } |
Patrick Williams | c1d4de5 | 2021-10-06 12:45:57 -0500 | [diff] [blame] | 548 | catch (const std::exception& e) |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 549 | { |
Jay Meyer | 6a3fd2c | 2020-08-25 16:37:16 -0500 | [diff] [blame] | 550 | log<level::ERR>( |
| 551 | std::string(e.what() + std::string(" PATH=") + inventoryPath) |
| 552 | .c_str()); |
Brandon Wyman | 1d7a7df | 2020-03-26 10:14:05 -0500 | [diff] [blame] | 553 | } |
| 554 | #endif |
| 555 | } |
| 556 | } |
| 557 | |
Adriana Kobylak | 4175ffb | 2021-08-02 14:51:05 +0000 | [diff] [blame] | 558 | void PowerSupply::getInputVoltage(double& actualInputVoltage, |
| 559 | int& inputVoltage) const |
| 560 | { |
| 561 | using namespace phosphor::pmbus; |
| 562 | |
| 563 | actualInputVoltage = in_input::VIN_VOLTAGE_0; |
| 564 | inputVoltage = in_input::VIN_VOLTAGE_0; |
| 565 | |
| 566 | if (present) |
| 567 | { |
| 568 | try |
| 569 | { |
| 570 | // Read input voltage in millivolts |
| 571 | auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon); |
| 572 | |
| 573 | // Convert to volts |
| 574 | actualInputVoltage = std::stod(inputVoltageStr) / 1000; |
| 575 | |
| 576 | // Calculate the voltage based on voltage thresholds |
| 577 | if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN) |
| 578 | { |
| 579 | inputVoltage = in_input::VIN_VOLTAGE_0; |
| 580 | } |
| 581 | else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD) |
| 582 | { |
| 583 | inputVoltage = in_input::VIN_VOLTAGE_110; |
| 584 | } |
| 585 | else |
| 586 | { |
| 587 | inputVoltage = in_input::VIN_VOLTAGE_220; |
| 588 | } |
| 589 | } |
| 590 | catch (const std::exception& e) |
| 591 | { |
| 592 | log<level::ERR>( |
| 593 | fmt::format("READ_VIN read error: {}", e.what()).c_str()); |
| 594 | } |
| 595 | } |
| 596 | } |
| 597 | |
Brandon Wyman | 3f1242f | 2020-01-28 13:11:25 -0600 | [diff] [blame] | 598 | } // namespace phosphor::power::psu |