From adfc46f288f2989094f3b02b8f4c87bab367be77 Mon Sep 17 00:00:00 2001 From: Christopher Meis Date: Wed, 29 Sep 2021 13:29:12 +0200 Subject: [PATCH] Reduce API: * Remove PCIReadVendorID() * Remove PCIReadDeviceID() * Replace usage with PCIReadConfigSpace Signed-off-by: Christopher Meis --- cmd/test_pci_walk/main.go | 12 +++++++++--- pkg/hwapi/api.go | 2 -- pkg/hwapi/hostbridge.go | 21 +++++++++------------ pkg/hwapi/pci.go | 18 ------------------ pkg/hwapi/pci_test.go | 27 +++++++++++---------------- 5 files changed, 29 insertions(+), 51 deletions(-) diff --git a/cmd/test_pci_walk/main.go b/cmd/test_pci_walk/main.go index 2bafdd0..f43396e 100644 --- a/cmd/test_pci_walk/main.go +++ b/cmd/test_pci_walk/main.go @@ -11,10 +11,16 @@ func main() { h := hwapi.GetAPI() if err := h.PCIEnumerateVisibleDevices( func(d hwapi.PCIDevice) (abort bool) { - vend, _ := h.PCIReadVendorID(d) - devid, _ := h.PCIReadDeviceID(d) + var venid, devid uint16 + if err := h.PCIReadConfigSpace(d, 0, &venid); err != nil { + fmt.Fprintf(os.Stderr, "%v", err) + } + if err := h.PCIReadConfigSpace(d, 2, &devid); err != nil { + fmt.Fprintf(os.Stderr, "%v", err) + } + fmt.Printf("Found device: %02x:%02x.%x [%04x:%04x]\n", - d.Bus, d.Device, d.Function, vend, devid) + d.Bus, d.Device, d.Function, venid, devid) return false }); err != nil { fmt.Fprintf(os.Stderr, "PCIEnumerateVisibleDevices failed wiht: %v", err) diff --git a/pkg/hwapi/api.go b/pkg/hwapi/api.go index d6f165a..03c4898 100644 --- a/pkg/hwapi/api.go +++ b/pkg/hwapi/api.go @@ -40,8 +40,6 @@ type LowLevelHardwareInterfaces interface { PCIEnumerateVisibleDevices(cb func(d PCIDevice) (abort bool)) (err error) PCIReadConfigSpace(d PCIDevice, off int, out interface{}) error PCIWriteConfigSpace(d PCIDevice, off int, val interface{}) error - PCIReadVendorID(d PCIDevice) (uint16, error) - PCIReadDeviceID(d PCIDevice) (uint16, error) // hostbridge.go ReadHostBridgeTseg() (uint32, uint32, error) diff --git a/pkg/hwapi/hostbridge.go b/pkg/hwapi/hostbridge.go index 05d570c..0ab1312 100644 --- a/pkg/hwapi/hostbridge.go +++ b/pkg/hwapi/hostbridge.go @@ -109,16 +109,15 @@ func (h HwAPI) ReadHostBridgeTseg() (uint32, uint32, error) { var tsegLimitOff int var tsegBroadwellDEfix bool var devicenum int + var vendorid, deviceid uint16 - vendorid, err := h.PCIReadVendorID(pciHostbridge) - if err != nil { + if err := h.PCIReadConfigSpace(pciHostbridge, 0, &vendorid); err != nil { return 0, 0, err } if vendorid != 0x8086 { return 0, 0, fmt.Errorf("hostbridge is not made by Intel") } - deviceid, err := h.PCIReadDeviceID(pciHostbridge) - if err != nil { + if err := h.PCIReadConfigSpace(pciHostbridge, 2, &deviceid); err != nil { return 0, 0, err } @@ -158,11 +157,11 @@ func (h HwAPI) ReadHostBridgeTseg() (uint32, uint32, error) { var tsegbase uint32 var tseglimit uint32 - if err = h.PCIReadConfigSpace(tsegDev, tsegBaseOff, &tsegbase); err != nil { + if err := h.PCIReadConfigSpace(tsegDev, tsegBaseOff, &tsegbase); err != nil { return 0, 0, err } - if err = h.PCIReadConfigSpace(tsegDev, tsegLimitOff, &tseglimit); err != nil { + if err := h.PCIReadConfigSpace(tsegDev, tsegLimitOff, &tseglimit); err != nil { return 0, 0, err } @@ -188,16 +187,14 @@ func (h HwAPI) ReadHostBridgeDPR() (DMAProtectedRange, error) { var dprOff int var devicenum int var ret DMAProtectedRange - - vendorid, err := h.PCIReadVendorID(pciHostbridge) - if err != nil { + var vendorid, deviceid uint16 + if err := h.PCIReadConfigSpace(pciHostbridge, 0, &vendorid); err != nil { return ret, err } if vendorid != 0x8086 { return ret, fmt.Errorf("hostbridge is not made by Intel") } - deviceid, err := h.PCIReadDeviceID(pciHostbridge) - if err != nil { + if err := h.PCIReadConfigSpace(pciHostbridge, 2, &deviceid); err != nil { return ret, err } @@ -233,7 +230,7 @@ func (h HwAPI) ReadHostBridgeDPR() (DMAProtectedRange, error) { var u32 uint32 - if err = h.PCIReadConfigSpace(tsegDev, dprOff, &u32); err != nil { + if err := h.PCIReadConfigSpace(tsegDev, dprOff, &u32); err != nil { return ret, err } diff --git a/pkg/hwapi/pci.go b/pkg/hwapi/pci.go index 210875a..43fd6ec 100644 --- a/pkg/hwapi/pci.go +++ b/pkg/hwapi/pci.go @@ -75,24 +75,6 @@ func (h HwAPI) PCIReadConfigSpace(d PCIDevice, off int, out interface{}) (err er return } -//PCIReadVendorID reads the device vendor ID from PCI config space -func (h HwAPI) PCIReadVendorID(d PCIDevice) (id uint16, err error) { - if err = h.PCIReadConfigSpace(d, 0, &id); err != nil { - return 0, err - } - - return -} - -//PCIReadDeviceID reads the device ID from PCI config space -func (h HwAPI) PCIReadDeviceID(d PCIDevice) (id uint16, err error) { - if err = h.PCIReadConfigSpace(d, 2, &id); err != nil { - return 0, err - } - - return -} - //pciWriteConfigSpace writes to PCI config space from in func (h HwAPI) PCIWriteConfigSpace(d PCIDevice, off int, in interface{}) (err error) { var path string diff --git a/pkg/hwapi/pci_test.go b/pkg/hwapi/pci_test.go index d07e6d8..21f504f 100644 --- a/pkg/hwapi/pci_test.go +++ b/pkg/hwapi/pci_test.go @@ -37,15 +37,14 @@ func TestPCIQemu(t *testing.T) { Device: 0x1f, Function: 0, } - reg16, err := h.PCIReadVendorID(d1ff0) - if err != nil { + + if err := h.PCIReadConfigSpace(d1ff0, 0, ®16); err != nil { t.Errorf("PCIReadVendorID failed with error %v", err) } if reg16 != 0x8086 { t.Errorf("Unexpected value: %v", reg16) } - reg16, err = h.PCIReadDeviceID(d1ff0) - if err != nil { + if err := h.PCIReadConfigSpace(d1ff0, 2, ®16); err != nil { t.Errorf("PCIReadDeviceID failed with error %v", err) } if reg16 != 0x2918 { @@ -63,7 +62,7 @@ func TestPCIQemu(t *testing.T) { } class |= uint16(reg8) << 8 - if err = h.PCIReadConfigSpace(d0f0, 0xb, ®8); err != nil { + if err := h.PCIReadConfigSpace(d0f0, 0xb, ®8); err != nil { t.Errorf("PCIReadConfig failed with error %v", err) } if reg8 != 6 { @@ -71,7 +70,7 @@ func TestPCIQemu(t *testing.T) { } class |= uint16(reg8) - if err = h.PCIReadConfigSpace(d0f0, 0xb, ®16); err != nil { + if err := h.PCIReadConfigSpace(d0f0, 0xb, ®16); err != nil { t.Errorf("PCIReadConfig failed with error %v", err) } if reg16 != class { @@ -91,7 +90,7 @@ func TestPCIQemu(t *testing.T) { t.Errorf("Unexpected value: %x", reg32) } - if err = h.PCIReadConfigSpace(d1f0, 0x18, ®32); err != nil { + if err := h.PCIReadConfigSpace(d1f0, 0x18, ®32); err != nil { t.Errorf("PCIReadConfig failed with error %v", err) } if reg32 == 0 { @@ -107,21 +106,19 @@ func TestPCIQemu(t *testing.T) { } reg32 = 0xffffffff - err = h.PCIWriteConfigSpace(d1f0, 0x10, reg32) - if err != nil { + if err := h.PCIWriteConfigSpace(d1f0, 0x10, reg32); err != nil { t.Errorf("PCIWriteConfig32 failed with error %v", err) } // check if bits are moving - if err = h.PCIReadConfigSpace(d1f0, 0x10, ®32); err != nil { + if err := h.PCIReadConfigSpace(d1f0, 0x10, ®32); err != nil { t.Errorf("PCIReadConfig failed with error %v", err) } if reg32 == 0xfd000008 { t.Errorf("Unexpected value: %x", reg32) } - err = h.PCIWriteConfigSpace(d1f0, 0x10, backup) - if err != nil { + if err := h.PCIWriteConfigSpace(d1f0, 0x10, backup); err != nil { t.Errorf("PCIWriteConfig32 failed with error %v", err) } } @@ -156,15 +153,13 @@ func TestPCIDeviceVendorIDQemu(t *testing.T) { Device: 0x1f, Function: 0, } - reg16, err := h.PCIReadVendorID(d1ff0) - if err != nil { + if err := h.PCIReadConfigSpace(d1ff0, 0, ®16); err != nil { t.Errorf("PCIReadVendorID failed with error %v", err) } if reg16 != 0x8086 { t.Errorf("Unexpected value: %v", reg16) } - reg16, err = h.PCIReadDeviceID(d1ff0) - if err != nil { + if err := h.PCIReadConfigSpace(d1ff0, 2, ®16); err != nil { t.Errorf("PCIReadDeviceID failed with error %v", err) } if reg16 != 0x2918 {