#!/bin/sh
#################################################################
#
# This file is provided under a dual BSD/GPLv2 license.  When using or
#   redistributing this file, you may do so under either license.
# 
#   GPL LICENSE SUMMARY
# 
#   Copyright(c) 2007-2022 Intel Corporation. All rights reserved.
# 
#   This program is free software; you can redistribute it and/or modify
#   it under the terms of version 2 of the GNU General Public License as
#   published by the Free Software Foundation.
# 
#   This program is distributed in the hope that it will be useful, but
#   WITHOUT ANY WARRANTY; without even the implied warranty of
#   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
#   General Public License for more details.
# 
#   You should have received a copy of the GNU General Public License
#   along with this program; if not, write to the Free Software
#   Foundation, Inc., 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
#   The full GNU General Public License is included in this distribution
#   in the file called LICENSE.GPL.
# 
#   Contact Information:
#   Intel Corporation
# 
#   BSD LICENSE
# 
#   Copyright(c) 2007-2022 Intel Corporation. All rights reserved.
#   All rights reserved.
# 
#   Redistribution and use in source and binary forms, with or without
#   modification, are permitted provided that the following conditions
#   are met:
# 
#     * Redistributions of source code must retain the above copyright
#       notice, this list of conditions and the following disclaimer.
#     * Redistributions in binary form must reproduce the above copyright
#       notice, this list of conditions and the following disclaimer in
#       the documentation and/or other materials provided with the
#       distribution.
#     * Neither the name of Intel Corporation nor the names of its
#       contributors may be used to endorse or promote products derived
#       from this software without specific prior written permission.
# 
#   THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
#   "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
#   LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
#   A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
#   OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
#   SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
#   LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
#   DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
#   THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
#   (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
#   OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# 
# 
#  version: QAT.B.3.12.0-00004
#
#################################################################
#
### 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 dependant \
#       modules, before calling the user space \
#       utility to pass configuration parameters

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

INTEL_VENDORID="8086"
DH895_DEVICE_PCI_ID="0435"
DH895_DEVICE_PCI_ID_VM="0443"
C62X_DEVICE_PCI_ID="37c8"
C62X_DEVICE_PCI_ID_VM="37c9"
C3XX_DEVICE_PCI_ID="19e2"
C3XX_DEVICE_PCI_ID_VM="19e3"
QAT_200XX_DEVICE_PCI_ID="18ee"
QAT_200XX_DEVICE_PCI_ID_VM="18ef"
D15XX_DEVICE_PCI_ID="6f54"
D15XX_DEVICE_PCI_ID_VM="6f55"
C4XX_DEVICE_PCI_ID="18a0"
C4XX_DEVICE_PCI_ID_VM="18a1"

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/bin/adf_ctl

# store the total number of each type of device
numDh895xDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$DH895_DEVICE_PCI_ID")
numDh895xDevicesVF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$DH895_DEVICE_PCI_ID_VM")
numC62xDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$C62X_DEVICE_PCI_ID")
numC62xDevicesVF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$C62X_DEVICE_PCI_ID_VM")
numC3xxDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$C3XX_DEVICE_PCI_ID")
numC3xxDevicesVF=$(pciconf -lbvec| egrep "$INTEL_VENDORID" | egrep -c "$C3XX_DEVICE_PCI_ID_VM")
num200xxDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$QAT_200XX_DEVICE_PCI_ID")
num200xxDevicesVF=$(pciconf -lbvec| egrep "$INTEL_VENDORID" | egrep -c "$QAT_200XX_DEVICE_PCI_ID_VM")
numD15xxDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$D15XX_DEVICE_PCI_ID")
numD15xxDevicesVF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$D15XX_DEVICE_PCI_ID_VM")
numC4xxDevicesPF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$C4XX_DEVICE_PCI_ID")
numC4xxDevicesVF=$(pciconf -lbvec | egrep "$INTEL_VENDORID" | egrep -c "$C4XX_DEVICE_PCI_ID_VM")

