#-----------------------------------------------------------------------------
#
#      File         :  Plx_load
#      Abstract     :  Loads a PLX module
#
#-----------------------------------------------------------------------------


# Remove comment to display script commands as it runs
#set -x


#=============================================================================
# Modify the "export" statement below to set 'PLX_SDK_DIR' to the location
# of the PLX Linux installation path.  Some examples are:
#
#      export PLX_SDK_DIR=/usr/src/PlxSdk
#      export PLX_SDK_DIR=$HOME/PlxSdk
#=============================================================================
if [ "${PLX_SDK_DIR}1" = "1" ]; then

    # Attempt to auto-find PLX directory
    PLX_DIR=PlxSdk
    CWD=`pwd`

    while [ `pwd` != "/" ]; do
        if [ -d $PLX_DIR ]; then
            export PLX_SDK_DIR=`pwd`/$PLX_DIR
            break
        fi
        cd ..
    done

    # Return to original directory
    cd $CWD

    if [ "${PLX_SDK_DIR}1" = "1" ]; then
        # Add environment variable
        export PLX_SDK_DIR=$HOME/$PLX_DIR

        echo
        echo "  Note:"
        echo
        echo "      The required environment variable, PLX_SDK_DIR, is not defined"
        echo "      in the environment.  The default value (\"$PLX_SDK_DIR\")"
        echo "      will be used instead.  Please update your environment to override"
        echo "      the default, or modify this script accordingly.  To set an"
        echo "      environment variable, use the \"export\" command as follows:"
        echo
        echo "         export PLX_SDK_DIR=<root of PLX installation>"
        echo
    fi
else
    # Make sure the variable is in the environment, not just a shell variable
    export PLX_SDK_DIR=${PLX_SDK_DIR}
fi


# Verify insmod is found
if [ -f /sbin/insmod ]; then
    InsMod=/sbin/insmod
fi
if [ -f /usr/sbin/insmod ]; then
    InsMod=/usr/sbin/insmod
fi
if [ "$InsMod" = "" ]; then
    echo "ERROR: 'insmod' not found in /sbin or /usr/sbin"
    echo
    exit
fi


# Set directory
PLX_DIR=${PLX_SDK_DIR}

# Store parameters
Param1=`echo $1 |awk '{print tolower($0)}'`
Param2=$2

# Check for debug version
if [ "$2" = "d" ]; then
    debug=_dbg
    Param2=
fi


# Verify command-line
case "$Param1" in
    8000n) plx_drv_name=8000_NT  ;;
    8000d) plx_drv_name=8000_DMA ;;
    6000n) plx_drv_name=6000_NT  ;;
    9050)  plx_drv_name=9050     ;;
    9030)  plx_drv_name=9030     ;;
    9080)  plx_drv_name=9080     ;;
    9054)  plx_drv_name=9054     ;;
    9056)  plx_drv_name=9056     ;;
    9656)  plx_drv_name=9656     ;;
    8311)  plx_drv_name=8311     ;;
    svc)   plx_drv_name=Svc      ;;

    *)
    echo
    echo "PLX Linux module load script"
    echo
    echo "  Usage: Plx_load {Driver} [DebugOption]"
    echo
    echo "     Driver      = 8000n      : PLX 8000 NT-mode driver"
    echo "                   8000d      : PLX 8000 DMA Controller driver"
    echo "                   9050       : PLX 9050/9052 driver"
    echo "                   9030       : PLX 9030 driver"
    echo "                   9080       : PLX 9080 driver"
    echo "                   9054       : PLX 9054 driver"
    echo "                   9056       : PLX 9056 driver"
    echo "                   9656       : PLX 9656 driver"
    echo "                   8311       : PLX 8311 driver"
    echo "                   6000n      : PLX 6254/6540/6466 NT-mode driver"
    echo "                   Svc        : PLX PCI/PCIe Service driver"
    echo
    echo "     DebugOption = <none>     : Load Release build of module"
    echo "                   'd'        : Load Debug build of module"
    echo
    echo "  E.g.: Plx_load 9054 d   - Load 9054 module debug version"
    echo "        Plx_load 8000n    - Load 8000 NT module release version"
    echo
    exit
    ;;
esac


# Verify superuser access
if [ `id -u` != 0 ]; then
    echo
    echo "   ***************************************************"
    echo "   * NOTE: You must be superuser, logged in as root, *"
    echo "   *       or have sufficient rights to install      *"
    echo "   *       modules or this script will not work.     *"
    echo "   ***************************************************"
    echo
    echo "    ---- ERROR: Admin rights not detected, halting ----"
    echo
    exit
fi


