#!/usr/bin/env bash
# 检测MLX网卡 mlxconfig 配置
no_find_mlx_nic(){
    for opt in $(lspci -n | grep 15b3: | awk -F: '{print $NF}'); do
        case $opt in
            1021)
                PRINT_INFO "[INFO] 发现 MLX 设备 ConnectX-7"
                return 1
                ;;
            abcd)
                PRINT_INFO "[INFO] 发现 MLX 设备 abcd"
                return 255
                ;;
        esac
    done
    return 0
}

_install() {
    no_find_mlx_nic && return 0
    for dev in $(lspci -nnDb -d 15b3:: | cut -d' ' -f1)
    do
        local conf
        conf=$(mlxconfig -d $dev q)
        if echo "$conf" | grep LINK_TYPE_P |grep -q IB ;then
            if echo "$conf" |grep KEEP_ETH_LINK_UP_P1 |grep -q True ;then
                mlxconfig -d "$dev" -y set KEEP_ETH_LINK_UP_P1=0
            fi
            if echo "$conf" |grep KEEP_IB_LINK_UP_P1 |grep -q False ;then
                mlxconfig -d "$dev" -y set KEEP_IB_LINK_UP_P1=1
            fi
        fi
    done

}

_check(){
    _check="PASS"
    no_find_mlx_nic && return 0
    for dev in $(lspci -nnDb -d 15b3:: | cut -d' ' -f1)
    do
        local conf
        conf=$(mlxconfig -d $dev q)
        if echo "$conf" | grep LINK_TYPE_P |grep -q IB ;then
            if echo "$conf" |grep KEEP_ETH_LINK_UP_P1 |grep -q True ;then
                _check="ERROR"
                PRINT_ERR "[ERROR] $dev IB NIC KEEP_ETH_LINK_UP_P1 State Not OK"
            fi
            if echo "$conf" |grep KEEP_IB_LINK_UP_P1 |grep -q False ;then
                _check="ERROR"
                PRINT_ERR "[ERROR] $dev IB NIC KEEP_IB_LINK_UP_P1 State Not OK"
            fi
        fi
    done
    if [[ $_check == "ERROR" ]]; then
        return 1
    fi
}

