#*****************************************************************************
# Copyright 2004-2008 LSI Logic Corporation.
#
# This program is free software; you can redistribute it and/or modify it
# under the terms of the GNU General Public License as published by the
# Free Software Foundation,  version 2 of the License.
# 
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#*****************************************************************************
# Makefile for Linux RDAC (MPP) Driver
#######################################################################
#
# name: public_makefile26p
# instance: 1
# created by:	sdachepa
#    %version: 14.1.1 %
#    %derived_by:        sdachepa %
#    %date_created:      Thu Jun 12 17:33:19 2008 %
#
# description: 
#
#
#######################################################################

OS_VER := $(shell uname -r)
HOST_TYPE := $(shell uname -m)
IS_SMP := $(shell (uname -v | grep -c "SMP"))
# Kernel Distribution (either REDHAT or SUSE)
DIST := $(shell (if [ -f /etc/redhat-release ]; then echo REDHAT; else echo SUSE; fi))

ifeq ("$(KERNEL_OBJ)","")
	KERNEL_OBJ := /lib/modules/$(shell uname -r)/build
endif

#Flag to check if gcc is installed
GCC_CHK_FLAG := $(shell (/bin/rpm -qa | grep gcc | wc -l))
#Flag to check if gcc is installed
KERNEL_SRC_CHK_FLAG := $(shell (if [ -f $(KERNEL_OBJ)/Makefile ]; then echo 1; else echo 0; fi))
REDHAT_KERNEL_CHECK := $(shell ((cat /etc/issue | grep "Asianux Server 3" | wc -l) 2> /dev/null))
SUSE_KERNEL_CHECK := $(shell ((cat /etc/issue | grep "10" | grep "SP1\|SP2" | wc -l) 2> /dev/null))

COMMON_INCLUDE = ./mpp_linux_headers
SYS_DEP_INCLUDE = ./mpp_linux_sys_headers
UTILITY_DIR = ./utility
DRIVER_TARGETS=mppUpper mppVhba
DEPENDENT_TARGETS=MPP_hba.o mppLnx26p_upper.o mppLnx26p_sysdep.o mppCmn_s2tos3.o mppCmn_SysInterface.o
UTIL_TARGETS=mppUtil genuniqueid
UTIL_DEPENDS=mppUtil.o mppUtilSysdep.o
#Virtual HBA driver object files
VHBA_OBJS=mppLnx26p_vhbamisc.o  mppLnx26p_vhbatask.o mppLnx26p_vhba.o mppLnx26p_vhbaproc.o \
         mppLnx26p_vhbalib.o \
         mppLnx26p_vhbaio.o
CC      =$(CROSS_COMPILE)gcc
 
RDAC_RPM_HELPER_DIR=/opt/mpp/.mppLnx_rpm_helpers

ifeq ("$(KERNEL_OBJ)","")
	KERNEL_OBJ := /lib/modules/$(shell uname -r)/build
endif

INITRD_DIR=/boot

# Set optional parameters for build
ifeq ($(HOST_TYPE),ia64)
	EXTRA_UTIL_FLAGS=-DPAGE_SHIFT=12
endif

#set optional parameters for uninstall
ifeq ($(HOST_TYPE),ia64)
	ifeq ($(DIST),REDHAT)
		EXTRA_INITRD_DIR=/boot/efi/efi/redhat
	else
		EXTRA_INITRD_DIR=/boot/efi/efi/SuSE
	endif
endif

ifeq ($(HOST_TYPE),ppc64)
#workaround SLES10 ppc build problem where /usr/include/linux and /usr/src/linux/include/linux 
#are not synchronized
EXTRA_SPINLOCK_INC := $(shell if [ -f /etc/SuSE-release ]; then \
        IS_SLES10=`cat /etc/SuSE-release |grep VERSION |grep -c 10`; \
    fi; \
        if [ "$${IS_SLES10}" == "1" ]; then \
                echo " -I"${KERNEL_OBJ}/include|sed "s/build/source/" ; \
        fi )

ifndef ($(CROSS_COMPILE))
CROSS_COMPILE := $(shell if [ -e /usr/local/ppc64-current3.0/bin/powerpc64-linux-gcc ]; then \
    echo /usr/local/ppc64-current3.0/bin/powerpc64-linux-; \
    else if [ -e /opt/cross/bin/powerpc64-linux-gcc ]; then \
    echo /opt/cross/bin/powerpc64-linux- ; fi fi)