# Set path to driver
case "$Param1" in
    9* | 8311 )  plx_drv_path=$PLX_DIR/Driver/Source.Plx9000/Output          ;;
    *         )  plx_drv_path=$PLX_DIR/Driver/Source.Plx$plx_drv_name/Output ;;
esac

# Registered name of driver
name=Plx$plx_drv_name

# Name of module
module=$name$debug

# Probe for a loaded conflicting driver
drv_Name=`lsmod | awk "\\$1==\"$name\" {print \\$1}"`
if [ "$drv_Name" = "" ]; then
    drv_Name=`lsmod | awk "\\$1==\"${name}_dbg\" {print \\$1}"`
fi


# Install the module
# echo
# echo "   *****************************************************************"
# echo "   *  NOTE: A warning regarding 'kernel tainting' is normal. This  *"
# echo "   *  is because the PLX driver is marked with a 'Proprietary'     *"
# echo "   *  license tag & not GPL.  For more information, refer to:      *"
# echo "   *        http://www.tux.org/lkml/#export-tainted                *"
# echo "   *****************************************************************"
echo
echo Install: $module

echo -n "  Load module......... "
# Check if conflicting driver already loaded
if [ "$drv_Name" != "" ]; then
    echo "ERROR: '$drv_Name' conflicts & already loaded"
    echo
    exit
fi

# Verify driver file exists
if [ ! -f $plx_drv_path/$module.ko ]; then
    echo "ERROR: Driver not built or invalid path"
    echo "    \-- $plx_drv_path/$module.ko"
    echo
    exit
fi

# Get driver & kernel version numbers
magic_Ker=`uname -r`
magic_Drv=`modinfo $plx_drv_path/$module.ko | grep magic | awk '{print \$2}'`

# Load module
if $InsMod $plx_drv_path/$module.ko $Param2 $3 $4 $5 $6 $7 2>/dev/null; then
    echo "Ok ($plx_drv_path)"
else
    # Check if built driver magic number doesn't match kernel
    if [ "$magic_Ker" != "$magic_Drv" ]; then
        echo ERROR: Load error or no supported devices found
        echo "      \-- NOTE: $module.ko magic number doesn't match kernel, please rebuid"
        echo "                   Kernel: $magic_Ker"
        echo "                   Driver: $magic_Drv"
    else
        # Note special 8000-series drivers only for endpoints & not switches
        if [ "$Param1" = "8000n" ]; then
            echo "      \-- NOTE: 8000_NT driver is only for PLX switch Non-Transparent(NT) ports --"
        fi
        if [ "$Param1" = "8000d" ]; then
            echo "      \-- NOTE: 8000_DMA driver is only for PLX switches with DMA functions --"
        fi
    fi
    echo
    exit
fi

# Verify driver loaded
echo -n "  Verify load......... "
drv_Name=`lsmod | awk "\\$1==\"$module\" {print \\$1}"`
if [ "$drv_Name" = "" ]; then
    echo ERROR: \'$module\' not detected
    echo
    exit
fi
echo Ok

# Get the major number
echo -n "  Get major number.... "
major=`cat /proc/devices | awk "\\$2==\"$name\" {print \\$1}"`

# Check if valid
if [ "$major" = "" ]; then
    echo ERROR: Module major number not detected
    echo
    exit
fi

# Display Major ID
echo "Ok (MajorID = $major)"

# Create the device node path
path=/dev/plx
echo -n "  Create node path.... "
if [ -d $path ]; then
    echo "Ok ($path already exists)"
else
    mkdir $path
    chmod 0777 $path
    echo "Ok ($path)"
fi

# Create the device nodes
echo -n "  Create nodes........ "
rm -f $path/$name*
mknod $path/$name c $major 255

# Create additional nodes for non-service driver
if [ "$plx_drv_name" != "Svc" ]; then
    mknod ${path}/$name-0  c $major 0
    mknod ${path}/$name-1  c $major 1
    mknod ${path}/$name-2  c $major 2
    mknod ${path}/$name-3  c $major 3
    mknod ${path}/$name-4  c $major 4
    mknod ${path}/$name-5  c $major 5
    mknod ${path}/$name-6  c $major 6
    mknod ${path}/$name-7  c $major 7
    mknod ${path}/$name-8  c $major 8
    mknod ${path}/$name-9  c $major 9
    mknod ${path}/$name-10 c $major 10
    mknod ${path}/$name-11 c $major 11
    mknod ${path}/$name-12 c $major 12
    mknod ${path}/$name-13 c $major 13
    mknod ${path}/$name-14 c $major 14
    mknod ${path}/$name-15 c $major 15
    mknod ${path}/$name-16 c $major 16
fi

chmod 777 $path/*
echo "Ok ($path/$name)"
echo
