#!/bin/bash
#
# This shell script takes care of the ICEPIC driver - picdrv.  See icepic.hlp
#
#    added support for systemd
#
# description: make, installation, and init script for the ICEPIC driver
#
#   /sys/devices/system/node/online
#
#    added comment lines parsed by chkconfig for init management under
#    IRIX, late Solaris, and RedHat-based Linux. Suse uses INIT INFO block
# chkconfig: 345 05 05
#
### BEGIN INIT INFO
# Provides: icepic
# Required-Start:
# Required-Stop:
# X-United-Linux-Required-Start: $ALL
# Default-Start: 3 4 5
# Default-Stop: 0 1 2 6
# Description: IcePic Card Drive Configuration at boot or hotplug
### END INIT INFO

# global strings
module="picdrv"
group="sys"
mode="666"
ostype="UNIX"
osname="LINX"

# location for executables and optional icepic_pre, icepic_post, and icepic_check scripts
bindir="/usr/bin"
NRLIM=10
rbfile="/var/local/.icereboot"
unitfile="/etc/systemd/system/icepic.service"

# configuration options
chk=-1
smp=-1
kmv=-1
kam=-1
vma=2
new=1
mdr=0
mxd=12
lat=10

# DMA RAM setup default parameters (USE kernel boot parameters to over-ride)
#   ram_alloc = DMA RAM allocator {0=Manual, 1=Kernel<4G, 2=Kernel>4G, 3=Kernel>ram_start, 4=PreKernel}
#   ram_start = IceDisk RAM Start in Mbytes (-1 or AUTO for top of the OS ram set by mem=xxxM in boot cmdline)
#   ram_sized = IceDisk RAM in Mbytes (-1 or AUTO for all memory available above mem=xxxM)
#   ram_sizem = Mappable RAM in Mbytes (at least 24 for PIC self tests)
#
ram_alloc=0
ram_nodes=1
ram_start=-1
ram_sized=-1
ram_sizem=-1

minrate=-1
maxrate=-1

if [ $ram_alloc -ge 1 ]; then
  ram_start=AUTO
  ram_sized=1000
  ram_sizem=24
else
  ram_start=AUTO
  ram_sized=AUTO
  ram_sizem=24
fi

# boot cmdline overrides
CMDMEM=0
CMDMAP="NONE"
CMDLN=`cat /proc/cmdline`
if [ "$1" == "test" ]; then CMDLN=$2; fi
ARG2=$2
for arg in $CMDLN
do
  values=`echo $arg | sed s/.*=//`
  value=`echo $values | sed s/M.*//`
  # echo " Arg=$arg Values=$values Value=$value"
  case "$arg" in
    "mem="*)    CMDMEM=$value ;;
    "memmap="*) CMDMAP=$values ;;
    "ice.ra="*) ram_alloc=$value ;;
    "ice.rn="*) ram_nodes=$value ;;
    "ice.rs="*) ram_start=$value ;;
    "ice.rd="*) ram_sized=$value ;;
    "ice.rm="*) ram_sizem=$value ;;
    "ice.lat="*) lat=$value ;;
    "ice.nc="*) mdr=$value ;;
    "ice.mc="*) mxd=$value ;;
    "ice.minrate="*) minrate=$value ;;
    "ice.maxrate="*) maxrate=$value ;;
    "picdrv.ice_ra="*) ram_alloc=$value ;;
    "picdrv.ice_rn="*) ram_nodes=$value ;;
    "picdrv.ice_rs="*) ram_start=$value ;;
    "picdrv.ice_rd="*) ram_sized=$value ;;
    "picdrv.ice_rm="*) ram_sizem=$value ;;
    "picdrv.ice_lat="*) lat=$value ;;
  esac
done

# auto install branch
if [ "$1" == "all" ]; then
  $0 make
  $0 remove
  $0 install
  $0 start
  exit 0
fi

echo "Ice RAM Settings: Alloc=$ram_alloc Nodes=$ram_nodes Start=$ram_start Disk=$ram_sized Mappable=$ram_sizem"

# now convert all parameters to numeric
if [ "$ram_start" == "AUTO" ]; then ram_start=-1; fi
if [ "$ram_sized" == "AUTO" ]; then ram_sized=-1; fi
if [ "$ram_alloc" == "AUTO" ]; then ram_alloc=-1; fi
if [ "$ram_alloc" == "MANUAL" ]; then ram_alloc=0; fi
if [ "$ram_alloc" == "KERNEL" ]; then ram_alloc=1; fi

