#!/bin/bash
####################################################################
#
#      File: S44klmfabric
#      Name: Rajesh Ranga
#
#      Description:
#      broadcom klm for fabric on FC card
#
# Copyright (c) 2015-2021 by cisco Systems, Inc.
# All rights reserved.
#
#####################################################################

. /etc/init.d/mod_ins/module-load-functions
. /etc/init.d/spirit_pd.sh

boot_debug "Executing $0 [S44klmfabric]"

JERICHO_CNTRL_REG_0=0x000003A0
JERICHO_CNTRL_REG_1=0x000003A4
FE3200_CNTRL_REG=0x000003F0
IOFPGA_RESET_CTRL_REG=0x00000084
RESET_BIT_BCM8456=6

# 27014 = Zermatt(NCS5502-SE)       8 Jericho Version
# 27015 = Zermatt(NCS5502-SE-PROTO) 2 Jericho Version 
# 27016 = Turin(NCS5501-SE)         1 Qumran  Version 
# 27019 = Zermatt(NCS5502)          8 Jericho Version, no TCAM
# 27020 = Taihu(NCS5501)            1 Qumran  Version 
# 27027 = Tortin(NCS5501-HD)        1 Qumran  Version 
# 27028 = Trift(NCS5501-HD-S)       1 Qumran  Version 
# 27030 = Peyto(NCS55A2-MOD-SE-S)   1 J+ version
# 27031 = Peyto(NCS55A2-MOD-S)      1 J+ version, no TCAM
# 27045 = Peyto(NCS55A2-MOD-HD-S)   1 J+ version, no TCAM HD
# 60000 = Peyto(NCS55A2-MOD-HX-S)   1 J+ version, no TCAM HX CC
# 60001 = Peyto(NC55A2-MOD-SE-H-S)  1 J+ version with TCAM HD CC
card_index=$(get_cmdline_card_index)

function S44klmfabric_disable_devices()
{
    case ${card_index} in 
        27014 | 27015 | 27019)
               for bdf in $(lspci -nn | egrep "14e4:86|14e4:87|14e4:89|14e4:8456" | cut -d " " -f 1);
               do
                   echo 1 > /sys/bus/pci/devices/0000:${bdf}/remove;
               done

               # Base IOFPGA
               #register JERICHO_CNTRL_REG_0 contains only Jer 1,3
               iofpga_reg_write 1 JERICHO_CNTRL_REG_0 0x0
               #register JERICHO_CNTRL_REG_0 contains only Jer 4,5
               iofpga_reg_write 1 JERICHO_CNTRL_REG_1 0x0

               # Put BCM 8456 in reset - Base board only set bit 6
               reg_val=$(iofpga_reg_read 1 IOFPGA_RESET_CTRL_REG)
               reg_val=$(( $reg_val | (1 << $RESET_BIT_BCM8456) ))
               hex_reg_val=$(echo $(printf 0x%08X $reg_val))
               iofpga_reg_write 1 IOFPGA_RESET_CTRL_REG $hex_reg_val

               # Mezz IOFPGA
               #register JERICHO_CNTRL_REG_0 contains only Jer 1,3
               iofpga_reg_write 2 JERICHO_CNTRL_REG_0 0x0
               #register JERICHO_CNTRL_REG_0 contains only Jer 4,5
               iofpga_reg_write 2 JERICHO_CNTRL_REG_1 0x0

               #FE3200 reset clear Mezz board only
               iofpga_reg_write 2  FE3200_CNTRL_REG 0x0
           ;;

        27016 | 27020 | 27028 | 27030 | 27031 | 27045 | 27066 | 27067 | 60001 | 60004)
               for bdf in $(lspci -nn | grep "14e4:8[3-4]" | cut -d " " -f 1);
               do
                   echo 1 > /sys/bus/pci/devices/0000:${bdf}/remove;
               done

               #register JERICHO_CNTRL_REG_0/QUMRAN(used in Turin NCS5501)
               # contains only Jer 0
               iofpga_reg_write 1 JERICHO_CNTRL_REG_0 0x0
           ;;

        *) echo "Unknown platform"
           return -1
           ;;
    esac

    return 0
}
readonly -f S44klmfabric_disable_devices


BRCM_KBDE_DEV_NAME=/dev/linux-kernel-bde
BRCM_UBDE_DEV_NAME=/dev/linux-user-bde

case $1 in
    start)
        # Disable the fabric and switch devices before inserting the BDE KLMs
        S44klmfabric_disable_devices

        # Insert the KLMs
        modprobe linux-kernel-bde reverse_enum_dev_total=0 debug=2 dmasize=64M usemsi=1
        modprobe linux-user-bde
        BRCM_KBDE_MAJ_NUM=`cat /proc/devices | fgrep linux-kernel-bde | awk '$0=$1'`
        BRCM_UBDE_MAJ_NUM=`cat /proc/devices | fgrep linux-user-bde | awk '$0=$1'`
        mknod $BRCM_KBDE_DEV_NAME c $BRCM_KBDE_MAJ_NUM 0
        mknod $BRCM_UBDE_DEV_NAME c $BRCM_UBDE_MAJ_NUM 0
    ;;

    stop)
        # Remove the KLMs
        rmmod linux-user-bde
        rmmod linux-kernel-bde
    ;;
esac
