blob: 9bbd08273dc025fef66026f952ff10f2cbd4e666 [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.
Patrick Venturef085d912019-03-15 08:50:00 -070014#include "handler.hpp"
15
Patrick Williams444b5ea2023-05-19 13:56:42 -050016#include "bm_config.h"
17
Nikhil Namjoshi8ec41062022-10-24 21:07:25 +000018#include "bmc_mode_enum.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070019#include "errors.hpp"
Patrick Venturec87de552020-05-20 20:25:39 -070020#include "handler_impl.hpp"
Patrick Ventureab650002019-03-16 09:08:47 -070021#include "util.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070022
Willy Tu3b1b4272021-03-02 17:58:10 -080023#include <fcntl.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070024#include <ipmid/api.h>
Willy Tu3b1b4272021-03-02 17:58:10 -080025#include <mtd/mtd-abi.h>
26#include <mtd/mtd-user.h>
27#include <sys/ioctl.h>
28#include <unistd.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070029
Patrick Williams444b5ea2023-05-19 13:56:42 -050030#include <nlohmann/json.hpp>
31#include <phosphor-logging/elog-errors.hpp>
32#include <phosphor-logging/log.hpp>
33#include <sdbusplus/bus.hpp>
34#include <xyz/openbmc_project/Common/error.hpp>
35
Patrick Venturebb90d4f2019-03-15 13:42:06 -070036#include <cinttypes>
Patrick Ventured2037c62019-03-15 10:29:47 -070037#include <cstdio>
38#include <filesystem>
39#include <fstream>
Patrick Venture07f85152019-03-15 21:36:56 -070040#include <map>
Patrick Ventured2037c62019-03-15 10:29:47 -070041#include <sstream>
42#include <string>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080043#include <string_view>
Patrick Ventured2037c62019-03-15 10:29:47 -070044#include <tuple>
Steve Foreman4f0d1de2021-09-20 14:06:32 -070045#include <variant>
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +000046
Patrick Venturef085d912019-03-15 08:50:00 -070047#ifndef NCSI_IF_NAME
48#define NCSI_IF_NAME eth0
49#endif
50
51// To deal with receiving a string without quotes.
52#define QUOTE(name) #name
53#define STR(macro) QUOTE(macro)
54#define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
55
William A. Kennington III8d3d46a2021-07-13 12:35:35 -070056namespace ipmi
57{
58std::uint8_t getChannelByName(const std::string& chName);
59}
60
Patrick Venturef085d912019-03-15 08:50:00 -070061namespace google
62{
63namespace ipmi
64{
Patrick Ventured2037c62019-03-15 10:29:47 -070065namespace fs = std::filesystem;
Patrick Venture07f85152019-03-15 21:36:56 -070066using Json = nlohmann::json;
67using namespace phosphor::logging;
68using InternalFailure =
69 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Venturef085d912019-03-15 08:50:00 -070070
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +000071uint8_t isBmcInBareMetalMode()
72{
73#if BARE_METAL
74 return static_cast<uint8_t>(BmcMode::BM_MODE);
75#else
Brandon Kim3f3ca032023-03-17 18:49:00 +000076 std::error_code ec;
77 if (fs::exists(BM_SIGNAL_PATH, ec))
78 {
79 std::fprintf(stderr, "%s exists so we must be in BM mode\n",
80 BM_SIGNAL_PATH);
81 return static_cast<uint8_t>(BmcMode::BM_MODE);
82 }
83
84 std::fprintf(stderr, "Unable to find %s so we must not be in BM mode\n",
85 BM_SIGNAL_PATH);
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +000086 return static_cast<uint8_t>(BmcMode::NON_BM_MODE);
87#endif
88}
89
90uint8_t Handler::getBmcMode()
91{
92 // BM_CLEANING_MODE is not implemented yet
93 return isBmcInBareMetalMode();
94}
95
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070096std::tuple<std::uint8_t, std::string>
97 Handler::getEthDetails(std::string intf) const
Patrick Venturef085d912019-03-15 08:50:00 -070098{
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070099 if (intf.empty())
100 {
101 intf = NCSI_IF_NAME_STR;
102 }
103 return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf));
Patrick Venturef085d912019-03-15 08:50:00 -0700104}
105
Patrick Ventured2037c62019-03-15 10:29:47 -0700106std::int64_t Handler::getRxPackets(const std::string& name) const
107{
108 std::ostringstream opath;
109 opath << "/sys/class/net/" << name << "/statistics/rx_packets";
110 std::string path = opath.str();
111
112 // Minor sanity & security check (of course, I'm less certain if unicode
113 // comes into play here.
114 //
115 // Basically you can't easily inject ../ or /../ into the path below.
116 if (name.find("/") != std::string::npos)
117 {
118 std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
Michael Shene5a06672022-06-20 05:08:32 +0000119 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventured2037c62019-03-15 10:29:47 -0700120 }
121
122 std::error_code ec;
123 if (!fs::exists(path, ec))
124 {
125 std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
Michael Shene5a06672022-06-20 05:08:32 +0000126 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventured2037c62019-03-15 10:29:47 -0700127 }
128 // We're uninterested in the state of ec.
129
130 int64_t count = 0;
131 std::ifstream ifs;
132 ifs.exceptions(std::ifstream::failbit);
133 try
134 {
135 ifs.open(path);
136 ifs >> count;
137 }
138 catch (std::ios_base::failure& fail)
139 {
Michael Shene5a06672022-06-20 05:08:32 +0000140 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventured2037c62019-03-15 10:29:47 -0700141 }
142
143 return count;
144}
145
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700146VersionTuple Handler::getCpldVersion(unsigned int id) const
147{
148 std::ostringstream opath;
149 opath << "/run/cpld" << id << ".version";
150 // Check for file
151
152 std::error_code ec;
153 if (!fs::exists(opath.str(), ec))
154 {
155 std::fprintf(stderr, "Path: '%s' doesn't exist.\n",
156 opath.str().c_str());
Michael Shene5a06672022-06-20 05:08:32 +0000157 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700158 }
159 // We're uninterested in the state of ec.
160
161 // If file exists, read.
162 std::ifstream ifs;
163 ifs.exceptions(std::ifstream::failbit);
164 std::string value;
165 try
166 {
167 ifs.open(opath.str());
168 ifs >> value;
169 }
170 catch (std::ios_base::failure& fail)
171 {
Michael Shene5a06672022-06-20 05:08:32 +0000172 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700173 }
174
175 // If value parses as expected, return version.
176 VersionTuple version = std::make_tuple(0, 0, 0, 0);
177
Patrick Williams444b5ea2023-05-19 13:56:42 -0500178 int num_fields = std::sscanf(value.c_str(),
179 "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
180 &std::get<0>(version), &std::get<1>(version),
181 &std::get<2>(version), &std::get<3>(version));
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700182 if (num_fields == 0)
183 {
184 std::fprintf(stderr, "Invalid version.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000185 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700186 }
187
188 return version;
189}
190
Patrick Ventureaa374122019-03-15 15:09:10 -0700191static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
192static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
193static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
194static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
195static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
196
197void Handler::psuResetDelay(std::uint32_t delay) const
198{
Patrick Ventureaa374122019-03-15 15:09:10 -0700199 std::ofstream ofs;
200 ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
201 if (!ofs.good())
202 {
203 std::fprintf(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000204 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700205 }
206
207 ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
208 if (ofs.fail())
209 {
210 std::fprintf(stderr, "Write failed\n");
211 ofs.close();
Michael Shene5a06672022-06-20 05:08:32 +0000212 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700213 }
214
215 // Write succeeded, please continue.
216 ofs.flush();
217 ofs.close();
218
219 auto bus = sdbusplus::bus::new_default();
220 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
221 SYSTEMD_INTERFACE, "StartUnit");
222
223 method.append(PSU_HARDRESET_TARGET);
224 method.append("replace");
225
226 try
227 {
228 bus.call_noreply(method);
229 }
230 catch (const sdbusplus::exception::SdBusError& ex)
231 {
232 log<level::ERR>("Failed to call PSU hard reset");
Michael Shene5a06672022-06-20 05:08:32 +0000233 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700234 }
235}
236
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800237static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5";
238
239void Handler::psuResetOnShutdown() const
240{
241 std::ofstream ofs;
242 ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out);
243 if (!ofs.good())
244 {
245 std::fprintf(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000246 throw IpmiException(::ipmi::ccUnspecifiedError);
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800247 }
248 ofs.close();
249}
250
Willy Tu3b1b4272021-03-02 17:58:10 -0800251uint32_t Handler::getFlashSize()
252{
253 mtd_info_t info;
254 int fd = open("/dev/mtd0", O_RDONLY);
255 int err = ioctl(fd, MEMGETINFO, &info);
256 close(fd);
257
258 if (err)
259 {
Michael Shene5a06672022-06-20 05:08:32 +0000260 throw IpmiException(::ipmi::ccUnspecifiedError);
Willy Tu3b1b4272021-03-02 17:58:10 -0800261 }
262 return info.size;
263}
264
Patrick Ventureab650002019-03-16 09:08:47 -0700265std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
Patrick Venture07f85152019-03-15 21:36:56 -0700266{
Patrick Ventureab650002019-03-16 09:08:47 -0700267 // Check if we support this Entity ID.
268 auto it = _entityIdToName.find(id);
269 if (it == _entityIdToName.end())
Patrick Venture07f85152019-03-15 21:36:56 -0700270 {
Patrick Ventureab650002019-03-16 09:08:47 -0700271 log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
Michael Shene5a06672022-06-20 05:08:32 +0000272 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Venture07f85152019-03-15 21:36:56 -0700273 }
274
Patrick Ventureab650002019-03-16 09:08:47 -0700275 std::string entityName;
276 try
Patrick Venture07f85152019-03-15 21:36:56 -0700277 {
Patrick Ventureab650002019-03-16 09:08:47 -0700278 // Parse the JSON config file.
279 if (!_entityConfigParsed)
280 {
281 _entityConfig = parseConfig(_configFile);
282 _entityConfigParsed = true;
283 }
284
285 // Find the "entity id:entity instance" mapping to entity name.
286 entityName = readNameFromConfig(it->second, instance, _entityConfig);
287 if (entityName.empty())
288 {
Michael Shene5a06672022-06-20 05:08:32 +0000289 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventureab650002019-03-16 09:08:47 -0700290 }
291 }
292 catch (InternalFailure& e)
293 {
Michael Shene5a06672022-06-20 05:08:32 +0000294 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venture07f85152019-03-15 21:36:56 -0700295 }
296
Patrick Ventureab650002019-03-16 09:08:47 -0700297 return entityName;
Patrick Venture07f85152019-03-15 21:36:56 -0700298}
299
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800300std::string Handler::getMachineName()
301{
302 const char* path = "/etc/os-release";
303 std::ifstream ifs(path);
304 if (ifs.fail())
305 {
306 std::fprintf(stderr, "Failed to open: %s\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000307 throw IpmiException(::ipmi::ccUnspecifiedError);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800308 }
309
310 std::string line;
311 while (true)
312 {
313 std::getline(ifs, line);
314 if (ifs.eof())
315 {
316 std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n",
317 path);
Michael Shene5a06672022-06-20 05:08:32 +0000318 throw IpmiException(::ipmi::ccInvalidCommand);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800319 }
320 if (ifs.fail())
321 {
322 std::fprintf(stderr, "Failed to read: %s\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000323 throw IpmiException(::ipmi::ccUnspecifiedError);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800324 }
325 std::string_view lineView(line);
326 constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
327 if (lineView.substr(0, prefix.size()) != prefix)
328 {
329 continue;
330 }
331 lineView.remove_prefix(prefix.size());
332 lineView.remove_prefix(
333 std::min(lineView.find_first_not_of('"'), lineView.size()));
334 lineView.remove_suffix(
335 lineView.size() - 1 -
336 std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
337 return std::string(lineView);
338 }
339}
340
linyuny8cfa4c42021-06-16 13:53:08 -0700341static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay";
342static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target";
343
344void Handler::hostPowerOffDelay(std::uint32_t delay) const
345{
346 // Set time delay
347 std::ofstream ofs;
348 ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out);
349 if (!ofs.good())
350 {
351 std::fprintf(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000352 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700353 }
354
355 ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl;
356 ofs.close();
357 if (ofs.fail())
358 {
359 std::fprintf(stderr, "Write failed\n");
Michael Shene5a06672022-06-20 05:08:32 +0000360 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700361 }
362
363 // Write succeeded, please continue.
364 auto bus = sdbusplus::bus::new_default();
365 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
366 SYSTEMD_INTERFACE, "StartUnit");
367
368 method.append(HOST_POWEROFF_TARGET);
369 method.append("replace");
370
371 try
372 {
373 bus.call_noreply(method);
374 }
375 catch (const sdbusplus::exception::SdBusError& ex)
376 {
377 log<level::ERR>("Failed to call Power Off",
378 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000379 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700380 }
381}
382
Patrick Ventureab650002019-03-16 09:08:47 -0700383std::string readNameFromConfig(const std::string& type, uint8_t instance,
384 const Json& config)
Patrick Venture07f85152019-03-15 21:36:56 -0700385{
386 static const std::vector<Json> empty{};
387 std::vector<Json> readings = config.value(type, empty);
388 std::string name = "";
Patrick Ventureab650002019-03-16 09:08:47 -0700389
Patrick Venture07f85152019-03-15 21:36:56 -0700390 for (const auto& j : readings)
391 {
392 uint8_t instanceNum = j.value("instance", 0);
393 // Not the instance we're interested in
394 if (instanceNum != instance)
395 {
396 continue;
397 }
398
399 // Found the instance we're interested in
400 name = j.value("name", "");
401
402 break;
403 }
Patrick Ventureab650002019-03-16 09:08:47 -0700404
Patrick Venture07f85152019-03-15 21:36:56 -0700405 return name;
406}
407
Patrick Venture49f23ad2019-03-16 11:59:55 -0700408void Handler::buildI2cPcieMapping()
409{
410 _pcie_i2c_map = buildPcieMap();
411}
412
413size_t Handler::getI2cPcieMappingSize() const
414{
415 return _pcie_i2c_map.size();
416}
417
418std::tuple<std::uint32_t, std::string>
419 Handler::getI2cEntry(unsigned int entry) const
420{
421 return _pcie_i2c_map[entry];
422}
423
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700424namespace
425{
426
427static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/";
428static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel";
429static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR";
430
431// C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects()
432using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>;
433using AnyTypeList = std::vector<std::pair<std::string, AnyType>>;
434using NamedArrayOfAnyTypeLists =
435 std::vector<std::pair<std::string, AnyTypeList>>;
436using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector<
437 std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>;
438
439} // namespace
440
Patrick Williams65371222023-05-19 02:31:40 -0500441sdbusplus::bus_t Handler::getDbus() const
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700442{
443 return sdbusplus::bus::new_default();
444}
445
446uint32_t Handler::accelOobDeviceCount() const
447{
448 ArrayOfObjectPathsAndTieredAnyTypeLists data;
449
450 try
451 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000452 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700453 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
454 "org.freedesktop.DBus.ObjectManager",
455 "GetManagedObjects");
456 bus.call(method).read(data);
457 }
458 catch (const sdbusplus::exception::SdBusError& ex)
459 {
460 log<level::ERR>(
461 "Failed to call GetManagedObjects on com.google.custom_accel",
462 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000463 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700464 }
465
466 return data.size();
467}
468
469std::string Handler::accelOobDeviceName(size_t index) const
470{
471 ArrayOfObjectPathsAndTieredAnyTypeLists data;
472
473 try
474 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000475 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700476 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
477 "org.freedesktop.DBus.ObjectManager",
478 "GetManagedObjects");
479 bus.call(method).read(data);
480 }
481 catch (const sdbusplus::exception::SdBusError& ex)
482 {
483 log<level::ERR>(
484 "Failed to call GetManagedObjects on com.google.custom_accel",
485 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000486 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700487 }
488
489 if (index >= data.size())
490 {
491 log<level::WARNING>(
492 "Requested index is larger than the number of entries.",
493 entry("INDEX=%zu", index), entry("NUM_NAMES=%zu", data.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000494 throw IpmiException(::ipmi::ccParmOutOfRange);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700495 }
496
497 std::string_view name(data[index].first.str);
498 if (!name.starts_with(ACCEL_OOB_ROOT))
499 {
Michael Shene5a06672022-06-20 05:08:32 +0000500 throw IpmiException(::ipmi::ccInvalidCommand);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700501 }
502 name.remove_prefix(ACCEL_OOB_ROOT.length());
503 return std::string(name);
504}
505
506uint64_t Handler::accelOobRead(std::string_view name, uint64_t address,
507 uint8_t num_bytes) const
508{
509 static constexpr char ACCEL_OOB_METHOD[] = "Read";
510
511 std::string object_name(ACCEL_OOB_ROOT);
512 object_name.append(name);
513
Michael Shen0e928ac2022-06-20 05:21:52 +0000514 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700515 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
516 ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD);
517 method.append(address, static_cast<uint64_t>(num_bytes));
518
519 std::vector<uint8_t> bytes;
520
521 try
522 {
523 bus.call(method).read(bytes);
524 }
525 catch (const sdbusplus::exception::SdBusError& ex)
526 {
527 log<level::ERR>("Failed to call Read on com.google.custom_accel",
528 entry("WHAT=%s", ex.what()),
529 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
530 entry("DBUS_OBJECT=%s", object_name.c_str()),
531 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
532 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
533 entry("DBUS_ARG_ADDRESS=%016llx", address),
534 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes));
Michael Shene5a06672022-06-20 05:08:32 +0000535 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700536 }
537
538 if (bytes.size() < num_bytes)
539 {
540 log<level::ERR>(
541 "Call to Read on com.google.custom_accel didn't return the expected"
542 " number of bytes.",
543 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
544 entry("DBUS_OBJECT=%s", object_name.c_str()),
545 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
546 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
547 entry("DBUS_ARG_ADDRESS=%016llx", address),
548 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
549 entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000550 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700551 }
552
553 if (bytes.size() > sizeof(uint64_t))
554 {
555 log<level::ERR>(
556 "Call to Read on com.google.custom_accel returned more than 8B.",
557 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
558 entry("DBUS_OBJECT=%s", object_name.c_str()),
559 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
560 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
561 entry("DBUS_ARG_ADDRESS=%016llx", address),
562 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
563 entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000564 throw IpmiException(::ipmi::ccReqDataTruncated);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700565 }
566
567 uint64_t data = 0;
568 for (size_t i = 0; i < num_bytes; ++i)
569 {
570 data = (data << 8) | bytes[i];
571 }
572
573 return data;
574}
575
576void Handler::accelOobWrite(std::string_view name, uint64_t address,
577 uint8_t num_bytes, uint64_t data) const
578{
579 static constexpr std::string_view ACCEL_OOB_METHOD = "Write";
580
581 std::string object_name(ACCEL_OOB_ROOT);
582 object_name.append(name);
583
584 if (num_bytes > sizeof(data))
585 {
586 log<level::ERR>(
587 "Call to Write on com.google.custom_accel requested more than 8B.",
588 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
589 entry("DBUS_OBJECT=%s", object_name.c_str()),
590 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
591 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
592 entry("DBUS_ARG_ADDRESS=%016llx", address),
593 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
594 entry("DBUS_ARG_DATA=%016llx", data));
Michael Shene5a06672022-06-20 05:08:32 +0000595 throw IpmiException(::ipmi::ccParmOutOfRange);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700596 }
597
598 std::vector<uint8_t> bytes;
599 bytes.reserve(num_bytes);
600 for (size_t i = 0; i < num_bytes; ++i)
601 {
602 bytes.emplace_back(data & 0xff);
603 data >>= 8;
604 }
605
606 try
607 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000608 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700609 auto method =
610 bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
611 ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD.data());
612 method.append(address, bytes);
613 bus.call_noreply(method);
614 }
615 catch (const sdbusplus::exception::SdBusError& ex)
616 {
617 log<level::ERR>("Failed to call Write on com.google.custom_accel",
618 entry("WHAT=%s", ex.what()),
619 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
620 entry("DBUS_OBJECT=%s", object_name.c_str()),
621 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
622 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
623 entry("DBUS_ARG_ADDRESS=%016llx", address),
624 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
625 entry("DBUS_ARG_DATA=%016llx", data));
Michael Shene5a06672022-06-20 05:08:32 +0000626 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700627 }
628}
629
Willy Tu6c71b0f2021-10-10 13:34:41 -0700630std::vector<uint8_t> Handler::pcieBifurcation(uint8_t index)
631{
632 return bifurcationHelper.get().getBifurcation(index).value_or(
633 std::vector<uint8_t>{});
634}
635
Patrick Venturef085d912019-03-15 08:50:00 -0700636} // namespace ipmi
637} // namespace google