|
| 1 | +#!/usr/bin/env python |
| 2 | + |
| 3 | +try: |
| 4 | + import os |
| 5 | + import re |
| 6 | + import time |
| 7 | + from sonic_sfp.sfputilbase import SfpUtilBase |
| 8 | +except ImportError, e: |
| 9 | + raise ImportError (str(e) + "- required module not found") |
| 10 | + |
| 11 | + |
| 12 | +class SfpUtil(SfpUtilBase): |
| 13 | + """Platform specific sfputil class""" |
| 14 | + |
| 15 | + port_start = 0 |
| 16 | + port_end = 53 |
| 17 | + ports_in_block = 54 |
| 18 | + cplda_sfp_num = 24 |
| 19 | + cpldb_sfp_num = 12 |
| 20 | + cpldc_sfp_num = 18 |
| 21 | + |
| 22 | + port_to_eeprom_mapping = {} |
| 23 | + port_to_i2c_mapping = {} |
| 24 | + sfp_ports = range(0, ports_in_block) |
| 25 | + qsfp_ports = range(ports_in_block - 6, ports_in_block) |
| 26 | + |
| 27 | + |
| 28 | + def __init__(self): |
| 29 | + for x in range(self.port_start, self.port_end + 1): |
| 30 | + if x < self.cpldb_sfp_num: |
| 31 | + self.port_to_i2c_mapping.update({x:7}) |
| 32 | + elif x < self.cplda_sfp_num + self.cpldb_sfp_num: |
| 33 | + self.port_to_i2c_mapping.update({x:6}) |
| 34 | + else: |
| 35 | + self.port_to_i2c_mapping.update({x:8}) |
| 36 | + |
| 37 | + for x in range(self.port_start, self.port_end+1): |
| 38 | + eeprom_path = '/sys/bus/i2c/devices/{0}-0050/sfp'+str(x+1)+'_eeprom' |
| 39 | + port_eeprom_path = eeprom_path.format(self.port_to_i2c_mapping[x]) |
| 40 | + self.port_to_eeprom_mapping[x] = port_eeprom_path |
| 41 | + SfpUtilBase.__init__(self) |
| 42 | + |
| 43 | + |
| 44 | + def get_presence(self, port_num): |
| 45 | + if port_num < self.port_start or port_num > self.port_end: |
| 46 | + return False |
| 47 | + |
| 48 | + if port_num < self.cpldb_sfp_num: |
| 49 | + presence_path = '/sys/bus/i2c/devices/7-0075/sfp'+str(port_num+1)+'_present' |
| 50 | + elif port_num < self.cpldb_sfp_num + self.cplda_sfp_num: |
| 51 | + presence_path = '/sys/bus/i2c/devices/6-0074/sfp'+str(port_num+1)+'_present' |
| 52 | + else: |
| 53 | + presence_path = '/sys/bus/i2c/devices/8-0076/sfp'+str(port_num+1)+'_present' |
| 54 | + |
| 55 | + try: |
| 56 | + file = open(presence_path) |
| 57 | + except IOError as e: |
| 58 | + print "Error: unable to open file: %s" % str(e) |
| 59 | + return False |
| 60 | + |
| 61 | + value = int(file.readline().rstrip()) |
| 62 | + |
| 63 | + file.close() |
| 64 | + if value == 0: |
| 65 | + return True |
| 66 | + |
| 67 | + return False |
| 68 | + |
| 69 | + def get_low_power_mode(self, port_num): |
| 70 | + if port_num not in self.qsfp_ports: |
| 71 | + return False |
| 72 | + |
| 73 | + lowpower_path = '/sys/bus/i2c/devices/8-0076/sfp'+str(port_num+1)+'_lowpower' |
| 74 | + |
| 75 | + try: |
| 76 | + file = open(lowpower_path) |
| 77 | + except IOError as e: |
| 78 | + print "Error: unable to open file: %s" % str(e) |
| 79 | + return False |
| 80 | + |
| 81 | + value = int(file.readline().rstrip()) |
| 82 | + |
| 83 | + file.close() |
| 84 | + if value == 1: |
| 85 | + return True |
| 86 | + |
| 87 | + return False |
| 88 | + |
| 89 | + def set_low_power_mode(self, port_num, lpmode): |
| 90 | + if port_num not in self.qsfp_ports: |
| 91 | + return False |
| 92 | + |
| 93 | + lowpower_path = '/sys/bus/i2c/devices/8-0076/sfp'+str(port_num+1)+'_lowpower' |
| 94 | + |
| 95 | + # LPMode is active high; set or clear the bit accordingly |
| 96 | + if lpmode is True: |
| 97 | + value = 1 |
| 98 | + else: |
| 99 | + value = 0 |
| 100 | + |
| 101 | + try: |
| 102 | + file = open(lowpower_path, "r+") |
| 103 | + except IOError as e: |
| 104 | + print "Error: unable to open file: %s" % str(e) |
| 105 | + return False |
| 106 | + |
| 107 | + file.seek(0) |
| 108 | + file.write(str(value)) |
| 109 | + file.close() |
| 110 | + |
| 111 | + return True |
| 112 | + |
| 113 | + def reset(self, port_num): |
| 114 | + if port_num not in self.qsfp_ports: |
| 115 | + return False |
| 116 | + reset_path = '/sys/bus/i2c/devices/8-0076/sfp'+str(port_num+1)+'_reset' |
| 117 | + |
| 118 | + try: |
| 119 | + file = open(reset_path, "r+") |
| 120 | + except IOError as e: |
| 121 | + print "Error: unable to open file: %s" % str(e) |
| 122 | + return False |
| 123 | + |
| 124 | + file.seek(0) |
| 125 | + file.write(str(2)) |
| 126 | + file.close() |
| 127 | + |
| 128 | + # Sleep 1 second to allow it to settle |
| 129 | + time.sleep(1) |
| 130 | + |
| 131 | + try: |
| 132 | + file = open(reset_path, "r+") |
| 133 | + except IOError as e: |
| 134 | + print "Error: unable to open file: %s" % str(e) |
| 135 | + return False |
| 136 | + |
| 137 | + file.seek(0) |
| 138 | + file.write(str(1)) |
| 139 | + file.close() |
| 140 | + |
| 141 | + return True |
| 142 | + |
| 143 | + def read_porttab_mappings(self, porttabfile): |
| 144 | + logical = [] |
| 145 | + logical_to_bcm = {} |
| 146 | + logical_to_physical = {} |
| 147 | + physical_to_logical = {} |
| 148 | + last_fp_port_index = 0 |
| 149 | + last_portname = "" |
| 150 | + first = 1 |
| 151 | + port_pos_in_file = 0 |
| 152 | + parse_fmt_port_config_ini = False |
| 153 | + |
| 154 | + try: |
| 155 | + f = open(porttabfile) |
| 156 | + except: |
| 157 | + raise |
| 158 | + |
| 159 | + parse_fmt_port_config_ini = (os.path.basename(porttabfile) == "port_config.ini") |
| 160 | + |
| 161 | + # Read the porttab file and generate dicts |
| 162 | + # with mapping for future reference. |
| 163 | + # XXX: move the porttab |
| 164 | + # parsing stuff to a separate module, or reuse |
| 165 | + # if something already exists |
| 166 | + for line in f: |
| 167 | + line.strip() |
| 168 | + if re.search("^#", line) is not None: |
| 169 | + continue |
| 170 | + |
| 171 | + # Parsing logic for 'port_config.ini' file |
| 172 | + if (parse_fmt_port_config_ini): |
| 173 | + # bcm_port is not explicitly listed in port_config.ini format |
| 174 | + # Currently we assume ports are listed in numerical order according to bcm_port |
| 175 | + # so we use the port's position in the file (zero-based) as bcm_port |
| 176 | + portname = line.split()[0] |
| 177 | + |
| 178 | + bcm_port = str(port_pos_in_file) |
| 179 | + |
| 180 | + if len(line.split()) >= 4: |
| 181 | + fp_port_index = int(line.split()[3]) |
| 182 | + else: |
| 183 | + fp_port_index = portname.split("Ethernet").pop() |
| 184 | + fp_port_index = int(fp_port_index.split("s").pop(0))/4 |
| 185 | + else: # Parsing logic for older 'portmap.ini' file |
| 186 | + (portname, bcm_port) = line.split("=")[1].split(",")[:2] |
| 187 | + |
| 188 | + fp_port_index = portname.split("Ethernet").pop() |
| 189 | + fp_port_index = int(fp_port_index.split("s").pop(0))/4 |
| 190 | + |
| 191 | + if ((len(self.sfp_ports) > 0) and (fp_port_index not in self.sfp_ports)): |
| 192 | + continue |
| 193 | + |
| 194 | + if first == 1: |
| 195 | + # Initialize last_[physical|logical]_port |
| 196 | + # to the first valid port |
| 197 | + last_fp_port_index = fp_port_index |
| 198 | + last_portname = portname |
| 199 | + first = 0 |
| 200 | + |
| 201 | + logical.append(portname) |
| 202 | + |
| 203 | + logical_to_bcm[portname] = "xe" + bcm_port |
| 204 | + logical_to_physical[portname] = [fp_port_index] |
| 205 | + if physical_to_logical.get(fp_port_index) is None: |
| 206 | + physical_to_logical[fp_port_index] = [portname] |
| 207 | + else: |
| 208 | + physical_to_logical[fp_port_index].append( |
| 209 | + portname) |
| 210 | + |
| 211 | + if (fp_port_index - last_fp_port_index) > 1: |
| 212 | + # last port was a gang port |
| 213 | + for p in range(last_fp_port_index+1, fp_port_index): |
| 214 | + logical_to_physical[last_portname].append(p) |
| 215 | + if physical_to_logical.get(p) is None: |
| 216 | + physical_to_logical[p] = [last_portname] |
| 217 | + else: |
| 218 | + physical_to_logical[p].append(last_portname) |
| 219 | + |
| 220 | + last_fp_port_index = fp_port_index |
| 221 | + last_portname = portname |
| 222 | + |
| 223 | + port_pos_in_file += 1 |
| 224 | + |
| 225 | + self.logical = logical |
| 226 | + self.logical_to_bcm = logical_to_bcm |
| 227 | + self.logical_to_physical = logical_to_physical |
| 228 | + self.physical_to_logical = physical_to_logical |
| 229 | + |
| 230 | + """ |
| 231 | + print "logical: " + self.logical |
| 232 | + print "logical to bcm: " + self.logical_to_bcm |
| 233 | + print "logical to physical: " + self.logical_to_physical |
| 234 | + print "physical to logical: " + self.physical_to_logical |
| 235 | + """ |
| 236 | + |
| 237 | + |
| 238 | + |
0 commit comments