|
root / base / usr / src / cmd / fcinfo / fcinfo-list.c
fcinfo-list.c C 1412 lines 39.8 KB
   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
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
/*
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License (the "License").
 * You may not use this file except in compliance with the License.
 *
 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
 * or http://www.opensolaris.org/os/licensing.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */
/*
 * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * Use is subject to license terms.
 */


#include "fcinfo.h"
#include <libintl.h>

struct lun {
	uchar_t val[8];
};

typedef enum {
    HBA_PORT,
    REMOTE_PORT,
    LOGICAL_UNIT
} resource_type;

typedef struct rep_luns_rsp {
	uint32_t    length;
	uint32_t    rsrvd;
	struct lun  lun[1];
} rep_luns_rsp_t;

static int getTargetMapping(HBA_HANDLE, HBA_WWN myhbaPortWWN,
    HBA_FCPTARGETMAPPINGV2 **mapping);
static int processHBA(HBA_HANDLE handle, HBA_ADAPTERATTRIBUTES attrs,
    int portIndex, HBA_PORTATTRIBUTES port, HBA_FCPTARGETMAPPINGV2 *map,
    int resourceType, int flags, int mode);
static void processRemotePort(HBA_HANDLE handle, HBA_WWN portWWN,
    HBA_FCPTARGETMAPPINGV2 *map, int wwnCount, char **wwn_argv, int flags);
static void handleRemotePort(HBA_HANDLE handle, HBA_WWN portWWN,
    HBA_WWN myRemotePortWWN, HBA_PORTATTRIBUTES *discPort);
static void printLinkStat(HBA_HANDLE handle, HBA_WWN hbaportWWN,
    HBA_WWN destWWN);
static void handleScsiTarget(HBA_HANDLE handle, HBA_WWN hbaPortWWN,
    HBA_WWN scsiTargetWWN, HBA_FCPTARGETMAPPINGV2 *map);
static int retrieveAttrs(HBA_HANDLE handle, HBA_WWN hbaPortWWN,
    HBA_ADAPTERATTRIBUTES *attrs, HBA_PORTATTRIBUTES *port, int *portIndex);
static void searchDevice(discoveredDevice **devList, HBA_FCPSCSIENTRYV2 entry,
    HBA_WWN initiatorPortWWN, HBA_HANDLE handle, boolean_t verbose);

/*
 * This function retrieve the adapater attributes, port attributes, and
 * portIndex for the given handle and hba port WWN.
 *
 * Arguments:
 *	handle	    an HBA_HANDLE to a adapter
 *	hbaPortWWN  WWN of the port on the adapter to which to retrieve
 *			HBA_PORTATTRIBUTES from
 *	attrs	    pointer to a HBA_ADAPTERATTRIBUTES structure.  Upon
 *			successful completion, this structure will be filled in
 *	port	    pointer to a HBA_PORTATTRIBUTES structure.  Upon successful
 *			completion, this structure will be fill in
 *	portIndex   the Index count of the port on the adapter that is
 *			associated with the WWN.
 *
 * Returns
 *	0	    successfully retrieve all information
 *	>0	    otherwise
 */
static int
retrieveAttrs(HBA_HANDLE handle, HBA_WWN hbaPortWWN,
    HBA_ADAPTERATTRIBUTES *attrs, HBA_PORTATTRIBUTES *port, int *portIndex)
{
	HBA_STATUS		status;
	int			portCtr;
	int			times;

	/* argument checking */
	if (attrs == NULL || port == NULL || portIndex == NULL) {
		fprintf(stderr, gettext("Error: Invalid arguments to "
			    "retreiveAttrs\n"));
		return (1);
	}

	/* retrieve Adapter attributes */
	memset(attrs, 0, sizeof (HBA_ADAPTERATTRIBUTES));
	status = HBA_GetAdapterAttributes(handle, attrs);
	times = 0;
	while ((status == HBA_STATUS_ERROR_TRY_AGAIN ||
	    status == HBA_STATUS_ERROR_BUSY) &&
	    times++ < HBA_MAX_RETRIES) {
		(void) sleep(1);
		status = HBA_GetAdapterAttributes(handle, attrs);
		if (status == HBA_STATUS_OK) {
			break;
		}
	}
	if (status != HBA_STATUS_OK) {
		fprintf(stderr, gettext("Failed to get adapter "
		    "attributes handle(%d) Reason: "), handle);
		printStatus(status);
		fprintf(stderr, "\n");
		return (1);
	}

	/*
	 * find the corresponding port on the adapter and retrieve
	 * port attributes as well as the port index
	 */
	memset(port,  0, sizeof (HBA_PORTATTRIBUTES));
	for (portCtr = 0; portCtr < attrs->NumberOfPorts; portCtr++) {
		if ((status = HBA_GetAdapterPortAttributes(handle,
				    portCtr, port)) != HBA_STATUS_OK) {
			fprintf(stderr,
			    gettext("Error: Failed to get port (%d) "
				    "attributes reason: "), portCtr);
			printStatus(status);
			fprintf(stderr, "\n");
			return (1);
		}
		if (memcmp(hbaPortWWN.wwn, port->PortWWN.wwn,
			    sizeof (port->PortWWN.wwn)) == 0) {
			break;
		}
	}
	if (portCtr >= attrs->NumberOfPorts) {
		/*
		 * not able to find corresponding port WWN
		 * returning an error
		 */
		*portIndex = 0;
		return (1);
	}
	*portIndex = portCtr;
	return (0);
}

/*
 * This function retrieves target mapping information for the HBA port WWN.
 * This function will allocate space for the mapping structure which the caller
 * must free when they are finished
 *
 * Arguments:
 *	handle - a handle to a HBA that we will be processing
 *	hbaPortWWN - the port WWN for the HBA port to retrieve the mappings for
 *	mapping - a pointer to a pointer for the target mapping structure
 *	    Upon successful completion of this function, *mapping will contain
 *	    the target mapping information
 *
 * returns:
 *	0	if successful
 *	1	otherwise
 */
