# -*- shell-script -*-

# 27device_scsi_sysfs - SCSI device support using sysfs.

# 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: 27device_scsi_sysfs,v 1.15 2004/10/21 05:35:33 martins Exp $

[ -n "$sysfs_dir" ] || return 0

# This should work with Linux >= 2.6.2.
[ -d "${sysfs_dir}/class/scsi_generic" ] || modprobe sg >/dev/null 2>&1
[ -d "${sysfs_dir}/class/scsi_generic" ] || return 0

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

list_devices_scsi ()
{
    local d bn dl t
    for d in "${sysfs_dir}/class/scsi_generic/"* ; do
	bn="${d##*/}" # basename
	dl="${d}/device"
	if [ -h "$dl" ] ; then
	    t=$(readlink "$dl")
	    echo "${t##*/}" # basename = host:bus:target:lun
	fi
    done
}

unset -f device_setup_scsi

# This gets the "kernel name", ignoring any userspace naming.
get_kernel_device_name_scsi ()
{
    local hbtl="$1"

    local d l dl t name

    d="${sysfs_dir}/bus/scsi/devices/${hbtl}"

    for l in "block" "tape" "generic" ; do
	dl="${d}/${l}"
	if [ -L "$dl" ] ; then
	    t=$(readlink "$dl")
	    name="${t##*/}" # basename
	    # FIXME!  Gross hack...
	    name="${name/#sr/scd}"
	    [ -n "$name" ] && echo "/dev/${name}"
	    break
	fi
    done
}

add_device_hook_scsi ()
{
    # Uses: bus_info, adapter
    # Sets: adapter_os_dir, device_bus_dir, yl

    local adapter_bus_dir yl_prefix

    if sysfs_add_device_hook_common ; then
	yl="${adapter_bus_dir%/*}" # dirname
    elif sysfs_scsi_usb_hack ; then
	yl="$adapter_bus_dir"
    else
	return 1
    fi

    yl="${yl##*/}"  # basename
    yl="${yl_prefix}${yl}/${bus_info#*/}"
}

get_major_minor_scsi ()
{
    local hbtl="$1"

    local f mm
    f="${sysfs_dir}/bus/scsi/devices/${hbtl}/generic/dev"
    [ -f "$f" ] && read mm <"$f"
    [ -n "$mm" ] && echo "$mm"
}

sysfs_add_device_bus_alias_scsi ()
{
    # Uses: bus_info, device_bus_dir

    local hbtl="${bus_info##*/}" # basename
    local d="${sysfs_dir}/bus/scsi/devices/${hbtl}"

    local bl="${d}/block"
    if [ -L "$bl" ] ; then
        # pseudo_realpath won't work here due to symlink madness.
	local sysfs_node=$(cd "$bl" ; pwd -P) # realpath
	bus_alias_set "${sysfs_node#/}" "${device_bus_dir#${db_bus_dir}/}"
    fi
}

remove_device_hook_scsi ()
{
    # Uses: device_bus_dir
    # Sets: bus_info

    local dn="${device_bus_dir%/*}" # dirname
    # FIXME: Gross hack, just to make it work for now...
    local t
    read t < "${device_bus_dir}/linux,vpd/000/YL"
    t="${t##*/}"
    bus_info="scsi/${t}"
}

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

sysfs_scsi_usb_hack ()
{
    # Uses: adapter, bus_info

    local c=$(sysfs_get_adapter_class "scsi")
    local l="${sysfs_dir}/class/${c}/${adapter}/device"

    if [ -L "$l" ] ; then
	local t=$(readlink "$l")

	case "$t" in
	    */usb*/$adapter)
		adapter="${t#*/usb}"
		adapter="usb${adapter%%/*}"

		# Yuck!
		local old_bus_info="$bus_info"
		local bus_info="usb/${bus_info#scsi/}"
		add_device_hook_common || return 1

		t="${t#*/${adapter}/}"
		device_bus_dir="${adapter_bus_dir}/${t}"
		bus_alias_set "$old_bus_info" \
		    "${device_bus_dir#${db_bus_dir}/}"
		;;
	esac
    fi

    return 0
}
