Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 1 | // Copyright 2022 Google LLC |
Willy Tu | a2056e9 | 2021-10-10 13:36:16 -0700 | [diff] [blame] | 2 | // |
| 3 | // Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | // you may not use this file except in compliance with the License. |
| 5 | // You may obtain a copy of the License at |
| 6 | // |
| 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | // |
| 9 | // Unless required by applicable law or agreed to in writing, software |
| 10 | // distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | // See the License for the specific language governing permissions and |
| 13 | // limitations under the License. |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 14 | |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 15 | #include "handler.hpp" |
| 16 | |
Patrick Williams | 444b5ea | 2023-05-19 13:56:42 -0500 | [diff] [blame] | 17 | #include "bm_config.h" |
| 18 | |
Brandon Kim | 559cb01 | 2024-05-03 09:12:07 +0000 | [diff] [blame] | 19 | #include "bm_instance.hpp" |
Nikhil Namjoshi | 8ec4106 | 2022-10-24 21:07:25 +0000 | [diff] [blame] | 20 | #include "bmc_mode_enum.hpp" |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 21 | #include "errors.hpp" |
Patrick Venture | c87de55 | 2020-05-20 20:25:39 -0700 | [diff] [blame] | 22 | #include "handler_impl.hpp" |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 23 | #include "util.hpp" |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 24 | |
Willy Tu | 3b1b427 | 2021-03-02 17:58:10 -0800 | [diff] [blame] | 25 | #include <fcntl.h> |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 26 | #include <ipmid/api.h> |
Willy Tu | 3b1b427 | 2021-03-02 17:58:10 -0800 | [diff] [blame] | 27 | #include <mtd/mtd-abi.h> |
| 28 | #include <mtd/mtd-user.h> |
| 29 | #include <sys/ioctl.h> |
| 30 | #include <unistd.h> |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 31 | |
Patrick Williams | 444b5ea | 2023-05-19 13:56:42 -0500 | [diff] [blame] | 32 | #include <nlohmann/json.hpp> |
| 33 | #include <phosphor-logging/elog-errors.hpp> |
| 34 | #include <phosphor-logging/log.hpp> |
| 35 | #include <sdbusplus/bus.hpp> |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 36 | #include <stdplus/print.hpp> |
Patrick Williams | 444b5ea | 2023-05-19 13:56:42 -0500 | [diff] [blame] | 37 | #include <xyz/openbmc_project/Common/error.hpp> |
| 38 | |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 39 | #include <cinttypes> |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 40 | #include <cstdio> |
| 41 | #include <filesystem> |
| 42 | #include <fstream> |
Brandon Kim | 559cb01 | 2024-05-03 09:12:07 +0000 | [diff] [blame] | 43 | #include <iomanip> |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 44 | #include <map> |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 45 | #include <sstream> |
| 46 | #include <string> |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 47 | #include <string_view> |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 48 | #include <tuple> |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 49 | #include <variant> |
Nikhil Namjoshi | 5e70dc8 | 2022-09-16 00:36:07 +0000 | [diff] [blame] | 50 | |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 51 | #ifndef NCSI_IF_NAME |
| 52 | #define NCSI_IF_NAME eth0 |
| 53 | #endif |
| 54 | |
| 55 | // To deal with receiving a string without quotes. |
| 56 | #define QUOTE(name) #name |
| 57 | #define STR(macro) QUOTE(macro) |
| 58 | #define NCSI_IF_NAME_STR STR(NCSI_IF_NAME) |
| 59 | |
William A. Kennington III | 8d3d46a | 2021-07-13 12:35:35 -0700 | [diff] [blame] | 60 | namespace ipmi |
| 61 | { |
| 62 | std::uint8_t getChannelByName(const std::string& chName); |
| 63 | } |
| 64 | |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 65 | namespace google |
| 66 | { |
| 67 | namespace ipmi |
| 68 | { |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 69 | using Json = nlohmann::json; |
| 70 | using namespace phosphor::logging; |
| 71 | using InternalFailure = |
| 72 | sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure; |
Gaurav Gandhi | d455bfd | 2024-01-29 22:32:27 -0800 | [diff] [blame] | 73 | using Value = std::variant<double>; |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 74 | |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 75 | uint8_t isBmcInBareMetalMode(const std::unique_ptr<FileSystemInterface>& fs) |
Nikhil Namjoshi | 5e70dc8 | 2022-09-16 00:36:07 +0000 | [diff] [blame] | 76 | { |
| 77 | #if BARE_METAL |
| 78 | return static_cast<uint8_t>(BmcMode::BM_MODE); |
| 79 | #else |
Brandon Kim | 3f3ca03 | 2023-03-17 18:49:00 +0000 | [diff] [blame] | 80 | std::error_code ec; |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 81 | |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 82 | if (fs->exists(bmDriveCleaningDoneAckFlagPath, ec)) |
Brandon Kim | 3f3ca03 | 2023-03-17 18:49:00 +0000 | [diff] [blame] | 83 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 84 | stdplus::print( |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 85 | stderr, |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 86 | "{} exists so we acked cleaning done and must be in BM mode\n", |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 87 | bmDriveCleaningDoneAckFlagPath); |
Brandon Kim | 3f3ca03 | 2023-03-17 18:49:00 +0000 | [diff] [blame] | 88 | return static_cast<uint8_t>(BmcMode::BM_MODE); |
| 89 | } |
| 90 | |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 91 | if (fs->exists(bmDriveCleaningDoneFlagPath, ec)) |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 92 | { |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 93 | fs->rename(bmDriveCleaningDoneFlagPath, bmDriveCleaningDoneAckFlagPath, |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 94 | ec); |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 95 | stdplus::print( |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 96 | stderr, |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 97 | "{} exists so we just finished cleaning and must be in BM mode\n", |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 98 | bmDriveCleaningDoneFlagPath); |
| 99 | return static_cast<uint8_t>(BmcMode::BM_MODE); |
| 100 | } |
| 101 | |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 102 | if (fs->exists(BM_SIGNAL_PATH, ec)) |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 103 | { |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 104 | if (!fs->exists(bmDriveCleaningFlagPath, ec)) |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 105 | { |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 106 | fs->create(bmDriveCleaningFlagPath); |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 107 | } |
| 108 | |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 109 | stdplus::print( |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 110 | stderr, |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 111 | "{} exists and no done/ack flag, we must be in BM cleaning mode\n", |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 112 | BM_SIGNAL_PATH); |
| 113 | return static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE); |
| 114 | } |
| 115 | |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 116 | stdplus::print( |
Hao Zhou | 4baf41c | 2023-07-06 22:34:04 +0000 | [diff] [blame] | 117 | stderr, |
| 118 | "Unable to find any BM state files so we must not be in BM mode\n"); |
Nikhil Namjoshi | 5e70dc8 | 2022-09-16 00:36:07 +0000 | [diff] [blame] | 119 | return static_cast<uint8_t>(BmcMode::NON_BM_MODE); |
| 120 | #endif |
| 121 | } |
| 122 | |
| 123 | uint8_t Handler::getBmcMode() |
| 124 | { |
| 125 | // BM_CLEANING_MODE is not implemented yet |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 126 | return isBmcInBareMetalMode(this->getFs()); |
Nikhil Namjoshi | 5e70dc8 | 2022-09-16 00:36:07 +0000 | [diff] [blame] | 127 | } |
| 128 | |
William A. Kennington III | b69209b | 2021-07-13 13:22:24 -0700 | [diff] [blame] | 129 | std::tuple<std::uint8_t, std::string> |
| 130 | Handler::getEthDetails(std::string intf) const |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 131 | { |
William A. Kennington III | b69209b | 2021-07-13 13:22:24 -0700 | [diff] [blame] | 132 | if (intf.empty()) |
| 133 | { |
| 134 | intf = NCSI_IF_NAME_STR; |
| 135 | } |
| 136 | return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf)); |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 137 | } |
| 138 | |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 139 | std::int64_t Handler::getRxPackets(const std::string& name) const |
| 140 | { |
| 141 | std::ostringstream opath; |
| 142 | opath << "/sys/class/net/" << name << "/statistics/rx_packets"; |
| 143 | std::string path = opath.str(); |
| 144 | |
| 145 | // Minor sanity & security check (of course, I'm less certain if unicode |
| 146 | // comes into play here. |
| 147 | // |
| 148 | // Basically you can't easily inject ../ or /../ into the path below. |
| 149 | if (name.find("/") != std::string::npos) |
| 150 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 151 | stdplus::print(stderr, "Invalid or illegal name: '{}'\n", name); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 152 | throw IpmiException(::ipmi::ccInvalidFieldRequest); |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | std::error_code ec; |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 156 | if (!this->getFs()->exists(path, ec)) |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 157 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 158 | stdplus::print(stderr, "Path: '{}' doesn't exist.\n", path); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 159 | throw IpmiException(::ipmi::ccInvalidFieldRequest); |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 160 | } |
| 161 | // We're uninterested in the state of ec. |
| 162 | |
| 163 | int64_t count = 0; |
| 164 | std::ifstream ifs; |
| 165 | ifs.exceptions(std::ifstream::failbit); |
| 166 | try |
| 167 | { |
| 168 | ifs.open(path); |
| 169 | ifs >> count; |
| 170 | } |
| 171 | catch (std::ios_base::failure& fail) |
| 172 | { |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 173 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Patrick Venture | d2037c6 | 2019-03-15 10:29:47 -0700 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | return count; |
| 177 | } |
| 178 | |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 179 | VersionTuple Handler::getCpldVersion(unsigned int id) const |
| 180 | { |
| 181 | std::ostringstream opath; |
| 182 | opath << "/run/cpld" << id << ".version"; |
| 183 | // Check for file |
| 184 | |
| 185 | std::error_code ec; |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 186 | if (!this->getFs()->exists(opath.str(), ec)) |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 187 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 188 | stdplus::print(stderr, "Path: '{}' doesn't exist.\n", opath.str()); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 189 | throw IpmiException(::ipmi::ccInvalidFieldRequest); |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 190 | } |
| 191 | // We're uninterested in the state of ec. |
| 192 | |
| 193 | // If file exists, read. |
| 194 | std::ifstream ifs; |
| 195 | ifs.exceptions(std::ifstream::failbit); |
| 196 | std::string value; |
| 197 | try |
| 198 | { |
| 199 | ifs.open(opath.str()); |
| 200 | ifs >> value; |
| 201 | } |
| 202 | catch (std::ios_base::failure& fail) |
| 203 | { |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 204 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | // If value parses as expected, return version. |
| 208 | VersionTuple version = std::make_tuple(0, 0, 0, 0); |
| 209 | |
Patrick Williams | 8c0094e | 2024-08-16 15:22:37 -0400 | [diff] [blame] | 210 | int num_fields = |
| 211 | std::sscanf(value.c_str(), "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8, |
| 212 | &std::get<0>(version), &std::get<1>(version), |
| 213 | &std::get<2>(version), &std::get<3>(version)); |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 214 | if (num_fields == 0) |
| 215 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 216 | stdplus::print(stderr, "Invalid version.\n"); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 217 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Patrick Venture | bb90d4f | 2019-03-15 13:42:06 -0700 | [diff] [blame] | 218 | } |
| 219 | |
| 220 | return version; |
| 221 | } |
| 222 | |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 223 | static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay"; |
| 224 | static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1"; |
| 225 | static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1"; |
| 226 | static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager"; |
| 227 | static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target"; |
| 228 | |
| 229 | void Handler::psuResetDelay(std::uint32_t delay) const |
| 230 | { |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 231 | std::ofstream ofs; |
| 232 | ofs.open(TIME_DELAY_FILENAME, std::ofstream::out); |
| 233 | if (!ofs.good()) |
| 234 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 235 | stdplus::print(stderr, "Unable to open file for output.\n"); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 236 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 237 | } |
| 238 | |
| 239 | ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl; |
| 240 | if (ofs.fail()) |
| 241 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 242 | stdplus::print(stderr, "Write failed\n"); |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 243 | ofs.close(); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 244 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 245 | } |
| 246 | |
| 247 | // Write succeeded, please continue. |
| 248 | ofs.flush(); |
| 249 | ofs.close(); |
| 250 | |
| 251 | auto bus = sdbusplus::bus::new_default(); |
| 252 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, |
| 253 | SYSTEMD_INTERFACE, "StartUnit"); |
| 254 | |
| 255 | method.append(PSU_HARDRESET_TARGET); |
| 256 | method.append("replace"); |
| 257 | |
| 258 | try |
| 259 | { |
| 260 | bus.call_noreply(method); |
| 261 | } |
| 262 | catch (const sdbusplus::exception::SdBusError& ex) |
| 263 | { |
| 264 | log<level::ERR>("Failed to call PSU hard reset"); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 265 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Patrick Venture | aa37412 | 2019-03-15 15:09:10 -0700 | [diff] [blame] | 266 | } |
| 267 | } |
| 268 | |
Shounak Mitra | ac4a16f | 2021-02-02 11:11:44 -0800 | [diff] [blame] | 269 | static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5"; |
| 270 | |
| 271 | void Handler::psuResetOnShutdown() const |
| 272 | { |
| 273 | std::ofstream ofs; |
| 274 | ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out); |
| 275 | if (!ofs.good()) |
| 276 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 277 | stdplus::print(stderr, "Unable to open file for output.\n"); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 278 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Shounak Mitra | ac4a16f | 2021-02-02 11:11:44 -0800 | [diff] [blame] | 279 | } |
| 280 | ofs.close(); |
| 281 | } |
| 282 | |
Willy Tu | 3b1b427 | 2021-03-02 17:58:10 -0800 | [diff] [blame] | 283 | uint32_t Handler::getFlashSize() |
| 284 | { |
| 285 | mtd_info_t info; |
| 286 | int fd = open("/dev/mtd0", O_RDONLY); |
| 287 | int err = ioctl(fd, MEMGETINFO, &info); |
| 288 | close(fd); |
| 289 | |
| 290 | if (err) |
| 291 | { |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 292 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Willy Tu | 3b1b427 | 2021-03-02 17:58:10 -0800 | [diff] [blame] | 293 | } |
| 294 | return info.size; |
| 295 | } |
| 296 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 297 | std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance) |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 298 | { |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 299 | // Check if we support this Entity ID. |
| 300 | auto it = _entityIdToName.find(id); |
| 301 | if (it == _entityIdToName.end()) |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 302 | { |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 303 | log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id)); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 304 | throw IpmiException(::ipmi::ccInvalidFieldRequest); |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 305 | } |
| 306 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 307 | std::string entityName; |
| 308 | try |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 309 | { |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 310 | // Parse the JSON config file. |
| 311 | if (!_entityConfigParsed) |
| 312 | { |
| 313 | _entityConfig = parseConfig(_configFile); |
| 314 | _entityConfigParsed = true; |
| 315 | } |
| 316 | |
| 317 | // Find the "entity id:entity instance" mapping to entity name. |
| 318 | entityName = readNameFromConfig(it->second, instance, _entityConfig); |
| 319 | if (entityName.empty()) |
| 320 | { |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 321 | throw IpmiException(::ipmi::ccInvalidFieldRequest); |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 322 | } |
| 323 | } |
| 324 | catch (InternalFailure& e) |
| 325 | { |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 326 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 327 | } |
| 328 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 329 | return entityName; |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 330 | } |
| 331 | |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 332 | std::string Handler::getMachineName() |
| 333 | { |
| 334 | const char* path = "/etc/os-release"; |
| 335 | std::ifstream ifs(path); |
| 336 | if (ifs.fail()) |
| 337 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 338 | stdplus::print(stderr, "Failed to open: {}\n", path); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 339 | throw IpmiException(::ipmi::ccUnspecifiedError); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 340 | } |
| 341 | |
| 342 | std::string line; |
| 343 | while (true) |
| 344 | { |
| 345 | std::getline(ifs, line); |
| 346 | if (ifs.eof()) |
| 347 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 348 | stdplus::print(stderr, |
| 349 | "Failed to find OPENBMC_TARGET_MACHINE: {}\n", path); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 350 | throw IpmiException(::ipmi::ccInvalidCommand); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 351 | } |
| 352 | if (ifs.fail()) |
| 353 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 354 | stdplus::print(stderr, "Failed to read: {}\n", path); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 355 | throw IpmiException(::ipmi::ccUnspecifiedError); |
William A. Kennington III | 29f35bc | 2020-11-03 23:30:31 -0800 | [diff] [blame] | 356 | } |
| 357 | std::string_view lineView(line); |
| 358 | constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE="; |
| 359 | if (lineView.substr(0, prefix.size()) != prefix) |
| 360 | { |
| 361 | continue; |
| 362 | } |
| 363 | lineView.remove_prefix(prefix.size()); |
| 364 | lineView.remove_prefix( |
| 365 | std::min(lineView.find_first_not_of('"'), lineView.size())); |
| 366 | lineView.remove_suffix( |
| 367 | lineView.size() - 1 - |
| 368 | std::min(lineView.find_last_not_of('"'), lineView.size() - 1)); |
| 369 | return std::string(lineView); |
| 370 | } |
| 371 | } |
| 372 | |
linyuny | 8cfa4c4 | 2021-06-16 13:53:08 -0700 | [diff] [blame] | 373 | static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay"; |
| 374 | static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target"; |
| 375 | |
| 376 | void Handler::hostPowerOffDelay(std::uint32_t delay) const |
| 377 | { |
| 378 | // Set time delay |
| 379 | std::ofstream ofs; |
| 380 | ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out); |
| 381 | if (!ofs.good()) |
| 382 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 383 | stdplus::print(stderr, "Unable to open file for output.\n"); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 384 | throw IpmiException(::ipmi::ccUnspecifiedError); |
linyuny | 8cfa4c4 | 2021-06-16 13:53:08 -0700 | [diff] [blame] | 385 | } |
| 386 | |
| 387 | ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl; |
| 388 | ofs.close(); |
| 389 | if (ofs.fail()) |
| 390 | { |
Michael Shen | 8d61853 | 2023-10-25 09:14:07 +0000 | [diff] [blame] | 391 | stdplus::print(stderr, "Write failed\n"); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 392 | throw IpmiException(::ipmi::ccUnspecifiedError); |
linyuny | 8cfa4c4 | 2021-06-16 13:53:08 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | // Write succeeded, please continue. |
| 396 | auto bus = sdbusplus::bus::new_default(); |
| 397 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, |
| 398 | SYSTEMD_INTERFACE, "StartUnit"); |
| 399 | |
| 400 | method.append(HOST_POWEROFF_TARGET); |
| 401 | method.append("replace"); |
| 402 | |
| 403 | try |
| 404 | { |
| 405 | bus.call_noreply(method); |
| 406 | } |
| 407 | catch (const sdbusplus::exception::SdBusError& ex) |
| 408 | { |
| 409 | log<level::ERR>("Failed to call Power Off", |
| 410 | entry("WHAT=%s", ex.what())); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 411 | throw IpmiException(::ipmi::ccUnspecifiedError); |
linyuny | 8cfa4c4 | 2021-06-16 13:53:08 -0700 | [diff] [blame] | 412 | } |
| 413 | } |
| 414 | |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 415 | std::string readNameFromConfig(const std::string& type, uint8_t instance, |
| 416 | const Json& config) |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 417 | { |
| 418 | static const std::vector<Json> empty{}; |
| 419 | std::vector<Json> readings = config.value(type, empty); |
| 420 | std::string name = ""; |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 421 | |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 422 | for (const auto& j : readings) |
| 423 | { |
| 424 | uint8_t instanceNum = j.value("instance", 0); |
| 425 | // Not the instance we're interested in |
| 426 | if (instanceNum != instance) |
| 427 | { |
| 428 | continue; |
| 429 | } |
| 430 | |
| 431 | // Found the instance we're interested in |
| 432 | name = j.value("name", ""); |
| 433 | |
| 434 | break; |
| 435 | } |
Patrick Venture | ab65000 | 2019-03-16 09:08:47 -0700 | [diff] [blame] | 436 | |
Patrick Venture | 07f8515 | 2019-03-15 21:36:56 -0700 | [diff] [blame] | 437 | return name; |
| 438 | } |
| 439 | |
Patrick Venture | 49f23ad | 2019-03-16 11:59:55 -0700 | [diff] [blame] | 440 | void Handler::buildI2cPcieMapping() |
| 441 | { |
| 442 | _pcie_i2c_map = buildPcieMap(); |
| 443 | } |
| 444 | |
| 445 | size_t Handler::getI2cPcieMappingSize() const |
| 446 | { |
| 447 | return _pcie_i2c_map.size(); |
| 448 | } |
| 449 | |
| 450 | std::tuple<std::uint32_t, std::string> |
| 451 | Handler::getI2cEntry(unsigned int entry) const |
| 452 | { |
| 453 | return _pcie_i2c_map[entry]; |
| 454 | } |
| 455 | |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 456 | namespace |
| 457 | { |
| 458 | |
| 459 | static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/"; |
| 460 | static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel"; |
| 461 | static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR"; |
| 462 | |
| 463 | // C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects() |
| 464 | using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>; |
| 465 | using AnyTypeList = std::vector<std::pair<std::string, AnyType>>; |
| 466 | using NamedArrayOfAnyTypeLists = |
| 467 | std::vector<std::pair<std::string, AnyTypeList>>; |
| 468 | using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector< |
| 469 | std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>; |
| 470 | |
| 471 | } // namespace |
| 472 | |
Patrick Williams | 6537122 | 2023-05-19 02:31:40 -0500 | [diff] [blame] | 473 | sdbusplus::bus_t Handler::getDbus() const |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 474 | { |
| 475 | return sdbusplus::bus::new_default(); |
| 476 | } |
| 477 | |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 478 | const std::unique_ptr<FileSystemInterface>& Handler::getFs() const |
| 479 | { |
| 480 | return this->fsPtr; |
| 481 | } |
| 482 | |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 483 | uint32_t Handler::accelOobDeviceCount() const |
| 484 | { |
| 485 | ArrayOfObjectPathsAndTieredAnyTypeLists data; |
| 486 | |
| 487 | try |
| 488 | { |
Michael Shen | 0e928ac | 2022-06-20 05:21:52 +0000 | [diff] [blame] | 489 | auto bus = getDbus(); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 490 | auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/", |
| 491 | "org.freedesktop.DBus.ObjectManager", |
| 492 | "GetManagedObjects"); |
| 493 | bus.call(method).read(data); |
| 494 | } |
| 495 | catch (const sdbusplus::exception::SdBusError& ex) |
| 496 | { |
| 497 | log<level::ERR>( |
| 498 | "Failed to call GetManagedObjects on com.google.custom_accel", |
| 499 | entry("WHAT=%s", ex.what())); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 500 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 501 | } |
| 502 | |
| 503 | return data.size(); |
| 504 | } |
| 505 | |
| 506 | std::string Handler::accelOobDeviceName(size_t index) const |
| 507 | { |
| 508 | ArrayOfObjectPathsAndTieredAnyTypeLists data; |
| 509 | |
| 510 | try |
| 511 | { |
Michael Shen | 0e928ac | 2022-06-20 05:21:52 +0000 | [diff] [blame] | 512 | auto bus = getDbus(); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 513 | auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/", |
| 514 | "org.freedesktop.DBus.ObjectManager", |
| 515 | "GetManagedObjects"); |
| 516 | bus.call(method).read(data); |
| 517 | } |
| 518 | catch (const sdbusplus::exception::SdBusError& ex) |
| 519 | { |
| 520 | log<level::ERR>( |
| 521 | "Failed to call GetManagedObjects on com.google.custom_accel", |
| 522 | entry("WHAT=%s", ex.what())); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 523 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 524 | } |
| 525 | |
| 526 | if (index >= data.size()) |
| 527 | { |
| 528 | log<level::WARNING>( |
| 529 | "Requested index is larger than the number of entries.", |
| 530 | entry("INDEX=%zu", index), entry("NUM_NAMES=%zu", data.size())); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 531 | throw IpmiException(::ipmi::ccParmOutOfRange); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | std::string_view name(data[index].first.str); |
| 535 | if (!name.starts_with(ACCEL_OOB_ROOT)) |
| 536 | { |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 537 | throw IpmiException(::ipmi::ccInvalidCommand); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 538 | } |
| 539 | name.remove_prefix(ACCEL_OOB_ROOT.length()); |
| 540 | return std::string(name); |
| 541 | } |
| 542 | |
| 543 | uint64_t Handler::accelOobRead(std::string_view name, uint64_t address, |
| 544 | uint8_t num_bytes) const |
| 545 | { |
| 546 | static constexpr char ACCEL_OOB_METHOD[] = "Read"; |
| 547 | |
| 548 | std::string object_name(ACCEL_OOB_ROOT); |
| 549 | object_name.append(name); |
| 550 | |
Michael Shen | 0e928ac | 2022-06-20 05:21:52 +0000 | [diff] [blame] | 551 | auto bus = getDbus(); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 552 | auto method = bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(), |
| 553 | ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD); |
| 554 | method.append(address, static_cast<uint64_t>(num_bytes)); |
| 555 | |
| 556 | std::vector<uint8_t> bytes; |
| 557 | |
| 558 | try |
| 559 | { |
| 560 | bus.call(method).read(bytes); |
| 561 | } |
| 562 | catch (const sdbusplus::exception::SdBusError& ex) |
| 563 | { |
| 564 | log<level::ERR>("Failed to call Read on com.google.custom_accel", |
| 565 | entry("WHAT=%s", ex.what()), |
| 566 | entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), |
| 567 | entry("DBUS_OBJECT=%s", object_name.c_str()), |
| 568 | entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), |
| 569 | entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), |
| 570 | entry("DBUS_ARG_ADDRESS=%016llx", address), |
| 571 | entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes)); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 572 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 573 | } |
| 574 | |
| 575 | if (bytes.size() < num_bytes) |
| 576 | { |
| 577 | log<level::ERR>( |
| 578 | "Call to Read on com.google.custom_accel didn't return the expected" |
| 579 | " number of bytes.", |
| 580 | entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), |
| 581 | entry("DBUS_OBJECT=%s", object_name.c_str()), |
| 582 | entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), |
| 583 | entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), |
| 584 | entry("DBUS_ARG_ADDRESS=%016llx", address), |
| 585 | entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), |
| 586 | entry("DBUS_RETURN_SIZE=%zu", bytes.size())); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 587 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 588 | } |
| 589 | |
| 590 | if (bytes.size() > sizeof(uint64_t)) |
| 591 | { |
| 592 | log<level::ERR>( |
| 593 | "Call to Read on com.google.custom_accel returned more than 8B.", |
| 594 | entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), |
| 595 | entry("DBUS_OBJECT=%s", object_name.c_str()), |
| 596 | entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), |
| 597 | entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD), |
| 598 | entry("DBUS_ARG_ADDRESS=%016llx", address), |
| 599 | entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), |
| 600 | entry("DBUS_RETURN_SIZE=%zu", bytes.size())); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 601 | throw IpmiException(::ipmi::ccReqDataTruncated); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | uint64_t data = 0; |
| 605 | for (size_t i = 0; i < num_bytes; ++i) |
| 606 | { |
| 607 | data = (data << 8) | bytes[i]; |
| 608 | } |
| 609 | |
| 610 | return data; |
| 611 | } |
| 612 | |
| 613 | void Handler::accelOobWrite(std::string_view name, uint64_t address, |
| 614 | uint8_t num_bytes, uint64_t data) const |
| 615 | { |
| 616 | static constexpr std::string_view ACCEL_OOB_METHOD = "Write"; |
| 617 | |
| 618 | std::string object_name(ACCEL_OOB_ROOT); |
| 619 | object_name.append(name); |
| 620 | |
| 621 | if (num_bytes > sizeof(data)) |
| 622 | { |
| 623 | log<level::ERR>( |
| 624 | "Call to Write on com.google.custom_accel requested more than 8B.", |
| 625 | entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), |
| 626 | entry("DBUS_OBJECT=%s", object_name.c_str()), |
| 627 | entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), |
| 628 | entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()), |
| 629 | entry("DBUS_ARG_ADDRESS=%016llx", address), |
| 630 | entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), |
| 631 | entry("DBUS_ARG_DATA=%016llx", data)); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 632 | throw IpmiException(::ipmi::ccParmOutOfRange); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 633 | } |
| 634 | |
| 635 | std::vector<uint8_t> bytes; |
| 636 | bytes.reserve(num_bytes); |
| 637 | for (size_t i = 0; i < num_bytes; ++i) |
| 638 | { |
| 639 | bytes.emplace_back(data & 0xff); |
| 640 | data >>= 8; |
| 641 | } |
| 642 | |
| 643 | try |
| 644 | { |
Michael Shen | 0e928ac | 2022-06-20 05:21:52 +0000 | [diff] [blame] | 645 | auto bus = getDbus(); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 646 | auto method = |
| 647 | bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(), |
| 648 | ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD.data()); |
| 649 | method.append(address, bytes); |
| 650 | bus.call_noreply(method); |
| 651 | } |
| 652 | catch (const sdbusplus::exception::SdBusError& ex) |
| 653 | { |
| 654 | log<level::ERR>("Failed to call Write on com.google.custom_accel", |
| 655 | entry("WHAT=%s", ex.what()), |
| 656 | entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE), |
| 657 | entry("DBUS_OBJECT=%s", object_name.c_str()), |
| 658 | entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE), |
| 659 | entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()), |
| 660 | entry("DBUS_ARG_ADDRESS=%016llx", address), |
| 661 | entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes), |
| 662 | entry("DBUS_ARG_DATA=%016llx", data)); |
Michael Shen | e5a0667 | 2022-06-20 05:08:32 +0000 | [diff] [blame] | 663 | throw IpmiException(::ipmi::ccUnspecifiedError); |
Steve Foreman | 4f0d1de | 2021-09-20 14:06:32 -0700 | [diff] [blame] | 664 | } |
| 665 | } |
| 666 | |
Willy Tu | 6c71b0f | 2021-10-10 13:34:41 -0700 | [diff] [blame] | 667 | std::vector<uint8_t> Handler::pcieBifurcation(uint8_t index) |
| 668 | { |
| 669 | return bifurcationHelper.get().getBifurcation(index).value_or( |
| 670 | std::vector<uint8_t>{}); |
| 671 | } |
| 672 | |
John Wedig | 378b59a | 2024-11-06 16:42:50 -0800 | [diff] [blame] | 673 | static constexpr auto BARE_METAL_TARGET = "gbmc-bare-metal-active@0.target"; |
John Wedig | a92d0e6 | 2023-06-29 10:43:47 -0700 | [diff] [blame] | 674 | |
| 675 | void Handler::linuxBootDone() const |
| 676 | { |
Hao Zhou | 15d4d21 | 2023-07-11 20:18:04 +0000 | [diff] [blame] | 677 | if (isBmcInBareMetalMode(this->fsPtr) != |
| 678 | static_cast<uint8_t>(BmcMode::BM_MODE)) |
John Wedig | a92d0e6 | 2023-06-29 10:43:47 -0700 | [diff] [blame] | 679 | { |
| 680 | return; |
| 681 | } |
| 682 | |
| 683 | log<level::INFO>("LinuxBootDone: Disabling IPMI"); |
| 684 | |
| 685 | // Start the bare metal active systemd target. |
| 686 | auto bus = sdbusplus::bus::new_default(); |
| 687 | auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT, |
| 688 | SYSTEMD_INTERFACE, "StartUnit"); |
| 689 | |
| 690 | method.append(BARE_METAL_TARGET); |
| 691 | method.append("replace"); |
| 692 | |
| 693 | try |
| 694 | { |
| 695 | bus.call_noreply(method); |
| 696 | } |
| 697 | catch (const sdbusplus::exception::SdBusError& ex) |
| 698 | { |
| 699 | log<level::ERR>("Failed to start bare metal active systemd target", |
| 700 | entry("WHAT=%s", ex.what())); |
| 701 | throw IpmiException(::ipmi::ccUnspecifiedError); |
| 702 | } |
| 703 | } |
| 704 | |
Gaurav Gandhi | d455bfd | 2024-01-29 22:32:27 -0800 | [diff] [blame] | 705 | static constexpr char ACCEL_POWER_SERVICE[] = "xyz.openbmc_project.AccelPower"; |
| 706 | static constexpr char ACCEL_POWER_PATH_PREFIX[] = |
| 707 | "/xyz/openbmc_project/control/accel_power_"; |
| 708 | static constexpr char POWER_MODE_IFC[] = |
| 709 | "xyz.openbmc_project.Control.Power.Mode"; |
| 710 | |
| 711 | void Handler::accelSetVrSettings(::ipmi::Context::ptr ctx, uint8_t chip_id, |
| 712 | uint8_t settings_id, uint16_t value) const |
| 713 | { |
| 714 | int vrSettingsReq; |
| 715 | boost::system::error_code ec; |
| 716 | if (_vrSettingsMap.find(settings_id) == _vrSettingsMap.end()) |
| 717 | { |
| 718 | log<level::ERR>("Settings ID is not supported", |
| 719 | entry("settings_id=%d", settings_id)); |
| 720 | throw IpmiException(::ipmi::ccParmOutOfRange); |
| 721 | } |
| 722 | |
| 723 | vrSettingsReq = static_cast<int>(settings_id | value << 8); |
| 724 | std::string object_name( |
| 725 | std::format("{}{}", ACCEL_POWER_PATH_PREFIX, chip_id)); |
| 726 | |
| 727 | std::variant<int> val = vrSettingsReq; |
Patrick Williams | 8c0094e | 2024-08-16 15:22:37 -0400 | [diff] [blame] | 728 | ctx->bus->yield_method_call( |
| 729 | ctx->yield, ec, ACCEL_POWER_SERVICE, object_name.c_str(), |
| 730 | "org.freedesktop.DBus.Properties", "Set", POWER_MODE_IFC, "PowerMode", |
| 731 | val); |
Gaurav Gandhi | d455bfd | 2024-01-29 22:32:27 -0800 | [diff] [blame] | 732 | if (ec) |
| 733 | { |
| 734 | log<level::ERR>("Failed to set PowerMode property"); |
| 735 | throw IpmiException(::ipmi::ccUnspecifiedError); |
| 736 | } |
| 737 | } |
| 738 | |
| 739 | static constexpr char EXTERNAL_SENSOR_SERVICE[] = |
| 740 | "xyz.openbmc_project.ExternalSensor"; |
| 741 | static constexpr char EXTERNAL_SENSOR_PATH_PREFIX[] = |
| 742 | "/xyz/openbmc_project/sensors/power/"; |
| 743 | static constexpr char SENSOR_VALUE_IFC[] = "xyz.openbmc_project.Sensor.Value"; |
| 744 | |
| 745 | uint16_t Handler::accelGetVrSettings(::ipmi::Context::ptr ctx, uint8_t chip_id, |
| 746 | uint8_t settings_id) const |
| 747 | { |
| 748 | Value value; |
| 749 | boost::system::error_code ec; |
Patrick Williams | 8c0094e | 2024-08-16 15:22:37 -0400 | [diff] [blame] | 750 | std::string object_name( |
| 751 | std::format("{}{}{}", EXTERNAL_SENSOR_PATH_PREFIX, |
| 752 | _vrSettingsMap.at(settings_id), chip_id)); |
Gaurav Gandhi | d455bfd | 2024-01-29 22:32:27 -0800 | [diff] [blame] | 753 | |
| 754 | if (_vrSettingsMap.find(settings_id) == _vrSettingsMap.end()) |
| 755 | { |
| 756 | log<level::ERR>("Settings ID is not supported", |
| 757 | entry("settings_id=%d", settings_id)); |
| 758 | throw IpmiException(::ipmi::ccParmOutOfRange); |
| 759 | } |
| 760 | |
| 761 | value = ctx->bus->yield_method_call<std::variant<double>>( |
| 762 | ctx->yield, ec, EXTERNAL_SENSOR_SERVICE, object_name.c_str(), |
| 763 | "org.freedesktop.DBus.Properties", "Get", SENSOR_VALUE_IFC, "Value"); |
| 764 | if (ec) |
| 765 | { |
| 766 | log<level::ERR>("accelGetVrSettings: Failed to call GetObject "); |
| 767 | throw IpmiException(::ipmi::ccUnspecifiedError); |
| 768 | } |
| 769 | |
| 770 | return static_cast<uint16_t>(std::get<double>(value)); |
| 771 | } |
Brandon Kim | 559cb01 | 2024-05-03 09:12:07 +0000 | [diff] [blame] | 772 | |
| 773 | std::string Handler::getBMInstanceProperty(uint8_t propertyType) const |
| 774 | { |
| 775 | std::string propertyTypeString; |
| 776 | if (auto it = bmInstanceTypeStringMap.find(propertyType); |
| 777 | it == bmInstanceTypeStringMap.end()) |
| 778 | { |
| 779 | stdplus::print(stderr, "PropertyType: '{}' is invalid.\n", |
| 780 | propertyType); |
| 781 | throw IpmiException(::ipmi::ccInvalidFieldRequest); |
| 782 | } |
| 783 | else |
| 784 | { |
| 785 | propertyTypeString = it->second; |
| 786 | } |
| 787 | std::string opath = std::format("/run/bm-instance/{}", propertyTypeString); |
| 788 | // Check for file |
| 789 | |
| 790 | std::error_code ec; |
| 791 | // TODO(brandonkim@google.com): Fix this to use stdplus::ManagedFd |
| 792 | if (!this->getFs()->exists(opath, ec)) |
| 793 | { |
| 794 | stdplus::print(stderr, "Path: '{}' doesn't exist.\n", opath); |
| 795 | throw IpmiException(::ipmi::ccInvalidFieldRequest); |
| 796 | } |
| 797 | |
Brandon Kim | 559cb01 | 2024-05-03 09:12:07 +0000 | [diff] [blame] | 798 | std::ifstream ifs; |
| 799 | ifs.exceptions(std::ifstream::failbit); |
| 800 | std::string property; |
| 801 | try |
| 802 | { |
| 803 | ifs.open(opath); |
Brandon Kim | 56b2d9f | 2024-05-30 20:54:33 +0000 | [diff] [blame] | 804 | std::getline(ifs, property); |
Brandon Kim | 559cb01 | 2024-05-03 09:12:07 +0000 | [diff] [blame] | 805 | } |
| 806 | catch (std::ios_base::failure& fail) |
| 807 | { |
| 808 | stdplus::print(stderr, "Failed to read: '{}'.\n", opath); |
| 809 | throw IpmiException(::ipmi::ccUnspecifiedError); |
| 810 | } |
| 811 | return property; |
| 812 | } |
| 813 | |
Patrick Venture | f085d91 | 2019-03-15 08:50:00 -0700 | [diff] [blame] | 814 | } // namespace ipmi |
| 815 | } // namespace google |