static int
getTargetMapping(HBA_HANDLE handle, HBA_WWN hbaPortWWN,
    HBA_FCPTARGETMAPPINGV2 **mapping)
{
	HBA_FCPTARGETMAPPINGV2	*map;
	HBA_STATUS		status;
	int			count;

	/* argument sanity checking */
	if (mapping == NULL) {
		fprintf(stderr, gettext("Internal Error: mapping is NULL"));
		return (1);
	}
	*mapping = NULL;
	if ((map = calloc(1, sizeof (HBA_FCPTARGETMAPPINGV2))) == NULL) {
		fprintf(stderr,
		    gettext("Internal Error: Unable to calloc map"));
		return (1);
	}
	status = HBA_GetFcpTargetMappingV2(handle, hbaPortWWN, map);
	count = map->NumberOfEntries;
	if (status == HBA_STATUS_ERROR_MORE_DATA) {
		free(map);
		if ((map = calloc(1, (sizeof (HBA_FCPSCSIENTRYV2)*(count-1)) +
				    sizeof (HBA_FCPTARGETMAPPINGV2))) == NULL) {
			fprintf(stderr,
			    gettext("Unable to calloc map of size: %d"), count);
			return (1);
		}
		map->NumberOfEntries = count;
		status = HBA_GetFcpTargetMappingV2(handle, hbaPortWWN, map);
	}
	if (status != HBA_STATUS_OK) {
		fprintf(stderr,
		    gettext("Error: Unable to get Target Mapping\n"));
		printStatus(status);
		fprintf(stderr, "\n");
		free(map);
		return (1);
	}
	*mapping = map;
	return (0);
}

/*
 * This function handles the remoteport object.  It will issue a report lun
 * to determine whether it is a scsi-target and then print the information.
 *
 * Arguments:
 *	handle - a handle to a HBA that we will be processing
 *	portWWN - the port WWN for the HBA port we will be issuing the SCSI
 *	    ReportLUNS through
 *	remotePortWWN - the port WWN we will be issuing the report lun call to
 *	discPort - PORTATTRIBUTES structure for the remotePortWWN
 */
static void
handleRemotePort(HBA_HANDLE handle, HBA_WWN portWWN, HBA_WWN remotePortWWN,
    HBA_PORTATTRIBUTES *discPort)
{
	HBA_STATUS		status;
	int			scsiTargetType;
	uchar_t			raw_luns[LUN_LENGTH];
	HBA_UINT32		responseSize = LUN_LENGTH;
	struct scsi_extended_sense  sense;
	HBA_UINT32		senseSize = sizeof (struct scsi_extended_sense);
	HBA_UINT8		rep_luns_status;

	/* argument checking */
	if (discPort == NULL) {
		return;
	}

	memset(raw_luns, 0, sizeof (raw_luns));
	/* going to issue a report lun to check if this is a scsi-target */
	status = HBA_ScsiReportLUNsV2(handle, portWWN, remotePortWWN,
	    (void *)raw_luns, &responseSize, &rep_luns_status,
	    (void *)&sense, &senseSize);
	if (status == HBA_STATUS_OK) {
		scsiTargetType = SCSI_TARGET_TYPE_YES;
	} else if (status == HBA_STATUS_ERROR_NOT_A_TARGET) {
		scsiTargetType = SCSI_TARGET_TYPE_NO;
	} else {
		scsiTargetType = SCSI_TARGET_TYPE_UNKNOWN;
	}
	printDiscoPortInfo(discPort, scsiTargetType);
}

/*
 * This function will issue the RLS and print out the port statistics for
 * the given destWWN
 *
 * Arguments
 *	handle - a handle to a HBA that we will be processing
 *	hbaPortWWN - the hba port WWN through which the RLS will be sent
 *	destWWN - the remote port to which the RLS will be sent
 */
static void
printLinkStat(HBA_HANDLE handle, HBA_WWN hbaPortWWN, HBA_WWN destWWN)
{
	HBA_STATUS		status;
	fc_rls_acc_t		rls_payload;
	uint32_t		rls_payload_size;

	memset(&rls_payload, 0, sizeof (rls_payload));
	rls_payload_size = sizeof (rls_payload);
	status = HBA_SendRLS(handle, hbaPortWWN, destWWN,
	    &rls_payload, &rls_payload_size);
	if (status != HBA_STATUS_OK) {
		fprintf(stderr, gettext("Error: SendRLS failed for %016llx\n"),
		    wwnConversion(destWWN.wwn));
	} else {
		printPortStat(&rls_payload);
	}
}

int
printHBANPIVPortInfo(HBA_HANDLE handle, int portindex)
{
	HBA_PORTNPIVATTRIBUTES	portattrs;
	HBA_NPIVATTRIBUTES	npivattrs;
	HBA_STATUS		status;
	int			index;
	int			times = 0;

	status = Sun_HBA_GetPortNPIVAttributes(handle, portindex, &portattrs);
	while (status == HBA_STATUS_ERROR_TRY_AGAIN ||
	    status == HBA_STATUS_ERROR_BUSY) {
		(void) sleep(1);
		status = Sun_HBA_GetPortNPIVAttributes(
		    handle, portindex, &portattrs);
		if (times++ > HBA_MAX_RETRIES) {
			break;
		}
	}

	if (status == HBA_STATUS_ERROR_NOT_SUPPORTED) {
		fprintf(stdout, gettext("\tNPIV Not Supported\n"));
		return (0);
	}

	if (status != HBA_STATUS_OK) {
		fprintf(stderr,
		    gettext("Error: Failed to get port (%d) "
		    "npiv attributes reason: "), portindex);
		printStatus(status);
		fprintf(stderr, "\n");
		return (1);
	}
	if (portattrs.MaxNumberOfNPIVPorts) {
		fprintf(stdout, gettext("\tMax NPIV Ports: %d\n"),
		    portattrs.MaxNumberOfNPIVPorts);
	} else {
		fprintf(stdout, gettext("\tNPIV Not Supported\n"));
		return (0);
	}
	fprintf(stdout, gettext("\tNPIV port list:\n"));
	for (index = 0; index < portattrs.NumberOfNPIVPorts; index++) {
		int times = 0;
		status = Sun_HBA_GetNPIVPortInfo(handle,
		    portindex, index, &npivattrs);
		while (status == HBA_STATUS_ERROR_TRY_AGAIN ||
		    status == HBA_STATUS_ERROR_BUSY) {
			(void) sleep(1);
			status = Sun_HBA_GetNPIVPortInfo(handle,
			    portindex, index, &npivattrs);
			if (times++ > HBA_MAX_RETRIES) {
				break;
			}
		}

		if (status != HBA_STATUS_OK) {
			fprintf(stderr,
			    gettext("Error: Failed to get npiv port (%d) "
			    "attributes reason: "), index);
			printStatus(status);
			fprintf(stderr, "\n");
			return (1);
		} else {
			fprintf(stdout,
			    gettext("\t  Virtual Port%d:\n"), index+1);
			fprintf(stdout, gettext("\t\tNode WWN: %016llx\n"),
			    wwnConversion(npivattrs.NodeWWN.wwn));
			fprintf(stdout, gettext("\t\tPort WWN: %016llx\n"),
			    wwnConversion(npivattrs.PortWWN.wwn));
		}
	}
	return (0);
}

