def __init__(self): log = Logger(__name__) self.logger = log.getLogger() self.ioexp = IOExpander() self.ipmitool = IPMITool() self.brd_id_util = BrdIDUtility() self.board_id = self.brd_id_util.get_board_id()
def __init__(self): log = Logger(__name__) self.logger = log.getLogger() self.ioexp = IOExpander() self.cpld = CPLD() self.lpc = LPC() self.ioexp = IOExpander() #GPIO REGs self.ALERT_GPIO_REG = 0x1 #I2C Alert REGs self.ALERT_STATUS_REG = 0x0 self.ALERT_DIS_REG = 0x11
def ut_sysfs(argv): try: ioexp_util = IOExpander() eeprom_util = EEPRomUtility() if len(argv) < 3: sysfs_usage(argv[0]) elif argv[2] == '1': print( json.dumps(ioexp_util.dump_sysfs(), sort_keys=False, indent=4)) elif argv[2] == '2': print( json.dumps(eeprom_util.dump_sysfs(), sort_keys=False, indent=4)) else: sysfs_usage(argv[0]) except: raise
class UARTMuxUtility: def __init__(self): log = Logger(__name__) self.logger = log.getLogger() self.ioexp = IOExpander() self.ipmitool = IPMITool() self.brd_id_util = BrdIDUtility() self.board_id = self.brd_id_util.get_board_id() def set_uart_mux(self, source): try: if source not in [UARTMux.CPU, UARTMux.BMC]: raise ValueError( "invalid uart source {0}, range is 0-1".format(source)) if self.board_id == BID.NCP1_1_PROTO: self.ioexp.set_uart_mux(source) else: self.ipmitool.set_uart_mux(source) except Exception as e: self.logger.error("set_uart_mux {} failed, error: {}".format( source, e))
class USBMuxUtility: def __init__(self): log = Logger(__name__) self.logger = log.getLogger() self.ioexp = IOExpander() self.brd_id_util = BrdIDUtility() self.board_id = self.brd_id_util.get_board_id() def set_usb_mux(self, source): try: if source not in [USBMux.CPU, USBMux.BMC]: raise ValueError( "invalid usb source {0}, range is 0-1".format(source)) if (self.board_id & BID.BUILD_REV_MASK) >= BID.NCP1_1_BETA: raise ValueError( "USB Mux is not supported since BETA".format(source)) self.ioexp.set_usb_mux(source) except Exception as e: self.logger.error("set_usb_mux {0} failed, error: {1}".format( source, e))
def __init__(self): try: log = Logger(__name__) self.logger = log.getLogger() # I2C drivers self._load_i2c_drivers() #Check Mux Ctrl self.lpc = LPC() self._check_i2c_mux_ctrl() self.ioexp = IOExpander() self.i2c_mux = I2CMux() self.eeprom = EEPRom() self.cpld = CPLD() self.thermal = Thermal() self.rov_util = ROVUtility() self.intr_util = INTRUtility() self.bsp_util = BSPUtility() except Exception: raise
def __init__(self): log = Logger(__name__) self.logger = log.getLogger() self.ioexp = IOExpander()
class BEACON_LED: SEG7_L = [ [0, 0, 0, 0, 1, 0, 0], #0 [0, 0, 1, 1, 1, 1, 1], #1 [1, 0, 0, 0, 0, 1, 0], #2 [0, 0, 1, 0, 0, 1, 0], #3 [0, 0, 1, 1, 0, 0, 1], #4 [0, 1, 1, 0, 0, 0, 0], #5 [0, 1, 0, 0, 0, 0, 0], #6 [0, 0, 1, 1, 1, 0, 0], #7 [0, 0, 0, 0, 0, 0, 0], #8 [0, 0, 1, 1, 0, 0, 0], #9 [0, 0, 0, 1, 0, 0, 0], #a [0, 1, 0, 0, 0, 0, 1], #b [1, 1, 0, 0, 1, 0, 0], #c [0, 0, 0, 0, 0, 1, 1], #d [1, 1, 0, 0, 0, 0, 0], #e [1, 1, 0, 1, 0, 0, 0] ] #f SEG7_R = [ [0, 0, 0, 0, 0, 0, 1], #0 [1, 1, 0, 1, 1, 0, 1], #1 [0, 0, 1, 1, 0, 0, 0], #2 [0, 1, 0, 1, 0, 0, 0], #3 [1, 1, 0, 0, 1, 0, 0], #4 [0, 1, 0, 0, 0, 1, 0], #5 [0, 0, 0, 0, 0, 1, 0], #6 [0, 1, 0, 0, 1, 0, 1], #7 [0, 0, 0, 0, 0, 0, 0], #8 [0, 1, 0, 0, 1, 0, 0], #9 [0, 0, 0, 0, 1, 0, 0], #a [1, 0, 0, 0, 0, 1, 0], #b [0, 0, 1, 0, 0, 1, 1], #c [1, 0, 0, 1, 0, 0, 0], #d [0, 0, 1, 0, 0, 1, 0], #e [0, 0, 1, 0, 1, 1, 0] ] #f def __init__(self): log = Logger(__name__) self.logger = log.getLogger() self.ioexp = IOExpander() def set_beacon_num(self, beacon_num): #int to hex string beacon_hex = '{:02x}'.format(beacon_num) try: #left/right 7-seg led seg7_left = self.SEG7_L[int(beacon_hex[0], 16)] seg7_right = self.SEG7_R[int(beacon_hex[1], 16)] self.ioexp.set_beacon_led(seg7_left, seg7_right) except Exception as e: self.logger.error(e) raise def init(self): pass def deinit(self): pass
class PlatformUtility: builtin_pca954x_path = "/lib/modules/{}/kernel/drivers/i2c/muxes/i2c-mux-pca954x.ko" #kernel release version kr_version_cmd = "uname -r" depmod_cmd = "depmod -a" #pci device PCI_CHECK_LIST = { "00:14.0": "USB controller", "00:1d.0": "USB controller", "00:1f.2": "SATA controller", "00:1f.3": "SMBus", "02:00.0": "10 GbE SFP+", "02:00.1": "10 GbE SFP+", "06:00.0": "PEX 8724", "07:01.0": "PEX 8724", "07:02.0": "PEX 8724", "07:08.0": "PEX 8724", "07:09.0": "PEX 8724", "07:0a.0": "PEX 8724", "0b:00.0": "J2", "0c:00.0": "OP2", "0d:00.0": "I210" } USB_CHECK_LIST = {"046b:ff20": "BMC", "046b:ff01": "BMC"} BSP_INIT_STOP = "[BSP Init Stop]" IGNORE_ERR = True def __init__(self): try: log = Logger(__name__) self.logger = log.getLogger() # I2C drivers self._load_i2c_drivers() #Check Mux Ctrl self.lpc = LPC() self._check_i2c_mux_ctrl() self.ioexp = IOExpander() self.i2c_mux = I2CMux() self.eeprom = EEPRom() self.cpld = CPLD() self.thermal = Thermal() self.rov_util = ROVUtility() self.intr_util = INTRUtility() self.bsp_util = BSPUtility() except Exception: raise def device_init(self): try: # Unmount kernel modules self._unload_kernel_module("gpio_ich") # Mount kernel modules # I2C Mux subprocess.run( ['modprobe', 'i2c_mux_pca954x', 'force_deselect_on_exit=1']) self.i2c_mux.init() # EEPROM self._load_kernel_module("x86_64_ufispace_s9700_53dx_eeprom_mb") self._load_kernel_module("x86_64_ufispace_s9700_53dx_optoe") self.eeprom.init() # Thermal self._load_kernel_module("lm75") self.thermal.init() # GPIO self._load_kernel_module("gpio-pca953x") self.ioexp.init(self.i2c_mux.MUXs) # IPMI self._load_kernel_module("ipmi_devintf") self._load_kernel_module("ipmi_si") # CPLD self._load_kernel_module(self.cpld.DRIVER_NAME) self.cpld.init() except Exception: raise def device_deinit(self): try: # Remove kernel modules # CPLD self.cpld.deinit() self._unload_kernel_module(self.cpld.DRIVER_NAME) # GPIO self.ioexp.deinit(self.i2c_mux.MUXs) self._unload_kernel_module("gpio-pca953x") # Thermal self.thermal.deinit() self._unload_kernel_module("lm75") # MB EEPROM self._unload_kernel_module("x86_64_ufispace_s9700_53dx_eeprom_mb") # OPTOE EEPROM self._unload_kernel_module("x86_64_ufispace_s9700_53dx_optoe") # I2C Mux self.i2c_mux.deinit() self._unload_kernel_module("i2c_mux_pca954x") except Exception: raise def _get_kernel_release(self): try: return subprocess.getoutput(self.kr_version_cmd) except Exception: raise def _check_bsp_init(self): try: is_bsp_inited = self.bsp_util.is_bsp_inited() if is_bsp_inited: self.logger.error( "BSP may be already initialized. Please run 'platform_utility.py deinit' first" ) self.logger.error(self.BSP_INIT_STOP) sys.exit() except Exception: raise def _check_ko(self): try: #get kernel release kr = self._get_kernel_release() #remove built-in i2c-mux-pca954x.ko if exist pca954x_path = self.builtin_pca954x_path.format(kr) if os.path.exists(pca954x_path): os.remove(pca954x_path) #generate modules.dep subprocess.run(self.depmod_cmd, shell=True) except Exception: raise def check_idt(self): try: #Open Mux(0x73) for IDT(0x53) retcode, _ = subprocess.getstatusoutput("i2cset -y 0 0x73 0x8") if retcode != 0: self.logger.error( "Open I2C Mux channel (0x73) for IDT failed.") return #Check IDT retcode, _ = subprocess.getstatusoutput("i2cget -y 0 0x53 0x0") if retcode != 0: self.logger.error( "Access IDT(0x53) failed. Trying to reset IDT") #Reset IDT self.lpc.regSet(LPCDevType.CPLD_ON_MAIN_BOARD, 0x4D, 0xF7) sleep(0.5) #Access IDT again retcode, _ = subprocess.getstatusoutput("i2cget -y 0 0x53 0x0") if retcode != 0: self.logger.error( "Access IDT(0x53) failed and reset still failed") else: self.logger.error( "Access IDT(0x53) failed and reset succeed") #Close Mux(0x73) retcode, _ = subprocess.getstatusoutput("i2cset -y 0 0x73 0x0") if retcode != 0: self.logger.error("Close I2C Mux channel (0x73) failed.") except Exception: raise def _check_intr(self): try: alert_dis = self.intr_util.get_alert_dis() if alert_dis == 0: self.logger.warning( "I2C Alert is already enabled. I2C tree performance may be affected." ) except Exception: raise def _check_i2c_mux_ctrl(self): try: default_val = 0x80 reg_val = self.lpc.regGet(LPCDevType.CPLD_ON_MAIN_BOARD, CPLDMBReg.REG_MUX_CTRL) if reg_val != default_val: self.logger.error( "I2C Mux Ctrl [0x{:02X}] is not valid. Expected [0x{:02X}]" .format(reg_val, default_val)) self.logger.error(self.BSP_INIT_STOP) sys.exit() else: self.logger.info("[Check Mux Control Register] Pass") except Exception as e: self.logger.error(e) raise def _check_cpld(self): #CPLD missing workaround :BEGIN #open mux 0x75 retcode, _ = subprocess.getstatusoutput("i2cset -y 0 0x75 0x1") if retcode != 0: self.logger.error( "Open I2C Mux channel 0x75 for CPLD WA failed, return code = {}, exit" .format(retcode)) self.logger.error(self.BSP_INIT_STOP) sys.exit() #Read CPLD version for i in range(len(self.cpld.CPLD_I2C_ADDR)): retcode, _ = subprocess.getstatusoutput( "i2cget -y 0 {} 0x2".format(self.cpld.CPLD_I2C_ADDR[i])) if retcode != 0: #log info for reading cpld version self.logger.info("Reading CPLD{} 0x{:02X} failed".format( i + 1, self.cpld.CPLD_I2C_ADDR[i])) #Check CPLD version for i in range(len(self.cpld.CPLD_I2C_ADDR)): retcode, _ = subprocess.getstatusoutput( "i2cget -y 0 {} 0x2".format(self.cpld.CPLD_I2C_ADDR[i])) if retcode != 0: #log error for checking cpld version self.logger.error( "Reading CPLD{} 0x{:02X} failed, return code = {}, exit". format(i + 1, self.cpld.CPLD_I2C_ADDR[i], retcode)) self.logger.error(self.BSP_INIT_STOP) sys.exit() #close mux 0x75 retcode, _ = subprocess.getstatusoutput("i2cset -y 0 0x75 0x0") if retcode != 0: self.logger.error( "Close I2C Mux channel for CPLD WA failed, return code = {}, exit" .format(retcode)) self.logger.error(self.BSP_INIT_STOP) sys.exit() #CPLD missing workaround :END self.logger.info("[Check CPLD] Pass") def _check_pci(self): err_cnt = 0 result = "" err_list = ["rev ff"] err_desc = ["under reset"] cmd_lspci = "lspci" cmd_lspci_s = "lspci -s {}" #check lspci command retcode, _ = subprocess.getstatusoutput(cmd_lspci) if retcode != 0: self.logger.warning( "lspci commnad not found, skip checking pci devices") return #check pci devices for pci_id in self.PCI_CHECK_LIST.keys(): pci_name = self.PCI_CHECK_LIST[pci_id] retcode, output = subprocess.getstatusoutput( cmd_lspci_s.format(pci_id)) #check device existence if retcode != 0: self.logger.error( "Invalid PCI device {} ({}), retcode={}, output={}".format( pci_name, pci_id, retcode, output)) err_cnt += 1 elif len(output) == 0: self.logger.error("PCI device {} ({}) not found".format( pci_name, pci_id)) if not self.IGNORE_ERR: self.logger.error(self.BSP_INIT_STOP) sys.exit() else: #check device error status for i in range(len(err_list)): if output.find(err_list[i]) >= 0: self.logger.warning("PCI device {} ({}) {}".format( pci_name, pci_id, err_desc[i])) err_cnt += 1 if err_cnt == 0: result = "Pass" self.logger.info("[Check PCI] {}".format(result)) else: result = "Fail" self.logger.warning("[Check PCI] {}".format(result)) def _check_usb(self): warn_cnt = 0 result = "" cmd_lsusb = "lsusb" cmd_lsusb_s = "lsusb -d {}" #check lsusb command retcode, _ = subprocess.getstatusoutput(cmd_lsusb) if retcode != 0: self.logger.warning( "cmd_lsusb commnad not found, skip checking usb devices") return #check usb devices for usb_id in self.USB_CHECK_LIST.keys(): usb_name = self.USB_CHECK_LIST[usb_id] retcode, output = subprocess.getstatusoutput( cmd_lsusb_s.format(usb_id)) #check device existence if retcode != 0: self.logger.warning("USB device {} ({}) not found".format( usb_name, usb_id)) warn_cnt += 1 elif len(output) == 0: self.logger.warning("USB device {} ({}) not found".format( usb_name, usb_id)) warn_cnt += 1 else: if output.find(usb_id) < 0: self.logger.warning("USB device {} ({}) not found".format( usb_name, usb_id)) warn_cnt += 1 if warn_cnt == 0: result = "Pass" self.logger.info("[Check USB] {}".format(result)) else: result = "Warning" self.logger.warning("[Check USB] {}".format(result)) return 0 def pre_init(self): self._check_bsp_init() # don't check ko for sonic platform #self._check_ko() self._check_intr() retcode = self.i2c_mux.pre_init() if retcode != 0: self.logger.error(self.BSP_INIT_STOP) sys.exit() self._check_cpld() self.check_idt() self._check_pci() self._check_usb() def post_init(self): try: #set j2 rov if self.rov_util.platform_check(): self.rov_util.set_j2_rov() except Exception: raise def _load_i2c_drivers(self): # I2C I801 self._load_kernel_module("i2c_i801") # I2C Dev self._load_kernel_module("i2c_dev") def _load_kernel_module(self, module_name): #replace "-" to "_" since lsmod show module name with "_" module_name = module_name.replace("-", "_") #check if module is already loaded retcode, _ = subprocess.getstatusoutput( "lsmod | grep {}".format(module_name)) #module is not loaded yet if retcode != 0: retcode, _ = subprocess.getstatusoutput( "modprobe {}".format(module_name)) #load module failed if retcode != 0: self.logger.error( "Load kernel module {} failed, retcode={}".format( module_name, retcode)) if not self.IGNORE_ERR: self.logger.error(self.BSP_INIT_STOP) sys.exit() def _unload_kernel_module(self, module_name): #replace "-" to "_" since lsmod show module name with "_" module_name = module_name.replace("-", "_") #check if module is already loaded retcode, _ = subprocess.getstatusoutput( "lsmod | grep {}".format(module_name)) #module is loaded if retcode == 0: retcode, _ = subprocess.getstatusoutput( "rmmod {}".format(module_name)) #unload module failed if retcode != 0: self.logger.error( "Unload kernel module {} failed, retcode={}".format( module_name, retcode))
class INTRUtility: def __init__(self): log = Logger(__name__) self.logger = log.getLogger() self.ioexp = IOExpander() self.cpld = CPLD() self.lpc = LPC() self.ioexp = IOExpander() #GPIO REGs self.ALERT_GPIO_REG = 0x1 #I2C Alert REGs self.ALERT_STATUS_REG = 0x0 self.ALERT_DIS_REG = 0x11 def init_i2c_alert(self): i2c_detect_cmd = "i2cdetect -y 0" #i2cdetect -y 0 retcode, _ = subprocess.getstatusoutput(i2c_detect_cmd) if retcode != 0: self.logger.error("i2c command error: {}".format(i2c_detect_cmd)) #Set pin function to alert mode on CPU #./ioset 0x501 0xf7 #IO_x500[11] SMBALERT_N_GPIO11 #0/Alert mode,1/GPIO mode #Default is 0 regVal = self.lpc.regGet(LPCDevType.BDE_GPIO_ON_CPU_BOARD, self.ALERT_GPIO_REG) self.lpc.regSet(LPCDevType.BDE_GPIO_ON_CPU_BOARD, self.ALERT_GPIO_REG, regVal & 0xF7) #Enable alert function on CPU #./ioset 0xf011 0x0 #IO_xF011[2] SMBALERT_DIS #0/Enable Alert, 1/Disable alert #Default is 1 regVal = self.lpc.regGet(LPCDevType.CPU_I2C_ALERT, self.ALERT_DIS_REG) self.lpc.regSet(LPCDevType.CPU_I2C_ALERT, self.ALERT_DIS_REG, regVal & 0xFB) def deinit_i2c_alert(self): regVal = self.lpc.regGet(LPCDevType.CPU_I2C_ALERT, self.ALERT_DIS_REG) self.lpc.regSet(LPCDevType.CPU_I2C_ALERT, self.ALERT_DIS_REG, regVal | 0x04) def get_alert_gpio(self): regVal = self.lpc.regGet(LPCDevType.BDE_GPIO_ON_CPU_BOARD, self.ALERT_GPIO_REG) return (regVal >> 3) & 1 def get_alert_dis(self): regVal = self.lpc.regGet(LPCDevType.CPU_I2C_ALERT, self.ALERT_DIS_REG) return (regVal >> 2) & 1 def get_alert_sts(self): regVal = self.lpc.regGet(LPCDevType.CPU_I2C_ALERT, self.ALERT_STATUS_REG) return (regVal >> 5) & 1 def clear_i2c_alert(self): intr_reg = "9539_HOST_GPIO_I2C" cpu_gpio_reg = "9539_CPU_I2C" #clear alert on ioexpander #i2cdump -y 0 0x74 reg_vals = self.ioexp.dump_reg(intr_reg) self.logger.info("clear_i2c_alert() intr_reg_vals={}".format(reg_vals)) #i2cdump -y 0 0x77 reg_vals = self.ioexp.dump_reg(cpu_gpio_reg) self.logger.info( "clear_i2c_alert() cpu_gpio_reg_vals={}".format(reg_vals)) #clear i2c alert status on CPU #./ioset 0xf000 0x60 self.lpc.regSet(LPCDevType.CPU_I2C_ALERT, self.ALERT_STATUS_REG, 0x60) def get_cpld_to_cpu_intr(self): cpld_intr_name = [ "op2", "cpld_1_2", "cpld_3", "cpld_4", "cpld_5", "j2" ] cpld_intr = {} try: cpld_intr_val = self.ioexp.get_cpld_to_cpu_intr() for i in range(len(cpld_intr_val)): cpld_intr[cpld_intr_name[i]] = cpld_intr_val[i] return cpld_intr except Exception as e: self.logger.error( "get_cpld_to_cpu_intr failed, error: {}".format(e)) def get_all_cpld_intr(self): cpld_name = "cpld_" all_cpld_intr = {} try: for i in range(CPLDConst.CPLD_MAX): cpld_intr = self.get_cpld_intr(i) all_cpld_intr[cpld_name + str(i + 1)] = cpld_intr return all_cpld_intr except Exception as e: self.logger.error("get_cpld_intr failed, error: {}".format(e)) def get_cpld_intr(self, cpld_num): cpld_intr_name = [ "port_intr", "gearbox", "usb", "port_presence", "psu0", "psu1", "pex8724", "cs4227", "retimer" ] cpld_intr = {} try: intr_reg = self.cpld.get_intr_reg(cpld_num, 0) if cpld_num == CPLDConst.CPLD_1: intr_reg_2 = self.cpld.get_intr_reg(cpld_num, 1) for i in range(8): cpld_intr[cpld_intr_name[i]] = intr_reg >> i & 0b1 cpld_intr[cpld_intr_name[8]] = intr_reg_2 & 0b1 else: cpld_intr[cpld_intr_name[0]] = intr_reg >> 0 & 0b1 cpld_intr[cpld_intr_name[3]] = intr_reg >> 3 & 0b1 return cpld_intr except Exception as e: self.logger.error("get_cpld_intr failed, error: {}".format(e)) def clear_all_intr(self): try: for i in range(CPLDConst.CPLD_MAX): self.get_cpld_intr(i) self.clear_i2c_alert() except Exception as e: self.logger.error("clear_all_intr failed, error: {}".format(e)) def get_gbox_intr(self): gbox_name = "gearbox" gbox_intr = {} gbox_max_1 = 8 gbox_max_2 = 2 try: reg1, reg2 = self.cpld.gbox_get_intr() for i in range(gbox_max_1): gbox_intr[gbox_name + "_" + str(i)] = reg1 >> i & 0b1 for i in range(gbox_max_2): gbox_intr[gbox_name + "_" + str(gbox_max_1 + i)] = reg2 >> i & 0b1 return gbox_intr except Exception as e: self.logger.error("get_gbox_intr failed, error: {}".format(e)) def get_retimer_intr(self): retimer_name = "retimer" retimer_intr = {} retimer_max = 5 try: reg = self.cpld.retimer_get_intr() for i in range(retimer_max): retimer_intr[retimer_name + "_" + str(i)] = reg >> i & 0b1 return retimer_intr except Exception as e: self.logger.error("get_retimer_intr failed, error: {}".format(e))