# -*- shell-script -*-

# 02ethtool - Ethernet adapter handling using ethtool.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2002, 2003, 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: 05adapter_ethernet_ethtool,v 1.7 2004/06/18 05:27:02 martins Exp $

ethtool=$(type -p ethtool)
ifconfig=$(type -p ifconfig)
[ -n "$ethtool" -a -n "$ifconfig" ] || return 0

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

# unset -f this function if name lookups can be done dynamically.
adapter_setup_ethernet ()
{
    local i
    for i in $("$ifconfig" -a | sed -n -e 's/^\(eth[0-9]*\)[ \t].*$/\1/p') ; do
	
	local pci_addr=$("$ethtool" -i "$i" | \
	    sed -n -e 's/^bus-info:[ \t]*\([0-9A-Fa-f:.]*\).*/\1/p')

	local seps="${pci_addr//[0-9A-Fa-f]/}"

	case "$seps" in
	    ::.) : ;; # pci_addr is dom:bus:dev.fun
	    :.)
	        # No PCI domain.  Assume a broken 2.4 big-bus-num system.
		local irq=$("$ifconfig" "$i" | \
		    sed -n -e 's/^[ \t]*Interrupt:\([0-9]*\)[ \t].*$/\1/p')
		[ -n "$irq" ] || irq="*"

		local dom bus
		pci_hack_dom_bus_hook "ethernet" "${pci_addr}-${irq}"
		if [ -n "$dom" -a -n "$bus" ] ; then
		    pci_addr=$(printf "%04x:%02x:%s" \
			"$dom" "$bus" "${pci_addr##*:}")
		else
		    pci_addr=""
		fi
		;;
	    *)
		debug "adapter_setup_ethernet: bad ethtool PCI info for \"${i}\""
		pci_addr=""
	esac

	[ -n "$pci_addr" ] && \
	    do_adapter_crosslink "ethernet" "pci/${pci_addr}" "$i"
    done
}
