This file is indexed.

/usr/sbin/ql-set-cmd-timeout is in qla-tools 20140529-2.

This file is owned by root:root, with mode 0o755.

The actual contents of the file can be viewed below.

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
#!/bin/bash

# QLogic FC HBA Set Device Timeout Utility
# Copyright (C) 2006 QLogic Corporation (www.qlogic.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 of the License, 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., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA

#--------------------------------------------------------------------------# 
#This script is used to set the timeout on devices connected to QLogic  HBAs 
#The timeout is applied to the commands sent to the target. This will be useful
#where targets need longer time to execute a command during heavy I/O. 
#Setting a longer timeout can reduce the chances of Linux SCSI midlayer aborting
#the tasks.
#---------------------------------------------------------------------------#


QL_SETTIMEOUT_VERSION=1.7
QL_SUCCESS=0
QL_FAIL=1
QL_CALL_AGAIN=2
QL_CALL_RETURNED=3
QL_CALL_AGAIN_NO_WAIT=4
QL_CALL_AGAIN_NO_WAIT1=5
QL_COMMON=0
QL_SETT_SCRIPT=$0
QL_MAX_TO=65536 
QL_MIN_TO=0 
ALL_HOSTS=()
ALL_TARGETS=()

#*****************************************#
# qlu_hit_any_key()
# Waits for user to hit a key
# PARAMETERS : NONE
# RETURNS        : None
#*****************************************#
function qlu_hit_any_key()
{
        echo -n "Hit any key to continue......"
        read -n 1
        clear
}

#*****************************************#
# qlu_is_number()
# Check is input is a number or not
# PARAMETERS : IN Values
# RETURNS        : 0: If number
#*****************************************#
function qlu_is_number()
{
	echo $1 | grep -w "^[0-9]\+$" >& /dev/null
	return $?
}

#*****************************************#
# qlu_display_usage()
# Displays the short usage message
# PARAMETERS : None
# RETURNS        : None
#*****************************************#
function qlu_display_usage()
{
	echo "Usage: $QL_SETT_SCRIPT HOST TARGET TIMEOUT"
	echo "For more information please see help"
	echo "$QL_SETT_SCRIPT -h"
	exit 1
}

# --------------------------------------------------------- #
# echo_b()		                                    #
# Prints messages in bold				    #
# Parameter: 						    #
#	$1	Message to be printed			    #
# Returns: None.		 			    #
# --------------------------------------------------------- #

function echo_b() {
	echo -en "\033[1m${1}\033[0m"
	tput sgr0
}


# --------------------------------------------------------- #
# qlu_settimeout_help ()		                    #
# Prints the help message                                   #
# Parameter: None                                           #
# Returns: None                                             #
# --------------------------------------------------------- #
function qlu_settimeout_help() {
	clear
        echo ""
        echo_b "QLogic Linux Set Device Timeout Utility v$QL_SETTIMEOUT_VERSION"
        echo ""
        echo ""
        echo "Usage: ${QL_SETT_SCRIPT} [OPTIONS]"
        echo ""
        echo " [DEFAULT]"
        echo "				Display timeout of devices connected to all HOSTs"
        echo ""
        echo " [HOST]"
        echo "				Display timeout of devices connected to HOST"
	echo ""
        echo " [HOST] [TARGET]"
        echo "				Display timeout of devices connected to a TARGET on HOST"
        echo ""
        echo " [HOST] [TARGET] [TIMEOUT]"
        echo "				To set timeout on devices connected to a TARGET on HOST"

	echo ""
        echo " -h,  --help, ?"
	echo "				Prints this help message"
	echo ""
        echo " -i,  --interactive"
        echo "				Use this option to use the menu driven program"

}

