blob: 74bacccfbc38635222139b7a63b794e6c006cb16 [file] [log] [blame]
Tony Lee6c595012019-06-19 10:54:59 +08001#pragma once
2
Tony Lee84d430c2019-06-13 15:26:15 +08003#include "config.h"
4
Tony Lee6c595012019-06-19 10:54:59 +08005#include "nvmes.hpp"
Tony Lee89659212019-06-21 17:34:14 +08006#include "sdbusplus.hpp"
Tony Lee6c595012019-06-19 10:54:59 +08007
Tony Lee84d430c2019-06-13 15:26:15 +08008#include <sdbusplus/bus.hpp>
9#include <sdbusplus/server.hpp>
10#include <sdbusplus/server/object.hpp>
11#include <sdeventplus/clock.hpp>
12#include <sdeventplus/event.hpp>
13#include <sdeventplus/utility/timer.hpp>
14
Patrick Williams7da98582023-05-10 07:50:46 -050015#include <fstream>
16
Tony Lee84d430c2019-06-13 15:26:15 +080017namespace phosphor
18{
19namespace nvme
20{
21
Tony Lee6c595012019-06-19 10:54:59 +080022/** @class Nvme
23 * @brief Nvme manager implementation.
24 */
Tony Lee84d430c2019-06-13 15:26:15 +080025class Nvme
26{
27 public:
28 Nvme() = delete;
29 Nvme(const Nvme&) = delete;
30 Nvme& operator=(const Nvme&) = delete;
31 Nvme(Nvme&&) = delete;
32 Nvme& operator=(Nvme&&) = delete;
33
Tony Lee6c595012019-06-19 10:54:59 +080034 /** @brief Constructs Nvme
35 *
36 * @param[in] bus - Handle to system dbus
Tony Lee89659212019-06-21 17:34:14 +080037 * @param[in] objPath - The dbus path of nvme
Tony Lee6c595012019-06-19 10:54:59 +080038 */
Patrick Williams7ce68bc2022-07-22 19:26:55 -050039 Nvme(sdbusplus::bus_t& bus) :
Tony Lee84d430c2019-06-13 15:26:15 +080040 bus(bus), _event(sdeventplus::Event::get_default()),
41 _timer(_event, std::bind(&Nvme::read, this))
42 {
Tony Lee6c595012019-06-19 10:54:59 +080043 // read json file
44 configs = getNvmeConfig();
Tony Lee84d430c2019-06-13 15:26:15 +080045 }
46
Tony Lee6c595012019-06-19 10:54:59 +080047 /**
48 * Structure for keeping nvme configure data required by nvme monitoring
49 */
50 struct NVMeConfig
51 {
52 std::string index;
53 uint8_t busID;
Tony Lee89659212019-06-21 17:34:14 +080054 std::string faultLedGroupPath;
George Hung873b5b32020-06-20 14:56:15 +080055 uint16_t presentPin;
56 uint16_t pwrGoodPin;
Tony Lee89659212019-06-21 17:34:14 +080057 std::string locateLedControllerBusName;
58 std::string locateLedControllerPath;
59 std::string locateLedGroupPath;
Tony Lee6c595012019-06-19 10:54:59 +080060 int8_t criticalHigh;
61 int8_t criticalLow;
62 int8_t maxValue;
63 int8_t minValue;
64 int8_t warningHigh;
65 int8_t warningLow;
66 };
67
68 /**
69 * Structure for keeping nvme data required by nvme monitoring
70 */
71 struct NVMeData
72 {
73 bool present; /* Whether or not the nvme is present */
74 std::string vendor; /* The nvme manufacturer */
75 std::string serialNumber; /* The nvme serial number */
George Hung92a15ba2020-09-14 17:14:52 +080076 std::string modelNumber; /* The nvme model number */
Tony Lee6c595012019-06-19 10:54:59 +080077 std::string smartWarnings; /* Indicates smart warnings for the state */
78 std::string statusFlags; /* Indicates the status of the drives */
79 std::string
80 driveLifeUsed; /* A vendor specific estimate of the percentage */
81 int8_t sensorValue; /* Sensor value, if sensor value didn't be
82 update, means sensor failure, default set to
83 129(0x81) accroding to NVMe-MI SPEC*/
George Hung93455332021-01-06 20:57:04 +080084 int8_t wcTemp; /* Indicates over temperature warning threshold.
85 This is intended to initially match the temperature
86 reported in the WCTEMP field in the NVMe Identify
87 Controller data structure */
Tony Lee6c595012019-06-19 10:54:59 +080088 };
89
90 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
91 * event loop.
92 */
Tony Lee84d430c2019-06-13 15:26:15 +080093 void run();
94
Tony Lee6c595012019-06-19 10:54:59 +080095 /** @brief Get GPIO value of nvme by sysfs */
96 std::string getGPIOValueOfNvme(const std::string& fullPath);
97 /** @brief Map of the object NvmeSSD */
98 std::unordered_map<std::string, std::shared_ptr<phosphor::nvme::NvmeSSD>>
99 nvmes;
Tony Lee84d430c2019-06-13 15:26:15 +0800100
Tony Lee89659212019-06-21 17:34:14 +0800101 /** @brief Set locate and fault LED status of SSD
102 *
103 * @param[in] config - Nvme configure data
104 * @param[in] success - Success or not that get NVMe Info by SMbus
105 * @param[in] nvmeData - Nvme information
106 */
107 void setLEDsStatus(const phosphor::nvme::Nvme::NVMeConfig& config,
108 bool success,
109 const phosphor::nvme::Nvme::NVMeData& nvmeData);
110
111 /** @brief Set SSD fault LED status */
112 void setFaultLED(const std::string& locateLedGroupPath,
113 const std::string& ledPath, bool request);
114 /** @brief Set SSD locate LED status */
115 void setLocateLED(const std::string& ledPath,
116 const std::string& locateLedBusName,
117 const std::string& locateLedPath, bool ispresent);
118 /** @brief Get Identify State*/
119 bool getLEDGroupState(const std::string& ledPath);
120
121 /** @brief Set inventory properties of nvme */
122 void setNvmeInventoryProperties(
Chanh Nguyene528b0a2021-07-14 16:57:57 +0700123 NVMeConfig& config, bool present,
124 const phosphor::nvme::Nvme::NVMeData& nvmeData,
Tony Lee89659212019-06-21 17:34:14 +0800125 const std::string& inventoryPath);
126
127 void createNVMeInventory();
128
George Hung5e23bcd2021-07-01 12:16:11 +0800129 /** @brief read NVMe information via Smbus */
130 bool getNVMeInfobyBusID(int busID,
131 phosphor::nvme::Nvme::NVMeData& nvmeData);
132
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700133 /** @brief read and update NVME data to dbus */
Potin Lai03144732023-02-14 22:20:38 +0800134 void readNvmeData(NVMeConfig& config, bool isPwrGood);
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700135
Tony Lee6c595012019-06-19 10:54:59 +0800136 private:
137 /** @brief sdbusplus bus client connection. */
Patrick Williams7ce68bc2022-07-22 19:26:55 -0500138 sdbusplus::bus_t& bus;
Tony Lee6c595012019-06-19 10:54:59 +0800139 /** @brief the Event Loop structure */
Tony Lee84d430c2019-06-13 15:26:15 +0800140 sdeventplus::Event _event;
141 /** @brief Read Timer */
142 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
143
Tony Lee6c595012019-06-19 10:54:59 +0800144 std::vector<phosphor::nvme::Nvme::NVMeConfig> configs;
145
George Hung5e23bcd2021-07-01 12:16:11 +0800146 /** @brief error status for Smbus */
147 std::unordered_map<int, bool> isErrorSmbus;
148 /** @brief error status for LED */
149 std::unordered_map<std::string, bool> isError;
150 /** @brief error status for NVMe power*/
151 std::unordered_map<std::string, bool> isErrorPower;
152
Tony Lee6c595012019-06-19 10:54:59 +0800153 /** @brief Set up initial configuration value of NVMe */
Tony Lee84d430c2019-06-13 15:26:15 +0800154 void init();
Tony Lee6c595012019-06-19 10:54:59 +0800155 /** @brief Monitor NVMe drives every one second */
Tony Lee84d430c2019-06-13 15:26:15 +0800156 void read();
Tony Lee6c595012019-06-19 10:54:59 +0800157
158 std::vector<phosphor::nvme::Nvme::NVMeConfig> getNvmeConfig();
Potin Laibcae0872022-10-20 15:29:17 +0800159
160 /** @brief Monitor interval in second */
161 size_t monitorIntervalSec;
Potin Lai6e149e92022-11-23 10:04:05 +0800162 /** @brief Maximum smbus error retry */
163 uint16_t maxSmbusErrorRetry;
164 /** @brief Map of each NVMe smbus error count */
165 std::unordered_map<int, uint16_t> nvmeSmbusErrCnt;
Tony Lee84d430c2019-06-13 15:26:15 +0800166};
167} // namespace nvme
Tony Lee6c595012019-06-19 10:54:59 +0800168} // namespace phosphor