Skip to content

Commit d9c9c70

Browse files
authored
[BFN] Move qsfp eeprom reading to new cached api (#9909)
* Move qsfp eeprom reading to new cached api * provide reading multiple pages in recursive manner * workaround with flat memory on cmis * remove workaround with memory model * Remove unused imports
1 parent 1f9c89a commit d9c9c70

File tree

3 files changed

+48
-43
lines changed

3 files changed

+48
-43
lines changed

platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/platform_thrift_client.py

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
#!/usr/bin/env python
22

33
try:
4-
import os
5-
import sys
64
import time
7-
import importlib
85

9-
sys.path.append(os.path.dirname(__file__))
6+
from sonic_platform.pltfm_mgr_rpc.pltfm_mgr_rpc import Client
7+
from sonic_platform.pltfm_mgr_rpc.pltfm_mgr_rpc import InvalidPltfmMgrOperation
108

119
from thrift.transport import TSocket
1210
from thrift.transport import TTransport
@@ -25,9 +23,8 @@ def open(self):
2523
self.transport = TTransport.TBufferedTransport(self.transport)
2624
bprotocol = TBinaryProtocol.TBinaryProtocol(self.transport)
2725

28-
self.pltfm_mgr_module = importlib.import_module(".".join(["pltfm_mgr_rpc", "pltfm_mgr_rpc"]))
2926
pltfm_mgr_protocol = TMultiplexedProtocol.TMultiplexedProtocol(bprotocol, "pltfm_mgr_rpc")
30-
self.pltfm_mgr = self.pltfm_mgr_module.Client(pltfm_mgr_protocol)
27+
self.pltfm_mgr = Client(pltfm_mgr_protocol)
3128

3229
self.transport.open()
3330
return self
@@ -59,7 +56,7 @@ def pltfm_mgr_try(func, default=None, thrift_attempts=35):
5956
def pm_cb_run(client):
6057
try:
6158
return (None, func(client.pltfm_mgr))
62-
except client.pltfm_mgr_module.InvalidPltfmMgrOperation as ouch:
59+
except InvalidPltfmMgrOperation as ouch:
6360
return (ouch.code, default)
6461

6562
return thrift_try(pm_cb_run)

platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/pltfm_mgr_rpc/ttypes.py

Lines changed: 0 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

platform/barefoot/sonic-platform-modules-bfn-montara/sonic_platform/sfp.py

Lines changed: 44 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,29 +11,38 @@
1111
SFP_TYPE = "SFP"
1212
QSFP_TYPE = "QSFP"
1313
QSFP_DD_TYPE = "QSFP_DD"
14+
EEPROM_PAGE_SIZE = 128
1415

16+
try:
17+
from thrift.Thrift import TApplicationException
18+
19+
def cached_num_bytes_get(client):
20+
return client.pltfm_mgr.pltfm_mgr_qsfp_cached_num_bytes_get(1, 0, 0, 0)
21+
thrift_try(cached_num_bytes_get, 1)
22+
EEPROM_CACHED_API_SUPPORT = True
23+
except TApplicationException as e:
24+
EEPROM_CACHED_API_SUPPORT = False
1525

1626
class Sfp(SfpOptoeBase):
1727
"""
1828
BFN Platform-specific SFP class
1929
"""
2030

21-
SFP_EEPROM_PATH = "/var/run/platform/sfp/"
22-
2331
def __init__(self, port_num):
2432
SfpOptoeBase.__init__(self)
2533
self.index = port_num
2634
self.port_num = port_num
2735
self.sfp_type = QSFP_TYPE
36+
self.SFP_EEPROM_PATH = "/var/run/platform/sfp/"
2837

29-
if not os.path.exists(self.SFP_EEPROM_PATH):
30-
try:
31-
os.makedirs(self.SFP_EEPROM_PATH)
32-
except OSError as e:
33-
if e.errno != errno.EEXIST:
34-
raise
35-
36-
self.eeprom_path = self.SFP_EEPROM_PATH + "sfp{}-eeprom-cache".format(self.index)
38+
if not EEPROM_CACHED_API_SUPPORT:
39+
if not os.path.exists(self.SFP_EEPROM_PATH):
40+
try:
41+
os.makedirs(self.SFP_EEPROM_PATH)
42+
except OSError as e:
43+
if e.errno != errno.EEXIST:
44+
raise
45+
self.eeprom_path = self.SFP_EEPROM_PATH + "sfp{}-eeprom-cache".format(self.index)
3746

3847
def get_presence(self):
3948
"""
@@ -47,7 +56,7 @@ def qsfp_presence_get(client):
4756
try:
4857
presence = thrift_try(qsfp_presence_get)
4958
except Exception as e:
50-
print( e.__doc__)
59+
print(e.__doc__)
5160
print(e.message)
5261

5362
return presence
@@ -75,14 +84,31 @@ def get_eeprom_path(self):
7584
def qsfp_info_get(client):
7685
return client.pltfm_mgr.pltfm_mgr_qsfp_info_get(self.index)
7786

78-
if self.get_presence():
79-
eeprom_hex = thrift_try(qsfp_info_get)
80-
eeprom_raw = bytearray.fromhex(eeprom_hex)
81-
with open(self.eeprom_path, 'wb') as fp:
82-
fp.write(eeprom_raw)
83-
return self.eeprom_path
87+
eeprom_hex = thrift_try(qsfp_info_get)
88+
eeprom_raw = bytearray.fromhex(eeprom_hex)
89+
with open(self.eeprom_path, 'wb') as fp:
90+
fp.write(eeprom_raw)
91+
return self.eeprom_path
92+
93+
def read_eeprom(self, offset, num_bytes):
94+
if not self.get_presence():
95+
return None
96+
97+
if not EEPROM_CACHED_API_SUPPORT:
98+
return super().read_eeprom(offset, num_bytes)
99+
100+
def cached_num_bytes_get(page, offset, num_bytes):
101+
def qsfp_cached_num_bytes_get(client):
102+
return client.pltfm_mgr.pltfm_mgr_qsfp_cached_num_bytes_get(self.index, page, offset, num_bytes)
103+
return bytearray.fromhex(thrift_try(qsfp_cached_num_bytes_get))
104+
105+
page_offset = offset % EEPROM_PAGE_SIZE
106+
if page_offset + num_bytes > EEPROM_PAGE_SIZE:
107+
curr_page_num_bytes_left = EEPROM_PAGE_SIZE - page_offset
108+
curr_page_bytes = cached_num_bytes_get(offset // EEPROM_PAGE_SIZE, page_offset, curr_page_num_bytes_left)
109+
return curr_page_bytes + self.read_eeprom(offset + curr_page_num_bytes_left, num_bytes - curr_page_num_bytes_left)
84110

85-
return None
111+
return cached_num_bytes_get(offset // EEPROM_PAGE_SIZE, page_offset, num_bytes)
86112

87113
def write_eeprom(self, offset, num_bytes, write_buffer):
88114
# Not supported at the moment

0 commit comments

Comments
 (0)