#!/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 []
	--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

	Kernel modules configuration:

	--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_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-srp-mod    make CONFIG_INFINIBAND_SRP=m [no]
	--without-srp-mod    [yes]

	--with-srp-target-mod    make CONFIG_INFINIBAND_SRPT=m [no]
	--without-srp-target-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 [no]
	--without-user_access-mod    [yes]

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

	--with-mthca-mod    make CONFIG_INFINIBAND_MTHCA=m [no]
	--without-mthca-mod    [yes]

	--with-mthca_debug-mod    make CONFIG_INFINIBAND_MTHCA_DEBUG=y [yes]
	--without-mthca_debug-mod    [no]

	--with-mlx4-mod         make CONFIG_MLX4_CORE=y CONFIG_MLX4_INFINIBAND=y [no]
	--without-mlx4-mod      [yes]

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

	--with-mlx4_core-mod         make CONFIG_MLX4_CORE=y [no]
	--without-mlx4_core-mod      [yes]

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

	--with-mlx4_en-mod         make CONFIG_MLX4_EN=y [no]
	--without-mlx4_en-mod      [yes]

	--with-mlx4_inf-mod         make CONFIG_MLX4_INFINIBAND=y [no]
	--without-mlx4_inf-mod      [yes]

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

	--with-mlx4_debug-mod         make CONFIG_MLX4_DEBUG=y [yes]
	--without-mlx4_debug-mod      [no]

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

	--with-ibscif-mod    make CONFIG_INFINIBAND_SCIF=m [no]
	--without-ibscif-mod    [yes]

	--with-ibp-server-mod    make CONFIG_IBP_SERVER=m [no]
	--without-ibp-server-mod    [yes]

	--with-ibp-debug-mod    make CONFIG_IBP_DEBUG=y [no]
	--without-ibp-debug-mod    [yes]

	--with-ipath_inf-mod    make CONFIG_INFINIBAND_IPATH=m [no]
	--without-ipath_inf-mod    [yes]

	--with-qib-mod    make CONFIG_INFINIBAND_QIB=m [no]
	--without-qib-mod    [yes]

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

	--with-rdmavt-mod    make CONFIG_INFINIBAND_RDMAVT=m [no]
	--without-rdmavt-mod    [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-ehca-mod    make CONFIG_INFINIBAND_EHCA=m [no]
	--without-ehca-mod    [yes]

	--with-rds-mod    make CONFIG_RDS=m [no]
	--without-rds-mod   [no]

	--with-rds_debug-mod    make CONFIG_RDS_DEBUG=y [no]
	--without-rds_debug-mod    [yes]

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

	--with-qlgc_vnic-mod    make CONFIG_INFINIBAND_QLGC_VNIC=m [no]
	--without-qlgc_vnic-mod    [yes]

	--with-qlgc_vnic_stats-mod    make CONFIG_INFINIBAND_QLGC_VNIC_STATS=y [no]
	--without-qlgc_vnic_stats-mod    [yes]

	--with-cxgb3-mod    make CONFIG_INFINIBAND_CXGB3=m [no]
	--without-cxgb3-mod    [yes]

	--with-cxgb3_debug-mod    make CONFIG_INFINIBAND_CXGB3_DEBUG=y [no]
	--without-cxgb3_debug-mod    [yes]

	--with-cxgb4-mod    make CONFIG_INFINIBAND_CXGB4=m [no]
	--without-cxgb4-mod    [yes]

	--with-i40iw-mod    make CONFIG_INFINIBAND_I40IW=m [no]
	--without-i40iw-mod    [yes]

	--with-nes-mod    make CONFIG_INFINIBAND_NES=m [no]
	--without-nes-mod    [yes]

	--with-ocrdma-mod    make CONFIG_INFINIBAND_OCRDMA=m [no]
	--without-ocrdma-mod    [yes]

	--with-be2net-mod    make CONFIG_BE2NET=m [no]
	--without-be2net-mod    [yes]

	--with-bnxt_en-mod    make CONFIG_BNXT=m [no]
	--without-bnxt_en-mod    [yes]

	--with-bnxt_re-mod    make CONFIG_INFINIBAND_BNXT_RE=m [no]
	--without-bnxt_re-mod    [yes]

	--with-nes_debug-mod    make CONFIG_INFINIBAND_NES_DEBUG=y [no]
	--without-nes_debug-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-vmw_pvrdma-mod    make CONFIG_INFINIBAND_VMWARE_PVRDMA=m [no]
	--without-vmw_pvrdma-mod    [yes]

	--with-qedr-mod    make CONFIG_INFINIBAND_QEDR [no]
	--without-qedr-mod    [yes]

	--with-nvme_host-mod
	--without-nvme_host-mod
	--with-nvme_host-without-fc
	--without-nvme_host-without-fc
	--with-nvme_target-mod
	--without-nvme_target-mod

	--help - print out options


EOF

}

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

