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