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