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_PARAMETER_REVISION = 2; |
| 36 | constexpr auto DCMI_SPEC_MAJOR_VERSION = 1; |
| 37 | constexpr auto DCMI_SPEC_MINOR_VERSION = 5; |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 38 | constexpr auto DCMI_CONFIG_PARAMETER_REVISION = 1; |
| 39 | constexpr auto DCMI_RAND_BACK_OFF_MASK = 0x80; |
| 40 | constexpr auto DCMI_OPTION_60_43_MASK = 0x02; |
| 41 | constexpr auto DCMI_OPTION_12_MASK = 0x01; |
| 42 | constexpr auto DCMI_ACTIVATE_DHCP_MASK = 0x01; |
| 43 | constexpr auto DCMI_ACTIVATE_DHCP_REPLY = 0x00; |
William A. Kennington III | 5d06cc6 | 2019-04-25 02:10:55 -0700 | [diff] [blame] | 44 | constexpr auto DCMI_SET_CONF_PARAM_REQ_PACKET_MAX_SIZE = 0x04; |
| 45 | constexpr auto DCMI_SET_CONF_PARAM_REQ_PACKET_MIN_SIZE = 0x03; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 46 | constexpr auto DHCP_TIMING1 = 0x04; // 4 sec |
| 47 | constexpr auto DHCP_TIMING2_UPPER = 0x00; // 2 min |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 48 | constexpr auto DHCP_TIMING2_LOWER = 0x78; |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 49 | constexpr auto DHCP_TIMING3_UPPER = 0x00; // 64 sec |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 50 | constexpr auto DHCP_TIMING3_LOWER = 0x40; |
| 51 | // When DHCP Option 12 is enabled the string "SendHostName=true" will be |
| 52 | // added into n/w configuration file and the parameter |
| 53 | // SendHostNameEnabled will set to true. |
| 54 | constexpr auto DHCP_OPT12_ENABLED = "SendHostNameEnabled"; |
| 55 | |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 56 | constexpr auto SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value"; |
| 57 | constexpr auto SENSOR_VALUE_PROP = "Value"; |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 58 | |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 59 | using namespace phosphor::logging; |
| 60 | |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 61 | namespace dcmi |
| 62 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 63 | constexpr auto assetTagMaxOffset = 62; |
| 64 | constexpr auto assetTagMaxSize = 63; |
| 65 | constexpr auto maxBytes = 16; |
| 66 | constexpr size_t maxCtrlIdStrLen = 63; |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 67 | |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 68 | // Refer Table 6-14, DCMI Entity ID Extension, DCMI v1.5 spec |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 69 | static const std::map<uint8_t, std::string> entityIdToName{ |
| 70 | {0x40, "inlet"}, {0x37, "inlet"}, {0x41, "cpu"}, |
| 71 | {0x03, "cpu"}, {0x42, "baseboard"}, {0x07, "baseboard"}}; |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 72 | |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 73 | bool isDCMIPowerMgmtSupported() |
| 74 | { |
Vernon Mauery | f4eb35d | 2023-07-27 11:08:49 -0700 | [diff] [blame] | 75 | static bool parsed = false; |
| 76 | static bool supported = false; |
| 77 | if (!parsed) |
| 78 | { |
| 79 | auto data = parseJSONConfig(gDCMICapabilitiesConfig); |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 80 | |
Vernon Mauery | f4eb35d | 2023-07-27 11:08:49 -0700 | [diff] [blame] | 81 | supported = (gDCMIPowerMgmtSupported == |
| 82 | data.value(gDCMIPowerMgmtCapability, 0)); |
| 83 | } |
| 84 | return supported; |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 85 | } |
| 86 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 87 | std::optional<uint32_t> getPcap(ipmi::Context::ptr& ctx) |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 88 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 89 | std::string service{}; |
| 90 | boost::system::error_code ec = ipmi::getService(ctx, pcapInterface, |
| 91 | pcapPath, service); |
| 92 | if (ec.value()) |
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 | return std::nullopt; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 95 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 96 | uint32_t pcap{}; |
| 97 | ec = ipmi::getDbusProperty(ctx, service, pcapPath, pcapInterface, |
| 98 | powerCapProp, pcap); |
| 99 | if (ec.value()) |
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 | log<level::ERR>("Error in getPcap prop", |
| 102 | entry("ERROR=%s", ec.message().c_str())); |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 103 | elog<InternalFailure>(); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 104 | return std::nullopt; |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 105 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 106 | return pcap; |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 107 | } |
| 108 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 109 | std::optional<bool> getPcapEnabled(ipmi::Context::ptr& ctx) |
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 | std::string service{}; |
| 112 | boost::system::error_code ec = ipmi::getService(ctx, pcapInterface, |
| 113 | pcapPath, service); |
| 114 | if (ec.value()) |
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 | return std::nullopt; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 117 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 118 | bool pcapEnabled{}; |
| 119 | ec = ipmi::getDbusProperty(ctx, service, pcapPath, pcapInterface, |
| 120 | powerCapEnableProp, pcapEnabled); |
| 121 | if (ec.value()) |
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 | log<level::ERR>("Error in getPcap prop"); |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 124 | elog<InternalFailure>(); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 125 | return std::nullopt; |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 126 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 127 | return pcapEnabled; |
Andrew Geissler | 50c0c8f | 2017-07-11 16:18:51 -0500 | [diff] [blame] | 128 | } |
Chris Austen | 1810bec | 2015-10-13 12:12:39 -0500 | [diff] [blame] | 129 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 130 | bool setPcap(ipmi::Context::ptr& ctx, const uint32_t powerCap) |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 131 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 132 | std::string service{}; |
| 133 | boost::system::error_code ec = ipmi::getService(ctx, pcapInterface, |
| 134 | pcapPath, service); |
| 135 | if (ec.value()) |
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 | return false; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 138 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 139 | |
| 140 | ec = ipmi::setDbusProperty(ctx, service, pcapPath, pcapInterface, |
| 141 | powerCapProp, powerCap); |
| 142 | if (ec.value()) |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 143 | { |
| 144 | log<level::ERR>("Error in setPcap property", |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 145 | entry("ERROR=%s", ec.message().c_str())); |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 146 | elog<InternalFailure>(); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 147 | return false; |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 148 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 149 | return true; |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 150 | } |
| 151 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 152 | bool setPcapEnable(ipmi::Context::ptr& ctx, bool enabled) |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 153 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 154 | std::string service{}; |
| 155 | boost::system::error_code ec = ipmi::getService(ctx, pcapInterface, |
| 156 | pcapPath, service); |
| 157 | if (ec.value()) |
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 | return false; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 160 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 161 | |
| 162 | ec = ipmi::setDbusProperty(ctx, service, pcapPath, pcapInterface, |
| 163 | powerCapEnableProp, enabled); |
| 164 | if (ec.value()) |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 165 | { |
| 166 | log<level::ERR>("Error in setPcapEnabled property", |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 167 | entry("ERROR=%s", ec.message().c_str())); |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 168 | elog<InternalFailure>(); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 169 | return false; |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 170 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 171 | return true; |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 172 | } |
| 173 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 174 | std::optional<std::string> readAssetTag(ipmi::Context::ptr& ctx) |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 175 | { |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 176 | // Read the object tree with the inventory root to figure out the object |
| 177 | // that has implemented the Asset tag interface. |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 178 | ipmi::DbusObjectInfo objectInfo; |
| 179 | boost::system::error_code ec = getDbusObject( |
| 180 | ctx, dcmi::assetTagIntf, ipmi::sensor::inventoryRoot, "", objectInfo); |
| 181 | if (ec.value()) |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 182 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 183 | return std::nullopt; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 184 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 185 | |
| 186 | std::string assetTag{}; |
| 187 | ec = ipmi::getDbusProperty(ctx, objectInfo.second, objectInfo.first, |
| 188 | dcmi::assetTagIntf, dcmi::assetTagProp, |
| 189 | assetTag); |
| 190 | if (ec.value()) |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 191 | { |
| 192 | log<level::ERR>("Error in reading asset tag", |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 193 | entry("ERROR=%s", ec.message().c_str())); |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 194 | elog<InternalFailure>(); |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 195 | return std::nullopt; |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 196 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 197 | |
| 198 | return assetTag; |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 199 | } |
| 200 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 201 | bool writeAssetTag(ipmi::Context::ptr& ctx, const std::string& assetTag) |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 202 | { |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 203 | // Read the object tree with the inventory root to figure out the object |
| 204 | // that has implemented the Asset tag interface. |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 205 | ipmi::DbusObjectInfo objectInfo; |
| 206 | boost::system::error_code ec = getDbusObject( |
| 207 | ctx, dcmi::assetTagIntf, ipmi::sensor::inventoryRoot, "", objectInfo); |
| 208 | if (ec.value()) |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 209 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 210 | return false; |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 211 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 212 | |
| 213 | ec = ipmi::setDbusProperty(ctx, objectInfo.second, objectInfo.first, |
| 214 | dcmi::assetTagIntf, dcmi::assetTagProp, |
| 215 | assetTag); |
| 216 | if (ec.value()) |
George Liu | 3e3cc35 | 2023-07-26 15:59:31 +0800 | [diff] [blame] | 217 | { |
| 218 | log<level::ERR>("Error in writing asset tag", |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 219 | entry("ERROR=%s", ec.message().c_str())); |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 220 | elog<InternalFailure>(); |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 221 | return false; |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 222 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 223 | return true; |
Tom Joseph | be5b989 | 2017-07-15 00:55:23 +0530 | [diff] [blame] | 224 | } |
| 225 | |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 226 | std::string getHostName(void) |
| 227 | { |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 228 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 229 | |
| 230 | auto service = ipmi::getService(bus, networkConfigIntf, networkConfigObj); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 231 | auto value = ipmi::getDbusProperty(bus, service, networkConfigObj, |
| 232 | networkConfigIntf, hostNameProp); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 233 | |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 234 | return std::get<std::string>(value); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 235 | } |
| 236 | |
Thang Tran | 55cbf55 | 2023-01-31 14:43:02 +0700 | [diff] [blame] | 237 | EthernetInterface::DHCPConf getDHCPEnabled() |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 238 | { |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 239 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 240 | |
Johnathan Mantey | 74a2102 | 2018-12-13 13:17:56 -0800 | [diff] [blame] | 241 | auto ethdevice = ipmi::getChannelName(ethernetDefaultChannelNum); |
Patrick Williams | fbc6c9d | 2023-05-10 07:50:16 -0500 | [diff] [blame] | 242 | auto ethernetObj = ipmi::getDbusObject(bus, ethernetIntf, networkRoot, |
| 243 | ethdevice); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 244 | auto service = ipmi::getService(bus, ethernetIntf, ethernetObj.first); |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 245 | auto value = ipmi::getDbusProperty(bus, service, ethernetObj.first, |
| 246 | ethernetIntf, "DHCPEnabled"); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 247 | |
Thang Tran | 55cbf55 | 2023-01-31 14:43:02 +0700 | [diff] [blame] | 248 | return EthernetInterface::convertDHCPConfFromString( |
| 249 | std::get<std::string>(value)); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | bool getDHCPOption(std::string prop) |
| 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 | |
| 256 | auto service = ipmi::getService(bus, dhcpIntf, dhcpObj); |
| 257 | auto value = ipmi::getDbusProperty(bus, service, dhcpObj, dhcpIntf, prop); |
| 258 | |
Vernon Mauery | f442e11 | 2019-04-09 11:44:36 -0700 | [diff] [blame] | 259 | return std::get<bool>(value); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 260 | } |
| 261 | |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 262 | void setDHCPOption(std::string prop, bool value) |
| 263 | { |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 264 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 265 | |
| 266 | auto service = ipmi::getService(bus, dhcpIntf, dhcpObj); |
| 267 | ipmi::setDbusProperty(bus, service, dhcpObj, dhcpIntf, prop, value); |
| 268 | } |
| 269 | |
Kirill Pakhomov | a257362 | 2018-11-02 19:00:18 +0300 | [diff] [blame] | 270 | Json parseJSONConfig(const std::string& configFile) |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 271 | { |
| 272 | std::ifstream jsonFile(configFile); |
| 273 | if (!jsonFile.is_open()) |
| 274 | { |
| 275 | log<level::ERR>("Temperature readings JSON file not found"); |
| 276 | elog<InternalFailure>(); |
| 277 | } |
| 278 | |
| 279 | auto data = Json::parse(jsonFile, nullptr, false); |
| 280 | if (data.is_discarded()) |
| 281 | { |
| 282 | log<level::ERR>("Temperature readings JSON parser failure"); |
| 283 | elog<InternalFailure>(); |
| 284 | } |
| 285 | |
| 286 | return data; |
| 287 | } |
| 288 | |
Tom Joseph | be5eaa1 | 2017-07-12 19:54:44 +0530 | [diff] [blame] | 289 | } // namespace dcmi |
Chris Austen | 1810bec | 2015-10-13 12:12:39 -0500 | [diff] [blame] | 290 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 291 | constexpr uint8_t exceptionPowerOff = 0x01; |
| 292 | ipmi::RspType<uint16_t, // reserved |
| 293 | uint8_t, // exception actions |
| 294 | uint16_t, // power limit requested in watts |
| 295 | uint32_t, // correction time in milliseconds |
| 296 | uint16_t, // reserved |
| 297 | uint16_t // statistics sampling period in seconds |
| 298 | > |
| 299 | getPowerLimit(ipmi::Context::ptr ctx, uint16_t reserved) |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 300 | { |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 301 | if (!dcmi::isDCMIPowerMgmtSupported()) |
| 302 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 303 | return ipmi::responseInvalidCommand(); |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 304 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 305 | if (reserved) |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 306 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 307 | return ipmi::responseInvalidFieldRequest(); |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 308 | } |
| 309 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 310 | std::optional<uint16_t> pcapValue = dcmi::getPcap(ctx); |
| 311 | std::optional<bool> pcapEnable = dcmi::getPcapEnabled(ctx); |
| 312 | if (!pcapValue || !pcapEnable) |
| 313 | { |
| 314 | return ipmi::responseUnspecifiedError(); |
| 315 | } |
| 316 | |
| 317 | constexpr uint16_t reserved1{}; |
| 318 | constexpr uint16_t reserved2{}; |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 319 | /* |
| 320 | * Exception action if power limit is exceeded and cannot be controlled |
| 321 | * with the correction time limit is hardcoded to Hard Power Off system |
| 322 | * and log event to SEL. |
| 323 | */ |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 324 | constexpr uint8_t exception = exceptionPowerOff; |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 325 | /* |
| 326 | * Correction time limit and Statistics sampling period is currently not |
| 327 | * populated. |
| 328 | */ |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 329 | constexpr uint32_t correctionTime{}; |
| 330 | constexpr uint16_t statsPeriod{}; |
| 331 | if (!pcapEnable) |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 332 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 333 | constexpr ipmi::Cc responseNoPowerLimitSet = 0x80; |
| 334 | constexpr uint16_t noPcap{}; |
| 335 | return ipmi::response(responseNoPowerLimitSet, reserved1, exception, |
| 336 | noPcap, correctionTime, reserved2, statsPeriod); |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 337 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 338 | return ipmi::responseSuccess(reserved1, exception, *pcapValue, |
| 339 | correctionTime, reserved2, statsPeriod); |
Tom Joseph | b9d86f4 | 2017-07-26 18:03:47 +0530 | [diff] [blame] | 340 | } |
| 341 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 342 | ipmi::RspType<> setPowerLimit(ipmi::Context::ptr& ctx, uint16_t reserved1, |
| 343 | uint8_t exceptionAction, uint16_t powerLimit, |
| 344 | uint32_t correctionTime, uint16_t reserved2, |
| 345 | uint16_t statsPeriod) |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 346 | { |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 347 | if (!dcmi::isDCMIPowerMgmtSupported()) |
| 348 | { |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 349 | log<level::ERR>("DCMI Power management is unsupported!"); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 350 | return ipmi::responseInvalidCommand(); |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 351 | } |
| 352 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 353 | // Only process the power limit requested in watts. Return errors |
| 354 | // for other fields that are set |
| 355 | if (reserved1 || reserved2 || correctionTime || statsPeriod || |
| 356 | exceptionAction != exceptionPowerOff) |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 357 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 358 | return ipmi::responseInvalidFieldRequest(); |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 359 | } |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 360 | |
| 361 | if (!dcmi::setPcap(ctx, powerLimit)) |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 362 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 363 | return ipmi::responseUnspecifiedError(); |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 364 | } |
| 365 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 366 | log<level::INFO>("Set Power Cap", entry("POWERCAP=%u", powerLimit)); |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 367 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 368 | return ipmi::responseSuccess(); |
Tom Joseph | 46fa37d | 2017-07-26 18:11:55 +0530 | [diff] [blame] | 369 | } |
| 370 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 371 | ipmi::RspType<> applyPowerLimit(ipmi::Context::ptr& ctx, bool enabled, |
| 372 | uint7_t reserved1, uint16_t reserved2) |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 373 | { |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 374 | if (!dcmi::isDCMIPowerMgmtSupported()) |
| 375 | { |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 376 | log<level::ERR>("DCMI Power management is unsupported!"); |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 377 | return ipmi::responseInvalidCommand(); |
| 378 | } |
| 379 | if (reserved1 || reserved2) |
| 380 | { |
| 381 | return ipmi::responseInvalidFieldRequest(); |
Kirill Pakhomov | 2c2af2c | 2018-11-06 16:06:10 +0300 | [diff] [blame] | 382 | } |
| 383 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 384 | if (!dcmi::setPcapEnable(ctx, enabled)) |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 385 | { |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 386 | return ipmi::responseUnspecifiedError(); |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | log<level::INFO>("Set Power Cap Enable", |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 390 | entry("POWERCAPENABLE=%u", static_cast<uint8_t>(enabled))); |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 391 | |
Vernon Mauery | d4222fd | 2023-07-27 11:26:51 -0700 | [diff] [blame] | 392 | return ipmi::responseSuccess(); |
Tom Joseph | 6c8d51b | 2017-07-26 18:18:06 +0530 | [diff] [blame] | 393 | } |
| 394 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 395 | ipmi::RspType<uint8_t, // total tag length |
| 396 | std::vector<char> // tag data |
| 397 | > |
| 398 | getAssetTag(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count) |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 399 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 400 | // Verify offset to read and number of bytes to read are not exceeding |
| 401 | // the range. |
| 402 | if ((offset > dcmi::assetTagMaxOffset) || (count > dcmi::maxBytes) || |
| 403 | ((offset + count) > dcmi::assetTagMaxSize)) |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 404 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 405 | return ipmi::responseParmOutOfRange(); |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 406 | } |
| 407 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 408 | std::optional<std::string> assetTagResp = dcmi::readAssetTag(ctx); |
| 409 | if (!assetTagResp) |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 410 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 411 | return ipmi::responseUnspecifiedError(); |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 412 | } |
| 413 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 414 | std::string& assetTag = assetTagResp.value(); |
| 415 | // If the asset tag is longer than 63 bytes, restrict it to 63 bytes to |
| 416 | // suit Get Asset Tag command. |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 417 | if (assetTag.size() > dcmi::assetTagMaxSize) |
| 418 | { |
| 419 | assetTag.resize(dcmi::assetTagMaxSize); |
| 420 | } |
| 421 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 422 | if (offset >= assetTag.size()) |
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::responseParmOutOfRange(); |
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 | // silently truncate reads beyond the end of assetTag |
| 428 | if ((offset + count) >= assetTag.size()) |
| 429 | { |
| 430 | count = assetTag.size() - offset; |
| 431 | } |
Tom Joseph | 6f6dd4d | 2017-07-12 20:07:11 +0530 | [diff] [blame] | 432 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 433 | auto totalTagSize = static_cast<uint8_t>(assetTag.size()); |
| 434 | std::vector<char> data{assetTag.begin() + offset, |
| 435 | assetTag.begin() + offset + count}; |
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::responseSuccess(totalTagSize, data); |
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 | ipmi::RspType<uint8_t // new asset tag length |
| 441 | > |
| 442 | setAssetTag(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count, |
| 443 | const std::vector<char>& data) |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 444 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 445 | // Verify offset to read and number of bytes to read are not exceeding |
| 446 | // the range. |
| 447 | if ((offset > dcmi::assetTagMaxOffset) || (count > dcmi::maxBytes) || |
| 448 | ((offset + count) > dcmi::assetTagMaxSize)) |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 449 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 450 | return ipmi::responseParmOutOfRange(); |
| 451 | } |
| 452 | if (data.size() != count) |
| 453 | { |
| 454 | return ipmi::responseReqDataLenInvalid(); |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 455 | } |
| 456 | |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 457 | std::optional<std::string> assetTagResp = dcmi::readAssetTag(ctx); |
| 458 | if (!assetTagResp) |
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 | return ipmi::responseUnspecifiedError(); |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 461 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 462 | |
| 463 | std::string& assetTag = assetTagResp.value(); |
| 464 | |
| 465 | if (offset > assetTag.size()) |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 466 | { |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 467 | return ipmi::responseParmOutOfRange(); |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 468 | } |
Vernon Mauery | dca4720 | 2023-07-27 11:32:01 -0700 | [diff] [blame^] | 469 | |
| 470 | // operation is to truncate at offset and append new data |
| 471 | assetTag.resize(offset); |
| 472 | assetTag.append(data.begin(), data.end()); |
| 473 | |
| 474 | if (!dcmi::writeAssetTag(ctx, assetTag)) |
| 475 | { |
| 476 | return ipmi::responseUnspecifiedError(); |
| 477 | } |
| 478 | |
| 479 | auto totalTagSize = static_cast<uint8_t>(assetTag.size()); |
| 480 | return ipmi::responseSuccess(totalTagSize); |
Tom Joseph | 545dd23 | 2017-07-12 20:20:49 +0530 | [diff] [blame] | 481 | } |
| 482 | |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 483 | ipmi_ret_t getMgmntCtrlIdStr(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, |
| 484 | ipmi_response_t response, ipmi_data_len_t data_len, |
| 485 | ipmi_context_t) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 486 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 487 | auto requestData = |
| 488 | reinterpret_cast<const dcmi::GetMgmntCtrlIdStrRequest*>(request); |
| 489 | auto responseData = |
| 490 | reinterpret_cast<dcmi::GetMgmntCtrlIdStrResponse*>(response); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 491 | std::string hostName; |
| 492 | |
| 493 | *data_len = 0; |
| 494 | |
William A. Kennington III | 5d06cc6 | 2019-04-25 02:10:55 -0700 | [diff] [blame] | 495 | if (requestData->bytes > dcmi::maxBytes || |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 496 | requestData->offset + requestData->bytes > dcmi::maxCtrlIdStrLen) |
| 497 | { |
| 498 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 499 | } |
| 500 | |
| 501 | try |
| 502 | { |
| 503 | hostName = dcmi::getHostName(); |
| 504 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 505 | catch (const InternalFailure& e) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 506 | { |
| 507 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 508 | } |
| 509 | |
| 510 | if (requestData->offset > hostName.length()) |
| 511 | { |
| 512 | return IPMI_CC_PARM_OUT_OF_RANGE; |
| 513 | } |
| 514 | auto responseStr = hostName.substr(requestData->offset, requestData->bytes); |
| 515 | auto responseStrLen = std::min(static_cast<std::size_t>(requestData->bytes), |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 516 | responseStr.length() + 1); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 517 | responseData->strLen = hostName.length(); |
| 518 | std::copy(begin(responseStr), end(responseStr), responseData->data); |
| 519 | |
| 520 | *data_len = sizeof(*responseData) + responseStrLen; |
| 521 | return IPMI_CC_OK; |
| 522 | } |
| 523 | |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 524 | ipmi_ret_t setMgmntCtrlIdStr(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, |
| 525 | ipmi_response_t response, ipmi_data_len_t data_len, |
| 526 | ipmi_context_t) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 527 | { |
| 528 | static std::array<char, dcmi::maxCtrlIdStrLen + 1> newCtrlIdStr; |
| 529 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 530 | auto requestData = |
| 531 | reinterpret_cast<const dcmi::SetMgmntCtrlIdStrRequest*>(request); |
| 532 | auto responseData = |
| 533 | reinterpret_cast<dcmi::SetMgmntCtrlIdStrResponse*>(response); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 534 | |
| 535 | *data_len = 0; |
| 536 | |
William A. Kennington III | 5d06cc6 | 2019-04-25 02:10:55 -0700 | [diff] [blame] | 537 | if (requestData->bytes > dcmi::maxBytes || |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 538 | requestData->offset + requestData->bytes > dcmi::maxCtrlIdStrLen + 1 || |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 539 | (requestData->offset + requestData->bytes == |
| 540 | dcmi::maxCtrlIdStrLen + 1 && |
| 541 | requestData->data[requestData->bytes - 1] != '\0')) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 542 | { |
| 543 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 544 | } |
| 545 | |
| 546 | try |
| 547 | { |
| 548 | /* if there is no old value and offset is not 0 */ |
| 549 | if (newCtrlIdStr[0] == '\0' && requestData->offset != 0) |
| 550 | { |
| 551 | /* read old ctrlIdStr */ |
| 552 | auto hostName = dcmi::getHostName(); |
| 553 | hostName.resize(dcmi::maxCtrlIdStrLen); |
| 554 | std::copy(begin(hostName), end(hostName), begin(newCtrlIdStr)); |
| 555 | newCtrlIdStr[hostName.length()] = '\0'; |
| 556 | } |
| 557 | |
| 558 | /* replace part of string and mark byte after the last as \0 */ |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 559 | auto restStrIter = |
| 560 | std::copy_n(requestData->data, requestData->bytes, |
| 561 | begin(newCtrlIdStr) + requestData->offset); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 562 | /* if the last written byte is not 64th - add '\0' */ |
| 563 | if (requestData->offset + requestData->bytes <= dcmi::maxCtrlIdStrLen) |
| 564 | { |
| 565 | *restStrIter = '\0'; |
| 566 | } |
| 567 | |
| 568 | /* if input data contains '\0' whole string is sent - update hostname */ |
| 569 | auto it = std::find(requestData->data, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 570 | requestData->data + requestData->bytes, '\0'); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 571 | if (it != requestData->data + requestData->bytes) |
| 572 | { |
Patrick Williams | 5d82f47 | 2022-07-22 19:26:53 -0500 | [diff] [blame] | 573 | sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()}; |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 574 | ipmi::setDbusProperty(bus, dcmi::networkServiceName, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 575 | dcmi::networkConfigObj, |
| 576 | dcmi::networkConfigIntf, dcmi::hostNameProp, |
| 577 | std::string(newCtrlIdStr.data())); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 578 | } |
| 579 | } |
Patrick Williams | a2ad2da | 2021-10-06 12:21:46 -0500 | [diff] [blame] | 580 | catch (const InternalFailure& e) |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 581 | { |
| 582 | *data_len = 0; |
| 583 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 584 | } |
| 585 | |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 586 | responseData->offset = requestData->offset + requestData->bytes; |
| 587 | *data_len = sizeof(*responseData); |
| 588 | return IPMI_CC_OK; |
| 589 | } |
| 590 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 591 | // List of the capabilities under each parameter |
| 592 | dcmi::DCMICaps dcmiCaps = { |
| 593 | // Supported DCMI Capabilities |
| 594 | {dcmi::DCMICapParameters::SUPPORTED_DCMI_CAPS, |
| 595 | {3, |
| 596 | {{"PowerManagement", 2, 0, 1}, |
| 597 | {"OOBSecondaryLan", 3, 2, 1}, |
| 598 | {"SerialTMODE", 3, 1, 1}, |
| 599 | {"InBandSystemInterfaceChannel", 3, 0, 1}}}}, |
| 600 | // Mandatory Platform Attributes |
| 601 | {dcmi::DCMICapParameters::MANDATORY_PLAT_ATTRIBUTES, |
| 602 | {5, |
| 603 | {{"SELAutoRollOver", 1, 15, 1}, |
| 604 | {"FlushEntireSELUponRollOver", 1, 14, 1}, |
| 605 | {"RecordLevelSELFlushUponRollOver", 1, 13, 1}, |
| 606 | {"NumberOfSELEntries", 1, 0, 12}, |
| 607 | {"TempMonitoringSamplingFreq", 5, 0, 8}}}}, |
| 608 | // Optional Platform Attributes |
| 609 | {dcmi::DCMICapParameters::OPTIONAL_PLAT_ATTRIBUTES, |
| 610 | {2, |
| 611 | {{"PowerMgmtDeviceSlaveAddress", 1, 1, 7}, |
| 612 | {"BMCChannelNumber", 2, 4, 4}, |
| 613 | {"DeviceRivision", 2, 0, 4}}}}, |
| 614 | // Manageability Access Attributes |
| 615 | {dcmi::DCMICapParameters::MANAGEABILITY_ACCESS_ATTRIBUTES, |
| 616 | {3, |
| 617 | {{"MandatoryPrimaryLanOOBSupport", 1, 0, 8}, |
| 618 | {"OptionalSecondaryLanOOBSupport", 2, 0, 8}, |
| 619 | {"OptionalSerialOOBMTMODECapability", 3, 0, 8}}}}}; |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 620 | |
Willy Tu | 11d6889 | 2022-01-20 10:37:34 -0800 | [diff] [blame] | 621 | ipmi_ret_t getDCMICapabilities(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request, |
| 622 | ipmi_response_t response, |
| 623 | ipmi_data_len_t data_len, ipmi_context_t) |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 624 | { |
Kirill Pakhomov | a257362 | 2018-11-02 19:00:18 +0300 | [diff] [blame] | 625 | std::ifstream dcmiCapFile(dcmi::gDCMICapabilitiesConfig); |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 626 | if (!dcmiCapFile.is_open()) |
| 627 | { |
| 628 | log<level::ERR>("DCMI Capabilities file not found"); |
| 629 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 630 | } |
| 631 | |
| 632 | auto data = nlohmann::json::parse(dcmiCapFile, nullptr, false); |
| 633 | if (data.is_discarded()) |
| 634 | { |
| 635 | log<level::ERR>("DCMI Capabilities JSON parser failure"); |
| 636 | return IPMI_CC_UNSPECIFIED_ERROR; |
| 637 | } |
| 638 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 639 | auto requestData = |
| 640 | reinterpret_cast<const dcmi::GetDCMICapRequest*>(request); |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 641 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 642 | // get list of capabilities in a parameter |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 643 | auto caps = |
| 644 | dcmiCaps.find(static_cast<dcmi::DCMICapParameters>(requestData->param)); |
| 645 | if (caps == dcmiCaps.end()) |
| 646 | { |
| 647 | log<level::ERR>("Invalid input parameter"); |
| 648 | return IPMI_CC_INVALID_FIELD_REQUEST; |
| 649 | } |
| 650 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 651 | auto responseData = reinterpret_cast<dcmi::GetDCMICapResponse*>(response); |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 652 | |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 653 | // For each capabilities in a parameter fill the data from |
| 654 | // the json file based on the capability name. |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 655 | for (auto cap : caps->second.capList) |
| 656 | { |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 657 | // If the data is beyond first byte boundary, insert in a |
| 658 | // 16bit pattern for example number of SEL entries are represented |
| 659 | // in 12bits. |
Kirill Pakhomov | db5d9b0 | 2018-11-06 19:17:51 +0300 | [diff] [blame] | 660 | if ((cap.length + cap.position) > dcmi::gByteBitSize) |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 661 | { |
Kirill Pakhomov | db5d9b0 | 2018-11-06 19:17:51 +0300 | [diff] [blame] | 662 | uint16_t val = data.value(cap.name.c_str(), 0); |
| 663 | // According to DCMI spec v1.5, max number of SEL entries is |
| 664 | // 4096, but bit 12b of DCMI capabilities Mandatory Platform |
| 665 | // Attributes field is reserved and therefore we can use only |
| 666 | // the provided 12 bits with maximum value of 4095. |
| 667 | // We're playing safe here by applying the mask |
| 668 | // to ensure that provided value will fit into 12 bits. |
| 669 | if (cap.length > dcmi::gByteBitSize) |
| 670 | { |
| 671 | val &= dcmi::gMaxSELEntriesMask; |
| 672 | } |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 673 | val <<= cap.position; |
Kirill Pakhomov | db5d9b0 | 2018-11-06 19:17:51 +0300 | [diff] [blame] | 674 | responseData->data[cap.bytePosition - 1] |= |
| 675 | static_cast<uint8_t>(val); |
| 676 | responseData->data[cap.bytePosition] |= val >> dcmi::gByteBitSize; |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 677 | } |
| 678 | else |
| 679 | { |
| 680 | responseData->data[cap.bytePosition - 1] |= |
| 681 | data.value(cap.name.c_str(), 0) << cap.position; |
| 682 | } |
| 683 | } |
| 684 | |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 685 | responseData->major = DCMI_SPEC_MAJOR_VERSION; |
| 686 | responseData->minor = DCMI_SPEC_MINOR_VERSION; |
| 687 | responseData->paramRevision = DCMI_PARAMETER_REVISION; |
| 688 | *data_len = sizeof(*responseData) + caps->second.size; |
| 689 | |
| 690 | return IPMI_CC_OK; |
| 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> |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 1350 | |
| 1351 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_MGMNT_CTRL_ID_STR, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1352 | NULL, getMgmntCtrlIdStr, PRIVILEGE_USER); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 1353 | |
| 1354 | // <Set Management Controller Identifier String> |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 1355 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::SET_MGMNT_CTRL_ID_STR, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1356 | NULL, setMgmntCtrlIdStr, PRIVILEGE_ADMIN); |
Vladislav Vovchenko | 8f7a6f6 | 2017-08-17 00:31:14 +0300 | [diff] [blame] | 1357 | |
Dhruvaraj Subhashchandran | e29be41 | 2018-01-16 05:11:56 -0600 | [diff] [blame] | 1358 | // <Get DCMI capabilities> |
| 1359 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_CAPABILITIES, |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1360 | NULL, getDCMICapabilities, PRIVILEGE_USER); |
Deepak Kodihalli | ee717d7 | 2018-01-24 04:53:09 -0600 | [diff] [blame] | 1361 | |
| 1362 | // <Get Temperature Readings> |
| 1363 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_TEMP_READINGS, |
| 1364 | NULL, getTempReadings, PRIVILEGE_USER); |
| 1365 | |
Marri Devender Rao | 66c5fda | 2018-01-18 10:48:37 -0600 | [diff] [blame] | 1366 | // <Get Power Reading> |
| 1367 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_POWER_READING, |
| 1368 | NULL, getPowerReading, PRIVILEGE_USER); |
adarshgrami | 042e9db | 2022-09-15 10:34:34 +0530 | [diff] [blame] | 1369 | // The Get sensor should get the senor details dynamically when |
| 1370 | // FEATURE_DYNAMIC_SENSORS is enabled. |
| 1371 | #ifndef FEATURE_DYNAMIC_SENSORS |
Deepak Kodihalli | 0b45955 | 2018-02-06 06:25:12 -0600 | [diff] [blame] | 1372 | // <Get Sensor Info> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1373 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_SENSOR_INFO, NULL, |
Chau Ly | d74df5f | 2023-05-25 10:33:00 +0000 | [diff] [blame] | 1374 | getSensorInfo, PRIVILEGE_OPERATOR); |
adarshgrami | 042e9db | 2022-09-15 10:34:34 +0530 | [diff] [blame] | 1375 | #endif |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 1376 | // <Get DCMI Configuration Parameters> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1377 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_CONF_PARAMS, NULL, |
| 1378 | getDCMIConfParams, PRIVILEGE_USER); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 1379 | |
| 1380 | // <Set DCMI Configuration Parameters> |
Patrick Venture | 0b02be9 | 2018-08-31 11:55:55 -0700 | [diff] [blame] | 1381 | ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::SET_CONF_PARAMS, NULL, |
| 1382 | setDCMIConfParams, PRIVILEGE_ADMIN); |
Nagaraju Goruganti | 22be97b | 2018-02-07 01:19:59 -0600 | [diff] [blame] | 1383 | |
Chris Austen | 1810bec | 2015-10-13 12:12:39 -0500 | [diff] [blame] | 1384 | return; |
| 1385 | } |
Tom | 0573237 | 2016-09-06 17:21:23 +0530 | [diff] [blame] | 1386 | // 956379 |