Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1 | #include "config.h" |
| 2 | |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 3 | #include "dcmihandler.hpp" |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 4 | |
Johnathan Mantey | 74a2102 | 2018-12-13 13:17:56 -0800 | [diff] [blame] | 5 | #include "user_channel/channel_layer.hpp" |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 6 | |
Vernon Mauery | e08fbff | 2019-04-03 09:19:34 -0700 | [diff] [blame] | 7 | #include <ipmid/api.hpp> |
Vernon Mauery | 6a98fe7 | 2019-03-11 15:57:48 -0700 | [diff] [blame] | 8 | #include <ipmid/utils.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 9 | #include <nlohmann/json.hpp> |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 10 | #include <phosphor-logging/elog-errors.hpp> |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 11 | #include <phosphor-logging/log.hpp> |
| 12 | #include <sdbusplus/bus.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 13 | #include <xyz/openbmc_project/Common/error.hpp> |
Thang Tran | 55cbf55 | 2023-01-31 14:43:02 +0700 | [diff] [blame] | 14 | #include <xyz/openbmc_project/Network/EthernetInterface/server.hpp> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 15 | |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 16 | #include <bitset> |
| 17 | #include <cmath> |
| 18 | #include <fstream> |
| 19 | #include <variant> |
| 20 | |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 21 | using namespace phosphor::logging; |
Thang Tran | 55cbf55 | 2023-01-31 14:43:02 +0700 | [diff] [blame] | 22 | using sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface; |
| 23 | |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 24 | using InternalFailure = |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 25 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Chris Austen | 1810bec | 2015-10-13 12:12:39 -0500 | [diff] [blame] | 26 | |
| 27 | void register_netfn_dcmi_functions() __attribute__((constructor)); |
| 28 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 29 | constexpr auto pcapPath = "/xyz/openbmc_project/control/host0/power_cap"; |
| 30 | constexpr auto pcapInterface = "xyz.openbmc_project.Control.Power.Cap"; |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 31 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 32 | constexpr auto powerCapProp = "PowerCap"; |
| 33 | constexpr auto powerCapEnableProp = "PowerCapEnable"; |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 34 | |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 35 | constexpr auto DCMI_SPEC_MAJOR_VERSION = 1; |
| 36 | constexpr auto DCMI_SPEC_MINOR_VERSION = 5; |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 37 | constexpr auto DCMI_CONFIG_PARAMETER_REVISION = 1; |
| 38 | constexpr auto DCMI_RAND_BACK_OFF_MASK = 0x80; |
| 39 | constexpr auto DCMI_OPTION_60_43_MASK = 0x02; |
| 40 | constexpr auto DCMI_OPTION_12_MASK = 0x01; |
| 41 | constexpr auto DCMI_ACTIVATE_DHCP_MASK = 0x01; |
| 42 | constexpr auto DCMI_ACTIVATE_DHCP_REPLY = 0x00; |
William A. Kennington III | 5d06cc6 | 2019-04-25 02:10:55 -0700 | [diff] [blame] | 43 | constexpr auto DCMI_SET_CONF_PARAM_REQ_PACKET_MAX_SIZE = 0x04; |
| 44 | constexpr auto DCMI_SET_CONF_PARAM_REQ_PACKET_MIN_SIZE = 0x03; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 45 | constexpr auto DHCP_TIMING1 = 0x04; // 4 sec |
| 46 | constexpr auto DHCP_TIMING2_UPPER = 0x00; // 2 min |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 47 | constexpr auto DHCP_TIMING2_LOWER = 0x78; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 48 | constexpr auto DHCP_TIMING3_UPPER = 0x00; // 64 sec |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 49 | constexpr auto DHCP_TIMING3_LOWER = 0x40; |
| 50 | // When DHCP Option 12 is enabled the string "SendHostName=true" will be |
| 51 | // added into n/w configuration file and the parameter |
| 52 | // SendHostNameEnabled will set to true. |
| 53 | constexpr auto DHCP_OPT12_ENABLED = "SendHostNameEnabled"; |
| 54 | |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 55 | constexpr auto SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value"; |
| 56 | constexpr auto SENSOR_VALUE_PROP = "Value"; |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 57 | |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 58 | using namespace phosphor::logging; |
| 59 | |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 60 | namespace dcmi |
| 61 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 62 | constexpr auto assetTagMaxOffset = 62; |
| 63 | constexpr auto assetTagMaxSize = 63; |
| 64 | constexpr auto maxBytes = 16; |
| 65 | constexpr size_t maxCtrlIdStrLen = 63; |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 66 | |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 67 | constexpr uint8_t parameterRevision = 2; |
| 68 | constexpr uint8_t specMajorVersion = 1; |
| 69 | constexpr uint8_t specMinorVersion = 5; |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 70 | constexpr auto sensorValueIntf = "xyz.openbmc_project.Sensor.Value"; |
| 71 | constexpr auto sensorValueProp = "Value"; |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 72 | |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 73 | // Refer Table 6-14, DCMI Entity ID Extension, DCMI v1.5 spec |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 74 | static const std::map<uint8_t, std::string> entityIdToName{ |
| 75 | {0x40, "inlet"}, {0x37, "inlet"}, {0x41, "cpu"}, |
| 76 | {0x03, "cpu"}, {0x42, "baseboard"}, {0x07, "baseboard"}}; |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 77 | |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 78 | bool isDCMIPowerMgmtSupported() |
| 79 | { |
Vernon Mauery | f4eb35d | 2023-07-27 11:08:49 -0700 | [diff] [blame] | 80 | static bool parsed = false; |
| 81 | static bool supported = false; |
| 82 | if (!parsed) |
| 83 | { |
| 84 | auto data = parseJSONConfig(gDCMICapabilitiesConfig); |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 85 | |
Vernon Mauery | f4eb35d | 2023-07-27 11:08:49 -0700 | [diff] [blame] | 86 | supported = (gDCMIPowerMgmtSupported == |
| 87 | data.value(gDCMIPowerMgmtCapability, 0)); |
| 88 | } |
| 89 | return supported; |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 90 | } |
| 91 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 92 | std::optional<uint32_t> getPcap(ipmi::Context::ptr& ctx) |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 93 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 94 | std::string service{}; |
| 95 | boost::system::error_code ec = ipmi::getService(ctx, pcapInterface, |
| 96 | pcapPath, service); |
| 97 | if (ec.value()) |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 98 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 99 | return std::nullopt; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 100 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 101 | uint32_t pcap{}; |
| 102 | ec = ipmi::getDbusProperty(ctx, service, pcapPath, pcapInterface, |
| 103 | powerCapProp, pcap); |
| 104 | if (ec.value()) |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 105 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 106 | log<level::ERR>("Error in getPcap prop", |
| 107 | entry("ERROR=%s", ec.message().c_str())); |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 108 | elog<InternalFailure>(); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 109 | return std::nullopt; |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 110 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 111 | return pcap; |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 112 | } |
| 113 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 114 | std::optional<bool> getPcapEnabled(ipmi::Context::ptr& ctx) |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 115 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 116 | std::string service{}; |
| 117 | boost::system::error_code ec = ipmi::getService(ctx, pcapInterface, |
| 118 | pcapPath, service); |
| 119 | if (ec.value()) |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 120 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 121 | return std::nullopt; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 122 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 123 | bool pcapEnabled{}; |
| 124 | ec = ipmi::getDbusProperty(ctx, service, pcapPath, pcapInterface, |
| 125 | powerCapEnableProp, pcapEnabled); |
| 126 | if (ec.value()) |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 127 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 128 | log<level::ERR>("Error in getPcap prop"); |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 129 | elog<InternalFailure>(); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 130 | return std::nullopt; |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 131 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 132 | return pcapEnabled; |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 133 | } |
Chris Austen | 1810bec | 2015-10-13 12:12:39 -0500 | [diff] [blame] | 134 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 135 | bool setPcap(ipmi::Context::ptr& ctx, const uint32_t powerCap) |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 136 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 137 | std::string service{}; |
| 138 | boost::system::error_code ec = ipmi::getService(ctx, pcapInterface, |
| 139 | pcapPath, service); |
| 140 | if (ec.value()) |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 141 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 142 | return false; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 143 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 144 | |
| 145 | ec = ipmi::setDbusProperty(ctx, service, pcapPath, pcapInterface, |
| 146 | powerCapProp, powerCap); |
| 147 | if (ec.value()) |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 148 | { |
| 149 | log<level::ERR>("Error in setPcap property", |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 150 | entry("ERROR=%s", ec.message().c_str())); |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 151 | elog<InternalFailure>(); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 152 | return false; |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 153 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 154 | return true; |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 155 | } |
| 156 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 157 | bool setPcapEnable(ipmi::Context::ptr& ctx, bool enabled) |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 158 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 159 | std::string service{}; |
| 160 | boost::system::error_code ec = ipmi::getService(ctx, pcapInterface, |
| 161 | pcapPath, service); |
| 162 | if (ec.value()) |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 163 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 164 | return false; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 165 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 166 | |
| 167 | ec = ipmi::setDbusProperty(ctx, service, pcapPath, pcapInterface, |
| 168 | powerCapEnableProp, enabled); |
| 169 | if (ec.value()) |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 170 | { |
| 171 | log<level::ERR>("Error in setPcapEnabled property", |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 172 | entry("ERROR=%s", ec.message().c_str())); |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 173 | elog<InternalFailure>(); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 174 | return false; |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 175 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 176 | return true; |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 177 | } |
| 178 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 179 | std::optional<std::string> readAssetTag(ipmi::Context::ptr& ctx) |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 180 | { |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 181 | // Read the object tree with the inventory root to figure out the object |
| 182 | // that has implemented the Asset tag interface. |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 183 | ipmi::DbusObjectInfo objectInfo; |
| 184 | boost::system::error_code ec = getDbusObject( |
| 185 | ctx, dcmi::assetTagIntf, ipmi::sensor::inventoryRoot, "", objectInfo); |
| 186 | if (ec.value()) |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 187 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 188 | return std::nullopt; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 189 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 190 | |
| 191 | std::string assetTag{}; |
| 192 | ec = ipmi::getDbusProperty(ctx, objectInfo.second, objectInfo.first, |
| 193 | dcmi::assetTagIntf, dcmi::assetTagProp, |
| 194 | assetTag); |
| 195 | if (ec.value()) |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 196 | { |
| 197 | log<level::ERR>("Error in reading asset tag", |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 198 | entry("ERROR=%s", ec.message().c_str())); |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 199 | elog<InternalFailure>(); |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 200 | return std::nullopt; |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 201 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 202 | |
| 203 | return assetTag; |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 204 | } |
| 205 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 206 | bool writeAssetTag(ipmi::Context::ptr& ctx, const std::string& assetTag) |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 207 | { |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 208 | // Read the object tree with the inventory root to figure out the object |
| 209 | // that has implemented the Asset tag interface. |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 210 | ipmi::DbusObjectInfo objectInfo; |
| 211 | boost::system::error_code ec = getDbusObject( |
| 212 | ctx, dcmi::assetTagIntf, ipmi::sensor::inventoryRoot, "", objectInfo); |
| 213 | if (ec.value()) |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 214 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 215 | return false; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 216 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 217 | |
| 218 | ec = ipmi::setDbusProperty(ctx, objectInfo.second, objectInfo.first, |
| 219 | dcmi::assetTagIntf, dcmi::assetTagProp, |
| 220 | assetTag); |
| 221 | if (ec.value()) |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 222 | { |
| 223 | log<level::ERR>("Error in writing asset tag", |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 224 | entry("ERROR=%s", ec.message().c_str())); |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 225 | elog<InternalFailure>(); |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 226 | return false; |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 227 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 228 | return true; |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 229 | } |
| 230 | |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 231 | std::optional<std::string> getHostName(ipmi::Context::ptr& ctx) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 232 | { |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 233 | std::string service{}; |
| 234 | boost::system::error_code ec = ipmi::getService(ctx, networkConfigIntf, |
| 235 | networkConfigObj, service); |
| 236 | if (ec.value()) |
| 237 | { |
| 238 | return std::nullopt; |
| 239 | } |
| 240 | std::string hostname{}; |
| 241 | ec = ipmi::getDbusProperty(ctx, service, networkConfigObj, |
| 242 | networkConfigIntf, hostNameProp, hostname); |
| 243 | if (ec.value()) |
| 244 | { |
| 245 | log<level::ERR>("Error fetching hostname"); |
| 246 | elog<InternalFailure>(); |
| 247 | return std::nullopt; |
| 248 | } |
| 249 | return hostname; |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 250 | } |
| 251 | |
Thang Tran | 55cbf55 | 2023-01-31 14:43:02 +0700 | [diff] [blame] | 252 | EthernetInterface::DHCPConf getDHCPEnabled() |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 253 | { |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 254 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 255 | |
Johnathan Mantey | 74a2102 | 2018-12-13 13:17:56 -0800 | [diff] [blame] | 256 | auto ethdevice = ipmi::getChannelName(ethernetDefaultChannelNum); |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 257 | auto ethernetObj = ipmi::getDbusObject(bus, ethernetIntf, networkRoot, |
| 258 | ethdevice); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 259 | auto service = ipmi::getService(bus, ethernetIntf, ethernetObj.first); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 260 | auto value = ipmi::getDbusProperty(bus, service, ethernetObj.first, |
| 261 | ethernetIntf, "DHCPEnabled"); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 262 | |
Thang Tran | 55cbf55 | 2023-01-31 14:43:02 +0700 | [diff] [blame] | 263 | return EthernetInterface::convertDHCPConfFromString( |
| 264 | std::get<std::string>(value)); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | bool getDHCPOption(std::string prop) |
| 268 | { |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 269 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 270 | |
| 271 | auto service = ipmi::getService(bus, dhcpIntf, dhcpObj); |
| 272 | auto value = ipmi::getDbusProperty(bus, service, dhcpObj, dhcpIntf, prop); |
| 273 | |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 274 | return std::get<bool>(value); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 275 | } |
| 276 | |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 277 | void setDHCPOption(std::string prop, bool value) |
| 278 | { |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 279 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 280 | |
| 281 | auto service = ipmi::getService(bus, dhcpIntf, dhcpObj); |
| 282 | ipmi::setDbusProperty(bus, service, dhcpObj, dhcpIntf, prop, value); |
| 283 | } |
| 284 | |
Kirill Pakhomov | a257362 | 2018-11-02 19:00:18 +0300 | [diff] [blame] | 285 | Json parseJSONConfig(const std::string& configFile) |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 286 | { |
| 287 | std::ifstream jsonFile(configFile); |
| 288 | if (!jsonFile.is_open()) |
| 289 | { |
| 290 | log<level::ERR>("Temperature readings JSON file not found"); |
| 291 | elog<InternalFailure>(); |
| 292 | } |
| 293 | |
| 294 | auto data = Json::parse(jsonFile, nullptr, false); |
| 295 | if (data.is_discarded()) |
| 296 | { |
| 297 | log<level::ERR>("Temperature readings JSON parser failure"); |
| 298 | elog<InternalFailure>(); |
| 299 | } |
| 300 | |
| 301 | return data; |
| 302 | } |
| 303 | |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 304 | } // namespace dcmi |
Chris Austen | 1810bec | 2015-10-13 12:12:39 -0500 | [diff] [blame] | 305 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 306 | constexpr uint8_t exceptionPowerOff = 0x01; |
| 307 | ipmi::RspType<uint16_t, // reserved |
| 308 | uint8_t, // exception actions |
| 309 | uint16_t, // power limit requested in watts |
| 310 | uint32_t, // correction time in milliseconds |
| 311 | uint16_t, // reserved |
| 312 | uint16_t // statistics sampling period in seconds |
| 313 | > |
| 314 | getPowerLimit(ipmi::Context::ptr ctx, uint16_t reserved) |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 315 | { |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 316 | if (!dcmi::isDCMIPowerMgmtSupported()) |
| 317 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 318 | return ipmi::responseInvalidCommand(); |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 319 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 320 | if (reserved) |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 321 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 322 | return ipmi::responseInvalidFieldRequest(); |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 323 | } |
| 324 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 325 | std::optional<uint16_t> pcapValue = dcmi::getPcap(ctx); |
| 326 | std::optional<bool> pcapEnable = dcmi::getPcapEnabled(ctx); |
| 327 | if (!pcapValue || !pcapEnable) |
| 328 | { |
| 329 | return ipmi::responseUnspecifiedError(); |
| 330 | } |
| 331 | |
| 332 | constexpr uint16_t reserved1{}; |
| 333 | constexpr uint16_t reserved2{}; |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 334 | /* |
| 335 | * Exception action if power limit is exceeded and cannot be controlled |
| 336 | * with the correction time limit is hardcoded to Hard Power Off system |
| 337 | * and log event to SEL. |
| 338 | */ |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 339 | constexpr uint8_t exception = exceptionPowerOff; |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 340 | /* |
| 341 | * Correction time limit and Statistics sampling period is currently not |
| 342 | * populated. |
| 343 | */ |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 344 | constexpr uint32_t correctionTime{}; |
| 345 | constexpr uint16_t statsPeriod{}; |
| 346 | if (!pcapEnable) |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 347 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 348 | constexpr ipmi::Cc responseNoPowerLimitSet = 0x80; |
| 349 | constexpr uint16_t noPcap{}; |
| 350 | return ipmi::response(responseNoPowerLimitSet, reserved1, exception, |
| 351 | noPcap, correctionTime, reserved2, statsPeriod); |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 352 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 353 | return ipmi::responseSuccess(reserved1, exception, *pcapValue, |
| 354 | correctionTime, reserved2, statsPeriod); |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 355 | } |
| 356 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 357 | ipmi::RspType<> setPowerLimit(ipmi::Context::ptr& ctx, uint16_t reserved1, |
| 358 | uint8_t exceptionAction, uint16_t powerLimit, |
| 359 | uint32_t correctionTime, uint16_t reserved2, |
| 360 | uint16_t statsPeriod) |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 361 | { |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 362 | if (!dcmi::isDCMIPowerMgmtSupported()) |
| 363 | { |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 364 | log<level::ERR>("DCMI Power management is unsupported!"); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 365 | return ipmi::responseInvalidCommand(); |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 366 | } |
| 367 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 368 | // Only process the power limit requested in watts. Return errors |
| 369 | // for other fields that are set |
| 370 | if (reserved1 || reserved2 || correctionTime || statsPeriod || |
| 371 | exceptionAction != exceptionPowerOff) |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 372 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 373 | return ipmi::responseInvalidFieldRequest(); |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 374 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 375 | |
| 376 | if (!dcmi::setPcap(ctx, powerLimit)) |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 377 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 378 | return ipmi::responseUnspecifiedError(); |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 379 | } |
| 380 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 381 | log<level::INFO>("Set Power Cap", entry("POWERCAP=%u", powerLimit)); |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 382 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 383 | return ipmi::responseSuccess(); |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 384 | } |
| 385 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 386 | ipmi::RspType<> applyPowerLimit(ipmi::Context::ptr& ctx, bool enabled, |
| 387 | uint7_t reserved1, uint16_t reserved2) |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 388 | { |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 389 | if (!dcmi::isDCMIPowerMgmtSupported()) |
| 390 | { |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 391 | log<level::ERR>("DCMI Power management is unsupported!"); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 392 | return ipmi::responseInvalidCommand(); |
| 393 | } |
| 394 | if (reserved1 || reserved2) |
| 395 | { |
| 396 | return ipmi::responseInvalidFieldRequest(); |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 397 | } |
| 398 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 399 | if (!dcmi::setPcapEnable(ctx, enabled)) |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 400 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 401 | return ipmi::responseUnspecifiedError(); |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 402 | } |
| 403 | |
| 404 | log<level::INFO>("Set Power Cap Enable", |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 405 | entry("POWERCAPENABLE=%u", static_cast<uint8_t>(enabled))); |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 406 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 407 | return ipmi::responseSuccess(); |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 408 | } |
| 409 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 410 | ipmi::RspType<uint8_t, // total tag length |
| 411 | std::vector<char> // tag data |
| 412 | > |
| 413 | getAssetTag(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count) |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 414 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 415 | // Verify offset to read and number of bytes to read are not exceeding |
| 416 | // the range. |
| 417 | if ((offset > dcmi::assetTagMaxOffset) || (count > dcmi::maxBytes) || |
| 418 | ((offset + count) > dcmi::assetTagMaxSize)) |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 419 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 420 | return ipmi::responseParmOutOfRange(); |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 421 | } |
| 422 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 423 | std::optional<std::string> assetTagResp = dcmi::readAssetTag(ctx); |
| 424 | if (!assetTagResp) |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 425 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 426 | return ipmi::responseUnspecifiedError(); |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 427 | } |
| 428 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 429 | std::string& assetTag = assetTagResp.value(); |
| 430 | // If the asset tag is longer than 63 bytes, restrict it to 63 bytes to |
| 431 | // suit Get Asset Tag command. |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 432 | if (assetTag.size() > dcmi::assetTagMaxSize) |
| 433 | { |
| 434 | assetTag.resize(dcmi::assetTagMaxSize); |
| 435 | } |
| 436 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 437 | if (offset >= assetTag.size()) |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 438 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 439 | return ipmi::responseParmOutOfRange(); |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 440 | } |
| 441 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 442 | // silently truncate reads beyond the end of assetTag |
| 443 | if ((offset + count) >= assetTag.size()) |
| 444 | { |
| 445 | count = assetTag.size() - offset; |
| 446 | } |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 447 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 448 | auto totalTagSize = static_cast<uint8_t>(assetTag.size()); |
| 449 | std::vector<char> data{assetTag.begin() + offset, |
| 450 | assetTag.begin() + offset + count}; |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 451 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 452 | return ipmi::responseSuccess(totalTagSize, data); |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 453 | } |
| 454 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 455 | ipmi::RspType<uint8_t // new asset tag length |
| 456 | > |
| 457 | setAssetTag(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count, |
| 458 | const std::vector<char>& data) |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 459 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 460 | // Verify offset to read and number of bytes to read are not exceeding |
| 461 | // the range. |
| 462 | if ((offset > dcmi::assetTagMaxOffset) || (count > dcmi::maxBytes) || |
| 463 | ((offset + count) > dcmi::assetTagMaxSize)) |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 464 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 465 | return ipmi::responseParmOutOfRange(); |
| 466 | } |
| 467 | if (data.size() != count) |
| 468 | { |
| 469 | return ipmi::responseReqDataLenInvalid(); |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 470 | } |
| 471 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 472 | std::optional<std::string> assetTagResp = dcmi::readAssetTag(ctx); |
| 473 | if (!assetTagResp) |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 474 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 475 | return ipmi::responseUnspecifiedError(); |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 476 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 477 | |
| 478 | std::string& assetTag = assetTagResp.value(); |
| 479 | |
| 480 | if (offset > assetTag.size()) |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 481 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 482 | return ipmi::responseParmOutOfRange(); |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 483 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 484 | |
| 485 | // operation is to truncate at offset and append new data |
| 486 | assetTag.resize(offset); |
| 487 | assetTag.append(data.begin(), data.end()); |
| 488 | |
| 489 | if (!dcmi::writeAssetTag(ctx, assetTag)) |
| 490 | { |
| 491 | return ipmi::responseUnspecifiedError(); |
| 492 | } |
| 493 | |
| 494 | auto totalTagSize = static_cast<uint8_t>(assetTag.size()); |
| 495 | return ipmi::responseSuccess(totalTagSize); |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 496 | } |
| 497 | |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 498 | ipmi::RspType<uint8_t, // length |
| 499 | std::vector<char> // data |
| 500 | > |
| 501 | getMgmntCtrlIdStr(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 502 | { |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 503 | if (count > dcmi::maxBytes || offset + count > dcmi::maxCtrlIdStrLen) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 504 | { |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 505 | return ipmi::responseParmOutOfRange(); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 506 | } |
| 507 | |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 508 | std::optional<std::string> hostnameResp = dcmi::getHostName(ctx); |
| 509 | if (!hostnameResp) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 510 | { |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 511 | return ipmi::responseUnspecifiedError(); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 512 | } |
| 513 | |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 514 | std::string& hostname = hostnameResp.value(); |
| 515 | // If the id string is longer than 63 bytes, restrict it to 63 bytes to |
| 516 | // suit set management ctrl str command. |
| 517 | if (hostname.size() > dcmi::maxCtrlIdStrLen) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 518 | { |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 519 | hostname.resize(dcmi::maxCtrlIdStrLen); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 520 | } |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 521 | |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 522 | if (offset >= hostname.size()) |
| 523 | { |
| 524 | return ipmi::responseParmOutOfRange(); |
| 525 | } |
| 526 | |
| 527 | // silently truncate reads beyond the end of hostname |
| 528 | if ((offset + count) >= hostname.size()) |
| 529 | { |
| 530 | count = hostname.size() - offset; |
| 531 | } |
| 532 | |
| 533 | auto nameSize = static_cast<uint8_t>(hostname.size()); |
| 534 | std::vector<char> data{hostname.begin() + offset, |
| 535 | hostname.begin() + offset + count}; |
| 536 | |
| 537 | return ipmi::responseSuccess(nameSize, data); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 538 | } |
| 539 | |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 540 | ipmi::RspType<uint8_t> setMgmntCtrlIdStr(ipmi::Context::ptr& ctx, |
| 541 | uint8_t offset, uint8_t count, |
| 542 | std::vector<char> data) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 543 | { |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 544 | if ((offset > dcmi::maxCtrlIdStrLen) || (count > dcmi::maxBytes) || |
| 545 | ((offset + count) > dcmi::maxCtrlIdStrLen)) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 546 | { |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 547 | return ipmi::responseParmOutOfRange(); |
| 548 | } |
| 549 | if (data.size() != count) |
| 550 | { |
| 551 | return ipmi::responseReqDataLenInvalid(); |
| 552 | } |
| 553 | bool terminalWrite{data.back() == '\0'}; |
| 554 | if (terminalWrite) |
| 555 | { |
| 556 | // remove the null termination from the data (no need with std::string) |
| 557 | data.resize(count - 1); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 558 | } |
| 559 | |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 560 | static std::string hostname{}; |
| 561 | // read in the current value if not starting at offset 0 |
| 562 | if (hostname.size() == 0 && offset != 0) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 563 | { |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 564 | /* read old ctrlIdStr */ |
| 565 | std::optional<std::string> hostnameResp = dcmi::getHostName(ctx); |
| 566 | if (!hostnameResp) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 567 | { |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 568 | return ipmi::responseUnspecifiedError(); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 569 | } |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 570 | hostname = hostnameResp.value(); |
| 571 | hostname.resize(offset); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 572 | } |
| 573 | |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 574 | // operation is to truncate at offset and append new data |
| 575 | hostname.append(data.begin(), data.end()); |
| 576 | |
| 577 | // do the update if this is the last write |
| 578 | if (terminalWrite) |
| 579 | { |
| 580 | boost::system::error_code ec = ipmi::setDbusProperty( |
| 581 | ctx, dcmi::networkServiceName, dcmi::networkConfigObj, |
| 582 | dcmi::networkConfigIntf, dcmi::hostNameProp, hostname); |
| 583 | hostname.clear(); |
| 584 | if (ec.value()) |
| 585 | { |
| 586 | return ipmi::responseUnspecifiedError(); |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | auto totalIdSize = static_cast<uint8_t>(offset + count); |
| 591 | return ipmi::responseSuccess(totalIdSize); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 592 | } |
| 593 | |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 594 | ipmi::RspType<ipmi::message::Payload> getDCMICapabilities(uint8_t parameter) |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 595 | { |
Kirill Pakhomov | a257362 | 2018-11-02 19:00:18 +0300 | [diff] [blame] | 596 | std::ifstream dcmiCapFile(dcmi::gDCMICapabilitiesConfig); |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 597 | if (!dcmiCapFile.is_open()) |
| 598 | { |
| 599 | log<level::ERR>("DCMI Capabilities file not found"); |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 600 | return ipmi::responseUnspecifiedError(); |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 601 | } |
| 602 | |
| 603 | auto data = nlohmann::json::parse(dcmiCapFile, nullptr, false); |
| 604 | if (data.is_discarded()) |
| 605 | { |
| 606 | log<level::ERR>("DCMI Capabilities JSON parser failure"); |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 607 | return ipmi::responseUnspecifiedError(); |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 608 | } |
| 609 | |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 610 | constexpr bool reserved1{}; |
| 611 | constexpr uint5_t reserved5{}; |
| 612 | constexpr uint7_t reserved7{}; |
| 613 | constexpr uint8_t reserved8{}; |
| 614 | constexpr uint16_t reserved16{}; |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 615 | |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 616 | ipmi::message::Payload payload; |
| 617 | payload.pack(dcmi::specMajorVersion, dcmi::specMinorVersion, |
| 618 | dcmi::parameterRevision); |
| 619 | |
| 620 | enum class DCMICapParameters : uint8_t |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 621 | { |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 622 | SupportedDcmiCaps = 0x01, // Supported DCMI Capabilities |
| 623 | MandatoryPlatAttributes = 0x02, // Mandatory Platform Attributes |
| 624 | OptionalPlatAttributes = 0x03, // Optional Platform Attributes |
| 625 | ManageabilityAccessAttributes = 0x04, // Manageability Access Attributes |
| 626 | }; |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 627 | |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 628 | switch (static_cast<DCMICapParameters>(parameter)) |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 629 | { |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 630 | case DCMICapParameters::SupportedDcmiCaps: |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 631 | { |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 632 | bool powerManagement = data.value("PowerManagement", 0); |
| 633 | bool oobSecondaryLan = data.value("OOBSecondaryLan", 0); |
| 634 | bool serialTMode = data.value("SerialTMODE", 0); |
| 635 | bool inBandSystemInterfaceChannel = |
| 636 | data.value("InBandSystemInterfaceChannel", 0); |
| 637 | payload.pack(reserved8, powerManagement, reserved7, |
| 638 | inBandSystemInterfaceChannel, serialTMode, |
| 639 | oobSecondaryLan, reserved5); |
| 640 | break; |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 641 | } |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 642 | // Mandatory Platform Attributes |
| 643 | case DCMICapParameters::MandatoryPlatAttributes: |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 644 | { |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 645 | bool selAutoRollOver = data.value("SELAutoRollOver", 0); |
| 646 | bool flushEntireSELUponRollOver = |
| 647 | data.value("FlushEntireSELUponRollOver", 0); |
| 648 | bool recordLevelSELFlushUponRollOver = |
| 649 | data.value("RecordLevelSELFlushUponRollOver", 0); |
| 650 | uint12_t numberOfSELEntries = data.value("NumberOfSELEntries", |
| 651 | 0xcac); |
| 652 | uint8_t tempMonitoringSamplingFreq = |
| 653 | data.value("TempMonitoringSamplingFreq", 0); |
| 654 | payload.pack(numberOfSELEntries, reserved1, |
| 655 | recordLevelSELFlushUponRollOver, |
| 656 | flushEntireSELUponRollOver, selAutoRollOver, |
| 657 | reserved16, tempMonitoringSamplingFreq); |
| 658 | break; |
| 659 | } |
| 660 | // Optional Platform Attributes |
| 661 | case DCMICapParameters::OptionalPlatAttributes: |
| 662 | { |
| 663 | uint7_t powerMgmtDeviceSlaveAddress = |
| 664 | data.value("PowerMgmtDeviceSlaveAddress", 0); |
| 665 | uint4_t bmcChannelNumber = data.value("BMCChannelNumber", 0); |
| 666 | uint4_t deviceRivision = data.value("DeviceRivision", 0); |
| 667 | payload.pack(powerMgmtDeviceSlaveAddress, reserved1, deviceRivision, |
| 668 | bmcChannelNumber); |
| 669 | break; |
| 670 | } |
| 671 | // Manageability Access Attributes |
| 672 | case DCMICapParameters::ManageabilityAccessAttributes: |
| 673 | { |
| 674 | uint8_t mandatoryPrimaryLanOOBSupport = |
| 675 | data.value("MandatoryPrimaryLanOOBSupport", 0xff); |
| 676 | uint8_t optionalSecondaryLanOOBSupport = |
| 677 | data.value("OptionalSecondaryLanOOBSupport", 0xff); |
| 678 | uint8_t optionalSerialOOBMTMODECapability = |
| 679 | data.value("OptionalSerialOOBMTMODECapability", 0xff); |
| 680 | payload.pack(mandatoryPrimaryLanOOBSupport, |
| 681 | optionalSecondaryLanOOBSupport, |
| 682 | optionalSerialOOBMTMODECapability); |
| 683 | break; |
| 684 | } |
| 685 | default: |
| 686 | { |
| 687 | log<level::ERR>("Invalid input parameter"); |
| 688 | return ipmi::responseInvalidFieldRequest(); |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 689 | } |
| 690 | } |
| 691 | |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 692 | return ipmi::responseSuccess(payload); |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 693 | } |
| 694 | |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 695 | namespace dcmi |
| 696 | { |
| 697 | namespace temp_readings |
| 698 | { |
| 699 | |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 700 | std::tuple<bool, bool, uint8_t> readTemp(ipmi::Context::ptr& ctx, |
| 701 | const std::string& dbusService, |
| 702 | const std::string& dbusPath) |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 703 | { |
| 704 | // Read the temperature value from d-bus object. Need some conversion. |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 705 | // As per the interface xyz.openbmc_project.Sensor.Value, the |
| 706 | // temperature is an double and in degrees C. It needs to be scaled by |
| 707 | // using the formula Value * 10^Scale. The ipmi spec has the temperature |
| 708 | // as a uint8_t, with a separate single bit for the sign. |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 709 | |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 710 | ipmi::PropertyMap result{}; |
| 711 | boost::system::error_code ec = ipmi::getAllDbusProperties( |
| 712 | ctx, dbusService, dbusPath, "xyz.openbmc_project.Sensor.Value", result); |
| 713 | if (ec.value()) |
| 714 | { |
| 715 | return std::make_tuple(false, false, 0); |
| 716 | } |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 717 | auto temperature = std::visit(ipmi::VariantToDoubleVisitor(), |
| 718 | result.at("Value")); |
James Feist | 9cc0ea5 | 2018-10-09 10:53:11 -0700 | [diff] [blame] | 719 | double absTemp = std::abs(temperature); |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 720 | |
James Feist | 9cc0ea5 | 2018-10-09 10:53:11 -0700 | [diff] [blame] | 721 | auto findFactor = result.find("Scale"); |
| 722 | double factor = 0.0; |
| 723 | if (findFactor != result.end()) |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 724 | { |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 725 | factor = std::visit(ipmi::VariantToDoubleVisitor(), findFactor->second); |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 726 | } |
James Feist | 9cc0ea5 | 2018-10-09 10:53:11 -0700 | [diff] [blame] | 727 | double scale = std::pow(10, factor); |
| 728 | |
| 729 | auto tempDegrees = absTemp * scale; |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 730 | // Max absolute temp as per ipmi spec is 127. |
| 731 | constexpr auto maxTemp = 127; |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 732 | if (tempDegrees > maxTemp) |
| 733 | { |
| 734 | tempDegrees = maxTemp; |
| 735 | } |
| 736 | |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 737 | return std::make_tuple(true, (temperature < 0), |
| 738 | static_cast<uint8_t>(tempDegrees)); |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 739 | } |
| 740 | |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 741 | std::tuple<std::vector<std::tuple<uint7_t, bool, uint8_t>>, uint8_t> |
| 742 | read(ipmi::Context::ptr& ctx, const std::string& type, uint8_t instance, |
| 743 | size_t count) |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 744 | { |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 745 | std::vector<std::tuple<uint7_t, bool, uint8_t>> response{}; |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 746 | |
Kirill Pakhomov | a257362 | 2018-11-02 19:00:18 +0300 | [diff] [blame] | 747 | auto data = parseJSONConfig(gDCMISensorsConfig); |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 748 | static const std::vector<nlohmann::json> empty{}; |
| 749 | std::vector<nlohmann::json> readings = data.value(type, empty); |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 750 | for (const auto& j : readings) |
| 751 | { |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 752 | // Max of 8 response data sets |
| 753 | if (response.size() == count) |
| 754 | { |
| 755 | break; |
| 756 | } |
| 757 | |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 758 | uint8_t instanceNum = j.value("instance", 0); |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 759 | // Not in the instance range we're interested in |
| 760 | if (instanceNum < instance) |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 761 | { |
| 762 | continue; |
| 763 | } |
| 764 | |
| 765 | std::string path = j.value("dbus", ""); |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 766 | std::string service{}; |
| 767 | boost::system::error_code ec = ipmi::getService( |
| 768 | ctx, "xyz.openbmc_project.Sensor.Value", path, service); |
| 769 | if (ec.value()) |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 770 | { |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 771 | // not found on dbus |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 772 | continue; |
| 773 | } |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 774 | |
| 775 | const auto& [ok, sign, temp] = readTemp(ctx, service, path); |
| 776 | if (ok) |
| 777 | { |
| 778 | response.emplace_back(uint7_t{temp}, sign, instanceNum); |
| 779 | } |
Deepak Kodihalli | b1e8fba | 2018-01-24 04:57:10 -0600 | [diff] [blame] | 780 | } |
| 781 | |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 782 | auto totalInstances = |
| 783 | static_cast<uint8_t>(std::min(readings.size(), maxInstances)); |
| 784 | return std::make_tuple(response, totalInstances); |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 785 | } |
| 786 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 787 | } // namespace temp_readings |
| 788 | } // namespace dcmi |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 789 | |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 790 | ipmi::RspType<uint8_t, // total instances for entity id |
| 791 | uint8_t, // number of instances in this reply |
| 792 | std::vector< // zero or more of the following two bytes |
| 793 | std::tuple<uint7_t, // temperature value |
| 794 | bool, // sign bit |
| 795 | uint8_t // entity instance |
| 796 | >>> |
| 797 | getTempReadings(ipmi::Context::ptr& ctx, uint8_t sensorType, |
| 798 | uint8_t entityId, uint8_t entityInstance, |
| 799 | uint8_t instanceStart) |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 800 | { |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 801 | auto it = dcmi::entityIdToName.find(entityId); |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 802 | if (it == dcmi::entityIdToName.end()) |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 803 | { |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 804 | log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", entityId)); |
| 805 | return ipmi::responseInvalidFieldRequest(); |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 806 | } |
| 807 | |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 808 | if (sensorType != dcmi::temperatureSensorType) |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 809 | { |
| 810 | log<level::ERR>("Invalid sensor type", |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 811 | entry("SENSOR_TYPE=%d", sensorType)); |
| 812 | return ipmi::responseInvalidFieldRequest(); |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 813 | } |
| 814 | |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 815 | uint8_t requestedRecords = (entityInstance == 0) ? dcmi::maxRecords : 1; |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 816 | |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 817 | // Read requested instances |
| 818 | const auto& [temps, totalInstances] = dcmi::temp_readings::read( |
| 819 | ctx, it->second, instanceStart, requestedRecords); |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 820 | |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 821 | auto numInstances = static_cast<uint8_t>(temps.size()); |
| 822 | |
| 823 | return ipmi::responseSuccess(totalInstances, numInstances, temps); |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 824 | } |
| 825 | |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 826 | int64_t getPowerReading(sdbusplus::bus_t& bus) |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 827 | { |
| 828 | std::ifstream sensorFile(POWER_READING_SENSOR); |
| 829 | std::string objectPath; |
| 830 | if (!sensorFile.is_open()) |
| 831 | { |
| 832 | log<level::ERR>("Power reading configuration file not found", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 833 | entry("POWER_SENSOR_FILE=%s", POWER_READING_SENSOR)); |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 834 | elog<InternalFailure>(); |
| 835 | } |
| 836 | |
| 837 | auto data = nlohmann::json::parse(sensorFile, nullptr, false); |
| 838 | if (data.is_discarded()) |
| 839 | { |
| 840 | log<level::ERR>("Error in parsing configuration file", |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 841 | entry("POWER_SENSOR_FILE=%s", POWER_READING_SENSOR)); |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 842 | elog<InternalFailure>(); |
| 843 | } |
| 844 | |
| 845 | objectPath = data.value("path", ""); |
| 846 | if (objectPath.empty()) |
| 847 | { |
| 848 | log<level::ERR>("Power sensor D-Bus object path is empty", |
| 849 | entry("POWER_SENSOR_FILE=%s", POWER_READING_SENSOR)); |
| 850 | elog<InternalFailure>(); |
| 851 | } |
| 852 | |
Marri Devender Rao | ce6a795 | 2018-02-11 08:45:00 -0600 | [diff] [blame] | 853 | // Return default value if failed to read from D-Bus object |
| 854 | int64_t power = 0; |
| 855 | try |
| 856 | { |
| 857 | auto service = ipmi::getService(bus, SENSOR_VALUE_INTF, objectPath); |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 858 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 859 | // Read the sensor value and scale properties |
Chris Cain | 4cc61e0 | 2022-01-12 17:23:14 -0600 | [diff] [blame] | 860 | auto value = ipmi::getDbusProperty( |
| 861 | bus, service, objectPath, SENSOR_VALUE_INTF, SENSOR_VALUE_PROP); |
| 862 | power = std::get<double>(value); |
Marri Devender Rao | ce6a795 | 2018-02-11 08:45:00 -0600 | [diff] [blame] | 863 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 864 | catch (const std::exception& e) |
Marri Devender Rao | ce6a795 | 2018-02-11 08:45:00 -0600 | [diff] [blame] | 865 | { |
Chris Cain | 4cc61e0 | 2022-01-12 17:23:14 -0600 | [diff] [blame] | 866 | log<level::ERR>("Failure to read power value from D-Bus object", |
| 867 | entry("OBJECT_PATH=%s", objectPath.c_str()), |
| 868 | entry("INTERFACE=%s", SENSOR_VALUE_INTF)); |
Marri Devender Rao | ce6a795 | 2018-02-11 08:45:00 -0600 | [diff] [blame] | 869 | } |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 870 | return power; |
| 871 | } |
| 872 | |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 873 | ipmi_ret_t setDCMIConfParams(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, |
| 874 | ipmi_response_t, ipmi_data_len_t data_len, |
| 875 | ipmi_context_t) |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 876 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 877 | auto requestData = |
| 878 | reinterpret_cast<const dcmi::SetConfParamsRequest*>(request); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 879 | |
William A. Kennington III | 5d06cc6 | 2019-04-25 02:10:55 -0700 | [diff] [blame] | 880 | if (*data_len < DCMI_SET_CONF_PARAM_REQ_PACKET_MIN_SIZE || |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 881 | *data_len > DCMI_SET_CONF_PARAM_REQ_PACKET_MAX_SIZE) |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 882 | { |
William A. Kennington III | 5d06cc6 | 2019-04-25 02:10:55 -0700 | [diff] [blame] | 883 | log<level::ERR>("Invalid Requested Packet size", |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 884 | entry("PACKET SIZE=%d", *data_len)); |
William A. Kennington III | 5d06cc6 | 2019-04-25 02:10:55 -0700 | [diff] [blame] | 885 | *data_len = 0; |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 886 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 887 | } |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 888 | *data_len = 0; |
| 889 | |
| 890 | try |
| 891 | { |
| 892 | // Take action based on the Parameter Selector |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 893 | switch ( |
| 894 | static_cast<dcmi::DCMIConfigParameters>(requestData->paramSelect)) |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 895 | { |
| 896 | case dcmi::DCMIConfigParameters::ActivateDHCP: |
| 897 | |
| 898 | if ((requestData->data[0] & DCMI_ACTIVATE_DHCP_MASK) && |
Thang Tran | 55cbf55 | 2023-01-31 14:43:02 +0700 | [diff] [blame] | 899 | (dcmi::getDHCPEnabled() != |
| 900 | EthernetInterface::DHCPConf::none)) |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 901 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 902 | // When these conditions are met we have to trigger DHCP |
| 903 | // protocol restart using the latest parameter settings, but |
| 904 | // as per n/w manager design, each time when we update n/w |
| 905 | // parameters, n/w service is restarted. So we no need to |
| 906 | // take any action in this case. |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 907 | } |
| 908 | break; |
| 909 | |
| 910 | case dcmi::DCMIConfigParameters::DiscoveryConfig: |
| 911 | |
| 912 | if (requestData->data[0] & DCMI_OPTION_12_MASK) |
| 913 | { |
| 914 | dcmi::setDHCPOption(DHCP_OPT12_ENABLED, true); |
| 915 | } |
| 916 | else |
| 917 | { |
| 918 | dcmi::setDHCPOption(DHCP_OPT12_ENABLED, false); |
| 919 | } |
| 920 | |
| 921 | // Systemd-networkd doesn't support Random Back off |
| 922 | if (requestData->data[0] & DCMI_RAND_BACK_OFF_MASK) |
| 923 | { |
| 924 | return IPMI_CC_INVALID; |
| 925 | } |
| 926 | break; |
| 927 | // Systemd-networkd doesn't allow to configure DHCP timigs |
| 928 | case dcmi::DCMIConfigParameters::DHCPTiming1: |
| 929 | case dcmi::DCMIConfigParameters::DHCPTiming2: |
| 930 | case dcmi::DCMIConfigParameters::DHCPTiming3: |
| 931 | default: |
| 932 | return IPMI_CC_INVALID; |
| 933 | } |
| 934 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 935 | catch (const std::exception& e) |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 936 | { |
| 937 | log<level::ERR>(e.what()); |
| 938 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 939 | } |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 940 | return IPMI_CC_OK; |
| 941 | } |
| 942 | |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 943 | ipmi_ret_t getDCMIConfParams(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, |
| 944 | ipmi_response_t response, ipmi_data_len_t data_len, |
| 945 | ipmi_context_t) |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 946 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 947 | auto requestData = |
| 948 | reinterpret_cast<const dcmi::GetConfParamsRequest*>(request); |
| 949 | auto responseData = |
| 950 | reinterpret_cast<dcmi::GetConfParamsResponse*>(response); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 951 | |
| 952 | responseData->data[0] = 0x00; |
| 953 | |
William A. Kennington III | 5d06cc6 | 2019-04-25 02:10:55 -0700 | [diff] [blame] | 954 | if (*data_len != sizeof(dcmi::GetConfParamsRequest)) |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 955 | { |
William A. Kennington III | 5d06cc6 | 2019-04-25 02:10:55 -0700 | [diff] [blame] | 956 | log<level::ERR>("Invalid Requested Packet size", |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 957 | entry("PACKET SIZE=%d", *data_len)); |
| 958 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 959 | } |
| 960 | |
| 961 | *data_len = 0; |
| 962 | |
| 963 | try |
| 964 | { |
| 965 | // Take action based on the Parameter Selector |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 966 | switch ( |
| 967 | static_cast<dcmi::DCMIConfigParameters>(requestData->paramSelect)) |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 968 | { |
| 969 | case dcmi::DCMIConfigParameters::ActivateDHCP: |
| 970 | responseData->data[0] = DCMI_ACTIVATE_DHCP_REPLY; |
| 971 | *data_len = sizeof(dcmi::GetConfParamsResponse) + 1; |
| 972 | break; |
| 973 | case dcmi::DCMIConfigParameters::DiscoveryConfig: |
| 974 | if (dcmi::getDHCPOption(DHCP_OPT12_ENABLED)) |
| 975 | { |
| 976 | responseData->data[0] |= DCMI_OPTION_12_MASK; |
| 977 | } |
| 978 | *data_len = sizeof(dcmi::GetConfParamsResponse) + 1; |
| 979 | break; |
| 980 | // Get below values from Systemd-networkd source code |
| 981 | case dcmi::DCMIConfigParameters::DHCPTiming1: |
| 982 | responseData->data[0] = DHCP_TIMING1; |
| 983 | *data_len = sizeof(dcmi::GetConfParamsResponse) + 1; |
| 984 | break; |
| 985 | case dcmi::DCMIConfigParameters::DHCPTiming2: |
| 986 | responseData->data[0] = DHCP_TIMING2_LOWER; |
| 987 | responseData->data[1] = DHCP_TIMING2_UPPER; |
| 988 | *data_len = sizeof(dcmi::GetConfParamsResponse) + 2; |
| 989 | break; |
| 990 | case dcmi::DCMIConfigParameters::DHCPTiming3: |
| 991 | responseData->data[0] = DHCP_TIMING3_LOWER; |
| 992 | responseData->data[1] = DHCP_TIMING3_UPPER; |
| 993 | *data_len = sizeof(dcmi::GetConfParamsResponse) + 2; |
| 994 | break; |
| 995 | default: |
| 996 | *data_len = 0; |
| 997 | return IPMI_CC_INVALID; |
| 998 | } |
| 999 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 1000 | catch (const std::exception& e) |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 1001 | { |
| 1002 | log<level::ERR>(e.what()); |
| 1003 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 1004 | } |
| 1005 | |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 1006 | responseData->major = DCMI_SPEC_MAJOR_VERSION; |
| 1007 | responseData->minor = DCMI_SPEC_MINOR_VERSION; |
| 1008 | responseData->paramRevision = DCMI_CONFIG_PARAMETER_REVISION; |
| 1009 | |
| 1010 | return IPMI_CC_OK; |
| 1011 | } |
| 1012 | |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 1013 | ipmi_ret_t getPowerReading(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t, |
| 1014 | ipmi_response_t response, ipmi_data_len_t data_len, |
| 1015 | ipmi_context_t) |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 1016 | { |
Chalapathi Venkataramashetty | 90e81fa | 2021-05-11 09:13:23 +0000 | [diff] [blame] | 1017 | *data_len = 0; |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 1018 | if (!dcmi::isDCMIPowerMgmtSupported()) |
| 1019 | { |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 1020 | log<level::ERR>("DCMI Power management is unsupported!"); |
| 1021 | return IPMI_CC_INVALID; |
| 1022 | } |
| 1023 | |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 1024 | ipmi_ret_t rc = IPMI_CC_OK; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1025 | auto responseData = |
| 1026 | reinterpret_cast<dcmi::GetPowerReadingResponse*>(response); |
Marri Devender Rao | 9c966e0 | 2018-01-22 05:55:10 -0600 | [diff] [blame] | 1027 | |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 1028 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Marri Devender Rao | 9c966e0 | 2018-01-22 05:55:10 -0600 | [diff] [blame] | 1029 | int64_t power = 0; |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 1030 | try |
| 1031 | { |
Marri Devender Rao | 9c966e0 | 2018-01-22 05:55:10 -0600 | [diff] [blame] | 1032 | power = getPowerReading(bus); |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 1033 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 1034 | catch (const InternalFailure& e) |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 1035 | { |
| 1036 | log<level::ERR>("Error in reading power sensor value", |
| 1037 | entry("INTERFACE=%s", SENSOR_VALUE_INTF), |
| 1038 | entry("PROPERTY=%s", SENSOR_VALUE_PROP)); |
| 1039 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 1040 | } |
Marri Devender Rao | 9c966e0 | 2018-01-22 05:55:10 -0600 | [diff] [blame] | 1041 | |
| 1042 | // TODO: openbmc/openbmc#2819 |
Gunnar Mills | 8466b79 | 2018-03-23 12:18:12 -0500 | [diff] [blame] | 1043 | // Minimum, Maximum, Average power, TimeFrame, TimeStamp, |
Marri Devender Rao | 9c966e0 | 2018-01-22 05:55:10 -0600 | [diff] [blame] | 1044 | // PowerReadingState readings need to be populated |
| 1045 | // after Telemetry changes. |
| 1046 | uint16_t totalPower = static_cast<uint16_t>(power); |
| 1047 | responseData->currentPower = totalPower; |
| 1048 | responseData->minimumPower = totalPower; |
| 1049 | responseData->maximumPower = totalPower; |
| 1050 | responseData->averagePower = totalPower; |
| 1051 | |
| 1052 | *data_len = sizeof(*responseData); |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 1053 | return rc; |
| 1054 | } |
| 1055 | |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1056 | namespace dcmi |
| 1057 | { |
| 1058 | namespace sensor_info |
| 1059 | { |
| 1060 | |
Deepak Kodihalli | dd4cff1 | 2018-02-06 06:48:29 -0600 | [diff] [blame] | 1061 | Response createFromJson(const Json& config) |
| 1062 | { |
| 1063 | Response response{}; |
| 1064 | uint16_t recordId = config.value("record_id", 0); |
| 1065 | response.recordIdLsb = recordId & 0xFF; |
| 1066 | response.recordIdMsb = (recordId >> 8) & 0xFF; |
| 1067 | return response; |
| 1068 | } |
| 1069 | |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1070 | std::tuple<Response, NumInstances> read(const std::string& type, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1071 | uint8_t instance, const Json& config) |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1072 | { |
Deepak Kodihalli | dd4cff1 | 2018-02-06 06:48:29 -0600 | [diff] [blame] | 1073 | Response response{}; |
| 1074 | |
| 1075 | if (!instance) |
| 1076 | { |
| 1077 | log<level::ERR>("Expected non-zero instance"); |
| 1078 | elog<InternalFailure>(); |
| 1079 | } |
| 1080 | |
| 1081 | static const std::vector<Json> empty{}; |
| 1082 | std::vector<Json> readings = config.value(type, empty); |
| 1083 | size_t numInstances = readings.size(); |
| 1084 | for (const auto& reading : readings) |
| 1085 | { |
| 1086 | uint8_t instanceNum = reading.value("instance", 0); |
| 1087 | // Not the instance we're interested in |
| 1088 | if (instanceNum != instance) |
| 1089 | { |
| 1090 | continue; |
| 1091 | } |
| 1092 | |
| 1093 | response = createFromJson(reading); |
| 1094 | |
| 1095 | // Found the instance we're interested in |
| 1096 | break; |
| 1097 | } |
| 1098 | |
| 1099 | if (numInstances > maxInstances) |
| 1100 | { |
| 1101 | log<level::DEBUG>("Trimming IPMI num instances", |
| 1102 | entry("NUM_INSTANCES=%d", numInstances)); |
| 1103 | numInstances = maxInstances; |
| 1104 | } |
| 1105 | return std::make_tuple(response, numInstances); |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1106 | } |
| 1107 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1108 | std::tuple<ResponseList, NumInstances> |
| 1109 | readAll(const std::string& type, uint8_t instanceStart, const Json& config) |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1110 | { |
Deepak Kodihalli | dd4cff1 | 2018-02-06 06:48:29 -0600 | [diff] [blame] | 1111 | ResponseList responses{}; |
| 1112 | |
| 1113 | size_t numInstances = 0; |
| 1114 | static const std::vector<Json> empty{}; |
| 1115 | std::vector<Json> readings = config.value(type, empty); |
| 1116 | numInstances = readings.size(); |
| 1117 | for (const auto& reading : readings) |
| 1118 | { |
| 1119 | try |
| 1120 | { |
| 1121 | // Max of 8 records |
| 1122 | if (responses.size() == maxRecords) |
| 1123 | { |
| 1124 | break; |
| 1125 | } |
| 1126 | |
| 1127 | uint8_t instanceNum = reading.value("instance", 0); |
| 1128 | // Not in the instance range we're interested in |
| 1129 | if (instanceNum < instanceStart) |
| 1130 | { |
| 1131 | continue; |
| 1132 | } |
| 1133 | |
| 1134 | Response response = createFromJson(reading); |
| 1135 | responses.push_back(response); |
| 1136 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 1137 | catch (const std::exception& e) |
Deepak Kodihalli | dd4cff1 | 2018-02-06 06:48:29 -0600 | [diff] [blame] | 1138 | { |
| 1139 | log<level::DEBUG>(e.what()); |
| 1140 | continue; |
| 1141 | } |
| 1142 | } |
| 1143 | |
| 1144 | if (numInstances > maxInstances) |
| 1145 | { |
| 1146 | log<level::DEBUG>("Trimming IPMI num instances", |
| 1147 | entry("NUM_INSTANCES=%d", numInstances)); |
| 1148 | numInstances = maxInstances; |
| 1149 | } |
| 1150 | return std::make_tuple(responses, numInstances); |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1151 | } |
| 1152 | |
| 1153 | } // namespace sensor_info |
| 1154 | } // namespace dcmi |
| 1155 | |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 1156 | ipmi_ret_t getSensorInfo(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, |
| 1157 | ipmi_response_t response, ipmi_data_len_t data_len, |
| 1158 | ipmi_context_t) |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1159 | { |
| 1160 | auto requestData = |
| 1161 | reinterpret_cast<const dcmi::GetSensorInfoRequest*>(request); |
| 1162 | auto responseData = |
| 1163 | reinterpret_cast<dcmi::GetSensorInfoResponseHdr*>(response); |
| 1164 | |
| 1165 | if (*data_len != sizeof(dcmi::GetSensorInfoRequest)) |
| 1166 | { |
| 1167 | log<level::ERR>("Malformed request data", |
| 1168 | entry("DATA_SIZE=%d", *data_len)); |
| 1169 | return IPMI_CC_REQ_DATA_LEN_INVALID; |
| 1170 | } |
| 1171 | *data_len = 0; |
| 1172 | |
| 1173 | auto it = dcmi::entityIdToName.find(requestData->entityId); |
| 1174 | if (it == dcmi::entityIdToName.end()) |
| 1175 | { |
| 1176 | log<level::ERR>("Unknown Entity ID", |
| 1177 | entry("ENTITY_ID=%d", requestData->entityId)); |
| 1178 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 1179 | } |
| 1180 | |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1181 | if (requestData->sensorType != dcmi::temperatureSensorType) |
| 1182 | { |
| 1183 | log<level::ERR>("Invalid sensor type", |
| 1184 | entry("SENSOR_TYPE=%d", requestData->sensorType)); |
| 1185 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 1186 | } |
| 1187 | |
| 1188 | dcmi::sensor_info::ResponseList sensors{}; |
| 1189 | static dcmi::Json config{}; |
| 1190 | static bool parsed = false; |
| 1191 | |
| 1192 | try |
| 1193 | { |
| 1194 | if (!parsed) |
| 1195 | { |
Kirill Pakhomov | a257362 | 2018-11-02 19:00:18 +0300 | [diff] [blame] | 1196 | config = dcmi::parseJSONConfig(dcmi::gDCMISensorsConfig); |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1197 | parsed = true; |
| 1198 | } |
| 1199 | |
| 1200 | if (!requestData->entityInstance) |
| 1201 | { |
| 1202 | // Read all instances |
| 1203 | std::tie(sensors, responseData->numInstances) = |
| 1204 | dcmi::sensor_info::readAll(it->second, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1205 | requestData->instanceStart, config); |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1206 | } |
| 1207 | else |
| 1208 | { |
| 1209 | // Read one instance |
| 1210 | sensors.resize(1); |
| 1211 | std::tie(sensors[0], responseData->numInstances) = |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1212 | dcmi::sensor_info::read(it->second, requestData->entityInstance, |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1213 | config); |
| 1214 | } |
| 1215 | responseData->numRecords = sensors.size(); |
| 1216 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 1217 | catch (const InternalFailure& e) |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1218 | { |
| 1219 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 1220 | } |
| 1221 | |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1222 | size_t payloadSize = sensors.size() * sizeof(dcmi::sensor_info::Response); |
| 1223 | if (!sensors.empty()) |
| 1224 | { |
| 1225 | memcpy(responseData + 1, // copy payload right after the response header |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1226 | sensors.data(), payloadSize); |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1227 | } |
| 1228 | *data_len = sizeof(dcmi::GetSensorInfoResponseHdr) + payloadSize; |
| 1229 | |
| 1230 | return IPMI_CC_OK; |
| 1231 | } |
| 1232 | |
Chris Austen | 1810bec | 2015-10-13 12:12:39 -0500 | [diff] [blame] | 1233 | void register_netfn_dcmi_functions() |
| 1234 | { |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1235 | // <Get Power Limit> |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 1236 | registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI, |
| 1237 | ipmi::dcmi::cmdGetPowerLimit, ipmi::Privilege::User, |
| 1238 | getPowerLimit); |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 1239 | |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 1240 | // <Set Power Limit> |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 1241 | registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI, |
| 1242 | ipmi::dcmi::cmdSetPowerLimit, |
| 1243 | ipmi::Privilege::Operator, setPowerLimit); |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 1244 | |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 1245 | // <Activate/Deactivate Power Limit> |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 1246 | registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI, |
| 1247 | ipmi::dcmi::cmdActDeactivatePwrLimit, |
| 1248 | ipmi::Privilege::Operator, applyPowerLimit); |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 1249 | |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 1250 | // <Get Asset Tag> |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 1251 | registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI, |
| 1252 | ipmi::dcmi::cmdGetAssetTag, ipmi::Privilege::User, |
| 1253 | getAssetTag); |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 1254 | |
| 1255 | // <Set Asset Tag> |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame] | 1256 | registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI, |
| 1257 | ipmi::dcmi::cmdSetAssetTag, ipmi::Privilege::Operator, |
| 1258 | setAssetTag); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 1259 | |
Gunnar Mills | 8991dd6 | 2017-10-25 17:11:29 -0500 | [diff] [blame] | 1260 | // <Get Management Controller Identifier String> |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 1261 | registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI, |
| 1262 | ipmi::dcmi::cmdGetMgmtCntlrIdString, |
| 1263 | ipmi::Privilege::User, getMgmntCtrlIdStr); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 1264 | |
| 1265 | // <Set Management Controller Identifier String> |
Vernon Mauery | efb5ae5 | 2023-07-27 11:35:43 -0700 | [diff] [blame] | 1266 | registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI, |
| 1267 | ipmi::dcmi::cmdSetMgmtCntlrIdString, |
| 1268 | ipmi::Privilege::Admin, setMgmntCtrlIdStr); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 1269 | |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 1270 | // <Get DCMI capabilities> |
Vernon Mauery | f038dc0 | 2023-07-27 14:06:11 -0700 | [diff] [blame] | 1271 | registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI, |
| 1272 | ipmi::dcmi::cmdGetDcmiCapabilitiesInfo, |
| 1273 | ipmi::Privilege::User, getDCMICapabilities); |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 1274 | |
| 1275 | // <Get Temperature Readings> |
Vernon Mauery | cce9ffd | 2023-07-27 14:15:17 -0700 | [diff] [blame^] | 1276 | registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI, |
| 1277 | ipmi::dcmi::cmdGetTemperatureReadings, |
| 1278 | ipmi::Privilege::User, getTempReadings); |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 1279 | |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 1280 | // <Get Power Reading> |
| 1281 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_POWER_READING, |
| 1282 | NULL, getPowerReading, PRIVILEGE_USER); |
adarshgrami | 042e9db | 2022-09-15 10:34:34 +0530 | [diff] [blame] | 1283 | // The Get sensor should get the senor details dynamically when |
| 1284 | // FEATURE_DYNAMIC_SENSORS is enabled. |
| 1285 | #ifndef FEATURE_DYNAMIC_SENSORS |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1286 | // <Get Sensor Info> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1287 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_SENSOR_INFO, NULL, |
Chau Ly | d74df5f | 2023-05-25 10:33:00 +0000 | [diff] [blame] | 1288 | getSensorInfo, PRIVILEGE_OPERATOR); |
adarshgrami | 042e9db | 2022-09-15 10:34:34 +0530 | [diff] [blame] | 1289 | #endif |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 1290 | // <Get DCMI Configuration Parameters> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1291 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_CONF_PARAMS, NULL, |
| 1292 | getDCMIConfParams, PRIVILEGE_USER); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 1293 | |
| 1294 | // <Set DCMI Configuration Parameters> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1295 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::SET_CONF_PARAMS, NULL, |
| 1296 | setDCMIConfParams, PRIVILEGE_ADMIN); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 1297 | |
Chris Austen | 1810bec | 2015-10-13 12:12:39 -0500 | [diff] [blame] | 1298 | return; |
| 1299 | } |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1300 | // 956379 |