endif
CFLAGS += -Wstrict-prototypes -Wno-trigraphs -O2 -fomit-frame-pointer \
          -fno-common -pipe -Wno-unused -fsigned-char -msoft-float \
          -Wno-uninitialized -mminimal-toc -fno-builtin -fno-strict-aliasing

AS      =$(CROSS_COMPILE)as
LD      =$(CROSS_COMPILE)ld
CC      =$(CROSS_COMPILE)gcc
CPP     =$(CC) -E
AR      =$(CROSS_COMPILE)ar
NM      =$(CROSS_COMPILE)nm
STRIP   =$(CROSS_COMPILE)strip
OBJDUMP =$(CROSS_COMPILE)objdump
AWK     =awk
MAKE    =make

LD := $(LD) -m elf64ppc
CC := $(CC) -m64
AS := $(AS) -64
ELF := elf64ppc
CFLAGS := $(CFLAGS) -DBIG_ENDIAN
endif

final: all
 
all	: gcccheck kernelvercheck.$(DIST) kernelsrccheck $(DRIVER_TARGETS) $(UTIL_TARGETS)
 
driver	: gcccheck kernelvercheck.$(DIST) kernelsrccheck $(DRIVER_TARGETS)
 
install : all hbacheck copyfiles moduledep setupfiles setupdriver

hbacheck : 
	@if [ -f /opt/mpp/makefile.saved ]; then echo "The system has old MPP driver package installed." ; echo "Please do \"make uninstall\" in the current directory before installing the new one."; exit 1; fi
	@echo "Checking Host Adapter Configuration..."
	@chmod +x hbaCheck
	@./hbaCheck
	@echo "Your kernel version is $(OS_VER)" ;
	@echo "Preparing to install MPP driver against this kernel version..." ;

gcccheck:
	@if [ $(GCC_CHK_FLAG) -eq 0 ]; then echo "GCC rpm is not installed"; exit 1; fi

kernelvercheck.REDHAT:
	@if [ $(REDHAT_KERNEL_CHECK) -eq 0 ]; then echo "This Version of MPP build is not supported on this Version of Redhat Kernel `uname -r`."; exit 1; fi

kernelvercheck.SUSE:
	@if [ $(SUSE_KERNEL_CHECK) -eq 0 ]; then echo "This Version of MPP build is not supported on this Version of SuSE Kernel `uname -r`."; exit 1; fi

kernelsrccheck:
	@if [ $(KERNEL_SRC_CHK_FLAG) -eq 0 ]; then echo "Kernel source is not installed"; exit 1; fi

copyfiles :
	@install -o root -g root -m 0644 -D mppVhba.ko $(DEST_DIR)/lib/modules/$(OS_VER)/kernel/drivers/scsi/mppVhba.ko
	@install -o root -g root -m 0644 -D mppUpper.ko $(DEST_DIR)/lib/modules/$(OS_VER)/kernel/drivers/scsi/mppUpper.ko
	@install -o root -g root -m 0500 -D genuniqueid $(DEST_DIR)/opt/mpp/genuniqueid
	@install -o root -g root -m 0500 -D mppSupport $(DEST_DIR)/opt/mpp/mppSupport
	@install -o root -g root -m 0500 -D lsvdev $(DEST_DIR)/opt/mpp/lsvdev
	@install -o root -g root -m 0500 -D mppiscsi_umountall $(DEST_DIR)/opt/mpp/mppiscsi_umountall
	@install -o root -g root -m 0500 -D utility/mppBusRescan26p $(DEST_DIR)/usr/sbin/mppBusRescan
	@install -o root -g root -m 0644 -D utility/mppBusRescan.1 $(DEST_DIR)/usr/share/man/man1/mppBusRescan.1
	@install -o root -g root -m 0644 -D utility/RDAC.9 $(DEST_DIR)/usr/share/man/man9/RDAC.9
	@install -o root -g root -m 0500 -D utility/mppMkInitrd $(DEST_DIR)/opt/mpp/mppMkInitrdHelper
	@install -o root -g root -m 0500 -D utility/mppUpdate26p $(DEST_DIR)/usr/sbin/mppUpdate
	@install -o root -g root -m 0644 -D utility/mppUpdate.1 $(DEST_DIR)/usr/share/man/man1/mppUpdate.1
	@install -o root -g root -m 0500 -D mppUtil $(DEST_DIR)/usr/sbin/mppUtil
	@install -o root -g root -m 0644 -D utility/mppUtil.1 $(DEST_DIR)/usr/share/man/man1/mppUtil.1

