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 16444b5ea4SPatrick Williams #include "bm_config.h" 17444b5ea4SPatrick 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 30444b5ea4SPatrick Williams #include <nlohmann/json.hpp> 31444b5ea4SPatrick Williams #include <phosphor-logging/elog-errors.hpp> 32444b5ea4SPatrick Williams #include <phosphor-logging/log.hpp> 33444b5ea4SPatrick Williams #include <sdbusplus/bus.hpp> 34444b5ea4SPatrick Williams #include <xyz/openbmc_project/Common/error.hpp> 35444b5ea4SPatrick 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 71*4baf41c8SHao Zhou static constexpr auto bmDriveCleaningFlagPath = "/run/bm-drive-cleaning.flag"; 72*4baf41c8SHao Zhou static constexpr auto bmDriveCleaningDoneFlagPath = 73*4baf41c8SHao Zhou "/run/bm-drive-cleaning-done.flag"; 74*4baf41c8SHao Zhou static constexpr auto bmDriveCleaningDoneAckFlagPath = 75*4baf41c8SHao Zhou "/run/bm-drive-cleaning-done-ack.flag"; 76*4baf41c8SHao Zhou 775e70dc8cSNikhil Namjoshi uint8_t isBmcInBareMetalMode() 785e70dc8cSNikhil Namjoshi { 795e70dc8cSNikhil Namjoshi #if BARE_METAL 805e70dc8cSNikhil Namjoshi return static_cast<uint8_t>(BmcMode::BM_MODE); 815e70dc8cSNikhil Namjoshi #else 823f3ca035SBrandon Kim std::error_code ec; 83*4baf41c8SHao Zhou 84*4baf41c8SHao Zhou if (fs::exists(bmDriveCleaningDoneAckFlagPath, ec)) 853f3ca035SBrandon Kim { 86*4baf41c8SHao Zhou std::fprintf( 87*4baf41c8SHao Zhou stderr, 88*4baf41c8SHao Zhou "%s exists so we acked cleaning done and must be in BM mode\n", 89*4baf41c8SHao Zhou bmDriveCleaningDoneAckFlagPath); 903f3ca035SBrandon Kim return static_cast<uint8_t>(BmcMode::BM_MODE); 913f3ca035SBrandon Kim } 923f3ca035SBrandon Kim 93*4baf41c8SHao Zhou if (fs::exists(bmDriveCleaningDoneFlagPath, ec)) 94*4baf41c8SHao Zhou { 95*4baf41c8SHao Zhou fs::rename(bmDriveCleaningDoneFlagPath, bmDriveCleaningDoneAckFlagPath, 96*4baf41c8SHao Zhou ec); 97*4baf41c8SHao Zhou std::fprintf( 98*4baf41c8SHao Zhou stderr, 99*4baf41c8SHao Zhou "%s exists so we just finished cleaning and must be in BM mode\n", 100*4baf41c8SHao Zhou bmDriveCleaningDoneFlagPath); 101*4baf41c8SHao Zhou return static_cast<uint8_t>(BmcMode::BM_MODE); 102*4baf41c8SHao Zhou } 103*4baf41c8SHao Zhou 104*4baf41c8SHao Zhou if (fs::exists(BM_SIGNAL_PATH, ec)) 105*4baf41c8SHao Zhou { 106*4baf41c8SHao Zhou if (!fs::exists(bmDriveCleaningFlagPath, ec)) 107*4baf41c8SHao Zhou { 108*4baf41c8SHao Zhou std::ofstream ofs; 109*4baf41c8SHao Zhou ofs.open(bmDriveCleaningFlagPath, std::ofstream::out); 110*4baf41c8SHao Zhou ofs.close(); 111*4baf41c8SHao Zhou } 112*4baf41c8SHao Zhou 113*4baf41c8SHao Zhou std::fprintf( 114*4baf41c8SHao Zhou stderr, 115*4baf41c8SHao Zhou "%s exists and no done/ack flag, we must be in BM cleaning mode\n", 1163f3ca035SBrandon Kim BM_SIGNAL_PATH); 117*4baf41c8SHao Zhou return static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE); 118*4baf41c8SHao Zhou } 119*4baf41c8SHao Zhou 120*4baf41c8SHao Zhou std::fprintf( 121*4baf41c8SHao Zhou stderr, 122*4baf41c8SHao Zhou "Unable to find any BM state files so we must not be in BM mode\n"); 1235e70dc8cSNikhil Namjoshi return static_cast<uint8_t>(BmcMode::NON_BM_MODE); 1245e70dc8cSNikhil Namjoshi #endif 1255e70dc8cSNikhil Namjoshi } 1265e70dc8cSNikhil Namjoshi 1275e70dc8cSNikhil Namjoshi uint8_t Handler::getBmcMode() 1285e70dc8cSNikhil Namjoshi { 1295e70dc8cSNikhil Namjoshi // BM_CLEANING_MODE is not implemented yet 1305e70dc8cSNikhil Namjoshi return isBmcInBareMetalMode(); 1315e70dc8cSNikhil Namjoshi } 1325e70dc8cSNikhil Namjoshi 133b69209b4SWilliam A. Kennington III std::tuple<std::uint8_t, std::string> 134b69209b4SWilliam A. Kennington III Handler::getEthDetails(std::string intf) const 135f085d91dSPatrick Venture { 136b69209b4SWilliam A. Kennington III if (intf.empty()) 137b69209b4SWilliam A. Kennington III { 138b69209b4SWilliam A. Kennington III intf = NCSI_IF_NAME_STR; 139b69209b4SWilliam A. Kennington III } 140b69209b4SWilliam A. Kennington III return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf)); 141f085d91dSPatrick Venture } 142f085d91dSPatrick Venture 143d2037c6aSPatrick Venture std::int64_t Handler::getRxPackets(const std::string& name) const 144d2037c6aSPatrick Venture { 145d2037c6aSPatrick Venture std::ostringstream opath; 146d2037c6aSPatrick Venture opath << "/sys/class/net/" << name << "/statistics/rx_packets"; 147d2037c6aSPatrick Venture std::string path = opath.str(); 148d2037c6aSPatrick Venture 149d2037c6aSPatrick Venture // Minor sanity & security check (of course, I'm less certain if unicode 150d2037c6aSPatrick Venture // comes into play here. 151d2037c6aSPatrick Venture // 152d2037c6aSPatrick Venture // Basically you can't easily inject ../ or /../ into the path below. 153d2037c6aSPatrick Venture if (name.find("/") != std::string::npos) 154d2037c6aSPatrick Venture { 155d2037c6aSPatrick Venture std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str()); 156e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 157d2037c6aSPatrick Venture } 158d2037c6aSPatrick Venture 159d2037c6aSPatrick Venture std::error_code ec; 160d2037c6aSPatrick Venture if (!fs::exists(path, ec)) 161d2037c6aSPatrick Venture { 162d2037c6aSPatrick Venture std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str()); 163e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 164d2037c6aSPatrick Venture } 165d2037c6aSPatrick Venture // We're uninterested in the state of ec. 166d2037c6aSPatrick Venture 167d2037c6aSPatrick Venture int64_t count = 0; 168d2037c6aSPatrick Venture std::ifstream ifs; 169d2037c6aSPatrick Venture ifs.exceptions(std::ifstream::failbit); 170d2037c6aSPatrick Venture try 171d2037c6aSPatrick Venture { 172d2037c6aSPatrick Venture ifs.open(path); 173d2037c6aSPatrick Venture ifs >> count; 174d2037c6aSPatrick Venture } 175d2037c6aSPatrick Venture catch (std::ios_base::failure& fail) 176d2037c6aSPatrick Venture { 177e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 178d2037c6aSPatrick Venture } 179d2037c6aSPatrick Venture 180d2037c6aSPatrick Venture return count; 181d2037c6aSPatrick Venture } 182d2037c6aSPatrick Venture 183bb90d4fdSPatrick Venture VersionTuple Handler::getCpldVersion(unsigned int id) const 184bb90d4fdSPatrick Venture { 185bb90d4fdSPatrick Venture std::ostringstream opath; 186bb90d4fdSPatrick Venture opath << "/run/cpld" << id << ".version"; 187bb90d4fdSPatrick Venture // Check for file 188bb90d4fdSPatrick Venture 189bb90d4fdSPatrick Venture std::error_code ec; 190bb90d4fdSPatrick Venture if (!fs::exists(opath.str(), ec)) 191bb90d4fdSPatrick Venture { 192bb90d4fdSPatrick Venture std::fprintf(stderr, "Path: '%s' doesn't exist.\n", 193bb90d4fdSPatrick Venture opath.str().c_str()); 194e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 195bb90d4fdSPatrick Venture } 196bb90d4fdSPatrick Venture // We're uninterested in the state of ec. 197bb90d4fdSPatrick Venture 198bb90d4fdSPatrick Venture // If file exists, read. 199bb90d4fdSPatrick Venture std::ifstream ifs; 200bb90d4fdSPatrick Venture ifs.exceptions(std::ifstream::failbit); 201bb90d4fdSPatrick Venture std::string value; 202bb90d4fdSPatrick Venture try 203bb90d4fdSPatrick Venture { 204bb90d4fdSPatrick Venture ifs.open(opath.str()); 205bb90d4fdSPatrick Venture ifs >> value; 206bb90d4fdSPatrick Venture } 207bb90d4fdSPatrick Venture catch (std::ios_base::failure& fail) 208bb90d4fdSPatrick Venture { 209e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 210bb90d4fdSPatrick Venture } 211bb90d4fdSPatrick Venture 212bb90d4fdSPatrick Venture // If value parses as expected, return version. 213bb90d4fdSPatrick Venture VersionTuple version = std::make_tuple(0, 0, 0, 0); 214bb90d4fdSPatrick Venture 215444b5ea4SPatrick Williams int num_fields = std::sscanf(value.c_str(), 216444b5ea4SPatrick Williams "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8, 217bb90d4fdSPatrick Venture &std::get<0>(version), &std::get<1>(version), 218bb90d4fdSPatrick Venture &std::get<2>(version), &std::get<3>(version)); 219bb90d4fdSPatrick Venture if (num_fields == 0) 220bb90d4fdSPatrick Venture { 221bb90d4fdSPatrick Venture std::fprintf(stderr, "Invalid version.\n"); 222e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 223bb90d4fdSPatrick Venture } 224bb90d4fdSPatrick Venture 225bb90d4fdSPatrick Venture return version; 226bb90d4fdSPatrick Venture } 227bb90d4fdSPatrick Venture 228aa374120SPatrick Venture static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay"; 229aa374120SPatrick Venture static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; 230aa374120SPatrick Venture static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; 231aa374120SPatrick Venture static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; 232aa374120SPatrick Venture static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target"; 233aa374120SPatrick Venture 234aa374120SPatrick Venture void Handler::psuResetDelay(std::uint32_t delay) const 235aa374120SPatrick Venture { 236aa374120SPatrick Venture std::ofstream ofs; 237aa374120SPatrick Venture ofs.open(TIME_DELAY_FILENAME, std::ofstream::out); 238aa374120SPatrick Venture if (!ofs.good()) 239aa374120SPatrick Venture { 240aa374120SPatrick Venture std::fprintf(stderr, "Unable to open file for output.\n"); 241e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 242aa374120SPatrick Venture } 243aa374120SPatrick Venture 244aa374120SPatrick Venture ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl; 245aa374120SPatrick Venture if (ofs.fail()) 246aa374120SPatrick Venture { 247aa374120SPatrick Venture std::fprintf(stderr, "Write failed\n"); 248aa374120SPatrick Venture ofs.close(); 249e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 250aa374120SPatrick Venture } 251aa374120SPatrick Venture 252aa374120SPatrick Venture // Write succeeded, please continue. 253aa374120SPatrick Venture ofs.flush(); 254aa374120SPatrick Venture ofs.close(); 255aa374120SPatrick Venture 256aa374120SPatrick Venture auto bus = sdbusplus::bus::new_default(); 257aa374120SPatrick Venture auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 258aa374120SPatrick Venture SYSTEMD_INTERFACE, "StartUnit"); 259aa374120SPatrick Venture 260aa374120SPatrick Venture method.append(PSU_HARDRESET_TARGET); 261aa374120SPatrick Venture method.append("replace"); 262aa374120SPatrick Venture 263aa374120SPatrick Venture try 264aa374120SPatrick Venture { 265aa374120SPatrick Venture bus.call_noreply(method); 266aa374120SPatrick Venture } 267aa374120SPatrick Venture catch (const sdbusplus::exception::SdBusError& ex) 268aa374120SPatrick Venture { 269aa374120SPatrick Venture log<level::ERR>("Failed to call PSU hard reset"); 270e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 271aa374120SPatrick Venture } 272aa374120SPatrick Venture } 273aa374120SPatrick Venture 274ac4a16f7SShounak Mitra static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5"; 275ac4a16f7SShounak Mitra 276ac4a16f7SShounak Mitra void Handler::psuResetOnShutdown() const 277ac4a16f7SShounak Mitra { 278ac4a16f7SShounak Mitra std::ofstream ofs; 279ac4a16f7SShounak Mitra ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out); 280ac4a16f7SShounak Mitra if (!ofs.good()) 281ac4a16f7SShounak Mitra { 282ac4a16f7SShounak Mitra std::fprintf(stderr, "Unable to open file for output.\n"); 283e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 284ac4a16f7SShounak Mitra } 285ac4a16f7SShounak Mitra ofs.close(); 286ac4a16f7SShounak Mitra } 287ac4a16f7SShounak Mitra 2883b1b427cSWilly Tu uint32_t Handler::getFlashSize() 2893b1b427cSWilly Tu { 2903b1b427cSWilly Tu mtd_info_t info; 2913b1b427cSWilly Tu int fd = open("/dev/mtd0", O_RDONLY); 2923b1b427cSWilly Tu int err = ioctl(fd, MEMGETINFO, &info); 2933b1b427cSWilly Tu close(fd); 2943b1b427cSWilly Tu 2953b1b427cSWilly Tu if (err) 2963b1b427cSWilly Tu { 297e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 2983b1b427cSWilly Tu } 2993b1b427cSWilly Tu return info.size; 3003b1b427cSWilly Tu } 3013b1b427cSWilly Tu 302ab650004SPatrick Venture std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance) 30307f85150SPatrick Venture { 304ab650004SPatrick Venture // Check if we support this Entity ID. 305ab650004SPatrick Venture auto it = _entityIdToName.find(id); 306ab650004SPatrick Venture if (it == _entityIdToName.end()) 30707f85150SPatrick Venture { 308ab650004SPatrick Venture log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id)); 309e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 31007f85150SPatrick Venture } 31107f85150SPatrick Venture 312ab650004SPatrick Venture std::string entityName; 313ab650004SPatrick Venture try 31407f85150SPatrick Venture { 315ab650004SPatrick Venture // Parse the JSON config file. 316ab650004SPatrick Venture if (!_entityConfigParsed) 317ab650004SPatrick Venture { 318ab650004SPatrick Venture _entityConfig = parseConfig(_configFile); 319ab650004SPatrick Venture _entityConfigParsed = true; 32007f85150SPatrick Venture } 32107f85150SPatrick Venture 322ab650004SPatrick Venture // Find the "entity id:entity instance" mapping to entity name. 323ab650004SPatrick Venture entityName = readNameFromConfig(it->second, instance, _entityConfig); 324ab650004SPatrick Venture if (entityName.empty()) 325ab650004SPatrick Venture { 326e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidFieldRequest); 327ab650004SPatrick Venture } 328ab650004SPatrick Venture } 329ab650004SPatrick Venture catch (InternalFailure& e) 330ab650004SPatrick Venture { 331e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 33207f85150SPatrick Venture } 33307f85150SPatrick Venture 334ab650004SPatrick Venture return entityName; 335ab650004SPatrick Venture } 336ab650004SPatrick Venture 33729f35bceSWilliam A. Kennington III std::string Handler::getMachineName() 33829f35bceSWilliam A. Kennington III { 33929f35bceSWilliam A. Kennington III const char* path = "/etc/os-release"; 34029f35bceSWilliam A. Kennington III std::ifstream ifs(path); 34129f35bceSWilliam A. Kennington III if (ifs.fail()) 34229f35bceSWilliam A. Kennington III { 34329f35bceSWilliam A. Kennington III std::fprintf(stderr, "Failed to open: %s\n", path); 344e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 34529f35bceSWilliam A. Kennington III } 34629f35bceSWilliam A. Kennington III 34729f35bceSWilliam A. Kennington III std::string line; 34829f35bceSWilliam A. Kennington III while (true) 34929f35bceSWilliam A. Kennington III { 35029f35bceSWilliam A. Kennington III std::getline(ifs, line); 35129f35bceSWilliam A. Kennington III if (ifs.eof()) 35229f35bceSWilliam A. Kennington III { 35329f35bceSWilliam A. Kennington III std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n", 35429f35bceSWilliam A. Kennington III path); 355e5a06675SMichael Shen throw IpmiException(::ipmi::ccInvalidCommand); 35629f35bceSWilliam A. Kennington III } 35729f35bceSWilliam A. Kennington III if (ifs.fail()) 35829f35bceSWilliam A. Kennington III { 35929f35bceSWilliam A. Kennington III std::fprintf(stderr, "Failed to read: %s\n", path); 360e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 36129f35bceSWilliam A. Kennington III } 36229f35bceSWilliam A. Kennington III std::string_view lineView(line); 36329f35bceSWilliam A. Kennington III constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE="; 36429f35bceSWilliam A. Kennington III if (lineView.substr(0, prefix.size()) != prefix) 36529f35bceSWilliam A. Kennington III { 36629f35bceSWilliam A. Kennington III continue; 36729f35bceSWilliam A. Kennington III } 36829f35bceSWilliam A. Kennington III lineView.remove_prefix(prefix.size()); 36929f35bceSWilliam A. Kennington III lineView.remove_prefix( 37029f35bceSWilliam A. Kennington III std::min(lineView.find_first_not_of('"'), lineView.size())); 37129f35bceSWilliam A. Kennington III lineView.remove_suffix( 37229f35bceSWilliam A. Kennington III lineView.size() - 1 - 37329f35bceSWilliam A. Kennington III std::min(lineView.find_last_not_of('"'), lineView.size() - 1)); 37429f35bceSWilliam A. Kennington III return std::string(lineView); 37529f35bceSWilliam A. Kennington III } 37629f35bceSWilliam A. Kennington III } 37729f35bceSWilliam A. Kennington III 3788cfa4c44Slinyuny static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay"; 3798cfa4c44Slinyuny static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target"; 3808cfa4c44Slinyuny 3818cfa4c44Slinyuny void Handler::hostPowerOffDelay(std::uint32_t delay) const 3828cfa4c44Slinyuny { 3838cfa4c44Slinyuny // Set time delay 3848cfa4c44Slinyuny std::ofstream ofs; 3858cfa4c44Slinyuny ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out); 3868cfa4c44Slinyuny if (!ofs.good()) 3878cfa4c44Slinyuny { 3888cfa4c44Slinyuny std::fprintf(stderr, "Unable to open file for output.\n"); 389e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 3908cfa4c44Slinyuny } 3918cfa4c44Slinyuny 3928cfa4c44Slinyuny ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl; 3938cfa4c44Slinyuny ofs.close(); 3948cfa4c44Slinyuny if (ofs.fail()) 3958cfa4c44Slinyuny { 3968cfa4c44Slinyuny std::fprintf(stderr, "Write failed\n"); 397e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 3988cfa4c44Slinyuny } 3998cfa4c44Slinyuny 4008cfa4c44Slinyuny // Write succeeded, please continue. 4018cfa4c44Slinyuny auto bus = sdbusplus::bus::new_default(); 4028cfa4c44Slinyuny auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 4038cfa4c44Slinyuny SYSTEMD_INTERFACE, "StartUnit"); 4048cfa4c44Slinyuny 4058cfa4c44Slinyuny method.append(HOST_POWEROFF_TARGET); 4068cfa4c44Slinyuny method.append("replace"); 4078cfa4c44Slinyuny 4088cfa4c44Slinyuny try 4098cfa4c44Slinyuny { 4108cfa4c44Slinyuny bus.call_noreply(method); 4118cfa4c44Slinyuny } 4128cfa4c44Slinyuny catch (const sdbusplus::exception::SdBusError& ex) 4138cfa4c44Slinyuny { 4148cfa4c44Slinyuny log<level::ERR>("Failed to call Power Off", 4158cfa4c44Slinyuny entry("WHAT=%s", ex.what())); 416e5a06675SMichael Shen throw IpmiException(::ipmi::ccUnspecifiedError); 4178cfa4c44Slinyuny } 4188cfa4c44Slinyuny } 4198cfa4c44Slinyuny 420ab650004SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance, 421ab650004SPatrick Venture const Json& config) 42207f85150SPatrick Venture { 42307f85150SPatrick Venture static const std::vector<Json> empty{}; 42407f85150SPatrick Venture std::vector<Json> readings = config.value(type, empty); 42507f85150SPatrick Venture std::string name = ""; 426ab650004SPatrick Venture 42707f85150SPatrick Venture for (const auto& j : readings) 42807f85150SPatrick Venture { 42907f85150SPatrick Venture uint8_t instanceNum = j.value("instance", 0); 43007f85150SPatrick Venture // Not the instance we're interested in 43107f85150SPatrick Venture if (instanceNum != instance) 43207f85150SPatrick Venture { 43307f85150SPatrick Venture continue; 43407f85150SPatrick Venture } 43507f85150SPatrick Venture 43607f85150SPatrick Venture // Found the instance we're interested in 43707f85150SPatrick Venture name = j.value("name", ""); 43807f85150SPatrick Venture 43907f85150SPatrick Venture break; 44007f85150SPatrick Venture } 441ab650004SPatrick Venture 44207f85150SPatrick Venture return name; 44307f85150SPatrick Venture } 44407f85150SPatrick Venture 44549f23ad9SPatrick Venture void Handler::buildI2cPcieMapping() 44649f23ad9SPatrick Venture { 44749f23ad9SPatrick Venture _pcie_i2c_map = buildPcieMap(); 44849f23ad9SPatrick Venture } 44949f23ad9SPatrick Venture 45049f23ad9SPatrick Venture size_t Handler::getI2cPcieMappingSize() const 45149f23ad9SPatrick Venture { 45249f23ad9SPatrick Venture return _pcie_i2c_map.size(); 45349f23ad9SPatrick Venture } 45449f23ad9SPatrick Venture 45549f23ad9SPatrick Venture std::tuple<std::uint32_t, std::string> 45649f23ad9SPatrick Venture Handler::getI2cEntry(unsigned int entry) const 45749f23ad9SPatrick Venture { 45849f23ad9SPatrick Venture return _pcie_i2c_map[entry]; 45949f23ad9SPatrick Venture } 46049f23ad9SPatrick Venture 4614f0d1de6SSteve Foreman namespace 4624f0d1de6SSteve Foreman { 4634f0d1de6SSteve Foreman 4644f0d1de6SSteve Foreman static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/"; 4654f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel"; 4664f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR"; 4674f0d1de6SSteve Foreman 4684f0d1de6SSteve Foreman // C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects() 4694f0d1de6SSteve Foreman using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>; 4704f0d1de6SSteve Foreman using AnyTypeList = std::vector<std::pair<std::string, AnyType>>; 4714f0d1de6SSteve Foreman using NamedArrayOfAnyTypeLists = 4724f0d1de6SSteve Foreman std::vector<std::pair<std::string, AnyTypeList>>; 4734f0d1de6SSteve Foreman using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector< 4744f0d1de6SSteve Foreman std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>; 4754f0d1de6SSteve Foreman 4764f0d1de6SSteve Foreman } // namespace 4774f0d1de6SSteve Foreman 47865371228SPatrick Williams sdbusplus::bus_t Handler::getDbus() const 4794f0d1de6SSteve Foreman { 4804f0d1de6SSteve Foreman return sdbusplus::bus::new_default(); 4814f0d1de6SSteve Foreman } 4824f0d1de6SSteve Foreman 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 673f085d91dSPatrick Venture } // namespace ipmi 674f085d91dSPatrick Venture } // namespace google 675