diff --git a/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/chassis.py b/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/chassis.py index d984b3578b..a3ccc9439a 100644 --- a/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/chassis.py +++ b/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/chassis.py @@ -16,9 +16,11 @@ import json try: from sonic_platform_base.chassis_base import ChassisBase + from sonic_platform.component import Component from sonic_platform.eeprom import Tlv from sonic_platform.fan import Fan from sonic_platform.psu import Psu + from sonic_platform.thermal import Thermal from helper import APIHelper except ImportError as e: raise ImportError(str(e) + "- required module not found") @@ -26,7 +28,7 @@ except ImportError as e: NUM_FAN_TRAY = 7 NUM_FAN = 2 NUM_PSU = 2 -NUM_THERMAL = 5 +NUM_THERMAL = 10 NUM_SFP = 32 NUM_COMPONENT = 5 @@ -50,6 +52,12 @@ class Chassis(ChassisBase): for index in range(0, NUM_PSU): psu = Psu(index) self._psu_list.append(psu) + for index in range(0, NUM_COMPONENT): + component = Component(index) + self._component_list.append(component) + for index in range(0, NUM_THERMAL): + thermal = Thermal(index) + self._thermal_list.append(thermal) def get_base_mac(self): """ @@ -92,7 +100,8 @@ class Chassis(ChassisBase): status, raw_cause = self._api_helper.ipmi_raw( IPMI_OEM_NETFN, IPMI_GET_REBOOT_CAUSE) - hx_cause = raw_cause.split()[0] if status else 00 + hx_cause = raw_cause.split()[0] if status and len( + raw_cause.split()) > 0 else 00 reboot_cause = { "00": self.REBOOT_CAUSE_HARDWARE_OTHER, "11": self.REBOOT_CAUSE_POWER_LOSS, diff --git a/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/component.py b/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/component.py new file mode 100644 index 0000000000..737908b5b5 --- /dev/null +++ b/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/component.py @@ -0,0 +1,121 @@ +#!/usr/bin/env python + +############################################################################# +# Celestica +# +# Component contains an implementation of SONiC Platform Base API and +# provides the components firmware management function +# +############################################################################# + +import json +import os.path + +try: + from sonic_platform_base.component_base import ComponentBase + from helper import APIHelper +except ImportError as e: + raise ImportError(str(e) + "- required module not found") + +COMPONENT_LIST = [ + ("BIOS", "Basic Input/Output System"), + ("BMC", "Baseboard Management Controller"), + ("SWITCH_CPLD", "Switch board CPLD"), + ("BASE_CPLD", "Base board CPLD"), + ("FPGA", "Field-programmable gate array") +] +SW_CPLD_VER_PATH = "/sys/module/switch_cpld/version" +BASE_CPLD_VER_PATH = "/sys/module/baseboard_lpc/version" +CPLD_UPGRADE_OPT = 4 +BIOS_VER_PATH = "/sys/class/dmi/id/bios_version" +BIOS__UPGRADE_OPT = 2 +BMC_VER_CMD = "ipmitool mc info | grep 'Firmware Revision'" +BMC_UPGRADE_OPT = 1 +CFUFLASH_FW_UPGRADE_CMD = "CFUFLASH -cd -d {} -mse 3 {}" +MEM_PCI_RESOURCE = "/sys/bus/pci/devices/0000:09:00.0/resource0" +FPGA_VER_MEM_OFFSET = 0 + + +class Component(ComponentBase): + """Platform-specific Component class""" + + DEVICE_TYPE = "component" + + def __init__(self, component_index): + ComponentBase.__init__(self) + self.index = component_index + self.name = self.get_name() + self._api_helper = APIHelper() + + def __get_bmc_ver(self): + bmc_ver = "Unknown" + status, raw_bmc_data = self._api_helper.run_command(BMC_VER_CMD) + if status: + bmc_ver_data = raw_bmc_data.split(":") + bmc_ver = bmc_ver_data[-1].strip() if len( + bmc_ver_data) > 1 else bmc_ver + return bmc_ver + + def __get_fpga_ver(self): + fpga_ver = "Unknown" + status, reg_val = self._api_helper.pci_get_value( + MEM_PCI_RESOURCE, FPGA_VER_MEM_OFFSET) + if status: + major = reg_val[0] >> 16 + minor = int(bin(reg_val[0])[16:32], 2) + fpga_ver = '{}.{}'.format(major, minor) + return fpga_ver + + def get_name(self): + """ + Retrieves the name of the component + Returns: + A string containing the name of the component + """ + return COMPONENT_LIST[self.index][0] + + def get_description(self): + """ + Retrieves the description of the component + Returns: + A string containing the description of the component + """ + return COMPONENT_LIST[self.index][1] + + def get_firmware_version(self): + """ + Retrieves the firmware version of module + Returns: + string: The firmware versions of the module + """ + fw_version = { + "BIOS": self._api_helper.read_txt_file(BIOS_VER_PATH), + "BMC": self.__get_bmc_ver(), + "FPGA": self.__get_fpga_ver(), + "SWITCH_CPLD": self._api_helper.read_txt_file(SW_CPLD_VER_PATH), + "BASE_CPLD": self._api_helper.read_txt_file(BASE_CPLD_VER_PATH), + }.get(self.name, "Unknown") + + return fw_version + + def install_firmware(self, image_path): + """ + Install firmware to module + Args: + image_path: A string, path to firmware image + Returns: + A boolean, True if install successfully, False if not + """ + install_command = { + "BMC": CFUFLASH_FW_UPGRADE_CMD.format(BMC_UPGRADE_OPT, image_path), + "BIOS": CFUFLASH_FW_UPGRADE_CMD.format(BIOS__UPGRADE_OPT, image_path), + "SWITCH_CPLD": CFUFLASH_FW_UPGRADE_CMD.format(CPLD_UPGRADE_OPT, image_path), + "BASE_CPLD": CFUFLASH_FW_UPGRADE_CMD.format(CPLD_UPGRADE_OPT, image_path) + }.get(self.name, None) + + if not os.path.isfile(image_path) or install_command is None: + return False + + # print(install_command) + status = self._api_helper.run_interactive_command(install_command) + return status diff --git a/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/helper.py b/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/helper.py index 1132b5b067..82f0eea6b4 100644 --- a/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/helper.py +++ b/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/helper.py @@ -1,8 +1,9 @@ #!/usr/bin/env python import os +import struct import subprocess - +from mmap import * HOST_CHK_CMD = "docker > /dev/null 2>&1" EMPTY_STRING = "" @@ -16,6 +17,39 @@ class APIHelper(): def is_host(self): return os.system(HOST_CHK_CMD) == 0 + def pci_get_value(self, resource, offset): + status = True + result = "" + try: + fd = os.open(resource, os.O_RDWR) + mm = mmap(fd, 0) + mm.seek(int(offset)) + read_data_stream = mm.read(4) + result = struct.unpack('I', read_data_stream) + except: + status = False + return status, result + + def run_command(self, cmd): + status = True + result = "" + try: + p = subprocess.Popen( + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + raw_data, err = p.communicate() + if err == '': + result = raw_data.strip() + except: + status = False + return status, result + + def run_interactive_command(self, cmd): + try: + os.system(cmd) + except: + return False + return True + def read_txt_file(self, file_path): try: with open(file_path, 'r') as fd: @@ -35,6 +69,8 @@ class APIHelper(): raw_data, err = p.communicate() if err == '': result = raw_data.strip() + else: + status = False except: status = False return status, result @@ -51,6 +87,24 @@ class APIHelper(): raw_data, err = p.communicate() if err == '': result = raw_data.strip() + else: + status = False + except: + status = False + return status, result + + def ipmi_set_ss_thres(self, id, threshold_key, value): + status = True + result = "" + try: + cmd = "ipmitool sensor thresh '{}' {} {}".format(str(id), str(threshold_key), str(value)) + p = subprocess.Popen( + cmd, shell=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE) + raw_data, err = p.communicate() + if err == '': + result = raw_data.strip() + else: + status = False except: status = False return status, result diff --git a/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/thermal.py b/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/thermal.py new file mode 100644 index 0000000000..e3138e6bb8 --- /dev/null +++ b/device/celestica/x86_64-cel_silverstone-r0/sonic_platform/thermal.py @@ -0,0 +1,153 @@ +#!/usr/bin/env python + +############################################################################# +# Celestica +# +# Thermal contains an implementation of SONiC Platform Base API and +# provides the thermal device status which are available in the platform +# +############################################################################# + +import os +import re +import os.path + +try: + from sonic_platform_base.thermal_base import ThermalBase + from helper import APIHelper +except ImportError as e: + raise ImportError(str(e) + "- required module not found") + +IPMI_SENSOR_NETFN = "0x04" +IPMI_SS_READ_CMD = "0x2D {}" +IPMI_SS_THRESHOLD_CMD = "0x27 {}" +DEFUALT_LOWER_TRESHOLD = 0.0 +HIGH_TRESHOLD_SET_KEY = "unc" + + +class Thermal(ThermalBase): + """Platform-specific Thermal class""" + + def __init__(self, thermal_index): + ThermalBase.__init__(self) + self._api_helper = APIHelper() + self.index = thermal_index + self.THERMAL_LIST = [ + ('TEMP_FAN_U52', 'Fan Tray Middle Temperature Sensor', '0x00'), + ('TEMP_FAN_U17', 'Fan Tray Right Temperature Sensor', '0x01'), + ('TEMP_SW_U52', 'Switchboard Left Inlet Temperature Sensor', '0x02'), + ('TEMP_SW_U16', 'Switchboard Right Inlet Temperature Sensor', '0x03'), + ('TEMP_BB_U3', 'Baseboard Temperature Sensor', '0x04'), + ('TEMP_CPU', 'CPU Internal Temperature Sensor', '0x05'), + ('TEMP_SW_Internal', 'ASIC Internal Temperature Sensor', '0x61'), + ('SW_U04_Temp', 'IR3595 Chip Left Temperature Sensor', '0x4F'), + ('SW_U14_Temp', 'IR3595 Chip Right Temperature Sensor', '0x56'), + ('SW_U4403_Temp', 'IR3584 Chip Temperature Sensor', '0x5D'), + ] + self.sensor_id = self.THERMAL_LIST[self.index][0] + self.sensor_des = self.THERMAL_LIST[self.index][1] + self.sensor_reading_addr = self.THERMAL_LIST[self.index][2] + def __set_threshold(self, key, value): + print(key, value) + + def get_temperature(self): + """ + Retrieves current temperature reading from thermal + Returns: + A float number of current temperature in Celsius up to nearest thousandth + of one degree Celsius, e.g. 30.125 + """ + temperature = 0.0 + status, raw_ss_read = self._api_helper.ipmi_raw( + IPMI_SENSOR_NETFN, IPMI_SS_READ_CMD.format(self.sensor_reading_addr)) + if status and len(raw_ss_read.split()) > 0: + ss_read = raw_ss_read.split()[0] + temperature = float(int(ss_read, 16)) + return temperature + + def get_high_threshold(self): + """ + Retrieves the high threshold temperature of thermal + Returns: + A float number, the high threshold temperature of thermal in Celsius + up to nearest thousandth of one degree Celsius, e.g. 30.125 + """ + high_threshold = 0.0 + status, raw_up_thres_read = self._api_helper.ipmi_raw( + IPMI_SENSOR_NETFN, IPMI_SS_THRESHOLD_CMD.format(self.sensor_reading_addr)) + if status and len(raw_up_thres_read.split()) > 6: + ss_read = raw_up_thres_read.split()[4] + high_threshold = float(int(ss_read, 16)) + return high_threshold + + def get_low_threshold(self): + """ + Retrieves the low threshold temperature of thermal + Returns: + A float number, the low threshold temperature of thermal in Celsius + up to nearest thousandth of one degree Celsius, e.g. 30.125 + """ + return DEFUALT_LOWER_TRESHOLD + + def set_high_threshold(self, temperature): + """ + Sets the high threshold temperature of thermal + Args : + temperature: A float number up to nearest thousandth of one degree Celsius, + e.g. 30.125 + Returns: + A boolean, True if threshold is set successfully, False if not + """ + status, ret_txt = self._api_helper.ipmi_set_ss_thres(self.sensor_id, HIGH_TRESHOLD_SET_KEY, temperature) + return status + + def set_low_threshold(self, temperature): + """ + Sets the low threshold temperature of thermal + Args : + temperature: A float number up to nearest thousandth of one degree Celsius, + e.g. 30.125 + Returns: + A boolean, True if threshold is set successfully, False if not + """ + return False + + def get_name(self): + """ + Retrieves the name of the thermal device + Returns: + string: The name of the thermal device + """ + return self.THERMAL_LIST[self.index][0] + + def get_presence(self): + """ + Retrieves the presence of the device + Returns: + bool: True if device is present, False if not + """ + return True if self.get_temperature() > 0 else False + + def get_model(self): + """ + Retrieves the model number (or part number) of the device + Returns: + string: Model/part number of device + """ + return self.sensor_des + + def get_serial(self): + """ + Retrieves the serial number of the device + Returns: + string: Serial number of device + """ + return "Unknown" + + def get_status(self): + """ + Retrieves the operational status of the device + Returns: + A boolean value, True if device is operating properly, False if not + """ + return self.get_presence() \ No newline at end of file