diff --git a/fboss/platform/platform_manager/PciExplorer.cpp b/fboss/platform/platform_manager/PciExplorer.cpp index bd1fb146e9140..136660237fb38 100644 --- a/fboss/platform/platform_manager/PciExplorer.cpp +++ b/fboss/platform/platform_manager/PciExplorer.cpp @@ -499,45 +499,70 @@ std::map PciExplorer::getSpiDeviceCharDevPaths( if (!re2::RE2::FullMatch(spiDevId, kSpiDevIdRe, &busNum, &chipSelect)) { continue; } + // Find corresponding SpiDeviceConfig + auto spiDeviceConfigItr = std::find_if( + spiMasterConfig.spiDeviceConfigs()->begin(), + spiMasterConfig.spiDeviceConfigs()->end(), + [chipSelect](auto spiDeviceConfig) { + return *spiDeviceConfig.chipSelect() == chipSelect; + }); + if (spiDeviceConfigItr == spiMasterConfig.spiDeviceConfigs()->end()) { + throw PciSubDeviceRuntimeError( + fmt::format( + "Unexpected SpiDevice created at {}. \ + No matching SpiDeviceConfig defined with ChipSelect {} for SpiDevice {}", + childDirEntry.path().string(), + chipSelect, + *spiDeviceConfigItr->pmUnitScopedName()), + *spiDeviceConfigItr->pmUnitScopedName()); + } auto spiCharDevPath = fmt::format("/dev/spidev{}.{}", busNum, chipSelect); if (!fs::exists(spiCharDevPath)) { // 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)); + auto overrideDriver = + PlatformUtils() + .execCommand(fmt::format( + "echo spidev > /sys/bus/spi/devices/{}/driver_override", + spiDevId)) + .first == 0; + if (!overrideDriver) { + throw PciSubDeviceRuntimeError( + fmt::format( + "Failed overridng SpiDriver spidev to /sys/bus/spi/devices/{}/driver_override " + "for SpiDevice {}", + spiDevId, + *spiDeviceConfigItr->pmUnitScopedName()), + *spiDeviceConfigItr->pmUnitScopedName()); + } // Bind SpiDevice to spidev driver in order to create its char device. - PlatformUtils().execCommand(fmt::format( - "echo {} > /sys/bus/spi/drivers/spidev/bind", spiDevId)); + auto bindSpiDev = + PlatformUtils() + .execCommand(fmt::format( + "echo {} > /sys/bus/spi/drivers/spidev/bind", spiDevId)) + .first == 0; + if (!bindSpiDev) { + throw PciSubDeviceRuntimeError( + fmt::format( + "Failed binding SpiDevice {} to /sys/bus/spi/drivers/spidev/bind for SpiDevice {}", + spiDevId, + *spiDeviceConfigItr->pmUnitScopedName()), + *spiDeviceConfigItr->pmUnitScopedName()); + } XLOG(INFO) << fmt::format( - "Completed binding SpiDevice {} to {} for SpiController {}", + "Completed initializing SpiDevice {} as {} for SpiDevice {}", spiDevId, spiCharDevPath, - *spiMasterConfig.fpgaIpBlockConfig()->pmUnitScopedName()); + *spiDeviceConfigItr->pmUnitScopedName()); } else { XLOG(INFO) << fmt::format( - "{} already exists. Skipping binding SpiDevice {} for SpiController {}", + "{} already exists. Skipping binding SpiDevice {} for SpiDevice {}", spiCharDevPath, spiDevId, - *spiMasterConfig.fpgaIpBlockConfig()->pmUnitScopedName()); - } - auto itr = std::find_if( - spiMasterConfig.spiDeviceConfigs()->begin(), - spiMasterConfig.spiDeviceConfigs()->end(), - [chipSelect](auto spiDeviceConfig) { - return *spiDeviceConfig.chipSelect() == chipSelect; - }); - if (itr == spiMasterConfig.spiDeviceConfigs()->end()) { - throw PciSubDeviceRuntimeError( - fmt::format( - "Unexpected SpiDevice created at {}. \ - No matching SpiDeviceConfig defined with ChipSelect {} for SpiController {}", - childDirEntry.path().string(), - chipSelect, - *spiMasterConfig.fpgaIpBlockConfig()->pmUnitScopedName()), - *spiMasterConfig.fpgaIpBlockConfig()->pmUnitScopedName()); + *spiDeviceConfigItr->pmUnitScopedName()); } - spiCharDevPaths[*itr->pmUnitScopedName()] = spiCharDevPath; + spiCharDevPaths[*spiDeviceConfigItr->pmUnitScopedName()] = spiCharDevPath; } } return spiCharDevPaths;