#!/bin/bash
#################################################################
#
#   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.L.4.19.0-00005
#
#################################################################
#
### BEGIN INIT INFO
# Provides: QAT_VFs
# Required-Start:
# Required-Stop:
# Default-Start:
# Default-Stop:
# Description:  Intel QAT service for VFs
### END INIT INFO
#
# qat_service_vfs          Start/Stop the Intel QAT VFs.
#
# description: modprobe the QAT VF modules

test -f /etc/default/qat && . /etc/default/qat
SRIOV_ENABLE=${SRIOV_ENABLE-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/sbin/adf_ctl
ADF_CTL_SUPPORT=:

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


start_vfs() {
    VF_LIST=`$ADF_CTL $1 status | grep "vf," | awk '{print $1}'`
    for VF_DEV in $VF_LIST
    do
        echo Starting $VF_DEV
        $ADF_CTL $VF_DEV restart > /dev/null
    done

    SUPPORT=`$ADF_CTL_SUPPORT start`
}

stop_vfs() {
    SUPPORT=`$ADF_CTL_SUPPORT stop`
    VF_LIST=`$ADF_CTL $1 status | grep "vf," | awk '{print $1}'`
    for VF_DEV in $VF_LIST
    do
        echo Stopping $VF_DEV
        $ADF_CTL $VF_DEV down > /dev/null
    done
}

vfs_status() {
    $ADF_CTL $1 status | grep "vf,"
}

load_vf_driver() {
    # 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 -a $numDh895xDevicesVF != 0 ];then
            modprobe qat_dh895xccvf
        fi
        if [ $numC62xDevicesPF != 0 -a $numC62xDevicesVF != 0 ];then
            modprobe qat_c62xvf
        fi
        if [ $numC3xxDevicesPF != 0 -a $numC3xxDevicesVF != 0 ];then
            modprobe qat_c3xxxvf
        fi
        if [ $num200xxDevicesPF != 0 -a $num200xxDevicesVF != 0 ];then
            modprobe qat_200xxvf
        fi
        if [ $numD15xxDevicesPF != 0 -a $numD15xxDevicesVF != 0 ];then
            modprobe qat_d15xxvf
        fi
        if [ $numC4xxDevicesPF != 0 -a $numC4xxDevicesVF != 0 ];then
            modprobe qat_c4xxxvf
        fi
    fi
}

case $1 in
  Start|start)
    load_vf_driver
    start_vfs $2
            ;;

 Shutdown|shutdown)
    stop_vfs $2
    if [ "$2" == "" ]
    then
       modprobe -q -r qat_dh895xccvf
       modprobe -q -r qat_c62xvf
       modprobe -q -r qat_c3xxxvf
       modprobe -q -r qat_d15xxvf
       modprobe -q -r qat_200xxvf
       modprobe -q -r qat_c4xxxvf
    fi
            ;;

 Stop|stop)
    stop_vfs $2
            ;;

 Restart|restart)
    start_vfs $2
            ;;

 Status|status)
    $ADF_CTL status | grep "vf," &>/dev/null
    if [ "$?" -ne 0 ]
    then
        echo "No VF devices found. Please start the driver using:"
        echo "$0 start"
    else
        vfs_status $2
    fi
            ;;

 *)
      usage
            ;;

esac
exit 0

