################################################################################
#
# Driver for Xilinx network controllers and boards
# Copyright 2021 Xilinx Inc.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License version 2 as published
# by the Free Software Foundation, incorporated herein by reference.
#
################################################################################

ifndef EFCT_UPSTREAM

# The EFCT_UPSTREAM variable is never defined.  Sections of the Makefile
# that are only needed for an out-of-tree build are guarded by
# "ifndef EFCT_UPSTREAM" and removed using sed when it is exported to a
# kernel tree.

# The KERNELRELEASE variable is defined by kbuild.  Some parts of the
# Makefile should only be included when this is the top-level Makefile
# and not when it is included by kbuild; these are guarded by
# "ifndef KERNELRELEASE".
# Configuration
ifndef KERNELRELEASE
export CONFIG_NET_VENDOR_XILINX := y
export CONFIG_XILINX_EFCT := m
ifdef EFCT_FOR_UPSTREAM
# match UNIFDEF_DEFINES in export.sh
export CONFIG_XILINX_DEBUGFS :=
else
export CONFIG_XILINX_DEBUGFS := y
endif
export CONFIG_XILINX_MCDI_MON := y
export CONFIG_X3_BUSYPOLL := n
export CONFIG_XILINX_AUX_EFCT := y
export CONFIG_XILINX_DUMP :=
export CONFIG_XILINX_MCDI_LOGGING := y
export CONFIG_XILINX_PTP := y
export CONFIG_XILINX_EFCT_TRACING :=
endif

ifdef KSRCDIR
$(error KSRCDIR has been replaced with KDIR)
endif

ifdef KERNELRELEASE

# Check config dependencies under Kbuild.  We should only do this if
# .config has actually been included (it isn't when cleaning), so test
# for a config symbol that should always be defined.
ifdef CONFIG_NET

ifdef CONFIG_XILINX_EFCT_TRACING
ifndef CONFIG_EVENT_TRACING
$(error Kernel does not support EVENT_TRACING)
endif
endif
ifdef CONFIG_XILINX_DEBUGFS
ifndef CONFIG_DEBUG_FS
$(warning Kernel does not support DEBUG_FS)
override CONFIG_XILINX_DEBUGFS :=
$(warning => Some diagnostic information will be unavailable)
endif
endif
ifdef CONFIG_XILINX_MCDI_MON
ifndef CONFIG_HWMON
$(warning Kernel does not support HWMON)
override CONFIG_XILINX_MCDI_MON :=
endif
endif # CONFIG_XILINX_MCDI_MON
ifndef CONFIG_XILINX_MCDI_MON
$(warning => Temperature and voltage reporting for the boards will be disabled)
endif
ifndef CONFIG_XILINX_MCDI_LOGGING
$(warning => MCDI tracing will not be supported)
endif

endif # CONFIG_NET

else # !KERNELRELEASE ###### The following is for the standalone Makefile case

TOPDIR = ../../../..
include $(TOPDIR)/scripts/Makefile.common

ifneq ($(MAKECMDGOALS),export-srpm)

KVERPARTS = $(subst -, ,$(subst ., ,$(KVER)))
ifeq ($(word 1,$(KVERPARTS)),2)
ifneq ($(word 2,$(KVERPARTS)),6)
$(error Kernel version $(KVER) is not supported; minimum version is 2.6.16)
endif
ifneq ($(filter 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15,$(word 3,$(KVERPARTS))),)
$(error Kernel version $(KVER) is not supported; minimum version is 2.6.16)
endif
endif

endif # export-srpm

endif # !KERNELRELEASE


ifndef CONFIG_AUXILIARY_BUS
ifdef CONFIG_XILINX_AUX_EFCT
ifneq (,$(wildcard /lib/modules/$(KVER)/updates/auxiliary.symvers))
KBUILD_EXTRA_SYMBOLS += /lib/modules/$(KVER)/updates/auxiliary.symvers
else
ifndef AUX_BUS_PATH
AUX_BUS_PATH = $(realpath $(TOPDIR)/../cns-auxiliary-bus)
$(info Defaulting AUX_BUS_PATH to $(AUX_BUS_PATH))
endif
EXTRA_CFLAGS += -I$(AUX_BUS_PATH)/include
KBUILD_EXTRA_SYMBOLS := $(AUX_BUS_PATH)/drivers/base/Module.symvers
endif
endif # CONFIG_XILINX_AUX_EFCT
endif # CONFIG_AUXILIARY_BUS
endif # !EFCT_UPSTREAM

xilinx_efct-y := mcdi.o mcdi_port_common.o mcdi_functions.o efct_netdev.o efct_common.o nic.o \
			efct_nic.o efct.o efct_evq.o efct_tx.o efct_rx.o efct_ethtool.o ioctl_common.o \
			efct_reflash.o efct_devlink.o