copyrpmfiles :
	@install -o root -g root -m 0444 -D mpp.conf $(DEST_DIR)/etc/mpp.conf
	@install -o root -g root -m 0500 -D hbaCheck $(DEST_DIR)/$(RDAC_RPM_HELPER_DIR)/hbaCheck
	@install -o root -g root -m 0500 -D setupDriver.$(DIST) $(DEST_DIR)/$(RDAC_RPM_HELPER_DIR)/setupDriver.$(DIST)
	@install -o root -g root -m 0755 -D mpp_rcscript.$(DIST) $(DEST_DIR)/$(RDAC_RPM_HELPER_DIR)/mpp
	@/bin/sed -e ' s/"MPPLNX_REPLACE_THIS_WITH_BUILD_VERSION"/'$(KERNEL_VERSION)\/ -e ' s/MPPLNX_REPLACE_THIS_WITH_BUILD_ARCH/'$(HOST_TYPE)\/ utility/setupSuseBoot > setupSuseBoot
	@if [ -f /etc/SuSE-release ]; then install -o root -g root -m 0500 -D setupSuseBoot $(DEST_DIR)/$(RDAC_RPM_HELPER_DIR)/setupSuseBoot; fi
	@rm -rf setupSuseBoot

moduledep :
	@echo "Generating module dependencies..."
	@/sbin/depmod $(uname -r)
	
setupfiles :
	@install -o root -g root -m 0500 -D Makefile $(DEST_DIR)/opt/mpp/makefile.saved
	@install -o root -g root -m 0500 -D hbaCheck $(DEST_DIR)/opt/mpp/hbaCheck
	@install -o root -g root -m 0500 -D setupDriver.$(DIST) $(DEST_DIR)/opt/mpp/setupDriver.$(DIST)
	@install -o root -g root -m 0755 -D mpp_rcscript.$(DIST) $(DEST_DIR)/etc/init.d/mpp
	@gzip /usr/share/man/man9/RDAC.9
	@gzip /usr/share/man/man1/mppBusRescan.1
	@ln -s /usr/sbin/mppBusRescan /usr/sbin/hot_add
	@ln -s /usr/share/man/man1/mppBusRescan.1.gz /usr/share/man/man1/hot_add.1.gz
	@gzip /usr/share/man/man1/mppUpdate.1
	@gzip /usr/share/man/man1/mppUtil.1
	@if [ -f /etc/mpp.conf ]; then cp /etc/mpp.conf /etc/mpp.conf.mppsave; fi
	@install -o root -g root -m 0444 -D mpp.conf $(DEST_DIR)/etc/mpp.conf
	@if [ ! -f /var/mpp/devicemapping ]; then touch devicemapping;\
	install -o root -g root -m 0755 -d $(DEST_DIR)/var/mpp; install -o root -g root -m 0644 -D devicemapping $(DEST_DIR)/var/mpp/devicemapping;\
	rm devicemapping; fi

setupdriver:
	@/opt/mpp/setupDriver.$(DIST)
	@if [ -f /etc/SuSE-release ]; then \
		/sbin/insserv  /etc/init.d/mpp; \
	else \
		/sbin/chkconfig --add mpp;\
	fi
	@echo "MPP driver package has been successfully installed on your system."
 
uninstall:
	@if [ ! -f /opt/mpp/makefile.saved ]; then \
	echo "MPP driver is currently not installed on the system" ;\
	else \
	echo "Preparing to uninstall MPP driver..." ;\
	make -f /opt/mpp/makefile.saved uninstall_doer ;\
	rm -f /opt/mpp/makefile.saved ;\
	rmdir /opt/mpp ;\
	echo "MPP driver package has been successfully removed from your system.";\
	fi
 
