meta-facebook: yosemite4: HSC fault event

Summary:
The system shall register an event in response to an HSC fault.
BMC monitors Medusa HSC fault pin (FLT_HSC_SERVER_SLOTx_N)
on spider board CPLD IO expander.
Check power fault reason via I2C to HSC STATUS_WORD (0x79).

Test Plan:
Trigger HSC OTP fault by setting lower OT_FAULT_LIMIT (0x4F)
to pull FLT_HSC_SERVER_SLOTx_N low.

Testing:
Take FLT_HSC_SERVER_SLOT7_N for example.

1. Check init status
root@bmc:~# cat /sys/kernel/debug/gpio | grep -i FLT_HSC_SERVER_SLOT7
 gpio-698 (FLT_HSC_SERVER_SLOT7|gpio_monitor        ) in  hi IRQ
root@bmc:~# i2ctransfer -f -y 6 w1@0x40 0x79 r2
0x00 0x00
root@bmc:~# i2ctransfer -f -y 6 w1@0x40 0x4f r2
0xff 0x0f

2. Trigger HSC OTP fault
root@bmc:~# i2ctransfer -f -y 6 w3@0x40 0x4f 0x02 0x1
root@bmc:~# i2ctransfer -f -y 6 w1@0x40 0x4f r2
0x02 0x01
root@bmc:~# i2ctransfer -f -y 6 w1@0x40 0x79 r2
0x45 0x18
root@bmc:~# cat /sys/kernel/debug/gpio | grep -i FLT_HSC_SERVER_SLOT7
 gpio-698 (FLT_HSC_SERVER_SLOT7|gpio_monitor        ) in  lo IRQ

3. Check event log
root@bmc:~# mfg-tool log-display
...
    },
    "97": {
        "additional_data": [
            "STATUS_WORD=0x1845"
        ],
        "event_id": "",
        "message": "SLOT7 HSC Fault",
        "resolution": "",
        "resolved": false,
        "severity": "xyz.openbmc_project.Logging.Entry.Level.Error",
        "timestamp": "2024-07-17T02:21:30.707000000Z",
        "updated_timestamp": "2024-07-17T02:21:30.707000000Z"
    },

Change-Id: I88bac394a174165d91b471db25c0e43ccc744eec
Signed-off-by: Yikai Tsai <yikai.tsai.wiwynn@gmail.com>
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/slot-hsc-fault b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/slot-hsc-fault
new file mode 100644
index 0000000..b9afc44
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/slot-hsc-fault
@@ -0,0 +1,36 @@
+#!/bin/bash
+
+dec_to_hex() {
+    local decimal=$1
+    local hex=""
+    local digits="0123456789ABCDEF"
+
+    while [ "$decimal" -gt 0 ]; do
+        remainder=$((decimal % 16))
+        hex="${digits:remainder:1}$hex"
+        decimal=$((decimal / 16))
+    done
+
+    # ensure the length of $hex >= 4
+    while [ ${#hex} -lt 4 ]; do
+        hex="0$hex"
+    done
+
+    echo "0x${hex:-0}"
+}
+
+i2c_bus=$(($1 - 1))
+
+#i2ctransfer to get STATUS_WORD
+read -r LOW_BYTE HIGH_BYTE <<< "$(i2ctransfer -f -y $i2c_bus w1@0x40 0x79 r2)"
+STATUS_WORD=$((LOW_BYTE | (HIGH_BYTE << 8) ))
+STATUS_WORD=$(dec_to_hex $STATUS_WORD)
+
+#Add event log
+MESSAGE="SLOT$1 HSC Fault"
+ARG="STATUS_WORD"
+
+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 "$ARG" "$STATUS_WORD"
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/slot-hsc-fault@.service b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/slot-hsc-fault@.service
new file mode 100644
index 0000000..5a673ff
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/slot-hsc-fault@.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=slot hsc-fault:%i
+
+[Service]
+Type=oneshot
+ExecStart=/usr/libexec/phosphor-gpio-monitor/slot-hsc-fault %i
+SyslogIdentifier=slot-hsc-fault%i
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 b0834b8..bf625a6 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
@@ -225,5 +225,93 @@
                 "slot-hot-plug@8.service"]
         },
         "Continue": true
