diff --git a/fboss/platform/platform_manager/BUCK b/fboss/platform/platform_manager/BUCK index dbb6a0ca7d1dd..d267eeba9a271 100644 --- a/fboss/platform/platform_manager/BUCK +++ b/fboss/platform/platform_manager/BUCK @@ -87,6 +87,7 @@ cpp_library( ":i2c_explorer", ":platform_manager_config-cpp2-types", ":utils", + "//fboss/platform/helpers:platform_fs_utils", "//folly:file_util", "//folly:string", "//folly/logging:logging", diff --git a/fboss/platform/platform_manager/PciExplorer.cpp b/fboss/platform/platform_manager/PciExplorer.cpp index 70d0c1e325929..ccc1daecd9976 100644 --- a/fboss/platform/platform_manager/PciExplorer.cpp +++ b/fboss/platform/platform_manager/PciExplorer.cpp @@ -10,6 +10,7 @@ #include #include +#include "fboss/platform/helpers/PlatformFsUtils.h" #include "fboss/platform/platform_manager/I2cExplorer.h" #include "fboss/platform/platform_manager/Utils.h" @@ -50,12 +51,15 @@ fbiob_aux_data getAuxData( namespace facebook::fboss::platform::platform_manager { -PciDevice::PciDevice(const PciDeviceConfig& pciDeviceConfig) +PciDevice::PciDevice( + const PciDeviceConfig& pciDeviceConfig, + const std::shared_ptr platformFsUtils) : name_(*pciDeviceConfig.pmUnitScopedName()), vendorId_(*pciDeviceConfig.vendorId()), deviceId_(*pciDeviceConfig.deviceId()), subSystemVendorId_(*pciDeviceConfig.subSystemVendorId()), - subSystemDeviceId_(*pciDeviceConfig.subSystemDeviceId()) { + subSystemDeviceId_(*pciDeviceConfig.subSystemDeviceId()), + platformFsUtils_(std::move(platformFsUtils)) { checkSysfsReadiness(); // Note: bindDriver() needs to be called after checkSysfsReadiness() but @@ -126,14 +130,13 @@ void PciDevice::bindDriver(const std::string& desiredDriver) { return; } - auto desiredDriverPath = - fmt::format("/sys/bus/pci/drivers/{}", desiredDriver); + fs::path desiredDriverPath = fs::path("/sys/bus/pci/drivers") / desiredDriver; if (!fs::exists(desiredDriverPath)) { throw std::runtime_error(fmt::format( "Failed to bind driver {} to device {}: {} does not exist", desiredDriver, name_, - desiredDriverPath)); + desiredDriverPath.string())); } // Add PCI device ID to the driver's "new_id" file. Check below doc for @@ -150,8 +153,7 @@ void PciDevice::bindDriver(const std::string& desiredDriver) { name_, pciDevId, desiredDriver); - auto cmd = fmt::format("echo {} > {}/new_id", pciDevId, desiredDriverPath); - PlatformUtils().execCommand(cmd); + platformFsUtils_->writeStringToFile(pciDevId, desiredDriverPath / "new_id"); } void PciDevice::checkCharDevReadiness() { @@ -191,6 +193,9 @@ std::string PciDevice::charDevPath() const { return charDevPath_; } +PciExplorer::PciExplorer(const std::shared_ptr platformFsUtils) + : platformFsUtils_(std::move(platformFsUtils)) {} + std::vector PciExplorer::createI2cAdapter( const PciDevice& pciDevice, const I2cAdapterConfig& i2cAdapterConfig, @@ -490,11 +495,12 @@ std::map PciExplorer::getSpiDeviceCharDevPaths( // For more details on the two commands: // https://github.com/torvalds/linux/blob/master/Documentation/spi/spidev.rst#device-creation-driver-binding // Overriding driver of the SpiDevice so spidev doesn't fail to probe. - PlatformUtils().execCommand(fmt::format( - "echo spidev > /sys/bus/spi/devices/{}/driver_override", spiDevId)); + platformFsUtils_->writeStringToFile( + "spidev", + fs::path("/sys/bus/spi/devices") / spiDevId / "driver_override"); // Bind SpiDevice to spidev driver in order to create its char device. - PlatformUtils().execCommand(fmt::format( - "echo {} > /sys/bus/spi/drivers/spidev/bind", spiDevId)); + platformFsUtils_->writeStringToFile( + spiDevId, "/sys/bus/spi/drivers/spidev/bind"); XLOG(INFO) << fmt::format( "Completed binding SpiDevice {} to {} for SpiController {}", spiDevId, diff --git a/fboss/platform/platform_manager/PciExplorer.h b/fboss/platform/platform_manager/PciExplorer.h index 2c5650a7bb2be..354c0c7408439 100644 --- a/fboss/platform/platform_manager/PciExplorer.h +++ b/fboss/platform/platform_manager/PciExplorer.h @@ -3,6 +3,7 @@ #pragma once #include +#include "fboss/platform/helpers/PlatformFsUtils.h" #include "fboss/platform/platform_manager/gen-cpp2/platform_manager_config_types.h" #include "fboss/platform/platform_manager/uapi/fbiob-ioctl.h" @@ -11,7 +12,10 @@ namespace facebook::fboss::platform::platform_manager { struct PciDevice { public: - explicit PciDevice(const PciDeviceConfig& pciDevConfig); + explicit PciDevice( + const PciDeviceConfig& pciDevConfig, + const std::shared_ptr platformFsUtils = + std::make_shared()); std::string sysfsPath() const; std::string charDevPath() const; @@ -23,6 +27,7 @@ struct PciDevice { std::string subSystemDeviceId_{}; std::string charDevPath_{}; std::string sysfsPath_{}; + const std::shared_ptr platformFsUtils_; void checkSysfsReadiness(); void bindDriver(const std::string& desiredDriver); @@ -31,6 +36,9 @@ struct PciDevice { class PciExplorer { public: + explicit PciExplorer( + const std::shared_ptr platformFsUtils = + std::make_shared()); // Create the I2C Adapter based on the given i2cAdapterConfig residing // at the given PciDevice path. It returns the the kernel assigned i2c bus // number(s) for the created adapter(s). Throw std::runtime_error on failure. @@ -119,6 +127,8 @@ class PciExplorer { uint32_t instanceId); private: + const std::shared_ptr platformFsUtils_; + std::vector getI2cAdapterBusNums( const PciDevice& pciDevice, const I2cAdapterConfig& i2cAdapterConfig, diff --git a/fboss/platform/platform_manager/PlatformExplorer.cpp b/fboss/platform/platform_manager/PlatformExplorer.cpp index e6f3f64eedfa0..71cf26cdb91cf 100644 --- a/fboss/platform/platform_manager/PlatformExplorer.cpp +++ b/fboss/platform/platform_manager/PlatformExplorer.cpp @@ -129,6 +129,7 @@ PlatformExplorer::PlatformExplorer( const PlatformConfig& config, const std::shared_ptr platformFsUtils) : platformConfig_(config), + pciExplorer_(platformFsUtils), dataStore_(platformConfig_), devicePathResolver_(dataStore_), presenceChecker_(devicePathResolver_), @@ -451,7 +452,7 @@ void PlatformExplorer::explorePciDevices( const std::vector& pciDeviceConfigs) { for (const auto& pciDeviceConfig : pciDeviceConfigs) { try { - auto pciDevice = PciDevice(pciDeviceConfig); + auto pciDevice = PciDevice(pciDeviceConfig, platformFsUtils_); auto charDevPath = pciDevice.charDevPath(); auto instId = getFpgaInstanceId(slotPath, *pciDeviceConfig.pmUnitScopedName()); diff --git a/fboss/platform/platform_manager/PlatformExplorer.h b/fboss/platform/platform_manager/PlatformExplorer.h index 74d585ed1412b..dbb75b0fe2233 100644 --- a/fboss/platform/platform_manager/PlatformExplorer.h +++ b/fboss/platform/platform_manager/PlatformExplorer.h @@ -111,7 +111,7 @@ class PlatformExplorer { PlatformConfig platformConfig_{}; I2cExplorer i2cExplorer_{}; - PciExplorer pciExplorer_{}; + PciExplorer pciExplorer_; CachedFbossEepromParser eepromParser_{}; DataStore dataStore_; DevicePathResolver devicePathResolver_;