# auto modes cannot address below 4G or above 256G
aram_min=4096
aram_max=262144

# auto detection of manual mode options
if [ $ram_alloc = 0 ]; then
  # determine end of usable ram
  ram_end=$aram_min
  lines=`dmesg | grep BIOS-e820 | grep usable | sed 's/- 0/-0x0/' | sed 's/.*-0x/0x/' | cut -c 1-18 `
  for line in $lines
  do
    let addrM=($line+1)/1048576
    #echo $addrM
    if [ $addrM -gt $ram_end ]; then ram_end=$addrM ; fi
  done
  if [ $ram_end -gt $aram_max ]; then ram_end=$aram_max ; fi

  # process memmap=A$B syntax - make sure $ is in mapmem value
  maptst=`echo $CMDMAP | awk -F $ '{ print $1 }' ` 
  if [ $CMDMAP != $maptst ]; then
    mapsz=`echo $CMDMAP | awk -F $ '{ print $1 }' | sed s/M.*//` 
    mapst=`echo $CMDMAP | awk -F $ '{ print $2 }' | sed s/M.*//` 
    let aram_min=$mapst
    let aram_max=$mapst+mapsz
  fi

  # automatic ram start ?
  if [ $ram_start = -1 ]; then
    ram_start=$CMDMEM
    if [ $ram_start -lt $aram_min ]; then
      echo "icepic ram_start=-1 only valid for mem=xxxM when xxx >= $aram_min. Setting to $aram_min."
      ram_start=$aram_min
    fi
  fi

  #automatic ram size ?
  if [ $ram_sized = -1 ]; then
    let ram_sized=$ram_end-$ram_sizem-$ram_start
  fi
  let ram_stop=$ram_start+$ram_sized-$ram_sizem
  if [ $ram_stop -gt $aram_max ]; then
    let ram_sized=$aram_max-$ram_sizem-$ram_start
    echo "Limiting ram_sized to $ram_sized to keep under $aram_max limit"
  fi
fi
  
if [ "$1" == "test" ]; then
  echo "Ice RAM Settings: Alloc=$ram_alloc Start=$ram_start Disk=$ram_sized Mappable=$ram_sizem"
  exit 0
fi

numa=1
if [ $ram_nodes -gt 1 ]; then
  nuno=`cat /sys/devices/system/node/online`
  if [ "$nuno" == "0-1" ]; then numa=2; fi
  if [ "$nuno" == "0-3" ]; then numa=4; fi
  if [ "$nuno" == "0-7" ]; then numa=8; fi
  if [ $ram_nodes -gt $numa ]; then
    echo "Asking for $ram_nodes memory nodes with only $numa NUMA nodes present. Aliasing entries across available NUMA nodes."
  fi
fi

if [ -d /etc/init.d ]; then 
  initd=init.d
  rcd=
else
  initd=rc.d/init.d
  rcd=rc.d
fi

if [ $new = 1 ]; then
  kmv=1
fi

if [ $smp = -1 ]; then
  if echo `uname -a` | grep -q SMP ; then
    smp=1
  else
    smp=0
  fi
fi

if [ $kmv = -1 ]; then
  if /sbin/ksyms -a | grep pci_devices | grep -q _R ; then
    kmv=1
  else
    kmv=0
  fi
fi

if [ $kam = -1 ]; then
  if [ -f icekam.cobf ]; then
    kam=2
  else
    kam=0
  fi
fi

if [ $vma = -1 ]; then
  vma=1
fi

if [ $chk = -1 ]; then
  sysd=`ps --pid 1 -o comm=`
  if [ $sysd == "systemd" ]; then
    chk=3
  elif hash chkconfig 2> /dev/null; then
    chk=1
  elif hash update-rc.d 2> /dev/null; then
    chk=2
  else 
    chk=0
  fi
fi

# check to insure systemd start|stop are from the unit file
if [ "$1" == "start" -o "$1" == "stop" ]; then
  if [ $chk = 3 -a "$2" != "1" ]; then
    echo "Using 'systemctl start|stop icepic'"
    systemctl $1 icepic
    exit 1
  fi
fi

