# -*- shell-script -*-

# 40ibm,loc-code - Routines for device-tree with ibm,loc-code property.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2004

# 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: 40ibm,loc-code,v 1.6 2005/02/11 05:05:14 martins Exp $

[ -n "$source_device_tree" -a \
    \( -f "${source_device_tree}/ibm,loc-code" -o \
       -f "${source_device_tree}/ibm,converged-loc-codes" \) ] || \
    return 0

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

bus_set_yl ()
{
    # Sets: yl
    
    local node="$1"

    dt_manufacture_yl_hook "$node"
}

adapter_set_yl ()
{
    # Uses: adapter_bus_dir
    # Sets: yl

    dt_manufacture_yl_hook "$adapter_bus_dir"
}

device_set_yl_scsi ()
{
    # Uses: bus_info, device_bus_dir
    # Sets: yl
    yl=""

    local adapter_yl
    dt_device_set_yl_common "scsi" "$device_bus_dir"

    if [ -z "$yl" -a -n "$adapter_yl" ] ; then
	local host bus target lun
	scsi_parse_addr_hook "${bus_info#*/}"
	dt_device_yl_hook "$target" "$lun"
    fi

    [ -n "$yl" ]
}

device_set_yl_ide ()
{
    # Uses: bus_info, device_bus_dir
    # Sets: yl
    yl=""

    local adapter_yl
    dt_device_set_yl_common "ide" "$device_bus_dir"

    local number
    dt_ide_set_number "$bus_info"
    [ -z "$yl" -a -n "$adapter_yl" ] && \
	dt_device_yl_hook "$number" ""

    [ -n "$yl" ]
}

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

dt_device_set_yl_common ()
{
    local type="$1"
    local device_bus_dir="$2"

    # Sets: yl, adapter_yl
    yl=""

    if [ -f "${device_bus_dir}/ibm,loc-code" ] ; then
	dt_manufacture_yl_hook "$device_bus_dir"
    else 
	device_set_adapter_yl "$type" "$device_bus_dir"
    fi
}

dt_manufacture_yl_hook ()
{
    # Sets: yl
    yl=""

    local d="$1"
    while [ ! -r "$d/ibm,loc-code" -a "$d" != "$db_bus_dt_dir" ] ; do
	d="${d%/*}" # dirname
    done

    [ -r "$d/ibm,loc-code" ] && { read yl <"${d}/ibm,loc-code" ; true ; }
}

dt_device_yl_hook ()
{
    # Uses: adapter_yl
    # Sets: yl

    local id="$1"
    local subid="$2"

    if [ -n "$adapter_yl" -a -n "$id" ] ; then
	local t=$(dec2hex $id)
	yl="${adapter_yl}-A${t}"
	if [ -n "$subid" ] && [ "$subid" -ne 0 ] ; then
	    t=$(dec2hex $subid)
	    yl="${yl},${t}"
	fi
    fi
}
