blob: 564eab8ed19f17809d4b7a00e536b36b07022431 [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
Nikhil Namjoshi8ec41062022-10-24 21:07:25 +000016#include "bmc_mode_enum.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070017#include "errors.hpp"
Patrick Venturec87de552020-05-20 20:25:39 -070018#include "handler_impl.hpp"
Patrick Ventureab650002019-03-16 09:08:47 -070019#include "util.hpp"
Patrick Ventured2037c62019-03-15 10:29:47 -070020
Willy Tu3b1b4272021-03-02 17:58:10 -080021#include <fcntl.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070022#include <ipmid/api.h>
Willy Tu3b1b4272021-03-02 17:58:10 -080023#include <mtd/mtd-abi.h>
24#include <mtd/mtd-user.h>
25#include <sys/ioctl.h>
26#include <unistd.h>
Patrick Ventured2037c62019-03-15 10:29:47 -070027
Patrick Venturebb90d4f2019-03-15 13:42:06 -070028#include <cinttypes>
Patrick Ventured2037c62019-03-15 10:29:47 -070029#include <cstdio>
30#include <filesystem>
31#include <fstream>
Patrick Venture07f85152019-03-15 21:36:56 -070032#include <map>
33#include <nlohmann/json.hpp>
34#include <phosphor-logging/elog-errors.hpp>
Patrick Ventureaa374122019-03-15 15:09:10 -070035#include <phosphor-logging/log.hpp>
36#include <sdbusplus/bus.hpp>
Patrick Ventured2037c62019-03-15 10:29:47 -070037#include <sstream>
38#include <string>
William A. Kennington III29f35bc2020-11-03 23:30:31 -080039#include <string_view>
Patrick Ventured2037c62019-03-15 10:29:47 -070040#include <tuple>
Steve Foreman4f0d1de2021-09-20 14:06:32 -070041#include <variant>
Patrick Venture07f85152019-03-15 21:36:56 -070042#include <xyz/openbmc_project/Common/error.hpp>
Patrick Ventured2037c62019-03-15 10:29:47 -070043
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +000044#include "bm_config.h"
45
Patrick Venturef085d912019-03-15 08:50:00 -070046#ifndef NCSI_IF_NAME
47#define NCSI_IF_NAME eth0
48#endif
49
50// To deal with receiving a string without quotes.
51#define QUOTE(name) #name
52#define STR(macro) QUOTE(macro)
53#define NCSI_IF_NAME_STR STR(NCSI_IF_NAME)
54
William A. Kennington III8d3d46a2021-07-13 12:35:35 -070055namespace ipmi
56{
57std::uint8_t getChannelByName(const std::string& chName);
58}
59
Patrick Venturef085d912019-03-15 08:50:00 -070060namespace google
61{
62namespace ipmi
63{
Patrick Ventured2037c62019-03-15 10:29:47 -070064namespace fs = std::filesystem;
Patrick Venture07f85152019-03-15 21:36:56 -070065using Json = nlohmann::json;
66using namespace phosphor::logging;
67using InternalFailure =
68 sdbusplus::xyz::openbmc_project::Common::Error::InternalFailure;
Patrick Venturef085d912019-03-15 08:50:00 -070069
Nikhil Namjoshi5e70dc82022-09-16 00:36:07 +000070uint8_t isBmcInBareMetalMode()
71{
72#if BARE_METAL
73 return static_cast<uint8_t>(BmcMode::BM_MODE);
74#else
75 return static_cast<uint8_t>(BmcMode::NON_BM_MODE);
76#endif
77}
78
79uint8_t Handler::getBmcMode()
80{
81 // BM_CLEANING_MODE is not implemented yet
82 return isBmcInBareMetalMode();
83}
84
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070085std::tuple<std::uint8_t, std::string>
86 Handler::getEthDetails(std::string intf) const
Patrick Venturef085d912019-03-15 08:50:00 -070087{
William A. Kennington IIIb69209b2021-07-13 13:22:24 -070088 if (intf.empty())
89 {
90 intf = NCSI_IF_NAME_STR;
91 }
92 return std::make_tuple(::ipmi::getChannelByName(intf), std::move(intf));
Patrick Venturef085d912019-03-15 08:50:00 -070093}
94
Patrick Ventured2037c62019-03-15 10:29:47 -070095std::int64_t Handler::getRxPackets(const std::string& name) const
96{
97 std::ostringstream opath;
98 opath << "/sys/class/net/" << name << "/statistics/rx_packets";
99 std::string path = opath.str();
100
101 // Minor sanity & security check (of course, I'm less certain if unicode
102 // comes into play here.
103 //
104 // Basically you can't easily inject ../ or /../ into the path below.
105 if (name.find("/") != std::string::npos)
106 {
107 std::fprintf(stderr, "Invalid or illegal name: '%s'\n", name.c_str());
Michael Shene5a06672022-06-20 05:08:32 +0000108 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventured2037c62019-03-15 10:29:47 -0700109 }
110
111 std::error_code ec;
112 if (!fs::exists(path, ec))
113 {
114 std::fprintf(stderr, "Path: '%s' doesn't exist.\n", path.c_str());
Michael Shene5a06672022-06-20 05:08:32 +0000115 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventured2037c62019-03-15 10:29:47 -0700116 }
117 // We're uninterested in the state of ec.
118
119 int64_t count = 0;
120 std::ifstream ifs;
121 ifs.exceptions(std::ifstream::failbit);
122 try
123 {
124 ifs.open(path);
125 ifs >> count;
126 }
127 catch (std::ios_base::failure& fail)
128 {
Michael Shene5a06672022-06-20 05:08:32 +0000129 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventured2037c62019-03-15 10:29:47 -0700130 }
131
132 return count;
133}
134
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700135VersionTuple Handler::getCpldVersion(unsigned int id) const
136{
137 std::ostringstream opath;
138 opath << "/run/cpld" << id << ".version";
139 // Check for file
140
141 std::error_code ec;
142 if (!fs::exists(opath.str(), ec))
143 {
144 std::fprintf(stderr, "Path: '%s' doesn't exist.\n",
145 opath.str().c_str());
Michael Shene5a06672022-06-20 05:08:32 +0000146 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700147 }
148 // We're uninterested in the state of ec.
149
150 // If file exists, read.
151 std::ifstream ifs;
152 ifs.exceptions(std::ifstream::failbit);
153 std::string value;
154 try
155 {
156 ifs.open(opath.str());
157 ifs >> value;
158 }
159 catch (std::ios_base::failure& fail)
160 {
Michael Shene5a06672022-06-20 05:08:32 +0000161 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700162 }
163
164 // If value parses as expected, return version.
165 VersionTuple version = std::make_tuple(0, 0, 0, 0);
166
167 int num_fields =
168 std::sscanf(value.c_str(), "%" SCNu8 ".%" SCNu8 ".%" SCNu8 ".%" SCNu8,
169 &std::get<0>(version), &std::get<1>(version),
170 &std::get<2>(version), &std::get<3>(version));
171 if (num_fields == 0)
172 {
173 std::fprintf(stderr, "Invalid version.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000174 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venturebb90d4f2019-03-15 13:42:06 -0700175 }
176
177 return version;
178}
179
Patrick Ventureaa374122019-03-15 15:09:10 -0700180static constexpr auto TIME_DELAY_FILENAME = "/run/psu_timedelay";
181static constexpr auto SYSTEMD_SERVICE = "org.freedesktop.systemd1";
182static constexpr auto SYSTEMD_ROOT = "/org/freedesktop/systemd1";
183static constexpr auto SYSTEMD_INTERFACE = "org.freedesktop.systemd1.Manager";
184static constexpr auto PSU_HARDRESET_TARGET = "gbmc-psu-hardreset.target";
185
186void Handler::psuResetDelay(std::uint32_t delay) const
187{
Patrick Ventureaa374122019-03-15 15:09:10 -0700188 std::ofstream ofs;
189 ofs.open(TIME_DELAY_FILENAME, std::ofstream::out);
190 if (!ofs.good())
191 {
192 std::fprintf(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000193 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700194 }
195
196 ofs << "PSU_HARDRESET_DELAY=" << delay << std::endl;
197 if (ofs.fail())
198 {
199 std::fprintf(stderr, "Write failed\n");
200 ofs.close();
Michael Shene5a06672022-06-20 05:08:32 +0000201 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700202 }
203
204 // Write succeeded, please continue.
205 ofs.flush();
206 ofs.close();
207
208 auto bus = sdbusplus::bus::new_default();
209 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
210 SYSTEMD_INTERFACE, "StartUnit");
211
212 method.append(PSU_HARDRESET_TARGET);
213 method.append("replace");
214
215 try
216 {
217 bus.call_noreply(method);
218 }
219 catch (const sdbusplus::exception::SdBusError& ex)
220 {
221 log<level::ERR>("Failed to call PSU hard reset");
Michael Shene5a06672022-06-20 05:08:32 +0000222 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Ventureaa374122019-03-15 15:09:10 -0700223 }
224}
225
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800226static constexpr auto RESET_ON_SHUTDOWN_FILENAME = "/run/powercycle_on_s5";
227
228void Handler::psuResetOnShutdown() const
229{
230 std::ofstream ofs;
231 ofs.open(RESET_ON_SHUTDOWN_FILENAME, std::ofstream::out);
232 if (!ofs.good())
233 {
234 std::fprintf(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000235 throw IpmiException(::ipmi::ccUnspecifiedError);
Shounak Mitraac4a16f2021-02-02 11:11:44 -0800236 }
237 ofs.close();
238}
239
Willy Tu3b1b4272021-03-02 17:58:10 -0800240uint32_t Handler::getFlashSize()
241{
242 mtd_info_t info;
243 int fd = open("/dev/mtd0", O_RDONLY);
244 int err = ioctl(fd, MEMGETINFO, &info);
245 close(fd);
246
247 if (err)
248 {
Michael Shene5a06672022-06-20 05:08:32 +0000249 throw IpmiException(::ipmi::ccUnspecifiedError);
Willy Tu3b1b4272021-03-02 17:58:10 -0800250 }
251 return info.size;
252}
253
Patrick Ventureab650002019-03-16 09:08:47 -0700254std::string Handler::getEntityName(std::uint8_t id, std::uint8_t instance)
Patrick Venture07f85152019-03-15 21:36:56 -0700255{
Patrick Ventureab650002019-03-16 09:08:47 -0700256 // Check if we support this Entity ID.
257 auto it = _entityIdToName.find(id);
258 if (it == _entityIdToName.end())
Patrick Venture07f85152019-03-15 21:36:56 -0700259 {
Patrick Ventureab650002019-03-16 09:08:47 -0700260 log<level::ERR>("Unknown Entity ID", entry("ENTITY_ID=%d", id));
Michael Shene5a06672022-06-20 05:08:32 +0000261 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Venture07f85152019-03-15 21:36:56 -0700262 }
263
Patrick Ventureab650002019-03-16 09:08:47 -0700264 std::string entityName;
265 try
Patrick Venture07f85152019-03-15 21:36:56 -0700266 {
Patrick Ventureab650002019-03-16 09:08:47 -0700267 // Parse the JSON config file.
268 if (!_entityConfigParsed)
269 {
270 _entityConfig = parseConfig(_configFile);
271 _entityConfigParsed = true;
272 }
273
274 // Find the "entity id:entity instance" mapping to entity name.
275 entityName = readNameFromConfig(it->second, instance, _entityConfig);
276 if (entityName.empty())
277 {
Michael Shene5a06672022-06-20 05:08:32 +0000278 throw IpmiException(::ipmi::ccInvalidFieldRequest);
Patrick Ventureab650002019-03-16 09:08:47 -0700279 }
280 }
281 catch (InternalFailure& e)
282 {
Michael Shene5a06672022-06-20 05:08:32 +0000283 throw IpmiException(::ipmi::ccUnspecifiedError);
Patrick Venture07f85152019-03-15 21:36:56 -0700284 }
285
Patrick Ventureab650002019-03-16 09:08:47 -0700286 return entityName;
Patrick Venture07f85152019-03-15 21:36:56 -0700287}
288
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800289std::string Handler::getMachineName()
290{
291 const char* path = "/etc/os-release";
292 std::ifstream ifs(path);
293 if (ifs.fail())
294 {
295 std::fprintf(stderr, "Failed to open: %s\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000296 throw IpmiException(::ipmi::ccUnspecifiedError);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800297 }
298
299 std::string line;
300 while (true)
301 {
302 std::getline(ifs, line);
303 if (ifs.eof())
304 {
305 std::fprintf(stderr, "Failed to find OPENBMC_TARGET_MACHINE: %s\n",
306 path);
Michael Shene5a06672022-06-20 05:08:32 +0000307 throw IpmiException(::ipmi::ccInvalidCommand);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800308 }
309 if (ifs.fail())
310 {
311 std::fprintf(stderr, "Failed to read: %s\n", path);
Michael Shene5a06672022-06-20 05:08:32 +0000312 throw IpmiException(::ipmi::ccUnspecifiedError);
William A. Kennington III29f35bc2020-11-03 23:30:31 -0800313 }
314 std::string_view lineView(line);
315 constexpr std::string_view prefix = "OPENBMC_TARGET_MACHINE=";
316 if (lineView.substr(0, prefix.size()) != prefix)
317 {
318 continue;
319 }
320 lineView.remove_prefix(prefix.size());
321 lineView.remove_prefix(
322 std::min(lineView.find_first_not_of('"'), lineView.size()));
323 lineView.remove_suffix(
324 lineView.size() - 1 -
325 std::min(lineView.find_last_not_of('"'), lineView.size() - 1));
326 return std::string(lineView);
327 }
328}
329
linyuny8cfa4c42021-06-16 13:53:08 -0700330static constexpr auto HOST_TIME_DELAY_FILENAME = "/run/host_poweroff_delay";
331static constexpr auto HOST_POWEROFF_TARGET = "gbmc-host-poweroff.target";
332
333void Handler::hostPowerOffDelay(std::uint32_t delay) const
334{
335 // Set time delay
336 std::ofstream ofs;
337 ofs.open(HOST_TIME_DELAY_FILENAME, std::ofstream::out);
338 if (!ofs.good())
339 {
340 std::fprintf(stderr, "Unable to open file for output.\n");
Michael Shene5a06672022-06-20 05:08:32 +0000341 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700342 }
343
344 ofs << "HOST_POWEROFF_DELAY=" << delay << std::endl;
345 ofs.close();
346 if (ofs.fail())
347 {
348 std::fprintf(stderr, "Write failed\n");
Michael Shene5a06672022-06-20 05:08:32 +0000349 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700350 }
351
352 // Write succeeded, please continue.
353 auto bus = sdbusplus::bus::new_default();
354 auto method = bus.new_method_call(SYSTEMD_SERVICE, SYSTEMD_ROOT,
355 SYSTEMD_INTERFACE, "StartUnit");
356
357 method.append(HOST_POWEROFF_TARGET);
358 method.append("replace");
359
360 try
361 {
362 bus.call_noreply(method);
363 }
364 catch (const sdbusplus::exception::SdBusError& ex)
365 {
366 log<level::ERR>("Failed to call Power Off",
367 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000368 throw IpmiException(::ipmi::ccUnspecifiedError);
linyuny8cfa4c42021-06-16 13:53:08 -0700369 }
370}
371
Patrick Ventureab650002019-03-16 09:08:47 -0700372std::string readNameFromConfig(const std::string& type, uint8_t instance,
373 const Json& config)
Patrick Venture07f85152019-03-15 21:36:56 -0700374{
375 static const std::vector<Json> empty{};
376 std::vector<Json> readings = config.value(type, empty);
377 std::string name = "";
Patrick Ventureab650002019-03-16 09:08:47 -0700378
Patrick Venture07f85152019-03-15 21:36:56 -0700379 for (const auto& j : readings)
380 {
381 uint8_t instanceNum = j.value("instance", 0);
382 // Not the instance we're interested in
383 if (instanceNum != instance)
384 {
385 continue;
386 }
387
388 // Found the instance we're interested in
389 name = j.value("name", "");
390
391 break;
392 }
Patrick Ventureab650002019-03-16 09:08:47 -0700393
Patrick Venture07f85152019-03-15 21:36:56 -0700394 return name;
395}
396
Patrick Venture49f23ad2019-03-16 11:59:55 -0700397void Handler::buildI2cPcieMapping()
398{
399 _pcie_i2c_map = buildPcieMap();
400}
401
402size_t Handler::getI2cPcieMappingSize() const
403{
404 return _pcie_i2c_map.size();
405}
406
407std::tuple<std::uint32_t, std::string>
408 Handler::getI2cEntry(unsigned int entry) const
409{
410 return _pcie_i2c_map[entry];
411}
412
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700413namespace
414{
415
416static constexpr std::string_view ACCEL_OOB_ROOT = "/com/google/customAccel/";
417static constexpr char ACCEL_OOB_SERVICE[] = "com.google.custom_accel";
418static constexpr char ACCEL_OOB_INTERFACE[] = "com.google.custom_accel.BAR";
419
420// C type for "a{oa{sa{sv}}}" from DBus.ObjectManager::GetManagedObjects()
421using AnyType = std::variant<std::string, uint8_t, uint32_t, uint64_t>;
422using AnyTypeList = std::vector<std::pair<std::string, AnyType>>;
423using NamedArrayOfAnyTypeLists =
424 std::vector<std::pair<std::string, AnyTypeList>>;
425using ArrayOfObjectPathsAndTieredAnyTypeLists = std::vector<
426 std::pair<sdbusplus::message::object_path, NamedArrayOfAnyTypeLists>>;
427
428} // namespace
429
Michael Shen0e928ac2022-06-20 05:21:52 +0000430sdbusplus::bus::bus Handler::getDbus() const
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700431{
432 return sdbusplus::bus::new_default();
433}
434
435uint32_t Handler::accelOobDeviceCount() const
436{
437 ArrayOfObjectPathsAndTieredAnyTypeLists data;
438
439 try
440 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000441 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700442 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
443 "org.freedesktop.DBus.ObjectManager",
444 "GetManagedObjects");
445 bus.call(method).read(data);
446 }
447 catch (const sdbusplus::exception::SdBusError& ex)
448 {
449 log<level::ERR>(
450 "Failed to call GetManagedObjects on com.google.custom_accel",
451 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000452 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700453 }
454
455 return data.size();
456}
457
458std::string Handler::accelOobDeviceName(size_t index) const
459{
460 ArrayOfObjectPathsAndTieredAnyTypeLists data;
461
462 try
463 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000464 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700465 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, "/",
466 "org.freedesktop.DBus.ObjectManager",
467 "GetManagedObjects");
468 bus.call(method).read(data);
469 }
470 catch (const sdbusplus::exception::SdBusError& ex)
471 {
472 log<level::ERR>(
473 "Failed to call GetManagedObjects on com.google.custom_accel",
474 entry("WHAT=%s", ex.what()));
Michael Shene5a06672022-06-20 05:08:32 +0000475 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700476 }
477
478 if (index >= data.size())
479 {
480 log<level::WARNING>(
481 "Requested index is larger than the number of entries.",
482 entry("INDEX=%zu", index), entry("NUM_NAMES=%zu", data.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000483 throw IpmiException(::ipmi::ccParmOutOfRange);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700484 }
485
486 std::string_view name(data[index].first.str);
487 if (!name.starts_with(ACCEL_OOB_ROOT))
488 {
Michael Shene5a06672022-06-20 05:08:32 +0000489 throw IpmiException(::ipmi::ccInvalidCommand);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700490 }
491 name.remove_prefix(ACCEL_OOB_ROOT.length());
492 return std::string(name);
493}
494
495uint64_t Handler::accelOobRead(std::string_view name, uint64_t address,
496 uint8_t num_bytes) const
497{
498 static constexpr char ACCEL_OOB_METHOD[] = "Read";
499
500 std::string object_name(ACCEL_OOB_ROOT);
501 object_name.append(name);
502
Michael Shen0e928ac2022-06-20 05:21:52 +0000503 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700504 auto method = bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
505 ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD);
506 method.append(address, static_cast<uint64_t>(num_bytes));
507
508 std::vector<uint8_t> bytes;
509
510 try
511 {
512 bus.call(method).read(bytes);
513 }
514 catch (const sdbusplus::exception::SdBusError& ex)
515 {
516 log<level::ERR>("Failed to call Read on com.google.custom_accel",
517 entry("WHAT=%s", ex.what()),
518 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
519 entry("DBUS_OBJECT=%s", object_name.c_str()),
520 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
521 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
522 entry("DBUS_ARG_ADDRESS=%016llx", address),
523 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes));
Michael Shene5a06672022-06-20 05:08:32 +0000524 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700525 }
526
527 if (bytes.size() < num_bytes)
528 {
529 log<level::ERR>(
530 "Call to Read on com.google.custom_accel didn't return the expected"
531 " number of bytes.",
532 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
533 entry("DBUS_OBJECT=%s", object_name.c_str()),
534 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
535 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
536 entry("DBUS_ARG_ADDRESS=%016llx", address),
537 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
538 entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000539 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700540 }
541
542 if (bytes.size() > sizeof(uint64_t))
543 {
544 log<level::ERR>(
545 "Call to Read on com.google.custom_accel returned more than 8B.",
546 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
547 entry("DBUS_OBJECT=%s", object_name.c_str()),
548 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
549 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD),
550 entry("DBUS_ARG_ADDRESS=%016llx", address),
551 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
552 entry("DBUS_RETURN_SIZE=%zu", bytes.size()));
Michael Shene5a06672022-06-20 05:08:32 +0000553 throw IpmiException(::ipmi::ccReqDataTruncated);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700554 }
555
556 uint64_t data = 0;
557 for (size_t i = 0; i < num_bytes; ++i)
558 {
559 data = (data << 8) | bytes[i];
560 }
561
562 return data;
563}
564
565void Handler::accelOobWrite(std::string_view name, uint64_t address,
566 uint8_t num_bytes, uint64_t data) const
567{
568 static constexpr std::string_view ACCEL_OOB_METHOD = "Write";
569
570 std::string object_name(ACCEL_OOB_ROOT);
571 object_name.append(name);
572
573 if (num_bytes > sizeof(data))
574 {
575 log<level::ERR>(
576 "Call to Write on com.google.custom_accel requested more than 8B.",
577 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
578 entry("DBUS_OBJECT=%s", object_name.c_str()),
579 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
580 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
581 entry("DBUS_ARG_ADDRESS=%016llx", address),
582 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
583 entry("DBUS_ARG_DATA=%016llx", data));
Michael Shene5a06672022-06-20 05:08:32 +0000584 throw IpmiException(::ipmi::ccParmOutOfRange);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700585 }
586
587 std::vector<uint8_t> bytes;
588 bytes.reserve(num_bytes);
589 for (size_t i = 0; i < num_bytes; ++i)
590 {
591 bytes.emplace_back(data & 0xff);
592 data >>= 8;
593 }
594
595 try
596 {
Michael Shen0e928ac2022-06-20 05:21:52 +0000597 auto bus = getDbus();
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700598 auto method =
599 bus.new_method_call(ACCEL_OOB_SERVICE, object_name.c_str(),
600 ACCEL_OOB_INTERFACE, ACCEL_OOB_METHOD.data());
601 method.append(address, bytes);
602 bus.call_noreply(method);
603 }
604 catch (const sdbusplus::exception::SdBusError& ex)
605 {
606 log<level::ERR>("Failed to call Write on com.google.custom_accel",
607 entry("WHAT=%s", ex.what()),
608 entry("DBUS_SERVICE=%s", ACCEL_OOB_SERVICE),
609 entry("DBUS_OBJECT=%s", object_name.c_str()),
610 entry("DBUS_INTERFACE=%s", ACCEL_OOB_INTERFACE),
611 entry("DBUS_METHOD=%s", ACCEL_OOB_METHOD.data()),
612 entry("DBUS_ARG_ADDRESS=%016llx", address),
613 entry("DBUS_ARG_NUM_BYTES=%zu", (size_t)num_bytes),
614 entry("DBUS_ARG_DATA=%016llx", data));
Michael Shene5a06672022-06-20 05:08:32 +0000615 throw IpmiException(::ipmi::ccUnspecifiedError);
Steve Foreman4f0d1de2021-09-20 14:06:32 -0700616 }
617}
618
Willy Tu6c71b0f2021-10-10 13:34:41 -0700619std::vector<uint8_t> Handler::pcieBifurcation(uint8_t index)
620{
621 return bifurcationHelper.get().getBifurcation(index).value_or(
622 std::vector<uint8_t>{});
623}
624
Patrick Venturef085d912019-03-15 08:50:00 -0700625} // namespace ipmi
626} // namespace google