#!/bin/sh
#
# Copyright (c) 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.
#

FLOW_DUMP_FILE=${1:-ppe_flow_dump}
FLOW_DUMP_ROOT=/dev/qca-nss-ppe

#
# usage: ppe_flow_dump
#
module_state_mount() {
	local flow_dump_file=$1
	local flow_dump_dir=$2
	local stats_file="/sys/kernel/debug/qca-nss-ppe/ppe_drv_flow_dump/ppe_flow_dump"

	if [ -e "${flow_dump_dir}/${flow_dump_file}" ]
	then
		#echo "already mounted"
		return 0
	fi

	if [ ! -e "$stats_file" ]
	then
		#echo "... Dump not supported"
		return 1
	fi

	local major_num="`cat $stats_file`"
	#echo "... Mounting stats $stats_file with major_num: $major_num"
	mknod "${flow_dump_dir}/${flow_dump_file}" c $major_num 0
}


# all state files are mounted under MOUNT_ROOT, so make sure it exists
mkdir -p ${FLOW_DUMP_ROOT}

#
# attempt to mount state files for the requested module and cat it
# if the mount succeeded
#
module_state_mount ${FLOW_DUMP_FILE} ${FLOW_DUMP_ROOT} && {
	cat ${FLOW_DUMP_ROOT}/${FLOW_DUMP_FILE}
	exit 0
}

exit 2