xilinx_efct-$(CONFIG_XILINX_AUX_EFCT) += efct_auxbus.o
xilinx_efct-$(CONFIG_XILINX_DEBUGFS) += debugfs.o
xilinx_efct-$(CONFIG_XILINX_MCDI_MON) += mcdi_mon.o
xilinx_efct-$(CONFIG_XILINX_PTP) += efct_ptp.o
ifndef EFCT_NO_KCOMPAT
ifneq ($(MAKECMDGOALS),export)
xilinx_efct-y			+= kernel_compat.o
endif
endif # !EFCT_NO_KCOMPAT

obj-$(CONFIG_XILINX_EFCT)	+= xilinx_efct.o

#
ifndef EFCT_UPSTREAM

# Compiler flags
EXTRA_CFLAGS += -I$(src) -DWITH_MCDI_V2
EXTRA_CFLAGS += -I$(realpath $(src)/../../../../include/linux/net/xilinx/)
ifdef CONFIG_GCOV_KERNEL
EXTRA_CFLAGS += -fprofile-arcs -ftest-coverage
endif
all_xilinx_ko = $(subst .o,.ko,$(obj-m))

ifdef KERNELRELEASE
TOPDIR = $(src)/../../../..
include $(TOPDIR)/scripts/Makefile.common

define filechk_config.h
	printf "$(foreach name,XILINX_AUX_EFCT XILINX_DEBUGFS XILINX_PTP XILINX_DUMP XILINX_EFCT_TRACING XILINX_MCDI_LOGGING XILINX_MCDI_MON X3_BUSYPOLL X3_STAT_DMA,#undef CONFIG_$(name)\n$(if $(filter y m,$(CONFIG_$(name))),#define CONFIG_$(name) $(CONFIG_$(name))\n))"
endef

$(obj)/config.h: $(src)/Makefile FORCE
	$(call filechk,config.h)

$(addprefix $(obj)/,$(xilinx_efct-y)): \
	$(obj)/.kpath $(obj)/autocompat.h $(obj)/config.h

# Select the right warnings - complicated by working out which options work
ifndef try-run
try-run = $(shell set -e;		\
	TMP="$(obj)/.$$$$.tmp";		\
	TMPO="$(obj)/.$$$$.o";		\
	if ($(1)) >/dev/null 2>&1;	\
	then echo "$(2)";		\
	else echo "$(3)";		\
	fi;				\
	rm -f "$$TMP" "$$TMPO")
endif
ifndef cc-disable-warning
cc-disable-warning = $(call try-run,\
	$(CC) $(KBUILD_CPPFLAGS) $(KBUILD_CFLAGS) -W$(strip $(1)) -c -xc /dev/null -o "$$TMP",-Wno-$(strip $(1)))
endif
override EXTRA_CFLAGS += $(call cc-disable-warning, unused-but-set-variable) \
			 $(call cc-disable-warning, format-zero-length)

else # !KERNELRELEASE

# Some distribution packages of 2.6.16 fail to define KERNELRELEASE
# when compiling out-of-tree modules.
ifneq ($(filter 2.6.16-% 2.6.16.%,$(KVER)),)
EXTRA_MAKEFLAGS := KERNELRELEASE=$(KVER)
endif

default : top

top:
	$(MAKE) -C ../../../.. all

all : modules

modules :
	$(MAKE) -C $(KDIR) $(EXTRA_MAKEFLAGS) M=$$(pwd) AUX_BUS_PATH=$(AUX_BUS_PATH)
# strip objects
ifdef NDEBUG
	$(CROSS_COMPILE)strip --strip-debug $(all_xilinx_ko)
endif
ifdef EFCT_NOT_EXPORTED
	@echo "module sizes are:"
	@ls -l $(all_xilinx_ko)
endif # EFCT_NOT_EXPORTED

modules_install :
	mkdir -p $(INSTALL_MOD_PATH)/lib/modules/$(KVER)/$(INSTALL_MOD_DIR)
	$(MAKE) -C $(KDIR) $(EXTRA_MAKEFLAGS) M=$$(pwd) INSTALL_MOD_DIR=$(INSTALL_MOD_DIR) modules_install

clean : clean_modules

clean_modules :
	rm -f *.o *.s *.ko *.mod.c *.symvers config.h autocompat.h .kpath
	$(MAKE) -C $(KDIR) $(EXTRA_MAKEFLAGS) M=$$(pwd) clean

.PHONY : all modules modules_install clean clean_modules top FORCE

clean : clean_util

util :
	$(MAKE) -C util

clean_util:
	$(MAKE) -C util clean

# Export into kernel tree
export:
	$(EXPORT_CMD) $(KDIR) $(xilinx_efct-y:%.o=%.c)

export-srpm:
	$(EXPORT_CMD) -o $(KDIR) $(xilinx_efct-y:%.o=%.c) Kconfig kernel_compat.sh $(TOPDIR)/scripts/kernel_compat_funcs.sh

.PHONY : export util clean_util export-srpm

endif # !KERELRELEASE
endif # !EFCT_UPSTREAM
