-
Notifications
You must be signed in to change notification settings - Fork 32
/
driver.py
195 lines (146 loc) · 5.83 KB
/
driver.py
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
import os
from ...core import utils
from ...core.config import Config
from ...core.i2c_utils import I2cMsg
from ...core.log import getLogger
from ...core.driver.kernel.pci import PciKernelDriver
from ...core.driver.kernel.sysfs import (
FanSysfsImpl,
GpioSysfsImpl,
LedSysfsImpl,
ResetSysfsImpl,
)
from ...core.driver.user.i2c import I2cDevDriver
from ...libs.i2c import i2cBusFromName
logging = getLogger(__name__)
SCD_WAIT_TIMEOUT = 5.
class ScdKernelDriver(PciKernelDriver):
def __init__(self, scd=None, **kwargs):
self.scd = scd
super(ScdKernelDriver, self).__init__(module='scd-hwmon', **kwargs)
def __str__(self):
return '%s(addr=%s)' % (self.__class__.__name__, self.addr)
def writeComponents(self, components, filename):
PAGE_SIZE = 4096
data = []
data_size = 0
for entry in components:
entry_size = len(entry) + 1
if entry_size + data_size > PAGE_SIZE:
utils.writeConfig(self.addr.getSysfsPath(), {filename: '\n'.join(data)})
data_size = 0
data = []
data.append(entry)
data_size += entry_size
if data:
utils.writeConfig(self.addr.getSysfsPath(), {filename: '\n'.join(data)})
def waitReadySim(self):
logging.info('Waiting SCD %s.', os.path.join(self.addr.getSysfsPath(),
'smbus_tweaks'))
logging.info('Done.')
@utils.simulateWith(waitReadySim)
def waitReady(self):
path = os.path.join(self.addr.getSysfsPath(), 'smbus_tweaks')
utils.FileWaiter(path, SCD_WAIT_TIMEOUT).waitFileReady()
def getMasterNameForBus(self, bus):
bpm = next(iter(self.scd.smbusMasters.values()))['bus']
return self.getMasterName(bus // bpm, bus % bpm)
def getMasterName(self, master, bus):
return "SCD %s SMBus master %d bus %d" % (self.addr, master, bus)
def refresh(self):
# reload i2c bus cache
masterName = self.getMasterName(0, 0)
if not utils.inSimulation():
self.scd.i2cOffset = i2cBusFromName(masterName, force=True)
else:
self.scd.i2cOffset = 2
def setup(self):
super(ScdKernelDriver, self).setup()
scd = self.scd
data = []
for addr, info in scd.smbusMasters.items():
data += ["smbus_master %#x %d %d" % (addr, info['id'], info['bus'])]
for addr, info in scd.mdioMasters.items():
data += ["mdio_master %#x %d %d %d" % (addr, info['id'], info['bus'],
info['speed'])]
for mdio in scd.mdios:
data += ["mdio_device %d %d %d %d %d %d" % \
(mdio.master, mdio.bus, mdio.id, mdio.portAddr, mdio.deviceAddr, \
mdio.clause)]
for addr, info in scd.uartPorts.items():
data += ["uart %#x %d" % (addr, info['id'])]
for addr, platform, slots, count in scd.fanGroups:
data += ["fan_group %#x %u %u %u" % (addr, platform, slots, count)]
for addr, name in scd.leds:
data += ["led %#x %s" % (addr, name)]
for addr, xcvrId in scd.osfps:
data += ["osfp %#x %u" % (addr, xcvrId)]
for addr, xcvrId in scd.qsfps:
data += ["qsfp %#x %u" % (addr, xcvrId)]
for addr, xcvrId in scd.sfps:
data += ["sfp %#x %u" % (addr, xcvrId)]
for reset in scd.resets:
data += ["reset %#x %s %u" % (reset.addr, reset.name, reset.bit)]
for gpio in scd.gpios:
data += ["gpio %#x %s %u %d %d" % (gpio.addr, gpio.name, gpio.bit,
int(gpio.ro), int(gpio.activeLow))]
self.waitReady()
logging.debug('creating scd objects')
self.writeComponents(data, "new_object")
if scd.msiRearmOffset:
path = self.addr.getSysfsPath()
utils.writeConfig(path, {'msi_rearm_offset': '%d' % scd.msiRearmOffset})
for intrReg in scd.interrupts:
intrReg.setup()
self.refresh() # sync with kernel runtime state
tweaks = []
for tweak in scd.tweaks.values():
tweaks += ["%#x %#x %#x %#x %#x %#x" % (
tweak.addr.bus, tweak.addr.address, tweak.t, tweak.datr, tweak.datw,
tweak.ed)]
if tweaks:
logging.debug('applying scd tweaks')
self.writeComponents(tweaks, "smbus_tweaks")
def finish(self):
logging.debug('applying scd configuration')
path = self.addr.getSysfsPath()
if Config().lock_scd_conf:
utils.writeConfig(path, {'init_trigger': '1'})
super(ScdKernelDriver, self).finish()
def resetSim(self, value):
resets = [reset.getName() for reset in self.scd.getResets(autoOnly=True)]
logging.debug('resetting devices %s', resets)
@utils.simulateWith(resetSim)
def reset(self, value):
for reset in self.scd.getResets(autoOnly=True):
if value:
reset.resetIn()
else:
reset.resetOut()
def resetIn(self):
self.reset(True)
def resetOut(self):
self.reset(False)
# FIXME: remove below methods once this driver has been reparented
def getFan(self, desc):
return FanSysfsImpl(self, desc)
def getFanLed(self, desc):
return LedSysfsImpl(self, desc)
def getReset(self, desc, **kwargs):
return ResetSysfsImpl(self, desc, **kwargs)
def getGpio(self, desc, **kwargs):
return GpioSysfsImpl(self, desc, hwActiveLow=True, **kwargs)
def getLed(self, desc, **kwargs):
return LedSysfsImpl(self, desc, **kwargs)
class ScdI2cDevDriver(I2cDevDriver):
def __init__(self, **kwargs):
super(ScdI2cDevDriver, self).__init__(**kwargs)
self.msgBus_ = None
@property
def msgBus(self):
if not self.msgBus_:
self.msgBus_ = I2cMsg(self.addr)
self.msgBus_.open()
return self.msgBus_
def writeMsg(self, msg):
self.msgBus.write_bytes(self.msgBus.addr.address, msg)