#
# This Makefile is intended to be used when the iSCSI driver is
# built on a Linux 2.6.0 Platform. Copy it to Makefile.
#
#   vi: set autoindent tabstop=8 shiftwidth=8 :
#
# Makefile for the Linux UNH iSCSI driver in 2.6 Environment
# 09/10/03 Matt Wilcox. Original draft.
# 09/25/03 John Donnelly At HP dott Com.
#
# Misc notes.
# To Make
# - Within a kernel tree :  Copy the unh_iscsi/src to Linux Kernel
#			under drivers/scsi/unh_iscsi/ . then execute:
#			make V=1 drivers/scsi/unh_iscsi/
# - Standalone: make
#
# To Clean :  make clean
# To create a RPM :  make rpm
#

########################################
# RDMA/iSER support enabled by default.
#
ifndef RDMA
RDMA=yes
#RDMA=no
endif
#
# Using OFED enabled by default
#
ifeq ($(RDMA), yes)
ifndef OFED
OFED=yes
#OFED=no
endif
endif
#
########################################

# use of KERNEL_VER contributed by Charles Coffing, ccoffing@novell.com
ifndef KERNEL_VER
KERNEL_VER := `uname -r`
endif

ifndef KERNEL_DIR
KERNEL_DIR = /lib/modules/$(KERNEL_VER)/build
endif

EXTRA_CFLAGS += -Iinclude/scsi
EXTRA_CFLAGS += -I../common
EXTRA_CFLAGS += -I../security/chap
EXTRA_CFLAGS += -I../security/srp
EXTRA_CFLAGS += -I../security/hash
EXTRA_CFLAGS += -I$(PWD)/datamover/include

EXTRA_CFLAGS += -fsigned-char

# define ISCSI_STATS in order to compile in code that dynamically accumulates
# statistics about the number of pdus of each type send and received, etc.
# also need to define this to display those statistics via /proc
# you probably always want this defined
EXTRA_CFLAGS += -DISCSI_STATS

# define ISCSI_CHECK_MEMORY in order to compile in code that
# tracks all calls to kmalloc and kfree to ensure that something is not
# freed if it has not been previously allocated, and, at the end, prints
# any allocated but unfreed memory and gives the maximum number of
# allocated memory slots -- see common/my_memory.h and common/my_memory.c.
# you want this defined during all development and testing
# undefine it when you are confident there are no memory leaks in the code!
#EXTRA_CFLAGS += -DISCSI_CHECK_MEMORY

# define CONFIG_ISCSI_DEBUG in order to compile in code that enables dynamic
# tracing and printing -- see common/debug.h and common/debug.c for details.
# this code enables tracing to be dynamically and selectively enabled/disabled.
# you want this defined during all development and testing
# undefine it when you want top performance and have completely debugged
# both the target and the initiator.
EXTRA_CFLAGS += -DCONFIG_ISCSI_DEBUG

# define ISCSI_CHECK_PDU_FORMAT in order to compile in code that checks
# the unused/reserved fields of every received pdu to ensure that they have
# been set to the proper value (normally 0, but sometimes 0xffffffff).
# Also checks for other inconsistencies between fields within a pdu,
# and other violations of the standard (such as sequences that are too
# long) which can safely be ignored without damage under the philosopy
# "conservative in what you send, liberal in what you receive".
# see initiator/initiator_rx.c for most uses of this symbol.
# you want this defined during all development and testing
# undefine it when you are sure the other side always builds pdus correctly.
#EXTRA_CFLAGS += -DISCSI_CHECK_PDU_FORMAT

# define UNH_LOCK_DEBUG in order to compile in code that checks the usage
# of the UNH_LOCK and UNH_UNLOCK macros, on both uniprocessors and smp
# machines (this helps detect lock/unlock mismatches even on uniprocessor
# machines where spinlocks are normally nops.
# you want this defined during all development, especially on SMP machines
# undefine it when all the locks have been thoroughly debugged.
#EXTRA_CFLAGS += -DUNH_LOCK_DEBUG

# define MAX_LOOP_COUNT=100 (or some other large number)
# to check that no loop (in the target) has more than 100 iterations
# (useful to catch "infinite" loops when debugging)
#EXTRA_CFLAGS += -DMAX_LOOP_COUNT=100

# define K26 in order to compile in code for the 2.6 kernels (and to compile
# out code for the 2.4 kernels).  When defining this you should MUST be
# using this version of the Makefile (Makefile-26) -- see README.
# you ALWAYS want this defined when using this Makefile!
EXTRA_CFLAGS += -DK26

# define FC2 in order to compile in code for the 2.6 kernels in Fedora Core 2.
# For some reason, RedHat has added an extra (5th) parameter to the
# kernel function sock_create()!  The standard kernel has only 4 parameters.
# This symbol is referenced in only 2 files: initiator/iscsi_initiator.c and
# target/iscsi_target.c (because those are the only 2 places where
# sock_create() is called).
# undefine it ONLY when you are compiling for Fedore Core 2 and the 2.6 kernel.
#EXTRA_CFLAGS += -DFC2

EXTRA_LDFLAGS += -t

