# -*- shell-script -*-

# 33adapter_scsi_dt_nosysfs - Device-tree only SCSI 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_scsi_dt_nosysfs,v 1.5 2004/11/25 05:12:00 martins Exp $

pci_scsi_map=$(type -p pci_scsi_map)

[ -d "/proc/scsi" -a -n "$pci_scsi_map" -a \
    -n "$source_device_tree" -a -z "$sysfs_dir" ] || \
    return 0

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

dt_list_adapters_setup_scsi ()
{
    ensure_directory "$db_bus_dt_dir"
    pci_scsi_map > "${db_bus_dt_dir}/lsvpd,pci-scsi-map"
}

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

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

    local ch=""
    if [ "$bus_addr" != "${bus_addr%/*}" ] ; then
	ch=":${bus_addr#*/}"
	bus_addr="${bus_addr%/*}"
    fi

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

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