+    },
+    {
+        "Name": "FLT_HSC_SERVER_SLOT1_N",
+        "ChipId": "0",
+        "GpioNum": 104,
+        "EventMon": "FALLING",
+        "Targets": {
+            "FALLING": [
+                "slot-hsc-fault@1.service"]
+        },
+        "Continue": true
+    },
+    {
+        "Name": "FLT_HSC_SERVER_SLOT2_N",
+        "ChipId": "0",
+        "GpioNum": 105,
+        "EventMon": "FALLING",
+        "Targets": {
+            "FALLING": [
+                "slot-hsc-fault@2.service"]
+        },
+        "Continue": true
+    },
+    {
+        "Name": "FLT_HSC_SERVER_SLOT3_N",
+        "ChipId": "0",
+        "GpioNum": 106,
+        "EventMon": "FALLING",
+        "Targets": {
+            "FALLING": [
+                "slot-hsc-fault@3.service"]
+        },
+        "Continue": true
+    },
+    {
+        "Name": "FLT_HSC_SERVER_SLOT4_N",
+        "ChipId": "0",
+        "GpioNum": 107,
+        "EventMon": "FALLING",
+        "Targets": {
+            "FALLING": [
+                "slot-hsc-fault@4.service"]
+        },
+        "Continue": true
+    },
+    {
+        "Name": "FLT_HSC_SERVER_SLOT5_N",
+        "ChipId": "0",
+        "GpioNum": 201,
+        "EventMon": "FALLING",
+        "Targets": {
+            "FALLING": [
+                "slot-hsc-fault@5.service"]
+        },
+        "Continue": true
+    },
+    {
+        "Name": "FLT_HSC_SERVER_SLOT6_N",
+        "ChipId": "0",
+        "GpioNum": 185,
+        "EventMon": "FALLING",
+        "Targets": {
+            "FALLING": [
+                "slot-hsc-fault@6.service"]
+        },
+        "Continue": true
+    },
+    {
+        "Name": "FLT_HSC_SERVER_SLOT7_N",
+        "ChipId": "0",
+        "GpioNum": 186,
+        "EventMon": "FALLING",
+        "Targets": {
+            "FALLING": [
+                "slot-hsc-fault@7.service"]
+        },
+        "Continue": true
+    },
+    {
+        "Name": "FLT_HSC_SERVER_SLOT8_N",
+        "ChipId": "0",
+        "GpioNum": 8,
+        "EventMon": "FALLING",
+        "Targets": {
+            "FALLING": [
+                "slot-hsc-fault@8.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 112b342..6ca4f74 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
@@ -16,6 +16,8 @@
             file://slot-hot-plug@.service \
             file://rescan-wf-bic \
             file://rescan-wf-bic@.service \
+            file://slot-hsc-fault \
+            file://slot-hsc-fault@.service \
             "
 
 RDEPENDS:${PN}:append = " bash"
@@ -30,6 +32,7 @@
     setup-nic-endpoint-slot@.service \
     remove-nic-endpoint-slot@.service \
     rescan-wf-bic@.service \
+    slot-hsc-fault@.service \
     reconfig-net-interface@.service \
     "
 
@@ -47,11 +50,13 @@
     install -m 0644 ${WORKDIR}/reconfig-net-interface@.service  ${D}${systemd_system_unitdir}/reconfig-net-interface@.service
     install -m 0644 ${WORKDIR}/remove-nic-endpoint-slot@.service ${D}${systemd_system_unitdir}/
     install -m 0644 ${WORKDIR}/rescan-wf-bic@.service ${D}${systemd_system_unitdir}/
+    install -m 0644 ${WORKDIR}/slot-hsc-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}/
     install -m 0755 ${WORKDIR}/rescan-fru-device ${D}${libexecdir}/${PN}/
     install -m 0755 ${WORKDIR}/rescan-wf-bic ${D}${libexecdir}/${PN}/
+    install -m 0755 ${WORKDIR}/slot-hsc-fault ${D}${libexecdir}/${PN}/
     install -d ${D}/${bindir}
     install -m 0755 ${WORKDIR}/configure-nic-mctp-endpoint.sh ${D}/${bindir}/
 }