uninstall_doer:
	@if [ -f /etc/modprobe.conf ]; then \
	/bin/sed '/BEGIN MPP Driver Comments/,/END MPP Driver Comments/d' /etc/modprobe.conf > modprobe.conf.restored;\
	/bin/cp modprobe.conf.restored /etc/modprobe.conf ;\
	/bin/rm modprobe.conf.restored;\
	fi
	@if [ -f /etc/sysconfig/kernel ]; then \
	/bin/sed '/BEGIN MPP Driver Comments/,/END MPP Driver Comments/d' /etc/sysconfig/kernel > sysconfig.kernel.restored;\
	/bin/cp sysconfig.kernel.restored /etc/sysconfig/kernel;\
	/bin/rm sysconfig.kernel.restored;\
	fi
	@if [ -f /etc/modprobe.conf.local ]; then \
	/bin/sed '/BEGIN MPP Driver Comments/,/END MPP Driver Comments/d' /etc/modprobe.conf.local > modprobe.conf.restored;\
	/bin/cp modprobe.conf.restored /etc/modprobe.conf.local;\
	/bin/rm modprobe.conf.restored;\
	fi
	@if [ -f /etc/init.d/iscsi ]; then \
	/bin/sed -i -e '/BEGIN_MPP/,/END_MPP/d' /etc/init.d/iscsi;\
	fi
	@if [ -f /etc/init.d/open-iscsi ]; then \
	/bin/sed -i -e '/BEGIN_MPP/,/END_MPP/d' /etc/init.d/open-iscsi;\
	fi
	@if [ -f /etc/rc.d/init.d/iscsi ]; then \
	/bin/sed -i -e '/BEGIN_MPP/,/END_MPP/d' /etc/rc.d/init.d/iscsi;\
	fi
	@if [ -f /etc/SuSE-release ]; then \
	/sbin/insserv -r  /etc/init.d/mpp;\
	else \
	/sbin/chkconfig --del mpp;\
	fi
	@rm -r /etc/init.d/mpp
	@rm -f /lib/modules/$(OS_VER)/kernel/drivers/scsi/mppVhba.ko
	@rm -f /lib/modules/$(OS_VER)/kernel/drivers/scsi/mppUpper.ko
	@rm -f /opt/mpp/genuniqueid
	@rm -f /opt/mpp/hbaCheck
	@rm -f /opt/mpp/setupDriver.$(DIST)
	@rm -f /opt/mpp/mppiscsi_umountall
	@rm -f /opt/mpp/mppmkinitrd
	@rm -f /opt/mpp/mppMkInitrdHelper
	@rm -f /opt/mpp/mppSupport
	@rm -f /opt/mpp/modprobe.conf.mppappend
	@rm -f /opt/mpp/kernel.suseinitrd
	@rm -f /opt/mpp/lsvdev
	@rm -f /usr/sbin/hot_add
	@rm -f /usr/sbin/mppBusRescan
	@rm -f /usr/sbin/mppUpdate
	@rm -f /usr/sbin/mppUtil
	@rm -f /usr/share/man/man1/mppUtil.1.gz
	@rm -f /usr/share/man/man1/hot_add.1.gz
	@rm -f /usr/share/man/man1/mppBusRescan.1.gz
	@rm -f /usr/share/man/man9/RDAC.9.gz
	@rm -f /usr/share/man/man1/mppUpdate.1.gz
	@rm -f $(INITRD_DIR)/mpp-$(OS_VER).img
	@if [ -n $(EXTRA_INITRD_DIR) ] ; then rm -f $(EXTRA_INITRD_DIR)/mpp-$(OS_VER).img ; fi
	@echo "The mpp RAMdisk image mpp-$(OS_VER).img has been removed. You may want to remove it from your boot loader config file."
	@if test ! -s /var/mpp/devicemapping ; then rm -rf /var/mpp/; fi
	@echo "Generating module dependencies..."
	@/sbin/depmod $(uname -r)


LINUX_RDAC_DIR :=  $(shell pwd)
export LINUX_RDAC_DIR



KERNEL_VERSION=`grep UTS_RELEASE $(KERNEL_OBJ)/include/linux/version.h | awk '{print $$3}'`

