James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 1 | #pragma once |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 2 | #include <boost/asio/steady_timer.hpp> |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 3 | #include <boost/container/flat_map.hpp> |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 4 | #include <sensor.hpp> |
James Feist | 38fb598 | 2020-05-28 10:09:54 -0700 | [diff] [blame] | 5 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 6 | #include <chrono> |
| 7 | #include <limits> |
Patrick Venture | fd6ba73 | 2019-10-31 14:27:39 -0700 | [diff] [blame] | 8 | #include <memory> |
| 9 | #include <optional> |
| 10 | #include <string> |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 11 | #include <vector> |
| 12 | |
Ed Tanous | 828c5a6 | 2024-02-09 16:59:22 -0800 | [diff] [blame] | 13 | constexpr const char* sensorType = "IpmbSensor"; |
| 14 | constexpr const char* sdrInterface = "IpmbDevice"; |
| 15 | |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 16 | enum class IpmbType |
| 17 | { |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 18 | none, |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 19 | meSensor, |
| 20 | PXE1410CVR, |
| 21 | IR38363VR, |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 22 | ADM1278HSC, |
Rebecca Cran | 394f0c5 | 2023-12-17 20:08:55 -0700 | [diff] [blame] | 23 | mpsVR, |
| 24 | SMPro |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 25 | }; |
| 26 | |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 27 | enum class IpmbSubType |
| 28 | { |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 29 | none, |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 30 | temp, |
| 31 | curr, |
| 32 | power, |
Adrian Ambrożewicz | 45e9277 | 2020-06-04 13:59:55 +0200 | [diff] [blame] | 33 | volt, |
| 34 | util |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 35 | }; |
| 36 | |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 37 | enum class ReadingFormat |
| 38 | { |
| 39 | byte0, |
| 40 | byte3, |
Rebecca Cran | 394f0c5 | 2023-12-17 20:08:55 -0700 | [diff] [blame] | 41 | nineBit, |
| 42 | tenBit, |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 43 | elevenBit, |
| 44 | elevenBitShift, |
Rebecca Cran | 394f0c5 | 2023-12-17 20:08:55 -0700 | [diff] [blame] | 45 | linearElevenBit, |
| 46 | fifteenBit |
James Feist | d7ae29a | 2020-06-25 15:42:46 -0700 | [diff] [blame] | 47 | }; |
| 48 | |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 49 | namespace ipmi |
| 50 | { |
| 51 | namespace sensor |
| 52 | { |
| 53 | constexpr uint8_t netFn = 0x04; |
| 54 | constexpr uint8_t getSensorReading = 0x2d; |
| 55 | |
Patrick Williams | d02ad49 | 2023-05-08 09:15:21 -0500 | [diff] [blame] | 56 | static inline bool isValid(const std::vector<uint8_t>& data) |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 57 | { |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 58 | constexpr auto readingUnavailableBit = 5; |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 59 | |
| 60 | // Proper 'Get Sensor Reading' response has at least 4 bytes, including |
| 61 | // Completion Code. Our IPMB stack strips Completion Code from payload so we |
| 62 | // compare here against the rest of payload |
| 63 | if (data.size() < 3) |
| 64 | { |
| 65 | return false; |
| 66 | } |
| 67 | |
| 68 | // Per IPMI 'Get Sensor Reading' specification |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 69 | if ((data[1] & (1 << readingUnavailableBit)) != 0) |
Adrian Ambrożewicz | 58e02ef | 2020-08-06 14:42:38 +0200 | [diff] [blame] | 70 | { |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | return true; |
| 75 | } |
| 76 | |
| 77 | } // namespace sensor |
| 78 | namespace me_bridge |
| 79 | { |
| 80 | constexpr uint8_t netFn = 0x2e; |
| 81 | constexpr uint8_t sendRawPmbus = 0xd9; |
| 82 | } // namespace me_bridge |
| 83 | } // namespace ipmi |
| 84 | |
Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 85 | using IpmbMethodType = |
| 86 | std::tuple<int, uint8_t, uint8_t, uint8_t, uint8_t, std::vector<uint8_t>>; |
| 87 | |
| 88 | struct IpmbSensor : |
| 89 | public Sensor, |
| 90 | public std::enable_shared_from_this<IpmbSensor> |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 91 | { |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 92 | IpmbSensor(std::shared_ptr<sdbusplus::asio::connection>& conn, |
Ed Tanous | 1f97863 | 2023-02-28 18:16:39 -0800 | [diff] [blame] | 93 | boost::asio::io_context& io, const std::string& name, |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 94 | const std::string& sensorConfiguration, |
| 95 | sdbusplus::asio::object_server& objectServer, |
Ed Tanous | 2049bd2 | 2022-07-09 07:20:26 -0700 | [diff] [blame] | 96 | std::vector<thresholds::Threshold>&& thresholdData, |
| 97 | uint8_t deviceAddress, uint8_t hostSMbusIndex, float pollRate, |
| 98 | std::string& sensorTypeName); |
Ed Tanous | 8a57ec0 | 2020-10-09 12:46:52 -0700 | [diff] [blame] | 99 | ~IpmbSensor() override; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 100 | |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 101 | void checkThresholds() override; |
| 102 | void read(); |
| 103 | void init(); |
| 104 | std::string getSubTypeUnits() const; |
| 105 | void loadDefaults(); |
| 106 | void runInitCmd(); |
Ed Tanous | 828c5a6 | 2024-02-09 16:59:22 -0800 | [diff] [blame] | 107 | static bool processReading(ReadingFormat readingFormat, uint8_t command, |
| 108 | const std::vector<uint8_t>& data, double& resp, |
| 109 | size_t errCount); |
Jayashree Dhanapal | 8418975 | 2022-03-07 12:51:54 +0530 | [diff] [blame] | 110 | void parseConfigValues(const SensorBaseConfigMap& entry); |
| 111 | bool sensorClassType(const std::string& sensorClass); |
| 112 | void sensorSubType(const std::string& sensorTypeName); |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 113 | |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 114 | IpmbType type = IpmbType::none; |
| 115 | IpmbSubType subType = IpmbSubType::none; |
| 116 | double scaleVal = 1.0; |
| 117 | double offsetVal = 0.0; |
| 118 | uint8_t commandAddress = 0; |
| 119 | uint8_t netfn = 0; |
| 120 | uint8_t command = 0; |
| 121 | uint8_t deviceAddress = 0; |
| 122 | uint8_t errorCount = 0; |
| 123 | uint8_t hostSMbusIndex = 0; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 124 | std::vector<uint8_t> commandData; |
| 125 | std::optional<uint8_t> initCommand; |
| 126 | std::vector<uint8_t> initData; |
Jayashree-D | 9f6d4fd | 2021-04-13 18:27:22 +0530 | [diff] [blame] | 127 | int sensorPollMs; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 128 | |
Ed Tanous | a771f6a | 2022-01-14 09:36:51 -0800 | [diff] [blame] | 129 | ReadingFormat readingFormat = ReadingFormat::byte0; |
James Feist | 6ef2040 | 2019-01-07 16:45:08 -0800 | [diff] [blame] | 130 | |
| 131 | private: |
Ed Tanous | 201a101 | 2024-04-03 18:07:28 -0700 | [diff] [blame] | 132 | void sendIpmbRequest(); |
James Feist | d870587 | 2019-02-08 13:26:09 -0800 | [diff] [blame] | 133 | sdbusplus::asio::object_server& objectServer; |
Ed Tanous | 9b4a20e | 2022-09-06 08:47:11 -0700 | [diff] [blame] | 134 | boost::asio::steady_timer waitTimer; |
Vikash Chandola | 1f84797 | 2022-09-28 09:47:32 +0000 | [diff] [blame] | 135 | void ipmbRequestCompletionCb(const boost::system::error_code& ec, |
| 136 | const IpmbMethodType& response); |
Vijay Khemka | 682a5cb | 2019-07-18 17:34:03 -0700 | [diff] [blame] | 137 | }; |
Ed Tanous | 828c5a6 | 2024-02-09 16:59:22 -0800 | [diff] [blame] | 138 | |
| 139 | void createSensors( |
| 140 | boost::asio::io_context& io, sdbusplus::asio::object_server& objectServer, |
| 141 | boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>& |
| 142 | sensors, |
| 143 | std::shared_ptr<sdbusplus::asio::connection>& dbusConnection); |
| 144 | |
| 145 | void interfaceRemoved( |
| 146 | sdbusplus::message_t& message, |
| 147 | boost::container::flat_map<std::string, std::shared_ptr<IpmbSensor>>& |
| 148 | sensors); |