#*****************************************#
# modify_devices()
# Changes the timeout of specified device
# PARAMETERS : HOST TARGET AND TIMEOUT
# RETURNS : NONE
#*****************************************#
function modify_devices ()
{
local RET=1
local L_WC=0
local L_HOST=${1}
local L_TARGET=${2}
local L_TO=${3}

echo ""
while [ $RET -ne 0 ]
do
  if  [ ${L_TO} -ge ${QL_MIN_TO} ] && [ ${L_TO} -le ${QL_MAX_TO} ]; then 
           get_luns ${L_HOST} ${L_TARGET}
           if [ $? -eq 0 ]; then
                 #echo "No LUN for this host,target pair"
                 return 1
           else
    		echo "Modifying the devices......"
    		echo ""
	        echo -e "DEVICE\t\t\t\tOLD TIMEOUT\t\tNEW TIMEOUT" 
                 QL_SORTED_LUNS=(`echo ${QL_LUNS[@]} | sed "s/ /\\n/g" | sort -n`)
                   cd /sys/class/scsi_device
                   for LUN in ${QL_SORTED_LUNS[@]}
                   do
                       cd ${L_HOST}\:0\:${L_TARGET}\:${LUN}/device
                       echo_b "${L_HOST}:0:${L_TARGET}:${LUN}"
                       L_WC=`echo ${L_HOST}:0:${L_TARGET}:${LUN} | wc -m`
                       qlu_is_number $L_WC
                       if [ $? -eq 0 ]; then
                            if [ $L_WC -lt 9 ]; then
                                 echo -en "\t\t\t\t"
                            elif [ $L_WC -lt 17 ]; then
                                   echo -en "\t\t\t"
                            elif [ $L_WC -lt 25 ]; then
                                   echo -en "\t\t"
                            else
                                  echo -en "\t"
                            fi
		       else
				echo -e "\t"
	 	       fi
	     
		echo -en "`cat timeout`"
		echo ${L_TO} > timeout
		echo -e "\t\t\t`cat timeout`"
		cd ../..
          done | more
         RET=0
      fi
 else
      while [ $RET -ne 0 ]   
      do
         echo -en "Select new timeout in the range [$QL_MIN_TO - $QL_MAX_TO] : "
         read L_TO
	 qlu_is_number $L_TO
	 RET=$?
	 if [ $RET -ne 0 ] ; then
		echo "Please enter a numeric value : "
	 fi
      done
        RET=1
 fi
done
}

# --------------------------------------------------------- #
# get_hosts ()                                              #
# Detects all the targets connected to given host and fills #
# ALL_TARGETS array                                         #
# Parameters : 					            #		 
#         	NONE                                        #
# Returns :                                                 #
#               Number of hosts                             #
# --------------------------------------------------------- #