# 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
}

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

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
}

main()
{
	ofed_patch_params=
	# Parsing parameters
	while [ ! -z "$1" ]
	do
		case $1 in
			-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
				;;
			--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
				KSRC=$1
				;;
			--with-linux=*)
				KSRC=`expr "x$1" : 'x[^=]*=\(.*\)'`
				;;
			--with-linux-obj)
				shift
				KSRC_OBJ=$1
				;;
			--with-linux-obj=*)
				KSRC_OBJ=`expr "x$1" : 'x[^=]*=\(.*\)'`
				;;
			-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"
				add_conf "# Load IPoIB" "IPOIB_LOAD=yes"
				add_conf "# Enable IPoIB Connected Mode" "SET_IPOIB_CM=yes"
				;;
			--without-ipoib-mod)
				CONFIG_INFINIBAND_IPOIB=
				;;
			--with-ipoib-cm)
				CONFIG_INFINIBAND_IPOIB_CM="y"
				;;
			--without-ipoib-cm)
				CONFIG_INFINIBAND_IPOIB_CM=
				;;
			--with-ipoib_debug-mod)
				CONFIG_INFINIBAND_IPOIB_DEBUG="y"
				;;
			--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-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-srp-target-mod)
				CONFIG_INFINIBAND_SRPT="m"
				add_conf "# Load SRP Target module" "SRPT_LOAD=no"
				;;
			--without-srp-target-mod)
				CONFIG_INFINIBAND_SRPT=
				;;
			--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_MEM=y
				add_conf "# Load UVERBS module" "UVERBS_LOAD=yes"
				add_conf "# Load UCM module" "UCM_LOAD=yes"
				;;
			--without-user_access-mod)
				CONFIG_INFINIBAND_USER_ACCESS=
				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-mthca-mod)
				CONFIG_INFINIBAND_MTHCA="m"
				CONFIG_INFINIBAND_MTHCA_DEBUG="y"
				add_conf "# Load MTHCA" "MTHCA_LOAD=no"
				;;
			--without-mthca-mod)
				CONFIG_INFINIBAND_MTHCA=
				;;
			--with-mthca_debug-mod)
				CONFIG_INFINIBAND_MTHCA_DEBUG="y"
				;;
			--without-mthca_debug-mod)
				CONFIG_INFINIBAND_MTHCA_DEBUG=
				;;
			--with-mlx4-mod)
				CONFIG_MLX4_CORE="m"
				CONFIG_MLX4_INFINIBAND="m"
				CONFIG_MLX4_DEBUG="y"
				add_conf "# Load MLX4 modules" "MLX4_LOAD=yes"
				;;
			--with-mlx5-mod)
				CONFIG_MLX5_CORE="m"
				CONFIG_MLX5_CORE_EN="y"
				CONFIG_MLX5_CORE_EN_DCB="y"
				CONFIG_MLX5_INFINIBAND="m"
				CONFIG_BACKPORT_LRO="m"
				CONFIG_MLX5_DEBUG="y"
				CONFIG_MLX5_ESWITCH="y"
				CONFIG_MLXFW="m"
				add_conf "# Load MLX5 modules" "MLX5_LOAD=yes"
				;;
				--with-mlxfw-mod)
					CONFIG_MLXFW="m"
				;;
				--without-mlxfw-mod)
					CONFIG_MLXFW=
				;;
			--with-mlx4_core-mod)
				CONFIG_MLX4_CORE="m"
				;;
			--without-mlx4_core-mod)
				CONFIG_MLX4_CORE=
				;;
			--with-mlx5_core-mod)
				CONFIG_MLX5_CORE="m"
				CONFIG_MLX5_CORE_EN="y"
				CONFIG_MLX5_CORE_EN_DCB="y"
				CONFIG_MLX5_ESWITCH="y"
				;;
			--without-mlx5_core-mod)
				CONFIG_MLX5_CORE=
				CONFIG_MLX5_CORE_EN=
				CONFIG_MLX5_CORE_EN_DCB=
				CONFIG_MLX5_ESWITCH=
				;;
			--without-mlx4-mod)
				CONFIG_MLX4_CORE=
				CONFIG_MLX4_INFINIBAND=
				CONFIG_MLX4_DEBUG=
				;;
			--without-mlx5-mod)
				CONFIG_MLX5_CORE=
				CONFIG_MLX5_CORE_EN=
				CONFIG_MLX5_CORE_EN_DCB=
				CONFIG_MLX5_INFINIBAND=
				CONFIG_MLX5_DEBUG=
				CONFIG_MLX5_ESWITCH=
				;;
			--with-mlx4_en-mod)
				CONFIG_MLX4_CORE="m"
				CONFIG_MLX4_EN="m"
				add_conf "# Load MLX4_EN module" "MLX4_EN_LOAD=yes"
				;;
			--without-mlx4_en-mod)
				CONFIG_MLX4_EN=
				;;
			--with-mlx4_inf-mod)
				CONFIG_MLX4_CORE="m"
				CONFIG_MLX4_INFINIBAND="m"
				;;
			--without-mlx4_inf-mod)
				CONFIG_MLX4_INFINIBAND=
				;;
			--with-mlx5_inf-mod)
				CONFIG_MLX5_CORE="m"
				CONFIG_MLX5_CORE_EN="y"
				CONFIG_MLX5_CORE_EN_DCB="y"
				CONFIG_MLX5_INFINIBAND="m"
				;;
			--without-mlx5_inf-mod)
				CONFIG_MLX5_INFINIBAND=
				CONFIG_MLX5_CORE_EN=
				CONFIG_MLX5_CORE_EN_DCB=
				;;
			--with-mlx4_debug-mod)
				CONFIG_MLX4_DEBUG="y"
				;;
			--without-mlx4_debug-mod)
				CONFIG_MLX4_DEBUG=
				;;
			--with-mlx5_debug-mod)
				CONFIG_MLX5_DEBUG="y"
				;;
			--without-mlx5_debug-mod)
				CONFIG_MLX5_DEBUG=
				;;
			--with-ibscif-mod)
				CONFIG_INFINIBAND_SCIF="m"
				;;
			--with-ibp-server-mod)
				CONFIG_IBP_SERVER="m"
				;;
			--with-ibp-debug-mod)
				CONFIG_IBP_DEBUG="y"
				;;
			--with-ipath_inf-mod)
				CONFIG_INFINIBAND_IPATH="m"
				add_conf "# Load IPATH" "IPATH_LOAD=yes"
				;;
			--without-ipath_inf-mod)
				CONFIG_INFINIBAND_IPATH=
				;;
			--with-rxe-mod)
				CONFIG_RDMA_RXE="m"
				;;
			--without-rxe-mod)
				CONFIG_RDMA_RXE=
				;;
			--with-rdmavt-mod)
				CONFIG_INFINIBAND_RDMAVT="m"
				;;
			--without-rdmavt-mod)
				CONFIG_INFINIBAND_RDMAVT=
				;;
			--with-qib-mod)
				CONFIG_INFINIBAND_QIB="m"
				add_conf "# Load QIB" "QIB_LOAD=yes"
				add_conf "# QIB QME BP VER" "QIB_QME_BPVER=1"
				;;
			--without-qib-mod)
				CONFIG_INFINIBAND_QIB=
				;;
			--with-iser-mod)
				CONFIG_INFINIBAND_ISER="m"
				add_conf "# Load ISER module" "ISER_LOAD=no"
				;;
			--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=
				;;
			--with-isert-mod)
				CONFIG_INFINIBAND_ISERT="m"
				;;
			--without-isert-mod)
				CONFIG_INFINIBAND_ISERT=
				;;
			--with-ehca-mod)
				CONFIG_INFINIBAND_EHCA="m"
				CONFIG_INFINIBAND_EHCA_SCALING="y"
				add_conf "$ Load eHCA" "EHCA_LAOD=yes"
				;;
			--without-ehca-mod)
				CONFIG_INFINIBAND_EHCA=
				CONFIG_INFINIBAND_EHCA_SCALING=
				;;
			--with-rds-mod)
				CONFIG_RDS="m"
				CONFIG_RDS_RDMA="m"
				CONFIG_RDS_TCP="m"
				add_conf "# Load RDS module" "RDS_LOAD=no"
				;;
			--without-rds-mod)
				CONFIG_RDS=
				CONFIG_RDS_RDMA=
				CONFIG_RDS_TCP=
				;;
			--with-rds_debug-mod)
				CONFIG_RDS_DEBUG="y"
				;;
			--without-rds_debug-mod)
				CONFIG_RDS_DEBUG=
				;;
			--with-madeye-mod)
				CONFIG_INFINIBAND_MADEYE=m
				;;
			--without-madeye-mod)
				CONFIG_INFINIBAND_MADEYE=
				;;
			--with-qlgc_vnic-mod)
				CONFIG_INFINIBAND_QLGC_VNIC="m"
				add_conf "# Load QLogic VNIC module" "QLGC_VNIC_LOAD=yes"
				;;
			--without-qlgc_vnic-mod)
				CONFIG_INFINIBAND_QLGC_VNIC=
				;;
			--with-qlgc_vnic_stats-mod)
				CONFIG_INFINIBAND_QLGC_VNIC_STATS="y"
				;;
			--without-qlgc_vnic_stats-mod)
				CONFIG_INFINIBAND_QLGC_VNIC_STATS=
				;;
			--with-cxgb3-mod)
				CONFIG_INFINIBAND_CXGB3="m"
				CONFIG_CHELSIO_T3="m"
				add_conf "# Load CXGB3 modules" "CXGB3_LOAD=yes"
				;;
			--without-cxgb3-mod)
				CONFIG_INFINIBAND_CXGB3=
				CONFIG_CHELSIO_T3=
				;;
			--with-cxgb3_debug-mod)
				CONFIG_INFINIBAND_CXGB3_DEBUG="y"
				;;
			--without-cxgb3_debug-mod)
				CONFIG_INFINIBAND_CXGB3_DEBUG=
				;;
			--with-cxgb4-mod)
				CONFIG_INFINIBAND_CXGB4="m"
				CONFIG_CHELSIO_T4="m"
				add_conf "# Load CXGB4 modules" "CXGB4_LOAD=yes"
				;;
			--without-cxgb4-mod)
				CONFIG_INFINIBAND_CXGB4=
				CONFIG_CHELSIO_T4=
				;;
			--with-cxgb3i-mod)
				CONFIG_SCSI_CXGB3_ISCSI="m"
				;;
			--without-cxgb3i-mod)
				CONFIG_SCSI_CXGB3_ISCSI=
				;;
			--with-cxgb4i-mod)
				CONFIG_SCSI_CXGB4_ISCSI="m"
				;;
			--without-cxgb4i-mod)
				CONFIG_SCSI_CXGB4_ISCSI=
				;;
			--with-i40iw-mod)
				CONFIG_INFINIBAND_I40IW="m"
				CONFIG_I40E="m"
				add_conf "# Load I40IW modules" "I40IW_LOAD=yes"
				;;
			--without-i40iw-mod)
				CONFIG_INFINIBAND_I40IW=
				CONFIG_I40E=
				add_conf "# Load I40IW modules" "I40IW_LOAD=no"
				;;
			--with-nes-mod)
				CONFIG_INFINIBAND_NES="m"
				add_conf "# Load NES modules" "NES_LOAD=yes"
				;;
			--without-nes-mod)
				CONFIG_INFINIBAND_NES=
				;;
			--with-nes_debug-mod)
				CONFIG_INFINIBAND_NES_DEBUG="y"
				;;
			--without-nes_debug-mod)
				CONFIG_INFINIBAND_NES_DEBUG=
				;;
			--with-ocrdma-mod)
				CONFIG_INFINIBAND_OCRDMA="m"
				CONFIG_BE2NET="m"
				add_conf "# Load OCRDMA modules" "OCRDMA_LOAD=yes"
				;;
			--without-ocrdma-mod)
				CONFIG_INFINIBAND_OCRDMA=
				;;
			--with-be2net-mod)
				CONFIG_BE2NET="m"
				;;
			--without-be2net-mod)
				CONFIG_BE2NET=
				;;
			--with-bnxt_re-mod)
				CONFIG_INFINIBAND_BNXT_RE="m"
				CONFIG_BNXT="m"
				add_conf "# Load BNXT_RE modules" "BNXT_RE_LOAD=yes"
				;;
			--without-bnxt_re-mod)
				CONFIG_INFINIBAND_BNXT_RE=
				;;
			--with-bnxt_en-mod)
				CONFIG_BNXT="m"
				CONFIG_BNXT_DCB="y"
				;;
			--without-bnxt_en-mod)
				CONFIG_BNXT=
				;;
			--with-nvme_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_MULTIPATH=
				CONFIG_NVME_HOST_WITHOUT_FC=
				;;
			--without-nvme_host-mod)
				CONFIG_NVME_CORE=
				CONFIG_BLK_DEV_NVME=
				CONFIG_NVME_FABRICS=
				CONFIG_NVME_FC=
				CONFIG_NVME_RDMA=
				CONFIG_NVME_MULTIPATH=
				CONFIG_NVME_HOST_WITHOUT_FC=
				;;
			--with-nvme_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_MULTIPATH=
				CONFIG_NVME_TARGET=
				CONFIG_NVME_TARGET_LOOP=
				CONFIG_NVME_TARGET_RDMA=
				CONFIG_NVME_TARGET_FC=
				CONFIG_NVME_TARGET_FCLOOP=
				;;
			--without-nvme_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_MULTIPATH=
				CONFIG_NVME_TARGET=
				CONFIG_NVME_TARGET_LOOP=
				CONFIG_NVME_TARGET_RDMA=
				CONFIG_NVME_TARGET_FC=
				CONFIG_NVME_TARGET_FCLOOP=
				;;
			--with-nvme_target-mod)
				CONFIG_NVME_TARGET="m"
				CONFIG_NVME_TARGET_LOOP="m"
				CONFIG_NVME_TARGET_RDMA="m"
				CONFIG_NVME_TARGET_FC="m"
				CONFIG_NVME_TARGET_FCLOOP="m"
				CONFIG_NVME_HOST_WITHOUT_FC=
				;;
			--without-nvme_target-mod)
				CONFIG_NVME_TARGET=
				CONFIG_NVME_TARGET_LOOP=
				CONFIG_NVME_TARGET_RDMA=
				CONFIG_NVME_TARGET_FC=
				CONFIG_NVME_TARGET_FCLOOP=
				CONFIG_NVME_HOST_WITHOUT_FC=
				;;
			--with-nfsrdma-mod)
				CONFIG_SUNRPC_XPRT_RDMA="m"
				CONFIG_SUNRPC_XPRT_RDMA_CLIENT="m"
				CONFIG_SUNRPC_XPRT_RDMA_SERVER="m"
				;;
			--without-nfsrdma-mod)
				CONFIG_SUNRPC_XPRT_RDMA=
				CONFIG_SUNRPC_XPRT_RDMA_CLIENT=
				CONFIG_SUNRPC_XPRT_RDMA_SERVER=
				;;
			--with-vmw_pvrdma-mod)
				CONFIG_INFINIBAND_VMWARE_PVRDMA="m"
				;;
			--without-vmw_pvrdma-mod)
				CONFIG_INFINIBAND_VMWARE_PVRDMA=
				;;
			--with-qed-mod)
				CONFIG_QED="m"
				;;
			--without-qed-mod)
				CONFIG_QED=
				;;
			--with-qede-mod)
				CONFIG_QED="m"
				CONFIG_QEDE="m"
				;;
			--without-qede-mod)
				CONFIG_QED=
				CONFIG_QEDE=
				;;
			--with-qedr-mod)
				CONFIG_QED="m"
				CONFIG_QED_LL2="y"
				CONFIG_QED_RDMA="y"
				CONFIG_QEDE="m"
				CONFIG_INFINIBAND_QEDR="m"
				CONFIG_QED_OOO="y"
				;;
			--without-qedr-mod)
				CONFIG_QED=
				CONFIG_QED_LL2=
				CONFIG_QED_RDMA=
				CONFIG_QEDE=
				CONFIG_INFINIBAND_QEDR=
				CONFIG_QED_OOO=
				;;
			--without-firmware-mod)
				;;

			-h | --help)
				usage
				exit 0
				;;
			--without*)
				tmpconfig=`echo $1 | sed -e 's/--without/CONFIG/' -e 's/-/_/g' | tr '[:lower:]' '[:upper:]'`
				eval export $tmpconfig=
				;;
			--with*=*)
				tmpconfig=`echo $1 | cut -d '=' -f 1 | sed -e 's/--with/CONFIG/' -e 's/-/_/g' | tr '[:lower:]' '[:upper:]'`
				eval export $tmpconfig=`expr "x$1" : 'x[^=]*=\(.*\)'`
				;;
			--with*)
				tmpconfig=`echo $1 | sed -e 's/--with/CONFIG/' -e 's/-/_/g' | tr '[:lower:]' '[:upper:]'`
				eval export $tmpconfig=y
				;;
			*)
				echo
				echo "Unsupported parameter $1"
				echo
				;;
		esac
		shift

	done

	#Set default values
	KVERSION=${KVERSION:-$(uname -r)}
	MIN_KVERSION="3.10"
	MODULES_DIR=${MODULES_DIR:-/lib/modules/${KVERSION}/updates}
	KSRC=${KSRC:-/lib/modules/${KVERSION}/source}
	KSRC_OBJ=${KSRC_OBJ:-/lib/modules/$KVERSION/build}

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

	case $KVERSION in
		*)
		CONFIG_COMPAT_RDMA=m
		;;
	esac


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

	# Additional parameters to be passed to configure command
	CWD=$(pwd)
	CONFIG="configure.mk.kernel"
	# ALL_CONFIGS - includes all used config macros
	ALL_CONFIGS="allconfigs"

	find $CWD -name Kconfig | xargs grep -h ^config | sed -e "s/config /CONFIG_/" | sort -n | uniq > $ALL_CONFIGS

	if (/bin/ls -1 $KSRC_OBJ/include/*/autoconf.h 2>/dev/null | head -1 | grep -q generated); then
		AUTOCONF_PREFIX=generated
	else
		AUTOCONF_PREFIX=linux
	fi
	mkdir -p ${CWD}/include/${AUTOCONF_PREFIX}
	AUTOCONF_H="${CWD}/include/${AUTOCONF_PREFIX}/autoconf.h"

	# disable unsupported modules on 32bit
	if [ $ARCH != "x86_64" ]; then
		CONFIG_INFINIBAND_IPATH=
		CONFIG_INFINIBAND_QIB=
		CONFIG_INFINIBAND_RDMAVT=
		CONFIG_RDMA_RXE=
		CONFIG_INFINIBAND_VMWARE_PVRDMA=
	fi

	SKIP_AUTOCONF=${SKIP_AUTOCONF:-0}
	FORCE_AUTOGEN=${FORCE_AUTOGEN:-0}

	if [ "X${CONFIG_MLX4_EN}" == "Xm" ]; then
		check_autofconf CONFIG_DCB
		if [ X${CONFIG_DCB} == "X1" ]; then
			CONFIG_MLX4_EN_DCB=y
		fi
	fi

	if [ "X${CONFIG_BNXT}" == "Xm" ]; then
		check_autofconf CONFIG_DCB
		if [ X${CONFIG_DCB} == "X1" ]; then
			CONFIG_BNXT_DCB=y
		fi
	fi

	# 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}
	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

	check_autofconf CONFIG_NET_SWITCHDEV
	if [ "X${CONFIG_NET_SWITCHDEV}" != "X1" ]; then
		CONFIG_MLX5_ESWITCH=
	fi

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

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

AUTOCONF_H=${AUTOCONF_H}

WITH_MAKE_PARAMS=${WITH_MAKE_PARAMS}
EOFCONFIG

	while read config
	do
		eval echo $config=\$$config >> ${CONFIG}
	done < ${ALL_CONFIGS}

	echo "Created ${CONFIG}:"

	. ${CONFIG}

	CONFIG_VALUES=`grep ^CONFIG ${CONFIG} | tr -s '\n' ' '`
	sed -e "s/@CONFIG_VALUES@/$CONFIG_VALUES/" makefile.in > makefile

	# Create autoconf.h
	TMPAUTOCONF1=`mktemp $CWD/XXXXXX.autoconf1`
	TMPAUTOCONF2=`mktemp $CWD/XXXXXX.autoconf2`
	while read config
	do
		val=`eval echo \\$$config`
		case $val in
			y|m)
				echo "#define $config 1" >> ${TMPAUTOCONF1}
				echo -e "#undef $config\n#define $config 1" >> ${TMPAUTOCONF2}
				;;
			*)
				echo "#undef $config" >> ${TMPAUTOCONF2}
				;;
		esac
	done < ${ALL_CONFIGS}

	cat > ${AUTOCONF_H} << EOFAUTOCONF
#ifndef __OFED_BUILD__
#include_next <$AUTOCONF_PREFIX/autoconf.h>

EOFAUTOCONF

	cat ${TMPAUTOCONF1} >> ${AUTOCONF_H}
	echo -e "\n#else\n" >> ${AUTOCONF_H}
	cat ${TMPAUTOCONF2} >> ${AUTOCONF_H}
	echo -e "\n#endif" >> ${AUTOCONF_H}

	echo Created ${AUTOCONF_H}
	/bin/rm -f ${TMPAUTOCONF1}
	/bin/rm -f ${TMPAUTOCONF2}

	if [ ! -x compat/autogen.sh ]; then
		exit 0
	fi

	if [ $SKIP_AUTOCONF -eq 0 ]; then
		cd compat
		if [[ ! -x configure || $FORCE_AUTOGEN -eq 1 ]]; then
			ex ./autogen.sh
		fi
		ex ./configure --with-linux-obj=$KSRC_OBJ --with-linux=$KSRC
	fi
}

main $@
