Skip to content

Commit

Permalink
Reduce API:
Browse files Browse the repository at this point in the history
* Remove PCIReadVendorID()
* Remove PCIReadDeviceID()
* Replace usage with PCIReadConfigSpace

Signed-off-by: Christopher Meis <christopher.meis@9elements.com>
  • Loading branch information
ChriMarMe committed Sep 29, 2021
1 parent a1b0104 commit adfc46f
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 51 deletions.
12 changes: 9 additions & 3 deletions cmd/test_pci_walk/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
2 changes: 0 additions & 2 deletions pkg/hwapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
21 changes: 9 additions & 12 deletions pkg/hwapi/hostbridge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down Expand Up @@ -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
}

Expand All @@ -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
}

Expand Down Expand Up @@ -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
}

Expand Down
18 changes: 0 additions & 18 deletions pkg/hwapi/pci.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 11 additions & 16 deletions pkg/hwapi/pci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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, &reg16); 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, &reg16); err != nil {
t.Errorf("PCIReadDeviceID failed with error %v", err)
}
if reg16 != 0x2918 {
Expand All @@ -63,15 +62,15 @@ func TestPCIQemu(t *testing.T) {
}
class |= uint16(reg8) << 8

if err = h.PCIReadConfigSpace(d0f0, 0xb, &reg8); err != nil {
if err := h.PCIReadConfigSpace(d0f0, 0xb, &reg8); err != nil {
t.Errorf("PCIReadConfig failed with error %v", err)
}
if reg8 != 6 {
t.Errorf("Unexpected value: %v", reg8)
}
class |= uint16(reg8)

if err = h.PCIReadConfigSpace(d0f0, 0xb, &reg16); err != nil {
if err := h.PCIReadConfigSpace(d0f0, 0xb, &reg16); err != nil {
t.Errorf("PCIReadConfig failed with error %v", err)
}
if reg16 != class {
Expand All @@ -91,7 +90,7 @@ func TestPCIQemu(t *testing.T) {
t.Errorf("Unexpected value: %x", reg32)
}

if err = h.PCIReadConfigSpace(d1f0, 0x18, &reg32); err != nil {
if err := h.PCIReadConfigSpace(d1f0, 0x18, &reg32); err != nil {
t.Errorf("PCIReadConfig failed with error %v", err)
}
if reg32 == 0 {
Expand All @@ -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, &reg32); err != nil {
if err := h.PCIReadConfigSpace(d1f0, 0x10, &reg32); 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)
}
}
Expand Down Expand Up @@ -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, &reg16); 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, &reg16); err != nil {
t.Errorf("PCIReadDeviceID failed with error %v", err)
}
if reg16 != 0x2918 {
Expand Down

0 comments on commit adfc46f

Please sign in to comment.