/*
 * This function will process hba port, remote port and scsi-target information
 * for the given handle.
 *
 * Arguments:
 *	handle - a handle to a HBA that we will be processing
 *	resourceType - resourceType flag
 *		possible values include: HBA_PORT, REMOTE_PORT
 *	flags - represents options passed in by the user
 *
 *  Return Value:
 *	    0		sucessfully processed handle
 *	    1		error has occured
 */
static int
processHBA(HBA_HANDLE handle, HBA_ADAPTERATTRIBUTES attrs, int portIndex,
    HBA_PORTATTRIBUTES port, HBA_FCPTARGETMAPPINGV2 *map,
    int resourceType, int flags, int mode)
{
	HBA_PORTATTRIBUTES	discPort;
	HBA_STATUS		status;
	int			discPortCount;

	if (resourceType == HBA_PORT) {
		if ((flags & PRINT_FCOE) == PRINT_FCOE &&
		    attrs.VendorSpecificID != 0xFC0E) {
			return (0);
		}
		printHBAPortInfo(&port, &attrs, mode);
		if ((flags & PRINT_LINKSTAT) == PRINT_LINKSTAT) {
			printLinkStat(handle, port.PortWWN, port.PortWWN);
		}
		return (0);
	}
	/*
	 * process each of the remote targets from this hba port
	 */
	for (discPortCount = 0;
	    discPortCount < port.NumberofDiscoveredPorts;
	    discPortCount++) {
		status = HBA_GetDiscoveredPortAttributes(handle,
		    portIndex, discPortCount, &discPort);
		if (status != HBA_STATUS_OK) {
			fprintf(stderr,
			    gettext("Failed to get discovered port (%d)"
				    " attributes reason :"), discPortCount);
			printStatus(status);
			fprintf(stderr, "\n");
			continue;
		}
		if (resourceType == REMOTE_PORT) {
			handleRemotePort(handle, port.PortWWN, discPort.PortWWN,
			    &discPort);
			if ((flags & PRINT_LINKSTAT) == PRINT_LINKSTAT) {
			    printLinkStat(handle, port.PortWWN,
				discPort.PortWWN);
			}
			if ((flags & PRINT_SCSI_TARGET) == PRINT_SCSI_TARGET) {
				handleScsiTarget(handle, port.PortWWN,
				    discPort.PortWWN, map);
			}
		}
	}
	return (0);
}

/*
 * This function will process remote port information for the given handle.
 *
 * Arguments:
 *	handle - a handle to a HBA that we will be processing
 *	portWWN - the port WWN for the HBA port we will be issuing the SCSI
 *	    ReportLUNS through
 *	wwnCount - the number of wwns in wwn_argv
 *	wwn_argv - argument vector of WWNs
 */
static void
processRemotePort(HBA_HANDLE handle, HBA_WWN portWWN,
    HBA_FCPTARGETMAPPINGV2 *map, int wwnCount, char **wwn_argv, int flags)
{
	int			remote_wwn_counter;
	uint64_t		remotePortWWN;
	HBA_WWN			myremotePortWWN;
	HBA_PORTATTRIBUTES	discPort;
	HBA_STATUS		status;

	for (remote_wwn_counter = 0;
	    remote_wwn_counter < wwnCount;
	    remote_wwn_counter++) {
		int times = 0;
		sscanf(wwn_argv[remote_wwn_counter], "%016llx",
		    &remotePortWWN);
		remotePortWWN = htonll(remotePortWWN);
		memcpy(myremotePortWWN.wwn, &remotePortWWN,
		    sizeof (remotePortWWN));
		memset(&discPort, 0, sizeof (discPort));
		status = HBA_GetPortAttributesByWWN(handle, myremotePortWWN,
		    &discPort);
		while (status == HBA_STATUS_ERROR_TRY_AGAIN ||
		    status == HBA_STATUS_ERROR_BUSY) {
			(void) sleep(1);
			status = HBA_GetPortAttributesByWWN(handle,
			    myremotePortWWN, &discPort);
			if (times++ > HBA_MAX_RETRIES) {
				break;
			}
		}
		if (status != HBA_STATUS_OK) {
			fprintf(stderr, gettext("HBA_GetPortAttributesByWWN "
				    "failed: reason: "));
			printStatus(status);
			fprintf(stderr, "\n");
			continue;
		}
		handleRemotePort(handle, portWWN, myremotePortWWN, &discPort);
		if ((flags & PRINT_LINKSTAT) == PRINT_LINKSTAT) {
			printLinkStat(handle, portWWN, myremotePortWWN);
		}
		if ((flags & PRINT_SCSI_TARGET) == PRINT_SCSI_TARGET) {
			handleScsiTarget(handle, portWWN,
			    myremotePortWWN, map);
		}
	}
}

/*
 * This function handles printing Scsi target information for remote ports
 *
 * Arguments:
 *	handle - a handle to a HBA that we will be processing
 *	hbaPortWWN - the port WWN for the HBA port through which the SCSI call
 *	    is being sent
 *	scsiTargetWWN - target port WWN of the remote target the SCSI call is
 *	    being sent to
 *	map - a pointer to the target mapping structure for the given HBA port
 */
