|
1 |
| -#!/usr/bin/env python |
| 1 | +# sfputil.py |
| 2 | +# |
| 3 | +# Platform-specific SFP transceiver interface for SONiC |
| 4 | +# |
2 | 5 |
|
3 | 6 | try:
|
4 |
| - from sonic_sfp.sfputilbase import sfputilbase |
5 |
| -except ImportError, e: |
6 |
| - raise ImportError (str(e) + "- required module not found") |
| 7 | + import time |
| 8 | + from sonic_sfp.sfputilbase import SfpUtilBase |
| 9 | +except ImportError as e: |
| 10 | + raise ImportError("%s - required module not found" % str(e)) |
7 | 11 |
|
8 | 12 |
|
9 |
| -class sfputil(sfputilbase): |
10 |
| - """Platform specific sfputil class""" |
| 13 | +class SfpUtil(SfpUtilBase): |
| 14 | + """Platform-specific SfpUtil class""" |
11 | 15 |
|
12 |
| - port_start = 0 |
13 |
| - port_end = 31 |
14 |
| - ports_in_block = 32 |
| 16 | + PORT_START = 0 |
| 17 | + PORT_END = 31 |
| 18 | + PORTS_IN_BLOCK = 32 |
15 | 19 |
|
16 |
| - port_to_eeprom_mapping = {} |
| 20 | + BASE_DIR_PATH = "/sys/class/gpio/gpio{0}/direction" |
| 21 | + BASE_VAL_PATH = "/sys/class/gpio/gpio{0}/value" |
| 22 | + |
| 23 | + _port_to_eeprom_mapping = {} |
17 | 24 | port_to_i2c_mapping = {
|
18 | 25 | 0: 11,
|
19 | 26 | 1: 10,
|
@@ -49,12 +56,227 @@ class sfputil(sfputilbase):
|
49 | 56 | 31: 40
|
50 | 57 | }
|
51 | 58 |
|
52 |
| - _qsfp_ports = range(0, ports_in_block + 1) |
| 59 | + abs_to_gpio_mapping = { |
| 60 | + 0: 241, |
| 61 | + 1: 240, |
| 62 | + 2: 243, |
| 63 | + 3: 242, |
| 64 | + 4: 245, |
| 65 | + 5: 244, |
| 66 | + 6: 247, |
| 67 | + 7: 246, |
| 68 | + 8: 249, |
| 69 | + 9: 248, |
| 70 | + 10: 251, |
| 71 | + 11: 250, |
| 72 | + 12: 253, |
| 73 | + 13: 252, |
| 74 | + 14: 255, |
| 75 | + 15: 254, |
| 76 | + 16: 225, |
| 77 | + 17: 224, |
| 78 | + 18: 227, |
| 79 | + 19: 226, |
| 80 | + 20: 229, |
| 81 | + 21: 228, |
| 82 | + 22: 231, |
| 83 | + 23: 230, |
| 84 | + 24: 233, |
| 85 | + 25: 232, |
| 86 | + 26: 235, |
| 87 | + 27: 234, |
| 88 | + 28: 237, |
| 89 | + 29: 236, |
| 90 | + 30: 239, |
| 91 | + 31: 238 |
| 92 | + } |
| 93 | + |
| 94 | + lpmode_to_gpio_mapping = { |
| 95 | + 0: 177, |
| 96 | + 1: 176, |
| 97 | + 2: 179, |
| 98 | + 3: 178, |
| 99 | + 4: 181, |
| 100 | + 5: 180, |
| 101 | + 6: 183, |
| 102 | + 7: 182, |
| 103 | + 8: 185, |
| 104 | + 9: 184, |
| 105 | + 10: 187, |
| 106 | + 11: 186, |
| 107 | + 12: 189, |
| 108 | + 13: 188, |
| 109 | + 14: 191, |
| 110 | + 15: 190, |
| 111 | + 16: 161, |
| 112 | + 17: 160, |
| 113 | + 18: 163, |
| 114 | + 19: 162, |
| 115 | + 20: 165, |
| 116 | + 21: 164, |
| 117 | + 22: 167, |
| 118 | + 23: 166, |
| 119 | + 24: 169, |
| 120 | + 25: 168, |
| 121 | + 26: 171, |
| 122 | + 27: 170, |
| 123 | + 28: 173, |
| 124 | + 29: 172, |
| 125 | + 30: 175, |
| 126 | + 31: 174 |
| 127 | + } |
| 128 | + |
| 129 | + reset_to_gpio_mapping = { |
| 130 | + 0: 145, |
| 131 | + 1: 144, |
| 132 | + 2: 147, |
| 133 | + 3: 146, |
| 134 | + 4: 149, |
| 135 | + 5: 148, |
| 136 | + 6: 151, |
| 137 | + 7: 150, |
| 138 | + 8: 153, |
| 139 | + 9: 152, |
| 140 | + 10: 155, |
| 141 | + 11: 154, |
| 142 | + 12: 157, |
| 143 | + 13: 156, |
| 144 | + 14: 159, |
| 145 | + 15: 158, |
| 146 | + 16: 129, |
| 147 | + 17: 128, |
| 148 | + 18: 131, |
| 149 | + 19: 130, |
| 150 | + 20: 133, |
| 151 | + 21: 132, |
| 152 | + 22: 135, |
| 153 | + 23: 134, |
| 154 | + 24: 137, |
| 155 | + 25: 136, |
| 156 | + 26: 139, |
| 157 | + 27: 138, |
| 158 | + 28: 141, |
| 159 | + 29: 140, |
| 160 | + 30: 143, |
| 161 | + 31: 142 |
| 162 | + } |
| 163 | + |
| 164 | + @property |
| 165 | + def port_start(self): |
| 166 | + return self.PORT_START |
| 167 | + |
| 168 | + @property |
| 169 | + def port_end(self): |
| 170 | + return self.PORT_END |
| 171 | + |
| 172 | + @property |
| 173 | + def qsfp_ports(self): |
| 174 | + return range(0, self.PORTS_IN_BLOCK + 1) |
53 | 175 |
|
54 |
| - def __init__(self, port_num): |
| 176 | + @property |
| 177 | + def port_to_eeprom_mapping(self): |
| 178 | + return self._port_to_eeprom_mapping |
| 179 | + |
| 180 | + def __init__(self): |
55 | 181 | # Override port_to_eeprom_mapping for class initialization
|
56 |
| - eeprom_path = '/sys/class/i2c-adapter/i2c-{0}/{0}-0050/eeprom' |
| 182 | + eeprom_path = "/sys/class/i2c-adapter/i2c-{0}/{0}-0050/eeprom" |
| 183 | + |
57 | 184 | for x in range(self.port_start, self.port_end + 1):
|
58 | 185 | port_eeprom_path = eeprom_path.format(self.port_to_i2c_mapping[x])
|
59 | 186 | self.port_to_eeprom_mapping[x] = port_eeprom_path
|
60 |
| - sfputilbase.__init__(self, port_num) |
| 187 | + |
| 188 | + SfpUtilBase.__init__(self) |
| 189 | + |
| 190 | + def get_presence(self, port_num): |
| 191 | + # Check for invalid port_num |
| 192 | + if port_num < self.port_start or port_num > self.port_end: |
| 193 | + return False |
| 194 | + |
| 195 | + try: |
| 196 | + abs_device_file = self.BASE_VAL_PATH.format( |
| 197 | + self.abs_to_gpio_mapping[port_num]) |
| 198 | + val_file = open(abs_device_file) |
| 199 | + except IOError as e: |
| 200 | + print "Error: unable to open file: %s" % str(e) |
| 201 | + return False |
| 202 | + |
| 203 | + content = val_file.readline().rstrip() |
| 204 | + val_file.close() |
| 205 | + |
| 206 | + # content is a string, either "0" or "1" |
| 207 | + if content == "1": |
| 208 | + return True |
| 209 | + |
| 210 | + return False |
| 211 | + |
| 212 | + def get_low_power_mode(self, port_num): |
| 213 | + # Check for invalid port_num |
| 214 | + if port_num < self.port_start or port_num > self.port_end: |
| 215 | + return False |
| 216 | + |
| 217 | + try: |
| 218 | + lpmode_val_device_file = self.BASE_VAL_PATH.format( |
| 219 | + self.lpmode_to_gpio_mapping[port_num]) |
| 220 | + val_file = open(lpmode_val_device_file) |
| 221 | + except IOError as e: |
| 222 | + print "Error: unable to open file: %s" % str(e) |
| 223 | + return False |
| 224 | + |
| 225 | + content = val_file.readline().rstrip() |
| 226 | + val_file.close() |
| 227 | + |
| 228 | + # content is a string, either "0" or "1" |
| 229 | + if content == "1": |
| 230 | + return True |
| 231 | + |
| 232 | + return False |
| 233 | + |
| 234 | + def set_low_power_mode(self, port_num, lpmode): |
| 235 | + # Check for invalid port_num |
| 236 | + if port_num < self.port_start or port_num > self.port_end: |
| 237 | + return False |
| 238 | + |
| 239 | + try: |
| 240 | + lpmode_val_device_file = self.BASE_VAL_PATH.format( |
| 241 | + self.lpmode_to_gpio_mapping[port_num]) |
| 242 | + val_file = open(lpmode_val_device_file, "w") |
| 243 | + except IOError as e: |
| 244 | + print "Error: unable to open file: %s" % str(e) |
| 245 | + return False |
| 246 | + |
| 247 | + val_file.write("1" if lpmode is True else "0") |
| 248 | + val_file.close() |
| 249 | + |
| 250 | + return True |
| 251 | + |
| 252 | + def reset(self, port_num): |
| 253 | + # Check for invalid port_num |
| 254 | + if port_num < self.port_start or port_num > self.port_end: |
| 255 | + return False |
| 256 | + |
| 257 | + try: |
| 258 | + reset_val_device_file = self.BASE_VAL_PATH.format( |
| 259 | + self.reset_to_gpio_mapping[port_num]) |
| 260 | + val_file = open(reset_val_device_file, "w") |
| 261 | + except IOError as e: |
| 262 | + print "Error: unable to open file: %s" % str(e) |
| 263 | + return False |
| 264 | + |
| 265 | + val_file.write("1") |
| 266 | + val_file.close() |
| 267 | + |
| 268 | + # Sleep 1 second to allow it to settle |
| 269 | + time.sleep(1) |
| 270 | + |
| 271 | + try: |
| 272 | + reset_val_device_file = self.BASE_VAL_PATH.format( |
| 273 | + self.reset_to_gpio_mapping[port_num]) |
| 274 | + val_file = open(reset_val_device_file, "w") |
| 275 | + except IOError as e: |
| 276 | + print "Error: unable to open file: %s" % str(e) |
| 277 | + return False |
| 278 | + |
| 279 | + val_file.write("0") |
| 280 | + val_file.close() |
| 281 | + |
| 282 | + return True |
0 commit comments