#
# Copyright (c) 2011-2018, 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.
#

KMOD = ocs_fc_scst
OS		= ../common
OCS		= ../../common/ocs
HAL		= ../../common/hal
XPORT		= ../../common/ocs_fc
INITIATOR	= ../common/ocs_ml_fc
TARGET		= ../common/ocs_scst

# 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 -DOCS_INCLUDE_DEBUG -DNEW_API -Werror
EXTRA_CFLAGS += -Wextra -Wno-missing-field-initializers -Wall -Wno-unused-parameter -Wno-ignored-qualifiers
EXTRA_CFLAGS += -Wframe-larger-than=2048

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

#Increase the number of SGLs available to account for dif separates
EXTRA_CFLAGS += -DOCS_FC_MAX_SGL=192

#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_fc.o \
	$(OS)/ocs_lnx.o \
	$(OS)/ocs_os.o \
	$(XPORT)/ocs_nvme_stub.o \
	$(XPORT)/ocs_driver.o \
	$(XPORT)/ocs_debug.o \
	$(XPORT)/ocs_domain.o \
	$(XPORT)/ocs_sport.o \
	$(XPORT)/ocs_node.o \
	$(XPORT)/ocs_elxu.o \
	$(XPORT)/ocs_unsol.o \
	$(XPORT)/ocs_io.o \
	$(XPORT)/ocs_scsi.o \
	$(XPORT)/ocs_els.o \
	$(XPORT)/ocs_fabric.o \
	$(XPORT)/ocs_device.o \
	$(XPORT)/ocs_ddump.o \
	$(XPORT)/ocs_xport.o \
	$(XPORT)/ocs_mgmt.o \
	$(XPORT)/spv.o \
	$(HAL)/sli4.o \
	$(HAL)/sli4_fc.o \
	$(HAL)/ocs_sm.o \
	$(HAL)/ocs_hal.o \
	$(HAL)/ocs_hal_rqpair.o \
	$(HAL)/ocs_hal_queues.o \
	$(HAL)/ocs_hal_workaround.o \
	$(TARGET)/ocs_scst.o \
	$(INITIATOR)/ocs_ml_fc.o \
	$(INITIATOR)/ocs_lnx_xport_fc.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_fc.o \
	$(OCS)/ocs_gendump.o \
	$(OCS)/ocs_ras.o \
	$(OCS)/ocs_recovery.o \
	$(OCS)/ocs_ddumplib.o \
	$(OCS)/ocs_pm.o \
        $(OCS)/ocs_cbuf.o \
        $(OCS)/ocs_array.o \
	$(OCS)/ocs_pool.o


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

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

all:
	cp $(SCST_SYMVER)/Module.symvers .
	make -j4 -C /lib/modules/$(KERN_VERS)/build M=$(PWD) modules

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_fc_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

