1*4f0d1de6SSteve 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 15f085d91dSPatrick Venture #include "handler.hpp" 16f085d91dSPatrick Venture 17d2037c6aSPatrick Venture #include "errors.hpp" 18c87de558SPatrick Venture #include "handler_impl.hpp" 19ab650004SPatrick Venture #include "util.hpp" 20d2037c6aSPatrick Venture 213b1b427cSWilly Tu #include <fcntl.h> 22d2037c6aSPatrick Venture #include <ipmid/api.h> 233b1b427cSWilly Tu #include <mtd/mtd-abi.h> 243b1b427cSWilly Tu #include <mtd/mtd-user.h> 253b1b427cSWilly Tu #include <sys/ioctl.h> 263b1b427cSWilly Tu #include <unistd.h> 27d2037c6aSPatrick Venture 28bb90d4fdSPatrick Venture #include <cinttypes> 29d2037c6aSPatrick Venture #include <cstdio> 30d2037c6aSPatrick Venture #include <filesystem> 31d2037c6aSPatrick Venture #include <fstream> 3207f85150SPatrick Venture #include <map> 3307f85150SPatrick Venture #include <nlohmann/json.hpp> 3407f85150SPatrick Venture #include <phosphor-logging/elog-errors.hpp> 35aa374120SPatrick Venture #include <phosphor-logging/log.hpp> 36aa374120SPatrick Venture #include <sdbusplus/bus.hpp> 37d2037c6aSPatrick Venture #include <sstream> 38d2037c6aSPatrick Venture #include <string> 3929f35bceSWilliam A. Kennington III #include <string_view> 40d2037c6aSPatrick Venture #include <tuple> 41*4f0d1de6SSteve Foreman #include <variant> 4207f85150SPatrick Venture #include <xyz/openbmc_project/Common/error.hpp> 43d2037c6aSPatrick Venture 44f085d91dSPatrick Venture #ifndef NCSI_IF_NAME 45f085d91dSPatrick Venture #define NCSI_IF_NAME eth0 46f085d91dSPatrick Venture #endif 47f085d91dSPatrick Venture 48f085d91dSPatrick Venture // To deal with receiving a string without quotes. 49f085d91dSPatrick Venture #define QUOTE(name) #name 50f085d91dSPatrick Venture #define STR(macro) QUOTE(macro) 51f085d91dSPatrick Venture #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME) 52f085d91dSPatrick Venture 538d3d46a2SWilliam A. Kennington III namespace ipmi 548d3d46a2SWilliam A. Kennington III { 558d3d46a2SWilliam A. Kennington III std::uint8_t getChannelByName(const std::string& chName); 568d3d46a2SWilliam A. Kennington III } 578d3d46a2SWilliam A. Kennington III 58f085d91dSPatrick Venture namespace google 59f085d91dSPatrick Venture { 60f085d91dSPatrick Venture namespace ipmi 61f085d91dSPatrick Venture { 62d2037c6aSPatrick Venture namespace fs = std::filesystem; 6307f85150SPatrick Venture using Json = nlohmann::json; 6407f85150SPatrick Venture using namespace phosphor::logging; 6507f85150SPatrick Venture using InternalFailure = 6607f85150SPatrick Venture sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; 67f085d91dSPatrick Venture 68b69209b4SWilliam A. Kennington III std::tuple<std::uint8_t, std::string> 69b69209b4SWilliam A. Kennington III Handler::getEthDetails(std::string intf) const 70f085d91dSPatrick Venture { 71b69209b4SWilliam A. Kennington III if (intf.empty()) 72b69209b4SWilliam A. Kennington III { 73b69209b4SWilliam A. Kennington III intf = NCSI_IF_NAME_STR; 74b69209b4SWilliam A. Kennington III } 75b69209b4SWilliam A. Kennington III return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf)); 76f085d91dSPatrick Venture } 77f085d91dSPatrick Venture 78d2037c6aSPatrick Venture std::int64_t Handler::getRxPackets(const std::string& name) const 79d2037c6aSPatrick Venture { 80d2037c6aSPatrick Venture std::ostringstream opath; 81d2037c6aSPatrick Venture opath << "/sys/class/net/" << name << "/statistics/rx_packets"; 82d2037c6aSPatrick Venture std::string path = opath.str(); 83d2037c6aSPatrick Venture 84d2037c6aSPatrick Venture // Minor sanity & security check (of course, I'm less certain if unicode 85d2037c6aSPatrick Venture // comes into play here. 86d2037c6aSPatrick Venture // 87d2037c6aSPatrick Venture // Basically you can't easily inject ../ or /../ into the path below. 88d2037c6aSPatrick Venture if (name.find("/") != std::string::npos) 89d2037c6aSPatrick Venture { 90d2037c6aSPatrick Venture std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str()); 91d2037c6aSPatrick Venture throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST); 92d2037c6aSPatrick Venture } 93d2037c6aSPatrick Venture 94d2037c6aSPatrick Venture std::error_code ec; 95d2037c6aSPatrick Venture if (!fs::exists(path, ec)) 96d2037c6aSPatrick Venture { 97d2037c6aSPatrick Venture std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str()); 98d2037c6aSPatrick Venture throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST); 99d2037c6aSPatrick Venture } 100d2037c6aSPatrick Venture // We're uninterested in the state of ec. 101d2037c6aSPatrick Venture 102d2037c6aSPatrick Venture int64_t count = 0; 103d2037c6aSPatrick Venture std::ifstream ifs; 104d2037c6aSPatrick Venture ifs.exceptions(std::ifstream::failbit); 105d2037c6aSPatrick Venture try 106d2037c6aSPatrick Venture { 107d2037c6aSPatrick Venture ifs.open(path); 108d2037c6aSPatrick Venture ifs >> count; 109d2037c6aSPatrick Venture } 110d2037c6aSPatrick Venture catch (std::ios_base::failure& fail) 111d2037c6aSPatrick Venture { 112d2037c6aSPatrick Venture throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 113d2037c6aSPatrick Venture } 114d2037c6aSPatrick Venture 115d2037c6aSPatrick Venture return count; 116d2037c6aSPatrick Venture } 117d2037c6aSPatrick Venture 118bb90d4fdSPatrick Venture VersionTuple Handler::getCpldVersion(unsigned int id) const 119bb90d4fdSPatrick Venture { 120bb90d4fdSPatrick Venture std::ostringstream opath; 121bb90d4fdSPatrick Venture opath << "/run/cpld" << id << ".version"; 122bb90d4fdSPatrick Venture // Check for file 123bb90d4fdSPatrick Venture 124bb90d4fdSPatrick Venture std::error_code ec; 125bb90d4fdSPatrick Venture if (!fs::exists(opath.str(), ec)) 126bb90d4fdSPatrick Venture { 127bb90d4fdSPatrick Venture std::fprintf(stderr, "Path: '%s' doesn't exist.\n", 128bb90d4fdSPatrick Venture opath.str().c_str()); 129bb90d4fdSPatrick Venture throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST); 130bb90d4fdSPatrick Venture } 131bb90d4fdSPatrick Venture // We're uninterested in the state of ec. 132bb90d4fdSPatrick Venture 133bb90d4fdSPatrick Venture // If file exists, read. 134bb90d4fdSPatrick Venture std::ifstream ifs; 135bb90d4fdSPatrick Venture ifs.exceptions(std::ifstream::failbit); 136bb90d4fdSPatrick Venture std::string value; 137bb90d4fdSPatrick Venture try 138bb90d4fdSPatrick Venture { 139bb90d4fdSPatrick Venture ifs.open(opath.str()); 140bb90d4fdSPatrick Venture ifs >> value; 141bb90d4fdSPatrick Venture } 142bb90d4fdSPatrick Venture catch (std::ios_base::failure& fail) 143bb90d4fdSPatrick Venture { 144bb90d4fdSPatrick Venture throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 145bb90d4fdSPatrick Venture } 146bb90d4fdSPatrick Venture 147bb90d4fdSPatrick Venture // If value parses as expected, return version. 148bb90d4fdSPatrick Venture VersionTuple version = std::make_tuple(0, 0, 0, 0); 149bb90d4fdSPatrick Venture 150bb90d4fdSPatrick Venture int num_fields = 151bb90d4fdSPatrick Venture std::sscanf(value.c_str(), "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8, 152bb90d4fdSPatrick Venture &std::get<0>(version), &std::get<1>(version), 153bb90d4fdSPatrick Venture &std::get<2>(version), &std::get<3>(version)); 154bb90d4fdSPatrick Venture if (num_fields == 0) 155bb90d4fdSPatrick Venture { 156bb90d4fdSPatrick Venture std::fprintf(stderr, "Invalid version.\n"); 157bb90d4fdSPatrick Venture throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 158bb90d4fdSPatrick Venture } 159bb90d4fdSPatrick Venture 160bb90d4fdSPatrick Venture return version; 161bb90d4fdSPatrick Venture } 162bb90d4fdSPatrick Venture 163aa374120SPatrick Venture static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay"; 164aa374120SPatrick Venture static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; 165aa374120SPatrick Venture static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; 166aa374120SPatrick Venture static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; 167aa374120SPatrick Venture static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target"; 168aa374120SPatrick Venture 169aa374120SPatrick Venture void Handler::psuResetDelay(std::uint32_t delay) const 170aa374120SPatrick Venture { 171aa374120SPatrick Venture std::ofstream ofs; 172aa374120SPatrick Venture ofs.open(TIME_DELAY_FILENAME, std::ofstream::out); 173aa374120SPatrick Venture if (!ofs.good()) 174aa374120SPatrick Venture { 175aa374120SPatrick Venture std::fprintf(stderr, "Unable to open file for output.\n"); 176aa374120SPatrick Venture throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 177aa374120SPatrick Venture } 178aa374120SPatrick Venture 179aa374120SPatrick Venture ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl; 180aa374120SPatrick Venture if (ofs.fail()) 181aa374120SPatrick Venture { 182aa374120SPatrick Venture std::fprintf(stderr, "Write failed\n"); 183aa374120SPatrick Venture ofs.close(); 184aa374120SPatrick Venture throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 185aa374120SPatrick Venture } 186aa374120SPatrick Venture 187aa374120SPatrick Venture // Write succeeded, please continue. 188aa374120SPatrick Venture ofs.flush(); 189aa374120SPatrick Venture ofs.close(); 190aa374120SPatrick Venture 191aa374120SPatrick Venture auto bus = sdbusplus::bus::new_default(); 192aa374120SPatrick Venture auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 193aa374120SPatrick Venture SYSTEMD_INTERFACE, "StartUnit"); 194aa374120SPatrick Venture 195aa374120SPatrick Venture method.append(PSU_HARDRESET_TARGET); 196aa374120SPatrick Venture method.append("replace"); 197aa374120SPatrick Venture 198aa374120SPatrick Venture try 199aa374120SPatrick Venture { 200aa374120SPatrick Venture bus.call_noreply(method); 201aa374120SPatrick Venture } 202aa374120SPatrick Venture catch (const sdbusplus::exception::SdBusError& ex) 203aa374120SPatrick Venture { 204aa374120SPatrick Venture log<level::ERR>("Failed to call PSU hard reset"); 205aa374120SPatrick Venture throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 206aa374120SPatrick Venture } 207aa374120SPatrick Venture } 208aa374120SPatrick Venture 209ac4a16f7SShounak Mitra static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5"; 210ac4a16f7SShounak Mitra 211ac4a16f7SShounak Mitra void Handler::psuResetOnShutdown() const 212ac4a16f7SShounak Mitra { 213ac4a16f7SShounak Mitra std::ofstream ofs; 214ac4a16f7SShounak Mitra ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out); 215ac4a16f7SShounak Mitra if (!ofs.good()) 216ac4a16f7SShounak Mitra { 217ac4a16f7SShounak Mitra std::fprintf(stderr, "Unable to open file for output.\n"); 218ac4a16f7SShounak Mitra throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 219ac4a16f7SShounak Mitra } 220ac4a16f7SShounak Mitra ofs.close(); 221ac4a16f7SShounak Mitra } 222ac4a16f7SShounak Mitra 2233b1b427cSWilly Tu uint32_t Handler::getFlashSize() 2243b1b427cSWilly Tu { 2253b1b427cSWilly Tu mtd_info_t info; 2263b1b427cSWilly Tu int fd = open("/dev/mtd0", O_RDONLY); 2273b1b427cSWilly Tu int err = ioctl(fd, MEMGETINFO, &info); 2283b1b427cSWilly Tu close(fd); 2293b1b427cSWilly Tu 2303b1b427cSWilly Tu if (err) 2313b1b427cSWilly Tu { 2323b1b427cSWilly Tu throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 2333b1b427cSWilly Tu } 2343b1b427cSWilly Tu return info.size; 2353b1b427cSWilly Tu } 2363b1b427cSWilly Tu 237ab650004SPatrick Venture std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance) 23807f85150SPatrick Venture { 239ab650004SPatrick Venture // Check if we support this Entity ID. 240ab650004SPatrick Venture auto it = _entityIdToName.find(id); 241ab650004SPatrick Venture if (it == _entityIdToName.end()) 24207f85150SPatrick Venture { 243ab650004SPatrick Venture log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id)); 244ab650004SPatrick Venture throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST); 24507f85150SPatrick Venture } 24607f85150SPatrick Venture 247ab650004SPatrick Venture std::string entityName; 248ab650004SPatrick Venture try 24907f85150SPatrick Venture { 250ab650004SPatrick Venture // Parse the JSON config file. 251ab650004SPatrick Venture if (!_entityConfigParsed) 252ab650004SPatrick Venture { 253ab650004SPatrick Venture _entityConfig = parseConfig(_configFile); 254ab650004SPatrick Venture _entityConfigParsed = true; 25507f85150SPatrick Venture } 25607f85150SPatrick Venture 257ab650004SPatrick Venture // Find the "entity id:entity instance" mapping to entity name. 258ab650004SPatrick Venture entityName = readNameFromConfig(it->second, instance, _entityConfig); 259ab650004SPatrick Venture if (entityName.empty()) 260ab650004SPatrick Venture { 261ab650004SPatrick Venture throw IpmiException(IPMI_CC_INVALID_FIELD_REQUEST); 262ab650004SPatrick Venture } 263ab650004SPatrick Venture } 264ab650004SPatrick Venture catch (InternalFailure& e) 265ab650004SPatrick Venture { 266ab650004SPatrick Venture throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 26707f85150SPatrick Venture } 26807f85150SPatrick Venture 269ab650004SPatrick Venture return entityName; 270ab650004SPatrick Venture } 271ab650004SPatrick Venture 27229f35bceSWilliam A. Kennington III std::string Handler::getMachineName() 27329f35bceSWilliam A. Kennington III { 27429f35bceSWilliam A. Kennington III const char* path = "/etc/os-release"; 27529f35bceSWilliam A. Kennington III std::ifstream ifs(path); 27629f35bceSWilliam A. Kennington III if (ifs.fail()) 27729f35bceSWilliam A. Kennington III { 27829f35bceSWilliam A. Kennington III std::fprintf(stderr, "Failed to open: %s\n", path); 27929f35bceSWilliam A. Kennington III throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 28029f35bceSWilliam A. Kennington III } 28129f35bceSWilliam A. Kennington III 28229f35bceSWilliam A. Kennington III std::string line; 28329f35bceSWilliam A. Kennington III while (true) 28429f35bceSWilliam A. Kennington III { 28529f35bceSWilliam A. Kennington III std::getline(ifs, line); 28629f35bceSWilliam A. Kennington III if (ifs.eof()) 28729f35bceSWilliam A. Kennington III { 28829f35bceSWilliam A. Kennington III std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n", 28929f35bceSWilliam A. Kennington III path); 29029f35bceSWilliam A. Kennington III throw IpmiException(IPMI_CC_INVALID); 29129f35bceSWilliam A. Kennington III } 29229f35bceSWilliam A. Kennington III if (ifs.fail()) 29329f35bceSWilliam A. Kennington III { 29429f35bceSWilliam A. Kennington III std::fprintf(stderr, "Failed to read: %s\n", path); 29529f35bceSWilliam A. Kennington III throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 29629f35bceSWilliam A. Kennington III } 29729f35bceSWilliam A. Kennington III std::string_view lineView(line); 29829f35bceSWilliam A. Kennington III constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE="; 29929f35bceSWilliam A. Kennington III if (lineView.substr(0, prefix.size()) != prefix) 30029f35bceSWilliam A. Kennington III { 30129f35bceSWilliam A. Kennington III continue; 30229f35bceSWilliam A. Kennington III } 30329f35bceSWilliam A. Kennington III lineView.remove_prefix(prefix.size()); 30429f35bceSWilliam A. Kennington III lineView.remove_prefix( 30529f35bceSWilliam A. Kennington III std::min(lineView.find_first_not_of('"'), lineView.size())); 30629f35bceSWilliam A. Kennington III lineView.remove_suffix( 30729f35bceSWilliam A. Kennington III lineView.size() - 1 - 30829f35bceSWilliam A. Kennington III std::min(lineView.find_last_not_of('"'), lineView.size() - 1)); 30929f35bceSWilliam A. Kennington III return std::string(lineView); 31029f35bceSWilliam A. Kennington III } 31129f35bceSWilliam A. Kennington III } 31229f35bceSWilliam A. Kennington III 3138cfa4c44Slinyuny static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay"; 3148cfa4c44Slinyuny static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target"; 3158cfa4c44Slinyuny 3168cfa4c44Slinyuny void Handler::hostPowerOffDelay(std::uint32_t delay) const 3178cfa4c44Slinyuny { 3188cfa4c44Slinyuny // Set time delay 3198cfa4c44Slinyuny std::ofstream ofs; 3208cfa4c44Slinyuny ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out); 3218cfa4c44Slinyuny if (!ofs.good()) 3228cfa4c44Slinyuny { 3238cfa4c44Slinyuny std::fprintf(stderr, "Unable to open file for output.\n"); 3248cfa4c44Slinyuny throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 3258cfa4c44Slinyuny } 3268cfa4c44Slinyuny 3278cfa4c44Slinyuny ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl; 3288cfa4c44Slinyuny ofs.close(); 3298cfa4c44Slinyuny if (ofs.fail()) 3308cfa4c44Slinyuny { 3318cfa4c44Slinyuny std::fprintf(stderr, "Write failed\n"); 3328cfa4c44Slinyuny throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 3338cfa4c44Slinyuny } 3348cfa4c44Slinyuny 3358cfa4c44Slinyuny // Write succeeded, please continue. 3368cfa4c44Slinyuny auto bus = sdbusplus::bus::new_default(); 3378cfa4c44Slinyuny auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, 3388cfa4c44Slinyuny SYSTEMD_INTERFACE, "StartUnit"); 3398cfa4c44Slinyuny 3408cfa4c44Slinyuny method.append(HOST_POWEROFF_TARGET); 3418cfa4c44Slinyuny method.append("replace"); 3428cfa4c44Slinyuny 3438cfa4c44Slinyuny try 3448cfa4c44Slinyuny { 3458cfa4c44Slinyuny bus.call_noreply(method); 3468cfa4c44Slinyuny } 3478cfa4c44Slinyuny catch (const sdbusplus::exception::SdBusError& ex) 3488cfa4c44Slinyuny { 3498cfa4c44Slinyuny log<level::ERR>("Failed to call Power Off", 3508cfa4c44Slinyuny entry("WHAT=%s", ex.what())); 3518cfa4c44Slinyuny throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 3528cfa4c44Slinyuny } 3538cfa4c44Slinyuny } 3548cfa4c44Slinyuny 355ab650004SPatrick Venture std::string readNameFromConfig(const std::string& type, uint8_t instance, 356ab650004SPatrick Venture const Json& config) 35707f85150SPatrick Venture { 35807f85150SPatrick Venture static const std::vector<Json> empty{}; 35907f85150SPatrick Venture std::vector<Json> readings = config.value(type, empty); 36007f85150SPatrick Venture std::string name = ""; 361ab650004SPatrick Venture 36207f85150SPatrick Venture for (const auto& j : readings) 36307f85150SPatrick Venture { 36407f85150SPatrick Venture uint8_t instanceNum = j.value("instance", 0); 36507f85150SPatrick Venture // Not the instance we're interested in 36607f85150SPatrick Venture if (instanceNum != instance) 36707f85150SPatrick Venture { 36807f85150SPatrick Venture continue; 36907f85150SPatrick Venture } 37007f85150SPatrick Venture 37107f85150SPatrick Venture // Found the instance we're interested in 37207f85150SPatrick Venture name = j.value("name", ""); 37307f85150SPatrick Venture 37407f85150SPatrick Venture break; 37507f85150SPatrick Venture } 376ab650004SPatrick Venture 37707f85150SPatrick Venture return name; 37807f85150SPatrick Venture } 37907f85150SPatrick Venture 38049f23ad9SPatrick Venture void Handler::buildI2cPcieMapping() 38149f23ad9SPatrick Venture { 38249f23ad9SPatrick Venture _pcie_i2c_map = buildPcieMap(); 38349f23ad9SPatrick Venture } 38449f23ad9SPatrick Venture 38549f23ad9SPatrick Venture size_t Handler::getI2cPcieMappingSize() const 38649f23ad9SPatrick Venture { 38749f23ad9SPatrick Venture return _pcie_i2c_map.size(); 38849f23ad9SPatrick Venture } 38949f23ad9SPatrick Venture 39049f23ad9SPatrick Venture std::tuple<std::uint32_t, std::string> 39149f23ad9SPatrick Venture Handler::getI2cEntry(unsigned int entry) const 39249f23ad9SPatrick Venture { 39349f23ad9SPatrick Venture return _pcie_i2c_map[entry]; 39449f23ad9SPatrick Venture } 39549f23ad9SPatrick Venture 396*4f0d1de6SSteve Foreman namespace 397*4f0d1de6SSteve Foreman { 398*4f0d1de6SSteve Foreman 399*4f0d1de6SSteve Foreman static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/"; 400*4f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel"; 401*4f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR"; 402*4f0d1de6SSteve Foreman 403*4f0d1de6SSteve Foreman // C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects() 404*4f0d1de6SSteve Foreman using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>; 405*4f0d1de6SSteve Foreman using AnyTypeList = std::vector<std::pair<std::string, AnyType>>; 406*4f0d1de6SSteve Foreman using NamedArrayOfAnyTypeLists = 407*4f0d1de6SSteve Foreman std::vector<std::pair<std::string, AnyTypeList>>; 408*4f0d1de6SSteve Foreman using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector< 409*4f0d1de6SSteve Foreman std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>; 410*4f0d1de6SSteve Foreman 411*4f0d1de6SSteve Foreman } // namespace 412*4f0d1de6SSteve Foreman 413*4f0d1de6SSteve Foreman sdbusplus::bus::bus Handler::accelOobGetDbus() const 414*4f0d1de6SSteve Foreman { 415*4f0d1de6SSteve Foreman return sdbusplus::bus::new_default(); 416*4f0d1de6SSteve Foreman } 417*4f0d1de6SSteve Foreman 418*4f0d1de6SSteve Foreman uint32_t Handler::accelOobDeviceCount() const 419*4f0d1de6SSteve Foreman { 420*4f0d1de6SSteve Foreman ArrayOfObjectPathsAndTieredAnyTypeLists data; 421*4f0d1de6SSteve Foreman 422*4f0d1de6SSteve Foreman try 423*4f0d1de6SSteve Foreman { 424*4f0d1de6SSteve Foreman auto bus = accelOobGetDbus(); 425*4f0d1de6SSteve Foreman auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/", 426*4f0d1de6SSteve Foreman "org.freedesktop.DBus.ObjectManager", 427*4f0d1de6SSteve Foreman "GetManagedObjects"); 428*4f0d1de6SSteve Foreman bus.call(method).read(data); 429*4f0d1de6SSteve Foreman } 430*4f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 431*4f0d1de6SSteve Foreman { 432*4f0d1de6SSteve Foreman log<level::ERR>( 433*4f0d1de6SSteve Foreman "Failed to call GetManagedObjects on com.google.custom_accel", 434*4f0d1de6SSteve Foreman entry("WHAT=%s", ex.what())); 435*4f0d1de6SSteve Foreman throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 436*4f0d1de6SSteve Foreman } 437*4f0d1de6SSteve Foreman 438*4f0d1de6SSteve Foreman return data.size(); 439*4f0d1de6SSteve Foreman } 440*4f0d1de6SSteve Foreman 441*4f0d1de6SSteve Foreman std::string Handler::accelOobDeviceName(size_t index) const 442*4f0d1de6SSteve Foreman { 443*4f0d1de6SSteve Foreman ArrayOfObjectPathsAndTieredAnyTypeLists data; 444*4f0d1de6SSteve Foreman 445*4f0d1de6SSteve Foreman try 446*4f0d1de6SSteve Foreman { 447*4f0d1de6SSteve Foreman auto bus = accelOobGetDbus(); 448*4f0d1de6SSteve Foreman auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/", 449*4f0d1de6SSteve Foreman "org.freedesktop.DBus.ObjectManager", 450*4f0d1de6SSteve Foreman "GetManagedObjects"); 451*4f0d1de6SSteve Foreman bus.call(method).read(data); 452*4f0d1de6SSteve Foreman } 453*4f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 454*4f0d1de6SSteve Foreman { 455*4f0d1de6SSteve Foreman log<level::ERR>( 456*4f0d1de6SSteve Foreman "Failed to call GetManagedObjects on com.google.custom_accel", 457*4f0d1de6SSteve Foreman entry("WHAT=%s", ex.what())); 458*4f0d1de6SSteve Foreman throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 459*4f0d1de6SSteve Foreman } 460*4f0d1de6SSteve Foreman 461*4f0d1de6SSteve Foreman if (index >= data.size()) 462*4f0d1de6SSteve Foreman { 463*4f0d1de6SSteve Foreman log<level::WARNING>( 464*4f0d1de6SSteve Foreman "Requested index is larger than the number of entries.", 465*4f0d1de6SSteve Foreman entry("INDEX=%zu", index), entry("NUM_NAMES=%zu", data.size())); 466*4f0d1de6SSteve Foreman throw IpmiException(IPMI_CC_PARM_OUT_OF_RANGE); 467*4f0d1de6SSteve Foreman } 468*4f0d1de6SSteve Foreman 469*4f0d1de6SSteve Foreman std::string_view name(data[index].first.str); 470*4f0d1de6SSteve Foreman if (!name.starts_with(ACCEL_OOB_ROOT)) 471*4f0d1de6SSteve Foreman { 472*4f0d1de6SSteve Foreman throw IpmiException(IPMI_CC_INVALID); 473*4f0d1de6SSteve Foreman } 474*4f0d1de6SSteve Foreman name.remove_prefix(ACCEL_OOB_ROOT.length()); 475*4f0d1de6SSteve Foreman return std::string(name); 476*4f0d1de6SSteve Foreman } 477*4f0d1de6SSteve Foreman 478*4f0d1de6SSteve Foreman uint64_t Handler::accelOobRead(std::string_view name, uint64_t address, 479*4f0d1de6SSteve Foreman uint8_t num_bytes) const 480*4f0d1de6SSteve Foreman { 481*4f0d1de6SSteve Foreman static constexpr char ACCEL_OOB_METHOD[] = "Read"; 482*4f0d1de6SSteve Foreman 483*4f0d1de6SSteve Foreman std::string object_name(ACCEL_OOB_ROOT); 484*4f0d1de6SSteve Foreman object_name.append(name); 485*4f0d1de6SSteve Foreman 486*4f0d1de6SSteve Foreman auto bus = accelOobGetDbus(); 487*4f0d1de6SSteve Foreman auto method = bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(), 488*4f0d1de6SSteve Foreman ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD); 489*4f0d1de6SSteve Foreman method.append(address, static_cast<uint64_t>(num_bytes)); 490*4f0d1de6SSteve Foreman 491*4f0d1de6SSteve Foreman std::vector<uint8_t> bytes; 492*4f0d1de6SSteve Foreman 493*4f0d1de6SSteve Foreman try 494*4f0d1de6SSteve Foreman { 495*4f0d1de6SSteve Foreman bus.call(method).read(bytes); 496*4f0d1de6SSteve Foreman } 497*4f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 498*4f0d1de6SSteve Foreman { 499*4f0d1de6SSteve Foreman log<level::ERR>("Failed to call Read on com.google.custom_accel", 500*4f0d1de6SSteve Foreman entry("WHAT=%s", ex.what()), 501*4f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 502*4f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 503*4f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 504*4f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), 505*4f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 506*4f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes)); 507*4f0d1de6SSteve Foreman throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 508*4f0d1de6SSteve Foreman } 509*4f0d1de6SSteve Foreman 510*4f0d1de6SSteve Foreman if (bytes.size() < num_bytes) 511*4f0d1de6SSteve Foreman { 512*4f0d1de6SSteve Foreman log<level::ERR>( 513*4f0d1de6SSteve Foreman "Call to Read on com.google.custom_accel didn't return the expected" 514*4f0d1de6SSteve Foreman " number of bytes.", 515*4f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 516*4f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 517*4f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 518*4f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), 519*4f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 520*4f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 521*4f0d1de6SSteve Foreman entry("DBUS_RETURN_SIZE=%zu", bytes.size())); 522*4f0d1de6SSteve Foreman throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 523*4f0d1de6SSteve Foreman } 524*4f0d1de6SSteve Foreman 525*4f0d1de6SSteve Foreman if (bytes.size() > sizeof(uint64_t)) 526*4f0d1de6SSteve Foreman { 527*4f0d1de6SSteve Foreman log<level::ERR>( 528*4f0d1de6SSteve Foreman "Call to Read on com.google.custom_accel returned more than 8B.", 529*4f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 530*4f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 531*4f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 532*4f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), 533*4f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 534*4f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 535*4f0d1de6SSteve Foreman entry("DBUS_RETURN_SIZE=%zu", bytes.size())); 536*4f0d1de6SSteve Foreman throw IpmiException(IPMI_CC_REQ_DATA_TRUNCATED); 537*4f0d1de6SSteve Foreman } 538*4f0d1de6SSteve Foreman 539*4f0d1de6SSteve Foreman uint64_t data = 0; 540*4f0d1de6SSteve Foreman for (size_t i = 0; i < num_bytes; ++i) 541*4f0d1de6SSteve Foreman { 542*4f0d1de6SSteve Foreman data = (data << 8) | bytes[i]; 543*4f0d1de6SSteve Foreman } 544*4f0d1de6SSteve Foreman 545*4f0d1de6SSteve Foreman return data; 546*4f0d1de6SSteve Foreman } 547*4f0d1de6SSteve Foreman 548*4f0d1de6SSteve Foreman void Handler::accelOobWrite(std::string_view name, uint64_t address, 549*4f0d1de6SSteve Foreman uint8_t num_bytes, uint64_t data) const 550*4f0d1de6SSteve Foreman { 551*4f0d1de6SSteve Foreman static constexpr std::string_view ACCEL_OOB_METHOD = "Write"; 552*4f0d1de6SSteve Foreman 553*4f0d1de6SSteve Foreman std::string object_name(ACCEL_OOB_ROOT); 554*4f0d1de6SSteve Foreman object_name.append(name); 555*4f0d1de6SSteve Foreman 556*4f0d1de6SSteve Foreman if (num_bytes > sizeof(data)) 557*4f0d1de6SSteve Foreman { 558*4f0d1de6SSteve Foreman log<level::ERR>( 559*4f0d1de6SSteve Foreman "Call to Write on com.google.custom_accel requested more than 8B.", 560*4f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 561*4f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 562*4f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 563*4f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()), 564*4f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 565*4f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 566*4f0d1de6SSteve Foreman entry("DBUS_ARG_DATA=%016llx", data)); 567*4f0d1de6SSteve Foreman throw IpmiException(IPMI_CC_PARM_OUT_OF_RANGE); 568*4f0d1de6SSteve Foreman } 569*4f0d1de6SSteve Foreman 570*4f0d1de6SSteve Foreman std::vector<uint8_t> bytes; 571*4f0d1de6SSteve Foreman bytes.reserve(num_bytes); 572*4f0d1de6SSteve Foreman for (size_t i = 0; i < num_bytes; ++i) 573*4f0d1de6SSteve Foreman { 574*4f0d1de6SSteve Foreman bytes.emplace_back(data & 0xff); 575*4f0d1de6SSteve Foreman data >>= 8; 576*4f0d1de6SSteve Foreman } 577*4f0d1de6SSteve Foreman 578*4f0d1de6SSteve Foreman try 579*4f0d1de6SSteve Foreman { 580*4f0d1de6SSteve Foreman auto bus = accelOobGetDbus(); 581*4f0d1de6SSteve Foreman auto method = 582*4f0d1de6SSteve Foreman bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(), 583*4f0d1de6SSteve Foreman ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD.data()); 584*4f0d1de6SSteve Foreman method.append(address, bytes); 585*4f0d1de6SSteve Foreman bus.call_noreply(method); 586*4f0d1de6SSteve Foreman } 587*4f0d1de6SSteve Foreman catch (const sdbusplus::exception::SdBusError& ex) 588*4f0d1de6SSteve Foreman { 589*4f0d1de6SSteve Foreman log<level::ERR>("Failed to call Write on com.google.custom_accel", 590*4f0d1de6SSteve Foreman entry("WHAT=%s", ex.what()), 591*4f0d1de6SSteve Foreman entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), 592*4f0d1de6SSteve Foreman entry("DBUS_OBJECT=%s", object_name.c_str()), 593*4f0d1de6SSteve Foreman entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), 594*4f0d1de6SSteve Foreman entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()), 595*4f0d1de6SSteve Foreman entry("DBUS_ARG_ADDRESS=%016llx", address), 596*4f0d1de6SSteve Foreman entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), 597*4f0d1de6SSteve Foreman entry("DBUS_ARG_DATA=%016llx", data)); 598*4f0d1de6SSteve Foreman throw IpmiException(IPMI_CC_UNSPECIFIED_ERROR); 599*4f0d1de6SSteve Foreman } 600*4f0d1de6SSteve Foreman } 601*4f0d1de6SSteve Foreman 602f085d91dSPatrick Venture } // namespace ipmi 603f085d91dSPatrick Venture } // namespace google 604