blob: dafd9d80ac7f8cadb4a4371bf2adb1259a0abfb6 [file] [log] [blame]
Patrick Venture0b02be92018-08-31 11:55:55 -07001#include "config.h"
2
Tom Josephbe5eaa12017-07-12 19:54:44 +05303#include "dcmihandler.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07004
Johnathan Mantey74a21022018-12-13 13:17:56 -08005#include "user_channel/channel_layer.hpp"
Patrick Venture0b02be92018-08-31 11:55:55 -07006
Vernon Mauerye08fbff2019-04-03 09:19:34 -07007#include <ipmid/api.hpp>
Vernon Mauery6a98fe72019-03-11 15:57:48 -07008#include <ipmid/utils.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -07009#include <nlohmann/json.hpp>
Tom Josephbe5eaa12017-07-12 19:54:44 +053010#include <phosphor-logging/elog-errors.hpp>
Andrew Geissler50c0c8f2017-07-11 16:18:51 -050011#include <phosphor-logging/log.hpp>
12#include <sdbusplus/bus.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070013#include <xyz/openbmc_project/Common/error.hpp>
Thang Tran55cbf552023-01-31 14:43:02 +070014#include <xyz/openbmc_project/Network/EthernetInterface/server.hpp>
Patrick Venture0b02be92018-08-31 11:55:55 -070015
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -050016#include <bitset>
17#include <cmath>
18#include <fstream>
19#include <variant>
20
Tom Josephbe5eaa12017-07-12 19:54:44 +053021using namespace phosphor::logging;
Thang Tran55cbf552023-01-31 14:43:02 +070022using sdbusplus::xyz::openbmc_project::Network::server::EthernetInterface;
23
Tom Josephbe5eaa12017-07-12 19:54:44 +053024using InternalFailure =
Patrick Venture0b02be92018-08-31 11:55:55 -070025 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Chris Austen1810bec2015-10-13 12:12:39 -050026
27void register_netfn_dcmi_functions() __attribute__((constructor));
28
Vernon Maueryd4222fd2023-07-27 11:26:51 -070029constexpr auto pcapPath = "/xyz/openbmc_project/control/host0/power_cap";
30constexpr auto pcapInterface = "xyz.openbmc_project.Control.Power.Cap";
Andrew Geissler50c0c8f2017-07-11 16:18:51 -050031
Vernon Maueryd4222fd2023-07-27 11:26:51 -070032constexpr auto powerCapProp = "PowerCap";
33constexpr auto powerCapEnableProp = "PowerCapEnable";
Andrew Geissler50c0c8f2017-07-11 16:18:51 -050034
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -060035constexpr auto DCMI_SPEC_MAJOR_VERSION = 1;
36constexpr auto DCMI_SPEC_MINOR_VERSION = 5;
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -060037constexpr auto DCMI_CONFIG_PARAMETER_REVISION = 1;
38constexpr auto DCMI_RAND_BACK_OFF_MASK = 0x80;
39constexpr auto DCMI_OPTION_60_43_MASK = 0x02;
40constexpr auto DCMI_OPTION_12_MASK = 0x01;
41constexpr auto DCMI_ACTIVATE_DHCP_MASK = 0x01;
42constexpr auto DCMI_ACTIVATE_DHCP_REPLY = 0x00;
William A. Kennington III5d06cc62019-04-25 02:10:55 -070043constexpr auto DCMI_SET_CONF_PARAM_REQ_PACKET_MAX_SIZE = 0x04;
44constexpr auto DCMI_SET_CONF_PARAM_REQ_PACKET_MIN_SIZE = 0x03;
Patrick Venture0b02be92018-08-31 11:55:55 -070045constexpr auto DHCP_TIMING1 = 0x04; // 4 sec
46constexpr auto DHCP_TIMING2_UPPER = 0x00; // 2 min
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -060047constexpr auto DHCP_TIMING2_LOWER = 0x78;
Patrick Venture0b02be92018-08-31 11:55:55 -070048constexpr auto DHCP_TIMING3_UPPER = 0x00; // 64 sec
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -060049constexpr 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.
53constexpr auto DHCP_OPT12_ENABLED = "SendHostNameEnabled";
54
Marri Devender Rao66c5fda2018-01-18 10:48:37 -060055constexpr auto SENSOR_VALUE_INTF = "xyz.openbmc_project.Sensor.Value";
56constexpr auto SENSOR_VALUE_PROP = "Value";
Marri Devender Rao66c5fda2018-01-18 10:48:37 -060057
Andrew Geissler50c0c8f2017-07-11 16:18:51 -050058using namespace phosphor::logging;
59
Tom Josephb9d86f42017-07-26 18:03:47 +053060namespace dcmi
61{
Vernon Mauerydca47202023-07-27 11:32:01 -070062constexpr auto assetTagMaxOffset = 62;
63constexpr auto assetTagMaxSize = 63;
64constexpr auto maxBytes = 16;
65constexpr size_t maxCtrlIdStrLen = 63;
Tom Josephb9d86f42017-07-26 18:03:47 +053066
Vernon Maueryf038dc02023-07-27 14:06:11 -070067constexpr uint8_t parameterRevision = 2;
68constexpr uint8_t specMajorVersion = 1;
69constexpr uint8_t specMinorVersion = 5;
70
Deepak Kodihalli0b459552018-02-06 06:25:12 -060071// Refer Table 6-14, DCMI Entity ID Extension, DCMI v1.5 spec
Patrick Venture0b02be92018-08-31 11:55:55 -070072static const std::map<uint8_t, std::string> entityIdToName{
73 {0x40, "inlet"}, {0x37, "inlet"}, {0x41, "cpu"},
74 {0x03, "cpu"}, {0x42, "baseboard"}, {0x07, "baseboard"}};
Deepak Kodihalli0b459552018-02-06 06:25:12 -060075
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +030076bool isDCMIPowerMgmtSupported()
77{
Vernon Maueryf4eb35d2023-07-27 11:08:49 -070078 static bool parsed = false;
79 static bool supported = false;
80 if (!parsed)
81 {
82 auto data = parseJSONConfig(gDCMICapabilitiesConfig);
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +030083
Vernon Maueryf4eb35d2023-07-27 11:08:49 -070084 supported = (gDCMIPowerMgmtSupported ==
85 data.value(gDCMIPowerMgmtCapability, 0));
86 }
87 return supported;
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +030088}
89
Vernon Maueryd4222fd2023-07-27 11:26:51 -070090std::optional<uint32_t> getPcap(ipmi::Context::ptr& ctx)
Andrew Geissler50c0c8f2017-07-11 16:18:51 -050091{
Vernon Maueryd4222fd2023-07-27 11:26:51 -070092 std::string service{};
93 boost::system::error_code ec = ipmi::getService(ctx, pcapInterface,
94 pcapPath, service);
95 if (ec.value())
Andrew Geissler50c0c8f2017-07-11 16:18:51 -050096 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -070097 return std::nullopt;
George Liu3e3cc352023-07-26 15:59:31 +080098 }
Vernon Maueryd4222fd2023-07-27 11:26:51 -070099 uint32_t pcap{};
100 ec = ipmi::getDbusProperty(ctx, service, pcapPath, pcapInterface,
101 powerCapProp, pcap);
102 if (ec.value())
George Liu3e3cc352023-07-26 15:59:31 +0800103 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700104 log<level::ERR>("Error in getPcap prop",
105 entry("ERROR=%s", ec.message().c_str()));
Tom Josephb9d86f42017-07-26 18:03:47 +0530106 elog<InternalFailure>();
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700107 return std::nullopt;
Andrew Geissler50c0c8f2017-07-11 16:18:51 -0500108 }
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700109 return pcap;
Andrew Geissler50c0c8f2017-07-11 16:18:51 -0500110}
111
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700112std::optional<bool> getPcapEnabled(ipmi::Context::ptr& ctx)
Andrew Geissler50c0c8f2017-07-11 16:18:51 -0500113{
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700114 std::string service{};
115 boost::system::error_code ec = ipmi::getService(ctx, pcapInterface,
116 pcapPath, service);
117 if (ec.value())
Andrew Geissler50c0c8f2017-07-11 16:18:51 -0500118 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700119 return std::nullopt;
George Liu3e3cc352023-07-26 15:59:31 +0800120 }
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700121 bool pcapEnabled{};
122 ec = ipmi::getDbusProperty(ctx, service, pcapPath, pcapInterface,
123 powerCapEnableProp, pcapEnabled);
124 if (ec.value())
George Liu3e3cc352023-07-26 15:59:31 +0800125 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700126 log<level::ERR>("Error in getPcap prop");
Tom Josephb9d86f42017-07-26 18:03:47 +0530127 elog<InternalFailure>();
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700128 return std::nullopt;
Andrew Geissler50c0c8f2017-07-11 16:18:51 -0500129 }
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700130 return pcapEnabled;
Andrew Geissler50c0c8f2017-07-11 16:18:51 -0500131}
Chris Austen1810bec2015-10-13 12:12:39 -0500132
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700133bool setPcap(ipmi::Context::ptr& ctx, const uint32_t powerCap)
Tom Joseph46fa37d2017-07-26 18:11:55 +0530134{
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700135 std::string service{};
136 boost::system::error_code ec = ipmi::getService(ctx, pcapInterface,
137 pcapPath, service);
138 if (ec.value())
Tom Joseph46fa37d2017-07-26 18:11:55 +0530139 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700140 return false;
George Liu3e3cc352023-07-26 15:59:31 +0800141 }
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700142
143 ec = ipmi::setDbusProperty(ctx, service, pcapPath, pcapInterface,
144 powerCapProp, powerCap);
145 if (ec.value())
George Liu3e3cc352023-07-26 15:59:31 +0800146 {
147 log<level::ERR>("Error in setPcap property",
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700148 entry("ERROR=%s", ec.message().c_str()));
Tom Joseph46fa37d2017-07-26 18:11:55 +0530149 elog<InternalFailure>();
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700150 return false;
Tom Joseph46fa37d2017-07-26 18:11:55 +0530151 }
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700152 return true;
Tom Joseph46fa37d2017-07-26 18:11:55 +0530153}
154
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700155bool setPcapEnable(ipmi::Context::ptr& ctx, bool enabled)
Tom Joseph6c8d51b2017-07-26 18:18:06 +0530156{
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700157 std::string service{};
158 boost::system::error_code ec = ipmi::getService(ctx, pcapInterface,
159 pcapPath, service);
160 if (ec.value())
Tom Joseph6c8d51b2017-07-26 18:18:06 +0530161 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700162 return false;
George Liu3e3cc352023-07-26 15:59:31 +0800163 }
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700164
165 ec = ipmi::setDbusProperty(ctx, service, pcapPath, pcapInterface,
166 powerCapEnableProp, enabled);
167 if (ec.value())
George Liu3e3cc352023-07-26 15:59:31 +0800168 {
169 log<level::ERR>("Error in setPcapEnabled property",
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700170 entry("ERROR=%s", ec.message().c_str()));
Tom Joseph6c8d51b2017-07-26 18:18:06 +0530171 elog<InternalFailure>();
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700172 return false;
Tom Joseph6c8d51b2017-07-26 18:18:06 +0530173 }
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700174 return true;
Tom Joseph6c8d51b2017-07-26 18:18:06 +0530175}
176
Vernon Mauerydca47202023-07-27 11:32:01 -0700177std::optional<std::string> readAssetTag(ipmi::Context::ptr& ctx)
Tom Josephbe5eaa12017-07-12 19:54:44 +0530178{
Tom Josephbe5eaa12017-07-12 19:54:44 +0530179 // Read the object tree with the inventory root to figure out the object
180 // that has implemented the Asset tag interface.
Vernon Mauerydca47202023-07-27 11:32:01 -0700181 ipmi::DbusObjectInfo objectInfo;
182 boost::system::error_code ec = getDbusObject(
183 ctx, dcmi::assetTagIntf, ipmi::sensor::inventoryRoot, "", objectInfo);
184 if (ec.value())
Tom Josephbe5eaa12017-07-12 19:54:44 +0530185 {
Vernon Mauerydca47202023-07-27 11:32:01 -0700186 return std::nullopt;
George Liu3e3cc352023-07-26 15:59:31 +0800187 }
Vernon Mauerydca47202023-07-27 11:32:01 -0700188
189 std::string assetTag{};
190 ec = ipmi::getDbusProperty(ctx, objectInfo.second, objectInfo.first,
191 dcmi::assetTagIntf, dcmi::assetTagProp,
192 assetTag);
193 if (ec.value())
George Liu3e3cc352023-07-26 15:59:31 +0800194 {
195 log<level::ERR>("Error in reading asset tag",
Vernon Mauerydca47202023-07-27 11:32:01 -0700196 entry("ERROR=%s", ec.message().c_str()));
Tom Josephbe5eaa12017-07-12 19:54:44 +0530197 elog<InternalFailure>();
Vernon Mauerydca47202023-07-27 11:32:01 -0700198 return std::nullopt;
Tom Josephbe5eaa12017-07-12 19:54:44 +0530199 }
Vernon Mauerydca47202023-07-27 11:32:01 -0700200
201 return assetTag;
Tom Josephbe5eaa12017-07-12 19:54:44 +0530202}
203
Vernon Mauerydca47202023-07-27 11:32:01 -0700204bool writeAssetTag(ipmi::Context::ptr& ctx, const std::string& assetTag)
Tom Josephbe5b9892017-07-15 00:55:23 +0530205{
Tom Josephbe5b9892017-07-15 00:55:23 +0530206 // Read the object tree with the inventory root to figure out the object
207 // that has implemented the Asset tag interface.
Vernon Mauerydca47202023-07-27 11:32:01 -0700208 ipmi::DbusObjectInfo objectInfo;
209 boost::system::error_code ec = getDbusObject(
210 ctx, dcmi::assetTagIntf, ipmi::sensor::inventoryRoot, "", objectInfo);
211 if (ec.value())
Tom Josephbe5b9892017-07-15 00:55:23 +0530212 {
Vernon Mauerydca47202023-07-27 11:32:01 -0700213 return false;
George Liu3e3cc352023-07-26 15:59:31 +0800214 }
Vernon Mauerydca47202023-07-27 11:32:01 -0700215
216 ec = ipmi::setDbusProperty(ctx, objectInfo.second, objectInfo.first,
217 dcmi::assetTagIntf, dcmi::assetTagProp,
218 assetTag);
219 if (ec.value())
George Liu3e3cc352023-07-26 15:59:31 +0800220 {
221 log<level::ERR>("Error in writing asset tag",
Vernon Mauerydca47202023-07-27 11:32:01 -0700222 entry("ERROR=%s", ec.message().c_str()));
Tom Josephbe5b9892017-07-15 00:55:23 +0530223 elog<InternalFailure>();
Vernon Mauerydca47202023-07-27 11:32:01 -0700224 return false;
Tom Josephbe5b9892017-07-15 00:55:23 +0530225 }
Vernon Mauerydca47202023-07-27 11:32:01 -0700226 return true;
Tom Josephbe5b9892017-07-15 00:55:23 +0530227}
228
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700229std::optional<std::string> getHostName(ipmi::Context::ptr& ctx)
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300230{
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700231 std::string service{};
232 boost::system::error_code ec = ipmi::getService(ctx, networkConfigIntf,
233 networkConfigObj, service);
234 if (ec.value())
235 {
236 return std::nullopt;
237 }
238 std::string hostname{};
239 ec = ipmi::getDbusProperty(ctx, service, networkConfigObj,
240 networkConfigIntf, hostNameProp, hostname);
241 if (ec.value())
242 {
243 log<level::ERR>("Error fetching hostname");
244 elog<InternalFailure>();
245 return std::nullopt;
246 }
247 return hostname;
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300248}
249
Thang Tran55cbf552023-01-31 14:43:02 +0700250EthernetInterface::DHCPConf getDHCPEnabled()
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600251{
Patrick Williams5d82f472022-07-22 19:26:53 -0500252 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600253
Johnathan Mantey74a21022018-12-13 13:17:56 -0800254 auto ethdevice = ipmi::getChannelName(ethernetDefaultChannelNum);
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500255 auto ethernetObj = ipmi::getDbusObject(bus, ethernetIntf, networkRoot,
256 ethdevice);
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600257 auto service = ipmi::getService(bus, ethernetIntf, ethernetObj.first);
Patrick Venture0b02be92018-08-31 11:55:55 -0700258 auto value = ipmi::getDbusProperty(bus, service, ethernetObj.first,
259 ethernetIntf, "DHCPEnabled");
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600260
Thang Tran55cbf552023-01-31 14:43:02 +0700261 return EthernetInterface::convertDHCPConfFromString(
262 std::get<std::string>(value));
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600263}
264
265bool getDHCPOption(std::string prop)
266{
Patrick Williams5d82f472022-07-22 19:26:53 -0500267 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600268
269 auto service = ipmi::getService(bus, dhcpIntf, dhcpObj);
270 auto value = ipmi::getDbusProperty(bus, service, dhcpObj, dhcpIntf, prop);
271
Vernon Maueryf442e112019-04-09 11:44:36 -0700272 return std::get<bool>(value);
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600273}
274
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600275void setDHCPOption(std::string prop, bool value)
276{
Patrick Williams5d82f472022-07-22 19:26:53 -0500277 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600278
279 auto service = ipmi::getService(bus, dhcpIntf, dhcpObj);
280 ipmi::setDbusProperty(bus, service, dhcpObj, dhcpIntf, prop, value);
281}
282
Kirill Pakhomova2573622018-11-02 19:00:18 +0300283Json parseJSONConfig(const std::string& configFile)
Deepak Kodihalli0b459552018-02-06 06:25:12 -0600284{
285 std::ifstream jsonFile(configFile);
286 if (!jsonFile.is_open())
287 {
288 log<level::ERR>("Temperature readings JSON file not found");
289 elog<InternalFailure>();
290 }
291
292 auto data = Json::parse(jsonFile, nullptr, false);
293 if (data.is_discarded())
294 {
295 log<level::ERR>("Temperature readings JSON parser failure");
296 elog<InternalFailure>();
297 }
298
299 return data;
300}
301
Tom Josephbe5eaa12017-07-12 19:54:44 +0530302} // namespace dcmi
Chris Austen1810bec2015-10-13 12:12:39 -0500303
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700304constexpr uint8_t exceptionPowerOff = 0x01;
305ipmi::RspType<uint16_t, // reserved
306 uint8_t, // exception actions
307 uint16_t, // power limit requested in watts
308 uint32_t, // correction time in milliseconds
309 uint16_t, // reserved
310 uint16_t // statistics sampling period in seconds
311 >
312 getPowerLimit(ipmi::Context::ptr ctx, uint16_t reserved)
Tom Josephb9d86f42017-07-26 18:03:47 +0530313{
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +0300314 if (!dcmi::isDCMIPowerMgmtSupported())
315 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700316 return ipmi::responseInvalidCommand();
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +0300317 }
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700318 if (reserved)
Tom Josephb9d86f42017-07-26 18:03:47 +0530319 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700320 return ipmi::responseInvalidFieldRequest();
Tom Josephb9d86f42017-07-26 18:03:47 +0530321 }
322
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700323 std::optional<uint16_t> pcapValue = dcmi::getPcap(ctx);
324 std::optional<bool> pcapEnable = dcmi::getPcapEnabled(ctx);
325 if (!pcapValue || !pcapEnable)
326 {
327 return ipmi::responseUnspecifiedError();
328 }
329
330 constexpr uint16_t reserved1{};
331 constexpr uint16_t reserved2{};
Tom Josephb9d86f42017-07-26 18:03:47 +0530332 /*
333 * Exception action if power limit is exceeded and cannot be controlled
334 * with the correction time limit is hardcoded to Hard Power Off system
335 * and log event to SEL.
336 */
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700337 constexpr uint8_t exception = exceptionPowerOff;
Tom Josephb9d86f42017-07-26 18:03:47 +0530338 /*
339 * Correction time limit and Statistics sampling period is currently not
340 * populated.
341 */
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700342 constexpr uint32_t correctionTime{};
343 constexpr uint16_t statsPeriod{};
344 if (!pcapEnable)
Tom Josephb9d86f42017-07-26 18:03:47 +0530345 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700346 constexpr ipmi::Cc responseNoPowerLimitSet = 0x80;
347 constexpr uint16_t noPcap{};
348 return ipmi::response(responseNoPowerLimitSet, reserved1, exception,
349 noPcap, correctionTime, reserved2, statsPeriod);
Tom Josephb9d86f42017-07-26 18:03:47 +0530350 }
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700351 return ipmi::responseSuccess(reserved1, exception, *pcapValue,
352 correctionTime, reserved2, statsPeriod);
Tom Josephb9d86f42017-07-26 18:03:47 +0530353}
354
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700355ipmi::RspType<> setPowerLimit(ipmi::Context::ptr& ctx, uint16_t reserved1,
356 uint8_t exceptionAction, uint16_t powerLimit,
357 uint32_t correctionTime, uint16_t reserved2,
358 uint16_t statsPeriod)
Tom Joseph46fa37d2017-07-26 18:11:55 +0530359{
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +0300360 if (!dcmi::isDCMIPowerMgmtSupported())
361 {
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +0300362 log<level::ERR>("DCMI Power management is unsupported!");
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700363 return ipmi::responseInvalidCommand();
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +0300364 }
365
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700366 // Only process the power limit requested in watts. Return errors
367 // for other fields that are set
368 if (reserved1 || reserved2 || correctionTime || statsPeriod ||
369 exceptionAction != exceptionPowerOff)
Tom Joseph46fa37d2017-07-26 18:11:55 +0530370 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700371 return ipmi::responseInvalidFieldRequest();
Tom Joseph46fa37d2017-07-26 18:11:55 +0530372 }
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700373
374 if (!dcmi::setPcap(ctx, powerLimit))
Tom Joseph46fa37d2017-07-26 18:11:55 +0530375 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700376 return ipmi::responseUnspecifiedError();
Tom Joseph46fa37d2017-07-26 18:11:55 +0530377 }
378
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700379 log<level::INFO>("Set Power Cap", entry("POWERCAP=%u", powerLimit));
Tom Joseph46fa37d2017-07-26 18:11:55 +0530380
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700381 return ipmi::responseSuccess();
Tom Joseph46fa37d2017-07-26 18:11:55 +0530382}
383
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700384ipmi::RspType<> applyPowerLimit(ipmi::Context::ptr& ctx, bool enabled,
385 uint7_t reserved1, uint16_t reserved2)
Tom Joseph6c8d51b2017-07-26 18:18:06 +0530386{
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +0300387 if (!dcmi::isDCMIPowerMgmtSupported())
388 {
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +0300389 log<level::ERR>("DCMI Power management is unsupported!");
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700390 return ipmi::responseInvalidCommand();
391 }
392 if (reserved1 || reserved2)
393 {
394 return ipmi::responseInvalidFieldRequest();
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +0300395 }
396
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700397 if (!dcmi::setPcapEnable(ctx, enabled))
Tom Joseph6c8d51b2017-07-26 18:18:06 +0530398 {
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700399 return ipmi::responseUnspecifiedError();
Tom Joseph6c8d51b2017-07-26 18:18:06 +0530400 }
401
402 log<level::INFO>("Set Power Cap Enable",
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700403 entry("POWERCAPENABLE=%u", static_cast<uint8_t>(enabled)));
Tom Joseph6c8d51b2017-07-26 18:18:06 +0530404
Vernon Maueryd4222fd2023-07-27 11:26:51 -0700405 return ipmi::responseSuccess();
Tom Joseph6c8d51b2017-07-26 18:18:06 +0530406}
407
Vernon Mauerydca47202023-07-27 11:32:01 -0700408ipmi::RspType<uint8_t, // total tag length
409 std::vector<char> // tag data
410 >
411 getAssetTag(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count)
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530412{
Vernon Mauerydca47202023-07-27 11:32:01 -0700413 // Verify offset to read and number of bytes to read are not exceeding
414 // the range.
415 if ((offset > dcmi::assetTagMaxOffset) || (count > dcmi::maxBytes) ||
416 ((offset + count) > dcmi::assetTagMaxSize))
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530417 {
Vernon Mauerydca47202023-07-27 11:32:01 -0700418 return ipmi::responseParmOutOfRange();
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530419 }
420
Vernon Mauerydca47202023-07-27 11:32:01 -0700421 std::optional<std::string> assetTagResp = dcmi::readAssetTag(ctx);
422 if (!assetTagResp)
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530423 {
Vernon Mauerydca47202023-07-27 11:32:01 -0700424 return ipmi::responseUnspecifiedError();
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530425 }
426
Vernon Mauerydca47202023-07-27 11:32:01 -0700427 std::string& assetTag = assetTagResp.value();
428 // If the asset tag is longer than 63 bytes, restrict it to 63 bytes to
429 // suit Get Asset Tag command.
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530430 if (assetTag.size() > dcmi::assetTagMaxSize)
431 {
432 assetTag.resize(dcmi::assetTagMaxSize);
433 }
434
Vernon Mauerydca47202023-07-27 11:32:01 -0700435 if (offset >= assetTag.size())
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530436 {
Vernon Mauerydca47202023-07-27 11:32:01 -0700437 return ipmi::responseParmOutOfRange();
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530438 }
439
Vernon Mauerydca47202023-07-27 11:32:01 -0700440 // silently truncate reads beyond the end of assetTag
441 if ((offset + count) >= assetTag.size())
442 {
443 count = assetTag.size() - offset;
444 }
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530445
Vernon Mauerydca47202023-07-27 11:32:01 -0700446 auto totalTagSize = static_cast<uint8_t>(assetTag.size());
447 std::vector<char> data{assetTag.begin() + offset,
448 assetTag.begin() + offset + count};
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530449
Vernon Mauerydca47202023-07-27 11:32:01 -0700450 return ipmi::responseSuccess(totalTagSize, data);
Tom Joseph6f6dd4d2017-07-12 20:07:11 +0530451}
452
Vernon Mauerydca47202023-07-27 11:32:01 -0700453ipmi::RspType<uint8_t // new asset tag length
454 >
455 setAssetTag(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count,
456 const std::vector<char>& data)
Tom Joseph545dd232017-07-12 20:20:49 +0530457{
Vernon Mauerydca47202023-07-27 11:32:01 -0700458 // Verify offset to read and number of bytes to read are not exceeding
459 // the range.
460 if ((offset > dcmi::assetTagMaxOffset) || (count > dcmi::maxBytes) ||
461 ((offset + count) > dcmi::assetTagMaxSize))
Tom Joseph545dd232017-07-12 20:20:49 +0530462 {
Vernon Mauerydca47202023-07-27 11:32:01 -0700463 return ipmi::responseParmOutOfRange();
464 }
465 if (data.size() != count)
466 {
467 return ipmi::responseReqDataLenInvalid();
Tom Joseph545dd232017-07-12 20:20:49 +0530468 }
469
Vernon Mauerydca47202023-07-27 11:32:01 -0700470 std::optional<std::string> assetTagResp = dcmi::readAssetTag(ctx);
471 if (!assetTagResp)
Tom Joseph545dd232017-07-12 20:20:49 +0530472 {
Vernon Mauerydca47202023-07-27 11:32:01 -0700473 return ipmi::responseUnspecifiedError();
Tom Joseph545dd232017-07-12 20:20:49 +0530474 }
Vernon Mauerydca47202023-07-27 11:32:01 -0700475
476 std::string& assetTag = assetTagResp.value();
477
478 if (offset > assetTag.size())
Tom Joseph545dd232017-07-12 20:20:49 +0530479 {
Vernon Mauerydca47202023-07-27 11:32:01 -0700480 return ipmi::responseParmOutOfRange();
Tom Joseph545dd232017-07-12 20:20:49 +0530481 }
Vernon Mauerydca47202023-07-27 11:32:01 -0700482
483 // operation is to truncate at offset and append new data
484 assetTag.resize(offset);
485 assetTag.append(data.begin(), data.end());
486
487 if (!dcmi::writeAssetTag(ctx, assetTag))
488 {
489 return ipmi::responseUnspecifiedError();
490 }
491
492 auto totalTagSize = static_cast<uint8_t>(assetTag.size());
493 return ipmi::responseSuccess(totalTagSize);
Tom Joseph545dd232017-07-12 20:20:49 +0530494}
495
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700496ipmi::RspType<uint8_t, // length
497 std::vector<char> // data
498 >
499 getMgmntCtrlIdStr(ipmi::Context::ptr& ctx, uint8_t offset, uint8_t count)
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300500{
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700501 if (count > dcmi::maxBytes || offset + count > dcmi::maxCtrlIdStrLen)
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300502 {
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700503 return ipmi::responseParmOutOfRange();
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300504 }
505
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700506 std::optional<std::string> hostnameResp = dcmi::getHostName(ctx);
507 if (!hostnameResp)
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300508 {
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700509 return ipmi::responseUnspecifiedError();
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300510 }
511
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700512 std::string& hostname = hostnameResp.value();
513 // If the id string is longer than 63 bytes, restrict it to 63 bytes to
514 // suit set management ctrl str command.
515 if (hostname.size() > dcmi::maxCtrlIdStrLen)
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300516 {
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700517 hostname.resize(dcmi::maxCtrlIdStrLen);
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300518 }
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300519
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700520 if (offset >= hostname.size())
521 {
522 return ipmi::responseParmOutOfRange();
523 }
524
525 // silently truncate reads beyond the end of hostname
526 if ((offset + count) >= hostname.size())
527 {
528 count = hostname.size() - offset;
529 }
530
531 auto nameSize = static_cast<uint8_t>(hostname.size());
532 std::vector<char> data{hostname.begin() + offset,
533 hostname.begin() + offset + count};
534
535 return ipmi::responseSuccess(nameSize, data);
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300536}
537
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700538ipmi::RspType<uint8_t> setMgmntCtrlIdStr(ipmi::Context::ptr& ctx,
539 uint8_t offset, uint8_t count,
540 std::vector<char> data)
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300541{
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700542 if ((offset > dcmi::maxCtrlIdStrLen) || (count > dcmi::maxBytes) ||
543 ((offset + count) > dcmi::maxCtrlIdStrLen))
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300544 {
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700545 return ipmi::responseParmOutOfRange();
546 }
547 if (data.size() != count)
548 {
549 return ipmi::responseReqDataLenInvalid();
550 }
551 bool terminalWrite{data.back() == '\0'};
552 if (terminalWrite)
553 {
554 // remove the null termination from the data (no need with std::string)
555 data.resize(count - 1);
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300556 }
557
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700558 static std::string hostname{};
559 // read in the current value if not starting at offset 0
560 if (hostname.size() == 0 && offset != 0)
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300561 {
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700562 /* read old ctrlIdStr */
563 std::optional<std::string> hostnameResp = dcmi::getHostName(ctx);
564 if (!hostnameResp)
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300565 {
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700566 return ipmi::responseUnspecifiedError();
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300567 }
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700568 hostname = hostnameResp.value();
569 hostname.resize(offset);
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300570 }
571
Vernon Maueryefb5ae52023-07-27 11:35:43 -0700572 // operation is to truncate at offset and append new data
573 hostname.append(data.begin(), data.end());
574
575 // do the update if this is the last write
576 if (terminalWrite)
577 {
578 boost::system::error_code ec = ipmi::setDbusProperty(
579 ctx, dcmi::networkServiceName, dcmi::networkConfigObj,
580 dcmi::networkConfigIntf, dcmi::hostNameProp, hostname);
581 hostname.clear();
582 if (ec.value())
583 {
584 return ipmi::responseUnspecifiedError();
585 }
586 }
587
588 auto totalIdSize = static_cast<uint8_t>(offset + count);
589 return ipmi::responseSuccess(totalIdSize);
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +0300590}
591
Vernon Maueryf038dc02023-07-27 14:06:11 -0700592ipmi::RspType<ipmi::message::Payload> getDCMICapabilities(uint8_t parameter)
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600593{
Kirill Pakhomova2573622018-11-02 19:00:18 +0300594 std::ifstream dcmiCapFile(dcmi::gDCMICapabilitiesConfig);
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600595 if (!dcmiCapFile.is_open())
596 {
597 log<level::ERR>("DCMI Capabilities file not found");
Vernon Maueryf038dc02023-07-27 14:06:11 -0700598 return ipmi::responseUnspecifiedError();
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600599 }
600
601 auto data = nlohmann::json::parse(dcmiCapFile, nullptr, false);
602 if (data.is_discarded())
603 {
604 log<level::ERR>("DCMI Capabilities JSON parser failure");
Vernon Maueryf038dc02023-07-27 14:06:11 -0700605 return ipmi::responseUnspecifiedError();
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600606 }
607
Vernon Maueryf038dc02023-07-27 14:06:11 -0700608 constexpr bool reserved1{};
609 constexpr uint5_t reserved5{};
610 constexpr uint7_t reserved7{};
611 constexpr uint8_t reserved8{};
612 constexpr uint16_t reserved16{};
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600613
Vernon Maueryf038dc02023-07-27 14:06:11 -0700614 ipmi::message::Payload payload;
615 payload.pack(dcmi::specMajorVersion, dcmi::specMinorVersion,
616 dcmi::parameterRevision);
617
618 enum class DCMICapParameters : uint8_t
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600619 {
Vernon Maueryf038dc02023-07-27 14:06:11 -0700620 SupportedDcmiCaps = 0x01, // Supported DCMI Capabilities
621 MandatoryPlatAttributes = 0x02, // Mandatory Platform Attributes
622 OptionalPlatAttributes = 0x03, // Optional Platform Attributes
623 ManageabilityAccessAttributes = 0x04, // Manageability Access Attributes
624 };
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600625
Vernon Maueryf038dc02023-07-27 14:06:11 -0700626 switch (static_cast<DCMICapParameters>(parameter))
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600627 {
Vernon Maueryf038dc02023-07-27 14:06:11 -0700628 case DCMICapParameters::SupportedDcmiCaps:
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600629 {
Vernon Maueryf038dc02023-07-27 14:06:11 -0700630 bool powerManagement = data.value("PowerManagement", 0);
631 bool oobSecondaryLan = data.value("OOBSecondaryLan", 0);
632 bool serialTMode = data.value("SerialTMODE", 0);
633 bool inBandSystemInterfaceChannel =
634 data.value("InBandSystemInterfaceChannel", 0);
635 payload.pack(reserved8, powerManagement, reserved7,
636 inBandSystemInterfaceChannel, serialTMode,
637 oobSecondaryLan, reserved5);
638 break;
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600639 }
Vernon Maueryf038dc02023-07-27 14:06:11 -0700640 // Mandatory Platform Attributes
641 case DCMICapParameters::MandatoryPlatAttributes:
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600642 {
Vernon Maueryf038dc02023-07-27 14:06:11 -0700643 bool selAutoRollOver = data.value("SELAutoRollOver", 0);
644 bool flushEntireSELUponRollOver =
645 data.value("FlushEntireSELUponRollOver", 0);
646 bool recordLevelSELFlushUponRollOver =
647 data.value("RecordLevelSELFlushUponRollOver", 0);
648 uint12_t numberOfSELEntries = data.value("NumberOfSELEntries",
649 0xcac);
650 uint8_t tempMonitoringSamplingFreq =
651 data.value("TempMonitoringSamplingFreq", 0);
652 payload.pack(numberOfSELEntries, reserved1,
653 recordLevelSELFlushUponRollOver,
654 flushEntireSELUponRollOver, selAutoRollOver,
655 reserved16, tempMonitoringSamplingFreq);
656 break;
657 }
658 // Optional Platform Attributes
659 case DCMICapParameters::OptionalPlatAttributes:
660 {
661 uint7_t powerMgmtDeviceSlaveAddress =
662 data.value("PowerMgmtDeviceSlaveAddress", 0);
663 uint4_t bmcChannelNumber = data.value("BMCChannelNumber", 0);
664 uint4_t deviceRivision = data.value("DeviceRivision", 0);
665 payload.pack(powerMgmtDeviceSlaveAddress, reserved1, deviceRivision,
666 bmcChannelNumber);
667 break;
668 }
669 // Manageability Access Attributes
670 case DCMICapParameters::ManageabilityAccessAttributes:
671 {
672 uint8_t mandatoryPrimaryLanOOBSupport =
673 data.value("MandatoryPrimaryLanOOBSupport", 0xff);
674 uint8_t optionalSecondaryLanOOBSupport =
675 data.value("OptionalSecondaryLanOOBSupport", 0xff);
676 uint8_t optionalSerialOOBMTMODECapability =
677 data.value("OptionalSerialOOBMTMODECapability", 0xff);
678 payload.pack(mandatoryPrimaryLanOOBSupport,
679 optionalSecondaryLanOOBSupport,
680 optionalSerialOOBMTMODECapability);
681 break;
682 }
683 default:
684 {
685 log<level::ERR>("Invalid input parameter");
686 return ipmi::responseInvalidFieldRequest();
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600687 }
688 }
689
Vernon Maueryf038dc02023-07-27 14:06:11 -0700690 return ipmi::responseSuccess(payload);
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -0600691}
692
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600693namespace dcmi
694{
695namespace temp_readings
696{
697
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600698Temperature readTemp(const std::string& dbusService,
699 const std::string& dbusPath)
700{
701 // Read the temperature value from d-bus object. Need some conversion.
702 // As per the interface xyz.openbmc_project.Sensor.Value, the temperature
James Feist9cc0ea52018-10-09 10:53:11 -0700703 // is an double and in degrees C. It needs to be scaled by using the
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600704 // formula Value * 10^Scale. The ipmi spec has the temperature as a uint8_t,
705 // with a separate single bit for the sign.
706
Patrick Williams5d82f472022-07-22 19:26:53 -0500707 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Patrick Venture0b02be92018-08-31 11:55:55 -0700708 auto result = ipmi::getAllDbusProperties(
709 bus, dbusService, dbusPath, "xyz.openbmc_project.Sensor.Value");
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500710 auto temperature = std::visit(ipmi::VariantToDoubleVisitor(),
711 result.at("Value"));
James Feist9cc0ea52018-10-09 10:53:11 -0700712 double absTemp = std::abs(temperature);
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600713
James Feist9cc0ea52018-10-09 10:53:11 -0700714 auto findFactor = result.find("Scale");
715 double factor = 0.0;
716 if (findFactor != result.end())
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600717 {
Vernon Maueryf442e112019-04-09 11:44:36 -0700718 factor = std::visit(ipmi::VariantToDoubleVisitor(), findFactor->second);
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600719 }
James Feist9cc0ea52018-10-09 10:53:11 -0700720 double scale = std::pow(10, factor);
721
722 auto tempDegrees = absTemp * scale;
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600723 // Max absolute temp as per ipmi spec is 128.
724 if (tempDegrees > maxTemp)
725 {
726 tempDegrees = maxTemp;
727 }
728
729 return std::make_tuple(static_cast<uint8_t>(tempDegrees),
730 (temperature < 0));
731}
732
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600733std::tuple<Response, NumInstances> read(const std::string& type,
734 uint8_t instance)
735{
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600736 Response response{};
Patrick Williams5d82f472022-07-22 19:26:53 -0500737 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600738
739 if (!instance)
740 {
741 log<level::ERR>("Expected non-zero instance");
742 elog<InternalFailure>();
743 }
744
Kirill Pakhomova2573622018-11-02 19:00:18 +0300745 auto data = parseJSONConfig(gDCMISensorsConfig);
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600746 static const std::vector<Json> empty{};
747 std::vector<Json> readings = data.value(type, empty);
748 size_t numInstances = readings.size();
749 for (const auto& j : readings)
750 {
751 uint8_t instanceNum = j.value("instance", 0);
752 // Not the instance we're interested in
753 if (instanceNum != instance)
754 {
755 continue;
756 }
757
758 std::string path = j.value("dbus", "");
759 std::string service;
760 try
761 {
Patrick Williamsfbc6c9d2023-05-10 07:50:16 -0500762 service = ipmi::getService(bus, "xyz.openbmc_project.Sensor.Value",
763 path);
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600764 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500765 catch (const std::exception& e)
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600766 {
767 log<level::DEBUG>(e.what());
768 return std::make_tuple(response, numInstances);
769 }
770
771 response.instance = instance;
772 uint8_t temp{};
773 bool sign{};
774 std::tie(temp, sign) = readTemp(service, path);
775 response.temperature = temp;
776 response.sign = sign;
777
778 // Found the instance we're interested in
779 break;
780 }
781
782 if (numInstances > maxInstances)
783 {
784 numInstances = maxInstances;
785 }
786 return std::make_tuple(response, numInstances);
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600787}
788
789std::tuple<ResponseList, NumInstances> readAll(const std::string& type,
790 uint8_t instanceStart)
791{
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600792 ResponseList response{};
Patrick Williams5d82f472022-07-22 19:26:53 -0500793 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600794
795 size_t numInstances = 0;
Kirill Pakhomova2573622018-11-02 19:00:18 +0300796 auto data = parseJSONConfig(gDCMISensorsConfig);
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600797 static const std::vector<Json> empty{};
798 std::vector<Json> readings = data.value(type, empty);
799 numInstances = readings.size();
800 for (const auto& j : readings)
801 {
802 try
803 {
804 // Max of 8 response data sets
805 if (response.size() == maxDataSets)
806 {
807 break;
808 }
809
810 uint8_t instanceNum = j.value("instance", 0);
811 // Not in the instance range we're interested in
812 if (instanceNum < instanceStart)
813 {
814 continue;
815 }
816
817 std::string path = j.value("dbus", "");
818 auto service =
Patrick Venture0b02be92018-08-31 11:55:55 -0700819 ipmi::getService(bus, "xyz.openbmc_project.Sensor.Value", path);
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600820
821 Response r{};
822 r.instance = instanceNum;
823 uint8_t temp{};
824 bool sign{};
825 std::tie(temp, sign) = readTemp(service, path);
826 r.temperature = temp;
827 r.sign = sign;
828 response.push_back(r);
829 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500830 catch (const std::exception& e)
Deepak Kodihallib1e8fba2018-01-24 04:57:10 -0600831 {
832 log<level::DEBUG>(e.what());
833 continue;
834 }
835 }
836
837 if (numInstances > maxInstances)
838 {
839 numInstances = maxInstances;
840 }
841 return std::make_tuple(response, numInstances);
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600842}
843
Patrick Venture0b02be92018-08-31 11:55:55 -0700844} // namespace temp_readings
845} // namespace dcmi
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600846
Willy Tu11d68892022-01-20 10:37:34 -0800847ipmi_ret_t getTempReadings(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
848 ipmi_response_t response, ipmi_data_len_t data_len,
849 ipmi_context_t)
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600850{
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600851 auto requestData =
852 reinterpret_cast<const dcmi::GetTempReadingsRequest*>(request);
853 auto responseData =
854 reinterpret_cast<dcmi::GetTempReadingsResponseHdr*>(response);
855
856 if (*data_len != sizeof(dcmi::GetTempReadingsRequest))
857 {
858 log<level::ERR>("Malformed request data",
859 entry("DATA_SIZE=%d", *data_len));
860 return IPMI_CC_REQ_DATA_LEN_INVALID;
861 }
862 *data_len = 0;
863
Deepak Kodihalli0b459552018-02-06 06:25:12 -0600864 auto it = dcmi::entityIdToName.find(requestData->entityId);
865 if (it == dcmi::entityIdToName.end())
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600866 {
867 log<level::ERR>("Unknown Entity ID",
868 entry("ENTITY_ID=%d", requestData->entityId));
869 return IPMI_CC_INVALID_FIELD_REQUEST;
870 }
871
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600872 if (requestData->sensorType != dcmi::temperatureSensorType)
873 {
874 log<level::ERR>("Invalid sensor type",
875 entry("SENSOR_TYPE=%d", requestData->sensorType));
876 return IPMI_CC_INVALID_FIELD_REQUEST;
877 }
878
879 dcmi::temp_readings::ResponseList temps{};
880 try
881 {
882 if (!requestData->entityInstance)
883 {
884 // Read all instances
885 std::tie(temps, responseData->numInstances) =
886 dcmi::temp_readings::readAll(it->second,
887 requestData->instanceStart);
888 }
889 else
890 {
891 // Read one instance
892 temps.resize(1);
893 std::tie(temps[0], responseData->numInstances) =
894 dcmi::temp_readings::read(it->second,
895 requestData->entityInstance);
896 }
897 responseData->numDataSets = temps.size();
898 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500899 catch (const InternalFailure& e)
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600900 {
901 return IPMI_CC_UNSPECIFIED_ERROR;
902 }
903
Patrick Venture0b02be92018-08-31 11:55:55 -0700904 size_t payloadSize = temps.size() * sizeof(dcmi::temp_readings::Response);
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600905 if (!temps.empty())
906 {
907 memcpy(responseData + 1, // copy payload right after the response header
Patrick Venture0b02be92018-08-31 11:55:55 -0700908 temps.data(), payloadSize);
Deepak Kodihalliee717d72018-01-24 04:53:09 -0600909 }
910 *data_len = sizeof(dcmi::GetTempReadingsResponseHdr) + payloadSize;
911
912 return IPMI_CC_OK;
913}
914
Patrick Williams5d82f472022-07-22 19:26:53 -0500915int64_t getPowerReading(sdbusplus::bus_t& bus)
Marri Devender Rao66c5fda2018-01-18 10:48:37 -0600916{
917 std::ifstream sensorFile(POWER_READING_SENSOR);
918 std::string objectPath;
919 if (!sensorFile.is_open())
920 {
921 log<level::ERR>("Power reading configuration file not found",
Patrick Venture0b02be92018-08-31 11:55:55 -0700922 entry("POWER_SENSOR_FILE=%s", POWER_READING_SENSOR));
Marri Devender Rao66c5fda2018-01-18 10:48:37 -0600923 elog<InternalFailure>();
924 }
925
926 auto data = nlohmann::json::parse(sensorFile, nullptr, false);
927 if (data.is_discarded())
928 {
929 log<level::ERR>("Error in parsing configuration file",
Patrick Venture0b02be92018-08-31 11:55:55 -0700930 entry("POWER_SENSOR_FILE=%s", POWER_READING_SENSOR));
Marri Devender Rao66c5fda2018-01-18 10:48:37 -0600931 elog<InternalFailure>();
932 }
933
934 objectPath = data.value("path", "");
935 if (objectPath.empty())
936 {
937 log<level::ERR>("Power sensor D-Bus object path is empty",
938 entry("POWER_SENSOR_FILE=%s", POWER_READING_SENSOR));
939 elog<InternalFailure>();
940 }
941
Marri Devender Raoce6a7952018-02-11 08:45:00 -0600942 // Return default value if failed to read from D-Bus object
943 int64_t power = 0;
944 try
945 {
946 auto service = ipmi::getService(bus, SENSOR_VALUE_INTF, objectPath);
Marri Devender Rao66c5fda2018-01-18 10:48:37 -0600947
Patrick Venture0b02be92018-08-31 11:55:55 -0700948 // Read the sensor value and scale properties
Chris Cain4cc61e02022-01-12 17:23:14 -0600949 auto value = ipmi::getDbusProperty(
950 bus, service, objectPath, SENSOR_VALUE_INTF, SENSOR_VALUE_PROP);
951 power = std::get<double>(value);
Marri Devender Raoce6a7952018-02-11 08:45:00 -0600952 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -0500953 catch (const std::exception& e)
Marri Devender Raoce6a7952018-02-11 08:45:00 -0600954 {
Chris Cain4cc61e02022-01-12 17:23:14 -0600955 log<level::ERR>("Failure to read power value from D-Bus object",
956 entry("OBJECT_PATH=%s", objectPath.c_str()),
957 entry("INTERFACE=%s", SENSOR_VALUE_INTF));
Marri Devender Raoce6a7952018-02-11 08:45:00 -0600958 }
Marri Devender Rao66c5fda2018-01-18 10:48:37 -0600959 return power;
960}
961
Willy Tu11d68892022-01-20 10:37:34 -0800962ipmi_ret_t setDCMIConfParams(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
963 ipmi_response_t, ipmi_data_len_t data_len,
964 ipmi_context_t)
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600965{
Patrick Venture0b02be92018-08-31 11:55:55 -0700966 auto requestData =
967 reinterpret_cast<const dcmi::SetConfParamsRequest*>(request);
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600968
William A. Kennington III5d06cc62019-04-25 02:10:55 -0700969 if (*data_len < DCMI_SET_CONF_PARAM_REQ_PACKET_MIN_SIZE ||
Patrick Venture0b02be92018-08-31 11:55:55 -0700970 *data_len > DCMI_SET_CONF_PARAM_REQ_PACKET_MAX_SIZE)
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600971 {
William A. Kennington III5d06cc62019-04-25 02:10:55 -0700972 log<level::ERR>("Invalid Requested Packet size",
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600973 entry("PACKET SIZE=%d", *data_len));
William A. Kennington III5d06cc62019-04-25 02:10:55 -0700974 *data_len = 0;
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600975 return IPMI_CC_INVALID_FIELD_REQUEST;
976 }
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600977 *data_len = 0;
978
979 try
980 {
981 // Take action based on the Parameter Selector
Patrick Venture0b02be92018-08-31 11:55:55 -0700982 switch (
983 static_cast<dcmi::DCMIConfigParameters>(requestData->paramSelect))
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600984 {
985 case dcmi::DCMIConfigParameters::ActivateDHCP:
986
987 if ((requestData->data[0] & DCMI_ACTIVATE_DHCP_MASK) &&
Thang Tran55cbf552023-01-31 14:43:02 +0700988 (dcmi::getDHCPEnabled() !=
989 EthernetInterface::DHCPConf::none))
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600990 {
Patrick Venture0b02be92018-08-31 11:55:55 -0700991 // When these conditions are met we have to trigger DHCP
992 // protocol restart using the latest parameter settings, but
993 // as per n/w manager design, each time when we update n/w
994 // parameters, n/w service is restarted. So we no need to
995 // take any action in this case.
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -0600996 }
997 break;
998
999 case dcmi::DCMIConfigParameters::DiscoveryConfig:
1000
1001 if (requestData->data[0] & DCMI_OPTION_12_MASK)
1002 {
1003 dcmi::setDHCPOption(DHCP_OPT12_ENABLED, true);
1004 }
1005 else
1006 {
1007 dcmi::setDHCPOption(DHCP_OPT12_ENABLED, false);
1008 }
1009
1010 // Systemd-networkd doesn't support Random Back off
1011 if (requestData->data[0] & DCMI_RAND_BACK_OFF_MASK)
1012 {
1013 return IPMI_CC_INVALID;
1014 }
1015 break;
1016 // Systemd-networkd doesn't allow to configure DHCP timigs
1017 case dcmi::DCMIConfigParameters::DHCPTiming1:
1018 case dcmi::DCMIConfigParameters::DHCPTiming2:
1019 case dcmi::DCMIConfigParameters::DHCPTiming3:
1020 default:
1021 return IPMI_CC_INVALID;
1022 }
1023 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001024 catch (const std::exception& e)
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001025 {
1026 log<level::ERR>(e.what());
1027 return IPMI_CC_UNSPECIFIED_ERROR;
1028 }
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001029 return IPMI_CC_OK;
1030}
1031
Willy Tu11d68892022-01-20 10:37:34 -08001032ipmi_ret_t getDCMIConfParams(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
1033 ipmi_response_t response, ipmi_data_len_t data_len,
1034 ipmi_context_t)
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001035{
Patrick Venture0b02be92018-08-31 11:55:55 -07001036 auto requestData =
1037 reinterpret_cast<const dcmi::GetConfParamsRequest*>(request);
1038 auto responseData =
1039 reinterpret_cast<dcmi::GetConfParamsResponse*>(response);
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001040
1041 responseData->data[0] = 0x00;
1042
William A. Kennington III5d06cc62019-04-25 02:10:55 -07001043 if (*data_len != sizeof(dcmi::GetConfParamsRequest))
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001044 {
William A. Kennington III5d06cc62019-04-25 02:10:55 -07001045 log<level::ERR>("Invalid Requested Packet size",
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001046 entry("PACKET SIZE=%d", *data_len));
1047 return IPMI_CC_INVALID_FIELD_REQUEST;
1048 }
1049
1050 *data_len = 0;
1051
1052 try
1053 {
1054 // Take action based on the Parameter Selector
Patrick Venture0b02be92018-08-31 11:55:55 -07001055 switch (
1056 static_cast<dcmi::DCMIConfigParameters>(requestData->paramSelect))
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001057 {
1058 case dcmi::DCMIConfigParameters::ActivateDHCP:
1059 responseData->data[0] = DCMI_ACTIVATE_DHCP_REPLY;
1060 *data_len = sizeof(dcmi::GetConfParamsResponse) + 1;
1061 break;
1062 case dcmi::DCMIConfigParameters::DiscoveryConfig:
1063 if (dcmi::getDHCPOption(DHCP_OPT12_ENABLED))
1064 {
1065 responseData->data[0] |= DCMI_OPTION_12_MASK;
1066 }
1067 *data_len = sizeof(dcmi::GetConfParamsResponse) + 1;
1068 break;
1069 // Get below values from Systemd-networkd source code
1070 case dcmi::DCMIConfigParameters::DHCPTiming1:
1071 responseData->data[0] = DHCP_TIMING1;
1072 *data_len = sizeof(dcmi::GetConfParamsResponse) + 1;
1073 break;
1074 case dcmi::DCMIConfigParameters::DHCPTiming2:
1075 responseData->data[0] = DHCP_TIMING2_LOWER;
1076 responseData->data[1] = DHCP_TIMING2_UPPER;
1077 *data_len = sizeof(dcmi::GetConfParamsResponse) + 2;
1078 break;
1079 case dcmi::DCMIConfigParameters::DHCPTiming3:
1080 responseData->data[0] = DHCP_TIMING3_LOWER;
1081 responseData->data[1] = DHCP_TIMING3_UPPER;
1082 *data_len = sizeof(dcmi::GetConfParamsResponse) + 2;
1083 break;
1084 default:
1085 *data_len = 0;
1086 return IPMI_CC_INVALID;
1087 }
1088 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001089 catch (const std::exception& e)
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001090 {
1091 log<level::ERR>(e.what());
1092 return IPMI_CC_UNSPECIFIED_ERROR;
1093 }
1094
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001095 responseData->major = DCMI_SPEC_MAJOR_VERSION;
1096 responseData->minor = DCMI_SPEC_MINOR_VERSION;
1097 responseData->paramRevision = DCMI_CONFIG_PARAMETER_REVISION;
1098
1099 return IPMI_CC_OK;
1100}
1101
Willy Tu11d68892022-01-20 10:37:34 -08001102ipmi_ret_t getPowerReading(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t,
1103 ipmi_response_t response, ipmi_data_len_t data_len,
1104 ipmi_context_t)
Marri Devender Rao66c5fda2018-01-18 10:48:37 -06001105{
Chalapathi Venkataramashetty90e81fa2021-05-11 09:13:23 +00001106 *data_len = 0;
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +03001107 if (!dcmi::isDCMIPowerMgmtSupported())
1108 {
Kirill Pakhomov2c2af2c2018-11-06 16:06:10 +03001109 log<level::ERR>("DCMI Power management is unsupported!");
1110 return IPMI_CC_INVALID;
1111 }
1112
Marri Devender Rao66c5fda2018-01-18 10:48:37 -06001113 ipmi_ret_t rc = IPMI_CC_OK;
Patrick Venture0b02be92018-08-31 11:55:55 -07001114 auto responseData =
1115 reinterpret_cast<dcmi::GetPowerReadingResponse*>(response);
Marri Devender Rao9c966e02018-01-22 05:55:10 -06001116
Patrick Williams5d82f472022-07-22 19:26:53 -05001117 sdbusplus::bus_t bus{ipmid_get_sd_bus_connection()};
Marri Devender Rao9c966e02018-01-22 05:55:10 -06001118 int64_t power = 0;
Marri Devender Rao66c5fda2018-01-18 10:48:37 -06001119 try
1120 {
Marri Devender Rao9c966e02018-01-22 05:55:10 -06001121 power = getPowerReading(bus);
Marri Devender Rao66c5fda2018-01-18 10:48:37 -06001122 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001123 catch (const InternalFailure& e)
Marri Devender Rao66c5fda2018-01-18 10:48:37 -06001124 {
1125 log<level::ERR>("Error in reading power sensor value",
1126 entry("INTERFACE=%s", SENSOR_VALUE_INTF),
1127 entry("PROPERTY=%s", SENSOR_VALUE_PROP));
1128 return IPMI_CC_UNSPECIFIED_ERROR;
1129 }
Marri Devender Rao9c966e02018-01-22 05:55:10 -06001130
1131 // TODO: openbmc/openbmc#2819
Gunnar Mills8466b792018-03-23 12:18:12 -05001132 // Minimum, Maximum, Average power, TimeFrame, TimeStamp,
Marri Devender Rao9c966e02018-01-22 05:55:10 -06001133 // PowerReadingState readings need to be populated
1134 // after Telemetry changes.
1135 uint16_t totalPower = static_cast<uint16_t>(power);
1136 responseData->currentPower = totalPower;
1137 responseData->minimumPower = totalPower;
1138 responseData->maximumPower = totalPower;
1139 responseData->averagePower = totalPower;
1140
1141 *data_len = sizeof(*responseData);
Marri Devender Rao66c5fda2018-01-18 10:48:37 -06001142 return rc;
1143}
1144
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001145namespace dcmi
1146{
1147namespace sensor_info
1148{
1149
Deepak Kodihallidd4cff12018-02-06 06:48:29 -06001150Response createFromJson(const Json& config)
1151{
1152 Response response{};
1153 uint16_t recordId = config.value("record_id", 0);
1154 response.recordIdLsb = recordId & 0xFF;
1155 response.recordIdMsb = (recordId >> 8) & 0xFF;
1156 return response;
1157}
1158
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001159std::tuple<Response, NumInstances> read(const std::string& type,
Patrick Venture0b02be92018-08-31 11:55:55 -07001160 uint8_t instance, const Json& config)
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001161{
Deepak Kodihallidd4cff12018-02-06 06:48:29 -06001162 Response response{};
1163
1164 if (!instance)
1165 {
1166 log<level::ERR>("Expected non-zero instance");
1167 elog<InternalFailure>();
1168 }
1169
1170 static const std::vector<Json> empty{};
1171 std::vector<Json> readings = config.value(type, empty);
1172 size_t numInstances = readings.size();
1173 for (const auto& reading : readings)
1174 {
1175 uint8_t instanceNum = reading.value("instance", 0);
1176 // Not the instance we're interested in
1177 if (instanceNum != instance)
1178 {
1179 continue;
1180 }
1181
1182 response = createFromJson(reading);
1183
1184 // Found the instance we're interested in
1185 break;
1186 }
1187
1188 if (numInstances > maxInstances)
1189 {
1190 log<level::DEBUG>("Trimming IPMI num instances",
1191 entry("NUM_INSTANCES=%d", numInstances));
1192 numInstances = maxInstances;
1193 }
1194 return std::make_tuple(response, numInstances);
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001195}
1196
Patrick Venture0b02be92018-08-31 11:55:55 -07001197std::tuple<ResponseList, NumInstances>
1198 readAll(const std::string& type, uint8_t instanceStart, const Json& config)
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001199{
Deepak Kodihallidd4cff12018-02-06 06:48:29 -06001200 ResponseList responses{};
1201
1202 size_t numInstances = 0;
1203 static const std::vector<Json> empty{};
1204 std::vector<Json> readings = config.value(type, empty);
1205 numInstances = readings.size();
1206 for (const auto& reading : readings)
1207 {
1208 try
1209 {
1210 // Max of 8 records
1211 if (responses.size() == maxRecords)
1212 {
1213 break;
1214 }
1215
1216 uint8_t instanceNum = reading.value("instance", 0);
1217 // Not in the instance range we're interested in
1218 if (instanceNum < instanceStart)
1219 {
1220 continue;
1221 }
1222
1223 Response response = createFromJson(reading);
1224 responses.push_back(response);
1225 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001226 catch (const std::exception& e)
Deepak Kodihallidd4cff12018-02-06 06:48:29 -06001227 {
1228 log<level::DEBUG>(e.what());
1229 continue;
1230 }
1231 }
1232
1233 if (numInstances > maxInstances)
1234 {
1235 log<level::DEBUG>("Trimming IPMI num instances",
1236 entry("NUM_INSTANCES=%d", numInstances));
1237 numInstances = maxInstances;
1238 }
1239 return std::make_tuple(responses, numInstances);
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001240}
1241
1242} // namespace sensor_info
1243} // namespace dcmi
1244
Willy Tu11d68892022-01-20 10:37:34 -08001245ipmi_ret_t getSensorInfo(ipmi_netfn_t, ipmi_cmd_t, ipmi_request_t request,
1246 ipmi_response_t response, ipmi_data_len_t data_len,
1247 ipmi_context_t)
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001248{
1249 auto requestData =
1250 reinterpret_cast<const dcmi::GetSensorInfoRequest*>(request);
1251 auto responseData =
1252 reinterpret_cast<dcmi::GetSensorInfoResponseHdr*>(response);
1253
1254 if (*data_len != sizeof(dcmi::GetSensorInfoRequest))
1255 {
1256 log<level::ERR>("Malformed request data",
1257 entry("DATA_SIZE=%d", *data_len));
1258 return IPMI_CC_REQ_DATA_LEN_INVALID;
1259 }
1260 *data_len = 0;
1261
1262 auto it = dcmi::entityIdToName.find(requestData->entityId);
1263 if (it == dcmi::entityIdToName.end())
1264 {
1265 log<level::ERR>("Unknown Entity ID",
1266 entry("ENTITY_ID=%d", requestData->entityId));
1267 return IPMI_CC_INVALID_FIELD_REQUEST;
1268 }
1269
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001270 if (requestData->sensorType != dcmi::temperatureSensorType)
1271 {
1272 log<level::ERR>("Invalid sensor type",
1273 entry("SENSOR_TYPE=%d", requestData->sensorType));
1274 return IPMI_CC_INVALID_FIELD_REQUEST;
1275 }
1276
1277 dcmi::sensor_info::ResponseList sensors{};
1278 static dcmi::Json config{};
1279 static bool parsed = false;
1280
1281 try
1282 {
1283 if (!parsed)
1284 {
Kirill Pakhomova2573622018-11-02 19:00:18 +03001285 config = dcmi::parseJSONConfig(dcmi::gDCMISensorsConfig);
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001286 parsed = true;
1287 }
1288
1289 if (!requestData->entityInstance)
1290 {
1291 // Read all instances
1292 std::tie(sensors, responseData->numInstances) =
1293 dcmi::sensor_info::readAll(it->second,
Patrick Venture0b02be92018-08-31 11:55:55 -07001294 requestData->instanceStart, config);
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001295 }
1296 else
1297 {
1298 // Read one instance
1299 sensors.resize(1);
1300 std::tie(sensors[0], responseData->numInstances) =
Patrick Venture0b02be92018-08-31 11:55:55 -07001301 dcmi::sensor_info::read(it->second, requestData->entityInstance,
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001302 config);
1303 }
1304 responseData->numRecords = sensors.size();
1305 }
Patrick Williamsa2ad2da2021-10-06 12:21:46 -05001306 catch (const InternalFailure& e)
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001307 {
1308 return IPMI_CC_UNSPECIFIED_ERROR;
1309 }
1310
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001311 size_t payloadSize = sensors.size() * sizeof(dcmi::sensor_info::Response);
1312 if (!sensors.empty())
1313 {
1314 memcpy(responseData + 1, // copy payload right after the response header
Patrick Venture0b02be92018-08-31 11:55:55 -07001315 sensors.data(), payloadSize);
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001316 }
1317 *data_len = sizeof(dcmi::GetSensorInfoResponseHdr) + payloadSize;
1318
1319 return IPMI_CC_OK;
1320}
1321
Chris Austen1810bec2015-10-13 12:12:39 -05001322void register_netfn_dcmi_functions()
1323{
Tom05732372016-09-06 17:21:23 +05301324 // <Get Power Limit>
Vernon Maueryd4222fd2023-07-27 11:26:51 -07001325 registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI,
1326 ipmi::dcmi::cmdGetPowerLimit, ipmi::Privilege::User,
1327 getPowerLimit);
Tom Joseph6f6dd4d2017-07-12 20:07:11 +05301328
Tom Joseph46fa37d2017-07-26 18:11:55 +05301329 // <Set Power Limit>
Vernon Maueryd4222fd2023-07-27 11:26:51 -07001330 registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI,
1331 ipmi::dcmi::cmdSetPowerLimit,
1332 ipmi::Privilege::Operator, setPowerLimit);
Tom Joseph46fa37d2017-07-26 18:11:55 +05301333
Tom Joseph6c8d51b2017-07-26 18:18:06 +05301334 // <Activate/Deactivate Power Limit>
Vernon Maueryd4222fd2023-07-27 11:26:51 -07001335 registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI,
1336 ipmi::dcmi::cmdActDeactivatePwrLimit,
1337 ipmi::Privilege::Operator, applyPowerLimit);
Tom Joseph6c8d51b2017-07-26 18:18:06 +05301338
Tom Joseph6f6dd4d2017-07-12 20:07:11 +05301339 // <Get Asset Tag>
Vernon Mauerydca47202023-07-27 11:32:01 -07001340 registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI,
1341 ipmi::dcmi::cmdGetAssetTag, ipmi::Privilege::User,
1342 getAssetTag);
Tom Joseph545dd232017-07-12 20:20:49 +05301343
1344 // <Set Asset Tag>
Vernon Mauerydca47202023-07-27 11:32:01 -07001345 registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI,
1346 ipmi::dcmi::cmdSetAssetTag, ipmi::Privilege::Operator,
1347 setAssetTag);
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +03001348
Gunnar Mills8991dd62017-10-25 17:11:29 -05001349 // <Get Management Controller Identifier String>
Vernon Maueryefb5ae52023-07-27 11:35:43 -07001350 registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI,
1351 ipmi::dcmi::cmdGetMgmtCntlrIdString,
1352 ipmi::Privilege::User, getMgmntCtrlIdStr);
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +03001353
1354 // <Set Management Controller Identifier String>
Vernon Maueryefb5ae52023-07-27 11:35:43 -07001355 registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI,
1356 ipmi::dcmi::cmdSetMgmtCntlrIdString,
1357 ipmi::Privilege::Admin, setMgmntCtrlIdStr);
Vladislav Vovchenko8f7a6f62017-08-17 00:31:14 +03001358
Dhruvaraj Subhashchandrane29be412018-01-16 05:11:56 -06001359 // <Get DCMI capabilities>
Vernon Maueryf038dc02023-07-27 14:06:11 -07001360 registerGroupHandler(ipmi::prioOpenBmcBase, ipmi::groupDCMI,
1361 ipmi::dcmi::cmdGetDcmiCapabilitiesInfo,
1362 ipmi::Privilege::User, getDCMICapabilities);
Deepak Kodihalliee717d72018-01-24 04:53:09 -06001363
1364 // <Get Temperature Readings>
1365 ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_TEMP_READINGS,
1366 NULL, getTempReadings, PRIVILEGE_USER);
1367
Marri Devender Rao66c5fda2018-01-18 10:48:37 -06001368 // <Get Power Reading>
1369 ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_POWER_READING,
1370 NULL, getPowerReading, PRIVILEGE_USER);
adarshgrami042e9db2022-09-15 10:34:34 +05301371// The Get sensor should get the senor details dynamically when
1372// FEATURE_DYNAMIC_SENSORS is enabled.
1373#ifndef FEATURE_DYNAMIC_SENSORS
Deepak Kodihalli0b459552018-02-06 06:25:12 -06001374 // <Get Sensor Info>
Patrick Venture0b02be92018-08-31 11:55:55 -07001375 ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_SENSOR_INFO, NULL,
Chau Lyd74df5f2023-05-25 10:33:00 +00001376 getSensorInfo, PRIVILEGE_OPERATOR);
adarshgrami042e9db2022-09-15 10:34:34 +05301377#endif
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001378 // <Get DCMI Configuration Parameters>
Patrick Venture0b02be92018-08-31 11:55:55 -07001379 ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::GET_CONF_PARAMS, NULL,
1380 getDCMIConfParams, PRIVILEGE_USER);
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001381
1382 // <Set DCMI Configuration Parameters>
Patrick Venture0b02be92018-08-31 11:55:55 -07001383 ipmi_register_callback(NETFUN_GRPEXT, dcmi::Commands::SET_CONF_PARAMS, NULL,
1384 setDCMIConfParams, PRIVILEGE_ADMIN);
Nagaraju Goruganti22be97b2018-02-07 01:19:59 -06001385
Chris Austen1810bec2015-10-13 12:12:39 -05001386 return;
1387}
Tom05732372016-09-06 17:21:23 +05301388// 956379