#!/bin/sh

# Name: adapter_info
# Copyright: (C)2003 Hewlett-Packard
#
# Description: lists all the qla* adapters in a system,
#              their state, WWNN, and WWPN, and other
#	       info
#
# Modification History
#
# Chad Dupuis	03/14/03	Initial Development
# Chad Dupuis   09/17/03        Added target listing option;
#                               display login retry count in
#                               some instances; Allow just the
#                               instance number to specified
#                               with the -d option
# Chad Dupuis   04/21/04	Look for SCSI targets searching
#                               for the "target" string only
# Chad Dupuis   06/11/04	Updates for SLES 9 (2.6 kernel)
# Chad Dupuis   08/03/04	Added -m switch to list adapter models

#
# Function defs
#

# just produces a list of adapters

list_adapters () {

 for i in $ADAPTERS
 do
	echo $i
 done
}

# This lists the state, WWPN, and WWNN of the adapter

normal_output () {

 if [ $VERBOSEFLAG -eq 0 ]
 then
	# normal output

	for i in $ADAPTERS
  	do
   		if [ "`uname -r | cut -c 1-3`" = "2.4" ]
                then
                        STATE=`cat $i | grep "state=" | awk '{print $4}' | sed -e 's/,//' -e 's/<//' -e 's/>//'`
                elif [ "`uname -r | cut -c 1-3`" = "2.6" ]
                then
                        STATE=`cat $i | grep "loop state" | awk '{print $5}' | sed -e 's/,//' -e 's/<//' -e 's/>//'`
                fi

		WWNN=`cat $i | grep "adapter-node=" | cut -c 24-39`
   		WWPN=`cat $i | grep "adapter-port=" | cut -c 24-39`
 
   		echo "${i}: STATE=${STATE} WWNN=${WWNN} WWPN=${WWPN}"
  	done
 else
 	# verbose output
  	# note that some fields are different between drivers for the 2.4
  	# and 2.6 kernels

  	for i in $ADAPTERS
  	do
   		if [ "`uname -r | cut -c 1-3`" = "2.4" ]
   		then
			STATE=`cat $i | grep "state=" | awk '{print $4}' | sed -e 's/,//' -e 's/<//' -e 's/>//'`
   		elif [ "`uname -r | cut -c 1-3`" = "2.6" ]
   		then
			STATE=`cat $i | grep "loop state" | awk '{print $5}' | sed -e 's/,//' -e 's/<//' -e 's/>//'`
   		fi

   		WWNN=`cat $i | grep "adapter-node=" | cut -c 24-39`
   		WWPN=`cat $i | grep "adapter-port=" | cut -c 24-39`
   		QDEPTH=`cat $i | grep "Device queue depth" | awk '{print $5}'`
   
   		if [ "`uname -r | cut -c 1-3`" = "2.4" ]
   		then
   			VERSION=`cat $i | grep "Driver version" | awk '{print $6}'`
   		elif [ "`uname -r | cut -c 1-3`" = "2.6" ]
   		then
			VERSION=`cat $i | grep "Driver version" | awk '{print $7}'`
   		fi

   		PDRC=`cat $i | grep "Port down retry" | awk '{print $5}'`
   		LRC=`cat $i | grep "Login retry count" | awk '{print $5}'`
	
		ATYPE=`cat $i | grep "QLogic PCI to Fibre Channel Host Adapter" | awk '{print $9}' | sed 's/://g'`

       		if [ "$ATYPE" = "HP" ]
        	then
                	ATYPE=`cat $i | grep "QLogic PCI to Fibre Channel Host Adapter" | awk '{print $10}' | sed 's/://g'`
        	fi
	
   		echo "${i}:"  
   		echo "	Driver version = $VERSION"
   		echo "	Adapter state = $STATE"
   		echo "	Adapter model = $ATYPE"
		echo "	WWNN = $WWNN"
   		echo "	WWPN = $WWPN"
   		echo "	Queue depth = `printf "%d" $QDEPTH`"
   		echo "	Port down retry count = $PDRC"
   		echo "        Login retry count = $LRC"
  	done
 fi
}

# produce a list of LUNs for each adapter

lun_output () {

 for i in $ADAPTERS
 do
  	echo ${i}":"
  	echo ""
  	cat $i | grep Pending 
  	echo ""
 done
}


# print out all the targets that each adapter sees

target_output () {

 for i in $ADAPTERS
 do
  	echo ${i}":"
  	echo ""
  	cat $i | grep target
  	echo ""
 done
}


# prints out what type of adapter each adapter instance is

adapter_types () {

 for i in $ADAPTERS
 do
 	ATYPE=`cat $i | grep "QLogic PCI to Fibre Channel Host Adapter" | awk '{print $9}' | sed 's/://g'`
	
	if [ "$ATYPE" = "HP" ]
	then
		ATYPE=`cat $i | grep "QLogic PCI to Fibre Channel Host Adapter" | awk '{print $10}' | sed 's/://g'`
	fi
	
	echo "${i} - $ATYPE"
 done
}


# produce output for a specific adapter

device_output () {

 # from adapter instance, get actual full path

 i=`find /proc/scsi/qla2200 2>/dev/null | grep /$1`
 
 if [ "$i" = "" ]
 then
  	# check qla2300 directory
  	i=`find /proc/scsi/qla2300 2>/dev/null | grep /$1`
 fi
 
 if [ "$i" = "" ]
 then
	# check qla2xxx directory
	i=`find /proc/scsi/qla2xxx 2>/dev/null | grep /$1`
 fi

 if [ ! "$i" ]
 then
  	echo "The instance number $1 does not exist!"
  	exit 1
 fi

 if [ "`uname -r | cut -c 1-3`" = "2.4" ]
 then
 	STATE=`cat $i | grep "state=" | awk '{print $4}' | sed -e 's/,//' -e 's/<//' -e 's/>//'`
 elif [ "`uname -r | cut -c 1-3`" = "2.6" ]
 then
	STATE=`cat $i | grep "loop state" | awk '{print $5}' | sed -e 's/,//' -e 's/<//' -e 's/>//'`
 fi

 WWNN=`cat $i | grep "adapter-node=" | cut -c 24-39`
 WWPN=`cat $i | grep "adapter-port=" | cut -c 24-39`
 QDEPTH=`cat $i | grep "Device queue depth" | awk '{print $5}'`
 
 if [ "`uname -r | cut -c 1-3`" = "2.4" ]
 then
	VERSION=`cat $i | grep "Driver version" | awk '{print $6}'`
 elif [ "`uname -r | cut -c 1-3`" = "2.6" ]
 then
	VERSION=`cat $i | grep "Driver version" | awk '{print $7}'`
 fi
 
 PDRC=`cat $i | grep "Port down retry" | awk '{print $5}'`
 LRC=`cat $i | grep "Login retry count" | awk '{print $5}'`

 ATYPE=`cat $i | grep "QLogic PCI to Fibre Channel Host Adapter" | awk '{print $9}' | sed 's/://g'`

 if [ "$ATYPE" = "HP" ]
 then
 	ATYPE=`cat $i | grep "QLogic PCI to Fibre Channel Host Adapter" | awk '{print $10}' | sed 's/://g'`
 fi

 echo "General Info"
 echo "------------------"
 echo "Driver version = $VERSION"
 echo "Adapter state = $STATE"
 echo "Adapter model = $ATYPE"
 echo "WWNN = $WWNN"
 echo "WWPN = $WWPN"
 echo "Queue depth = `printf "%d" $QDEPTH`"
 echo "Port down retry count = $PDRC"
 echo "Login retry count = $LRC"
 echo ""
 echo "LUNs"
 echo "----------"
 cat $i | grep Pending
 echo ""
 echo "Targets"
 echo "-------------"
 cat $i | grep target
}

# prints out a help message

help_message () {
 echo "Usage: adapter_info [ -lLvhd device instance ]"
 echo "adapter_info: lists information on qla2200 and qla2300"
 echo "              adapters in the system."
 echo ""
 echo "-l: lists all qla2200 and qla2300 adapters in system"
 echo "-L: lists all LUN's for each adapter"
 echo "-t: lists all targets seen by each adapter"
 echo "-m: lists each adapters model"
 echo "-v: verbose"
 echo "-h: prints this help menu"
 echo "-d: prints LUN and verbose information for a specific device"
}

#
# Script Main
#

# get a list of adapters on the system

ADAPTERS=`find /proc/scsi/ |grep /[0-9] 2>/dev/null | grep qla`

# set default values for output flags

LISTFLAG=0
LUNFLAG=0
VERBOSEFLAG=0
DEVICEFLAG=0
HELPFLAG=0
TARGETFLAG=0
TYPEFLAG=0

# parse command line into arguments

getopt lLtvhd:m $* 1>/dev/null 2>/dev/null

# check result of parsing

if [ $? != 0 ]
then
 echo "Bad argument or missing argument"
 exit 1
fi

set -- `getopt lLtvhd:m $*`

while [ $1 != -- ]
do
 case $1 in
 	-l) LISTFLAG=1;;
  	-L) LUNFLAG=1;;
  	-t) TARGETFLAG=1;;
  	-v) VERBOSEFLAG=1;;
	-m) TYPEFLAG=1;;
  	-h) HELPFLAG=1;;
  	-d) DEVICEFLAG=1
      	    DEVICE=$2
            shift;;
 esac
 shift   # next flag
done

shift   # skip --

if [ $HELPFLAG -eq 1 ]
then
 help_message
 exit 0
elif [ $DEVICEFLAG -eq 1 ]
then
 # list output for a specific adapter

 device_output $DEVICE
 exit 0
elif [ $LISTFLAG -eq 1 ]
then
 # list adapters, then exit

 list_adapters
 exit 0
elif [ $LUNFLAG -eq 1 ] || [ $TARGETFLAG -eq 1 ] || [ $TYPEFLAG -eq 1 ]
then
 # list LUNs for each adapter

 if [ $LUNFLAG -eq 1 ]
 then
 	echo "LUNs..."
	echo ""
 	lun_output
 fi

 # list targets for all devices

 if [ $TARGETFLAG -eq 1 ]
 then
  	echo "Targets..."
  	echo ""
  	target_output
 fi

 # list each adapter's model type

 if [ $TYPEFLAG -eq 1 ]
 then
	echo "Adapter Models..."
	echo ""
	adapter_types
 fi

 exit 0
fi

normal_output

