blob: c23fdc08330f2b411b170f0f8365251b0228ef74 [file] [log] [blame]
Steve Foreman4f0d1de2021-09-20 14:06:32 -07001// Copyright 2022 Google LLC
Willy Tua2056e92021-10-10 13:36:16 -07002//
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 Shen8d618532023-10-25 09:14:07 +000014
Patrick Venturef085d912019-03-15 08:50:00 -070015#include "handler.hpp"
16
Patrick Williams444b5ea2023-05-19 13:56:42 -050017#include "bm_config.h"
18
Brandon Kim559cb012024-05-03 09:12:07 +000019#include "bm_instance.hpp"
Nikhil Namjoshi8ec41062022-10-24 21:07:25 +000020#include "bmc_mode_enum.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070021#include "errors.hpp"
Patrick Venturec87de552020-05-20 20:25:39 -070022#include "handler_impl.hpp"
Patrick Ventureab650002019-03-16 09:08:47 -070023#include "util.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070024
Willy Tu3b1b4272021-03-02 17:58:10 -080025#include <fcntl.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070026#include <ipmid/api.h>
Willy Tu3b1b4272021-03-02 17:58:10 -080027#include <mtd/mtd-abi.h>
28#include <mtd/mtd-user.h>
29#include <sys/ioctl.h>
30#include <unistd.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070031
Patrick Williams444b5ea2023-05-19 13:56:42 -050032#include <nlohmann/json.hpp>
33#include <phosphor-logging/elog-errors.hpp>
34#include <phosphor-logging/log.hpp>
35#include <sdbusplus/bus.hpp>
Michael Shen8d618532023-10-25 09:14:07 +000036#include <stdplus/print.hpp>
Patrick Williams444b5ea2023-05-19 13:56:42 -050037#include <xyz/openbmc_project/Common/error.hpp>
38
Patrick Venturebb90d4f2019-03-15 13:42:06 -070039#include <cinttypes>
Patrick Ventured2037c62019-03-15 10:29:47 -070040#include <cstdio>
41#include <filesystem>
42#include <fstream>
Brandon Kim559cb012024-05-03 09:12:07 +000043#include <iomanip>
Patrick Venture07f85152019-03-15 21:36:56 -070044#include <map>
Patrick Ventured2037c62019-03-15 10:29:47 -070045#include <sstream>
46#include <string>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080047#include <string_view>
Patrick Ventured2037c62019-03-15 10:29:47 -070048#include <tuple>
Steve Foreman4f0d1de2021-09-20 14:06:32 -070049#include <variant>
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +000050
Patrick Venturef085d912019-03-15 08:50:00 -070051#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 III8d3d46a2021-07-13 12:35:35 -070060namespace ipmi
61{
62std::uint8_t getChannelByName(const std::string& chName);
63}
64
Patrick Venturef085d912019-03-15 08:50:00 -070065namespace google
66{
67namespace ipmi
68{
Patrick Venture07f85152019-03-15 21:36:56 -070069using Json = nlohmann::json;
70using namespace phosphor::logging;
71using InternalFailure =
72 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Gaurav Gandhid455bfd2024-01-29 22:32:27 -080073using Value = std::variant<double>;
Patrick Venturef085d912019-03-15 08:50:00 -070074
Hao Zhou15d4d212023-07-11 20:18:04 +000075uint8_t isBmcInBareMetalMode(const std::unique_ptr<FileSystemInterface>& fs)
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +000076{
77#if BARE_METAL
78 return static_cast<uint8_t>(BmcMode::BM_MODE);
79#else
Brandon Kim3f3ca032023-03-17 18:49:00 +000080 std::error_code ec;
Hao Zhou4baf41c2023-07-06 22:34:04 +000081
Hao Zhou15d4d212023-07-11 20:18:04 +000082 if (fs->exists(bmDriveCleaningDoneAckFlagPath, ec))
Brandon Kim3f3ca032023-03-17 18:49:00 +000083 {
Michael Shen8d618532023-10-25 09:14:07 +000084 stdplus::print(
Hao Zhou4baf41c2023-07-06 22:34:04 +000085 stderr,
Michael Shen8d618532023-10-25 09:14:07 +000086 "{} exists so we acked cleaning done and must be in BM mode\n",
Hao Zhou4baf41c2023-07-06 22:34:04 +000087 bmDriveCleaningDoneAckFlagPath);
Brandon Kim3f3ca032023-03-17 18:49:00 +000088 return static_cast<uint8_t>(BmcMode::BM_MODE);
89 }
90
Hao Zhou15d4d212023-07-11 20:18:04 +000091 if (fs->exists(bmDriveCleaningDoneFlagPath, ec))
Hao Zhou4baf41c2023-07-06 22:34:04 +000092 {
Hao Zhou15d4d212023-07-11 20:18:04 +000093 fs->rename(bmDriveCleaningDoneFlagPath, bmDriveCleaningDoneAckFlagPath,
Hao Zhou4baf41c2023-07-06 22:34:04 +000094 ec);
Michael Shen8d618532023-10-25 09:14:07 +000095 stdplus::print(
Hao Zhou4baf41c2023-07-06 22:34:04 +000096 stderr,
Michael Shen8d618532023-10-25 09:14:07 +000097 "{} exists so we just finished cleaning and must be in BM mode\n",
Hao Zhou4baf41c2023-07-06 22:34:04 +000098 bmDriveCleaningDoneFlagPath);
99 return static_cast<uint8_t>(BmcMode::BM_MODE);
100 }
101
Hao Zhou15d4d212023-07-11 20:18:04 +0000102 if (fs->exists(BM_SIGNAL_PATH, ec))
Hao Zhou4baf41c2023-07-06 22:34:04 +0000103 {
Hao Zhou15d4d212023-07-11 20:18:04 +0000104 if (!fs->exists(bmDriveCleaningFlagPath, ec))
Hao Zhou4baf41c2023-07-06 22:34:04 +0000105 {
Hao Zhou15d4d212023-07-11 20:18:04 +0000106 fs->create(bmDriveCleaningFlagPath);
Hao Zhou4baf41c2023-07-06 22:34:04 +0000107 }
108
Michael Shen8d618532023-10-25 09:14:07 +0000109 stdplus::print(
Hao Zhou4baf41c2023-07-06 22:34:04 +0000110 stderr,
Michael Shen8d618532023-10-25 09:14:07 +0000111 "{} exists and no done/ack flag, we must be in BM cleaning mode\n",
Hao Zhou4baf41c2023-07-06 22:34:04 +0000112 BM_SIGNAL_PATH);
113 return static_cast<uint8_t>(BmcMode::BM_CLEANING_MODE);
114 }
115
Michael Shen8d618532023-10-25 09:14:07 +0000116 stdplus::print(
Hao Zhou4baf41c2023-07-06 22:34:04 +0000117 stderr,
118 "Unable to find any BM state files so we must not be in BM mode\n");
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +0000119 return static_cast<uint8_t>(BmcMode::NON_BM_MODE);
120#endif
121}
122
123uint8_t Handler::getBmcMode()
124{
125 // BM_CLEANING_MODE is not implemented yet
Hao Zhou15d4d212023-07-11 20:18:04 +0000126 return isBmcInBareMetalMode(this->getFs());
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +0000127}
128
William A. Kennington IIIb69209b2021-07-13 13:22:24 -0700129std::tuple<std::uint8_t, std::string>
130 Handler::getEthDetails(std::string intf) const
Patrick Venturef085d912019-03-15 08:50:00 -0700131{
William A. Kennington IIIb69209b2021-07-13 13:22:24 -0700132 if (intf.empty())
133 {
134 intf = NCSI_IF_NAME_STR;
135 }
136 return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf));
Patrick Venturef085d912019-03-15 08:50:00 -0700137}
138
Patrick Ventured2037c62019-03-15 10:29:47 -0700139std::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 Shen8d618532023-10-25 09:14:07 +0000151 stdplus::print(stderr, "Invalid or illegal name: '{}'\n", name);
Michael Shene5a06672022-06-20 05:08:32 +0000152 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventured2037c62019-03-15 10:29:47 -0700153 }
154
155 std::error_code ec;
Hao Zhou15d4d212023-07-11 20:18:04 +0000156 if (!this->getFs()->exists(path, ec))
Patrick Ventured2037c62019-03-15 10:29:47 -0700157 {
Michael Shen8d618532023-10-25 09:14:07 +0000158 stdplus::print(stderr, "Path: '{}' doesn't exist.\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000159 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventured2037c62019-03-15 10:29:47 -0700160 }
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 Shene5a06672022-06-20 05:08:32 +0000173 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventured2037c62019-03-15 10:29:47 -0700174 }
175
176 return count;
177}
178
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700179VersionTuple 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 Zhou15d4d212023-07-11 20:18:04 +0000186 if (!this->getFs()->exists(opath.str(), ec))
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700187 {
Michael Shen8d618532023-10-25 09:14:07 +0000188 stdplus::print(stderr, "Path: '{}' doesn't exist.\n", opath.str());
Michael Shene5a06672022-06-20 05:08:32 +0000189 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700190 }
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 Shene5a06672022-06-20 05:08:32 +0000204 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700205 }
206
207 // If value parses as expected, return version.
208 VersionTuple version = std::make_tuple(0, 0, 0, 0);
209
Patrick Williams8c0094e2024-08-16 15:22:37 -0400210 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 Venturebb90d4f2019-03-15 13:42:06 -0700214 if (num_fields == 0)
215 {
Michael Shen8d618532023-10-25 09:14:07 +0000216 stdplus::print(stderr, "Invalid version.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000217 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700218 }
219
220 return version;
221}
222
Patrick Ventureaa374122019-03-15 15:09:10 -0700223static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
224static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
225static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
226static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
227static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
228
229void Handler::psuResetDelay(std::uint32_t delay) const
230{
Patrick Ventureaa374122019-03-15 15:09:10 -0700231 std::ofstream ofs;
232 ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
233 if (!ofs.good())
234 {
Michael Shen8d618532023-10-25 09:14:07 +0000235 stdplus::print(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000236 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700237 }
238
239 ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
240 if (ofs.fail())
241 {
Michael Shen8d618532023-10-25 09:14:07 +0000242 stdplus::print(stderr, "Write failed\n");
Patrick Ventureaa374122019-03-15 15:09:10 -0700243 ofs.close();
Michael Shene5a06672022-06-20 05:08:32 +0000244 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700245 }
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 Shene5a06672022-06-20 05:08:32 +0000265 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700266 }
267}
268
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800269static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5";
270
271void Handler::psuResetOnShutdown() const
272{
273 std::ofstream ofs;
274 ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out);
275 if (!ofs.good())
276 {
Michael Shen8d618532023-10-25 09:14:07 +0000277 stdplus::print(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000278 throw IpmiException(::ipmi::ccUnspecifiedError);
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800279 }
280 ofs.close();
281}
282
Willy Tu3b1b4272021-03-02 17:58:10 -0800283uint32_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 Shene5a06672022-06-20 05:08:32 +0000292 throw IpmiException(::ipmi::ccUnspecifiedError);
Willy Tu3b1b4272021-03-02 17:58:10 -0800293 }
294 return info.size;
295}
296
Patrick Ventureab650002019-03-16 09:08:47 -0700297std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
Patrick Venture07f85152019-03-15 21:36:56 -0700298{
Patrick Ventureab650002019-03-16 09:08:47 -0700299 // Check if we support this Entity ID.
300 auto it = _entityIdToName.find(id);
301 if (it == _entityIdToName.end())
Patrick Venture07f85152019-03-15 21:36:56 -0700302 {
Patrick Ventureab650002019-03-16 09:08:47 -0700303 log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
Michael Shene5a06672022-06-20 05:08:32 +0000304 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Venture07f85152019-03-15 21:36:56 -0700305 }
306
Patrick Ventureab650002019-03-16 09:08:47 -0700307 std::string entityName;
308 try
Patrick Venture07f85152019-03-15 21:36:56 -0700309 {
Patrick Ventureab650002019-03-16 09:08:47 -0700310 // 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 Shene5a06672022-06-20 05:08:32 +0000321 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventureab650002019-03-16 09:08:47 -0700322 }
323 }
324 catch (InternalFailure& e)
325 {
Michael Shene5a06672022-06-20 05:08:32 +0000326 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venture07f85152019-03-15 21:36:56 -0700327 }
328
Patrick Ventureab650002019-03-16 09:08:47 -0700329 return entityName;
Patrick Venture07f85152019-03-15 21:36:56 -0700330}
331
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800332std::string Handler::getMachineName()
333{
334 const char* path = "/etc/os-release";
335 std::ifstream ifs(path);
336 if (ifs.fail())
337 {
Michael Shen8d618532023-10-25 09:14:07 +0000338 stdplus::print(stderr, "Failed to open: {}\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000339 throw IpmiException(::ipmi::ccUnspecifiedError);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800340 }
341
342 std::string line;
343 while (true)
344 {
345 std::getline(ifs, line);
346 if (ifs.eof())
347 {
Michael Shen8d618532023-10-25 09:14:07 +0000348 stdplus::print(stderr,
349 "Failed to find OPENBMC_TARGET_MACHINE: {}\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000350 throw IpmiException(::ipmi::ccInvalidCommand);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800351 }
352 if (ifs.fail())
353 {
Michael Shen8d618532023-10-25 09:14:07 +0000354 stdplus::print(stderr, "Failed to read: {}\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000355 throw IpmiException(::ipmi::ccUnspecifiedError);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800356 }
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
linyuny8cfa4c42021-06-16 13:53:08 -0700373static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay";
374static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target";
375
376void 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 Shen8d618532023-10-25 09:14:07 +0000383 stdplus::print(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000384 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700385 }
386
387 ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl;
388 ofs.close();
389 if (ofs.fail())
390 {
Michael Shen8d618532023-10-25 09:14:07 +0000391 stdplus::print(stderr, "Write failed\n");
Michael Shene5a06672022-06-20 05:08:32 +0000392 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700393 }
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 Shene5a06672022-06-20 05:08:32 +0000411 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700412 }
413}
414
Patrick Ventureab650002019-03-16 09:08:47 -0700415std::string readNameFromConfig(const std::string& type, uint8_t instance,
416 const Json& config)
Patrick Venture07f85152019-03-15 21:36:56 -0700417{
418 static const std::vector<Json> empty{};
419 std::vector<Json> readings = config.value(type, empty);
420 std::string name = "";
Patrick Ventureab650002019-03-16 09:08:47 -0700421
Patrick Venture07f85152019-03-15 21:36:56 -0700422 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 Ventureab650002019-03-16 09:08:47 -0700436
Patrick Venture07f85152019-03-15 21:36:56 -0700437 return name;
438}
439
Patrick Venture49f23ad2019-03-16 11:59:55 -0700440void Handler::buildI2cPcieMapping()
441{
442 _pcie_i2c_map = buildPcieMap();
443}
444
445size_t Handler::getI2cPcieMappingSize() const
446{
447 return _pcie_i2c_map.size();
448}
449
450std::tuple<std::uint32_t, std::string>
451 Handler::getI2cEntry(unsigned int entry) const
452{
453 return _pcie_i2c_map[entry];
454}
455
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700456namespace
457{
458
459static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/";
460static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel";
461static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR";
462
463// C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects()
464using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>;
465using AnyTypeList = std::vector<std::pair<std::string, AnyType>>;
466using NamedArrayOfAnyTypeLists =
467 std::vector<std::pair<std::string, AnyTypeList>>;
468using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector<
469 std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>;
470
471} // namespace
472
Patrick Williams65371222023-05-19 02:31:40 -0500473sdbusplus::bus_t Handler::getDbus() const
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700474{
475 return sdbusplus::bus::new_default();
476}
477
Hao Zhou15d4d212023-07-11 20:18:04 +0000478const std::unique_ptr<FileSystemInterface>& Handler::getFs() const
479{
480 return this->fsPtr;
481}
482
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700483uint32_t Handler::accelOobDeviceCount() const
484{
485 ArrayOfObjectPathsAndTieredAnyTypeLists data;
486
487 try
488 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000489 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700490 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 Shene5a06672022-06-20 05:08:32 +0000500 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700501 }
502
503 return data.size();
504}
505
506std::string Handler::accelOobDeviceName(size_t index) const
507{
508 ArrayOfObjectPathsAndTieredAnyTypeLists data;
509
510 try
511 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000512 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700513 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 Shene5a06672022-06-20 05:08:32 +0000523 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700524 }
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 Shene5a06672022-06-20 05:08:32 +0000531 throw IpmiException(::ipmi::ccParmOutOfRange);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700532 }
533
534 std::string_view name(data[index].first.str);
535 if (!name.starts_with(ACCEL_OOB_ROOT))
536 {
Michael Shene5a06672022-06-20 05:08:32 +0000537 throw IpmiException(::ipmi::ccInvalidCommand);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700538 }
539 name.remove_prefix(ACCEL_OOB_ROOT.length());
540 return std::string(name);
541}
542
543uint64_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 Shen0e928ac2022-06-20 05:21:52 +0000551 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700552 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 Shene5a06672022-06-20 05:08:32 +0000572 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700573 }
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 Shene5a06672022-06-20 05:08:32 +0000587 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700588 }
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 Shene5a06672022-06-20 05:08:32 +0000601 throw IpmiException(::ipmi::ccReqDataTruncated);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700602 }
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
613void 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 Shene5a06672022-06-20 05:08:32 +0000632 throw IpmiException(::ipmi::ccParmOutOfRange);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700633 }
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 Shen0e928ac2022-06-20 05:21:52 +0000645 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700646 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 Shene5a06672022-06-20 05:08:32 +0000663 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700664 }
665}
666
Willy Tu6c71b0f2021-10-10 13:34:41 -0700667std::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 Wedig378b59a2024-11-06 16:42:50 -0800673static constexpr auto BARE_METAL_TARGET = "gbmc-bare-metal-active@0.target";
John Wediga92d0e62023-06-29 10:43:47 -0700674
675void Handler::linuxBootDone() const
676{
Hao Zhou15d4d212023-07-11 20:18:04 +0000677 if (isBmcInBareMetalMode(this->fsPtr) !=
678 static_cast<uint8_t>(BmcMode::BM_MODE))
John Wediga92d0e62023-06-29 10:43:47 -0700679 {
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 Gandhid455bfd2024-01-29 22:32:27 -0800705static constexpr char ACCEL_POWER_SERVICE[] = "xyz.openbmc_project.AccelPower";
706static constexpr char ACCEL_POWER_PATH_PREFIX[] =
707 "/xyz/openbmc_project/control/accel_power_";
708static constexpr char POWER_MODE_IFC[] =
709 "xyz.openbmc_project.Control.Power.Mode";
710
711void 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 Williams8c0094e2024-08-16 15:22:37 -0400728 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 Gandhid455bfd2024-01-29 22:32:27 -0800732 if (ec)
733 {
734 log<level::ERR>("Failed to set PowerMode property");
735 throw IpmiException(::ipmi::ccUnspecifiedError);
736 }
737}
738
739static constexpr char EXTERNAL_SENSOR_SERVICE[] =
740 "xyz.openbmc_project.ExternalSensor";
741static constexpr char EXTERNAL_SENSOR_PATH_PREFIX[] =
742 "/xyz/openbmc_project/sensors/power/";
743static constexpr char SENSOR_VALUE_IFC[] = "xyz.openbmc_project.Sensor.Value";
744
745uint16_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 Williams8c0094e2024-08-16 15:22:37 -0400750 std::string object_name(
751 std::format("{}{}{}", EXTERNAL_SENSOR_PATH_PREFIX,
752 _vrSettingsMap.at(settings_id), chip_id));
Gaurav Gandhid455bfd2024-01-29 22:32:27 -0800753
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 Kim559cb012024-05-03 09:12:07 +0000772
773std::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 Kim559cb012024-05-03 09:12:07 +0000798 std::ifstream ifs;
799 ifs.exceptions(std::ifstream::failbit);
800 std::string property;
801 try
802 {
803 ifs.open(opath);
Brandon Kim56b2d9f2024-05-30 20:54:33 +0000804 std::getline(ifs, property);
Brandon Kim559cb012024-05-03 09:12:07 +0000805 }
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 Venturef085d912019-03-15 08:50:00 -0700814} // namespace ipmi
815} // namespace google