function get_hosts
{
	ls -d /sys/bus/pci/drivers/qla*/*/host* &> /dev/null
	if [ $? -eq 0 ]; then
		ALL_HOSTS=( `ls -d /sys/bus/pci/drivers/qla*/*/host* | sed -e "s/.*host//"` )
	fi
		
        return ${#ALL_HOSTS[@]}
}

# --------------------------------------------------------- #
# get_targets ()                                            #
# Detects all the targets connected to given host and fills #
# ALL_TARGETS array                                          #
# Parameters :                                              #
#              HOST                                         #
# Returns :                                                 #
#           Number of targets                               #
# --------------------------------------------------------- #
function get_targets
{
local HOST=$1
local TARGET
local TARGETS=()
        if [ -e /sys/class/scsi_host/host$HOST/device/ ]; then
                cd /sys/class/scsi_host/host$HOST/device/

                ls -d rport-$HOST:0-* &> /dev/null
                if [ $? -eq 0 ]; then
                        ALL_TARGETS=( `ls -d rport-$HOST:0-* | sed "s/rport-$HOST:0-//"` )
                fi

                ls -d target$HOST:0:* &> /dev/null
                if [ $? -eq 0 ]; then
                        ALL_TARGETS=( `ls -d target$HOST:0:* | sed "s/target$HOST:0://"` )
                fi
                
		ls -d $HOST:0:* &> /dev/null
		if [ $? -eq 0 ]; then
			TARGETS=`ls -d $HOST:0:* | sed "s/$HOST:0:\(.*\):.*/\1/"`
			for TARGET in $TARGETS
			do
				echo "${ALL_TARGETS[@]}" | grep -w $TARGET >& /dev/null
				if [ $? -ne 0 ]; then
					ALL_TARGETS=(${ALL_TARGETS[@]} $TARGET)
				fi
			done
                fi

        fi
        return ${#ALL_TARGETS[@]}
}

# --------------------------------------------------------- #
# get_luns ()                                               #
# Detects all the LUNS connected to given TARGET and fills  #
# QL_LUNS array                                             #
# Parameters :                                              #
#              <HOST> <TARGET>                              #
# Returns :                                                 #
#           Number of LUNS                                  #
# --------------------------------------------------------- #

function get_luns
{
        local HOST=${1}
        local TARGET=${2}

        if [ -e /sys/class/scsi_host/host$HOST/device/target${HOST}:0:${TARGET} ]; then
                cd /sys/class/scsi_host/host$HOST/device/target${HOST}:0:${TARGET}/
        elif [ -e /sys/class/scsi_host/host$HOST/device/rport-$HOST:0-$TARGET/target${HOST}:0:${TARGET} ]; then
                cd /sys/class/scsi_host/host$HOST/device/rport-$HOST:0-$TARGET/target${HOST}:0:${TARGET}/
        fi

        QL_LUNS=( `ls -d $HOST:0:$TARGET:* 2> /dev/null | sed "s/$HOST:0:$TARGET://"` )
        return ${#QL_LUNS[@]}
}

#*****************************************#
# display_timeout()
# Displays the timeout of specified device
# PARAMETERS : HOST & TARGET
# RETURNS : NONE
#*****************************************#

function display_timeout
{
   local L_WC
   local L_HOST=${1}
   local L_TARGET=${2}

 get_luns ${L_HOST} ${L_TARGET}
        if [ $? -eq 0 ]; then
                echo "No LUNS for HOST:$L_HOST TARGET:$L_TARGET "
        else
                echo ""
                echo -e "DEVICE\t\t\t\tTIMEOUT"
                QL_SORTED_LUNS=(`echo ${QL_LUNS[@]} | sed "s/ /\\n/g" | sort -n`)
                cd /sys/class/scsi_device
                for LUN in ${QL_SORTED_LUNS[@]}
                do
                        cd ${L_HOST}\:0\:${L_TARGET}\:${LUN}/device
                        echo_b "${L_HOST}:0:${L_TARGET}:${LUN}"
                        L_WC=`echo ${L_HOST}:0:${L_TARGET}:${LUN} | wc -m`
			qlu_is_number $L_WC
                        if [ $? -eq 0 ]; then
                            if [ $L_WC -lt 9 ]; then
                                 echo -en "\t\t\t\t"
                            elif [ $L_WC -lt 17 ]; then
                                   echo -en "\t\t\t"
                            elif [ $L_WC -lt 25 ]; then
                                   echo -en "\t\t"
                            else
                                  echo -en "\t"
                            fi
                       else
                                echo -e "\t"
                       fi

                       echo -e "`cat timeout`"
                       cd ../..
                   done | more
          fi
}

#*****************************************#
# display_selective_dev() 
# Displays the timeout of specified device
# PARAMETERS :NONE 
# RETURNS : NONE
#*****************************************#

function display_selective_dev
{
	local L_WC
	local L_GO_BACK=0
	local L_QUIT=0
	local SEL_HOST		
	local SEL_TARGET
   	local SC2_CHOICE=0
   	local SC3_CHOICE=0

get_hosts
STATUS=$QL_CALL_AGAIN # to adjust with while loop
while [ $STATUS -eq $QL_CALL_AGAIN ]
do	
	clear
	LN=1 # Line number
	echo "Select  HOST "
	for HOST in ${ALL_HOSTS[@]}
	do
		echo "	$LN. Host $HOST"
		LN=$((LN+1))
	done
	echo "	$LN. GO BACK TO PREVIOUS MENU"
	L_GO_BACK=$LN
	LN=$((LN+1))

	echo "	$LN. QUIT"
	echo ""
	echo -n "Please select one of the options above : "
	read SC2_CHOICE  # read user choice for qlu_screen_2
	if [ "$SC1_CHOICE" == "" ]; then
		continue
	fi	
	case $SC2_CHOICE in
		q | Q | quit | $LN )
		exit 0
		;;
	esac

	if [ $SC2_CHOICE -ge 1 ] && [ $SC2_CHOICE -lt $LN ]; then 
		STATUS=$QL_CALL_RETURNED
	fi
done

if [ $SC2_CHOICE -ne $L_GO_BACK ]; then
	SEL_HOST=${ALL_HOSTS[((${SC2_CHOICE} - 1))]}
else
	return $QL_CALL_AGAIN_NO_WAIT
fi

STATUS=$QL_CALL_AGAIN # to adjust with while loop
while [ $STATUS -eq $QL_CALL_AGAIN ]
do	
	clear
	LN=1 # Line number
	echo "Select Target for HOST${SEL_HOST}"

	get_targets ${SEL_HOST}
	for TARGETS in ${ALL_TARGETS[@]}
	do
		echo "	$LN. Target $TARGETS"
		LN=$((LN+1))
	done

	echo "	$LN. GO BACK TO PREVIOUS MENU"
	L_GO_BACK=$LN
	LN=$((LN+1))
	echo "	$LN. QUIT"
	L_QUIT=$LN

	echo ""
	echo "Please select one of the options above : "
	read SC3_CHOICE # read user choice for qlu_screen_3
	case $SC3_CHOICE in
		q | Q | quit | $L_QUIT )
		exit 0
		;;
	esac

	if [ $SC3_CHOICE -ge 1 ] && [ $SC3_CHOICE -lt $LN ]; then
		STATUS=$QL_CALL_RETURNED
	fi
done
if [ $SC2_CHOICE -ne $L_GO_BACK ]; then
	SEL_TARGET=${ALL_TARGETS[((${SC3_CHOICE} - 1))]}
else
	return $QL_CALL_AGAIN_NO_WAIT1
fi
	get_luns ${SEL_HOST} ${SEL_TARGET}
	if [ $? -eq 0 ]; then
		echo "No LUNS for HOST:$SEL_HOST TARGET:$SEL_TARGET "
	else
		echo ""
		echo -e "DEVICE\t\t\t\tTIMEOUT"
		QL_SORTED_LUNS=(`echo ${QL_LUNS[@]} | sed "s/ /\\n/g" | sort -n`)
		cd /sys/class/scsi_device
		for LUN in ${QL_SORTED_LUNS[@]}
		do
			cd ${SEL_HOST}\:0\:${SEL_TARGET}\:${LUN}/device
			echo_b "${SEL_HOST}:0:${SEL_TARGET}:${LUN}" 
			L_WC=`echo ${SEL_HOST}:0:${SEL_TARGET}:${LUN} | wc -m`
			qlu_is_number $L_WC
			if [ $? -eq 0 ]; then
		   	    if [ $L_WC -lt 9 ]; then
		 	         echo -en "\t\t\t\t"
		            elif [ $L_WC -lt 17 ]; then
		   	           echo -en "\t\t\t"
 		            elif [ $L_WC -lt 25 ]; then
			           echo -en "\t\t"
		            else
		 	          echo -en "\t"
		            fi
		       else
                                echo -e "\t"
                       fi
 
	               echo -e "`cat timeout`" 
                       cd ../..
		done 
          fi
return $QL_CALL_RETURNED
} 

#*****************************************#
# display_all()                           #
# Display timeout of all devices,         #
# internally calls display_timeout        # 
# PARAMETERS : HOST / ALL                 #
# RETURNS    : NONE                       #
#*****************************************#	

function display_all()
{
local OPTION=${1}
local HOST_LIST=()

echo "${1}" | grep "^[0-9]" &> /dev/null
if [ $? -eq 0 ]; then
	get_hosts
	echo "${ALL_HOSTS[@]}" | grep -w "${1}" &> /dev/null
	if [ $? -ne 0 ]; then
		echo "ERROR: HOST $1 not found on the system"
		exit 1
	else
		HOST_LIST=($1)
	fi
elif [ "$OPTION" == "ALL" ]; then
	get_hosts
	HOST_LIST=( `echo ${ALL_HOSTS[@]}` )
else
	exit 1
fi

for HOST in ${HOST_LIST[@]}
do
	get_targets $HOST
	if [ $? -eq 0 ]; then
		echo "No TARGET connected to HOST$HOST"
	else
		for TARGET in ${ALL_TARGETS[@]}
		do
			display_timeout $HOST $TARGET 	
		done
	fi
done
}

#*****************************************#
# all_devices()
# Changes timeout of all devices,
# internally calls modify_devices
# PARAMETERS : TIMEOUT (if QL_COMMON =1)
#              else
#              NONE
# RETURNS    : NONE
#*****************************************#

function all_devices ()
{

BACK=$QL_CALL_AGAIN # to adjust with while loop
while [ $BACK -eq $QL_CALL_AGAIN ]
do
	STATUS=$QL_CALL_AGAIN # to adjust with while loop
	while [ $STATUS -eq $QL_CALL_AGAIN ]
	do
		clear
		LN=1 # Line number
		echo "Select option: "
		echo "	1. MODIFY TIMEOUT"
		echo "	2. DISPLAY TIMEOUT"
		echo "	3. GO BACK TO PREVIOUS MENU"
		echo "	4. QUIT "
		echo ""
		echo -n "Please select one of the options above : "
		read SC2_CHOICE # read user choice for qlu_screen_2

		case $SC2_CHOICE in
			q | Q | quit | 4 )
			exit 0
			;;
		esac

		if [ $SC2_CHOICE -ge 1 ] && [ $SC2_CHOICE -lt 4 ]; then
			STATUS=$QL_CALL_RETURNED
		fi
	done

	if [ $SC2_CHOICE -ne 3 ]; then
		if [ $SC2_CHOICE -eq 1 ]; then
			modify_all_devices
			BACK=$QL_CALL_AGAIN
			qlu_hit_any_key
		else
			display_all ALL | more
			BACK=$QL_CALL_AGAIN
			qlu_hit_any_key
		fi
	else
		return $QL_CALL_AGAIN_NO_WAIT
	fi
done

return $QL_CALL_RETURNED
}

#*****************************************#
# modify_all_devices()
# Changes timeout of all devices, 
# internally calls modify_devices
# PARAMETERS : TIMEOUT (if QL_COMMON =1)
#	       else 
#	       NONE
# RETURNS    : NONE
#*****************************************#

function modify_all_devices ()
{

local RET=1
# Clean ALL_TARGETS() first
ALL_TARGETS=()
cd /sys/class/scsi_device
get_hosts
for HOST in ${ALL_HOSTS[@]}
do
ls $HOST:0:*:*/device/timeout >& /dev/null
if [ $? -eq 0 ]; then
	for TARGET in `ls -d $HOST:0:* 2> /dev/null | sed -e "s/$HOST:0://" | sed -e "s/:.*//"`
	do
		#echo " target value $TARGETS"
		echo ${ALL_TARGETS[@]} | grep "$TARGET\b" >& /dev/null
		if [ $? -ne 0 ]; then
			ALL_TARGETS=(${ALL_TARGETS[@]} $TARGET)
		fi
	done

	for TARGET in ${ALL_TARGETS[@]}
	do
		if [ $QL_COMMON -eq 0 ]; then
			while [ $RET -ne 0 ]
			do
				echo -n "Enter new timeout for HOST $HOST and TARGET $TARGET: "
				read N_TIMEOUT
				qlu_is_number $N_TIMEOUT
				RET=$?	
				if [ $RET -ne 0 ]; then
					echo "Please enter a numeric value"
				fi
			done
			RET=1
		else 
			N_TIMEOUT=$1
		fi
		modify_devices ${HOST} ${TARGET} ${N_TIMEOUT}	
	done
fi
done
}

#*****************************************#
# modify_sel_device ()
# Changes timeout of selected HOST
# internally calls modify_devices
# PARAMETERS : HOST
# RETURNS    : $QL_CALL_RETURNED
#*****************************************#
function modify_sel_device ()
{
clear
local SEL_HOST=$1
local SC2_CHOICE=0
local L_GO_BACK=0
local L_QUIT=0
local RET=1

cd /sys/class/scsi_device

ls $SEL_HOST:0:*:*/device/timeout >& /dev/null
if [ $? -ne 0 ]; then
	echo "No Targets connected to Host $SEL_HOST"
	return $QL_CALL_RETURNED
fi
for TARGETS in `ls -d $SEL_HOST:0:* | sed -e "s/$SEL_HOST:0://" | sed -e "s/:.*//"`
do
	#echo " target value $TARGETS"
	echo ${ALL_TARGETS[@]} | grep "$TARGETS\b" >& /dev/null
	if [ $? -ne 0 ]; then
		ALL_TARGETS=(${ALL_TARGETS[@]} $TARGETS)
	fi
done

BACK=$QL_CALL_AGAIN
while [ $BACK -eq $QL_CALL_AGAIN ]
do
	STATUS=$QL_CALL_AGAIN # to adjust with while loop
	while [ $STATUS -eq $QL_CALL_AGAIN ]
	do	
		clear
		LN=1 # Line number
		echo "Select Target for HOST${SEL_HOST}"
		for TARGETS in ${ALL_TARGETS[@]}
		do
			echo "	$LN. Target $TARGETS"
			LN=$((LN+1))
		done
		echo "	$LN. GO BACK TO MAIN MENU"
			L_GO_BACK=$LN
			LN=$((LN+1))
		echo "	$LN. QUIT"
			L_QUIT=$LN
		echo ""
		echo -n "Please select one of the options above : "
		read SC2_CHOICE # read user choice for qlu_screen_2
	
		case $SC2_CHOICE in
			q | Q | quit | $L_QUIT )
			exit 0
			;;
		esac

		if [ $SC2_CHOICE -ge 1 ] && [ $SC2_CHOICE -lt $LN ]; then
			STATUS=$QL_CALL_RETURNED
		fi
	done
	if [ $SC2_CHOICE -ne $L_GO_BACK ]; then
		SEL_TARGET=${ALL_TARGETS[((${SC2_CHOICE} - 1))]}
		echo ""
		AGAIN=$QL_CALL_AGAIN # to adjust with while loop
		while [ $AGAIN -eq $QL_CALL_AGAIN ]
		do
			STATUS=$QL_CALL_AGAIN # to adjust with while loop
			while [ $STATUS -eq $QL_CALL_AGAIN ]
			do
        			clear
		        	LN=1 # Line number
	        		echo "Select option: "
				echo "	1. MODIFY TIMEOUT"
				echo "	2. DISPLAY TIMEOUT"
				echo "	3. GO BACK TO PREVIOUS MENU"
				echo "	4. GO BACK TO MAIN MENU"
				echo "	5. QUIT	"
				echo ""
				echo -n "Please select one of the options above : "
				read SC2_CHOICE # read user choice for qlu_screen_2
				case $SC2_CHOICE in
					q | Q | quit | 5 )
					exit 0
					;;
				esac

				if [ $SC2_CHOICE -ge 1 ] && [ $SC2_CHOICE -le 4 ]; then
					STATUS=$QL_CALL_RETURNED
				fi
			done
			if [ $SC2_CHOICE -eq 1 ]; then
				while [ $RET -ne 0 ]
				do
					echo -n "Please enter new timeout for selected device : "
					read N_TIMEOUT
					qlu_is_number $N_TIMEOUT
					RET=$?
					if [ $RET -ne 0 ]; then
						echo "Please enter a numeric value"
					fi
				done
				RET=1
				modify_devices ${SEL_HOST} ${SEL_TARGET} ${N_TIMEOUT}
				AGAIN=$QL_CALL_AGAIN # to adjust with while loop
				qlu_hit_any_key
			fi
			if [ $SC2_CHOICE -eq 2 ]; then
				display_timeout ${SEL_HOST} ${SEL_TARGET}
				AGAIN=$QL_CALL_AGAIN # to adjust with while loop
				qlu_hit_any_key
			fi
			if [ $SC2_CHOICE -eq 3 ]; then
				AGAIN=$QL_CALL_RETURNED
				BACK=$QL_CALL_AGAIN 
			fi
			if [ $SC2_CHOICE -eq 4 ]; then
				return $QL_CALL_AGAIN_NO_WAIT
			fi
		done
	else
		return $QL_CALL_AGAIN_NO_WAIT
	fi
done
return $QL_CALL_RETURNED
}


#*****************************************#
# qlu_main_screen ()
# Displays the main screen of the program
# Accepts and validates the input
# PARAMETERS : NONE
# RETURNS    : $QL_CALL_AGAIN
#*****************************************#
function qlu_main_screen ()
{
local RET=1
local HOST=0
local TARGET 
	
get_hosts
STATUS=$QL_CALL_AGAIN # to adjust with while loop
while [ $STATUS -eq $QL_CALL_AGAIN ]
do
	clear
	echo_b " Welcome to QLogic Set Device Timeout Utility"
	echo ""
	echo "=============================================="
	echo ""
	echo "MAIN MENU"
	LN=1  # LN for Line Number
	for HOST in ${ALL_HOSTS[@]}
	do
		echo "	$LN. HOST${HOST}" #1
		LN=$((LN+1)) #2
	done

	echo "	$LN. SELECT ALL HOSTS" #2
	echo "	$((LN+1)). SET COMMON TIMEOUT FOR ALL HOSTS"  #3
	echo "	$((LN+2)). QUIT"  #4
	echo ""
	echo -n "Please select one of the options above : "
	read SC1_CHOICE # read user choice for qlu_screen_2 
	
	TEMP=$((LN+2)) #4
	case $SC1_CHOICE in
		q | Q | quit | $TEMP )
		exit 0
		;;
	esac

	if [ $SC1_CHOICE -ge 1 ] && [ $SC1_CHOICE -le $((LN+2)) ]; then
		STATUS=$QL_CALL_RETURNED
	fi
done

TEMP=$((LN+1)) #3
#Choice 1
if [ $SC1_CHOICE -lt ${LN} ] && [ $SC1_CHOICE -gt 0 ]; then #0<choice<2
	SEL_HOST=${ALL_HOSTS[((${SC1_CHOICE} - 1))]}
	modify_sel_device $SEL_HOST
	if [ $? -eq $QL_CALL_AGAIN_NO_WAIT ]; then
		return $QL_CALL_AGAIN_NO_WAIT
	fi
	return $QL_CALL_AGAIN

#Choice	3
elif [ $SC1_CHOICE -eq $TEMP ]; then

	while [ $RET -ne 0 ]
	do
		echo -n "Enter the common timeout : "
		read N_TIMEOUT
		qlu_is_number $N_TIMEOUT
		RET=$?
		if [ $RET -ne 0 ]; then
			echo "Please enter a numeric value"
		fi
	done
	RET=1

	QL_COMMON=1
	modify_all_devices $N_TIMEOUT
	QL_COMMON=0
	return $QL_CALL_AGAIN

#Choice 2	
elif [ $SC1_CHOICE -eq $LN ]; then
	QL_COMMON=0
	all_devices
	STATUS=$?
	if [ $STATUS -eq $QL_CALL_AGAIN_NO_WAIT ]; then
		return $QL_CALL_AGAIN_NO_WAIT 
	else
		return $QL_CALL_AGAIN
	fi
else
	return $QL_CALL_AGAIN
fi
}

# Start
#check if /sys exist, if not cannot do anything
if [ ! -e /sys/class/scsi_device ]; then
	echo "Could not find /sys/class/scsi_device, cannot proceed."
	echo "Please make sure sysfs filesystem is mounted."
	exit 1
fi

if [ $# -ne 0 ]; then
	# need to loop thru the  args
	until [ -z "$1" ]  # Until all parameters used up...
	do
		case $1 in
			-i | --interactive )
				STATUS=$QL_CALL_AGAIN
				clear
				while [ $STATUS -eq $QL_CALL_AGAIN ]
				do
					qlu_main_screen
					STATUS=$?
					if [ $STATUS -eq $QL_CALL_AGAIN_NO_WAIT ]; then
						STATUS=$QL_CALL_AGAIN;	
					else
						qlu_hit_any_key
					fi
				done
				exit 0
			;;

			-h | --help | \? )
				qlu_settimeout_help
				exit 0
			;;
			* )
				#get host/target/timeout
				IN_HOST=${1}
				IN_TARGET=${2}
				IN_TIMEOUT=${3}

				#validate if valid HOST
				get_hosts
				HOST_LIST=( `echo "${ALL_HOSTS[@]}"` )
				echo "${HOST_LIST[@]}" | grep "${IN_HOST}\b" &> /dev/null
				if [ $? -ne 0 ]; then
					echo "Host: $IN_HOST is not a QLogic HBA"
					exit 1
				fi

				if [ $# -eq 3 ]; then                            
					get_targets ${IN_HOST}
					echo "${ALL_TARGETS[@]}" | grep "${IN_TARGET}\b" &> /dev/null
					if [ $? -ne 0 ]; then
						echo "Enter valid TARGET"
						qlu_display_usage
					        exit 0
					fi
					qlu_is_number ${IN_TIMEOUT}
					if [ $? -ne 0 ]; then
						echo "Input should be a number"
						qlu_display_usage
                        	        else  
					     modify_devices ${IN_HOST} ${IN_TARGET} ${IN_TIMEOUT}
					     exit 0
					fi
				fi								
				#here means user specified only HOST/TARGET
				if [ $# -eq 2 ]; then
					get_targets ${IN_HOST}
					echo "${ALL_TARGETS[@]}" | grep "${IN_TARGET}\b" &> /dev/null
					if [ $? -ne 0 ]; then
						echo "Enter valid TARGET"
						qlu_display_usage
					        exit 0
					fi	
						display_timeout	${IN_HOST} ${IN_TARGET}
						exit 0
                                else
					display_all ${IN_HOST} | more
                                fi
			;;
			
		esac
	     shift
	done
else
        display_all ALL | more
	#qlu_display_usage
fi