meta-facebook: yosemite4: NIC[0~3] power fault event log

Summary:
The system shall register an event when NIC[0~3] power fault.
BMC monitors GPIO "ALT_SMB_BMC_CPLD2_N" and then read IOE pin.

Test Plan:
With a testing CPLD firmware, set IOE pin high to trigger NIC power fault.

Testing:
1. trigger NIC1 power fault by setting IOE reg 0x0a, bit1 = 1:
root@bmc:~# i2ctransfer -f -y 28 w2@0x25 0x0a 0x02 r1
0x00

2. read IOE reg 0x01 (bit1 = 1):
root@bmc:~# i2ctransfer -f -y 28 w1@0x25 0x01 r1
0x02

3. Check event log
root@bmc:~# mfg-tool log-display
    "2": {
        "additional_data": [
            "NIC1 Power Fault=1"
        ],
        "event_id": "",
        "message": "NIC1 Power Fault",
        "resolution": "",
        "resolved": false,
        "severity": "xyz.openbmc_project.Logging.Entry.Level.Error",
        "timestamp": "2024-08-19T07:50:01.311000000Z",
        "updated_timestamp": "2024-08-19T07:50:01.311000000Z"
    },

Change-Id: I12b297b14b8be4c97c1f7d0949b0b6c16b91fa40
Signed-off-by: Rush Chen <rush.chen.wiwynn@gmail.com>
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/nic-power-fault b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/nic-power-fault
new file mode 100644
index 0000000..2e3c260
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/nic-power-fault
@@ -0,0 +1,88 @@
+#!/bin/bash
+
+hex_to_binary ()
+{
+    case $1 in
+    "0")
+    echo "0000"
+    ;;
+    "1")
+    echo "0001"
+    ;;
+    "2")
+    echo "0010"
+    ;;
+    "3")
+    echo "0011"
+    ;;
+    "4")
+    echo "0100"
+    ;;
+    "5")
+    echo "0101"
+    ;;
+    "6")
+    echo "0110"
+    ;;
+    "7")
+    echo "0111"
+    ;;
+    "8")
+    echo "1000"
+    ;;
+    "9")
+    echo "1001"
+    ;;
+    "a")
+    echo "1010"
+    ;;
+    "b")
+    echo "1011"
+    ;;
+    "c")
+    echo "1100"
+    ;;
+    "d")
+    echo "1101"
+    ;;
+    "e")
+    echo "1110"
+    ;;
+    "f")
+    echo "1111"
+    ;;
+    esac
+}
+
+hex_string_to_binary ()
+{
+    hex_string=$1
+    len=${#hex_string}
+    if [ "$len" != "4" ]
+    then
+        return 1
+    fi
+
+    h1=$(hex_to_binary "${hex_string:2:1}")
+    h2=$(hex_to_binary "${hex_string:3:1}")
+    echo "$h1$h2"
+}
+
+# i2ctranster to get CPLD IOE (0x25) addr 0x01 (port1, power fault)
+read -r FAULT_BYTE <<< "$(i2ctransfer -f -y 28 w1@0x25 0x01 r1)"
+
+# convert hex to binary
+FAULT_BIN=$(hex_string_to_binary "$FAULT_BYTE")
+
+# check bit 0~3: NICx_power_fault
+for i in 7 6 5 4; do
+    if [ "${FAULT_BIN:$i:1}" -ne "0" ]; then
+        nic=$((7-i))
+        MESSAGE="NIC$nic Power Fault"
+
+        busctl call \
+        xyz.openbmc_project.Logging /xyz/openbmc_project/logging \
+        xyz.openbmc_project.Logging.Create Create "ssa{ss}" "$MESSAGE" \
+        xyz.openbmc_project.Logging.Entry.Level.Error 1 "$MESSAGE" "1"
+    fi
+done
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/nic-power-fault@.service b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/nic-power-fault@.service
new file mode 100644
index 0000000..203714d
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/nic-power-fault@.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=nic-power-fault
+
+[Service]
+Type=oneshot
+ExecStart=/usr/libexec/phosphor-gpio-monitor/nic-power-fault
+SyslogIdentifier=nic-power-fault
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/yosemite4-phosphor-multi-gpio-monitor.json b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/yosemite4-phosphor-multi-gpio-monitor.json
index 6e722d6..0c14fbf 100644
--- a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/yosemite4-phosphor-multi-gpio-monitor.json
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/yosemite4-phosphor-multi-gpio-monitor.json
@@ -385,5 +385,15 @@
                 "fan-board-efuse-fault@0.service"]
         },
         "Continue": true
+    },
+    {
+        "Name": "ALT_SMB_BMC_CPLD2_N",
+        "LineName": "ALT_SMB_BMC_CPLD2_N",
+        "EventMon": "FALLING",
+        "Targets": {
+            "FALLING": [
+                "nic-power-fault@0.service"]
+        },
+        "Continue": true
     }
 ]
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend
index 739aa70..96f0fa0 100644
--- a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor_%.bbappend
@@ -26,6 +26,8 @@
             file://disable-i3c-hub@.service \
             file://check-interrupt \
             file://check-interrupt@.service \
+            file://nic-power-fault \
+            file://nic-power-fault@.service \
             "
 
 RDEPENDS:${PN}:append = " bash"
@@ -42,6 +44,7 @@
     rescan-wf-bic@.service \
     slot-hsc-fault@.service \
     fan-board-efuse-fault@.service \
+    nic-power-fault@.service \
     reconfig-net-interface@.service \
     enable-i3c-hub@.service \
     disable-i3c-hub@.service \
@@ -67,6 +70,7 @@
     install -m 0644 ${WORKDIR}/enable-i3c-hub@.service ${D}${systemd_system_unitdir}/
     install -m 0644 ${WORKDIR}/disable-i3c-hub@.service ${D}${systemd_system_unitdir}/
     install -m 0644 ${WORKDIR}/check-interrupt@.service ${D}${systemd_system_unitdir}/
+    install -m 0644 ${WORKDIR}/nic-power-fault@.service ${D}${systemd_system_unitdir}/
     install -d ${D}${libexecdir}/${PN}
     install -m 0755 ${WORKDIR}/probe-slot-device ${D}${libexecdir}/${PN}/
     install -m 0755 ${WORKDIR}/reconfig-net-interface ${D}${libexecdir}/${PN}/
@@ -77,6 +81,7 @@
     install -m 0755 ${WORKDIR}/enable-i3c-hub ${D}${libexecdir}/${PN}/
     install -m 0755 ${WORKDIR}/disable-i3c-hub ${D}${libexecdir}/${PN}/
     install -m 0755 ${WORKDIR}/check-interrupt ${D}${libexecdir}/${PN}/
+    install -m 0755 ${WORKDIR}/nic-power-fault ${D}${libexecdir}/${PN}/
     install -d ${D}/${bindir}
     install -m 0755 ${WORKDIR}/configure-nic-mctp-endpoint ${D}/${bindir}/
 }