summaryrefslogtreecommitdiff
path: root/usr/src/lib/hbaapi/common/HBAAPILIB-sun.c
blob: b0d4ca260b43ec33c40bb17f8af41f4be887ff7d (plain)
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
/*************************************************************************
 * Description
 *	HBAAPILIB-sun.c - Implements the Sun Extention for Target mode
 *		FCHBA discovery
 *
 * License:
 *	The contents of this file are subject to the SNIA Public License
 *	Version 1.0 (the "License"); you may not use this file except in
 *	compliance with the License. You may obtain a copy of the License at
 *
 *	http://www.snia.org/English/Resources/Code/OpenSource.html
 *
 *	Software distributed under the License is distributed on an "AS IS"
 *	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
 *	the License for the specific language governing rights and limitations
 *	under the License.
 *
 *************************************************************************
 */
/*
 * 	Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
 * 	Use is subject to license terms.
 */

#ifdef WIN32
#include <windows.h>
#include <string.h>
/*
 * Next define forces entry points in the dll to be exported
 * See hbaapi.h to see what it does.
 */
#define HBAAPI_EXPORTS
#else
#include <dlfcn.h>
#include <strings.h>
#endif
#include <stdio.h>
#include <time.h>
#include <dlfcn.h>
#include "hbaapi.h"
#include "hbaapi-sun.h"
#include "vendorhbaapi.h"
#include <stdlib.h>
#ifdef USESYSLOG
#include <syslog.h>
#endif


/*
 * LIBRARY_NUM is a shortcut to figure out which library we need to call.
 *  The top 16 bits of handle are the library index
 */
#define LIBRARY_NUM(handle)	((handle)>>16)

/*
 * VENDOR_HANDLE turns a global library handle into a vendor specific handle,
 * with all upper 16 bits set to 0
 */
#define VENDOR_HANDLE(handle)	((handle)&0xFFFF)

#define HBA_HANDLE_FROM_LOCAL(library, vendor) \
				(((library)<<16) | ((vendor)&0x0000FFFF))

extern int _hbaapi_debuglevel;
#define DEBUG(L, STR, A1, A2, A3)