static void
handleScsiTarget(HBA_HANDLE handle, HBA_WWN hbaPortWWN, HBA_WWN scsiTargetWWN,
    HBA_FCPTARGETMAPPINGV2 *map)
{
	HBA_STATUS		    status;
	struct scsi_inquiry	    inq;
	struct scsi_extended_sense  sense;
	HBA_UINT32		    responseSize, senseSize = 0;
	HBA_UINT8		    inq_status;
	uchar_t			    raw_luns[DEFAULT_LUN_LENGTH], *lun_string;
	HBA_UINT8		    rep_luns_status;
	rep_luns_rsp_t		    *lun_resp;
	uint64_t		    fcLUN;
	int			    lunNum, numberOfLun, lunCount, count;
	uint32_t		    lunlength, tmp_lunlength;

	responseSize = DEFAULT_LUN_LENGTH;
	senseSize = sizeof (struct scsi_extended_sense);
	memset(&sense, 0, sizeof (sense));
	status = HBA_ScsiReportLUNsV2(handle, hbaPortWWN,
	    scsiTargetWWN, (void *)raw_luns, &responseSize,
	    &rep_luns_status, (void *)&sense, &senseSize);
	/*
	 * if HBA_STATUS_ERROR_NOT_A_TARGET is return, we can assume this is
	 * a remote HBA and move on
	 */
	if (status == HBA_STATUS_ERROR_NOT_A_TARGET) {
		return;
	} else if (status != HBA_STATUS_OK) {
		fprintf(stderr, gettext("Error has occured. "
			    "HBA_ScsiReportLUNsV2 failed.  reason "));
		printStatus(status);
		fprintf(stderr, "\n");
		return;
	}
	lun_resp = (rep_luns_rsp_t *)raw_luns;
	memcpy(&tmp_lunlength, &(lun_resp->length), sizeof (tmp_lunlength));
	lunlength = htonl(tmp_lunlength);
	memcpy(&numberOfLun, &lunlength, sizeof (numberOfLun));
	for (lunCount = 0; lunCount < (numberOfLun / 8); lunCount++) {
		/*
		 * now issue standard inquiry to get Vendor
		 * and product information
		 */
		responseSize = sizeof (struct scsi_inquiry);
		senseSize = sizeof (struct scsi_extended_sense);
		memset(&inq, 0, sizeof (struct scsi_inquiry));
		memset(&sense, 0, sizeof (sense));
		fcLUN = ntohll(wwnConversion(lun_resp->lun[lunCount].val));
		status = HBA_ScsiInquiryV2(
			handle,
			hbaPortWWN,
			scsiTargetWWN,
			fcLUN,
			0, /* EVPD */
			0,
			&inq, &responseSize,
			&inq_status,
			&sense, &senseSize);
		if (status != HBA_STATUS_OK) {
		    fprintf(stderr, gettext("Not able to issue Inquiry.\n"));
		    printStatus(status);
		    fprintf(stderr, "\n");
		    strcpy(inq.inq_vid, "Unknown");
		    strcpy(inq.inq_pid, "Unknown");
		}
		if (map != NULL) {
			for (count = 0; count < map->NumberOfEntries; count++) {
			    if ((memcmp(map->entry[count].FcpId.PortWWN.wwn,
						    scsiTargetWWN.wwn,
						    sizeof (scsiTargetWWN.wwn))
					    == 0) &&
				    (memcmp(&(map->entry[count].FcpId.FcpLun),
					    &fcLUN, sizeof (fcLUN)) == 0)) {
				printLUNInfo(&inq,
				    map->entry[count].ScsiId.ScsiOSLun,
				    map->entry[count].ScsiId.OSDeviceName);
				    break;
			    }
			}
			if (count == map->NumberOfEntries) {
				lun_string = lun_resp->lun[lunCount].val;
				lunNum = ((lun_string[0] & 0x3F) << 8) |
				    lun_string[1];
				printLUNInfo(&inq, lunNum, "Unknown");
			}
		} else {
			/* Not able to get any target mapping information */
			lun_string = lun_resp->lun[lunCount].val;
			lunNum = ((lun_string[0] & 0x3F) << 8) |
			    lun_string[1];
			printLUNInfo(&inq, lunNum, "Unknown");
		}
	}
}

/*
 * function to handle the list remoteport command
 *
 * Arguments:
 *	wwnCount - the number of wwns in wwn_argv
 *	    if wwnCount == 0, then print information on all
 *		remote ports.  wwn_argv will not be used in this case
 *	    if wwnCount > 0, then print information for the WWNs
 *		given in wwn_argv
 *	wwn_argv - argument vector of WWNs
 *	options - any options specified by the caller
 *
 * returns:
 *	0	if successful
 *	1	otherwise
 */
int
fc_util_list_remoteport(int wwnCount, char **wwn_argv, cmdOptions_t *options)
{
	HBA_STATUS		status;
	HBA_FCPTARGETMAPPINGV2	*map = NULL;
	HBA_PORTATTRIBUTES	port;
	HBA_ADAPTERATTRIBUTES	attrs;
	HBA_HANDLE		handle;
	uint64_t		hbaPortWWN;
	HBA_WWN			myhbaPortWWN;
	int			processHBA_flags = 0, portCount = 0;
	int			mode;

	/* grab the hba port wwn from the -p option */
	for (; options->optval; options++) {
		if (options->optval == 'p') {
			sscanf(options->optarg, "%016llx",
			    &hbaPortWWN);
		} else if (options->optval == 's') {
			processHBA_flags |= PRINT_SCSI_TARGET;
		} else if (options->optval == 'l') {
			processHBA_flags |= PRINT_LINKSTAT;
		} else {
			fprintf(stderr, gettext("Error: Illegal option: %c.\n"),
			    options->optval);
			return (1);
		}
	}
	/*
	 * -h option was not specified, this should not happen either.
	 * cmdparse should catch this problem, but checking anyways
	 */
	if (hbaPortWWN == 0) {
		fprintf(stderr,
		    gettext("Error: -p option was not specified.\n"));
		return (1);
	}
	if ((status = HBA_LoadLibrary()) != HBA_STATUS_OK) {
		fprintf(stderr,
		    gettext("Failed to load FC-HBA common library\n"));
		printStatus(status);
		fprintf(stderr, "\n");
		return (1);
	}
	hbaPortWWN = htonll(hbaPortWWN);
	memcpy(myhbaPortWWN.wwn, &hbaPortWWN, sizeof (hbaPortWWN));
	if ((status = HBA_OpenAdapterByWWN(&handle, myhbaPortWWN))
	    != HBA_STATUS_OK) {
		status = Sun_HBA_OpenTgtAdapterByWWN(&handle, myhbaPortWWN);
		if (status != HBA_STATUS_OK) {
		    fprintf(stderr,
			gettext("Error: Failed to open adapter port. Reason "));
			printStatus(status);
		    fprintf(stderr, "\n");
		    HBA_FreeLibrary();
		    return (1);
		} else {
		    if ((processHBA_flags & PRINT_SCSI_TARGET) ==
			PRINT_SCSI_TARGET) {
			fprintf(stderr, gettext(
			    "Error: Unsupported option for target mode: %c.\n"),
			    's');
			HBA_FreeLibrary();
			return (1);
		    }
		    mode = TARGET_MODE;
		}
	} else {
	    mode = INITIATOR_MODE;
	}

	if ((processHBA_flags & PRINT_SCSI_TARGET) == PRINT_SCSI_TARGET) {
		getTargetMapping(handle, myhbaPortWWN, &map);
	}
	if (wwnCount == 0) {
		/* get adapater attributes for the given handle */
		memset(&attrs, 0, sizeof (attrs));
		memset(&port, 0, sizeof (port));
		if (retrieveAttrs(handle, myhbaPortWWN, &attrs, &port,
			    &portCount) != 0) {
			if (map != NULL) {
				free(map);
			}
			HBA_CloseAdapter(handle);
			HBA_FreeLibrary();
			return (1);
		}
		processHBA(handle, attrs, portCount, port, map, REMOTE_PORT,
		    processHBA_flags, mode);
	} else {
		processRemotePort(handle, myhbaPortWWN, map, wwnCount,
		    wwn_argv, processHBA_flags);
	}
	if (map != NULL) {
		free(map);
	}
	HBA_CloseAdapter(handle);
	HBA_FreeLibrary();
	return (0);
}

