2018-04-02 21:06:00 -05:00
|
|
|
try:
|
|
|
|
import time
|
2020-11-25 12:28:36 -06:00
|
|
|
from sonic_sfp.sfputilbase import SfpUtilBase
|
|
|
|
except ImportError as e:
|
|
|
|
raise ImportError(str(e) + "- required module not found")
|
2018-04-02 21:06:00 -05:00
|
|
|
|
2019-08-07 11:33:56 -05:00
|
|
|
SFP_STATUS_INSERTED = '1'
|
|
|
|
SFP_STATUS_REMOVED = '0'
|
2018-04-02 21:06:00 -05:00
|
|
|
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2018-04-02 21:06:00 -05:00
|
|
|
class SfpUtil(SfpUtilBase):
|
|
|
|
"""Platform specific SfpUtill class"""
|
|
|
|
|
|
|
|
_port_start = 0
|
|
|
|
_port_end = 53
|
|
|
|
_qsfp_port_start = 48
|
|
|
|
_ports_in_block = 54
|
|
|
|
|
|
|
|
_port_to_eeprom_mapping = {}
|
|
|
|
_port_to_i2c_mapping = {
|
2020-11-25 12:28:36 -06:00
|
|
|
0: 37,
|
|
|
|
1: 38,
|
|
|
|
2: 39,
|
|
|
|
3: 40,
|
|
|
|
4: 41,
|
|
|
|
5: 42,
|
|
|
|
6: 43,
|
|
|
|
7: 44,
|
|
|
|
8: 45,
|
|
|
|
9: 46,
|
|
|
|
10: 47,
|
|
|
|
11: 48,
|
|
|
|
12: 49,
|
|
|
|
13: 50,
|
|
|
|
14: 51,
|
|
|
|
15: 52,
|
|
|
|
16: 53,
|
|
|
|
17: 54,
|
|
|
|
18: 55,
|
|
|
|
19: 56,
|
|
|
|
20: 57,
|
|
|
|
21: 58,
|
|
|
|
22: 59,
|
|
|
|
23: 60,
|
|
|
|
24: 61,
|
|
|
|
25: 62,
|
|
|
|
26: 63,
|
|
|
|
27: 64,
|
|
|
|
28: 65,
|
|
|
|
29: 66,
|
|
|
|
30: 67,
|
|
|
|
31: 68,
|
|
|
|
32: 69,
|
|
|
|
33: 70,
|
|
|
|
34: 71,
|
|
|
|
35: 72,
|
|
|
|
36: 73,
|
|
|
|
37: 74,
|
|
|
|
38: 75,
|
|
|
|
39: 76,
|
|
|
|
40: 77,
|
|
|
|
41: 78,
|
|
|
|
42: 79,
|
|
|
|
43: 80,
|
|
|
|
44: 81,
|
|
|
|
45: 82,
|
|
|
|
46: 83,
|
|
|
|
47: 84,
|
|
|
|
48: 21,
|
|
|
|
49: 22,
|
|
|
|
50: 23,
|
|
|
|
51: 24,
|
|
|
|
52: 25,
|
|
|
|
53: 26,
|
2018-04-02 21:06:00 -05:00
|
|
|
}
|
|
|
|
|
2020-11-25 12:28:36 -06:00
|
|
|
_qsfp_ports = list(range(_qsfp_port_start, _ports_in_block + 1))
|
2018-04-02 21:06:00 -05:00
|
|
|
|
2019-08-07 11:33:56 -05:00
|
|
|
_present_status = dict()
|
|
|
|
|
2018-04-02 21:06:00 -05:00
|
|
|
def __init__(self):
|
|
|
|
eeprom_path = '/sys/bus/i2c/devices/{0}-0050/sfp_eeprom'
|
|
|
|
for x in range(self._port_start, self._port_end + 1):
|
|
|
|
port_eeprom_path = eeprom_path.format(self._port_to_i2c_mapping[x])
|
2020-11-25 12:28:36 -06:00
|
|
|
self._port_to_eeprom_mapping[x] = port_eeprom_path
|
2019-08-07 11:33:56 -05:00
|
|
|
self._present_status[x] = SFP_STATUS_REMOVED
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2019-08-07 11:33:56 -05:00
|
|
|
SfpUtilBase.__init__(self)
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2018-04-02 21:06:00 -05:00
|
|
|
def reset(self, port_num):
|
|
|
|
# Check for invalid port_num
|
2019-08-07 11:33:56 -05:00
|
|
|
if port_num < self._qsfp_port_start or port_num > self._port_end:
|
2020-11-25 12:28:36 -06:00
|
|
|
print("Error: port %d is not qsfp port" % port_num)
|
2018-04-02 21:06:00 -05:00
|
|
|
return False
|
|
|
|
|
|
|
|
path = "/sys/bus/i2c/devices/{0}-0050/sfp_port_reset"
|
|
|
|
port_ps = path.format(self._port_to_i2c_mapping[port_num])
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2018-04-02 21:06:00 -05:00
|
|
|
try:
|
|
|
|
reg_file = open(port_ps, 'w')
|
|
|
|
except IOError as e:
|
2020-11-25 12:28:36 -06:00
|
|
|
print("Error: unable to open file: %s" % str(e))
|
2018-04-02 21:06:00 -05:00
|
|
|
return False
|
|
|
|
|
2020-11-25 12:28:36 -06:00
|
|
|
# toggle reset
|
2018-04-02 21:06:00 -05:00
|
|
|
reg_file.seek(0)
|
|
|
|
reg_file.write('1')
|
|
|
|
time.sleep(1)
|
|
|
|
reg_file.seek(0)
|
|
|
|
reg_file.write('0')
|
|
|
|
reg_file.close()
|
|
|
|
return True
|
|
|
|
|
|
|
|
def set_low_power_mode(self, port_nuM, lpmode):
|
|
|
|
raise NotImplementedError
|
|
|
|
|
|
|
|
def get_low_power_mode(self, port_num):
|
|
|
|
raise NotImplementedError
|
2019-08-07 11:33:56 -05:00
|
|
|
|
2018-04-02 21:06:00 -05:00
|
|
|
def get_presence(self, port_num):
|
|
|
|
# Check for invalid port_num
|
|
|
|
if port_num < self._port_start or port_num > self._port_end:
|
|
|
|
return False
|
|
|
|
|
|
|
|
path = "/sys/bus/i2c/devices/{0}-0050/sfp_is_present"
|
|
|
|
port_ps = path.format(self._port_to_i2c_mapping[port_num])
|
|
|
|
|
2020-02-07 09:53:55 -06:00
|
|
|
reg_value = '0'
|
2018-04-02 21:06:00 -05:00
|
|
|
try:
|
|
|
|
reg_file = open(port_ps)
|
2020-02-07 09:53:55 -06:00
|
|
|
reg_value = reg_file.readline().rstrip()
|
|
|
|
reg_file.close()
|
2018-04-02 21:06:00 -05:00
|
|
|
except IOError as e:
|
2020-11-25 12:28:36 -06:00
|
|
|
print("Error: unable to access file: %s" % str(e))
|
2018-04-02 21:06:00 -05:00
|
|
|
return False
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2018-04-02 21:06:00 -05:00
|
|
|
if reg_value == '1':
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
2020-11-25 12:28:36 -06:00
|
|
|
|
2018-12-05 02:30:38 -06:00
|
|
|
def get_transceiver_change_event(self, timeout=0):
|
|
|
|
raise NotImplementedError
|
2018-04-02 21:06:00 -05:00
|
|
|
|
|
|
|
@property
|
|
|
|
def port_start(self):
|
|
|
|
return self._port_start
|
|
|
|
|
|
|
|
@property
|
|
|
|
def port_end(self):
|
|
|
|
return self._port_end
|
|
|
|
|
|
|
|
@property
|
|
|
|
def qsfp_ports(self):
|
|
|
|
return self._qsfp_ports
|
|
|
|
|
2020-11-25 12:28:36 -06:00
|
|
|
@property
|
2018-04-02 21:06:00 -05:00
|
|
|
def port_to_eeprom_mapping(self):
|
2020-11-25 12:28:36 -06:00
|
|
|
return self._port_to_eeprom_mapping
|
2019-08-07 11:33:56 -05:00
|
|
|
|
|
|
|
def get_transceiver_change_event(self, timeout=0):
|
|
|
|
ret_present = dict()
|
|
|
|
for phy_port in range(self._port_start, self._port_end + 1):
|
|
|
|
last_present_status = SFP_STATUS_INSERTED if self.get_presence(phy_port) else SFP_STATUS_REMOVED
|
|
|
|
if self._present_status[phy_port] != last_present_status:
|
2020-11-25 12:28:36 -06:00
|
|
|
ret_present[phy_port] = last_present_status
|
|
|
|
self._present_status[phy_port] = last_present_status
|
|
|
|
|
2019-08-07 11:33:56 -05:00
|
|
|
time.sleep(2)
|
2019-11-08 23:49:46 -06:00
|
|
|
|
2020-11-25 12:28:36 -06:00
|
|
|
return True, ret_present
|