Skip to content

Commit 7bee509

Browse files
[Mellanox] Support max/min speed for PSU fan (#5682)
As new hw-mgmt expose the sysfs for PSU fan max speed, we need support max/min speed for PSU fan in mellanox platform API.
1 parent e66d49a commit 7bee509

File tree

2 files changed

+81
-65
lines changed

2 files changed

+81
-65
lines changed

platform/mellanox/mlnx-platform-api/sonic_platform/fan.py

+24-65
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
try:
1515
from sonic_platform_base.fan_base import FanBase
1616
from .led import FanLed, ComponentFaultyIndicator
17+
from .utils import read_int_from_file, read_str_from_file, write_file
1718
except ImportError as e:
1819
raise ImportError (str(e) + "- required module not found")
1920

@@ -52,17 +53,18 @@ def __init__(self, fan_index, fan_drawer, psu_fan = False):
5253
else:
5354
self.led = FanLed(self.index)
5455

55-
self.fan_min_speed_path = "fan{}_min".format(self.index)
5656
if not self.is_psu_fan:
5757
self.fan_speed_get_path = "fan{}_speed_get".format(self.index)
5858
self.fan_speed_set_path = "fan{}_speed_set".format(self.index)
59-
self.fan_max_speed_path = "fan{}_max".format(self.index)
59+
self.fan_max_speed_path = os.path.join(FAN_PATH, "fan{}_max".format(self.index))
60+
self.fan_min_speed_path = os.path.join(FAN_PATH, "fan{}_min".format(self.index))
6061
self._name = "fan{}".format(self.index)
6162
else:
6263
self.fan_speed_get_path = "psu{}_fan1_speed_get".format(self.index)
6364
self.fan_presence_path = "psu{}_fan1_speed_get".format(self.index)
6465
self._name = 'psu_{}_fan_{}'.format(self.index, 1)
65-
self.fan_max_speed_path = None
66+
self.fan_max_speed_path = os.path.join(CONFIG_PATH, "psu_fan_max")
67+
self.fan_min_speed_path = os.path.join(CONFIG_PATH, "psu_fan_min")
6668
self.psu_i2c_bus_path = os.path.join(CONFIG_PATH, 'psu{0}_i2c_bus'.format(self.index))
6769
self.psu_i2c_addr_path = os.path.join(CONFIG_PATH, 'psu{0}_i2c_addr'.format(self.index))
6870
self.psu_i2c_command_path = os.path.join(CONFIG_PATH, 'fan_command')
@@ -109,11 +111,7 @@ def get_status(self):
109111
if self.is_psu_fan:
110112
status = 0
111113
else:
112-
try:
113-
with open(os.path.join(FAN_PATH, self.fan_status_path), 'r') as fault_status:
114-
status = int(fault_status.read().strip())
115-
except (ValueError, IOError):
116-
status = 1
114+
status = read_int_from_file(os.path.join(FAN_PATH, self.fan_status_path), 1)
117115

118116
return status == 0
119117

@@ -135,29 +133,6 @@ def get_presence(self):
135133
else:
136134
return self.fan_drawer.get_presence()
137135

138-
139-
def _get_min_speed_in_rpm(self):
140-
speed = 0
141-
try:
142-
with open(os.path.join(FAN_PATH, self.fan_min_speed_path), 'r') as min_fan_speed:
143-
speed = int(min_fan_speed.read())
144-
except (ValueError, IOError):
145-
speed = 0
146-
147-
return speed
148-
149-
150-
def _get_max_speed_in_rpm(self):
151-
speed = 0
152-
try:
153-
with open(os.path.join(FAN_PATH, self.fan_max_speed_path), 'r') as max_fan_speed:
154-
speed = int(max_fan_speed.read().strip())
155-
except (ValueError, IOError):
156-
speed = 0
157-
158-
return speed
159-
160-
161136
def get_speed(self):
162137
"""
163138
Retrieves the speed of fan
@@ -166,17 +141,12 @@ def get_speed(self):
166141
int: percentage of the max fan speed
167142
"""
168143
speed = 0
169-
try:
170-
with open(os.path.join(FAN_PATH, self.fan_speed_get_path), 'r') as fan_curr_speed:
171-
speed_in_rpm = int(fan_curr_speed.read().strip())
172-
except (ValueError, IOError):
173-
speed_in_rpm = 0
144+
speed_in_rpm = read_int_from_file(os.path.join(FAN_PATH, self.fan_speed_get_path))
174145

175-
if self.fan_max_speed_path is None:
176-
# in case of max speed unsupported, we just return speed in unit of RPM.
146+
max_speed_in_rpm = read_int_from_file(self.fan_max_speed_path)
147+
if max_speed_in_rpm == 0:
177148
return speed_in_rpm
178149

179-
max_speed_in_rpm = self._get_max_speed_in_rpm()
180150
speed = 100*speed_in_rpm/max_speed_in_rpm
181151
if speed > 100:
182152
speed = 100
@@ -192,18 +162,15 @@ def get_target_speed(self):
192162
int: percentage of the max fan speed
193163
"""
194164
if self.is_psu_fan:
195-
# Not like system fan, psu fan speed can not be modified, so target speed is N/A
196-
return self.get_speed()
165+
try:
166+
# Get PSU fan target speed according to current system cooling level
167+
cooling_level = self.get_cooling_level()
168+
return int(self.PSU_FAN_SPEED[cooling_level], 16)
169+
except Exception:
170+
return self.get_speed()
197171

198-
try:
199-
with open(os.path.join(FAN_PATH, self.fan_speed_set_path), 'r') as fan_pwm:
200-
pwm = int(fan_pwm.read().strip())
201-
except (ValueError, IOError):
202-
pwm = 0
203-
204-
speed = int(round(pwm*100.0/PWM_MAX))
205-
206-
return speed
172+
pwm = read_int_from_file(os.path.join(FAN_PATH, self.fan_speed_set_path))
173+
return int(round(pwm*100.0/PWM_MAX))
207174

208175

209176
def set_speed(self, speed):
@@ -224,12 +191,9 @@ def set_speed(self, speed):
224191
return False
225192
from .thermal import logger
226193
try:
227-
with open(self.psu_i2c_bus_path, 'r') as f:
228-
bus = f.read().strip()
229-
with open(self.psu_i2c_addr_path, 'r') as f:
230-
addr = f.read().strip()
231-
with open(self.psu_i2c_command_path, 'r') as f:
232-
command = f.read().strip()
194+
bus = read_str_from_file(self.psu_i2c_bus_path, raise_exception=True)
195+
addr = read_str_from_file(self.psu_i2c_addr_path, raise_exception=True)
196+
command = read_str_from_file(self.psu_i2c_command_path, raise_exception=True)
233197
speed = Fan.PSU_FAN_SPEED[int(speed / 10)]
234198
command = "i2cset -f -y {0} {1} {2} {3} wp".format(bus, addr, command, speed)
235199
subprocess.check_call(command, shell = True)
@@ -248,8 +212,7 @@ def set_speed(self, speed):
248212
speed = self.min_cooling_level * 10
249213
self.set_cooling_level(cooling_level, cooling_level)
250214
pwm = int(round(PWM_MAX*speed/100.0))
251-
with open(os.path.join(FAN_PATH, self.fan_speed_set_path), 'w') as fan_pwm:
252-
fan_pwm.write(str(pwm))
215+
write_file(os.path.join(FAN_PATH, self.fan_speed_set_path), pwm, raise_exception=True)
253216
except (ValueError, IOError):
254217
status = False
255218

@@ -311,21 +274,17 @@ def set_cooling_level(cls, level, cur_state):
311274
# Reset FAN cooling level vector. According to low level team,
312275
# if we need set cooling level to X, we need first write a (10+X)
313276
# to cooling_cur_state file to reset the cooling level vector.
314-
with open(COOLING_STATE_PATH, 'w') as cooling_state:
315-
cooling_state.write(str(level + 10))
277+
write_file(COOLING_STATE_PATH, level + 10, raise_exception=True)
316278

317279
# We need set cooling level after resetting the cooling level vector
318-
with open(COOLING_STATE_PATH, 'w') as cooling_state:
319-
cooling_state.write(str(cur_state))
280+
write_file(COOLING_STATE_PATH, cur_state, raise_exception=True)
320281
except (ValueError, IOError) as e:
321282
raise RuntimeError("Failed to set cooling level - {}".format(e))
322283

323284
@classmethod
324285
def get_cooling_level(cls):
325286
try:
326-
with open(COOLING_STATE_PATH, 'r') as cooling_state:
327-
cooling_level = int(cooling_state.read().strip())
328-
return cooling_level
287+
return read_int_from_file(COOLING_STATE_PATH, raise_exception=True)
329288
except (ValueError, IOError) as e:
330289
raise RuntimeError("Failed to get cooling level - {}".format(e))
331290

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
def read_str_from_file(file_path, default='', raise_exception=False):
2+
"""
3+
Read string content from file
4+
:param file_path: File path
5+
:param default: Default return value if any exception occur
6+
:param raise_exception: Raise exception to caller if True else just return default value
7+
:return: String content of the file
8+
"""
9+
try:
10+
with open(file_path, 'r') as f:
11+
value = f.read().strip()
12+
except (ValueError, IOError) as e:
13+
if not raise_exception:
14+
value = default
15+
else:
16+
raise e
17+
18+
return value
19+
20+
21+
def read_int_from_file(file_path, default=0, raise_exception=False):
22+
"""
23+
Read content from file and cast it to integer
24+
:param file_path: File path
25+
:param default: Default return value if any exception occur
26+
:param raise_exception: Raise exception to caller if True else just return default value
27+
:return: Integer value of the file content
28+
"""
29+
try:
30+
with open(file_path, 'r') as f:
31+
value = int(f.read().strip())
32+
except (ValueError, IOError) as e:
33+
if not raise_exception:
34+
value = default
35+
else:
36+
raise e
37+
38+
return value
39+
40+
41+
def write_file(file_path, content, raise_exception=False):
42+
"""
43+
Write the given value to a file
44+
:param file_path: File path
45+
:param content: Value to write to the file
46+
:param raise_exception: Raise exception to caller if True
47+
:return: True if write success else False
48+
"""
49+
try:
50+
with open(file_path, 'w') as f:
51+
f.write(str(content))
52+
except (ValueError, IOError) as e:
53+
if not raise_exception:
54+
return False
55+
else:
56+
raise e
57+
return True

0 commit comments

Comments
 (0)