SHA256
1
0
forked from pool/s390-tools
s390-tools/zfcp_host_configure
2017-02-21 11:14:26 +00:00

187 lines
4.9 KiB
Bash

#!/bin/sh
#
# zfcp_host_configure
#
# Configures a zfcp host adapter
#
# Usage:
# zfcp_host_configure <ccwid> <online>
#
# ccwid = x.y.ssss where
# x is always 0 until IBM creates something that uses that number
# y is the logical channel subsystem (lcss) number. Most often this is 0, but it could be non-zero
# ssss is the four digit subchannel address of the device, in hexidecimal, with leading zeros.
# online = 0 to take the device offline
# 1 to bring the device 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
# 10 adapter active but allow_lun_scan active
#
if [ "${DEBUG}" != "yes" ]; then
DEBUG="no"
fi
DATUM=$(date)
add_channel_for_cio() {
echo "$* # $DATUM" >> /boot/zipl/active_devices.txt
}
remove_channel_for_cio() {
[ -w /boot/zipl/active_devices.txt ] && sed -i -e "/^$1/d" /boot/zipl/active_devices.txt
}
mesg () {
echo "$@"
}
debug_mesg () {
case "$DEBUG" in
yes) mesg "$@" ;;
*) ;;
esac
}
if [ $# -ne 2 ] ; then
echo "Usage: $0 <ccwid> <online>"
echo " ccwid = x.y.ssss where"
echo " x is always 0 until IBM creates something that uses that number"
echo " y is the logical channel subsystem (lcss) number. Most often this is 0, but it could be non-zero"
echo " ssss is the four digit subchannel address of the device, in hexidecimal, with leading zeros."
echo " online = 0 to take the device offline"
echo " 1 to bring the device 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
mesg "/sysfs not present"
exit 1
fi
CCW_CHAN_ID=$1
ONLINE=$2
MODULE=zfcp
if [ -z "$CCW_CHAN_ID" ] ; then
mesg "No CCW device specified"
exit 1
fi
if [ -z "$ONLINE" ] || [ "$ONLINE" != "1" -a "$ONLINE" != "0" ]; then
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
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
mesg "Could not load module ${MODULE}"
exit 4
fi
fi
RULES_DIR=/etc/udev/rules.d
RULES_FILE=51-zfcp-${CCW_CHAN_ID}.rules
ALREADY_ONLINE=0
# 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}"
ALREADY_ONLINE=1
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
if [ "${ALREADY_ONLINE}" -eq "0" ]; then
# Activate the device
echo "$ONLINE" > $_zfcp_dir/online
# Now wait for the adapter to initialize
/sbin/udevadm settle
fi
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
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"
ACTION=="add", ENV{COLLECT_$CCW_CHAN_ID}=="0", ATTR{[ccw/$CCW_CHAN_ID]online}="1"
EOF
fi
add_channel_for_cio "${CCW_CHAN_ID}"
# Check whether we need to do something
read allow_lun_scan < /sys/module/zfcp/parameters/allow_lun_scan
if [ "$allow_lun_scan" = "Y" ] ; then
for host in ${_zfcp_dir}/host* ; do
[ -d $host ] || continue
hba_num=${host##*host}
done
# Automatic LUN scan is only possible on NPIV ports
read port_type < /sys/class/fc_host/host${hba_num}/port_type
if [ "$port_type" = "NPIV VPORT" ] ; then
exit 10;
fi
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
mesg "Could not change status of device ${CCW_CHAN_ID} to $ONLINE"
exit 5
fi
echo "${CCW_CHAN_ID}" > /sys/bus/ccw/drivers/zfcp/unbind
echo "${CCW_CHAN_ID}" > /sys/bus/ccw/drivers/zfcp/bind
remove_channel_for_cio "${CCW_CHAN_ID}"
debug_mesg "zFCP adapter at ${CCW_CHAN_ID} deactivated"
fi
# EOF