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