# -*- shell-script -*-

# 070device_minimal - Device scanning routines and variables.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2002, 2003, 2004, 2005

# Maintained by  Martin Schwenke <martins@au.ibm.com>

# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2, or (at your option)
# any later version.
 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
    
# $Id: 070device,v 1.7 2005/06/14 07:39:12 martins Exp $

# Default and common functions.

# This module is always loaded.
true || return 0

######################################################################

make_multiplexed device_set_yl

make_multiplexed add_device_hook
make_multiplexed remove_device_hook

make_multiplexed device_get_adapter_name
make_multiplexed device_vpd_set_order

######################################################################

list_devices ()
{
    # Sets: device_list
    device_list=$(device_handler "list_devices")
}

get_device_name ()
{
    # Sets: device_name

    local type="$1"
    local bus_addr="$2"

    device_name=$(device_handler "get_name" "$type" "${type}/${bus_addr}")
}

device_retrieve_vpd ()
{
    local type="$1"
    local bus_addr="$2"
    local tmp_dir="$3"
    local vpd_dir="$4"

    device_handler "get_vpd" "$type" "${type}/${bus_addr}" "$tmp_dir" "$vpd_dir"
}

######################################################################

add_device_hooks="add_device_hook"

add_device_hooks ()
{
    type="$1"

    for i in $add_device_hooks ; do
	$i "$type" || return 1
    done
}

add_device_hook_common ()
{
    # Uses: bus_info, device_adapter_name
    # Sets: adapter_bus_dir

    local adapter_subdir
    adapter_set_adapter_subdir "${bus_info%%/*}"
    local f="${db_os_dir}/${adapter_subdir}/${device_adapter_name}/bus-node"
    adapter_bus_dir=""
    if [ -L "$f" ] ; then
	if cd -P "$f" ; then
	    adapter_bus_dir="$PWD"
	    cd "$OLDPWD"
	    return 0
	fi
    fi

    return 1
}

device_add_name_and_crosslink ()
{
    local type="$1"
    local bus_addr="$2"
    local name="$3"

    local bus_alias
    bus_alias_get "${type}/${bus_addr}"
    device_add_name_and_crosslink_basic "$bus_alias" "$name"
}

device_add_name_and_crosslink_basic ()
{
    local device_bus_dir="$1"
    local name="$2"

    [ -n "$device_bus_dir" ] || return

    local l1="${device_bus_dir}/lsvpd,adapter-bus-node"
    local l2="${l1}/lsvpd,os-node"

    if [ -L "$l1" -a -L "$l2" ] ; then
	cd -P "$l2"
	local adapter_os_dir="$PWD"
	cd "$OLDPWD"

	local device_os_dir="${adapter_os_dir}/${name#/dev/}"

    	cross_link "$device_bus_dir" "$device_os_dir"
    fi

    local vpd_dir
    vpd_dir_set_hook "$device_bus_dir"
    vpd_field_ensure "$vpd_dir" "AX" "$name"
}

device_set_yl_DEFAULT ()
{
    # Sets: yl

    yl=""
}

device_set_adapter_yl ()
{
    # Sets: adapter_yl
    adapter_yl=""

    local type="$1"
    local device_bus_dir="$2"

    local yl
    local l="${device_bus_dir}/lsvpd,adapter-bus-node"

    if [ -L "$l" ] ; then
	cd -P "$l"
	local adapter_bus_dir="$PWD"
	cd "$OLDPWD"

	adapter_set_yl "$type"
	adapter_yl="$yl"
    fi
}

device_backlink_adapter_bus_dir ()
{
    local device_bus_dir="$1"
    local adapter_bus_dir="$2"

    local l="${device_bus_dir}/lsvpd,adapter-bus-node"
    if set_dot_dots_hook "$device_bus_dir" "$adapter_bus_dir" ; then
	rm -f "$l"
	ensure_directory "$device_bus_dir"
	ln -s "$dot_dots" "$l"
    fi
}

######################################################################

