meta-bletchley: phosphor-nvme: add nvme config script

In Bletchley, we use PCIE_PWR_EN pin as NVME power good pin from each
interposer board. Because PCIE_PWR_EN is form the I/O expander, it
won't have fixed GPIO pin number.

Add script to generate a nvme_config.json with correct GPIO pin number
before nvme management service start.

Tested:

- nvme_config.json generated
```
root@bletchley:~# cat /etc/nvme/nvme_config.json
{
    "config": [
        {
            "NVMeDriveIndex": 1,
            "NVMeDriveBusID": 0,
            "NVMeDrivePwrGoodPin": 779
        },
        {
            "NVMeDriveIndex": 2,
            "NVMeDriveBusID": 1,
            "NVMeDrivePwrGoodPin": 759
        },
        {
            "NVMeDriveIndex": 3,
            "NVMeDriveBusID": 2,
            "NVMeDrivePwrGoodPin": 739
        },
        {
            "NVMeDriveIndex": 4,
            "NVMeDriveBusID": 3,
            "NVMeDrivePwrGoodPin": 719
        },
        {
            "NVMeDriveIndex": 5,
            "NVMeDriveBusID": 4,
            "NVMeDrivePwrGoodPin": 699
        },
        {
            "NVMeDriveIndex": 6,
            "NVMeDriveBusID": 5,
            "NVMeDrivePwrGoodPin": 679
        }
    ],
    "threshold": [
        {
            "criticalHigh": 68,
            "criticalLow": 0,
            "warningHigh": 67,
            "warningLow": 1,
            "maxValue": 127,
            "minValue": -127
        }
    ]
}
```
- nvme sensor available (host on)
```
root@bletchley:~# busctl introspect xyz.openbmc_project.nvme.manager \
< /xyz/openbmc_project/sensors/temperature/nvme6 \
< xyz.openbmc_project.Sensor.Value
NAME                                             TYPE      SIGNATURE RESULT/VALUE                             FLAGS
xyz.openbmc_project.Sensor.Value                 interface -         -                                        -
.MaxValue                                        property  d         127                                      emits-change writable
.MinValue                                        property  d         -127                                     emits-change writable
.Unit                                            property  s         "xyz.openbmc_project.Sensor.Value.Uni... emits-change writable
.Value                                           property  d         36                                       emits-change writable
root@bletchley:~# busctl introspect xyz.openbmc_project.nvme.manager \
> /xyz/openbmc_project/sensors/temperature/nvme6 \
> xyz.openbmc_project.State.Decorator.Availability
NAME                                             TYPE      SIGNATURE RESULT/VALUE FLAGS
.Available                                       property  b         true         emits-change writable
```

