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