blob: 02bc819c97bfa663fcc4cbd8842d28e40a648be9 [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 {
Brandon Kimfdffe5c2021-03-30 15:07:59 -0700119 if (getLEDGroupState(faultLedGroupPath) != request)
120 {
121 util::SDBusPlus::setProperty(bus, LED_GROUP_BUSNAME,
122 faultLedGroupPath, LED_GROUP_IFACE,
123 "Asserted", request);
124 }
Tony Lee89659212019-06-21 17:34:14 +0800125 }
126}
127
128void 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
155bool 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
163void 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 Kim9b771e22020-10-05 12:02:01 -0700192 entry("OBJ_PATH=%s", config.index.c_str()));
Tony Lee89659212019-06-21 17:34:14 +0800193 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 Lee6c595012019-06-19 10:54:59 +0800203std::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 */
212bool getNVMeInfobyBusID(int busID, phosphor::nvme::Nvme::NVMeData& nvmeData)
213{
214 nvmeData.present = true;
215 nvmeData.vendor = "";
216 nvmeData.serialNumber = "";
217 nvmeData.smartWarnings = "";
218 nvmeData.statusFlags = "";
219 nvmeData.driveLifeUsed = "";
220 nvmeData.sensorValue = (int8_t)TEMPERATURE_SENSOR_FAILURE;
221
222 phosphor::smbus::Smbus smbus;
223
224 unsigned char rsp_data_command_0[I2C_DATA_MAX] = {0};
225 unsigned char rsp_data_command_8[I2C_DATA_MAX] = {0};
226
227 uint8_t tx_data = COMMAND_CODE_0;
228
229 auto init = smbus.smbusInit(busID);
230
231 static std::unordered_map<int, bool> isErrorSmbus;
232
233 if (init == -1)
234 {
235 if (isErrorSmbus[busID] != true)
236 {
237 log<level::ERR>("smbusInit fail!");
238 isErrorSmbus[busID] = true;
239 }
240
241 nvmeData.present = false;
242
243 return nvmeData.present;
244 }
245
George Hung92a15ba2020-09-14 17:14:52 +0800246 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 Lee6c595012019-06-19 10:54:59 +0800249
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 Hung92a15ba2020-09-14 17:14:52 +0800263 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 Hung93455332021-01-06 20:57:04 +0800266 nvmeData.sensorValue = static_cast<int8_t>(rsp_data_command_0[3]);
267 nvmeData.wcTemp = static_cast<int8_t>(rsp_data_command_0[5]);
George Hung92a15ba2020-09-14 17:14:52 +0800268
Tony Lee6c595012019-06-19 10:54:59 +0800269 tx_data = COMMAND_CODE_8;
270
George Hung92a15ba2020-09-14 17:14:52 +0800271 res_int = smbus.SendSmbusRWCmdRAW(busID, NVME_SSD_SLAVE_ADDRESS, &tx_data,
272 sizeof(tx_data), rsp_data_command_8,
273 CODE_8_LENGTH);
Tony Lee6c595012019-06-19 10:54:59 +0800274
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 Hung69b96182020-08-20 17:19:56 +0800291 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 Lee6c595012019-06-19 10:54:59 +0800300 for (int offset = SERIALNUMBER_START_INDEX; offset < SERIALNUMBER_END_INDEX;
301 offset++)
302 {
George Hung69b96182020-08-20 17:19:56 +0800303 if (rsp_data_command_8[offset] != ' ')
304 nvmeData.serialNumber +=
305 static_cast<char>(rsp_data_command_8[offset]);
Tony Lee6c595012019-06-19 10:54:59 +0800306 }
307
George Hung92a15ba2020-09-14 17:14:52 +0800308 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 Lee6c595012019-06-19 10:54:59 +0800340
341 smbus.smbusClose(busID);
342
343 isErrorSmbus[busID] = false;
344
345 return nvmeData.present;
346}
347
Tony Lee84d430c2019-06-13 15:26:15 +0800348void Nvme::run()
349{
Tony Lee89659212019-06-21 17:34:14 +0800350 init();
351
Tony Lee84d430c2019-06-13 15:26:15 +0800352 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 Kim9b771e22020-10-05 12:02:01 -0700360 log<level::ERR>("Error in polling loop. "), entry("ERROR=%s", e.what());
Tony Lee84d430c2019-06-13 15:26:15 +0800361 }
362}
363
Tony Lee6c595012019-06-19 10:54:59 +0800364/** @brief Parsing NVMe config JSON file */
365Json parseSensorConfig()
Tony Lee84d430c2019-06-13 15:26:15 +0800366{
Tony Lee6c595012019-06-19 10:54:59 +0800367 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 Lee84d430c2019-06-13 15:26:15 +0800380}
381
Tony Lee6c595012019-06-19 10:54:59 +0800382/** @brief Obtain the initial configuration value of NVMe */
383std::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 Lee89659212019-06-21 17:34:14 +0800425 std::string faultLedGroupPath =
426 instance.value("NVMeDriveFaultLEDGroupPath", "");
427 std::string locateLedGroupPath =
428 instance.value("NVMeDriveLocateLEDGroupPath", "");
George Hung873b5b32020-06-20 14:56:15 +0800429 uint16_t presentPin = instance.value("NVMeDrivePresentPin", 0);
430 uint16_t pwrGoodPin = instance.value("NVMeDrivePwrGoodPin", 0);
Tony Lee89659212019-06-21 17:34:14 +0800431 std::string locateLedControllerBusName =
432 instance.value("NVMeDriveLocateLEDControllerBusName", "");
433 std::string locateLedControllerPath =
434 instance.value("NVMeDriveLocateLEDControllerPath", "");
Tony Lee6c595012019-06-19 10:54:59 +0800435
436 nvmeConfig.index = std::to_string(index);
437 nvmeConfig.busID = busID;
Tony Lee89659212019-06-21 17:34:14 +0800438 nvmeConfig.faultLedGroupPath = faultLedGroupPath;
Tony Lee6c595012019-06-19 10:54:59 +0800439 nvmeConfig.presentPin = presentPin;
440 nvmeConfig.pwrGoodPin = pwrGoodPin;
Tony Lee89659212019-06-21 17:34:14 +0800441 nvmeConfig.locateLedControllerBusName =
442 locateLedControllerBusName;
443 nvmeConfig.locateLedControllerPath = locateLedControllerPath;
444 nvmeConfig.locateLedGroupPath = locateLedGroupPath;
Tony Lee6c595012019-06-19 10:54:59 +0800445 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 Kim9b771e22020-10-05 12:02:01 -0700461 log<level::ERR>("Json Exception caught."), entry("MSG=%s", e.what());
Tony Lee6c595012019-06-19 10:54:59 +0800462 }
463
464 return nvmeConfigs;
465}
466
467std::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 Kim9b771e22020-10-05 12:02:01 -0700488 entry("MSG=%s", e.what()));
Tony Lee6c595012019-06-19 10:54:59 +0800489 continue;
490 }
491 break;
492 }
493
494 ifs.close();
495 return val;
496}
497
Tony Lee89659212019-06-21 17:34:14 +0800498void Nvme::createNVMeInventory()
499{
Patrick Williams05eedaa2020-05-13 17:58:42 -0500500 using Properties = std::map<std::string, std::variant<std::string, bool>>;
Tony Lee89659212019-06-21 17:34:14 +0800501 using Interfaces = std::map<std::string, Properties>;
502
503 std::string inventoryPath;
504 std::map<sdbusplus::message::object_path, Interfaces> obj;
505
506 for (const auto config : configs)
507 {
508 inventoryPath = "/system/chassis/motherboard/nvme" + config.index;
509
510 obj = {{
511 inventoryPath,
512 {{ITEM_IFACE, {}}, {NVME_STATUS_IFACE, {}}, {ASSET_IFACE, {}}},
513 }};
514 util::SDBusPlus::CallMethod(bus, INVENTORY_BUSNAME, INVENTORY_NAMESPACE,
515 INVENTORY_MANAGER_IFACE, "Notify", obj);
516 }
517}
518
519void Nvme::init()
520{
521 createNVMeInventory();
522}
523
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700524void 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 Kim9b771e22020-10-05 12:02:01 -0700536 log<level::INFO>("SSD plug.", entry("INDEX=%s", config.index.c_str()));
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700537
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 Hung93455332021-01-06 20:57:04 +0800545 if (nvmeData.wcTemp != 0)
546 {
547 config.criticalHigh = nvmeData.wcTemp;
548 config.warningHigh = nvmeData.wcTemp;
549 }
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700550 nvmeSSD->setSensorThreshold(config.criticalHigh, config.criticalLow,
551 config.maxValue, config.minValue,
552 config.warningHigh, config.warningLow);
553
554 nvmeSSD->checkSensorThreshold();
555 setLEDsStatus(config, success, nvmeData);
556 }
557 else
558 {
559 setNvmeInventoryProperties(true, nvmeData, inventoryPath);
560 iter->second->setSensorValueToDbus(nvmeData.sensorValue);
561 iter->second->checkSensorThreshold();
562 setLEDsStatus(config, success, nvmeData);
563 }
564}
565
Tony Lee6c595012019-06-19 10:54:59 +0800566/** @brief Monitor NVMe drives every one second */
Tony Lee84d430c2019-06-13 15:26:15 +0800567void Nvme::read()
568{
Tony Lee6c595012019-06-19 10:54:59 +0800569 std::string devPresentPath;
570 std::string devPwrGoodPath;
Tony Lee89659212019-06-21 17:34:14 +0800571 std::string inventoryPath;
Tony Lee6c595012019-06-19 10:54:59 +0800572
573 static std::unordered_map<std::string, bool> isErrorPower;
574
575 for (auto config : configs)
576 {
577 NVMeData nvmeData;
Tony Lee6c595012019-06-19 10:54:59 +0800578
Tony Lee89659212019-06-21 17:34:14 +0800579 inventoryPath = NVME_INVENTORY_PATH + config.index;
580
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700581 if (config.presentPin)
Tony Lee6c595012019-06-19 10:54:59 +0800582 {
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700583 devPresentPath =
584 GPIO_BASE_PATH + std::to_string(config.presentPin) + "/value";
585
586 if (getGPIOValueOfNvme(devPresentPath) != IS_PRESENT)
Tony Lee6c595012019-06-19 10:54:59 +0800587 {
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700588 // Drive not present, remove nvme d-bus path ,
589 // clean all properties in inventory
590 // and turn off fault and locate LED
Tony Lee89659212019-06-21 17:34:14 +0800591
592 setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath,
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700593 false);
Tony Lee89659212019-06-21 17:34:14 +0800594 setLocateLED(config.locateLedGroupPath,
595 config.locateLedControllerBusName,
596 config.locateLedControllerPath, false);
597
Tony Lee6c595012019-06-19 10:54:59 +0800598 nvmeData = NVMeData();
Tony Lee89659212019-06-21 17:34:14 +0800599 setNvmeInventoryProperties(false, nvmeData, inventoryPath);
Tony Lee6c595012019-06-19 10:54:59 +0800600 nvmes.erase(config.index);
George Hung188ad952020-06-20 16:28:06 +0800601 continue;
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700602 }
603 else if (config.pwrGoodPin)
604 {
605 devPwrGoodPath = GPIO_BASE_PATH +
606 std::to_string(config.pwrGoodPin) + "/value";
Tony Lee6c595012019-06-19 10:54:59 +0800607
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700608 if (getGPIOValueOfNvme(devPwrGoodPath) != POWERGD)
Tony Lee6c595012019-06-19 10:54:59 +0800609 {
George Hung95b90f42021-01-11 21:18:59 +0800610 // IFDET should be used to provide the final say
611 // in SSD's presence - IFDET showing SSD is present
612 // but the power is off (if the drive is plugged in)
613 // is a valid state.
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700614
615 setFaultLED(config.locateLedGroupPath,
616 config.faultLedGroupPath, true);
617 setLocateLED(config.locateLedGroupPath,
618 config.locateLedControllerBusName,
619 config.locateLedControllerPath, false);
620
621 nvmeData = NVMeData();
George Hung95b90f42021-01-11 21:18:59 +0800622 setNvmeInventoryProperties(true, nvmeData, inventoryPath);
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700623
624 if (isErrorPower[config.index] != true)
625 {
626 log<level::ERR>(
627 "Present pin is true but power good pin is false.",
Brandon Kim9b771e22020-10-05 12:02:01 -0700628 entry("INDEX=%s", config.index.c_str()));
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700629 log<level::ERR>(
630 "Erase SSD from map and d-bus.",
Brandon Kim9b771e22020-10-05 12:02:01 -0700631 entry("INDEX=%s", config.index.c_str()));
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700632
633 isErrorPower[config.index] = true;
634 }
George Hung95b90f42021-01-11 21:18:59 +0800635
636 // Keep reading to report the invalid temperature
637 // (To make thermal loop know that the sensor reading
638 // is invalid).
639 readNvmeData(config);
George Hung188ad952020-06-20 16:28:06 +0800640 continue;
Tony Lee6c595012019-06-19 10:54:59 +0800641 }
642 }
643 }
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700644 // Drive status is good, update value or create d-bus and update
645 // value.
646 readNvmeData(config);
Tony Lee89659212019-06-21 17:34:14 +0800647
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700648 isErrorPower[config.index] = false;
Tony Lee6c595012019-06-19 10:54:59 +0800649 }
Tony Lee84d430c2019-06-13 15:26:15 +0800650}
651} // namespace nvme
652} // namespace phosphor