scsi_parse_addr_hook ()
{
    local scsi_addr="$1" # host:bus:target:lun

    # Sets: host, bus, target, lun

    local bustarget="${scsi_addr#*:}"
    bustarget="${bustarget%:*}"

    host="${scsi_addr%%:*}"
    bus="${bustarget%:*}"
    target="${bustarget#*:}"
    lun="${scsi_addr##*:}"
}

######################################################################

add_device ()
{
    local type="$1"
    local bus_addr="$2"

    local device_adapter_name
    device_get_adapter_name "$type" "$bus_addr"

    local tmp_dir="${db_bus_dir}/tmp/${bus_addr}"
    ensure_directory "$tmp_dir"
    local tmp_vpd_dir="${tmp_dir}/vpd"
    ensure_directory "$tmp_vpd_dir"

    if device_retrieve_vpd "$type" "$bus_addr" "$tmp_dir" "$tmp_vpd_dir" ; then
	
	# Used by hook.
	local device_type
	if [ -f "${tmp_dir}/type" ] ; then
	    read device_type < "${tmp_dir}/type"
	fi

	local bus_info="${type}/${bus_addr}"
	local yl

	if add_device_hooks "$type" ; then
	    local vpd_dir
	    vpd_dir_set_hook "$device_bus_dir"
	    ensure_directory "${vpd_dir%/*}" # dirname

	    mv "$tmp_vpd_dir" "$vpd_dir"
	    local fc
	    do_fc_hook "$device_bus_dir"
	    vpd_field_add_value "$vpd_dir" "FC" "$fc"
	    vpd_field_add_value "$vpd_dir" "YL" "$yl"
	    device_vpd_set_order "$type" "$vpd_dir"

	    # Keep for debugging.
	    local t="${device_bus_dir}/linux,tmp"
	    mv "$tmp_dir" "$t"
	else
	    # This is a little safer than an "rm -rf ..."
	    rm -f "${tmp_vpd_dir}/.lsvpd"
	    rm -f "${tmp_vpd_dir}/"*
	    rmdir "$tmp_vpd_dir"
	    rm -f "${tmp_dir}/"*
	    rmdir "$tmp_dir"
	fi
    else
	# This is a little safer than an "rm -rf ..."
	rm -f "${tmp_vpd_dir}/.lsvpd"
	rm -f "${tmp_vpd_dir}/"*
	rmdir "$tmp_vpd_dir"
	rm -f "${tmp_dir}/"*
	rmdir "$tmp_dir"
    fi
}

device_get_adapter_name_DEFAULT ()
{
    # Sets: device_adapter_name

    local type="$1"
    local bus_addr="$2"

    device_adapter_name=""
}

device_get_adapter_name_scsi ()
{
    # Sets: device_adapter_name

    local bus_addr="$1"

    device_adapter_name="host${bus_addr%%:*}"
}

device_vpd_set_order_scsi ()
{
    local vpd_dir="$1"

    local sort_order="FC DS AX PL MF TM CD LL YL FN RL SN EC PN"

    device_vpd_set_order_basic "$vpd_dir" "$sort_order"
}

device_vpd_set_order_DEFAULT ()
{
    local type="$1"
    local vpd_dir="$2"

    local sort_order="FC DS AX MF TM SN CD RL RM YL"

    device_vpd_set_order_basic "$vpd_dir" "$sort_order"
}

device_vpd_set_order_basic ()
{
    local vpd_dir="$1"
    local sort_order="$2"

    local ks=""

    cd "$vpd_dir"

    local i
    for i in $sort_order ; do
	[ -e "$i" ] && ks="${ks}${i} "
    done
    
    for i in * ; do
	case "$i" in
	    (U?) : ;;  # FIXME: U? fields only for SCSI ACARD.
	    *)
		case "$ks" in
		    (*${i}\ *) : ;;  # Ignore, already added in previous pass.
		    (*) ks="${ks}${i} " ;;
		esac
	esac
    done

    echo "$ks" > "${vpd_dir}/.lsvpd"

    cd "$OLDPWD"
}
