Merge pull request #662 from dgibson/fix611

drivers: Correct isPCIeDevice logic
This commit is contained in:
Julio Montes 2020-09-03 08:06:23 -05:00 committed by GitHub
commit c7745a3350
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 12 additions and 29 deletions

View File

@ -89,9 +89,6 @@ var SysIOMMUPath = "/sys/kernel/iommu_groups"
// SysBusPciDevicesPath is static string of /sys/bus/pci/devices // SysBusPciDevicesPath is static string of /sys/bus/pci/devices
var SysBusPciDevicesPath = "/sys/bus/pci/devices" var SysBusPciDevicesPath = "/sys/bus/pci/devices"
// SysBusPciSlotsPath is static string of /sys/bus/pci/slots
var SysBusPciSlotsPath = "/sys/bus/pci/slots"
var getSysDevPath = getSysDevPathImpl var getSysDevPath = getSysDevPathImpl
// DeviceInfo is an embedded type that contains device data common to all types of devices. // DeviceInfo is an embedded type that contains device data common to all types of devices.

View File

@ -9,6 +9,7 @@ package drivers
import ( import (
"fmt" "fmt"
"io/ioutil" "io/ioutil"
"os"
"path/filepath" "path/filepath"
"strings" "strings"
@ -22,6 +23,8 @@ const (
PCIDomain = "0000" PCIDomain = "0000"
PCIeKeyword = "PCIe" PCIeKeyword = "PCIe"
PCIConfigSpaceSize = 256
) )
type PCISysFsType string type PCISysFsType string
@ -52,23 +55,17 @@ func isPCIeDevice(bdf string) bool {
if len(strings.Split(bdf, ":")) == 2 { if len(strings.Split(bdf, ":")) == 2 {
bdf = PCIDomain + ":" + bdf bdf = PCIDomain + ":" + bdf
} }
slots, err := ioutil.ReadDir(config.SysBusPciSlotsPath)
configPath := filepath.Join(config.SysBusPciDevicesPath, bdf, "config")
fi, err := os.Stat(configPath)
if err != nil { if err != nil {
deviceLogger().WithError(err).WithField("path", config.SysBusPciSlotsPath).Warn("failed to list pci slots") deviceLogger().WithField("dev-bdf", bdf).WithField("error", err).Warning("Couldn't stat() configuration space file")
return false return false //Who knows?
} }
b := strings.Split(bdf, ".")[0]
for _, slot := range slots { // Plain PCI devices hav 256 bytes of configuration space,
address := getPCISlotProperty(slot.Name(), PCISysFsSlotsAddress) // PCI-Express devices have 4096 bytes
if b == address { return fi.Size() > PCIConfigSpaceSize
maxBusSpeed := getPCISlotProperty(slot.Name(), PCISysFsSlotsMaxBusSpeed)
if strings.Contains(maxBusSpeed, PCIeKeyword) {
return true
}
}
}
deviceLogger().WithField("dev-bdf", bdf).Debug("can not find slot for bdf of pci device")
return false
} }
// read from /sys/bus/pci/devices/xxx/property // read from /sys/bus/pci/devices/xxx/property
@ -85,17 +82,6 @@ func getPCIDeviceProperty(bdf string, property PCISysFsProperty) string {
return rlt return rlt
} }
// read from /sys/bus/pci/slots/xxx/property
func getPCISlotProperty(slot string, property PCISysFsProperty) string {
propertyPath := filepath.Join(config.SysBusPciSlotsPath, slot, string(property))
rlt, err := readPCIProperty(propertyPath)
if err != nil {
deviceLogger().WithError(err).WithField("path", propertyPath).Warn("failed to read pci slot property")
return ""
}
return rlt
}
func readPCIProperty(propertyPath string) (string, error) { func readPCIProperty(propertyPath string) (string, error) {
var ( var (
buf []byte buf []byte