blob: d643dbfb7cf3d34f2b5b3defb7b8003ba6e4f384 [file] [log] [blame]
Shawn McCarney14572cf2024-11-06 12:17:57 -06001/**
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 McCarney23dee382024-11-11 18:41:49 -060016#include "config.h"
17
Shawn McCarney14572cf2024-11-06 12:17:57 -060018#include "utils.hpp"
19
20#include "utility.hpp"
21
Anwaar Hadi72e584c2025-05-20 22:02:14 +000022#include <phosphor-logging/lg2.hpp>
Shawn McCarney14572cf2024-11-06 12:17:57 -060023#include <xyz/openbmc_project/Common/Device/error.hpp>
24
Shawn McCarney23dee382024-11-11 18:41:49 -060025#include <cassert>
Shawn McCarney14572cf2024-11-06 12:17:57 -060026#include <exception>
Shawn McCarney23dee382024-11-11 18:41:49 -060027#include <filesystem>
Shawn McCarney23dee382024-11-11 18:41:49 -060028#include <iomanip>
29#include <ios>
Shawn McCarney14572cf2024-11-06 12:17:57 -060030#include <iostream>
31#include <regex>
Shawn McCarney23dee382024-11-11 18:41:49 -060032#include <sstream>
Shawn McCarney14572cf2024-11-06 12:17:57 -060033#include <stdexcept>
34
35using namespace phosphor::logging;
36using namespace phosphor::power::util;
37using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Shawn McCarney23dee382024-11-11 18:41:49 -060038namespace fs = std::filesystem;
Shawn McCarney14572cf2024-11-06 12:17:57 -060039
40namespace utils
41{
42
43constexpr auto IBMCFFPSInterface =
44 "xyz.openbmc_project.Configuration.IBMCFFPSConnector";
45constexpr auto i2cBusProp = "I2CBus";
46constexpr auto i2cAddressProp = "I2CAddress";
47
48PsuI2cInfo 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 Hadi72e584c2025-05-20 22:02:14 +000098 lg2::warning("Error reading property {PROPERTY}: {ERROR}",
99 "PROPERTY", property.first, "ERROR", e);
Shawn McCarney14572cf2024-11-06 12:17:57 -0600100 }
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 Williams92261f82025-02-01 08:22:34 -0500118std::unique_ptr<phosphor::pmbus::PMBusBase> getPmbusIntf(std::uint64_t i2cBus,
119 std::uint64_t i2cAddr)
Shawn McCarney14572cf2024-11-06 12:17:57 -0600120{
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
126std::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 Hadi72e584c2025-05-20 22:02:14 +0000146 lg2::info(" {VPDNAME} resize needed. size: {SIZE}", "VPDNAME", vpdName,
147 "SIZE", vpdValue.size());
Shawn McCarney14572cf2024-11-06 12:17:57 -0600148 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
158bool checkFileExists(const std::string& filePath)
159{
160 try
161 {
162 return std::filesystem::exists(filePath);
163 }
164 catch (const std::exception& e)
165 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000166 lg2::error("Unable to check for existence of {FILEPATH}: {ERROR}",
167 "FILEPATH", filePath, "ERROR", e);
Shawn McCarney14572cf2024-11-06 12:17:57 -0600168 }
169 return false;
170}
171
Shawn McCarney23dee382024-11-11 18:41:49 -0600172std::string getDeviceName(std::string devPath)
173{
Faisal Awada5ace9fb2025-01-07 13:26:25 -0600174 if (devPath.empty())
175 {
176 return devPath;
177 }
Shawn McCarney23dee382024-11-11 18:41:49 -0600178 if (devPath.back() == '/')
179 {
180 devPath.pop_back();
181 }
182 return fs::path(devPath).stem().string();
183}
184
185std::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 Hadi72e584c2025-05-20 22:02:14 +0000200 lg2::warning("Unable to find psu devices or path");
Shawn McCarney23dee382024-11-11 18:41:49 -0600201 }
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 Hadi72e584c2025-05-20 22:02:14 +0000218 lg2::error("Error in getDevicePath: {ERROR}", "ERROR", e);
Shawn McCarney23dee382024-11-11 18:41:49 -0600219 return {};
220 }
221 catch (...)
222 {
Anwaar Hadi72e584c2025-05-20 22:02:14 +0000223 lg2::error("Unknown error occurred in getDevicePath");
Shawn McCarney23dee382024-11-11 18:41:49 -0600224 return {};
225 }
226}
227
228std::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
239bool usePsuJsonFile()
240{
241 return checkFileExists(PSU_JSON_PATH);
242}
243
Shawn McCarney14572cf2024-11-06 12:17:57 -0600244} // namespace utils