Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 1 | #include "nvme_manager.hpp" |
| 2 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 3 | #include "smbus.hpp" |
| 4 | |
| 5 | #include <filesystem> |
| 6 | #include <map> |
| 7 | #include <nlohmann/json.hpp> |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 8 | #include <phosphor-logging/elog-errors.hpp> |
| 9 | #include <phosphor-logging/log.hpp> |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 10 | #include <sdbusplus/message.hpp> |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 11 | #include <sstream> |
| 12 | #include <string> |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Led/Physical/server.hpp> |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 14 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 15 | #include "i2c.h" |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 16 | #define MONITOR_INTERVAL_SECONDS 1 |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 17 | #define NVME_SSD_SLAVE_ADDRESS 0x6a |
George Hung | 92a15ba | 2020-09-14 17:14:52 +0800 | [diff] [blame^] | 18 | #define NVME_SSD_VPD_SLAVE_ADDRESS 0x53 |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 19 | #define GPIO_BASE_PATH "/sys/class/gpio/gpio" |
| 20 | #define IS_PRESENT "0" |
| 21 | #define POWERGD "1" |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 22 | #define NOWARNING_STRING "ff" |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 23 | |
| 24 | static constexpr auto configFile = "/etc/nvme/nvme_config.json"; |
| 25 | static constexpr auto delay = std::chrono::milliseconds{100}; |
| 26 | using Json = nlohmann::json; |
| 27 | |
| 28 | static constexpr const uint8_t COMMAND_CODE_0 = 0; |
| 29 | static constexpr const uint8_t COMMAND_CODE_8 = 8; |
George Hung | 92a15ba | 2020-09-14 17:14:52 +0800 | [diff] [blame^] | 30 | static constexpr const uint8_t CODE_0_LENGTH = 8; |
| 31 | static constexpr const uint8_t CODE_8_LENGTH = 23; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 32 | |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 33 | static constexpr int CapacityFaultMask = 1; |
| 34 | static constexpr int temperatureFaultMask = 1 << 1; |
| 35 | static constexpr int DegradesFaultMask = 1 << 2; |
| 36 | static constexpr int MediaFaultMask = 1 << 3; |
| 37 | static constexpr int BackupDeviceFaultMask = 1 << 4; |
| 38 | static constexpr int NOWARNING = 255; |
| 39 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 40 | static constexpr int SERIALNUMBER_START_INDEX = 3; |
| 41 | static constexpr int SERIALNUMBER_END_INDEX = 23; |
George Hung | 92a15ba | 2020-09-14 17:14:52 +0800 | [diff] [blame^] | 42 | static constexpr int MODELNUMBER_START_INDEX = 46; |
| 43 | static constexpr int MODELNUMBER_END_INDEX = 85; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 44 | |
| 45 | static constexpr const int TEMPERATURE_SENSOR_FAILURE = 0x81; |
| 46 | |
George Hung | 69b9618 | 2020-08-20 17:19:56 +0800 | [diff] [blame] | 47 | static std::map<std::string, std::string> map_vendor = {{"80 86", "Intel"}, |
| 48 | {"14 4d", "Samsung"}}; |
| 49 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 50 | namespace fs = std::filesystem; |
| 51 | |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 52 | namespace phosphor |
| 53 | { |
| 54 | namespace nvme |
| 55 | { |
| 56 | |
| 57 | using namespace std; |
| 58 | using namespace phosphor::logging; |
| 59 | |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 60 | void Nvme::setNvmeInventoryProperties( |
| 61 | bool present, const phosphor::nvme::Nvme::NVMeData& nvmeData, |
| 62 | const std::string& inventoryPath) |
| 63 | { |
| 64 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
| 65 | ITEM_IFACE, "Present", present); |
| 66 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
| 67 | ASSET_IFACE, "Manufacturer", nvmeData.vendor); |
| 68 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
| 69 | ASSET_IFACE, "SerialNumber", |
| 70 | nvmeData.serialNumber); |
| 71 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
George Hung | 92a15ba | 2020-09-14 17:14:52 +0800 | [diff] [blame^] | 72 | ASSET_IFACE, "Model", nvmeData.modelNumber); |
| 73 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 74 | NVME_STATUS_IFACE, "SmartWarnings", |
| 75 | nvmeData.smartWarnings); |
| 76 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
| 77 | NVME_STATUS_IFACE, "StatusFlags", |
| 78 | nvmeData.statusFlags); |
| 79 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
| 80 | NVME_STATUS_IFACE, "DriveLifeUsed", |
| 81 | nvmeData.driveLifeUsed); |
| 82 | |
| 83 | auto smartWarning = (!nvmeData.smartWarnings.empty()) |
| 84 | ? std::stoi(nvmeData.smartWarnings, 0, 16) |
| 85 | : NOWARNING; |
| 86 | |
| 87 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
| 88 | NVME_STATUS_IFACE, "CapacityFault", |
| 89 | !(smartWarning & CapacityFaultMask)); |
| 90 | |
| 91 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
| 92 | NVME_STATUS_IFACE, "TemperatureFault", |
| 93 | !(smartWarning & temperatureFaultMask)); |
| 94 | |
| 95 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
| 96 | NVME_STATUS_IFACE, "DegradesFault", |
| 97 | !(smartWarning & DegradesFaultMask)); |
| 98 | |
| 99 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
| 100 | NVME_STATUS_IFACE, "MediaFault", |
| 101 | !(smartWarning & MediaFaultMask)); |
| 102 | |
| 103 | util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath, |
| 104 | NVME_STATUS_IFACE, "BackupDeviceFault", |
| 105 | !(smartWarning & BackupDeviceFaultMask)); |
| 106 | } |
| 107 | |
| 108 | void Nvme::setFaultLED(const std::string& locateLedGroupPath, |
| 109 | const std::string& faultLedGroupPath, bool request) |
| 110 | { |
| 111 | if (locateLedGroupPath.empty() || faultLedGroupPath.empty()) |
| 112 | { |
| 113 | return; |
| 114 | } |
| 115 | |
| 116 | // Before toggle LED, check whether is Identify or not. |
| 117 | if (!getLEDGroupState(locateLedGroupPath)) |
| 118 | { |
| 119 | util::SDBusPlus::setProperty(bus, LED_GROUP_BUSNAME, faultLedGroupPath, |
| 120 | LED_GROUP_IFACE, "Asserted", request); |
| 121 | } |
| 122 | } |
| 123 | |
| 124 | void Nvme::setLocateLED(const std::string& locateLedGroupPath, |
| 125 | const std::string& locateLedBusName, |
| 126 | const std::string& locateLedPath, bool isPresent) |
| 127 | { |
| 128 | if (locateLedGroupPath.empty() || locateLedBusName.empty() || |
| 129 | locateLedPath.empty()) |
| 130 | { |
| 131 | return; |
| 132 | } |
| 133 | |
| 134 | namespace server = sdbusplus::xyz::openbmc_project::Led::server; |
| 135 | |
| 136 | if (!getLEDGroupState(locateLedGroupPath)) |
| 137 | { |
| 138 | if (isPresent) |
| 139 | util::SDBusPlus::setProperty( |
| 140 | bus, locateLedBusName, locateLedPath, LED_CONTROLLER_IFACE, |
| 141 | "State", |
| 142 | server::convertForMessage(server::Physical::Action::On)); |
| 143 | else |
| 144 | util::SDBusPlus::setProperty( |
| 145 | bus, locateLedBusName, locateLedPath, LED_CONTROLLER_IFACE, |
| 146 | "State", |
| 147 | server::convertForMessage(server::Physical::Action::Off)); |
| 148 | } |
| 149 | } |
| 150 | |
| 151 | bool Nvme::getLEDGroupState(const std::string& ledPath) |
| 152 | { |
| 153 | auto asserted = util::SDBusPlus::getProperty<bool>( |
| 154 | bus, LED_GROUP_BUSNAME, ledPath, LED_GROUP_IFACE, "Asserted"); |
| 155 | |
| 156 | return asserted; |
| 157 | } |
| 158 | |
| 159 | void Nvme::setLEDsStatus(const phosphor::nvme::Nvme::NVMeConfig& config, |
| 160 | bool success, |
| 161 | const phosphor::nvme::Nvme::NVMeData& nvmeData) |
| 162 | { |
| 163 | static std::unordered_map<std::string, bool> isError; |
| 164 | |
| 165 | if (success) |
| 166 | { |
| 167 | if (!nvmeData.smartWarnings.empty()) |
| 168 | { |
| 169 | auto request = |
| 170 | (strcmp(nvmeData.smartWarnings.c_str(), NOWARNING_STRING) == 0) |
| 171 | ? false |
| 172 | : true; |
| 173 | |
| 174 | setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath, |
| 175 | request); |
| 176 | setLocateLED(config.locateLedGroupPath, |
| 177 | config.locateLedControllerBusName, |
| 178 | config.locateLedControllerPath, !request); |
| 179 | } |
| 180 | isError[config.index] = false; |
| 181 | } |
| 182 | else |
| 183 | { |
| 184 | if (isError[config.index] != true) |
| 185 | { |
| 186 | // Drive is present but can not get data, turn on fault LED. |
| 187 | log<level::ERR>("Drive status is good but can not get data.", |
| 188 | entry("objPath = %s", config.index.c_str())); |
| 189 | isError[config.index] = true; |
| 190 | } |
| 191 | |
| 192 | setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath, true); |
| 193 | setLocateLED(config.locateLedGroupPath, |
| 194 | config.locateLedControllerBusName, |
| 195 | config.locateLedControllerPath, false); |
| 196 | } |
| 197 | } |
| 198 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 199 | std::string intToHex(int input) |
| 200 | { |
| 201 | std::stringstream tmp; |
| 202 | tmp << std::hex << input; |
| 203 | |
| 204 | return tmp.str(); |
| 205 | } |
| 206 | |
| 207 | /** @brief Get NVMe info over smbus */ |
| 208 | bool getNVMeInfobyBusID(int busID, phosphor::nvme::Nvme::NVMeData& nvmeData) |
| 209 | { |
| 210 | nvmeData.present = true; |
| 211 | nvmeData.vendor = ""; |
| 212 | nvmeData.serialNumber = ""; |
| 213 | nvmeData.smartWarnings = ""; |
| 214 | nvmeData.statusFlags = ""; |
| 215 | nvmeData.driveLifeUsed = ""; |
| 216 | nvmeData.sensorValue = (int8_t)TEMPERATURE_SENSOR_FAILURE; |
| 217 | |
| 218 | phosphor::smbus::Smbus smbus; |
| 219 | |
| 220 | unsigned char rsp_data_command_0[I2C_DATA_MAX] = {0}; |
| 221 | unsigned char rsp_data_command_8[I2C_DATA_MAX] = {0}; |
| 222 | |
| 223 | uint8_t tx_data = COMMAND_CODE_0; |
| 224 | |
| 225 | auto init = smbus.smbusInit(busID); |
| 226 | |
| 227 | static std::unordered_map<int, bool> isErrorSmbus; |
| 228 | |
| 229 | if (init == -1) |
| 230 | { |
| 231 | if (isErrorSmbus[busID] != true) |
| 232 | { |
| 233 | log<level::ERR>("smbusInit fail!"); |
| 234 | isErrorSmbus[busID] = true; |
| 235 | } |
| 236 | |
| 237 | nvmeData.present = false; |
| 238 | |
| 239 | return nvmeData.present; |
| 240 | } |
| 241 | |
George Hung | 92a15ba | 2020-09-14 17:14:52 +0800 | [diff] [blame^] | 242 | auto res_int = smbus.SendSmbusRWCmdRAW(busID, NVME_SSD_SLAVE_ADDRESS, |
| 243 | &tx_data, sizeof(tx_data), |
| 244 | rsp_data_command_0, CODE_0_LENGTH); |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 245 | |
| 246 | if (res_int < 0) |
| 247 | { |
| 248 | if (isErrorSmbus[busID] != true) |
| 249 | { |
| 250 | log<level::ERR>("Send command code 0 fail!"); |
| 251 | isErrorSmbus[busID] = true; |
| 252 | } |
| 253 | |
| 254 | smbus.smbusClose(busID); |
| 255 | nvmeData.present = false; |
| 256 | return nvmeData.present; |
| 257 | } |
| 258 | |
George Hung | 92a15ba | 2020-09-14 17:14:52 +0800 | [diff] [blame^] | 259 | nvmeData.statusFlags = intToHex(rsp_data_command_0[1]); |
| 260 | nvmeData.smartWarnings = intToHex(rsp_data_command_0[2]); |
| 261 | nvmeData.driveLifeUsed = intToHex(rsp_data_command_0[4]); |
| 262 | nvmeData.sensorValue = (int8_t)rsp_data_command_0[3]; |
| 263 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 264 | tx_data = COMMAND_CODE_8; |
| 265 | |
George Hung | 92a15ba | 2020-09-14 17:14:52 +0800 | [diff] [blame^] | 266 | res_int = smbus.SendSmbusRWCmdRAW(busID, NVME_SSD_SLAVE_ADDRESS, &tx_data, |
| 267 | sizeof(tx_data), rsp_data_command_8, |
| 268 | CODE_8_LENGTH); |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 269 | |
| 270 | if (res_int < 0) |
| 271 | { |
| 272 | if (isErrorSmbus[busID] != true) |
| 273 | { |
| 274 | log<level::ERR>("Send command code 8 fail!"); |
| 275 | isErrorSmbus[busID] = true; |
| 276 | } |
| 277 | |
| 278 | smbus.smbusClose(busID); |
| 279 | nvmeData.present = false; |
| 280 | return nvmeData.present; |
| 281 | } |
| 282 | |
| 283 | nvmeData.vendor = |
| 284 | intToHex(rsp_data_command_8[1]) + " " + intToHex(rsp_data_command_8[2]); |
| 285 | |
George Hung | 69b9618 | 2020-08-20 17:19:56 +0800 | [diff] [blame] | 286 | for (auto iter = map_vendor.begin(); iter != map_vendor.end(); iter++) |
| 287 | { |
| 288 | if (iter->first == nvmeData.vendor) |
| 289 | { |
| 290 | nvmeData.vendor = iter->second; |
| 291 | break; |
| 292 | } |
| 293 | } |
| 294 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 295 | for (int offset = SERIALNUMBER_START_INDEX; offset < SERIALNUMBER_END_INDEX; |
| 296 | offset++) |
| 297 | { |
George Hung | 69b9618 | 2020-08-20 17:19:56 +0800 | [diff] [blame] | 298 | if (rsp_data_command_8[offset] != ' ') |
| 299 | nvmeData.serialNumber += |
| 300 | static_cast<char>(rsp_data_command_8[offset]); |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 301 | } |
| 302 | |
George Hung | 92a15ba | 2020-09-14 17:14:52 +0800 | [diff] [blame^] | 303 | if (nvmeData.vendor == "Samsung") |
| 304 | { |
| 305 | unsigned char rsp_data_vpd[I2C_DATA_MAX] = {0}; |
| 306 | const int rx_len = (MODELNUMBER_END_INDEX - MODELNUMBER_START_INDEX); |
| 307 | tx_data = MODELNUMBER_START_INDEX; |
| 308 | |
| 309 | auto res_int = |
| 310 | smbus.SendSmbusRWCmdRAW(busID, NVME_SSD_VPD_SLAVE_ADDRESS, &tx_data, |
| 311 | sizeof(tx_data), rsp_data_vpd, rx_len); |
| 312 | |
| 313 | if (res_int < 0) |
| 314 | { |
| 315 | if (isErrorSmbus[busID] != true) |
| 316 | { |
| 317 | log<level::ERR>("Send command read VPD fail!"); |
| 318 | isErrorSmbus[busID] = true; |
| 319 | } |
| 320 | |
| 321 | smbus.smbusClose(busID); |
| 322 | nvmeData.present = false; |
| 323 | return nvmeData.present; |
| 324 | } |
| 325 | |
| 326 | for (int i = 0; i < rx_len; i++) |
| 327 | { |
| 328 | if (rsp_data_vpd[i] != ' ') |
| 329 | nvmeData.modelNumber += static_cast<char>(rsp_data_vpd[i]); |
| 330 | } |
| 331 | |
| 332 | if (nvmeData.modelNumber.substr(0, nvmeData.vendor.size()) == "SAMSUNG") |
| 333 | nvmeData.modelNumber.erase(0, nvmeData.vendor.size()); |
| 334 | } |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 335 | |
| 336 | smbus.smbusClose(busID); |
| 337 | |
| 338 | isErrorSmbus[busID] = false; |
| 339 | |
| 340 | return nvmeData.present; |
| 341 | } |
| 342 | |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 343 | void Nvme::run() |
| 344 | { |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 345 | init(); |
| 346 | |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 347 | std::function<void()> callback(std::bind(&Nvme::read, this)); |
| 348 | try |
| 349 | { |
| 350 | u_int64_t interval = MONITOR_INTERVAL_SECONDS * 1000000; |
| 351 | _timer.restart(std::chrono::microseconds(interval)); |
| 352 | } |
| 353 | catch (const std::exception& e) |
| 354 | { |
| 355 | log<level::ERR>("Error in polling loop. "), |
| 356 | entry("ERROR = %s", e.what()); |
| 357 | } |
| 358 | } |
| 359 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 360 | /** @brief Parsing NVMe config JSON file */ |
| 361 | Json parseSensorConfig() |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 362 | { |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 363 | std::ifstream jsonFile(configFile); |
| 364 | if (!jsonFile.is_open()) |
| 365 | { |
| 366 | log<level::ERR>("NVMe config JSON file not found"); |
| 367 | } |
| 368 | |
| 369 | auto data = Json::parse(jsonFile, nullptr, false); |
| 370 | if (data.is_discarded()) |
| 371 | { |
| 372 | log<level::ERR>("NVMe config readings JSON parser failure"); |
| 373 | } |
| 374 | |
| 375 | return data; |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 376 | } |
| 377 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 378 | /** @brief Obtain the initial configuration value of NVMe */ |
| 379 | std::vector<phosphor::nvme::Nvme::NVMeConfig> Nvme::getNvmeConfig() |
| 380 | { |
| 381 | |
| 382 | phosphor::nvme::Nvme::NVMeConfig nvmeConfig; |
| 383 | std::vector<phosphor::nvme::Nvme::NVMeConfig> nvmeConfigs; |
| 384 | int8_t criticalHigh = 0; |
| 385 | int8_t criticalLow = 0; |
| 386 | int8_t maxValue = 0; |
| 387 | int8_t minValue = 0; |
| 388 | int8_t warningHigh = 0; |
| 389 | int8_t warningLow = 0; |
| 390 | |
| 391 | try |
| 392 | { |
| 393 | auto data = parseSensorConfig(); |
| 394 | static const std::vector<Json> empty{}; |
| 395 | std::vector<Json> readings = data.value("config", empty); |
| 396 | std::vector<Json> thresholds = data.value("threshold", empty); |
| 397 | if (!thresholds.empty()) |
| 398 | { |
| 399 | for (const auto& instance : thresholds) |
| 400 | { |
| 401 | criticalHigh = instance.value("criticalHigh", 0); |
| 402 | criticalLow = instance.value("criticalLow", 0); |
| 403 | maxValue = instance.value("maxValue", 0); |
| 404 | minValue = instance.value("minValue", 0); |
| 405 | warningHigh = instance.value("warningHigh", 0); |
| 406 | warningLow = instance.value("warningLow", 0); |
| 407 | } |
| 408 | } |
| 409 | else |
| 410 | { |
| 411 | log<level::ERR>( |
| 412 | "Invalid NVMe config file, thresholds dosen't exist"); |
| 413 | } |
| 414 | |
| 415 | if (!readings.empty()) |
| 416 | { |
| 417 | for (const auto& instance : readings) |
| 418 | { |
| 419 | uint8_t index = instance.value("NVMeDriveIndex", 0); |
| 420 | uint8_t busID = instance.value("NVMeDriveBusID", 0); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 421 | std::string faultLedGroupPath = |
| 422 | instance.value("NVMeDriveFaultLEDGroupPath", ""); |
| 423 | std::string locateLedGroupPath = |
| 424 | instance.value("NVMeDriveLocateLEDGroupPath", ""); |
George Hung | 873b5b3 | 2020-06-20 14:56:15 +0800 | [diff] [blame] | 425 | uint16_t presentPin = instance.value("NVMeDrivePresentPin", 0); |
| 426 | uint16_t pwrGoodPin = instance.value("NVMeDrivePwrGoodPin", 0); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 427 | std::string locateLedControllerBusName = |
| 428 | instance.value("NVMeDriveLocateLEDControllerBusName", ""); |
| 429 | std::string locateLedControllerPath = |
| 430 | instance.value("NVMeDriveLocateLEDControllerPath", ""); |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 431 | |
| 432 | nvmeConfig.index = std::to_string(index); |
| 433 | nvmeConfig.busID = busID; |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 434 | nvmeConfig.faultLedGroupPath = faultLedGroupPath; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 435 | nvmeConfig.presentPin = presentPin; |
| 436 | nvmeConfig.pwrGoodPin = pwrGoodPin; |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 437 | nvmeConfig.locateLedControllerBusName = |
| 438 | locateLedControllerBusName; |
| 439 | nvmeConfig.locateLedControllerPath = locateLedControllerPath; |
| 440 | nvmeConfig.locateLedGroupPath = locateLedGroupPath; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 441 | nvmeConfig.criticalHigh = criticalHigh; |
| 442 | nvmeConfig.criticalLow = criticalLow; |
| 443 | nvmeConfig.warningHigh = warningHigh; |
| 444 | nvmeConfig.warningLow = warningLow; |
| 445 | nvmeConfig.maxValue = maxValue; |
| 446 | nvmeConfig.minValue = minValue; |
| 447 | nvmeConfigs.push_back(nvmeConfig); |
| 448 | } |
| 449 | } |
| 450 | else |
| 451 | { |
| 452 | log<level::ERR>("Invalid NVMe config file, config dosen't exist"); |
| 453 | } |
| 454 | } |
| 455 | catch (const Json::exception& e) |
| 456 | { |
| 457 | log<level::ERR>("Json Exception caught."), entry("MSG: %s", e.what()); |
| 458 | } |
| 459 | |
| 460 | return nvmeConfigs; |
| 461 | } |
| 462 | |
| 463 | std::string Nvme::getGPIOValueOfNvme(const std::string& fullPath) |
| 464 | { |
| 465 | std::string val; |
| 466 | std::ifstream ifs; |
| 467 | auto retries = 3; |
| 468 | |
| 469 | while (retries != 0) |
| 470 | { |
| 471 | try |
| 472 | { |
| 473 | if (!ifs.is_open()) |
| 474 | ifs.open(fullPath); |
| 475 | ifs.clear(); |
| 476 | ifs.seekg(0); |
| 477 | ifs >> val; |
| 478 | } |
| 479 | catch (const std::exception& e) |
| 480 | { |
| 481 | --retries; |
| 482 | std::this_thread::sleep_for(delay); |
| 483 | log<level::ERR>("Can not open gpio path.", |
| 484 | entry("MSG: %s", e.what())); |
| 485 | continue; |
| 486 | } |
| 487 | break; |
| 488 | } |
| 489 | |
| 490 | ifs.close(); |
| 491 | return val; |
| 492 | } |
| 493 | |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 494 | void Nvme::createNVMeInventory() |
| 495 | { |
Patrick Williams | 05eedaa | 2020-05-13 17:58:42 -0500 | [diff] [blame] | 496 | using Properties = std::map<std::string, std::variant<std::string, bool>>; |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 497 | using Interfaces = std::map<std::string, Properties>; |
| 498 | |
| 499 | std::string inventoryPath; |
| 500 | std::map<sdbusplus::message::object_path, Interfaces> obj; |
| 501 | |
| 502 | for (const auto config : configs) |
| 503 | { |
| 504 | inventoryPath = "/system/chassis/motherboard/nvme" + config.index; |
| 505 | |
| 506 | obj = {{ |
| 507 | inventoryPath, |
| 508 | {{ITEM_IFACE, {}}, {NVME_STATUS_IFACE, {}}, {ASSET_IFACE, {}}}, |
| 509 | }}; |
| 510 | util::SDBusPlus::CallMethod(bus, INVENTORY_BUSNAME, INVENTORY_NAMESPACE, |
| 511 | INVENTORY_MANAGER_IFACE, "Notify", obj); |
| 512 | } |
| 513 | } |
| 514 | |
| 515 | void Nvme::init() |
| 516 | { |
| 517 | createNVMeInventory(); |
| 518 | } |
| 519 | |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 520 | void Nvme::readNvmeData(NVMeConfig& config) |
| 521 | { |
| 522 | std::string inventoryPath = NVME_INVENTORY_PATH + config.index; |
| 523 | NVMeData nvmeData; |
| 524 | |
| 525 | // get NVMe information through i2c by busID. |
| 526 | auto success = getNVMeInfobyBusID(config.busID, nvmeData); |
| 527 | auto iter = nvmes.find(config.index); |
| 528 | |
| 529 | // can not find. create dbus |
| 530 | if (iter == nvmes.end()) |
| 531 | { |
| 532 | log<level::INFO>("SSD plug.", |
| 533 | entry("index = %s", config.index.c_str())); |
| 534 | |
| 535 | std::string objPath = NVME_OBJ_PATH + config.index; |
| 536 | auto nvmeSSD = |
| 537 | std::make_shared<phosphor::nvme::NvmeSSD>(bus, objPath.c_str()); |
| 538 | nvmes.emplace(config.index, nvmeSSD); |
| 539 | |
| 540 | setNvmeInventoryProperties(true, nvmeData, inventoryPath); |
| 541 | nvmeSSD->setSensorValueToDbus(nvmeData.sensorValue); |
| 542 | nvmeSSD->setSensorThreshold(config.criticalHigh, config.criticalLow, |
| 543 | config.maxValue, config.minValue, |
| 544 | config.warningHigh, config.warningLow); |
| 545 | |
| 546 | nvmeSSD->checkSensorThreshold(); |
| 547 | setLEDsStatus(config, success, nvmeData); |
| 548 | } |
| 549 | else |
| 550 | { |
| 551 | setNvmeInventoryProperties(true, nvmeData, inventoryPath); |
| 552 | iter->second->setSensorValueToDbus(nvmeData.sensorValue); |
| 553 | iter->second->checkSensorThreshold(); |
| 554 | setLEDsStatus(config, success, nvmeData); |
| 555 | } |
| 556 | } |
| 557 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 558 | /** @brief Monitor NVMe drives every one second */ |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 559 | void Nvme::read() |
| 560 | { |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 561 | std::string devPresentPath; |
| 562 | std::string devPwrGoodPath; |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 563 | std::string inventoryPath; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 564 | |
| 565 | static std::unordered_map<std::string, bool> isErrorPower; |
| 566 | |
| 567 | for (auto config : configs) |
| 568 | { |
| 569 | NVMeData nvmeData; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 570 | |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 571 | inventoryPath = NVME_INVENTORY_PATH + config.index; |
| 572 | |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 573 | if (config.presentPin) |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 574 | { |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 575 | devPresentPath = |
| 576 | GPIO_BASE_PATH + std::to_string(config.presentPin) + "/value"; |
| 577 | |
| 578 | if (getGPIOValueOfNvme(devPresentPath) != IS_PRESENT) |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 579 | { |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 580 | // Drive not present, remove nvme d-bus path , |
| 581 | // clean all properties in inventory |
| 582 | // and turn off fault and locate LED |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 583 | |
| 584 | setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath, |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 585 | false); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 586 | setLocateLED(config.locateLedGroupPath, |
| 587 | config.locateLedControllerBusName, |
| 588 | config.locateLedControllerPath, false); |
| 589 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 590 | nvmeData = NVMeData(); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 591 | setNvmeInventoryProperties(false, nvmeData, inventoryPath); |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 592 | nvmes.erase(config.index); |
George Hung | 188ad95 | 2020-06-20 16:28:06 +0800 | [diff] [blame] | 593 | continue; |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 594 | } |
| 595 | else if (config.pwrGoodPin) |
| 596 | { |
| 597 | devPwrGoodPath = GPIO_BASE_PATH + |
| 598 | std::to_string(config.pwrGoodPin) + "/value"; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 599 | |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 600 | if (getGPIOValueOfNvme(devPwrGoodPath) != POWERGD) |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 601 | { |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 602 | |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 603 | // Present pin is true but power good pin is false |
| 604 | // remove nvme d-bus path, clean all properties in inventory |
| 605 | // and turn on fault LED |
| 606 | |
| 607 | setFaultLED(config.locateLedGroupPath, |
| 608 | config.faultLedGroupPath, true); |
| 609 | setLocateLED(config.locateLedGroupPath, |
| 610 | config.locateLedControllerBusName, |
| 611 | config.locateLedControllerPath, false); |
| 612 | |
| 613 | nvmeData = NVMeData(); |
| 614 | setNvmeInventoryProperties(false, nvmeData, inventoryPath); |
| 615 | nvmes.erase(config.index); |
| 616 | |
| 617 | if (isErrorPower[config.index] != true) |
| 618 | { |
| 619 | log<level::ERR>( |
| 620 | "Present pin is true but power good pin is false.", |
| 621 | entry("index = %s", config.index.c_str())); |
| 622 | log<level::ERR>( |
| 623 | "Erase SSD from map and d-bus.", |
| 624 | entry("index = %s", config.index.c_str())); |
| 625 | |
| 626 | isErrorPower[config.index] = true; |
| 627 | } |
George Hung | 188ad95 | 2020-06-20 16:28:06 +0800 | [diff] [blame] | 628 | continue; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 629 | } |
| 630 | } |
| 631 | } |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 632 | // Drive status is good, update value or create d-bus and update |
| 633 | // value. |
| 634 | readNvmeData(config); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 635 | |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 636 | isErrorPower[config.index] = false; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 637 | } |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 638 | } |
| 639 | } // namespace nvme |
| 640 | } // namespace phosphor |