/*
 * process the hbaport object
 *
 * Arguments:
 *	wwnCount - count of the number of WWNs in wwn_argv
 *	    if wwnCount > 0, then we will only print information for
 *		the hba ports listed in wwn_argv
 *	    if wwnCount == 0, then we will print information on all hba ports
 *	wwn_argv - argument array of hba port WWNs
 *	options - any options specified by the caller
 *
 * returns:
 *	0	if successful
 *	1	otherwise
 */
int
fc_util_list_hbaport(int wwnCount, char **wwn_argv, cmdOptions_t *options)
{
	int	port_wwn_counter, numAdapters = 0, numTgtAdapters = 0, i;
	HBA_STATUS		status;
	char			adapterName[256];
	HBA_HANDLE		handle;
	uint64_t		hbaWWN;
	HBA_WWN			myWWN;
	int			processHBA_flags = 0;
	HBA_PORTATTRIBUTES	port;
	HBA_ADAPTERATTRIBUTES	attrs;
	int			portIndex = 0, err_cnt = 0;
	int			mode;

	/* process each of the options */
	for (; options->optval; options++) {
		if (options->optval == 'l') {
			processHBA_flags |= PRINT_LINKSTAT;
		} else if (options->optval == 'i') {
			processHBA_flags |= PRINT_INITIATOR;
		} else if (options->optval == 't') {
			processHBA_flags |= PRINT_TARGET;
		} else if (options->optval == 'e') {
			processHBA_flags |= PRINT_FCOE;
		}
	}

	/*
	 * Print both initiator and target if no initiator/target flag
	 * specified.
	 */
	if (((processHBA_flags & PRINT_INITIATOR) == 0) &&
	    ((processHBA_flags & PRINT_TARGET) == 0)) {
	    processHBA_flags |= PRINT_INITIATOR | PRINT_TARGET;
	}

	if ((status = HBA_LoadLibrary()) != HBA_STATUS_OK) {
		fprintf(stderr,
		    gettext("Failed to load FC-HBA common library\n"));
		printStatus(status);
		fprintf(stderr, "\n");
		return (1);
	}
	if (wwnCount > 0) {
		/* list only ports given in wwn_argv */
		for (port_wwn_counter = 0;
		    port_wwn_counter < wwnCount;
		    port_wwn_counter++) {
			sscanf(wwn_argv[port_wwn_counter], "%016llx", &hbaWWN);
			hbaWWN = htonll(hbaWWN);
			memcpy(myWWN.wwn, &hbaWWN, sizeof (hbaWWN));
			/* first check to see if it is an initiator port. */
			if ((processHBA_flags & PRINT_INITIATOR) ==
			    PRINT_INITIATOR) {
			    int times = 0;
			    status = HBA_OpenAdapterByWWN(&handle, myWWN);
			    while (status == HBA_STATUS_ERROR_TRY_AGAIN ||
				status == HBA_STATUS_ERROR_BUSY) {
				(void) sleep(1);
				status = HBA_OpenAdapterByWWN(&handle, myWWN);
				if (times++ > HBA_MAX_RETRIES) {
					break;
				}
			    }
			    if (status != HBA_STATUS_OK) {
				/* now see if it is a target mode FC port */
				if ((processHBA_flags & PRINT_TARGET) ==
				    PRINT_TARGET) {
				    status =
				    Sun_HBA_OpenTgtAdapterByWWN(&handle, myWWN);
				    if (status != HBA_STATUS_OK) {
					fprintf(stderr,
					    gettext(
					    "Error: HBA port %s: not found\n"),
					    wwn_argv[port_wwn_counter]);
					    err_cnt++;
					continue;
				    } else {
					/* set the port mode. */
					mode = TARGET_MODE;
				    }
				} else {
				    fprintf(stderr,
					gettext(
					    "Error: HBA port %s: not found\n"),
					    wwn_argv[port_wwn_counter]);
					    err_cnt++;
					continue;
				}
			    } else {
				/* set the port mode. */
				mode = INITIATOR_MODE;
			    }
			/* try target mode discovery if print target is set. */
			} else if ((processHBA_flags & PRINT_TARGET) ==
				PRINT_TARGET) {
			    status =
				Sun_HBA_OpenTgtAdapterByWWN(&handle, myWWN);
			    if (status != HBA_STATUS_OK) {
				fprintf(stderr, gettext(
				    "Error: HBA port %s: not found\n"),
				    wwn_argv[port_wwn_counter]);
				    err_cnt++;
				continue;
			    } else {
				/* set the port mode. */
				mode = TARGET_MODE;
			    }
			} else {
			    /* should not get here. */
			    fprintf(stderr, gettext(
				"Error: HBA port %s: not found\n"),
				wwn_argv[port_wwn_counter]);
			    err_cnt++;
			    continue;
			}
			memset(&attrs, 0, sizeof (attrs));
			memset(&port, 0, sizeof (port));
			if (retrieveAttrs(handle, myWWN, &attrs, &port,
				    &portIndex) != 0) {
				HBA_CloseAdapter(handle);
				continue;
			}
			processHBA(handle, attrs, portIndex, port, NULL,
			    HBA_PORT, processHBA_flags, mode);
			if ((processHBA_flags & PRINT_FCOE) != PRINT_FCOE &&
			    attrs.VendorSpecificID != 0xFC0E &&
			    printHBANPIVPortInfo(handle, portIndex) != 0) {
				err_cnt++;
			}
			HBA_CloseAdapter(handle);
		}
	} else {
		/*
		 * if PRINT_INITIATOR is specified, get the list of initiator
		 * mod port.
		 */
		if ((processHBA_flags & PRINT_INITIATOR) == PRINT_INITIATOR) {
		    numAdapters = HBA_GetNumberOfAdapters();
		    if ((numAdapters == 0) &&
			((processHBA_flags & ~PRINT_INITIATOR) == 0)) {
			fprintf(stdout, gettext("No Adapters Found.\n"));
		    }
		    for (i = 0; i < numAdapters; i++) {
			int times = 0;
			status = HBA_GetAdapterName(i, adapterName);
			if (status != HBA_STATUS_OK) {
				fprintf(stderr, gettext(
				    "failed to get adapter %d. Reason: "), i);
				printStatus(status);
				fprintf(stderr, "\n");
				continue;
			}
			if ((handle = HBA_OpenAdapter(adapterName)) == 0) {
				fprintf(stderr, gettext(
					    "Failed to open adapter %s.\n"),
				    adapterName);
				continue;
			}
			/* get adapater attributes for the given handle */
			memset(&attrs, 0, sizeof (attrs));
			status =
			    Sun_HBA_NPIVGetAdapterAttributes(handle,
			    &attrs);
			while ((status == HBA_STATUS_ERROR_TRY_AGAIN ||
			    status == HBA_STATUS_ERROR_BUSY) &&
			    times++ < HBA_MAX_RETRIES) {
				(void) sleep(1);
				status =
				    Sun_HBA_NPIVGetAdapterAttributes(handle,
				    &attrs);
				if (status == HBA_STATUS_OK) {
					break;
				}
			}
			if (status != HBA_STATUS_OK) {
				fprintf(stderr,
				    gettext("Failed to get adapter attributes "
					    "handle(%d) Reason: "), handle);
				printStatus(status);
				fprintf(stderr, "\n");
				HBA_CloseAdapter(handle);
				continue;
			}

			/* process each port on the given adatpter */
			for (portIndex = 0;
			    portIndex < attrs.NumberOfPorts;
			    portIndex++) {
				memset(&port, 0, sizeof (port));
				if ((status = HBA_GetAdapterPortAttributes(
						    handle, portIndex, &port))
				    != HBA_STATUS_OK) {
					/*
					 * not able to get port attributes.
					 * print out error * message and move
					 * on to the next port
					 */
					fprintf(stderr,
					    gettext("Error: Failed to get port "
						    "(%d) attributes reason: "),
					    portIndex);
					printStatus(status);
					fprintf(stderr, "\n");
					continue;
				}
				processHBA(handle, attrs, portIndex, port,
				    NULL, HBA_PORT, processHBA_flags,
				    INITIATOR_MODE);
				if ((processHBA_flags & PRINT_FCOE) !=
				    PRINT_FCOE &&
				    attrs.VendorSpecificID != 0xFC0E &&
				    printHBANPIVPortInfo(handle,
				    portIndex) != 0) {
					err_cnt++;
				}
			}
			HBA_CloseAdapter(handle);
		    }
		}

		/*
		 * Get the info on the target mode FC port if PRINT_TARGET
		 * is specified.
		 */
		if ((processHBA_flags & PRINT_TARGET) == PRINT_TARGET) {
		    numTgtAdapters = Sun_HBA_GetNumberOfTgtAdapters();
		    if (numTgtAdapters == 0 && numAdapters == 0) {
			fprintf(stdout,
			    gettext("No Adapters Found.\n"));
		    }
		    for (i = 0; i < numTgtAdapters; i++) {
			status = Sun_HBA_GetTgtAdapterName(i, adapterName);
			if (status != HBA_STATUS_OK) {
			    fprintf(stderr, gettext(
				"failed to get adapter %d. Reason: "), i);
			    printStatus(status);
			    fprintf(stderr, "\n");
			    continue;
			}
			if ((handle = Sun_HBA_OpenTgtAdapter(adapterName))
			    == 0) {
			    fprintf(stderr, gettext(
				"Failed to open adapter %s.\n"), adapterName);
			    continue;
			}
			/* get adapater attributes for the given handle */
			memset(&attrs, 0, sizeof (attrs));
			if ((status = HBA_GetAdapterAttributes(handle, &attrs))
			    != HBA_STATUS_OK) {
				fprintf(stderr,
				    gettext("Failed to get target mode adapter"
					"attributes handle(%d) Reason: "),
					handle);
				printStatus(status);
				fprintf(stderr, "\n");
				continue;
			}

			/* process each port on the given adatpter */
			for (portIndex = 0;
			    portIndex < attrs.NumberOfPorts;
			    portIndex++) {
				memset(&port, 0, sizeof (port));
				if ((status = HBA_GetAdapterPortAttributes(
						    handle, portIndex, &port))
				    != HBA_STATUS_OK) {
					/*
					 * not able to get port attributes.
					 * print out error * message and move
					 * on to the next port
					 */
					fprintf(stderr,
					    gettext("Error: Failed to get port "
						    "(%d) attributes reason: "),
					    portIndex);
					printStatus(status);
					fprintf(stderr, "\n");
					continue;
				}
				processHBA(handle, attrs, portIndex, port,
				    NULL, HBA_PORT, processHBA_flags,
				    TARGET_MODE);
			}
		    HBA_CloseAdapter(handle);
		}
	    }
	}

	HBA_FreeLibrary();

	/*
	 * print additional error msg for partial failure when more than
	 * one wwn is specified.
	 */
	if (err_cnt != 0) {
	    if (wwnCount > 1) {
		if (err_cnt == wwnCount) {
		    fprintf(stderr, gettext(
		    "Error: All specified HBA ports are not found\n"));
		} else {
		    fprintf(stderr, gettext(
		    "Error: Some of specified HBA ports are not found\n"));
		}
	    }
	    return (1);
	}

	return (0);
}

