obj-m += smartpqi.o
smartpqi-objs := smartpqi_init.o smartpqi_sis.o smartpqi_sas_transport.o smartpqi_kernel_compat.o

ifeq (${KVER},)
	KVER := $(shell echo `uname -r`)
endif
KVER_SHORT := $(shell echo $(KVER) | sed -r -e 's,(^[0-9]+\.[0-9]+).*,\1,')
KVERSION := $(shell echo $(KVER) | sed -r -e 's,(^[0-9]+)\.[0-9]+.*,\1,')
KMAJOR := $(shell echo $(KVER) | sed -r -e 's,^[0-9]+\.([0-9]+).*,\1,')

KERNEL_CLASS := $(shell \
   if [ $(KVERSION) -eq 3 ]; \
   then \
      if [ $(KMAJOR) -lt 5 ]; \
      then \
         echo "KCLASS$(KVERSION)A"; \
      elif [ $(KMAJOR) -lt 11 ]; \
      then \
         echo "KCLASS$(KVERSION)B"; \
      elif [ $(KMAJOR) -lt 19 ]; \
      then \
         echo "KCLASS$(KVERSION)C"; \
      else \
         echo "KCLASS$(KVERSION)D"; \
      fi; \
   elif [ $(KVERSION) -eq 4 ]; \
   then \
      if [ $(KMAJOR) -lt 11 ]; \
      then \
         echo "KCLASS$(KVERSION)A"; \
      elif [ $(KMAJOR) -lt 14 ]; \
      then \
         echo "KCLASS$(KVERSION)B"; \
      else \
         echo "KCLASS$(KVERSION)C"; \
      fi; \
   elif [ $(KVERSION) -eq 5 ]; \
   then \
      if [ $(KMAJOR) -lt 1 ]; \
      then \
        echo "KCLASS$(KVERSION)A"; \
      elif [ $(KMAJOR) -lt 5 ]; \
      then \
        echo "KCLASS$(KVERSION)B"; \
      elif [ $(KMAJOR) -eq 5 ]; \
      then \
        echo "KCLASS$(KVERSION)C"; \
      else \
        echo "KCLASS$(KVERSION)D"; \
      fi; \
   elif [ $(KVERSION) -eq 6 ]; \
   then \
      if [ $(KMAJOR) -lt 1 ]; \
      then \
        echo "KCLASS$(KVERSION)A"; \
      elif [ $(KMAJOR) -lt 14 ]; \
      then \
        echo "KCLASS$(KVERSION)B"; \
      else \
        echo "KCLASS$(KVERSION)C"; \
      fi; \
   elif [ $(KVERSION) -eq 7 ]; \
   then \
      echo "KCLASS$(KVERSION)A"; \
   fi; \
)

