This repository was archived by the owner on May 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 367
Correct isPCIeDevice logic #2889
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ package drivers | |
| import ( | ||
| "fmt" | ||
| "io/ioutil" | ||
| "os" | ||
| "path/filepath" | ||
| "strings" | ||
|
|
||
|
|
@@ -22,6 +23,8 @@ const ( | |
|
|
||
| PCIDomain = "0000" | ||
| PCIeKeyword = "PCIe" | ||
|
|
||
| PCIConfigSpaceSize = 256 | ||
| ) | ||
|
|
||
| type PCISysFsType string | ||
|
|
@@ -52,23 +55,17 @@ func isPCIeDevice(bdf string) bool { | |
| if len(strings.Split(bdf, ":")) == 2 { | ||
| bdf = PCIDomain + ":" + bdf | ||
| } | ||
| slots, err := ioutil.ReadDir(config.SysBusPciSlotsPath) | ||
|
|
||
| configPath := filepath.Join(config.SysBusPciDevicesPath, bdf, "config") | ||
| fi, err := os.Stat(configPath) | ||
| if err != nil { | ||
| deviceLogger().WithError(err).WithField("path", config.SysBusPciSlotsPath).Warn("failed to list pci slots") | ||
| return false | ||
| } | ||
| b := strings.Split(bdf, ".")[0] | ||
| for _, slot := range slots { | ||
| address := getPCISlotProperty(slot.Name(), PCISysFsSlotsAddress) | ||
| if b == address { | ||
| maxBusSpeed := getPCISlotProperty(slot.Name(), PCISysFsSlotsMaxBusSpeed) | ||
| if strings.Contains(maxBusSpeed, PCIeKeyword) { | ||
| return true | ||
| } | ||
| } | ||
| deviceLogger().WithField("dev-bdf", bdf).WithField("error", err).Warning("Couldn't stat() configuration space file") | ||
| return false //Who knows? | ||
| } | ||
| deviceLogger().WithField("dev-bdf", bdf).Debug("can not find slot for bdf of pci device") | ||
| return false | ||
|
|
||
| // Plain PCI devices hav 256 bytes of configuration space, | ||
| // PCI-Express devices have 4096 bytes | ||
| return fi.Size() > PCIConfigSpaceSize | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I was going to say it might be safer to check if the size is exactly 4096. But presumably if another PCI standard is released that would be a superset of PCI-e (as PCI-e is to PCI) so I guess this is "safe" as-is 😄
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Right, and my guess would be that that hypothetical extension will probably behave closer to PCI express than classic PCI. |
||
| } | ||
|
|
||
| // read from /sys/bus/pci/devices/xxx/property | ||
|
|
@@ -85,17 +82,6 @@ func getPCIDeviceProperty(bdf string, property PCISysFsProperty) string { | |
| 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) { | ||
| var ( | ||
| buf []byte | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think you can use
WithError(err), instead ofWithField("error", err), but that's a nitpick.