forked from jaypipes/ghw
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pci_linux.go
293 lines (275 loc) · 7.75 KB
/
pci_linux.go
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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// 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"
)
func (ctx *context) pciFillInfo(info *PCIInfo) error {
db, err := pcidb.New(pcidb.WithChroot(ctx.chroot))
if err != nil {
return err
}
info.Classes = db.Classes
info.Vendors = db.Vendors
info.Products = db.Products
info.ctx = ctx
return nil
}
func (ctx *context) getPCIDeviceModaliasPath(address string) string {
pciAddr := PCIAddressFromString(address)
if pciAddr == nil {
return ""
}
return filepath.Join(
ctx.pathSysBusPciDevices(),
pciAddr.Domain+":"+pciAddr.Bus+":"+pciAddr.Slot+"."+pciAddr.Function,
"modalias",
)
}
type deviceModaliasInfo struct {
vendorID string
productID string
subproductID string
subvendorID string
classID string
subclassID string
progIfaceID string
}
func parseModaliasFile(fp string) *deviceModaliasInfo {
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])
return &deviceModaliasInfo{
vendorID: vendorID,
productID: productID,
subproductID: subproductID,
subvendorID: subvendorID,
classID: classID,
subclassID: subclassID,
progIfaceID: progIfaceID,
}
}
// Returns a pointer to a pcidb.Vendor struct matching the supplied vendor
// ID string. If no such vendor ID string could be found, returns the
// pcidb.Vendor struct populated with "unknown" vendor Name attribute and
// empty Products attribute.
func findPCIVendor(info *PCIInfo, vendorID string) *pcidb.Vendor {
vendor := info.Vendors[vendorID]
if vendor == nil {
return &pcidb.Vendor{
ID: vendorID,
Name: UNKNOWN,
Products: []*pcidb.Product{},
}
}
return vendor
}
// Returns a pointer to a pcidb.Product struct matching the supplied vendor
// and product ID strings. If no such product could be found, returns the
// pcidb.Product struct populated with "unknown" product Name attribute and
// empty Subsystems attribute.
func findPCIProduct(
info *PCIInfo,
vendorID string,
productID string,
) *pcidb.Product {
product := info.Products[vendorID+productID]
if product == nil {
return &pcidb.Product{
ID: productID,
Name: UNKNOWN,
Subsystems: []*pcidb.Product{},
}
}
return product
}
// Returns a pointer to a pcidb.Product struct matching the supplied vendor,
// product, subvendor and subproduct ID strings. If no such product could be
// found, returns the pcidb.Product struct populated with "unknown" product
// Name attribute and empty Subsystems attribute.
func findPCISubsystem(
info *PCIInfo,
vendorID string,
productID string,
subvendorID string,
subproductID string,
) *pcidb.Product {
product := info.Products[vendorID+productID]
subvendor := info.Vendors[subvendorID]
if subvendor != nil && product != nil {
for _, p := range product.Subsystems {
if p.ID == subproductID {
return p
}
}
}
return &pcidb.Product{
VendorID: subvendorID,
ID: subproductID,
Name: UNKNOWN,
}
}
// Returns a pointer to a pcidb.Class struct matching the supplied class ID
// string. If no such class ID string could be found, returns the
// pcidb.Class struct populated with "unknown" class Name attribute and
// empty Subclasses attribute.
func findPCIClass(info *PCIInfo, classID string) *pcidb.Class {
class := info.Classes[classID]
if class == nil {
return &pcidb.Class{
ID: classID,
Name: UNKNOWN,
Subclasses: []*pcidb.Subclass{},
}
}
return class
}
// Returns a pointer to a pcidb.Subclass struct matching the supplied class
// and subclass ID strings. If no such subclass could be found, returns the
// pcidb.Subclass struct populated with "unknown" subclass Name attribute
// and empty ProgrammingInterfaces attribute.
func findPCISubclass(
info *PCIInfo,
classID string,
subclassID string,
) *pcidb.Subclass {
class := info.Classes[classID]
if class != nil {
for _, sc := range class.Subclasses {
if sc.ID == subclassID {
return sc
}
}
}
return &pcidb.Subclass{
ID: subclassID,
Name: UNKNOWN,
ProgrammingInterfaces: []*pcidb.ProgrammingInterface{},
}
}
// Returns a pointer to a pcidb.ProgrammingInterface struct matching the
// supplied class, subclass and programming interface ID strings. If no such
// programming interface could be found, returns the
// pcidb.ProgrammingInterface struct populated with "unknown" Name attribute
func findPCIProgrammingInterface(
info *PCIInfo,
classID string,
subclassID string,
progIfaceID string,
) *pcidb.ProgrammingInterface {
subclass := findPCISubclass(info, classID, subclassID)
for _, pi := range subclass.ProgrammingInterfaces {
if pi.ID == progIfaceID {
return pi
}
}
return &pcidb.ProgrammingInterface{
ID: progIfaceID,
Name: UNKNOWN,
}
}
// GetDevice 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 := info.ctx.getPCIDeviceModaliasPath(address)
if fp == "" {
return nil
}
modaliasInfo := parseModaliasFile(fp)
if modaliasInfo == nil {
return nil
}
vendor := findPCIVendor(info, modaliasInfo.vendorID)
product := findPCIProduct(
info,
modaliasInfo.vendorID,
modaliasInfo.productID,
)
subsystem := findPCISubsystem(
info,
modaliasInfo.vendorID,
modaliasInfo.productID,
modaliasInfo.subvendorID,
modaliasInfo.subproductID,
)
class := findPCIClass(info, modaliasInfo.classID)
subclass := findPCISubclass(
info,
modaliasInfo.classID,
modaliasInfo.subclassID,
)
progIface := findPCIProgrammingInterface(
info,
modaliasInfo.classID,
modaliasInfo.subclassID,
modaliasInfo.progIfaceID,
)
return &PCIDevice{
Address: address,
Vendor: vendor,
Subsystem: subsystem,
Product: product,
Class: class,
Subclass: subclass,
ProgrammingInterface: progIface,
}
}
// ListDevices 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(info.ctx.pathSysBusPciDevices())
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
}