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