xref: /openbmc/google-ipmi-sys/handler.cpp (revision 444b5ea4)
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.
14f085d91dSPatrick Venture #include "handler.hpp"
15f085d91dSPatrick Venture 
16*444b5ea4SPatrick Williams #include "bm_config.h"
17*444b5ea4SPatrick Williams 
188ec4106bSNikhil Namjoshi #include "bmc_mode_enum.hpp"
19d2037c6aSPatrick Venture #include "errors.hpp"
20c87de558SPatrick Venture #include "handler_impl.hpp"
21ab650004SPatrick Venture #include "util.hpp"
22d2037c6aSPatrick Venture 
233b1b427cSWilly Tu #include <fcntl.h>
24d2037c6aSPatrick Venture #include <ipmid/api.h>
253b1b427cSWilly Tu #include <mtd/mtd-abi.h>
263b1b427cSWilly Tu #include <mtd/mtd-user.h>
273b1b427cSWilly Tu #include <sys/ioctl.h>
283b1b427cSWilly Tu #include <unistd.h>
29d2037c6aSPatrick Venture 
30*444b5ea4SPatrick Williams #include <nlohmann/json.hpp>
31*444b5ea4SPatrick Williams #include <phosphor-logging/elog-errors.hpp>
32*444b5ea4SPatrick Williams #include <phosphor-logging/log.hpp>
33*444b5ea4SPatrick Williams #include <sdbusplus/bus.hpp>
34*444b5ea4SPatrick Williams #include <xyz/openbmc_project/Common/error.hpp>
35*444b5ea4SPatrick Williams 
36bb90d4fdSPatrick Venture #include <cinttypes>
37d2037c6aSPatrick Venture #include <cstdio>
38d2037c6aSPatrick Venture #include <filesystem>
39d2037c6aSPatrick Venture #include <fstream>
4007f85150SPatrick Venture #include <map>
41d2037c6aSPatrick Venture #include <sstream>
42d2037c6aSPatrick Venture #include <string>
4329f35bceSWilliam A. Kennington III #include <string_view>
44d2037c6aSPatrick Venture #include <tuple>
454f0d1de6SSteve Foreman #include <variant>
465e70dc8cSNikhil Namjoshi 
47f085d91dSPatrick Venture #ifndef NCSI_IF_NAME
48f085d91dSPatrick Venture #define NCSI_IF_NAME eth0
49f085d91dSPatrick Venture #endif
50f085d91dSPatrick Venture 
51f085d91dSPatrick Venture // To deal with receiving a string without quotes.
52f085d91dSPatrick Venture #define QUOTE(name) #name
53f085d91dSPatrick Venture #define STR(macro) QUOTE(macro)
54f085d91dSPatrick Venture #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
55f085d91dSPatrick Venture 
568d3d46a2SWilliam A. Kennington III namespace ipmi
578d3d46a2SWilliam A. Kennington III {
588d3d46a2SWilliam A. Kennington III std::uint8_t getChannelByName(const std::string& chName);
598d3d46a2SWilliam A. Kennington III }
608d3d46a2SWilliam A. Kennington III 
61f085d91dSPatrick Venture namespace google
62f085d91dSPatrick Venture {
63f085d91dSPatrick Venture namespace ipmi
64f085d91dSPatrick Venture {
65d2037c6aSPatrick Venture namespace fs = std::filesystem;
6607f85150SPatrick Venture using Json = nlohmann::json;
6707f85150SPatrick Venture using namespace phosphor::logging;
6807f85150SPatrick Venture using InternalFailure =
6907f85150SPatrick Venture     sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
70f085d91dSPatrick Venture 
715e70dc8cSNikhil Namjoshi uint8_t isBmcInBareMetalMode()
725e70dc8cSNikhil Namjoshi {
735e70dc8cSNikhil Namjoshi #if BARE_METAL
745e70dc8cSNikhil Namjoshi     return static_cast<uint8_t>(BmcMode::BM_MODE);
755e70dc8cSNikhil Namjoshi #else
763f3ca035SBrandon Kim     std::error_code ec;
773f3ca035SBrandon Kim     if (fs::exists(BM_SIGNAL_PATH, ec))
783f3ca035SBrandon Kim     {
793f3ca035SBrandon Kim         std::fprintf(stderr, "%s exists so we must be in BM mode\n",
803f3ca035SBrandon Kim                      BM_SIGNAL_PATH);
813f3ca035SBrandon Kim         return static_cast<uint8_t>(BmcMode::BM_MODE);
823f3ca035SBrandon Kim     }
833f3ca035SBrandon Kim 
843f3ca035SBrandon Kim     std::fprintf(stderr, "Unable to find %s so we must not be in BM mode\n",
853f3ca035SBrandon Kim                  BM_SIGNAL_PATH);
865e70dc8cSNikhil Namjoshi     return static_cast<uint8_t>(BmcMode::NON_BM_MODE);
875e70dc8cSNikhil Namjoshi #endif
885e70dc8cSNikhil Namjoshi }
895e70dc8cSNikhil Namjoshi 
905e70dc8cSNikhil Namjoshi uint8_t Handler::getBmcMode()
915e70dc8cSNikhil Namjoshi {
925e70dc8cSNikhil Namjoshi     // BM_CLEANING_MODE is not implemented yet
935e70dc8cSNikhil Namjoshi     return isBmcInBareMetalMode();
945e70dc8cSNikhil Namjoshi }
955e70dc8cSNikhil Namjoshi 
96b69209b4SWilliam A. Kennington III std::tuple<std::uint8_t, std::string>
97b69209b4SWilliam A. Kennington III     Handler::getEthDetails(std::string intf) const
98f085d91dSPatrick Venture {
99b69209b4SWilliam A. Kennington III     if (intf.empty())
100b69209b4SWilliam A. Kennington III     {
101b69209b4SWilliam A. Kennington III         intf = NCSI_IF_NAME_STR;
102b69209b4SWilliam A. Kennington III     }
103b69209b4SWilliam A. Kennington III     return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf));
104f085d91dSPatrick Venture }
105f085d91dSPatrick Venture 
106d2037c6aSPatrick Venture std::int64_t Handler::getRxPackets(const std::string& name) const
107d2037c6aSPatrick Venture {
108d2037c6aSPatrick Venture     std::ostringstream opath;
109d2037c6aSPatrick Venture     opath << "/sys/class/net/" << name << "/statistics/rx_packets";
110d2037c6aSPatrick Venture     std::string path = opath.str();
111d2037c6aSPatrick Venture 
112d2037c6aSPatrick Venture     // Minor sanity & security check (of course, I'm less certain if unicode
113d2037c6aSPatrick Venture     // comes into play here.
114d2037c6aSPatrick Venture     //
115d2037c6aSPatrick Venture     // Basically you can't easily inject ../ or /../ into the path below.
116d2037c6aSPatrick Venture     if (name.find("/") != std::string::npos)
117d2037c6aSPatrick Venture     {
118d2037c6aSPatrick Venture         std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
119e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
120d2037c6aSPatrick Venture     }
121d2037c6aSPatrick Venture 
122d2037c6aSPatrick Venture     std::error_code ec;
123d2037c6aSPatrick Venture     if (!fs::exists(path, ec))
124d2037c6aSPatrick Venture     {
125d2037c6aSPatrick Venture         std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
126e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
127d2037c6aSPatrick Venture     }
128d2037c6aSPatrick Venture     // We're uninterested in the state of ec.
129d2037c6aSPatrick Venture 
130d2037c6aSPatrick Venture     int64_t count = 0;
131d2037c6aSPatrick Venture     std::ifstream ifs;
132d2037c6aSPatrick Venture     ifs.exceptions(std::ifstream::failbit);
133d2037c6aSPatrick Venture     try
134d2037c6aSPatrick Venture     {
135d2037c6aSPatrick Venture         ifs.open(path);
136d2037c6aSPatrick Venture         ifs >> count;
137d2037c6aSPatrick Venture     }
138d2037c6aSPatrick Venture     catch (std::ios_base::failure& fail)
139d2037c6aSPatrick Venture     {
140e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
141d2037c6aSPatrick Venture     }
142d2037c6aSPatrick Venture 
143d2037c6aSPatrick Venture     return count;
144d2037c6aSPatrick Venture }
145d2037c6aSPatrick Venture 
146bb90d4fdSPatrick Venture VersionTuple Handler::getCpldVersion(unsigned int id) const
147bb90d4fdSPatrick Venture {
148bb90d4fdSPatrick Venture     std::ostringstream opath;
149bb90d4fdSPatrick Venture     opath << "/run/cpld" << id << ".version";
150bb90d4fdSPatrick Venture     // Check for file
151bb90d4fdSPatrick Venture 
152bb90d4fdSPatrick Venture     std::error_code ec;
153bb90d4fdSPatrick Venture     if (!fs::exists(opath.str(), ec))
154bb90d4fdSPatrick Venture     {
155bb90d4fdSPatrick Venture         std::fprintf(stderr, "Path: '%s' doesn't exist.\n",
156bb90d4fdSPatrick Venture                      opath.str().c_str());
157e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
158bb90d4fdSPatrick Venture     }
159bb90d4fdSPatrick Venture     // We're uninterested in the state of ec.
160bb90d4fdSPatrick Venture 
161bb90d4fdSPatrick Venture     // If file exists, read.
162bb90d4fdSPatrick Venture     std::ifstream ifs;
163bb90d4fdSPatrick Venture     ifs.exceptions(std::ifstream::failbit);
164bb90d4fdSPatrick Venture     std::string value;
165bb90d4fdSPatrick Venture     try
166bb90d4fdSPatrick Venture     {
167bb90d4fdSPatrick Venture         ifs.open(opath.str());
168bb90d4fdSPatrick Venture         ifs >> value;
169bb90d4fdSPatrick Venture     }
170bb90d4fdSPatrick Venture     catch (std::ios_base::failure& fail)
171bb90d4fdSPatrick Venture     {
172e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
173bb90d4fdSPatrick Venture     }
174bb90d4fdSPatrick Venture 
175bb90d4fdSPatrick Venture     // If value parses as expected, return version.
176bb90d4fdSPatrick Venture     VersionTuple version = std::make_tuple(0, 0, 0, 0);
177bb90d4fdSPatrick Venture 
178*444b5ea4SPatrick Williams     int num_fields = std::sscanf(value.c_str(),
179*444b5ea4SPatrick Williams                                  "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
180bb90d4fdSPatrick Venture                                  &std::get<0>(version), &std::get<1>(version),
181bb90d4fdSPatrick Venture                                  &std::get<2>(version), &std::get<3>(version));
182bb90d4fdSPatrick Venture     if (num_fields == 0)
183bb90d4fdSPatrick Venture     {
184bb90d4fdSPatrick Venture         std::fprintf(stderr, "Invalid version.\n");
185e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
186bb90d4fdSPatrick Venture     }
187bb90d4fdSPatrick Venture 
188bb90d4fdSPatrick Venture     return version;
189bb90d4fdSPatrick Venture }
190bb90d4fdSPatrick Venture 
191aa374120SPatrick Venture static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
192aa374120SPatrick Venture static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
193aa374120SPatrick Venture static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
194aa374120SPatrick Venture static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
195aa374120SPatrick Venture static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
196aa374120SPatrick Venture 
197aa374120SPatrick Venture void Handler::psuResetDelay(std::uint32_t delay) const
198aa374120SPatrick Venture {
199aa374120SPatrick Venture     std::ofstream ofs;
200aa374120SPatrick Venture     ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
201aa374120SPatrick Venture     if (!ofs.good())
202aa374120SPatrick Venture     {
203aa374120SPatrick Venture         std::fprintf(stderr, "Unable to open file for output.\n");
204e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
205aa374120SPatrick Venture     }
206aa374120SPatrick Venture 
207aa374120SPatrick Venture     ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
208aa374120SPatrick Venture     if (ofs.fail())
209aa374120SPatrick Venture     {
210aa374120SPatrick Venture         std::fprintf(stderr, "Write failed\n");
211aa374120SPatrick Venture         ofs.close();
212e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
213aa374120SPatrick Venture     }
214aa374120SPatrick Venture 
215aa374120SPatrick Venture     // Write succeeded, please continue.
216aa374120SPatrick Venture     ofs.flush();
217aa374120SPatrick Venture     ofs.close();
218aa374120SPatrick Venture 
219aa374120SPatrick Venture     auto bus = sdbusplus::bus::new_default();
220aa374120SPatrick Venture     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
221aa374120SPatrick Venture                                       SYSTEMD_INTERFACE, "StartUnit");
222aa374120SPatrick Venture 
223aa374120SPatrick Venture     method.append(PSU_HARDRESET_TARGET);
224aa374120SPatrick Venture     method.append("replace");
225aa374120SPatrick Venture 
226aa374120SPatrick Venture     try
227aa374120SPatrick Venture     {
228aa374120SPatrick Venture         bus.call_noreply(method);
229aa374120SPatrick Venture     }
230aa374120SPatrick Venture     catch (const sdbusplus::exception::SdBusError& ex)
231aa374120SPatrick Venture     {
232aa374120SPatrick Venture         log<level::ERR>("Failed to call PSU hard reset");
233e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
234aa374120SPatrick Venture     }
235aa374120SPatrick Venture }
236aa374120SPatrick Venture 
237ac4a16f7SShounak Mitra static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5";
238ac4a16f7SShounak Mitra 
239ac4a16f7SShounak Mitra void Handler::psuResetOnShutdown() const
240ac4a16f7SShounak Mitra {
241ac4a16f7SShounak Mitra     std::ofstream ofs;
242ac4a16f7SShounak Mitra     ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out);
243ac4a16f7SShounak Mitra     if (!ofs.good())
244ac4a16f7SShounak Mitra     {
245ac4a16f7SShounak Mitra         std::fprintf(stderr, "Unable to open file for output.\n");
246e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
247ac4a16f7SShounak Mitra     }
248ac4a16f7SShounak Mitra     ofs.close();
249ac4a16f7SShounak Mitra }
250ac4a16f7SShounak Mitra 
2513b1b427cSWilly Tu uint32_t Handler::getFlashSize()
2523b1b427cSWilly Tu {
2533b1b427cSWilly Tu     mtd_info_t info;
2543b1b427cSWilly Tu     int fd = open("/dev/mtd0", O_RDONLY);
2553b1b427cSWilly Tu     int err = ioctl(fd, MEMGETINFO, &info);
2563b1b427cSWilly Tu     close(fd);
2573b1b427cSWilly Tu 
2583b1b427cSWilly Tu     if (err)
2593b1b427cSWilly Tu     {
260e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
2613b1b427cSWilly Tu     }
2623b1b427cSWilly Tu     return info.size;
2633b1b427cSWilly Tu }
2643b1b427cSWilly Tu 
265ab650004SPatrick Venture std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
26607f85150SPatrick Venture {
267ab650004SPatrick Venture     // Check if we support this Entity ID.
268ab650004SPatrick Venture     auto it = _entityIdToName.find(id);
269ab650004SPatrick Venture     if (it == _entityIdToName.end())
27007f85150SPatrick Venture     {
271ab650004SPatrick Venture         log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
272e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidFieldRequest);
27307f85150SPatrick Venture     }
27407f85150SPatrick Venture 
275ab650004SPatrick Venture     std::string entityName;
276ab650004SPatrick Venture     try
27707f85150SPatrick Venture     {
278ab650004SPatrick Venture         // Parse the JSON config file.
279ab650004SPatrick Venture         if (!_entityConfigParsed)
280ab650004SPatrick Venture         {
281ab650004SPatrick Venture             _entityConfig = parseConfig(_configFile);
282ab650004SPatrick Venture             _entityConfigParsed = true;
28307f85150SPatrick Venture         }
28407f85150SPatrick Venture 
285ab650004SPatrick Venture         // Find the "entity id:entity instance" mapping to entity name.
286ab650004SPatrick Venture         entityName = readNameFromConfig(it->second, instance, _entityConfig);
287ab650004SPatrick Venture         if (entityName.empty())
288ab650004SPatrick Venture         {
289e5a06675SMichael Shen             throw IpmiException(::ipmi::ccInvalidFieldRequest);
290ab650004SPatrick Venture         }
291ab650004SPatrick Venture     }
292ab650004SPatrick Venture     catch (InternalFailure& e)
293ab650004SPatrick Venture     {
294e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
29507f85150SPatrick Venture     }
29607f85150SPatrick Venture 
297ab650004SPatrick Venture     return entityName;
298ab650004SPatrick Venture }
299ab650004SPatrick Venture 
30029f35bceSWilliam A. Kennington III std::string Handler::getMachineName()
30129f35bceSWilliam A. Kennington III {
30229f35bceSWilliam A. Kennington III     const char* path = "/etc/os-release";
30329f35bceSWilliam A. Kennington III     std::ifstream ifs(path);
30429f35bceSWilliam A. Kennington III     if (ifs.fail())
30529f35bceSWilliam A. Kennington III     {
30629f35bceSWilliam A. Kennington III         std::fprintf(stderr, "Failed to open: %s\n", path);
307e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
30829f35bceSWilliam A. Kennington III     }
30929f35bceSWilliam A. Kennington III 
31029f35bceSWilliam A. Kennington III     std::string line;
31129f35bceSWilliam A. Kennington III     while (true)
31229f35bceSWilliam A. Kennington III     {
31329f35bceSWilliam A. Kennington III         std::getline(ifs, line);
31429f35bceSWilliam A. Kennington III         if (ifs.eof())
31529f35bceSWilliam A. Kennington III         {
31629f35bceSWilliam A. Kennington III             std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n",
31729f35bceSWilliam A. Kennington III                          path);
318e5a06675SMichael Shen             throw IpmiException(::ipmi::ccInvalidCommand);
31929f35bceSWilliam A. Kennington III         }
32029f35bceSWilliam A. Kennington III         if (ifs.fail())
32129f35bceSWilliam A. Kennington III         {
32229f35bceSWilliam A. Kennington III             std::fprintf(stderr, "Failed to read: %s\n", path);
323e5a06675SMichael Shen             throw IpmiException(::ipmi::ccUnspecifiedError);
32429f35bceSWilliam A. Kennington III         }
32529f35bceSWilliam A. Kennington III         std::string_view lineView(line);
32629f35bceSWilliam A. Kennington III         constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
32729f35bceSWilliam A. Kennington III         if (lineView.substr(0, prefix.size()) != prefix)
32829f35bceSWilliam A. Kennington III         {
32929f35bceSWilliam A. Kennington III             continue;
33029f35bceSWilliam A. Kennington III         }
33129f35bceSWilliam A. Kennington III         lineView.remove_prefix(prefix.size());
33229f35bceSWilliam A. Kennington III         lineView.remove_prefix(
33329f35bceSWilliam A. Kennington III             std::min(lineView.find_first_not_of('"'), lineView.size()));
33429f35bceSWilliam A. Kennington III         lineView.remove_suffix(
33529f35bceSWilliam A. Kennington III             lineView.size() - 1 -
33629f35bceSWilliam A. Kennington III             std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
33729f35bceSWilliam A. Kennington III         return std::string(lineView);
33829f35bceSWilliam A. Kennington III     }
33929f35bceSWilliam A. Kennington III }
34029f35bceSWilliam A. Kennington III 
3418cfa4c44Slinyuny static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay";
3428cfa4c44Slinyuny static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target";
3438cfa4c44Slinyuny 
3448cfa4c44Slinyuny void Handler::hostPowerOffDelay(std::uint32_t delay) const
3458cfa4c44Slinyuny {
3468cfa4c44Slinyuny     // Set time delay
3478cfa4c44Slinyuny     std::ofstream ofs;
3488cfa4c44Slinyuny     ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out);
3498cfa4c44Slinyuny     if (!ofs.good())
3508cfa4c44Slinyuny     {
3518cfa4c44Slinyuny         std::fprintf(stderr, "Unable to open file for output.\n");
352e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
3538cfa4c44Slinyuny     }
3548cfa4c44Slinyuny 
3558cfa4c44Slinyuny     ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl;
3568cfa4c44Slinyuny     ofs.close();
3578cfa4c44Slinyuny     if (ofs.fail())
3588cfa4c44Slinyuny     {
3598cfa4c44Slinyuny         std::fprintf(stderr, "Write failed\n");
360e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
3618cfa4c44Slinyuny     }
3628cfa4c44Slinyuny 
3638cfa4c44Slinyuny     // Write succeeded, please continue.
3648cfa4c44Slinyuny     auto bus = sdbusplus::bus::new_default();
3658cfa4c44Slinyuny     auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
3668cfa4c44Slinyuny                                       SYSTEMD_INTERFACE, "StartUnit");
3678cfa4c44Slinyuny 
3688cfa4c44Slinyuny     method.append(HOST_POWEROFF_TARGET);
3698cfa4c44Slinyuny     method.append("replace");
3708cfa4c44Slinyuny 
3718cfa4c44Slinyuny     try
3728cfa4c44Slinyuny     {
3738cfa4c44Slinyuny         bus.call_noreply(method);
3748cfa4c44Slinyuny     }
3758cfa4c44Slinyuny     catch (const sdbusplus::exception::SdBusError& ex)
3768cfa4c44Slinyuny     {
3778cfa4c44Slinyuny         log<level::ERR>("Failed to call Power Off",
3788cfa4c44Slinyuny                         entry("WHAT=%s", ex.what()));
379e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
3808cfa4c44Slinyuny     }
3818cfa4c44Slinyuny }
3828cfa4c44Slinyuny 
383ab650004SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance,
384ab650004SPatrick Venture                                const Json& config)
38507f85150SPatrick Venture {
38607f85150SPatrick Venture     static const std::vector<Json> empty{};
38707f85150SPatrick Venture     std::vector<Json> readings = config.value(type, empty);
38807f85150SPatrick Venture     std::string name = "";
389ab650004SPatrick Venture 
39007f85150SPatrick Venture     for (const auto& j : readings)
39107f85150SPatrick Venture     {
39207f85150SPatrick Venture         uint8_t instanceNum = j.value("instance", 0);
39307f85150SPatrick Venture         // Not the instance we're interested in
39407f85150SPatrick Venture         if (instanceNum != instance)
39507f85150SPatrick Venture         {
39607f85150SPatrick Venture             continue;
39707f85150SPatrick Venture         }
39807f85150SPatrick Venture 
39907f85150SPatrick Venture         // Found the instance we're interested in
40007f85150SPatrick Venture         name = j.value("name", "");
40107f85150SPatrick Venture 
40207f85150SPatrick Venture         break;
40307f85150SPatrick Venture     }
404ab650004SPatrick Venture 
40507f85150SPatrick Venture     return name;
40607f85150SPatrick Venture }
40707f85150SPatrick Venture 
40849f23ad9SPatrick Venture void Handler::buildI2cPcieMapping()
40949f23ad9SPatrick Venture {
41049f23ad9SPatrick Venture     _pcie_i2c_map = buildPcieMap();
41149f23ad9SPatrick Venture }
41249f23ad9SPatrick Venture 
41349f23ad9SPatrick Venture size_t Handler::getI2cPcieMappingSize() const
41449f23ad9SPatrick Venture {
41549f23ad9SPatrick Venture     return _pcie_i2c_map.size();
41649f23ad9SPatrick Venture }
41749f23ad9SPatrick Venture 
41849f23ad9SPatrick Venture std::tuple<std::uint32_t, std::string>
41949f23ad9SPatrick Venture     Handler::getI2cEntry(unsigned int entry) const
42049f23ad9SPatrick Venture {
42149f23ad9SPatrick Venture     return _pcie_i2c_map[entry];
42249f23ad9SPatrick Venture }
42349f23ad9SPatrick Venture 
4244f0d1de6SSteve Foreman namespace
4254f0d1de6SSteve Foreman {
4264f0d1de6SSteve Foreman 
4274f0d1de6SSteve Foreman static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/";
4284f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel";
4294f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR";
4304f0d1de6SSteve Foreman 
4314f0d1de6SSteve Foreman // C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects()
4324f0d1de6SSteve Foreman using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>;
4334f0d1de6SSteve Foreman using AnyTypeList = std::vector<std::pair<std::string, AnyType>>;
4344f0d1de6SSteve Foreman using NamedArrayOfAnyTypeLists =
4354f0d1de6SSteve Foreman     std::vector<std::pair<std::string, AnyTypeList>>;
4364f0d1de6SSteve Foreman using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector<
4374f0d1de6SSteve Foreman     std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>;
4384f0d1de6SSteve Foreman 
4394f0d1de6SSteve Foreman } // namespace
4404f0d1de6SSteve Foreman 
44165371228SPatrick Williams sdbusplus::bus_t Handler::getDbus() const
4424f0d1de6SSteve Foreman {
4434f0d1de6SSteve Foreman     return sdbusplus::bus::new_default();
4444f0d1de6SSteve Foreman }
4454f0d1de6SSteve Foreman 
4464f0d1de6SSteve Foreman uint32_t Handler::accelOobDeviceCount() const
4474f0d1de6SSteve Foreman {
4484f0d1de6SSteve Foreman     ArrayOfObjectPathsAndTieredAnyTypeLists data;
4494f0d1de6SSteve Foreman 
4504f0d1de6SSteve Foreman     try
4514f0d1de6SSteve Foreman     {
4520e928ac6SMichael Shen         auto bus = getDbus();
4534f0d1de6SSteve Foreman         auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
4544f0d1de6SSteve Foreman                                           "org.freedesktop.DBus.ObjectManager",
4554f0d1de6SSteve Foreman                                           "GetManagedObjects");
4564f0d1de6SSteve Foreman         bus.call(method).read(data);
4574f0d1de6SSteve Foreman     }
4584f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
4594f0d1de6SSteve Foreman     {
4604f0d1de6SSteve Foreman         log<level::ERR>(
4614f0d1de6SSteve Foreman             "Failed to call GetManagedObjects on com.google.custom_accel",
4624f0d1de6SSteve Foreman             entry("WHAT=%s", ex.what()));
463e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
4644f0d1de6SSteve Foreman     }
4654f0d1de6SSteve Foreman 
4664f0d1de6SSteve Foreman     return data.size();
4674f0d1de6SSteve Foreman }
4684f0d1de6SSteve Foreman 
4694f0d1de6SSteve Foreman std::string Handler::accelOobDeviceName(size_t index) const
4704f0d1de6SSteve Foreman {
4714f0d1de6SSteve Foreman     ArrayOfObjectPathsAndTieredAnyTypeLists data;
4724f0d1de6SSteve Foreman 
4734f0d1de6SSteve Foreman     try
4744f0d1de6SSteve Foreman     {
4750e928ac6SMichael Shen         auto bus = getDbus();
4764f0d1de6SSteve Foreman         auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
4774f0d1de6SSteve Foreman                                           "org.freedesktop.DBus.ObjectManager",
4784f0d1de6SSteve Foreman                                           "GetManagedObjects");
4794f0d1de6SSteve Foreman         bus.call(method).read(data);
4804f0d1de6SSteve Foreman     }
4814f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
4824f0d1de6SSteve Foreman     {
4834f0d1de6SSteve Foreman         log<level::ERR>(
4844f0d1de6SSteve Foreman             "Failed to call GetManagedObjects on com.google.custom_accel",
4854f0d1de6SSteve Foreman             entry("WHAT=%s", ex.what()));
486e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
4874f0d1de6SSteve Foreman     }
4884f0d1de6SSteve Foreman 
4894f0d1de6SSteve Foreman     if (index >= data.size())
4904f0d1de6SSteve Foreman     {
4914f0d1de6SSteve Foreman         log<level::WARNING>(
4924f0d1de6SSteve Foreman             "Requested index is larger than the number of entries.",
4934f0d1de6SSteve Foreman             entry("INDEX=%zu", index), entry("NUM_NAMES=%zu", data.size()));
494e5a06675SMichael Shen         throw IpmiException(::ipmi::ccParmOutOfRange);
4954f0d1de6SSteve Foreman     }
4964f0d1de6SSteve Foreman 
4974f0d1de6SSteve Foreman     std::string_view name(data[index].first.str);
4984f0d1de6SSteve Foreman     if (!name.starts_with(ACCEL_OOB_ROOT))
4994f0d1de6SSteve Foreman     {
500e5a06675SMichael Shen         throw IpmiException(::ipmi::ccInvalidCommand);
5014f0d1de6SSteve Foreman     }
5024f0d1de6SSteve Foreman     name.remove_prefix(ACCEL_OOB_ROOT.length());
5034f0d1de6SSteve Foreman     return std::string(name);
5044f0d1de6SSteve Foreman }
5054f0d1de6SSteve Foreman 
5064f0d1de6SSteve Foreman uint64_t Handler::accelOobRead(std::string_view name, uint64_t address,
5074f0d1de6SSteve Foreman                                uint8_t num_bytes) const
5084f0d1de6SSteve Foreman {
5094f0d1de6SSteve Foreman     static constexpr char ACCEL_OOB_METHOD[] = "Read";
5104f0d1de6SSteve Foreman 
5114f0d1de6SSteve Foreman     std::string object_name(ACCEL_OOB_ROOT);
5124f0d1de6SSteve Foreman     object_name.append(name);
5134f0d1de6SSteve Foreman 
5140e928ac6SMichael Shen     auto bus = getDbus();
5154f0d1de6SSteve Foreman     auto method = bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
5164f0d1de6SSteve Foreman                                       ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD);
5174f0d1de6SSteve Foreman     method.append(address, static_cast<uint64_t>(num_bytes));
5184f0d1de6SSteve Foreman 
5194f0d1de6SSteve Foreman     std::vector<uint8_t> bytes;
5204f0d1de6SSteve Foreman 
5214f0d1de6SSteve Foreman     try
5224f0d1de6SSteve Foreman     {
5234f0d1de6SSteve Foreman         bus.call(method).read(bytes);
5244f0d1de6SSteve Foreman     }
5254f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
5264f0d1de6SSteve Foreman     {
5274f0d1de6SSteve Foreman         log<level::ERR>("Failed to call Read on com.google.custom_accel",
5284f0d1de6SSteve Foreman                         entry("WHAT=%s", ex.what()),
5294f0d1de6SSteve Foreman                         entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
5304f0d1de6SSteve Foreman                         entry("DBUS_OBJECT=%s", object_name.c_str()),
5314f0d1de6SSteve Foreman                         entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
5324f0d1de6SSteve Foreman                         entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
5334f0d1de6SSteve Foreman                         entry("DBUS_ARG_ADDRESS=%016llx", address),
5344f0d1de6SSteve Foreman                         entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes));
535e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
5364f0d1de6SSteve Foreman     }
5374f0d1de6SSteve Foreman 
5384f0d1de6SSteve Foreman     if (bytes.size() < num_bytes)
5394f0d1de6SSteve Foreman     {
5404f0d1de6SSteve Foreman         log<level::ERR>(
5414f0d1de6SSteve Foreman             "Call to Read on com.google.custom_accel didn't return the expected"
5424f0d1de6SSteve Foreman             " number of bytes.",
5434f0d1de6SSteve Foreman             entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
5444f0d1de6SSteve Foreman             entry("DBUS_OBJECT=%s", object_name.c_str()),
5454f0d1de6SSteve Foreman             entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
5464f0d1de6SSteve Foreman             entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
5474f0d1de6SSteve Foreman             entry("DBUS_ARG_ADDRESS=%016llx", address),
5484f0d1de6SSteve Foreman             entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
5494f0d1de6SSteve Foreman             entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
550e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
5514f0d1de6SSteve Foreman     }
5524f0d1de6SSteve Foreman 
5534f0d1de6SSteve Foreman     if (bytes.size() > sizeof(uint64_t))
5544f0d1de6SSteve Foreman     {
5554f0d1de6SSteve Foreman         log<level::ERR>(
5564f0d1de6SSteve Foreman             "Call to Read on com.google.custom_accel returned more than 8B.",
5574f0d1de6SSteve Foreman             entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
5584f0d1de6SSteve Foreman             entry("DBUS_OBJECT=%s", object_name.c_str()),
5594f0d1de6SSteve Foreman             entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
5604f0d1de6SSteve Foreman             entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
5614f0d1de6SSteve Foreman             entry("DBUS_ARG_ADDRESS=%016llx", address),
5624f0d1de6SSteve Foreman             entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
5634f0d1de6SSteve Foreman             entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
564e5a06675SMichael Shen         throw IpmiException(::ipmi::ccReqDataTruncated);
5654f0d1de6SSteve Foreman     }
5664f0d1de6SSteve Foreman 
5674f0d1de6SSteve Foreman     uint64_t data = 0;
5684f0d1de6SSteve Foreman     for (size_t i = 0; i < num_bytes; ++i)
5694f0d1de6SSteve Foreman     {
5704f0d1de6SSteve Foreman         data = (data << 8) | bytes[i];
5714f0d1de6SSteve Foreman     }
5724f0d1de6SSteve Foreman 
5734f0d1de6SSteve Foreman     return data;
5744f0d1de6SSteve Foreman }
5754f0d1de6SSteve Foreman 
5764f0d1de6SSteve Foreman void Handler::accelOobWrite(std::string_view name, uint64_t address,
5774f0d1de6SSteve Foreman                             uint8_t num_bytes, uint64_t data) const
5784f0d1de6SSteve Foreman {
5794f0d1de6SSteve Foreman     static constexpr std::string_view ACCEL_OOB_METHOD = "Write";
5804f0d1de6SSteve Foreman 
5814f0d1de6SSteve Foreman     std::string object_name(ACCEL_OOB_ROOT);
5824f0d1de6SSteve Foreman     object_name.append(name);
5834f0d1de6SSteve Foreman 
5844f0d1de6SSteve Foreman     if (num_bytes > sizeof(data))
5854f0d1de6SSteve Foreman     {
5864f0d1de6SSteve Foreman         log<level::ERR>(
5874f0d1de6SSteve Foreman             "Call to Write on com.google.custom_accel requested more than 8B.",
5884f0d1de6SSteve Foreman             entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
5894f0d1de6SSteve Foreman             entry("DBUS_OBJECT=%s", object_name.c_str()),
5904f0d1de6SSteve Foreman             entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
5914f0d1de6SSteve Foreman             entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
5924f0d1de6SSteve Foreman             entry("DBUS_ARG_ADDRESS=%016llx", address),
5934f0d1de6SSteve Foreman             entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
5944f0d1de6SSteve Foreman             entry("DBUS_ARG_DATA=%016llx", data));
595e5a06675SMichael Shen         throw IpmiException(::ipmi::ccParmOutOfRange);
5964f0d1de6SSteve Foreman     }
5974f0d1de6SSteve Foreman 
5984f0d1de6SSteve Foreman     std::vector<uint8_t> bytes;
5994f0d1de6SSteve Foreman     bytes.reserve(num_bytes);
6004f0d1de6SSteve Foreman     for (size_t i = 0; i < num_bytes; ++i)
6014f0d1de6SSteve Foreman     {
6024f0d1de6SSteve Foreman         bytes.emplace_back(data & 0xff);
6034f0d1de6SSteve Foreman         data >>= 8;
6044f0d1de6SSteve Foreman     }
6054f0d1de6SSteve Foreman 
6064f0d1de6SSteve Foreman     try
6074f0d1de6SSteve Foreman     {
6080e928ac6SMichael Shen         auto bus = getDbus();
6094f0d1de6SSteve Foreman         auto method =
6104f0d1de6SSteve Foreman             bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
6114f0d1de6SSteve Foreman                                 ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD.data());
6124f0d1de6SSteve Foreman         method.append(address, bytes);
6134f0d1de6SSteve Foreman         bus.call_noreply(method);
6144f0d1de6SSteve Foreman     }
6154f0d1de6SSteve Foreman     catch (const sdbusplus::exception::SdBusError& ex)
6164f0d1de6SSteve Foreman     {
6174f0d1de6SSteve Foreman         log<level::ERR>("Failed to call Write on com.google.custom_accel",
6184f0d1de6SSteve Foreman                         entry("WHAT=%s", ex.what()),
6194f0d1de6SSteve Foreman                         entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
6204f0d1de6SSteve Foreman                         entry("DBUS_OBJECT=%s", object_name.c_str()),
6214f0d1de6SSteve Foreman                         entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
6224f0d1de6SSteve Foreman                         entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
6234f0d1de6SSteve Foreman                         entry("DBUS_ARG_ADDRESS=%016llx", address),
6244f0d1de6SSteve Foreman                         entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
6254f0d1de6SSteve Foreman                         entry("DBUS_ARG_DATA=%016llx", data));
626e5a06675SMichael Shen         throw IpmiException(::ipmi::ccUnspecifiedError);
6274f0d1de6SSteve Foreman     }
6284f0d1de6SSteve Foreman }
6294f0d1de6SSteve Foreman 
6306c71b0f9SWilly Tu std::vector<uint8_t> Handler::pcieBifurcation(uint8_t index)
6316c71b0f9SWilly Tu {
6326c71b0f9SWilly Tu     return bifurcationHelper.get().getBifurcation(index).value_or(
6336c71b0f9SWilly Tu         std::vector<uint8_t>{});
6346c71b0f9SWilly Tu }
6356c71b0f9SWilly Tu 
636f085d91dSPatrick Venture } // namespace ipmi
637f085d91dSPatrick Venture } // namespace google
638