obj-m:= mppUpper.o mppVhba.o
mppUpper-objs:=$(DEPENDENT_TARGETS) 
mppVhba-objs:=$(VHBA_OBJS)


EXTRA_CFLAGS= -I$(M) -I$(M)/mpp_linux_headers/ -I$(M)/mpp_linux_sys_headers/

ifeq ("1", "$(DEBUG)")
	EXTRA_CFLAGS += -g -DMPP_DEBUG
endif
 
#Setting the value of V=1 ('make V=1') will print more information during compilation,by default it is 0.
ifeq ("", "$(V)")
	V = 0
endif
 

mppUpper: kernelsrccheck
	make V=$(V) -C$(KERNEL_OBJ)  M=$(LINUX_RDAC_DIR) MODVERDIR=$(KERNEL_OBJ)/.tmp_versions SUBDIRS=$(PWD) modules

mppVhba: kernelsrccheck
	make V=$(V) -C$(KERNEL_OBJ)  M=$(LINUX_RDAC_DIR) MODVERDIR=$(KERNEL_OBJ)/.tmp_versions SUBDIRS=$(PWD) modules

clean:  kernelsrccheck
	make V=$(V) -C$(KERNEL_OBJ)  M=$(LINUX_RDAC_DIR) MODVERDIR=$(KERNEL_OBJ)/.tmp_versions SUBDIRS=$(PWD) clean
	rm -f $(SYS_DEP_INCLUDE)/mppLnx26p_spinlock.h mppLnx_Spinlock_Size
	rm -f Module.symvers
	rm -f mppUtil 
	rm -f genuniqueid

mppUtil: mppLnx_Spinlock_Size $(UTIL_DEPENDS)
	$(CC) mppUtil.o mppUtilSysdep.o -o $@

genuniqueid: genuniqueid.c
	$(CC) -o $@ genuniqueid.c

mppLnx_Spinlock_Size: mppLnx26p_spinlock_size.c
	$(CC) -D__KERNEL__ $(EXTRA_UTIL_FLAGS) $(EXTRA_SPINLOCK_INC) -I$(KERNEL_OBJ)/include mppLnx26p_spinlock_size.c -o mppLnx_Spinlock_Size

mppUtil.o:  $(UTILITY_DIR)/mppUtil.c
	@echo "/* This file is automatically generated by the makefile */" > $(SYS_DEP_INCLUDE)/mppLnx26p_spinlock.h;
	@echo "typedef struct {"                >> $(SYS_DEP_INCLUDE)/mppLnx26p_spinlock.h;
	@echo "  unsigned char pack_bytes["     >>  $(SYS_DEP_INCLUDE)/mppLnx26p_spinlock.h;
	@if [ $(IS_SMP) -eq 1 ]; then \
	./mppLnx_Spinlock_Size                  >> $(SYS_DEP_INCLUDE)/mppLnx26p_spinlock.h; \
	else \
	echo " 0"                               >> $(SYS_DEP_INCLUDE)/mppLnx26p_spinlock.h; \
	fi
	@echo "]; " >> $(SYS_DEP_INCLUDE)/mppLnx26p_spinlock.h;
	@echo "} spinlock_t;"                    >> $(SYS_DEP_INCLUDE)/mppLnx26p_spinlock.h;
	$(CC) $(EXTRA_UTIL_FLAGS) -I$(LINUX_RDAC_DIR) -I$(LINUX_RDAC_DIR)/mpp_linux_headers/ -I$(LINUX_RDAC_DIR)/mpp_linux_sys_headers/ -c $(UTILITY_DIR)/mppUtil.c  -o $@


mppUtilSysdep.o:  $(UTILITY_DIR)/mppUtil26p_sysdep.c $(SYS_DEP_INCLUDE)/mppLnx_bld_version.h
	$(CC) $(EXTRA_UTIL_FLAGS) -I$(LINUX_RDAC_DIR) -I$(LINUX_RDAC_DIR)/mpp_linux_headers/ -I$(LINUX_RDAC_DIR)/mpp_linux_sys_headers/ -c $(UTILITY_DIR)/mppUtil26p_sysdep.c -o $@

$(SYS_DEP_INCLUDE)/mppLnx_bld_version.h:always
	/bin/bash ./genfileattributes bld

always:
