#*****************************************************************************
# Copyright 2004-2007 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_makefile26
# instance: 1
# created by:	yqi
#    %version: 35 %
#    %derived_by:        skashyap %
#    %date_created:      Wed May 23 03:27:51 2007 %
#
# description: 
#
# 
#
#######################################################################

INSTALL=/usr/bin/install
OS_VER := 2.6.9-67.0.7.EL_lustre.1.6.5.1smp
HOST_TYPE := $(shell uname -m)
# Kernel Distribution (either REDHAT or SUSE)
DIST := $(shell (if [ -f /etc/redhat-release ]; then echo REDHAT; else echo SUSE; fi))

#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 (/bin/rpm -qa | grep kernel-source-2.6 | wc -l))
REDHAT_KERNEL_CHECK := $(shell ((cat /etc/issue | grep "release 5" | wc -l) 2> /dev/null))
SUSE_KERNEL_CHECK := $(shell ((cat /etc/issue | grep "10" | grep "SP1" | 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 mppLnx26_upper.o mppLnx26_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=mppLnx26_vhba.o mppLnx26_vhbaio.o mppLnx26_vhbatask.o \
         mppLnx26_vhbalib.o mppLnx26_vhbamisc.o\
         mppLnx26_vhbaproc.o
CC      =$(CROSS_COMPILE)gcc
 
RDAC_RPM_HELPER_DIR=/opt/mpp/.mppLnx_rpm_helpers

EXTRA_SPINLOCK_INC  = -I /lib/modules/$(OS_VER)/source/include

ifeq ("$(KERNEL_OBJ)","")
	KERNEL_OBJ := /lib/modules/$(OS_VER)/source
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.$(DIST) $(DRIVER_TARGETS) $(UTIL_TARGETS)
 
driver	: gcccheck kernelvercheck.$(DIST) kernelsrccheck.$(DIST) $(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 1 ]; then echo "This Version of MPP build is not supported on this Version of Redhat Kernel $(OS_VER)."; exit 1; fi

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

kernelsrccheck.REDHAT:

kernelsrccheck.SUSE:
	@if [ $(KERNEL_SRC_CHK_FLAG) -eq 0 ]; then echo "Kernel source rpm 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/mppBusRescan26 $(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/mppUpdate26 $(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 $(OS_VER)
	
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) $(OS_VER)
	@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 $(OS_VER)


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
 

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

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

clean:
	make V=1 -C$(KERNEL_OBJ)  M=$(LINUX_RDAC_DIR) MODVERDIR=$(KERNEL_OBJ)/.tmp_versions SUBDIRS=$(PWD) clean
	rm -f $(SYS_DEP_INCLUDE)/mppLnx26_spinlock.h mppLnx_Spinlock_Size

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

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

mppLnx_Spinlock_Size: mppLnx26_spinlock_size.c
	$(CC) -D__KERNEL__ $(EXTRA_UTIL_FLAGS) $(EXTRA_SPINLOCK_INC) -I$(KERNEL_OBJ)/include mppLnx26_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)/mppLnx26_spinlock.h;
	@echo "typedef struct {"                  >> $(SYS_DEP_INCLUDE)/mppLnx26_spinlock.h;
	@echo "  unsigned char pack_bytes["     >>  $(SYS_DEP_INCLUDE)/mppLnx26_spinlock.h;
	@./mppLnx_Spinlock_Size                 >> $(SYS_DEP_INCLUDE)/mppLnx26_spinlock.h;
	@echo "]; " >> $(SYS_DEP_INCLUDE)/mppLnx26_spinlock.h;
	@echo "} spinlock_t;"                    >> $(SYS_DEP_INCLUDE)/mppLnx26_spinlock.h;
	$(CC) -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)/mppUtil26_sysdep.c $(SYS_DEP_INCLUDE)/mppLnx26_bld_version.h
	$(CC) -I$(LINUX_RDAC_DIR) -I$(LINUX_RDAC_DIR)/mpp_linux_headers/ -I$(LINUX_RDAC_DIR)/mpp_linux_sys_headers/ -c $(UTILITY_DIR)/mppUtil26_sysdep.c -o $@

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

always:
