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