[Mellanox] Optimize SFP Platform API implementation (#5476)

Each SFP object inside Chassis will open an SDK client, this is not necessary and SDK client can be shared between SFP objects.
This commit is contained in:
Kebo Liu 2020-10-20 02:30:38 +08:00 committed by GitHub
parent edf4971b16
commit 73f38f6ce9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 62 deletions

View File

@ -75,6 +75,10 @@ class Chassis(ChassisBase):
if self.sfp_event_initialized: if self.sfp_event_initialized:
self.sfp_event.deinitialize() self.sfp_event.deinitialize()
if self.sfp_module_initialized:
from sonic_platform.sfp import deinitialize_sdk_handle
deinitialize_sdk_handle(self.sdk_handle)
def initialize_psu(self): def initialize_psu(self):
from sonic_platform.psu import Psu from sonic_platform.psu import Psu
@ -108,8 +112,14 @@ class Chassis(ChassisBase):
def initialize_sfp(self): def initialize_sfp(self):
from sonic_platform.sfp import SFP from sonic_platform.sfp import SFP
from sonic_platform.sfp import initialize_sdk_handle
self.sfp_module = SFP self.sfp_module = SFP
self.sdk_handle = initialize_sdk_handle()
if self.sdk_handle is None:
self.sfp_module_initialized = False
return
# Initialize SFP list # Initialize SFP list
port_position_tuple = self._get_port_position_tuple_by_platform_name() port_position_tuple = self._get_port_position_tuple_by_platform_name()
@ -120,9 +130,9 @@ class Chassis(ChassisBase):
for index in range(self.PORT_START, self.PORT_END + 1): for index in range(self.PORT_START, self.PORT_END + 1):
if index in range(self.QSFP_PORT_START, self.PORTS_IN_BLOCK + 1): if index in range(self.QSFP_PORT_START, self.PORTS_IN_BLOCK + 1):
sfp_module = SFP(index, 'QSFP') sfp_module = SFP(index, 'QSFP', self.sdk_handle)
else: else:
sfp_module = SFP(index, 'SFP') sfp_module = SFP(index, 'SFP', self.sdk_handle)
self._sfp_list.append(sfp_module) self._sfp_list.append(sfp_module)
self.sfp_module_initialized = True self.sfp_module_initialized = True

View File

@ -9,7 +9,6 @@
############################################################################# #############################################################################
try: try:
import os
import subprocess import subprocess
import time import time
from sonic_platform_base.sfp_base import SfpBase from sonic_platform_base.sfp_base import SfpBase
@ -283,59 +282,41 @@ CPU_MASK = PORT_TYPE_MASK & (PORT_TYPE_CPU << PORT_TYPE_OFFSET)
# Global logger class instance # Global logger class instance
logger = Logger() logger = Logger()
# SDK initializing stuff, called from chassis
def initialize_sdk_handle():
rc, sdk_handle = sx_api_open(None)
if (rc != SX_STATUS_SUCCESS):
logger.log_warning("Failed to open api handle, please check whether SDK is running.")
sdk_handle = None
return sdk_handle
def deinitialize_sdk_handle(sdk_handle):
if sdk_handle is not None:
rc = sx_api_close(sdk_handle)
if (rc != SX_STATUS_SUCCESS):
logger.log_warning("Failed to close api handle.")
return rc == SXD_STATUS_SUCCESS
else:
logger.log_warning("Sdk handle is none")
return False
class SFP(SfpBase): class SFP(SfpBase):
"""Platform-specific SFP class""" """Platform-specific SFP class"""
def __init__(self, sfp_index, sfp_type): def __init__(self, sfp_index, sfp_type, sdk_handle):
self.index = sfp_index + 1 self.index = sfp_index + 1
self.sfp_eeprom_path = "qsfp{}".format(self.index) self.sfp_eeprom_path = "qsfp{}".format(self.index)
self.sfp_status_path = "qsfp{}_status".format(self.index) self.sfp_status_path = "qsfp{}_status".format(self.index)
self._detect_sfp_type(sfp_type) self._detect_sfp_type(sfp_type)
self.dom_tx_disable_supported = False self.dom_tx_disable_supported = False
self._dom_capability_detect() self._dom_capability_detect()
self.sdk_handle = None self.sdk_handle = sdk_handle
self.sdk_index = sfp_index self.sdk_index = sfp_index
#SDK initializing stuff
def _initialize_sdk_handle(self):
"""
reference: device\mellanox\<sku>\pulgins\sfpreset.py
"""
rc, self.sdk_handle = sx_api_open(None)
if (rc != SX_STATUS_SUCCESS):
logger.log_warning("Failed to open api handle, please check whether SDK is running.")
self.sdk_handle = None
self.mypid = os.getpid()
def _open_sdk(self):
if self.sdk_handle is None:
self._initialize_sdk_handle()
rc = sxd_access_reg_init(self.mypid, None, 0)
if rc != 0:
logger.log_warning("Failed to initializing register access, please check that SDK is running.")
return False
return True
def _close_sdk(self):
rc = sxd_access_reg_deinit()
if rc != 0:
logger.log_warning("Failed to deinitializing register access.")
#no further actions here
def _init_sx_meta_data(self):
meta = sxd_reg_meta_t()
meta.dev_id = DEVICE_ID
meta.swid = SWITCH_ID
return meta
def get_presence(self): def get_presence(self):
""" """
Retrieves the presence of the device Retrieves the presence of the device
@ -1481,14 +1462,8 @@ class SFP(SfpBase):
Returns: Returns:
A Boolean, True if lpmode is enabled, False if disabled A Boolean, True if lpmode is enabled, False if disabled
""" """
handle = self._open_sdk()
if handle is None:
logger.log_error("SDK handler is missing for sfp %d object" % self.index)
return False
admin_pwr_mode, oper_pwr_mode = self.mgmt_phy_mod_pwr_attr_get(SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E) admin_pwr_mode, oper_pwr_mode = self.mgmt_phy_mod_pwr_attr_get(SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E)
self._close_sdk()
return oper_pwr_mode == SX_MGMT_PHY_MOD_PWR_MODE_LOW_E return oper_pwr_mode == SX_MGMT_PHY_MOD_PWR_MODE_LOW_E
@ -1863,16 +1838,10 @@ class SFP(SfpBase):
refer plugins/sfpreset.py refer plugins/sfpreset.py
""" """
handle = self._open_sdk()
if handle is None:
logger.log_error("SDK handler is missing for sfp %d object" % self.index)
return False
rc = sx_mgmt_phy_mod_reset(self.sdk_handle, self.sdk_index) rc = sx_mgmt_phy_mod_reset(self.sdk_handle, self.sdk_index)
if rc != SX_STATUS_SUCCESS: if rc != SX_STATUS_SUCCESS:
logger.log_warning("sx_mgmt_phy_mod_reset failed, rc = %d" % rc) logger.log_warning("sx_mgmt_phy_mod_reset failed, rc = %d" % rc)
self._close_sdk()
return rc == SX_STATUS_SUCCESS return rc == SX_STATUS_SUCCESS
@ -1927,6 +1896,11 @@ class SFP(SfpBase):
assert rc == SXD_STATUS_SUCCESS, "sx_api_port_state_get failed, rc = %d" % rc assert rc == SXD_STATUS_SUCCESS, "sx_api_port_state_get failed, rc = %d" % rc
admin_state = sx_port_admin_state_t_p_value(admin_state_p) admin_state = sx_port_admin_state_t_p_value(admin_state_p)
delete_sx_port_oper_state_t_p(oper_state_p)
delete_sx_port_admin_state_t_p(admin_state_p)
delete_sx_port_module_state_t_p(module_state_p)
if admin_state == SX_PORT_ADMIN_STATUS_UP: if admin_state == SX_PORT_ADMIN_STATUS_UP:
return True return True
else: else:
@ -1960,6 +1934,8 @@ class SFP(SfpBase):
and self.is_port_admin_status_up(port_attributes.log_port): and self.is_port_admin_status_up(port_attributes.log_port):
log_port_list.append(port_attributes.log_port) log_port_list.append(port_attributes.log_port)
delete_sx_port_attributes_t_arr(port_attributes_list)
delete_uint32_t_p(port_cnt_p)
return log_port_list return log_port_list
@ -2017,11 +1993,6 @@ class SFP(SfpBase):
Returns: Returns:
A boolean, True if lpmode is set successfully, False if not A boolean, True if lpmode is set successfully, False if not
""" """
handle = self._open_sdk()
if handle is None:
logger.log_error("SDK handler is missing for sfp %d object" % self.index)
return False
log_port_list = self.get_logical_ports() log_port_list = self.get_logical_ports()
if lpmode: if lpmode:
self._set_lpmode_raw(log_port_list, SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E, SX_MGMT_PHY_MOD_PWR_MODE_LOW_E) self._set_lpmode_raw(log_port_list, SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E, SX_MGMT_PHY_MOD_PWR_MODE_LOW_E)
@ -2029,7 +2000,7 @@ class SFP(SfpBase):
else: else:
self._set_lpmode_raw(log_port_list, SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E, SX_MGMT_PHY_MOD_PWR_MODE_AUTO_E) self._set_lpmode_raw(log_port_list, SX_MGMT_PHY_MOD_PWR_ATTR_PWR_MODE_E, SX_MGMT_PHY_MOD_PWR_MODE_AUTO_E)
logger.log_info( "Disabled low power mode for module [%d]" % (self.sdk_index)) logger.log_info( "Disabled low power mode for module [%d]" % (self.sdk_index))
self._close_sdk()
return True return True