OSVENDOR := $(shell \
   if [ -f /etc/redhat-release ]; \
   then \
      if [ -n "`grep -E 'Coughlan|release 10' /etc/redhat-release`" -a \
           $(KVER_SHORT) = "6.12" -a -n "`echo $(KVER) | grep -v uek`" ]; \
      then \
         UPDATE=`grep "release 10" /etc/redhat-release | \
            sed -r -e 's,^.* release 10.([0-9]+).*,\1,'`; \
         echo "RHEL10U$${UPDATE}"; \
      elif [ -n "`grep -E 'Plow|release 9' /etc/redhat-release`" -a \
           $(KVER_SHORT) = "5.14" ]; \
      then \
         UPDATE=`grep "release 9" /etc/redhat-release | \
            sed -r -e 's,^.* release [0-9]+.([0-9]+).*,\1,'`; \
         echo "RHEL9U$${UPDATE}"; \
      elif [ -n "`grep -E 'Ootpa|release 8' /etc/redhat-release`" -a \
           $(KVER_SHORT) = "4.18" ]; \
      then \
         UPDATE=`grep "release 8" /etc/redhat-release | \
            sed -r -e 's,^.* release [0-9]+.([0-9]+).*,\1,'`; \
         echo "RHEL8U$${UPDATE}"; \
      elif [ -n "`grep 'AltArch' /etc/redhat-release | grep 'release 7'`" -a \
           $(KVER_SHORT) = "4.18" ]; \
      then \
         echo "CENTOS7ALTARM"; \
      elif [ -n "`grep -E 'Maipo|release 7' /etc/redhat-release`" -a \
           $(KVER_SHORT) = "3.10" ]; \
      then \
         UPDATE=`grep "release 7" /etc/redhat-release | \
            sed -r -e 's,^.* release [0-9]+.([0-9]+).*,\1,'`; \
         echo "RHEL7U$${UPDATE}"; \
      elif [ -n "`grep -E 'Santiago|release 6' /etc/redhat-release`" -a \
             $(KVER_SHORT) = "2.6" ]; \
      then \
         UPDATE=`cat /etc/redhat-release | \
            sed -r -e 's,^.* release [0-9]+.([0-9]+).*,\1,'`; \
         echo "RHEL6U$${UPDATE}"; \
      else \
         echo "KERNEL"; \
      fi; \
   elif [ -f /etc/SuSE-release ]; \
   then \
      if [ -n "`grep 'Enterprise Server 12' /etc/SuSE-release`" -a \
           $(KVER_SHORT) = "3.12" -o $(KVER_SHORT) = "4.12" ]; \
      then \
         PATCHLEVEL=`grep PATCHLEVEL /etc/SuSE-release | awk '{ print $$3 }'`; \
         echo "SLES12SP$${PATCHLEVEL}"; \
      elif [ -n "`grep 'Enterprise Server 11' /etc/SuSE-release`" -a \
             $(KVER_SHORT) = "2.6" -o $(KVER_SHORT) = "3.0" ]; \
      then \
         PATCHLEVEL=`grep PATCHLEVEL /etc/SuSE-release | awk '{ print $$3 }'`; \
         echo "SLES11SP$${PATCHLEVEL}"; \
      else \
         echo "KERNEL"; \
      fi; \
   elif [ -f /etc/openEuler-release ]; \
   then \
      if [ -n "`grep 'release 20.03' /etc/openEuler-release`" -a \
           $(KVER_SHORT) = "4.19" ]; \
      then \
         echo "OEULER2003"; \
      elif [ -n "`grep 'release 22.03' /etc/openEuler-release`" -a \
             $(KVER_SHORT) = "5.10" ]; \
      then \
         echo "OEULER2203"; \
      elif [ -n "`grep 'release 24.03' /etc/openEuler-release`" -a \
             $(KVER_SHORT) = "6.6" ]; \
      then \
         echo "OEULER2403"; \
      fi; \
   elif [ -f /etc/os-release ]; \
   then \
      if [ -n "`grep 'SUSE Linux Enterprise Server' /etc/os-release`" ]; \
      then \
         SVER=`grep "^VERSION_ID=" /etc/os-release | cut -d "\"" -f 2`; \
         if [ -n "`echo $$SVER | grep "\."`" ]; \
         then \
            MAJORV=`echo $$SVER | cut -d "." -f 1`; \
            PATCHLEVEL=`echo $$SVER | cut -d "." -f 2`; \
         else \
            MAJORV=$$SVER; \
            PATCHLEVEL="0"; \
         fi; \
         echo "SLES$${MAJORV}SP$${PATCHLEVEL}"; \
      elif [ -n "`grep 'VERSION="V10 (Sword)"' /etc/os-release`" -a \
             $(KVER_SHORT) = "4.19" ]; \
      then \
         echo "K10SP2"; \
      else \
         echo "KERNEL"; \
      fi; \
   else \
      echo "KERNEL"; \
   fi; \
)

COMPAT_SWITCH := $(shell \
   if [ "$(OSVENDOR)" = "KERNEL" ]; \
   then \
      echo "ccflags-y+=-D$(KERNEL_CLASS)"; \
   else \
      echo "ccflags-y+=-D$(OSVENDOR)"; \
   fi; \
)
ifeq (${KDIR},)
   KDIR := /lib/modules/$(KVER)
endif
KBDIR := ${KDIR}/build
PWD   := $(shell pwd)

BLK := $(shell \
   GF=`find -L $(KDIR) -name blkdev.h -o -name blk-mq.h | xargs grep blk_rq_is_passthrough | tail -1`; \
   echo $$GF; \
)

MORE_CFLAGS := $(shell \
   if [ -z "$(BLK)" ]; \
   then \
      echo "ccflags-y+=-DKFEATURE_NEEDS_BLK_RQ_IS_PASSTHROUGH"; \
   fi; \
)

FALLTHRU := $(shell \
   GF=`find -L $(KDIR)/*/include/linux -type f -print | xargs grep '__has_attribute(__fallthrough__)'`; \
   echo $$GF; \
)

MORE_CFLAGS += $(shell \
   if [ ! -z "$(FALLTHRU)" ]; \
   then \
      echo "ccflags-y+=-Wno-implicit-fallthrough"; \
   fi; \
)

MORE_CFLAGS += $(shell \
   GF=`find -L $(KDIR) -name scsi_host.h | xargs grep scsi_done | tail -1`; \
   if [ -z "$$GF" ]; \
   then \
      echo -n "ccflags-y+=-DKFEATURE_HAS_GLOBAL_SCSI_DONE"; \
   fi; \
)