#if defined(USESYSLOG) && defined(USELOGFILE)
extern FILE *_hbaapi_debug_fd;
extern int _hbaapi_sysloginit;
#undef DEBUG
#ifdef WIN32
#define DEBUG(L, STR, A1, A2, A3)\
    if ((L) <= _hbaapi_debuglevel) {\
	if(_hbaapi_sysloginit == 0) {\
	    openlog("HBAAPI", LOG_PID|LOG_ODELAY ,LOG_USER);\
	    _hbaapi_sysloginit = 1;\
	}\
	syslog (LOG_INFO, (STR), (A1), (A2), (A3));\
	if(_hbaapi_debug_fd == NULL) {\
	    char _logFile[MAX_PATH]; \
	    GetTempPath(MAX_PATH, _logFile); \
	    strcat(_logFile, "HBAAPI.log"); \
	    _hbaapi_debug_fd = fopen(_logFile, "a");\
	}\
        if(_hbaapi_debug_fd != NULL) {\
	    fprintf(_hbaapi_debug_fd, (STR ## "\n"), (A1), (A2), (A3));\
	}\
    }
#else /* WIN32*/
#define DEBUG(L, STR, A1, A2, A3)\
    if ((L) <= _hbaapi_debuglevel) {\
	if(_hbaapi_sysloginit == 0) {\
	    openlog("HBAAPI", LOG_PID|LOG_ODELAY ,LOG_USER);\
	    _hbaapi_sysloginit = 1;\
	}\
	syslog (LOG_INFO, (STR), (A1), (A2), (A3));\
	if(_hbaapi_debug_fd == NULL) {\
	    _hbaapi_debug_fd = fopen("/tmp/HBAAPI.log", "a");\
	}\
        if(_hbaapi_debug_fd != NULL) {\
	    fprintf(_hbaapi_debug_fd, (STR ## "\n"), (A1), (A2), (A3));\
	}\
    }
#endif /* WIN32*/

#else /* Not both USESYSLOG and USELOGFILE */
#if defined(USESYSLOG)
int _hbaapi_sysloginit = 0;
#undef DEBUG
#define DEBUG(L, STR, A1, A2, A3) \
    if ((L) <= _hbaapi_debuglevel) {\
	if(_hbaapi_sysloginit == 0) {\
	    openlog("HBAAPI", LOG_PID|LOG_ODELAY ,LOG_USER);\
	    _hbaapi_sysloginit = 1;\
	}\
	syslog (LOG_INFO, (STR), (A1), (A2), (A3));\
    }
#endif /* USESYSLOG */
#if defined(USELOGFILE)
FILE *_hbaapi_debug_fd = NULL;
#undef DEBUG
#ifdef WIN32
#define DEBUG(L, STR, A1, A2, A3) \
    if((L) <= _hbaapi_debuglevel) {\
	if(_hbaapi_debug_fd == NULL) {\
	    char _logFile[MAX_PATH]; \
	    GetTempPath(MAX_PATH, _logFile); \
	    strcat(_logFile, "HBAAPI.log"); \
	    _hbaapi_debug_fd = fopen(_logFile, "a");\
        }\
    }
#else /* WIN32 */
#define DEBUG(L, STR, A1, A2, A3) \
    if((L) <= _hbaapi_debuglevel) {\
	if(_hbaapi_debug_fd == NULL) {\
	    _hbaapi_debug_fd = fopen("/tmp/HBAAPI.log", "a");\
	}\
	if(_hbaapi_debug_fd != NULL) { \
	    fprintf(_hbaapi_debug_fd, (STR) ## "\n", (A1), (A2), (A3));\
	}\
    }
#endif /* WIN32 */
#endif /* USELOGFILE */
#endif /* Not both USELOGFILE and USESYSLOG */

#ifdef POSIX_THREADS
#include <pthread.h>
/*
 * When multiple mutex's are grabed, they must be always be grabbed in
 * the same order, or deadlock can result.  There are three levels
 * of mutex's involved in this API.  If LL_mutex is grabbed, always grap
 * it first.  If AL_mutex is grabbed, it may not be grabbed before
 * LL_mutex.  If grabbed in a multi grab sequence, the mutex's protecting
 * the callback lists must always be grabbed last and release before calling
 * a vendor specific library function that might invoke a callback function
 * on the same thread.
 */
#define GRAB_MUTEX(M)			grab_mutex(M)
#define RELEASE_MUTEX(M)		release_mutex(M)
#define RELEASE_MUTEX_RETURN(M,RET)	release_mutex(M); return(RET)
#elif defined (WIN32)
#define GRAB_MUTEX(m)			EnterCriticalSection(m)
#define RELEASE_MUTEX(m)		LeaveCriticalSection(m)
#define RELEASE_MUTEX_RETURN(m, RET)	LeaveCriticalSection(m); return(RET)
#else
#define GRAB_MUTEX(M)
#define RELEASE_MUTEX(M)
#define RELEASE_MUTEX_RETURN(M,RET)	return(RET)
#endif

/*
 * HBA_LIBRARY_STATUS and HBA_LIBRARY_INFO are redefined here.
 * Avoid any change in the common code.
 */
typedef enum {
    HBA_LIBRARY_UNKNOWN,
    HBA_LIBRARY_LOADED,
    HBA_LIBRARY_NOT_LOADED
} HBA_LIBRARY_STATUS;

typedef struct hba_library_info {
    struct hba_library_info
			*next;
#ifdef WIN32
    HINSTANCE		hLibrary;		/* Handle to a loaded DLL */
#else
    char		*LibraryName;
    void*		hLibrary;		/* Handle to a loaded DLL */
#endif
    char		*LibraryPath;
    HBA_ENTRYPOINTSV2	functionTable;		/* Function pointers */
    HBA_LIBRARY_STATUS	status;			/* info on this library */
    HBA_UINT32		index;
} HBA_LIBRARY_INFO, *PHBA_LIBRARY_INFO;

#define ARE_WE_INITED() \
	if (_hbaapi_librarylist == NULL) { \
		return(HBA_STATUS_ERROR); \
	}

extern HBA_LIBRARY_INFO *_hbaapi_librarylist;
extern HBA_UINT32 _hbaapi_total_library_count;
#ifdef POSIX_THREADS
extern pthread_mutex_t _hbaapi_LL_mutex;
#elif defined(WIN32)
extern CRITICAL_SECTION _hbaapi_LL_mutex;
#endif

/*
 * Function type def fop Sun extentions.
 */
typedef HBA_UINT32	(* Sun_HBAGetNumberOfTgtAdaptersFunc)();
typedef HBA_STATUS	(* Sun_HBAGetTgtAdapterNameFunc)(HBA_UINT32, char *);
typedef HBA_HANDLE	(* Sun_HBAOpenTgtAdapterFunc)(char *);
typedef HBA_STATUS	(* Sun_HBAOpenTgtAdapterByWWNFunc)
			    (HBA_HANDLE *, HBA_WWN);
typedef	HBA_STATUS	(* Sun_HBANPIVGetAdapterAttributesFunc)
			    (HBA_HANDLE, HBA_ADAPTERATTRIBUTES *);
typedef	HBA_STATUS	(* Sun_HBAGetNPIVPortInfoFunc)
			    (HBA_HANDLE, HBA_UINT32, HBA_UINT32, HBA_NPIVATTRIBUTES *);
typedef HBA_STATUS	(* Sun_HBADeleteNPIVPortFunc)
			    (HBA_HANDLE, HBA_UINT32, HBA_WWN);
typedef HBA_STATUS	(* Sun_HBACreateNPIVPortFunc)
			    (HBA_HANDLE, HBA_UINT32, HBA_WWN, HBA_WWN, HBA_UINT32 *);
typedef	HBA_STATUS	(* Sun_HBAAdapterReturnWWNFunc)
			    (HBA_HANDLE, HBA_UINT32, HBA_WWN *, HBA_WWN *);
typedef	HBA_STATUS	(* Sun_HBAAdapterCreateWWNFunc)
			    (HBA_HANDLE, HBA_UINT32, HBA_WWN *, HBA_WWN *, HBA_WWN *,
			    HBA_INT32);
typedef	HBA_STATUS	(* Sun_HBAGetPortNPIVAttributesFunc)
			    (HBA_HANDLE, HBA_UINT32, HBA_PORTNPIVATTRIBUTES *);
typedef	HBA_STATUS	(* Sun_HBARegisterForAdapterDeviceEventsFunc)
			    (void (*)(void *, HBA_WWN, HBA_UINT32, HBA_UINT32),
			    void *, HBA_HANDLE, HBA_WWN, HBA_CALLBACKHANDLE *);
typedef	HBA_STATUS	(* Sun_HBADoForceLipFunc)(HBA_HANDLE, int *);

/*
 * Individual adapter (hba) information
 * Same as hbaadapter with different structure name.
 */
typedef struct hba_tgtadapter_info {
    struct hba_tgtadapter_info
			*next;
    HBA_STATUS		GNstatus; /* status from GetTgtAdapterNameFunc */
    char		*name;
    HBA_WWN		nodeWWN;
    HBA_LIBRARY_INFO	*library;
    HBA_UINT32		index;
} HBA_TGTADAPTER_INFO;

/*
 * Make the list as an array with max size 16
 */
HBA_TGTADAPTER_INFO *_hbaapi_tgtadapterlist;
HBA_UINT32 _hbaapi_total_tgtadapter_count = 0;
#ifdef POSIX_THREADS
pthread_mutex_t _hbaapi_tgtAL_mutex = PTHREAD_MUTEX_INITIALIZER;
#elif defined(WIN32)
CRITICAL_SECTION _hbaapi_tgtAL_mutex;
#endif

/*
 * Common library internal. Mutex handling
 */
#ifdef POSIX_THREADS
static void
grab_mutex(pthread_mutex_t *mp) {
    int ret;
    if((ret = pthread_mutex_lock(mp)) != 0) {
	perror("pthread_mutex_lock - HBAAPI:");
	DEBUG(0, "pthread_mutex_lock returned %d", ret, 0, 0);
    }
}

static void
release_mutex(pthread_mutex_t *mp) {
    int ret;
    if((ret = pthread_mutex_unlock(mp)) != 0) {
	perror("pthread_mutex_unlock - HBAAPI:");
	DEBUG(0, "pthread_mutex_unlock returned %d", ret, 0, 0);
    }
}
#endif

/*
 * The API used to use fixed size tables as its primary data structure.
 * Indexing from 1 to N identified each adapters.  Now the adapters are
 * on a linked list.  There is a unique "index" foreach each adapter.
 * Adapters always keep their index, even if they are removed from the
 * hardware.  The only time the indexing is reset is on HBA_FreeLibrary
 */
HBA_UINT32
Sun_HBA_GetNumberOfTgtAdapters()
{
    int j=0;
    HBA_LIBRARY_INFO	*lib_infop;
    Sun_HBAGetNumberOfTgtAdaptersFunc
			GetNumberOfTgtAdaptersFunc = NULL;
    Sun_HBAGetTgtAdapterNameFunc
			GetTgtAdapterNameFunc = NULL;
    HBA_BOOLEAN		found_name;
    HBA_TGTADAPTER_INFO	*adapt_infop;
    HBA_STATUS		status;

    char adaptername[256];
    int num_adapters; /* local */

    if(_hbaapi_librarylist == NULL) {
	return (0);
    }
    GRAB_MUTEX(&_hbaapi_LL_mutex); /* pay attention to order */
    GRAB_MUTEX(&_hbaapi_tgtAL_mutex);

    for (lib_infop = _hbaapi_librarylist;
	 lib_infop != NULL;
	 lib_infop = lib_infop->next) {

	if (lib_infop->status != HBA_LIBRARY_LOADED) {
	    continue;
	}

	if (lib_infop->hLibrary != NULL) {
            GetNumberOfTgtAdaptersFunc = (Sun_HBAGetNumberOfTgtAdaptersFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcGetNumberOfTgtAdapters");
            GetTgtAdapterNameFunc = (Sun_HBAGetTgtAdapterNameFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcGetTgtAdapterName");
	    if (GetNumberOfTgtAdaptersFunc == NULL ||
		GetTgtAdapterNameFunc == NULL)	{
		GetNumberOfTgtAdaptersFunc = GetTgtAdapterNameFunc = NULL;
                continue;
            }
	} else {
	    continue;
	}

	num_adapters = ((GetNumberOfTgtAdaptersFunc)());
#ifndef WIN32
	DEBUG(1, "HBAAPI: number of target mode adapters for %s = %d\n",
	      lib_infop->LibraryName, num_adapters, 0);
#else
	DEBUG(1, "HBAAPI: number of target mode_adapters for %s = %d\n",
	      lib_infop->LibraryPath, num_adapters, 0);
#endif

	for (j = 0; j < num_adapters; j++) {
	    found_name = 0;
	    status = (GetTgtAdapterNameFunc)(j, (char *)&adaptername);
	    if(status == HBA_STATUS_OK) {
		for(adapt_infop = _hbaapi_tgtadapterlist;
		    adapt_infop != NULL;
		    adapt_infop = adapt_infop->next) {
		    /*
		     * check for duplicates, really, this may just be a second
		     * call to this function
		     * ??? how do we know when a name becomes stale?
		     */
		    if(strcmp(adaptername, adapt_infop->name) == 0) {
			/* already got this one */
			found_name++;
			break;
		    }
		}
		if(found_name != 0) {
		    continue;
		}
	    }

	    adapt_infop = (HBA_TGTADAPTER_INFO *)
		calloc(1, sizeof(HBA_TGTADAPTER_INFO));
	    if(adapt_infop == NULL) {
#ifndef WIN32
		fprintf(stderr,
			"HBA_GetNumberOfAdapters: calloc failed on sizeof:%d\n",
			sizeof(HBA_TGTADAPTER_INFO));
#endif
		RELEASE_MUTEX(&_hbaapi_tgtAL_mutex);
		RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex,
				     _hbaapi_total_tgtadapter_count);
	    }
	    if((adapt_infop->GNstatus = status) == HBA_STATUS_OK) {
		adapt_infop->name = strdup(adaptername);
	    } else {
		char dummyname[512];
		sprintf(dummyname, "NULLADAPTER-%s-%03d",
			lib_infop->LibraryPath, _hbaapi_total_tgtadapter_count);
		dummyname[255] = '\0';
		adapt_infop->name = strdup(dummyname);
	    }
	    adapt_infop->library = lib_infop;
	    adapt_infop->next = _hbaapi_tgtadapterlist;
	    adapt_infop->index = _hbaapi_total_tgtadapter_count;
	    _hbaapi_tgtadapterlist = adapt_infop;
	    _hbaapi_total_tgtadapter_count++;
	}
	GetNumberOfTgtAdaptersFunc = GetTgtAdapterNameFunc = NULL;
    }
    RELEASE_MUTEX(&_hbaapi_tgtAL_mutex);
    RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, _hbaapi_total_tgtadapter_count);
}

HBA_STATUS
Sun_HBA_GetTgtAdapterName(
    HBA_UINT32 adapterindex,
    char *adaptername)
{
    HBA_TGTADAPTER_INFO	*adapt_infop;
    HBA_STATUS		ret = HBA_STATUS_ERROR_ILLEGAL_INDEX;

    if (adaptername == NULL) {
	    return(HBA_STATUS_ERROR_ARG);
    }
    /*
     * The adapter index is from old code, but we have
     * to support it.  Go down the list looking for
     * the adapter
     */
    ARE_WE_INITED();
    GRAB_MUTEX(&_hbaapi_tgtAL_mutex);
    *adaptername = '\0';
    for(adapt_infop = _hbaapi_tgtadapterlist;
	adapt_infop != NULL;
	adapt_infop = adapt_infop->next) {

	if(adapt_infop->index == adapterindex) {
	    if(adapt_infop->name != NULL &&
	       adapt_infop->GNstatus == HBA_STATUS_OK) {
		strcpy(adaptername, adapt_infop->name);
	    } else {
		*adaptername = '\0';
	    }
	    ret = adapt_infop->GNstatus;
	    break;
	}
    }
    DEBUG(2, "GetAdapterName for index:%d ->%s", adapterindex, adaptername, 0);
    RELEASE_MUTEX_RETURN(&_hbaapi_AL_mutex, ret);
}

HBA_HANDLE
Sun_HBA_OpenTgtAdapter(char* adaptername)
{
    HBA_HANDLE		handle;
    Sun_HBAOpenTgtAdapterFunc	OpenTgtAdapterFunc;
    HBA_TGTADAPTER_INFO	*adapt_infop;
    HBA_LIBRARY_INFO	*lib_infop;

    DEBUG(2, "OpenAdapter: %s", adaptername, 0, 0);

    if(_hbaapi_librarylist == NULL) {
	return(HBA_HANDLE_INVALID);
    }
    if (adaptername == NULL) {
	return(HBA_STATUS_ERROR_ARG);
    }
    handle = HBA_HANDLE_INVALID;
    GRAB_MUTEX(&_hbaapi_AL_mutex);
    for(adapt_infop = _hbaapi_tgtadapterlist;
	adapt_infop != NULL;
	adapt_infop = adapt_infop->next) {
	if (strcmp(adaptername, adapt_infop->name) != 0) {
	    continue;
	}
	lib_infop = adapt_infop->library;
        OpenTgtAdapterFunc = (Sun_HBAOpenTgtAdapterFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcOpenTgtAdapter");
	if (OpenTgtAdapterFunc != NULL) {
	    /* retrieve the vendor handle */
	    handle = (OpenTgtAdapterFunc)(adaptername);
	    if(handle != 0) {
		/* or this with the library index to get the common handle */
		handle = HBA_HANDLE_FROM_LOCAL(lib_infop->index, handle);
	    }
	}
	break;
    }
    RELEASE_MUTEX_RETURN(&_hbaapi_AL_mutex, handle);
}

/*
 * This function ignores the list of known adapters and instead tries
 * each vendors open function to see if one of them
 * can open an adapter when referenced with a particular WWN
 */
HBA_STATUS
Sun_HBA_OpenTgtAdapterByWWN(HBA_HANDLE *phandle, HBA_WWN nodeWWN)
{
    HBA_HANDLE		handle;
    HBA_LIBRARY_INFO	*lib_infop;
    Sun_HBAGetNumberOfTgtAdaptersFunc
			GetNumberOfTgtAdaptersFunc;
    Sun_HBAOpenTgtAdapterByWWNFunc
			OpenTgtAdapterByWWNFunc;
    HBA_STATUS		status;

    DEBUG(2, "OpenAdapterByWWN: %s", WWN2STR1(&nodeWWN), 0, 0);

    if (phandle == NULL) {
	    return(HBA_STATUS_ERROR_ARG);
    }

    ARE_WE_INITED();

    *phandle = HBA_HANDLE_INVALID;

    GRAB_MUTEX(&_hbaapi_LL_mutex);
    for (lib_infop = _hbaapi_librarylist;
	 lib_infop != NULL;
	 lib_infop = lib_infop->next) {

	status = HBA_STATUS_ERROR_ILLEGAL_WWN;

	if (lib_infop->status != HBA_LIBRARY_LOADED) {
	    continue;
	}

        GetNumberOfTgtAdaptersFunc = (Sun_HBAGetNumberOfTgtAdaptersFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcGetNumberOfTgtAdapters");
        OpenTgtAdapterByWWNFunc = (Sun_HBAOpenTgtAdapterByWWNFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcOpenTgtAdapterByWWN");
	if (GetNumberOfTgtAdaptersFunc == NULL ||
		OpenTgtAdapterByWWNFunc == NULL) {
		GetNumberOfTgtAdaptersFunc = OpenTgtAdapterByWWNFunc = NULL;
                continue;
        }

	(void) ((GetNumberOfTgtAdaptersFunc)());

	if((status = (OpenTgtAdapterByWWNFunc)(&handle, nodeWWN))
	    != HBA_STATUS_OK) {
	    GetNumberOfTgtAdaptersFunc = OpenTgtAdapterByWWNFunc = NULL;
	    continue;
	}
	/* OK, make a vendor non-specific handle */
	*phandle = HBA_HANDLE_FROM_LOCAL(lib_infop->index, handle);
	status = HBA_STATUS_OK;
	break;

	GetNumberOfTgtAdaptersFunc = OpenTgtAdapterByWWNFunc = NULL;
    }
    RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
}

static HBA_STATUS
HBA_NPIV_CheckLibrary(HBA_HANDLE handle,
			HBA_LIBRARY_INFO **lib_infopp,
			HBA_HANDLE *vendorhandle) {
	HBA_UINT32		libraryIndex;
	HBA_LIBRARY_INFO	*lib_infop;

	if (vendorhandle == NULL) {
		return(HBA_STATUS_ERROR_ARG);
	}
	if(_hbaapi_librarylist == NULL) {
		return(HBA_STATUS_ERROR);
	}
	libraryIndex = LIBRARY_NUM(handle);

	GRAB_MUTEX(&_hbaapi_LL_mutex);
	for(lib_infop = _hbaapi_librarylist;
	    lib_infop != NULL;
	    lib_infop = lib_infop->next) {
		if(lib_infop->index == libraryIndex) {
			if(lib_infop->status != HBA_LIBRARY_LOADED) {
				return HBA_STATUS_ERROR;
			}
			*lib_infopp = lib_infop;
			*vendorhandle = VENDOR_HANDLE(handle);
			/* caller will release the mutex */
			return HBA_STATUS_OK;
		}
	}
	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, HBA_STATUS_ERROR_INVALID_HANDLE);
}
#define	NPIVCHECKLIBRARY() \
	status = HBA_NPIV_CheckLibrary(handle, &lib_infop, &vendorHandle); \
	if(status != HBA_STATUS_OK) { \
		return(status); \
	}

HBA_STATUS
Sun_HBA_NPIVGetAdapterAttributes (
    HBA_HANDLE		handle,
    HBA_ADAPTERATTRIBUTES
			*hbaattributes)
{
	HBA_STATUS		status;
	HBA_LIBRARY_INFO	*lib_infop;
	HBA_HANDLE		vendorHandle;
	Sun_HBANPIVGetAdapterAttributesFunc	NPIVGetAdapterAttributesFunc;

	DEBUG(2, "HBA_NPIVGetAdapterAttributes", 0, 0, 0);

	NPIVCHECKLIBRARY();
	NPIVGetAdapterAttributesFunc = (Sun_HBANPIVGetAdapterAttributesFunc)
	    dlsym(lib_infop->hLibrary, "Sun_fcNPIVGetAdapterAttributes");
	if (NPIVGetAdapterAttributesFunc != NULL) {
		status = ((NPIVGetAdapterAttributesFunc)(vendorHandle,
			hbaattributes));
	} else {
		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
	}
	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
}

HBA_STATUS
Sun_HBA_GetNPIVPortInfo (
    HBA_HANDLE		handle,
    HBA_UINT32		portindex,
    HBA_UINT32		vportindex,
    HBA_NPIVATTRIBUTES	*attributes)
{
	HBA_STATUS		status;
	HBA_LIBRARY_INFO	*lib_infop;
	HBA_HANDLE		vendorHandle;
	Sun_HBAGetNPIVPortInfoFunc	GetNPIVPortInfoFunc;

	NPIVCHECKLIBRARY();
	GetNPIVPortInfoFunc = (Sun_HBAGetNPIVPortInfoFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcGetNPIVPortInfo");
	if (GetNPIVPortInfoFunc != NULL) {
		status = ((GetNPIVPortInfoFunc)(vendorHandle, portindex,
			vportindex, attributes));
	} else {
		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
	}
	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
}

HBA_STATUS
Sun_HBA_DeleteNPIVPort (
    HBA_HANDLE		handle,
    HBA_UINT32		portindex,
    HBA_WWN		vportWWN)
{
	HBA_STATUS		status;
	HBA_LIBRARY_INFO	*lib_infop;
	HBA_HANDLE		vendorHandle;
	Sun_HBADeleteNPIVPortFunc	DeleteNPIVPortFunc;

	NPIVCHECKLIBRARY();
	DeleteNPIVPortFunc = (Sun_HBADeleteNPIVPortFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcDeleteNPIVPort");
	if (DeleteNPIVPortFunc != NULL) {
		status = ((DeleteNPIVPortFunc)(vendorHandle,
		    portindex, vportWWN));
	} else {
		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
	}
	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
}

HBA_STATUS
Sun_HBA_CreateNPIVPort (
    HBA_HANDLE		handle,
    HBA_UINT32		portindex,
    HBA_WWN		vnodeWWN,
    HBA_WWN		vportWWN,
    HBA_UINT32		*vportindex)
{
	HBA_STATUS		status;
	HBA_LIBRARY_INFO	*lib_infop;
	HBA_HANDLE		vendorHandle;
	Sun_HBACreateNPIVPortFunc	CreateNPIVPortFunc;

	NPIVCHECKLIBRARY();
	CreateNPIVPortFunc = (Sun_HBACreateNPIVPortFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcCreateNPIVPort");
	if (CreateNPIVPortFunc != NULL) {
		status = ((CreateNPIVPortFunc)(vendorHandle,
		    portindex, vnodeWWN, vportWWN, vportindex));
	} else {
		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
	}
	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
}

HBA_STATUS
Sun_HBA_GetPortNPIVAttributes (
    HBA_HANDLE		handle,
    HBA_UINT32		portindex,
    HBA_PORTNPIVATTRIBUTES	*portnpivattributes)
{
	HBA_STATUS		status;
	HBA_LIBRARY_INFO	*lib_infop;
	HBA_HANDLE		vendorHandle;
	Sun_HBAGetPortNPIVAttributesFunc	GetPortNPIVAttributesFunc;

	NPIVCHECKLIBRARY();
	GetPortNPIVAttributesFunc = (Sun_HBAGetPortNPIVAttributesFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcGetPortNPIVAttributes");
	if (GetPortNPIVAttributesFunc != NULL) {
		status = ((GetPortNPIVAttributesFunc)(
		    vendorHandle, portindex, portnpivattributes));
	} else {
		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
	}
	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
}

HBA_STATUS
Sun_HBA_AdapterCreateWWN (
    HBA_HANDLE		handle,
    HBA_UINT32		portindex,
    HBA_WWN		*nwwn,
    HBA_WWN		*pwwn,
    HBA_WWN		*OUI,
    HBA_INT32		method)
{
	HBA_STATUS		status;
	HBA_LIBRARY_INFO	*lib_infop;
	HBA_HANDLE		vendorHandle;
	Sun_HBAAdapterCreateWWNFunc	AdapterCreateWWNFunc;

	NPIVCHECKLIBRARY();
	AdapterCreateWWNFunc = (Sun_HBAAdapterCreateWWNFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcAdapterCreateWWN");
	if (AdapterCreateWWNFunc != NULL) {
		status = ((AdapterCreateWWNFunc)(vendorHandle,
		    portindex, nwwn, pwwn, OUI, method));
	} else {
		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
	}
	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
}

HBA_STATUS
Sun_HBA_AdapterReturnWWN (
    HBA_HANDLE		handle,
    HBA_UINT32		portindex,
    HBA_WWN		*nwwn,
    HBA_WWN		*pwwn)
{
	HBA_STATUS		status;
	HBA_LIBRARY_INFO	*lib_infop;
	HBA_HANDLE		vendorHandle;
	Sun_HBAAdapterReturnWWNFunc	AdapterReturnWWNFunc;

	NPIVCHECKLIBRARY();
	AdapterReturnWWNFunc = (Sun_HBAAdapterReturnWWNFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcAdapterReturnWWN");
	if (AdapterReturnWWNFunc != NULL) {
		status = ((AdapterReturnWWNFunc)(vendorHandle,
		    portindex, nwwn, pwwn));
	} else {
		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
	}
	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
}

typedef struct hba_npivadaptercallback_elem {
    struct hba_npivadaptercallback_elem
			*next;
    HBA_LIBRARY_INFO	*lib_info;
    void		*userdata;
    HBA_CALLBACKHANDLE	vendorcbhandle;
    void		(*callback)();
} HBA_NPIVADAPTERCALLBACK_ELEM;
extern HBA_NPIVADAPTERCALLBACK_ELEM *_hbaapi_adapterdeviceevents_callback_list;

/* Adapter Device Events ********************************************************/
static void
adapterdeviceevents_callback (void *data,
    HBA_WWN	PortWWN,
    HBA_UINT32	eventType,
    HBA_UINT32	fabricPortID)
{
	HBA_NPIVADAPTERCALLBACK_ELEM	*acbp;

	DEBUG(3, "AdapterDeviceEvent, port:%s, eventType:%d fabricPortID:0X%06x",
	    WWN2STR1(&PortWWN), eventType, fabricPortID);

	GRAB_MUTEX(&_hbaapi_APE_mutex);

	for(acbp = _hbaapi_adapterdeviceevents_callback_list;
	    acbp != NULL;
	    acbp = acbp->next) {
		if(data == (void *)acbp) {
			(*acbp->callback)(acbp->userdata, PortWWN, eventType, fabricPortID);
			break;
		}
	}
	RELEASE_MUTEX(&_hbaapi_APE_mutex);
}

HBA_STATUS
Sun_HBA_RegisterForAdapterDeviceEvents (
    void	(*callback) (
	void		*data,
	HBA_WWN		PortWWN,
	HBA_UINT32	eventType,
	HBA_UINT32	fabricPortID
	),
    void		*userData,
    HBA_HANDLE		handle,
    HBA_WWN		PortWWN,
    HBA_CALLBACKHANDLE	*callbackHandle)
{
	HBA_NPIVADAPTERCALLBACK_ELEM	*acbp;
	HBA_STATUS			status;
	HBA_LIBRARY_INFO		*lib_infop;
	HBA_HANDLE			vendorHandle;
	Sun_HBARegisterForAdapterDeviceEventsFunc
					registeredfunc;

	if (callbackHandle == NULL) {
		return(HBA_STATUS_ERROR_ARG);
	}

        NPIVCHECKLIBRARY();
	registeredfunc = (Sun_HBARegisterForAdapterDeviceEventsFunc)
                dlsym(lib_infop->hLibrary,
		    "Sun_fcRegisterForAdapterDeviceEvents");
	if (registeredfunc == NULL) {
		RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, HBA_STATUS_ERROR_NOT_SUPPORTED);
	}

	acbp = (HBA_NPIVADAPTERCALLBACK_ELEM *)
		calloc(1, sizeof(HBA_NPIVADAPTERCALLBACK_ELEM));

	if(acbp == NULL) {
		RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, HBA_STATUS_ERROR);
	}

	*callbackHandle = (HBA_CALLBACKHANDLE) acbp;
	acbp->callback = callback;
	acbp->userdata = userData;
	acbp->lib_info = lib_infop;

	status = (registeredfunc)(adapterdeviceevents_callback,
		(void *)acbp,
		vendorHandle,
		PortWWN,
		&acbp->vendorcbhandle);
	if(status != HBA_STATUS_OK) {
		free(acbp);
		RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
	}

	GRAB_MUTEX(&_hbaapi_APE_mutex);
	acbp->next = _hbaapi_adapterdeviceevents_callback_list;
	_hbaapi_adapterdeviceevents_callback_list = acbp;
	RELEASE_MUTEX(&_hbaapi_APE_mutex);

	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, HBA_STATUS_OK);
}

HBA_STATUS
Sun_HBA_ForceLip(HBA_HANDLE handle, int *rval)
{
	HBA_STATUS		status;
	HBA_LIBRARY_INFO	*lib_infop;
	HBA_HANDLE		vendorHandle;

	Sun_HBADoForceLipFunc	DoForceLipFunc;

	DEBUG(2, "Sun_HBA_DoForceLip", 0, 0, 0);

	NPIVCHECKLIBRARY();
	DoForceLipFunc = (Sun_HBADoForceLipFunc)
		dlsym(lib_infop->hLibrary, "Sun_fcDoForceLip");
	if (DoForceLipFunc != NULL) {
		status = ((DoForceLipFunc)(vendorHandle, rval));
	} else {
		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
	}
	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
}