#
# Copyright (c) 2011-2015, Emulex
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice,
#    this list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
#    this list of conditions and the following disclaimer in the documentation
#    and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its contributors
#    may be used to endorse or promote products derived from this software
#    without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
# POSSIBILITY OF SUCH DAMAGE.
#

# Include ocs_template to get the template structures, and the template back-end
# initiator driver

KMOD = ocs_iscsi_scst
OS		= ../common
OCS		= ../../common/ocs
HAL		= ../../common/ihal
XPORT		= ../../common/ocs_iscsi
TARGET		= ../common/ocs_scst
INITIATOR	= ../../common/ocs_template
OCS_OPENISCSI	= ../ocs_openiscsi/

# Need three different variables:
# SCST_PREFERRED is for testing inside a target.  Can't use echo or other commands outside a target.
# SCST_INCLUDE is for setting the include path, and can change if the preferred directory is not available and uses $(src)
# SCST_SYMVER is for copying Module.symvers.  It can also change if peferred is not available, and does not use $(src)
SCST_PREFERRED = /usr/local/include/scst
SCST_INCLUDE = $(SCST_PREFERRED)
SCST_SYMVER = $(SCST_INCLUDE)

ifndef KERN_VERS
       KERN_VERS=$(shell uname -r)
endif
ifndef VERSION
        VERSION=$(shell pwd|cut -f5 -d/)
endif

# If the SCST files are installed, use those, otherwise use a saved copy.
# This is to allow the driver to be built on systems that have not installed the SCST package.
# If SCST_INCLUDE does not exist, then the wildcard test will evaluate to an empty string.
ifeq ($(wildcard $(SCST_INCLUDE)),)
	SCST_INCLUDE = $(src)/$(TARGET)/scst_include
	SCST_SYMVER = $(TARGET)/scst_include
endif

EXTRA_CFLAGS += -g -Werror -DOCS_INCLUDE_DEBUG -DOCS_QUEUE_LIMIT=256
EXTRA_CFLAGS += -Wextra -Wno-missing-field-initializers -Wall -Wno-unused-parameter -Wno-ignored-qualifiers

## Add For debugging.  Remove for performance. See SCST README file.
# EXTRA_CFLAGS += -DCONFIG_SCST_EXTRACHECKS

#Increase the timeout for domain shutdown to 180 seconds.
#It can take some time to clean up if there are a large number of IOs active to abort
#when the driver is unloaded.
#EXTRA_CFLAGS += -DOCS_FC_DOMAIN_SHUTDOWN_TIMEOUT_USEC=180000000

SRC_TAR = $(KMOD)-src.tgz
DOC_TAR = $(KMOD)-doc.tgz

obj-m := $(KMOD).o

ccflags-y := -I$(src) -I$(src)/$(TARGET) -I$(src)/$(OCS) -I$(src)/$(HAL) -I$(src)/$(XPORT) -I$(src)/$(OS) -I$(src)/$(INITIATOR) -isystem $(SCST_INCLUDE)

$(KMOD)-y := \
	ocs_scst_iscsi.o \
	$(OS)/ocs_lnx.o \
	$(OS)/ocs_os.o \
	$(XPORT)/ocs_driver.o \
	$(XPORT)/ocs_debug.o \
	$(XPORT)/ocs_iscsi.o \
	$(XPORT)/ocs_iscsi_api.o \
	$(XPORT)/ocs_cxn.o \
	$(XPORT)/ocs_login.o \
	$(XPORT)/ocs_elxu.o \
	$(XPORT)/ocs_scsi.o \
	$(XPORT)/ocs_session.o \
	$(XPORT)/ocs_ddump.o \
	$(XPORT)/ocs_domain.o \
	$(XPORT)/ocs_sport.o \
	$(XPORT)/ocs_io.o \
	$(XPORT)/ocs_mgmt.o \
	$(XPORT)/ocs_md5.o \
	$(XPORT)/ocs_xport.o \
	$(XPORT)/ocs_isns.o \
	$(HAL)/ocs_hal.o \
	$(HAL)/ocs_sm.o \
	$(HAL)/sli4.o \
	$(HAL)/sli4_iscsi.o \
	$(HAL)/sli4_be3_iscsi.o \
	$(HAL)/sli4_skh_iscsi.o \
	$(HAL)/ocs_hal_workaround.o \
	$(TARGET)/ocs_scst.o \
	$(INITIATOR)/ocs_ini_template.o \
	$(OCS)/ocs_textbuf.o \
	$(OCS)/ocs_mgmt_common.o \
	$(OCS)/ocs_ramlog.o \
	$(OCS)/ocs_dif.o \
	$(OCS)/t10crc16.o \
	$(OCS)/ocs_list.o \
	$(OCS)/ocs_ioctl.o \
	$(OCS)/ocs_ioctl_iscsi.o \
	$(OCS)/ocs_ddumplib.o \
	$(OCS)/ocs_pm.o

clean-files := $($(KMOD)-y)

# files from this directory to include in a release
RELEASE_FILES = \
	Doxyfile \
	DoxygenLayout.xml \
	main.doxygen \
	Makefile \
	ocs_iscsi_scst-doc.tgz \
	ocs.h \
	ocs_scst_iscsi.c \
	version.h

all:
	cp $(SCST_SYMVER)/Module.symvers .
	make -C /lib/modules/$(KERN_VERS)/build M=$(PWD) modules
	@sh $(OCS_OPENISCSI)/ocs_gen_incl.sh $(OCS_OPENISCSI) "$(TARGET)" "$(PWD)/Module.symvers"

clean:
	make -C /lib/modules/$(KERN_VERS)/build M=$(PWD) $@
	@rm -f $(SRC_TAR) $(DOC_TAR)
	@rm -rf doc doxygen.log

list_release_files:
	@echo $(RELEASE_FILES)

docdist:
	@echo "Creating document tar file: ${KMOD}-doc.tgz"
	@doxygen > doxygen.log 2>&1
	@cp ../../common/doxy/images/linux_ocs_iscsi_scst/*.jpg doc/html
	@cp ../../common/doxy/images/common/*.png doc/html
	@tar cfz ${DOC_TAR} doc

install:
	@echo "Installing $(KMOD).ko"
	@cp $(KMOD).ko /lib/modules/$(KERN_VERS)/extra
	@/sbin/depmod -a

uninstall:
	@echo "Removing $(KMOD).ko"
	@rm -I /lib/modules/$(KERN_VERS)/extra/$(KMOD).ko
	@/sbin/depmod -a
