xref: /openbmc/google-ipmi-sys/handler.cpp (revision d455bfd6)
14f0d1de6SSteve Foreman // Copyright 2022 Google LLC
2a2056e9cSWilly Tu //
3a2056e9cSWilly Tu // Licensed under the Apache License, Version 2.0 (the "License");
4a2056e9cSWilly Tu // you may not use this file except in compliance with the License.
5a2056e9cSWilly Tu // You may obtain a copy of the License at
6a2056e9cSWilly Tu //
7a2056e9cSWilly Tu //      http://www.apache.org/licenses/LICENSE-2.0
8a2056e9cSWilly Tu //
9a2056e9cSWilly Tu // Unless required by applicable law or agreed to in writing, software
10a2056e9cSWilly Tu // distributed under the License is distributed on an "AS IS" BASIS,
11a2056e9cSWilly Tu // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12a2056e9cSWilly Tu // See the License for the specific language governing permissions and
13a2056e9cSWilly Tu // limitations under the License.
148d618532SMichael Shen 
15f085d91dSPatrick Venture #include "handler.hpp"
16f085d91dSPatrick Venture 
17444b5ea4SPatrick Williams #include "bm_config.h"
18444b5ea4SPatrick Williams 
198ec4106bSNikhil Namjoshi #include "bmc_mode_enum.hpp"
20d2037c6aSPatrick Venture #include "errors.hpp"
21c87de558SPatrick Venture #include "handler_impl.hpp"
22ab650004SPatrick Venture #include "util.hpp"
23d2037c6aSPatrick Venture 
243b1b427cSWilly Tu #include <fcntl.h>
25d2037c6aSPatrick Venture #include <ipmid/api.h>
263b1b427cSWilly Tu #include <mtd/mtd-abi.h>
273b1b427cSWilly Tu #include <mtd/mtd-user.h>
283b1b427cSWilly Tu #include <sys/ioctl.h>
293b1b427cSWilly Tu #include <unistd.h>
30d2037c6aSPatrick Venture 
31444b5ea4SPatrick Williams #include <nlohmann/json.hpp>
32444b5ea4SPatrick Williams #include <phosphor-logging/elog-errors.hpp>
33444b5ea4SPatrick Williams #include <phosphor-logging/log.hpp>
34444b5ea4SPatrick Williams #include <sdbusplus/bus.hpp>
358d618532SMichael Shen #include <stdplus/print.hpp>
36444b5ea4SPatrick Williams #include <xyz/openbmc_project/Common/error.hpp>
37444b5ea4SPatrick Williams 
38bb90d4fdSPatrick Venture #include <cinttypes>
39d2037c6aSPatrick Venture #include <cstdio>
40d2037c6aSPatrick Venture #include <filesystem>
41d2037c6aSPatrick Venture #include <fstream>
4207f85150SPatrick Venture #include <map>
43d2037c6aSPatrick Venture #include <sstream>
44d2037c6aSPatrick Venture #include <string>
4529f35bceSWilliam A. Kennington III #include <string_view>
46d2037c6aSPatrick Venture #include <tuple>
474f0d1de6SSteve Foreman #include <variant>
485e70dc8cSNikhil Namjoshi 
49f085d91dSPatrick Venture #ifndef NCSI_IF_NAME
50f085d91dSPatrick Venture #define NCSI_IF_NAME eth0
51f085d91dSPatrick Venture #endif
52f085d91dSPatrick Venture 
53f085d91dSPatrick Venture // To deal with receiving a string without quotes.
54f085d91dSPatrick Venture #define QUOTE(name) #name
55f085d91dSPatrick Venture #define STR(macro) QUOTE(macro)
56f085d91dSPatrick Venture #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
57f085d91dSPatrick Venture 
588d3d46a2SWilliam A. Kennington III namespace ipmi
598d3d46a2SWilliam A. Kennington III {
608d3d46a2SWilliam A. Kennington III std::uint8_t getChannelByName(const std::string& chName);
618d3d46a2SWilliam A. Kennington III }
628d3d46a2SWilliam A. Kennington III 
63f085d91dSPatrick Venture namespace google
64f085d91dSPatrick Venture {
65f085d91dSPatrick Venture namespace ipmi
66f085d91dSPatrick Venture {
6707f85150SPatrick Venture using Json = nlohmann::json;
6807f85150SPatrick Venture using namespace phosphor::logging;
6907f85150SPatrick Venture using InternalFailure =
7007f85150SPatrick Venture     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
71*d455bfd6SGaurav Gandhi using Value = std::variant<double>;
72f085d91dSPatrick Venture 
7315d4d21cSHao Zhou uint8_t isBmcInBareMetalMode(const std::unique_ptr<FileSystemInterface>& fs)
745e70dc8cSNikhil Namjoshi {
755e70dc8cSNikhil Namjoshi #if BARE_METAL
765e70dc8cSNikhil Namjoshi     return static_cast<uint8_t>(BmcMode::BM_MODE);
775e70dc8cSNikhil Namjoshi #else
783f3ca035SBrandon Kim     std::error_code ec;
794baf41c8SHao Zhou 
8015d4d21cSHao Zhou     if (fs->exists(bmDriveCleaningDoneAckFlagPath, ec))
813f3ca035SBrandon Kim     {
828d618532SMichael Shen         stdplus::print(
834baf41c8SHao Zhou             stderr,
848d618532SMichael Shen             "{} exists so we acked cleaning done and must be in BM mode\n",
854baf41c8SHao Zhou             bmDriveCleaningDoneAckFlagPath);
863f3ca035SBrandon Kim         return static_cast<uint8_t>(BmcMode::BM_MODE);
873f3ca035SBrandon Kim     }
883f3ca035SBrandon Kim 
8915d4d21cSHao Zhou     if (fs->exists(bmDriveCleaningDoneFlagPath, ec))
904baf41c8SHao Zhou     {
9115d4d21cSHao Zhou         fs->rename(bmDriveCleaningDoneFlagPath, bmDriveCleaningDoneAckFlagPath,
924baf41c8SHao Zhou                    ec);
938d618532SMichael Shen         stdplus::print(
944baf41c8SHao Zhou             stderr,
958d618532SMichael Shen             "{} exists so we just finished cleaning and must be in BM mode\n",
964baf41c8SHao Zhou             bmDriveCleaningDoneFlagPath);
974baf41c8SHao Zhou         return static_cast<uint8_t>(BmcMode::BM_MODE);
984baf41c8SHao Zhou     }
994baf41c8SHao Zhou 
10015d4d21cSHao Zhou     if (fs->exists(BM_SIGNAL_PATH, ec))
1014baf41c8SHao Zhou     {
10215d4d21cSHao Zhou         if (!fs->exists(bmDriveCleaningFlagPath, ec))
1034baf41c8SHao Zhou         {
10415d4d21cSHao Zhou             fs->create(bmDriveCleaningFlagPath);
1054baf41c8SHao Zhou         }
1064baf41c8SHao Zhou 
1078d618532SMichael Shen         stdplus::print(
1084baf41c8SHao Zhou             stderr,
1098d618532SMichael Shen             "{} exists and no done/ack flag, we must be in BM cleaning mode\n",
1103f3ca035SBrandon Kim             BM_SIGNAL_PATH);
1114baf41c8SHao Zhou         return static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE);
1124baf41c8SHao Zhou     }
1134baf41c8SHao Zhou 
1148d618532SMichael Shen     stdplus::print(
1154baf41c8SHao Zhou         stderr,
1164baf41c8SHao Zhou         "Unable to find any BM state files so we must not be in BM mode\n");
1175e70dc8cSNikhil Namjoshi     return static_cast<uint8_t>(BmcMode::NON_BM_MODE);
1185e70dc8cSNikhil Namjoshi #endif
1195e70dc8cSNikhil Namjoshi }
1205e70dc8cSNikhil Namjoshi 
1215e70dc8cSNikhil Namjoshi uint8_t Handler::getBmcMode()
1225e70dc8cSNikhil Namjoshi {
1235e70dc8cSNikhil Namjoshi     // BM_CLEANING_MODE is not implemented yet
12415d4d21cSHao Zhou     return isBmcInBareMetalMode(this->getFs());
1255e70dc8cSNikhil Namjoshi }
1265e70dc8cSNikhil Namjoshi 
127b69209b4SWilliam A. Kennington III std::tuple<std::uint8_t, std::string>
128b69209b4SWilliam A. Kennington III     Handler::getEthDetails(std::string intf) const
129f085d91dSPatrick Venture {
130b69209b4SWilliam A. Kennington III     if (intf.empty())
131b69209b4SWilliam A. Kennington III     {
132b69209b4SWilliam A. Kennington III         intf = NCSI_IF_NAME_STR;
133b69209b4SWilliam A. Kennington III     }
134b69209b4SWilliam A. Kennington III     return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf));
135f085d91dSPatrick Venture }
136f085d91dSPatrick Venture 
137d2037c6aSPatrick Venture std::int64_t Handler::getRxPackets(const std::string& name) const
138d2037c6aSPatrick Venture {
139d2037c6aSPatrick Venture     std::ostringstream opath;
140d2037c6aSPatrick Venture     opath << "/sys/class/net/" << name << "/statistics/rx_packets";
141d2037c6aSPatrick Venture     std::string path = opath.str();
142d2037c6aSPatrick Venture 
143d2037c6aSPatrick Venture     // Minor sanity & security check (of course, I'm less certain if unicode
144d2037c6aSPatrick Venture     // comes into play here.
145d2037c6aSPatrick Venture     //
146d2037c6aSPatrick Venture     // Basically you can't easily inject ../ or /../ into the path below.
147d2037c6aSPatrick Venture     if (name.find("/") != std::string::npos)
148d2037c6aSPatrick Venture     {
1498d618532SMichael Shen         stdplus::print(stderr, "Invalid or illegal name: '{}'\n", name);
150e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
151d2037c6aSPatrick Venture     }
152d2037c6aSPatrick Venture 
153d2037c6aSPatrick Venture     std::error_code ec;
15415d4d21cSHao Zhou     if (!this->getFs()->exists(path, ec))
155d2037c6aSPatrick Venture     {
1568d618532SMichael Shen         stdplus::print(stderr, "Path: '{}' doesn't exist.\n", path);
157e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
158d2037c6aSPatrick Venture     }
159d2037c6aSPatrick Venture     // We're uninterested in the state of ec.
160d2037c6aSPatrick Venture 
161d2037c6aSPatrick Venture     int64_t count = 0;
162d2037c6aSPatrick Venture     std::ifstream ifs;
163d2037c6aSPatrick Venture     ifs.exceptions(std::ifstream::failbit);
164d2037c6aSPatrick Venture     try
165d2037c6aSPatrick Venture     {
166d2037c6aSPatrick Venture         ifs.open(path);
167d2037c6aSPatrick Venture         ifs >> count;
168d2037c6aSPatrick Venture     }
169d2037c6aSPatrick Venture     catch (std::ios_base::failure& fail)
170d2037c6aSPatrick Venture     {
171e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
172d2037c6aSPatrick Venture     }
173d2037c6aSPatrick Venture 
174d2037c6aSPatrick Venture     return count;
175d2037c6aSPatrick Venture }
176d2037c6aSPatrick Venture 
177bb90d4fdSPatrick Venture VersionTuple Handler::getCpldVersion(unsigned int id) const
178bb90d4fdSPatrick Venture {
179bb90d4fdSPatrick Venture     std::ostringstream opath;
180bb90d4fdSPatrick Venture     opath << "/run/cpld" << id << ".version";
181bb90d4fdSPatrick Venture     // Check for file
182bb90d4fdSPatrick Venture 
183bb90d4fdSPatrick Venture     std::error_code ec;
18415d4d21cSHao Zhou     if (!this->getFs()->exists(opath.str(), ec))
185bb90d4fdSPatrick Venture     {
1868d618532SMichael Shen         stdplus::print(stderr, "Path: '{}' doesn't exist.\n", opath.str());
187e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
188bb90d4fdSPatrick Venture     }
189bb90d4fdSPatrick Venture     // We're uninterested in the state of ec.
190bb90d4fdSPatrick Venture 
191bb90d4fdSPatrick Venture     // If file exists, read.
192bb90d4fdSPatrick Venture     std::ifstream ifs;
193bb90d4fdSPatrick Venture     ifs.exceptions(std::ifstream::failbit);
194bb90d4fdSPatrick Venture     std::string value;
195bb90d4fdSPatrick Venture     try
196bb90d4fdSPatrick Venture     {
197bb90d4fdSPatrick Venture         ifs.open(opath.str());
198bb90d4fdSPatrick Venture         ifs >> value;
199bb90d4fdSPatrick Venture     }
200bb90d4fdSPatrick Venture     catch (std::ios_base::failure& fail)
201bb90d4fdSPatrick Venture     {
202e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
203bb90d4fdSPatrick Venture     }
204bb90d4fdSPatrick Venture 
205bb90d4fdSPatrick Venture     // If value parses as expected, return version.
206bb90d4fdSPatrick Venture     VersionTuple version = std::make_tuple(0, 0, 0, 0);
207bb90d4fdSPatrick Venture 
208444b5ea4SPatrick Williams     int num_fields = std::sscanf(value.c_str(),
209444b5ea4SPatrick Williams                                  "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
210bb90d4fdSPatrick Venture                                  &std::get<0>(version), &std::get<1>(version),
211bb90d4fdSPatrick Venture                                  &std::get<2>(version), &std::get<3>(version));
212bb90d4fdSPatrick Venture     if (num_fields == 0)
213bb90d4fdSPatrick Venture     {
2148d618532SMichael Shen         stdplus::print(stderr, "Invalid version.\n");
215e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
216bb90d4fdSPatrick Venture     }
217bb90d4fdSPatrick Venture 
218bb90d4fdSPatrick Venture     return version;
219bb90d4fdSPatrick Venture }
220bb90d4fdSPatrick Venture 
221aa374120SPatrick Venture static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
222aa374120SPatrick Venture static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
223aa374120SPatrick Venture static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
224aa374120SPatrick Venture static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
225aa374120SPatrick Venture static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
226aa374120SPatrick Venture 
227aa374120SPatrick Venture void Handler::psuResetDelay(std::uint32_t delay) const
228aa374120SPatrick Venture {
229aa374120SPatrick Venture     std::ofstream ofs;
230aa374120SPatrick Venture     ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
231aa374120SPatrick Venture     if (!ofs.good())
232aa374120SPatrick Venture     {
2338d618532SMichael Shen         stdplus::print(stderr, "Unable to open file for output.\n");
234e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
235aa374120SPatrick Venture     }
236aa374120SPatrick Venture 
237aa374120SPatrick Venture     ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
238aa374120SPatrick Venture     if (ofs.fail())
239aa374120SPatrick Venture     {
2408d618532SMichael Shen         stdplus::print(stderr, "Write failed\n");
241aa374120SPatrick Venture         ofs.close();
242e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
243aa374120SPatrick Venture     }
244aa374120SPatrick Venture 
245aa374120SPatrick Venture     // Write succeeded, please continue.
246aa374120SPatrick Venture     ofs.flush();
247aa374120SPatrick Venture     ofs.close();
248aa374120SPatrick Venture 
249aa374120SPatrick Venture     auto bus = sdbusplus::bus::new_default();
250aa374120SPatrick Venture     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
251aa374120SPatrick Venture                                       SYSTEMD_INTERFACE, "StartUnit");
252aa374120SPatrick Venture 
253aa374120SPatrick Venture     method.append(PSU_HARDRESET_TARGET);
254aa374120SPatrick Venture     method.append("replace");
255aa374120SPatrick Venture 
256aa374120SPatrick Venture     try
257aa374120SPatrick Venture     {
258aa374120SPatrick Venture         bus.call_noreply(method);
259aa374120SPatrick Venture     }
260aa374120SPatrick Venture     catch (const sdbusplus::exception::SdBusError& ex)
261aa374120SPatrick Venture     {
262aa374120SPatrick Venture         log<level::ERR>("Failed to call PSU hard reset");
263e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
264aa374120SPatrick Venture     }
265aa374120SPatrick Venture }
266aa374120SPatrick Venture 
267ac4a16f7SShounak Mitra static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5";
268ac4a16f7SShounak Mitra 
269ac4a16f7SShounak Mitra void Handler::psuResetOnShutdown() const
270ac4a16f7SShounak Mitra {
271ac4a16f7SShounak Mitra     std::ofstream ofs;
272ac4a16f7SShounak Mitra     ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out);
273ac4a16f7SShounak Mitra     if (!ofs.good())
274ac4a16f7SShounak Mitra     {
2758d618532SMichael Shen         stdplus::print(stderr, "Unable to open file for output.\n");
276e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
277ac4a16f7SShounak Mitra     }
278ac4a16f7SShounak Mitra     ofs.close();
279ac4a16f7SShounak Mitra }
280ac4a16f7SShounak Mitra 
2813b1b427cSWilly Tu uint32_t Handler::getFlashSize()
2823b1b427cSWilly Tu {
2833b1b427cSWilly Tu     mtd_info_t info;
2843b1b427cSWilly Tu     int fd = open("/dev/mtd0", O_RDONLY);
2853b1b427cSWilly Tu     int err = ioctl(fd, MEMGETINFO, &info);
2863b1b427cSWilly Tu     close(fd);
2873b1b427cSWilly Tu 
2883b1b427cSWilly Tu     if (err)
2893b1b427cSWilly Tu     {
290e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
2913b1b427cSWilly Tu     }
2923b1b427cSWilly Tu     return info.size;
2933b1b427cSWilly Tu }
2943b1b427cSWilly Tu 
295ab650004SPatrick Venture std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
29607f85150SPatrick Venture {
297ab650004SPatrick Venture     // Check if we support this Entity ID.
298ab650004SPatrick Venture     auto it = _entityIdToName.find(id);
299ab650004SPatrick Venture     if (it == _entityIdToName.end())
30007f85150SPatrick Venture     {
301ab650004SPatrick Venture         log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
302e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
30307f85150SPatrick Venture     }
30407f85150SPatrick Venture 
305ab650004SPatrick Venture     std::string entityName;
306ab650004SPatrick Venture     try
30707f85150SPatrick Venture     {
308ab650004SPatrick Venture         // Parse the JSON config file.
309ab650004SPatrick Venture         if (!_entityConfigParsed)
310ab650004SPatrick Venture         {
311ab650004SPatrick Venture             _entityConfig = parseConfig(_configFile);
312ab650004SPatrick Venture             _entityConfigParsed = true;
31307f85150SPatrick Venture         }
31407f85150SPatrick Venture 
315ab650004SPatrick Venture         // Find the "entity id:entity instance" mapping to entity name.
316ab650004SPatrick Venture         entityName = readNameFromConfig(it->second, instance, _entityConfig);
317ab650004SPatrick Venture         if (entityName.empty())
318ab650004SPatrick Venture         {
319e5a06675SMichael Shen             throw IpmiException(::ipmi::ccInvalidFieldRequest);
320ab650004SPatrick Venture         }
321ab650004SPatrick Venture     }
322ab650004SPatrick Venture     catch (InternalFailure& e)
323ab650004SPatrick Venture     {
324e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
32507f85150SPatrick Venture     }
32607f85150SPatrick Venture 
327ab650004SPatrick Venture     return entityName;
328ab650004SPatrick Venture }
329ab650004SPatrick Venture 
33029f35bceSWilliam A. Kennington III std::string Handler::getMachineName()
33129f35bceSWilliam A. Kennington III {
33229f35bceSWilliam A. Kennington III     const char* path = "/etc/os-release";
33329f35bceSWilliam A. Kennington III     std::ifstream ifs(path);
33429f35bceSWilliam A. Kennington III     if (ifs.fail())
33529f35bceSWilliam A. Kennington III     {
3368d618532SMichael Shen         stdplus::print(stderr, "Failed to open: {}\n", path);
337e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
33829f35bceSWilliam A. Kennington III     }
33929f35bceSWilliam A. Kennington III 
34029f35bceSWilliam A. Kennington III     std::string line;
34129f35bceSWilliam A. Kennington III     while (true)
34229f35bceSWilliam A. Kennington III     {
34329f35bceSWilliam A. Kennington III         std::getline(ifs, line);
34429f35bceSWilliam A. Kennington III         if (ifs.eof())
34529f35bceSWilliam A. Kennington III         {
3468d618532SMichael Shen             stdplus::print(stderr,
3478d618532SMichael Shen                            "Failed to find OPENBMC_TARGET_MACHINE: {}\n", path);
348e5a06675SMichael Shen             throw IpmiException(::ipmi::ccInvalidCommand);
34929f35bceSWilliam A. Kennington III         }
35029f35bceSWilliam A. Kennington III         if (ifs.fail())
35129f35bceSWilliam A. Kennington III         {
3528d618532SMichael Shen             stdplus::print(stderr, "Failed to read: {}\n", path);
353e5a06675SMichael Shen             throw IpmiException(::ipmi::ccUnspecifiedError);
35429f35bceSWilliam A. Kennington III         }
35529f35bceSWilliam A. Kennington III         std::string_view lineView(line);
35629f35bceSWilliam A. Kennington III         constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
35729f35bceSWilliam A. Kennington III         if (lineView.substr(0, prefix.size()) != prefix)
35829f35bceSWilliam A. Kennington III         {
35929f35bceSWilliam A. Kennington III             continue;
36029f35bceSWilliam A. Kennington III         }
36129f35bceSWilliam A. Kennington III         lineView.remove_prefix(prefix.size());
36229f35bceSWilliam A. Kennington III         lineView.remove_prefix(
36329f35bceSWilliam A. Kennington III             std::min(lineView.find_first_not_of('"'), lineView.size()));
36429f35bceSWilliam A. Kennington III         lineView.remove_suffix(
36529f35bceSWilliam A. Kennington III             lineView.size() - 1 -
36629f35bceSWilliam A. Kennington III             std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
36729f35bceSWilliam A. Kennington III         return std::string(lineView);
36829f35bceSWilliam A. Kennington III     }
36929f35bceSWilliam A. Kennington III }
37029f35bceSWilliam A. Kennington III 
3718cfa4c44Slinyuny static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay";
3728cfa4c44Slinyuny static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target";
3738cfa4c44Slinyuny 
3748cfa4c44Slinyuny void Handler::hostPowerOffDelay(std::uint32_t delay) const
3758cfa4c44Slinyuny {
3768cfa4c44Slinyuny     // Set time delay
3778cfa4c44Slinyuny     std::ofstream ofs;
3788cfa4c44Slinyuny     ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out);
3798cfa4c44Slinyuny     if (!ofs.good())
3808cfa4c44Slinyuny     {
3818d618532SMichael Shen         stdplus::print(stderr, "Unable to open file for output.\n");
382e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
3838cfa4c44Slinyuny     }
3848cfa4c44Slinyuny 
3858cfa4c44Slinyuny     ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl;
3868cfa4c44Slinyuny     ofs.close();
3878cfa4c44Slinyuny     if (ofs.fail())
3888cfa4c44Slinyuny     {
3898d618532SMichael Shen         stdplus::print(stderr, "Write failed\n");
390e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
3918cfa4c44Slinyuny     }
3928cfa4c44Slinyuny 
3938cfa4c44Slinyuny     // Write succeeded, please continue.
3948cfa4c44Slinyuny     auto bus = sdbusplus::bus::new_default();
3958cfa4c44Slinyuny     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
3968cfa4c44Slinyuny                                       SYSTEMD_INTERFACE, "StartUnit");
3978cfa4c44Slinyuny 
3988cfa4c44Slinyuny     method.append(HOST_POWEROFF_TARGET);
3998cfa4c44Slinyuny     method.append("replace");
4008cfa4c44Slinyuny 
4018cfa4c44Slinyuny     try
4028cfa4c44Slinyuny     {
4038cfa4c44Slinyuny         bus.call_noreply(method);
4048cfa4c44Slinyuny     }
4058cfa4c44Slinyuny     catch (const sdbusplus::exception::SdBusError& ex)
4068cfa4c44Slinyuny     {
4078cfa4c44Slinyuny         log<level::ERR>("Failed to call Power Off",
4088cfa4c44Slinyuny                         entry("WHAT=%s", ex.what()));
409e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
4108cfa4c44Slinyuny     }
4118cfa4c44Slinyuny }
4128cfa4c44Slinyuny 
413ab650004SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance,
414ab650004SPatrick Venture                                const Json& config)
41507f85150SPatrick Venture {
41607f85150SPatrick Venture     static const std::vector<Json> empty{};
41707f85150SPatrick Venture     std::vector<Json> readings = config.value(type, empty);
41807f85150SPatrick Venture     std::string name = "";
419ab650004SPatrick Venture 
42007f85150SPatrick Venture     for (const auto& j : readings)
42107f85150SPatrick Venture     {
42207f85150SPatrick Venture         uint8_t instanceNum = j.value("instance", 0);
42307f85150SPatrick Venture         // Not the instance we're interested in
42407f85150SPatrick Venture         if (instanceNum != instance)
42507f85150SPatrick Venture         {
42607f85150SPatrick Venture             continue;
42707f85150SPatrick Venture         }
42807f85150SPatrick Venture 
42907f85150SPatrick Venture         // Found the instance we're interested in
43007f85150SPatrick Venture         name = j.value("name", "");
43107f85150SPatrick Venture 
43207f85150SPatrick Venture         break;
43307f85150SPatrick Venture     }
434ab650004SPatrick Venture 
43507f85150SPatrick Venture     return name;
43607f85150SPatrick Venture }
43707f85150SPatrick Venture 
43849f23ad9SPatrick Venture void Handler::buildI2cPcieMapping()
43949f23ad9SPatrick Venture {
44049f23ad9SPatrick Venture     _pcie_i2c_map = buildPcieMap();
44149f23ad9SPatrick Venture }
44249f23ad9SPatrick Venture 
44349f23ad9SPatrick Venture size_t Handler::getI2cPcieMappingSize() const
44449f23ad9SPatrick Venture {
44549f23ad9SPatrick Venture     return _pcie_i2c_map.size();
44649f23ad9SPatrick Venture }
44749f23ad9SPatrick Venture 
44849f23ad9SPatrick Venture std::tuple<std::uint32_t, std::string>
44949f23ad9SPatrick Venture     Handler::getI2cEntry(unsigned int entry) const
45049f23ad9SPatrick Venture {
45149f23ad9SPatrick Venture     return _pcie_i2c_map[entry];
45249f23ad9SPatrick Venture }
45349f23ad9SPatrick Venture 
4544f0d1de6SSteve Foreman namespace
4554f0d1de6SSteve Foreman {
4564f0d1de6SSteve Foreman 
4574f0d1de6SSteve Foreman static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/";
4584f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel";
4594f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR";
4604f0d1de6SSteve Foreman 
4614f0d1de6SSteve Foreman // C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects()
4624f0d1de6SSteve Foreman using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>;
4634f0d1de6SSteve Foreman using AnyTypeList = std::vector<std::pair<std::string, AnyType>>;
4644f0d1de6SSteve Foreman using NamedArrayOfAnyTypeLists =
4654f0d1de6SSteve Foreman     std::vector<std::pair<std::string, AnyTypeList>>;
4664f0d1de6SSteve Foreman using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector<
4674f0d1de6SSteve Foreman     std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>;
4684f0d1de6SSteve Foreman 
4694f0d1de6SSteve Foreman } // namespace
4704f0d1de6SSteve Foreman 
47165371228SPatrick Williams sdbusplus::bus_t Handler::getDbus() const
4724f0d1de6SSteve Foreman {
4734f0d1de6SSteve Foreman     return sdbusplus::bus::new_default();
4744f0d1de6SSteve Foreman }
4754f0d1de6SSteve Foreman 
47615d4d21cSHao Zhou const std::unique_ptr<FileSystemInterface>& Handler::getFs() const
47715d4d21cSHao Zhou {
47815d4d21cSHao Zhou     return this->fsPtr;
47915d4d21cSHao Zhou }
48015d4d21cSHao Zhou 
4814f0d1de6SSteve Foreman uint32_t Handler::accelOobDeviceCount() const
4824f0d1de6SSteve Foreman {
4834f0d1de6SSteve Foreman     ArrayOfObjectPathsAndTieredAnyTypeLists data;
4844f0d1de6SSteve Foreman 
4854f0d1de6SSteve Foreman     try
4864f0d1de6SSteve Foreman     {
4870e928ac6SMichael Shen         auto bus = getDbus();
4884f0d1de6SSteve Foreman         auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
4894f0d1de6SSteve Foreman                                           "org.freedesktop.DBus.ObjectManager",
4904f0d1de6SSteve Foreman                                           "GetManagedObjects");
4914f0d1de6SSteve Foreman         bus.call(method).read(data);
4924f0d1de6SSteve Foreman     }
4934f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
4944f0d1de6SSteve Foreman     {
4954f0d1de6SSteve Foreman         log<level::ERR>(
4964f0d1de6SSteve Foreman             "Failed to call GetManagedObjects on com.google.custom_accel",
4974f0d1de6SSteve Foreman             entry("WHAT=%s", ex.what()));
498e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
4994f0d1de6SSteve Foreman     }
5004f0d1de6SSteve Foreman 
5014f0d1de6SSteve Foreman     return data.size();
5024f0d1de6SSteve Foreman }
5034f0d1de6SSteve Foreman 
5044f0d1de6SSteve Foreman std::string Handler::accelOobDeviceName(size_t index) const
5054f0d1de6SSteve Foreman {
5064f0d1de6SSteve Foreman     ArrayOfObjectPathsAndTieredAnyTypeLists data;
5074f0d1de6SSteve Foreman 
5084f0d1de6SSteve Foreman     try
5094f0d1de6SSteve Foreman     {
5100e928ac6SMichael Shen         auto bus = getDbus();
5114f0d1de6SSteve Foreman         auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
5124f0d1de6SSteve Foreman                                           "org.freedesktop.DBus.ObjectManager",
5134f0d1de6SSteve Foreman                                           "GetManagedObjects");
5144f0d1de6SSteve Foreman         bus.call(method).read(data);
5154f0d1de6SSteve Foreman     }
5164f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
5174f0d1de6SSteve Foreman     {
5184f0d1de6SSteve Foreman         log<level::ERR>(
5194f0d1de6SSteve Foreman             "Failed to call GetManagedObjects on com.google.custom_accel",
5204f0d1de6SSteve Foreman             entry("WHAT=%s", ex.what()));
521e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
5224f0d1de6SSteve Foreman     }
5234f0d1de6SSteve Foreman 
5244f0d1de6SSteve Foreman     if (index >= data.size())
5254f0d1de6SSteve Foreman     {
5264f0d1de6SSteve Foreman         log<level::WARNING>(
5274f0d1de6SSteve Foreman             "Requested index is larger than the number of entries.",
5284f0d1de6SSteve Foreman             entry("INDEX=%zu", index), entry("NUM_NAMES=%zu", data.size()));
529e5a06675SMichael Shen         throw IpmiException(::ipmi::ccParmOutOfRange);
5304f0d1de6SSteve Foreman     }
5314f0d1de6SSteve Foreman 
5324f0d1de6SSteve Foreman     std::string_view name(data[index].first.str);
5334f0d1de6SSteve Foreman     if (!name.starts_with(ACCEL_OOB_ROOT))
5344f0d1de6SSteve Foreman     {
535e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidCommand);
5364f0d1de6SSteve Foreman     }
5374f0d1de6SSteve Foreman     name.remove_prefix(ACCEL_OOB_ROOT.length());
5384f0d1de6SSteve Foreman     return std::string(name);
5394f0d1de6SSteve Foreman }
5404f0d1de6SSteve Foreman 
5414f0d1de6SSteve Foreman uint64_t Handler::accelOobRead(std::string_view name, uint64_t address,
5424f0d1de6SSteve Foreman                                uint8_t num_bytes) const
5434f0d1de6SSteve Foreman {
5444f0d1de6SSteve Foreman     static constexpr char ACCEL_OOB_METHOD[] = "Read";
5454f0d1de6SSteve Foreman 
5464f0d1de6SSteve Foreman     std::string object_name(ACCEL_OOB_ROOT);
5474f0d1de6SSteve Foreman     object_name.append(name);
5484f0d1de6SSteve Foreman 
5490e928ac6SMichael Shen     auto bus = getDbus();
5504f0d1de6SSteve Foreman     auto method = bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
5514f0d1de6SSteve Foreman                                       ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD);
5524f0d1de6SSteve Foreman     method.append(address, static_cast<uint64_t>(num_bytes));
5534f0d1de6SSteve Foreman 
5544f0d1de6SSteve Foreman     std::vector<uint8_t> bytes;
5554f0d1de6SSteve Foreman 
5564f0d1de6SSteve Foreman     try
5574f0d1de6SSteve Foreman     {
5584f0d1de6SSteve Foreman         bus.call(method).read(bytes);
5594f0d1de6SSteve Foreman     }
5604f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
5614f0d1de6SSteve Foreman     {
5624f0d1de6SSteve Foreman         log<level::ERR>("Failed to call Read on com.google.custom_accel",
5634f0d1de6SSteve Foreman                         entry("WHAT=%s", ex.what()),
5644f0d1de6SSteve Foreman                         entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
5654f0d1de6SSteve Foreman                         entry("DBUS_OBJECT=%s", object_name.c_str()),
5664f0d1de6SSteve Foreman                         entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
5674f0d1de6SSteve Foreman                         entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
5684f0d1de6SSteve Foreman                         entry("DBUS_ARG_ADDRESS=%016llx", address),
5694f0d1de6SSteve Foreman                         entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes));
570e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
5714f0d1de6SSteve Foreman     }
5724f0d1de6SSteve Foreman 
5734f0d1de6SSteve Foreman     if (bytes.size() < num_bytes)
5744f0d1de6SSteve Foreman     {
5754f0d1de6SSteve Foreman         log<level::ERR>(
5764f0d1de6SSteve Foreman             "Call to Read on com.google.custom_accel didn't return the expected"
5774f0d1de6SSteve Foreman             " number of bytes.",
5784f0d1de6SSteve Foreman             entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
5794f0d1de6SSteve Foreman             entry("DBUS_OBJECT=%s", object_name.c_str()),
5804f0d1de6SSteve Foreman             entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
5814f0d1de6SSteve Foreman             entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
5824f0d1de6SSteve Foreman             entry("DBUS_ARG_ADDRESS=%016llx", address),
5834f0d1de6SSteve Foreman             entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
5844f0d1de6SSteve Foreman             entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
585e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
5864f0d1de6SSteve Foreman     }
5874f0d1de6SSteve Foreman 
5884f0d1de6SSteve Foreman     if (bytes.size() > sizeof(uint64_t))
5894f0d1de6SSteve Foreman     {
5904f0d1de6SSteve Foreman         log<level::ERR>(
5914f0d1de6SSteve Foreman             "Call to Read on com.google.custom_accel returned more than 8B.",
5924f0d1de6SSteve Foreman             entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
5934f0d1de6SSteve Foreman             entry("DBUS_OBJECT=%s", object_name.c_str()),
5944f0d1de6SSteve Foreman             entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
5954f0d1de6SSteve Foreman             entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
5964f0d1de6SSteve Foreman             entry("DBUS_ARG_ADDRESS=%016llx", address),
5974f0d1de6SSteve Foreman             entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
5984f0d1de6SSteve Foreman             entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
599e5a06675SMichael Shen         throw IpmiException(::ipmi::ccReqDataTruncated);
6004f0d1de6SSteve Foreman     }
6014f0d1de6SSteve Foreman 
6024f0d1de6SSteve Foreman     uint64_t data = 0;
6034f0d1de6SSteve Foreman     for (size_t i = 0; i < num_bytes; ++i)
6044f0d1de6SSteve Foreman     {
6054f0d1de6SSteve Foreman         data = (data << 8) | bytes[i];
6064f0d1de6SSteve Foreman     }
6074f0d1de6SSteve Foreman 
6084f0d1de6SSteve Foreman     return data;
6094f0d1de6SSteve Foreman }
6104f0d1de6SSteve Foreman 
6114f0d1de6SSteve Foreman void Handler::accelOobWrite(std::string_view name, uint64_t address,
6124f0d1de6SSteve Foreman                             uint8_t num_bytes, uint64_t data) const
6134f0d1de6SSteve Foreman {
6144f0d1de6SSteve Foreman     static constexpr std::string_view ACCEL_OOB_METHOD = "Write";
6154f0d1de6SSteve Foreman 
6164f0d1de6SSteve Foreman     std::string object_name(ACCEL_OOB_ROOT);
6174f0d1de6SSteve Foreman     object_name.append(name);
6184f0d1de6SSteve Foreman 
6194f0d1de6SSteve Foreman     if (num_bytes > sizeof(data))
6204f0d1de6SSteve Foreman     {
6214f0d1de6SSteve Foreman         log<level::ERR>(
6224f0d1de6SSteve Foreman             "Call to Write on com.google.custom_accel requested more than 8B.",
6234f0d1de6SSteve Foreman             entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
6244f0d1de6SSteve Foreman             entry("DBUS_OBJECT=%s", object_name.c_str()),
6254f0d1de6SSteve Foreman             entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
6264f0d1de6SSteve Foreman             entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
6274f0d1de6SSteve Foreman             entry("DBUS_ARG_ADDRESS=%016llx", address),
6284f0d1de6SSteve Foreman             entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
6294f0d1de6SSteve Foreman             entry("DBUS_ARG_DATA=%016llx", data));
630e5a06675SMichael Shen         throw IpmiException(::ipmi::ccParmOutOfRange);
6314f0d1de6SSteve Foreman     }
6324f0d1de6SSteve Foreman 
6334f0d1de6SSteve Foreman     std::vector<uint8_t> bytes;
6344f0d1de6SSteve Foreman     bytes.reserve(num_bytes);
6354f0d1de6SSteve Foreman     for (size_t i = 0; i < num_bytes; ++i)
6364f0d1de6SSteve Foreman     {
6374f0d1de6SSteve Foreman         bytes.emplace_back(data & 0xff);
6384f0d1de6SSteve Foreman         data >>= 8;
6394f0d1de6SSteve Foreman     }
6404f0d1de6SSteve Foreman 
6414f0d1de6SSteve Foreman     try
6424f0d1de6SSteve Foreman     {
6430e928ac6SMichael Shen         auto bus = getDbus();
6444f0d1de6SSteve Foreman         auto method =
6454f0d1de6SSteve Foreman             bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
6464f0d1de6SSteve Foreman                                 ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD.data());
6474f0d1de6SSteve Foreman         method.append(address, bytes);
6484f0d1de6SSteve Foreman         bus.call_noreply(method);
6494f0d1de6SSteve Foreman     }
6504f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
6514f0d1de6SSteve Foreman     {
6524f0d1de6SSteve Foreman         log<level::ERR>("Failed to call Write on com.google.custom_accel",
6534f0d1de6SSteve Foreman                         entry("WHAT=%s", ex.what()),
6544f0d1de6SSteve Foreman                         entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
6554f0d1de6SSteve Foreman                         entry("DBUS_OBJECT=%s", object_name.c_str()),
6564f0d1de6SSteve Foreman                         entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
6574f0d1de6SSteve Foreman                         entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
6584f0d1de6SSteve Foreman                         entry("DBUS_ARG_ADDRESS=%016llx", address),
6594f0d1de6SSteve Foreman                         entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
6604f0d1de6SSteve Foreman                         entry("DBUS_ARG_DATA=%016llx", data));
661e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
6624f0d1de6SSteve Foreman     }
6634f0d1de6SSteve Foreman }
6644f0d1de6SSteve Foreman 
6656c71b0f9SWilly Tu std::vector<uint8_t> Handler::pcieBifurcation(uint8_t index)
6666c71b0f9SWilly Tu {
6676c71b0f9SWilly Tu     return bifurcationHelper.get().getBifurcation(index).value_or(
6686c71b0f9SWilly Tu         std::vector<uint8_t>{});
6696c71b0f9SWilly Tu }
6706c71b0f9SWilly Tu 
671a92d0e6bSJohn Wedig static constexpr auto BARE_METAL_TARGET = "gbmc-bare-metal-active.target";
672a92d0e6bSJohn Wedig 
673a92d0e6bSJohn Wedig void Handler::linuxBootDone() const
674a92d0e6bSJohn Wedig {
67515d4d21cSHao Zhou     if (isBmcInBareMetalMode(this->fsPtr) !=
67615d4d21cSHao Zhou         static_cast<uint8_t>(BmcMode::BM_MODE))
677a92d0e6bSJohn Wedig     {
678a92d0e6bSJohn Wedig         return;
679a92d0e6bSJohn Wedig     }
680a92d0e6bSJohn Wedig 
681a92d0e6bSJohn Wedig     log<level::INFO>("LinuxBootDone: Disabling IPMI");
682a92d0e6bSJohn Wedig 
683a92d0e6bSJohn Wedig     // Start the bare metal active systemd target.
684a92d0e6bSJohn Wedig     auto bus = sdbusplus::bus::new_default();
685a92d0e6bSJohn Wedig     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
686a92d0e6bSJohn Wedig                                       SYSTEMD_INTERFACE, "StartUnit");
687a92d0e6bSJohn Wedig 
688a92d0e6bSJohn Wedig     method.append(BARE_METAL_TARGET);
689a92d0e6bSJohn Wedig     method.append("replace");
690a92d0e6bSJohn Wedig 
691a92d0e6bSJohn Wedig     try
692a92d0e6bSJohn Wedig     {
693a92d0e6bSJohn Wedig         bus.call_noreply(method);
694a92d0e6bSJohn Wedig     }
695a92d0e6bSJohn Wedig     catch (const sdbusplus::exception::SdBusError& ex)
696a92d0e6bSJohn Wedig     {
697a92d0e6bSJohn Wedig         log<level::ERR>("Failed to start bare metal active systemd target",
698a92d0e6bSJohn Wedig                         entry("WHAT=%s", ex.what()));
699a92d0e6bSJohn Wedig         throw IpmiException(::ipmi::ccUnspecifiedError);
700a92d0e6bSJohn Wedig     }
701a92d0e6bSJohn Wedig }
702a92d0e6bSJohn Wedig 
703*d455bfd6SGaurav Gandhi static constexpr char ACCEL_POWER_SERVICE[] = "xyz.openbmc_project.AccelPower";
704*d455bfd6SGaurav Gandhi static constexpr char ACCEL_POWER_PATH_PREFIX[] =
705*d455bfd6SGaurav Gandhi     "/xyz/openbmc_project/control/accel_power_";
706*d455bfd6SGaurav Gandhi static constexpr char POWER_MODE_IFC[] =
707*d455bfd6SGaurav Gandhi     "xyz.openbmc_project.Control.Power.Mode";
708*d455bfd6SGaurav Gandhi 
709*d455bfd6SGaurav Gandhi void Handler::accelSetVrSettings(::ipmi::Context::ptr ctx, uint8_t chip_id,
710*d455bfd6SGaurav Gandhi                                  uint8_t settings_id, uint16_t value) const
711*d455bfd6SGaurav Gandhi {
712*d455bfd6SGaurav Gandhi     int vrSettingsReq;
713*d455bfd6SGaurav Gandhi     boost::system::error_code ec;
714*d455bfd6SGaurav Gandhi     if (_vrSettingsMap.find(settings_id) == _vrSettingsMap.end())
715*d455bfd6SGaurav Gandhi     {
716*d455bfd6SGaurav Gandhi         log<level::ERR>("Settings ID is not supported",
717*d455bfd6SGaurav Gandhi                         entry("settings_id=%d", settings_id));
718*d455bfd6SGaurav Gandhi         throw IpmiException(::ipmi::ccParmOutOfRange);
719*d455bfd6SGaurav Gandhi     }
720*d455bfd6SGaurav Gandhi 
721*d455bfd6SGaurav Gandhi     vrSettingsReq = static_cast<int>(settings_id | value << 8);
722*d455bfd6SGaurav Gandhi     std::string object_name(
723*d455bfd6SGaurav Gandhi         std::format("{}{}", ACCEL_POWER_PATH_PREFIX, chip_id));
724*d455bfd6SGaurav Gandhi 
725*d455bfd6SGaurav Gandhi     std::variant<int> val = vrSettingsReq;
726*d455bfd6SGaurav Gandhi     ctx->bus->yield_method_call(ctx->yield, ec, ACCEL_POWER_SERVICE,
727*d455bfd6SGaurav Gandhi                                 object_name.c_str(),
728*d455bfd6SGaurav Gandhi                                 "org.freedesktop.DBus.Properties", "Set",
729*d455bfd6SGaurav Gandhi                                 POWER_MODE_IFC, "PowerMode", val);
730*d455bfd6SGaurav Gandhi     if (ec)
731*d455bfd6SGaurav Gandhi     {
732*d455bfd6SGaurav Gandhi         log<level::ERR>("Failed to set PowerMode property");
733*d455bfd6SGaurav Gandhi         throw IpmiException(::ipmi::ccUnspecifiedError);
734*d455bfd6SGaurav Gandhi     }
735*d455bfd6SGaurav Gandhi }
736*d455bfd6SGaurav Gandhi 
737*d455bfd6SGaurav Gandhi static constexpr char EXTERNAL_SENSOR_SERVICE[] =
738*d455bfd6SGaurav Gandhi     "xyz.openbmc_project.ExternalSensor";
739*d455bfd6SGaurav Gandhi static constexpr char EXTERNAL_SENSOR_PATH_PREFIX[] =
740*d455bfd6SGaurav Gandhi     "/xyz/openbmc_project/sensors/power/";
741*d455bfd6SGaurav Gandhi static constexpr char SENSOR_VALUE_IFC[] = "xyz.openbmc_project.Sensor.Value";
742*d455bfd6SGaurav Gandhi 
743*d455bfd6SGaurav Gandhi uint16_t Handler::accelGetVrSettings(::ipmi::Context::ptr ctx, uint8_t chip_id,
744*d455bfd6SGaurav Gandhi                                      uint8_t settings_id) const
745*d455bfd6SGaurav Gandhi {
746*d455bfd6SGaurav Gandhi     Value value;
747*d455bfd6SGaurav Gandhi     boost::system::error_code ec;
748*d455bfd6SGaurav Gandhi     std::string object_name(std::format("{}{}{}", EXTERNAL_SENSOR_PATH_PREFIX,
749*d455bfd6SGaurav Gandhi                                         _vrSettingsMap.at(settings_id),
750*d455bfd6SGaurav Gandhi                                         chip_id));
751*d455bfd6SGaurav Gandhi 
752*d455bfd6SGaurav Gandhi     if (_vrSettingsMap.find(settings_id) == _vrSettingsMap.end())
753*d455bfd6SGaurav Gandhi     {
754*d455bfd6SGaurav Gandhi         log<level::ERR>("Settings ID is not supported",
755*d455bfd6SGaurav Gandhi                         entry("settings_id=%d", settings_id));
756*d455bfd6SGaurav Gandhi         throw IpmiException(::ipmi::ccParmOutOfRange);
757*d455bfd6SGaurav Gandhi     }
758*d455bfd6SGaurav Gandhi 
759*d455bfd6SGaurav Gandhi     value = ctx->bus->yield_method_call<std::variant<double>>(
760*d455bfd6SGaurav Gandhi         ctx->yield, ec, EXTERNAL_SENSOR_SERVICE, object_name.c_str(),
761*d455bfd6SGaurav Gandhi         "org.freedesktop.DBus.Properties", "Get", SENSOR_VALUE_IFC, "Value");
762*d455bfd6SGaurav Gandhi     if (ec)
763*d455bfd6SGaurav Gandhi     {
764*d455bfd6SGaurav Gandhi         log<level::ERR>("accelGetVrSettings: Failed to call GetObject ");
765*d455bfd6SGaurav Gandhi         throw IpmiException(::ipmi::ccUnspecifiedError);
766*d455bfd6SGaurav Gandhi     }
767*d455bfd6SGaurav Gandhi 
768*d455bfd6SGaurav Gandhi     return static_cast<uint16_t>(std::get<double>(value));
769*d455bfd6SGaurav Gandhi }
770f085d91dSPatrick Venture } // namespace ipmi
771f085d91dSPatrick Venture } // namespace google
772