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