blob: f2bb0575f7a04721fd3df20a6932155ee00990b3 [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
Jim Wright71a14132022-01-28 09:46:46 -060067bool UCD90320Monitor::checkPGOODFaults(
68 std::map<std::string, std::string>& additionalData)
69{
70 // Check only the GPIs configured on this system.
71 std::vector<int> values = lines.get_values();
72
73 bool errorCreated = false;
74 for (size_t pin = 0; pin < pins.size(); ++pin)
75 {
76 if (pin < values.size() && !values[pin])
77 {
78 try
79 {
80 additionalData.emplace(
81 "STATUS_WORD", fmt::format("{:#04x}", readStatusWord()));
82 additionalData.emplace("MFR_STATUS",
83 fmt::format("{:#04x}", readMFRStatus()));
84 }
85 catch (device_error::ReadFailure& e)
86 {
87 log<level::ERR>("ReadFailure when collecting metadata");
88 }
89 additionalData.emplace("INPUT_NUM",
90 fmt::format("{}", pins[pin].line));
91 additionalData.emplace("INPUT_NAME", pins[pin].name);
92 additionalData.emplace("INPUT_STATUS",
93 fmt::format("{}", values[pin]));
94
95 logError("xyz.openbmc_project.Power.Error.PowerSequencerPGOODFault",
96 additionalData);
97
98 errorCreated = true;
99 break;
100 }
101 }
102 return errorCreated;
103}
104
105bool UCD90320Monitor::checkVOUTFaults(
106 std::map<std::string, std::string>& additionalData)
107{
108 // The status_word register has a summary bit to tell us
109 // if each page even needs to be checked
110 auto statusWord = readStatusWord();
111 if (!(statusWord & status_word::VOUT_FAULT))
112 {
113 return false;
114 }
115
116 constexpr size_t numberPages = 24;
117 bool errorCreated = false;
118 for (size_t page = 0; page < numberPages; page++)
119 {
120 auto statusVout = pmbusInterface.insertPageNum(STATUS_VOUT, page);
121 uint8_t vout = pmbusInterface.read(statusVout, Type::Debug);
122
123 // If any bits are on log them, though some are just
124 // warnings so they won't cause errors
125 if (vout)
126 {
127 log<level::INFO>("A voltage rail has bits on in STATUS_VOUT",
128 entry("STATUS_VOUT=0x%X", vout),
129 entry("PAGE=%d", page));
130 }
131
132 // Log errors if any non-warning bits on
133 if (vout & ~status_vout::WARNING_MASK)
134 {
135 auto railName = rails[page];
136
137 additionalData.emplace("STATUS_WORD",
138 fmt::format("{:#04x}", statusWord));
139 additionalData.emplace("STATUS_VOUT", fmt::format("{:#02x}", vout));
140 try
141 {
142 additionalData.emplace("MFR_STATUS",
143 fmt::format("{:#04x}", readMFRStatus()));
144 }
145 catch (device_error::ReadFailure& e)
146 {
147 log<level::ERR>("ReadFailure when collecting MFR_STATUS");
148 }
149 additionalData.emplace("RAIL", fmt::format("{}", page));
150 additionalData.emplace("RAIL_NAME", railName);
151
152 logError(
153 "xyz.openbmc_project.Power.Error.PowerSequencerVoltageFault",
154 additionalData);
155
156 errorCreated = true;
157 break;
158 }
159 }
160
161 return errorCreated;
162}
163
Jim Wright56ae78e2021-12-01 14:46:15 -0600164void UCD90320Monitor::findCompatibleSystemTypes()
165{
166 try
167 {
168 auto subTree = util::getSubTree(bus, "/xyz/openbmc_project/inventory",
169 compatibleInterface, 0);
170
171 auto objectIt = subTree.cbegin();
172 if (objectIt != subTree.cend())
173 {
174 const auto& objPath = objectIt->first;
175
176 // Get the first service name
177 auto serviceIt = objectIt->second.cbegin();
178 if (serviceIt != objectIt->second.cend())
179 {
180 std::string service = serviceIt->first;
181 if (!service.empty())
182 {
183 std::vector<std::string> compatibleSystemTypes;
184
185 // Get compatible system types property value
186 util::getProperty(compatibleInterface,
187 compatibleNamesProperty, objPath, service,
188 bus, compatibleSystemTypes);
189
190 log<level::DEBUG>(
191 fmt::format("Found compatible systems: {}",
192 compatibleSystemTypes)
193 .c_str());
194 // Use compatible systems information to find config file
Jim Wrightd8a86172021-12-08 11:38:26 -0600195 findConfigFile(compatibleSystemTypes);
Jim Wright56ae78e2021-12-01 14:46:15 -0600196 }
197 }
198 }
199 }
200 catch (const std::exception&)
201 {
202 // Compatible system types information is not available.
203 }
204}
205
Jim Wrightd8a86172021-12-08 11:38:26 -0600206void UCD90320Monitor::findConfigFile(
207 const std::vector<std::string>& compatibleSystemTypes)
208{
209 // Expected config file path name:
210 // /usr/share/phosphor-power-sequencer/UCD90320Monitor_<systemType>.json
211
212 // Add possible file names based on compatible system types (if any)
213 for (const std::string& systemType : compatibleSystemTypes)
214 {
215 // Check if file exists
216 std::filesystem::path pathName{
217 "/usr/share/phosphor-power-sequencer/UCD90320Monitor_" +
218 systemType + ".json"};
219 if (std::filesystem::exists(pathName))
220 {
221 log<level::INFO>(
222 fmt::format("Config file path: {}", pathName.string()).c_str());
223 parseConfigFile(pathName);
224 break;
225 }
226 }
227}
228
Jim Wright56ae78e2021-12-01 14:46:15 -0600229void UCD90320Monitor::interfacesAddedHandler(sdbusplus::message::message& msg)
230{
Jim Wrightd8a86172021-12-08 11:38:26 -0600231 // Only continue if message is valid and rails / pins have not already been
232 // found
233 if (!msg || !rails.empty())
Jim Wright56ae78e2021-12-01 14:46:15 -0600234 {
235 return;
236 }
237
238 try
239 {
240 // Read the dbus message
241 sdbusplus::message::object_path objPath;
242 std::map<std::string,
243 std::map<std::string, std::variant<std::vector<std::string>>>>
244 interfaces;
245 msg.read(objPath, interfaces);
246
247 // Find the compatible interface, if present
248 auto itIntf = interfaces.find(compatibleInterface);
249 if (itIntf != interfaces.cend())
250 {
251 // Find the Names property of the compatible interface, if present
252 auto itProp = itIntf->second.find(compatibleNamesProperty);
253 if (itProp != itIntf->second.cend())
254 {
255 // Get value of Names property
256 const auto& propValue = std::get<0>(itProp->second);
257 if (!propValue.empty())
258 {
259 log<level::INFO>(
260 fmt::format(
261 "InterfacesAdded for compatible systems: {}",
262 propValue)
263 .c_str());
264
265 // Use compatible systems information to find config file
Jim Wrightd8a86172021-12-08 11:38:26 -0600266 findConfigFile(propValue);
Jim Wright56ae78e2021-12-01 14:46:15 -0600267 }
268 }
269 }
270 }
271 catch (const std::exception&)
272 {
273 // Error trying to read interfacesAdded message.
274 }
275}
Jim Wright7945dd22021-04-06 16:55:15 -0500276
Jim Wrightd8a86172021-12-08 11:38:26 -0600277void UCD90320Monitor::parseConfigFile(const std::filesystem::path& pathName)
278{
279 try
280 {
281 std::ifstream file{pathName};
282 json rootElement = json::parse(file);
283
284 // Parse rail information from config file
285 auto railsIterator = rootElement.find("rails");
286 if (railsIterator != rootElement.end())
287 {
288 for (const auto& railElement : *railsIterator)
289 {
290 std::string rail = railElement.get<std::string>();
291 rails.emplace_back(std::move(rail));
292 }
293 }
294 else
295 {
296 log<level::ERR>(
297 fmt::format("No rails found in configuration file: {}",
298 pathName.string())
299 .c_str());
300 }
301 log<level::DEBUG>(fmt::format("Found rails: {}", rails).c_str());
302
Jim Wrightd8fc0682022-01-11 15:36:00 -0600303 // List of line offsets for libgpiod
304 std::vector<unsigned int> offsets;
305
Jim Wrightd8a86172021-12-08 11:38:26 -0600306 // Parse pin information from config file
307 auto pinsIterator = rootElement.find("pins");
308 if (pinsIterator != rootElement.end())
309 {
310 for (const auto& pinElement : *pinsIterator)
311 {
312 auto nameIterator = pinElement.find("name");
313 auto lineIterator = pinElement.find("line");
314
315 if (nameIterator != pinElement.end() &&
316 lineIterator != pinElement.end())
317 {
318 std::string name = (*nameIterator).get<std::string>();
Jim Wrightd8fc0682022-01-11 15:36:00 -0600319 unsigned int line = (*lineIterator).get<unsigned int>();
Jim Wrightd8a86172021-12-08 11:38:26 -0600320
321 Pin pin;
322 pin.name = name;
323 pin.line = line;
324 pins.emplace_back(std::move(pin));
Jim Wrightd8fc0682022-01-11 15:36:00 -0600325 offsets.emplace_back(line);
Jim Wrightd8a86172021-12-08 11:38:26 -0600326 }
327 else
328 {
329 log<level::ERR>(
330 fmt::format(
331 "No name or line found within pin in configuration file: {}",
332 pathName.string())
333 .c_str());
334 }
335 }
336 }
337 else
338 {
339 log<level::ERR>(
340 fmt::format("No pins found in configuration file: {}",
341 pathName.string())
342 .c_str());
343 }
344 log<level::DEBUG>(
345 fmt::format("Found number of pins: {}", rails.size()).c_str());
Jim Wrightd8fc0682022-01-11 15:36:00 -0600346 setUpGpio(offsets);
Jim Wrightd8a86172021-12-08 11:38:26 -0600347 }
348 catch (const std::exception& e)
349 {
350 // Log error message in journal
351 log<level::ERR>(std::string("Exception parsing configuration file: " +
352 std::string(e.what()))
353 .c_str());
354 }
355}
356
Jim Wright71a14132022-01-28 09:46:46 -0600357void UCD90320Monitor::onFailure(bool timeout,
358 const std::string& powerSupplyError)
359{
360 std::map<std::string, std::string> additionalData{};
361 if (!powerSupplyError.empty())
362 {
363 logError(powerSupplyError, additionalData);
364 return;
365 }
366
367 try
368 {
369 bool voutError = checkVOUTFaults(additionalData);
370 bool pgoodError = checkPGOODFaults(additionalData);
371
372 // Not a voltage or PGOOD fault, but we know something
373 // failed so still create an error log.
374 if (!voutError && !pgoodError)
375 {
376 // Default to generic pgood error
377 logError("xyz.openbmc_project.Power.Error.Shutdown",
378 additionalData);
379 }
380 }
381 catch (device_error::ReadFailure& e)
382 {
383 log<level::ERR>("ReadFailure when collecting metadata");
384
385 if (timeout)
386 {
387 // Default to timeout error
388 logError("xyz.openbmc_project.Power.Error.PowerOnTimeout",
389 additionalData);
390 }
391 else
392 {
393 // Default to generic pgood error
394 logError("xyz.openbmc_project.Power.Error.Shutdown",
395 additionalData);
396 }
397 }
398}
399
400uint16_t UCD90320Monitor::readStatusWord()
401{
402 return pmbusInterface.read(STATUS_WORD, Type::Debug);
403}
404
405uint32_t UCD90320Monitor::readMFRStatus()
406{
407 const std::string mfrStatus = "mfr_status";
408 return pmbusInterface.read(mfrStatus, Type::HwmonDeviceDebug);
409}
410
Jim Wrightd8fc0682022-01-11 15:36:00 -0600411void UCD90320Monitor::setUpGpio(const std::vector<unsigned int>& offsets)
412{
413 gpiod::chip chip{"ucd90320", gpiod::chip::OPEN_BY_LABEL};
414 lines = chip.get_lines(offsets);
415 lines.request(
416 {"phosphor-power-control", gpiod::line_request::DIRECTION_INPUT, 0});
417}
418
Jim Wright7945dd22021-04-06 16:55:15 -0500419} // namespace phosphor::power::sequencer