#!/bin/sh
#################################################################
#
#   SPDX-License-Identifier: BSD-3-Clause
#   Copyright(c) 2007-2026 Intel Corporation
# 
#  version: QAT.B.3.15.0-00012
#   These contents may have been developed with support from one or more
#   Intel-operated generative artificial intelligence solutions.
#
#################################################################
#
### BEGIN INIT INFO
# Provides: QAT
# Required-Start: $ALL
# Required-Stop:
# Default-Start: 2 3 5
# Default-Stop: 0 1 4 6
# Description:  Intel QAT service
### END INIT INFO
#
# qat_service          Start/Stop the Intel QAT.
#
# chkconfig: 345 99 99
# description: modprobe the QAT modules, which loads dependent \
#       modules, before calling the user space \
#       utility to pass configuration parameters

test -f /etc/defaults/qat && . /etc/defaults/qat
SRIOV_ENABLE=${SRIOV_ENABLE-0}

INTEL_VENDORID="8086"
DH895_DEVICE_PCI_ID="0435"
C62X_DEVICE_PCI_ID="37c8"
C3XX_DEVICE_PCI_ID="19e2"
C4XX_DEVICE_PCI_ID="18a0"
QAT_200XX_DEVICE_PCI_ID="18ee"
QAT_4XXX_DEVICE_PCI_ID="4940"
QAT_401XX_DEVICE_PCI_ID="4942"
QAT_420XX_DEVICE_PCI_ID="4946"

usage()
{
    echo
    echo --------------------------------------------------------
    echo USAGE:
    echo --------------------------------------------------------
    echo "#  $0 start||stop||status||restart||shutdown"
    echo --------------------------------------------------------
    echo " Note: If there is more devices in the system"
    echo " you can start, stop or restart separate device by "
    echo " passing the dev to be restarted or stopped as a"
    echo " parameter for instance: "
    echo " $0 stop qat_dev<N>"
    echo " where N is device number."
    echo " To see all devices in the system use:"
    echo " $0 status"
    echo --------------------------------------------------------
    exit 1
}

ADF_CTL=/usr/local/bin/adf_ctl
KMOD_PATH=/boot/modules

# store the total number of each type of device
numDh895xDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$DH895_DEVICE_PCI_ID")
numC3xxDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$C3XX_DEVICE_PCI_ID")
numC4xxDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$C4XX_DEVICE_PCI_ID")
numC62xDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$C62X_DEVICE_PCI_ID")
num200xxDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$QAT_200XX_DEVICE_PCI_ID")
num4xxxDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$QAT_4XXX_DEVICE_PCI_ID|$QAT_401XX_DEVICE_PCI_ID")
num420xxDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$QAT_420XX_DEVICE_PCI_ID")

