#!/usr/bin/make
# Makefile for building Linux Broadcom Gigabit L5 driver as a module.
# $id$
KVER=
ifeq ($(KVER),)
  KVER=$(shell uname -r)
endif

# PREFIX may be set by the RPM build to set the effective root.
PREFIX=
ifeq ($(shell ls /lib/modules/$(shell uname -r)/build > /dev/null 2>&1 && echo build),)
  ifeq ($(shell ls /usr/src/linux > /dev/null 2>&1 && echo linux),)
    LINUX=
  else
    LINUX=/usr/src/linux
  endif
else
  LINUX=/lib/modules/$(shell uname -r)/build
  ifeq ($(shell ls /lib/modules/$(shell uname -r)/source > /dev/null 2>&1 && echo source),)
    LINUXSRC=$(LINUX)
  else
    LINUXSRC=/lib/modules/$(shell uname -r)/source
  endif
endif

# Make sure only one bnx2? DKMS RPM is installed.
ifneq ($(shell rpm -qa | grep -v "bnx2i-*" | grep "netxtreme2-.*dkms" | wc -l), 1)
    $(error More than one netxtreme2 DKMS RPM installed!!!  Failing build.)
endif

ifneq ($(shell ls /usr/src/bnx2/bnx2.h /usr/src/bnx2/cnic_if.h > /dev/null 2>&1 && echo bnx2i),)
    BNX2_CNIC_INC=/usr/src/bnx2
else
  $(error bnx2/cnic package not found)
endif

ifeq ($(shell grep scsi_get_resid $(LINUXSRC)/include/scsi/*.h > /dev/null 2>&1 && echo resid_defined),)
  BRCM_FLAGS = -D_DEFINE_SCSI_GET_RESID
endif

ifeq ($(shell grep scsi_set_resid $(LINUXSRC)/include/scsi/*.h > /dev/null 2>&1 && echo resid_defined),)
  BRCM_FLAGS += -D_DEFINE_SCSI_SET_RESID
endif

ifeq ($(shell awk '/(*create_session)/,/;/ {printf $$0; next}' $(LINUXSRC)/include/scsi/scsi_transport_iscsi.h | awk -F ',' '{print NF}'),6)
  BRCM_FLAGS += -D_CREATE_SESS_NEW_
endif

# check if 2.4 kernel or 2.5+ kernel
BCM_KVER:=$(shell uname -r | cut -c1-3 | sed 's/2\.[56]/2\.6/')

ifeq ($(BCM_KVER), 2.6)
BCM_DRV = bnx2i.ko
ARCH:=$(shell uname -m)
BCM_USR=../bin/bnx2id

ifeq ($(ARCH),x86_64)
  BCM_USR=../bin/bnx2id64
endif

ifneq ($(KERNELRELEASE),)
bnx2i-objs := bnx2i_iscsi.o bnx2i_hwi.o bnx2i_init.o bnx2i_sysfs.o
obj-m += bnx2i.o
EXTRA_CFLAGS += -ffast-math -mhard-float -I${BNX2_CNIC_INC} -D_SYSFS_INCL_ ${BRCM_FLAGS}
else # ($(KERNELRELEASE),)
default:
	make -C $(LINUX) SUBDIRS=$(shell pwd) modules
endif

else # ifeq ($(BCM_KVER),2.6)
  $(error bnx2i is not supported on 2.4 kernel)
endif # ifeq ($(BCM_KVER),2.6)
