meta-facebook: yosemite4: Rescan WF BIC and CXL EID

The WF BIC and CXL might not ready when BMC setting up MCTP network.
Add rescan mechanism to make sure it get the EIDs.

Change-Id: I7d57f549a4b7c9441eadab08d4df0d1e4b46e4d1
Signed-off-by: Delphine CC Chiu <Delphine_CC_Chiu@wiwynn.com>
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/rescan-wf-bic b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/rescan-wf-bic
new file mode 100644
index 0000000..fb4b970
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/rescan-wf-bic
@@ -0,0 +1,37 @@
+#!/bin/bash
+
+sd_eid=$(($1 * 10))
+wf_eid=$(($1 * 10 + 2))
+
+# It takes about 6 seconds for setting up MCTP endpoint after slot plugged-in
+sleep 10
+# Check if we got WF BIC's EID
+wf_eid=$(($1 * 10 + 2))
+busctl get-property xyz.openbmc_project.MCTP /xyz/openbmc_project/mctp/1/"$wf_eid" xyz.openbmc_project.MCTP.Endpoint EID
+status=$?
+if [ $status -eq 0 ]; then
+    exit 0
+fi
+
+count=0
+while [ $count -lt 3 ]; do
+    # Check WF BIC status
+    output=$(pldmtool raw -m "$sd_eid" -d 0x80 0x02 0x3A 0x4c 0xff)
+    status=$?
+    if [ $status -ne 0 ]; then
+        echo "Error executing pldmtool command"
+        exit 1
+    fi
+    # Get data at Rx line
+    rx_data=$(echo "$output" | grep "Rx:" | awk '{for (i=3; i<=NF; i++) print $i}')
+    # Get last byte
+    wf_ready=$(echo "$rx_data" | tail -n 1)
+    # 02 means WF BIC ready, otherwise 01.
+    if [ "$wf_ready" == "02" ]; then
+        busctl call xyz.openbmc_project.MCTP /xyz/openbmc_project/mctp au.com.CodeConstruct.MCTP SetupEndpointByConfigPath s /xyz/openbmc_project/inventory/system/board/Yosemite_4_Wailua_Falls_Slot_"$1"/BIC
+        break
+    else
+        count=$((count + 1))
+        sleep 2
+    fi
+done
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/rescan-wf-bic@.service b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/rescan-wf-bic@.service
new file mode 100644
index 0000000..08fb2b1
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/gpio/phosphor-gpio-monitor/rescan-wf-bic@.service
@@ -0,0 +1,7 @@
+[Unit]
+Description=Rescan WF BIC's EID for slot %i
+
+[Service]
+Type=oneshot
+ExecStart=/usr/libexec/phosphor-gpio-monitor/rescan-wf-bic %i
+SyslogIdentifier=rescan-wf-bic%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 925719c..fc7169f 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
@@ -15,7 +15,7 @@
         "GpioNum": 10,
         "EventMon": "BOTH",
         "Targets": {
-            "RISING": ["probe-slot-device@1.service", "rescan-fru-device@1.service"],
+            "RISING": ["probe-slot-device@1.service", "rescan-fru-device@1.service", "rescan-wf-bic@1.service"],
             "FALLING": ["rescan-fru-device@1.service"]
         },
         "Continue": true
@@ -26,7 +26,7 @@
         "GpioNum": 11,
         "EventMon": "BOTH",
         "Targets": {
-            "RISING": ["probe-slot-device@2.service", "rescan-fru-device@2.service"],
+            "RISING": ["probe-slot-device@2.service", "rescan-fru-device@2.service", "rescan-wf-bic@2.service"],
             "FALLING": ["rescan-fru-device@2.service"]
         },
         "Continue": true
@@ -37,7 +37,7 @@
         "GpioNum": 12,
         "EventMon": "BOTH",
         "Targets": {
-            "RISING": ["probe-slot-device@3.service", "rescan-fru-device@3.service"],
+            "RISING": ["probe-slot-device@3.service", "rescan-fru-device@3.service", "rescan-wf-bic@3.service"],
             "FALLING": ["rescan-fru-device@3.service"]
         },
         "Continue": true
@@ -48,7 +48,7 @@
         "GpioNum": 13,
         "EventMon": "BOTH",
         "Targets": {
-            "RISING": ["probe-slot-device@4.service", "rescan-fru-device@4.service"],
+            "RISING": ["probe-slot-device@4.service", "rescan-fru-device@4.service", "rescan-wf-bic@4.service"],
             "FALLING": ["rescan-fru-device@4.service"]
         },
         "Continue": true
@@ -59,7 +59,7 @@
         "GpioNum": 190,
         "EventMon": "BOTH",
         "Targets": {
-            "RISING": ["probe-slot-device@5.service", "rescan-fru-device@5.service"],
+            "RISING": ["probe-slot-device@5.service", "rescan-fru-device@5.service", "rescan-wf-bic@5.service"],
             "FALLING": ["rescan-fru-device@5.service"]
         },
         "Continue": true
@@ -70,7 +70,7 @@
         "GpioNum": 191,
         "EventMon": "BOTH",
         "Targets": {
-            "RISING": ["probe-slot-device@6.service", "rescan-fru-device@6.service"],
+            "RISING": ["probe-slot-device@6.service", "rescan-fru-device@6.service", "rescan-wf-bic@6.service"],
             "FALLING": ["rescan-fru-device@6.service"]
         },
         "Continue": true
