nicsensor/nicsensor.sh
2026-03-12 15:54:06 +08:00

1781 lines
59 KiB
Bash
Executable File

#!/bin/sh
set -eo pipefail
SCRIPT_VERSION="1.10"
# ---------------------------------------------------------
# Project Feature Varible (Change if need)
# ---------------------------------------------------------
# ADC128 Division factor
votage_division_factor_0="1"
votage_division_factor_1="1"
votage_division_factor_2="1"
votage_division_factor_3="0.8"
votage_division_factor_4="0.6"
votage_division_factor_5="0.6"
votage_division_factor_6="0.2326"
votage_division_factor_7="1"
# ADC128 channel name
ADC128_Channel0_name="Channel0"
ADC128_Channel1_name="Channel1"
ADC128_Channel2_name="Channel2"
ADC128_Channel3_name="Channel3"
ADC128_Channel4_name="Channel4"
ADC128_Channel5_name="Channel5"
ADC128_Channel6_name="Channel6"
ADC128_Channel7_name="Channel7"
# INA3221 shunt resistor(unit: mohm)
shunt_resistor_0="2"
shunt_resistor_1="2"
shunt_resistor_2="5"
# INA3221 channel name
INA3221_Channel0_name="Channel0"
INA3221_Channel1_name="Channel1"
INA3221_Channel2_name="Channel2"
# EMC1413 channel name
EMC1413_Channel0_name="Channel0"
EMC1413_Channel1_name="Channel1"
EMC1413_Channel2_name="Channel2"
# TMP468 channel name
TMP468_Channel0_name="Local"
TMP468_Channel1_name="Remote1"
TMP468_Channel2_name="Remote2"
TMP468_Channel3_name="Remote3"
TMP468_Channel4_name="Remote4"
TMP468_Channel5_name="Remote5"
TMP468_Channel6_name="Remote6"
TMP468_Channel7_name="Remote7"
TMP468_Channel8_name="Remote8"
# TMP112 sensor name
TMP112_Sensor_name="Temperature"
# INA226 channel name
INA226_Channel0_name="Channel0"
# LM95241 channel name
LM95241_Local_name="Local"
LM95241_Remote1_name="Remote1"
LM95241_Remote2_name="Remote2"
# ---------------------------------------------------------
# Global Settings
# ---------------------------------------------------------
Support_Sensor_List="emc1413, adc128, ina3221, ina226, tmp468, tmp112, lm95241"
Support_Server_List="5280m7, 5468m7, 5688m7, donghu, yichun, qiandaohu"
Support_Property_List="debug|n_detect|server|fru_size|fru_offset|i2cbus"
# Which server will use this script
server_type="auto"
# DISABLE_DETECT=0 : do i2cdetect when i2c status changed
# =1 : don't do i2cdetect when i2c status changed
DISABLE_DETECT=1
# DEBUG MODE=0 : Disable debug mode
# =1 : Enable debug mode
DEBUG_MODE=0
i2c_bus=12
# fru basic offset
fru_offset="0x00 0x00"
fru_size=256
# ---------------------------------------------------------
# Global Varible (Don't Change)
# ---------------------------------------------------------
# Input Param
nic_slot=$1
sensor_type=$2
chip_slave=$3
option_data=$4
option_data2=$5
param_num=$#
# Param alias
Param1=$1
Param2=$2
Param3=$3
# REGISTER Table
REG_pca9641_controll="0x01"
REG_adc128_config="0x00"
REG_adc128_advance="0x0b"
REG_adc128_status="0x0c"
REG_adc128_ch0="0x20"
REG_adc128_ch1="0x21"
REG_adc128_ch2="0x22"
REG_adc128_ch3="0x23"
REG_adc128_ch4="0x24"
REG_adc128_ch5="0x25"
REG_adc128_ch6="0x26"
REG_adc128_ch7="0x27"
REG_emc1413_TD1_H="0x00"
REG_emc1413_TD1_L="0x29"
REG_emc1413_TD2_H="0x01"
REG_emc1413_TD2_L="0x10"
REG_emc1413_TD3_H="0x23"
REG_emc1413_TD3_L="0x24"
REG_ina3221_ch1="0x01"
REG_ina3221_ch2="0x03"
REG_ina3221_ch3="0x05"
REG_ina3221_bus1="0x02"
REG_ina3221_bus2="0x04"
REG_ina3221_bus3="0x06"
REG_ina226_ch0_current="0x01"
REG_ina226_ch0_voltage="0x02"
REG_tmp112="0x00"
REG_tmp468="0x80"
REG_lm95241_local_H="0x10"
REG_lm95241_remote1_H="0x11"
REG_lm95241_remote2_H="0x12"
REG_lm95241_local_L="0x20"
REG_lm95241_remote1_L="0x21"
REG_lm95241_remote2_L="0x22"
# Global Param
pca9641_slave=0x41
pca9548_channel=0x04
pca9548_slave=0x72
is_have_pca9641=1
INA3221_SHUNT_VOLT=0
INA3221_BUS_VOLT=1
INA3221_POWER=2
ina3221_ch0_volt="0"
ina3221_ch1_volt="0"
ina3221_ch2_volt="0"
ina3221_ch0_current="0"
ina3221_ch1_current="0"
ina3221_ch2_current="0"
fru_file_name=$option_data2
fru_write_size=0
fru_write_data=""
nic_type=""
slot_number=""
res_tmp468=""
INFO="Info"
WARNING="Warning"
ERROR="Error"
log="/tmp/nicsensor_debug.log"
pmbus_op="get"
hex_pmbus_cmd=0xff
hex_pmbus_size=0
CONFIG_DIR="/tmp/nicsensor_config/"
# ---------------------------------------------------------
# Script Function Defination
# ---------------------------------------------------------
search_plugin_help(){
PMBUS_PLUGIN="./pmbus_cmd_list.sh"
STRESS_PLUGIN="./stress_test.sh"
plugin_flag=0
if [ -e ${PMBUS_PLUGIN} ];then
$PMBUS_PLUGIN "help"
plugin_flag=1
fi
if [ -e ${STRESS_PLUGIN} ];then
$STRESS_PLUGIN "help"
plugin_flag=1
fi
if [ $plugin_flag -eq 0 ];then
echo " No Plugin found in file system"
fi
}
print_usage_guide(){
echo ""
echo "====================>>> User Gudie <<<======================="
echo " Tool Format : ./nicsensor.sh Param1 Param2 Param3 ..."
echo " See more help info by: ./nicsensor.sh help <index>"
echo " Index info:"
echo " all : Show all help"
echo " ex : Show plugin help"
echo " 1 : Read Sensor Value"
echo " 2 : Detect I2C for each slot"
echo " 3 : Send an i2c command to chip on Nic"
echo " 4 : FRU Read and Wirte"
echo " 5 : Properties Settings"
echo ""
echo " E.G. : ./nicsensor.sh help 1"
}
#@Param1 - help index
print_usage(){
echo ""
echo "=====================>>> nicsensor script usage <<<======================"
if [ "$1" = "1" ] || [ "$1" = "all" ];then
echo " Function 1 - Read Sensor Value"
echo " 1) Command Format : ./nicsensor.sh [slot] [sensor tpye] [slave]"
echo " 2) Option Detail"
echo " - [slot] : 0 1 2 3 4 5 ..."
echo " - [sensor type] : $Support_Sensor_List"
echo " - [slave] : chip slave address , please provide 7 bit address"
echo " 3) E.G. : ./nicsensor.sh 1 adc128 0x1f"
echo " ./nicsensor.sh pcie1 ina3221 0x42"
echo " ./nicsensor.sh ocp0 emc1413 0x4c"
echo ""
fi
if [ "$1" = "2" ] || [ "$1" = "all" ];then
echo " Function 2 - Detect I2C for each slot"
echo " 1) Command Format : ./nicsensor.sh detect [pcie/ocp]"
echo " 2) E.G. : ./nicsensor.sh detect pcie"
echo " ./nicsensor.sh detect ocp [some server don't support]"
echo " 3) Note : This function now only support on server:"
echo " $Support_Server_List"
echo ""
fi
if [ "$1" = "3" ] || [ "$1" = "all" ];then
echo " Function 3 - Send an i2c command to chip on Nic"
echo " 1) Command Format : ./nicsensor.sh [slot] chip [slave] [command]"
echo " 2) Option Detail"
echo " - [slot] : 0 1 2 3 4 5 ..."
echo " - [slave] : chip slave address , please provide 7 bit address"
echo " - [command] : The i2c command which will set to the chip"
echo " 3) E.G. : ./nicsensor.sh pcie1 chip 0x10 'i2ctransfer -y 12 w1@0x10 0x00 r2'"
echo ""
fi
if [ "$1" = "4" ] || [ "$1" = "all" ];then
echo " Function 4 - FRU Read and Wirte"
echo " 1) Command Format : ./nicsensor.sh [slot] fru [slave] [option] [frudata]"
echo " 2) Option Detail"
echo " - [slot] : 0 1 2 3 4 5 ..."
echo " - [slave] : chip slave address , please provide 7 bit address"
echo " - [option] : read/write, default to read"
echo " - [frudata] : fru bin file which will write to eeprom, only for write option"
echo " 3) E.G. : ./nicsensor.sh pcie0 fru 0x57"
echo " : ./nicsensor.sh pcie0 fru 0x57 read"
echo " : ./nicsensor.sh pcie0 fru 0x57 write /tmp/fru.bin"
echo ""
fi
if [ "$1" = "5" ] || [ "$1" = "all" ];then
echo " Function 5 - Properties Settings"
echo " 1) Command Format : ./nicsensor.sh [get/set] [property] [value]"
echo " 2) Option Detail"
echo " - [property] : $Support_Property_List"
echo " - [value] : property's value"
echo " 3) E.G. : ./nicsensor.sh set server 5280m7"
echo " : ./nicsensor.sh set debug 1"
echo " : ./nicsensor.sh set n_detect 1"
echo ""
fi
if [ "$1" = "ex" ] || [ "$1" = "all" ];then
search_plugin_help
fi
}
systemtool_check(){
systemtool_exist_flag="${CONFIG_DIR}systemtool_exist_flag"
# Check system tools exist flag file, if exists, skip check
if [ -e $systemtool_exist_flag ];then
fmt_print "log" $INFO "System tools found in file system"
return
fi
# Check system tools exist
systemtool_list="i2ctransfer i2cdetect i2cget bc"
for tool in $systemtool_list;do
if ! command -v $tool &> /dev/null;then
fmt_print "console" $ERROR "System tool $tool not found in file system"
fmt_print "console" $INFO "Please Check BMC Version, Only Support on AMI BMC!"
exit 1
fi
done
# Create system tools exist flag file
touch $systemtool_exist_flag
}
# Convert a hexadecimal number to a decimal number
# @Param1 hex data
hex_to_dec(){
upper_hex_value=$(echo "$1" | awk '{
for(i=1; i<=length($0); i++){
if(tolower(substr($0,i,1)) ~ /^[a-f]$/)
printf toupper(substr($0,i,1));
else
printf substr($0,i,1);
}
print ""
}')
dec_val=$(echo "ibase=16; $upper_hex_value" | bc)
echo $dec_val
}
# print format message to console|debuglog
# @Param1 direction [console|log], default record to log if invalid
# @Param2 message level [Info Warning Error]
# @Param3 message content
fmt_print(){
if [ "$1" = "console" ];then
echo ">>> [$2] $3"
else
echo "[$2] $3" >> $log
fi
}
# switch pca9548 channels then do i2cdetect one times
# @Param1 i2c bus number
# @Param2 pca9548 slave address
# @Param3 pca9548 channel hex value
# @Param4 pca9548 channel
# @Param5 pcie slot number
do_i2c_detect(){
if [ $nic_type = "ocp" ];then
fmt_print "console" $INFO "OCP slot $5 : bus$1 9548channel$4"
else
fmt_print "console" $INFO "PCIe slot $5 : bus$1 9548channel$4"
fi
i2ctransfer -y $1 w1@$2 $3
i2cdetect -y $1
}
# Reset pca9548 channel to 0x00
# @Param1 i2c bus number
# @Param2 pca9548 slave address
reset_pca9548(){
# In Debug mode, skip this function
if [ $DEBUG_MODE -eq 1 ];then
return
fi
i2ctransfer -y $1 w1@$2 0x00
}
# Get nic type and slot number
parse_nic_slot(){
fullname=0
case $nic_slot in
ocp*)
nic_type="ocp"
;;
pcie*)
nic_type="pcie"
fullname=1
;;
*)
nic_type="pcie"
fullname=0;
;;
esac
if [ $nic_type = "ocp" ];then
slot_number=$(echo $nic_slot | cut -b 4-)
else
if [ $fullname -eq 1 ];then
slot_number=$(echo $nic_slot | cut -b 5-)
else
slot_number=$nic_slot
fi
fi
fmt_print "log" $INFO "Nic Type : $nic_type"
fmt_print "log" $INFO "Slot Num : $slot_number"
}
get_set_properties(){
if [ "$Param2" = "server" ];then
target_file="${CONFIG_DIR}ns_servertype"
default_data=$server_type
elif [ "$Param2" = "debug" ];then
target_file="${CONFIG_DIR}ns_debugmode"
default_data=$DEBUG_MODE
elif [ "$Param2" = "n_detect" ];then
target_file="${CONFIG_DIR}ns_disabledetect"
default_data=$DISABLE_DETECT
elif [ "$Param2" = "fru_size" ];then
target_file="${CONFIG_DIR}ns_frusize"
default_data=$fru_size
elif [ "$Param2" = "fru_offset" ];then
target_file="${CONFIG_DIR}ns_fruoffset"
default_data=$fru_offset
elif [ "$Param2" = "i2cbus" ];then
target_file="${CONFIG_DIR}ns_i2cbus"
default_data=$i2c_bus
else
fmt_print "console" $ERROR "Invalid Property : $Param2"
fmt_print "console" $INFO "Support Property List : $Support_Property_List"
exit 1
fi
if [ "$Param1" = "get" ];then
if [ -e "$target_file" ];then
tmpdata=`cat $target_file`
fmt_print "console" $INFO "$Param2 : $tmpdata"
else
fmt_print "console" $INFO "$Param2 : $default_data"
fi
else
echo $Param3 > $target_file
fmt_print "console" $INFO "Set $Param3 -> $Param2 successfully"
fi
exit 0
}
load_properties_from_cache(){
if [ -e "${CONFIG_DIR}ns_servertype" ];then
server_type=`cat ${CONFIG_DIR}ns_servertype`
fi
if [ -e "${CONFIG_DIR}ns_debugmode" ];then
DEBUG_MODE=`cat ${CONFIG_DIR}ns_debugmode`
fi
if [ -e "${CONFIG_DIR}ns_disabledetect" ];then
DISABLE_DETECT=`cat ${CONFIG_DIR}ns_disabledetect`
fi
if [ -e "${CONFIG_DIR}ns_frusize" ];then
fru_size=`cat ${CONFIG_DIR}ns_frusize`
fi
if [ -e "${CONFIG_DIR}ns_fruoffset" ];then
fru_offset=`cat ${CONFIG_DIR}ns_fruoffset`
fi
if [ -e "${CONFIG_DIR}ns_i2cbus" ];then
i2c_bus=`cat ${CONFIG_DIR}ns_i2cbus`
fi
}
# According to boardid, parse server_type
try_get_server_type(){
if [ -e "${CONFIG_DIR}ns_servertype" ];then
server_type=`cat ${CONFIG_DIR}ns_servertype`
else
boardid=`cat /proc/cmdline | awk -F 'boardid=' '{split($2, a," "); print a[1]}'`
if [ "$boardid" = "" ];then
server_type="Unknown"
fmt_print "console" $WARNING "Can't find boardid, auto work failed"
fmt_print "console" $INFO "Please modify server_type in script manually!"
return
fi
if [ $boardid -eq 130 ];then
server_type="5280m7"
elif [ $boardid -eq 150 ];then
server_type="5468m7"
elif [ $boardid -eq 152 ];then
server_type="5688m7"
else
fmt_print "console" $ERROR "Invalid boardid value: $boardid"
fmt_print "console" $ERROR "Can't specify server type by auto method"
fmt_print "console" $INFO "Please modify server_type in script manually!"
exit 1
fi
echo $server_type > ${CONFIG_DIR}ns_servertype
fi
}
init_debuglog(){
if [ -e $log ];then
rm $log
fi
# create config dir if not exists
if [ ! -d $CONFIG_DIR ];then
mkdir -p $CONFIG_DIR
fi
res_date=`date +%Y-%m-%dT%H:%M:%S`
fmt_print "log" $INFO "Start Time : $res_date"
fmt_print "log" $INFO "Script Version : $SCRIPT_VERSION"
fmt_print "log" $INFO "Debug Mode : $DEBUG_MODE"
fmt_print "log" $INFO "Disable Detect : $DISABLE_DETECT"
if [ "$Param1" = "detect" ];then
fmt_print "log" $INFO "Operation Type : i2c detect"
elif [ "$Param1" = "version" ];then
fmt_print "log" $INFO "Operation Type : get version"
elif [ "$Param1" = "get" ];then
fmt_print "log" $INFO "Operation Type : get property value"
elif [ "$Param1" = "set" ];then
fmt_print "log" $INFO "Operation Type : set property value"
elif [ "$Param1" = "help" ];then
fmt_print "log" $INFO "Operation Type : help info"
elif [ $param_num -lt 2 ];then
fmt_print "log" $INFO "Operation Type : help info"
else
fmt_print "log" $INFO "Operation Type : read sensor"
fi
}
# Before start test, record configurtion to log
record_config_info(){
fmt_print "log" $INFO "PCIE slot : $nic_slot"
fmt_print "log" $INFO "I2C Bus: $i2c_bus"
if [ $is_have_pca9641 -eq 1 ];then
fmt_print "log" $INFO "PCA9641 slave: $pca9641_slave"
fi
fmt_print "log" $INFO "PCA9548 slave: $pca9548_slave"
fmt_print "log" $INFO "PCA9548 channel: $pca9548_channel"
# Record i2c device info to log
if [ ${DISABLE_DETECT} -eq 0 ];then
fmt_print "log" $INFO "At the beginning, I2C bus status:"
i2cdetect -y $i2c_bus >> $log
fi
}
# Desc : Gain control of the PCA9641 chip
get_pca9641_control(){
# Request 9641 lock
res_lock=`i2ctransfer -y $i2c_bus w2@$pca9641_slave $REG_pca9641_controll 0x81 r1`
fmt_print "log" $INFO "After request 9641 lock, The REG value is $res_lock"
# Build 9641 Connection
res_build=`i2ctransfer -y $i2c_bus w2@$pca9641_slave $REG_pca9641_controll 0x85 r1`
fmt_print "log" $INFO "After Build 9641 connection, The REG value is $res_build"
# After get 9641 controll, Record i2c device info to log
if [ ${DISABLE_DETECT} -eq 0 ];then
i2cdetect -y $i2c_bus >> $log
fi
if [ "$res_build" != "0x87" ];then
fmt_print "console" $ERROR "Cannot establish connection with pca9641 !!!"
exit 1
fi
}
# Desc : Switch the channel of the 9548 chip.
switch_pca9548_channel(){
# set 9548 channel
res_setchannel=`i2ctransfer -y $i2c_bus w1@$pca9548_slave $pca9548_channel`
fmt_print "log" $INFO "After switch channel"
# After set 9548 channel , record i2c device info
if [ ${DISABLE_DETECT} -eq 0 ];then
i2cdetect -y $i2c_bus >> $log
fi
}
extension_preprocess(){
if [ "$Param2" = "pmbus" ];then
if [ $param_num -le 3 ];then
fmt_print "console" $ERROR "Command Format illegal"
echo "Command Format : ./nicsensor.sh [slot] pmbus [slave] [pmbus_command]"
exit 1
fi
# Pmbus Command need pre-process
PMBUS_PLUGIN="./pmbus_cmd_list.sh"
if [ -e ${PMBUS_PLUGIN} ];then
str_pmbus_cmd=$option_data
hex_pmbus_cmd=`$PMBUS_PLUGIN "code" $str_pmbus_cmd`
if [ "$hex_pmbus_cmd" = "0xff" ];then
fmt_print "console" $ERROR "Unsupport PMBUS Command : $str_pmbus_cmd"
exit 1
fi
hex_pmbus_size=`$PMBUS_PLUGIN "size" $str_pmbus_cmd`
pmbus_op=`$PMBUS_PLUGIN "operation" $str_pmbus_cmd`
else
fmt_print "console" $ERROR "Can't find pmbus plugin file, please copy pmbus_cmd_list.sh to here!"
exit 1
fi
fi
if [ "$Param2" = "stress" ];then
STRESS_PLUGIN="./stress_test.sh"
if [ -e ${STRESS_PLUGIN} ];then
${STRESS_PLUGIN} $Param3 $option_data
exit 0
else
fmt_print "console" $ERROR "Can't find stress plugin file, please copy stress_test.sh to here!"
exit 1
fi
fi
}
# Desc : Pre-execute hook function
# Function 1 : Parameter Integrity Check
# Function 2 : Partial Data Preprocessing
# Function 3 : Runtime Environment Initialization
# Function 4 : Implementation of Parameter Setting Function
pre_exec_hook(){
init_debuglog
systemtool_check
if [ "$Param1" = "help" ] && [ $param_num -eq 2 ];then
print_usage $Param2
exit 0
elif [ "$Param1" = "help" ] && [ $param_num -gt 2 ];then
print_usage_guide
exit 0
fi
if [ "$Param1" = "get" ] || [ "$Param1" = "set" ];then
get_set_properties
fi
load_properties_from_cache
# check server_type if is auto
# if in debug mode, skip this check
if [ "$server_type" = "auto" ] && [ $DEBUG_MODE -eq 0 ];then
try_get_server_type
fi
extension_preprocess
# is param legel?
if [ "$Param2" = "chip" ];then
if [ $param_num -le 3 ];then
fmt_print "console" $ERROR "Command Format illegal"
echo "Command Format : ./nicsensor.sh [slot] chip [slave] [i2c_command]"
exit 1
fi
fi
# if the action is fru write, need pre-process fru data
if [ "$Param2" = "fru" ];then
if [ "$option_data" = "write" ];then
fmt_print "log" $INFO "Opreation FRU Write"
# is option_data(fru file name) valid?
if [ "$option_data2" = "" ];then
fmt_print "console" $ERROR "Please provide fru file name"
echo "Command Format : ./nicsensor.sh [slot] fru [slave] write [fru_file]"
exit 1
fi
parse_fru_write_data
fi
fi
}
# ---------------------------------------------------------
# Set i2c configuration
# ---------------------------------------------------------
set_configuration_5280m7(){
if [ $nic_type = "ocp" ];then
is_have_pca9641=0
pca9548_slave="0x70"
i2c_bus=3
if [ $slot_number -eq 0 ];then
pca9548_channel="0x01"
elif [ $slot_number -eq 1 ];then
pca9548_channel="0x02"
elif [ $slot_number -eq 2 ];then
pca9548_channel="0x04"
else
fmt_print "console" $WARNING "Unspecified card slot!"
fi
else
is_have_pca9641=1
if [ $slot_number -le 2 ];then
pca9641_slave="0x41"
pca9548_slave="0x72"
i2c_bus=12
else
pca9641_slave="0x42"
pca9548_slave="0x72"
i2c_bus=13
fi
if [ $slot_number -eq 0 ];then
pca9548_channel="0x02"
elif [ $slot_number -eq 1 ];then
pca9548_channel="0x04"
elif [ $slot_number -eq 2 ];then
pca9548_channel="0x08"
elif [ $slot_number -eq 3 ];then
pca9548_channel="0x02"
elif [ $slot_number -eq 4 ];then
pca9548_channel="0x04"
elif [ $slot_number -eq 5 ];then
pca9548_channel="0x08"
else
fmt_print "console" $WARNING "Unspecified card slot!"
fi
fi
}
set_configuration_5468m7(){
if [ $nic_type = "ocp" ];then
fmt_print "console" $ERROR "Unsupport OCP Nic on $server_type"
fmt_print "console" $INFO "Please check if script has update version"
exit 1
else
is_have_pca9641=1
if [ $slot_number -le 4 ];then
pca9641_slave="0x31"
pca9548_slave="0x70"
i2c_bus=13
else
pca9641_slave="0x42"
pca9548_slave="0x71"
i2c_bus=14
fi
if [ $slot_number -eq 0 ];then
pca9548_channel="0x01"
elif [ $slot_number -eq 1 ];then
pca9548_channel="0x02"
elif [ $slot_number -eq 2 ];then
pca9548_channel="0x04"
elif [ $slot_number -eq 3 ];then
pca9548_channel="0x08"
elif [ $slot_number -eq 4 ];then
pca9548_channel="0x10"
elif [ $slot_number -eq 5 ];then
pca9548_channel="0x01"
elif [ $slot_number -eq 6 ];then
pca9548_channel="0x02"
elif [ $slot_number -eq 7 ];then
pca9548_channel="0x04"
elif [ $slot_number -eq 8 ];then
pca9548_channel="0x08"
elif [ $slot_number -eq 9 ];then
pca9548_channel="0x10"
elif [ $slot_number -eq 10 ];then
pca9548_channel="0x20"
else
fmt_print "console" $WARNING "Unspecified card slot!"
fi
fi
}
set_configuration_donghu(){
if [ $nic_type = "ocp" ];then
fmt_print "console" $ERROR "Unsupport OCP Nic on $server_type"
fmt_print "console" $INFO "Please check if script has update version"
exit 1
else
is_have_pca9641=0
i2c_bus=3
pca9548_slave="0x70"
if [ $slot_number -eq 0 ];then
pca9548_channel="0x80"
elif [ $slot_number -eq 1 ];then
pca9548_channel="0x40"
elif [ $slot_number -eq 2 ];then
pca9548_channel="0x20"
elif [ $slot_number -eq 3 ];then
pca9548_channel="0x10"
elif [ $slot_number -eq 4 ];then
pca9548_channel="0x08"
elif [ $slot_number -eq 5 ];then
pca9548_channel="0x04"
elif [ $slot_number -eq 6 ];then
pca9548_channel="0x02"
elif [ $slot_number -eq 7 ];then
pca9548_channel="0x01"
else
fmt_print "console" $WARNING "Unspecified card slot!"
fi
fi
}
set_configuration_yichun(){
if [ $nic_type = "ocp" ];then
fmt_print "console" $ERROR "Unsupport OCP Nic on $server_type"
fmt_print "console" $INFO "Please check if script has update version"
exit 1
else
is_have_pca9641=0
pca9548_slave="0x74"
if [ $slot_number -eq 0 ];then
i2c_bus=12
pca9548_channel="0x02"
elif [ $slot_number -eq 1 ];then
i2c_bus=13
pca9548_channel="0x02"
elif [ $slot_number -eq 2 ];then
i2c_bus=14
pca9548_channel="0x02"
else
fmt_print "console" $WARNING "Unspecified card slot!"
fi
fi
}
set_configuration_qiandaohu(){
if [ $nic_type = "ocp" ];then
fmt_print "console" $ERROR "Unsupport OCP Nic on $server_type"
fmt_print "console" $INFO "Please check if script has update version"
exit 1
else
is_have_pca9641=0
if [ $slot_number -le 7 ];then
i2c_bus=3
pca9548_slave=0x70
else
i2c_bus=6
pca9548_slave=0x71
fi
if [ $slot_number -eq 0 ];then
pca9548_channel="0x08"
elif [ $slot_number -eq 1 ];then
pca9548_channel="0x04"
elif [ $slot_number -eq 2 ];then
pca9548_channel="0x01"
elif [ $slot_number -eq 3 ];then
pca9548_channel="0x02"
elif [ $slot_number -eq 4 ];then
pca9548_channel="0x80"
elif [ $slot_number -eq 5 ];then
pca9548_channel="0x40"
elif [ $slot_number -eq 6 ];then
pca9548_channel="0x10"
elif [ $slot_number -eq 7 ];then
pca9548_channel="0x20"
elif [ $slot_number -eq 8 ];then
pca9548_channel="0x08"
elif [ $slot_number -eq 9 ];then
pca9548_channel="0x04"
elif [ $slot_number -eq 10 ];then
pca9548_channel="0x01"
elif [ $slot_number -eq 11 ];then
pca9548_channel="0x02"
else
fmt_print "console" $WARNING "Unspecified card slot!"
fi
fi
}
set_configuration_5688m7(){
if [ $nic_type = "ocp" ];then
fmt_print "console" $ERROR "Unsupport OCP Nic on $server_type"
fmt_print "console" $INFO "Please check if script has update version"
exit 1
else
is_have_pca9641=1
pca9641_slave="0x31"
i2c_bus=12
if [ $slot_number -le 3 ];then
pca9548_slave=0x71
else
pca9548_slave=0x72
fi
if [ $slot_number -eq 0 ];then
pca9548_channel="0x01"
elif [ $slot_number -eq 1 ];then
pca9548_channel="0x02"
elif [ $slot_number -eq 2 ];then
pca9548_channel="0x04"
elif [ $slot_number -eq 3 ];then
pca9548_channel="0x08"
elif [ $slot_number -eq 4 ];then
pca9548_channel="0x01"
elif [ $slot_number -eq 5 ];then
pca9548_channel="0x02"
elif [ $slot_number -eq 6 ];then
pca9548_channel="0x04"
elif [ $slot_number -eq 7 ];then
pca9548_channel="0x08"
else
fmt_print "console" $WARNING "Unspecified card slot!"
fi
fi
}
# Base on the server type, set i2c conf
set_configuration(){
fmt_print "log" $INFO "Server Type : $server_type"
parse_nic_slot
case $server_type in
"5280m7")
# Support PCIe and OCP configuration
set_configuration_5280m7
;;
"5468m7")
set_configuration_5468m7
;;
"donghu")
set_configuration_donghu
;;
"yichun")
set_configuration_yichun
;;
"qiandaohu")
set_configuration_qiandaohu
;;
"5688m7")
set_configuration_5688m7
;;
*)
fmt_print "console" $ERROR "Error: Unsupport Server Type !!! - $server_type"
fmt_print "console" $INFO "Support list: $Support_Server_List"
exit 1
;;
esac
}
# ---------------------------------------------------------
# Chip EMC1413
# ---------------------------------------------------------
# process sensor data then print to console
# @Param1 emc1413 data high 8bit
# @Param2 emc1413 data low 8bit
# @Param3 customization channel name
convert_emc1413_data(){
if [ "$3" = "disable" ];then
return
fi
# remove data prefix '0x'
hex_value1=$(echo "$1" | awk '{sub(/^0x/,""); print}')
hex_value2=$(echo "$2" | awk '{sub(/^0x/,""); print}')
dec_value1=$(hex_to_dec $hex_value1)
dec_value2=$(hex_to_dec $hex_value2)
temp=$(echo "scale=4; $dec_value1 + ($dec_value2 / 32 * 0.125 )" | bc)
format_temp=$(echo "$temp" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
fmt_print "log" $INFO "$3 - Hex data : $hex_value1 $hex_value2"
echo "$3 : $format_temp C"
}
# Get the sensor data, then parse raw data
read_emc1413_channel_value(){
fmt_print "log" $INFO "Start EMC1413 channel data ..."
res_td1_h=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_emc1413_TD1_H r1`
res_td1_l=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_emc1413_TD1_L r1`
res_td2_h=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_emc1413_TD2_H r1`
res_td2_l=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_emc1413_TD2_L r1`
res_td3_h=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_emc1413_TD3_H r1`
res_td3_l=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_emc1413_TD3_L r1`
reset_pca9548 $i2c_bus $pca9548_slave
fmt_print "log" $INFO "channel 1 : $res_td1_h $res_td1_l"
fmt_print "log" $INFO "channel 2 : $res_td2_h $res_td2_l"
fmt_print "log" $INFO "channel 3 : $res_td3_h $res_td3_l"
echo ">>> The emc1413 value is:"
convert_emc1413_data $res_td1_h $res_td1_l "$EMC1413_Channel0_name"
convert_emc1413_data $res_td2_h $res_td2_l "$EMC1413_Channel1_name"
convert_emc1413_data $res_td3_h $res_td3_l "$EMC1413_Channel2_name"
}
process_emc1413(){
read_emc1413_channel_value
}
# ---------------------------------------------------------
# Chip ADC128
# ---------------------------------------------------------
# adc128 chip init if need
check_adc128_init(){
# Get Reg 0x00 status
res_adc128_status=`i2cget -y $i2c_bus $chip_slave $REG_adc128_config`
fmt_print "log" $INFO "REG adc128 STATUS : $res_adc128_status"
# if stauts is not 0x01 (Start Monitor) ,then do init
if [ "$res_adc128_status" != "0x01" ];then
fmt_print "log" $INFO "Start Init ADC128 Chip"
# Init ADC128 work as mode 1 (0x02)
res_adc128_advance=`i2ctransfer -y $i2c_bus w2@$chip_slave $REG_adc128_advance 0x02`
# Set ADC128 on start (0x01)
res_adc128_setstart=`i2ctransfer -y $i2c_bus w2@$chip_slave $REG_adc128_config 0x01 r1`
fmt_print "log" $INFO "After Set status, the REG 0x00 value is $res_adc128_setstart"
fi
}
# process sensor data then print to console
# @Param 1 adc128 data high 8bit
# @Param 2 adc128 data low 8bit
# @Param 3 customization channel name
# @Param 4 division factor
convert_adc128_data(){
if [ "$3" = "disable" ];then
return
fi
# remove data prefix '0x'
hex_value1=$(echo "$1" | awk '{sub(/^0x/,""); print}')
hex_value2=$(echo "$2" | awk '{sub(/^0x/,""); print}')
merge_value="${hex_value1}${hex_value2}"
dec_val=$(hex_to_dec $merge_value)
volt=$(echo "scale=4; $dec_val / 16 / 4096 * 2.65 / $4" | bc)
format_volt=$(echo "$volt" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
fmt_print "log" $INFO "$3 - Hex data: 0x$merge_value"
echo "$3 : $format_volt V"
}
# Get the sensor data, then parse raw data
read_adc128_channel_value(){
fmt_print "log" $INFO "Start Read ADC128 channel data ..."
res_ch0=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_adc128_ch0 r2`
res_ch1=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_adc128_ch1 r2`
res_ch2=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_adc128_ch2 r2`
res_ch3=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_adc128_ch3 r2`
res_ch4=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_adc128_ch4 r2`
res_ch5=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_adc128_ch5 r2`
res_ch6=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_adc128_ch6 r2`
res_ch7=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_adc128_ch7 r2`
reset_pca9548 $i2c_bus $pca9548_slave
fmt_print "log" $INFO "channel0 : $res_ch0"
fmt_print "log" $INFO "channel1 : $res_ch1"
fmt_print "log" $INFO "channel2 : $res_ch2"
fmt_print "log" $INFO "channel3 : $res_ch3"
fmt_print "log" $INFO "channel4 : $res_ch4"
fmt_print "log" $INFO "channel5 : $res_ch5"
fmt_print "log" $INFO "channel6 : $res_ch6"
fmt_print "log" $INFO "channel7 : $res_ch7"
echo ">>> The ADC128 value is :"
convert_adc128_data $res_ch0 "$ADC128_Channel0_name" $votage_division_factor_0
convert_adc128_data $res_ch1 "$ADC128_Channel1_name" $votage_division_factor_1
convert_adc128_data $res_ch2 "$ADC128_Channel2_name" $votage_division_factor_2
convert_adc128_data $res_ch3 "$ADC128_Channel3_name" $votage_division_factor_3
convert_adc128_data $res_ch4 "$ADC128_Channel4_name" $votage_division_factor_4
convert_adc128_data $res_ch5 "$ADC128_Channel5_name" $votage_division_factor_5
convert_adc128_data $res_ch6 "$ADC128_Channel6_name" $votage_division_factor_6
convert_adc128_data $res_ch7 "$ADC128_Channel7_name" $votage_division_factor_7
}
process_adc128(){
check_adc128_init
read_adc128_channel_value
}
# ---------------------------------------------------------
# Chip INA3221
# ---------------------------------------------------------
# process sensor data then print to console
# @Param1 ina3221 data high 8bit
# @Param2 ina3221 data high 8bit
# @Param3 channel number
# @Param4 mode select 0 - shunt volt
# 1 - bus volt
# @Param5 shunt resistor(only used in mode shunt volt)
# @Param6 customization channel name
convert_ina3221_data(){
if [ "$6" = "disable" ];then
return
fi
hex_value1=$(echo "$1" | awk '{sub(/^0x/,""); print}')
hex_value2=$(echo "$2" | awk '{sub(/^0x/,""); print}')
merge_value="${hex_value1}${hex_value2}"
dec_val=$(hex_to_dec $merge_value)
# bc calculator recognized upper case only, change data to upper case
upper_hex_value=$(echo "$merge_value" | awk '{
for(i=1; i<=length($0); i++){
if(tolower(substr($0,i,1)) ~ /^[a-f]$/)
printf toupper(substr($0,i,1));
else
printf substr($0,i,1);
}
print ""
}')
if [ $4 -eq $INA3221_BUS_VOLT ];then
volt=$(echo "scale=4; $dec_val / 8 * 40 / 10000 * 2" | bc)
format_volt=$(echo "$volt" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
fmt_print "log" $INFO "Channel $3 - bus volt - hex value: $upper_hex_value"
echo "$6 : $format_volt V"
if [ $3 -eq 0 ];then
ina3221_ch0_volt=$format_volt
elif [ $3 -eq 1 ];then
ina3221_ch1_volt=$format_volt
else
ina3221_ch2_volt=$format_volt
fi
elif [ $4 -eq $INA3221_SHUNT_VOLT ];then
current_mv=$(echo "scale=4; $dec_val / 8 * 40 / 1000" | bc)
current=$(echo "scale=4; $current_mv / $5" | bc)
format_current=$(echo "$current" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
format_current_mv=$(echo "$current_mv" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
fmt_print "log" $INFO "Channel $3 - shunt volt - hex value: $upper_hex_value"
echo "$6 : $format_current A, shunt volt: $format_current_mv mV, shunt resistor: $5 mOhm"
if [ $3 -eq 0 ];then
ina3221_ch0_current=$format_current
elif [ $3 -eq 1 ];then
ina3221_ch1_current=$format_current
else
ina3221_ch2_current=$format_current
fi
elif [ $4 -eq $INA3221_POWER ];then
power_ch0=$(echo "scale=4; $ina3221_ch0_volt * $ina3221_ch0_current" | bc)
power_ch1=$(echo "scale=4; $ina3221_ch1_volt * $ina3221_ch1_current" | bc)
power_ch2=$(echo "scale=4; $ina3221_ch2_volt * $ina3221_ch2_current" | bc)
format_power_ch0=$(echo "$power_ch0" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
format_power_ch1=$(echo "$power_ch1" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
format_power_ch2=$(echo "$power_ch2" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
total_power=$(echo "scale=4; $power_ch0 + $power_ch1 + $power_ch2" | bc)
echo "$INA3221_Channel0_name : $format_power_ch0 W"
echo "$INA3221_Channel1_name : $format_power_ch1 W"
echo "$INA3221_Channel2_name : $format_power_ch2 W"
echo "total power: $total_power W"
fi
}
# Get the sensor data, then parse raw data
read_ina3221_channel_value(){
fmt_print "log" $INFO "Start Read INA3221 channel data ..."
res_ch0=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_ina3221_ch1 r2`
res_ch1=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_ina3221_ch2 r2`
res_ch2=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_ina3221_ch3 r2`
res_bus0=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_ina3221_bus1 r2`
res_bus1=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_ina3221_bus2 r2`
res_bus2=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_ina3221_bus3 r2`
reset_pca9548 $i2c_bus $pca9548_slave
# record i2c raw data to log
fmt_print "log" $INFO "channel 0 shunt volt: $res_ch0"
fmt_print "log" $INFO "channel 1 shunt volt: $res_ch1"
fmt_print "log" $INFO "channel 2 shunt volt: $res_ch2"
fmt_print "log" $INFO "Channel 0 bus volt : $res_bus0"
fmt_print "log" $INFO "Channel 1 bus volt : $res_bus1"
fmt_print "log" $INFO "Channel 2 bus volt : $res_bus2"
# start parse raw data
echo ">>> The INA3221 shunt value is :"
convert_ina3221_data $res_ch0 0 $INA3221_SHUNT_VOLT $shunt_resistor_0 "$INA3221_Channel0_name"
convert_ina3221_data $res_ch1 1 $INA3221_SHUNT_VOLT $shunt_resistor_1 "$INA3221_Channel1_name"
convert_ina3221_data $res_ch2 2 $INA3221_SHUNT_VOLT $shunt_resistor_2 "$INA3221_Channel2_name"
echo ">>> The INA3221 bus value is :"
convert_ina3221_data $res_bus0 0 $INA3221_BUS_VOLT null "$INA3221_Channel0_name"
convert_ina3221_data $res_bus1 1 $INA3221_BUS_VOLT null "$INA3221_Channel1_name"
convert_ina3221_data $res_bus2 2 $INA3221_BUS_VOLT null "$INA3221_Channel2_name"
echo ">>> The INA3221 bus power is :"
convert_ina3221_data 0 0 0 $INA3221_POWER
}
process_ina3221(){
read_ina3221_channel_value
}
# ---------------------------------------------------------
# Chip TMP468
# ---------------------------------------------------------
# @Param1 tmp468 data high 8bit
# @Param2 tmp468 data high 8bit
# @Param3 customization channel name
convert_tmp468_data(){
if [ "$3" = "disable" ];then
return
fi
hex_value1=$(echo "$1" | awk '{sub(/^0x/,""); print}')
hex_value2=$(echo "$2" | awk '{sub(/^0x/,""); print}')
merge_value="${hex_value1}${hex_value2}"
dec_val=$(hex_to_dec $merge_value)
temp=$(echo "scale=4; $dec_val / 8 * 0.0625" | bc)
echo "$3 : $temp "
}
read_tmp468_value(){
fmt_print "log" $INFO "Start Read tmp468 channel data ..."
res_tmp468=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_tmp468 r18`
reset_pca9548 $i2c_bus $pca9548_slave
fmt_print "log" $INFO "Tmp468 raw data: $res_tmp468"
tmp468_res1_h=`echo $res_tmp468 | awk '{print $1}'`
tmp468_res1_l=`echo $res_tmp468 | awk '{print $2}'`
tmp468_res2_h=`echo $res_tmp468 | awk '{print $3}'`
tmp468_res2_l=`echo $res_tmp468 | awk '{print $4}'`
tmp468_res3_h=`echo $res_tmp468 | awk '{print $5}'`
tmp468_res3_l=`echo $res_tmp468 | awk '{print $6}'`
tmp468_res4_h=`echo $res_tmp468 | awk '{print $7}'`
tmp468_res4_l=`echo $res_tmp468 | awk '{print $8}'`
tmp468_res5_h=`echo $res_tmp468 | awk '{print $9}'`
tmp468_res5_l=`echo $res_tmp468 | awk '{print $10}'`
tmp468_res6_h=`echo $res_tmp468 | awk '{print $11}'`
tmp468_res6_l=`echo $res_tmp468 | awk '{print $12}'`
tmp468_res7_h=`echo $res_tmp468 | awk '{print $13}'`
tmp468_res7_l=`echo $res_tmp468 | awk '{print $14}'`
tmp468_res8_h=`echo $res_tmp468 | awk '{print $15}'`
tmp468_res8_l=`echo $res_tmp468 | awk '{print $16}'`
tmp468_res9_h=`echo $res_tmp468 | awk '{print $17}'`
tmp468_res9_l=`echo $res_tmp468 | awk '{print $18}'`
fmt_print "console" $INFO "Tmp468 read result:"
convert_tmp468_data $tmp468_res1_h $tmp468_res1_l "$TMP468_Channel0_name"
convert_tmp468_data $tmp468_res2_h $tmp468_res2_l "$TMP468_Channel1_name"
convert_tmp468_data $tmp468_res3_h $tmp468_res3_l "$TMP468_Channel2_name"
convert_tmp468_data $tmp468_res4_h $tmp468_res4_l "$TMP468_Channel3_name"
convert_tmp468_data $tmp468_res5_h $tmp468_res5_l "$TMP468_Channel4_name"
convert_tmp468_data $tmp468_res6_h $tmp468_res6_l "$TMP468_Channel5_name"
convert_tmp468_data $tmp468_res7_h $tmp468_res7_l "$TMP468_Channel6_name"
convert_tmp468_data $tmp468_res8_h $tmp468_res8_l "$TMP468_Channel7_name"
convert_tmp468_data $tmp468_res9_h $tmp468_res9_l "$TMP468_Channel8_name"
}
process_tmp468(){
read_tmp468_value
}
# ---------------------------------------------------------
# Chip TMP112
# ---------------------------------------------------------
# @Param1 tmp112 data high 8bit
# @Param2 tmp112 data low 8bit
# @Param3 customization sensor name
convert_tmp112_data(){
if [ "$3" = "disable" ];then
return
fi
hex_value1=$(echo "$1" | awk '{sub(/^0x/,""); print}')
hex_value2=$(echo "$2" | awk '{sub(/^0x/,""); print}')
merge_value="${hex_value1}${hex_value2}"
dec_val=$(hex_to_dec $merge_value)
upper_hex_value=$(echo "$merge_value" | awk '{
for(i=1; i<=length($0); i++){
if(tolower(substr($0,i,1)) ~ /^[a-f]$/)
printf toupper(substr($0,i,1));
else
printf substr($0,i,1);
}
print ""
}')
binary_number=$(echo "ibase=16;obase=2;$upper_hex_value" | bc)
bin_length=$(echo $binary_number | awk '{print length($0)}')
last_digit=$(echo $binary_number | cut -c $bin_length)
if [ $last_digit -eq 0 ];then
temp=$(echo "scale=4; $dec_val / 16 * 0.0625" | bc)
elif [ $last_digit -eq 1 ];then
temp=$(echo "scale=4; ( $dec_val - 1 ) / 8 * 0.0625" | bc)
fi
echo "$3 : $temp"
fmt_print "log" $INFO "dec_val : $dec_val"
fmt_print "log" $INFO "binary_number : $binary_number"
fmt_print "log" $INFO "last_digit : $last_digit"
fmt_print "log" $INFO "temp : $temp"
}
read_tmp112_value(){
fmt_print "log" $INFO "Start Read tmp112 channel data ..."
res_tmp112=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_tmp112 r2`
reset_pca9548 $i2c_bus $pca9548_slave
fmt_print "log" $INFO "Tmp112 raw data: $res_tmp112"
fmt_print "console" $INFO "Tmp112 read result:"
convert_tmp112_data $res_tmp112 "$TMP112_Sensor_name"
}
process_tmp112(){
read_tmp112_value
}
# ---------------------------------------------------------
# Chip INA226
# ---------------------------------------------------------
# @Param1 ina226 voltage data high 8bit
# @Param2 ina226 voltage data low 8bit
# @Param3 ina226 current data high 8bit
# @Param4 ina226 current data low 8bit
# @Param5 customization sensor name
convert_ina226_data(){
if [ "$5" = "disable" ];then
return
fi
hex_voltage_value1=$(echo "$1" | awk '{sub(/^0x/,""); print}')
hex_voltage_value2=$(echo "$2" | awk '{sub(/^0x/,""); print}')
hex_current_value1=$(echo "$3" | awk '{sub(/^0x/,""); print}')
hex_current_value2=$(echo "$4" | awk '{sub(/^0x/,""); print}')
merge_voltage_value="${hex_voltage_value1}${hex_voltage_value2}"
merge_current_value="${hex_current_value1}${hex_current_value2}"
dec_voltage_val=$(hex_to_dec $merge_voltage_value)
dec_current_val=$(hex_to_dec $merge_current_value)
real_voltage=$(echo "scale=4; $dec_voltage_val * 1.25 / 1000 " | bc)
real_current=$(echo "scale=4; $dec_current_val * 2.5 * 0.167 / 1000 " | bc)
format_voltage=$(echo "$real_voltage" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
format_current=$(echo "$real_current" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
real_power=$(echo "scale=4; $format_voltage * $format_current" | bc)
format_power=$(echo "$real_power" | awk '{ if ($0 ~ /^\./) print "0" $0; else print $0 }')
echo "$5 Voltage: ${format_voltage}V"
echo "$5 Current: ${format_current}A"
echo "$5 Power : ${format_power}W"
}
read_ina226_value(){
fmt_print "log" $INFO "Start Read INA226 channel0 data ..."
hex_voltage_res=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_ina226_ch0_voltage r2`
hex_current_res=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_ina226_ch0_current r2`
reset_pca9548 $i2c_bus $pca9548_slave
fmt_print "log" $INFO "INA226 raw volt : $hex_voltage_res"
fmt_print "log" $INFO "INA226 raw volt : $hex_current_res"
convert_ina226_data $hex_voltage_res $hex_current_res "$INA226_Channel0_name"
}
process_ina226(){
read_ina226_value
}
# ---------------------------------------------------------
# Chip LM95241
# ---------------------------------------------------------
# @Param1 lm95241 data high 8bit
# @Param2 lm95241 data low 8bit
# @Param3 customization sensor name
convert_lm95241_data(){
if [ "$3" = "disable" ];then
return
fi
hex_value1=$(echo "$1" | awk '{sub(/^0x/,""); print}')
hex_value2=$(echo "$2" | awk '{sub(/^0x/,""); print}')
dec_val1=$(hex_to_dec $hex_value1)
dec_val2=$(hex_to_dec $hex_value2)
temp=$(echo "scale=4; $dec_val1 + $dec_val2 / 32 * 0.125 " | bc)
echo "$3 : $temp"
}
read_lm95241_value(){
fmt_print "log" $INFO "Start Read LM95241 channel data ..."
res_lm95241_local_h=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_lm95241_local_H r1`
res_lm95241_remote1_h=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_lm95241_remote1_H r1`
res_lm95241_remote2_h=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_lm95241_remote2_H r1`
res_lm95241_local_l=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_lm95241_local_L r1`
res_lm95241_remote1_l=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_lm95241_remote1_L r1`
res_lm95241_remote2_l=`i2ctransfer -y $i2c_bus w1@$chip_slave $REG_lm95241_remote2_L r1`
reset_pca9548 $i2c_bus $pca9548_slave
fmt_print "log" $INFO "LM95241 local data: $res_lm95241_local_h $res_lm95241_local_l"
fmt_print "log" $INFO "LM95241 remote1 data: $res_lm95241_remote1_h $res_lm95241_remote1_l"
fmt_print "log" $INFO "LM95241 remote2 data: $res_lm95241_remote2_h $res_lm95241_remote2_l"
fmt_print "console" $INFO "LM95241 read result:"
convert_lm95241_data $res_lm95241_local_h $res_lm95241_local_l "$LM95241_Local_name"
convert_lm95241_data $res_lm95241_remote1_h $res_lm95241_remote1_l "$LM95241_Remote1_name"
convert_lm95241_data $res_lm95241_remote2_h $res_lm95241_remote2_l "$LM95241_Remote2_name"
}
process_lm95241(){
read_lm95241_value
}
# ---------------------------------------------------------
# CHIP
# ---------------------------------------------------------
# do a customization command
write_read_chip(){
cmd_wr=$option_data
res_wr=`$cmd_wr`
reset_pca9548 $i2c_bus $pca9548_slave
fmt_print "console" $INFO "Chip Command: $cmd_wr"
fmt_print "console" $INFO "The Result : $res_wr"
}
process_chip(){
write_read_chip
}
# ---------------------------------------------------------
# FRU
# ---------------------------------------------------------
# parse fru file to hex string which can be used in i2ctransfer
parse_fru_write_data(){
if [ -e $fru_file_name ];then
fmt_print "log" $INFO "Fru file exist!"
else
fmt_print "log" $ERROR "Fru file not exist!"
fmt_print "console" $WARNING "Fru file not exist in current directory!"
fmt_print "console" $ERROR "Operation Failed!"
exit 1
fi
# calculate fru size
fru_write_size=`ls -lht | grep $fru_file_name | awk '{print $5}'`
fmt_print "console" $INFO "Fru File [$fru_file_name] size = $fru_write_size Bytes"
fmt_print "log" $INFO "Fru File [$fru_file_name] size = $fru_write_size Bytes"
# get fru raw data
fru_raw_data=`hexdump -C $fru_file_name | awk '{
for(i=2;i<18;i++){
print $i
}
}'`
fmt_print "log" $INFO "Fru Raw Data: $fru_raw_data"
# parse data to hex string
fru_write_data=`echo $fru_raw_data | awk -v size=$fru_write_size '{
for(i=1;i<=size;i++){
printf "0x%s ",$i
}
}'`
fmt_print "console" $INFO "Success to get FRU data"
}
read_fru(){
res_fru=`i2ctransfer -y $i2c_bus w2@$chip_slave $fru_offset r$fru_size`
reset_pca9548 $i2c_bus $pca9548_slave
# print fru data every 16 bytes per row
fmt_print "console" $INFO "The Fru Data :"
echo "$res_fru" | \
awk '{
line="";
count=0;
for(i=1; i<=NF; i++){
hex=substr($i, 3);
if(line != ""){
line = line " ";
}
line = line hex;
count++;
if (count == 16){
print line;
line = "";
count = 0;
}
}
if(line != ""){
print line;
}
}'
}
write_fru(){
start_offset=1
write_offset=0
base_offset=0
hex_offset="0x00"
hex_base_offset="0x00"
bitshift_cnt=0
while true
do
if [ $fru_write_size -eq 0 ];then
fmt_print "console" $INFO "Complete write Fru action!!!"
break
fi
once_data=`echo $fru_write_data | awk -v start=$start_offset '{
for(i=1;i<=NF;i++)
{
if(i>=start && i<start+8)
{
printf "%s ", $i
}
}
}'`
fmt_print "console" $INFO "Start write fru data : $once_data"
# do once write
hex_num=$(echo "obase=16; $write_offset" | bc)
hex_offset=$(echo $hex_num | awk '{printf("0x%s", $0)}')
base_num=$(echo "obase=16; $base_offset" | bc)
hex_base_offset=$(echo $base_num | awk '{printf("0x%s", $0)}')
write_command="i2ctransfer -y $i2c_bus w10@$chip_slave $hex_base_offset $hex_offset $once_data"
fmt_print "log" $INFO "FRU write command : $write_command"
write_res=`$write_command`
# counting varible iterate
write_offset=$(($write_offset+8))
start_offset=$(($start_offset+8))
fru_write_size=$(($fru_write_size-8))
bitshift_cnt=$(($bitshift_cnt+1))
if [ $bitshift_cnt -eq 32 ];then
base_offset=$(($base_offset+1))
write_offset=0
bitshift_cnt=0
fi
done
reset_pca9548 $i2c_bus $pca9548_slave
}
process_fru(){
if [ "$option_data" = "write" ];then
write_fru
else
read_fru
fi
}
# ---------------------------------------------------------
# PMBUS Function
# ---------------------------------------------------------
#@Param1 : Operation Type, get|set
#@Param2 : Pmbus Command, hex data
#@Param3 : Pmbus reading size, BYTE(1)|WORD(2)|BLOCK(3)|RAW(4)
transfer_pmbus_command(){
if [ "$1" = "get" ];then
pmbus_res=`i2ctransfer -y $i2c_bus w1@$chip_slave $2 r$3`
reset_pca9548 $i2c_bus $pca9548_slave
fmt_print "console" $INFO "$option_data ,Command Code:$2"
fmt_print "console" $INFO "Result: $pmbus_res"
else
fmt_print "console" $WARNING "Not Support Set function in this version : $SCRIPT_VERSION"
fi
}
# Do pmbus test if plugin avalible
process_pmbus(){
transfer_pmbus_command $pmbus_op $hex_pmbus_cmd $hex_pmbus_size
}
# ---------------------------------------------------------
# Reserve Function
# ---------------------------------------------------------
# This function is temporarily retained for future functional expansion
handle_reserve(){
fmt_print "console" $INFO "Waiting for user defined"
}
# ---------------------------------------------------------
# END of CHIP Function
# ---------------------------------------------------------
start_get_sensor(){
# switch pca9641 and pca9548
# if enable debug mode, will not do this action.
if [ $DEBUG_MODE -eq 0 ];then
# set global varible by server type
set_configuration
record_config_info
# if we have 9641 on i2c, then do it
if [ $is_have_pca9641 -eq 1 ];then
get_pca9641_control
fi
switch_pca9548_channel
fi
# get sensor detail value
case $sensor_type in
"emc1413")
process_emc1413
;;
"adc128")
process_adc128
;;
"ina3221")
process_ina3221
;;
"ina226")
process_ina226
;;
"tmp468")
process_tmp468
;;
"tmp112")
process_tmp112
;;
"lm95241")
process_lm95241
;;
"chip")
process_chip
;;
"fru")
process_fru
;;
"pmbus")
process_pmbus
;;
"reserve")
handle_reserve
;;
*)
fmt_print "console" $ERROR "Unsupport Sensor Type !!! - $sensor_type"
fmt_print "console" $INFO "Support list: $Support_Sensor_List"
print_usage
;;
esac
}
detect_on_5280m7(){
if [ $nic_type = "ocp" ];then
i2c_bus=3
do_i2c_detect $i2c_bus 0x70 0x01 0 0
do_i2c_detect $i2c_bus 0x70 0x02 1 1
do_i2c_detect $i2c_bus 0x70 0x04 2 2
reset_pca9548 $i2c_bus 0x70
else
i2c_bus=12
pca9641_slave="0x41"
get_pca9641_control
do_i2c_detect $i2c_bus 0x72 0x02 1 0
do_i2c_detect $i2c_bus 0x72 0x04 2 1
do_i2c_detect $i2c_bus 0x72 0x08 3 2
reset_pca9548 $i2c_bus 0x72
i2c_bus=13
pca9641_slave="0x42"
get_pca9641_control
do_i2c_detect $i2c_bus 0x72 0x02 1 3
do_i2c_detect $i2c_bus 0x72 0x04 2 4
do_i2c_detect $i2c_bus 0x72 0x08 3 5
reset_pca9548 $i2c_bus 0x72
fi
}
detect_on_5468m7(){
if [ $nic_type = "ocp" ];then
fmt_print "console" $ERROR "Unsupport OCP Nic on $server_type"
fmt_print "console" $INFO "Please check if script has update version"
exit 1
fi
i2c_bus=13
pca9641_slave="0x31"
get_pca9641_control
do_i2c_detect $i2c_bus 0x70 0x01 0 0
do_i2c_detect $i2c_bus 0x70 0x02 1 1
do_i2c_detect $i2c_bus 0x70 0x04 2 2
do_i2c_detect $i2c_bus 0x70 0x08 3 3
do_i2c_detect $i2c_bus 0x70 0x10 4 4
reset_pca9548 $i2c_bus 0x70
i2c_bus=14
pca9641_slave="0x42"
get_pca9641_control
do_i2c_detect $i2c_bus 0x71 0x01 0 5
do_i2c_detect $i2c_bus 0x71 0x02 1 6
do_i2c_detect $i2c_bus 0x71 0x04 2 7
do_i2c_detect $i2c_bus 0x71 0x08 3 8
do_i2c_detect $i2c_bus 0x71 0x10 4 9
do_i2c_detect $i2c_bus 0x71 0x20 5 10
reset_pca9548 $i2c_bus 0x71
}
detect_on_donghu(){
if [ $nic_type = "ocp" ];then
fmt_print "console" $ERROR "Unsupport OCP Nic on $server_type"
fmt_print "console" $INFO "Please check if script has update version"
exit 1
fi
i2c_bus=3
do_i2c_detect $i2c_bus 0x70 0x80 7 0
do_i2c_detect $i2c_bus 0x70 0x40 6 1
do_i2c_detect $i2c_bus 0x70 0x20 5 2
do_i2c_detect $i2c_bus 0x70 0x10 4 3
do_i2c_detect $i2c_bus 0x70 0x08 3 4
do_i2c_detect $i2c_bus 0x70 0x04 2 5
do_i2c_detect $i2c_bus 0x70 0x02 1 6
do_i2c_detect $i2c_bus 0x70 0x01 0 7
reset_pca9548 $i2c_bus 0x70
}
detect_on_yichun(){
if [ $nic_type = "ocp" ];then
fmt_print "console" $ERROR "Unsupport OCP Nic on $server_type"
fmt_print "console" $INFO "Please check if script has update version"
exit 1
fi
i2c_bus=12
do_i2c_detect $i2c_bus 0x74 0x02 2 0
reset_pca9548 $i2c_bus 0x74
i2c_bus=13
do_i2c_detect $i2c_bus 0x74 0x02 2 1
reset_pca9548 $i2c_bus 0x74
i2c_bus=14
do_i2c_detect $i2c_bus 0x74 0x02 2 2
reset_pca9548 $i2c_bus 0x74
}
detect_on_qiandaohu(){
if [ $nic_type = "ocp" ];then
fmt_print "console" $ERROR "Unsupport OCP Nic on $server_type"
fmt_print "console" $INFO "Please check if script has update version"
exit 1
fi
i2c_bus=3
do_i2c_detect $i2c_bus 0x70 0x08 3 0
do_i2c_detect $i2c_bus 0x70 0x04 2 1
do_i2c_detect $i2c_bus 0x70 0x01 0 2
do_i2c_detect $i2c_bus 0x70 0x02 1 3
do_i2c_detect $i2c_bus 0x70 0x80 7 4
do_i2c_detect $i2c_bus 0x70 0x40 6 5
do_i2c_detect $i2c_bus 0x70 0x10 4 6
do_i2c_detect $i2c_bus 0x70 0x20 5 7
reset_pca9548 $i2c_bus 0x70
i2c_bus=6
do_i2c_detect $i2c_bus 0x71 0x08 3 8
do_i2c_detect $i2c_bus 0x71 0x04 2 9
do_i2c_detect $i2c_bus 0x71 0x01 0 10
do_i2c_detect $i2c_bus 0x71 0x08 3 11
reset_pca9548 $i2c_bus 0x71
}
detect_on_5688m7(){
if [ $nic_type = "ocp" ];then
fmt_print "console" $ERROR "Unsupport OCP Nic on $server_type"
fmt_print "console" $INFO "Please check if script has update version"
exit 1
fi
i2c_bus=12
pca9641_slave="0x31"
get_pca9641_control
do_i2c_detect $i2c_bus 0x71 0x01 0 0
do_i2c_detect $i2c_bus 0x71 0x02 1 1
do_i2c_detect $i2c_bus 0x71 0x04 2 2
do_i2c_detect $i2c_bus 0x71 0x08 3 3
reset_pca9548 $i2c_bus 0x71
do_i2c_detect $i2c_bus 0x72 0x01 0 4
do_i2c_detect $i2c_bus 0x72 0x02 1 5
do_i2c_detect $i2c_bus 0x72 0x04 2 6
do_i2c_detect $i2c_bus 0x72 0x08 3 7
reset_pca9548 $i2c_bus 0x72
}
debug_user_defined_detect(){
fmt_print "console" $INFO "Detect by user-defined"
# default execute
fmt_print "console" $INFO "Default detect on the default i2c: $i2c_bus"
i2cdetect -y $i2c_bus
}
start_detect_device(){
# In debug mode, do customization detect action
if [ $DEBUG_MODE -ne 0 ];then
fmt_print "console" $INFO "In debug mode now"
debug_user_defined_detect
exit 0
fi
fmt_print "console" $INFO "Detect on server : $server_type, nic type: $nic_type"
case $server_type in
"5280m7")
detect_on_5280m7
;;
"5468m7")
detect_on_5468m7
;;
"donghu")
detect_on_donghu
;;
"yichun")
detect_on_yichun
;;
"qiandaohu")
detect_on_qiandaohu
;;
"5688m7")
detect_on_5688m7
;;
*)
fmt_print "console" $ERROR "Unsupport Server Type - $server_type"
fmt_print "console" $INFO "Support list : $Support_Server_List"
;;
esac
}
# ---------------------------------------------------------
# Start Execute Script(main)
# ---------------------------------------------------------
pre_exec_hook
if [ "$1" = "version" ];then
echo "Script Version : $SCRIPT_VERSION"
exit 0
fi
if [ "$1" = "detect" ];then
if [ "$2" = "ocp" ];then
nic_type="ocp"
elif [ "$2" = "pcie" ];then
nic_type="pcie"
else
fmt_print "console" $WARNING "Please provide valid nic type, default to pcie"
nic_type="pcie"
fi
start_detect_device
exit 0
fi
if [ $param_num -le 2 ];then
print_usage "all"
else
start_get_sensor
fi