From b2286a24dc969e031f3226d57b95ec809d35c99e Mon Sep 17 00:00:00 2001 From: Stephen Sun <5379172+stephenxs@users.noreply.github.com> Date: Tue, 4 May 2021 08:10:18 +0800 Subject: [PATCH] [Mellanox] Adopt single way to get fan direction for all ASIC types (#7386) #### Why I did it Adopt a single way to get fan direction for all ASIC types. It depends on hw-mgmt V.7.0010.2000.2303. Depends on https://github.com/Azure/sonic-buildimage/pull/7419 #### How I did it Originally, the get_direction was implemented by fetching and parsing `/var/run/hw-management/system/fan_dir` on the Spectrum-2 and the Spectrum-3 systems. It isn't supported on the Spectrum system. Now, it is implemented by fetching `/var/run/hw-management/thermal/fanX_dir` for all the platforms. Signed-off-by: Stephen Sun --- .../sonic_platform/device_data.py | 10 +++++----- .../mlnx-platform-api/sonic_platform/fan.py | 6 ++++-- .../sonic_platform/fan_drawer.py | 17 +++++++++-------- 3 files changed, 18 insertions(+), 15 deletions(-) diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/device_data.py b/platform/mellanox/mlnx-platform-api/sonic_platform/device_data.py index a52886a9de..e1124d0d55 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/device_data.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/device_data.py @@ -10,7 +10,7 @@ DEVICE_DATA = { 'drawer_num': 4, 'drawer_type': 'real', 'fan_num_per_drawer': 2, - 'support_fan_direction': False, + 'support_fan_direction': True, 'hot_swappable': True }, 'psus': { @@ -31,7 +31,7 @@ DEVICE_DATA = { 'drawer_num': 4, 'drawer_type': 'real', 'fan_num_per_drawer': 1, - 'support_fan_direction': False, + 'support_fan_direction': True, 'hot_swappable': True }, 'psus': { @@ -52,7 +52,7 @@ DEVICE_DATA = { 'drawer_num': 1, 'drawer_type': 'virtual', 'fan_num_per_drawer': 4, - 'support_fan_direction': False, + 'support_fan_direction': True, 'hot_swappable': False }, 'psus': { @@ -73,7 +73,7 @@ DEVICE_DATA = { 'drawer_num': 4, 'drawer_type': 'real', 'fan_num_per_drawer': 2, - 'support_fan_direction': False, + 'support_fan_direction': True, 'hot_swappable': True }, 'psus': { @@ -94,7 +94,7 @@ DEVICE_DATA = { 'drawer_num': 1, 'drawer_type': 'virtual', 'fan_num_per_drawer': 4, - 'support_fan_direction': False, + 'support_fan_direction': True, 'hot_swappable': False }, 'psus': { diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/fan.py b/platform/mellanox/mlnx-platform-api/sonic_platform/fan.py index 98e5c40fc3..3ae5c4847c 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/fan.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/fan.py @@ -22,8 +22,10 @@ PWM_MAX = 255 FAN_PATH = "/var/run/hw-management/thermal/" CONFIG_PATH = "/var/run/hw-management/config" -# fan_dir isn't supported on Spectrum 1. It is supported on Spectrum 2 and later switches -FAN_DIR = "/var/run/hw-management/system/fan_dir" + +FAN_DIR = "/var/run/hw-management/thermal/fan{}_dir" +FAN_DIR_VALUE_EXHAUST = 0 +FAN_DIR_VALUE_INTAKE = 1 COOLING_STATE_PATH = "/var/run/hw-management/thermal/cooling_cur_state" class Fan(FanBase): diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/fan_drawer.py b/platform/mellanox/mlnx-platform-api/sonic_platform/fan_drawer.py index 66ee394917..fcf9b80263 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/fan_drawer.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/fan_drawer.py @@ -14,6 +14,7 @@ try: from sonic_platform_base.fan_drawer_base import FanDrawerBase from sonic_platform_base.fan_base import FanBase from .led import FanLed, SharedLed + from .utils import read_int_from_file except ImportError as e: raise ImportError (str(e) + "- required module not found") @@ -51,14 +52,14 @@ class MellanoxFanDrawer(FanDrawerBase): return FanBase.FAN_DIRECTION_NOT_APPLICABLE try: - from .fan import FAN_DIR - with open(FAN_DIR, 'r') as fan_dir: - fan_dir_bits = int(fan_dir.read()) - fan_mask = 1 << self._index - 1 - if fan_dir_bits & fan_mask: - return FanBase.FAN_DIRECTION_INTAKE - else: - return FanBase.FAN_DIRECTION_EXHAUST + from .fan import FAN_DIR, FAN_DIR_VALUE_INTAKE, FAN_DIR_VALUE_EXHAUST + fan_dir = read_int_from_file(FAN_DIR.format(self._index), raise_exception=True) + if fan_dir == FAN_DIR_VALUE_INTAKE: + return FanBase.FAN_DIRECTION_INTAKE + elif fan_dir == FAN_DIR_VALUE_EXHAUST: + return FanBase.FAN_DIRECTION_EXHAUST + else: + raise RuntimeError("Got wrong value {} for fan direction {}".format(fan_dir, self._index)) except (ValueError, IOError) as e: raise RuntimeError("Failed to read fan direction status to {}".format(repr(e)))