nvme_manager: skip smbus reading when power good is false

In order to reduce error log of `SendSmbusRWCmdRAW failed`, skip
nvme data read via smbus when power good is false.

Signed-off-by: Potin Lai <potin.lai@quantatw.com>
Change-Id: Ie33f0597a09f5ef5bd0892c6df07668a3e8b03e1
diff --git a/nvme_manager.cpp b/nvme_manager.cpp
index 54ca992..a70a842 100644
--- a/nvme_manager.cpp
+++ b/nvme_manager.cpp
@@ -550,14 +550,28 @@
     createNVMeInventory();
 }
 
-void Nvme::readNvmeData(NVMeConfig& config)
+void Nvme::readNvmeData(NVMeConfig& config, bool isPwrGood)
 {
     std::string inventoryPath = NVME_INVENTORY_PATH + config.index;
     NVMeData nvmeData;
 
     // get NVMe information through i2c by busID.
-    auto success = getNVMeInfobyBusID(config.busID, nvmeData);
-    auto iter = nvmes.find(config.index);
+    bool success;
+
+    // skip reading nvme data when power good is false
+    if (isPwrGood)
+    {
+        success = getNVMeInfobyBusID(config.busID, nvmeData);
+    }
+    else
+    {
+        nvmeData.present = false;
+        nvmeData.sensorValue = static_cast<int8_t>(TEMPERATURE_SENSOR_FAILURE);
+        success = false;
+        // Skip retry below when isPwrGood is false because smbus is going to
+        // fail
+        nvmeSmbusErrCnt[config.busID] = maxSmbusErrorRetry;
+    }
 
     if (success)
     {
@@ -567,7 +581,7 @@
     {
         if (nvmeSmbusErrCnt[config.busID] < maxSmbusErrorRetry)
         {
-            // skip this time if error count less than maxSmbusErrorRetry
+            // Return early so that we retry
             nvmeSmbusErrCnt[config.busID]++;
             log<level::INFO>("getNVMeInfobyBusID failed, retry...",
                              entry("INDEX=%s", config.index.c_str()),
@@ -576,6 +590,9 @@
         }
     }
 
+    // find NvmeSSD object by index
+    auto iter = nvmes.find(config.index);
+
     // can not find. create dbus
     if (iter == nvmes.end())
     {
@@ -695,7 +712,7 @@
         // Keep reading to report the invalid temperature
         // (To make thermal loop know that the sensor reading
         //  is invalid).
-        readNvmeData(config);
+        readNvmeData(config, isPwrGood);
         if (nvmes.find(config.index) != nvmes.end())
         {
             nvmes.find(config.index)->second->setSensorAvailability(isPwrGood);
diff --git a/nvme_manager.hpp b/nvme_manager.hpp
index 08cfc76..e837961 100644
--- a/nvme_manager.hpp
+++ b/nvme_manager.hpp
@@ -130,7 +130,7 @@
                             phosphor::nvme::Nvme::NVMeData& nvmeData);
 
     /** @brief read and update NVME data to dbus */
-    void readNvmeData(NVMeConfig& config);
+    void readNvmeData(NVMeConfig& config, bool isPwrGood);
 
   private:
     /** @brief sdbusplus bus client connection. */