blob: 847e686950ff352a90d06ac0f83d11a95fea35cc [file] [log] [blame]
Brandon Wyman1d7a7df2020-03-26 10:14:05 -05001#include "config.h"
2
Brandon Wymanaed1f752019-11-25 18:10:52 -06003#include "power_supply.hpp"
4
5#include "types.hpp"
Brandon Wyman3f1242f2020-01-28 13:11:25 -06006#include "util.hpp"
Brandon Wymanaed1f752019-11-25 18:10:52 -06007
Brandon Wymandf13c3a2020-12-15 14:25:22 -06008#include <fmt/format.h>
9
Brandon Wyman3f1242f2020-01-28 13:11:25 -060010#include <xyz/openbmc_project/Common/Device/error.hpp>
11
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050012#include <chrono> // sleep_for()
13#include <cstdint> // uint8_t...
14#include <thread> // sleep_for()
15
Brandon Wyman3f1242f2020-01-28 13:11:25 -060016namespace phosphor::power::psu
Brandon Wymanaed1f752019-11-25 18:10:52 -060017{
18
19using namespace phosphor::logging;
Brandon Wyman3f1242f2020-01-28 13:11:25 -060020using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Brandon Wymanaed1f752019-11-25 18:10:52 -060021
Brandon Wyman510acaa2020-11-05 18:32:04 -060022PowerSupply::PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath,
23 std::uint8_t i2cbus, std::uint16_t i2caddr) :
24 bus(bus),
25 inventoryPath(invpath)
26{
27 if (inventoryPath.empty())
28 {
29 throw std::invalid_argument{"Invalid empty inventoryPath"};
30 }
31
32 // Setup the functions to call when the D-Bus inventory path for the
33 // Present property changes.
34 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
35 bus,
36 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
37 INVENTORY_IFACE),
38 [this](auto& msg) { this->inventoryChanged(msg); });
39
40 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
41 bus,
42 sdbusplus::bus::match::rules::interfacesAdded() +
43 sdbusplus::bus::match::rules::argNpath(0, inventoryPath),
44 [this](auto& msg) { this->inventoryAdded(msg); });
45
46 std::ostringstream ss;
47 ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
48 std::string addrStr = ss.str();
49 pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr);
50
51 // Get the current state of the Present property.
52 updatePresence();
Brandon Wymancb091802021-04-26 15:26:27 -050053 updateInventory();
Brandon Wyman510acaa2020-11-05 18:32:04 -060054}
55
Brandon Wymanaed1f752019-11-25 18:10:52 -060056void PowerSupply::updatePresence()
57{
58 try
59 {
Brandon Wyman3f1242f2020-01-28 13:11:25 -060060 present = getPresence(bus, inventoryPath);
Brandon Wymanaed1f752019-11-25 18:10:52 -060061 }
62 catch (const sdbusplus::exception::SdBusError& e)
63 {
64 // Relying on property change or interface added to retry.
65 // Log an informational trace to the journal.
Brandon Wymandf13c3a2020-12-15 14:25:22 -060066 log<level::INFO>(
67 fmt::format("D-Bus property {} access failure exception",
68 inventoryPath)
69 .c_str());
Brandon Wymanaed1f752019-11-25 18:10:52 -060070 }
71}
72
Brandon Wyman3f1242f2020-01-28 13:11:25 -060073void PowerSupply::analyze()
74{
75 using namespace phosphor::pmbus;
76
Brandon Wymanf65c4062020-08-19 13:15:53 -050077 if ((present) && (readFail < LOG_LIMIT))
Brandon Wyman3f1242f2020-01-28 13:11:25 -060078 {
79 try
80 {
Brandon Wymanfed0ba22020-09-26 20:02:51 -050081 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug);
Brandon Wymanf65c4062020-08-19 13:15:53 -050082 // Read worked, reset the fail count.
83 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -060084
85 if (statusWord)
86 {
Jay Meyer10d94052020-11-30 14:41:21 -060087 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman3f1242f2020-01-28 13:11:25 -060088 if (statusWord & status_word::INPUT_FAULT_WARN)
89 {
90 if (!inputFault)
91 {
Jay Meyer10d94052020-11-30 14:41:21 -060092 log<level::INFO>(fmt::format("INPUT fault: "
93 "status word = {:#04x}, "
94 "MFR fault = {:#02x}",
95 statusWord, statusMFR)
96 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -060097 }
98
99 faultFound = true;
100 inputFault = true;
101 }
102
103 if (statusWord & status_word::MFR_SPECIFIC_FAULT)
104 {
105 if (!mfrFault)
106 {
Jay Meyer10d94052020-11-30 14:41:21 -0600107 log<level::ERR>(fmt::format("MFR fault: "
108 "status word = {:#04x} "
109 "MFR fault = {:#02x}",
110 statusWord, statusMFR)
111 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600112 }
113 faultFound = true;
114 mfrFault = true;
115 }
116
117 if (statusWord & status_word::VIN_UV_FAULT)
118 {
119 if (!vinUVFault)
120 {
Jay Meyer10d94052020-11-30 14:41:21 -0600121 log<level::INFO>(fmt::format("VIN_UV fault: "
122 "status word = {:#04x}, "
123 "MFR fault = {:#02x}",
124 statusWord, statusMFR)
125 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600126 }
127
128 faultFound = true;
129 vinUVFault = true;
130 }
131 }
132 else
133 {
134 faultFound = false;
135 inputFault = false;
136 mfrFault = false;
137 vinUVFault = false;
138 }
139 }
140 catch (ReadFailure& e)
141 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500142 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600143 phosphor::logging::commit<ReadFailure>();
144 }
145 }
146}
147
Brandon Wyman59a35792020-06-04 12:37:40 -0500148void PowerSupply::onOffConfig(uint8_t data)
149{
150 using namespace phosphor::pmbus;
151
152 if (present)
153 {
154 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
155 try
156 {
157 std::vector<uint8_t> configData{data};
158 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
159 Type::HwmonDeviceDebug);
160 }
161 catch (...)
162 {
163 // The underlying code in writeBinary will log a message to the
164 // journal if the write fails. If the ON_OFF_CONFIG is not setup as
165 // desired, later fault detection and analysis code should catch any
166 // of the fall out. We should not need to terminate the application
167 // if this write fails.
168 }
169 }
170}
171
Brandon Wyman3c208462020-05-13 16:25:58 -0500172void PowerSupply::clearFaults()
173{
Brandon Wyman5474c912021-02-23 14:39:43 -0600174 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500175 // The PMBus device driver does not allow for writing CLEAR_FAULTS
176 // directly. However, the pmbus hwmon device driver code will send a
177 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
178 // reading in1_input should result in clearing the fault bits in
179 // STATUS_BYTE/STATUS_WORD.
180 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600181 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500182 {
Brandon Wyman9564e942020-11-10 14:01:42 -0600183 faultFound = false;
184 inputFault = false;
185 mfrFault = false;
Jay Meyer10d94052020-11-30 14:41:21 -0600186 statusMFR = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600187 vinUVFault = false;
188 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600189
Brandon Wyman11151532020-11-10 13:45:57 -0600190 try
191 {
192 static_cast<void>(
193 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
194 }
195 catch (ReadFailure& e)
196 {
197 // Since I do not care what the return value is, I really do not
198 // care much if it gets a ReadFailure either. However, this should
199 // not prevent the application from continuing to run, so catching
200 // the read failure.
201 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500202 }
203}
204
Brandon Wymanaed1f752019-11-25 18:10:52 -0600205void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
206{
207 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500208 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600209 msg.read(msgSensor, msgData);
210
211 // Check if it was the Present property that changed.
212 auto valPropMap = msgData.find(PRESENT_PROP);
213 if (valPropMap != msgData.end())
214 {
215 if (std::get<bool>(valPropMap->second))
216 {
217 present = true;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500218 // TODO: Immediately trying to read or write the "files" causes read
219 // or write failures.
220 using namespace std::chrono_literals;
221 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600222 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500223 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600224 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500225 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600226 }
227 else
228 {
229 present = false;
230
231 // Clear out the now outdated inventory properties
232 updateInventory();
233 }
234 }
235}
236
Brandon Wyman9a507db2021-02-25 16:15:22 -0600237void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
238{
239 sdbusplus::message::object_path path;
240 msg.read(path);
241 // Make sure the signal is for the PSU inventory path
242 if (path == inventoryPath)
243 {
244 std::map<std::string, std::map<std::string, std::variant<bool>>>
245 interfaces;
246 // Get map of interfaces and their properties
247 msg.read(interfaces);
248
249 auto properties = interfaces.find(INVENTORY_IFACE);
250 if (properties != interfaces.end())
251 {
252 auto property = properties->second.find(PRESENT_PROP);
253 if (property != properties->second.end())
254 {
255 present = std::get<bool>(property->second);
256
257 log<level::INFO>(fmt::format("Power Supply {} Present {}",
258 inventoryPath, present)
259 .c_str());
260
261 updateInventory();
262 }
263 }
264 }
265}
266
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500267void PowerSupply::updateInventory()
268{
269 using namespace phosphor::pmbus;
270
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700271#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500272 std::string ccin;
273 std::string pn;
274 std::string fn;
275 std::string header;
276 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500277 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800278 std::map<std::string,
279 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500280 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800281 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500282 PropertyMap versionProps;
283 PropertyMap ipzvpdDINFProps;
284 PropertyMap ipzvpdVINIProps;
285 using InterfaceMap = std::map<std::string, PropertyMap>;
286 InterfaceMap interfaces;
287 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
288 ObjectMap object;
289#endif
290
291 if (present)
292 {
293 // TODO: non-IBM inventory updates?
294
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700295#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500296 try
297 {
298 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
299 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000300 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500301 }
302 catch (ReadFailure& e)
303 {
304 // Ignore the read failure, let pmbus code indicate failure, path...
305 // TODO - ibm918
306 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
307 // The BMC must log errors if any of the VPD cannot be properly
308 // parsed or fails ECC checks.
309 }
310
311 try
312 {
313 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
314 assetProps.emplace(PN_PROP, pn);
315 }
316 catch (ReadFailure& e)
317 {
318 // Ignore the read failure, let pmbus code indicate failure, path...
319 }
320
321 try
322 {
323 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
324 }
325 catch (ReadFailure& e)
326 {
327 // Ignore the read failure, let pmbus code indicate failure, path...
328 }
329
330 try
331 {
332 header =
333 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
334 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
335 assetProps.emplace(SN_PROP, sn);
336 }
337 catch (ReadFailure& e)
338 {
339 // Ignore the read failure, let pmbus code indicate failure, path...
340 }
341
342 try
343 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500344 fwVersion =
345 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
346 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500347 }
348 catch (ReadFailure& e)
349 {
350 // Ignore the read failure, let pmbus code indicate failure, path...
351 }
352
353 ipzvpdVINIProps.emplace("CC",
354 std::vector<uint8_t>(ccin.begin(), ccin.end()));
355 ipzvpdVINIProps.emplace("PN",
356 std::vector<uint8_t>(pn.begin(), pn.end()));
357 ipzvpdVINIProps.emplace("FN",
358 std::vector<uint8_t>(fn.begin(), fn.end()));
359 std::string header_sn = header + sn + '\0';
360 ipzvpdVINIProps.emplace(
361 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
362 std::string description = "IBM PS";
363 ipzvpdVINIProps.emplace(
364 "DR", std::vector<uint8_t>(description.begin(), description.end()));
365
366 // Update the Resource Identifier (RI) keyword
367 // 2 byte FRC: 0x0003
368 // 2 byte RID: 0x1000, 0x1001...
369 std::uint8_t num = std::stoul(
370 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
371 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
372 ipzvpdDINFProps.emplace("RI", ri);
373
374 // Fill in the FRU Label (FL) keyword.
375 std::string fl = "E";
376 fl.push_back(inventoryPath.back());
377 fl.resize(FL_KW_SIZE, ' ');
378 ipzvpdDINFProps.emplace("FL",
379 std::vector<uint8_t>(fl.begin(), fl.end()));
380
381 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
382 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
383 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
384 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
385
George Liu070c1bc2020-10-12 11:28:01 +0800386 // Update the Functional
387 operProps.emplace(FUNCTIONAL_PROP, present);
388 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
389
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500390 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
391 object.emplace(path, std::move(interfaces));
392
393 try
394 {
395 auto service =
396 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
397
398 if (service.empty())
399 {
400 log<level::ERR>("Unable to get inventory manager service");
401 return;
402 }
403
404 auto method =
405 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
406 INVENTORY_MGR_IFACE, "Notify");
407
408 method.append(std::move(object));
409
410 auto reply = bus.call(method);
411 }
412 catch (std::exception& e)
413 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500414 log<level::ERR>(
415 std::string(e.what() + std::string(" PATH=") + inventoryPath)
416 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500417 }
418#endif
419 }
420}
421
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600422} // namespace phosphor::power::psu