blob: 7017475a60448edb85c6ec7b54242d4cbfea7913 [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*/
83 };
84
85 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
86 * event loop.
87 */
Tony Lee84d430c2019-06-13 15:26:15 +080088 void run();
89
Tony Lee6c595012019-06-19 10:54:59 +080090 /** @brief Get GPIO value of nvme by sysfs */
91 std::string getGPIOValueOfNvme(const std::string& fullPath);
92 /** @brief Map of the object NvmeSSD */
93 std::unordered_map<std::string, std::shared_ptr<phosphor::nvme::NvmeSSD>>
94 nvmes;
Tony Lee84d430c2019-06-13 15:26:15 +080095
Tony Lee89659212019-06-21 17:34:14 +080096 /** @brief Set locate and fault LED status of SSD
97 *
98 * @param[in] config - Nvme configure data
99 * @param[in] success - Success or not that get NVMe Info by SMbus
100 * @param[in] nvmeData - Nvme information
101 */
102 void setLEDsStatus(const phosphor::nvme::Nvme::NVMeConfig& config,
103 bool success,
104 const phosphor::nvme::Nvme::NVMeData& nvmeData);
105
106 /** @brief Set SSD fault LED status */
107 void setFaultLED(const std::string& locateLedGroupPath,
108 const std::string& ledPath, bool request);
109 /** @brief Set SSD locate LED status */
110 void setLocateLED(const std::string& ledPath,
111 const std::string& locateLedBusName,
112 const std::string& locateLedPath, bool ispresent);
113 /** @brief Get Identify State*/
114 bool getLEDGroupState(const std::string& ledPath);
115
116 /** @brief Set inventory properties of nvme */
117 void setNvmeInventoryProperties(
118 bool present, const phosphor::nvme::Nvme::NVMeData& nvmeData,
119 const std::string& inventoryPath);
120
121 void createNVMeInventory();
122
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700123 /** @brief read and update NVME data to dbus */
124 void readNvmeData(NVMeConfig& config);
125
Tony Lee6c595012019-06-19 10:54:59 +0800126 private:
127 /** @brief sdbusplus bus client connection. */
128 sdbusplus::bus::bus& bus;
129 /** @brief the Event Loop structure */
Tony Lee84d430c2019-06-13 15:26:15 +0800130 sdeventplus::Event _event;
131 /** @brief Read Timer */
132 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
133
Tony Lee6c595012019-06-19 10:54:59 +0800134 std::vector<phosphor::nvme::Nvme::NVMeConfig> configs;
135
136 /** @brief Set up initial configuration value of NVMe */
Tony Lee84d430c2019-06-13 15:26:15 +0800137 void init();
Tony Lee6c595012019-06-19 10:54:59 +0800138 /** @brief Monitor NVMe drives every one second */
Tony Lee84d430c2019-06-13 15:26:15 +0800139 void read();
Tony Lee6c595012019-06-19 10:54:59 +0800140
141 std::vector<phosphor::nvme::Nvme::NVMeConfig> getNvmeConfig();
Tony Lee84d430c2019-06-13 15:26:15 +0800142};
143} // namespace nvme
Tony Lee6c595012019-06-19 10:54:59 +0800144} // namespace phosphor