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 | { |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 167 | |
| 168 | if (success) |
| 169 | { |
| 170 | if (!nvmeData.smartWarnings.empty()) |
| 171 | { |
| 172 | auto request = |
| 173 | (strcmp(nvmeData.smartWarnings.c_str(), NOWARNING_STRING) == 0) |
| 174 | ? false |
| 175 | : true; |
| 176 | |
| 177 | setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath, |
| 178 | request); |
| 179 | setLocateLED(config.locateLedGroupPath, |
| 180 | config.locateLedControllerBusName, |
| 181 | config.locateLedControllerPath, !request); |
| 182 | } |
| 183 | isError[config.index] = false; |
| 184 | } |
| 185 | else |
| 186 | { |
| 187 | if (isError[config.index] != true) |
| 188 | { |
| 189 | // Drive is present but can not get data, turn on fault LED. |
| 190 | log<level::ERR>("Drive status is good but can not get data.", |
Brandon Kim | 9b771e2 | 2020-10-05 12:02:01 -0700 | [diff] [blame] | 191 | entry("OBJ_PATH=%s", config.index.c_str())); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 192 | isError[config.index] = true; |
| 193 | } |
| 194 | |
| 195 | setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath, true); |
| 196 | setLocateLED(config.locateLedGroupPath, |
| 197 | config.locateLedControllerBusName, |
| 198 | config.locateLedControllerPath, false); |
| 199 | } |
| 200 | } |
| 201 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 202 | std::string intToHex(int input) |
| 203 | { |
| 204 | std::stringstream tmp; |
| 205 | tmp << std::hex << input; |
| 206 | |
| 207 | return tmp.str(); |
| 208 | } |
| 209 | |
| 210 | /** @brief Get NVMe info over smbus */ |
George Hung | 5e23bcd | 2021-07-01 12:16:11 +0800 | [diff] [blame^] | 211 | bool Nvme::getNVMeInfobyBusID(int busID, |
| 212 | phosphor::nvme::Nvme::NVMeData& nvmeData) |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 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 = ""; |
Brandon Kim | d5838d1 | 2021-05-19 12:51:55 -0700 | [diff] [blame] | 221 | nvmeData.sensorValue = static_cast<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 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 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 | |
George Hung | 831f204 | 2021-05-19 17:02:29 +0800 | [diff] [blame] | 506 | for (const auto& config : configs) |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 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 | } |
George Hung | 831f204 | 2021-05-19 17:02:29 +0800 | [diff] [blame] | 550 | nvmeSSD->setSensorMaxMin(config.maxValue, config.minValue); |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 551 | nvmeSSD->setSensorThreshold(config.criticalHigh, config.criticalLow, |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 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); |
George Hung | 831f204 | 2021-05-19 17:02:29 +0800 | [diff] [blame] | 561 | if (nvmeData.wcTemp != 0) |
| 562 | { |
| 563 | iter->second->setSensorThreshold( |
| 564 | config.criticalHigh, config.criticalLow, config.warningHigh, |
| 565 | config.warningLow); |
| 566 | } |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 567 | iter->second->checkSensorThreshold(); |
| 568 | setLEDsStatus(config, success, nvmeData); |
| 569 | } |
| 570 | } |
| 571 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 572 | /** @brief Monitor NVMe drives every one second */ |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 573 | void Nvme::read() |
| 574 | { |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 575 | std::string devPresentPath; |
| 576 | std::string devPwrGoodPath; |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 577 | std::string inventoryPath; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 578 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 579 | for (auto config : configs) |
| 580 | { |
| 581 | NVMeData nvmeData; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 582 | |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 583 | inventoryPath = NVME_INVENTORY_PATH + config.index; |
| 584 | |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 585 | if (config.presentPin) |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 586 | { |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 587 | devPresentPath = |
| 588 | GPIO_BASE_PATH + std::to_string(config.presentPin) + "/value"; |
| 589 | |
| 590 | if (getGPIOValueOfNvme(devPresentPath) != IS_PRESENT) |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 591 | { |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 592 | // Drive not present, remove nvme d-bus path , |
| 593 | // clean all properties in inventory |
| 594 | // and turn off fault and locate LED |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 595 | |
| 596 | setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath, |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 597 | false); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 598 | setLocateLED(config.locateLedGroupPath, |
| 599 | config.locateLedControllerBusName, |
| 600 | config.locateLedControllerPath, false); |
| 601 | |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 602 | nvmeData = NVMeData(); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 603 | setNvmeInventoryProperties(false, nvmeData, inventoryPath); |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 604 | nvmes.erase(config.index); |
George Hung | 188ad95 | 2020-06-20 16:28:06 +0800 | [diff] [blame] | 605 | continue; |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 606 | } |
| 607 | else if (config.pwrGoodPin) |
| 608 | { |
| 609 | devPwrGoodPath = GPIO_BASE_PATH + |
| 610 | std::to_string(config.pwrGoodPin) + "/value"; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 611 | |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 612 | if (getGPIOValueOfNvme(devPwrGoodPath) != POWERGD) |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 613 | { |
George Hung | 95b90f4 | 2021-01-11 21:18:59 +0800 | [diff] [blame] | 614 | // IFDET should be used to provide the final say |
| 615 | // in SSD's presence - IFDET showing SSD is present |
| 616 | // but the power is off (if the drive is plugged in) |
| 617 | // is a valid state. |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 618 | |
| 619 | setFaultLED(config.locateLedGroupPath, |
| 620 | config.faultLedGroupPath, true); |
| 621 | setLocateLED(config.locateLedGroupPath, |
| 622 | config.locateLedControllerBusName, |
| 623 | config.locateLedControllerPath, false); |
| 624 | |
| 625 | nvmeData = NVMeData(); |
George Hung | 95b90f4 | 2021-01-11 21:18:59 +0800 | [diff] [blame] | 626 | setNvmeInventoryProperties(true, nvmeData, inventoryPath); |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 627 | |
| 628 | if (isErrorPower[config.index] != true) |
| 629 | { |
| 630 | log<level::ERR>( |
| 631 | "Present pin is true but power good pin is false.", |
Brandon Kim | 9b771e2 | 2020-10-05 12:02:01 -0700 | [diff] [blame] | 632 | entry("INDEX=%s", config.index.c_str())); |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 633 | log<level::ERR>( |
| 634 | "Erase SSD from map and d-bus.", |
Brandon Kim | 9b771e2 | 2020-10-05 12:02:01 -0700 | [diff] [blame] | 635 | entry("INDEX=%s", config.index.c_str())); |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 636 | |
| 637 | isErrorPower[config.index] = true; |
| 638 | } |
George Hung | 95b90f4 | 2021-01-11 21:18:59 +0800 | [diff] [blame] | 639 | |
| 640 | // Keep reading to report the invalid temperature |
| 641 | // (To make thermal loop know that the sensor reading |
| 642 | // is invalid). |
| 643 | readNvmeData(config); |
George Hung | 188ad95 | 2020-06-20 16:28:06 +0800 | [diff] [blame] | 644 | continue; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 645 | } |
| 646 | } |
| 647 | } |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 648 | // Drive status is good, update value or create d-bus and update |
| 649 | // value. |
| 650 | readNvmeData(config); |
Tony Lee | 8965921 | 2019-06-21 17:34:14 +0800 | [diff] [blame] | 651 | |
Vijay Khemka | e41b2e4 | 2020-04-21 09:23:10 -0700 | [diff] [blame] | 652 | isErrorPower[config.index] = false; |
Tony Lee | 6c59501 | 2019-06-19 10:54:59 +0800 | [diff] [blame] | 653 | } |
Tony Lee | 84d430c | 2019-06-13 15:26:15 +0800 | [diff] [blame] | 654 | } |
| 655 | } // namespace nvme |
| 656 | } // namespace phosphor |