case $1 in
    Start | start)
        # First check if the modules are already loaded, load them as necessary
        # Load FWs
        if [ $numDh895xDevicesPF != 0 ] && [ $(kldstat | grep "qat_dh895xcc_fw.ko" | wc -l) == "0" ]; then
            kldload ${KMOD_PATH}/qat_dh895xcc_fw.ko
        fi
        if [ $numC3xxDevicesPF != 0 ] && [ $(kldstat | grep "qat_c3xxx_fw.ko" | wc -l) == "0" ]; then
            kldload ${KMOD_PATH}/qat_c3xxx_fw.ko
        fi
        if [ $numC4xxDevicesPF != 0 ] && [ $(kldstat | grep "qat_c4xxx_fw.ko" | wc -l) == "0" ]; then
            kldload ${KMOD_PATH}/qat_c4xxx_fw.ko
        fi
        if [ $numC62xDevicesPF != 0 ] && [ $(kldstat | grep "qat_c62x_fw.ko" | wc -l) == "0" ]; then
            kldload ${KMOD_PATH}/qat_c62x_fw.ko
        fi
        if [ $num200xxDevicesPF != 0 ] && [ $(kldstat | grep "qat_200xx_fw.ko" | wc -l) == "0" ]; then
            kldload ${KMOD_PATH}/qat_200xx_fw.ko
        fi
        if [ $num4xxxDevicesPF != 0 ] && [ $(kldstat | grep "qat_4xxx_fw.ko" | wc -l) == "0" ]; then
            kldload ${KMOD_PATH}/qat_4xxx_fw.ko
        fi
        if [ $num420xxDevicesPF != 0 ] && [ $(kldstat | grep "qat_420xx_fw.ko" | wc -l) == "0" ]; then
            kldload ${KMOD_PATH}/qat_420xx_fw.ko
        fi

        # Load QAT modules
        if [ $(kldstat | grep "qat_common" | wc -l) == "0" ]; then
            kldload ${KMOD_PATH}/qat_common.ko
        fi
        if [ $(kldstat | grep "qat_hw" | wc -l) == "0" ]; then
            kldload ${KMOD_PATH}/qat_hw.ko
        fi
        if [ $(kldstat | grep "usdm_drv" | wc -l) == "0" ]; then
            kldload ${KMOD_PATH}/usdm_drv.ko
        fi

        $ADF_CTL $2 down
        $ADF_CTL $2 up

        # Check if sriov should be enabled.
        if [ $SRIOV_ENABLE == 1 ]; then
            # If a specific device ($2), or any (empty $2) is specified that is a pf, then enable sriov.
            $ADF_CTL $2 status | grep -e "^ *qat_dev" | grep -v vf > /dev/null
            if [ $? == 0 ]; then
                DO_ENABLE_SRIOV=1
            else
                DO_ENABLE_SRIOV=0
            fi
        else
            DO_ENABLE_SRIOV=0
        fi

        if [ $DO_ENABLE_SRIOV == 1 ]; then
            echo enable sriov
            enable_sriov $2
        fi

        # Show device status
        $ADF_CTL $2 status
        ;;

    Shutdown | shutdown)
        $ADF_CTL down

        # Unload CMN driver
        if [ $(kldstat | grep "usdm_drv" | wc -l) == "1" ]; then
            kldunload usdm_drv
        fi
        # Unload QAT modules
        if [ $(kldstat | grep "qat_hw" | wc -l) == "1" ]; then
            kldunload qat_hw
        fi
        if [ $(kldstat | grep "qat_common" | wc -l) == "1" ]; then
            kldunload qat_common
        fi
        # Unload FW
        if [ $(kldstat | grep "qat_dh895xcc_fw.ko" | wc -l) == "1" ]; then
            kldunload qat_dh895xcc_fw.ko
        fi
        if [ $(kldstat | grep "qat_c62x_fw.ko" | wc -l) == "1" ]; then
            kldunload qat_c62x_fw.ko
        fi
        if [ $(kldstat | grep "qat_c4xxx_fw.ko" | wc -l) == "1" ]; then
            kldunload qat_c4xxx_fw.ko
        fi
        if [ $(kldstat | grep "qat_c3xxx_fw.ko" | wc -l) == "1" ]; then
            kldunload qat_c3xxx_fw.ko
        fi
        if [ $(kldstat | grep "qat_4xxx_fw.ko" | wc -l) == "1" ]; then
            kldunload qat_4xxx_fw.ko
        fi
        if [ $(kldstat | grep "qat_420xx_fw.ko" | wc -l) == "1" ]; then
            kldunload qat_420xx_fw.ko
        fi
        if [ $(kldstat | grep "qat_200xx_fw.ko" | wc -l) == "1" ]; then
            kldunload qat_200xx_fw.ko
        fi

        ;;

    Stop | stop)
        $ADF_CTL $2 down
        ;;

    Restart | restart)
        $ADF_CTL $2 down && $ADF_CTL $2 up
        ;;

    Status | status)
        $ADF_CTL status
        if [ "$?" -ne 0 ]; then
            echo "No devices found. Please start the driver using:"
            echo "$0 start"
        fi
        ;;

    *)
        usage
        ;;

esac
exit 0
