# -*- shell-script -*-

# 02ethtool - Extra ethernet handling, if ethtool is available.

# This file is part of the Linux lsvpd package.

# (C) Copyright IBM Corp. 2002, 2003

# 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.5 2003/12/22 05:09:13 martins Exp $

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

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

function get_pci_info_ethernet ()
{
    local name="$1"

    set -f
    set -- $("$ethtool" -i "$name" | \
	     sed -n -e 's/^bus-info:[ \t]*\([0-9A-Fa-f:.]\)/\1/p' | \
	     sed -e 's/[\.:]/ /g' \
		 -e 's/^\([0-9a-fA-F]\)/0x\1/' \
		 -e 's/ \([0-9a-fA-F]\)/ 0x\1/g')
    set +f

    local dom bus dev fun irq
    if [ $# -eq 4 ] ; then
	dom="$1"
	shift
    fi
    bus="$1"
    dev="$2"
    fun="$3"

    if [ -z "$dom" ] ; then
	# No PCI domain.  Assume we're on a broken 2.4 bignum system.
	
	irq=$(/sbin/ifconfig "$name" | \
	      sed -n -e 's/^[ \t]*Interrupt:\([0-9]*\)[ \t].*$/\1/p')

	if [ -n "$irq" ] ; then
	    bus=$(get_real_bus "ethernet" \
			       $(printf "%02x:%02x.%x-%d" \
					"$bus" "$dev" "$fun" "$irq"))
	    dom=$(( ($bus >> 8) & 0xffffff ))
	    bus=$(($bus & 0xff))

        else
	    debug "get_pci_info_ethernet (ethtool): failed to get IRQ"
	    return 1
	fi
    fi

    if [ -n "$dom" -a -n "$bus" -a -n "$dev" -a -n "$fun" ] ; then
	printf "0x%x 0x%x 0x%x 0x%x\n" "$dom" "$bus" "$dev" "$fun"
    else
	debug "get_pci_info_ethernet (ethtool): failed getting PCI info for \"${name}\""
	return 1
    fi
}
