# -*- shell-script -*-

# 33adapter_ethernet_dt_nosysfs - Device-tree only ETHERNET adapter code.

# 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: 33adapter_ethernet_dt_nosysfs,v 1.4 2004/11/25 05:11:27 martins Exp $

pci_ethernet_map=$(type -p pci_ethernet_map)

[ -n "$pci_ethernet_map" -a \
    -n "$source_device_tree" -a -z "$sysfs_dir" ] || \
    return 0

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

dt_list_adapters_setup_ethernet ()
{
    ensure_directory "$db_bus_dt_dir"
    pci_ethernet_map > "${db_bus_dt_dir}/lsvpd,pci-ethernet-map"
}

dt_adapter_crosslink_ethernet ()
{
    local bus_info="$1"
    local node="$2"

    local bus_addr="${bus_info#*/}"

    case "$bus_info" in
	pci/*)
	    local names items pci_addr name hwaddr
	    while read pci_addr name hwaddr ; do
		[ "$pci_addr" = "$bus_addr" ] && \
		    items="${items} ${name}@${hwaddr}"
	    done <"${db_bus_dt_dir}/lsvpd,pci-ethernet-map"
	    items="${items# }"

	    if [ "${items}" = "${items% *}" ] ; then
		# Only 1 item.
		names="${items%@*}"
	    else
		# Ambiguous.  Try to resolve with MAC addresses.
		local i f dthw
		for i in $items ; do
		    f="${node}/mac-address"
		    if [ -f "$f" ] ; then
			dthw=$(tdump "$f")
			dthw="${dthw// /:}"
			[ "$dthw" = "${i#*@}" ] && \
			    names="${names} ${i%@*}"
		    fi
		done
		names="${names# }"
	    fi

	    # If no spaces in $names, just 1, so no ambiguity.
	    [ -n "${names}" -a "${names}" = "${names% *}" ] && \
		add_adapter_crosslink "ethernet" "$bus_info" "$names"
	    ;;
    esac
}
