meta-quanta: gsj: Detect dual rotor fan fail

While rpm of dual rotor are lower than critical threshold
of fan, the script would stop phosphor-pid-control.service
and set other rotors pwm to 255 until fail rotor recover,
which test on gsj board pass.

(From meta-quanta rev: 94311c532fef9231cd71f9da0a2817f6d6575e7a)

Signed-off-by: Duke Du <Duke.Du@quantatw.com>
Change-Id: I1ed670a48b2de28dda6da2fdee2ea0ba2bc1802a
Signed-off-by: Brad Bishop <bradleyb@fuzziesquirrel.com>
diff --git a/meta-quanta/meta-gsj/recipes-gsj/detect-fan-fail/detect-fan-fail.bb b/meta-quanta/meta-gsj/recipes-gsj/detect-fan-fail/detect-fan-fail.bb
new file mode 100644
index 0000000..86303c3
--- /dev/null
+++ b/meta-quanta/meta-gsj/recipes-gsj/detect-fan-fail/detect-fan-fail.bb
@@ -0,0 +1,26 @@
+SUMMARY = "OpenBMC Quanta Detect Fan Fail Service"
+DESCRIPTION = "OpenBMC Quanta Detect Fan Fail Daemon."
+PR = "r1"
+LICENSE = "Apache-2.0"
+LIC_FILES_CHKSUM = "file://${QUANTABASE}/COPYING.apache-2.0;md5=34400b68072d710fecd0a2940a0d1658"
+
+inherit systemd
+
+DEPENDS += "systemd"
+RDEPENDS_${PN} += "bash"
+
+FILESEXTRAPATHS_append_gsj := "${THISDIR}/files:"
+SRC_URI_append_gsj =  " file://detect-fan-fail.sh \
+                        file://detect-fan-fail.service \
+                      "
+
+do_install_append_gsj() {
+    install -d ${D}${bindir}
+    install -m 0755 ${WORKDIR}/detect-fan-fail.sh ${D}${bindir}/
+
+    install -d ${D}${systemd_unitdir}/system/
+    install -m 0644 ${WORKDIR}/detect-fan-fail.service ${D}${systemd_unitdir}/system
+}
+
+SYSTEMD_PACKAGES = "${PN}"
+SYSTEMD_SERVICE_${PN} = "detect-fan-fail.service"
diff --git a/meta-quanta/meta-gsj/recipes-gsj/detect-fan-fail/files/detect-fan-fail.service b/meta-quanta/meta-gsj/recipes-gsj/detect-fan-fail/files/detect-fan-fail.service
new file mode 100644
index 0000000..0ccafe3
--- /dev/null
+++ b/meta-quanta/meta-gsj/recipes-gsj/detect-fan-fail/files/detect-fan-fail.service
@@ -0,0 +1,11 @@
+[Unit]
+Description=Detect Fan Fail Manager
+After=phosphor-pid-control.service
+
+[Service]
+ExecStart=/usr/bin/detect-fan-fail.sh
+Restart=always
+StandardOutput=syslog
+
+[Install]
+WantedBy=multi-user.target
diff --git a/meta-quanta/meta-gsj/recipes-gsj/detect-fan-fail/files/detect-fan-fail.sh b/meta-quanta/meta-gsj/recipes-gsj/detect-fan-fail/files/detect-fan-fail.sh
new file mode 100644
index 0000000..80f1f69
--- /dev/null
+++ b/meta-quanta/meta-gsj/recipes-gsj/detect-fan-fail/files/detect-fan-fail.sh
@@ -0,0 +1,58 @@
+#!/bin/bash
+
+# get fan state by d-bus command
+function get_fan_state() {
+    get_property_path='xyz.openbmc_project.Sensor.Threshold.Critical CriticalAlarmLow'
+    fan_state="$(busctl get-property $1 $2 $get_property_path | awk '{print $2}')"
+    echo "$fan_state"
+}
+
+# set fan pwm by d-bus command
+function set_fan_value() {
+    set_property_path='xyz.openbmc_project.Control.FanPwm'
+    busctl set-property $1 $2 $set_property_path Target t 255
+}
+
+fan_tach_path=( '/xyz/openbmc_project/sensors/fan_tach/Fan0_0_RPM'
+                '/xyz/openbmc_project/sensors/fan_tach/Fan0_1_RPM'
+                '/xyz/openbmc_project/sensors/fan_tach/Fan1_0_RPM'
+                '/xyz/openbmc_project/sensors/fan_tach/Fan1_1_RPM'
+                '/xyz/openbmc_project/sensors/fan_tach/Fan2_0_RPM'
+                '/xyz/openbmc_project/sensors/fan_tach/Fan2_1_RPM'
+                )
+
+check_fail_flag=0
+current_fan_state=()
+while true; do
+    hwmon_path="$(mapper get-service ${fan_tach_path[0]})"
+    for i in ${!fan_tach_path[@]};
+    do
+        current_fan_state[$i%2]=$(get_fan_state $hwmon_path ${fan_tach_path[$i]})
+
+        #Compare state of dual rotors with CriticalAlarmLow to check if fan fail
+        if [ ${#current_fan_state[@]} -eq 2 ];then
+            if [ "${current_fan_state[0]}" == "true" ] && \
+            [ "${current_fan_state[1]}" == "true" ] ;then
+                if [ $check_fail_flag -eq 0 ];then
+                    systemctl stop phosphor-pid-control
+                    for j in ${!fan_tach_path[@]};
+                    do
+                        #If fan fail is detect, set other fan rpm to pwm 255.
+                        set_fan_value $hwmon_path ${fan_tach_path[$j]}
+                        check_fail_flag=1
+                    done
+                fi
+                current_fan_state=()
+                break
+            fi
+            current_fan_state=()
+        fi
+
+        #fans are going to normal.
+        if [ $i -eq $((${#fan_tach_path[@]}-1)) ] && [ $check_fail_flag -eq 1 ]; then
+           check_fail_flag=0
+           systemctl restart phosphor-pid-control
+        fi
+    done
+    sleep 2
+done