blob: ccd2b604eeaa7ec12dc4859132e0982789923e9f [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 = "";
George Hung831f2042021-05-19 17:02:29 +0800217 nvmeData.modelNumber = "";
Tony Lee6c595012019-06-19 10:54:59 +0800218 nvmeData.smartWarnings = "";
219 nvmeData.statusFlags = "";
220 nvmeData.driveLifeUsed = "";
Brandon Kimd5838d12021-05-19 12:51:55 -0700221 nvmeData.sensorValue = static_cast<int8_t>(TEMPERATURE_SENSOR_FAILURE);
George Hung831f2042021-05-19 17:02:29 +0800222 nvmeData.wcTemp = 0;
Tony Lee6c595012019-06-19 10:54:59 +0800223
224 phosphor::smbus::Smbus smbus;
225
226 unsigned char rsp_data_command_0[I2C_DATA_MAX] = {0};
227 unsigned char rsp_data_command_8[I2C_DATA_MAX] = {0};
228
229 uint8_t tx_data = COMMAND_CODE_0;
230
231 auto init = smbus.smbusInit(busID);
232
233 static std::unordered_map<int, bool> isErrorSmbus;
234
235 if (init == -1)
236 {
237 if (isErrorSmbus[busID] != true)
238 {
239 log<level::ERR>("smbusInit fail!");
240 isErrorSmbus[busID] = true;
241 }
242
243 nvmeData.present = false;
244
245 return nvmeData.present;
246 }
247
George Hung92a15ba2020-09-14 17:14:52 +0800248 auto res_int = smbus.SendSmbusRWCmdRAW(busID, NVME_SSD_SLAVE_ADDRESS,
249 &tx_data, sizeof(tx_data),
250 rsp_data_command_0, CODE_0_LENGTH);
Tony Lee6c595012019-06-19 10:54:59 +0800251
252 if (res_int < 0)
253 {
254 if (isErrorSmbus[busID] != true)
255 {
256 log<level::ERR>("Send command code 0 fail!");
257 isErrorSmbus[busID] = true;
258 }
259
260 smbus.smbusClose(busID);
261 nvmeData.present = false;
262 return nvmeData.present;
263 }
264
George Hung92a15ba2020-09-14 17:14:52 +0800265 nvmeData.statusFlags = intToHex(rsp_data_command_0[1]);
266 nvmeData.smartWarnings = intToHex(rsp_data_command_0[2]);
267 nvmeData.driveLifeUsed = intToHex(rsp_data_command_0[4]);
George Hung93455332021-01-06 20:57:04 +0800268 nvmeData.sensorValue = static_cast<int8_t>(rsp_data_command_0[3]);
269 nvmeData.wcTemp = static_cast<int8_t>(rsp_data_command_0[5]);
George Hung92a15ba2020-09-14 17:14:52 +0800270
Tony Lee6c595012019-06-19 10:54:59 +0800271 tx_data = COMMAND_CODE_8;
272
George Hung92a15ba2020-09-14 17:14:52 +0800273 res_int = smbus.SendSmbusRWCmdRAW(busID, NVME_SSD_SLAVE_ADDRESS, &tx_data,
274 sizeof(tx_data), rsp_data_command_8,
275 CODE_8_LENGTH);
Tony Lee6c595012019-06-19 10:54:59 +0800276
277 if (res_int < 0)
278 {
279 if (isErrorSmbus[busID] != true)
280 {
281 log<level::ERR>("Send command code 8 fail!");
282 isErrorSmbus[busID] = true;
283 }
284
285 smbus.smbusClose(busID);
286 nvmeData.present = false;
287 return nvmeData.present;
288 }
289
290 nvmeData.vendor =
291 intToHex(rsp_data_command_8[1]) + " " + intToHex(rsp_data_command_8[2]);
292
George Hung69b96182020-08-20 17:19:56 +0800293 for (auto iter = map_vendor.begin(); iter != map_vendor.end(); iter++)
294 {
295 if (iter->first == nvmeData.vendor)
296 {
297 nvmeData.vendor = iter->second;
298 break;
299 }
300 }
301
Tony Lee6c595012019-06-19 10:54:59 +0800302 for (int offset = SERIALNUMBER_START_INDEX; offset < SERIALNUMBER_END_INDEX;
303 offset++)
304 {
George Hung69b96182020-08-20 17:19:56 +0800305 if (rsp_data_command_8[offset] != ' ')
306 nvmeData.serialNumber +=
307 static_cast<char>(rsp_data_command_8[offset]);
Tony Lee6c595012019-06-19 10:54:59 +0800308 }
309
George Hung92a15ba2020-09-14 17:14:52 +0800310 if (nvmeData.vendor == "Samsung")
311 {
312 unsigned char rsp_data_vpd[I2C_DATA_MAX] = {0};
313 const int rx_len = (MODELNUMBER_END_INDEX - MODELNUMBER_START_INDEX);
314 tx_data = MODELNUMBER_START_INDEX;
315
316 auto res_int =
317 smbus.SendSmbusRWCmdRAW(busID, NVME_SSD_VPD_SLAVE_ADDRESS, &tx_data,
318 sizeof(tx_data), rsp_data_vpd, rx_len);
319
320 if (res_int < 0)
321 {
322 if (isErrorSmbus[busID] != true)
323 {
324 log<level::ERR>("Send command read VPD fail!");
325 isErrorSmbus[busID] = true;
326 }
327
328 smbus.smbusClose(busID);
329 nvmeData.present = false;
330 return nvmeData.present;
331 }
332
333 for (int i = 0; i < rx_len; i++)
334 {
335 if (rsp_data_vpd[i] != ' ')
336 nvmeData.modelNumber += static_cast<char>(rsp_data_vpd[i]);
337 }
338
339 if (nvmeData.modelNumber.substr(0, nvmeData.vendor.size()) == "SAMSUNG")
340 nvmeData.modelNumber.erase(0, nvmeData.vendor.size());
341 }
Tony Lee6c595012019-06-19 10:54:59 +0800342
343 smbus.smbusClose(busID);
344
345 isErrorSmbus[busID] = false;
346
347 return nvmeData.present;
348}
349
Tony Lee84d430c2019-06-13 15:26:15 +0800350void Nvme::run()
351{
Tony Lee89659212019-06-21 17:34:14 +0800352 init();
353
Tony Lee84d430c2019-06-13 15:26:15 +0800354 std::function<void()> callback(std::bind(&Nvme::read, this));
355 try
356 {
357 u_int64_t interval = MONITOR_INTERVAL_SECONDS * 1000000;
358 _timer.restart(std::chrono::microseconds(interval));
359 }
360 catch (const std::exception& e)
361 {
Brandon Kim9b771e22020-10-05 12:02:01 -0700362 log<level::ERR>("Error in polling loop. "), entry("ERROR=%s", e.what());
Tony Lee84d430c2019-06-13 15:26:15 +0800363 }
364}
365
Tony Lee6c595012019-06-19 10:54:59 +0800366/** @brief Parsing NVMe config JSON file */
367Json parseSensorConfig()
Tony Lee84d430c2019-06-13 15:26:15 +0800368{
Tony Lee6c595012019-06-19 10:54:59 +0800369 std::ifstream jsonFile(configFile);
370 if (!jsonFile.is_open())
371 {
372 log<level::ERR>("NVMe config JSON file not found");
373 }
374
375 auto data = Json::parse(jsonFile, nullptr, false);
376 if (data.is_discarded())
377 {
378 log<level::ERR>("NVMe config readings JSON parser failure");
379 }
380
381 return data;
Tony Lee84d430c2019-06-13 15:26:15 +0800382}
383
Tony Lee6c595012019-06-19 10:54:59 +0800384/** @brief Obtain the initial configuration value of NVMe */
385std::vector<phosphor::nvme::Nvme::NVMeConfig> Nvme::getNvmeConfig()
386{
387
388 phosphor::nvme::Nvme::NVMeConfig nvmeConfig;
389 std::vector<phosphor::nvme::Nvme::NVMeConfig> nvmeConfigs;
390 int8_t criticalHigh = 0;
391 int8_t criticalLow = 0;
392 int8_t maxValue = 0;
393 int8_t minValue = 0;
394 int8_t warningHigh = 0;
395 int8_t warningLow = 0;
396
397 try
398 {
399 auto data = parseSensorConfig();
400 static const std::vector<Json> empty{};
401 std::vector<Json> readings = data.value("config", empty);
402 std::vector<Json> thresholds = data.value("threshold", empty);
403 if (!thresholds.empty())
404 {
405 for (const auto& instance : thresholds)
406 {
407 criticalHigh = instance.value("criticalHigh", 0);
408 criticalLow = instance.value("criticalLow", 0);
409 maxValue = instance.value("maxValue", 0);
410 minValue = instance.value("minValue", 0);
411 warningHigh = instance.value("warningHigh", 0);
412 warningLow = instance.value("warningLow", 0);
413 }
414 }
415 else
416 {
417 log<level::ERR>(
418 "Invalid NVMe config file, thresholds dosen't exist");
419 }
420
421 if (!readings.empty())
422 {
423 for (const auto& instance : readings)
424 {
425 uint8_t index = instance.value("NVMeDriveIndex", 0);
426 uint8_t busID = instance.value("NVMeDriveBusID", 0);
Tony Lee89659212019-06-21 17:34:14 +0800427 std::string faultLedGroupPath =
428 instance.value("NVMeDriveFaultLEDGroupPath", "");
429 std::string locateLedGroupPath =
430 instance.value("NVMeDriveLocateLEDGroupPath", "");
George Hung873b5b32020-06-20 14:56:15 +0800431 uint16_t presentPin = instance.value("NVMeDrivePresentPin", 0);
432 uint16_t pwrGoodPin = instance.value("NVMeDrivePwrGoodPin", 0);
Tony Lee89659212019-06-21 17:34:14 +0800433 std::string locateLedControllerBusName =
434 instance.value("NVMeDriveLocateLEDControllerBusName", "");
435 std::string locateLedControllerPath =
436 instance.value("NVMeDriveLocateLEDControllerPath", "");
Tony Lee6c595012019-06-19 10:54:59 +0800437
438 nvmeConfig.index = std::to_string(index);
439 nvmeConfig.busID = busID;
Tony Lee89659212019-06-21 17:34:14 +0800440 nvmeConfig.faultLedGroupPath = faultLedGroupPath;
Tony Lee6c595012019-06-19 10:54:59 +0800441 nvmeConfig.presentPin = presentPin;
442 nvmeConfig.pwrGoodPin = pwrGoodPin;
Tony Lee89659212019-06-21 17:34:14 +0800443 nvmeConfig.locateLedControllerBusName =
444 locateLedControllerBusName;
445 nvmeConfig.locateLedControllerPath = locateLedControllerPath;
446 nvmeConfig.locateLedGroupPath = locateLedGroupPath;
Tony Lee6c595012019-06-19 10:54:59 +0800447 nvmeConfig.criticalHigh = criticalHigh;
448 nvmeConfig.criticalLow = criticalLow;
449 nvmeConfig.warningHigh = warningHigh;
450 nvmeConfig.warningLow = warningLow;
451 nvmeConfig.maxValue = maxValue;
452 nvmeConfig.minValue = minValue;
453 nvmeConfigs.push_back(nvmeConfig);
454 }
455 }
456 else
457 {
458 log<level::ERR>("Invalid NVMe config file, config dosen't exist");
459 }
460 }
461 catch (const Json::exception& e)
462 {
Brandon Kim9b771e22020-10-05 12:02:01 -0700463 log<level::ERR>("Json Exception caught."), entry("MSG=%s", e.what());
Tony Lee6c595012019-06-19 10:54:59 +0800464 }
465
466 return nvmeConfigs;
467}
468
469std::string Nvme::getGPIOValueOfNvme(const std::string& fullPath)
470{
471 std::string val;
472 std::ifstream ifs;
473 auto retries = 3;
474
475 while (retries != 0)
476 {
477 try
478 {
479 if (!ifs.is_open())
480 ifs.open(fullPath);
481 ifs.clear();
482 ifs.seekg(0);
483 ifs >> val;
484 }
485 catch (const std::exception& e)
486 {
487 --retries;
488 std::this_thread::sleep_for(delay);
489 log<level::ERR>("Can not open gpio path.",
Brandon Kim9b771e22020-10-05 12:02:01 -0700490 entry("MSG=%s", e.what()));
Tony Lee6c595012019-06-19 10:54:59 +0800491 continue;
492 }
493 break;
494 }
495
496 ifs.close();
497 return val;
498}
499
Tony Lee89659212019-06-21 17:34:14 +0800500void Nvme::createNVMeInventory()
501{
Patrick Williams05eedaa2020-05-13 17:58:42 -0500502 using Properties = std::map<std::string, std::variant<std::string, bool>>;
Tony Lee89659212019-06-21 17:34:14 +0800503 using Interfaces = std::map<std::string, Properties>;
504
505 std::string inventoryPath;
506 std::map<sdbusplus::message::object_path, Interfaces> obj;
507
George Hung831f2042021-05-19 17:02:29 +0800508 for (const auto& config : configs)
Tony Lee89659212019-06-21 17:34:14 +0800509 {
510 inventoryPath = "/system/chassis/motherboard/nvme" + config.index;
511
512 obj = {{
513 inventoryPath,
514 {{ITEM_IFACE, {}}, {NVME_STATUS_IFACE, {}}, {ASSET_IFACE, {}}},
515 }};
516 util::SDBusPlus::CallMethod(bus, INVENTORY_BUSNAME, INVENTORY_NAMESPACE,
517 INVENTORY_MANAGER_IFACE, "Notify", obj);
518 }
519}
520
521void Nvme::init()
522{
523 createNVMeInventory();
524}
525
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700526void Nvme::readNvmeData(NVMeConfig& config)
527{
528 std::string inventoryPath = NVME_INVENTORY_PATH + config.index;
529 NVMeData nvmeData;
530
531 // get NVMe information through i2c by busID.
532 auto success = getNVMeInfobyBusID(config.busID, nvmeData);
533 auto iter = nvmes.find(config.index);
534
535 // can not find. create dbus
536 if (iter == nvmes.end())
537 {
Brandon Kim9b771e22020-10-05 12:02:01 -0700538 log<level::INFO>("SSD plug.", entry("INDEX=%s", config.index.c_str()));
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700539
540 std::string objPath = NVME_OBJ_PATH + config.index;
541 auto nvmeSSD =
542 std::make_shared<phosphor::nvme::NvmeSSD>(bus, objPath.c_str());
543 nvmes.emplace(config.index, nvmeSSD);
544
545 setNvmeInventoryProperties(true, nvmeData, inventoryPath);
546 nvmeSSD->setSensorValueToDbus(nvmeData.sensorValue);
George Hung93455332021-01-06 20:57:04 +0800547 if (nvmeData.wcTemp != 0)
548 {
549 config.criticalHigh = nvmeData.wcTemp;
550 config.warningHigh = nvmeData.wcTemp;
551 }
George Hung831f2042021-05-19 17:02:29 +0800552 nvmeSSD->setSensorMaxMin(config.maxValue, config.minValue);
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700553 nvmeSSD->setSensorThreshold(config.criticalHigh, config.criticalLow,
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700554 config.warningHigh, config.warningLow);
555
556 nvmeSSD->checkSensorThreshold();
557 setLEDsStatus(config, success, nvmeData);
558 }
559 else
560 {
561 setNvmeInventoryProperties(true, nvmeData, inventoryPath);
562 iter->second->setSensorValueToDbus(nvmeData.sensorValue);
George Hung831f2042021-05-19 17:02:29 +0800563 if (nvmeData.wcTemp != 0)
564 {
565 iter->second->setSensorThreshold(
566 config.criticalHigh, config.criticalLow, config.warningHigh,
567 config.warningLow);
568 }
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700569 iter->second->checkSensorThreshold();
570 setLEDsStatus(config, success, nvmeData);
571 }
572}
573
Tony Lee6c595012019-06-19 10:54:59 +0800574/** @brief Monitor NVMe drives every one second */
Tony Lee84d430c2019-06-13 15:26:15 +0800575void Nvme::read()
576{
Tony Lee6c595012019-06-19 10:54:59 +0800577 std::string devPresentPath;
578 std::string devPwrGoodPath;
Tony Lee89659212019-06-21 17:34:14 +0800579 std::string inventoryPath;
Tony Lee6c595012019-06-19 10:54:59 +0800580
581 static std::unordered_map<std::string, bool> isErrorPower;
582
583 for (auto config : configs)
584 {
585 NVMeData nvmeData;
Tony Lee6c595012019-06-19 10:54:59 +0800586
Tony Lee89659212019-06-21 17:34:14 +0800587 inventoryPath = NVME_INVENTORY_PATH + config.index;
588
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700589 if (config.presentPin)
Tony Lee6c595012019-06-19 10:54:59 +0800590 {
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700591 devPresentPath =
592 GPIO_BASE_PATH + std::to_string(config.presentPin) + "/value";
593
594 if (getGPIOValueOfNvme(devPresentPath) != IS_PRESENT)
Tony Lee6c595012019-06-19 10:54:59 +0800595 {
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700596 // Drive not present, remove nvme d-bus path ,
597 // clean all properties in inventory
598 // and turn off fault and locate LED
Tony Lee89659212019-06-21 17:34:14 +0800599
600 setFaultLED(config.locateLedGroupPath, config.faultLedGroupPath,
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700601 false);
Tony Lee89659212019-06-21 17:34:14 +0800602 setLocateLED(config.locateLedGroupPath,
603 config.locateLedControllerBusName,
604 config.locateLedControllerPath, false);
605
Tony Lee6c595012019-06-19 10:54:59 +0800606 nvmeData = NVMeData();
Tony Lee89659212019-06-21 17:34:14 +0800607 setNvmeInventoryProperties(false, nvmeData, inventoryPath);
Tony Lee6c595012019-06-19 10:54:59 +0800608 nvmes.erase(config.index);
George Hung188ad952020-06-20 16:28:06 +0800609 continue;
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700610 }
611 else if (config.pwrGoodPin)
612 {
613 devPwrGoodPath = GPIO_BASE_PATH +
614 std::to_string(config.pwrGoodPin) + "/value";
Tony Lee6c595012019-06-19 10:54:59 +0800615
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700616 if (getGPIOValueOfNvme(devPwrGoodPath) != POWERGD)
Tony Lee6c595012019-06-19 10:54:59 +0800617 {
George Hung95b90f42021-01-11 21:18:59 +0800618 // IFDET should be used to provide the final say
619 // in SSD's presence - IFDET showing SSD is present
620 // but the power is off (if the drive is plugged in)
621 // is a valid state.
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700622
623 setFaultLED(config.locateLedGroupPath,
624 config.faultLedGroupPath, true);
625 setLocateLED(config.locateLedGroupPath,
626 config.locateLedControllerBusName,
627 config.locateLedControllerPath, false);
628
629 nvmeData = NVMeData();
George Hung95b90f42021-01-11 21:18:59 +0800630 setNvmeInventoryProperties(true, nvmeData, inventoryPath);
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700631
632 if (isErrorPower[config.index] != true)
633 {
634 log<level::ERR>(
635 "Present pin is true but power good pin is false.",
Brandon Kim9b771e22020-10-05 12:02:01 -0700636 entry("INDEX=%s", config.index.c_str()));
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700637 log<level::ERR>(
638 "Erase SSD from map and d-bus.",
Brandon Kim9b771e22020-10-05 12:02:01 -0700639 entry("INDEX=%s", config.index.c_str()));
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700640
641 isErrorPower[config.index] = true;
642 }
George Hung95b90f42021-01-11 21:18:59 +0800643
644 // Keep reading to report the invalid temperature
645 // (To make thermal loop know that the sensor reading
646 // is invalid).
647 readNvmeData(config);
George Hung188ad952020-06-20 16:28:06 +0800648 continue;
Tony Lee6c595012019-06-19 10:54:59 +0800649 }
650 }
651 }
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700652 // Drive status is good, update value or create d-bus and update
653 // value.
654 readNvmeData(config);
Tony Lee89659212019-06-21 17:34:14 +0800655
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700656 isErrorPower[config.index] = false;
Tony Lee6c595012019-06-19 10:54:59 +0800657 }
Tony Lee84d430c2019-06-13 15:26:15 +0800658}
659} // namespace nvme
660} // namespace phosphor