ifeq ($(RDMA), yes)
# define RDMA_MODE in order to compile in code to interface with iSER.
# This expects an independent iSER module to already be loaded before
# the iSCSI modules are loaded.  When this is not defined, code is
# compiled for the ordinary TCP socket interface.
EXTRA_CFLAGS += -DRDMA_MODE

# define TCP_ISER in order to compile in code to test the iSER interface
# without actually using an independent iSER module.  Instead, the iSER
# code is simulated using the ordinary TCP socket interface.
#EXTRA_CFLAGS += -DTCP_ISER

DATAMOVER_OBJS := $(addprefix datamover/,	\
	iser_conn.o				\
	iser_dto.o				\
	iser_global.o				\
	iser_initiator.o			\
	iser_memory.o				\
	iser_mod.o				\
	iser_ofa.o				\
	iser_ofa_kernel.o			\
	iser_pdu.o				\
	iser_target.o				\
	iser_task.o				\
	iser_utils.o				\
	iser_x86.o)
endif # RDMA == yes

SECURITY_OBJS := $(addprefix security/, chap/chap.o hash/sha1.o hash/md5.o \
	math/bigint.o misc/misc_func.o srp/srp.o)

ICOMMON_OBJS := $(addprefix common/, iscsi_common.o range.o \
	crc.o tcp_utilities.o text_param.o initiator_negotiate.o \
	ffp_negotiate.o)

TCOMMON_OBJS := $(addprefix common/, iscsi_common.o range.o \
	crc.o tcp_utilities.o text_param.o target_negotiate.o \
	ffp_negotiate.o)

COMMON_OBJS := $(addprefix common/, lun_packing.o my_memory.o debug.o \
	iscsi_common.o debugfs.o)

INITIATOR_OBJS := $(addprefix initiator/,iscsi_initiator.o \
	initiator_utilities.o initiator_error_rec.o initiator_proc_iface.o \
	initiator_rx.o initiator_tx.o iscsi_device.o iser_initiator.o)

ITARGET_OBJS := $(addprefix target/, iscsi_target.o target_error_rec.o \
	iscsi_portal_group.o iser_target.o iscsi_cmnd.o scsi_midlevel.o \
	iscsi_rx.o iscsi_tx.o iscsi_target_module.o)

SCSI_OBJS := $(addprefix target/, scsi_target.o scsi_memoryio_target.o \
	scsi_diskio_target.o scsi_fileio_target.o scsi_genericio_target.o)

unh_iscsi_initiator-objs   := $(SECURITY_OBJS) $(ICOMMON_OBJS) $(COMMON_OBJS) \
         $(INITIATOR_OBJS) $(DATAMOVER_OBJS)

unh_iscsi_target-objs   := $(SECURITY_OBJS) $(TCOMMON_OBJS) $(COMMON_OBJS) \
         $(ITARGET_OBJS) $(DATAMOVER_OBJS)

unh_scsi_target-objs   := $(COMMON_OBJS) $(SCSI_OBJS)

obj-m += unh_iscsi_initiator.o unh_iscsi_target.o unh_scsi_target.o

all: Module.symvers iscsi cmd
	@echo
	@echo "RDMA: $(RDMA)"
	@echo "OFED: $(OFED)"

ifeq ($(OFED), yes)
OFA=/usr/src/ofa_kernel-1.3
Module.symvers:
	cp /usr/src/ofa_kernel/Module.symvers .
else
OFA=$(KERNEL_DIR)
Module.symvers:

endif

# use of KERNEL_VER contributed by Charles Coffing, ccoffing@novell.com
version:
	$(shell /bin/echo "#define OUR_NAME \"UNH-IOL iSCSI Initiator. RPM: `cat initiator/RPM | grep -v ^#`. Kernel: $(KERNEL_VER). Built on `date`. \"" > initiator/version.h)

cscope:
	find . -name "*.[csh]" -print > cscope.files
	 cscope -b
iscsi: version
	make -C $(KERNEL_DIR) SUBDIRS=`pwd` LINUXINCLUDE=" -I$(OFA)/include -Iinclude -include include/linux/autoconf.h -include $(OFA)/include/linux/autoconf.h"
	make -C cmd
cmd:
	make -C cmd

.PHONY: clean

#
# Standard clean
#
clean:

	@for i in ` find . -name "*.ko" -o -name ".*.o.*" -o  -name "*.o"`; do \
	(if [ -f $$i ] ; \
		then \
			echo $$i; \
			rm $$i ; \
			else \
		: ;\
	fi); \
	done
	rm -rf .tmp_versions
	rm -f initiator/version.h
	make -C cmd clean
	rm -f *.mod.c .*cmd
	rm -f Module.symvers

rpm: clean
	tar cf /usr/src/redhat/SOURCES/unh_iscsi.tar  .
	gzip -f /usr/src/redhat/SOURCES/unh_iscsi.tar
	rpmbuild -ba unh_iscsi.specfile

# For SuSE && UnitedLinux - Note pathname differences
suse_rpm: clean
	tar cf /usr/src/packages/SOURCES/unh_iscsi.tar  .
	gzip -f /usr/src/packages/SOURCES/unh_iscsi.tar
	rpm -ba unh_iscsi.specfile


tarball: clean
#
# Generate a tarball named from the spec file (unh_iscsi-1.5.03.tar.gz)
#
	cmd/tar.sh
