blob: e73d849d142a49150363952edd32f8e66a19a5e5 [file] [log] [blame]
Tony Lee84d430c2019-06-13 15:26:15 +08001#include "nvme_manager.hpp"
2
Tony Lee6c595012019-06-19 10:54:59 +08003#include "smbus.hpp"
4
5#include <filesystem>
6#include <map>
7#include <nlohmann/json.hpp>
Tony Lee84d430c2019-06-13 15:26:15 +08008#include <phosphor-logging/elog-errors.hpp>
9#include <phosphor-logging/log.hpp>
Tony Lee89659212019-06-21 17:34:14 +080010#include <sdbusplus/message.hpp>
Tony Lee6c595012019-06-19 10:54:59 +080011#include <sstream>
12#include <string>
Tony Lee89659212019-06-21 17:34:14 +080013#include <xyz/openbmc_project/Led/Physical/server.hpp>
Tony Lee84d430c2019-06-13 15:26:15 +080014
Tony Lee6c595012019-06-19 10:54:59 +080015#include "i2c.h"
Tony Lee84d430c2019-06-13 15:26:15 +080016#define MONITOR_INTERVAL_SECONDS 1
Tony Lee6c595012019-06-19 10:54:59 +080017#define NVME_SSD_SLAVE_ADDRESS 0x6a
George Hung92a15ba2020-09-14 17:14:52 +080018#define NVME_SSD_VPD_SLAVE_ADDRESS 0x53
Tony Lee6c595012019-06-19 10:54:59 +080019#define GPIO_BASE_PATH "/sys/class/gpio/gpio"
20#define IS_PRESENT "0"
21#define POWERGD "1"
Tony Lee89659212019-06-21 17:34:14 +080022#define NOWARNING_STRING "ff"
Tony Lee6c595012019-06-19 10:54:59 +080023
24static constexpr auto configFile = "/etc/nvme/nvme_config.json";
25static constexpr auto delay = std::chrono::milliseconds{100};
26using Json = nlohmann::json;
27
28static constexpr const uint8_t COMMAND_CODE_0 = 0;
29static constexpr const uint8_t COMMAND_CODE_8 = 8;
George Hung92a15ba2020-09-14 17:14:52 +080030static constexpr const uint8_t CODE_0_LENGTH = 8;
31static constexpr const uint8_t CODE_8_LENGTH = 23;
Tony Lee6c595012019-06-19 10:54:59 +080032
Tony Lee89659212019-06-21 17:34:14 +080033static constexpr int CapacityFaultMask = 1;
34static constexpr int temperatureFaultMask = 1 << 1;
35static constexpr int DegradesFaultMask = 1 << 2;
36static constexpr int MediaFaultMask = 1 << 3;
37static constexpr int BackupDeviceFaultMask = 1 << 4;
38static constexpr int NOWARNING = 255;
39
Tony Lee6c595012019-06-19 10:54:59 +080040static constexpr int SERIALNUMBER_START_INDEX = 3;
41static constexpr int SERIALNUMBER_END_INDEX = 23;
George Hung92a15ba2020-09-14 17:14:52 +080042static constexpr int MODELNUMBER_START_INDEX = 46;
43static constexpr int MODELNUMBER_END_INDEX = 85;
Tony Lee6c595012019-06-19 10:54:59 +080044
45static constexpr const int TEMPERATURE_SENSOR_FAILURE = 0x81;
46
George Hung69b96182020-08-20 17:19:56 +080047static std::map<std::string, std::string> map_vendor = {{"80 86", "Intel"},
48 {"14 4d", "Samsung"}};
49
Tony Lee6c595012019-06-19 10:54:59 +080050namespace fs = std::filesystem;
51
Tony Lee84d430c2019-06-13 15:26:15 +080052namespace phosphor
53{
54namespace nvme
55{
56
57using namespace std;
58using namespace phosphor::logging;
59
Tony Lee89659212019-06-21 17:34:14 +080060void 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 Hung92a15ba2020-09-14 17:14:52 +080072 ASSET_IFACE, "Model", nvmeData.modelNumber);
73 util::SDBusPlus::setProperty(bus, INVENTORY_BUSNAME, inventoryPath,
Tony Lee89659212019-06-21 17:34:14 +080074 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
108void Nvme::setFaultLED(const std::string& locateLedGroupPath,
109 const std::string& faultLedGroupPath, bool request)
110{
111 if (locateLedGroupPath.empty() || faultLedGroupPath.empty())
112 {
113 return;
114 }
115
116 // Before toggle LED, check whether is Identify or not.
117 if (!getLEDGroupState(locateLedGroupPath))
118 {
119 util::SDBusPlus::setProperty(bus, LED_GROUP_BUSNAME, faultLedGroupPath,
120 LED_GROUP_IFACE, "Asserted", request);
121 }
122}
123
124void Nvme::setLocateLED(const std::string& locateLedGroupPath,
125 const std::string& locateLedBusName,
126 const std::string& locateLedPath, bool isPresent)
127{
128 if (locateLedGroupPath.empty() || locateLedBusName.empty() ||
129 locateLedPath.empty())
130 {
131 return;
132 }
133
134 namespace server = sdbusplus::xyz::openbmc_project::Led::server;
135
136 if (!getLEDGroupState(locateLedGroupPath))
137 {
138 if (isPresent)
139 util::SDBusPlus::setProperty(
140 bus, locateLedBusName, locateLedPath, LED_CONTROLLER_IFACE,
141 "State",
142 server::convertForMessage(server::Physical::Action::On));
143 else
144 util::SDBusPlus::setProperty(
145 bus, locateLedBusName, locateLedPath, LED_CONTROLLER_IFACE,
146 "State",
147 server::convertForMessage(server::Physical::Action::Off));
148 }
149}
150
151bool Nvme::getLEDGroupState(const std::string& ledPath)
152{
153 auto asserted = util::SDBusPlus::getProperty<bool>(
154 bus, LED_GROUP_BUSNAME, ledPath, LED_GROUP_IFACE, "Asserted");
155
156 return asserted;
157}
158
159void Nvme::setLEDsStatus(const phosphor::nvme::Nvme::NVMeConfig& config,
160 bool success,
161 const phosphor::nvme::Nvme::NVMeData& nvmeData)
162{
163 static std::unordered_map<std::string, bool> isError;
164
165 if (success)
166 {
167 if (!nvmeData.smartWarnings.empty())
168 {
169 auto request =
170 (strcmp(nvmeData.smartWarnings.c_str(), NOWARNING_STRING) == 0)
171 ? false
172 : true;
173
174 setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath,
175 request);
176 setLocateLED(config.locateLedGroupPath,
177 config.locateLedControllerBusName,
178 config.locateLedControllerPath, !request);
179 }
180 isError[config.index] = false;
181 }
182 else
183 {
184 if (isError[config.index] != true)
185 {
186 // Drive is present but can not get data, turn on fault LED.
187 log<level::ERR>("Drive status is good but can not get data.",
Brandon Kim9b771e22020-10-05 12:02:01 -0700188 entry("OBJ_PATH=%s", config.index.c_str()));
Tony Lee89659212019-06-21 17:34:14 +0800189 isError[config.index] = true;
190 }
191
192 setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath, true);
193 setLocateLED(config.locateLedGroupPath,
194 config.locateLedControllerBusName,
195 config.locateLedControllerPath, false);
196 }
197}
198
Tony Lee6c595012019-06-19 10:54:59 +0800199std::string intToHex(int input)
200{
201 std::stringstream tmp;
202 tmp << std::hex << input;
203
204 return tmp.str();
205}
206
207/** @brief Get NVMe info over smbus */
208bool getNVMeInfobyBusID(int busID, phosphor::nvme::Nvme::NVMeData& nvmeData)
209{
210 nvmeData.present = true;
211 nvmeData.vendor = "";
212 nvmeData.serialNumber = "";
213 nvmeData.smartWarnings = "";
214 nvmeData.statusFlags = "";
215 nvmeData.driveLifeUsed = "";
216 nvmeData.sensorValue = (int8_t)TEMPERATURE_SENSOR_FAILURE;
217
218 phosphor::smbus::Smbus smbus;
219
220 unsigned char rsp_data_command_0[I2C_DATA_MAX] = {0};
221 unsigned char rsp_data_command_8[I2C_DATA_MAX] = {0};
222
223 uint8_t tx_data = COMMAND_CODE_0;
224
225 auto init = smbus.smbusInit(busID);
226
227 static std::unordered_map<int, bool> isErrorSmbus;
228
229 if (init == -1)
230 {
231 if (isErrorSmbus[busID] != true)
232 {
233 log<level::ERR>("smbusInit fail!");
234 isErrorSmbus[busID] = true;
235 }
236
237 nvmeData.present = false;
238
239 return nvmeData.present;
240 }
241
George Hung92a15ba2020-09-14 17:14:52 +0800242 auto res_int = smbus.SendSmbusRWCmdRAW(busID, NVME_SSD_SLAVE_ADDRESS,
243 &tx_data, sizeof(tx_data),
244 rsp_data_command_0, CODE_0_LENGTH);
Tony Lee6c595012019-06-19 10:54:59 +0800245
246 if (res_int < 0)
247 {
248 if (isErrorSmbus[busID] != true)
249 {
250 log<level::ERR>("Send command code 0 fail!");
251 isErrorSmbus[busID] = true;
252 }
253
254 smbus.smbusClose(busID);
255 nvmeData.present = false;
256 return nvmeData.present;
257 }
258
George Hung92a15ba2020-09-14 17:14:52 +0800259 nvmeData.statusFlags = intToHex(rsp_data_command_0[1]);
260 nvmeData.smartWarnings = intToHex(rsp_data_command_0[2]);
261 nvmeData.driveLifeUsed = intToHex(rsp_data_command_0[4]);
George Hung93455332021-01-06 20:57:04 +0800262 nvmeData.sensorValue = static_cast<int8_t>(rsp_data_command_0[3]);
263 nvmeData.wcTemp = static_cast<int8_t>(rsp_data_command_0[5]);
George Hung92a15ba2020-09-14 17:14:52 +0800264
Tony Lee6c595012019-06-19 10:54:59 +0800265 tx_data = COMMAND_CODE_8;
266
George Hung92a15ba2020-09-14 17:14:52 +0800267 res_int = smbus.SendSmbusRWCmdRAW(busID, NVME_SSD_SLAVE_ADDRESS, &tx_data,
268 sizeof(tx_data), rsp_data_command_8,
269 CODE_8_LENGTH);
Tony Lee6c595012019-06-19 10:54:59 +0800270
271 if (res_int < 0)
272 {
273 if (isErrorSmbus[busID] != true)
274 {
275 log<level::ERR>("Send command code 8 fail!");
276 isErrorSmbus[busID] = true;
277 }
278
279 smbus.smbusClose(busID);
280 nvmeData.present = false;
281 return nvmeData.present;
282 }
283
284 nvmeData.vendor =
285 intToHex(rsp_data_command_8[1]) + " " + intToHex(rsp_data_command_8[2]);
286
George Hung69b96182020-08-20 17:19:56 +0800287 for (auto iter = map_vendor.begin(); iter != map_vendor.end(); iter++)
288 {
289 if (iter->first == nvmeData.vendor)
290 {
291 nvmeData.vendor = iter->second;
292 break;
293 }
294 }
295
Tony Lee6c595012019-06-19 10:54:59 +0800296 for (int offset = SERIALNUMBER_START_INDEX; offset < SERIALNUMBER_END_INDEX;
297 offset++)
298 {
George Hung69b96182020-08-20 17:19:56 +0800299 if (rsp_data_command_8[offset] != ' ')
300 nvmeData.serialNumber +=
301 static_cast<char>(rsp_data_command_8[offset]);
Tony Lee6c595012019-06-19 10:54:59 +0800302 }
303
George Hung92a15ba2020-09-14 17:14:52 +0800304 if (nvmeData.vendor == "Samsung")
305 {
306 unsigned char rsp_data_vpd[I2C_DATA_MAX] = {0};
307 const int rx_len = (MODELNUMBER_END_INDEX - MODELNUMBER_START_INDEX);
308 tx_data = MODELNUMBER_START_INDEX;
309
310 auto res_int =
311 smbus.SendSmbusRWCmdRAW(busID, NVME_SSD_VPD_SLAVE_ADDRESS, &tx_data,
312 sizeof(tx_data), rsp_data_vpd, rx_len);
313
314 if (res_int < 0)
315 {
316 if (isErrorSmbus[busID] != true)
317 {
318 log<level::ERR>("Send command read VPD fail!");
319 isErrorSmbus[busID] = true;
320 }
321
322 smbus.smbusClose(busID);
323 nvmeData.present = false;
324 return nvmeData.present;
325 }
326
327 for (int i = 0; i < rx_len; i++)
328 {
329 if (rsp_data_vpd[i] != ' ')
330 nvmeData.modelNumber += static_cast<char>(rsp_data_vpd[i]);
331 }
332
333 if (nvmeData.modelNumber.substr(0, nvmeData.vendor.size()) == "SAMSUNG")
334 nvmeData.modelNumber.erase(0, nvmeData.vendor.size());
335 }
Tony Lee6c595012019-06-19 10:54:59 +0800336
337 smbus.smbusClose(busID);
338
339 isErrorSmbus[busID] = false;
340
341 return nvmeData.present;
342}
343
Tony Lee84d430c2019-06-13 15:26:15 +0800344void Nvme::run()
345{
Tony Lee89659212019-06-21 17:34:14 +0800346 init();
347
Tony Lee84d430c2019-06-13 15:26:15 +0800348 std::function<void()> callback(std::bind(&Nvme::read, this));
349 try
350 {
351 u_int64_t interval = MONITOR_INTERVAL_SECONDS * 1000000;
352 _timer.restart(std::chrono::microseconds(interval));
353 }
354 catch (const std::exception& e)
355 {
Brandon Kim9b771e22020-10-05 12:02:01 -0700356 log<level::ERR>("Error in polling loop. "), entry("ERROR=%s", e.what());
Tony Lee84d430c2019-06-13 15:26:15 +0800357 }
358}
359
Tony Lee6c595012019-06-19 10:54:59 +0800360/** @brief Parsing NVMe config JSON file */
361Json parseSensorConfig()
Tony Lee84d430c2019-06-13 15:26:15 +0800362{
Tony Lee6c595012019-06-19 10:54:59 +0800363 std::ifstream jsonFile(configFile);
364 if (!jsonFile.is_open())
365 {
366 log<level::ERR>("NVMe config JSON file not found");
367 }
368
369 auto data = Json::parse(jsonFile, nullptr, false);
370 if (data.is_discarded())
371 {
372 log<level::ERR>("NVMe config readings JSON parser failure");
373 }
374
375 return data;
Tony Lee84d430c2019-06-13 15:26:15 +0800376}
377
Tony Lee6c595012019-06-19 10:54:59 +0800378/** @brief Obtain the initial configuration value of NVMe */
379std::vector<phosphor::nvme::Nvme::NVMeConfig> Nvme::getNvmeConfig()
380{
381
382 phosphor::nvme::Nvme::NVMeConfig nvmeConfig;
383 std::vector<phosphor::nvme::Nvme::NVMeConfig> nvmeConfigs;
384 int8_t criticalHigh = 0;
385 int8_t criticalLow = 0;
386 int8_t maxValue = 0;
387 int8_t minValue = 0;
388 int8_t warningHigh = 0;
389 int8_t warningLow = 0;
390
391 try
392 {
393 auto data = parseSensorConfig();
394 static const std::vector<Json> empty{};
395 std::vector<Json> readings = data.value("config", empty);
396 std::vector<Json> thresholds = data.value("threshold", empty);
397 if (!thresholds.empty())
398 {
399 for (const auto& instance : thresholds)
400 {
401 criticalHigh = instance.value("criticalHigh", 0);
402 criticalLow = instance.value("criticalLow", 0);
403 maxValue = instance.value("maxValue", 0);
404 minValue = instance.value("minValue", 0);
405 warningHigh = instance.value("warningHigh", 0);
406 warningLow = instance.value("warningLow", 0);
407 }
408 }
409 else
410 {
411 log<level::ERR>(
412 "Invalid NVMe config file, thresholds dosen't exist");
413 }
414
415 if (!readings.empty())
416 {
417 for (const auto& instance : readings)
418 {
419 uint8_t index = instance.value("NVMeDriveIndex", 0);
420 uint8_t busID = instance.value("NVMeDriveBusID", 0);
Tony Lee89659212019-06-21 17:34:14 +0800421 std::string faultLedGroupPath =
422 instance.value("NVMeDriveFaultLEDGroupPath", "");
423 std::string locateLedGroupPath =
424 instance.value("NVMeDriveLocateLEDGroupPath", "");
George Hung873b5b32020-06-20 14:56:15 +0800425 uint16_t presentPin = instance.value("NVMeDrivePresentPin", 0);
426 uint16_t pwrGoodPin = instance.value("NVMeDrivePwrGoodPin", 0);
Tony Lee89659212019-06-21 17:34:14 +0800427 std::string locateLedControllerBusName =
428 instance.value("NVMeDriveLocateLEDControllerBusName", "");
429 std::string locateLedControllerPath =
430 instance.value("NVMeDriveLocateLEDControllerPath", "");
Tony Lee6c595012019-06-19 10:54:59 +0800431
432 nvmeConfig.index = std::to_string(index);
433 nvmeConfig.busID = busID;
Tony Lee89659212019-06-21 17:34:14 +0800434 nvmeConfig.faultLedGroupPath = faultLedGroupPath;
Tony Lee6c595012019-06-19 10:54:59 +0800435 nvmeConfig.presentPin = presentPin;
436 nvmeConfig.pwrGoodPin = pwrGoodPin;
Tony Lee89659212019-06-21 17:34:14 +0800437 nvmeConfig.locateLedControllerBusName =
438 locateLedControllerBusName;
439 nvmeConfig.locateLedControllerPath = locateLedControllerPath;
440 nvmeConfig.locateLedGroupPath = locateLedGroupPath;
Tony Lee6c595012019-06-19 10:54:59 +0800441 nvmeConfig.criticalHigh = criticalHigh;
442 nvmeConfig.criticalLow = criticalLow;
443 nvmeConfig.warningHigh = warningHigh;
444 nvmeConfig.warningLow = warningLow;
445 nvmeConfig.maxValue = maxValue;
446 nvmeConfig.minValue = minValue;
447 nvmeConfigs.push_back(nvmeConfig);
448 }
449 }
450 else
451 {
452 log<level::ERR>("Invalid NVMe config file, config dosen't exist");
453 }
454 }
455 catch (const Json::exception& e)
456 {
Brandon Kim9b771e22020-10-05 12:02:01 -0700457 log<level::ERR>("Json Exception caught."), entry("MSG=%s", e.what());
Tony Lee6c595012019-06-19 10:54:59 +0800458 }
459
460 return nvmeConfigs;
461}
462
463std::string Nvme::getGPIOValueOfNvme(const std::string& fullPath)
464{
465 std::string val;
466 std::ifstream ifs;
467 auto retries = 3;
468
469 while (retries != 0)
470 {
471 try
472 {
473 if (!ifs.is_open())
474 ifs.open(fullPath);
475 ifs.clear();
476 ifs.seekg(0);
477 ifs >> val;
478 }
479 catch (const std::exception& e)
480 {
481 --retries;
482 std::this_thread::sleep_for(delay);
483 log<level::ERR>("Can not open gpio path.",
Brandon Kim9b771e22020-10-05 12:02:01 -0700484 entry("MSG=%s", e.what()));
Tony Lee6c595012019-06-19 10:54:59 +0800485 continue;
486 }
487 break;
488 }
489
490 ifs.close();
491 return val;
492}
493
Tony Lee89659212019-06-21 17:34:14 +0800494void Nvme::createNVMeInventory()
495{
Patrick Williams05eedaa2020-05-13 17:58:42 -0500496 using Properties = std::map<std::string, std::variant<std::string, bool>>;
Tony Lee89659212019-06-21 17:34:14 +0800497 using Interfaces = std::map<std::string, Properties>;
498
499 std::string inventoryPath;
500 std::map<sdbusplus::message::object_path, Interfaces> obj;
501
502 for (const auto config : configs)
503 {
504 inventoryPath = "/system/chassis/motherboard/nvme" + config.index;
505
506 obj = {{
507 inventoryPath,
508 {{ITEM_IFACE, {}}, {NVME_STATUS_IFACE, {}}, {ASSET_IFACE, {}}},
509 }};
510 util::SDBusPlus::CallMethod(bus, INVENTORY_BUSNAME, INVENTORY_NAMESPACE,
511 INVENTORY_MANAGER_IFACE, "Notify", obj);
512 }
513}
514
515void Nvme::init()
516{
517 createNVMeInventory();
518}
519
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700520void Nvme::readNvmeData(NVMeConfig& config)
521{
522 std::string inventoryPath = NVME_INVENTORY_PATH + config.index;
523 NVMeData nvmeData;
524
525 // get NVMe information through i2c by busID.
526 auto success = getNVMeInfobyBusID(config.busID, nvmeData);
527 auto iter = nvmes.find(config.index);
528
529 // can not find. create dbus
530 if (iter == nvmes.end())
531 {
Brandon Kim9b771e22020-10-05 12:02:01 -0700532 log<level::INFO>("SSD plug.", entry("INDEX=%s", config.index.c_str()));
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700533
534 std::string objPath = NVME_OBJ_PATH + config.index;
535 auto nvmeSSD =
536 std::make_shared<phosphor::nvme::NvmeSSD>(bus, objPath.c_str());
537 nvmes.emplace(config.index, nvmeSSD);
538
539 setNvmeInventoryProperties(true, nvmeData, inventoryPath);
540 nvmeSSD->setSensorValueToDbus(nvmeData.sensorValue);
George Hung93455332021-01-06 20:57:04 +0800541 if (nvmeData.wcTemp != 0)
542 {
543 config.criticalHigh = nvmeData.wcTemp;
544 config.warningHigh = nvmeData.wcTemp;
545 }
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700546 nvmeSSD->setSensorThreshold(config.criticalHigh, config.criticalLow,
547 config.maxValue, config.minValue,
548 config.warningHigh, config.warningLow);
549
550 nvmeSSD->checkSensorThreshold();
551 setLEDsStatus(config, success, nvmeData);
552 }
553 else
554 {
555 setNvmeInventoryProperties(true, nvmeData, inventoryPath);
556 iter->second->setSensorValueToDbus(nvmeData.sensorValue);
557 iter->second->checkSensorThreshold();
558 setLEDsStatus(config, success, nvmeData);
559 }
560}
561
Tony Lee6c595012019-06-19 10:54:59 +0800562/** @brief Monitor NVMe drives every one second */
Tony Lee84d430c2019-06-13 15:26:15 +0800563void Nvme::read()
564{
Tony Lee6c595012019-06-19 10:54:59 +0800565 std::string devPresentPath;
566 std::string devPwrGoodPath;
Tony Lee89659212019-06-21 17:34:14 +0800567 std::string inventoryPath;
Tony Lee6c595012019-06-19 10:54:59 +0800568
569 static std::unordered_map<std::string, bool> isErrorPower;
570
571 for (auto config : configs)
572 {
573 NVMeData nvmeData;
Tony Lee6c595012019-06-19 10:54:59 +0800574
Tony Lee89659212019-06-21 17:34:14 +0800575 inventoryPath = NVME_INVENTORY_PATH + config.index;
576
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700577 if (config.presentPin)
Tony Lee6c595012019-06-19 10:54:59 +0800578 {
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700579 devPresentPath =
580 GPIO_BASE_PATH + std::to_string(config.presentPin) + "/value";
581
582 if (getGPIOValueOfNvme(devPresentPath) != IS_PRESENT)
Tony Lee6c595012019-06-19 10:54:59 +0800583 {
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700584 // Drive not present, remove nvme d-bus path ,
585 // clean all properties in inventory
586 // and turn off fault and locate LED
Tony Lee89659212019-06-21 17:34:14 +0800587
588 setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath,
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700589 false);
Tony Lee89659212019-06-21 17:34:14 +0800590 setLocateLED(config.locateLedGroupPath,
591 config.locateLedControllerBusName,
592 config.locateLedControllerPath, false);
593
Tony Lee6c595012019-06-19 10:54:59 +0800594 nvmeData = NVMeData();
Tony Lee89659212019-06-21 17:34:14 +0800595 setNvmeInventoryProperties(false, nvmeData, inventoryPath);
Tony Lee6c595012019-06-19 10:54:59 +0800596 nvmes.erase(config.index);
George Hung188ad952020-06-20 16:28:06 +0800597 continue;
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700598 }
599 else if (config.pwrGoodPin)
600 {
601 devPwrGoodPath = GPIO_BASE_PATH +
602 std::to_string(config.pwrGoodPin) + "/value";
Tony Lee6c595012019-06-19 10:54:59 +0800603
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700604 if (getGPIOValueOfNvme(devPwrGoodPath) != POWERGD)
Tony Lee6c595012019-06-19 10:54:59 +0800605 {
George Hung95b90f42021-01-11 21:18:59 +0800606 // IFDET should be used to provide the final say
607 // in SSD's presence - IFDET showing SSD is present
608 // but the power is off (if the drive is plugged in)
609 // is a valid state.
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700610
611 setFaultLED(config.locateLedGroupPath,
612 config.faultLedGroupPath, true);
613 setLocateLED(config.locateLedGroupPath,
614 config.locateLedControllerBusName,
615 config.locateLedControllerPath, false);
616
617 nvmeData = NVMeData();
George Hung95b90f42021-01-11 21:18:59 +0800618 setNvmeInventoryProperties(true, nvmeData, inventoryPath);
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700619
620 if (isErrorPower[config.index] != true)
621 {
622 log<level::ERR>(
623 "Present pin is true but power good pin is false.",
Brandon Kim9b771e22020-10-05 12:02:01 -0700624 entry("INDEX=%s", config.index.c_str()));
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700625 log<level::ERR>(
626 "Erase SSD from map and d-bus.",
Brandon Kim9b771e22020-10-05 12:02:01 -0700627 entry("INDEX=%s", config.index.c_str()));
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700628
629 isErrorPower[config.index] = true;
630 }
George Hung95b90f42021-01-11 21:18:59 +0800631
632 // Keep reading to report the invalid temperature
633 // (To make thermal loop know that the sensor reading
634 // is invalid).
635 readNvmeData(config);
George Hung188ad952020-06-20 16:28:06 +0800636 continue;
Tony Lee6c595012019-06-19 10:54:59 +0800637 }
638 }
639 }
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700640 // Drive status is good, update value or create d-bus and update
641 // value.
642 readNvmeData(config);
Tony Lee89659212019-06-21 17:34:14 +0800643
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700644 isErrorPower[config.index] = false;
Tony Lee6c595012019-06-19 10:54:59 +0800645 }
Tony Lee84d430c2019-06-13 15:26:15 +0800646}
647} // namespace nvme
648} // namespace phosphor