# reboot file counter incrementer 
rbinc() {
  nrebs=0
  # check if reboot file exists
  if [[ -f $rbfile ]]; then
    # read the file, check for consecutive # of reboots
    while read line
    do  
      # look for the number of reboots
      if [[ $line == NR=* ]]; then
        nrebs=${line:3}
        break
      fi  
    done < "$rbfile"
    if (( $nrebs < 0 )) || (( $nrebs > $NRLIM )); then
      nrebs=$NRLIM
    fi
  fi  

  # echo number of reboots into the reboot file
  let "nrebs += 1"
  printf "NR=$nrebs\n" > $rbfile
  sync
}

# function to check for ICEROOT env variable
geticeroot() {
  # check if variable exists
  if [[ -z $ICEROOT ]]; then
    cdir=$(pwd)
    # look at cwd, then look at some "common" iceroot dirs
    awked=$(echo $cdir | awk -F /icexxx '{ printf $1 }')
    if [[ $awked != $cdir ]]; then
      ICEROOT=$awked/icexxx
    elif [[ -d /opt/ice/pkg/icexxx/ ]]; then
      ICEROOT=/opt/ice/pkg/icexxx
    elif [[ -d /midas/opt/icexxx/ ]]; then
      ICEROOT=/midas/opt/icexxx
    fi
  fi
}

make_drv() {
  geticeroot
  cd $ICEROOT/drv/lnx
  echo "Making ICE driver for SymMultiProc=$smp ModVer=$kmv NewVma=$vma New=$new"
  echo "/* Defines generated by './icepic make' - do NOT edit */" > picdrv.h
  echo "#define RAM_START $ram_start" >> picdrv.h
  echo "#define RAM_SIZED $ram_sized" >> picdrv.h
  echo "#define RAM_SIZEM $ram_sizem" >> picdrv.h
  echo "#define _$ostype 1" >> picdrv.h
  echo "#define _$osname 1" >> picdrv.h
  echo "#define _NEW $new" >> picdrv.h
  echo "#define _VMA $vma" >> picdrv.h
  echo "#define _KAM $kam" >> picdrv.h
  if [ $new != 0 ]; then
    unset ARCH
    echo "Used system Makefile for new=1"
    echo "obj-m := picdrv.o" > Makefile
    if [ "$ARG2" == "bb" ]; then
      echo "#define _ALL 1" >> picdrv.h
      echo "#define _KAM 1" >> picdrv.h
    elif [ "$ARG2" != "" ]; then
      echo "Unzipping precompiled binary blob for ver=$ARG2"
      tar -xzf picdrv_$ARG2.tz
      echo "picdrv-y := picdrv_bb.o" >> Makefile
      if [ ! -f icekam.c ]; then cp dummy.c icekam.c ; fi
      if [ ! -f icebd.c ]; then cp dummy.c icebd.c ; fi
      if [ ! -f icefs.c ]; then cp dummy.c icefs.c ; fi
    fi

    make -C /lib/modules/$(uname -r)/build M=$(pwd)
    if [ "$ARG2" == "bb" ]; then
      cp picdrv.o picdrv_bb.o_shipped
      cp .picdrv.o.cmd .picdrv_bb.o.cmd
      tar -czf picdrv.tz picdrv_bb.o_shipped .picdrv_bb.o.cmd 
      rm picdrv_bb.o* 
    fi
  else
    incdir="/lib/modules/`uname -r`/build/include"
    if [ ! -d $incdir ]; then
      incdir="/usr/src/linux/include"
    fi
    INCLUDES="-I$incdir -I/usr/include -I../../inc"
    FLAGS="-D__KERNEL__ -DMODULE -DEXPORT_SYMTAB"
    if [ $kmv != 0 ]; then
      INCLUDES="$INCLUDES -include $incdir/linux/modversions.h"
      FLAGS="$FLAGS -DMODVERSIONS"
    fi
    if [ $smp != 0 ]; then
      FLAGS="$FLAGS -D__SMP__"
    fi
    if echo `uname -m` | grep -q x86_64 ; then
      FLAGS="$FLAGS -mcmodel=kernel -mno-red-zone"
    fi
    gcc -c picdrv.c $FLAGS -O -Wall $INCLUDES 
    echo "For 2.6+ kernels, set the new=1 flags in this script"
  fi
  # make icepic_check file
  rm -f icepic_check
  echo \#\!/bin/bash > icepic_check ; sync
  echo "export ICEROOT=$ICEROOT" >> icepic_check ; sync
  cat check >> icepic_check ; sync
  chmod +x icepic_check
  if [ -f iceqos.c ] ; then cc -o iceqos -D_$ostype -D_$osname iceqos.c ; fi
  cc -o setram -D_$ostype -D_$osname -I../../inc ../../lib/setram.c 
  cc -o test   -D_$ostype -D_$osname -I../../inc ../../test/test.c ../../lib/icelib.c -lm -pthread
}

