nvme_manager: decouple config dependency of PresentPin and PwrGoodPin

Nvme::read() does not allow power good checking if config just have
PwrGoodPin only available, for platform which doesn't have both
PresentPin and PwrGoodPin in config will not able to do power checking.

This patch move out GPIO pin reading to begin of each nvme read, and
set defaule value to PresentPin and PwrGoodPin if the pin is not
defined in the config file, so that the power checking can still be
performed without PresentPin defined.

default value of pins:
- PresentPin: IS_PRESENT (0)
- PwrGoodPin: POWERGD (1)

Tested on Bletchley (only PwrGoodPin defined):

- PwrGoodPin is low
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

- PwrGoodPin is high
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         38                                       emits-change writable

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: I00fabf8f387c83bf139539bba9d5ba6aed693778
diff --git a/nvme_manager.cpp b/nvme_manager.cpp
index bdbacd0..cdb76b1 100644
--- a/nvme_manager.cpp
+++ b/nvme_manager.cpp
@@ -607,77 +607,71 @@
         NVMeData nvmeData;
 
         inventoryPath = NVME_INVENTORY_PATH + config.index;
+        devPresentPath =
+            GPIO_BASE_PATH + std::to_string(config.presentPin) + "/value";
+        devPwrGoodPath =
+            GPIO_BASE_PATH + std::to_string(config.pwrGoodPin) + "/value";
 
-        if (config.presentPin)
+        auto presentPinValStr = (config.presentPin)
+                                    ? getGPIOValueOfNvme(devPresentPath)
+                                    : IS_PRESENT;
+        auto pwrGoodPinValStr =
+            (config.pwrGoodPin) ? getGPIOValueOfNvme(devPwrGoodPath) : POWERGD;
+
+        if (presentPinValStr != IS_PRESENT)
         {
-            devPresentPath =
-                GPIO_BASE_PATH + std::to_string(config.presentPin) + "/value";
+            // Drive not present, remove nvme d-bus path ,
+            // clean all properties in inventory
+            // and turn off fault and locate LED
 
-            if (getGPIOValueOfNvme(devPresentPath) != IS_PRESENT)
+            setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath,
+                        false);
+            setLocateLED(config.locateLedGroupPath,
+                         config.locateLedControllerBusName,
+                         config.locateLedControllerPath, false);
+
+            nvmeData = NVMeData();
+            setNvmeInventoryProperties(config, false, nvmeData, inventoryPath);
+            nvmes.erase(config.index);
+            continue;
+        }
+
+        if (pwrGoodPinValStr != POWERGD)
+        {
+            // IFDET should be used to provide the final say
+            // in SSD's presence - IFDET showing SSD is present
+            // but the power is off (if the drive is plugged in)
+            // is a valid state.
+
+            setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath,
+                        true);
+            setLocateLED(config.locateLedGroupPath,
+                         config.locateLedControllerBusName,
+                         config.locateLedControllerPath, false);
+
+            nvmeData = NVMeData();
+            setNvmeInventoryProperties(config, true, nvmeData, inventoryPath);
+
+            if (isErrorPower[config.index] != true)
             {
-                // Drive not present, remove nvme d-bus path ,
-                // clean all properties in inventory
-                // and turn off fault and locate LED
+                log<level::ERR>(
+                    "Present pin is true but power good pin is false.",
+                    entry("INDEX=%s", config.index.c_str()));
+                log<level::ERR>("Erase SSD from map and d-bus.",
+                                entry("INDEX=%s", config.index.c_str()));
 
-                setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath,
-                            false);
-                setLocateLED(config.locateLedGroupPath,
-                             config.locateLedControllerBusName,
-                             config.locateLedControllerPath, false);
-
-                nvmeData = NVMeData();
-                setNvmeInventoryProperties(config, false, nvmeData,
-                                           inventoryPath);
-                nvmes.erase(config.index);
-                continue;
-            }
-            else if (config.pwrGoodPin)
-            {
-                devPwrGoodPath = GPIO_BASE_PATH +
-                                 std::to_string(config.pwrGoodPin) + "/value";
-
-                if (getGPIOValueOfNvme(devPwrGoodPath) != POWERGD)
-                {
-                    // IFDET should be used to provide the final say
-                    // in SSD's presence - IFDET showing SSD is present
-                    // but the power is off (if the drive is plugged in)
-                    // is a valid state.
-
-                    setFaultLED(config.locateLedGroupPath,
-                                config.faultLedGroupPath, true);
-                    setLocateLED(config.locateLedGroupPath,
-                                 config.locateLedControllerBusName,
-                                 config.locateLedControllerPath, false);
-
-                    nvmeData = NVMeData();
-                    setNvmeInventoryProperties(config, true, nvmeData,
-                                               inventoryPath);
-
-                    if (isErrorPower[config.index] != true)
-                    {
-                        log<level::ERR>(
-                            "Present pin is true but power good pin is false.",
-                            entry("INDEX=%s", config.index.c_str()));
-                        log<level::ERR>(
-                            "Erase SSD from map and d-bus.",
-                            entry("INDEX=%s", config.index.c_str()));
-
-                        isErrorPower[config.index] = true;
-                    }
-
-                    // Keep reading to report the invalid temperature
-                    // (To make thermal loop know that the sensor reading
-                    //  is invalid).
-                    readNvmeData(config);
-                    continue;
-                }
+                isErrorPower[config.index] = true;
             }
         }
-        // Drive status is good, update value or create d-bus and update
-        // value.
-        readNvmeData(config);
+        else
+        {
+            isErrorPower[config.index] = false;
+        }
 
-        isErrorPower[config.index] = false;
+        // Keep reading to report the invalid temperature
+        // (To make thermal loop know that the sensor reading
+        //  is invalid).
+        readNvmeData(config);
     }
 }
 } // namespace nvme