Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 1 | /** |
| 2 | * Copyright © 2024 IBM Corporation |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 16 | #include "config.h" |
| 17 | |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 18 | #include "utils.hpp" |
| 19 | |
| 20 | #include "utility.hpp" |
| 21 | |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 22 | #include <phosphor-logging/lg2.hpp> |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 23 | #include <xyz/openbmc_project/Common/Device/error.hpp> |
| 24 | |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 25 | #include <cassert> |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 26 | #include <exception> |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 27 | #include <filesystem> |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 28 | #include <iomanip> |
| 29 | #include <ios> |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 30 | #include <iostream> |
| 31 | #include <regex> |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 32 | #include <sstream> |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 33 | #include <stdexcept> |
| 34 | |
| 35 | using namespace phosphor::logging; |
| 36 | using namespace phosphor::power::util; |
| 37 | using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error; |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 38 | namespace fs = std::filesystem; |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 39 | |
| 40 | namespace utils |
| 41 | { |
| 42 | |
| 43 | constexpr auto IBMCFFPSInterface = |
| 44 | "xyz.openbmc_project.Configuration.IBMCFFPSConnector"; |
| 45 | constexpr auto i2cBusProp = "I2CBus"; |
| 46 | constexpr auto i2cAddressProp = "I2CAddress"; |
| 47 | |
| 48 | PsuI2cInfo getPsuI2c(sdbusplus::bus_t& bus, const std::string& psuInventoryPath) |
| 49 | { |
| 50 | auto depth = 0; |
| 51 | auto objects = getSubTree(bus, "/", IBMCFFPSInterface, depth); |
| 52 | if (objects.empty()) |
| 53 | { |
| 54 | throw std::runtime_error("Supported Configuration Not Found"); |
| 55 | } |
| 56 | |
| 57 | std::optional<std::uint64_t> i2cbus; |
| 58 | std::optional<std::uint64_t> i2caddr; |
| 59 | |
| 60 | // GET a map of objects back. |
| 61 | // Each object will have a path, a service, and an interface. |
| 62 | for (const auto& [path, services] : objects) |
| 63 | { |
| 64 | auto service = services.begin()->first; |
| 65 | |
| 66 | if (path.empty() || service.empty()) |
| 67 | { |
| 68 | continue; |
| 69 | } |
| 70 | |
| 71 | // Match the PSU identifier in the path with the passed PSU inventory |
| 72 | // path. Compare the last character of both paths to find the PSU bus |
| 73 | // and address. example: PSU path: |
| 74 | // /xyz/openbmc_project/inventory/system/board/Nisqually_Backplane/Power_Supply_Slot_0 |
| 75 | // PSU inventory path: |
| 76 | // /xyz/openbmc_project/inventory/system/chassis/motherboard/powersupply0 |
| 77 | if (path.back() == psuInventoryPath.back()) |
| 78 | { |
| 79 | // Retrieve i2cBus and i2cAddress from array of properties. |
| 80 | auto properties = |
| 81 | getAllProperties(bus, path, IBMCFFPSInterface, service); |
| 82 | for (const auto& property : properties) |
| 83 | { |
| 84 | try |
| 85 | { |
| 86 | if (property.first == i2cBusProp) |
| 87 | { |
| 88 | i2cbus = std::get<uint64_t>(properties.at(i2cBusProp)); |
| 89 | } |
| 90 | else if (property.first == i2cAddressProp) |
| 91 | { |
| 92 | i2caddr = |
| 93 | std::get<uint64_t>(properties.at(i2cAddressProp)); |
| 94 | } |
| 95 | } |
| 96 | catch (const std::exception& e) |
| 97 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 98 | lg2::warning("Error reading property {PROPERTY}: {ERROR}", |
| 99 | "PROPERTY", property.first, "ERROR", e); |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 100 | } |
| 101 | } |
| 102 | |
| 103 | if (i2cbus.has_value() && i2caddr.has_value()) |
| 104 | { |
| 105 | break; |
| 106 | } |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | if (!i2cbus.has_value() || !i2caddr.has_value()) |
| 111 | { |
| 112 | throw std::runtime_error("Failed to get I2C bus or address"); |
| 113 | } |
| 114 | |
| 115 | return std::make_tuple(*i2cbus, *i2caddr); |
| 116 | } |
| 117 | |
Patrick Williams | 92261f8 | 2025-02-01 08:22:34 -0500 | [diff] [blame] | 118 | std::unique_ptr<phosphor::pmbus::PMBusBase> getPmbusIntf(std::uint64_t i2cBus, |
| 119 | std::uint64_t i2cAddr) |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 120 | { |
| 121 | std::stringstream ss; |
| 122 | ss << std::hex << std::setw(4) << std::setfill('0') << i2cAddr; |
| 123 | return phosphor::pmbus::createPMBus(i2cBus, ss.str()); |
| 124 | } |
| 125 | |
| 126 | std::string readVPDValue(phosphor::pmbus::PMBusBase& pmbusIntf, |
| 127 | const std::string& vpdName, |
| 128 | const phosphor::pmbus::Type& type, |
| 129 | const std::size_t& vpdSize) |
| 130 | { |
| 131 | std::string vpdValue; |
| 132 | const std::regex illegalVPDRegex = |
| 133 | std::regex("[^[:alnum:]]", std::regex::basic); |
| 134 | |
| 135 | try |
| 136 | { |
| 137 | vpdValue = pmbusIntf.readString(vpdName, type); |
| 138 | } |
| 139 | catch (const ReadFailure& e) |
| 140 | { |
| 141 | // Ignore the read failure, let pmbus code indicate failure. |
| 142 | } |
| 143 | |
| 144 | if (vpdValue.size() != vpdSize) |
| 145 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 146 | lg2::info(" {VPDNAME} resize needed. size: {SIZE}", "VPDNAME", vpdName, |
| 147 | "SIZE", vpdValue.size()); |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 148 | vpdValue.resize(vpdSize, ' '); |
| 149 | } |
| 150 | |
| 151 | // Replace any illegal values with space(s). |
| 152 | std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(), |
| 153 | illegalVPDRegex, " "); |
| 154 | |
| 155 | return vpdValue; |
| 156 | } |
| 157 | |
| 158 | bool checkFileExists(const std::string& filePath) |
| 159 | { |
| 160 | try |
| 161 | { |
| 162 | return std::filesystem::exists(filePath); |
| 163 | } |
| 164 | catch (const std::exception& e) |
| 165 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 166 | lg2::error("Unable to check for existence of {FILEPATH}: {ERROR}", |
| 167 | "FILEPATH", filePath, "ERROR", e); |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 168 | } |
| 169 | return false; |
| 170 | } |
| 171 | |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 172 | std::string getDeviceName(std::string devPath) |
| 173 | { |
Faisal Awada | 5ace9fb | 2025-01-07 13:26:25 -0600 | [diff] [blame] | 174 | if (devPath.empty()) |
| 175 | { |
| 176 | return devPath; |
| 177 | } |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 178 | if (devPath.back() == '/') |
| 179 | { |
| 180 | devPath.pop_back(); |
| 181 | } |
| 182 | return fs::path(devPath).stem().string(); |
| 183 | } |
| 184 | |
| 185 | std::string getDevicePath(sdbusplus::bus_t& bus, |
| 186 | const std::string& psuInventoryPath) |
| 187 | { |
| 188 | try |
| 189 | { |
| 190 | if (usePsuJsonFile()) |
| 191 | { |
| 192 | auto data = loadJSONFromFile(PSU_JSON_PATH); |
| 193 | if (data == nullptr) |
| 194 | { |
| 195 | return {}; |
| 196 | } |
| 197 | auto devicePath = data["psuDevices"][psuInventoryPath]; |
| 198 | if (devicePath.empty()) |
| 199 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 200 | lg2::warning("Unable to find psu devices or path"); |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 201 | } |
| 202 | return devicePath; |
| 203 | } |
| 204 | else |
| 205 | { |
| 206 | const auto [i2cbus, i2caddr] = getPsuI2c(bus, psuInventoryPath); |
| 207 | const auto DevicePath = "/sys/bus/i2c/devices/"; |
| 208 | std::ostringstream ss; |
| 209 | ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr; |
| 210 | std::string addrStr = ss.str(); |
| 211 | std::string busStr = std::to_string(i2cbus); |
| 212 | std::string devPath = DevicePath + busStr + "-" + addrStr; |
| 213 | return devPath; |
| 214 | } |
| 215 | } |
| 216 | catch (const std::exception& e) |
| 217 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 218 | lg2::error("Error in getDevicePath: {ERROR}", "ERROR", e); |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 219 | return {}; |
| 220 | } |
| 221 | catch (...) |
| 222 | { |
Anwaar Hadi | 72e584c | 2025-05-20 22:02:14 +0000 | [diff] [blame] | 223 | lg2::error("Unknown error occurred in getDevicePath"); |
Shawn McCarney | 23dee38 | 2024-11-11 18:41:49 -0600 | [diff] [blame] | 224 | return {}; |
| 225 | } |
| 226 | } |
| 227 | |
| 228 | std::pair<uint8_t, uint8_t> parseDeviceName(const std::string& devName) |
| 229 | { |
| 230 | // Get I2C bus and device address, e.g. 3-0068 |
| 231 | // is parsed to bus 3, device address 0x68 |
| 232 | auto pos = devName.find('-'); |
| 233 | assert(pos != std::string::npos); |
| 234 | uint8_t busId = std::stoi(devName.substr(0, pos)); |
| 235 | uint8_t devAddr = std::stoi(devName.substr(pos + 1), nullptr, 16); |
| 236 | return {busId, devAddr}; |
| 237 | } |
| 238 | |
| 239 | bool usePsuJsonFile() |
| 240 | { |
| 241 | return checkFileExists(PSU_JSON_PATH); |
| 242 | } |
| 243 | |
Shawn McCarney | 14572cf | 2024-11-06 12:17:57 -0600 | [diff] [blame] | 244 | } // namespace utils |