#!/bin/bash
#
# Copyright (c) 2006 Mellanox Technologies. All rights reserved.
#
# This Software is licensed under one of the following licenses:
#
# 1) under the terms of the "Common Public License 1.0" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/cpl.php.
#
# 2) under the terms of the "The BSD License" a copy of which is
#    available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/bsd-license.php.
#
# 3) under the terms of the "GNU General Public License (GPL) Version 2" a
#    copy of which is available from the Open Source Initiative, see
#    http://www.opensource.org/licenses/gpl-license.php.
#
# Licensee has the right to choose one of the above licenses.
#
# Redistributions of source code must retain the above copyright
# notice and one of the license notices.
#
# Redistributions in binary form must reproduce both the above copyright
# notice, one of the license notices in the documentation
# and/or other materials provided with the distribution.
#
#  $Id: configure 9097 2006-08-24 06:39:09Z vlad $
#

usage()
{
cat << EOF
\`configure' configures variables for kernel modules compilation

Usage:  `basename $0` [options]

    --prefix=PREFIX  for userspace components [/usr]
    --build_root=BUILD_ROOT  root directory to build RPM packages
    --kernel-version=VERSION  make for this kernel [$(uname -r)]
    --modules-dir=DIR  modules directory [/lib/modules/$(uname -r)]
    --kernel-sources=DIR  make for this kernel [/lib/modules/$(uname -r)/source]
    --with-linux=DIR  kernel sources directory [/lib/modules/$(uname -r)/source]
    --with-linux-obj=DIR  kernel obj directory [/lib/modules/$(uname -r)/build]
    --skip-autoconf
    -j[N]|--with-njobs=[N] : Allow N configure jobs at once; jobs as number of CPUs with no arg.

    Kernel modules configuration:

    --build-dummy-mods    Build dummy modules for ULPs and storage drivers [no]

    --with-core-mod    make CONFIG_INFINIBAND=m [no]
    --without-core-mod   [yes]

    --with-ipoib-mod    make CONFIG_INFINIBAND_IPOIB=m [no]
    --without-ipoib-mod   [yes]

    --with-ipoib-cm    make CONFIG_INFINIBAND_IPOIB_CM=y [yes]
    --without-ipoib-cm   [no]

    --with-ipoib-allmulti    make CONFIG_IPOIB_ALL_MULTI=y [yes]
    --without-ipoib-allmulti   [no]

    --with-ipoib_debug-mod    make CONFIG_INFINIBAND_IPOIB_DEBUG=y [yes]
    --without-ipoib_debug-mod    [no]

    --with-ipoib_debug_data-mod    make CONFIG_INFINIBAND_IPOIB_DEBUG_DATA=y [no]
    --without-ipoib_debug_data-mod    [yes]

    --with-sdp_debug_data-mod    make CONFIG_INFINIBAND_SDP_DEBUG_DATA=y [no]
    --without-sdp_debug_data-mod    [yes]

    --with-srp-mod    make CONFIG_INFINIBAND_SRP=m [no]
    --without-srp-mod    [yes]

    --with-rxe-mod    make CONFIG_RDMA_RXE=m [no]
    --without-rxe-mod    [yes]

    --with-user_mad-mod    make CONFIG_INFINIBAND_USER_MAD=m [no]
    --without-user_mad-mod    [yes]

    --with-user_access-mod    make CONFIG_INFINIBAND_USER_ACCESS=m CONFIG_INFINIBAND_USER_ACCESS_UCM=m [no]
    --without-user_access-mod    [yes]

    --with-addr_trans-mod    make CONFIG_INFINIBAND_ADDR_TRANS=y [no]
    --without-addr_trans-mod    [yes]

    --with-mlx5-mod         make CONFIG_MLX5_CORE=y CONFIG_MLX5_INFINIBAND=y [no]
    --without-mlx5-mod      [yes]

    Options for MLX5 development usage:
        --with-mlx5-core-only-mod         Build MLX5 Core, without EN, without IB, without IPoIB
        --with-mlx5-core-and-ib-mod       Build MLX5 Core, with EN, with IB, without IPoIB)
        --with-mlx5-core-and-en-mod       Build MLX5 Core, with EN, without IB, without IPoIB)
        --with-mlx5-core-and-ib-and-en-mod  Build MLX5 Core, with EN, with IB, without IPoIB)
        --with-mlx5-core-and-ipoib-mod    Build MLX5 Core, without EN, without IB, with IPoIB)

    --with-mlx5_core-mod         make CONFIG_MLX5_CORE=y [no]
    --without-mlx5_core-mod      [yes]

    --with-mlx5_inf-mod         make CONFIG_MLX5_INFINIBAND=y [no]
    --without-mlx5_inf-mod      [yes]

    --with-mlx5_debug-mod         make CONFIG_MLX5_DEBUG=y [yes]
    --without-mlx5_debug-mod      [no]

    --with-mlxfw-mod              make CONFIG_MLXFW=m [no]
    --without-mlxfw-mod           [yes]

    --with-mlx5-ipsec             make CONFIG_MLX5_ACCEL=y CONFIG_MLX5_IPSEC=y CONFIG_MLX5_EN_IPSEC=y [no]
    --without-mlx5-ipsec          [yes]

    --with-iser-mod    make CONFIG_INFINIBAND_ISER=m [no]
    --without-iser-mod    [yes]

    --with-isert-mod    make CONFIG_INFINIBAND_ISERT=m [no]
    --without-isert-mod    [yes]

    --with-madeye-mod   make CONFIG_INFINIBAND_MADEYE=m [no]
    --without-madeye-mod    [yes]

    --with-memtrack  enable momory tracking [no]
    --without-memtrack disable memory tracking [yes]

    --with-debug-info  make CONFIG_DEBUG_INFO=y [yes]
    --without-debug-info [no]

    --with-nfsrdma-mod    make CONFIG_SUNRPC_XPRT_RDMA=m [no]
    --without-nfsrdma-mod    [yes]

    --with-scsi_transport_srp-mod    make CONFIG_SCSI_SRP_ATTRS=m [no]
    --without-scsi_transport_srp-mod    [yes]

    --with-odp             make CONFIG_INFINIBAND_ON_DEMAND_PAGING=y [no]
    --without-odp             [yes]

    --with-wqe-format             make CONFIG_INFINIBAND_WQE_FORMAT=y [no]
    --without-wqe-format             [yes]

    --with-pa-mr             make CONFIG_INFINIBAND_PA_MR=y [no]
    --without-pa-mr             [yes]

    --with-nvmf_host-mod    make CONFIG_NVME_HOST=m [no]
    --without-nvmf_host-mod    [yes]

    --with-nvmf-host-without-fc        make CONFIG_NVME_HOST_WITHOUT_FC=m [no]
    --without-nvmf-host-without-fc     [yes]

    --with-nvmf_target-mod    make CONFIG_NVME_TARGET=m [no]
    --without-nvmf_target-mod    [yes]

    --with-nvme_poll       make CONFIG_NVME_POLL=y [no]
    --without-nvme_poll    [yes]

    --block-request-module     Prevent drivers from using request_module function [no]

    --with-bf-device-emulation    make CONFIG_BF_DEVICE_EMULATION=y [no]
    --without-bf-device-emulation    [yes]

    --with-mlxdevm-mod    make CONFIG_MLXDEVM=m [no]
    --without-mlxdevm-mod    [yes]

    --with-bf-power-failure-event make CONFIG_BF_POWER_FAILURE_EVENT=y [no]
    --without-bf-power-failure-event [yes]

    --with-gds             make CONFIG_GPU_DIRECT_STORAGE=y [no]
    --without-gds             [yes]

    --with-mlx5-fs-debugfs    make CONFIG_ENABLE_MLX5_FS_DEBUGFS=y [no]

    --enable-container-build     Support driver compilation in container environment. The driver will run on Host.
    --disable-container-build     Driver compiled on container will run from container. (Default)

    --without-xdp                Do not build with XDP support.
    --help - print out options

    --with-sf-cfg-drv    make CONFIG_MLX5_SF_CFG=y [no]
    --without-sf-cfg-drv    [yes]

    --with-sf-sfc     make CONFIG_MLX5_SF_SFC=y [no]
    --without-sf-sfc        [yes]

EOF

#   Currently not supported options
#    --with-sdp-zcopy  make CONFIG_INFINIBAND_SDP_SEND_ZCOPY=y CONFIG_INFINIBAND_SDP_RECV_ZCOPY=y [no]
#    --without-sdp-zcopy [yes]
}

# Execute command w/ echo and exit if it fail
ex()
{
        echo "$@"
        if ! "$@"; then
                printf "\nFailed executing $@\n\n"
                exit 1
        fi
}

check_kerver_list()
{
	local kver=$1
	shift
	local kverlist=$@

	for i in $kverlist; do
		if echo $kver | grep -q "\b$i" ; then
			return 0
		fi
	done

	return 1
}

# Compare 2 kernel versions
check_kerver()
{
        local kver=$1
        local min_kver=$2
        shift 2

        kver_a=$(echo -n ${kver} | cut -d '.' -f 1)
        kver_b=$(echo -n ${kver} | cut -d '.' -f 2)
        kver_c=$(echo -n ${kver} | cut -d '.' -f 3 | cut -d '-' -f 1 | tr -d [:alpha:][:punct:])

        min_kver_a=$(echo -n ${min_kver} | cut -d '.' -f 1)
        min_kver_b=$(echo -n ${min_kver} | cut -d '.' -f 2)
        min_kver_c=$(echo -n ${min_kver} | cut -d '.' -f 3 | cut -d '-' -f 1 | tr -d [:alpha:][:punct:])

        if [ ${kver_a} -lt ${min_kver_a} ] ||
                [[ ${kver_a} -eq ${min_kver_a} && ${kver_b} -lt ${min_kver_b} ]] ||
                [[ ${kver_a} -eq ${min_kver_a} && ${kver_b} -eq ${min_kver_b} && ${kver_c} -lt ${min_kver_c} ]]; then
                return 1
        fi

        return 0
}

support_only_base()
{
	[ "X$OFED_BASE_KVERSION" == "X$1" ]
}

check_compat_config_h_var()
{
	local var="#define $1 1"
	grep -q "${var}" $COMPAT_CONFIG_H
}

set_complex_define_to_config_h()
{
	echo "#define $1 1" >> $COMPAT_CONFIG_H
}

unset_complex_define_to_config_h()
{
	echo "/* #undef $1 */" >> $COMPAT_CONFIG_H
}

set_config_mk_kernel_module()
{
	echo "Complex config: Set $1 to m in ${CWD}/${CONFIG}"
	echo "Complex config: Set $1 to 1 in ${AUTOCONF_H}"
	echo "$1=m" >> ${CWD}/${CONFIG}
	echo "#define $1 1" >> ${AUTOCONF_H}
}

set_config_mk_kernel()
{
	echo "Complex config: Set $1 to y in ${CWD}/${CONFIG}"
	echo "Complex config: Set $1 to 1 in ${AUTOCONF_H}"
	echo "$1=y" >> ${CWD}/${CONFIG}
	echo "#define $1 1" >> ${AUTOCONF_H}
}

unset_config_mk_kernel()
{
	echo "Complex config: Unset $1 in ${CWD}/${CONFIG}"
	echo "Complex config: Unset $1 in ${AUTOCONF_H}"
	echo "$1=" >> ${CWD}/${CONFIG}
	echo "#undef $1" >> ${AUTOCONF_H}
}

check_config_comapt_tcf_pedit_mod()
{
	RHEL_MAJOR=$(grep ^RHEL_MAJOR ${KSRC_OBJ}/Makefile | sed -n 's/.*= *\(.*\)/\1/p')
	RHEL_MINOR=$(grep ^RHEL_MINOR ${KSRC_OBJ}/Makefile | sed -n 's/.*= *\(.*\)/\1/p')
	RHEL7_4_JD=$(echo ${KVERSION} | grep 3.10.0-693.21.3)

	[[ ${RHEL_MAJOR} -eq "7" && ${RHEL_MINOR} -le "4" && ! $RHEL7_4_JD ]]
}

# We are resolving some features availability
# by combining few defines generated by autogen.sh
# extracted from compat/config.h

check_complex_defines()
{
        echo "/* Defines in this section calculated in ofed_scripts/configure " >> $COMPAT_CONFIG_H
        echo " * based on defines prior this section" >> $COMPAT_CONFIG_H

        echo " *  _________________________________________________________ */" >> $COMPAT_CONFIG_H

#define REBASE_STAGE REBASE_STAGE_UPSTREAM/REBASE_STAGE_BASE/REBASE_STAGE_BACKPORTS
	if [ "$REBASE_STAGE" = "UPSTREAM" ]; then
		set_complex_define_to_config_h REBASE_STAGE_UPSTREAM
	elif [ "$REBASE_STAGE" = "BASE" ]; then
		set_complex_define_to_config_h REBASE_STAGE_BASE
	else
		set_complex_define_to_config_h REBASE_STAGE_BACKPORTS
	fi

# Define HAVE_BASECODE_EXTRAS always set define.
# Use to wrap extra code added to base code with backports apply.
# This wrapped code compile over all kernels.
        if [ "X${CONFIG_ENABLE_BASECODE_EXTRAS}" == "Xy" ]
	then
		set_complex_define_to_config_h HAVE_BASECODE_EXTRAS
	else
		unset_complex_define_to_config_h HAVE_BASECODE_EXTRAS
	fi

# Define HAVE_TC_SETUP_FLOW_ACTION from other flags
	if check_compat_config_h_var HAVE_TC_SETUP_FLOW_ACTION_FUNC ||
	   check_compat_config_h_var HAVE_TC_SETUP_OFFLOAD_ACTION_FUNC ||
	   check_compat_config_h_var HAVE_TC_SETUP_OFFLOAD_ACTION_FUNC_HAS_3_PARAM ||
	   check_compat_config_h_var HAVE_TC_SETUP_FLOW_ACTION_WITH_RTNL_HELD
	then
		set_complex_define_to_config_h HAVE_TC_SETUP_FLOW_ACTION
	else
		unset_complex_define_to_config_h HAVE_TC_SETUP_FLOW_ACTION
	fi

# Define HAVE_HMM_RANGE_FAULT_SUPPORT from other flags
	check_autofconf CONFIG_HMM_MIRROR
	if [ "${CONFIG_HMM_MIRROR}" == "1" ]; then
		if check_compat_config_h_var HAVE_HMM_RANGE_FAULT_HAS_ONE_PARAM &&
		   check_compat_config_h_var HAVE_HMM_RANGE_HAS_HMM_PFNS &&
		   check_compat_config_h_var HAVE_HMM_PFN_TO_MAP_ORDER
		then
			set_complex_define_to_config_h HAVE_HMM_RANGE_FAULT_SUPPORT
		else
			unset_complex_define_to_config_h HAVE_HMM_RANGE_FAULT_SUPPORT
		fi
	fi

# Define HAVE_DEVLINK_HEALTH_REPORT_SUPPORT from other flags
	if check_compat_config_h_var HAVE_DEVLINK_HEALTH_REPORT_BASE_SUPPORT &&
	   (check_compat_config_h_var HAVE_DEVLINK_HEALTH_REPORTER_CREATE_4_ARGS ||
	   check_compat_config_h_var HAVE_DEVLINK_HEALTH_REPORTER_CREATE_5_ARGS)
	then
		set_complex_define_to_config_h HAVE_DEVLINK_HEALTH_REPORT_SUPPORT
	else
		unset_complex_define_to_config_h HAVE_DEVLINK_HEALTH_REPORT_SUPPORT
	fi

# Define HAVE_KTLS_RX_SUPPORT from other flags
	if (check_compat_config_h_var HAVE_TLS_OFFLOAD_RX_FORCE_RESYNC_REQUEST ||
		check_compat_config_h_var HAVE_TLS_OFFLOAD_RX_RESYNC_ASYNC_REQUEST_START) &&
	       	[ "X${CONFIG_MLX5_EN_TLS}" == "Xy" ]
	then
		set_complex_define_to_config_h HAVE_KTLS_RX_SUPPORT
	else
		unset_complex_define_to_config_h HAVE_KTLS_RX_SUPPORT
	fi

# Define HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT from other flags
	if check_compat_config_h_var HAVE_DEVLINK_PORT_ATRRS_SET_GET_5_PARAMS ||
	   check_compat_config_h_var HAVE_DEVLINK_PORT_ATRRS_SET_GET_7_PARAMS ||
	   check_compat_config_h_var HAVE_DEVLINK_PORT_ATRRS_SET_GET_2_PARAMS
	then
		set_complex_define_to_config_h HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
	else
		unset_complex_define_to_config_h HAVE_DEVLINK_PORT_ATRRS_SET_GET_SUPPORT
	fi

# Define HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET from other flags
	if check_compat_config_h_var HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET_4_PARAMS ||
	   check_compat_config_h_var HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET_2_PARAMS ||
	   check_compat_config_h_var HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET_CONTROLLER_NUM
	then
		set_complex_define_to_config_h HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET
	else
		unset_complex_define_to_config_h HAVE_DEVLINK_PORT_ATTRS_PCI_PF_SET
	fi

# Define HAVE_XSK_UMEM_CONSUME_TX_GET_2_PARAMS from other flags
	if check_compat_config_h_var HAVE_XSK_UMEM_CONSUME_TX_GET_2_PARAMS_IN_SOCK_DRV ||
	   check_compat_config_h_var HAVE_XSK_UMEM_CONSUME_TX_GET_2_PARAMS_IN_SOCK
	then
		set_complex_define_to_config_h HAVE_XSK_UMEM_CONSUME_TX_GET_2_PARAMS
	else
		unset_complex_define_to_config_h HAVE_XSK_UMEM_CONSUME_TX_GET_2_PARAMS
	fi

# Define HAVE_GET_USER_PAGES_GUP_FLAGS from other flags
	if check_compat_config_h_var HAVE_GET_USER_PAGES_4_PARAMS ||
	   check_compat_config_h_var HAVE_GET_USER_PAGES_5_PARAMS ||
	   check_compat_config_h_var HAVE_GET_USER_PAGES_7_PARAMS
	then
		set_complex_define_to_config_h HAVE_GET_USER_PAGES_GUP_FLAGS
	else
		unset_complex_define_to_config_h HAVE_GET_USER_PAGES_GUP_FLAGS
	fi

# Define HAVE_XDP_SUPPORT from other flags
        if [ "X${CONFIG_ENABLE_XDP}" == "Xy" ]
	then
		set_complex_define_to_config_h HAVE_XDP_SUPPORT
	else
		unset_complex_define_to_config_h HAVE_XDP_SUPPORT
	fi

# Define HAVE_VFIO_SUPPORT from other flags
	if check_compat_config_h_var HAVE_VFIO_PRECOPY_INFO &&
	   check_compat_config_h_var HAVE_VFIO_PCI_CORE_INIT &&
	   [ "X${CONFIG_ENABLE_VFIO}" == "Xy" ]
	then
		set_complex_define_to_config_h HAVE_VFIO_SUPPORT
	else
		unset_complex_define_to_config_h HAVE_VFIO_SUPPORT
	fi

# Define HAVE_XSK_ZERO_COPY_SUPPORT from other flags
	if (check_compat_config_h_var HAVE_XSK_UMEM_CONSUME_TX_GET_2_PARAMS ||
		check_compat_config_h_var HAVE_XSK_BUFF_ALLOC) &&
	   check_compat_config_h_var HAVE_XDP_SUPPORT
	then
		set_complex_define_to_config_h HAVE_XSK_ZERO_COPY_SUPPORT
	else
		unset_complex_define_to_config_h HAVE_XSK_ZERO_COPY_SUPPORT
	fi

# Define HAVE_XDP_CONVERT_TO_XDP_FRAME from other flags
	if check_compat_config_h_var HAVE_XDP_CONVERT_TO_XDP_FRAME_IN_NET_XDP ||
	   check_compat_config_h_var HAVE_XDP_CONVERT_TO_XDP_FRAME_IN_UEK_KABI
	then
		set_complex_define_to_config_h HAVE_XDP_CONVERT_TO_XDP_FRAME
	else
		unset_complex_define_to_config_h HAVE_XDP_CONVERT_TO_XDP_FRAME
	fi

#define HAVE_KERNEL_WITH_VXLAN_SUPPORT_ON from other flags
	check_autofconf CONFIG_VXLAN
	if [ "${CONFIG_VXLAN}" == "1" ]; then
		if check_compat_config_h_var HAVE_NDO_UDP_TUNNEL_ADD ||
		   check_compat_config_h_var HAVE_UDP_TUNNEL_NIC_INFO
		then
			set_complex_define_to_config_h HAVE_KERNEL_WITH_VXLAN_SUPPORT_ON
		else
			unset_complex_define_to_config_h HAVE_KERNEL_WITH_VXLAN_SUPPORT_ON
		fi

		if (check_compat_config_h_var HAVE_UDP_TUNNEL_NIC_INFO) &&
		   check_compat_config_h_var HAVE_UDP_TUNNEL_NIC_INFO_STATIC_IANA_VXLAN
		then
			set_complex_define_to_config_h HAVE_UDP_TUNNEL_NIC_INFO_FULL
		else
			unset_complex_define_to_config_h HAVE_UDP_TUNNEL_NIC_INFO_FULL
		fi
	fi

# Define HAVE_IS_PCI_P2PDMA_PAGE from other flags
	if check_compat_config_h_var HAVE_IS_PCI_P2PDMA_PAGE_IN_MM_H ||
	   check_compat_config_h_var HAVE_IS_PCI_P2PDMA_PAGE_IN_MEMREMAP_H
	then
		set_complex_define_to_config_h HAVE_IS_PCI_P2PDMA_PAGE
	else
		unset_complex_define_to_config_h HAVE_IS_PCI_P2PDMA_PAGE
	fi

# Define CONFIG_MLX5_MACSEC based on kernel config and HAVE_
	check_autofconf CONFIG_MACSEC
	if [ "${CONFIG_MACSEC}" == "1" ]; then
	    if check_compat_config_h_var HAVE_STRUCT_MACSEC_INFO_METADATA; then
			CONFIG_MLX5_MACSEC="y"
			set_config_mk_kernel CONFIG_MLX5_MACSEC
		else
			CONFIG_MLX5_MACSEC=
			unset_config_mk_kernel CONFIG_MLX5_MACSEC
	    fi
	fi

# Define HAVE_BLK_MQ_BUSY_TAG_ITER_FN_BOOL from other flags
	if check_compat_config_h_var HAVE_BLK_MQ_BUSY_TAG_ITER_FN_BOOL_3_PARAMS ||
	   check_compat_config_h_var HAVE_BLK_MQ_BUSY_TAG_ITER_FN_BOOL_2_PARAMS
	then
		set_complex_define_to_config_h HAVE_BLK_MQ_BUSY_TAG_ITER_FN_BOOL
	else
		unset_complex_define_to_config_h HAVE_BLK_MQ_BUSY_TAG_ITER_FN_BOOL
	fi

# Define HAVE_BLK_MQ_ALLOC_DISK from other flags
	if check_compat_config_h_var HAVE_BLK_MQ_ALLOC_DISK_3_PARAMS ||
	   check_compat_config_h_var HAVE_BLK_MQ_ALLOC_DISK_2_PARAMS
	then
		set_complex_define_to_config_h HAVE_BLK_MQ_ALLOC_DISK
	else
		unset_complex_define_to_config_h HAVE_BLK_MQ_ALLOC_DISK
	fi

# Define HAVE_BLK_ALLOC_DISK from other flags
	if check_compat_config_h_var HAVE_BLK_ALLOC_DISK_2_PARAMS ||
	   check_compat_config_h_var HAVE_BLK_ALLOC_DISK_1_PARAM
	then
		set_complex_define_to_config_h HAVE_BLK_ALLOC_DISK
	else
		unset_complex_define_to_config_h HAVE_BLK_ALLOC_DISK
	fi

# define HAVE_DEVLINK_PORT_TYPE_ETH_SET from other flags
	if check_compat_config_h_var HAVE_DEVLINK_PORT_TYPE_ETH_SET_GET_1_PARAM ||
	   check_compat_config_h_var HAVE_DEVLINK_PORT_TYPE_ETH_SET_GET_2_PARAM
	then
		set_complex_define_to_config_h HAVE_DEVLINK_PORT_TYPE_ETH_SET
	else
		unset_complex_define_to_config_h HAVE_DEVLINK_PORT_TYPE_ETH_SET
	fi

# Define HAVE_DEVLINK_PER_AUXDEV from other flags
	if check_compat_config_h_var HAVE_NET_DEVICE_HAS_DEVLINK_PORT
	then
		set_complex_define_to_config_h HAVE_DEVLINK_PER_AUXDEV
	else
		unset_complex_define_to_config_h HAVE_DEVLINK_PER_AUXDEV
	fi

# define HAVE_TC_CLS_OFFLOAD_EXTACK from other flags
	if check_compat_config_h_var HAVE_FLOW_CLS_OFFLOAD ||
	   check_compat_config_h_var HAVE_TC_CLS_OFFLOAD_EXTACK_FIX
	then
		set_complex_define_to_config_h HAVE_TC_CLS_OFFLOAD_EXTACK
	else
		unset_complex_define_to_config_h HAVE_TC_CLS_OFFLOAD_EXTACK
	fi

# define HAVE_TC_CLSFLOWER_STATS from other flags
	if check_compat_config_h_var HAVE_FLOW_CLS_OFFLOAD ||
		check_compat_config_h_var HAVE_TC_CLSFLOWER_STATS_FIX
	then
		set_complex_define_to_config_h HAVE_TC_CLSFLOWER_STATS
	else
		unset_complex_define_to_config_h HAVE_TC_CLSFLOWER_STATS
	fi

# define HAVE_TC_CLS_FLOWER_OFFLOAD_HAS_STATS_FIELD from other flags
	if check_compat_config_h_var HAVE_FLOW_CLS_OFFLOAD ||
	   check_compat_config_h_var HAVE_TC_CLS_FLOWER_OFFLOAD_HAS_STATS_FIELD_FIX
	then
		set_complex_define_to_config_h HAVE_TC_CLS_FLOWER_OFFLOAD_HAS_STATS_FIELD
	else
		unset_complex_define_to_config_h HAVE_TC_CLS_FLOWER_OFFLOAD_HAS_STATS_FIELD
	fi

# define HAVE_TC_CLS_FLOWER_OFFLOAD_COMMON from other flags
	if check_compat_config_h_var HAVE_FLOW_CLS_OFFLOAD ||
	   check_compat_config_h_var HAVE_TC_CLS_FLOWER_OFFLOAD_COMMON_FIX
	then
		set_complex_define_to_config_h HAVE_TC_CLS_FLOWER_OFFLOAD_COMMON
	else
		unset_complex_define_to_config_h HAVE_TC_CLS_FLOWER_OFFLOAD_COMMON
	fi

# define HAVE_PRIO_CHAIN_SUPPORT from other flags
	if check_compat_config_h_var HAVE_TC_CLS_FLOWER_OFFLOAD_COMMON
	then
		set_complex_define_to_config_h HAVE_PRIO_CHAIN_SUPPORT
	else
		unset_complex_define_to_config_h HAVE_PRIO_CHAIN_SUPPORT
	fi

# define HAVE_PRIO_CHAIN_SUPPORT from other flags
	if check_compat_config_h_var HAVE___TC_INDR_BLOCK_CB_REGISTER ||
	   check_compat_config_h_var HAVE___FLOW_INDR_BLOCK_CB_REGISTER ||
	   check_compat_config_h_var HAVE_FLOW_BLOCK_CB
	then
		set_complex_define_to_config_h HAVE_TC_INDR_API
	else
		unset_complex_define_to_config_h HAVE_TC_INDR_API
	fi

# Define HAVE_DEVICE_ADD_DISK_3_ARGS from other flags
       if check_compat_config_h_var HAVE_DEVICE_ADD_DISK_3_ARGS_AND_RETURN ||
	       check_compat_config_h_var HAVE_DEVICE_ADD_DISK_3_ARGS_NO_RETURN
       then
	       set_complex_define_to_config_h HAVE_DEVICE_ADD_DISK_3_ARGS
       else
	       unset_complex_define_to_config_h HAVE_DEVICE_ADD_DISK_3_ARGS
       fi

# define HAVE_TCF_PEDIT_TCFP_KEYS_EX from other flags
	if check_compat_config_h_var HAVE_TCF_PEDIT_TCFP_KEYS_EX_FIX ||
	   check_config_comapt_tcf_pedit_mod
	then
		set_complex_define_to_config_h HAVE_TCF_PEDIT_TCFP_KEYS_EX
	else
		unset_complex_define_to_config_h HAVE_TCF_PEDIT_TCFP_KEYS_EX
	fi

# define HAVE_VDPA_SUPPORT from other flags
	if check_compat_config_h_var HAVE_VDPA_SET_CONFIG_HAS_DEVICE_FEATURES
	then
		set_complex_define_to_config_h HAVE_VDPA_SUPPORT
	else
		unset_complex_define_to_config_h HAVE_VDPA_SUPPORT
	fi

# define HAVE_NET_PAGE_POOL_H from other flags
	if check_compat_config_h_var HAVE_NET_PAGE_POOL_OLD_H ||
	   check_compat_config_h_var HAVE_NET_PAGE_POOL_TYPES_H
	then
		set_complex_define_to_config_h HAVE_NET_PAGE_POOL_H
	else
		unset_complex_define_to_config_h HAVE_NET_PAGE_POOL_H
	fi

# Define HAVE_SHAMPO_SUPPORT from other flags
	if check_compat_config_h_var HAVE_NET_PAGE_POOL_H
	then
		set_complex_define_to_config_h HAVE_SHAMPO_SUPPORT
	else
		unset_complex_define_to_config_h HAVE_SHAMPO_SUPPORT
	fi

# define HAVE_PAGE_POOL_GET_DMA_ADDR from other flags
	if check_compat_config_h_var HAVE_PAGE_POOL_GET_DMA_ADDR_OLD ||
	   check_compat_config_h_var HAVE_PAGE_POOL_GET_DMA_ADDR_HELPER
	then
		set_complex_define_to_config_h HAVE_PAGE_POOL_GET_DMA_ADDR
	else
		unset_complex_define_to_config_h HAVE_PAGE_POOL_GET_DMA_ADDR
	fi

# define HAVE_PAGE_POLL_NID_CHANGED from other flags
	if check_compat_config_h_var HAVE_PAGE_POLL_NID_CHANGED_OLD ||
	   check_compat_config_h_var HAVE_PAGE_POLL_NID_CHANGED_HELPERS
	then
		set_complex_define_to_config_h HAVE_PAGE_POLL_NID_CHANGED
	else
		unset_complex_define_to_config_h HAVE_PAGE_POLL_NID_CHANGED
	fi

# define HAVE_GUP_MUST_UNSHARE_GET_3_PARAMS from other flags
	if check_compat_config_h_var HAVE_MM_GUP_MUST_UNSHARE_GET_3_PARAMS ||
	   check_compat_config_h_var HAVE_ASSERT_FAULT_LOCKED
	then
		set_complex_define_to_config_h HAVE_GUP_MUST_UNSHARE_GET_3_PARAMS
	else
		unset_complex_define_to_config_h HAVE_GUP_MUST_UNSHARE_GET_3_PARAMS
	fi

# define HAVE_PAGE_POOL_PARAM_HAS_NAPI from other flags
	if check_compat_config_h_var HAVE_PAGE_POOL_PARAMS_NAPI_TYPES_H ||
	   check_compat_config_h_var HAVE_PAGE_POOL_PARAMS_NAPI_OLD
	then
		set_complex_define_to_config_h HAVE_PAGE_POOL_PARAM_HAS_NAPI
	else
		unset_complex_define_to_config_h HAVE_PAGE_POOL_PARAM_HAS_NAPI
	fi

# define HAVE_PAGE_POOL_DEFRAG_PAGE from other flags
	if check_compat_config_h_var HAVE_PAGE_POOL_DEFRAG_PAGE_IN_PAGE_POOL_H ||
	   check_compat_config_h_var HAVE_PAGE_POOL_DEFRAG_PAGE_IN_PAGE_POOL_TYPES_H ||
	   check_compat_config_h_var HAVE_PAGE_POOL_PUT_UNREFED_PAGE
	then
		set_complex_define_to_config_h HAVE_PAGE_POOL_DEFRAG_PAGE
	else
		unset_complex_define_to_config_h HAVE_PAGE_POOL_DEFRAG_PAGE
	fi

# Define HAVE_PAGE_POOL_RELEASE_PAGE from other flags
	if check_compat_config_h_var HAVE_PAGE_POOL_RELEASE_PAGE_IN_PAGE_POOL_H ||
	   check_compat_config_h_var HAVE_PAGE_POOL_RELEASE_PAGE_IN_TYPES_H
	then
		set_complex_define_to_config_h HAVE_PAGE_POOL_RELEASE_PAGE
	else
		unset_complex_define_to_config_h HAVE_PAGE_POOL_RELEASE_PAGE
	fi

# Define HAVE_DEVLINK_FMSG_BINARY_PAIR_PUT_ARG_U32 from other flags
	if check_compat_config_h_var HAVE_DEVLINK_FMSG_BINARY_PAIR_PUT_ARG_U32_RETURN_VOID ||
	   check_compat_config_h_var HAVE_DEVLINK_FMSG_BINARY_PAIR_PUT_ARG_U32_RETURN_INT
	then
		set_complex_define_to_config_h HAVE_DEVLINK_FMSG_BINARY_PAIR_PUT_ARG_U32
	else
		unset_complex_define_to_config_h HAVE_DEVLINK_FMSG_BINARY_PAIR_PUT_ARG_U32
	fi

# Define CONFIG_MLX5_VDPA_NET based on kernel config and HAVE_VDPA_SUPPORT
	check_autofconf CONFIG_MLX5_VDPA_NET
	if [ "${CONFIG_MLX5_VDPA_NET}" == "1" ]; then
	    if check_compat_config_h_var HAVE_VDPA_SUPPORT; then
			CONFIG_MLX5_VDPA_NET_DUMMY=
			unset_config_mk_kernel CONFIG_MLX5_VDPA_NET_DUMMY
		else
			CONFIG_MLX5_VDPA_NET_DUMMY="m"
			set_config_mk_kernel CONFIG_MLX5_VDPA_NET_DUMMY
	    fi
	fi

# Define HAVE_LIGHT_SFS from other flags
        if check_compat_config_h_var HAVE_DEVL_PARAM_DRIVERINIT_VALUE_GET &&
           !(check_compat_config_h_var HAVE_DEVLINK_SET_FEATURES)
        then
                set_complex_define_to_config_h HAVE_LIGHT_SFS
        else
                unset_complex_define_to_config_h HAVE_LIGHT_SFS
        fi

# Define HAVE_DPLL_SUPPORT from other flags
        if (check_compat_config_h_var HAVE_DPLL_NETDEV_PIN_SET || 
	    check_compat_config_h_var HAVE_NETDEV_DPLL_PIN_SET) &&
           check_compat_config_h_var HAVE_DPLL_STRUCTS
        then
                set_complex_define_to_config_h HAVE_DPLL_SUPPORT
        else
                unset_complex_define_to_config_h HAVE_DPLL_SUPPORT
        fi
# Define CONFIG_MLX5_DPLL based on kernel config and HAVE_DPLL_STRUCTS
	check_autofconf CONFIG_DPLL
	if [ "${CONFIG_DPLL}" == "1" ]; then
	    if check_compat_config_h_var HAVE_DPLL_SUPPORT; then
			CONFIG_MLX5_DPLL="m"
			set_config_mk_kernel_module CONFIG_MLX5_DPLL
		else
			CONFIG_MLX5_DPLL=
			unset_config_mk_kernel CONFIG_MLX5_DPLL
	    fi
	fi
}

check_kerver_rh_cls()
{
	perl -e '($v, $r) = split "-", "'$1'"; exit($v eq "3.10.0" && $r >= 693 ? 0 : 1)'
}

check_kerver_rh_bridge()
{
	perl -e '($v, $r) = split "-", "'$1'"; exit($v eq "4.18.0" && $r >= 147 ? 0 : 1)'
}


#NVMF HOST supports RHEL7.2 and above, all kernels from 4.8 and above
check_kerver_rh_nvmf_host()
{
	perl -e '($v, $r) = split "-", "'$1'"; exit($v eq "3.10.0" && $r >= 327 ? 0 : 1)'
}

#NVMF TARGET supports RHEL7.4 and above, all kernels from 4.8 and above
check_kerver_rh_nvmf_target()
{
	perl -e '($v, $r) = split "-", "'$1'"; exit($v eq "3.10.0" && $r >= 693 ? 0 : 1)'
}

#NFS RDMA supports RHEL7.5 and above, all kernels from 4.12 and above
check_kerver_rh_nfs_rdma()
{
	perl -e '($v, $r) = split "-", "'$1'"; exit($v eq "3.10.0" && $r >= 862 ? 0 : 1)'
}

# Return true if $1 this is a RHEL8 kernel, above revision $2
check_kerver_rh8()
{
	local kernel="$1"
	local minimal="$2"

	case "$kernel" in
	*.el8.*) :;;
	*) return 1;;
	esac

	perl -e '($v, $r) = split "-", "'$kernel'"; exit($v eq "4.18.0" && $r >= '$minimal' ? 0 : 1)'
}

check_kerver_rh7()
{
	local kernel="$1"
	local minimal="$2"

	perl -e '($v, $r) = split "-", "'$kernel'"; exit($v =~ /^(3\.10\.0|4\.1[14]\.0)$/ && $r =~/el7/ && $r >= '$minimal' ? 0 : 1)'
}

# Check if the kernel we build with has an auxiliary bus module
check_inbox_auxiliary() {
	check_autofconf CONFIG_AUXILIARY_BUS
	check_autofconf CONFIG_AUXILIARY_BUS_MODULE
	[ "$CONFIG_AUXILIARY_BUS$CONFIG_AUXILIARY_BUS_MODULE" != '' ]
}

# Check if the kernel we build with has an auxiliary bus module
check_inbox_fwctl() {
	check_autofconf CONFIG_FWCTL_MLX5
	check_autofconf CONFIG_FWCTL
	[ "$CONFIG_FWCTL_MLX5$CONFIG_FWCTL" != '' ]
}

function check_autofconf {
	VAR=$1
	VALUE=$(tac ${KSRC_OBJ}/include/*/autoconf.h | grep -m1 ${VAR} | sed -ne 's/.*\([01]\)$/\1/gp')

	if [ "${!VAR}" = '' ]; then
		# VAR will only be set here. Ignore it later
		# when generating configuration
		ignored_autofconf_vars="$ignored_autofconf_vars $VAR"
	fi
	eval "export $VAR=$VALUE"
}

function add_conf {
    COMMENT=$1
    PARAM=${2%=*}

    VALUE=${2#*=}
    OICONF=${PWD}/ofed_scripts/openib.conf.tmp

    touch $OICONF
    if ! (grep -wq "^$PARAM" $OICONF > /dev/null 2>&1); then
        echo >> "${OICONF}"
        echo "${COMMENT}" >> "${OICONF}"
        echo "${PARAM}=${VALUE}" >> "${OICONF}"
    fi
}

# set defaults

SKIP_AUTOCONF=${SKIP_AUTOCONF:-0}
BUILD_DUMMY_MODS=${BUILD_DUMMY_MODS:-0}
NJOBS=1
FORCE_AUTOGEN=${FORCE_AUTOGEN:-0}
CONFIG_MEMTRACK=${CONFIG_MEMTRACK:-''}
CONFIG_DEBUG_INFO=${CONFIG_DEBUG_INFO:-'y'}
CONFIG_INFINIBAND=${CONFIG_INFINIBAND:-''}
CONFIG_INFINIBAND_CORE_DUMMY=${CONFIG_INFINIBAND_CORE_DUMMY:-''}
CONFIG_INFINIBAND_IPOIB=${CONFIG_INFINIBAND_IPOIB:-''}
CONFIG_INFINIBAND_IPOIB_CM=${CONFIG_INFINIBAND_IPOIB_CM:-''}
CONFIG_INFINIBAND_SRP=${CONFIG_INFINIBAND_SRP:-''}
CONFIG_INFINIBAND_SRP_DUMMY=${CONFIG_INFINIBAND_SRP_DUMMY:-''}
CONFIG_MLX5_VDPA_NET_DUMMY=${CONFIG_MLX5_VDPA_NET_DUMMY:-''}

CONFIG_RDMA_RXE=${CONFIG_RDMA_RXE:-''}
CONFIG_RDMA_RXE_DUMMY=${CONFIG_RDMA_RXE_DUMMY:-''}

CONFIG_INFINIBAND_USER_MAD=${CONFIG_INFINIBAND_USER_MAD:-''}
CONFIG_INFINIBAND_USER_ACCESS=${CONFIG_INFINIBAND_USER_ACCESS:-''}
CONFIG_INFINIBAND_USER_ACCESS_UCM=${CONFIG_INFINIBAND_USER_ACCESS_UCM:-''}
CONFIG_INFINIBAND_ADDR_TRANS=${CONFIG_INFINIBAND_ADDR_TRANS:-''}
CONFIG_INFINIBAND_ADDR_TRANS_CONFIGFS=''
CONFIG_INFINIBAND_USER_MEM=${CONFIG_INFINIBAND_USER_MEM:-''}

CONFIG_MLXFW=${CONFIG_MLXFW:-''}
CONFIG_MLX5_ACCEL=${CONFIG_MLX5_ACCEL:-''}
CONFIG_MLX5_EN_ARFS=${CONFIG_MLX5_EN_ARFS:-''}
CONFIG_MLX5_EN_RXNFC=${CONFIG_MLX5_EN_RXNFC:-''}
CONFIG_MLX5_EN_TLS=${CONFIG_MLX5_EN_TLS:-''}
CONFIG_MLX5_TLS=${CONFIG_MLX5_TLS:-''}
CONFIG_MLX5_IPSEC=${CONFIG_MLX5_IPSEC:-''}
CONFIG_MLX5_EN_IPSEC=${CONFIG_MLX5_EN_IPSEC:-''}
CONFIG_MLX5_CORE=${CONFIG_MLX5_CORE:-''}
CONFIG_MLX5_CORE_EN=${CONFIG_MLX5_CORE_EN:-''}
CONFIG_MLX5_CORE_EN_DCB=${CONFIG_MLX5_CORE_EN_DCB:-''}
CONFIG_MLX5_MPFS=${CONFIG_MLX5_MPFS:-''}
CONFIG_MLX5_ESWITCH=${CONFIG_MLX5_ESWITCH:-''}
CONFIG_MLX5_CLS_ACT=${CONFIG_MLX5_CLS_ACT:-''}
CONFIG_MLX5_BRIDGE=${CONFIG_MLX5_BRIDGE:-''}
CONFIG_MLX5_TC_CT=${CONFIG_MLX5_TC_CT:-''}
CONFIG_MLX5_SF=${CONFIG_MLX5_SF:-''}
CONFIG_MLX5_SF_MANAGER=${CONFIG_MLX5_SF_MANAGER:-''}
CONFIG_MLX5_TC_SAMPLE=${CONFIG_MLX5_TC_SAMPLE:-''}
CONFIG_MLX5_SW_STEERING=${CONFIG_MLX5_SW_STEERING:-''}
CONFIG_MLX5_CORE_IPOIB=${CONFIG_MLX5_CORE_IPOIB:-''}
CONFIG_MLX5_EN_SPECIAL_SQ=${CONFIG_MLX5_EN_SPECIAL_SQ:-''}
CONFIG_MLX5_INFINIBAND=${CONFIG_MLX5_INFINIBAND:-''}
CONFIG_MLX5_DEBUG=${CONFIG_MLX5_DEBUG:-''}
CONFIG_BACKPORT_LRO=${CONFIG_BACKPORT_LRO:-''}
CONFIG_AUXILIARY_BUS=${CONFIG_AUXILIARY_BUS:-'m'}
CONFIG_FWCTL=${CONFIG_FWCTL:-'m'}
CONFIG_FWCTL_MLX5=${CONFIG_FWCTL_MLX5:-'m'}
CONFIG_MLXDEVM=${CONFIG_MLXDEVM:-'m'}
CONFIG_MLX5_SF_CFG=${CONFIG_MLX5_SF_CFG:-''}
CONFIG_MLX5_SF_SFC=${CONFIG_MLX5_SF_SFC:-''}
CONFIG_MLX5_MACSEC=${CONFIG_MLX5_MACSEC:-''}

CONFIG_COMPAT_KOBJECT_BACKPORT=${CONFIG_COMPAT_KOBJECT_BACKPORT:-''}
CONFIG_COMPAT_VERSION=${CONFIG_COMPAT_VERSION:-''}
CONFIG_IPOIB_ALL_MULTI=${CONFIG_IPOIB_ALL_MULTI:-''}
CONFIG_IPOIB_VERSION=${CONFIG_IPOIB_VERSION:-''}

# From OFED 5.3 FPGA feature disabled and inbox FPGA configs unset
CONFIG_MLX5_FPGA_TLS=""
CONFIG_MLX5_FPGA_IPSEC=""
CONFIG_MLX5_FPGA=""

CONFIG_INFINIBAND_IPOIB_DEBUG=${CONFIG_INFINIBAND_IPOIB_DEBUG:-''}
CONFIG_INFINIBAND_ISER=${CONFIG_INFINIBAND_ISER:-''}
CONFIG_INFINIBAND_ISER_DUMMY=${CONFIG_INFINIBAND_ISER_DUMMY:-''}
CONFIG_INFINIBAND_ISERT=${CONFIG_INFINIBAND_ISERT:-''}
CONFIG_INFINIBAND_ISERT_DUMMY=${CONFIG_INFINIBAND_ISERT_DUMMY:-''}
CONFIG_SCSI_ISCSI_ATTRS=${CONFIG_SCSI_ISCSI_ATTRS:-''}
CONFIG_ISCSI_TCP=${CONFIG_ISCSI_TCP:-''}
CONFIG_INFINIBAND_MADEYE=${CONFIG_INFINIBAND_MADEYE:-''}
CONFIG_NVME_CORE=${CONFIG_NVME_CORE:-''}
CONFIG_NVME_HOST_WITHOUT_FC=${CONFIG_NVME_HOST_WITHOUT_FC:-''}
CONFIG_BLK_DEV_NVME=${CONFIG_BLK_DEV_NVME:-''}
CONFIG_NVME_FABRICS=${CONFIG_NVME_FABRICS:-''}
CONFIG_NVME_FC=${CONFIG_NVME_FC:-''}
CONFIG_NVME_RDMA=${CONFIG_NVME_RDMA:-''}
CONFIG_NVME_TCP=${CONFIG_NVME_TCP:-''}
CONFIG_NVME_APPLE=${CONFIG_NVME_APPLE:-''}
CONFIG_NVME_HOST_AUTH=${CONFIG_NVME_HOST_AUTH:-''}
CONFIG_NVME_COMMON=${CONFIG_NVME_COMMON:-''}
CONFIG_NVME_MULTIPATH=${CONFIG_NVME_MULTIPATH:-''}
CONFIG_NVME_HOST_DUMMY=${CONFIG_NVME_HOST_DUMMY:-''}
CONFIG_NVME_TARGET=${CONFIG_NVME_TARGET:-''}
CONFIG_NVME_TARGET_LOOP=${CONFIG_NVME_TARGET_LOOP:-''}
CONFIG_NVME_TARGET_RDMA=${CONFIG_NVME_TARGET_RDMA:-''}
CONFIG_NVME_TARGET_TCP=${CONFIG_NVME_TARGET_TCP:-''}
CONFIG_NVME_TARGET_FC=${CONFIG_NVME_TARGET_FC:-''}
CONFIG_NVME_TARGET_FCLOOP=${CONFIG_NVME_TARGET_FCLOOP:-''}
CONFIG_NVME_TARGET_DUMMY=${CONFIG_NVME_TARGET_DUMMY:-''}
CONFIG_NVME_POLL=${CONFIG_NVME_POLL:-''}

CONFIG_SUNRPC_XPRT_RDMA=${CONFIG_SUNRPC_XPRT_RDMA:-''}
CONFIG_SUNRPC_XPRT_RDMA_DUMMY=${CONFIG_SUNRPC_XPRT_RDMA_DUMMY:-''}
CONFIG_SCSI_SRP_ATTRS=${CONFIG_SCSI_SRP_ATTRS:-''}
CONFIG_INFINIBAND_IPOIB_DEBUG_DATA=${CONFIG_INFINIBAND_IPOIB_DEBUG_DATA:-''}
CONFIG_INFINIBAND_SDP_SEND_ZCOPY=${CONFIG_INFINIBAND_SDP_SEND_ZCOPY:-''}
CONFIG_INFINIBAND_SDP_RECV_ZCOPY=${CONFIG_INFINIBAND_SDP_RECV_ZCOPY:-''}
CONFIG_INFINIBAND_SDP_DEBUG=${CONFIG_INFINIBAND_SDP_DEBUG:-''}
CONFIG_INFINIBAND_SDP_DEBUG_DATA=${CONFIG_INFINIBAND_SDP_DEBUG_DATA:-''}
CONFIG_INFINIBAND_ON_DEMAND_PAGING=${CONFIG_INFINIBAND_ON_DEMAND_PAGING:-'y'}
CONFIG_INFINIBAND_WQE_FORMAT=${CONFIG_INFINIBAND_WQE_FORMAT:-''}
CONFIG_INFINIBAND_PA_MR=${CONFIG_INFINIBAND_PA_MR:-''}

CONFIG_MLNX_BLOCK_REQUEST_MODULE=${CONFIG_MLNX_BLOCK_REQUEST_MODULE:-''}

CONFIG_BF_DEVICE_EMULATION=${CONFIG_BF_DEVICE_EMULATION:-''}
CONFIG_BF_POWER_FAILURE_EVENT=${CONFIG_BF_POWER_FAILURE_EVENT:-''}

CONFIG_GPU_DIRECT_STORAGE=${CONFIG_GPU_DIRECT_STORAGE:-''}

CONFIG_ENABLE_MLX5_FS_DEBUGFS=${CONFIG_ENABLE_MLX5_FS_DEBUGFS:-''}
CONFIG_ENABLE_XDP="y"
CONFIG_ENABLE_VFIO="y"
CONFIG_ENABLE_BASECODE_EXTRAS="y"

# should be unset once backport finished
# block RHEL supports
OFED_BASE_KVERSION="6.9"
MIN_KVERSION="4.18"

# Rebase stage UPSTREAM/BASE/BACKPORTS
REBASE_STAGE="BACKPORTS"

# nfs-rdma
NFSRDMA_SUPPORTED_KVERSION="4.18.0"
NFSRDMA_NON_SUPPORTED_KVERSION_AND_ABOVE="6.11"

# nvmf
NVMF_SUPPORTED_KVERSION="4.18.0"
NVMF_NON_SUPPORTED_KVERSION_AND_ABOVE="6.11"

# iser/isert/srp/gds
ISER_SUPPORTED_KVERSION="4.18.0"
ISERT_SUPPORTED_KVERSION="4.18.0"
ISER_ISERT_NON_SUPPORTED_KVERSION_AND_ABOVE="6.11"

SRP_SUPPORTED_KVERSION="4.18.0"
SRP_NON_SUPPORTED_KVERSION_AND_ABOVE="6.11"

GDS_SUPPORTED_KVERSION="4.18.0"

# eswitch
SWITCH_NON_SUPPORTED_KVERSION_AND_ABOVE="6.11"

# auxbux
AUXBUS_NON_SUPPORTED_KVERSION_AND_ABOVE="6.11"

# cls_act
CLS_ACT_SUPPORTED_KVERSION="4.18.0"
CLS_ACT_NON_SUPPORTED_KVERSION_AND_ABOVE="6.11"

# bridge offload
BRIDGE_SUPPORTED_KVERSION="5.0"
BRIDGE_NON_SUPPORTED_KVERSION_AND_ABOVE="6.11"

#ODP
ODP_SUPPORTED_KVERSION="4.18.0"

#IPSEC
IPSEC_SUPPORTED_KVERSION="4.18.0"

#mlxdevm
MLXDEVM_SUPPORTED_KVERSION="4.18.0"

KVERSION=${KVERSION:-$(uname -r)}
ENABLE_CONTAINER_BUILD=${ENABLE_CONTAINER_BUILD:-0}

ignored_autofconf_vars=""

# Run after compat/configure failed
# Run from compat/
check_autoconf_log() {
	if [ ! -f config.log ]; then
		# Something is wrong. Hopefully there's a better
		# error message
		return
	fi
	echo ""
	echo "################ Snippet from $PWD/config.log:"
	sed -n -e '1,/Cache variables/p' config.log | tail -n 50
	echo "################ End snippet from compat/config.log"
}

print_line() {
    printf '%*s\n' "$(tput cols)" '' | tr ' ' '-'
}

main()
{
        ofed_patch_params=
        # Parsing parameters
        # When a new flag is added, it should be added to the list of flags under "--all" flag.
        # Also programmer should decide if the new flag is needed for mlnx_en, and add it
        # accordingly (--with-new-flag/--without-new-flag)
        while [ ! -z "$1" ]
        do
                case $1 in
                        --mlnx_en)
                                CONFIG_MLNX_EN=m
                                CONFIG_MLXSW_CORE=
				shift
                                set -- "--mlnx_en" "--with-mlx5-mod" "--with-mlxdevm-mod" "--with-mlx5-ipsec" "--with-mlxfw-mod" "--with-sf-cfg-drv" \
                                        "--without-memtrack" "--without-core-mod" "--without-user_mad-mod" "--without-user_access-mod" "--without-addr_trans-mod" \
                                        "--without-ipoib-mod" "--without-srp-mod" "--without-iser-mod" "--without-isert-mod" \
                                        "--without-nvmf_host-mod" "--without-nvmf_target-mod" "--without-gds" "--without-nfsrdma-mod" "$@"
				print_line
				echo "configuring MLNX_OFED_EN with the flags below:"
				echo "${@:2}"
				print_line
                        ;;
                        --all)
				shift
                                set -- "--all" "--with-memtrack" "--with-core-mod" "--with-user_mad-mod" "--with-user_access-mod" "--with-addr_trans-mod" \
                                        "--with-mlx5-mod" "--with-ipoib-mod" "--with-mlxfw-mod" "--with-srp-mod" "--with-iser-mod" "--with-isert-mod" \
                                        "--with-nvmf_host-mod" "--with-nvmf_target-mod" "--with-gds" "--with-nfsrdma-mod" \
                                        "--with-mlxdevm-mod" "--with-mlx5-ipsec" "--with-sf-cfg-drv" "$@"
				print_line
				echo "configuring MLNX_OFED with all the flags:"
				echo "${@:2}"
				print_line
                        ;;
                        -prefix | --prefix | --prefi | --pref | --pre | --pr | --p | -p)
                                shift
                                prefix=$1
                                PREFIX="--prefix ${prefix}"
                        ;;
                        -prefix=* | --prefix=* | --prefi=* | --pref=* | --pre=* | --pr=* | --p=* | -p=*)
                                prefix=`expr "x$1" : 'x[^=]*=\(.*\)'`
                                PREFIX="--prefix ${prefix}"
                        ;;
                        -kernel-version | --kernel-version | --kern-ver | --ker-ver)
                                ofed_patch_params="$ofed_patch_params $1"
                                shift
                                ofed_patch_params="$ofed_patch_params $1"
                                KVERSION=$1
                        ;;
                        -kernel-version=* | --kernel-version=* | --kern-ver=* | --ker-ver=*)
                                ofed_patch_params="$ofed_patch_params $1"
                                KVERSION=`expr "x$1" : 'x[^=]*=\(.*\)'`
                        ;;

                        -modules-dir | --modules-dir | --mod-dir)
                                shift
                                MODULES_DIR=$1
                        ;;
                        -modules-dir=* | --modules-dir=* | --mod-dir=*)
                                MODULES_DIR=`expr "x$1" : 'x[^=]*=\(.*\)'`
                        ;;
                        --skip-autoconf)
                        SKIP_AUTOCONF=1
                        ;;
                        --build-dummy-mods)
                            BUILD_DUMMY_MODS=1
                        ;;
                        -j[0-9]*)
	                        NJOBS=`expr "x$1" : 'x\-j\(.*\)'`
                        ;;
                        --with-njobs=*)
	                        NJOBS=`expr "x$1" : 'x[^=]*=\(.*\)'`
                        ;;
                        -j |--with-njobs)
				if [[ $2 =~ ^[0-9]+ ]]; then
					shift
		                        NJOBS=$1
				else
		                        NJOBS=
				fi
                        ;;
                        --force-autogen)
                        FORCE_AUTOGEN=1
                        ;;
                        -kernel-sources | --kernel-sources | --kernel-src | --kern-src | --ker-src)
                                shift
                                KSRC=$1
                        ;;
                        -kernel-sources=* | --kernel-sources=* | --kernel-src=* | --kern-src=* | --ker-src=*)
                                KSRC=`expr "x$1" : 'x[^=]*=\(.*\)'`
                        ;;
                        --with-linux)
                                shift
                                LINUX_SRC=$1
                        ;;
                        --with-linux=*)
                                LINUX_SRC=`expr "x$1" : 'x[^=]*=\(.*\)'`
                        ;;
                        --with-linux-obj)
                                shift
                                LINUX_OBJ=$1
                        ;;
                        --with-linux-obj=*)
                                LINUX_OBJ=`expr "x$1" : 'x[^=]*=\(.*\)'`
                        ;;
                        -with-git | --with-git)
                                WITH_GIT="yes"
                                if [ ! -z "$2" ] && [ "`echo -n $2 | cut -c 1`" != '-' ]; then
                                        shift
                                        GIT_BRANCH=$1
                                fi
                                ofed_patch_params="$ofed_patch_params $1 $GIT_BRANCH"
                        ;;
                        -with-git=* | --with-git=*)
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        --without-git)
                                WITH_GIT="no"
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        -with-quilt | --with-quilt)
                                ofed_patch_params="$ofed_patch_params $1"
                                if [ ! -z "$2" ] && [ "`echo -n $2 | cut -c 1`" != '-' ]; then
                                        shift
                                        ofed_patch_params="$ofed_patch_params $1"
                                fi
                        ;;
                        -with-quilt=* | --with-quilt=*)
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        --without-quilt)
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        -with-patchdir | --with-patchdir)
                                ofed_patch_params="$ofed_patch_params $1"
                                shift
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        -with-patchdir=* | --with-patchdir=*)
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        --without-patch)
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        --with-kernel-fixes)
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        --without-kernel-fixes)
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        --with-hpage-patch)
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        --without-hpage-patch)
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        --with-backport-patches)
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        --without-backport-patches)
                                ofed_patch_params="$ofed_patch_params $1"
                        ;;
                        --with-memtrack)
                            CONFIG_MEMTRACK="m"
                        ;;
                        --without-memtrack)
                            CONFIG_MEMTRACK=
                        ;;
                        --with-debug-info)
                            CONFIG_DEBUG_INFO="y"
                        ;;
                        --without-debug-info)
                            CONFIG_DEBUG_INFO=
                        ;;
                        --with-core-mod)
                            CONFIG_INFINIBAND="m"
                        ;;
                        --without-core-mod)
                            CONFIG_INFINIBAND=
                        ;;
                        --with-ipoib-mod)
                            CONFIG_INFINIBAND_IPOIB="m"
                            CONFIG_INFINIBAND_IPOIB_CM="y"
                            CONFIG_INFINIBAND_IPOIB_DEBUG="y"
                            CONFIG_BACKPORT_LRO="m"
                            add_conf "# Load IPoIB" "IPOIB_LOAD=yes"
                        ;;
                        --without-ipoib-mod)
                            CONFIG_INFINIBAND_IPOIB=
                        ;;
                        --with-ipoib-cm)
                            CONFIG_INFINIBAND_IPOIB_CM="y"
                            CONFIG_BACKPORT_LRO="m"
                        ;;
                        --without-ipoib-cm)
                            CONFIG_INFINIBAND_IPOIB_CM=
                        ;;
                        --with-ipoib-allmulti)
                            CONFIG_IPOIB_ALL_MULTI="y"
                        ;;
                        --without-ipoib-allmulti)
                            CONFIG_IPOIB_ALL_MULTI=
                        ;;
                        --with-ipoib_debug-mod)
                            CONFIG_INFINIBAND_IPOIB_DEBUG="y"
                            CONFIG_BACKPORT_LRO="m"
                        ;;
                        --without-ipoib_debug-mod)
                            CONFIG_INFINIBAND_IPOIB_DEBUG=
                        ;;
                        --with-ipoib_debug_data-mod)
                            CONFIG_INFINIBAND_IPOIB_DEBUG_DATA="y"
                        ;;
                        --without-ipoib_debug_data-mod)
                            CONFIG_INFINIBAND_IPOIB_DEBUG_DATA=
                        ;;
                        --with-sdp-zcopy)
                            CONFIG_INFINIBAND_SDP_SEND_ZCOPY="y"
                            CONFIG_INFINIBAND_SDP_RECV_ZCOPY="y"
                        ;;
                        --without-sdp-zcopy)
                            CONFIG_INFINIBAND_SDP_SEND_ZCOPY=
                            CONFIG_INFINIBAND_SDP_RECV_ZCOPY=
                        ;;
                        --with-sdp_debug-mod)
                            CONFIG_INFINIBAND_SDP_DEBUG="y"
                        ;;
                        --without-sdp_debug-mod)
                            CONFIG_INFINIBAND_SDP_DEBUG=
                        ;;
                        --with-sdp_debug_data-mod)
                            CONFIG_INFINIBAND_SDP_DEBUG_DATA="y"
                        ;;
                        --without-sdp_debug_data-mod)
                            CONFIG_INFINIBAND_SDP_DEBUG_DATA=
                        ;;
                        --with-srp-mod)
                            CONFIG_INFINIBAND_SRP="m"
                            CONFIG_SCSI_SRP_ATTRS="m"
                            add_conf "# Load SRP module" "SRP_LOAD=no"
                        ;;
                        --without-srp-mod)
                            CONFIG_INFINIBAND_SRP=
                            CONFIG_SCSI_SRP_ATTRS=
                        ;;
                        --with-rxe-mod)
                            CONFIG_RDMA_RXE="m"
                        ;;
                        --without-rxe-mod)
                            CONFIG_RDMA_RXE=
                        ;;
                        --with-user_mad-mod)
                            CONFIG_INFINIBAND_USER_MAD="m"
                            add_conf "# Load UMAD module" "UMAD_LOAD=yes"
                        ;;
                        --without-user_mad-mod)
                            CONFIG_INFINIBAND_USER_MAD=
                        ;;
                        --with-user_access-mod)
                            CONFIG_INFINIBAND_USER_ACCESS="m"
                            CONFIG_INFINIBAND_USER_ACCESS_UCM="m"
                            CONFIG_INFINIBAND_USER_MEM=y
                            add_conf "# Load UVERBS module" "UVERBS_LOAD=yes"
                        ;;
                        --without-user_access-mod)
                            CONFIG_INFINIBAND_USER_ACCESS=
                            CONFIG_INFINIBAND_USER_ACCESS_UCM=
                            CONFIG_INFINIBAND_USER_MEM=
                        ;;
                        --with-user_memory-mod)
                            CONFIG_INFINIBAND_USER_MEM=y
                        ;;
                        --without-user_memory-mod)
                            CONFIG_INFINIBAND_USER_MEM=
                        ;;
                        --with-addr_trans-mod)
                            CONFIG_INFINIBAND_ADDR_TRANS=y
                            add_conf "# Load RDMA_CM module" "RDMA_CM_LOAD=yes"
                            add_conf "# Load RDMA_UCM module" "RDMA_UCM_LOAD=yes"
                        ;;
                        --without-addr_trans-mod)
                            CONFIG_INFINIBAND_ADDR_TRANS=
                        ;;
                        --with-mlxfw-mod)
                            CONFIG_MLXFW="m"
                        ;;
                        --without-mlxfw-mod)
                            CONFIG_MLXFW=
                        ;;
			--with-mlx5-ipsec)
                            CONFIG_MLX5_ACCEL="y"
                            CONFIG_MLX5_IPSEC="y"
                            CONFIG_MLX5_EN_IPSEC="y"
                        ;;
			--without-mlx5-ipsec)
                            CONFIG_MLX5_IPSEC=
                            CONFIG_MLX5_EN_IPSEC=
                        ;;
                        --with-mlx5-mod)
                            CONFIG_MLX5_CORE="m"
                            CONFIG_MLX5_CORE_EN="y"
                            CONFIG_MLX5_CORE_EN_DCB="y"
                            CONFIG_MLX5_EN_ARFS="y"
                            CONFIG_MLX5_EN_RXNFC="y"
                            CONFIG_MLX5_MPFS="y"
                            CONFIG_MLX5_ESWITCH="y"
                            CONFIG_MLX5_CLS_ACT="y"
                            CONFIG_MLX5_BRIDGE="y"
                            CONFIG_MLX5_TC_CT="y"
			    CONFIG_MLX5_SF='y'
			    CONFIG_MLX5_SF_MANAGER='y'
                            CONFIG_MLX5_TC_SAMPLE="y"
                            CONFIG_MLX5_SW_STEERING="y"
                            CONFIG_MLX5_CORE_IPOIB="y"
                            CONFIG_MLX5_EN_SPECIAL_SQ="y"
                            CONFIG_MLX5_INFINIBAND="m"
                            CONFIG_BACKPORT_LRO="m"
                            CONFIG_MLX5_DEBUG="y"
                            CONFIG_MLX5_ACCEL="y"
                            CONFIG_MLX5_TLS="y"
                            CONFIG_MLX5_EN_TLS="y"
                            add_conf "# Load MLX5 modules" "MLX5_LOAD=yes"
                        ;;
                        --with-mlx5-core-only-mod)
                            CONFIG_MLX5_CORE="m"
                            CONFIG_MLX5_CORE_EN=
                            CONFIG_MLX5_CORE_EN_DCB=
                            CONFIG_MLX5_EN_ARFS=
                            CONFIG_MLX5_EN_RXNFC=
                            CONFIG_MLX5_EN_TLS=
                            CONFIG_MLX5_MPFS=
                            CONFIG_MLX5_ESWITCH=
                            CONFIG_MLX5_CLS_ACT=
                            CONFIG_MLX5_BRIDGE=
                            CONFIG_MLX5_TC_CT=
			    CONFIG_MLX5_SF=
			    CONFIG_MLX5_SF_MANAGER=
                            CONFIG_MLX5_TC_SAMPLE=
                            CONFIG_MLX5_SW_STEERING=
                            CONFIG_MLX5_CORE_IPOIB=
                            CONFIG_MLX5_INFINIBAND=
                            CONFIG_BACKPORT_LRO="m"
                            CONFIG_MLX5_DEBUG="y"
                            add_conf "# Load MLX5 modules" "MLX5_LOAD=yes"
                        ;;
                        --with-mlx5-core-and-ib-mod)
                            CONFIG_MLX5_CORE="m"
                            CONFIG_MLX5_CORE_EN=
                            CONFIG_MLX5_CORE_EN_DCB=
                            CONFIG_MLX5_EN_ARFS=
                            CONFIG_MLX5_EN_RXNFC=
                            CONFIG_MLX5_EN_TLS=
                            CONFIG_MLX5_MPFS=
                            CONFIG_MLX5_ESWITCH=
                            CONFIG_MLX5_CLS_ACT=
                            CONFIG_MLX5_BRIDGE=
                            CONFIG_MLX5_TC_CT=
			    CONFIG_MLX5_SF=
			    CONFIG_MLX5_SF_MANAGER=
                            CONFIG_MLX5_TC_SAMPLE=
                            CONFIG_MLX5_SW_STEERING=
                            CONFIG_MLX5_CORE_IPOIB=
                            CONFIG_MLX5_INFINIBAND="m"
                            CONFIG_BACKPORT_LRO="m"
                            CONFIG_MLX5_DEBUG="y"
                            add_conf "# Load MLX5 modules" "MLX5_LOAD=yes"
                        ;;
                        --with-mlx5-core-and-en-mod)
                            CONFIG_MLX5_CORE="m"
                            CONFIG_MLX5_CORE_EN="y"
                            CONFIG_MLX5_CORE_EN_DCB="y"
                            CONFIG_MLX5_EN_ARFS="y"
                            CONFIG_MLX5_EN_RXNFC="y"
                            CONFIG_MLX5_MPFS="y"
                            CONFIG_MLX5_ESWITCH="y"
                            CONFIG_MLX5_CLS_ACT="y"
                            CONFIG_MLX5_BRIDGE="y"
                            CONFIG_MLX5_TC_CT="y"
			    CONFIG_MLX5_SF='y'
			    CONFIG_MLX5_SF_MANAGER='y'
                            CONFIG_MLX5_TC_SAMPLE="y"
                            CONFIG_MLX5_SW_STEERING=
                            CONFIG_MLX5_CORE_IPOIB=
                            CONFIG_MLX5_EN_SPECIAL_SQ="y"
                            CONFIG_MLX5_INFINIBAND=
                            CONFIG_BACKPORT_LRO="m"
                            CONFIG_MLX5_DEBUG="y"
                            CONFIG_MLX5_ACCEL="y"
                            CONFIG_MLX5_TLS="y"
                            CONFIG_MLX5_EN_TLS="y"
                            add_conf "# Load MLX5 modules" "MLX5_LOAD=yes"
                        ;;
                        --with-mlx5-core-and-ib-and-en-mod)
                            CONFIG_MLX5_CORE="m"
                            CONFIG_MLX5_CORE_EN="y"
                            CONFIG_MLX5_CORE_EN_DCB="y"
                            CONFIG_MLX5_EN_ARFS="y"
                            CONFIG_MLX5_EN_RXNFC="y"
                            CONFIG_MLX5_MPFS="y"
                            CONFIG_MLX5_ESWITCH="y"
                            CONFIG_MLX5_CLS_ACT="y"
                            CONFIG_MLX5_BRIDGE="y"
                            CONFIG_MLX5_TC_CT="y"
			    CONFIG_MLX5_SF='y'
			    CONFIG_MLX5_SF_MANAGER='y'
                            CONFIG_MLX5_TC_SAMPLE="y"
                            CONFIG_MLX5_SW_STEERING="y"
                            CONFIG_MLX5_CORE_IPOIB=
                            CONFIG_MLX5_INFINIBAND="m"
                            CONFIG_BACKPORT_LRO="m"
                            CONFIG_MLX5_DEBUG="y"
                            CONFIG_MLX5_ACCEL="y"
                            CONFIG_MLX5_TLS="y"
                            CONFIG_MLX5_EN_TLS="y"
                            add_conf "# Load MLX5 modules" "MLX5_LOAD=yes"
                        ;;
                        --with-mlx5-core-and-ipoib-mod)
                            CONFIG_MLX5_CORE="m"
                            CONFIG_MLX5_CORE_EN=
                            CONFIG_MLX5_CORE_EN_DCB=
                            CONFIG_MLX5_EN_ARFS=
                            CONFIG_MLX5_EN_RXNFC=
                            CONFIG_MLX5_EN_TLS=
                            CONFIG_MLX5_MPFS=
                            CONFIG_MLX5_ESWITCH=
                            CONFIG_MLX5_CLS_ACT=
                            CONFIG_MLX5_BRIDGE=
                            CONFIG_MLX5_TC_CT=
			    CONFIG_MLX5_SF=
			    CONFIG_MLX5_SF_MANAGER=
                            CONFIG_MLX5_TC_SAMPLE=
                            CONFIG_MLX5_SW_STEERING=
                            CONFIG_MLX5_CORE_IPOIB="y"
                            CONFIG_MLX5_INFINIBAND="m"
                            CONFIG_BACKPORT_LRO="m"
                            CONFIG_MLX5_DEBUG="y"
                            add_conf "# Load MLX5 modules" "MLX5_LOAD=yes"
                        ;;
                        --with-mlx5_core-mod)
                            CONFIG_MLX5_CORE="m"
                            CONFIG_MLX5_CORE_EN="y"
                            CONFIG_MLX5_CORE_EN_DCB="y"
                            CONFIG_MLX5_EN_ARFS="y"
                            CONFIG_MLX5_EN_RXNFC="y"
                            CONFIG_MLX5_MPFS="y"
                            CONFIG_MLX5_ESWITCH="y"
                            CONFIG_MLX5_CLS_ACT="y"
                            CONFIG_MLX5_BRIDGE="y"
                            CONFIG_MLX5_TC_CT="y"
			    CONFIG_MLX5_SF='y'
			    CONFIG_MLX5_SF_MANAGER='y'
                            CONFIG_MLX5_TC_SAMPLE="y"
                            CONFIG_MLX5_SW_STEERING="y"
                            CONFIG_MLX5_CORE_IPOIB="y"
                            CONFIG_MLX5_EN_SPECIAL_SQ="y"
                            CONFIG_MLX5_ACCEL="y"
                            CONFIG_MLX5_TLS="y"
                            CONFIG_MLX5_EN_TLS="y"
                        ;;
                        --without-mlx5_core-mod)
                            CONFIG_MLX5_CORE=
                            CONFIG_MLX5_CORE_EN=
                            CONFIG_MLX5_CORE_EN_DCB=
                            CONFIG_MLX5_EN_ARFS=
                            CONFIG_MLX5_EN_RXNFC=
                            CONFIG_MLX5_MPFS=
                            CONFIG_MLX5_ESWITCH=
                            CONFIG_MLX5_CLS_ACT=
                            CONFIG_MLX5_BRIDGE=
                            CONFIG_MLX5_TC_CT=
			    CONFIG_MLX5_SF=
			    CONFIG_MLX5_SF_MANAGER=
                            CONFIG_MLX5_TC_SAMPLE=
                            CONFIG_MLX5_SW_STEERING=
                            CONFIG_MLX5_CORE_IPOIB=
                            CONFIG_MLX5_EN_SPECIAL_SQ=
                            CONFIG_MLX5_TLS=
                            CONFIG_MLX5_EN_TLS=
                        ;;
                        --without-mlx5-mod)
                            CONFIG_MLX5_CORE=
                            CONFIG_MLX5_CORE_EN=
                            CONFIG_MLX5_CORE_EN_DCB=
                            CONFIG_MLX5_EN_ARFS=
                            CONFIG_MLX5_EN_RXNFC=
                            CONFIG_MLX5_MPFS=
                            CONFIG_MLX5_ESWITCH=
                            CONFIG_MLX5_CLS_ACT=
                            CONFIG_MLX5_BRIDGE=
                            CONFIG_MLX5_TC_CT=
			    CONFIG_MLX5_SF=
			    CONFIG_MLX5_SF_MANAGER=
                            CONFIG_MLX5_TC_SAMPLE=
                            CONFIG_MLX5_SW_STEERING=
                            CONFIG_MLX5_CORE_IPOIB=
                            CONFIG_MLX5_EN_SPECIAL_SQ=
                            CONFIG_MLX5_INFINIBAND=
                            CONFIG_MLX5_DEBUG=
                            CONFIG_MLX5_TLS=
                            CONFIG_MLX5_EN_TLS=
                        ;;
                        --with-mlx5_inf-mod)
                            CONFIG_MLX5_CORE="m"
                            CONFIG_MLX5_CORE_EN="y"
                            CONFIG_MLX5_CORE_EN_DCB="y"
                            CONFIG_MLX5_EN_ARFS="y"
                            CONFIG_MLX5_EN_RXNFC="y"
                            CONFIG_MLX5_MPFS="y"
                            CONFIG_MLX5_ESWITCH="y"
                            CONFIG_MLX5_CLS_ACT="y"
                            CONFIG_MLX5_BRIDGE="y"
                            CONFIG_MLX5_TC_CT="y"
			    CONFIG_MLX5_SF='y'
			    CONFIG_MLX5_SF_MANAGER='y'
                            CONFIG_MLX5_TC_SAMPLE="y"
                            CONFIG_MLX5_SW_STEERING="y"
                            CONFIG_MLX5_CORE_IPOIB="y"
                            CONFIG_MLX5_INFINIBAND="m"
                        ;;
                        --without-mlx5_inf-mod)
                            CONFIG_MLX5_INFINIBAND=
                            CONFIG_MLX5_CORE_EN=
                            CONFIG_MLX5_CORE_EN_DCB=
                            CONFIG_MLX5_EN_ARFS=
                            CONFIG_MLX5_EN_RXNFC=
                            CONFIG_MLX5_MPFS=
                            CONFIG_MLX5_ESWITCH=
                            CONFIG_MLX5_CLS_ACT=
                            CONFIG_MLX5_BRIDGE=
                            CONFIG_MLX5_TC_CT=
			    CONFIG_MLX5_SF=
			    CONFIG_MLX5_SF_MANAGER=
                            CONFIG_MLX5_TC_SAMPLE=
                            CONFIG_MLX5_SW_STEERING=
                            CONFIG_MLX5_CORE_IPOIB=
                        ;;
                        --with-mlx5_debug-mod)
                            CONFIG_MLX5_DEBUG="y"
                        ;;
                        --without-mlx5_debug-mod)
                            CONFIG_MLX5_DEBUG=
                        ;;
                        --with-iser-mod)
                            CONFIG_INFINIBAND_ISER="m"
                        ;;
                        --with-isert-mod)
                            CONFIG_INFINIBAND_ISERT="m"
                        ;;
                        --with-iscsi-mod)
                            CONFIG_SCSI_ISCSI_ATTRS="m"
                            CONFIG_ISCSI_TCP="m"
                        ;;
                        --without-iser-mod)
                            CONFIG_INFINIBAND_ISER=
                            CONFIG_SCSI_ISCSI_ATTRS=
                            CONFIG_ISCSI_TCP=
                        ;;
                        --without-isert-mod)
                            CONFIG_INFINIBAND_ISERT=
                        ;;
                        --with-madeye-mod)
                            CONFIG_INFINIBAND_MADEYE=m
                        ;;
                        --without-madeye-mod)
                            CONFIG_INFINIBAND_MADEYE=
                        ;;
                        --with-nvmf_host-mod)
                            CONFIG_NVME_CORE="m"
                            CONFIG_BLK_DEV_NVME="m"
                            CONFIG_NVME_FABRICS="m"
                            CONFIG_NVME_FC="m"
                            CONFIG_NVME_RDMA="m"
                            CONFIG_NVME_TCP="m"
                            CONFIG_NVME_APPLE="m"
                            CONFIG_NVME_HOST_AUTH="y"
                            CONFIG_NVME_COMMON="m"
                            CONFIG_NVME_MULTIPATH="y"
                            CONFIG_NVME_HOST_WITHOUT_FC=
                        ;;
                        --without-nvmf_host-mod)
                            CONFIG_NVME_CORE=
                            CONFIG_BLK_DEV_NVME=
                            CONFIG_NVME_FABRICS=
                            CONFIG_NVME_FC=
                            CONFIG_NVME_RDMA=
			    CONFIG_NVME_TCP=
			    CONFIG_NVME_APPLE=
			    CONFIG_NVME_HOST_AUTH=
			    CONFIG_NVME_COMMON=
                            CONFIG_NVME_MULTIPATH=
                            CONFIG_NVME_HOST_WITHOUT_FC=
                        ;;
                        --with-nvmf-host-without-fc)
                            CONFIG_NVME_HOST_WITHOUT_FC="m"
                            CONFIG_NVME_CORE="m"
                            CONFIG_BLK_DEV_NVME="m"
                            CONFIG_NVME_FABRICS="m"
                            CONFIG_NVME_FC=
                            CONFIG_NVME_RDMA="m"
                            CONFIG_NVME_TCP="m"
                            CONFIG_NVME_APPLE="m"
                            CONFIG_NVME_HOST_AUTH="y"
                            CONFIG_NVME_COMMON="m"
                            CONFIG_NVME_MULTIPATH="y"
                            CONFIG_NVME_TARGET=
                            CONFIG_NVME_TARGET_LOOP=
                            CONFIG_NVME_TARGET_RDMA=
                            CONFIG_NVME_TARGET_TCP=
                            CONFIG_NVME_TARGET_FC=
                            CONFIG_NVME_TARGET_FCLOOP=
                        ;;
                        --without-nvmf-host-without-fc)
                            CONFIG_NVME_HOST_WITHOUT_FC=
                            CONFIG_NVME_CORE=
                            CONFIG_BLK_DEV_NVME=
                            CONFIG_NVME_FABRICS=
                            CONFIG_NVME_FC=
                            CONFIG_NVME_RDMA=
			    CONFIG_NVME_TCP=
			    CONFIG_NVME_APPLE=
			    CONFIG_NVME_HOST_AUTH=
			    CONFIG_NVME_COMMON=
                            CONFIG_NVME_MULTIPATH=
                            CONFIG_NVME_TARGET=
                            CONFIG_NVME_TARGET_LOOP=
                            CONFIG_NVME_TARGET_RDMA=
                            CONFIG_NVME_TARGET_TCP=
                            CONFIG_NVME_TARGET_FC=
                            CONFIG_NVME_TARGET_FCLOOP=
                        ;;
                        --with-nvmf_target-mod)
                            CONFIG_NVME_TARGET="m"
                            CONFIG_NVME_TARGET_LOOP="m"
                            CONFIG_NVME_TARGET_RDMA="m"
                            CONFIG_NVME_TARGET_TCP="m"
                            CONFIG_NVME_TARGET_FC="m"
                            CONFIG_NVME_TARGET_FCLOOP="m"
                            CONFIG_NVME_TARGET_COMMON="m"
                            CONFIG_NVME_HOST_WITHOUT_FC=
                        ;;
                        --without-nvmf_target-mod)
                            CONFIG_NVME_TARGET=
                            CONFIG_NVME_TARGET_LOOP=
                            CONFIG_NVME_TARGET_RDMA=
                            CONFIG_NVME_TARGET_TCP=
                            CONFIG_NVME_TARGET_FC=
                            CONFIG_NVME_TARGET_FCLOOP=
                            CONFIG_NVME_HOST_WITHOUT_FC=
                        ;;
                        --with-nvme_poll)
                        CONFIG_NVME_POLL="y"
                        ;;
                        --without-nvme_poll)
                        CONFIG_NVME_POLL=
                        ;;
                        --with-nfsrdma-mod)
                            CONFIG_SUNRPC_XPRT_RDMA="m"
                        ;;
                        --without-nfsrdma-mod)
                            CONFIG_SUNRPC_XPRT_RDMA=
                        ;;
                        --with-scsi_transport_srp-mod)
                            CONFIG_SCSI_SRP_ATTRS="m"
                        ;;
                        --without-scsi_transport_srp-mod)
                            CONFIG_SCSI_SRP_ATTRS=
                        ;;
                        --with-modprobe|--without-modprobe)
                        ;;
                        --with-odp)
                        CONFIG_INFINIBAND_ON_DEMAND_PAGING="y"
                        ;;
                        --without-odp)
                        CONFIG_INFINIBAND_ON_DEMAND_PAGING=
                        ;;
                        --with-wqe-format)
                        CONFIG_INFINIBAND_WQE_FORMAT="y"
                        ;;
                        --without-wqe-format)
                        CONFIG_INFINIBAND_WQE_FORMAT=
                        ;;
                        --with-pa-mr)
                        CONFIG_INFINIBAND_PA_MR="y"
                        ;;
                        --without-pa-mr)
                        CONFIG_INFINIBAND_PA_MR=
                        ;;
                        --with-dummy-core-mods)
                        CONFIG_INFINIBAND_CORE_DUMMY="m"
                        ;;
                        --block-request-module)
                        CONFIG_MLNX_BLOCK_REQUEST_MODULE="y"
                        ;;
                        --with-bf-device-emulation)
                        CONFIG_BF_DEVICE_EMULATION="y"
                        ;;
                        --without-bf-device-emulation)
                        CONFIG_BF_DEVICE_EMULATION=
                        ;;
                        --with-bf-power-failure-event)
                        CONFIG_BF_POWER_FAILURE_EVENT="y"
                        ;;
                        --without-bf-power-failure-event)
                        CONFIG_BF_POWER_FAILURE_EVENT=
                        ;;
                        --with-mlx5-fs-debugfs)
                        CONFIG_ENABLE_MLX5_FS_DEBUGFS="y"
                        ;;
                        --with-gds)
                        CONFIG_GPU_DIRECT_STORAGE="y"
                        ;;
                        --without-gds)
                        CONFIG_GPU_DIRECT_STORAGE=
                        ;;
                        --enable-container-build)
                        ENABLE_CONTAINER_BUILD=1
                        ;;
                        --disable-container-build)
                        ENABLE_CONTAINER_BUILD=0
                        ;;
                        --without-xdp)
			CONFIG_ENABLE_XDP=
                        ;;
                        --without-vfio)
			CONFIG_ENABLE_VFIO=
                        ;;
                        --without-basecode-extras)
			CONFIG_ENABLE_BASECODE_EXTRAS=
                        ;;
                        --with-mlxdevm-mod)
                        CONFIG_MLXDEVM="m"
                        ;;
                        --without-mlxdevm-mod)
                        CONFIG_MLXDEVM=
                        ;;
                        --with-sf-cfg-drv)
                        CONFIG_MLX5_SF_CFG="y"
                        ;;
                        --with-sf-sfc)
                        CONFIG_MLX5_SF_SFC="y"
                        ;;
                        --without-sf-sfc)
                        CONFIG_MLX5_SF_SFC=
                        ;;
                    -h | --help)
                                usage
                                exit 0
                        ;;
                        --with*)
                                echo
                                echo "Unsupported or unused parameter: $1"
                                echo
                        ;;
                        *)
                                echo
                                echo "Unsupported parameter $1"
                                echo
                        ;;
                esac
                shift

        done

MODULES_DIR=${MODULES_DIR:-/lib/modules/${KVERSION}/updates}

if [ ! -z "$LINUX_SRC" ]; then
	KSRC=$LINUX_SRC
fi

if [ ! -z "$LINUX_OBJ" ]; then
	KSRC_OBJ=$LINUX_OBJ
fi

KSRC=${KSRC:-"/lib/modules/${KVERSION}/build"}

IS_SLES=0
if [[ -e "/etc/SuSE-release" ]] || [[ -e "/etc/SUSE-brand" ]] || \
	grep -qiE 'SLES|SUSE' /etc/os-release 2>/dev/null
then
	IS_SLES=1
fi

CONFIG_IS_MARINER=
if grep -qiE 'mariner' /etc/os-release 2>/dev/null; then
       CONFIG_IS_MARINER="y"
fi

if [ -z "$KSRC_OBJ" ]; then
	if [[ "$IS_SLES" == 1 && "$KSRC" =~ "build" && -d ${KSRC/build/source} ]] ||
	   [[ "$KSRC" =~ "build" && -d ${KSRC/build/source} &&
		"X$(readlink -f $KSRC)" != "X$(readlink -f ${KSRC/build/source})" ]]; then
		KSRC_OBJ=$KSRC
		KSRC=${KSRC_OBJ/build/source}
	elif [[ "$IS_SLES" == 1 && "$KSRC" =~ "linux-obj" ]]; then
		sources_dir=$(readlink -f $KSRC 2>/dev/null | sed -e 's/-obj.*//g')
		KSRC_OBJ=$KSRC
		KSRC=${sources_dir}
	fi
fi

KSRC_OBJ=${KSRC_OBJ:-"$KSRC"}

if [[ ! -d "${KSRC}/" && -d "${KSRC_OBJ}/" ]]; then
	KSRC=$KSRC_OBJ
fi

if check_inbox_auxiliary; then
	CONFIG_AUXILIARY_BUS=
else
	CONFIG_AUXILIARY_BUS="m"
fi

if check_inbox_fwctl; then
	CONFIG_FWCTL_MLX5=
	CONFIG_FWCTL=
else
	CONFIG_FWCTL_MLX5="m"
	CONFIG_FWCTL="m"
fi

if  check_kerver ${KVERSION} ${SWITCH_NON_SUPPORTED_KVERSION_AND_ABOVE}; then
	CONFIG_MLX5_ESWITCH=
	CONFIG_MLX5_SW_STEERING=
	echo "Warning: CONFIG_MLX5_ESWITCH not support kernel version ${SWITCH_NON_SUPPORTED_KVERSION_AND_ABOVE} or higher (current: ${KVERSION})."
fi

if ! check_kerver ${KVERSION} ${CLS_ACT_SUPPORTED_KVERSION}; then
		if (! check_kerver_rh_cls ${KVERSION}) || support_only_base ${CLS_ACT_SUPPORTED_KVERSION}; then
			CONFIG_MLX5_CLS_ACT=
			CONFIG_MLX5_TC_CT=
			CONFIG_MLX5_TC_SAMPLE=
			CONFIG_MLX5_SW_STEERING=
			echo "Warning: CONFIG_MLX5_CLS_ACT requires kernel version ${CLS_ACT_SUPPORTED_KVERSION} or higher (current: ${KVERSION})."
		fi
fi

if ! check_kerver ${KVERSION} ${BRIDGE_SUPPORTED_KVERSION}; then
	if (! check_kerver_rh_bridge ${KVERSION}) || support_only_base ${BRIDGE_SUPPORTED_KVERSION}; then
                        CONFIG_MLX5_BRIDGE=
			echo "Warning: CONFIG_MLX5_BRIDGE requires kernel version ${BRIDGE_SUPPORTED_KVERSION} or higher (current: ${KVERSION})."
	fi
fi

if  check_kerver ${KVERSION} ${BRIDGE_NON_SUPPORTED_KVERSION_AND_ABOVE}; then
        CONFIG_MLX5_BRIDGE=
	echo "Warning: CONFIG_MLX5_BRIDGE not support kernel version ${BRIDGE_NON_SUPPORTED_KVERSION_AND_ABOVE} or higher (current: ${KVERSION})."
fi

if  check_kerver ${KVERSION} ${CLS_ACT_NON_SUPPORTED_KVERSION_AND_ABOVE}; then
        CONFIG_MLX5_CLS_ACT=
        CONFIG_MLX5_BRIDGE=
        CONFIG_MLX5_TC_CT=
        CONFIG_MLX5_TC_SAMPLE=
        CONFIG_MLX5_SW_STEERING=
	echo "Warning: CONFIG_MLX5_CLS_ACT not support kernel version ${CLS_ACT_NON_SUPPORTED_KVERSION_AND_ABOVE} or higher (current: ${KVERSION})."
fi

if ! check_kerver ${KVERSION} ${NFSRDMA_SUPPORTED_KVERSION}; then
	if (! check_kerver_rh_nfs_rdma ${KVERSION}) || support_only_base ${NFSRDMA_SUPPORTED_KVERSION}; then
		CONFIG_SUNRPC_XPRT_RDMA=
		echo "Warning: NFS-RMDA requires RHEL7.5 and above or kernel version ${NFSORDMA_SUPPORTED_KVERSION} or higher (current: ${KVERSION})."
	fi
fi

if  check_kerver ${KVERSION} ${NFSRDMA_NON_SUPPORTED_KVERSION_AND_ABOVE}; then
	CONFIG_SUNRPC_XPRT_RDMA=
	echo "Warning: CONFIG_SUNRPC_XPRT_RDMA not support kernel version ${NFSRDMA_NON_SUPPORTED_KVERSION_AND_ABOVE} or higher (current: ${KVERSION})."
fi

if ! check_kerver ${KVERSION} ${NVMF_SUPPORTED_KVERSION}; then
	if (! check_kerver_rh_nvmf_host ${KVERSION}) || support_only_base ${NVMF_SUPPORTED_KVERSION}; then
		CONFIG_NVME_CORE=
		CONFIG_BLK_DEV_NVME=
		CONFIG_NVME_FABRICS=
		CONFIG_NVME_FC=
		CONFIG_NVME_RDMA=
		CONFIG_NVME_TCP=
		CONFIG_NVME_APPLE=
		CONFIG_NVME_HOST_AUTH=
		CONFIG_NVME_COMMON=
		CONFIG_NVME_MULTIPATH=
		CONFIG_NVME_HOST_WITHOUT_FC=
		echo "Warning: NVMF HOST requires RHEL7.2 and above or kernel version ${NVMF_SUPPORTED_KVERSION} or higher (current: ${KVERSION})."

	fi
	if (! check_kerver_rh_nvmf_target ${KVERSION}) || support_only_base ${NVMF_SUPPORTED_KVERSION}; then
		CONFIG_NVME_TARGET=
		CONFIG_NVME_TARGET_LOOP=
		CONFIG_NVME_TARGET_RDMA=
		CONFIG_NVME_TARGET_TCP=
		CONFIG_NVME_TARGET_FC=
		CONFIG_NVME_TARGET_FCLOOP=
		echo "Warning: NVMF TARGET requires RHEL7.4 and above or kernel version ${NVMF_SUPPORTED_KVERSION} or higher (current: ${KVERSION})."
	fi
fi

if  check_kerver ${KVERSION} ${NVMF_NON_SUPPORTED_KVERSION_AND_ABOVE}; then
        CONFIG_NVME_CORE=
        CONFIG_BLK_DEV_NVME=
        CONFIG_NVME_FABRICS=
        CONFIG_NVME_FC=
        CONFIG_NVME_RDMA=
        CONFIG_NVME_TCP=
        CONFIG_NVME_APPLE=
        CONFIG_NVME_HOST_AUTH=
        CONFIG_NVME_COMMON=
        CONFIG_NVME_MULTIPATH=
        CONFIG_NVME_HOST_WITHOUT_FC=
        CONFIG_NVME_TARGET=
        CONFIG_NVME_TARGET_LOOP=
        CONFIG_NVME_TARGET_RDMA=
        CONFIG_NVME_TARGET_TCP=
        CONFIG_NVME_TARGET_FC=
        CONFIG_NVME_TARGET_FCLOOP=
        echo "Warning: NVMF not support kernel version ${NVMF_NON_SUPPORTED_KVERSION_AND_ABOVE} or higher (current: ${KVERSION})."
fi


if  check_kerver ${KVERSION} ${ISER_ISERT_NON_SUPPORTED_KVERSION_AND_ABOVE}; then
	CONFIG_INFINIBAND_ISER=
	CONFIG_SCSI_ISCSI_ATTRS=
	CONFIG_ISCSI_TCP=
	CONFIG_INFINIBAND_ISERT=
	echo "Warning: ISER/ISERT not support kernel version ${ISER_ISERT_NON_SUPPORTED_KVERSION_AND_ABOVE} or higher (current: ${KVERSION})."
fi

if ! check_kerver ${KVERSION} ${ISER_SUPPORTED_KVERSION}; then
	CONFIG_INFINIBAND_ISER=
	CONFIG_SCSI_ISCSI_ATTRS=
	CONFIG_ISCSI_TCP=
	echo "Warning: ISER requires kernel version ${ISER_SUPPORTED_KVERSION} or higher (current: ${KVERSION})."
fi

if ! check_kerver ${KVERSION} ${ISERT_SUPPORTED_KVERSION}; then
	CONFIG_INFINIBAND_ISERT=
	echo "Warning: ISERT requires kernel version ${ISERT_SUPPORTED_KVERSION} or higher (current: ${KVERSION})."
fi

if  check_kerver ${KVERSION} ${SRP_NON_SUPPORTED_KVERSION_AND_ABOVE}; then
	CONFIG_INFINIBAND_SRP=
	CONFIG_SCSI_SRP_ATTRS=
	echo "Warning: SRP not support kernel version ${SRP_NON_SUPPORTED_KVERSION_AND_ABOVE} or higher (current: ${KVERSION})."
fi

if ! check_kerver ${KVERSION} $SRP_SUPPORTED_KVERSION; then
	CONFIG_INFINIBAND_SRP=
	CONFIG_SCSI_SRP_ATTRS=
	echo "Warning: SRP requires kernel version ${SRP_SUPPORTED_KVERSION} or higher (current: ${KVERSION})."
fi

prefix=${prefix:-/usr}

ARCH=${ARCH:-$(uname -m)}

case $ARCH in
	ppc*)
	ARCH=powerpc
	;;
	i?86)
	ARCH=i386
	;;
esac

# Additional parameters to be passed to configure command
cd `dirname $0`
CWD=$(pwd)
# W/A for Debian 6 dkms issue
if [ -f "/etc/debian_version" ]; then
    case "$CWD" in
        /var/lib/dkms/*source*)
        CWD=${CWD/source/build}
        cd $CWD
        ;;
    esac
fi

CONFIG="configure.mk.kernel"
COMPAT_CONFIG_H="compat/config.h"

mkdir -p ${CWD}/include/generated
AUTOCONF_H="${CWD}/include/generated/autoconf.h"

case $KVERSION in
	2.6.16*)
	BACKPORT_INCLUDES="-I$CWD/backport_includes/2.6.16_sles10_sp3/include"
	CONFIG_COMPAT_VERSION="-2.6.16"
	CONFIG_IPOIB_VERSION="_1.5.3"
	CONFIG_COMPAT_KOBJECT_BACKPORT=y
	;;
	2.6.18*)
	BACKPORT_INCLUDES="-I$CWD/backport_includes/2.6.18-EL5.2/include"
	CONFIG_COMPAT_VERSION="-2.6.18"
	CONFIG_COMPAT_KOBJECT_BACKPORT=y
	;;
	*)
	;;
esac

#
################### disable/enable features/modules  ###########################
#

check_autofconf CONFIG_MMU_NOTIFIER
if ! check_kerver ${KVERSION} ${ODP_SUPPORTED_KVERSION} || [ "X${CONFIG_MMU_NOTIFIER}" != "X1" ]; then
	CONFIG_INFINIBAND_ON_DEMAND_PAGING=
fi

CONFIG_INFINIBAND_SRP_DUMMY=''
if [ "X${CONFIG_INFINIBAND_SRP}" == "Xm" ]; then
    check_autofconf CONFIG_PPC_PSERIES
    if [ X${CONFIG_PPC_PSERIES} == "X1" ]; then
        CONFIG_INFINIBAND_SRP_DUMMY=m
        CONFIG_INFINIBAND_SRP=
        CONFIG_SCSI_SRP_ATTRS=
    fi
fi

# Configfs requires CONFIGFS_FS to be enabled in the kernel
if [ "X${CONFIG_INFINIBAND_ADDR_TRANS}" == "Xy" ]; then
    check_autofconf CONFIGFS_FS
    if [ X${CONFIGFS_FS} == "X1" ]; then
        CONFIG_INFINIBAND_ADDR_TRANS_CONFIGFS=y
    fi
fi

# Enable nvme-tcp only when it is enabled in the kernel
if [ "X${CONFIG_NVME_TCP}" == "Xm" ]; then
    check_autofconf CONFIG_NVME_TCP
    if [ X${CONFIG_NVME_TCP} == "X1" ]; then
        CONFIG_NVME_TCP=m
    fi
fi

# Enable nvme-apple only when it is enabled in the kernel
if [ "X${CONFIG_NVME_APPLE}" == "Xm" ]; then
    check_autofconf CONFIG_NVME_APPLE
    if [ X${CONFIG_NVME_APPLE} == "X1" ]; then
        CONFIG_NVME_APPLE=m
    fi
fi

# Enable nvme-host-auth only when it is enabled in the kernel
if [ "X${CONFIG_NVME_HOST_AUTH}" == "Xy" ]; then
    check_autofconf CONFIG_NVME_HOST_AUTH
    if [ X${CONFIG_NVME_HOST_AUTH} == "X1" ]; then
        CONFIG_NVME_HOST_AUTH=y
    fi
fi

# Enable nvme-common only when it is enabled in the kernel
if [ "X${CONFIG_NVME_COMMON}" == "Xm" ]; then
    check_autofconf CONFIG_NVME_COMMON
    if [ X${CONFIG_NVME_COMMON} == "X1" ]; then
        CONFIG_NVME_COMMON=m
    fi
fi

# Enable nvmet-tcp only when it is enabled in the kernel
if [ "X${CONFIG_NVME_TARGET_TCP}" == "Xm" ]; then
    check_autofconf CONFIG_NVME_TARGET_TCP
    if [ X${CONFIG_NVME_TARGET_TCP} == "X1" ]; then
        CONFIG_NVME_TARGET_TCP=m
    fi
fi

# Enable GDS only for supported kernels
if ! check_kerver ${KVERSION} ${GDS_SUPPORTED_KVERSION}; then
	CONFIG_GPU_DIRECT_STORAGE=
fi

if [ $ENABLE_CONTAINER_BUILD -eq 0 ]; then
    if [ -e "/.dockerenv" ] || (grep -q docker /proc/self/cgroup &>/dev/null); then
        CONFIG_MLNX_BLOCK_REQUEST_MODULE=y
    fi
fi

# Enable mlxdevm only for supported kernels
if ! check_kerver ${KVERSION} ${MLXDEVM_SUPPORTED_KVERSION}; then
	CONFIG_MLXDEVM=
	CONFIG_MLX5_SF_CFG=
	CONFIG_MLX5_SF=
fi

# if SF driver is not configured disable its configuration driver as well
if [ "X${CONFIG_MLX5_SF}" == "X" ]; then
	CONFIG_MLX5_SF_CFG=
fi

check_autofconf CONFIG_NF_FLOW_TABLE
if [ "${CONFIG_NF_FLOW_TABLE}" != "1" ]; then
	CONFIG_MLX5_TC_CT=
	echo "Warning: CONFIG_MLX5_TC_CT not supported in kernel once CONFIG_NF_FLOW_TABLE not set."
fi
#
# Dummy modules for ULPs and storage:
# Used for ofa_kernel RPM build, the actual drivers will be
# shipped in a standalone RPMs
#
if [ "X$BUILD_DUMMY_MODS" == "X1" ]; then
    echo
    # iser
    CONFIG_INFINIBAND_ISER_DUMMY="m"
    CONFIG_INFINIBAND_ISER=
    CONFIG_SCSI_ISCSI_ATTRS=
    CONFIG_ISCSI_TCP=

    # isert
    CONFIG_INFINIBAND_ISERT_DUMMY="m"
    CONFIG_INFINIBAND_ISERT=

    # srp
    CONFIG_INFINIBAND_SRP_DUMMY="m"
    CONFIG_INFINIBAND_SRP=
    CONFIG_SCSI_SRP_ATTRS=

    CONFIG_SUNRPC_XPRT_RDMA_DUMMY="m"
    CONFIG_SUNRPC_XPRT_RDMA=

   # nvmf host
   CONFIG_NVME_HOST_DUMMY="m"
   CONFIG_NVME_HOST_WITHOUT_FC=
   CONFIG_NVME_CORE=
   CONFIG_BLK_DEV_NVME=
   CONFIG_NVME_FABRICS=
   CONFIG_NVME_FC=
   CONFIG_NVME_RDMA=
   CONFIG_NVME_TCP=
   CONFIG_NVME_APPLE=
   CONFIG_NVME_COMMON=
   CONFIG_NVME_MULTIPATH=

   # nvmf target
   CONFIG_NVME_TARGET_DUMMY="m"
   CONFIG_NVME_TARGET=
   CONFIG_NVME_TARGET_LOOP=
   CONFIG_NVME_TARGET_RDMA=
   CONFIG_NVME_TARGET_TCP=
   CONFIG_NVME_TARGET_FC=
   CONFIG_NVME_TARGET_FCLOOP=

   CONFIG_NVME_COMMON=

    # rxe
    CONFIG_RDMA_RXE_DUMMY="m"
    CONFIG_RDMA_RXE=
fi

# Add dummy modules for ib_sa, ib_mad and ib_addr only if they were part of
# the target kernel to mask the inbox ones.
if (grep -wq ib_sa $KSRC/drivers/infiniband/core/Makefile >/dev/null) ||
   (modinfo -k "$KVERSION" ib_sa &>/dev/null); then
    CONFIG_INFINIBAND_CORE_DUMMY="m"
fi

check_autofconf CONFIG_NET_UDP_TUNNEL
if ! [ "X${CONFIG_NET_UDP_TUNNEL}" == "X1" ]; then
    if [ "X${CONFIG_RDMA_RXE}" == "Xm" ]; then
        echo "Error: udp_tunnl not present, soft roce requires udp_tunnel. Exiting...";
        echo "rebuild kernel with CONFIG_NET_UDP_TUNNEL=m";
        exit 1;
    fi
fi


check_autofconf CONFIG_XFRM_OFFLOAD
check_autofconf CONFIG_INET_ESP_OFFLOAD
check_autofconf CONFIG_INET6_ESP_OFFLOAD
if [ "X${CONFIG_MLX5_EN_IPSEC}" == "Xy" ]; then
    ipsec_err=0
    if ! [ "X${CONFIG_XFRM_OFFLOAD}" == "X1" ]; then
        echo "Warning: ConnectX IPsec requires CONFIG_XFRM_OFFLOAD to be enabled in kernel configuration."
        ipsec_err=1
    fi
    if ! [ "X${CONFIG_INET_ESP_OFFLOAD}" == "X1" ] && ! [ "X${CONFIG_INET6_ESP_OFFLOAD}" == "X1" ]; then
        echo "Warning: ConnectX IPsec requires one of CONFIG_INET6_ESP_OFFLOAD or CONFIG_INET_ESP_OFFLOAD to be enabled in kernel configuration."
        ipsec_err=1
    fi
    if [ $ipsec_err -eq 1 ]; then
        echo "Warning: Cannot build with ConnectX IPsec support. Exiting..."
        CONFIG_MLX5_EN_IPSEC=
	CONFIG_MLX5_IPSEC=
    fi
fi

check_autofconf CONFIG_RFS_ACCEL
if [ "X${CONFIG_MLX5_EN_ARFS=}" == "Xy" ]; then
    if ! [ "X${CONFIG_RFS_ACCEL=}" == "X1" ]; then
        echo "Warning: CONFIG_RFS_ACCEL is not enabled in the kernel, cannot enable CONFIG_MLX5_EN_ARFS."
        CONFIG_MLX5_EN_ARFS=
    fi
fi

check_autofconf CONFIG_TLS_DEVICE
if [ "X${CONFIG_MLX5_EN_TLS}" == "Xy" ]; then
    if ! [ "X${CONFIG_TLS_DEVICE}" == "X1" ]; then
        echo "Warning: CONFIG_TLS_DEVICE is not enabled in the kernel, cannot enable CONFIG_MLX5_EN_TLS."
        CONFIG_MLX5_EN_TLS=
        CONFIG_MLX5_TLS=
    fi
fi

if [ "X${CONFIG_MLX5_ESWITCH}" == "X" ] || [ "X${CONFIG_MLX5_SF}" == "X" ]; then
        CONFIG_MLX5_SF_MANAGER=
	CONFIG_MLXDEVM=
fi

check_autofconf CONFIG_DCB
if [ "X${CONFIG_MLX5_CORE_EN_DCB}" == "Xy" ]; then
    if ! [ "X${CONFIG_DCB}" == "X1" ]; then
        echo "Warning: CONFIG_DCB is not enabled in the kernel, cannot enable CONFIG_MLX5_CORE_EN_DCB."
        CONFIG_MLX5_CORE_EN_DCB=
    fi
fi

if [ "X${CONFIG_INFINIBAND_ISER}" == "Xm" ]; then
    check_autofconf CONFIG_ISCSI_TCP
    if ! [ "X${CONFIG_ISCSI_TCP}" == "X1" ]; then
        echo "Warning: CONFIG_ISCSI_TCP is not enabled in the kernel, cannot enable CONFIG_INFINIBAND_ISER."
	CONFIG_INFINIBAND_ISER=
	CONFIG_SCSI_ISCSI_ATTRS=
    fi
fi

if [ "X${CONFIG_INFINIBAND_ISERT}" == "Xm" ]; then
    check_autofconf CONFIG_ISCSI_TARGET
    if ! [ "X${CONFIG_ISCSI_TARGET}" == "X1" ]; then
        echo "Warning: CONFIG_ISCSI_TARGET is not enabled in the kernel, cannot enable CONFIG_INFINIBAND_ISERT."
	CONFIG_INFINIBAND_ISERT=
    fi
fi

#
# END of disable/enable features/modules  ###########################
#

# Check for minimal supported kernel version
if ! check_kerver ${KVERSION} ${MIN_KVERSION}; then
    echo "Kernel version ${KVERSION} is less then supported kernel ${MIN_KVERSION}. Exiting..."
    exit 1
fi

if [ ! -e backports_applied ]; then
	echo "backports_applied does not exist. running ofed_patch.sh"
	ex ${CWD}/ofed_scripts/ofed_patch.sh ${ofed_patch_params}
	if [ ! -z "$CONFIG_COMPAT_VERSION" ]; then
		# if it's SLES10, then apply 2.6.18 backport patches first
		if [ "X$CONFIG_COMPAT_VERSION" == "X-2.6.16" ]; then
			ex ${CWD}/ofed_scripts/ofed_patch.sh --with-patchdir=backports-2.6.18 ${ofed_patch_params}
		fi
		ex ${CWD}/ofed_scripts/ofed_patch.sh --with-patchdir=backports${CONFIG_COMPAT_VERSION} ${ofed_patch_params}
	fi
fi

# Create configure.mk
/bin/rm -f ${CWD}/${CONFIG}
cat >> ${CWD}/${CONFIG} << EOFCONFIG
# Current working directory
CWD=${CWD}
BACKPORT_INCLUDES=${BACKPORT_INCLUDES}

# Kernel level
KVERSION=${KVERSION}
ARCH=${ARCH}
MODULES_DIR=${MODULES_DIR}
KSRC=${KSRC}
KSRC_OBJ=${KSRC_OBJ}
KLIB_BUILD=${KSRC_OBJ}
prefix=${prefix}

AUTOCONF_H=${AUTOCONF_H}

WITH_MAKE_PARAMS=${WITH_MAKE_PARAMS}

EOFCONFIG

    config_vars=""
    defined_vars=""
    for config_var in "${!CONFIG_@}"; do
        if echo "$ignored_autofconf_vars" | grep -w -q "$config_var"; then
            continue
        fi
        echo "$config_var=${!config_var}"
        config_vars="$config_vars $config_var"
        if [ "$config_var" = "CONFIG_INFINIBAND_ISER" ]; then
            config_vars="$config_vars CONFIG_SCSI_ISCSI_ATTRS CONFIG_ISCSI_TCP"
	    CONFIG_SCSI_ISCSI_ATTRS="$CONFIG_INFINIBAND_ISER"
	    CONFIG_ISCSI_TCP="$CONFIG_INFINIBAND_ISER"
        fi
        val="${!config_var}"
        case "$val" in
        [ym])
            defined_vars="$defined_vars $config_var"
            if [ "$config_var" = "CONFIG_INFINIBAND_ISER" ]; then
                defined_vars="$defined_vars CONFIG_SCSI_ISCSI_ATTRS CONFIG_ISCSI_TCP"
            fi
            ;;
        esac
        echo "$config_var=$val"
        done >>${CWD}/${CONFIG}
        echo "Created ${CONFIG}:"
        cat ${CWD}/${CONFIG}


# Create autoconf.h
cat > ${AUTOCONF_H} << EOFAUTOCONF
#ifndef __OFED_BUILD__
#include_next <generated/autoconf.h>
EOFAUTOCONF
    for config_var in $defined_vars; do
        echo "#define $config_var 1"
    done >>${AUTOCONF_H}

cat >> ${AUTOCONF_H} << EOFAUTOCONF

#else
EOFAUTOCONF
    for config_var in $config_vars; do
        echo "#undef $config_var"
    done >>${AUTOCONF_H}

    for config_var in $defined_vars; do
        echo "#define $config_var 1"
    done >>${AUTOCONF_H}
    echo '#endif' >>${AUTOCONF_H}

    echo "Created ${AUTOCONF_H}:"
    cat ${AUTOCONF_H}

    if [ $SKIP_AUTOCONF -eq 0 ]; then
        cd compat

        if [[ ! -x configure || $FORCE_AUTOGEN -eq 1 ]]; then
            ex ./autogen.sh
        fi

        /bin/cp -f Makefile.real Makefile
        /bin/cp -f Makefile.real Makefile.in

        ./configure --with-linux-obj=$KSRC_OBJ --with-linux=$KSRC --with-njobs=$NJOBS
        rc=$?
        if [ $rc -ne 0 ]; then
            check_autoconf_log
            exit 42
        fi

        cd -

	check_complex_defines
    fi
}

main $@
if [ "X$BUILD_DUMMY_MODS" == "X1" ]; then
    echo
    echo "Going to build dummy modules for ULPs and storage drivers..."
    echo
fi
