#!/bin/bash
#
# Deconfigures a ccw interface.
# $Id: hwdown-ccw 1069 2004-09-02 18:23:18Z zoz $
#
# Currently handles ctc, qeth, and dasd devices.
# lcs and escon support untested.
# 

SCRIPTNAME=${0##*/}
CONFIG=$1
HWDESC=$2

# Read in common functions
. ./scripts/functions
test -r ./config && . ./config


if test -z "$SYSFS"; then
    message "sysfs not mounted, aborting."
    exit 1
fi

# Set defaults
if [ -z "$HWD_DEVICEPATH" ]; then
    HWD_DEVICEPATH=${SYSFS}/bus/ccwgroup/devices/${HWD_BUSID}
fi

if [ ! -d "$HWD_DEVICEPATH" ]; then
    message "No hardware devicepath given, cannot configure"
    exit 1
fi

# Check whether it is a group device
if test -d $HWD_DEVICEPATH/group_device; then
    HWD_DEVICEPATH=$HWD_DEVICEPATH/group_device
fi

# Deconfigure interface
message_n "Deconfigure device $HWD_BUSID: "

read _online < $HWD_DEVICEPATH/online
# Device configured, set it offline
if [ $_online -ne 0 ]; then
    echo "0" > $HWD_DEVICEPATH/online
    debug "Setting device $HWD_BUSID offline"
fi

# The sysfs entry might vanish after a CP DETACH
if [ ! -f $HWD_DEVICEPATH ]; then
    message "ok."
else
    # Re-read for failures
    read _online < $HWD_DEVICEPATH/online
    if [ $_online -ne 0 ]; then
	message "failed."
    else
        # Check whether we need to ungroup it
	if test -w $HWD_DEVICEPATH/ungroup ; then
            # We cannot use hotplug events here as the
            # devices are already gone from sysfs
	    echo "1" > $HWD_DEVICEPATH/ungroup
	    debug "Device group $HWD_BUSID deconfigured"
	fi

        # Check whether we need to reset DIAG access
	if test -w $HWD_DEVICEPATH/use_diag ; then
	    echo "0" > $HWD_DEVICEPATH/use_diag
	    debug "Reset DIAG access for $HWD_BUSID"
	fi
	message "ok."
    fi
fi
