| #include "tlbmc/hal/sysfs/peci.h" |
| |
| #include <cstdint> |
| #include <fstream> |
| #include <string> |
| |
| #include "absl/status/status.h" |
| #include "absl/strings/str_cat.h" |
| #include "hal_common_config.pb.h" |
| |
| namespace milotic_tlbmc { |
| |
| boost::filesystem::path PeciSysfs::GetBusPath(uint64_t bus) const { |
| boost::filesystem::path path(config_.peci_sysfs_path); |
| path /= "devices/peci-"; |
| return absl::StrCat(path.string(), bus); |
| } |
| |
| boost::filesystem::path PeciSysfs::GetPeciDevicePath(uint64_t bus) const { |
| return boost::filesystem::path(absl::StrCat(config_.peci_dev_path, bus)); |
| } |
| |
| boost::filesystem::path PeciSysfs::GetPeciRescanPath() const { |
| // From: https://www.kernel.org/doc/html/latest/admin-guide/abi-testing.html#abi-sys-bus-peci-rescan |
| return boost::filesystem::path(config_.peci_sysfs_path) / "rescan"; |
| } |
| |
| bool PeciSysfs::IsDevicePresent(const HalCommonConfig& config) const { |
| boost::filesystem::path path = GetBusPath(config.bus()); |
| path /= GetDeviceDirectoryName(config); |
| return boost::filesystem::exists(path); |
| } |
| |
| absl::Status PeciSysfs::DeleteDevice(const HalCommonConfig& config) const { |
| // No `IsDevicePresent` check on this like in `NewDevice`, since it |
| // might be used to clean up after a device instantiation that was only |
| // partially successful |
| |
| boost::filesystem::path path = GetBusPath(config.bus()) / "delete_device"; |
| std::ofstream file(path.string()); |
| if (!file.good()) { |
| return absl::InternalError(absl::StrCat("Failed to open ", path.string())); |
| } |
| |
| // Peci requires explicitly passing driver name when deleting device. |
| // From |
| // https://github.com/openbmc/dbus-sensors/blob/master/src/intel-cpu/IntelCPUSensorMain.cpp#L463 |
| file << absl::StrCat("peci-client 0x", absl::Hex(config.address())) << "\n"; |
| file.flush(); |
| if (!file.good()) { |
| return absl::InternalError( |
| absl::StrCat("Failed to write to ", path.string())); |
| } |
| |
| return absl::OkStatus(); |
| } |
| |
| std::string PeciSysfs::GetDeviceDirectoryName( |
| const HalCommonConfig& config) const { |
| return absl::StrCat(config.bus(), "-", absl::Hex(config.address())); |
| } |
| |
| } // namespace milotic_tlbmc |