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