#!/bin/sh /etc/rc.common
# Copyright (C) 2006 OpenWrt.org

START=39
EXTRA_COMMANDS='check_restart'
EXTRA_HELP='    chechk fpp tx restart'
LOCKFILE="/tmp/fpp_tx_enable.lock"
FPP_CPU_LOADING=50

start() {
    if [ -e /proc/fppmode ]; then
        if [ "`grep "0" /proc/fppmode`" ]; then
            exit 0
        fi
    fi
    #echo "Enable FPP Tx interface"
    echo 1 > /sys/class/net/eth0/fast_path_enable
    echo 1 > /sys/class/net/eth2/fast_path_enable
}

stop()  {
    if [ -e /proc/fppmode ]; then
        if [ "`grep "0" /proc/fppmode`" ]; then
            exit 0
        fi
    fi
    #echo "Disable FPP Tx interface"
    echo 0 > /sys/class/net/eth0/fast_path_enable
    echo 0 > /sys/class/net/eth2/fast_path_enable
}

check_restart() {
	mkdir $LOCKFILE 2>/dev/null || {
		echo "Fpp_tx_enable check_restart already running ... " > /dev/console
		exit 0
	}

	do_restart=$(mmiotool -r 0x0a000160 | cut -d ' ' -f 4)

	if [ "$do_restart" == "0x00000000" ]; then
		rmdir $LOCKFILE 2>/dev/null
		exit 0
	fi

	model=`head -n 1 /etc/version`

	if [ "$do_restart" == "0x00000001" ]; then
		echo "FPP TX queue got stuck !! Do fpp tx restart" > /dev/console
		stop
		sleep 1
		start
	fi

	port_linkup=""
	if [ "$do_restart" == "0x00000004" ]; then
		if [ "$model" != "Vigor3900" ] ;then
			#### Vigor2960, Vigor300B
			if [ "$model" = "Vigor300B" ];then
				/sbin/switch_queue_sender PCS LAN x x x Vigor300B
				sleep 1
				port_linkup=$(cat /tmp/lan_port_status 2>/dev/null | grep -c "1")
				#echo -n "OOOOOO Vigor300B LAN : " > /dev/console
				#cat /tmp/lan_port_status >/dev/console
			else
				/sbin/switch_queue_sender PCS WAN x x x Vigor2960
				sleep 1
				port_linkup=$(cat /tmp/wan_port_status 2>/dev/null | grep -c "1")
				#echo -n "OOOOOO Vigor2960 WAN : " > /dev/console
				#cat /tmp/wan_port_status > /dev/console
			fi
		else
			#### Vigor3900
			switch_model=$(cat /tmp/v39sw_model)
			if [ "$switch_model" = "rtk8366" ] ;then
				echo STATUS > /tmp/lanss
				echo lan >> /tmp/lanss
				cat /tmp/lanss > /proc/rtk8366
				port_lan=$(cat /proc/rtk8366 | grep "Up" -c)
				#echo "OOOOOO Vigor3900 $switch_model LAN : $port_lan" > /dev/console
				[ "$port_lan" != "1" ] && port_linkup="1"
			elif [ "$switch_model" = "ar8327" ] ;then
				/sbin/switch_queue_sender PCS LAN x x x Vigor3900
				sleep 1
				port_linkup=$(cat /tmp/lan_port_status 2>/dev/null | grep -c "1")
				#echo -n "OOOOOO Vigor3900 $switch_model LAN : " > /dev/console
				#cat /tmp/lan_port_status >/dev/console
			fi
		fi

		fpp_cpu=$(cat /sys/devices/fpp/cpu_load)
		if [ "$model" = "Vigor2960" -a $fpp_cpu -ge $FPP_CPU_LOADING ] ;then
			echo "No IRQ from port 1 ... WAN is idle, but LAN is busy" > /dev/null
		elif [ "$model" = "Vigor300B" -a $fpp_cpu -ge $FPP_CPU_LOADING ] ;then
			echo "No IRQ from port 1 ... WAN is idle, but LAN is busy" > /dev/null
		elif [ "$port_linkup" == "1" ]; then
			echo "No IRQ from port 0 ... Do fpp tx restart" > /dev/console
			stop
			sleep 1
			start
		else
			echo "No IRQ from port 0 ... No link up" > /dev/null
		fi
	fi

	if [ "$do_restart" == "0x00000008" ]; then
		if [ "$model" != "Vigor3900" ] ;then
			#### Vigor2960, Vigor300B
			if [ "$model" = "Vigor300B" ];then
				/sbin/switch_queue_sender PCS WAN x x x Vigor300B
				sleep 1
				port_linkup=$(cat /tmp/wan_port_status 2>/dev/null | grep -c "1")
				#echo -n "OOOOOO Vigor300B WAN : " > /dev/console
				#cat /tmp/wan_port_status >/dev/console
			else
				/sbin/switch_queue_sender PCS LAN x x x Vigor2960
				sleep 1
				port_linkup=$(cat /tmp/lan_port_status 2>/dev/null | grep -c "1")
				#echo -n "OOOOOO Vigor2960 LAN : " > /dev/console
				#cat /tmp/lan_port_status >/dev/console
			fi
		else
			#### Vigor3900
			switch_model=$(cat /tmp/v39sw_model)
			if [ "$switch_model" = "rtk8366" ] ;then
				echo STATUS > /tmp/wanss
				echo wan >> /tmp/wanss
				cat /tmp/wanss > /proc/rtk8366
				port_wan=$(cat /proc/rtk8366 | grep "Up" -c)
				#echo "OOOOOO Vigor3900 $switch_model WAN : $port_wan" > /dev/console
				[ "$port_wan" != "1" ] && port_linkup="1"
			elif [ "$switch_model" = "ar8327" ] ;then
				/sbin/switch_queue_sender PCS WAN x x x Vigor3900
				sleep 1
				port_linkup=$(cat /tmp/wan_port_status 2>/dev/null | grep -c "1")
				#echo -n "OOOOOO Vigor3900 $switch_model WAN : " > /dev/console
				#cat /tmp/wan_port_status >/dev/console
			fi
		fi

		fpp_cpu=$(cat /sys/devices/fpp/cpu_load)
		if [ "$model" = "Vigor3900" -a $fpp_cpu -ge $FPP_CPU_LOADING ] ;then
			echo "No IRQ from port 1 ... WAN is idle, but LAN is busy" > /dev/null
		elif [ "$port_linkup" == "1" ]; then
			echo "No IRQ from port 1 ... Do fpp tx restart" > /dev/console
			stop
			sleep 1
			start
		else
			echo "No IRQ from port 1 ... No link up" > /dev/null
		fi
	fi
	
	do_restart=$(mmiotool -r 0x0a000160 | cut -d ' ' -f 4)
	if [ "$do_restart" == "0x00000002" ]; then
		echo "No ACK from fpp ... do fpp restart" > /dev/console
		/sbin/fpp_restart
	fi

	mmiotool -w 0x0a000160 0x0 > /dev/null
	rmdir $LOCKFILE 2>/dev/null
}