blob: 78eabe395b9ea5aee5ff287a5acd27b17a6f635a [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
8#include <fstream>
Tony Lee84d430c2019-06-13 15:26:15 +08009#include <sdbusplus/bus.hpp>
10#include <sdbusplus/server.hpp>
11#include <sdbusplus/server/object.hpp>
12#include <sdeventplus/clock.hpp>
13#include <sdeventplus/event.hpp>
14#include <sdeventplus/utility/timer.hpp>
15
16namespace phosphor
17{
18namespace nvme
19{
20
Tony Lee6c595012019-06-19 10:54:59 +080021/** @class Nvme
22 * @brief Nvme manager implementation.
23 */
Tony Lee84d430c2019-06-13 15:26:15 +080024class Nvme
25{
26 public:
27 Nvme() = delete;
28 Nvme(const Nvme&) = delete;
29 Nvme& operator=(const Nvme&) = delete;
30 Nvme(Nvme&&) = delete;
31 Nvme& operator=(Nvme&&) = delete;
32
Tony Lee6c595012019-06-19 10:54:59 +080033 /** @brief Constructs Nvme
34 *
35 * @param[in] bus - Handle to system dbus
Tony Lee89659212019-06-21 17:34:14 +080036 * @param[in] objPath - The dbus path of nvme
Tony Lee6c595012019-06-19 10:54:59 +080037 */
Tony Lee84d430c2019-06-13 15:26:15 +080038 Nvme(sdbusplus::bus::bus& bus) :
39 bus(bus), _event(sdeventplus::Event::get_default()),
40 _timer(_event, std::bind(&Nvme::read, this))
41 {
Tony Lee6c595012019-06-19 10:54:59 +080042 // read json file
43 configs = getNvmeConfig();
Tony Lee84d430c2019-06-13 15:26:15 +080044 }
45
Tony Lee6c595012019-06-19 10:54:59 +080046 /**
47 * Structure for keeping nvme configure data required by nvme monitoring
48 */
49 struct NVMeConfig
50 {
51 std::string index;
52 uint8_t busID;
Tony Lee89659212019-06-21 17:34:14 +080053 std::string faultLedGroupPath;
George Hung873b5b32020-06-20 14:56:15 +080054 uint16_t presentPin;
55 uint16_t pwrGoodPin;
Tony Lee89659212019-06-21 17:34:14 +080056 std::string locateLedControllerBusName;
57 std::string locateLedControllerPath;
58 std::string locateLedGroupPath;
Tony Lee6c595012019-06-19 10:54:59 +080059 int8_t criticalHigh;
60 int8_t criticalLow;
61 int8_t maxValue;
62 int8_t minValue;
63 int8_t warningHigh;
64 int8_t warningLow;
65 };
66
67 /**
68 * Structure for keeping nvme data required by nvme monitoring
69 */
70 struct NVMeData
71 {
72 bool present; /* Whether or not the nvme is present */
73 std::string vendor; /* The nvme manufacturer */
74 std::string serialNumber; /* The nvme serial number */
George Hung92a15ba2020-09-14 17:14:52 +080075 std::string modelNumber; /* The nvme model number */
Tony Lee6c595012019-06-19 10:54:59 +080076 std::string smartWarnings; /* Indicates smart warnings for the state */
77 std::string statusFlags; /* Indicates the status of the drives */
78 std::string
79 driveLifeUsed; /* A vendor specific estimate of the percentage */
80 int8_t sensorValue; /* Sensor value, if sensor value didn't be
81 update, means sensor failure, default set to
82 129(0x81) accroding to NVMe-MI SPEC*/
George Hung93455332021-01-06 20:57:04 +080083 int8_t wcTemp; /* Indicates over temperature warning threshold.
84 This is intended to initially match the temperature
85 reported in the WCTEMP field in the NVMe Identify
86 Controller data structure */
Tony Lee6c595012019-06-19 10:54:59 +080087 };
88
89 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
90 * event loop.
91 */
Tony Lee84d430c2019-06-13 15:26:15 +080092 void run();
93
Tony Lee6c595012019-06-19 10:54:59 +080094 /** @brief Get GPIO value of nvme by sysfs */
95 std::string getGPIOValueOfNvme(const std::string& fullPath);
96 /** @brief Map of the object NvmeSSD */
97 std::unordered_map<std::string, std::shared_ptr<phosphor::nvme::NvmeSSD>>
98 nvmes;
Tony Lee84d430c2019-06-13 15:26:15 +080099
Tony Lee89659212019-06-21 17:34:14 +0800100 /** @brief Set locate and fault LED status of SSD
101 *
102 * @param[in] config - Nvme configure data
103 * @param[in] success - Success or not that get NVMe Info by SMbus
104 * @param[in] nvmeData - Nvme information
105 */
106 void setLEDsStatus(const phosphor::nvme::Nvme::NVMeConfig& config,
107 bool success,
108 const phosphor::nvme::Nvme::NVMeData& nvmeData);
109
110 /** @brief Set SSD fault LED status */
111 void setFaultLED(const std::string& locateLedGroupPath,
112 const std::string& ledPath, bool request);
113 /** @brief Set SSD locate LED status */
114 void setLocateLED(const std::string& ledPath,
115 const std::string& locateLedBusName,
116 const std::string& locateLedPath, bool ispresent);
117 /** @brief Get Identify State*/
118 bool getLEDGroupState(const std::string& ledPath);
119
120 /** @brief Set inventory properties of nvme */
121 void setNvmeInventoryProperties(
122 bool present, const phosphor::nvme::Nvme::NVMeData& nvmeData,
123 const std::string& inventoryPath);
124
125 void createNVMeInventory();
126
George Hung5e23bcd2021-07-01 12:16:11 +0800127 /** @brief read NVMe information via Smbus */
128 bool getNVMeInfobyBusID(int busID,
129 phosphor::nvme::Nvme::NVMeData& nvmeData);
130
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700131 /** @brief read and update NVME data to dbus */
132 void readNvmeData(NVMeConfig& config);
133
Tony Lee6c595012019-06-19 10:54:59 +0800134 private:
135 /** @brief sdbusplus bus client connection. */
136 sdbusplus::bus::bus& bus;
137 /** @brief the Event Loop structure */
Tony Lee84d430c2019-06-13 15:26:15 +0800138 sdeventplus::Event _event;
139 /** @brief Read Timer */
140 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
141
Tony Lee6c595012019-06-19 10:54:59 +0800142 std::vector<phosphor::nvme::Nvme::NVMeConfig> configs;
143
George Hung5e23bcd2021-07-01 12:16:11 +0800144 /** @brief error status for Smbus */
145 std::unordered_map<int, bool> isErrorSmbus;
146 /** @brief error status for LED */
147 std::unordered_map<std::string, bool> isError;
148 /** @brief error status for NVMe power*/
149 std::unordered_map<std::string, bool> isErrorPower;
150
Tony Lee6c595012019-06-19 10:54:59 +0800151 /** @brief Set up initial configuration value of NVMe */
Tony Lee84d430c2019-06-13 15:26:15 +0800152 void init();
Tony Lee6c595012019-06-19 10:54:59 +0800153 /** @brief Monitor NVMe drives every one second */
Tony Lee84d430c2019-06-13 15:26:15 +0800154 void read();
Tony Lee6c595012019-06-19 10:54:59 +0800155
156 std::vector<phosphor::nvme::Nvme::NVMeConfig> getNvmeConfig();
Tony Lee84d430c2019-06-13 15:26:15 +0800157};
158} // namespace nvme
Tony Lee6c595012019-06-19 10:54:59 +0800159} // namespace phosphor