mirror of
https://github.com/clearml/go-nvlib
synced 2025-06-26 18:28:08 +00:00
Fix the linting errors
This commit is contained in:
parent
96f9d0d39e
commit
2c175dcdbf
@ -21,10 +21,12 @@ import (
|
|||||||
"unsafe"
|
"unsafe"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Raw returns just the bytes without any assumptions about layout
|
||||||
type Raw interface {
|
type Raw interface {
|
||||||
Raw() *[]byte
|
Raw() *[]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reader used to read various data sizes in the byte array
|
||||||
type Reader interface {
|
type Reader interface {
|
||||||
Read8(pos int) uint8
|
Read8(pos int) uint8
|
||||||
Read16(pos int) uint16
|
Read16(pos int) uint16
|
||||||
@ -33,6 +35,7 @@ type Reader interface {
|
|||||||
Len() int
|
Len() int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Writer used to write various sizes of data in the byte array
|
||||||
type Writer interface {
|
type Writer interface {
|
||||||
Write8(pos int, value uint8)
|
Write8(pos int, value uint8)
|
||||||
Write16(pos int, value uint16)
|
Write16(pos int, value uint16)
|
||||||
@ -41,6 +44,7 @@ type Writer interface {
|
|||||||
Len() int
|
Len() int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Bytes object for manipulating arbitrary byte arrays
|
||||||
type Bytes interface {
|
type Bytes interface {
|
||||||
Raw
|
Raw
|
||||||
Reader
|
Reader
|
||||||
@ -66,22 +70,25 @@ func init() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New raw bytearray
|
||||||
func New(data *[]byte) Bytes {
|
func New(data *[]byte) Bytes {
|
||||||
return (*native)(data)
|
return (*native)(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewLittleEndian little endian ordering of bytes
|
||||||
func NewLittleEndian(data *[]byte) Bytes {
|
func NewLittleEndian(data *[]byte) Bytes {
|
||||||
if nativeByteOrder == binary.LittleEndian {
|
if nativeByteOrder == binary.LittleEndian {
|
||||||
return (*native)(data)
|
return (*native)(data)
|
||||||
} else {
|
|
||||||
return (*swapbo)(data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (*swapbo)(data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewBigEndian big endian ordering of bytes
|
||||||
func NewBigEndian(data *[]byte) Bytes {
|
func NewBigEndian(data *[]byte) Bytes {
|
||||||
if nativeByteOrder == binary.BigEndian {
|
if nativeByteOrder == binary.BigEndian {
|
||||||
return (*native)(data)
|
return (*native)(data)
|
||||||
} else {
|
|
||||||
return (*swapbo)(data)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
return (*swapbo)(data)
|
||||||
}
|
}
|
||||||
|
@ -29,10 +29,12 @@ const (
|
|||||||
pciCapabilityListPointer = 0x34
|
pciCapabilityListPointer = 0x34
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ConfigSpace PCI configuration space (standard extended) file path
|
||||||
type ConfigSpace struct {
|
type ConfigSpace struct {
|
||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ConfigSpaceIO Interface for reading and writing raw and preconfigured values
|
||||||
type ConfigSpaceIO interface {
|
type ConfigSpaceIO interface {
|
||||||
bytes.Bytes
|
bytes.Bytes
|
||||||
GetVendorID() uint16
|
GetVendorID() uint16
|
||||||
@ -44,15 +46,18 @@ type configSpaceIO struct {
|
|||||||
bytes.Bytes
|
bytes.Bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PCIStandardCapability standard PCI config space
|
||||||
type PCIStandardCapability struct {
|
type PCIStandardCapability struct {
|
||||||
bytes.Bytes
|
bytes.Bytes
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PCIExtendedCapability extended PCI config space
|
||||||
type PCIExtendedCapability struct {
|
type PCIExtendedCapability struct {
|
||||||
bytes.Bytes
|
bytes.Bytes
|
||||||
Version uint8
|
Version uint8
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// PCICapabilities combines the standard and extended config space
|
||||||
type PCICapabilities struct {
|
type PCICapabilities struct {
|
||||||
Standard map[uint8]*PCIStandardCapability
|
Standard map[uint8]*PCIStandardCapability
|
||||||
Extended map[uint16]*PCIExtendedCapability
|
Extended map[uint16]*PCIExtendedCapability
|
||||||
|
@ -25,6 +25,7 @@ import (
|
|||||||
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci/bytes"
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci/bytes"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// Mmio memory map a region
|
||||||
type Mmio interface {
|
type Mmio interface {
|
||||||
bytes.Raw
|
bytes.Raw
|
||||||
bytes.Reader
|
bytes.Reader
|
||||||
@ -41,25 +42,25 @@ type mmio struct {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func open(path string, offset int, size int, flags int) (Mmio, error) {
|
func open(path string, offset int, size int, flags int) (Mmio, error) {
|
||||||
var mmap_flags int
|
var mmapFlags int
|
||||||
switch flags {
|
switch flags {
|
||||||
case os.O_RDONLY:
|
case os.O_RDONLY:
|
||||||
mmap_flags = syscall.PROT_READ
|
mmapFlags = syscall.PROT_READ
|
||||||
case os.O_RDWR:
|
case os.O_RDWR:
|
||||||
mmap_flags = syscall.PROT_READ | syscall.PROT_WRITE
|
mmapFlags = syscall.PROT_READ | syscall.PROT_WRITE
|
||||||
default:
|
default:
|
||||||
return nil, fmt.Errorf("invalid flags: %v\n", flags)
|
return nil, fmt.Errorf("invalid flags: %v", flags)
|
||||||
}
|
}
|
||||||
|
|
||||||
file, err := os.OpenFile(path, flags, 0)
|
file, err := os.OpenFile(path, flags, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open file: %v\n", err)
|
return nil, fmt.Errorf("failed to open file: %v", err)
|
||||||
}
|
}
|
||||||
defer file.Close()
|
defer file.Close()
|
||||||
|
|
||||||
fi, err := file.Stat()
|
fi, err := file.Stat()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to get file info: %v\n", err)
|
return nil, fmt.Errorf("failed to get file info: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
if size > int(fi.Size()) {
|
if size > int(fi.Size()) {
|
||||||
@ -74,19 +75,21 @@ func open(path string, offset int, size int, flags int) (Mmio, error) {
|
|||||||
int(file.Fd()),
|
int(file.Fd()),
|
||||||
int64(offset),
|
int64(offset),
|
||||||
size,
|
size,
|
||||||
mmap_flags,
|
mmapFlags,
|
||||||
syscall.MAP_SHARED)
|
syscall.MAP_SHARED)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to mmap file: %v\n", err)
|
return nil, fmt.Errorf("failed to mmap file: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &mmio{bytes.New(&mmap)}, nil
|
return &mmio{bytes.New(&mmap)}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OpenRO open region readonly
|
||||||
func OpenRO(path string, offset int, size int) (Mmio, error) {
|
func OpenRO(path string, offset int, size int) (Mmio, error) {
|
||||||
return open(path, offset, size, os.O_RDONLY)
|
return open(path, offset, size, os.O_RDONLY)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OpenRW open region read write
|
||||||
func OpenRW(path string, offset int, size int) (Mmio, error) {
|
func OpenRW(path string, offset int, size int) (Mmio, error) {
|
||||||
return open(path, offset, size, os.O_RDWR)
|
return open(path, offset, size, os.O_RDWR)
|
||||||
}
|
}
|
||||||
@ -106,7 +109,7 @@ func (m *mmio) BigEndian() Mmio {
|
|||||||
func (m *mmio) Close() error {
|
func (m *mmio) Close() error {
|
||||||
err := syscall.Munmap(*m.Bytes.Raw())
|
err := syscall.Munmap(*m.Bytes.Raw())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to munmap file: %v\n", err)
|
return fmt.Errorf("failed to munmap file: %v", err)
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
@ -48,10 +48,12 @@ func mockOpen(source *[]byte, offset int, size int, rw bool) (Mmio, error) {
|
|||||||
return m, nil
|
return m, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MockOpenRO open read only
|
||||||
func MockOpenRO(source *[]byte, offset int, size int) (Mmio, error) {
|
func MockOpenRO(source *[]byte, offset int, size int) (Mmio, error) {
|
||||||
return mockOpen(source, offset, size, false)
|
return mockOpen(source, offset, size, false)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// MockOpenRW open read write
|
||||||
func MockOpenRW(source *[]byte, offset int, size int) (Mmio, error) {
|
func MockOpenRW(source *[]byte, offset int, size int) (Mmio, error) {
|
||||||
return mockOpen(source, offset, size, true)
|
return mockOpen(source, offset, size, true)
|
||||||
}
|
}
|
||||||
|
@ -25,12 +25,14 @@ import (
|
|||||||
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci/bytes"
|
"gitlab.com/nvidia/cloud-native/go-nvlib/pkg/nvpci/bytes"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MockNvpci mock pci device
|
||||||
type MockNvpci struct {
|
type MockNvpci struct {
|
||||||
*nvpci
|
*nvpci
|
||||||
}
|
}
|
||||||
|
|
||||||
var _ Interface = (*MockNvpci)(nil)
|
var _ Interface = (*MockNvpci)(nil)
|
||||||
|
|
||||||
|
// NewMockNvpci create new mock PCI and remove old devices
|
||||||
func NewMockNvpci() (mock *MockNvpci, rerr error) {
|
func NewMockNvpci() (mock *MockNvpci, rerr error) {
|
||||||
rootDir, err := ioutil.TempDir("", "")
|
rootDir, err := ioutil.TempDir("", "")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -49,10 +51,12 @@ func NewMockNvpci() (mock *MockNvpci, rerr error) {
|
|||||||
return mock, nil
|
return mock, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Cleanup remove the mocked PCI devices root folder
|
||||||
func (m *MockNvpci) Cleanup() {
|
func (m *MockNvpci) Cleanup() {
|
||||||
os.RemoveAll(m.pciDevicesRoot)
|
os.RemoveAll(m.pciDevicesRoot)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// AddMockA100 Create an A100 like GPU mock device
|
||||||
func (m *MockNvpci) AddMockA100(address string, numaNode int) error {
|
func (m *MockNvpci) AddMockA100(address string, numaNode int) error {
|
||||||
deviceDir := filepath.Join(m.pciDevicesRoot, address)
|
deviceDir := filepath.Join(m.pciDevicesRoot, address)
|
||||||
err := os.MkdirAll(deviceDir, 0755)
|
err := os.MkdirAll(deviceDir, 0755)
|
||||||
@ -111,6 +115,9 @@ func (m *MockNvpci) AddMockA100(address string, numaNode int) error {
|
|||||||
|
|
||||||
bar0 := []uint64{0x00000000c2000000, 0x00000000c2ffffff, 0x0000000000040200}
|
bar0 := []uint64{0x00000000c2000000, 0x00000000c2ffffff, 0x0000000000040200}
|
||||||
resource, err := os.Create(filepath.Join(deviceDir, "resource"))
|
resource, err := os.Create(filepath.Join(deviceDir, "resource"))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
_, err = resource.WriteString(fmt.Sprintf("0x%x 0x%x 0x%x", bar0[0], bar0[1], bar0[2]))
|
_, err = resource.WriteString(fmt.Sprintf("0x%x 0x%x 0x%x", bar0[0], bar0[1], bar0[2]))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
|
@ -66,30 +66,34 @@ type NvidiaPCIDevice struct {
|
|||||||
Resources map[int]*MemoryResource
|
Resources map[int]*MemoryResource
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsVGAController if class == 0x300
|
||||||
func (d *NvidiaPCIDevice) IsVGAController() bool {
|
func (d *NvidiaPCIDevice) IsVGAController() bool {
|
||||||
return d.Class == pciVgaControllerClass
|
return d.Class == pciVgaControllerClass
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Is3DController if class == 0x302
|
||||||
func (d *NvidiaPCIDevice) Is3DController() bool {
|
func (d *NvidiaPCIDevice) Is3DController() bool {
|
||||||
return d.Class == pci3dControllerClass
|
return d.Class == pci3dControllerClass
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsNVSwitch if classe == 0x068
|
||||||
func (d *NvidiaPCIDevice) IsNVSwitch() bool {
|
func (d *NvidiaPCIDevice) IsNVSwitch() bool {
|
||||||
return d.Class == pciNvSwitchClass
|
return d.Class == pciNvSwitchClass
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsGPU either VGA for older cards or 3D for newer
|
||||||
func (d *NvidiaPCIDevice) IsGPU() bool {
|
func (d *NvidiaPCIDevice) IsGPU() bool {
|
||||||
return d.IsVGAController() || d.Is3DController()
|
return d.IsVGAController() || d.Is3DController()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// IsResetAvailable some devices can be reset without rebooting,
|
||||||
|
// check if applicable
|
||||||
func (d *NvidiaPCIDevice) IsResetAvailable() bool {
|
func (d *NvidiaPCIDevice) IsResetAvailable() bool {
|
||||||
_, err := os.Stat(path.Join(d.Path, "reset"))
|
_, err := os.Stat(path.Join(d.Path, "reset"))
|
||||||
if err != nil {
|
return err == nil
|
||||||
return false
|
|
||||||
}
|
|
||||||
return true
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Reset perform a reset to apply a new configuration at HW level
|
||||||
func (d *NvidiaPCIDevice) Reset() error {
|
func (d *NvidiaPCIDevice) Reset() error {
|
||||||
err := ioutil.WriteFile(path.Join(d.Path, "reset"), []byte("1"), 0)
|
err := ioutil.WriteFile(path.Join(d.Path, "reset"), []byte("1"), 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -98,6 +102,7 @@ func (d *NvidiaPCIDevice) Reset() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// New interface that allows us to get a list of all NVIDIA PCI devices
|
||||||
func New() Interface {
|
func New() Interface {
|
||||||
return &nvpci{pciDevicesRoot}
|
return &nvpci{pciDevicesRoot}
|
||||||
}
|
}
|
||||||
@ -202,7 +207,7 @@ func (p *nvpci) GetAllDevices() ([]*NvidiaPCIDevice, error) {
|
|||||||
nvdevices = append(nvdevices, nvdevice)
|
nvdevices = append(nvdevices, nvdevice)
|
||||||
}
|
}
|
||||||
|
|
||||||
addressToId := func(address string) uint64 {
|
addressToID := func(address string) uint64 {
|
||||||
address = strings.ReplaceAll(address, ":", "")
|
address = strings.ReplaceAll(address, ":", "")
|
||||||
address = strings.ReplaceAll(address, ".", "")
|
address = strings.ReplaceAll(address, ".", "")
|
||||||
id, _ := strconv.ParseUint(address, 16, 64)
|
id, _ := strconv.ParseUint(address, 16, 64)
|
||||||
@ -210,7 +215,7 @@ func (p *nvpci) GetAllDevices() ([]*NvidiaPCIDevice, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
sort.Slice(nvdevices, func(i, j int) bool {
|
sort.Slice(nvdevices, func(i, j int) bool {
|
||||||
return addressToId(nvdevices[i].Address) < addressToId(nvdevices[j].Address)
|
return addressToID(nvdevices[i].Address) < addressToID(nvdevices[j].Address)
|
||||||
})
|
})
|
||||||
|
|
||||||
return nvdevices, nil
|
return nvdevices, nil
|
||||||
|
@ -28,6 +28,7 @@ const (
|
|||||||
pmcBigEndian = 0x01000001
|
pmcBigEndian = 0x01000001
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// MemoryResource represents a mmio region
|
||||||
type MemoryResource struct {
|
type MemoryResource struct {
|
||||||
Start uintptr
|
Start uintptr
|
||||||
End uintptr
|
End uintptr
|
||||||
@ -35,10 +36,11 @@ type MemoryResource struct {
|
|||||||
Path string
|
Path string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Open read write mmio region
|
||||||
func (mr *MemoryResource) Open() (mmio.Mmio, error) {
|
func (mr *MemoryResource) Open() (mmio.Mmio, error) {
|
||||||
rw, err := mmio.OpenRW(mr.Path, 0, int(mr.End-mr.Start+1))
|
rw, err := mmio.OpenRW(mr.Path, 0, int(mr.End-mr.Start+1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open file for mmio: %v\n", err)
|
return nil, fmt.Errorf("failed to open file for mmio: %v", err)
|
||||||
}
|
}
|
||||||
switch rw.Read32(pmcEndianRegister) {
|
switch rw.Read32(pmcEndianRegister) {
|
||||||
case pmcBigEndian:
|
case pmcBigEndian:
|
||||||
@ -46,13 +48,14 @@ func (mr *MemoryResource) Open() (mmio.Mmio, error) {
|
|||||||
case pmcLittleEndian:
|
case pmcLittleEndian:
|
||||||
return rw.LittleEndian(), nil
|
return rw.LittleEndian(), nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("unknown endianness for mmio: %v\n", err)
|
return nil, fmt.Errorf("unknown endianness for mmio: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// OpenReadOnly read only mmio region
|
||||||
func (mr *MemoryResource) OpenReadOnly() (mmio.Mmio, error) {
|
func (mr *MemoryResource) OpenReadOnly() (mmio.Mmio, error) {
|
||||||
ro, err := mmio.OpenRO(mr.Path, 0, int(mr.End-mr.Start+1))
|
ro, err := mmio.OpenRO(mr.Path, 0, int(mr.End-mr.Start+1))
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to open file for mmio: %v\n", err)
|
return nil, fmt.Errorf("failed to open file for mmio: %v", err)
|
||||||
}
|
}
|
||||||
switch ro.Read32(pmcEndianRegister) {
|
switch ro.Read32(pmcEndianRegister) {
|
||||||
case pmcBigEndian:
|
case pmcBigEndian:
|
||||||
@ -60,5 +63,5 @@ func (mr *MemoryResource) OpenReadOnly() (mmio.Mmio, error) {
|
|||||||
case pmcLittleEndian:
|
case pmcLittleEndian:
|
||||||
return ro.LittleEndian(), nil
|
return ro.LittleEndian(), nil
|
||||||
}
|
}
|
||||||
return nil, fmt.Errorf("unknown endianness for mmio: %v\n", err)
|
return nil, fmt.Errorf("unknown endianness for mmio: %v", err)
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user