/*
 * Search the existing device list
 *
 * Take one of two actions:
 *
 * Add an entry if an entry doesn't exist
 * Add WWN data to it if an entry does exist
 *
 * Arguments:
 *	devList - OS device path list
 *	map - target mapping data
 *	index - index into target mapping data
 *	initiatorPortWWN - HBA port WWN
 *	verbose - boolean indicating whether to get additional data
 *
 * returns:
 *	none
 */
static void
searchDevice(discoveredDevice **devList, HBA_FCPSCSIENTRYV2 entry,
HBA_WWN initiatorPortWWN, HBA_HANDLE handle, boolean_t verbose)
{
	discoveredDevice *discoveredDevList, *newDevice;
	portWWNList *WWNList, *newWWN;
	tgtPortWWNList *newTgtWWN;
	boolean_t foundDevice = B_FALSE, foundWWN;
	struct scsi_inquiry	    inq;
	struct scsi_extended_sense  sense;
	HBA_UINT32		    responseSize, senseSize = 0;
	HBA_UINT8		    inq_status;
	HBA_STATUS		status;

	for (discoveredDevList = *devList; discoveredDevList != NULL;
	    discoveredDevList = discoveredDevList->next) {
		if (strcmp(entry.ScsiId.OSDeviceName,
		    discoveredDevList->OSDeviceName) == 0) {
			/*
			 * if only device names are requested,
			 * no reason to go any further
			 */
			if (verbose == B_FALSE) {
				return;
			}
			foundDevice = B_TRUE;
			break;
		}
	}
	if (foundDevice == B_TRUE) {
		/* add initiator Port WWN if it doesn't exist */
		for (WWNList = discoveredDevList->HBAPortWWN,
		    foundWWN = B_FALSE; WWNList != NULL;
		    WWNList = WWNList->next) {
			if (memcmp((void *)&(WWNList->portWWN),
			    (void *)&initiatorPortWWN,
			    sizeof (HBA_WWN)) == 0) {
				foundWWN = B_TRUE;
				break;
			}
		}
		if (discoveredDevList->inqSuccess == B_FALSE) {
			responseSize = sizeof (struct scsi_inquiry);
			senseSize = sizeof (struct scsi_extended_sense);
			memset(&inq, 0, sizeof (struct scsi_inquiry));
			memset(&sense, 0, sizeof (sense));
			status = HBA_ScsiInquiryV2(
			    handle,
			    initiatorPortWWN,
			    entry.FcpId.PortWWN,
			    entry.FcpId.FcpLun,
			    0, /* CDB Byte 1 */
			    0, /* CDB Byte 2 */
			    &inq, &responseSize,
			    &inq_status,
			    &sense, &senseSize);
			if (status == HBA_STATUS_OK) {
				memcpy(discoveredDevList->VID, inq.inq_vid,
				    sizeof (discoveredDevList->VID));
				memcpy(discoveredDevList->PID, inq.inq_pid,
				    sizeof (discoveredDevList->PID));
				discoveredDevList->dType = inq.inq_dtype;
				discoveredDevList->inqSuccess = B_TRUE;
			}
		}

		if (foundWWN == B_FALSE) {
			newWWN = (portWWNList *)calloc(1, sizeof (portWWNList));
			if (newWWN == NULL) {
				perror("Out of memory");
				exit(1);
			}

			/* insert at head */
			newWWN->next = discoveredDevList->HBAPortWWN;
			discoveredDevList->HBAPortWWN = newWWN;
			memcpy((void *)&(newWWN->portWWN),
			    (void *)&initiatorPortWWN,
			    sizeof (newWWN->portWWN));
			/* add Target Port */
			newWWN->tgtPortWWN = (tgtPortWWNList *)calloc(1,
			    sizeof (tgtPortWWNList));
			if (newWWN->tgtPortWWN == NULL) {
				perror("Out of memory");
				exit(1);
			}

			memcpy((void *)&(newWWN->tgtPortWWN->portWWN),
			    (void *)&(entry.FcpId.PortWWN),
			    sizeof (newWWN->tgtPortWWN->portWWN));
			/* Set LUN data */
			newWWN->tgtPortWWN->scsiOSLun = entry.ScsiId.ScsiOSLun;
		} else { /* add it to existing */
			newTgtWWN = (tgtPortWWNList *)calloc(1,
			    sizeof (tgtPortWWNList));
			if (newTgtWWN == NULL) {
				perror("Out of memory");
				exit(1);
			}
			/* insert at head */
			newTgtWWN->next = WWNList->tgtPortWWN;
			WWNList->tgtPortWWN = newTgtWWN;
			memcpy((void *)&(newTgtWWN->portWWN),
			    (void *)&(entry.FcpId.PortWWN),
			    sizeof (newTgtWWN->portWWN));
			/* Set LUN data */
			newTgtWWN->scsiOSLun = entry.ScsiId.ScsiOSLun;
		}
	} else { /* add new entry */
		newDevice = (discoveredDevice *)calloc(1,
		    sizeof (discoveredDevice));
		if (newDevice == NULL) {
			perror("Out of memory");
			exit(1);
		}
		newDevice->next = *devList; /* insert at head */
		*devList = newDevice; /* set new head */

		/* Copy device name */
		strncpy(newDevice->OSDeviceName, entry.ScsiId.OSDeviceName,
		    sizeof (newDevice->OSDeviceName) - 1);

		/*
		 * if only device names are requested,
		 * no reason to go any further
		 */
		if (verbose == B_FALSE) {
			return;
		}

		/*
		 * copy WWN data
		 */
		newDevice->HBAPortWWN = (portWWNList *)calloc(1,
		    sizeof (portWWNList));
		if (newDevice->HBAPortWWN == NULL) {
			perror("Out of memory");
			exit(1);
		}
		memcpy((void *)&(newDevice->HBAPortWWN->portWWN),
		    (void *)&initiatorPortWWN, sizeof (newWWN->portWWN));

		newDevice->HBAPortWWN->tgtPortWWN =
		    (tgtPortWWNList *)calloc(1, sizeof (tgtPortWWNList));
		if (newDevice->HBAPortWWN->tgtPortWWN == NULL) {
			perror("Out of memory");
			exit(1);
		}

		memcpy((void *)&(newDevice->HBAPortWWN->tgtPortWWN->portWWN),
		    (void *)&(entry.FcpId.PortWWN),
		    sizeof (newDevice->HBAPortWWN->tgtPortWWN->portWWN));

		/* Set LUN data */
		newDevice->HBAPortWWN->tgtPortWWN->scsiOSLun =
		    entry.ScsiId.ScsiOSLun;

		responseSize = sizeof (struct scsi_inquiry);
		senseSize = sizeof (struct scsi_extended_sense);
		memset(&inq, 0, sizeof (struct scsi_inquiry));
		memset(&sense, 0, sizeof (sense));
		status = HBA_ScsiInquiryV2(
		    handle,
		    initiatorPortWWN,
		    entry.FcpId.PortWWN,
		    entry.FcpId.FcpLun,
		    0, /* CDB Byte 1 */
		    0, /* CDB Byte 2 */
		    &inq, &responseSize,
		    &inq_status,
		    &sense, &senseSize);
		if (status != HBA_STATUS_OK) {
			/* initialize VID/PID/dType as "Unknown" */
			strcpy(newDevice->VID, "Unknown");
			strcpy(newDevice->PID, "Unknown");
			newDevice->dType = DTYPE_UNKNOWN;
			/* initialize inq status */
			newDevice->inqSuccess = B_FALSE;
		} else {
			memcpy(newDevice->VID, inq.inq_vid,
			    sizeof (newDevice->VID));
			memcpy(newDevice->PID, inq.inq_pid,
			    sizeof (newDevice->PID));
			newDevice->dType = inq.inq_dtype;
			/* initialize inq status */
			newDevice->inqSuccess = B_TRUE;
		}
	}
}