- nvme sensor not available (host off)
```
root@bletchley:~# busctl introspect xyz.openbmc_project.nvme.manager \
> /xyz/openbmc_project/sensors/temperature/nvme6 \
> xyz.openbmc_project.Sensor.Value
NAME                             TYPE      SIGNATURE RESULT/VALUE                             FLAGS
.MaxValue                        property  d         127                                      emits-change writable
.MinValue                        property  d         -127                                     emits-change writable
.Unit                            property  s         "xyz.openbmc_project.Sensor.Value.Uni... emits-change writable
.Value                           property  d         -127                                     emits-change writable
root@bletchley:~# busctl introspect xyz.openbmc_project.nvme.manager \
> /xyz/openbmc_project/sensors/temperature/nvme6 \
> xyz.openbmc_project.State.Decorator.Availability
NAME                                             TYPE      SIGNATURE RESULT/VALUE FLAGS
.Available                                       property  b         false        emits-change writable
```

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: I2cda2e5e252fdfdd66ba6f9e0ea006c425cbf43b
diff --git a/meta-facebook/meta-bletchley/recipes-phosphor/sensors/phosphor-nvme/nvme-json-rewrite b/meta-facebook/meta-bletchley/recipes-phosphor/sensors/phosphor-nvme/nvme-json-rewrite
new file mode 100644
index 0000000..d30ed89
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-phosphor/sensors/phosphor-nvme/nvme-json-rewrite
@@ -0,0 +1,72 @@
+#!/bin/bash
+
+TARGET_FILE_NAME="/etc/nvme/nvme_config.json"
+
+export_gpio() {
+  if [ ! -d "/sys/class/gpio/gpio$1" ]; then
+    echo "$1" >/sys/class/gpio/export
+  fi
+}
+
+for i in {0..5}
+do
+    SLED_NVME_PCIE_EN[$i]=0
+    for file in /sys/bus/i2c/drivers/pca953x/"$i"-0041/gpio/gpiochip*/base
+    do
+        if [ -f "$file" ]; then
+            GPIOCHIP_BASE="$(cat /sys/bus/i2c/drivers/pca953x/"$i"-0041/gpio/gpiochip*/base)"
+            SLED_NVME_PCIE_EN[$i]=$((GPIOCHIP_BASE+3))
+            export_gpio ${SLED_NVME_PCIE_EN[$i]}
+            break
+        fi
+    done
+done
+
+cat > $TARGET_FILE_NAME << EOF1
+{
+    "maxSmbusErrorRetry": 3,
+    "config": [
+        {
+            "NVMeDriveIndex": 1,
+            "NVMeDriveBusID": 0,
+            "NVMeDrivePwrGoodPin": ${SLED_NVME_PCIE_EN[0]}
+        },
+        {
+            "NVMeDriveIndex": 2,
+            "NVMeDriveBusID": 1,
+            "NVMeDrivePwrGoodPin": ${SLED_NVME_PCIE_EN[1]}
+        },
+        {
+            "NVMeDriveIndex": 3,
+            "NVMeDriveBusID": 2,
+            "NVMeDrivePwrGoodPin": ${SLED_NVME_PCIE_EN[2]}
+        },
+        {
+            "NVMeDriveIndex": 4,
+            "NVMeDriveBusID": 3,
+            "NVMeDrivePwrGoodPin": ${SLED_NVME_PCIE_EN[3]}
+        },
+        {
+            "NVMeDriveIndex": 5,
+            "NVMeDriveBusID": 4,
+            "NVMeDrivePwrGoodPin": ${SLED_NVME_PCIE_EN[4]}
+        },
+        {
+            "NVMeDriveIndex": 6,
+            "NVMeDriveBusID": 5,
+            "NVMeDrivePwrGoodPin": ${SLED_NVME_PCIE_EN[5]}
+        }
+    ],
+    "threshold": [
+        {
+            "criticalHigh": 68,
+            "criticalLow": 0,
+            "warningHigh": 67,
+            "warningLow": 1,
+            "maxValue": 127,
+            "minValue": -127
+        }
+    ]
+}
+
+EOF1
diff --git a/meta-facebook/meta-bletchley/recipes-phosphor/sensors/phosphor-nvme/nvme-json-rewrite.conf b/meta-facebook/meta-bletchley/recipes-phosphor/sensors/phosphor-nvme/nvme-json-rewrite.conf
new file mode 100644
index 0000000..3e156e5
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-phosphor/sensors/phosphor-nvme/nvme-json-rewrite.conf
@@ -0,0 +1,2 @@
+[Service]
+ExecStartPre=/usr/libexec/phosphor-nvme/nvme-json-rewrite
diff --git a/meta-facebook/meta-bletchley/recipes-phosphor/sensors/phosphor-nvme_%.bbappend b/meta-facebook/meta-bletchley/recipes-phosphor/sensors/phosphor-nvme_%.bbappend
new file mode 100644
index 0000000..d495c8d
--- /dev/null
+++ b/meta-facebook/meta-bletchley/recipes-phosphor/sensors/phosphor-nvme_%.bbappend
@@ -0,0 +1,19 @@
+FILESEXTRAPATHS:prepend := "${THISDIR}/${PN}:"
+
+inherit obmc-phosphor-systemd
+
+SRC_URI:append:bletchley = " \
+    file://nvme-json-rewrite \
+    file://nvme-json-rewrite.conf \
+"
+
+RDEPENDS:${PN}:bletchley += "bash"
+
+do_install:append:bletchley() {
+        install -d ${D}${libexecdir}/${PN}/
+        install -m 0755 ${WORKDIR}/nvme-json-rewrite ${D}${libexecdir}/${PN}/
+}
+
+SYSTEMD_OVERRIDE:${PN}:append:bletchley = " \
+    nvme-json-rewrite.conf:xyz.openbmc_project.nvme.manager.service.d/nvme-json-rewrite.conf \
+"