install_drv() {
  geticeroot
  cd $ICEROOT/drv/lnx
  VER=`cat /proc/version | awk '{ print $3 }' `
  echo "Installing ICE driver for kernel build $VER"
  if [ $new != 0 ]; then
    make -C /lib/modules/$(uname -r)/build M=$(pwd) modules_install
  else
    install -d /lib/modules/$VER/misc 
    install -c picdrv.o /lib/modules/$VER/misc
  fi
  install -c setram $bindir/icepic_setram
  install -c test $bindir/icepic_test
  install -c icepic_check $bindir/icepic_check
  if [ $chk = 3 ]; then
    install -c icepic $bindir/icepic_script
    install -m 644 icepic.service $unitfile
    systemctl daemon-reload
    echo "Using systemctl enable"
    systemctl enable icepic.service
  else
    install -c icepic /etc/$initd/icepic
    if [ $chk = 1 ]; then
      echo "Using chkconfig utility"
      /sbin/chkconfig --add icepic
    elif [ $chk = 2 ]; then
      echo "Using update-rc.d utility"
      update-rc.d icepic start 05 3 4 5 . stop 05 0 1 2 6 .
    else
      ln -sf /etc/$initd/icepic /etc/$rcd/rc5.d/S05icepic
      ln -sf /etc/$initd/icepic /etc/$rcd/rc4.d/S05icepic
      ln -sf /etc/$initd/icepic /etc/$rcd/rc3.d/S05icepic
    fi
  fi
  /sbin/depmod -a
}

