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