Add in code to handle INPUT fault or warning

If the INPUT fault or warning bit in the STATUS_WORD turns on, report
a fault that includes STATUS_WORD and STATUS_INPUT values in the
metadata.

Change-Id: I2c0bc187357088a667dba1248be9a1c52f9dc073
Signed-off-by: Brandon Wyman <bjwyman@gmail.com>
diff --git a/elog-errors.hpp b/elog-errors.hpp
index 5a6fad8..7309c89 100644
--- a/elog-errors.hpp
+++ b/elog-errors.hpp
@@ -181,6 +181,26 @@
 {
 namespace Error
 {
+    struct PowerSupplyInputFault;
+} // namespace Error
+} // namespace Fault
+} // namespace Power
+} // namespace openbmc_project
+} // namespace xyz
+} // namespace sdbusplus
+
+namespace sdbusplus
+{
+namespace xyz
+{
+namespace openbmc_project
+{
+namespace Power
+{
+namespace Fault
+{
+namespace Error
+{
     struct GPUPowerFault;
 } // namespace Error
 } // namespace Fault
@@ -588,6 +608,53 @@
 {
 namespace Fault
 {
+namespace _PowerSupplyInputFault
+{
+
+struct RAW_STATUS
+{
+    static constexpr auto str = "RAW_STATUS=%s";
+    static constexpr auto str_short = "RAW_STATUS";
+    using type = std::tuple<std::decay_t<decltype(str)>,const char*>;
+    explicit constexpr RAW_STATUS(const char* a) : _entry(entry(str, a)) {};
+    type _entry;
+};
+
+}  // namespace _PowerSupplyInputFault
+
+struct PowerSupplyInputFault
+{
+    static constexpr auto L = level::ERR;
+    using RAW_STATUS = _PowerSupplyInputFault::RAW_STATUS;
+    using metadata_types = std::tuple<RAW_STATUS>;
+
+};
+
+} // namespace Fault
+} // namespace Power
+} // namespace openbmc_project
+} // namespace xyz
+
+
+namespace details
+{
+
+template <>
+struct map_exception_type<sdbusplus::xyz::openbmc_project::Power::Fault::Error::PowerSupplyInputFault>
+{
+    using type = xyz::openbmc_project::Power::Fault::PowerSupplyInputFault;
+};
+
+}
+
+namespace xyz
+{
+namespace openbmc_project
+{
+namespace Power
+{
+namespace Fault
+{
 namespace _Shutdown
 {
 
diff --git a/pmbus.hpp b/pmbus.hpp
index 9254fe7..214040d 100644
--- a/pmbus.hpp
+++ b/pmbus.hpp
@@ -11,11 +11,18 @@
 
 namespace fs = std::experimental::filesystem;
 
+// The file name Linux uses to capture the VIN_UV_FAULT bit from the STATUS_WORD
+constexpr auto VIN_UV_FAULT = "in1_alarm";
+
+// The file name Linux uses to capture the input fault or warning bit from the
+// STATUS_WORD
+constexpr auto INPUT_FAULT_WARN = "power1_alarm";
+
 // The file name Linux uses to capture the STATUS_WORD from pmbus.
 constexpr auto STATUS_WORD = "status0";
 
-// The file name Linux uses to capture the VIN_UV_FAULT bit from the STATUS_WORD
-constexpr auto VIN_UV_FAULT = "in1_alarm";
+// The file name Linux uses to capture the STATUS_INPUT from pmbus.
+constexpr auto STATUS_INPUT = "status0_input";
 
 // Uses Page substitution
 constexpr auto STATUS_VOUT = "statusP_vout";
diff --git a/power-supply/power_supply.cpp b/power-supply/power_supply.cpp
index f70b79d..f5d3fa3 100644
--- a/power-supply/power_supply.cpp
+++ b/power-supply/power_supply.cpp
@@ -46,8 +46,6 @@
     : Device(name, inst), monitorPath(objpath), inventoryPath(invpath),
       bus(bus), pmbusIntf(objpath)
 {
-    updatePresence();
-
     using namespace sdbusplus::bus;
     auto present_obj_path = INVENTORY_OBJ_PATH + inventoryPath;
     presentMatch = std::make_unique<match_t>(bus,
@@ -58,6 +56,8 @@
     {
         this->inventoryChanged(msg);
     });
+
+    updatePresence();
 }
 
 void PowerSupply::analyze()
@@ -76,11 +76,12 @@
             // If count reaches 3, we have fault. If count reaches 0, fault is
             // cleared.
 
-            //TODO: INPUT FAULT or WARNING bit to check from STATUS_WORD
-            // pmbus-core update to read high byte of STATUS_WORD?
+            auto curInputFault = pmbusIntf.readBit(INPUT_FAULT_WARN,
+                                                   Type::Hwmon);
 
-            if ((curUVFault != vinUVFault) || inputFault)
+            if (curUVFault != vinUVFault)
             {
+                vinUVFault = curUVFault;
 
                 if (curUVFault)
                 {
@@ -103,8 +104,37 @@
                     log<level::INFO>("VIN_UV_FAULT cleared",
                                      entry("POWERSUPPLY=%s",
                                            inventoryPath.c_str()));
-                    vinUVFault = false;
                 }
+
+            }
+
+            if (curInputFault != inputFault)
+            {
+                if (curInputFault)
+                {
+                    std::uint16_t statusWord = 0;
+                    std::uint8_t  statusInput = 0;
+
+                    statusWord = pmbusIntf.read(STATUS_WORD, Type::Debug);
+                    statusInput = pmbusIntf.read(STATUS_INPUT, Type::Debug);
+
+                    util::NamesValues nv;
+                    nv.add("STATUS_WORD", statusWord);
+                    nv.add("STATUS_INPUT", statusInput);
+
+                    using metadata = xyz::openbmc_project::Power::Fault::
+                            PowerSupplyInputFault;
+
+                    report<PowerSupplyInputFault>(
+                            metadata::RAW_STATUS(nv.get().c_str()));
+
+                    inputFault = true;
+                }
+                else
+                {
+                    inputFault = false;
+                }
+
             }
         }
     }
@@ -138,6 +168,7 @@
         {
             readFailLogged = false;
             vinUVFault = false;
+            inputFault = false;
         }
     }
 
diff --git a/xyz/openbmc_project/Power/Fault.errors.yaml b/xyz/openbmc_project/Power/Fault.errors.yaml
index bd9d7b5..6911753 100644
--- a/xyz/openbmc_project/Power/Fault.errors.yaml
+++ b/xyz/openbmc_project/Power/Fault.errors.yaml
@@ -1,5 +1,7 @@
 - name: PowerSupplyUnderVoltageFault
   description: The power supply has indicated an input or under voltage fault condition.
+- name: PowerSupplyInputFault
+  description: The power supply has indicated an input fault or warn condition.
 - name: Shutdown
   description: A power off was issued because a power fault was detected
 
diff --git a/xyz/openbmc_project/Power/Fault.metadata.yaml b/xyz/openbmc_project/Power/Fault.metadata.yaml
index 5f02864..184fffa 100644
--- a/xyz/openbmc_project/Power/Fault.metadata.yaml
+++ b/xyz/openbmc_project/Power/Fault.metadata.yaml
@@ -3,6 +3,11 @@
   meta:
     - str: "RAW_STATUS=%s"
       type: string
+- name: PowerSupplyInputFault
+  level: ERR
+  meta:
+    - str: "RAW_STATUS=%s"
+      type: string
 - name: Shutdown
   level: ERR