case $1 in
  Start|start)
    # First check if the modules are already installed
    # install them as necessary
    if [ `kldstat | grep "qat_common" | wc -l` == "0" ];then
        kldload qat_common
    fi
    if [ $numDh895xDevicesPF != 0 ] && [ `kldstat | grep "qat_895xcc_mmp.bin" | wc -l` == "0" ];then
        kldload qat_895xcc_mmp.bin
    fi
    if [ $numC62xDevicesPF != 0 ] && [ `kldstat | grep "qat_c62x_mmp.bin" | wc -l` == "0" ];then
        kldload qat_c62x_mmp.bin
    fi
    if [ $numC3xxDevicesPF != 0 ] && [ `kldstat | grep "qat_c3xxx_mmp.bin" | wc -l` == "0" ];then
        kldload qat_c3xxx_mmp.bin
    fi
    if [ $num200xxDevicesPF != 0 ] && [ `kldstat | grep "qat_200xx_mmp.bin" | wc -l` == "0" ];then
        kldload qat_200xx_mmp.bin
    fi
    if [ $numC4xxDevicesPF != 0 ] && [ `kldstat | grep "qat_c4xxx_mmp.bin" | wc -l` == "0" ];then
        kldload qat_c4xxx_mmp.bin
    fi
    if [ $numDh895xDevicesPF != 0 ] && [ `kldstat | grep "qat_dh895xcc" | wc -l` == "0" ];then
        kldload qat_dh895xcc
    fi
    if [ $numC62xDevicesPF != 0 ] && [ `kldstat | grep "qat_c62x" | grep -v "qat_c62x_mmp.bin" | wc -l` == "0" ];then
        kldload qat_c62x
    fi
    if [ $numC3xxDevicesPF != 0 ] && [ `kldstat | grep "qat_c3xxx" | grep -v "qat_c3xxx_mmp.bin" | wc -l` == "0" ];then
        kldload qat_c3xxx
    fi
    if [ $num200xxDevicesPF != 0 ] && [ `kldstat | grep "qat_200xx" | grep -v "qat_200xx_mmp.bin" | wc -l` == "0" ];then
        kldload qat_200xx
    fi
    if [ $numC4xxDevicesPF != 0 ] && [ `kldstat | grep "qat_c4xxx" | grep -v "qat_c4xxx_mmp.bin" | wc -l` == "0" ];then
        kldload qat_c4xxx
    fi
    if [ `kldstat | grep "usdm_drv" | wc -l` == "0" ];then
        kldload usdm_drv
    fi
    # Loading VF drivers as necessary
    # The VF devices only appear after SRIOV is enabled on the PF,
    #  therefore we can't use lspci to determine that the VF driver should be loaded.
    #  Instead, if we want SRIOV, and we have a specific PF device, then load the driver.
    if [ $SRIOV_ENABLE == 1 ]; then
        if [ $numDh895xDevicesPF != 0 -o $numDh895xDevicesVF != 0 ];then
            kldload qat_dh895xccvf
        fi
        if [ $numC62xDevicesPF != 0 -o $numC62xDevicesVF != 0 ];then
            kldload qat_c62xvf
        fi
        if [ $numC3xxDevicesPF != 0 -o $numC3xxDevicesVF != 0 ];then
            kldload qat_c3xxxvf
        fi
        if [ $num200xxDevicesPF != 0 -o $num200xxDevicesVF != 0 ];then
            kldload qat_200xxvf
        fi
        if [ $numD15xxDevicesPF != 0 -o $numD15xxDevicesVF != 0 ];then
            kldload qat_d15xxvf
        fi
        if [ $numC4xxDevicesPF != 0 -o $numC4xxDevicesVF != 0 ];then
            kldload qat_c4xxxvf
        fi
    fi

    if [ $ENABLE_KAPI == 1 ]; then
        kldload qat_api.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
    /usr/bin/adf_ctl $2 status
            ;;

 Shutdown|shutdown)
    /usr/bin/adf_ctl down

    if [ `kldstat | grep "qat_api" | wc -l` == "1" ];then
        kldunload qat_api
    fi
    if [ `kldstat | grep "qat_895xcc_mmp.bin" | wc -l` == "1" ];then
        kldunload qat_895xcc_mmp.bin
    fi
    if [ `kldstat | grep "qat_c62x_mmp.bin" | wc -l` == "1" ];then
        kldunload qat_c62x_mmp.bin
    fi
    if [ `kldstat | grep "qat_c3xxx_mmp.bin" | wc -l` == "1" ];then
        kldunload qat_c3xxx_mmp.bin
    fi
    if [ `kldstat | grep "qat_200xx_mmp.bin" | wc -l` == "1" ];then
        kldunload qat_200xx_mmp.bin
    fi
    if [ `kldstat | grep "qat_c4xxx_mmp.bin" | wc -l` == "1" ];then
        kldunload qat_c4xxx_mmp.bin
    fi
    if [ `kldstat | grep "qat_dh895xcc" | wc -l` == "1" ];then
        kldunload qat_dh895xcc
    fi
    if [ `kldstat | grep "qat_c62x" | wc -l` == "1" ];then
        kldunload qat_c62x
    fi
    if [ `kldstat | grep "qat_c3xxx" | wc -l` == "1" ];then
        kldunload qat_c3xxx
    fi
    if [ `kldstat | grep "qat_200xx" | wc -l` == "1" ];then
        kldunload qat_200xx
    fi
    if [ `kldstat | grep "qat_c4xxx" | wc -l` == "1" ];then
        kldunload qat_c4xxx
    fi
    if [ `kldstat | grep "qat_dh895xccvf" | wc -l` == "1" ];then
        kldunload qat_dh895xccvf
    fi
    if [ `kldstat | grep "qat_c62xvf" | wc -l` == "1" ];then
        kldunload qat_c62xvf
    fi
    if [ `kldstat | grep "qat_c3xxxvf" | wc -l` == "1" ];then
        kldunload qat_c3xxxvf
    fi
    if [ `kldstat | grep "qat_200xxvf" | wc -l` == "1" ];then
        kldunload qat_200xxvf
    fi
    if [ `kldstat | grep "qat_c4xxxvf" | wc -l` == "1" ];then
        kldunload qat_c4xxxvf
    fi
    if [ `kldstat | grep "qat_common" | wc -l` == "1" ];then
        kldunload qat_common
    fi
    if [ `kldstat | grep "usdm_drv" | wc -l` == "1" ]; then
        kldunload usdm_drv
    fi

            ;;

 Stop|stop)
    /usr/bin/adf_ctl $2 down
            ;;

 Restart|restart)
    /usr/bin/adf_ctl $2 down && /usr/bin/adf_ctl $2 up
            ;;

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

 *)
      usage
            ;;

esac
exit 0

