blob: c54c4a4c05c767fff02ede1fcac192af8594415e [file] [log] [blame]
Jim Wright1553cd92021-03-31 16:11:59 -05001/**
2 * Copyright © 2021 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 */
16
Jim Wright7945dd22021-04-06 16:55:15 -050017#include "ucd90320_monitor.hpp"
18
Jim Wright56ae78e2021-12-01 14:46:15 -060019#include "utility.hpp"
Jim Wright7945dd22021-04-06 16:55:15 -050020
Jim Wright56ae78e2021-12-01 14:46:15 -060021#include <fmt/format.h>
22#include <fmt/ranges.h>
23
Jim Wrightd8a86172021-12-08 11:38:26 -060024#include <nlohmann/json.hpp>
Jim Wright56ae78e2021-12-01 14:46:15 -060025#include <phosphor-logging/log.hpp>
26#include <sdbusplus/bus.hpp>
Jim Wright71a14132022-01-28 09:46:46 -060027#include <xyz/openbmc_project/Common/Device/error.hpp>
Jim Wright56ae78e2021-12-01 14:46:15 -060028
Jim Wrightd8a86172021-12-08 11:38:26 -060029#include <fstream>
Jim Wright56ae78e2021-12-01 14:46:15 -060030#include <map>
Jim Wright7945dd22021-04-06 16:55:15 -050031#include <string>
32
33namespace phosphor::power::sequencer
Jim Wright1553cd92021-03-31 16:11:59 -050034{
Jim Wright7945dd22021-04-06 16:55:15 -050035
Jim Wrightd8a86172021-12-08 11:38:26 -060036using json = nlohmann::json;
Jim Wright71a14132022-01-28 09:46:46 -060037using namespace pmbus;
Jim Wright56ae78e2021-12-01 14:46:15 -060038using namespace phosphor::logging;
39using namespace phosphor::power;
40
41const std::string compatibleInterface =
42 "xyz.openbmc_project.Configuration.IBMCompatibleSystem";
43const std::string compatibleNamesProperty = "Names";
44
Jim Wright71a14132022-01-28 09:46:46 -060045namespace device_error = sdbusplus::xyz::openbmc_project::Common::Device::Error;
46
Jim Wright7945dd22021-04-06 16:55:15 -050047UCD90320Monitor::UCD90320Monitor(sdbusplus::bus::bus& bus, std::uint8_t i2cBus,
48 std::uint16_t i2cAddress) :
Jim Wright930458c2022-01-24 14:37:27 -060049 PowerSequencerMonitor(bus),
50 match{bus,
51 sdbusplus::bus::match::rules::interfacesAdded() +
52 sdbusplus::bus::match::rules::sender(
53 "xyz.openbmc_project.EntityManager"),
54 std::bind(&UCD90320Monitor::interfacesAddedHandler, this,
55 std::placeholders::_1)},
Jim Wright56ae78e2021-12-01 14:46:15 -060056 pmbusInterface{
57 fmt::format("/sys/bus/i2c/devices/{}-{:04x}", i2cBus, i2cAddress)
58 .c_str(),
59 "ucd9000", 0}
60
61{
62 // Use the compatible system types information, if already available, to
63 // load the configuration file
64 findCompatibleSystemTypes();
Jim Wright1553cd92021-03-31 16:11:59 -050065}
Jim Wright56ae78e2021-12-01 14:46:15 -060066
67void UCD90320Monitor::findCompatibleSystemTypes()
68{
69 try
70 {
71 auto subTree = util::getSubTree(bus, "/xyz/openbmc_project/inventory",
72 compatibleInterface, 0);
73
74 auto objectIt = subTree.cbegin();
75 if (objectIt != subTree.cend())
76 {
77 const auto& objPath = objectIt->first;
78
79 // Get the first service name
80 auto serviceIt = objectIt->second.cbegin();
81 if (serviceIt != objectIt->second.cend())
82 {
83 std::string service = serviceIt->first;
84 if (!service.empty())
85 {
86 std::vector<std::string> compatibleSystemTypes;
87
88 // Get compatible system types property value
89 util::getProperty(compatibleInterface,
90 compatibleNamesProperty, objPath, service,
91 bus, compatibleSystemTypes);
92
93 log<level::DEBUG>(
94 fmt::format("Found compatible systems: {}",
95 compatibleSystemTypes)
96 .c_str());
97 // Use compatible systems information to find config file
Jim Wrightd8a86172021-12-08 11:38:26 -060098 findConfigFile(compatibleSystemTypes);
Jim Wright56ae78e2021-12-01 14:46:15 -060099 }
100 }
101 }
102 }
103 catch (const std::exception&)
104 {
105 // Compatible system types information is not available.
106 }
107}
108
Jim Wrightd8a86172021-12-08 11:38:26 -0600109void UCD90320Monitor::findConfigFile(
110 const std::vector<std::string>& compatibleSystemTypes)
111{
112 // Expected config file path name:
113 // /usr/share/phosphor-power-sequencer/UCD90320Monitor_<systemType>.json
114
115 // Add possible file names based on compatible system types (if any)
116 for (const std::string& systemType : compatibleSystemTypes)
117 {
118 // Check if file exists
119 std::filesystem::path pathName{
120 "/usr/share/phosphor-power-sequencer/UCD90320Monitor_" +
121 systemType + ".json"};
122 if (std::filesystem::exists(pathName))
123 {
124 log<level::INFO>(
125 fmt::format("Config file path: {}", pathName.string()).c_str());
126 parseConfigFile(pathName);
127 break;
128 }
129 }
130}
131
Jim Wright56ae78e2021-12-01 14:46:15 -0600132void UCD90320Monitor::interfacesAddedHandler(sdbusplus::message::message& msg)
133{
Jim Wrightd8a86172021-12-08 11:38:26 -0600134 // Only continue if message is valid and rails / pins have not already been
135 // found
136 if (!msg || !rails.empty())
Jim Wright56ae78e2021-12-01 14:46:15 -0600137 {
138 return;
139 }
140
141 try
142 {
143 // Read the dbus message
144 sdbusplus::message::object_path objPath;
145 std::map<std::string,
146 std::map<std::string, std::variant<std::vector<std::string>>>>
147 interfaces;
148 msg.read(objPath, interfaces);
149
150 // Find the compatible interface, if present
151 auto itIntf = interfaces.find(compatibleInterface);
152 if (itIntf != interfaces.cend())
153 {
154 // Find the Names property of the compatible interface, if present
155 auto itProp = itIntf->second.find(compatibleNamesProperty);
156 if (itProp != itIntf->second.cend())
157 {
158 // Get value of Names property
159 const auto& propValue = std::get<0>(itProp->second);
160 if (!propValue.empty())
161 {
162 log<level::INFO>(
163 fmt::format(
164 "InterfacesAdded for compatible systems: {}",
165 propValue)
166 .c_str());
167
168 // Use compatible systems information to find config file
Jim Wrightd8a86172021-12-08 11:38:26 -0600169 findConfigFile(propValue);
Jim Wright56ae78e2021-12-01 14:46:15 -0600170 }
171 }
172 }
173 }
174 catch (const std::exception&)
175 {
176 // Error trying to read interfacesAdded message.
177 }
178}
Jim Wright7945dd22021-04-06 16:55:15 -0500179
Jim Wrightd8a86172021-12-08 11:38:26 -0600180void UCD90320Monitor::parseConfigFile(const std::filesystem::path& pathName)
181{
182 try
183 {
184 std::ifstream file{pathName};
185 json rootElement = json::parse(file);
186
187 // Parse rail information from config file
188 auto railsIterator = rootElement.find("rails");
189 if (railsIterator != rootElement.end())
190 {
191 for (const auto& railElement : *railsIterator)
192 {
193 std::string rail = railElement.get<std::string>();
194 rails.emplace_back(std::move(rail));
195 }
196 }
197 else
198 {
199 log<level::ERR>(
200 fmt::format("No rails found in configuration file: {}",
201 pathName.string())
202 .c_str());
203 }
204 log<level::DEBUG>(fmt::format("Found rails: {}", rails).c_str());
205
Jim Wrightd8fc0682022-01-11 15:36:00 -0600206 // List of line offsets for libgpiod
207 std::vector<unsigned int> offsets;
208
Jim Wrightd8a86172021-12-08 11:38:26 -0600209 // Parse pin information from config file
210 auto pinsIterator = rootElement.find("pins");
211 if (pinsIterator != rootElement.end())
212 {
213 for (const auto& pinElement : *pinsIterator)
214 {
215 auto nameIterator = pinElement.find("name");
216 auto lineIterator = pinElement.find("line");
217
218 if (nameIterator != pinElement.end() &&
219 lineIterator != pinElement.end())
220 {
221 std::string name = (*nameIterator).get<std::string>();
Jim Wrightd8fc0682022-01-11 15:36:00 -0600222 unsigned int line = (*lineIterator).get<unsigned int>();
Jim Wrightd8a86172021-12-08 11:38:26 -0600223
224 Pin pin;
225 pin.name = name;
226 pin.line = line;
227 pins.emplace_back(std::move(pin));
Jim Wrightd8fc0682022-01-11 15:36:00 -0600228 offsets.emplace_back(line);
Jim Wrightd8a86172021-12-08 11:38:26 -0600229 }
230 else
231 {
232 log<level::ERR>(
233 fmt::format(
234 "No name or line found within pin in configuration file: {}",
235 pathName.string())
236 .c_str());
237 }
238 }
239 }
240 else
241 {
242 log<level::ERR>(
243 fmt::format("No pins found in configuration file: {}",
244 pathName.string())
245 .c_str());
246 }
247 log<level::DEBUG>(
248 fmt::format("Found number of pins: {}", rails.size()).c_str());
Jim Wrightd8fc0682022-01-11 15:36:00 -0600249 setUpGpio(offsets);
Jim Wrightd8a86172021-12-08 11:38:26 -0600250 }
251 catch (const std::exception& e)
252 {
253 // Log error message in journal
254 log<level::ERR>(std::string("Exception parsing configuration file: " +
255 std::string(e.what()))
256 .c_str());
257 }
258}
259
Jim Wright71a14132022-01-28 09:46:46 -0600260void UCD90320Monitor::onFailure(bool timeout,
261 const std::string& powerSupplyError)
262{
Jim Wrightf6f0da92022-02-28 21:08:33 -0600263 std::string message;
Jim Wright71a14132022-01-28 09:46:46 -0600264 std::map<std::string, std::string> additionalData{};
Jim Wright71a14132022-01-28 09:46:46 -0600265
266 try
267 {
Jim Wrightf6f0da92022-02-28 21:08:33 -0600268 auto statusWord = readStatusWord();
269 additionalData.emplace("STATUS_WORD",
270 fmt::format("{:#04x}", statusWord));
271 try
Jim Wright71a14132022-01-28 09:46:46 -0600272 {
Jim Wrightf6f0da92022-02-28 21:08:33 -0600273 additionalData.emplace("MFR_STATUS",
274 fmt::format("{:#04x}", readMFRStatus()));
275 }
276 catch (device_error::ReadFailure& e)
277 {
278 log<level::ERR>(
279 fmt::format("ReadFailure when collecting MFR_STATUS, error {}",
280 e.what())
281 .c_str());
282 }
283
284 // The status_word register has a summary bit to tell us if each page
285 // even needs to be checked
286 if (statusWord & status_word::VOUT_FAULT)
287 {
288 constexpr size_t numberPages = 24;
289 for (size_t page = 0; page < numberPages; page++)
290 {
291 auto statusVout =
292 pmbusInterface.insertPageNum(STATUS_VOUT, page);
293 uint8_t vout = pmbusInterface.read(statusVout, Type::Debug);
294
295 // If any bits are on log them, though some are just warnings so
296 // they won't cause errors
297 if (vout)
298 {
299 log<level::INFO>(
300 fmt::format("STATUS_VOUT, page: {}, value: {:#02x}",
301 page, vout)
302 .c_str());
303 }
304
305 // Log errors if any non-warning bits on
306 if (vout & ~status_vout::WARNING_MASK)
307 {
308 additionalData.emplace("STATUS_VOUT",
309 fmt::format("{:#02x}", vout));
310 additionalData.emplace("PAGE", fmt::format("{}", page));
311 additionalData.emplace("RAIL_NAME", rails[page]);
312
313 // Use power supply error if set and 12v rail has failed,
314 // else use voltage error
315 message =
316 ((page == 0) && !powerSupplyError.empty())
317 ? powerSupplyError
318 : "xyz.openbmc_project.Power.Error.PowerSequencerVoltageFault";
319 break;
320 }
321 }
322 }
323
324 // Check only the GPIs configured on this system.
325 std::vector<int> values = lines.get_values();
326 additionalData.emplace("GPIO_VALUES", fmt::format("{}", values));
327
328 if (message.empty())
329 {
330 // Didn't find a rail fail, examine pins
331 for (size_t pin = 0; pin < pins.size(); ++pin)
332 {
333 if (pin < values.size() && !values[pin])
334 {
335 additionalData.emplace("INPUT_NUM",
336 fmt::format("{}", pins[pin].line));
337 additionalData.emplace("INPUT_NAME", pins[pin].name);
338 additionalData.emplace("INPUT_STATUS",
339 fmt::format("{}", values[pin]));
340 message =
341 "xyz.openbmc_project.Power.Error.PowerSequencerPGOODFault";
342 break;
343 }
344 }
Jim Wright71a14132022-01-28 09:46:46 -0600345 }
346 }
347 catch (device_error::ReadFailure& e)
348 {
Jim Wrightf6f0da92022-02-28 21:08:33 -0600349 log<level::ERR>(
350 fmt::format("ReadFailure when collecting metadata, error {}",
351 e.what())
352 .c_str());
Jim Wright71a14132022-01-28 09:46:46 -0600353 }
Jim Wrightf6f0da92022-02-28 21:08:33 -0600354
355 if (message.empty())
356 {
357 // Could not isolate, but we know something failed, so issue a timeout
358 // or generic power good error
359 message = timeout ? "xyz.openbmc_project.Power.Error.PowerOnTimeout"
360 : "xyz.openbmc_project.Power.Error.Shutdown";
361 }
362 logError(message, additionalData);
Jim Wright71a14132022-01-28 09:46:46 -0600363}
364
365uint16_t UCD90320Monitor::readStatusWord()
366{
367 return pmbusInterface.read(STATUS_WORD, Type::Debug);
368}
369
370uint32_t UCD90320Monitor::readMFRStatus()
371{
372 const std::string mfrStatus = "mfr_status";
373 return pmbusInterface.read(mfrStatus, Type::HwmonDeviceDebug);
374}
375
Jim Wrightd8fc0682022-01-11 15:36:00 -0600376void UCD90320Monitor::setUpGpio(const std::vector<unsigned int>& offsets)
377{
378 gpiod::chip chip{"ucd90320", gpiod::chip::OPEN_BY_LABEL};
379 lines = chip.get_lines(offsets);
380 lines.request(
381 {"phosphor-power-control", gpiod::line_request::DIRECTION_INPUT, 0});
382}
383
Jim Wright7945dd22021-04-06 16:55:15 -0500384} // namespace phosphor::power::sequencer