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