@@ -81,7 +81,7 @@
         "GpioNum": 195,
         "EventMon": "BOTH",
         "Targets": {
-            "RISING": ["probe-slot-device@7.service", "rescan-fru-device@7.service"],
+            "RISING": ["probe-slot-device@7.service", "rescan-fru-device@7.service", "rescan-wf-bic@7.service"],
             "FALLING": ["rescan-fru-device@7.service"]
         },
         "Continue": true
@@ -92,7 +92,7 @@
         "GpioNum": 202,
         "EventMon": "BOTH",
         "Targets": {
-            "RISING": ["probe-slot-device@8.service", "rescan-fru-device@8.service"],
+            "RISING": ["probe-slot-device@8.service", "rescan-fru-device@8.service", "rescan-wf-bic@8.service"],
             "FALLING": ["rescan-fru-device@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 9033632..14057f6 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
@@ -12,6 +12,8 @@
             file://rescan-fru-device@.service \
             file://rescan-fru-device \
             file://slot-hot-plug@.service \
+            file://rescan-wf-bic \
+            file://rescan-wf-bic@.service \
             "
 
 RDEPENDS:${PN}:append = " bash"
@@ -25,6 +27,7 @@
     slot-hot-plug@.service \
     setup-nic-endpoint-slot@.service \
     remove-nic-endpoint-slot@.service \
+    rescan-wf-bic@.service \
     "
 
 SYSTEMD_AUTO_ENABLE = "enable"
@@ -39,9 +42,11 @@
     install -m 0644 ${WORKDIR}/slot-hot-plug@.service ${D}${systemd_system_unitdir}/slot-hot-plug@.service
     install -m 0644 ${WORKDIR}/setup-nic-endpoint-slot@.service ${D}${systemd_system_unitdir}/
     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 -d ${D}${libexecdir}/${PN}
     install -m 0755 ${WORKDIR}/probe-slot-device ${D}${libexecdir}/${PN}/
     install -m 0755 ${WORKDIR}/rescan-fru-device ${D}${libexecdir}/${PN}/
+    install -m 0755 ${WORKDIR}/rescan-wf-bic ${D}${libexecdir}/${PN}/
     install -d ${D}/${bindir}
     install -m 0755 ${WORKDIR}/configure-nic-mctp-endpoint.sh ${D}/${bindir}/
 }
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/host-poweron@.service b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/host-poweron@.service
index 6f59a6b..5a09fdc 100644
--- a/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/host-poweron@.service
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/host-poweron@.service
@@ -4,6 +4,7 @@
 [Service]
 Type=oneshot
 ExecStart=/usr/libexec/phosphor-state-manager/host-poweron %i
+ExecStartPost=/usr/libexec/phosphor-state-manager/rescan-cxl-eid %i
 
 [Install]
 WantedBy=obmc-host-start@%i.target
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/rescan-cxl-eid b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/rescan-cxl-eid
new file mode 100644
index 0000000..4775ac8
--- /dev/null
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager/rescan-cxl-eid
@@ -0,0 +1,33 @@
+#!/bin/bash
+
+# CXL need about 30 seconds to boot up. Wait 30 seconds then start polling CXLs status.
+sleep 30
+
+wf_eid=$(($1 * 10 + 2))
+count=0
+while [ $count -lt 3 ]; do
+
+    output=$(pldmtool raw -m "$wf_eid" -d 0x80 0x02 0x3A 0x04 0x01)
+
+    rx_line=$(echo "$output" | grep "Rx:")
+    rx_data=$(echo "$rx_line" | awk '{print $4,$5,$6,$7,$8,$9,$10,$11,$12,$13}')
+
+    cxl1_ready=$(echo "$rx_data" | awk '{print substr($7,1,2)}')
+    cxl2_ready=$(echo "$rx_data" | awk '{print substr($10,1,2)}')
+
+    if [ "$cxl1_ready" == "01" ]; then
+        busctl call xyz.openbmc_project.MCTP /xyz/openbmc_project/mctp au.com.CodeConstruct.MCTP SetupEndpointByConfigPath s /xyz/openbmc_project/inventory/system/board/Yosemite_4_Wailua_Falls_Slot_"$1"/CXL1
+    fi
+
+    if [ "$cxl2_ready" == "01" ]; then
+        busctl call xyz.openbmc_project.MCTP /xyz/openbmc_project/mctp au.com.CodeConstruct.MCTP SetupEndpointByConfigPath s /xyz/openbmc_project/inventory/system/board/Yosemite_4_Wailua_Falls_Slot_"$1"/CXL2
+    fi
+
+    if [ "$cxl1_ready" == "01" ] && [ "$cxl2_ready" == "01" ]; then
+        break
+    fi
+
+    count=$((count + 1))
+    sleep 2
+
+done
diff --git a/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager_%.bbappend b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager_%.bbappend
index b0ba7fd..a3f032c 100644
--- a/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager_%.bbappend
+++ b/meta-facebook/meta-yosemite4/recipes-phosphor/state/phosphor-state-manager_%.bbappend
@@ -43,6 +43,7 @@
     file://host-powerreset \
     file://power-cmd \
     file://wait-until-mctp-connection-done \
+    file://rescan-cxl-eid \
     "
 
 RDEPENDS:${PN}:append = " bash"
@@ -61,6 +62,7 @@
     install -m 0755 ${WORKDIR}/host-powerreset ${D}${libexecdir}/${PN}/
     install -m 0755 ${WORKDIR}/power-cmd ${D}${libexecdir}/${PN}/
     install -m 0755 ${WORKDIR}/wait-until-mctp-connection-done ${D}${libexecdir}/${PN}/
+    install -m 0755 ${WORKDIR}/rescan-cxl-eid ${D}${libexecdir}/${PN}/
 }
 
 FILES:${PN} += " ${systemd_system_unitdir}/*.service"