#
# 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_ramd
OS		= ../common
OCS		= ../../common/ocs
HAL		= ../../common/hal
XPORT		= ../../common/ocs_fc
TARGET		= ../../common/ocs_ramd
INITIATOR	= ../common/ocs_ml_fc

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

#
# Allow cross-compiling the driver by specifying CROSS=xyz
# e.g. make CROSS=arm64
#
ifdef CROSS
ifeq ($(CROSS),arm64)
XCOMPILE=CROSS_COMPILE=aarch64-linux-gnu- ARCH=arm64
else
$(error "Unsupported architecture $(CROSS)")
endif
endif

#
# Allow specifying an alternate kernel source directory
# e.g. make KERN_SRC=/usr/src/linux-arm64
#
KERN_SRC ?= /lib/modules/$(KERN_VERS)/build

EXTRA_CFLAGS += -g -DOCS_INCLUDE_DEBUG -Werror
EXTRA_CFLAGS += -Wextra -Wno-missing-field-initializers -Wall -Wno-unused-parameter -Wno-ignored-qualifiers
EXTRA_CFLAGS += -DENABLE_FABRIC_EMULATION
EXTRA_CFLAGS += -DOCS_EXCLUDE_BE3
#EXTRA_CFLAGS += -DOCS_DISC_SPIN_DELAY
#EXTRA_CFLAGS += -DOCS_ENABLE_FIRST_BURST

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

obj-m := $(KMOD).o

ccflags-y := -I$(src) -I$(src)/$(OCS) -I$(src)/$(HAL) -I$(src)/$(XPORT) -I$(src)/$(TARGET) -I$(src)/../common -I$(src)/$(INITIATOR)

$(KMOD)-y := \
	$(OS)/ocs_lnx.o \
	$(OS)/ocs_os.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_mgmt.o \
	$(XPORT)/ocs_nvme_stub.o \
	$(XPORT)/ocs_xport.o \
	$(XPORT)/ocs_femul.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 \
	$(OCS)/ocs_mgmt_common.o \
	$(OCS)/ocs_textbuf.o \
	$(OCS)/ocs_ramlog.o \
	$(OCS)/ocs_dif.o \
	$(OCS)/t10crc16.o \
	$(OCS)/ocs_list.o \
	$(OCS)/ocs_gendump.o \
	$(OCS)/ocs_recovery.o \
	$(OCS)/ocs_ioctl.o \
	$(OCS)/ocs_ioctl_fc.o \
	$(OCS)/ocs_pm.o \
	$(OCS)/ocs_ddumplib.o \
	$(OCS)/ocs_cbuf.o \
	$(OCS)/ocs_array.o \
	$(OCS)/ocs_pool.o \
	$(OCS)/ocs_ras.o \
	$(TARGET)/ocs_ramdisc.o \
	$(TARGET)/ocs_ramd.o \
	$(TARGET)/ocs_ramd_thread.o \
	$(TARGET)/mode_sense.o \
	$(INITIATOR)/ocs_ml_fc.o \
	$(INITIATOR)/ocs_lnx_xport_fc.o

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

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


all:
	make $(XCOMPILE) -j4 -C $(KERN_SRC) M=$(PWD) modules

clean:
	make $(XCOMPILE) -C $(KERN_SRC) 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/common/*.png doc/html
	@cp ../../common/doxy/images/linux_ocs_fc_ramd/*.jpg doc/html
	@cp ../../common/doxy/images/linux_ocs_fc_ramd/*.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