MORE_CFLAGS += $(shell \
   GF=`find -L $(KDIR) -name scsi_cmnd.h | xargs grep scsi_cmd_to_rq | tail -1`; \
   if [ ! -z "$$GF" ]; \
   then \
      echo -n "ccflags-y+=-DKFEATURE_HAS_SCSI_CMD_TO_RQ"; \
   fi; \
)

MORE_CFLAGS += $(shell \
   GF=`find -L $(KDIR) -name scsi_cmnd.h | xargs grep scsi_cmd_priv | tail -1`; \
   if [ ! -z "$$GF" ]; \
   then \
      echo -n "ccflags-y+=-DKFEATURE_HAS_SCSI_CMD_PRIV"; \
   fi; \
)

MORE_CFLAGS += $(shell \
   GF=`find -L $(KDIR) -name scsi_host.h | xargs awk '/^struct scsi_host_template[^_a-zA-Z0-9]/{in_t=1} in_t && /init_cmd_priv/{print "y"; exit} in_t && /^\};/{in_t=0}' 2>/dev/null | tail -1`; \
   if [ ! -z "$$GF" ]; \
   then \
      echo -n "ccflags-y+=-DKFEATURE_HAS_INIT_CMD_PRIV"; \
   fi; \
)

MORE_CFLAGS += $(shell \
   GF=`find -L $(KDIR) -name scsi_host.h | xargs grep shost_groups | tail -1`; \
   if [ ! -z "$$GF" ]; \
   then \
      echo -n "ccflags-y+=-DKFEATURE_HAS_SDEV_GROUPS"; \
   fi; \
)

MORE_CFLAGS += $(shell \
   GF=`find -L $(KDIR) -name scsi_host.h | xargs grep host_tagset | tail -1`; \
   if [ ! -z "$$GF" ]; \
   then \
      echo -n "ccflags-y+=-DKFEATURE_HAS_HOST_TAGSET_SUPPORT"; \
   fi; \
)

MORE_CFLAGS += $(shell \
    GF=`find -L $(KDIR) -name scsi_host.h | xargs grep 'void scsi_rescan_device' | tail -1`; \
    if [ ! -z "$$GF" ]; \
    then \
       echo -n "ccflags-y+=-DKFEATURE_HAS_OLDER_SCSI_RESCAN_DEVICE"; \
    fi; \
)

# Check for strlcpy
MORE_CFLAGS += $(shell \
    GF=`find -L $(KDIR) -path *include/linux* -not -path "*/tools/*" -not -path "*/usr/*"  -name string.h | xargs grep 'size_t strlcpy' | tail -1`; \
    if [ ! -z "$$GF" ]; \
    then \
       echo -n "ccflags-y+=-DKFEATURE_HAS_OLD_STRLCPY"; \
    fi; \
)

MORE_CFLAGS += $(shell \
    GF=`find -L $(KDIR) -path *include/linux* -name unaligned.h | tail -1`; \
    if [ ! -z "$$GF" ]; \
    then \
       echo -n "ccflags-y+=-DKFEATURE_LOCATION_LINUX_UNALIGNED"; \
    fi; \
)

MORE_CFLAGS += $(shell \
   GF=`find -L $(KDIR) -name timer.h | xargs grep timer_delete_sync | tail -1`; \
   if [ ! -z "$$GF" ]; \
   then \
      echo -n "ccflags-y+=-DKFEATURE_HAS_TIMER_DELETE_SYNC"; \
   fi; \
)

MORE_CFLAGS += $(shell \
   GF=`find -L $(KDIR) -name timer.h | xargs grep timer_container_of | tail -1`; \
   if [ ! -z "$$GF" ]; \
   then \
      echo -n "ccflags-y+=-DKFEATURE_HAS_TIMER_CONTAINER"; \
   fi; \
)

MORE_CFLAGS += $(shell \
   GF=`find -L $(KDIR) -path "*/scsi/scsi.h" | xargs grep scsi_qc_status | tail -1`; \
   if [ ! -z "$$GF" ]; \
   then \
      echo -n "ccflags-y+=-DKFEATURE_HAS_SCSI_QC_STATUS"; \
   fi; \
)

default:
	$(MAKE) -C $(KBDIR) M=$(PWD) $(COMPAT_SWITCH) $(MORE_CFLAGS) modules

clean:
	$(MAKE) -C $(KBDIR) M=$(PWD) clean

install:
	$(MAKE) -C $(KBDIR) M=$(PWD) modules_install

