#!/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 "bnx2.*dkms" | wc -l), 1)
	$(error More than one bnx2 DKMS RPM installed!!!  Failing build.)
endif


# Find the required include directory.  Some qualified versions of bnx2 failed
# to install the required header files in a predictable location.  Find those too.
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
	ifneq ($(shell ls /usr/src/bnx2-1.6.7b/bnx2.h /usr/src/bnx2-1.6.7b/cnic_if.h > /dev/null 2>&1 && echo found),)
		BNX2_CNIC_INC=/usr/src/bnx2-1.6.7b
	else
		ifneq ($(shell ls /usr/src/bnx2-1.6.5g/bnx2.h /usr/src/bnx2-1.6.5g/cnic_if.h > /dev/null 2>&1 && echo found),)
			BNX2_CNIC_INC=/usr/src/bnx2-1.6.5g
		else
  			$(error bnx2/cnic package not found)
		endif
	endif
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)
# Makefile for 2.5+ kernel

BCM_DRV = bnx2i.ko

ifneq ($(KERNELRELEASE),)

bnx2i-objs := bnx2i_iscsi.o bnx2i_hwi.o bnx2i_init.o
obj-m += bnx2i.o

EXTRA_CFLAGS = $(NETDUMP_CFLAG) -ffast-math -mhard-float -I${BNX2_CNIC_INC}
else

default:
	make -C $(LINUX) SUBDIRS=$(shell pwd) modules

endif

else # ifeq ($(BCM_KVER),2.6)
# Makefile for 2.4 kernel

BCM_DRV = bnx2i.o

ifeq ($(LINUX),)
  $(error Linux kernel source tree not found)
endif

CC = gcc

CFLAGS=-D_LITTLE_ENDIAN -D__LITTLE_ENDIAN -Wall -Wstrict-prototypes -I$(LINUX)/include $(NETDUMP_CFLAG)

ifeq ($(shell grep netif_poll_disable $(LINUXSRC)/include/linux/netdevice.h > /dev/null 2>&1 && echo newnetif),)
  CFLAGS+=-DOLD_NETIF
endif

ifeq ($(wildcard ~/rpmdir),)
  rpmdir = /usr/src/redhat
else
  rpmdir = $(wildcard ~/rpmdir)
endif

ARCH:=$(shell uname -m)

ifeq ($(ARCH),x86_64)
  CFLAGS+=-mno-red-zone -mcmodel=kernel -pipe -finline-limit=2000
endif

ifeq ($(ARCH),ia64)
  CFLAGS+=-pipe -ffixed-r13 -mfixed-range=f10-f15,f32-f127 -falign-functions=32
endif

ifeq ($(ARCH),ppc64)
  ifneq ($(shell ls /opt/cross/bin/powerpc64-linux-gcc > /dev/null 2>&1 && echo ppcgcc),)
    CC=/opt/cross/bin/powerpc64-linux-gcc
  endif

  CFLAGS+=-fno-strict-aliasing -fno-common -fomit-frame-pointer -msoft-float -pipe -mminimal-toc -fno-builtin
endif

ifdef SMALL
  CFLAGS += -Os
else
  CFLAGS += -O2
endif

all: bnx2i.o

endif # ifeq ($(BCM_KVER),2.6)
