[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.
This commit is contained in:
Junchao-Mellanox 2020-10-27 03:47:12 +08:00 committed by GitHub
parent e66d49a57c
commit 7bee5093f1
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 81 additions and 65 deletions

View File

@ -14,6 +14,7 @@ import subprocess
try: try:
from sonic_platform_base.fan_base import FanBase from sonic_platform_base.fan_base import FanBase
from .led import FanLed, ComponentFaultyIndicator from .led import FanLed, ComponentFaultyIndicator
from .utils import read_int_from_file, read_str_from_file, write_file
except ImportError as e: except ImportError as e:
raise ImportError (str(e) + "- required module not found") raise ImportError (str(e) + "- required module not found")
@ -52,17 +53,18 @@ class Fan(FanBase):
else: else:
self.led = FanLed(self.index) self.led = FanLed(self.index)
self.fan_min_speed_path = "fan{}_min".format(self.index)
if not self.is_psu_fan: if not self.is_psu_fan:
self.fan_speed_get_path = "fan{}_speed_get".format(self.index) self.fan_speed_get_path = "fan{}_speed_get".format(self.index)
self.fan_speed_set_path = "fan{}_speed_set".format(self.index) self.fan_speed_set_path = "fan{}_speed_set".format(self.index)
self.fan_max_speed_path = "fan{}_max".format(self.index) self.fan_max_speed_path = os.path.join(FAN_PATH, "fan{}_max".format(self.index))
self.fan_min_speed_path = os.path.join(FAN_PATH, "fan{}_min".format(self.index))
self._name = "fan{}".format(self.index) self._name = "fan{}".format(self.index)
else: else:
self.fan_speed_get_path = "psu{}_fan1_speed_get".format(self.index) self.fan_speed_get_path = "psu{}_fan1_speed_get".format(self.index)
self.fan_presence_path = "psu{}_fan1_speed_get".format(self.index) self.fan_presence_path = "psu{}_fan1_speed_get".format(self.index)
self._name = 'psu_{}_fan_{}'.format(self.index, 1) self._name = 'psu_{}_fan_{}'.format(self.index, 1)
self.fan_max_speed_path = None self.fan_max_speed_path = os.path.join(CONFIG_PATH, "psu_fan_max")
self.fan_min_speed_path = os.path.join(CONFIG_PATH, "psu_fan_min")
self.psu_i2c_bus_path = os.path.join(CONFIG_PATH, 'psu{0}_i2c_bus'.format(self.index)) self.psu_i2c_bus_path = os.path.join(CONFIG_PATH, 'psu{0}_i2c_bus'.format(self.index))
self.psu_i2c_addr_path = os.path.join(CONFIG_PATH, 'psu{0}_i2c_addr'.format(self.index)) self.psu_i2c_addr_path = os.path.join(CONFIG_PATH, 'psu{0}_i2c_addr'.format(self.index))
self.psu_i2c_command_path = os.path.join(CONFIG_PATH, 'fan_command') self.psu_i2c_command_path = os.path.join(CONFIG_PATH, 'fan_command')
@ -109,11 +111,7 @@ class Fan(FanBase):
if self.is_psu_fan: if self.is_psu_fan:
status = 0 status = 0
else: else:
try: status = read_int_from_file(os.path.join(FAN_PATH, self.fan_status_path), 1)
with open(os.path.join(FAN_PATH, self.fan_status_path), 'r') as fault_status:
status = int(fault_status.read().strip())
except (ValueError, IOError):
status = 1
return status == 0 return status == 0
@ -135,29 +133,6 @@ class Fan(FanBase):
else: else:
return self.fan_drawer.get_presence() return self.fan_drawer.get_presence()
def _get_min_speed_in_rpm(self):
speed = 0
try:
with open(os.path.join(FAN_PATH, self.fan_min_speed_path), 'r') as min_fan_speed:
speed = int(min_fan_speed.read())
except (ValueError, IOError):
speed = 0
return speed
def _get_max_speed_in_rpm(self):
speed = 0
try:
with open(os.path.join(FAN_PATH, self.fan_max_speed_path), 'r') as max_fan_speed:
speed = int(max_fan_speed.read().strip())
except (ValueError, IOError):
speed = 0
return speed
def get_speed(self): def get_speed(self):
""" """
Retrieves the speed of fan Retrieves the speed of fan
@ -166,17 +141,12 @@ class Fan(FanBase):
int: percentage of the max fan speed int: percentage of the max fan speed
""" """
speed = 0 speed = 0
try: speed_in_rpm = read_int_from_file(os.path.join(FAN_PATH, self.fan_speed_get_path))
with open(os.path.join(FAN_PATH, self.fan_speed_get_path), 'r') as fan_curr_speed:
speed_in_rpm = int(fan_curr_speed.read().strip())
except (ValueError, IOError):
speed_in_rpm = 0
if self.fan_max_speed_path is None: max_speed_in_rpm = read_int_from_file(self.fan_max_speed_path)
# in case of max speed unsupported, we just return speed in unit of RPM. if max_speed_in_rpm == 0:
return speed_in_rpm return speed_in_rpm
max_speed_in_rpm = self._get_max_speed_in_rpm()
speed = 100*speed_in_rpm/max_speed_in_rpm speed = 100*speed_in_rpm/max_speed_in_rpm
if speed > 100: if speed > 100:
speed = 100 speed = 100
@ -192,18 +162,15 @@ class Fan(FanBase):
int: percentage of the max fan speed int: percentage of the max fan speed
""" """
if self.is_psu_fan: if self.is_psu_fan:
# Not like system fan, psu fan speed can not be modified, so target speed is N/A try:
return self.get_speed() # Get PSU fan target speed according to current system cooling level
cooling_level = self.get_cooling_level()
return int(self.PSU_FAN_SPEED[cooling_level], 16)
except Exception:
return self.get_speed()
try: pwm = read_int_from_file(os.path.join(FAN_PATH, self.fan_speed_set_path))
with open(os.path.join(FAN_PATH, self.fan_speed_set_path), 'r') as fan_pwm: return int(round(pwm*100.0/PWM_MAX))
pwm = int(fan_pwm.read().strip())
except (ValueError, IOError):
pwm = 0
speed = int(round(pwm*100.0/PWM_MAX))
return speed
def set_speed(self, speed): def set_speed(self, speed):
@ -224,12 +191,9 @@ class Fan(FanBase):
return False return False
from .thermal import logger from .thermal import logger
try: try:
with open(self.psu_i2c_bus_path, 'r') as f: bus = read_str_from_file(self.psu_i2c_bus_path, raise_exception=True)
bus = f.read().strip() addr = read_str_from_file(self.psu_i2c_addr_path, raise_exception=True)
with open(self.psu_i2c_addr_path, 'r') as f: command = read_str_from_file(self.psu_i2c_command_path, raise_exception=True)
addr = f.read().strip()
with open(self.psu_i2c_command_path, 'r') as f:
command = f.read().strip()
speed = Fan.PSU_FAN_SPEED[int(speed / 10)] speed = Fan.PSU_FAN_SPEED[int(speed / 10)]
command = "i2cset -f -y {0} {1} {2} {3} wp".format(bus, addr, command, speed) command = "i2cset -f -y {0} {1} {2} {3} wp".format(bus, addr, command, speed)
subprocess.check_call(command, shell = True) subprocess.check_call(command, shell = True)
@ -248,8 +212,7 @@ class Fan(FanBase):
speed = self.min_cooling_level * 10 speed = self.min_cooling_level * 10
self.set_cooling_level(cooling_level, cooling_level) self.set_cooling_level(cooling_level, cooling_level)
pwm = int(round(PWM_MAX*speed/100.0)) pwm = int(round(PWM_MAX*speed/100.0))
with open(os.path.join(FAN_PATH, self.fan_speed_set_path), 'w') as fan_pwm: write_file(os.path.join(FAN_PATH, self.fan_speed_set_path), pwm, raise_exception=True)
fan_pwm.write(str(pwm))
except (ValueError, IOError): except (ValueError, IOError):
status = False status = False
@ -311,21 +274,17 @@ class Fan(FanBase):
# Reset FAN cooling level vector. According to low level team, # Reset FAN cooling level vector. According to low level team,
# if we need set cooling level to X, we need first write a (10+X) # if we need set cooling level to X, we need first write a (10+X)
# to cooling_cur_state file to reset the cooling level vector. # to cooling_cur_state file to reset the cooling level vector.
with open(COOLING_STATE_PATH, 'w') as cooling_state: write_file(COOLING_STATE_PATH, level + 10, raise_exception=True)
cooling_state.write(str(level + 10))
# We need set cooling level after resetting the cooling level vector # We need set cooling level after resetting the cooling level vector
with open(COOLING_STATE_PATH, 'w') as cooling_state: write_file(COOLING_STATE_PATH, cur_state, raise_exception=True)
cooling_state.write(str(cur_state))
except (ValueError, IOError) as e: except (ValueError, IOError) as e:
raise RuntimeError("Failed to set cooling level - {}".format(e)) raise RuntimeError("Failed to set cooling level - {}".format(e))
@classmethod @classmethod
def get_cooling_level(cls): def get_cooling_level(cls):
try: try:
with open(COOLING_STATE_PATH, 'r') as cooling_state: return read_int_from_file(COOLING_STATE_PATH, raise_exception=True)
cooling_level = int(cooling_state.read().strip())
return cooling_level
except (ValueError, IOError) as e: except (ValueError, IOError) as e:
raise RuntimeError("Failed to get cooling level - {}".format(e)) raise RuntimeError("Failed to get cooling level - {}".format(e))

View File

@ -0,0 +1,57 @@
def read_str_from_file(file_path, default='', raise_exception=False):
"""
Read string content from file
:param file_path: File path
:param default: Default return value if any exception occur
:param raise_exception: Raise exception to caller if True else just return default value
:return: String content of the file
"""
try:
with open(file_path, 'r') as f:
value = f.read().strip()
except (ValueError, IOError) as e:
if not raise_exception:
value = default
else:
raise e
return value
def read_int_from_file(file_path, default=0, raise_exception=False):
"""
Read content from file and cast it to integer
:param file_path: File path
:param default: Default return value if any exception occur
:param raise_exception: Raise exception to caller if True else just return default value
:return: Integer value of the file content
"""
try:
with open(file_path, 'r') as f:
value = int(f.read().strip())
except (ValueError, IOError) as e:
if not raise_exception:
value = default
else:
raise e
return value
def write_file(file_path, content, raise_exception=False):
"""
Write the given value to a file
:param file_path: File path
:param content: Value to write to the file
:param raise_exception: Raise exception to caller if True
:return: True if write success else False
"""
try:
with open(file_path, 'w') as f:
f.write(str(content))
except (ValueError, IOError) as e:
if not raise_exception:
return False
else:
raise e
return True