#
# Makefile to build the be2net network driver
#

# find a source path that exists among the possible paths
ver = $(shell uname -r)
paths := /lib/modules/$(ver)/source /lib/modules/$(ver)/build
exists = $(shell [ -e $(path)/include/linux ] && echo $(path))
paths := $(foreach path, $(paths), $(exists))
ifeq (,$(KSRC))
	KSRC := $(firstword $(paths))
endif

# ************** Header checks ********************
ifneq (,$(shell grep -o alloc_etherdev_mqs $(KSRC)/include/linux/etherdevice.h))
	EXTRA_CFLAGS += -DALLOC_ETHDEV_MQS_defined
endif

ifneq (,$(shell grep -o alloc_etherdev_mq $(KSRC)/include/linux/etherdevice.h))
       EXTRA_CFLAGS += -DALLOC_ETHDEV_MQ_defined
endif

ifneq (,$(shell grep -o netif_set_real_num_rx_queues $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DNETIF_SET_REAL_NUM_RX_QS_defined
endif

ifneq (,$(shell grep -o netif_set_real_num_tx_queues $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DNETIF_SET_REAL_NUM_TX_QS_defined
endif

ifneq (,$(shell grep -o real_num_tx_queues $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DREAL_NUM_TX_QS_defined
endif

ifneq (,$(shell grep -o netif_get_num_default_rss_queues $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DGET_NUM_DEF_RSS_defined
endif

ifneq (,$(shell grep -o PCI_DEV_FLAGS_ASSIGNED $(KSRC)/include/linux/pci.h))
	EXTRA_CFLAGS += -DPCI_FLAGS_ASSIGNED_defined
endif

ifneq (,$(shell grep -o skb_frag_set_page $(KSRC)/include/linux/skbuff.h))
	EXTRA_CFLAGS += -DSKB_FRAG_API_defined
endif

ifneq (,$(shell grep -o skb_frag_size $(KSRC)/include/linux/skbuff.h))
	EXTRA_CFLAGS += -DSKB_FRAG_SIZE_defined
endif

ifneq (,$(shell grep -o netdev_alloc_skb_ip_align $(KSRC)/include/linux/skbuff.h))
	EXTRA_CFLAGS += -DALLOC_SKB_IP_ALIGN_defined
endif

ifneq (,$(shell grep -o skb_record_rx_queue $(KSRC)/include/linux/skbuff.h))
	EXTRA_CFLAGS += -DSKB_RECORD_RX_QUEUE_defined
endif

ifneq (,$(shell grep -o cancel_delayed_work_sync $(KSRC)/include/linux/workqueue.h))
	EXTRA_CFLAGS += -DCANCEL_DELAYED_WORK_SYNC_defined
endif

ifneq (,$(shell grep -o rxhash $(KSRC)/include/linux/skbuff.h))
	EXTRA_CFLAGS += -DRXHASH_defined
endif

#GRO is usable only when full implementation is available
ifneq (,$(shell grep -o napi_get_frags $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DGRO_defined
endif

ifneq (,$(shell grep -o netdev_alloc_skb $(KSRC)/include/linux/skbuff.h))
	EXTRA_CFLAGS += -DALLOC_SKB_defined
endif

ifneq (,$(shell grep -o dev_mc_list $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DDEV_MC_LIST_defined
endif

ifneq (,$(shell grep -so vlan_group_set_device $(KSRC)/include/linux/if_vlan.h $(KSRC)/net/8021q/vlan.h))
	EXTRA_CFLAGS += -DVLAN_GRP_SET_DEV_defined
endif

ifneq (,$(shell grep -o "struct vlan_group" $(KSRC)/include/linux/if_vlan.h))
	EXTRA_CFLAGS += -DVLAN_GRP_defined
endif

ifeq (,$(shell grep -o "vlan_hwaccel_receive_skb" $(KSRC)/include/linux/if_vlan.h))
	EXTRA_CFLAGS += -DUSE_NEW_VLAN_MODEL
endif

ifneq (,$(shell grep -o ndo_set_vf_mac $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DNDO_VF_CFG_defined
endif

ifneq (,$(shell grep -o ndo_set_features $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DNDO_SET_FEATURES_defined
endif

ifneq (,$(shell grep -o netdev_features_t $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DNETDEV_FEATURES_defined
endif

ifneq (,$(shell grep -so "ethtool_cmd_speed_set" $(KSRC)/include/linux/ethtool.h \
						$(KSRC)/include/uapi/linux/ethtool.h))
	EXTRA_CFLAGS += -DETHTOOL_CMD_SPEED_SET_defined
endif

ifneq (,$(shell grep -so "ethtool_cmd_speed" $(KSRC)/include/linux/ethtool.h \
					$(KSRC)/include/uapi/linux/ethtool.h))
	EXTRA_CFLAGS += -DETHTOOL_CMD_SPEED_defined
endif

ifneq (,$(shell grep -o "ethtool_phys_id_state" $(KSRC)/include/linux/ethtool.h))
	EXTRA_CFLAGS += -DPHYS_ID_STATE_defined
endif

ifneq (,$(shell grep -o "flash_device" $(KSRC)/include/linux/ethtool.h))
	EXTRA_CFLAGS += -DETHTOOL_FLASH_defined
endif

ifneq (,$(shell grep -o "get_sset_count" $(KSRC)/include/linux/ethtool.h))
	EXTRA_CFLAGS += -DETHTOOL_SSET_COUNT_defined
endif

ifneq (,$(shell grep -o "set_dump" $(KSRC)/include/linux/ethtool.h))
	EXTRA_CFLAGS += -DETHTOOL_SET_DUMP_defined
endif

ifneq (,$(shell grep -o "ethtool_ops_ext" $(KSRC)/include/linux/ethtool.h))
	EXTRA_CFLAGS += -DETHTOOL_OPS_EXT_defined
endif

ifneq (,$(shell grep -o "pci_physfn" $(KSRC)/include/linux/pci.h))
	EXTRA_CFLAGS += -DPCI_PHYSFN_defined
endif

ifneq (,$(shell grep -o "set_rxnfc" $(KSRC)/include/linux/ethtool.h))
	EXTRA_CFLAGS += -DETHTOOL_RXNFC_OPS_defined
endif

ifneq (,$(shell grep -o "set_channels" $(KSRC)/include/linux/ethtool.h))
	EXTRA_CFLAGS += -DETHTOOL_OPS_CHANNELS_defined
endif

ifneq (,$(shell grep -o ndo_get_stats64 $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DNDO_GET_STATS64_defined
endif

ifneq (,$(shell grep -o txq_trans_update $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DTXQ_TRANS_UPDATE_defined
endif

ifeq (,$(shell grep hlist_for_each_entry_safe $(KSRC)/include/linux/list.h | grep tpos))
	EXTRA_CFLAGS += -DHLIST_ENTRY_IS_NEW
endif

ifneq (,$(shell grep -o pci_vfs_assigned $(KSRC)/include/linux/pci.h))
	EXTRA_CFLAGS += -DPCI_VFS_ASSIGNED_defined
endif

ifneq (,$(shell grep -o pci_num_vf $(KSRC)/include/linux/pci.h))
	EXTRA_CFLAGS += -DPCI_NUM_VF_defined
endif

ifneq (,$(shell grep -o pci_sriov_get_totalvfs $(KSRC)/include/linux/pci.h))
	EXTRA_CFLAGS += -DPCI_SRIOV_GET_TOTALVFS_defined
endif

ifneq (,$(shell grep __vlan_put_tag $(KSRC)/include/linux/if_vlan.h | grep vlan_proto))
	EXTRA_CFLAGS += -DVLAN_FUNCS_USES_PROTO
endif

ifeq (,$(shell grep icmp6_hdr $(KSRC)/include/linux/icmpv6.h))
	EXTRA_CFLAGS += -DICMP6_HDR_IS_OLD
endif

ifeq (,$(shell grep udp_hdr $(KSRC)/include/linux/udp.h))
	EXTRA_CFLAGS += -DUDP_HDR_IS_OLD
endif

ifeq (,$(shell grep skb_transport_header $(KSRC)/include/linux/skbuff.h))
	EXTRA_CFLAGS += -DSKB_TRANSPORT_HDR_OLD
endif

ifeq (,$(shell grep NETIF_F_HW_VLAN $(KSRC)/include/linux/netdevice.h ))
ifneq (,$(shell grep NETIF_F_HW_VLAN_CTAG $(KSRC)/include/linux/netdev_features.h ))
	EXTRA_CFLAGS += -DNETIF_F_HW_VLAN_CTAG_DEFINED
endif
endif

ifneq (,$(shell grep -o ndo_bridge_setlink $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DNDO_BRIDGE_SETLINK_defined
endif

ifneq (,$(shell grep -o "struct netdev_rps_info" $(KSRC)/include/linux/netdevice.h))
	EXTRA_CFLAGS += -DNETDEV_RPS_INFO_defined
endif

# ************* End header checks *****************

# ************* Targets ***************************
ifeq ($(KERNELRELEASE),)
ifeq ($(KDIR),)
	KDIR ?= /lib/modules/`uname -r`/build
endif	
default:
	@$(MAKE) -C$(KDIR) M=$$PWD
clean:
	@$(MAKE) -C$(KDIR) M=$$PWD clean
else
obj-$(CONFIG_BE2NET) += be2net.o
be2net-y :=  be_main.o be_cmds.o be_ethtool.o be_compat.o be_misc.o inet_lro.o be_roce.o
endif
# ************* End Targets ************************
