#!/bin/sh


# On Zermatt CHASSIS,
# eth0 is wired out to mgmt
# eth1 is not used
# eth3 will be the eobc interface
# 
# identify platform
. /etc/init.d/mod_ins/module-load-functions

# Global vars
MGMT_INTF=eth0
MY_MOD_ID_HEX="$(my_slot_id x)"
MY_MOD_ID="$(my_slot_id)"
MY_SLOT_ID="$(($MY_MOD_ID-1))"
INBAND_MTU=9702
VEOBC_MTU=9702
    set_irq_affinity eth3 "0-1"


boot_debug "Got my slot: MY_MOD_ID_HEX=$MY_MOD_ID_HEX MY_MOD_ID=$MY_MOD_ID MY_SLOT_ID=$MY_SLOT_ID"

setup_eobc_sup() {

    # turn off flow control
    /usr/sbin/ethtool -A eth0 rx off tx off
    /usr/sbin/ethtool -A eth1 rx off tx off
    /sbin/ifconfig eth0 down hw ether 00:00:00:00:$MY_MOD_ID_HEX:01 mtu $VEOBC_MTU
    /sbin/ifconfig eth1 down hw ether 00:00:00:00:$MY_MOD_ID_HEX:01 mtu $VEOBC_MTU
}

setup_inband_sup() {

    # turn off flow control
    /usr/sbin/ethtool -A eth2 rx off tx off
    /usr/sbin/ethtool -A eth3 rx off tx off
    # change the mac for eth2
    /sbin/ifconfig eth2 down hw ether 00:00:00:01:$MY_MOD_ID_HEX:01 mtu $INBAND_MTU
    /sbin/ifconfig eth3 down hw ether 00:00:00:01:$MY_MOD_ID_HEX:01 mtu $INBAND_MTU
}

install_psdev_sup() {

    boot_debug "Installing psdev ..."

    local is_vm=0
    if [ -f /vmachine ]; then
        is_vm=1
    fi
    insmod -f /lib/modules/klm_psdev.o eobc_dev_names=eth0,eth1 epc_dev_names=eth2,eth3 vmachine=$is_vm

    # configure the interfaces
    /sbin/ifconfig ps-eobc down
    /sbin/ifconfig ps-eobc hw ether 00:00:00:00:$MY_MOD_ID_HEX:01 mtu $VEOBC_MTU
    /sbin/ifconfig ps-eobc up
    /sbin/ifconfig ps-inb down
    /sbin/ifconfig ps-inb hw ether 00:00:00:01:$MY_MOD_ID_HEX:01
    /sbin/ifconfig ps-inb up
    /sbin/ifconfig ps-inb mtu $INBAND_MTU
}

install_veobc() {

    boot_debug "Installing veobc ..."

    insmod -f /lib/modules/klm_veobc.o eobc=ps-eobc inband=ps-inb

    # configure the interfaces
    /sbin/ifconfig veobc down
    /sbin/ifconfig veobc hw ether 00:00:00:00:$MY_MOD_ID_HEX:01 mtu $VEOBC_MTU
    /sbin/ifconfig veobc up
}

sanitize_eth() {

    # Disable all offload optimizations
    # netstack cannot handle it.

    /usr/sbin/ethtool -K $1 gso off 
    /usr/sbin/ethtool -K $1 tso off 
    /usr/sbin/ethtool -K $1 lro off 
    /usr/sbin/ethtool -K $1 gro off 
}


create_dummy_eobc () {

    # Create dummy interfaces for faking eobc 
    /sbin/ip link add type dummy
    /sbin/ip link add type dummy
    /sbin/ip link add type dummy
}

set_irq_affinity() {                                                                                                                               
    dev_name=$1
    local smp_mask=$2
    /sbin/ifconfig $dev_name up    
    for i in `cat /proc/interrupts | grep $dev_name | awk -F':' '{printf("%d\n", $1)}'`;
    do
        echo $smp_mask > /proc/irq/$i/smp_affinity_list
    done
    /sbin/ifconfig $dev_name down
}

configure_igb_rings() {
    local dev_name=$1
    local ring_size=$2
    /usr/sbin/ethtool -G $1 tx $ring_size
    /usr/sbin/ethtool -G $1 rx $ring_size
}

sup_pre_init() {
    # Setup interrupt affinity
    # Separate out the EPC traffic from EOBC/Mgmt
    set_irq_affinity eth0 "2-3"
    set_irq_affinity eth1 "2-3"

    set_irq_affinity eth2 "0-1"
    # Setting mgmt affinity
    #set_irq_affinity eth6 1

    setup_eobc_sup

    setup_inband_sup
}


sup_main() {

    local mgmt_intf=eth0

    install_psdev_sup

    sanitize_eth $mgmt_intf


    # set the eobc and epc descriptors to 4K.
    configure_igb_rings eth2 4096
    configure_igb_rings eth3 4096
    configure_igb_rings eth0 4096
    configure_igb_rings eth6 4096
}


setup_hw_eobc() {

    echo "Setting up hw eobc:"

    #MAC ADDRESS: 00:00:00:<eobc:0, epc:1>:<slot-id+1>:<card-type>
    #card-type: SUP = 1, LC = 2, FC = 3, SC = 4
    /sbin/ifconfig $1 down hw ether 00:00:00:00:$MY_MOD_ID_HEX:3

    # setup the rxq size to 4096
    echo 0 0 4096 > /sys/devices/platform/neta/gbe/rxq_size
    # setup the txq size
    echo 0 0 0 1024 > /sys/devices/platform/neta/gbe/txq_size

    # turn off flow control
    /usr/sbin/ethtool -A $1 rx off tx off

    /sbin/ifconfig $1 mtu $VEOBC_MTU up
    /sbin/ifconfig lo netmask 255.255.0.0

}

BOARDTYPE=$(get_cctrl_board_type)

main() {

    boot_debug "Configuring network ..."

    sup_main
}

case $1 in
    pre_init)
	sup_pre_init
       
        ;;
    *)
        main
        ;;
esac

