#!/bin/sh
#set -x
#dmesg -n 7

. /usr/cisco/bin/ciscosetup.sh

if [ -f /proc/sys/kernel/softlockup_all_cpu_backtrace ]; then
    echo 1 > /proc/sys/kernel/softlockup_all_cpu_backtrace
fi

if [ "$MERAKI_BOARD" == "axel-bcm" ]; then
    # Setup Antenna-D for 2.4G instead of BLE
    devmem 0xff800568 32 0x2
    devmem 0xff80056c 32 0x21
    io_status=`devmem 0xff800500` # Read the current I/O status of the GPIO pins
    devmem 0xff800500 32 $((io_status | 0x00000004)) # Set GPIO Pin 2 to Output Mode. This is the Pin to control Antenna-D config
    status=`devmem 0xff800528` # Read the current Line status of the GPIO pins
    devmem 0xff800528 32 $((status & ~0x00000004)) # Set Pin2 Low for Antenna-D to 2.4G Wi-Fi mapping

    # load bcm driver
#   modload /lib/modules/wfd.ko && modrm /lib/modules/wfd.ko
    modload /lib/modules/hnd.ko && modrm /lib/modules/hnd.ko
    modload /lib/modules/emf.ko && modrm /lib/modules/emf.ko
#   modload /lib/modules/igs.ko && modrm /lib/modules/igs.ko
    nvm_file="/etc/nvram_setting"
    /opt/cisco/bin/cisco_wifi_nvm_util --restore "/storage/nvram_setting"
    if [ -f /storage/nvram_setting ]; then
# restore will create a file specified by the argument if active and backup
# do not match. It will not create the file if the caldata partition is not
# programmed
        nvm_file="/storage/nvram_setting"
    fi
    # Default family nvram setting (compile info)
    /usr/sbin/nvram kernelset $nvm_file
    # Board specific nvram override (runtime info)
    /usr/sbin/nvram kernelset /var/platform/wifi_nvram_setting

fi

if [ "$MERAKI_BOARD" == "corsica" ]; then
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss | head -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    write_u_boot_info_file $MERAKI_BOARD
elif [ "$MERAKI_BOARD" == "axel-qca" ]; then
    # Assign all the NSS0 & NSS1 interrupts handling to Host CPU core 0
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_empty_buf_sos | head -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_empty_buf_sos | tail -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_empty_buf_queue | head -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_empty_buf_queue | tail -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss-tx-unblock | head -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss-tx-unblock | tail -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_queue0 | head -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_queue0 | tail -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_queue1 | head -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_queue1 | tail -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_queue2 | head -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_queue2 | tail -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_queue3 | head -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_queue3 | tail -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_coredump_complete | head -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_coredump_complete | tail -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_paged_empty_buf_sos | head -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    echo 1 > /proc/irq/`cat /proc/interrupts | grep nss_paged_empty_buf_sos | tail -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity

    write_u_boot_info_file $MERAKI_BOARD
elif [ "$MERAKI_BOARD" == "mallorca" -o "$MERAKI_BOARD" == "entr17" ]; then
    #echo 1 > /proc/irq/`cat /proc/interrupts |grep edma_eth_rx0 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    write_u_boot_info_file $MERAKI_BOARD
elif [ "$MERAKI_BOARD" == "barbados" -o "$MERAKI_BOARD" == "axel-mvl" ]; then
    # This should be enabled after NSS is enabled
    #echo 1 > /proc/irq/`cat /proc/interrupts | grep nss | head -1 | awk '{print $1}' | sed -e 's/://'`/smp_affinity
    write_u_boot_info_file $MERAKI_BOARD
elif [ "$MERAKI_BOARD" == "axel-bcm" ]; then
    taskset -p 4 `pidof bcmsw_rx` > /dev/null
    renice -n -20  -p `pidof bcmsw_rx` > /dev/null
    taskset -p 4 `pidof enet-txq-kthrd` > /dev/null
    renice -n -20  -p `pidof enet-txq-kthrd` > /dev/null
    taskset -p 4 `pidof skb_free_task` > /dev/null
    renice -n -19  -p `pidof skb_free_task` > /dev/null

    irq_affinity_num=`grep -E -m1 'GIC  77' /proc/interrupts | cut -d ':' -f 1 | tail -n1 | tr -d ' '`
    [ -n "$irq_affinity_num" ] && echo 4 > /proc/irq/$irq_affinity_num/smp_affinity
    write_u_boot_info_file $MERAKI_BOARD
fi

/usr/bin/check_bootreason
BOOT_REASON=`cat /BOOT_REASON`
echo "Last reload reason : $BOOT_REASON"

exit 0
