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