blob: 226d03b233ca4636988536e22a1c69f998045235 [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...
B. J. Wyman681b2a32021-04-20 22:31:22 +000014#include <fstream>
15#include <thread> // sleep_for()
Brandon Wyman1d7a7df2020-03-26 10:14:05 -050016
Brandon Wyman3f1242f2020-01-28 13:11:25 -060017namespace phosphor::power::psu
Brandon Wymanaed1f752019-11-25 18:10:52 -060018{
B. J. Wyman681b2a32021-04-20 22:31:22 +000019// Amount of time in milliseconds to delay between power supply going from
20// missing to present before running the bind command(s).
21constexpr auto bindDelay = 1000;
Brandon Wymanaed1f752019-11-25 18:10:52 -060022
23using namespace phosphor::logging;
Brandon Wyman3f1242f2020-01-28 13:11:25 -060024using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
Brandon Wymanaed1f752019-11-25 18:10:52 -060025
Brandon Wyman510acaa2020-11-05 18:32:04 -060026PowerSupply::PowerSupply(sdbusplus::bus::bus& bus, const std::string& invpath,
B. J. Wyman681b2a32021-04-20 22:31:22 +000027 std::uint8_t i2cbus, std::uint16_t i2caddr,
28 const std::string& gpioLineName) :
Brandon Wyman510acaa2020-11-05 18:32:04 -060029 bus(bus),
B. J. Wyman681b2a32021-04-20 22:31:22 +000030 inventoryPath(invpath), bindPath("/sys/bus/i2c/drivers/ibm-cffps")
Brandon Wyman510acaa2020-11-05 18:32:04 -060031{
32 if (inventoryPath.empty())
33 {
34 throw std::invalid_argument{"Invalid empty inventoryPath"};
35 }
36
B. J. Wyman681b2a32021-04-20 22:31:22 +000037 if (gpioLineName.empty())
38 {
39 throw std::invalid_argument{"Invalid empty gpioLineName"};
40 }
Brandon Wyman510acaa2020-11-05 18:32:04 -060041
B. J. Wyman681b2a32021-04-20 22:31:22 +000042 log<level::DEBUG>(fmt::format("gpioLineName: {}", gpioLineName).c_str());
43 presenceGPIO = createGPIO(gpioLineName);
Brandon Wyman510acaa2020-11-05 18:32:04 -060044
45 std::ostringstream ss;
46 ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
47 std::string addrStr = ss.str();
B. J. Wyman681b2a32021-04-20 22:31:22 +000048 std::string busStr = std::to_string(i2cbus);
49 bindDevice = busStr;
50 bindDevice.append("-");
51 bindDevice.append(addrStr);
52
Brandon Wyman510acaa2020-11-05 18:32:04 -060053 pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr);
54
55 // Get the current state of the Present property.
B. J. Wyman681b2a32021-04-20 22:31:22 +000056 try
57 {
58 updatePresenceGPIO();
59 }
60 catch (...)
61 {
62 // If the above attempt to use the GPIO failed, it likely means that the
63 // GPIOs are in use by the kernel, meaning it is using gpio-keys.
64 // So, I should rely on phosphor-gpio-presence to update D-Bus, and
65 // work that way for power supply presence.
66 presenceGPIO = nullptr;
67 // Setup the functions to call when the D-Bus inventory path for the
68 // Present property changes.
69 presentMatch = std::make_unique<sdbusplus::bus::match_t>(
70 bus,
71 sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
72 INVENTORY_IFACE),
73 [this](auto& msg) { this->inventoryChanged(msg); });
74
75 presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
76 bus,
77 sdbusplus::bus::match::rules::interfacesAdded() +
78 sdbusplus::bus::match::rules::argNpath(0, inventoryPath),
79 [this](auto& msg) { this->inventoryAdded(msg); });
80
81 updatePresence();
82 updateInventory();
83 }
84}
85
86void PowerSupply::bindOrUnbindDriver(bool present)
87{
88 auto action = (present) ? "bind" : "unbind";
89 auto path = bindPath / action;
90
91 if (present)
92 {
93 log<level::INFO>(
94 fmt::format("Binding device driver. path: {} device: {}",
95 path.string(), bindDevice)
96 .c_str());
97 }
98 else
99 {
100 log<level::INFO>(
101 fmt::format("Unbinding device driver. path: {} device: {}",
102 path.string(), bindDevice)
103 .c_str());
104 }
105
106 std::ofstream file;
107
108 file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
109 std::ofstream::eofbit);
110
111 try
112 {
113 file.open(path);
114 file << bindDevice;
115 file.close();
116 }
117 catch (std::exception& e)
118 {
119 auto err = errno;
120
121 log<level::ERR>(
122 fmt::format("Failed binding or unbinding device. errno={}", err)
123 .c_str());
124 }
Brandon Wyman510acaa2020-11-05 18:32:04 -0600125}
126
Brandon Wymanaed1f752019-11-25 18:10:52 -0600127void PowerSupply::updatePresence()
128{
129 try
130 {
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600131 present = getPresence(bus, inventoryPath);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600132 }
133 catch (const sdbusplus::exception::SdBusError& e)
134 {
135 // Relying on property change or interface added to retry.
136 // Log an informational trace to the journal.
Brandon Wymandf13c3a2020-12-15 14:25:22 -0600137 log<level::INFO>(
138 fmt::format("D-Bus property {} access failure exception",
139 inventoryPath)
140 .c_str());
Brandon Wymanaed1f752019-11-25 18:10:52 -0600141 }
142}
143
B. J. Wyman681b2a32021-04-20 22:31:22 +0000144void PowerSupply::updatePresenceGPIO()
145{
146 bool presentOld = present;
147
148 try
149 {
150 if (presenceGPIO->read() > 0)
151 {
152 present = true;
153 }
154 else
155 {
156 present = false;
157 }
158 }
159 catch (std::exception& e)
160 {
161 log<level::ERR>(
162 fmt::format("presenceGPIO read fail: {}", e.what()).c_str());
163 throw;
164 }
165
166 if (presentOld != present)
167 {
168 log<level::DEBUG>(
169 fmt::format("presentOld: {} present: {}", presentOld, present)
170 .c_str());
171 if (present)
172 {
173 std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay));
174 bindOrUnbindDriver(present);
175 pmbusIntf->findHwmonDir();
176 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
177 clearFaults();
178 }
179 else
180 {
181 bindOrUnbindDriver(present);
182 }
183
184 auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
185 auto const lastSlashPos = invpath.find_last_of('/');
186 std::string prettyName = invpath.substr(lastSlashPos + 1);
187 setPresence(bus, invpath, present, prettyName);
188 updateInventory();
189 }
190}
191
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600192void PowerSupply::analyze()
193{
194 using namespace phosphor::pmbus;
195
B. J. Wyman681b2a32021-04-20 22:31:22 +0000196 if (presenceGPIO)
197 {
198 updatePresenceGPIO();
199 }
200
Brandon Wymanf65c4062020-08-19 13:15:53 -0500201 if ((present) && (readFail < LOG_LIMIT))
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600202 {
203 try
204 {
Brandon Wymanfed0ba22020-09-26 20:02:51 -0500205 statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug);
Brandon Wymanf65c4062020-08-19 13:15:53 -0500206 // Read worked, reset the fail count.
207 readFail = 0;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600208
209 if (statusWord)
210 {
Jay Meyer10d94052020-11-30 14:41:21 -0600211 statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600212 if (statusWord & status_word::INPUT_FAULT_WARN)
213 {
214 if (!inputFault)
215 {
Jay Meyer10d94052020-11-30 14:41:21 -0600216 log<level::INFO>(fmt::format("INPUT fault: "
217 "status word = {:#04x}, "
218 "MFR fault = {:#02x}",
219 statusWord, statusMFR)
220 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600221 }
222
223 faultFound = true;
224 inputFault = true;
225 }
226
227 if (statusWord & status_word::MFR_SPECIFIC_FAULT)
228 {
229 if (!mfrFault)
230 {
Jay Meyer10d94052020-11-30 14:41:21 -0600231 log<level::ERR>(fmt::format("MFR fault: "
232 "status word = {:#04x} "
233 "MFR fault = {:#02x}",
234 statusWord, statusMFR)
235 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600236 }
237 faultFound = true;
238 mfrFault = true;
239 }
240
241 if (statusWord & status_word::VIN_UV_FAULT)
242 {
243 if (!vinUVFault)
244 {
Jay Meyer10d94052020-11-30 14:41:21 -0600245 log<level::INFO>(fmt::format("VIN_UV fault: "
246 "status word = {:#04x}, "
247 "MFR fault = {:#02x}",
248 statusWord, statusMFR)
249 .c_str());
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600250 }
251
252 faultFound = true;
253 vinUVFault = true;
254 }
255 }
256 else
257 {
258 faultFound = false;
259 inputFault = false;
260 mfrFault = false;
261 vinUVFault = false;
262 }
263 }
264 catch (ReadFailure& e)
265 {
Brandon Wymanf65c4062020-08-19 13:15:53 -0500266 readFail++;
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600267 phosphor::logging::commit<ReadFailure>();
268 }
269 }
270}
271
Brandon Wyman59a35792020-06-04 12:37:40 -0500272void PowerSupply::onOffConfig(uint8_t data)
273{
274 using namespace phosphor::pmbus;
275
276 if (present)
277 {
278 log<level::INFO>("ON_OFF_CONFIG write", entry("DATA=0x%02X", data));
279 try
280 {
281 std::vector<uint8_t> configData{data};
282 pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
283 Type::HwmonDeviceDebug);
284 }
285 catch (...)
286 {
287 // The underlying code in writeBinary will log a message to the
B. J. Wyman681b2a32021-04-20 22:31:22 +0000288 // journal if the write fails. If the ON_OFF_CONFIG is not setup
289 // as desired, later fault detection and analysis code should
290 // catch any of the fall out. We should not need to terminate
291 // the application if this write fails.
Brandon Wyman59a35792020-06-04 12:37:40 -0500292 }
293 }
294}
295
Brandon Wyman3c208462020-05-13 16:25:58 -0500296void PowerSupply::clearFaults()
297{
Brandon Wyman5474c912021-02-23 14:39:43 -0600298 faultLogged = false;
Brandon Wyman3c208462020-05-13 16:25:58 -0500299 // The PMBus device driver does not allow for writing CLEAR_FAULTS
300 // directly. However, the pmbus hwmon device driver code will send a
301 // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
302 // reading in1_input should result in clearing the fault bits in
303 // STATUS_BYTE/STATUS_WORD.
304 // I do not care what the return value is.
Brandon Wyman11151532020-11-10 13:45:57 -0600305 if (present)
Brandon Wyman3c208462020-05-13 16:25:58 -0500306 {
Brandon Wyman9564e942020-11-10 14:01:42 -0600307 faultFound = false;
308 inputFault = false;
309 mfrFault = false;
Jay Meyer10d94052020-11-30 14:41:21 -0600310 statusMFR = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600311 vinUVFault = false;
312 readFail = 0;
Brandon Wyman9564e942020-11-10 14:01:42 -0600313
Brandon Wyman11151532020-11-10 13:45:57 -0600314 try
315 {
316 static_cast<void>(
317 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
318 }
319 catch (ReadFailure& e)
320 {
321 // Since I do not care what the return value is, I really do not
B. J. Wyman681b2a32021-04-20 22:31:22 +0000322 // care much if it gets a ReadFailure either. However, this
323 // should not prevent the application from continuing to run, so
324 // catching the read failure.
Brandon Wyman11151532020-11-10 13:45:57 -0600325 }
Brandon Wyman3c208462020-05-13 16:25:58 -0500326 }
327}
328
Brandon Wymanaed1f752019-11-25 18:10:52 -0600329void PowerSupply::inventoryChanged(sdbusplus::message::message& msg)
330{
331 std::string msgSensor;
Patrick Williamsabe49412020-05-13 17:59:47 -0500332 std::map<std::string, std::variant<uint32_t, bool>> msgData;
Brandon Wymanaed1f752019-11-25 18:10:52 -0600333 msg.read(msgSensor, msgData);
334
335 // Check if it was the Present property that changed.
336 auto valPropMap = msgData.find(PRESENT_PROP);
337 if (valPropMap != msgData.end())
338 {
339 if (std::get<bool>(valPropMap->second))
340 {
341 present = true;
B. J. Wyman681b2a32021-04-20 22:31:22 +0000342 // TODO: Immediately trying to read or write the "files" causes
343 // read or write failures.
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500344 using namespace std::chrono_literals;
345 std::this_thread::sleep_for(20ms);
Brandon Wyman9564e942020-11-10 14:01:42 -0600346 pmbusIntf->findHwmonDir();
Brandon Wyman59a35792020-06-04 12:37:40 -0500347 onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
Brandon Wymanaed1f752019-11-25 18:10:52 -0600348 clearFaults();
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500349 updateInventory();
Brandon Wymanaed1f752019-11-25 18:10:52 -0600350 }
351 else
352 {
353 present = false;
354
355 // Clear out the now outdated inventory properties
356 updateInventory();
357 }
358 }
359}
360
Brandon Wyman9a507db2021-02-25 16:15:22 -0600361void PowerSupply::inventoryAdded(sdbusplus::message::message& msg)
362{
363 sdbusplus::message::object_path path;
364 msg.read(path);
365 // Make sure the signal is for the PSU inventory path
366 if (path == inventoryPath)
367 {
368 std::map<std::string, std::map<std::string, std::variant<bool>>>
369 interfaces;
370 // Get map of interfaces and their properties
371 msg.read(interfaces);
372
373 auto properties = interfaces.find(INVENTORY_IFACE);
374 if (properties != interfaces.end())
375 {
376 auto property = properties->second.find(PRESENT_PROP);
377 if (property != properties->second.end())
378 {
379 present = std::get<bool>(property->second);
380
381 log<level::INFO>(fmt::format("Power Supply {} Present {}",
382 inventoryPath, present)
383 .c_str());
384
385 updateInventory();
386 }
387 }
388 }
389}
390
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500391void PowerSupply::updateInventory()
392{
393 using namespace phosphor::pmbus;
394
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700395#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500396 std::string ccin;
397 std::string pn;
398 std::string fn;
399 std::string header;
400 std::string sn;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500401 using PropertyMap =
George Liu070c1bc2020-10-12 11:28:01 +0800402 std::map<std::string,
403 std::variant<std::string, std::vector<uint8_t>, bool>>;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500404 PropertyMap assetProps;
George Liu070c1bc2020-10-12 11:28:01 +0800405 PropertyMap operProps;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500406 PropertyMap versionProps;
407 PropertyMap ipzvpdDINFProps;
408 PropertyMap ipzvpdVINIProps;
409 using InterfaceMap = std::map<std::string, PropertyMap>;
410 InterfaceMap interfaces;
411 using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
412 ObjectMap object;
413#endif
B. J. Wyman681b2a32021-04-20 22:31:22 +0000414 log<level::DEBUG>(
415 fmt::format("updateInventory() inventoryPath: {}", inventoryPath)
416 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500417
418 if (present)
419 {
420 // TODO: non-IBM inventory updates?
421
Chanh Nguyenc12c53b2021-04-06 17:24:47 +0700422#if IBM_VPD
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500423 try
424 {
425 ccin = pmbusIntf->readString(CCIN, Type::HwmonDeviceDebug);
426 assetProps.emplace(MODEL_PROP, ccin);
Adriana Kobylak572a9052021-03-30 15:58:07 +0000427 modelName = ccin;
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500428 }
429 catch (ReadFailure& e)
430 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000431 // Ignore the read failure, let pmbus code indicate failure,
432 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500433 // TODO - ibm918
434 // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
435 // The BMC must log errors if any of the VPD cannot be properly
436 // parsed or fails ECC checks.
437 }
438
439 try
440 {
441 pn = pmbusIntf->readString(PART_NUMBER, Type::HwmonDeviceDebug);
442 assetProps.emplace(PN_PROP, pn);
443 }
444 catch (ReadFailure& e)
445 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000446 // Ignore the read failure, let pmbus code indicate failure,
447 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500448 }
449
450 try
451 {
452 fn = pmbusIntf->readString(FRU_NUMBER, Type::HwmonDeviceDebug);
453 }
454 catch (ReadFailure& e)
455 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000456 // Ignore the read failure, let pmbus code indicate failure,
457 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500458 }
459
460 try
461 {
462 header =
463 pmbusIntf->readString(SERIAL_HEADER, Type::HwmonDeviceDebug);
464 sn = pmbusIntf->readString(SERIAL_NUMBER, Type::HwmonDeviceDebug);
465 assetProps.emplace(SN_PROP, sn);
466 }
467 catch (ReadFailure& e)
468 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000469 // Ignore the read failure, let pmbus code indicate failure,
470 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500471 }
472
473 try
474 {
Brandon Wymanc9efe412020-10-09 15:42:50 -0500475 fwVersion =
476 pmbusIntf->readString(FW_VERSION, Type::HwmonDeviceDebug);
477 versionProps.emplace(VERSION_PROP, fwVersion);
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500478 }
479 catch (ReadFailure& e)
480 {
B. J. Wyman681b2a32021-04-20 22:31:22 +0000481 // Ignore the read failure, let pmbus code indicate failure,
482 // path...
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500483 }
484
485 ipzvpdVINIProps.emplace("CC",
486 std::vector<uint8_t>(ccin.begin(), ccin.end()));
487 ipzvpdVINIProps.emplace("PN",
488 std::vector<uint8_t>(pn.begin(), pn.end()));
489 ipzvpdVINIProps.emplace("FN",
490 std::vector<uint8_t>(fn.begin(), fn.end()));
491 std::string header_sn = header + sn + '\0';
492 ipzvpdVINIProps.emplace(
493 "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
494 std::string description = "IBM PS";
495 ipzvpdVINIProps.emplace(
496 "DR", std::vector<uint8_t>(description.begin(), description.end()));
497
498 // Update the Resource Identifier (RI) keyword
499 // 2 byte FRC: 0x0003
500 // 2 byte RID: 0x1000, 0x1001...
501 std::uint8_t num = std::stoul(
502 inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
503 std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
504 ipzvpdDINFProps.emplace("RI", ri);
505
506 // Fill in the FRU Label (FL) keyword.
507 std::string fl = "E";
508 fl.push_back(inventoryPath.back());
509 fl.resize(FL_KW_SIZE, ' ');
510 ipzvpdDINFProps.emplace("FL",
511 std::vector<uint8_t>(fl.begin(), fl.end()));
512
513 interfaces.emplace(ASSET_IFACE, std::move(assetProps));
514 interfaces.emplace(VERSION_IFACE, std::move(versionProps));
515 interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
516 interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
517
George Liu070c1bc2020-10-12 11:28:01 +0800518 // Update the Functional
519 operProps.emplace(FUNCTIONAL_PROP, present);
520 interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
521
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500522 auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
523 object.emplace(path, std::move(interfaces));
524
525 try
526 {
527 auto service =
528 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
529
530 if (service.empty())
531 {
532 log<level::ERR>("Unable to get inventory manager service");
533 return;
534 }
535
536 auto method =
537 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
538 INVENTORY_MGR_IFACE, "Notify");
539
540 method.append(std::move(object));
541
542 auto reply = bus.call(method);
543 }
544 catch (std::exception& e)
545 {
Jay Meyer6a3fd2c2020-08-25 16:37:16 -0500546 log<level::ERR>(
547 std::string(e.what() + std::string(" PATH=") + inventoryPath)
548 .c_str());
Brandon Wyman1d7a7df2020-03-26 10:14:05 -0500549 }
550#endif
551 }
552}
553
Brandon Wyman3f1242f2020-01-28 13:11:25 -0600554} // namespace phosphor::power::psu