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