From 06ca906a64f09be6be02dc40c743c0219c717a35 Mon Sep 17 00:00:00 2001 From: Stephen Sun Date: Thu, 23 Jun 2022 06:09:14 +0000 Subject: [PATCH 1/4] Support get_port_or_cage_type Signed-off-by: Stephen Sun --- .../sonic_platform/chassis.py | 24 +++++++++++++++++++ 1 file changed, 24 insertions(+) diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py index ea4d7df30f9a..9951bdb8ee89 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py @@ -313,6 +313,30 @@ def get_sfp(self, index): self.initialize_single_sfp(index) return super(Chassis, self).get_sfp(index) + def get_port_or_cage_type(self, index): + """ + Retrieves sfp port or cage type corresponding to physical port + + Args: + index: An integer (>=0), the index of the sfp to retrieve. + The index should correspond to the physical port in a chassis. + For example:- + 1 for Ethernet0, 2 for Ethernet4 and so on for one platform. + 0 for Ethernet0, 1 for Ethernet4 and so on for another platform. + + Returns: + The masks of all types of port or cage that can be supported on the port + Types are defined in sfp_base.py + Eg. + Both SFP and SFP+ are supported on the port, the return value should be 0x0a + which is 0x02 | 0x08 + """ + index = index - 1 + if self.RJ45_port_list and index in self.RJ45_port_list: + from sonic_platform_base.sfp_base import SfpBase + return SfpBase.SFP_PORT_TYPE_BIT_RJ45 + raise NotImplementedError + def get_change_event(self, timeout=0): """ Returns a nested dictionary containing all devices which have From 16f548e0a357b7c01f66df89e1da625fd0b431ca Mon Sep 17 00:00:00 2001 From: Stephen Sun Date: Mon, 27 Jun 2022 06:15:43 +0000 Subject: [PATCH 2/4] Avoid calling SDK stuff on host Signed-off-by: Stephen Sun --- platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py index 9951bdb8ee89..211e89bcd5d1 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py @@ -120,6 +120,8 @@ def __del__(self): self.sfp_event.deinitialize() if self._sfp_list: + if utils.is_host(): + return from .sfp import SFP, deinitialize_sdk_handle if SFP.shared_sdk_handle: deinitialize_sdk_handle(SFP.shared_sdk_handle) From aef9904ae27cee0d74d1132910ea7c7588d9a6e7 Mon Sep 17 00:00:00 2001 From: Stephen Sun Date: Fri, 1 Jul 2022 10:02:39 +0000 Subject: [PATCH 3/4] Update unit test Signed-off-by: Stephen Sun --- .../mlnx-platform-api/tests/test_chassis.py | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/platform/mellanox/mlnx-platform-api/tests/test_chassis.py b/platform/mellanox/mlnx-platform-api/tests/test_chassis.py index c6235b195a02..38b6bd6cfffd 100644 --- a/platform/mellanox/mlnx-platform-api/tests/test_chassis.py +++ b/platform/mellanox/mlnx-platform-api/tests/test_chassis.py @@ -29,6 +29,7 @@ sys.path.insert(0, modules_path) import sonic_platform.chassis +from sonic_platform_base.sfp_base import SfpBase from sonic_platform.chassis import Chassis from sonic_platform.device_data import DeviceDataManager @@ -282,3 +283,16 @@ def test_revision_permission(self): sonic_platform.chassis.DMI_FILE = old_dmi_file os.system("rm -f " + new_dmi_file) assert rev == "N/A" + + def test_get_port_or_cage_type(self): + chassis = Chassis() + chassis.RJ45_port_list = [0] + assert SfpBase.SFP_PORT_TYPE_BIT_RJ45 == chassis.get_port_or_cage_type(1) + + exceptionRaised = False + try: + chassis.get_port_or_cage_type(2) + except NotImplementedError: + exceptionRaised = True + + assert exceptionRaised From 17c5bf276a4b60121c726cff0f91cd36a43755a1 Mon Sep 17 00:00:00 2001 From: Stephen Sun Date: Mon, 4 Jul 2022 10:09:50 +0000 Subject: [PATCH 4/4] Fix issue: can not import the module SFP in destructor Signed-off-by: Stephen Sun --- .../sonic_platform/chassis.py | 36 ++++++++++--------- 1 file changed, 20 insertions(+), 16 deletions(-) diff --git a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py index 211e89bcd5d1..b83ee33cc316 100644 --- a/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py +++ b/platform/mellanox/mlnx-platform-api/sonic_platform/chassis.py @@ -30,7 +30,6 @@ from .utils import extract_RJ45_ports_index from . import utils from .device_data import DeviceDataManager - from .sfp import SFP, RJ45Port, deinitialize_sdk_handle except ImportError as e: raise ImportError (str(e) + "- required module not found") @@ -110,6 +109,8 @@ def __init__(self): self.sfp_event = None self.reboot_cause_initialized = False + self.sfp_module = None + # Build the RJ45 port list from platform.json and hwsku.json self.RJ45_port_list = extract_RJ45_ports_index() @@ -120,11 +121,8 @@ def __del__(self): self.sfp_event.deinitialize() if self._sfp_list: - if utils.is_host(): - return - from .sfp import SFP, deinitialize_sdk_handle - if SFP.shared_sdk_handle: - deinitialize_sdk_handle(SFP.shared_sdk_handle) + if self.sfp_module.SFP.shared_sdk_handle: + self.sfp_module.deinitialize_sdk_handle(sfp_module.SFP.shared_sdk_handle) ############################################## # PSU methods @@ -243,6 +241,12 @@ def get_fan_drawer(self, index): # SFP methods ############################################## + def _import_sfp_module(self): + if not self.sfp_module: + from . import sfp as sfp_module + self.sfp_module = sfp_module + return self.sfp_module + def initialize_single_sfp(self, index): sfp_count = self.get_num_sfps() if index < sfp_count: @@ -250,32 +254,32 @@ def initialize_single_sfp(self, index): self._sfp_list = [None] * sfp_count if not self._sfp_list[index]: - from .sfp import SFP + sfp_module = self._import_sfp_module() if self.RJ45_port_list and index in self.RJ45_port_list: - self._sfp_list[index] = RJ45Port(index) + self._sfp_list[index] = sfp_module.RJ45Port(index) else: - self._sfp_list[index] = SFP(index) + self._sfp_list[index] = sfp_module.SFP(index) self.sfp_initialized_count += 1 def initialize_sfp(self): if not self._sfp_list: - from .sfp import SFP + sfp_module = self._import_sfp_module() sfp_count = self.get_num_sfps() for index in range(sfp_count): if self.RJ45_port_list and index in self.RJ45_port_list: - sfp_module = RJ45Port(index) + sfp_object = sfp_module.RJ45Port(index) else: - sfp_module = SFP(index) - self._sfp_list.append(sfp_module) + sfp_object = sfp_module.SFP(index) + self._sfp_list.append(sfp_object) self.sfp_initialized_count = sfp_count elif self.sfp_initialized_count != len(self._sfp_list): - from .sfp import SFP + sfp_module = self._import_sfp_module() for index in range(len(self._sfp_list)): if self._sfp_list[index] is None: if self.RJ45_port_list and index in self.RJ45_port_list: - self._sfp_list[index] = RJ45Port(index) + self._sfp_list[index] = sfp_module.RJ45Port(index) else: - self._sfp_list[index] = SFP(index) + self._sfp_list[index] = sfp_module.SFP(index) self.sfp_initialized_count = len(self._sfp_list) def get_num_sfps(self):