blob: 3bf1b988fef3efc224b496ac81fbce9ef007b718 [file] [log] [blame]
#include "tlbmc/hal/peci/peci_access_impl.h"
#include <array>
#include <cstdint>
#include <string>
#include "absl/status/status.h"
#include "absl/status/statusor.h"
#include "absl/strings/str_format.h"
#include "absl/strings/string_view.h"
#include "g3/macros.h"
#include "peci.h"
namespace milotic_tlbmc {
// Helper to convert EPECIStatus and completion code to absl::Status.
absl::Status PeciAccessImpl::PeciStatusToAbslStatus(EPECIStatus status,
uint8_t completion_code,
absl::string_view message) {
if (status != PECI_CC_SUCCESS) {
return absl::InternalError(absl::StrFormat(
"%s failed with EPECIStatus status %d which is not PECI_CC_SUCCESS",
message, status));
}
if (completion_code != PECI_DEV_CC_SUCCESS && completion_code != 0) {
return absl::InternalError(
absl::StrFormat("%s failed with PECI status %d but completion code "
"0x%02x, which is not PECI_DEV_CC_SUCCESS",
message, status, completion_code));
}
return absl::OkStatus();
}
absl::Status PeciAccessImpl::Ping(uint8_t target) const {
EPECIStatus status = peci_Ping(target);
return PeciStatusToAbslStatus(status, 0, "peci_Ping");
}
absl::StatusOr<std::array<uint8_t, 8>> PeciAccessImpl::RdPkgConfig(
uint8_t target, uint8_t index, uint16_t value, uint8_t read_len) const {
std::array<uint8_t, 8> pkg_config{};
uint8_t completion_code = 0;
EPECIStatus status = peci_RdPkgConfig(target, index, value, read_len,
pkg_config.data(), &completion_code);
ECCLESIA_RETURN_IF_ERROR(
PeciStatusToAbslStatus(status, completion_code, "peci_RdPkgConfig"));
return pkg_config;
}
void PeciAccessImpl::SetDevName(absl::string_view dev_name) const {
peci_SetDevName(std::string(dev_name).data());
}
absl::StatusOr<int> PeciAccessImpl::Lock(EPECITimeout timeout) const {
int peci_fd = -1;
EPECIStatus status = peci_Lock(&peci_fd, timeout);
ECCLESIA_RETURN_IF_ERROR(PeciStatusToAbslStatus(status, 0, "peci_Lock"));
return peci_fd;
}
void PeciAccessImpl::Unlock(int peci_fd) const { peci_Unlock(peci_fd); }
} // namespace milotic_tlbmc