File zfcp_host_configure of Package s390-tools

#!/bin/sh
#
# zfcp_host_configure
#
# Configures a zfcp host adapter
#
# Usage:
#   zfcp_host_configure <ccwid> <online>
#
# Return codes
#   1  sysfs not mounted
#   2  invalid value for <online>
#   3  device <ccwid> does not exist
#   4  module zfcp could not be loaded
#   5  adapter status could not be changed
#   6  wwpn ports still active
#

mesg () {
    echo "$@"
}

debug_mesg () {
    case "$DEBUG" in
        yes) mesg "$@" ;;
        *) ;;
    esac
}

if [ $# -ne 2 ] ; then
    echo "Usage: $0  <ccwid> <online>"
    exit 1
fi

# Get the mount point for sysfs
while read MNTPT MNTDIR MNTSYS MNTTYPE; do
    if test "$MNTSYS" = "sysfs"; then
	SYSFS="$MNTDIR"
	break;
    fi 
done </proc/mounts

if [ -z "$SYSFS" ]; then
    exit 1
fi

CCW_CHAN_ID=$1
ONLINE=$2
MODULE=zfcp

DEBUG="yes"

if [ -z "$CCW_CHAN_ID" ] ; then
    debug_mesg "No CCW device specified"
    exit 1
fi

if [ -z "$ONLINE" ] || [ "$ONLINE" -ne "1" -a "$ONLINE" -ne "0" ]; then
    debug_mesg "Invalid device status $ONLINE"
    exit 2
fi

_ccw_dir=${SYSFS}/bus/ccw/devices

_zfcp_dir="$_ccw_dir/$CCW_CHAN_ID"

if test ! -d "$_zfcp_dir" ; then
    debug_mesg "No device ${CCW_CHAN_ID}"
    exit 3
fi

# Check whether we need to load the zfcp module
if test ! -d "${SYSFS}/bus/ccw/drivers/zfcp"; then
    modprobe ${MODULE}

    # Re-check whether module loading has succeeded
    if test ! -d "${SYSFS}/bus/ccw/drivers/zfcp"; then
	debug_mesg "Could not load module ${MODULE}"
	exit 4
    fi
fi

RULES_DIR=/etc/udev/rules.d
RULES_FILE=51-zfcp-${CCW_CHAN_ID}.rules

# Check whether we need to do something
_zfcp_dev_status=$(cat $_zfcp_dir/online)
if [ "$_zfcp_dev_status" -eq "$ONLINE" ]; then
    debug_mesg "ZFCP adapter ${CCW_CHAN_ID} already in status ${ONLINE}"
    exit 0
fi

debug_mesg "Configuring device ${CCW_CHAN_ID}"

if [ -f ${RULES_DIR}/${RULES_FILE} ]; then
    rm -f ${RULES_DIR}/${RULES_FILE}
fi


if [ "$ONLINE" -eq "1" ]; then
    # Activate the device
    echo "$ONLINE" > $_zfcp_dir/online

    # Now wait for the adapter to initialize
    /sbin/udevadm settle

    for loop in 1 2 3 4 5 ; do
	read status < /sys/bus/ccw/devices/$CCW_CHAN_ID/status
	(( $status & 0x10000000 )) && break;
    done
    read wwpn_status < /sys/bus/ccw/devices/$CCW_CHAN_ID/status
    if !(( $wwpn_status & 0x10000000 )) ; then
	echo 0 > /sys/bus/ccw/devices/$CCW_CHAN_ID/online
	debug_mesg "Could not activate adapter, status $wwpn_status"
	exit 5
    fi

    # Write the configuration file
    if test -d ${RULES_DIR}; then
	cat > ${RULES_DIR}/${RULES_FILE} <<EOF
# Configuration for the zfcp adapter at CCW ID ${CCW_CHAN_ID}
ACTION=="add", SUBSYSTEM=="ccw", KERNEL=="$CCW_CHAN_ID", IMPORT{program}="collect $CCW_CHAN_ID %k $CCW_CHAN_ID zfcp"
ACTION=="add", SUBSYSTEM=="drivers", KERNEL=="zfcp", IMPORT{program}="collect $CCW_CHAN_ID %k $CCW_CHAN_ID zfcp"
EOF
    fi
else
    echo "0" > ${_zfcp_dir}/online
    # Re-read to check whether we have succeeded
    _ccw_dev_status=$(cat $_zfcp_dir/online)
    if [ "$_ccw_dev_status" -ne "$ONLINE" ]; then
	debug_mesg "Could not change status of device ${CCW_CHAN_ID} to $ONLINE"
	exit 5
    fi

    debug_mesg "ZFCP adapter at ${CCW_CHAN_ID} deactivated"
fi

# EOF