Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Restore optoe page to default 0 for active cables #548

Merged
merged 5 commits into from
Mar 3, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions sonic_platform_base/sonic_xcvr/sfp_optoe_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@

from ..sfp_base import SfpBase

SFP_OPTOE_PAGE_SELECT_OFFSET = 127
SFP_OPTOE_UPPER_PAGE0_OFFSET = 128
SFP_OPTOE_PAGE_SIZE = 128

class SfpOptoeBase(SfpBase):
def __init__(self):
SfpBase.__init__(self)
Expand Down Expand Up @@ -261,6 +265,12 @@ def set_optoe_write_max(self, write_max):
except (OSError, IOError):
pass

def get_optoe_current_page(self):
return self.read_eeprom(SFP_OPTOE_PAGE_SELECT_OFFSET, 1)[0]

def set_page0(self):
self.write_eeprom(SFP_OPTOE_PAGE_SELECT_OFFSET, 1, bytearray([0x00]))

def set_optoe_write_timeout(self, write_timeout):
sys_path = self.get_eeprom_path()
sys_path = sys_path.replace("eeprom", "write_timeout")
Expand All @@ -273,6 +283,12 @@ def set_optoe_write_timeout(self, write_timeout):
def read_eeprom(self, offset, num_bytes):
try:
with open(self.get_eeprom_path(), mode='rb', buffering=0) as f:
if offset >= SFP_OPTOE_UPPER_PAGE0_OFFSET and \
offset < (SFP_OPTOE_UPPER_PAGE0_OFFSET+SFP_OPTOE_PAGE_SIZE) and \
self.get_optoe_current_page() != 0:
# Restoring the page to 0 helps in cases where the optoe driver failed to restore
# the page when say the module was busy with CDB command processing
self.set_page0()
f.seek(offset)
return bytearray(f.read(num_bytes))
except (OSError, IOError):
Expand Down
12 changes: 11 additions & 1 deletion tests/sonic_xcvr/test_sfp_optoe_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from mock import MagicMock
from mock import patch
import pytest
from sonic_platform_base.sonic_xcvr.sfp_optoe_base import SfpOptoeBase
from sonic_platform_base.sonic_xcvr.sfp_optoe_base import SfpOptoeBase, SFP_OPTOE_UPPER_PAGE0_OFFSET, SFP_OPTOE_PAGE_SELECT_OFFSET
from sonic_platform_base.sonic_xcvr.api.public.c_cmis import CCmisApi
from sonic_platform_base.sonic_xcvr.api.public.cmis import CmisApi
from sonic_platform_base.sonic_xcvr.mem_maps.public.c_cmis import CCmisMemMap
Expand Down Expand Up @@ -132,3 +132,13 @@ def test_set_power(self):
exception_raised = True
assert exception_raised

def test_default_page(self):
with patch("builtins.open", mock_open(read_data=b'\x01')) as mocked_file:
self.sfp_optoe_api.write_eeprom = MagicMock(return_value=True)
self.sfp_optoe_api.get_optoe_current_page = MagicMock(return_value=0x10)
self.sfp_optoe_api.get_eeprom_path = MagicMock(return_value='/sys/class/eeprom')
data = self.sfp_optoe_api.read_eeprom(SFP_OPTOE_UPPER_PAGE0_OFFSET, 1)
mocked_file.assert_called_once_with("/sys/class/eeprom", mode='rb', buffering=0)
assert data == b'\x01'
self.sfp_optoe_api.write_eeprom.assert_called_once_with(SFP_OPTOE_PAGE_SELECT_OFFSET, 1, b'\x00')
self.sfp_optoe_api.get_optoe_current_page.assert_called_once()
Loading