blob: 2b0e27d56d4f19cc6290528b1d85c006a1165773 [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"
6
7#include <fstream>
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
15namespace phosphor
16{
17namespace nvme
18{
19
Tony Lee6c595012019-06-19 10:54:59 +080020/** @class Nvme
21 * @brief Nvme manager implementation.
22 */
Tony Lee84d430c2019-06-13 15:26:15 +080023class Nvme
24{
25 public:
26 Nvme() = delete;
27 Nvme(const Nvme&) = delete;
28 Nvme& operator=(const Nvme&) = delete;
29 Nvme(Nvme&&) = delete;
30 Nvme& operator=(Nvme&&) = delete;
31
Tony Lee6c595012019-06-19 10:54:59 +080032 /** @brief Constructs Nvme
33 *
34 * @param[in] bus - Handle to system dbus
35 * @param[in] objPath - The Dbus path of nvme
36 */
Tony Lee84d430c2019-06-13 15:26:15 +080037 Nvme(sdbusplus::bus::bus& bus) :
38 bus(bus), _event(sdeventplus::Event::get_default()),
39 _timer(_event, std::bind(&Nvme::read, this))
40 {
Tony Lee6c595012019-06-19 10:54:59 +080041 // read json file
42 configs = getNvmeConfig();
Tony Lee84d430c2019-06-13 15:26:15 +080043 }
44
Tony Lee6c595012019-06-19 10:54:59 +080045 /**
46 * Structure for keeping nvme configure data required by nvme monitoring
47 */
48 struct NVMeConfig
49 {
50 std::string index;
51 uint8_t busID;
52 uint8_t presentPin;
53 uint8_t pwrGoodPin;
54 int8_t criticalHigh;
55 int8_t criticalLow;
56 int8_t maxValue;
57 int8_t minValue;
58 int8_t warningHigh;
59 int8_t warningLow;
60 };
61
62 /**
63 * Structure for keeping nvme data required by nvme monitoring
64 */
65 struct NVMeData
66 {
67 bool present; /* Whether or not the nvme is present */
68 std::string vendor; /* The nvme manufacturer */
69 std::string serialNumber; /* The nvme serial number */
70 std::string smartWarnings; /* Indicates smart warnings for the state */
71 std::string statusFlags; /* Indicates the status of the drives */
72 std::string
73 driveLifeUsed; /* A vendor specific estimate of the percentage */
74 int8_t sensorValue; /* Sensor value, if sensor value didn't be
75 update, means sensor failure, default set to
76 129(0x81) accroding to NVMe-MI SPEC*/
77 };
78
79 /** @brief Setup polling timer in a sd event loop and attach to D-Bus
80 * event loop.
81 */
Tony Lee84d430c2019-06-13 15:26:15 +080082 void run();
83
Tony Lee6c595012019-06-19 10:54:59 +080084 /** @brief Get GPIO value of nvme by sysfs */
85 std::string getGPIOValueOfNvme(const std::string& fullPath);
86 /** @brief Map of the object NvmeSSD */
87 std::unordered_map<std::string, std::shared_ptr<phosphor::nvme::NvmeSSD>>
88 nvmes;
Tony Lee84d430c2019-06-13 15:26:15 +080089
Tony Lee6c595012019-06-19 10:54:59 +080090 private:
91 /** @brief sdbusplus bus client connection. */
92 sdbusplus::bus::bus& bus;
93 /** @brief the Event Loop structure */
Tony Lee84d430c2019-06-13 15:26:15 +080094 sdeventplus::Event _event;
95 /** @brief Read Timer */
96 sdeventplus::utility::Timer<sdeventplus::ClockId::Monotonic> _timer;
97
Tony Lee6c595012019-06-19 10:54:59 +080098 std::vector<phosphor::nvme::Nvme::NVMeConfig> configs;
99
100 /** @brief Set up initial configuration value of NVMe */
Tony Lee84d430c2019-06-13 15:26:15 +0800101 void init();
Tony Lee6c595012019-06-19 10:54:59 +0800102 /** @brief Monitor NVMe drives every one second */
Tony Lee84d430c2019-06-13 15:26:15 +0800103 void read();
Tony Lee6c595012019-06-19 10:54:59 +0800104
105 std::vector<phosphor::nvme::Nvme::NVMeConfig> getNvmeConfig();
Tony Lee84d430c2019-06-13 15:26:15 +0800106};
107} // namespace nvme
Tony Lee6c595012019-06-19 10:54:59 +0800108} // namespace phosphor