blob: fb73ba9aacca72b2d073aa1a383bb3840df6b408 [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
206 // Parse pin information from config file
207 auto pinsIterator = rootElement.find("pins");
208 if (pinsIterator != rootElement.end())
209 {
210 for (const auto& pinElement : *pinsIterator)
211 {
212 auto nameIterator = pinElement.find("name");
213 auto lineIterator = pinElement.find("line");
214
215 if (nameIterator != pinElement.end() &&
216 lineIterator != pinElement.end())
217 {
218 std::string name = (*nameIterator).get<std::string>();
Jim Wrightd8fc0682022-01-11 15:36:00 -0600219 unsigned int line = (*lineIterator).get<unsigned int>();
Jim Wrightd8a86172021-12-08 11:38:26 -0600220
221 Pin pin;
222 pin.name = name;
223 pin.line = line;
224 pins.emplace_back(std::move(pin));
225 }
226 else
227 {
228 log<level::ERR>(
229 fmt::format(
230 "No name or line found within pin in configuration file: {}",
231 pathName.string())
232 .c_str());
233 }
234 }
235 }
236 else
237 {
238 log<level::ERR>(
239 fmt::format("No pins found in configuration file: {}",
240 pathName.string())
241 .c_str());
242 }
243 log<level::DEBUG>(
244 fmt::format("Found number of pins: {}", rails.size()).c_str());
245 }
246 catch (const std::exception& e)
247 {
248 // Log error message in journal
249 log<level::ERR>(std::string("Exception parsing configuration file: " +
250 std::string(e.what()))
251 .c_str());
252 }
253}
254
Jim Wright71a14132022-01-28 09:46:46 -0600255void UCD90320Monitor::onFailure(bool timeout,
256 const std::string& powerSupplyError)
257{
Jim Wrightf6f0da92022-02-28 21:08:33 -0600258 std::string message;
Jim Wright71a14132022-01-28 09:46:46 -0600259 std::map<std::string, std::string> additionalData{};
Jim Wright71a14132022-01-28 09:46:46 -0600260
261 try
262 {
Jim Wright3accffe2022-03-10 17:19:42 -0600263 onFailureCheckRails(message, additionalData, powerSupplyError);
264 log<level::INFO>(
265 fmt::format("After onFailureCheckRails, message: {}", message)
266 .c_str());
267 onFailureCheckPins(message, additionalData);
268 log<level::INFO>(
269 fmt::format("After onFailureCheckPins, message: {}", message)
270 .c_str());
Jim Wright71a14132022-01-28 09:46:46 -0600271 }
272 catch (device_error::ReadFailure& e)
273 {
Jim Wrightf6f0da92022-02-28 21:08:33 -0600274 log<level::ERR>(
275 fmt::format("ReadFailure when collecting metadata, error {}",
276 e.what())
277 .c_str());
Jim Wright71a14132022-01-28 09:46:46 -0600278 }
Jim Wrightf6f0da92022-02-28 21:08:33 -0600279
280 if (message.empty())
281 {
282 // Could not isolate, but we know something failed, so issue a timeout
283 // or generic power good error
284 message = timeout ? "xyz.openbmc_project.Power.Error.PowerOnTimeout"
285 : "xyz.openbmc_project.Power.Error.Shutdown";
286 }
287 logError(message, additionalData);
Jim Wright71a14132022-01-28 09:46:46 -0600288}
289
Jim Wright3accffe2022-03-10 17:19:42 -0600290void UCD90320Monitor::onFailureCheckPins(
291 std::string& message, std::map<std::string, std::string>& additionalData)
292{
293 // Setup a list of all the GPIOs on the chip
294 gpiod::chip chip{"ucd90320", gpiod::chip::OPEN_BY_LABEL};
295 log<level::INFO>(fmt::format("GPIO chip name: {}", chip.name()).c_str());
296 log<level::INFO>(fmt::format("GPIO chip label: {}", chip.label()).c_str());
297 unsigned int numberLines = chip.num_lines();
298 log<level::INFO>(
299 fmt::format("GPIO chip number of lines: {}", numberLines).c_str());
300
301 // Workaround libgpiod bulk line maximum by getting values from individual
302 // lines
303 std::vector<int> values;
304 for (unsigned int offset = 0; offset < numberLines; ++offset)
305 {
306 gpiod::line line = chip.get_line(offset);
307 line.request({"phosphor-power-control",
308 gpiod::line_request::DIRECTION_INPUT, 0});
309 values.push_back(line.get_value());
310 line.release();
311 }
312
313 // Add GPIO values to additional data
314 log<level::INFO>(fmt::format("GPIO values: {}", values).c_str());
315 additionalData.emplace("GPIO_VALUES", fmt::format("{}", values));
316
317 // Only check GPIOs if no rail fail was found
318 if (message.empty())
319 {
320 for (size_t pin = 0; pin < pins.size(); ++pin)
321 {
322 unsigned int line = pins[pin].line;
323 if (line < values.size())
324 {
325 int value = values[line];
326 if (value == 0)
327 {
328 additionalData.emplace("INPUT_NUM",
329 fmt::format("{}", line));
330 additionalData.emplace("INPUT_NAME", pins[pin].name);
331 additionalData.emplace("INPUT_STATUS",
332 fmt::format("{}", value));
333 message =
334 "xyz.openbmc_project.Power.Error.PowerSequencerPGOODFault";
335 return;
336 }
337 }
338 }
339 }
340}
341
342void UCD90320Monitor::onFailureCheckRails(
343 std::string& message, std::map<std::string, std::string>& additionalData,
344 const std::string& powerSupplyError)
345{
346 auto statusWord = readStatusWord();
347 additionalData.emplace("STATUS_WORD", fmt::format("{:#06x}", statusWord));
348 try
349 {
350 additionalData.emplace("MFR_STATUS",
351 fmt::format("{:#010x}", readMFRStatus()));
352 }
353 catch (device_error::ReadFailure& e)
354 {
355 log<level::ERR>(
356 fmt::format("ReadFailure when collecting MFR_STATUS, error {}",
357 e.what())
358 .c_str());
359 }
360
361 // The status_word register has a summary bit to tell us if each page even
362 // needs to be checked
363 if (statusWord & status_word::VOUT_FAULT)
364 {
365 constexpr size_t numberPages = 24;
366 for (size_t page = 0; page < numberPages; page++)
367 {
368 auto statusVout = pmbusInterface.insertPageNum(STATUS_VOUT, page);
Jim Wright2a054922022-04-13 09:39:27 -0500369 if (pmbusInterface.exists(statusVout, Type::Debug))
Jim Wright3accffe2022-03-10 17:19:42 -0600370 {
Jim Wright2a054922022-04-13 09:39:27 -0500371 uint8_t vout = pmbusInterface.read(statusVout, Type::Debug);
Jim Wright3accffe2022-03-10 17:19:42 -0600372
Jim Wright2a054922022-04-13 09:39:27 -0500373 // If any bits are on log them, though some are just warnings so
374 // they won't cause errors
375 if (vout)
376 {
377 log<level::INFO>(
378 fmt::format("STATUS_VOUT, page: {}, value: {:#04x}",
379 page, vout)
380 .c_str());
381 }
Jim Wright3accffe2022-03-10 17:19:42 -0600382
Jim Wright2a054922022-04-13 09:39:27 -0500383 // Log errors if any non-warning bits on
384 if (vout & ~status_vout::WARNING_MASK)
385 {
386 additionalData.emplace("STATUS_VOUT",
387 fmt::format("{:#04x}", vout));
388 additionalData.emplace("PAGE", fmt::format("{}", page));
389 if (page < rails.size())
390 {
391 additionalData.emplace("RAIL_NAME", rails[page]);
392 }
393
394 // Use power supply error if set and 12v rail has failed,
395 // else use voltage error
396 message =
397 ((page == 0) && !powerSupplyError.empty())
398 ? powerSupplyError
399 : "xyz.openbmc_project.Power.Error.PowerSequencerVoltageFault";
400 return;
401 }
Jim Wright3accffe2022-03-10 17:19:42 -0600402 }
403 }
404 }
405}
406
Jim Wright71a14132022-01-28 09:46:46 -0600407uint16_t UCD90320Monitor::readStatusWord()
408{
409 return pmbusInterface.read(STATUS_WORD, Type::Debug);
410}
411
412uint32_t UCD90320Monitor::readMFRStatus()
413{
414 const std::string mfrStatus = "mfr_status";
415 return pmbusInterface.read(mfrStatus, Type::HwmonDeviceDebug);
416}
417
Jim Wright7945dd22021-04-06 16:55:15 -0500418} // namespace phosphor::power::sequencer