blob: 2a68a0a8cb4720145327f7efcec13b770d62c19f [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;
Tony Lee6c595012019-06-19 10:54:59 +080054 uint8_t presentPin;
55 uint8_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 */
75 std::string smartWarnings; /* Indicates smart warnings for the state */
76 std::string statusFlags; /* Indicates the status of the drives */
77 std::string
78 driveLifeUsed; /* A vendor specific estimate of the percentage */
79 int8_t sensorValue; /* Sensor value, if sensor value didn't be
80 update, means sensor failure, default set to
81 129(0x81) accroding to NVMe-MI SPEC*/
82 };
83
84 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
85 * event loop.
86 */
Tony Lee84d430c2019-06-13 15:26:15 +080087 void run();
88
Tony Lee6c595012019-06-19 10:54:59 +080089 /** @brief Get GPIO value of nvme by sysfs */
90 std::string getGPIOValueOfNvme(const std::string& fullPath);
91 /** @brief Map of the object NvmeSSD */
92 std::unordered_map<std::string, std::shared_ptr<phosphor::nvme::NvmeSSD>>
93 nvmes;
Tony Lee84d430c2019-06-13 15:26:15 +080094
Tony Lee89659212019-06-21 17:34:14 +080095 /** @brief Set locate and fault LED status of SSD
96 *
97 * @param[in] config - Nvme configure data
98 * @param[in] success - Success or not that get NVMe Info by SMbus
99 * @param[in] nvmeData - Nvme information
100 */
101 void setLEDsStatus(const phosphor::nvme::Nvme::NVMeConfig& config,
102 bool success,
103 const phosphor::nvme::Nvme::NVMeData& nvmeData);
104
105 /** @brief Set SSD fault LED status */
106 void setFaultLED(const std::string& locateLedGroupPath,
107 const std::string& ledPath, bool request);
108 /** @brief Set SSD locate LED status */
109 void setLocateLED(const std::string& ledPath,
110 const std::string& locateLedBusName,
111 const std::string& locateLedPath, bool ispresent);
112 /** @brief Get Identify State*/
113 bool getLEDGroupState(const std::string& ledPath);
114
115 /** @brief Set inventory properties of nvme */
116 void setNvmeInventoryProperties(
117 bool present, const phosphor::nvme::Nvme::NVMeData& nvmeData,
118 const std::string& inventoryPath);
119
120 void createNVMeInventory();
121
Vijay Khemkae41b2e42020-04-21 09:23:10 -0700122 /** @brief read and update NVME data to dbus */
123 void readNvmeData(NVMeConfig& config);
124
Tony Lee6c595012019-06-19 10:54:59 +0800125 private:
126 /** @brief sdbusplus bus client connection. */
127 sdbusplus::bus::bus& bus;
128 /** @brief the Event Loop structure */
Tony Lee84d430c2019-06-13 15:26:15 +0800129 sdeventplus::Event _event;
130 /** @brief Read Timer */
131 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
132
Tony Lee6c595012019-06-19 10:54:59 +0800133 std::vector<phosphor::nvme::Nvme::NVMeConfig> configs;
134
135 /** @brief Set up initial configuration value of NVMe */
Tony Lee84d430c2019-06-13 15:26:15 +0800136 void init();
Tony Lee6c595012019-06-19 10:54:59 +0800137 /** @brief Monitor NVMe drives every one second */
Tony Lee84d430c2019-06-13 15:26:15 +0800138 void read();
Tony Lee6c595012019-06-19 10:54:59 +0800139
140 std::vector<phosphor::nvme::Nvme::NVMeConfig> getNvmeConfig();
Tony Lee84d430c2019-06-13 15:26:15 +0800141};
142} // namespace nvme
Tony Lee6c595012019-06-19 10:54:59 +0800143} // namespace phosphor