Skip to content

Commit

Permalink
Use PlatformFsUtils in PciExplorer/PciDevice
Browse files Browse the repository at this point in the history
Summary: TSIA

Reviewed By: tao-ren

Differential Revision: D66529269

fbshipit-source-id: c593a18f016e73e3c48283e2c9f67a21da600182
  • Loading branch information
rationalis authored and facebook-github-bot committed Dec 5, 2024
1 parent 0e622b7 commit c5ab5fc
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 14 deletions.
1 change: 1 addition & 0 deletions fboss/platform/platform_manager/BUCK
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
28 changes: 17 additions & 11 deletions fboss/platform/platform_manager/PciExplorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
#include <folly/String.h>
#include <folly/logging/xlog.h>

#include "fboss/platform/helpers/PlatformFsUtils.h"
#include "fboss/platform/platform_manager/I2cExplorer.h"
#include "fboss/platform/platform_manager/Utils.h"

Expand Down Expand Up @@ -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> 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
Expand Down Expand Up @@ -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
Expand All @@ -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() {
Expand Down Expand Up @@ -191,6 +193,9 @@ std::string PciDevice::charDevPath() const {
return charDevPath_;
}

PciExplorer::PciExplorer(const std::shared_ptr<PlatformFsUtils> platformFsUtils)
: platformFsUtils_(std::move(platformFsUtils)) {}

std::vector<uint16_t> PciExplorer::createI2cAdapter(
const PciDevice& pciDevice,
const I2cAdapterConfig& i2cAdapterConfig,
Expand Down Expand Up @@ -490,11 +495,12 @@ std::map<std::string, std::string> 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,
Expand Down
12 changes: 11 additions & 1 deletion fboss/platform/platform_manager/PciExplorer.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#pragma once

#include <re2/re2.h>
#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"
Expand All @@ -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> platformFsUtils =
std::make_shared<PlatformFsUtils>());
std::string sysfsPath() const;
std::string charDevPath() const;

Expand All @@ -23,6 +27,7 @@ struct PciDevice {
std::string subSystemDeviceId_{};
std::string charDevPath_{};
std::string sysfsPath_{};
const std::shared_ptr<PlatformFsUtils> platformFsUtils_;

void checkSysfsReadiness();
void bindDriver(const std::string& desiredDriver);
Expand All @@ -31,6 +36,9 @@ struct PciDevice {

class PciExplorer {
public:
explicit PciExplorer(
const std::shared_ptr<PlatformFsUtils> platformFsUtils =
std::make_shared<PlatformFsUtils>());
// 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.
Expand Down Expand Up @@ -119,6 +127,8 @@ class PciExplorer {
uint32_t instanceId);

private:
const std::shared_ptr<PlatformFsUtils> platformFsUtils_;

std::vector<uint16_t> getI2cAdapterBusNums(
const PciDevice& pciDevice,
const I2cAdapterConfig& i2cAdapterConfig,
Expand Down
3 changes: 2 additions & 1 deletion fboss/platform/platform_manager/PlatformExplorer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ PlatformExplorer::PlatformExplorer(
const PlatformConfig& config,
const std::shared_ptr<PlatformFsUtils> platformFsUtils)
: platformConfig_(config),
pciExplorer_(platformFsUtils),
dataStore_(platformConfig_),
devicePathResolver_(dataStore_),
presenceChecker_(devicePathResolver_),
Expand Down Expand Up @@ -451,7 +452,7 @@ void PlatformExplorer::explorePciDevices(
const std::vector<PciDeviceConfig>& 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());
Expand Down
2 changes: 1 addition & 1 deletion fboss/platform/platform_manager/PlatformExplorer.h
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ class PlatformExplorer {

PlatformConfig platformConfig_{};
I2cExplorer i2cExplorer_{};
PciExplorer pciExplorer_{};
PciExplorer pciExplorer_;
CachedFbossEepromParser eepromParser_{};
DataStore dataStore_;
DevicePathResolver devicePathResolver_;
Expand Down

0 comments on commit c5ab5fc

Please sign in to comment.