xref: /openbmc/phosphor-power/phosphor-power-supply/new_power_supply.cpp (revision 9ed0f38e1d59564106c6020b854416ece6753776)
1 #include "config.h"
2 
3 #include "new_power_supply.hpp"
4 
5 #include "types.hpp"
6 #include "util.hpp"
7 
8 #include <phosphor-logging/lg2.hpp>
9 #include <xyz/openbmc_project/Common/Device/error.hpp>
10 
11 #include <chrono>  // sleep_for()
12 #include <cmath>
13 #include <cstdint> // uint8_t...
14 #include <format>
15 #include <fstream>
16 #include <regex>
17 #include <thread> // sleep_for()
18 
19 namespace phosphor::power::psu
20 {
21 // Amount of time in milliseconds to delay between power supply going from
22 // missing to present before running the bind command(s).
23 constexpr auto bindDelay = 1000;
24 
25 using namespace sdbusplus::xyz::openbmc_project::Common::Device::Error;
26 
PowerSupply(sdbusplus::bus_t & bus,const std::string & invpath,std::uint8_t i2cbus,std::uint16_t i2caddr,const std::string & driver,const std::string & gpioLineName,std::function<bool ()> && callback,const std::string & chassisShortName)27 PowerSupply::PowerSupply(
28     sdbusplus::bus_t& bus, const std::string& invpath, std::uint8_t i2cbus,
29     std::uint16_t i2caddr, const std::string& driver,
30     const std::string& gpioLineName, std::function<bool()>&& callback,
31     const std::string& chassisShortName) :
32     bus(bus), inventoryPath(invpath),
33     bindPath("/sys/bus/i2c/drivers/" + driver), isPowerOn(std::move(callback)),
34     driverName(driver), chassisName(chassisShortName)
35 {
36     if (inventoryPath.empty())
37     {
38         throw std::invalid_argument{"Invalid empty inventoryPath"};
39     }
40 
41     if (gpioLineName.empty())
42     {
43         throw std::invalid_argument{"Invalid empty gpioLineName"};
44     }
45 
46     shortName = findShortName(inventoryPath);
47 
48     lg2::debug("{SHORT_NAME} gpioLineName: {GPIO_LINE_NAME}", "SHORT_NAME",
49                shortName, "GPIO_LINE_NAME", gpioLineName);
50     presenceGPIO = createGPIO(gpioLineName);
51 
52     std::ostringstream ss;
53     ss << std::hex << std::setw(4) << std::setfill('0') << i2caddr;
54     std::string addrStr = ss.str();
55     std::string busStr = std::to_string(i2cbus);
56     bindDevice = busStr;
57     bindDevice.append("-");
58     bindDevice.append(addrStr);
59 
60     pmbusIntf = phosphor::pmbus::createPMBus(i2cbus, addrStr);
61 
62     // Get the current state of the Present property.
63     try
64     {
65         updatePresenceGPIO();
66     }
67     catch (...)
68     {
69         // If the above attempt to use the GPIO failed, it likely means that the
70         // GPIOs are in use by the kernel, meaning it is using gpio-keys.
71         // So, I should rely on phosphor-gpio-presence to update D-Bus, and
72         // work that way for power supply presence.
73         presenceGPIO = nullptr;
74         // Setup the functions to call when the D-Bus inventory path for the
75         // Present property changes.
76         presentMatch = std::make_unique<sdbusplus::bus::match_t>(
77             bus,
78             sdbusplus::bus::match::rules::propertiesChanged(inventoryPath,
79                                                             INVENTORY_IFACE),
80             [this](auto& msg) { this->inventoryChanged(msg); });
81 
82         presentAddedMatch = std::make_unique<sdbusplus::bus::match_t>(
83             bus,
84             sdbusplus::bus::match::rules::interfacesAdded() +
85                 sdbusplus::bus::match::rules::argNpath(0, inventoryPath),
86             [this](auto& msg) { this->inventoryAdded(msg); });
87 
88         updatePresence();
89         updateInventory();
90         setupSensors();
91     }
92     try
93     {
94         setInputVoltageRating();
95     }
96     catch (const std::exception& e)
97     {
98         lg2::info("setInputVoltageRating exception: {ERR}", "ERR", e);
99     }
100 }
101 
PowerSupply(sdbusplus::bus_t & bus,const std::string & invpath,std::uint8_t i2cbus,std::uint16_t i2caddr,const std::string & driver,const std::string & gpioLineName,std::function<bool ()> && callback)102 PowerSupply::PowerSupply(
103     sdbusplus::bus_t& bus, const std::string& invpath, std::uint8_t i2cbus,
104     std::uint16_t i2caddr, const std::string& driver,
105     const std::string& gpioLineName, std::function<bool()>&& callback) :
106     PowerSupply(bus, invpath, i2cbus, i2caddr, driver, gpioLineName,
107                 std::move(callback), "")
108 {}
109 
bindOrUnbindDriver(bool present)110 void PowerSupply::bindOrUnbindDriver(bool present)
111 {
112     // Symbolic link to the device will exist if the driver is bound.
113     // So exit no action required if both the link and PSU are present
114     // or neither is present.
115     namespace fs = std::filesystem;
116     fs::path path;
117     auto action = (present) ? "bind" : "unbind";
118 
119     // This case should not happen, if no device driver name return.
120     if (driverName.empty())
121     {
122         lg2::info("No device driver name found");
123         return;
124     }
125     if (bindPath.string().find(driverName) != std::string::npos)
126     {
127         // bindPath has driver name
128         path = bindPath / action;
129     }
130     else
131     {
132         // Add driver name to bindPath
133         path = bindPath / driverName / action;
134         bindPath = bindPath / driverName;
135     }
136 
137     if ((std::filesystem::exists(bindPath / bindDevice) && present) ||
138         (!std::filesystem::exists(bindPath / bindDevice) && !present))
139     {
140         return;
141     }
142     if (present)
143     {
144         std::this_thread::sleep_for(std::chrono::milliseconds(bindDelay));
145         lg2::info("Binding device driver. path: {PATH} device: {BIND_DEVICE}",
146                   "PATH", path, "BIND_DEVICE", bindDevice);
147     }
148     else
149     {
150         lg2::info("Unbinding device driver. path: {PATH} device: {BIND_DEVICE}",
151                   "PATH", path, "BIND_DEVICE", bindDevice);
152     }
153 
154     std::ofstream file;
155 
156     file.exceptions(std::ofstream::failbit | std::ofstream::badbit |
157                     std::ofstream::eofbit);
158 
159     try
160     {
161         file.open(path);
162         file << bindDevice;
163         file.close();
164     }
165     catch (const std::exception& e)
166     {
167         auto err = errno;
168 
169         lg2::error("Failed binding or unbinding device. errno={ERRNO}", "ERRNO",
170                    err);
171     }
172 }
173 
updatePresence()174 void PowerSupply::updatePresence()
175 {
176     try
177     {
178         present = getPresence(bus, inventoryPath);
179     }
180     catch (const sdbusplus::exception_t& e)
181     {
182         // Relying on property change or interface added to retry.
183         // Log an informational trace to the journal.
184         lg2::info("D-Bus property {INVENTORY_PATH} access failure exception",
185                   "INVENTORY_PATH", inventoryPath);
186     }
187 }
188 
updatePresenceGPIO()189 void PowerSupply::updatePresenceGPIO()
190 {
191     bool presentOld = present;
192 
193     try
194     {
195         if (presenceGPIO->read() > 0)
196         {
197             present = true;
198         }
199         else
200         {
201             present = false;
202         }
203     }
204     catch (const std::exception& e)
205     {
206         lg2::error("presenceGPIO read fail: {ERROR}", "ERROR", e);
207         throw;
208     }
209 
210     if (presentOld != present)
211     {
212         lg2::debug("{SHORT_NAME} presentOld: {PRESENT_OLD} present: {PRESENT}",
213                    "SHORT_NAME", shortName, "PRESENT_OLD", presentOld,
214                    "PRESENT", present);
215 
216         auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
217 
218         bindOrUnbindDriver(present);
219         if (present)
220         {
221             // If the power supply was present, then missing, and present again,
222             // the hwmon path may have changed. We will need the correct/updated
223             // path before any reads or writes are attempted.
224             pmbusIntf->findHwmonDir();
225         }
226 
227         setPresence(bus, invpath, present, shortName);
228         setupSensors();
229         updateInventory();
230 
231         // Need Functional to already be correct before calling this.
232         checkAvailability();
233 
234         if (present)
235         {
236             onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
237             clearFaults();
238             // Indicate that the input history data and timestamps between all
239             // the power supplies that are present in the system need to be
240             // synchronized.
241             syncHistoryRequired = true;
242         }
243         else
244         {
245             setSensorsNotAvailable();
246         }
247     }
248 }
249 
analyzeCMLFault()250 void PowerSupply::analyzeCMLFault()
251 {
252     if (statusWord & phosphor::pmbus::status_word::CML_FAULT)
253     {
254         if (cmlFault < DEGLITCH_LIMIT)
255         {
256             if (statusWord != statusWordOld)
257             {
258                 lg2::error(
259                     "{SHORT_NAME} CML fault: STATUS_WORD = {STATUS_WORD}, "
260                     "STATUS_CML = {STATUS_CML}",
261                     "SHORT_NAME", shortName, "STATUS_WORD",
262                     lg2::hex | lg2::field16, statusWord, "STATUS_CML",
263                     lg2::hex | lg2::field8, statusCML);
264             }
265             cmlFault++;
266         }
267     }
268     else
269     {
270         cmlFault = 0;
271     }
272 }
273 
analyzeInputFault()274 void PowerSupply::analyzeInputFault()
275 {
276     if (statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN)
277     {
278         if (inputFault < DEGLITCH_LIMIT)
279         {
280             if (statusWord != statusWordOld)
281             {
282                 lg2::error(
283                     "{SHORT_NAME} INPUT fault: STATUS_WORD = {STATUS_WORD}, "
284                     "STATUS_MFR_SPECIFIC = {STATUS_MFR_SPECIFIC}, "
285                     "STATUS_INPUT = {STATUS_INPUT}",
286                     "SHORT_NAME", shortName, "STATUS_WORD",
287                     lg2::hex | lg2::field16, statusWord, "STATUS_MFR_SPECIFIC",
288                     lg2::hex | lg2::field8, statusMFR, "STATUS_INPUT",
289                     lg2::hex | lg2::field8, statusInput);
290             }
291             inputFault++;
292         }
293     }
294 
295     // If had INPUT/VIN_UV fault, and now off.
296     // Trace that odd behavior.
297     if (inputFault &&
298         !(statusWord & phosphor::pmbus::status_word::INPUT_FAULT_WARN))
299     {
300         lg2::info(
301             "{SHORT_NAME} INPUT fault cleared: STATUS_WORD = {STATUS_WORD}, "
302             "STATUS_MFR_SPECIFIC = {STATUS_MFR_SPECIFIC}, "
303             "STATUS_INPUT = {STATUS_INPUT}",
304             "SHORT_NAME", shortName, "STATUS_WORD", lg2::hex | lg2::field16,
305             statusWord, "STATUS_MFR_SPECIFIC", lg2::hex | lg2::field8,
306             statusMFR, "STATUS_INPUT", lg2::hex | lg2::field8, statusInput);
307         inputFault = 0;
308     }
309 }
310 
analyzeVoutOVFault()311 void PowerSupply::analyzeVoutOVFault()
312 {
313     if (statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT)
314     {
315         if (voutOVFault < DEGLITCH_LIMIT)
316         {
317             if (statusWord != statusWordOld)
318             {
319                 lg2::error(
320                     "{SHORT_NAME} VOUT_OV_FAULT fault: STATUS_WORD = {STATUS_WORD}, "
321                     "STATUS_MFR_SPECIFIC = {STATUS_MFR_SPECIFIC}, "
322                     "STATUS_VOUT = {STATUS_VOUT}",
323                     "SHORT_NAME", shortName, "STATUS_WORD",
324                     lg2::hex | lg2::field16, statusWord, "STATUS_MFR_SPECIFIC",
325                     lg2::hex | lg2::field8, statusMFR, "STATUS_VOUT",
326                     lg2::hex | lg2::field8, statusVout);
327             }
328 
329             voutOVFault++;
330         }
331     }
332     else
333     {
334         voutOVFault = 0;
335     }
336 }
337 
analyzeIoutOCFault()338 void PowerSupply::analyzeIoutOCFault()
339 {
340     if (statusWord & phosphor::pmbus::status_word::IOUT_OC_FAULT)
341     {
342         if (ioutOCFault < DEGLITCH_LIMIT)
343         {
344             if (statusWord != statusWordOld)
345             {
346                 lg2::error(
347                     "{SHORT_NAME} IOUT fault: STATUS_WORD = {STATUS_WORD}, "
348                     "STATUS_MFR_SPECIFIC = {STATUS_MFR_SPECIFIC}, "
349                     "STATUS_IOUT = {STATUS_IOUT}",
350                     "SHORT_NAME", shortName, "STATUS_WORD",
351                     lg2::hex | lg2::field16, statusWord, "STATUS_MFR_SPECIFIC",
352                     lg2::hex | lg2::field8, statusMFR, "STATUS_IOUT",
353                     lg2::hex | lg2::field8, statusIout);
354             }
355 
356             ioutOCFault++;
357         }
358     }
359     else
360     {
361         ioutOCFault = 0;
362     }
363 }
364 
analyzeVoutUVFault()365 void PowerSupply::analyzeVoutUVFault()
366 {
367     if ((statusWord & phosphor::pmbus::status_word::VOUT_FAULT) &&
368         !(statusWord & phosphor::pmbus::status_word::VOUT_OV_FAULT))
369     {
370         if (voutUVFault < DEGLITCH_LIMIT)
371         {
372             if (statusWord != statusWordOld)
373             {
374                 lg2::error(
375                     "{SHORT_NAME} VOUT_UV_FAULT fault: STATUS_WORD = {STATUS_WORD}, "
376                     "STATUS_MFR_SPECIFIC = {STATUS_MFR_SPECIFIC}, "
377                     "STATUS_VOUT = {STATUS_VOUT}",
378                     "SHORT_NAME", shortName, "STATUS_WORD",
379                     lg2::hex | lg2::field16, statusWord, "STATUS_MFR_SPECIFIC",
380                     lg2::hex | lg2::field8, statusMFR, "STATUS_VOUT",
381                     lg2::hex | lg2::field8, statusVout);
382             }
383             voutUVFault++;
384         }
385     }
386     else
387     {
388         voutUVFault = 0;
389     }
390 }
391 
analyzeFanFault()392 void PowerSupply::analyzeFanFault()
393 {
394     if (statusWord & phosphor::pmbus::status_word::FAN_FAULT)
395     {
396         if (fanFault < DEGLITCH_LIMIT)
397         {
398             if (statusWord != statusWordOld)
399             {
400                 lg2::error("{SHORT_NAME} FANS fault/warning: "
401                            "STATUS_WORD = {STATUS_WORD}, "
402                            "STATUS_MFR_SPECIFIC = {STATUS_MFR_SPECIFIC}, "
403                            "STATUS_FANS_1_2 = {STATUS_FANS_1_2}",
404                            "SHORT_NAME", shortName, "STATUS_WORD",
405                            lg2::hex | lg2::field16, statusWord,
406                            "STATUS_MFR_SPECIFIC", lg2::hex | lg2::field8,
407                            statusMFR, "STATUS_FANS_1_2", lg2::hex | lg2::field8,
408                            statusFans12);
409             }
410             fanFault++;
411         }
412     }
413     else
414     {
415         fanFault = 0;
416     }
417 }
418 
analyzeTemperatureFault()419 void PowerSupply::analyzeTemperatureFault()
420 {
421     if (statusWord & phosphor::pmbus::status_word::TEMPERATURE_FAULT_WARN)
422     {
423         if (tempFault < DEGLITCH_LIMIT)
424         {
425             if (statusWord != statusWordOld)
426             {
427                 lg2::error("{SHORT_NAME} TEMPERATURE fault/warning: "
428                            "STATUS_WORD = {STATUS_WORD}, "
429                            "STATUS_MFR_SPECIFIC = {STATUS_MFR_SPECIFIC}, "
430                            "STATUS_TEMPERATURE = {STATUS_TEMPERATURE}",
431                            "SHORT_NAME", shortName, "STATUS_WORD",
432                            lg2::hex | lg2::field16, statusWord,
433                            "STATUS_MFR_SPECIFIC", lg2::hex | lg2::field8,
434                            statusMFR, "STATUS_TEMPERATURE",
435                            lg2::hex | lg2::field8, statusTemperature);
436             }
437             tempFault++;
438         }
439     }
440     else
441     {
442         tempFault = 0;
443     }
444 }
445 
analyzePgoodFault()446 void PowerSupply::analyzePgoodFault()
447 {
448     if ((statusWord & phosphor::pmbus::status_word::POWER_GOOD_NEGATED) ||
449         (statusWord & phosphor::pmbus::status_word::UNIT_IS_OFF))
450     {
451         if (pgoodFault < PGOOD_DEGLITCH_LIMIT)
452         {
453             if (statusWord != statusWordOld)
454             {
455                 lg2::error("{SHORT_NAME} PGOOD fault: "
456                            "STATUS_WORD = {STATUS_WORD}, "
457                            "STATUS_MFR_SPECIFIC = {STATUS_MFR_SPECIFIC}",
458                            "SHORT_NAME", shortName, "STATUS_WORD",
459                            lg2::hex | lg2::field16, statusWord,
460                            "STATUS_MFR_SPECIFIC", lg2::hex | lg2::field8,
461                            statusMFR);
462             }
463             pgoodFault++;
464         }
465     }
466     else
467     {
468         pgoodFault = 0;
469     }
470 }
471 
determineMFRFault()472 void PowerSupply::determineMFRFault()
473 {
474     if (bindPath.string().find(IBMCFFPS_DD_NAME) != std::string::npos)
475     {
476         // IBM MFR_SPECIFIC[4] is PS_Kill fault
477         if (statusMFR & 0x10)
478         {
479             if (psKillFault < DEGLITCH_LIMIT)
480             {
481                 psKillFault++;
482             }
483         }
484         else
485         {
486             psKillFault = 0;
487         }
488         // IBM MFR_SPECIFIC[6] is 12Vcs fault.
489         if (statusMFR & 0x40)
490         {
491             if (ps12VcsFault < DEGLITCH_LIMIT)
492             {
493                 ps12VcsFault++;
494             }
495         }
496         else
497         {
498             ps12VcsFault = 0;
499         }
500         // IBM MFR_SPECIFIC[7] is 12V Current-Share fault.
501         if (statusMFR & 0x80)
502         {
503             if (psCS12VFault < DEGLITCH_LIMIT)
504             {
505                 psCS12VFault++;
506             }
507         }
508         else
509         {
510             psCS12VFault = 0;
511         }
512     }
513 }
514 
analyzeMFRFault()515 void PowerSupply::analyzeMFRFault()
516 {
517     if (statusWord & phosphor::pmbus::status_word::MFR_SPECIFIC_FAULT)
518     {
519         if (mfrFault < DEGLITCH_LIMIT)
520         {
521             if (statusWord != statusWordOld)
522             {
523                 lg2::error("{SHORT_NAME} MFR fault: "
524                            "STATUS_WORD = {STATUS_WORD} "
525                            "STATUS_MFR_SPECIFIC = {STATUS_MFR_SPECIFIC}",
526                            "SHORT_NAME", shortName, "STATUS_WORD",
527                            lg2::hex | lg2::field16, statusWord,
528                            "STATUS_MFR_SPECIFIC", lg2::hex | lg2::field8,
529                            statusMFR);
530             }
531             mfrFault++;
532         }
533 
534         determineMFRFault();
535     }
536     else
537     {
538         mfrFault = 0;
539     }
540 }
541 
analyzeVinUVFault()542 void PowerSupply::analyzeVinUVFault()
543 {
544     if (statusWord & phosphor::pmbus::status_word::VIN_UV_FAULT)
545     {
546         if (vinUVFault < DEGLITCH_LIMIT)
547         {
548             if (statusWord != statusWordOld)
549             {
550                 lg2::error(
551                     "{SHORT_NAME} VIN_UV fault: STATUS_WORD = {STATUS_WORD}, "
552                     "STATUS_MFR_SPECIFIC = {STATUS_MFR_SPECIFIC}, "
553                     "STATUS_INPUT = {STATUS_INPUT}",
554                     "SHORT_NAME", shortName, "STATUS_WORD",
555                     lg2::hex | lg2::field16, statusWord, "STATUS_MFR_SPECIFIC",
556                     lg2::hex | lg2::field8, statusMFR, "STATUS_INPUT",
557                     lg2::hex | lg2::field8, statusInput);
558             }
559             vinUVFault++;
560         }
561         // Remember that this PSU has seen an AC fault
562         acFault = AC_FAULT_LIMIT;
563     }
564     else
565     {
566         if (vinUVFault != 0)
567         {
568             lg2::info(
569                 "{SHORT_NAME} VIN_UV fault cleared: STATUS_WORD = {STATUS_WORD}, "
570                 "STATUS_MFR_SPECIFIC = {STATUS_MFR_SPECIFIC}, "
571                 "STATUS_INPUT = {STATUS_INPUT}",
572                 "SHORT_NAME", shortName, "STATUS_WORD", lg2::hex | lg2::field16,
573                 statusWord, "STATUS_MFR_SPECIFIC", lg2::hex | lg2::field8,
574                 statusMFR, "STATUS_INPUT", lg2::hex | lg2::field8, statusInput);
575             vinUVFault = 0;
576         }
577         // No AC fail, decrement counter
578         if (acFault != 0)
579         {
580             --acFault;
581         }
582     }
583 }
584 
analyze()585 void PowerSupply::analyze()
586 {
587     using namespace phosphor::pmbus;
588 
589     if (presenceGPIO)
590     {
591         updatePresenceGPIO();
592     }
593 
594     if (present)
595     {
596         try
597         {
598             statusWordOld = statusWord;
599             statusWord = pmbusIntf->read(STATUS_WORD, Type::Debug,
600                                          (readFail < LOG_LIMIT));
601             // Read worked, reset the fail count.
602             readFail = 0;
603 
604             if (statusWord)
605             {
606                 statusInput = pmbusIntf->read(STATUS_INPUT, Type::Debug);
607                 if (bindPath.string().find(IBMCFFPS_DD_NAME) !=
608                     std::string::npos)
609                 {
610                     statusMFR = pmbusIntf->read(STATUS_MFR, Type::Debug);
611                 }
612                 statusCML = pmbusIntf->read(STATUS_CML, Type::Debug);
613                 auto status0Vout = pmbusIntf->insertPageNum(STATUS_VOUT, 0);
614                 statusVout = pmbusIntf->read(status0Vout, Type::Debug);
615                 statusIout = pmbusIntf->read(STATUS_IOUT, Type::Debug);
616                 statusFans12 = pmbusIntf->read(STATUS_FANS_1_2, Type::Debug);
617                 statusTemperature =
618                     pmbusIntf->read(STATUS_TEMPERATURE, Type::Debug);
619 
620                 analyzeCMLFault();
621 
622                 analyzeInputFault();
623 
624                 analyzeVoutOVFault();
625 
626                 analyzeIoutOCFault();
627 
628                 analyzeVoutUVFault();
629 
630                 analyzeFanFault();
631 
632                 analyzeTemperatureFault();
633 
634                 analyzePgoodFault();
635 
636                 analyzeMFRFault();
637 
638                 analyzeVinUVFault();
639             }
640             else
641             {
642                 if (statusWord != statusWordOld)
643                 {
644                     lg2::info("{SHORT_NAME} STATUS_WORD = {STATUS_WORD}",
645                               "SHORT_NAME", shortName, "STATUS_WORD",
646                               lg2::hex | lg2::field16, statusWord);
647                 }
648 
649                 // if INPUT/VIN_UV fault was on, it cleared, trace it.
650                 if (inputFault)
651                 {
652                     lg2::info(
653                         "{SHORT_NAME} INPUT fault cleared: STATUS_WORD = {STATUS_WORD}",
654                         "SHORT_NAME", shortName, "STATUS_WORD",
655                         lg2::hex | lg2::field16, statusWord);
656                 }
657 
658                 if (vinUVFault)
659                 {
660                     lg2::info(
661                         "{SHORT_NAME} VIN_UV cleared: STATUS_WORD = {STATUS_WORD}",
662                         "SHORT_NAME", shortName, "STATUS_WORD",
663                         lg2::hex | lg2::field16, statusWord);
664                 }
665 
666                 if (pgoodFault > 0)
667                 {
668                     lg2::info("{SHORT_NAME} pgoodFault cleared", "SHORT_NAME",
669                               shortName);
670                 }
671 
672                 clearFaultFlags();
673                 // No AC fail, decrement counter
674                 if (acFault != 0)
675                 {
676                     --acFault;
677                 }
678             }
679 
680             // Save off old inputVoltage value.
681             // Get latest inputVoltage.
682             // If voltage went from below minimum, and now is not, clear faults.
683             // Note: getInputVoltage() has its own try/catch.
684             int inputVoltageOld = inputVoltage;
685             double actualInputVoltageOld = actualInputVoltage;
686             getInputVoltage(actualInputVoltage, inputVoltage);
687             if ((inputVoltageOld == in_input::VIN_VOLTAGE_0) &&
688                 (inputVoltage != in_input::VIN_VOLTAGE_0))
689             {
690                 lg2::info(
691                     "{SHORT_NAME} READ_VIN back in range: actualInputVoltageOld = {ACTUAL_INPUT_VOLTAGE_OLD} "
692                     "actualInputVoltage = {ACTUAL_INPUT_VOLTAGE}",
693                     "SHORT_NAME", shortName, "ACTUAL_INPUT_VOLTAGE_OLD",
694                     actualInputVoltageOld, "ACTUAL_INPUT_VOLTAGE",
695                     actualInputVoltage);
696                 clearVinUVFault();
697             }
698             else if (vinUVFault && (inputVoltage != in_input::VIN_VOLTAGE_0))
699             {
700                 lg2::info(
701                     "{SHORT_NAME} CLEAR_FAULTS: vinUVFault {VIN_UV_FAULT} actualInputVoltage {ACTUAL_INPUT_VOLTAGE}",
702                     "SHORT_NAME", shortName, "VIN_UV_FAULT", vinUVFault,
703                     "ACTUAL_INPUT_VOLTAGE", actualInputVoltage);
704                 // Do we have a VIN_UV fault latched that can now be cleared
705                 // due to voltage back in range? Attempt to clear the
706                 // fault(s), re-check faults on next call.
707                 clearVinUVFault();
708             }
709             else if (std::abs(actualInputVoltageOld - actualInputVoltage) >
710                      10.0)
711             {
712                 lg2::info(
713                     "{SHORT_NAME} actualInputVoltageOld = {ACTUAL_INPUT_VOLTAGE_OLD} actualInputVoltage = {ACTUAL_INPUT_VOLTAGE}",
714                     "SHORT_NAME", shortName, "ACTUAL_INPUT_VOLTAGE_OLD",
715                     actualInputVoltageOld, "ACTUAL_INPUT_VOLTAGE",
716                     actualInputVoltage);
717             }
718 
719             monitorSensors();
720 
721             checkAvailability();
722         }
723         catch (const ReadFailure& e)
724         {
725             if (readFail < SIZE_MAX)
726             {
727                 readFail++;
728             }
729             if (readFail == LOG_LIMIT)
730             {
731                 phosphor::logging::commit<ReadFailure>();
732             }
733         }
734     }
735 }
736 
onOffConfig(uint8_t data)737 void PowerSupply::onOffConfig(uint8_t data)
738 {
739     using namespace phosphor::pmbus;
740 
741     if (present && driverName != ACBEL_FSG032_DD_NAME)
742     {
743         lg2::info("ON_OFF_CONFIG write: DATA={DATA}", "DATA",
744                   lg2::hex | lg2::field8, data);
745         try
746         {
747             std::vector<uint8_t> configData{data};
748             pmbusIntf->writeBinary(ON_OFF_CONFIG, configData,
749                                    Type::HwmonDeviceDebug);
750         }
751         catch (...)
752         {
753             // The underlying code in writeBinary will log a message to the
754             // journal if the write fails. If the ON_OFF_CONFIG is not setup
755             // as desired, later fault detection and analysis code should
756             // catch any of the fall out. We should not need to terminate
757             // the application if this write fails.
758         }
759     }
760 }
761 
clearVinUVFault()762 void PowerSupply::clearVinUVFault()
763 {
764     // Read in1_lcrit_alarm to clear bits 3 and 4 of STATUS_INPUT.
765     // The fault bits in STAUTS_INPUT roll-up to STATUS_WORD. Clearing those
766     // bits in STATUS_INPUT should result in the corresponding STATUS_WORD bits
767     // also clearing.
768     //
769     // Do not care about return value. Should be 1 if active, 0 if not.
770     if (driverName != ACBEL_FSG032_DD_NAME)
771     {
772         static_cast<void>(
773             pmbusIntf->read("in1_lcrit_alarm", phosphor::pmbus::Type::Hwmon));
774     }
775     else
776     {
777         static_cast<void>(
778             pmbusIntf->read("curr1_crit_alarm", phosphor::pmbus::Type::Hwmon));
779     }
780     vinUVFault = 0;
781 }
782 
clearFaults()783 void PowerSupply::clearFaults()
784 {
785     lg2::debug("clearFaults() inventoryPath: {INVENTORY_PATH}",
786                "INVENTORY_PATH", inventoryPath);
787     faultLogged = false;
788     // The PMBus device driver does not allow for writing CLEAR_FAULTS
789     // directly. However, the pmbus hwmon device driver code will send a
790     // CLEAR_FAULTS after reading from any of the hwmon "files" in sysfs, so
791     // reading in1_input should result in clearing the fault bits in
792     // STATUS_BYTE/STATUS_WORD.
793     // I do not care what the return value is.
794     if (present)
795     {
796         clearFaultFlags();
797         checkAvailability();
798         readFail = 0;
799 
800         try
801         {
802             clearVinUVFault();
803             static_cast<void>(
804                 pmbusIntf->read("in1_input", phosphor::pmbus::Type::Hwmon));
805         }
806         catch (const ReadFailure& e)
807         {
808             // Since I do not care what the return value is, I really do not
809             // care much if it gets a ReadFailure either. However, this
810             // should not prevent the application from continuing to run, so
811             // catching the read failure.
812         }
813     }
814 }
815 
inventoryChanged(sdbusplus::message_t & msg)816 void PowerSupply::inventoryChanged(sdbusplus::message_t& msg)
817 {
818     std::string msgSensor;
819     std::map<std::string, std::variant<uint32_t, bool>> msgData;
820     msg.read(msgSensor, msgData);
821 
822     // Check if it was the Present property that changed.
823     auto valPropMap = msgData.find(PRESENT_PROP);
824     if (valPropMap != msgData.end())
825     {
826         if (std::get<bool>(valPropMap->second))
827         {
828             present = true;
829             // TODO: Immediately trying to read or write the "files" causes
830             // read or write failures.
831             using namespace std::chrono_literals;
832             std::this_thread::sleep_for(20ms);
833             pmbusIntf->findHwmonDir();
834             onOffConfig(phosphor::pmbus::ON_OFF_CONFIG_CONTROL_PIN_ONLY);
835             clearFaults();
836             updateInventory();
837         }
838         else
839         {
840             present = false;
841 
842             // Clear out the now outdated inventory properties
843             updateInventory();
844         }
845         checkAvailability();
846     }
847 }
848 
inventoryAdded(sdbusplus::message_t & msg)849 void PowerSupply::inventoryAdded(sdbusplus::message_t& msg)
850 {
851     sdbusplus::message::object_path path;
852     msg.read(path);
853     // Make sure the signal is for the PSU inventory path
854     if (path == inventoryPath)
855     {
856         std::map<std::string, std::map<std::string, std::variant<bool>>>
857             interfaces;
858         // Get map of interfaces and their properties
859         msg.read(interfaces);
860 
861         auto properties = interfaces.find(INVENTORY_IFACE);
862         if (properties != interfaces.end())
863         {
864             auto property = properties->second.find(PRESENT_PROP);
865             if (property != properties->second.end())
866             {
867                 present = std::get<bool>(property->second);
868 
869                 lg2::info("Power Supply {INVENTORY_PATH} Present {PRESENT}",
870                           "INVENTORY_PATH", inventoryPath, "PRESENT", present);
871 
872                 updateInventory();
873                 checkAvailability();
874             }
875         }
876     }
877 }
878 
readVPDValue(const std::string & vpdName,const phosphor::pmbus::Type & type,const std::size_t & vpdSize)879 auto PowerSupply::readVPDValue(const std::string& vpdName,
880                                const phosphor::pmbus::Type& type,
881                                const std::size_t& vpdSize)
882 {
883     std::string vpdValue;
884     const std::regex illegalVPDRegex =
885         std::regex("[^[:alnum:]]", std::regex::basic);
886 
887     try
888     {
889         vpdValue = pmbusIntf->readString(vpdName, type);
890     }
891     catch (const ReadFailure& e)
892     {
893         // Ignore the read failure, let pmbus code indicate failure,
894         // path...
895         // TODO - ibm918
896         // https://github.com/openbmc/docs/blob/master/designs/vpd-collection.md
897         // The BMC must log errors if any of the VPD cannot be properly
898         // parsed or fails ECC checks.
899     }
900 
901     if (vpdValue.size() != vpdSize)
902     {
903         lg2::info("{SHORT_NAME} {VPD_NAME} resize needed. size: {SIZE}",
904                   "SHORT_NAME", shortName, "VPD_NAME", vpdName, "SIZE",
905                   vpdValue.size());
906         vpdValue.resize(vpdSize, ' ');
907     }
908 
909     // Replace any illegal values with space(s).
910     std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
911                        illegalVPDRegex, " ");
912 
913     return vpdValue;
914 }
915 
updateInventory()916 void PowerSupply::updateInventory()
917 {
918     using namespace phosphor::pmbus;
919 
920 #if IBM_VPD
921     std::string pn;
922     std::string fn;
923     std::string header;
924     std::string sn;
925     // The IBM power supply splits the full serial number into two parts.
926     // Each part is 6 bytes long, which should match up with SN_KW_SIZE.
927     const auto HEADER_SIZE = 6;
928     const auto SERIAL_SIZE = 6;
929     // The IBM PSU firmware version size is a bit complicated. It was originally
930     // 1-byte, per command. It was later expanded to 2-bytes per command, then
931     // up to 8-bytes per command. The device driver only reads up to 2 bytes per
932     // command, but combines all three of the 2-byte reads, or all 4 of the
933     // 1-byte reads into one string. So, the maximum size expected is 6 bytes.
934     // However, it is formatted by the driver as a hex string with two ASCII
935     // characters per byte.  So the maximum ASCII string size is 12.
936     const auto IBMCFFPS_FW_VERSION_SIZE = 12;
937     const auto ACBEL_FSG032_FW_VERSION_SIZE = 6;
938 
939     using PropertyMap =
940         std::map<std::string,
941                  std::variant<std::string, std::vector<uint8_t>, bool>>;
942     PropertyMap assetProps;
943     PropertyMap operProps;
944     PropertyMap versionProps;
945     PropertyMap ipzvpdDINFProps;
946     PropertyMap ipzvpdVINIProps;
947     using InterfaceMap = std::map<std::string, PropertyMap>;
948     InterfaceMap interfaces;
949     using ObjectMap = std::map<sdbusplus::message::object_path, InterfaceMap>;
950     ObjectMap object;
951 #endif
952     lg2::debug("updateInventory() inventoryPath: {INVENTORY_PATH}",
953                "INVENTORY_PATH", inventoryPath);
954 
955     if (present)
956     {
957         // TODO: non-IBM inventory updates?
958 
959 #if IBM_VPD
960         if (driverName == ACBEL_FSG032_DD_NAME)
961         {
962             getPsuVpdFromDbus("CC", modelName);
963             getPsuVpdFromDbus("PN", pn);
964             getPsuVpdFromDbus("FN", fn);
965             getPsuVpdFromDbus("SN", sn);
966             assetProps.emplace(SN_PROP, sn);
967             fwVersion = readVPDValue(FW_VERSION, Type::Debug,
968                                      ACBEL_FSG032_FW_VERSION_SIZE);
969             versionProps.emplace(VERSION_PROP, fwVersion);
970         }
971         else
972         {
973             modelName = readVPDValue(CCIN, Type::HwmonDeviceDebug, CC_KW_SIZE);
974             pn = readVPDValue(PART_NUMBER, Type::Debug, PN_KW_SIZE);
975             fn = readVPDValue(FRU_NUMBER, Type::Debug, FN_KW_SIZE);
976 
977             header = readVPDValue(SERIAL_HEADER, Type::Debug, HEADER_SIZE);
978             sn = readVPDValue(SERIAL_NUMBER, Type::Debug, SERIAL_SIZE);
979             assetProps.emplace(SN_PROP, header + sn);
980             fwVersion = readVPDValue(FW_VERSION, Type::HwmonDeviceDebug,
981                                      IBMCFFPS_FW_VERSION_SIZE);
982             versionProps.emplace(VERSION_PROP, fwVersion);
983         }
984 
985         assetProps.emplace(MODEL_PROP, modelName);
986         assetProps.emplace(PN_PROP, pn);
987         assetProps.emplace(SPARE_PN_PROP, fn);
988 
989         ipzvpdVINIProps.emplace(
990             "CC", std::vector<uint8_t>(modelName.begin(), modelName.end()));
991         ipzvpdVINIProps.emplace("PN",
992                                 std::vector<uint8_t>(pn.begin(), pn.end()));
993         ipzvpdVINIProps.emplace("FN",
994                                 std::vector<uint8_t>(fn.begin(), fn.end()));
995         std::string header_sn = header + sn;
996         ipzvpdVINIProps.emplace(
997             "SN", std::vector<uint8_t>(header_sn.begin(), header_sn.end()));
998         std::string description = "IBM PS";
999         ipzvpdVINIProps.emplace(
1000             "DR", std::vector<uint8_t>(description.begin(), description.end()));
1001 
1002         // Populate the VINI Resource Type (RT) keyword
1003         ipzvpdVINIProps.emplace("RT", std::vector<uint8_t>{'V', 'I', 'N', 'I'});
1004 
1005         // Update the Resource Identifier (RI) keyword
1006         // 2 byte FRC: 0x0003
1007         // 2 byte RID: 0x1000, 0x1001...
1008         std::uint8_t num = std::stoul(
1009             inventoryPath.substr(inventoryPath.size() - 1, 1), nullptr, 0);
1010         std::vector<uint8_t> ri{0x00, 0x03, 0x10, num};
1011         ipzvpdDINFProps.emplace("RI", ri);
1012 
1013         // Fill in the FRU Label (FL) keyword.
1014         std::string fl = "E";
1015         fl.push_back(inventoryPath.back());
1016         fl.resize(FL_KW_SIZE, ' ');
1017         ipzvpdDINFProps.emplace("FL",
1018                                 std::vector<uint8_t>(fl.begin(), fl.end()));
1019 
1020         // Populate the DINF Resource Type (RT) keyword
1021         ipzvpdDINFProps.emplace("RT", std::vector<uint8_t>{'D', 'I', 'N', 'F'});
1022 
1023         interfaces.emplace(ASSET_IFACE, std::move(assetProps));
1024         interfaces.emplace(VERSION_IFACE, std::move(versionProps));
1025         interfaces.emplace(DINF_IFACE, std::move(ipzvpdDINFProps));
1026         interfaces.emplace(VINI_IFACE, std::move(ipzvpdVINIProps));
1027 
1028         // Update the Functional
1029         operProps.emplace(FUNCTIONAL_PROP, present);
1030         interfaces.emplace(OPERATIONAL_STATE_IFACE, std::move(operProps));
1031 
1032         auto path = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
1033         object.emplace(path, std::move(interfaces));
1034 
1035         try
1036         {
1037             auto service =
1038                 util::getService(INVENTORY_OBJ_PATH, INVENTORY_MGR_IFACE, bus);
1039 
1040             if (service.empty())
1041             {
1042                 lg2::error("Unable to get inventory manager service");
1043                 return;
1044             }
1045 
1046             auto method =
1047                 bus.new_method_call(service.c_str(), INVENTORY_OBJ_PATH,
1048                                     INVENTORY_MGR_IFACE, "Notify");
1049 
1050             method.append(std::move(object));
1051 
1052             auto reply = bus.call(method);
1053         }
1054         catch (const std::exception& e)
1055         {
1056             lg2::error(
1057                 "Exception in updateInventory(): {ERROR}, PATH={INVENTORY_PATH}",
1058                 "ERROR", e, "INVENTORY_PATH", inventoryPath);
1059         }
1060 #endif
1061     }
1062 }
1063 
getMaxPowerOut() const1064 auto PowerSupply::getMaxPowerOut() const
1065 {
1066     using namespace phosphor::pmbus;
1067 
1068     auto maxPowerOut = 0;
1069 
1070     if (present)
1071     {
1072         try
1073         {
1074             // Read max_power_out, should be direct format
1075             auto maxPowerOutStr =
1076                 pmbusIntf->readString(MFR_POUT_MAX, Type::HwmonDeviceDebug);
1077             lg2::info("{SHORT_NAME} MFR_POUT_MAX read {MAX_POWER_OUT_STR}",
1078                       "SHORT_NAME", shortName, "MAX_POWER_OUT_STR",
1079                       maxPowerOutStr);
1080             maxPowerOut = std::stod(maxPowerOutStr);
1081         }
1082         catch (const std::exception& e)
1083         {
1084             lg2::error("{SHORT_NAME} MFR_POUT_MAX read error: {ERROR}",
1085                        "SHORT_NAME", shortName, "ERROR", e);
1086         }
1087     }
1088 
1089     return maxPowerOut;
1090 }
1091 
setupSensors()1092 void PowerSupply::setupSensors()
1093 {
1094     setupInputPowerPeakSensor();
1095 }
1096 
setupInputPowerPeakSensor()1097 void PowerSupply::setupInputPowerPeakSensor()
1098 {
1099     if (peakInputPowerSensor || !present ||
1100         (bindPath.string().find(IBMCFFPS_DD_NAME) == std::string::npos))
1101     {
1102         return;
1103     }
1104 
1105     // This PSU has problems with the input_history command
1106     if (getMaxPowerOut() == phosphor::pmbus::IBM_CFFPS_1400W)
1107     {
1108         return;
1109     }
1110 
1111     auto sensorPath = std::format(
1112         "/xyz/openbmc_project/sensors/power/{}_ps{}_input_power_peak",
1113         chassisName, shortName.back());
1114     peakInputPowerSensor = std::make_unique<PowerSensorObject>(
1115         bus, sensorPath.c_str(), PowerSensorObject::action::defer_emit);
1116 
1117     // The others can remain at the defaults.
1118     peakInputPowerSensor->functional(true, true);
1119     peakInputPowerSensor->available(true, true);
1120     peakInputPowerSensor->value(0, true);
1121     peakInputPowerSensor->unit(
1122         sdbusplus::xyz::openbmc_project::Sensor::server::Value::Unit::Watts,
1123         true);
1124 
1125     auto associations = getSensorAssociations();
1126     peakInputPowerSensor->associations(associations, true);
1127 
1128     peakInputPowerSensor->emit_object_added();
1129 }
1130 
setSensorsNotAvailable()1131 void PowerSupply::setSensorsNotAvailable()
1132 {
1133     if (peakInputPowerSensor)
1134     {
1135         peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1136         peakInputPowerSensor->available(false);
1137     }
1138 }
1139 
monitorSensors()1140 void PowerSupply::monitorSensors()
1141 {
1142     monitorPeakInputPowerSensor();
1143 }
1144 
monitorPeakInputPowerSensor()1145 void PowerSupply::monitorPeakInputPowerSensor()
1146 {
1147     if (!peakInputPowerSensor)
1148     {
1149         return;
1150     }
1151 
1152     constexpr size_t recordSize = 5;
1153     std::vector<uint8_t> data;
1154 
1155     // Get the peak input power with input history command.
1156     // New data only shows up every 30s, but just try to read it every 1s
1157     // anyway so we always have the most up to date value.
1158     try
1159     {
1160         data = pmbusIntf->readBinary(INPUT_HISTORY,
1161                                      pmbus::Type::HwmonDeviceDebug, recordSize);
1162     }
1163     catch (const ReadFailure& e)
1164     {
1165         peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1166         peakInputPowerSensor->functional(false);
1167         throw;
1168     }
1169 
1170     if (data.size() != recordSize)
1171     {
1172         lg2::debug(
1173             "Input history command returned {DATA_SIZE} bytes instead of 5",
1174             "DATA_SIZE", data.size());
1175         peakInputPowerSensor->value(std::numeric_limits<double>::quiet_NaN());
1176         peakInputPowerSensor->functional(false);
1177         return;
1178     }
1179 
1180     // The format is SSAAAAPPPP:
1181     //   SS = packet sequence number
1182     //   AAAA = average power (linear format, little endian)
1183     //   PPPP = peak power (linear format, little endian)
1184     auto peak = static_cast<uint16_t>(data[4]) << 8 | data[3];
1185     auto peakPower = linearToInteger(peak);
1186 
1187     peakInputPowerSensor->value(peakPower);
1188     peakInputPowerSensor->functional(true);
1189     peakInputPowerSensor->available(true);
1190 }
1191 
getInputVoltage(double & actualInputVoltage,int & inputVoltage) const1192 void PowerSupply::getInputVoltage(double& actualInputVoltage,
1193                                   int& inputVoltage) const
1194 {
1195     using namespace phosphor::pmbus;
1196 
1197     actualInputVoltage = in_input::VIN_VOLTAGE_0;
1198     inputVoltage = in_input::VIN_VOLTAGE_0;
1199 
1200     if (present)
1201     {
1202         try
1203         {
1204             // Read input voltage in millivolts
1205             auto inputVoltageStr = pmbusIntf->readString(READ_VIN, Type::Hwmon);
1206 
1207             // Convert to volts
1208             actualInputVoltage = std::stod(inputVoltageStr) / 1000;
1209 
1210             // Calculate the voltage based on voltage thresholds
1211             if (actualInputVoltage < in_input::VIN_VOLTAGE_MIN)
1212             {
1213                 inputVoltage = in_input::VIN_VOLTAGE_0;
1214             }
1215             else if (actualInputVoltage < in_input::VIN_VOLTAGE_110_THRESHOLD)
1216             {
1217                 inputVoltage = in_input::VIN_VOLTAGE_110;
1218             }
1219             else
1220             {
1221                 inputVoltage = in_input::VIN_VOLTAGE_220;
1222             }
1223         }
1224         catch (const std::exception& e)
1225         {
1226             lg2::error("{SHORT_NAME} READ_VIN read error: {ERROR}",
1227                        "SHORT_NAME", shortName, "ERROR", e);
1228         }
1229     }
1230 }
1231 
checkAvailability()1232 void PowerSupply::checkAvailability()
1233 {
1234     bool origAvailability = available;
1235     bool faulted = isPowerOn() && (hasPSKillFault() || hasIoutOCFault());
1236     available = present && !hasInputFault() && !hasVINUVFault() && !faulted;
1237 
1238     if (origAvailability != available)
1239     {
1240         auto invpath = inventoryPath.substr(strlen(INVENTORY_OBJ_PATH));
1241         phosphor::power::psu::setAvailable(bus, invpath, available);
1242 
1243         // Check if the health rollup needs to change based on the
1244         // new availability value.
1245         phosphor::power::psu::handleChassisHealthRollup(bus, inventoryPath,
1246                                                         !available);
1247     }
1248 }
1249 
setInputVoltageRating()1250 void PowerSupply::setInputVoltageRating()
1251 {
1252     if (!present)
1253     {
1254         if (inputVoltageRatingIface)
1255         {
1256             inputVoltageRatingIface->value(0);
1257             inputVoltageRatingIface.reset();
1258         }
1259         return;
1260     }
1261 
1262     double inputVoltageValue{};
1263     int inputVoltageRating{};
1264     getInputVoltage(inputVoltageValue, inputVoltageRating);
1265 
1266     if (!inputVoltageRatingIface)
1267     {
1268         auto path = std::format(
1269             "/xyz/openbmc_project/sensors/voltage/ps{}_input_voltage_rating",
1270             shortName.back());
1271 
1272         inputVoltageRatingIface = std::make_unique<SensorObject>(
1273             bus, path.c_str(), SensorObject::action::defer_emit);
1274 
1275         // Leave other properties at their defaults
1276         inputVoltageRatingIface->unit(SensorInterface::Unit::Volts, true);
1277         inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating),
1278                                        true);
1279 
1280         inputVoltageRatingIface->emit_object_added();
1281     }
1282     else
1283     {
1284         inputVoltageRatingIface->value(static_cast<double>(inputVoltageRating));
1285     }
1286 }
1287 
getPsuVpdFromDbus(const std::string & keyword,std::string & vpdStr)1288 void PowerSupply::getPsuVpdFromDbus(const std::string& keyword,
1289                                     std::string& vpdStr)
1290 {
1291     try
1292     {
1293         std::vector<uint8_t> value;
1294         vpdStr.clear();
1295         util::getProperty(VINI_IFACE, keyword, inventoryPath,
1296                           INVENTORY_MGR_IFACE, bus, value);
1297         for (char c : value)
1298         {
1299             vpdStr += c;
1300         }
1301     }
1302     catch (const sdbusplus::exception_t& e)
1303     {
1304         lg2::error("Failed getProperty error: {ERROR}", "ERROR", e);
1305     }
1306 }
1307 
linearToInteger(uint16_t data)1308 double PowerSupply::linearToInteger(uint16_t data)
1309 {
1310     // The exponent is the first 5 bits, followed by 11 bits of mantissa.
1311     int8_t exponent = (data & 0xF800) >> 11;
1312     int16_t mantissa = (data & 0x07FF);
1313 
1314     // If exponent's MSB on, then it's negative.
1315     // Convert from two's complement.
1316     if (exponent & 0x10)
1317     {
1318         exponent = (~exponent) & 0x1F;
1319         exponent = (exponent + 1) * -1;
1320     }
1321 
1322     // If mantissa's MSB on, then it's negative.
1323     // Convert from two's complement.
1324     if (mantissa & 0x400)
1325     {
1326         mantissa = (~mantissa) & 0x07FF;
1327         mantissa = (mantissa + 1) * -1;
1328     }
1329 
1330     auto value = static_cast<double>(mantissa) * pow(2, exponent);
1331     return value;
1332 }
1333 
getSensorAssociations()1334 std::vector<AssociationTuple> PowerSupply::getSensorAssociations()
1335 {
1336     std::vector<AssociationTuple> associations;
1337 
1338     associations.emplace_back("inventory", "sensors", inventoryPath);
1339 
1340     auto chassis = getChassis(bus, inventoryPath);
1341     associations.emplace_back("chassis", "all_sensors", std::move(chassis));
1342 
1343     return associations;
1344 }
1345 
1346 } // namespace phosphor::power::psu
1347