remove_drv() {
  VER=`cat /proc/version | awk '{ print $3 }' `
  if [ -e /dev/icepic0 ]; then
    stop_drv
  fi
  if [ $chk = 3 ]; then
    echo "Using systemctl disable"
    systemctl disable icepic.service
    rm -f $unitfile
    rm -f $bindir/icepic
  else
    if [ $chk = 1 ]; then
      echo "Using chkconfig utility"
      /sbin/chkconfig --del icepic
    elif [ $chk = 2 ]; then
      echo "Using update-rc.d utility"
      update-rc.d -f icepic remove
    else
      rm -f /etc/$initd/icepic
      rm -f /etc/$rcd/rc3.d/S05icepic
      rm -f /etc/$rcd/rc4.d/S05icepic
      rm -f /etc/$rcd/rc5.d/S05icepic
      rm -f /etc/pcmcia/icepic
    fi
    rm -f /etc/$rcd/rc*.d/K*icepic
    rm -f /etc/$rcd/rc*.d/S*icepic
  fi
  # previous install files
  rm -f /dev/pmem
  rm -f /lib/modules/$VER/*/picdrv.ko
  # remove icepic check files
  rm -f icepic_check
  rm -f $bindir/icepic_check
  rm -f $bindir/icepic_test
}

start_drv() {
  if [ $mxd -lt 0 ]; then exit 0 ; fi
  # remove stale nodes
  rm -f /dev/icepic*
  rm -f /dev/iceram*
  # load driver module
  if [ $ram_alloc -le 3 ]; then
    /sbin/rmmod $module 2>/dev/null
    echo "ICEPIC: $module ice_ra=$ram_alloc ice_rn=$ram_nodes ice_nn=$numa ice_rs=$ram_start ice_rd=$ram_sized ice_rm=$ram_sizem ice_lat=$lat"
    /sbin/modprobe $module ice_ra=$ram_alloc ice_rn=$ram_nodes ice_nn=$numa ice_rs=$ram_start ice_rd=$ram_sized ice_rm=$ram_sizem ice_lat=$lat ice_mc=$mxd
  fi
  # extract dynamic major number
  major=`cat /proc/devices | grep icepic | awk '{print $1}'`
  if [ "$major" = "" ]; then
    echo "No icepic driver found"
    exit 1
  fi
  # extract number of devices
  if [ -e /proc/driver/icepic ]; then
    ndevs=`cat /proc/driver/icepic | grep Devices | awk '{ print $3 }'`
  else
    ndevs=0
  fi

  # increment the reboot counter
  rbinc
  # auto reboot if minimum devices not found
  if [[ $ndevs -lt $mdr && $nrebs -le $NRLIM ]]; then
    echo "Executing warm boot to pick up $mdr cards"
    sleep 2
    reboot
    exit 1
  fi

  # create new icepic nodes 
  if [ $ndevs -gt 0 ] ; then
    for i in {0..11} ; do
      if [ $i -lt $ndevs ]; then mknod /dev/icepic$i c $major $i ; fi
    done
    chgrp $group /dev/icepic*
    chmod $mode /dev/icepic*
  fi

  # create nodes for access to driver ram
  rmajor=`cat /proc/devices | grep iceram | awk '{print $1}'`
  if [ "$rmajor" = "" ]; then
    ln -sf icepic0 /dev/pmem
  else
    nrams=$ram_nodes
    mknod /dev/iceram c $rmajor 0
    for i in {0..8} ; do  
      if [ $i -lt $nrams ]; then mknod /dev/iceram$i c $rmajor $i ; fi
    done
    chgrp $group /dev/iceram*
    chmod $mode /dev/iceram*
    ln -sf iceram /dev/pmem
  fi

  # the block access devices are made internally by the driver code as /dev/iceramb
  bmajor=`cat /proc/devices | grep icebd | awk '{print $1}'`

  # start PSM daemon
  if [ -x /sbin/psm_iced ]; then /sbin/psm_iced 0 ; fi

  # mount icefs disks 
  # /etc/fstab line=/dev/nvmexxx /mnt/icefs defaults,nofail 0 0
  mount -a -t icefs

  # look to run check script
  if [[ $mdr -gt 0 && $nrebs -le $NRLIM ]]; then
    if [[ -f $bindir/icepic_check && -f $bindir/icepic_test && -n $minrate && -n $maxrate ]]; then
      # run script
      $bindir/icepic_check $bindir/icepic_test $mdr $minrate $maxrate
      rv=$?
      if (( $rv != 0 )); then
        # give the user time to see what failed
        echo "Rebooting, attempt $nrebs out of $NRLIM"
        sleep 2
        # reboot and throw 
        reboot
        exit 1
      fi
    fi
    # print (success) status into the reboot file
    printf "$(date)\nSuccesfully booted at attempt $nrebs\nNumber of cards: $mdr\nMin Rate: $minrate\nMax Rate: $maxrate\n" > $rbfile
    sync
  fi
}

stop_drv() {
  #if [ $chk = 3 ]; then
#    if [ $( systemctl is-active icepic ) == "active" ]; then
#      systemctl stop icepic.service
#      exit 0
#    fi
#  fi
  if [ $mxd -lt 0 ]; then exit 0 ; fi
  # unmount icefs disks 
  umount -a -t icefs
  # unload driver module
  /sbin/rmmod $module
  # remove stale nodes
  rm -f /dev/icepic*
  rm -f /dev/iceram*
}

# run pre script if it exists
if [ -f $bindir/icepic_pre ]; then $bindir/icepic_pre $1 ; fi

case "$1" in
  make)
	make_drv
	;;
  install)
	install_drv
	;;
  remove)
	remove_drv
	;;
  start)
	start_drv
	;;
  stop)
	stop_drv
	;;
  restart)
	stop_drv
	start_drv
	;;
  reinstall)
	stop_drv
	install_drv
	start_drv
	;;
  numa)
	cat /sys/devices/system/node/online
	if [ -f /usr/bin/numactl ]; then numactl -H ; fi
	;;
  status)
	cat /proc/driver/icepic
	;;
  clear)
	# reset RAM allocation
	$bindir/icepic_setram $ram_start $ram_sized $ram_sizem
	;;
  clean)
	rm picdrv.mod.* .*.cmd
	;;
  check)
	;;
  help)
	cat icepic.hlp
	;;
  liveboot)
	make_drv
	install_drv
	start_drv
	;;
  grub2)
	vi /etc/default/grub
	grub2-mkconfig -o /boot/grub2/grub.cfg
	;;
  mount)
	mount -a -t icefs
	;;
#  all)
#    make_drv
#    remove_drv
#    install_drv
#    start_drv
#    ;;
  *)
	echo "Usage: icepic {help|make|install|remove|start|stop|restart|status}"
	exit 1
    ;;
esac

# run post script if it exists
if [ -f $bindir/icepic_post ]; then $bindir/icepic_post $1 ; fi

exit 0
