#!/bin/sh /etc/rc.common
#
# Copyright (c) 2021-2023 Qualcomm Innovation Center, Inc. All rights reserved.
#
# Permission to use, copy, modify, and/or distribute this software for any
# purpose with or without fee is hereby granted, provided that the above
# copyright notice and this permission notice appear in all copies.
#
# THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
# OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
#

START=18

qca_nss_dp_dependency() {
	while [ -e /lib/modules/$(uname -r)/qca-nss-dp.ko ] && [ ! -d /sys/module/qca_nss_dp ]
	do
		sleep 1
	done
}

enable_rps() {
	i=1
	irq_rps=`grep edma_rxdesc /proc/interrupts | cut -d ':' -f 1 | tr -d ' '`
	for entry in $irq_rps
	do
		echo $i > /proc/irq/$entry/smp_affinity
		i=$(expr $i \* 2)
		if [ $i -gt 8 ];then
			i=1
		fi
	done

	i=1
	irq_rps=`grep edma_txcmpl /proc/interrupts | cut -d ':' -f 1 | tr -d ' '`
	for entry in $irq_rps
	do
		echo $i > /proc/irq/$entry/smp_affinity
		i=$(expr $i \* 2)
		if [ $i -gt 8 ];then
			i=1
		fi
	done

	#Enable RPS to use only 3 cores by default
	echo 3 > /proc/sys/net/edma/rps_num_cores
}

start() {
	qca_nss_dp_dependency
	local rps_enabled="$(uci_get qca_nss_dp @general[0] enable_rps)"
	if [ "$rps_enabled" -eq 1 ]; then
		enable_rps
	fi
}
