forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpci_linux.go
More file actions
194 lines (179 loc) · 4.68 KB
/
Copy pathpci_linux.go
File metadata and controls
194 lines (179 loc) · 4.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
// Use and distribution licensed under the Apache license version 2.
//
// See the COPYING file in the root project directory for full text.
//
package ghw
import (
"fmt"
"io/ioutil"
"os"
"path/filepath"
"strings"
"github.com/jaypipes/pcidb"
)
const (
PATH_SYSFS_PCI_DEVICES = "/sys/bus/pci/devices"
)
func pciFillInfo(info *PCIInfo) error {
db, err := pcidb.New()
if err != nil {
return err
}
info.Classes = db.Classes
info.Vendors = db.Vendors
info.Products = db.Products
return nil
}
func getPCIDeviceModaliasPath(address string) string {
pciAddr := PCIAddressFromString(address)
if pciAddr == nil {
return ""
}
return filepath.Join(
PATH_SYSFS_PCI_DEVICES,
pciAddr.Domain+":"+pciAddr.Bus+":"+pciAddr.Slot+"."+pciAddr.Function,
"modalias",
)
}
// Returns a pointer to a PCIDevice struct that describes the PCI device at
// the requested address. If no such device could be found, returns nil
func (info *PCIInfo) GetDevice(address string) *PCIDevice {
fp := getPCIDeviceModaliasPath(address)
if fp == "" {
return nil
}
if _, err := os.Stat(fp); err != nil {
return nil
}
data, err := ioutil.ReadFile(fp)
if err != nil {
return nil
}
// The modalias file is an encoded file that looks like this:
//
// $ cat /sys/devices/pci0000\:00/0000\:00\:03.0/0000\:03\:00.0/modalias
// pci:v000010DEd00001C82sv00001043sd00008613bc03sc00i00
//
// It is interpreted like so:
//
// pci: -- ignore
// v000010DE -- PCI vendor ID
// d00001C82 -- PCI device ID (the product/model ID)
// sv00001043 -- PCI subsystem vendor ID
// sd00008613 -- PCI subsystem device ID (subdevice product/model ID)
// bc03 -- PCI base class
// sc00 -- PCI subclass
// i00 -- programming interface
vendorId := strings.ToLower(string(data[9:13]))
productId := strings.ToLower(string(data[18:22]))
subvendorId := strings.ToLower(string(data[28:32]))
subproductId := strings.ToLower(string(data[38:42]))
classId := string(data[44:46])
subclassId := string(data[48:50])
progIfaceId := string(data[51:53])
// Find the vendor
vendor := info.Vendors[vendorId]
if vendor == nil {
vendor = &pcidb.PCIVendor{
Id: vendorId,
Name: "UNKNOWN",
Products: []*pcidb.PCIProduct{},
}
}
// Find the product
product := info.Products[vendorId+productId]
if product == nil {
product = &pcidb.PCIProduct{
Id: productId,
Name: "UNKNOWN",
Subsystems: []*pcidb.PCIProduct{},
}
}
// Find the subsystem information
subvendor := info.Vendors[subvendorId]
var subsystem *pcidb.PCIProduct
if subvendor != nil && product != nil {
for _, p := range product.Subsystems {
if p.Id == subproductId {
subsystem = p
}
}
}
if subsystem == nil {
subsystem = &pcidb.PCIProduct{
VendorId: subvendorId,
Id: subproductId,
Name: "UNKNOWN",
}
}
// Find the class and subclass
class := info.Classes[classId]
var subclass *pcidb.PCISubclass
if class != nil {
for _, sc := range class.Subclasses {
if sc.Id == subclassId {
subclass = sc
}
}
} else {
class = &pcidb.PCIClass{
Id: classId,
Name: "UNKNOWN",
Subclasses: []*pcidb.PCISubclass{},
}
}
// Find the programming interface
var progIface *pcidb.PCIProgrammingInterface
if subclass != nil {
for _, pi := range subclass.ProgrammingInterfaces {
if pi.Id == progIfaceId {
progIface = pi
}
}
} else {
subclass = &pcidb.PCISubclass{
Id: subclassId,
Name: "UNKNOWN",
ProgrammingInterfaces: []*pcidb.PCIProgrammingInterface{},
}
}
if progIface == nil {
progIface = &pcidb.PCIProgrammingInterface{
Id: progIfaceId,
Name: "UNKNOWN",
}
}
return &PCIDevice{
Address: address,
Vendor: vendor,
Subsystem: subsystem,
Product: product,
Class: class,
Subclass: subclass,
ProgrammingInterface: progIface,
}
}
// Returns a list of pointers to PCIDevice structs present on the host system
func (info *PCIInfo) ListDevices() []*PCIDevice {
devs := make([]*PCIDevice, 0)
// We scan the /sys/bus/pci/devices directory which contains a collection
// of symlinks. The names of the symlinks are all the known PCI addresses
// for the host. For each address, we grab a *PCIDevice matching the
// address and append to the returned array.
links, err := ioutil.ReadDir(PATH_SYSFS_PCI_DEVICES)
if err != nil {
fmt.Fprintf(os.Stderr, "error: failed to read /sys/bus/pci/devices")
return nil
}
var dev *PCIDevice
for _, link := range links {
addr := link.Name()
dev = info.GetDevice(addr)
if dev == nil {
fmt.Fprintf(os.Stderr, "error: failed to get device information for PCI address %s\n", addr)
} else {
devs = append(devs, dev)
}
}
return devs
}