# SPDX-License-Identifier: GPL-2.0-or-later

# Copyright (c) 2024, DapuStor Corporation.

CONFIG_MODULES_SIG=n

ifneq ($(KERNELRELEASE),)
# kbuild part of makefile
#
# Makefile for the DAPU(R) 10GbE PCI Express Linux Network Driver
#
	EXTRA_CFLAGS += -DDN200_VF_OVER_NVME=1 -DDN200_FW_TEST=1 -DDN200_PRODUCT=1
	EXTRA_CFLAGS += -g -O3
	EXTRA_CFLAGS += $(CFLAGS)

	obj-m := dn200.o
	dn200-objs :=   \
		dn200_main.o dn200_ethtool.o dn200_mdio.o ring_mode.o	\
	    mmc_core.o dn200_hwtstamp.o dn200_ptp.o \
	    hwif.o \
	    dwxgmac2_core.o dwxgmac2_dma.o dwxgmac2_descs.o \
	    dn200_selftests.o dn200_pci.o\
	    dn200_sriov.o dn200_ctrl.o \
	    dn200_phy_impl.o dn200_spec_acc.o \
	    dn200_eprom.o dn200_iatu.o dn200_pool.o dn200_reg.o \
	    extern_phy.o

	dn200-$(CONFIG_DN200_DCB) += dn200_dcb.o

else	# ifneq($(KERNELRELEASE),)

DRIVER := dn200

# Check that kernel version is at least 2.6.0, since we don't support 2.4.x
# kernels with the ixgbe driver. We can't use minimum_kver_check since SLES 10
# SP4's Make has a bug which causes $(eval) inside an ifeq conditional to error
# out. This was fixed in Make 3.81, but SLES 10 SP4 does not have a fix for
# this yet.
ifeq (0,$(shell [ ${KVER_CODE} -lt $(call get_kvercode,2,6,0) ]; echo "$?"))
  $(warning *** Aborting the build.)
  $(error This driver is not supported on kernel versions older than 2.6.0)
endif

######################
# Kernel Build Macro #
######################

# customized kernelbuild function
#
# ${1} is the kernel build target
# ${2} may contain extra rules to pass to kernelbuild macro
#
# We customize the kernelbuild target in order to provide our hack to disable
# CONFIG_PTP_1588_CLOCK support should -DNO_PTP_SUPPORT be defined in the extra
# cflags given on the command line.

devkernelbuild = $(call kernelbuild, ${2},${1})
# Command to update initramfs or display a warning message
ifeq (${install_cmd_initrd},)
define insatll_cmd_initramfs
@echo "Unable to update initramfs. You may need to do this manually."
endef
else
define install_cmd_initramfs
@echo "Updating initramfs..."
-@$(call install_cmd_initrd)
endef
endif

ifeq (${rm_cmd_initrd},)
define rm_cmd_initramfs
@echo "Unable to update initramfs. You may need to do this manually."
endef
else
define rm_cmd_initramfs
@echo "Updating initramfs..."
-@$(call rm_cmd_initrd)
endef
endif

###############
# Build rules #
###############

# Standard compilation, with regular output
default:
	@+$(call devkernelbuild,modules)

# Build manfiles
manfile:
	@gzip -c ../${DRIVER}.${MANSECTION} > ${DRIVER}.${MANSECTION}.gz

# Clean the module subdirectories
clean:
	@+$(call devkernelbuild,clean)
	@-rm -rf kcompat_generated_defs.h
	@-rm -rf *.ko

# Install the modules and manpage
mandocs_install: manfile
	@echo "Copying manpages..."
	@install -D -m 644 ${DRIVER}.${MANSECTION}.gz ${INSTALL_MOD_PATH}${MANDIR}/man${MANSECTION}/${DRIVER}.${MANSECTION}.gz

modules_install: default
	@echo "Installing modules..."
	@+$(call devkernelbuild,modules_install)
# After installing all the files, perform necessary work to ensure the
# system will use the new modules. This includes running depmod to update
# module dependencies and updating the initramfs image in case the module is
# loaded during early boot.
install: modules_install
	@echo "Running depmod..."
	$(call cmd_depmod)
	$(call install_cmd_initramfs)

mandocs_uninstall:
	if [ -e ${INSTALL_MOD_PATH}${MANDIR}/man${MANSECTION}/${DRIVER}.${MANSECTION}.gz ] ; then \
		rm -f ${INSTALL_MOD_PATH}${MANDIR}/man${MANSECTION}/${DRIVER}.${MANSECTION}.gz ; \
	fi;

# Remove installed module files. This target is called by the RPM specfile
# when generating binary RPMs, and is not expected to modify files outside
# of the build root. Thus, it must not update the initramfs image or run
# depmod.
modules_uninstall:
	rm -f ${INSTALL_MOD_PATH}/lib/modules/${KVER}/${INSTALL_MOD_DIR}/${DRIVER}.ko;

# After uninstalling all the files, perform necessary work to restore the
# system back to using the default kernel modules. This includes running
# depmod to update module dependencies and updating the initramfs image.
uninstall:
	rm -f ${INSTALL_MOD_PATH}/lib/modules/${KVER}/${INSTALL_MOD_DIR}/${DRIVER}.ko;
	$(call cmd_depmod)
	$(call rm_cmd_initramfs)

upgrade:
	@echo "Running upgrade..."
	rm -f ${INSTALL_MOD_PATH}/lib/modules/${KVER}/${INSTALL_MOD_DIR}/${DRIVER}.ko;
	$(call cmd_depmod)
	$(call rm_cmd_initramfs)
	@echo "remove modules complete..."
	@+$(call devkernelbuild,modules)
	@echo "Installing modules..."
	@+$(call devkernelbuild,modules_install)
	@echo "Running depmod..."
	$(call cmd_depmod)
	$(call install_cmd_initramfs)

########
# Help #
########
help:
	@echo 'Build targets:'
	@echo '  default             - Build module(s) with standard verbosity'
	@echo 'Cleaning targets:'
	@echo '  clean               - Clean files generated by kernel module build'
	@echo ''
	@echo 'Other targets:'
	@echo '  manfile             - Generate a gzipped manpage'
	@echo '  modules_install     - Install the module(s) only'
	@echo '  mandocs_install     - Install the manpage only'
	@echo '  install             - Build then install the module(s) and manpage, and update initramfs'
	@echo '  modules_uninstall   - Uninstall the module(s) only'
	@echo '  mandocs_uninstall   - Uninstall the manpage only'
	@echo '  uninstall           - Uninstall the module(s) and manpage, and update initramfs'
	@echo '  help                - Display this help message'
	@echo ' Other variables may be available for tuning make process, see'
	@echo ' Kernel Kbuild documentation for more information'
	@echo '  upgrade     - upgrade the module(s) '

.PHONY: default install uninstall upgrade help

endif	# ifneq($(KERNELRELEASE),)