/*
 * process the logical-unit object
 *
 * Arguments:
 *	luCount - count of the number of device paths in paths_argv
 *	    if pathCount > 0, then we will only print information for
 *		the device paths listed in paths_argv
 *	    if pathCount == 0, then we will print information on all device
 *	        paths
 *	luArgv - argument array of device paths
 *	options - any options specified by the caller
 *
 * returns:
 *	0	if successful
 *	> 0	otherwise
 */
int
fc_util_list_logicalunit(int luCount, char **luArgv, cmdOptions_t *options)
{
	int			pathCtr, numAdapters, i, count;
	HBA_STATUS		status;
	char			adapterName[256];
	HBA_HANDLE		handle;
	HBA_PORTATTRIBUTES	port;
	HBA_ADAPTERATTRIBUTES	attrs;
	int			portIndex = 0;
	int			ret = 0;
	boolean_t		verbose = B_FALSE;
	HBA_FCPTARGETMAPPINGV2	*map = NULL;
	discoveredDevice	*devListWalk, *devList = NULL;
	boolean_t		pathFound;

	/* process each of the options */
	for (; options->optval; options++) {
		if (options->optval == 'v') {
			verbose = B_TRUE;
		}
	}

	if ((status = HBA_LoadLibrary()) != HBA_STATUS_OK) {
		fprintf(stderr,
		    gettext("Failed to load FC-HBA common library\n"));
		printStatus(status);
		fprintf(stderr, "\n");
		return (1);
	}
	/*
	 * Retrieve all device paths. We'll need to traverse the list
	 * until we find the input paths or all paths if none were given. We
	 * cannot print as we go since there can be duplicate paths returned
	 */
	numAdapters = HBA_GetNumberOfAdapters();
	if (numAdapters == 0) {
		return (0);
	}
	for (i = 0; i < numAdapters; i++) {
		int times;
		status = HBA_GetAdapterName(i, adapterName);
		if (status != HBA_STATUS_OK) {
			fprintf(stderr, gettext(
			    "Failed to get adapter %d. Reason: "), i);
			printStatus(status);
			fprintf(stderr, "\n");
			ret++;
			continue;
		}
		if ((handle = HBA_OpenAdapter(adapterName)) == 0) {
			fprintf(stderr, gettext("Failed to open adapter %s\n"),
			    adapterName);
			ret++;
			continue;
		}
		/* get adapter attributes for the given handle */
		memset(&attrs, 0, sizeof (attrs));
		times = 0;
		status = HBA_GetAdapterAttributes(handle, &attrs);
		while ((status == HBA_STATUS_ERROR_TRY_AGAIN ||
		    status == HBA_STATUS_ERROR_BUSY) &&
		    times++ < HBA_MAX_RETRIES) {
			(void) sleep(1);
			status = HBA_GetAdapterAttributes(handle, &attrs);
			if (status == HBA_STATUS_OK) {
				break;
			}
		}
		if (status != HBA_STATUS_OK) {
			fprintf(stderr,
			    gettext("Failed to get adapter attributes "
				    "handle(%d) Reason: "), handle);
			printStatus(status);
			fprintf(stderr, "\n");
			ret++;
			HBA_CloseAdapter(handle);
			continue;
		}

		/* process each port on adapter */
		for (portIndex = 0; portIndex < attrs.NumberOfPorts;
		    portIndex++) {
			memset(&port, 0, sizeof (port));
			if ((status = HBA_GetAdapterPortAttributes(handle,
			    portIndex, &port)) != HBA_STATUS_OK) {
				/*
				 * not able to get port attributes.
				 * print out error message and move
				 * on to the next port
				 */
				fprintf(stderr, gettext("Failed to get port "
				    "(%d) attributes reason: "),
				    portIndex);
				printStatus(status);
				fprintf(stderr, "\n");
				ret++;
				continue;
			}

			/* get OS Device Paths */
			getTargetMapping(handle, port.PortWWN, &map);
			if (map != NULL) {
				for (count = 0; count < map->NumberOfEntries;
				    count++) {
					searchDevice(&devList,
					    map->entry[count], port.PortWWN,
					    handle, verbose);
				}
			}
		}
		HBA_CloseAdapter(handle);
	}
	HBA_FreeLibrary();

	if (luCount == 0) {
		/* list all paths */
		for (devListWalk = devList; devListWalk != NULL;
		    devListWalk = devListWalk->next) {
			printOSDeviceNameInfo(devListWalk, verbose);
		}
	} else {
		/*
		 * list any paths not found first
		 * this gives the user cleaner output
		 */
		for (pathCtr = 0; pathCtr < luCount; pathCtr++) {
			for (devListWalk = devList, pathFound = B_FALSE;
			    devListWalk != NULL;
			    devListWalk = devListWalk->next) {
				if (strcmp(devListWalk->OSDeviceName,
				    luArgv[pathCtr]) == 0) {
					pathFound = B_TRUE;
				}
			}
			if (pathFound == B_FALSE) {
				fprintf(stderr, "%s: no such path\n",
				    luArgv[pathCtr]);
				ret++;
			}
		}
		/* list all paths requested in order requested */
		for (pathCtr = 0; pathCtr < luCount; pathCtr++) {
			for (devListWalk = devList; devListWalk != NULL;
			    devListWalk = devListWalk->next) {
				if (strcmp(devListWalk->OSDeviceName,
				    luArgv[pathCtr]) == 0) {
					printOSDeviceNameInfo(devListWalk,
					    verbose);
				}
			}
		}
	}
	return (ret);
}