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 19*559cb011SBrandon Kim #include "bm_instance.hpp" 208ec4106bSNikhil Namjoshi #include "bmc_mode_enum.hpp" 21d2037c6aSPatrick Venture #include "errors.hpp" 22c87de558SPatrick Venture #include "handler_impl.hpp" 23ab650004SPatrick Venture #include "util.hpp" 24d2037c6aSPatrick Venture 253b1b427cSWilly Tu #include <fcntl.h> 26d2037c6aSPatrick Venture #include <ipmid/api.h> 273b1b427cSWilly Tu #include <mtd/mtd-abi.h> 283b1b427cSWilly Tu #include <mtd/mtd-user.h> 293b1b427cSWilly Tu #include <sys/ioctl.h> 303b1b427cSWilly Tu #include <unistd.h> 31d2037c6aSPatrick Venture 32444b5ea4SPatrick Williams #include <nlohmann/json.hpp> 33444b5ea4SPatrick Williams #include <phosphor-logging/elog-errors.hpp> 34444b5ea4SPatrick Williams #include <phosphor-logging/log.hpp> 35444b5ea4SPatrick Williams #include <sdbusplus/bus.hpp> 368d618532SMichael Shen #include <stdplus/print.hpp> 37444b5ea4SPatrick Williams #include <xyz/openbmc_project/Common/error.hpp> 38444b5ea4SPatrick Williams 39bb90d4fdSPatrick Venture #include <cinttypes> 40d2037c6aSPatrick Venture #include <cstdio> 41d2037c6aSPatrick Venture #include <filesystem> 42d2037c6aSPatrick Venture #include <fstream> 43*559cb011SBrandon Kim #include <iomanip> 4407f85150SPatrick Venture #include <map> 45d2037c6aSPatrick Venture #include <sstream> 46d2037c6aSPatrick Venture #include <string> 4729f35bceSWilliam A. Kennington III #include <string_view> 48d2037c6aSPatrick Venture #include <tuple> 494f0d1de6SSteve Foreman #include <variant> 505e70dc8cSNikhil Namjoshi 51f085d91dSPatrick Venture #ifndef NCSI_IF_NAME 52f085d91dSPatrick Venture #define NCSI_IF_NAME eth0 53f085d91dSPatrick Venture #endif 54f085d91dSPatrick Venture 55f085d91dSPatrick Venture // To deal with receiving a string without quotes. 56f085d91dSPatrick Venture #define QUOTE(name) #name 57f085d91dSPatrick Venture #define STR(macro) QUOTE(macro) 58f085d91dSPatrick Venture #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME) 59f085d91dSPatrick Venture 608d3d46a2SWilliam A. Kennington III namespace ipmi 618d3d46a2SWilliam A. Kennington III { 628d3d46a2SWilliam A. Kennington III std::uint8_t getChannelByName(const std::string& chName); 638d3d46a2SWilliam A. Kennington III } 648d3d46a2SWilliam A. Kennington III 65f085d91dSPatrick Venture namespace google 66f085d91dSPatrick Venture { 67f085d91dSPatrick Venture namespace ipmi 68f085d91dSPatrick Venture { 6907f85150SPatrick Venture using Json = nlohmann::json; 7007f85150SPatrick Venture using namespace phosphor::logging; 7107f85150SPatrick Venture using InternalFailure = 7207f85150SPatrick Venture sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; 73d455bfd6SGaurav Gandhi using Value = std::variant<double>; 74f085d91dSPatrick Venture 7515d4d21cSHao Zhou uint8_t isBmcInBareMetalMode(const std::unique_ptr<FileSystemInterface>& fs) 765e70dc8cSNikhil Namjoshi { 775e70dc8cSNikhil Namjoshi #if BARE_METAL 785e70dc8cSNikhil Namjoshi return static_cast<uint8_t>(BmcMode::BM_MODE); 795e70dc8cSNikhil Namjoshi #else 803f3ca035SBrandon Kim std::error_code ec; 814baf41c8SHao Zhou 8215d4d21cSHao Zhou if (fs->exists(bmDriveCleaningDoneAckFlagPath, ec)) 833f3ca035SBrandon Kim { 848d618532SMichael Shen stdplus::print( 854baf41c8SHao Zhou stderr, 868d618532SMichael Shen "{} exists so we acked cleaning done and must be in BM mode\n", 874baf41c8SHao Zhou bmDriveCleaningDoneAckFlagPath); 883f3ca035SBrandon Kim return static_cast<uint8_t>(BmcMode::BM_MODE); 893f3ca035SBrandon Kim } 903f3ca035SBrandon Kim 9115d4d21cSHao Zhou if (fs->exists(bmDriveCleaningDoneFlagPath, ec)) 924baf41c8SHao Zhou { 9315d4d21cSHao Zhou fs->rename(bmDriveCleaningDoneFlagPath, bmDriveCleaningDoneAckFlagPath, 944baf41c8SHao Zhou ec); 958d618532SMichael Shen stdplus::print( 964baf41c8SHao Zhou stderr, 978d618532SMichael Shen "{} exists so we just finished cleaning and must be in BM mode\n", 984baf41c8SHao Zhou bmDriveCleaningDoneFlagPath); 994baf41c8SHao Zhou return static_cast<uint8_t>(BmcMode::BM_MODE); 1004baf41c8SHao Zhou } 1014baf41c8SHao Zhou 10215d4d21cSHao Zhou if (fs->exists(BM_SIGNAL_PATH, ec)) 1034baf41c8SHao Zhou { 10415d4d21cSHao Zhou if (!fs->exists(bmDriveCleaningFlagPath, ec)) 1054baf41c8SHao Zhou { 10615d4d21cSHao Zhou fs->create(bmDriveCleaningFlagPath); 1074baf41c8SHao Zhou } 1084baf41c8SHao Zhou 1098d618532SMichael Shen stdplus::print( 1104baf41c8SHao Zhou stderr, 1118d618532SMichael Shen "{} exists and no done/ack flag, we must be in BM cleaning mode\n", 1123f3ca035SBrandon Kim BM_SIGNAL_PATH); 1134baf41c8SHao Zhou return static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE); 1144baf41c8SHao Zhou } 1154baf41c8SHao Zhou 1168d618532SMichael Shen stdplus::print( 1174baf41c8SHao Zhou stderr, 1184baf41c8SHao Zhou "Unable to find any BM state files so we must not be in BM mode\n"); 1195e70dc8cSNikhil Namjoshi return static_cast<uint8_t>(BmcMode::NON_BM_MODE); 1205e70dc8cSNikhil Namjoshi #endif 1215e70dc8cSNikhil Namjoshi } 1225e70dc8cSNikhil Namjoshi 1235e70dc8cSNikhil Namjoshi uint8_t Handler::getBmcMode() 1245e70dc8cSNikhil Namjoshi { 1255e70dc8cSNikhil Namjoshi // BM_CLEANING_MODE is not implemented yet 12615d4d21cSHao Zhou return isBmcInBareMetalMode(this->getFs()); 1275e70dc8cSNikhil Namjoshi } 1285e70dc8cSNikhil Namjoshi 129b69209b4SWilliam A. Kennington III std::tuple<std::uint8_t, std::string> 130b69209b4SWilliam A. Kennington III Handler::getEthDetails(std::string intf) const 131f085d91dSPatrick Venture { 132b69209b4SWilliam A. Kennington III if (intf.empty()) 133b69209b4SWilliam A. Kennington III { 134b69209b4SWilliam A. Kennington III intf = NCSI_IF_NAME_STR; 135b69209b4SWilliam A. Kennington III } 136b69209b4SWilliam A. Kennington III return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf)); 137f085d91dSPatrick Venture } 138f085d91dSPatrick Venture 139d2037c6aSPatrick Venture std::int64_t Handler::getRxPackets(const std::string& name) const 140d2037c6aSPatrick Venture { 141d2037c6aSPatrick Venture std::ostringstream opath; 142d2037c6aSPatrick Venture opath << "/sys/class/net/" << name << "/statistics/rx_packets"; 143d2037c6aSPatrick Venture std::string path = opath.str(); 144d2037c6aSPatrick Venture 145d2037c6aSPatrick Venture // Minor sanity & security check (of course, I'm less certain if unicode 146d2037c6aSPatrick Venture // comes into play here. 147d2037c6aSPatrick Venture // 148d2037c6aSPatrick Venture // Basically you can't easily inject ../ or /../ into the path below. 149d2037c6aSPatrick Venture if (name.find("/") != std::string::npos) 150d2037c6aSPatrick Venture { 1518d618532SMichael Shen stdplus::print(stderr, "Invalid or illegal name: '{}'\n", name); 152e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 153d2037c6aSPatrick Venture } 154d2037c6aSPatrick Venture 155d2037c6aSPatrick Venture std::error_code ec; 15615d4d21cSHao Zhou if (!this->getFs()->exists(path, ec)) 157d2037c6aSPatrick Venture { 1588d618532SMichael Shen stdplus::print(stderr, "Path: '{}' doesn't exist.\n", path); 159e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 160d2037c6aSPatrick Venture } 161d2037c6aSPatrick Venture // We're uninterested in the state of ec. 162d2037c6aSPatrick Venture 163d2037c6aSPatrick Venture int64_t count = 0; 164d2037c6aSPatrick Venture std::ifstream ifs; 165d2037c6aSPatrick Venture ifs.exceptions(std::ifstream::failbit); 166d2037c6aSPatrick Venture try 167d2037c6aSPatrick Venture { 168d2037c6aSPatrick Venture ifs.open(path); 169d2037c6aSPatrick Venture ifs >> count; 170d2037c6aSPatrick Venture } 171d2037c6aSPatrick Venture catch (std::ios_base::failure& fail) 172d2037c6aSPatrick Venture { 173e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 174d2037c6aSPatrick Venture } 175d2037c6aSPatrick Venture 176d2037c6aSPatrick Venture return count; 177d2037c6aSPatrick Venture } 178d2037c6aSPatrick Venture 179bb90d4fdSPatrick Venture VersionTuple Handler::getCpldVersion(unsigned int id) const 180bb90d4fdSPatrick Venture { 181bb90d4fdSPatrick Venture std::ostringstream opath; 182bb90d4fdSPatrick Venture opath << "/run/cpld" << id << ".version"; 183bb90d4fdSPatrick Venture // Check for file 184bb90d4fdSPatrick Venture 185bb90d4fdSPatrick Venture std::error_code ec; 18615d4d21cSHao Zhou if (!this->getFs()->exists(opath.str(), ec)) 187bb90d4fdSPatrick Venture { 1888d618532SMichael Shen stdplus::print(stderr, "Path: '{}' doesn't exist.\n", opath.str()); 189e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 190bb90d4fdSPatrick Venture } 191bb90d4fdSPatrick Venture // We're uninterested in the state of ec. 192bb90d4fdSPatrick Venture 193bb90d4fdSPatrick Venture // If file exists, read. 194bb90d4fdSPatrick Venture std::ifstream ifs; 195bb90d4fdSPatrick Venture ifs.exceptions(std::ifstream::failbit); 196bb90d4fdSPatrick Venture std::string value; 197bb90d4fdSPatrick Venture try 198bb90d4fdSPatrick Venture { 199bb90d4fdSPatrick Venture ifs.open(opath.str()); 200bb90d4fdSPatrick Venture ifs >> value; 201bb90d4fdSPatrick Venture } 202bb90d4fdSPatrick Venture catch (std::ios_base::failure& fail) 203bb90d4fdSPatrick Venture { 204e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 205bb90d4fdSPatrick Venture } 206bb90d4fdSPatrick Venture 207bb90d4fdSPatrick Venture // If value parses as expected, return version. 208bb90d4fdSPatrick Venture VersionTuple version = std::make_tuple(0, 0, 0, 0); 209bb90d4fdSPatrick Venture 210444b5ea4SPatrick Williams int num_fields = std::sscanf(value.c_str(), 211444b5ea4SPatrick Williams "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8, 212bb90d4fdSPatrick Venture &std::get<0>(version), &std::get<1>(version), 213bb90d4fdSPatrick Venture &std::get<2>(version), &std::get<3>(version)); 214bb90d4fdSPatrick Venture if (num_fields == 0) 215bb90d4fdSPatrick Venture { 2168d618532SMichael Shen stdplus::print(stderr, "Invalid version.\n"); 217e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 218bb90d4fdSPatrick Venture } 219bb90d4fdSPatrick Venture 220bb90d4fdSPatrick Venture return version; 221bb90d4fdSPatrick Venture } 222bb90d4fdSPatrick Venture 223aa374120SPatrick Venture static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay"; 224aa374120SPatrick Venture static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; 225aa374120SPatrick Venture static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; 226aa374120SPatrick Venture static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; 227aa374120SPatrick Venture static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target"; 228aa374120SPatrick Venture 229aa374120SPatrick Venture void Handler::psuResetDelay(std::uint32_t delay) const 230aa374120SPatrick Venture { 231aa374120SPatrick Venture std::ofstream ofs; 232aa374120SPatrick Venture ofs.open(TIME_DELAY_FILENAME, std::ofstream::out); 233aa374120SPatrick Venture if (!ofs.good()) 234aa374120SPatrick Venture { 2358d618532SMichael Shen stdplus::print(stderr, "Unable to open file for output.\n"); 236e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 237aa374120SPatrick Venture } 238aa374120SPatrick Venture 239aa374120SPatrick Venture ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl; 240aa374120SPatrick Venture if (ofs.fail()) 241aa374120SPatrick Venture { 2428d618532SMichael Shen stdplus::print(stderr, "Write failed\n"); 243aa374120SPatrick Venture ofs.close(); 244e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 245aa374120SPatrick Venture } 246aa374120SPatrick Venture 247aa374120SPatrick Venture // Write succeeded, please continue. 248aa374120SPatrick Venture ofs.flush(); 249aa374120SPatrick Venture ofs.close(); 250aa374120SPatrick Venture 251aa374120SPatrick Venture auto bus = sdbusplus::bus::new_default(); 252aa374120SPatrick Venture auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 253aa374120SPatrick Venture SYSTEMD_INTERFACE, "StartUnit"); 254aa374120SPatrick Venture 255aa374120SPatrick Venture method.append(PSU_HARDRESET_TARGET); 256aa374120SPatrick Venture method.append("replace"); 257aa374120SPatrick Venture 258aa374120SPatrick Venture try 259aa374120SPatrick Venture { 260aa374120SPatrick Venture bus.call_noreply(method); 261aa374120SPatrick Venture } 262aa374120SPatrick Venture catch (const sdbusplus::exception::SdBusError& ex) 263aa374120SPatrick Venture { 264aa374120SPatrick Venture log<level::ERR>("Failed to call PSU hard reset"); 265e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 266aa374120SPatrick Venture } 267aa374120SPatrick Venture } 268aa374120SPatrick Venture 269ac4a16f7SShounak Mitra static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5"; 270ac4a16f7SShounak Mitra 271ac4a16f7SShounak Mitra void Handler::psuResetOnShutdown() const 272ac4a16f7SShounak Mitra { 273ac4a16f7SShounak Mitra std::ofstream ofs; 274ac4a16f7SShounak Mitra ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out); 275ac4a16f7SShounak Mitra if (!ofs.good()) 276ac4a16f7SShounak Mitra { 2778d618532SMichael Shen stdplus::print(stderr, "Unable to open file for output.\n"); 278e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 279ac4a16f7SShounak Mitra } 280ac4a16f7SShounak Mitra ofs.close(); 281ac4a16f7SShounak Mitra } 282ac4a16f7SShounak Mitra 2833b1b427cSWilly Tu uint32_t Handler::getFlashSize() 2843b1b427cSWilly Tu { 2853b1b427cSWilly Tu mtd_info_t info; 2863b1b427cSWilly Tu int fd = open("/dev/mtd0", O_RDONLY); 2873b1b427cSWilly Tu int err = ioctl(fd, MEMGETINFO, &info); 2883b1b427cSWilly Tu close(fd); 2893b1b427cSWilly Tu 2903b1b427cSWilly Tu if (err) 2913b1b427cSWilly Tu { 292e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 2933b1b427cSWilly Tu } 2943b1b427cSWilly Tu return info.size; 2953b1b427cSWilly Tu } 2963b1b427cSWilly Tu 297ab650004SPatrick Venture std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance) 29807f85150SPatrick Venture { 299ab650004SPatrick Venture // Check if we support this Entity ID. 300ab650004SPatrick Venture auto it = _entityIdToName.find(id); 301ab650004SPatrick Venture if (it == _entityIdToName.end()) 30207f85150SPatrick Venture { 303ab650004SPatrick Venture log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id)); 304e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 30507f85150SPatrick Venture } 30607f85150SPatrick Venture 307ab650004SPatrick Venture std::string entityName; 308ab650004SPatrick Venture try 30907f85150SPatrick Venture { 310ab650004SPatrick Venture // Parse the JSON config file. 311ab650004SPatrick Venture if (!_entityConfigParsed) 312ab650004SPatrick Venture { 313ab650004SPatrick Venture _entityConfig = parseConfig(_configFile); 314ab650004SPatrick Venture _entityConfigParsed = true; 31507f85150SPatrick Venture } 31607f85150SPatrick Venture 317ab650004SPatrick Venture // Find the "entity id:entity instance" mapping to entity name. 318ab650004SPatrick Venture entityName = readNameFromConfig(it->second, instance, _entityConfig); 319ab650004SPatrick Venture if (entityName.empty()) 320ab650004SPatrick Venture { 321e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 322ab650004SPatrick Venture } 323ab650004SPatrick Venture } 324ab650004SPatrick Venture catch (InternalFailure& e) 325ab650004SPatrick Venture { 326e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 32707f85150SPatrick Venture } 32807f85150SPatrick Venture 329ab650004SPatrick Venture return entityName; 330ab650004SPatrick Venture } 331ab650004SPatrick Venture 33229f35bceSWilliam A. Kennington III std::string Handler::getMachineName() 33329f35bceSWilliam A. Kennington III { 33429f35bceSWilliam A. Kennington III const char* path = "/etc/os-release"; 33529f35bceSWilliam A. Kennington III std::ifstream ifs(path); 33629f35bceSWilliam A. Kennington III if (ifs.fail()) 33729f35bceSWilliam A. Kennington III { 3388d618532SMichael Shen stdplus::print(stderr, "Failed to open: {}\n", path); 339e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 34029f35bceSWilliam A. Kennington III } 34129f35bceSWilliam A. Kennington III 34229f35bceSWilliam A. Kennington III std::string line; 34329f35bceSWilliam A. Kennington III while (true) 34429f35bceSWilliam A. Kennington III { 34529f35bceSWilliam A. Kennington III std::getline(ifs, line); 34629f35bceSWilliam A. Kennington III if (ifs.eof()) 34729f35bceSWilliam A. Kennington III { 3488d618532SMichael Shen stdplus::print(stderr, 3498d618532SMichael Shen "Failed to find OPENBMC_TARGET_MACHINE: {}\n", path); 350e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidCommand); 35129f35bceSWilliam A. Kennington III } 35229f35bceSWilliam A. Kennington III if (ifs.fail()) 35329f35bceSWilliam A. Kennington III { 3548d618532SMichael Shen stdplus::print(stderr, "Failed to read: {}\n", path); 355e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 35629f35bceSWilliam A. Kennington III } 35729f35bceSWilliam A. Kennington III std::string_view lineView(line); 35829f35bceSWilliam A. Kennington III constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE="; 35929f35bceSWilliam A. Kennington III if (lineView.substr(0, prefix.size()) != prefix) 36029f35bceSWilliam A. Kennington III { 36129f35bceSWilliam A. Kennington III continue; 36229f35bceSWilliam A. Kennington III } 36329f35bceSWilliam A. Kennington III lineView.remove_prefix(prefix.size()); 36429f35bceSWilliam A. Kennington III lineView.remove_prefix( 36529f35bceSWilliam A. Kennington III std::min(lineView.find_first_not_of('"'), lineView.size())); 36629f35bceSWilliam A. Kennington III lineView.remove_suffix( 36729f35bceSWilliam A. Kennington III lineView.size() - 1 - 36829f35bceSWilliam A. Kennington III std::min(lineView.find_last_not_of('"'), lineView.size() - 1)); 36929f35bceSWilliam A. Kennington III return std::string(lineView); 37029f35bceSWilliam A. Kennington III } 37129f35bceSWilliam A. Kennington III } 37229f35bceSWilliam A. Kennington III 3738cfa4c44Slinyuny static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay"; 3748cfa4c44Slinyuny static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target"; 3758cfa4c44Slinyuny 3768cfa4c44Slinyuny void Handler::hostPowerOffDelay(std::uint32_t delay) const 3778cfa4c44Slinyuny { 3788cfa4c44Slinyuny // Set time delay 3798cfa4c44Slinyuny std::ofstream ofs; 3808cfa4c44Slinyuny ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out); 3818cfa4c44Slinyuny if (!ofs.good()) 3828cfa4c44Slinyuny { 3838d618532SMichael Shen stdplus::print(stderr, "Unable to open file for output.\n"); 384e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 3858cfa4c44Slinyuny } 3868cfa4c44Slinyuny 3878cfa4c44Slinyuny ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl; 3888cfa4c44Slinyuny ofs.close(); 3898cfa4c44Slinyuny if (ofs.fail()) 3908cfa4c44Slinyuny { 3918d618532SMichael Shen stdplus::print(stderr, "Write failed\n"); 392e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 3938cfa4c44Slinyuny } 3948cfa4c44Slinyuny 3958cfa4c44Slinyuny // Write succeeded, please continue. 3968cfa4c44Slinyuny auto bus = sdbusplus::bus::new_default(); 3978cfa4c44Slinyuny auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 3988cfa4c44Slinyuny SYSTEMD_INTERFACE, "StartUnit"); 3998cfa4c44Slinyuny 4008cfa4c44Slinyuny method.append(HOST_POWEROFF_TARGET); 4018cfa4c44Slinyuny method.append("replace"); 4028cfa4c44Slinyuny 4038cfa4c44Slinyuny try 4048cfa4c44Slinyuny { 4058cfa4c44Slinyuny bus.call_noreply(method); 4068cfa4c44Slinyuny } 4078cfa4c44Slinyuny catch (const sdbusplus::exception::SdBusError& ex) 4088cfa4c44Slinyuny { 4098cfa4c44Slinyuny log<level::ERR>("Failed to call Power Off", 4108cfa4c44Slinyuny entry("WHAT=%s", ex.what())); 411e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 4128cfa4c44Slinyuny } 4138cfa4c44Slinyuny } 4148cfa4c44Slinyuny 415ab650004SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance, 416ab650004SPatrick Venture const Json& config) 41707f85150SPatrick Venture { 41807f85150SPatrick Venture static const std::vector<Json> empty{}; 41907f85150SPatrick Venture std::vector<Json> readings = config.value(type, empty); 42007f85150SPatrick Venture std::string name = ""; 421ab650004SPatrick Venture 42207f85150SPatrick Venture for (const auto& j : readings) 42307f85150SPatrick Venture { 42407f85150SPatrick Venture uint8_t instanceNum = j.value("instance", 0); 42507f85150SPatrick Venture // Not the instance we're interested in 42607f85150SPatrick Venture if (instanceNum != instance) 42707f85150SPatrick Venture { 42807f85150SPatrick Venture continue; 42907f85150SPatrick Venture } 43007f85150SPatrick Venture 43107f85150SPatrick Venture // Found the instance we're interested in 43207f85150SPatrick Venture name = j.value("name", ""); 43307f85150SPatrick Venture 43407f85150SPatrick Venture break; 43507f85150SPatrick Venture } 436ab650004SPatrick Venture 43707f85150SPatrick Venture return name; 43807f85150SPatrick Venture } 43907f85150SPatrick Venture 44049f23ad9SPatrick Venture void Handler::buildI2cPcieMapping() 44149f23ad9SPatrick Venture { 44249f23ad9SPatrick Venture _pcie_i2c_map = buildPcieMap(); 44349f23ad9SPatrick Venture } 44449f23ad9SPatrick Venture 44549f23ad9SPatrick Venture size_t Handler::getI2cPcieMappingSize() const 44649f23ad9SPatrick Venture { 44749f23ad9SPatrick Venture return _pcie_i2c_map.size(); 44849f23ad9SPatrick Venture } 44949f23ad9SPatrick Venture 45049f23ad9SPatrick Venture std::tuple<std::uint32_t, std::string> 45149f23ad9SPatrick Venture Handler::getI2cEntry(unsigned int entry) const 45249f23ad9SPatrick Venture { 45349f23ad9SPatrick Venture return _pcie_i2c_map[entry]; 45449f23ad9SPatrick Venture } 45549f23ad9SPatrick Venture 4564f0d1de6SSteve Foreman namespace 4574f0d1de6SSteve Foreman { 4584f0d1de6SSteve Foreman 4594f0d1de6SSteve Foreman static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/"; 4604f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel"; 4614f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR"; 4624f0d1de6SSteve Foreman 4634f0d1de6SSteve Foreman // C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects() 4644f0d1de6SSteve Foreman using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>; 4654f0d1de6SSteve Foreman using AnyTypeList = std::vector<std::pair<std::string, AnyType>>; 4664f0d1de6SSteve Foreman using NamedArrayOfAnyTypeLists = 4674f0d1de6SSteve Foreman std::vector<std::pair<std::string, AnyTypeList>>; 4684f0d1de6SSteve Foreman using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector< 4694f0d1de6SSteve Foreman std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>; 4704f0d1de6SSteve Foreman 4714f0d1de6SSteve Foreman } // namespace 4724f0d1de6SSteve Foreman 47365371228SPatrick Williams sdbusplus::bus_t Handler::getDbus() const 4744f0d1de6SSteve Foreman { 4754f0d1de6SSteve Foreman return sdbusplus::bus::new_default(); 4764f0d1de6SSteve Foreman } 4774f0d1de6SSteve Foreman 47815d4d21cSHao Zhou const std::unique_ptr<FileSystemInterface>& Handler::getFs() const 47915d4d21cSHao Zhou { 48015d4d21cSHao Zhou return this->fsPtr; 48115d4d21cSHao Zhou } 48215d4d21cSHao Zhou 4834f0d1de6SSteve Foreman uint32_t Handler::accelOobDeviceCount() const 4844f0d1de6SSteve Foreman { 4854f0d1de6SSteve Foreman ArrayOfObjectPathsAndTieredAnyTypeLists data; 4864f0d1de6SSteve Foreman 4874f0d1de6SSteve Foreman try 4884f0d1de6SSteve Foreman { 4890e928ac6SMichael Shen auto bus = getDbus(); 4904f0d1de6SSteve Foreman auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/", 4914f0d1de6SSteve Foreman "org.freedesktop.DBus.ObjectManager", 4924f0d1de6SSteve Foreman "GetManagedObjects"); 4934f0d1de6SSteve Foreman bus.call(method).read(data); 4944f0d1de6SSteve Foreman } 4954f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 4964f0d1de6SSteve Foreman { 4974f0d1de6SSteve Foreman log<level::ERR>( 4984f0d1de6SSteve Foreman "Failed to call GetManagedObjects on com.google.custom_accel", 4994f0d1de6SSteve Foreman entry("WHAT=%s", ex.what())); 500e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 5014f0d1de6SSteve Foreman } 5024f0d1de6SSteve Foreman 5034f0d1de6SSteve Foreman return data.size(); 5044f0d1de6SSteve Foreman } 5054f0d1de6SSteve Foreman 5064f0d1de6SSteve Foreman std::string Handler::accelOobDeviceName(size_t index) const 5074f0d1de6SSteve Foreman { 5084f0d1de6SSteve Foreman ArrayOfObjectPathsAndTieredAnyTypeLists data; 5094f0d1de6SSteve Foreman 5104f0d1de6SSteve Foreman try 5114f0d1de6SSteve Foreman { 5120e928ac6SMichael Shen auto bus = getDbus(); 5134f0d1de6SSteve Foreman auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/", 5144f0d1de6SSteve Foreman "org.freedesktop.DBus.ObjectManager", 5154f0d1de6SSteve Foreman "GetManagedObjects"); 5164f0d1de6SSteve Foreman bus.call(method).read(data); 5174f0d1de6SSteve Foreman } 5184f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 5194f0d1de6SSteve Foreman { 5204f0d1de6SSteve Foreman log<level::ERR>( 5214f0d1de6SSteve Foreman "Failed to call GetManagedObjects on com.google.custom_accel", 5224f0d1de6SSteve Foreman entry("WHAT=%s", ex.what())); 523e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 5244f0d1de6SSteve Foreman } 5254f0d1de6SSteve Foreman 5264f0d1de6SSteve Foreman if (index >= data.size()) 5274f0d1de6SSteve Foreman { 5284f0d1de6SSteve Foreman log<level::WARNING>( 5294f0d1de6SSteve Foreman "Requested index is larger than the number of entries.", 5304f0d1de6SSteve Foreman entry("INDEX=%zu", index), entry("NUM_NAMES=%zu", data.size())); 531e5a06675SMichael Shen throw IpmiException(::ipmi::ccParmOutOfRange); 5324f0d1de6SSteve Foreman } 5334f0d1de6SSteve Foreman 5344f0d1de6SSteve Foreman std::string_view name(data[index].first.str); 5354f0d1de6SSteve Foreman if (!name.starts_with(ACCEL_OOB_ROOT)) 5364f0d1de6SSteve Foreman { 537e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidCommand); 5384f0d1de6SSteve Foreman } 5394f0d1de6SSteve Foreman name.remove_prefix(ACCEL_OOB_ROOT.length()); 5404f0d1de6SSteve Foreman return std::string(name); 5414f0d1de6SSteve Foreman } 5424f0d1de6SSteve Foreman 5434f0d1de6SSteve Foreman uint64_t Handler::accelOobRead(std::string_view name, uint64_t address, 5444f0d1de6SSteve Foreman uint8_t num_bytes) const 5454f0d1de6SSteve Foreman { 5464f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_METHOD[] = "Read"; 5474f0d1de6SSteve Foreman 5484f0d1de6SSteve Foreman std::string object_name(ACCEL_OOB_ROOT); 5494f0d1de6SSteve Foreman object_name.append(name); 5504f0d1de6SSteve Foreman 5510e928ac6SMichael Shen auto bus = getDbus(); 5524f0d1de6SSteve Foreman auto method = bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(), 5534f0d1de6SSteve Foreman ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD); 5544f0d1de6SSteve Foreman method.append(address, static_cast<uint64_t>(num_bytes)); 5554f0d1de6SSteve Foreman 5564f0d1de6SSteve Foreman std::vector<uint8_t> bytes; 5574f0d1de6SSteve Foreman 5584f0d1de6SSteve Foreman try 5594f0d1de6SSteve Foreman { 5604f0d1de6SSteve Foreman bus.call(method).read(bytes); 5614f0d1de6SSteve Foreman } 5624f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 5634f0d1de6SSteve Foreman { 5644f0d1de6SSteve Foreman log<level::ERR>("Failed to call Read on com.google.custom_accel", 5654f0d1de6SSteve Foreman entry("WHAT=%s", ex.what()), 5664f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 5674f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 5684f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 5694f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), 5704f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 5714f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes)); 572e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 5734f0d1de6SSteve Foreman } 5744f0d1de6SSteve Foreman 5754f0d1de6SSteve Foreman if (bytes.size() < num_bytes) 5764f0d1de6SSteve Foreman { 5774f0d1de6SSteve Foreman log<level::ERR>( 5784f0d1de6SSteve Foreman "Call to Read on com.google.custom_accel didn't return the expected" 5794f0d1de6SSteve Foreman " number of bytes.", 5804f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 5814f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 5824f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 5834f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), 5844f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 5854f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 5864f0d1de6SSteve Foreman entry("DBUS_RETURN_SIZE=%zu", bytes.size())); 587e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 5884f0d1de6SSteve Foreman } 5894f0d1de6SSteve Foreman 5904f0d1de6SSteve Foreman if (bytes.size() > sizeof(uint64_t)) 5914f0d1de6SSteve Foreman { 5924f0d1de6SSteve Foreman log<level::ERR>( 5934f0d1de6SSteve Foreman "Call to Read on com.google.custom_accel returned more than 8B.", 5944f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 5954f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 5964f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 5974f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), 5984f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 5994f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 6004f0d1de6SSteve Foreman entry("DBUS_RETURN_SIZE=%zu", bytes.size())); 601e5a06675SMichael Shen throw IpmiException(::ipmi::ccReqDataTruncated); 6024f0d1de6SSteve Foreman } 6034f0d1de6SSteve Foreman 6044f0d1de6SSteve Foreman uint64_t data = 0; 6054f0d1de6SSteve Foreman for (size_t i = 0; i < num_bytes; ++i) 6064f0d1de6SSteve Foreman { 6074f0d1de6SSteve Foreman data = (data << 8) | bytes[i]; 6084f0d1de6SSteve Foreman } 6094f0d1de6SSteve Foreman 6104f0d1de6SSteve Foreman return data; 6114f0d1de6SSteve Foreman } 6124f0d1de6SSteve Foreman 6134f0d1de6SSteve Foreman void Handler::accelOobWrite(std::string_view name, uint64_t address, 6144f0d1de6SSteve Foreman uint8_t num_bytes, uint64_t data) const 6154f0d1de6SSteve Foreman { 6164f0d1de6SSteve Foreman static constexpr std::string_view ACCEL_OOB_METHOD = "Write"; 6174f0d1de6SSteve Foreman 6184f0d1de6SSteve Foreman std::string object_name(ACCEL_OOB_ROOT); 6194f0d1de6SSteve Foreman object_name.append(name); 6204f0d1de6SSteve Foreman 6214f0d1de6SSteve Foreman if (num_bytes > sizeof(data)) 6224f0d1de6SSteve Foreman { 6234f0d1de6SSteve Foreman log<level::ERR>( 6244f0d1de6SSteve Foreman "Call to Write on com.google.custom_accel requested more than 8B.", 6254f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 6264f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 6274f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 6284f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()), 6294f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 6304f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 6314f0d1de6SSteve Foreman entry("DBUS_ARG_DATA=%016llx", data)); 632e5a06675SMichael Shen throw IpmiException(::ipmi::ccParmOutOfRange); 6334f0d1de6SSteve Foreman } 6344f0d1de6SSteve Foreman 6354f0d1de6SSteve Foreman std::vector<uint8_t> bytes; 6364f0d1de6SSteve Foreman bytes.reserve(num_bytes); 6374f0d1de6SSteve Foreman for (size_t i = 0; i < num_bytes; ++i) 6384f0d1de6SSteve Foreman { 6394f0d1de6SSteve Foreman bytes.emplace_back(data & 0xff); 6404f0d1de6SSteve Foreman data >>= 8; 6414f0d1de6SSteve Foreman } 6424f0d1de6SSteve Foreman 6434f0d1de6SSteve Foreman try 6444f0d1de6SSteve Foreman { 6450e928ac6SMichael Shen auto bus = getDbus(); 6464f0d1de6SSteve Foreman auto method = 6474f0d1de6SSteve Foreman bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(), 6484f0d1de6SSteve Foreman ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD.data()); 6494f0d1de6SSteve Foreman method.append(address, bytes); 6504f0d1de6SSteve Foreman bus.call_noreply(method); 6514f0d1de6SSteve Foreman } 6524f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 6534f0d1de6SSteve Foreman { 6544f0d1de6SSteve Foreman log<level::ERR>("Failed to call Write on com.google.custom_accel", 6554f0d1de6SSteve Foreman entry("WHAT=%s", ex.what()), 6564f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 6574f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 6584f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 6594f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()), 6604f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 6614f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 6624f0d1de6SSteve Foreman entry("DBUS_ARG_DATA=%016llx", data)); 663e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 6644f0d1de6SSteve Foreman } 6654f0d1de6SSteve Foreman } 6664f0d1de6SSteve Foreman 6676c71b0f9SWilly Tu std::vector<uint8_t> Handler::pcieBifurcation(uint8_t index) 6686c71b0f9SWilly Tu { 6696c71b0f9SWilly Tu return bifurcationHelper.get().getBifurcation(index).value_or( 6706c71b0f9SWilly Tu std::vector<uint8_t>{}); 6716c71b0f9SWilly Tu } 6726c71b0f9SWilly Tu 673a92d0e6bSJohn Wedig static constexpr auto BARE_METAL_TARGET = "gbmc-bare-metal-active.target"; 674a92d0e6bSJohn Wedig 675a92d0e6bSJohn Wedig void Handler::linuxBootDone() const 676a92d0e6bSJohn Wedig { 67715d4d21cSHao Zhou if (isBmcInBareMetalMode(this->fsPtr) != 67815d4d21cSHao Zhou static_cast<uint8_t>(BmcMode::BM_MODE)) 679a92d0e6bSJohn Wedig { 680a92d0e6bSJohn Wedig return; 681a92d0e6bSJohn Wedig } 682a92d0e6bSJohn Wedig 683a92d0e6bSJohn Wedig log<level::INFO>("LinuxBootDone: Disabling IPMI"); 684a92d0e6bSJohn Wedig 685a92d0e6bSJohn Wedig // Start the bare metal active systemd target. 686a92d0e6bSJohn Wedig auto bus = sdbusplus::bus::new_default(); 687a92d0e6bSJohn Wedig auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 688a92d0e6bSJohn Wedig SYSTEMD_INTERFACE, "StartUnit"); 689a92d0e6bSJohn Wedig 690a92d0e6bSJohn Wedig method.append(BARE_METAL_TARGET); 691a92d0e6bSJohn Wedig method.append("replace"); 692a92d0e6bSJohn Wedig 693a92d0e6bSJohn Wedig try 694a92d0e6bSJohn Wedig { 695a92d0e6bSJohn Wedig bus.call_noreply(method); 696a92d0e6bSJohn Wedig } 697a92d0e6bSJohn Wedig catch (const sdbusplus::exception::SdBusError& ex) 698a92d0e6bSJohn Wedig { 699a92d0e6bSJohn Wedig log<level::ERR>("Failed to start bare metal active systemd target", 700a92d0e6bSJohn Wedig entry("WHAT=%s", ex.what())); 701a92d0e6bSJohn Wedig throw IpmiException(::ipmi::ccUnspecifiedError); 702a92d0e6bSJohn Wedig } 703a92d0e6bSJohn Wedig } 704a92d0e6bSJohn Wedig 705d455bfd6SGaurav Gandhi static constexpr char ACCEL_POWER_SERVICE[] = "xyz.openbmc_project.AccelPower"; 706d455bfd6SGaurav Gandhi static constexpr char ACCEL_POWER_PATH_PREFIX[] = 707d455bfd6SGaurav Gandhi "/xyz/openbmc_project/control/accel_power_"; 708d455bfd6SGaurav Gandhi static constexpr char POWER_MODE_IFC[] = 709d455bfd6SGaurav Gandhi "xyz.openbmc_project.Control.Power.Mode"; 710d455bfd6SGaurav Gandhi 711d455bfd6SGaurav Gandhi void Handler::accelSetVrSettings(::ipmi::Context::ptr ctx, uint8_t chip_id, 712d455bfd6SGaurav Gandhi uint8_t settings_id, uint16_t value) const 713d455bfd6SGaurav Gandhi { 714d455bfd6SGaurav Gandhi int vrSettingsReq; 715d455bfd6SGaurav Gandhi boost::system::error_code ec; 716d455bfd6SGaurav Gandhi if (_vrSettingsMap.find(settings_id) == _vrSettingsMap.end()) 717d455bfd6SGaurav Gandhi { 718d455bfd6SGaurav Gandhi log<level::ERR>("Settings ID is not supported", 719d455bfd6SGaurav Gandhi entry("settings_id=%d", settings_id)); 720d455bfd6SGaurav Gandhi throw IpmiException(::ipmi::ccParmOutOfRange); 721d455bfd6SGaurav Gandhi } 722d455bfd6SGaurav Gandhi 723d455bfd6SGaurav Gandhi vrSettingsReq = static_cast<int>(settings_id | value << 8); 724d455bfd6SGaurav Gandhi std::string object_name( 725d455bfd6SGaurav Gandhi std::format("{}{}", ACCEL_POWER_PATH_PREFIX, chip_id)); 726d455bfd6SGaurav Gandhi 727d455bfd6SGaurav Gandhi std::variant<int> val = vrSettingsReq; 728d455bfd6SGaurav Gandhi ctx->bus->yield_method_call(ctx->yield, ec, ACCEL_POWER_SERVICE, 729d455bfd6SGaurav Gandhi object_name.c_str(), 730d455bfd6SGaurav Gandhi "org.freedesktop.DBus.Properties", "Set", 731d455bfd6SGaurav Gandhi POWER_MODE_IFC, "PowerMode", val); 732d455bfd6SGaurav Gandhi if (ec) 733d455bfd6SGaurav Gandhi { 734d455bfd6SGaurav Gandhi log<level::ERR>("Failed to set PowerMode property"); 735d455bfd6SGaurav Gandhi throw IpmiException(::ipmi::ccUnspecifiedError); 736d455bfd6SGaurav Gandhi } 737d455bfd6SGaurav Gandhi } 738d455bfd6SGaurav Gandhi 739d455bfd6SGaurav Gandhi static constexpr char EXTERNAL_SENSOR_SERVICE[] = 740d455bfd6SGaurav Gandhi "xyz.openbmc_project.ExternalSensor"; 741d455bfd6SGaurav Gandhi static constexpr char EXTERNAL_SENSOR_PATH_PREFIX[] = 742d455bfd6SGaurav Gandhi "/xyz/openbmc_project/sensors/power/"; 743d455bfd6SGaurav Gandhi static constexpr char SENSOR_VALUE_IFC[] = "xyz.openbmc_project.Sensor.Value"; 744d455bfd6SGaurav Gandhi 745d455bfd6SGaurav Gandhi uint16_t Handler::accelGetVrSettings(::ipmi::Context::ptr ctx, uint8_t chip_id, 746d455bfd6SGaurav Gandhi uint8_t settings_id) const 747d455bfd6SGaurav Gandhi { 748d455bfd6SGaurav Gandhi Value value; 749d455bfd6SGaurav Gandhi boost::system::error_code ec; 750d455bfd6SGaurav Gandhi std::string object_name(std::format("{}{}{}", EXTERNAL_SENSOR_PATH_PREFIX, 751d455bfd6SGaurav Gandhi _vrSettingsMap.at(settings_id), 752d455bfd6SGaurav Gandhi chip_id)); 753d455bfd6SGaurav Gandhi 754d455bfd6SGaurav Gandhi if (_vrSettingsMap.find(settings_id) == _vrSettingsMap.end()) 755d455bfd6SGaurav Gandhi { 756d455bfd6SGaurav Gandhi log<level::ERR>("Settings ID is not supported", 757d455bfd6SGaurav Gandhi entry("settings_id=%d", settings_id)); 758d455bfd6SGaurav Gandhi throw IpmiException(::ipmi::ccParmOutOfRange); 759d455bfd6SGaurav Gandhi } 760d455bfd6SGaurav Gandhi 761d455bfd6SGaurav Gandhi value = ctx->bus->yield_method_call<std::variant<double>>( 762d455bfd6SGaurav Gandhi ctx->yield, ec, EXTERNAL_SENSOR_SERVICE, object_name.c_str(), 763d455bfd6SGaurav Gandhi "org.freedesktop.DBus.Properties", "Get", SENSOR_VALUE_IFC, "Value"); 764d455bfd6SGaurav Gandhi if (ec) 765d455bfd6SGaurav Gandhi { 766d455bfd6SGaurav Gandhi log<level::ERR>("accelGetVrSettings: Failed to call GetObject "); 767d455bfd6SGaurav Gandhi throw IpmiException(::ipmi::ccUnspecifiedError); 768d455bfd6SGaurav Gandhi } 769d455bfd6SGaurav Gandhi 770d455bfd6SGaurav Gandhi return static_cast<uint16_t>(std::get<double>(value)); 771d455bfd6SGaurav Gandhi } 772*559cb011SBrandon Kim 773*559cb011SBrandon Kim std::string Handler::getBMInstanceProperty(uint8_t propertyType) const 774*559cb011SBrandon Kim { 775*559cb011SBrandon Kim std::string propertyTypeString; 776*559cb011SBrandon Kim if (auto it = bmInstanceTypeStringMap.find(propertyType); 777*559cb011SBrandon Kim it == bmInstanceTypeStringMap.end()) 778*559cb011SBrandon Kim { 779*559cb011SBrandon Kim stdplus::print(stderr, "PropertyType: '{}' is invalid.\n", 780*559cb011SBrandon Kim propertyType); 781*559cb011SBrandon Kim throw IpmiException(::ipmi::ccInvalidFieldRequest); 782*559cb011SBrandon Kim } 783*559cb011SBrandon Kim else 784*559cb011SBrandon Kim { 785*559cb011SBrandon Kim propertyTypeString = it->second; 786*559cb011SBrandon Kim } 787*559cb011SBrandon Kim std::string opath = std::format("/run/bm-instance/{}", propertyTypeString); 788*559cb011SBrandon Kim // Check for file 789*559cb011SBrandon Kim 790*559cb011SBrandon Kim std::error_code ec; 791*559cb011SBrandon Kim // TODO(brandonkim@google.com): Fix this to use stdplus::ManagedFd 792*559cb011SBrandon Kim if (!this->getFs()->exists(opath, ec)) 793*559cb011SBrandon Kim { 794*559cb011SBrandon Kim stdplus::print(stderr, "Path: '{}' doesn't exist.\n", opath); 795*559cb011SBrandon Kim throw IpmiException(::ipmi::ccInvalidFieldRequest); 796*559cb011SBrandon Kim } 797*559cb011SBrandon Kim 798*559cb011SBrandon Kim // If file exists, read up to 64 bytes (normally shouldn't be more than 32) 799*559cb011SBrandon Kim std::ifstream ifs; 800*559cb011SBrandon Kim ifs.exceptions(std::ifstream::failbit); 801*559cb011SBrandon Kim std::string property; 802*559cb011SBrandon Kim try 803*559cb011SBrandon Kim { 804*559cb011SBrandon Kim ifs.open(opath); 805*559cb011SBrandon Kim ifs >> std::setw(64) >> property; 806*559cb011SBrandon Kim } 807*559cb011SBrandon Kim catch (std::ios_base::failure& fail) 808*559cb011SBrandon Kim { 809*559cb011SBrandon Kim stdplus::print(stderr, "Failed to read: '{}'.\n", opath); 810*559cb011SBrandon Kim throw IpmiException(::ipmi::ccUnspecifiedError); 811*559cb011SBrandon Kim } 812*559cb011SBrandon Kim return property; 813*559cb011SBrandon Kim } 814*559cb011SBrandon Kim 815f085d91dSPatrick Venture } // namespace ipmi 816f085d91dSPatrick Venture } // namespace google 817