xref: /titanic_44/usr/src/lib/hbaapi/common/HBAAPILIB-sun.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
1*fcf3ce44SJohn Forte /*************************************************************************
2*fcf3ce44SJohn Forte  * Description
3*fcf3ce44SJohn Forte  *	HBAAPILIB-sun.c - Implements the Sun Extention for Target mode
4*fcf3ce44SJohn Forte  *		FCHBA discovery
5*fcf3ce44SJohn Forte  *
6*fcf3ce44SJohn Forte  * License:
7*fcf3ce44SJohn Forte  *	The contents of this file are subject to the SNIA Public License
8*fcf3ce44SJohn Forte  *	Version 1.0 (the "License"); you may not use this file except in
9*fcf3ce44SJohn Forte  *	compliance with the License. You may obtain a copy of the License at
10*fcf3ce44SJohn Forte  *
11*fcf3ce44SJohn Forte  *	/http://www.snia.org/English/Resources/Code/OpenSource.html
12*fcf3ce44SJohn Forte  *
13*fcf3ce44SJohn Forte  *	Software distributed under the License is distributed on an "AS IS"
14*fcf3ce44SJohn Forte  *	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
15*fcf3ce44SJohn Forte  *	the License for the specific language governing rights and limitations
16*fcf3ce44SJohn Forte  *	under the License.
17*fcf3ce44SJohn Forte  *
18*fcf3ce44SJohn Forte  *************************************************************************
19*fcf3ce44SJohn Forte  */
20*fcf3ce44SJohn Forte 
21*fcf3ce44SJohn Forte #ifdef WIN32
22*fcf3ce44SJohn Forte #include <windows.h>
23*fcf3ce44SJohn Forte #include <string.h>
24*fcf3ce44SJohn Forte /*
25*fcf3ce44SJohn Forte  * Next define forces entry points in the dll to be exported
26*fcf3ce44SJohn Forte  * See hbaapi.h to see what it does.
27*fcf3ce44SJohn Forte  */
28*fcf3ce44SJohn Forte #define HBAAPI_EXPORTS
29*fcf3ce44SJohn Forte #else
30*fcf3ce44SJohn Forte #include <dlfcn.h>
31*fcf3ce44SJohn Forte #include <strings.h>
32*fcf3ce44SJohn Forte #endif
33*fcf3ce44SJohn Forte #include <stdio.h>
34*fcf3ce44SJohn Forte #include <time.h>
35*fcf3ce44SJohn Forte #include <dlfcn.h>
36*fcf3ce44SJohn Forte #include "hbaapi.h"
37*fcf3ce44SJohn Forte #include "hbaapi-sun.h"
38*fcf3ce44SJohn Forte #include "vendorhbaapi.h"
39*fcf3ce44SJohn Forte #include <stdlib.h>
40*fcf3ce44SJohn Forte #ifdef USESYSLOG
41*fcf3ce44SJohn Forte #include <syslog.h>
42*fcf3ce44SJohn Forte #endif
43*fcf3ce44SJohn Forte 
44*fcf3ce44SJohn Forte 
45*fcf3ce44SJohn Forte /*
46*fcf3ce44SJohn Forte  * LIBRARY_NUM is a shortcut to figure out which library we need to call.
47*fcf3ce44SJohn Forte  *  The top 16 bits of handle are the library index
48*fcf3ce44SJohn Forte  */
49*fcf3ce44SJohn Forte #define LIBRARY_NUM(handle)	((handle)>>16)
50*fcf3ce44SJohn Forte 
51*fcf3ce44SJohn Forte /*
52*fcf3ce44SJohn Forte  * VENDOR_HANDLE turns a global library handle into a vendor specific handle,
53*fcf3ce44SJohn Forte  * with all upper 16 bits set to 0
54*fcf3ce44SJohn Forte  */
55*fcf3ce44SJohn Forte #define VENDOR_HANDLE(handle)	((handle)&0xFFFF)
56*fcf3ce44SJohn Forte 
57*fcf3ce44SJohn Forte #define HBA_HANDLE_FROM_LOCAL(library, vendor) \
58*fcf3ce44SJohn Forte 				(((library)<<16) | ((vendor)&0x0000FFFF))
59*fcf3ce44SJohn Forte 
60*fcf3ce44SJohn Forte extern int _hbaapi_debuglevel;
61*fcf3ce44SJohn Forte #define DEBUG(L, STR, A1, A2, A3)
62*fcf3ce44SJohn Forte 
63*fcf3ce44SJohn Forte #if defined(USESYSLOG) && defined(USELOGFILE)
64*fcf3ce44SJohn Forte extern FILE *_hbaapi_debug_fd;
65*fcf3ce44SJohn Forte extern int _hbaapi_sysloginit;
66*fcf3ce44SJohn Forte #undef DEBUG
67*fcf3ce44SJohn Forte #ifdef WIN32
68*fcf3ce44SJohn Forte #define DEBUG(L, STR, A1, A2, A3)\
69*fcf3ce44SJohn Forte     if ((L) <= _hbaapi_debuglevel) {\
70*fcf3ce44SJohn Forte 	if(_hbaapi_sysloginit == 0) {\
71*fcf3ce44SJohn Forte 	    openlog("HBAAPI", LOG_PID|LOG_ODELAY ,LOG_USER);\
72*fcf3ce44SJohn Forte 	    _hbaapi_sysloginit = 1;\
73*fcf3ce44SJohn Forte 	}\
74*fcf3ce44SJohn Forte 	syslog (LOG_INFO, (STR), (A1), (A2), (A3));\
75*fcf3ce44SJohn Forte 	if(_hbaapi_debug_fd == NULL) {\
76*fcf3ce44SJohn Forte 	    char _logFile[MAX_PATH]; \
77*fcf3ce44SJohn Forte 	    GetTempPath(MAX_PATH, _logFile); \
78*fcf3ce44SJohn Forte 	    strcat(_logFile, "HBAAPI.log"); \
79*fcf3ce44SJohn Forte 	    _hbaapi_debug_fd = fopen(_logFile, "a");\
80*fcf3ce44SJohn Forte 	}\
81*fcf3ce44SJohn Forte         if(_hbaapi_debug_fd != NULL) {\
82*fcf3ce44SJohn Forte 	    fprintf(_hbaapi_debug_fd, (STR ## "\n"), (A1), (A2), (A3));\
83*fcf3ce44SJohn Forte 	}\
84*fcf3ce44SJohn Forte     }
85*fcf3ce44SJohn Forte #else /* WIN32*/
86*fcf3ce44SJohn Forte #define DEBUG(L, STR, A1, A2, A3)\
87*fcf3ce44SJohn Forte     if ((L) <= _hbaapi_debuglevel) {\
88*fcf3ce44SJohn Forte 	if(_hbaapi_sysloginit == 0) {\
89*fcf3ce44SJohn Forte 	    openlog("HBAAPI", LOG_PID|LOG_ODELAY ,LOG_USER);\
90*fcf3ce44SJohn Forte 	    _hbaapi_sysloginit = 1;\
91*fcf3ce44SJohn Forte 	}\
92*fcf3ce44SJohn Forte 	syslog (LOG_INFO, (STR), (A1), (A2), (A3));\
93*fcf3ce44SJohn Forte 	if(_hbaapi_debug_fd == NULL) {\
94*fcf3ce44SJohn Forte 	    _hbaapi_debug_fd = fopen("/tmp/HBAAPI.log", "a");\
95*fcf3ce44SJohn Forte 	}\
96*fcf3ce44SJohn Forte         if(_hbaapi_debug_fd != NULL) {\
97*fcf3ce44SJohn Forte 	    fprintf(_hbaapi_debug_fd, (STR ## "\n"), (A1), (A2), (A3));\
98*fcf3ce44SJohn Forte 	}\
99*fcf3ce44SJohn Forte     }
100*fcf3ce44SJohn Forte #endif /* WIN32*/
101*fcf3ce44SJohn Forte 
102*fcf3ce44SJohn Forte #else /* Not both USESYSLOG and USELOGFILE */
103*fcf3ce44SJohn Forte #if defined(USESYSLOG)
104*fcf3ce44SJohn Forte int _hbaapi_sysloginit = 0;
105*fcf3ce44SJohn Forte #undef DEBUG
106*fcf3ce44SJohn Forte #define DEBUG(L, STR, A1, A2, A3) \
107*fcf3ce44SJohn Forte     if ((L) <= _hbaapi_debuglevel) {\
108*fcf3ce44SJohn Forte 	if(_hbaapi_sysloginit == 0) {\
109*fcf3ce44SJohn Forte 	    openlog("HBAAPI", LOG_PID|LOG_ODELAY ,LOG_USER);\
110*fcf3ce44SJohn Forte 	    _hbaapi_sysloginit = 1;\
111*fcf3ce44SJohn Forte 	}\
112*fcf3ce44SJohn Forte 	syslog (LOG_INFO, (STR), (A1), (A2), (A3));\
113*fcf3ce44SJohn Forte     }
114*fcf3ce44SJohn Forte #endif /* USESYSLOG */
115*fcf3ce44SJohn Forte #if defined(USELOGFILE)
116*fcf3ce44SJohn Forte FILE *_hbaapi_debug_fd = NULL;
117*fcf3ce44SJohn Forte #undef DEBUG
118*fcf3ce44SJohn Forte #ifdef WIN32
119*fcf3ce44SJohn Forte #define DEBUG(L, STR, A1, A2, A3) \
120*fcf3ce44SJohn Forte     if((L) <= _hbaapi_debuglevel) {\
121*fcf3ce44SJohn Forte 	if(_hbaapi_debug_fd == NULL) {\
122*fcf3ce44SJohn Forte 	    char _logFile[MAX_PATH]; \
123*fcf3ce44SJohn Forte 	    GetTempPath(MAX_PATH, _logFile); \
124*fcf3ce44SJohn Forte 	    strcat(_logFile, "HBAAPI.log"); \
125*fcf3ce44SJohn Forte 	    _hbaapi_debug_fd = fopen(_logFile, "a");\
126*fcf3ce44SJohn Forte         }\
127*fcf3ce44SJohn Forte     }
128*fcf3ce44SJohn Forte #else /* WIN32 */
129*fcf3ce44SJohn Forte #define DEBUG(L, STR, A1, A2, A3) \
130*fcf3ce44SJohn Forte     if((L) <= _hbaapi_debuglevel) {\
131*fcf3ce44SJohn Forte 	if(_hbaapi_debug_fd == NULL) {\
132*fcf3ce44SJohn Forte 	    _hbaapi_debug_fd = fopen("/tmp/HBAAPI.log", "a");\
133*fcf3ce44SJohn Forte 	}\
134*fcf3ce44SJohn Forte 	if(_hbaapi_debug_fd != NULL) { \
135*fcf3ce44SJohn Forte 	    fprintf(_hbaapi_debug_fd, (STR) ## "\n", (A1), (A2), (A3));\
136*fcf3ce44SJohn Forte 	}\
137*fcf3ce44SJohn Forte     }
138*fcf3ce44SJohn Forte #endif /* WIN32 */
139*fcf3ce44SJohn Forte #endif /* USELOGFILE */
140*fcf3ce44SJohn Forte #endif /* Not both USELOGFILE and USESYSLOG */
141*fcf3ce44SJohn Forte 
142*fcf3ce44SJohn Forte #ifdef POSIX_THREADS
143*fcf3ce44SJohn Forte #include <pthread.h>
144*fcf3ce44SJohn Forte /*
145*fcf3ce44SJohn Forte  * When multiple mutex's are grabed, they must be always be grabbed in
146*fcf3ce44SJohn Forte  * the same order, or deadlock can result.  There are three levels
147*fcf3ce44SJohn Forte  * of mutex's involved in this API.  If LL_mutex is grabbed, always grap
148*fcf3ce44SJohn Forte  * it first.  If AL_mutex is grabbed, it may not be grabbed before
149*fcf3ce44SJohn Forte  * LL_mutex.  If grabbed in a multi grab sequence, the mutex's protecting
150*fcf3ce44SJohn Forte  * the callback lists must always be grabbed last and release before calling
151*fcf3ce44SJohn Forte  * a vendor specific library function that might invoke a callback function
152*fcf3ce44SJohn Forte  * on the same thread.
153*fcf3ce44SJohn Forte  */
154*fcf3ce44SJohn Forte #define GRAB_MUTEX(M)			grab_mutex(M)
155*fcf3ce44SJohn Forte #define RELEASE_MUTEX(M)		release_mutex(M)
156*fcf3ce44SJohn Forte #define RELEASE_MUTEX_RETURN(M,RET)	release_mutex(M); return(RET)
157*fcf3ce44SJohn Forte #elif defined (WIN32)
158*fcf3ce44SJohn Forte #define GRAB_MUTEX(m)			EnterCriticalSection(m)
159*fcf3ce44SJohn Forte #define RELEASE_MUTEX(m)		LeaveCriticalSection(m)
160*fcf3ce44SJohn Forte #define RELEASE_MUTEX_RETURN(m, RET)	LeaveCriticalSection(m); return(RET)
161*fcf3ce44SJohn Forte #else
162*fcf3ce44SJohn Forte #define GRAB_MUTEX(M)
163*fcf3ce44SJohn Forte #define RELEASE_MUTEX(M)
164*fcf3ce44SJohn Forte #define RELEASE_MUTEX_RETURN(M,RET)	return(RET)
165*fcf3ce44SJohn Forte #endif
166*fcf3ce44SJohn Forte 
167*fcf3ce44SJohn Forte /*
168*fcf3ce44SJohn Forte  * HBA_LIBRARY_STATUS and HBA_LIBRARY_INFO are redefined here.
169*fcf3ce44SJohn Forte  * Avoid any change in the common code.
170*fcf3ce44SJohn Forte  */
171*fcf3ce44SJohn Forte typedef enum {
172*fcf3ce44SJohn Forte     HBA_LIBRARY_UNKNOWN,
173*fcf3ce44SJohn Forte     HBA_LIBRARY_LOADED,
174*fcf3ce44SJohn Forte     HBA_LIBRARY_NOT_LOADED
175*fcf3ce44SJohn Forte } HBA_LIBRARY_STATUS;
176*fcf3ce44SJohn Forte 
177*fcf3ce44SJohn Forte typedef struct hba_library_info {
178*fcf3ce44SJohn Forte     struct hba_library_info
179*fcf3ce44SJohn Forte 			*next;
180*fcf3ce44SJohn Forte #ifdef WIN32
181*fcf3ce44SJohn Forte     HINSTANCE		hLibrary;		/* Handle to a loaded DLL */
182*fcf3ce44SJohn Forte #else
183*fcf3ce44SJohn Forte     char		*LibraryName;
184*fcf3ce44SJohn Forte     void*		hLibrary;		/* Handle to a loaded DLL */
185*fcf3ce44SJohn Forte #endif
186*fcf3ce44SJohn Forte     char		*LibraryPath;
187*fcf3ce44SJohn Forte     HBA_ENTRYPOINTSV2	functionTable;		/* Function pointers */
188*fcf3ce44SJohn Forte     HBA_LIBRARY_STATUS	status;			/* info on this library */
189*fcf3ce44SJohn Forte     HBA_UINT32		index;
190*fcf3ce44SJohn Forte } HBA_LIBRARY_INFO, *PHBA_LIBRARY_INFO;
191*fcf3ce44SJohn Forte 
192*fcf3ce44SJohn Forte #define ARE_WE_INITED() \
193*fcf3ce44SJohn Forte 	if (_hbaapi_librarylist == NULL) { \
194*fcf3ce44SJohn Forte 		return(HBA_STATUS_ERROR); \
195*fcf3ce44SJohn Forte 	}
196*fcf3ce44SJohn Forte 
197*fcf3ce44SJohn Forte extern HBA_LIBRARY_INFO *_hbaapi_librarylist;
198*fcf3ce44SJohn Forte extern HBA_UINT32 _hbaapi_total_library_count;
199*fcf3ce44SJohn Forte #ifdef POSIX_THREADS
200*fcf3ce44SJohn Forte extern pthread_mutex_t _hbaapi_LL_mutex;
201*fcf3ce44SJohn Forte #elif defined(WIN32)
202*fcf3ce44SJohn Forte extern CRITICAL_SECTION _hbaapi_LL_mutex;
203*fcf3ce44SJohn Forte #endif
204*fcf3ce44SJohn Forte 
205*fcf3ce44SJohn Forte /*
206*fcf3ce44SJohn Forte  * Function type def fop Sun extentions.
207*fcf3ce44SJohn Forte  */
208*fcf3ce44SJohn Forte typedef HBA_UINT32	(* Sun_HBAGetNumberOfTgtAdaptersFunc)();
209*fcf3ce44SJohn Forte typedef HBA_STATUS	(* Sun_HBAGetTgtAdapterNameFunc)(HBA_UINT32, char *);
210*fcf3ce44SJohn Forte typedef HBA_HANDLE	(* Sun_HBAOpenTgtAdapterFunc)(char *);
211*fcf3ce44SJohn Forte typedef HBA_STATUS	(* Sun_HBAOpenTgtAdapterByWWNFunc)
212*fcf3ce44SJohn Forte 			    (HBA_HANDLE *, HBA_WWN);
213*fcf3ce44SJohn Forte typedef	HBA_STATUS	(* Sun_HBANPIVGetAdapterAttributesFunc)
214*fcf3ce44SJohn Forte 			    (HBA_HANDLE, HBA_ADAPTERATTRIBUTES *);
215*fcf3ce44SJohn Forte typedef	HBA_STATUS	(* Sun_HBAGetNPIVPortInfoFunc)
216*fcf3ce44SJohn Forte 			    (HBA_HANDLE, HBA_UINT32, HBA_UINT32, HBA_NPIVATTRIBUTES *);
217*fcf3ce44SJohn Forte typedef HBA_STATUS	(* Sun_HBADeleteNPIVPortFunc)
218*fcf3ce44SJohn Forte 			    (HBA_HANDLE, HBA_UINT32, HBA_WWN);
219*fcf3ce44SJohn Forte typedef HBA_STATUS	(* Sun_HBACreateNPIVPortFunc)
220*fcf3ce44SJohn Forte 			    (HBA_HANDLE, HBA_UINT32, HBA_WWN, HBA_WWN, HBA_UINT32 *);
221*fcf3ce44SJohn Forte typedef	HBA_STATUS	(* Sun_HBAAdapterReturnWWNFunc)
222*fcf3ce44SJohn Forte 			    (HBA_HANDLE, HBA_UINT32, HBA_WWN *, HBA_WWN *);
223*fcf3ce44SJohn Forte typedef	HBA_STATUS	(* Sun_HBAAdapterCreateWWNFunc)
224*fcf3ce44SJohn Forte 			    (HBA_HANDLE, HBA_UINT32, HBA_WWN *, HBA_WWN *, HBA_WWN *,
225*fcf3ce44SJohn Forte 			    HBA_INT32);
226*fcf3ce44SJohn Forte typedef	HBA_STATUS	(* Sun_HBAGetPortNPIVAttributesFunc)
227*fcf3ce44SJohn Forte 			    (HBA_HANDLE, HBA_UINT32, HBA_PORTNPIVATTRIBUTES *);
228*fcf3ce44SJohn Forte typedef	HBA_STATUS	(* Sun_HBARegisterForAdapterDeviceEventsFunc)
229*fcf3ce44SJohn Forte 			    (void (*)(void *, HBA_WWN, HBA_UINT32, HBA_UINT32),
230*fcf3ce44SJohn Forte 			    void *, HBA_HANDLE, HBA_WWN, HBA_CALLBACKHANDLE *);
231*fcf3ce44SJohn Forte 
232*fcf3ce44SJohn Forte /*
233*fcf3ce44SJohn Forte  * Individual adapter (hba) information
234*fcf3ce44SJohn Forte  * Same as hbaadapter with different structure name.
235*fcf3ce44SJohn Forte  */
236*fcf3ce44SJohn Forte typedef struct hba_tgtadapter_info {
237*fcf3ce44SJohn Forte     struct hba_tgtadapter_info
238*fcf3ce44SJohn Forte 			*next;
239*fcf3ce44SJohn Forte     HBA_STATUS		GNstatus; /* status from GetTgtAdapterNameFunc */
240*fcf3ce44SJohn Forte     char		*name;
241*fcf3ce44SJohn Forte     HBA_WWN		nodeWWN;
242*fcf3ce44SJohn Forte     HBA_LIBRARY_INFO	*library;
243*fcf3ce44SJohn Forte     HBA_UINT32		index;
244*fcf3ce44SJohn Forte } HBA_TGTADAPTER_INFO;
245*fcf3ce44SJohn Forte 
246*fcf3ce44SJohn Forte /*
247*fcf3ce44SJohn Forte  * Make the list as an array with max size 16
248*fcf3ce44SJohn Forte  */
249*fcf3ce44SJohn Forte HBA_TGTADAPTER_INFO *_hbaapi_tgtadapterlist;
250*fcf3ce44SJohn Forte HBA_UINT32 _hbaapi_total_tgtadapter_count = 0;
251*fcf3ce44SJohn Forte #ifdef POSIX_THREADS
252*fcf3ce44SJohn Forte pthread_mutex_t _hbaapi_tgtAL_mutex = PTHREAD_MUTEX_INITIALIZER;
253*fcf3ce44SJohn Forte #elif defined(WIN32)
254*fcf3ce44SJohn Forte CRITICAL_SECTION _hbaapi_tgtAL_mutex;
255*fcf3ce44SJohn Forte #endif
256*fcf3ce44SJohn Forte 
257*fcf3ce44SJohn Forte /*
258*fcf3ce44SJohn Forte  * Common library internal. Mutex handling
259*fcf3ce44SJohn Forte  */
260*fcf3ce44SJohn Forte #ifdef POSIX_THREADS
261*fcf3ce44SJohn Forte static void
262*fcf3ce44SJohn Forte grab_mutex(pthread_mutex_t *mp) {
263*fcf3ce44SJohn Forte     int ret;
264*fcf3ce44SJohn Forte     if((ret = pthread_mutex_lock(mp)) != 0) {
265*fcf3ce44SJohn Forte 	perror("pthread_mutex_lock - HBAAPI:");
266*fcf3ce44SJohn Forte 	DEBUG(0, "pthread_mutex_lock returned %d", ret, 0, 0);
267*fcf3ce44SJohn Forte     }
268*fcf3ce44SJohn Forte }
269*fcf3ce44SJohn Forte 
270*fcf3ce44SJohn Forte static void
271*fcf3ce44SJohn Forte release_mutex(pthread_mutex_t *mp) {
272*fcf3ce44SJohn Forte     int ret;
273*fcf3ce44SJohn Forte     if((ret = pthread_mutex_unlock(mp)) != 0) {
274*fcf3ce44SJohn Forte 	perror("pthread_mutex_unlock - HBAAPI:");
275*fcf3ce44SJohn Forte 	DEBUG(0, "pthread_mutex_unlock returned %d", ret, 0, 0);
276*fcf3ce44SJohn Forte     }
277*fcf3ce44SJohn Forte }
278*fcf3ce44SJohn Forte #endif
279*fcf3ce44SJohn Forte 
280*fcf3ce44SJohn Forte /*
281*fcf3ce44SJohn Forte  * The API used to use fixed size tables as its primary data structure.
282*fcf3ce44SJohn Forte  * Indexing from 1 to N identified each adapters.  Now the adapters are
283*fcf3ce44SJohn Forte  * on a linked list.  There is a unique "index" foreach each adapter.
284*fcf3ce44SJohn Forte  * Adapters always keep their index, even if they are removed from the
285*fcf3ce44SJohn Forte  * hardware.  The only time the indexing is reset is on HBA_FreeLibrary
286*fcf3ce44SJohn Forte  */
287*fcf3ce44SJohn Forte HBA_UINT32
288*fcf3ce44SJohn Forte Sun_HBA_GetNumberOfTgtAdapters()
289*fcf3ce44SJohn Forte {
290*fcf3ce44SJohn Forte     int j=0;
291*fcf3ce44SJohn Forte     HBA_LIBRARY_INFO	*lib_infop;
292*fcf3ce44SJohn Forte     Sun_HBAGetNumberOfTgtAdaptersFunc
293*fcf3ce44SJohn Forte 			GetNumberOfTgtAdaptersFunc = NULL;
294*fcf3ce44SJohn Forte     Sun_HBAGetTgtAdapterNameFunc
295*fcf3ce44SJohn Forte 			GetTgtAdapterNameFunc = NULL;
296*fcf3ce44SJohn Forte     HBA_BOOLEAN		found_name;
297*fcf3ce44SJohn Forte     HBA_TGTADAPTER_INFO	*adapt_infop;
298*fcf3ce44SJohn Forte     HBA_STATUS		status;
299*fcf3ce44SJohn Forte 
300*fcf3ce44SJohn Forte     char adaptername[256];
301*fcf3ce44SJohn Forte     int num_adapters; /* local */
302*fcf3ce44SJohn Forte 
303*fcf3ce44SJohn Forte     if(_hbaapi_librarylist == NULL) {
304*fcf3ce44SJohn Forte 	return (0);
305*fcf3ce44SJohn Forte     }
306*fcf3ce44SJohn Forte     GRAB_MUTEX(&_hbaapi_LL_mutex); /* pay attention to order */
307*fcf3ce44SJohn Forte     GRAB_MUTEX(&_hbaapi_tgtAL_mutex);
308*fcf3ce44SJohn Forte 
309*fcf3ce44SJohn Forte     for (lib_infop = _hbaapi_librarylist;
310*fcf3ce44SJohn Forte 	 lib_infop != NULL;
311*fcf3ce44SJohn Forte 	 lib_infop = lib_infop->next) {
312*fcf3ce44SJohn Forte 
313*fcf3ce44SJohn Forte 	if (lib_infop->status != HBA_LIBRARY_LOADED) {
314*fcf3ce44SJohn Forte 	    continue;
315*fcf3ce44SJohn Forte 	}
316*fcf3ce44SJohn Forte 
317*fcf3ce44SJohn Forte 	if (lib_infop->hLibrary != NULL) {
318*fcf3ce44SJohn Forte             GetNumberOfTgtAdaptersFunc = (Sun_HBAGetNumberOfTgtAdaptersFunc)
319*fcf3ce44SJohn Forte 		dlsym(lib_infop->hLibrary, "Sun_fcGetNumberOfTgtAdapters");
320*fcf3ce44SJohn Forte             GetTgtAdapterNameFunc = (Sun_HBAGetTgtAdapterNameFunc)
321*fcf3ce44SJohn Forte 		dlsym(lib_infop->hLibrary, "Sun_fcGetTgtAdapterName");
322*fcf3ce44SJohn Forte 	    if (GetNumberOfTgtAdaptersFunc == NULL ||
323*fcf3ce44SJohn Forte 		GetTgtAdapterNameFunc == NULL)	{
324*fcf3ce44SJohn Forte 		GetNumberOfTgtAdaptersFunc = GetTgtAdapterNameFunc = NULL;
325*fcf3ce44SJohn Forte                 continue;
326*fcf3ce44SJohn Forte             }
327*fcf3ce44SJohn Forte 	} else {
328*fcf3ce44SJohn Forte 	    continue;
329*fcf3ce44SJohn Forte 	}
330*fcf3ce44SJohn Forte 
331*fcf3ce44SJohn Forte 	num_adapters = ((GetNumberOfTgtAdaptersFunc)());
332*fcf3ce44SJohn Forte #ifndef WIN32
333*fcf3ce44SJohn Forte 	DEBUG(1, "HBAAPI: number of target mode adapters for %s = %d\n",
334*fcf3ce44SJohn Forte 	      lib_infop->LibraryName, num_adapters, 0);
335*fcf3ce44SJohn Forte #else
336*fcf3ce44SJohn Forte 	DEBUG(1, "HBAAPI: number of target mode_adapters for %s = %d\n",
337*fcf3ce44SJohn Forte 	      lib_infop->LibraryPath, num_adapters, 0);
338*fcf3ce44SJohn Forte #endif
339*fcf3ce44SJohn Forte 
340*fcf3ce44SJohn Forte 	for (j = 0; j < num_adapters; j++) {
341*fcf3ce44SJohn Forte 	    found_name = 0;
342*fcf3ce44SJohn Forte 	    status = (GetTgtAdapterNameFunc)(j, (char *)&adaptername);
343*fcf3ce44SJohn Forte 	    if(status == HBA_STATUS_OK) {
344*fcf3ce44SJohn Forte 		for(adapt_infop = _hbaapi_tgtadapterlist;
345*fcf3ce44SJohn Forte 		    adapt_infop != NULL;
346*fcf3ce44SJohn Forte 		    adapt_infop = adapt_infop->next) {
347*fcf3ce44SJohn Forte 		    /*
348*fcf3ce44SJohn Forte 		     * check for duplicates, really, this may just be a second
349*fcf3ce44SJohn Forte 		     * call to this function
350*fcf3ce44SJohn Forte 		     * ??? how do we know when a name becomes stale?
351*fcf3ce44SJohn Forte 		     */
352*fcf3ce44SJohn Forte 		    if(strcmp(adaptername, adapt_infop->name) == 0) {
353*fcf3ce44SJohn Forte 			/* already got this one */
354*fcf3ce44SJohn Forte 			found_name++;
355*fcf3ce44SJohn Forte 			break;
356*fcf3ce44SJohn Forte 		    }
357*fcf3ce44SJohn Forte 		}
358*fcf3ce44SJohn Forte 		if(found_name != 0) {
359*fcf3ce44SJohn Forte 		    continue;
360*fcf3ce44SJohn Forte 		}
361*fcf3ce44SJohn Forte 	    }
362*fcf3ce44SJohn Forte 
363*fcf3ce44SJohn Forte 	    adapt_infop = (HBA_TGTADAPTER_INFO *)
364*fcf3ce44SJohn Forte 		calloc(1, sizeof(HBA_TGTADAPTER_INFO));
365*fcf3ce44SJohn Forte 	    if(adapt_infop == NULL) {
366*fcf3ce44SJohn Forte #ifndef WIN32
367*fcf3ce44SJohn Forte 		fprintf(stderr,
368*fcf3ce44SJohn Forte 			"HBA_GetNumberOfAdapters: calloc failed on sizeof:%d\n",
369*fcf3ce44SJohn Forte 			sizeof(HBA_TGTADAPTER_INFO));
370*fcf3ce44SJohn Forte #endif
371*fcf3ce44SJohn Forte 		RELEASE_MUTEX(&_hbaapi_tgtAL_mutex);
372*fcf3ce44SJohn Forte 		RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex,
373*fcf3ce44SJohn Forte 				     _hbaapi_total_tgtadapter_count);
374*fcf3ce44SJohn Forte 	    }
375*fcf3ce44SJohn Forte 	    if((adapt_infop->GNstatus = status) == HBA_STATUS_OK) {
376*fcf3ce44SJohn Forte 		adapt_infop->name = strdup(adaptername);
377*fcf3ce44SJohn Forte 	    } else {
378*fcf3ce44SJohn Forte 		char dummyname[512];
379*fcf3ce44SJohn Forte 		sprintf(dummyname, "NULLADAPTER-%s-%03d",
380*fcf3ce44SJohn Forte 			lib_infop->LibraryPath, _hbaapi_total_tgtadapter_count);
381*fcf3ce44SJohn Forte 		dummyname[255] = '\0';
382*fcf3ce44SJohn Forte 		adapt_infop->name = strdup(dummyname);
383*fcf3ce44SJohn Forte 	    }
384*fcf3ce44SJohn Forte 	    adapt_infop->library = lib_infop;
385*fcf3ce44SJohn Forte 	    adapt_infop->next = _hbaapi_tgtadapterlist;
386*fcf3ce44SJohn Forte 	    adapt_infop->index = _hbaapi_total_tgtadapter_count;
387*fcf3ce44SJohn Forte 	    _hbaapi_tgtadapterlist = adapt_infop;
388*fcf3ce44SJohn Forte 	    _hbaapi_total_tgtadapter_count++;
389*fcf3ce44SJohn Forte 	}
390*fcf3ce44SJohn Forte 	GetNumberOfTgtAdaptersFunc = GetTgtAdapterNameFunc = NULL;
391*fcf3ce44SJohn Forte     }
392*fcf3ce44SJohn Forte     RELEASE_MUTEX(&_hbaapi_tgtAL_mutex);
393*fcf3ce44SJohn Forte     RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, _hbaapi_total_tgtadapter_count);
394*fcf3ce44SJohn Forte }
395*fcf3ce44SJohn Forte 
396*fcf3ce44SJohn Forte HBA_STATUS
397*fcf3ce44SJohn Forte Sun_HBA_GetTgtAdapterName(
398*fcf3ce44SJohn Forte     HBA_UINT32 adapterindex,
399*fcf3ce44SJohn Forte     char *adaptername)
400*fcf3ce44SJohn Forte {
401*fcf3ce44SJohn Forte     HBA_TGTADAPTER_INFO	*adapt_infop;
402*fcf3ce44SJohn Forte     HBA_STATUS		ret = HBA_STATUS_ERROR_ILLEGAL_INDEX;
403*fcf3ce44SJohn Forte 
404*fcf3ce44SJohn Forte     if (adaptername == NULL) {
405*fcf3ce44SJohn Forte 	    return(HBA_STATUS_ERROR_ARG);
406*fcf3ce44SJohn Forte     }
407*fcf3ce44SJohn Forte     /*
408*fcf3ce44SJohn Forte      * The adapter index is from old code, but we have
409*fcf3ce44SJohn Forte      * to support it.  Go down the list looking for
410*fcf3ce44SJohn Forte      * the adapter
411*fcf3ce44SJohn Forte      */
412*fcf3ce44SJohn Forte     ARE_WE_INITED();
413*fcf3ce44SJohn Forte     GRAB_MUTEX(&_hbaapi_tgtAL_mutex);
414*fcf3ce44SJohn Forte     *adaptername = '\0';
415*fcf3ce44SJohn Forte     for(adapt_infop = _hbaapi_tgtadapterlist;
416*fcf3ce44SJohn Forte 	adapt_infop != NULL;
417*fcf3ce44SJohn Forte 	adapt_infop = adapt_infop->next) {
418*fcf3ce44SJohn Forte 
419*fcf3ce44SJohn Forte 	if(adapt_infop->index == adapterindex) {
420*fcf3ce44SJohn Forte 	    if(adapt_infop->name != NULL &&
421*fcf3ce44SJohn Forte 	       adapt_infop->GNstatus == HBA_STATUS_OK) {
422*fcf3ce44SJohn Forte 		strcpy(adaptername, adapt_infop->name);
423*fcf3ce44SJohn Forte 	    } else {
424*fcf3ce44SJohn Forte 		*adaptername = '\0';
425*fcf3ce44SJohn Forte 	    }
426*fcf3ce44SJohn Forte 	    ret = adapt_infop->GNstatus;
427*fcf3ce44SJohn Forte 	    break;
428*fcf3ce44SJohn Forte 	}
429*fcf3ce44SJohn Forte     }
430*fcf3ce44SJohn Forte     DEBUG(2, "GetAdapterName for index:%d ->%s", adapterindex, adaptername, 0);
431*fcf3ce44SJohn Forte     RELEASE_MUTEX_RETURN(&_hbaapi_AL_mutex, ret);
432*fcf3ce44SJohn Forte }
433*fcf3ce44SJohn Forte 
434*fcf3ce44SJohn Forte HBA_HANDLE
435*fcf3ce44SJohn Forte Sun_HBA_OpenTgtAdapter(char* adaptername)
436*fcf3ce44SJohn Forte {
437*fcf3ce44SJohn Forte     HBA_HANDLE		handle;
438*fcf3ce44SJohn Forte     Sun_HBAOpenTgtAdapterFunc	OpenTgtAdapterFunc;
439*fcf3ce44SJohn Forte     HBA_TGTADAPTER_INFO	*adapt_infop;
440*fcf3ce44SJohn Forte     HBA_LIBRARY_INFO	*lib_infop;
441*fcf3ce44SJohn Forte 
442*fcf3ce44SJohn Forte     DEBUG(2, "OpenAdapter: %s", adaptername, 0, 0);
443*fcf3ce44SJohn Forte 
444*fcf3ce44SJohn Forte     if(_hbaapi_librarylist == NULL) {
445*fcf3ce44SJohn Forte 	return(HBA_HANDLE_INVALID);
446*fcf3ce44SJohn Forte     }
447*fcf3ce44SJohn Forte     if (adaptername == NULL) {
448*fcf3ce44SJohn Forte 	return(HBA_STATUS_ERROR_ARG);
449*fcf3ce44SJohn Forte     }
450*fcf3ce44SJohn Forte     handle = HBA_HANDLE_INVALID;
451*fcf3ce44SJohn Forte     GRAB_MUTEX(&_hbaapi_AL_mutex);
452*fcf3ce44SJohn Forte     for(adapt_infop = _hbaapi_tgtadapterlist;
453*fcf3ce44SJohn Forte 	adapt_infop != NULL;
454*fcf3ce44SJohn Forte 	adapt_infop = adapt_infop->next) {
455*fcf3ce44SJohn Forte 	if (strcmp(adaptername, adapt_infop->name) != 0) {
456*fcf3ce44SJohn Forte 	    continue;
457*fcf3ce44SJohn Forte 	}
458*fcf3ce44SJohn Forte 	lib_infop = adapt_infop->library;
459*fcf3ce44SJohn Forte         OpenTgtAdapterFunc = (Sun_HBAOpenTgtAdapterFunc)
460*fcf3ce44SJohn Forte 		dlsym(lib_infop->hLibrary, "Sun_fcOpenTgtAdapter");
461*fcf3ce44SJohn Forte 	if (OpenTgtAdapterFunc != NULL) {
462*fcf3ce44SJohn Forte 	    /* retrieve the vendor handle */
463*fcf3ce44SJohn Forte 	    handle = (OpenTgtAdapterFunc)(adaptername);
464*fcf3ce44SJohn Forte 	    if(handle != 0) {
465*fcf3ce44SJohn Forte 		/* or this with the library index to get the common handle */
466*fcf3ce44SJohn Forte 		handle = HBA_HANDLE_FROM_LOCAL(lib_infop->index, handle);
467*fcf3ce44SJohn Forte 	    }
468*fcf3ce44SJohn Forte 	}
469*fcf3ce44SJohn Forte 	break;
470*fcf3ce44SJohn Forte     }
471*fcf3ce44SJohn Forte     RELEASE_MUTEX_RETURN(&_hbaapi_AL_mutex, handle);
472*fcf3ce44SJohn Forte }
473*fcf3ce44SJohn Forte 
474*fcf3ce44SJohn Forte /*
475*fcf3ce44SJohn Forte  * This function ignores the list of known adapters and instead tries
476*fcf3ce44SJohn Forte  * each vendors open function to see if one of them
477*fcf3ce44SJohn Forte  * can open an adapter when referenced with a particular WWN
478*fcf3ce44SJohn Forte  */
479*fcf3ce44SJohn Forte HBA_STATUS
480*fcf3ce44SJohn Forte Sun_HBA_OpenTgtAdapterByWWN(HBA_HANDLE *phandle, HBA_WWN nodeWWN)
481*fcf3ce44SJohn Forte {
482*fcf3ce44SJohn Forte     HBA_HANDLE		handle;
483*fcf3ce44SJohn Forte     HBA_LIBRARY_INFO	*lib_infop;
484*fcf3ce44SJohn Forte     Sun_HBAGetNumberOfTgtAdaptersFunc
485*fcf3ce44SJohn Forte 			GetNumberOfTgtAdaptersFunc;
486*fcf3ce44SJohn Forte     Sun_HBAOpenTgtAdapterByWWNFunc
487*fcf3ce44SJohn Forte 			OpenTgtAdapterByWWNFunc;
488*fcf3ce44SJohn Forte     HBA_STATUS		status;
489*fcf3ce44SJohn Forte 
490*fcf3ce44SJohn Forte     DEBUG(2, "OpenAdapterByWWN: %s", WWN2STR1(&nodeWWN), 0, 0);
491*fcf3ce44SJohn Forte 
492*fcf3ce44SJohn Forte     if (phandle == NULL) {
493*fcf3ce44SJohn Forte 	    return(HBA_STATUS_ERROR_ARG);
494*fcf3ce44SJohn Forte     }
495*fcf3ce44SJohn Forte 
496*fcf3ce44SJohn Forte     ARE_WE_INITED();
497*fcf3ce44SJohn Forte 
498*fcf3ce44SJohn Forte     *phandle = HBA_HANDLE_INVALID;
499*fcf3ce44SJohn Forte 
500*fcf3ce44SJohn Forte     GRAB_MUTEX(&_hbaapi_LL_mutex);
501*fcf3ce44SJohn Forte     for (lib_infop = _hbaapi_librarylist;
502*fcf3ce44SJohn Forte 	 lib_infop != NULL;
503*fcf3ce44SJohn Forte 	 lib_infop = lib_infop->next) {
504*fcf3ce44SJohn Forte 
505*fcf3ce44SJohn Forte 	status = HBA_STATUS_ERROR_ILLEGAL_WWN;
506*fcf3ce44SJohn Forte 
507*fcf3ce44SJohn Forte 	if (lib_infop->status != HBA_LIBRARY_LOADED) {
508*fcf3ce44SJohn Forte 	    continue;
509*fcf3ce44SJohn Forte 	}
510*fcf3ce44SJohn Forte 
511*fcf3ce44SJohn Forte         GetNumberOfTgtAdaptersFunc = (Sun_HBAGetNumberOfTgtAdaptersFunc)
512*fcf3ce44SJohn Forte 		dlsym(lib_infop->hLibrary, "Sun_fcGetNumberOfTgtAdapters");
513*fcf3ce44SJohn Forte         OpenTgtAdapterByWWNFunc = (Sun_HBAOpenTgtAdapterByWWNFunc)
514*fcf3ce44SJohn Forte 		dlsym(lib_infop->hLibrary, "Sun_fcOpenTgtAdapterByWWN");
515*fcf3ce44SJohn Forte 	if (GetNumberOfTgtAdaptersFunc == NULL ||
516*fcf3ce44SJohn Forte 		OpenTgtAdapterByWWNFunc == NULL) {
517*fcf3ce44SJohn Forte 		GetNumberOfTgtAdaptersFunc = OpenTgtAdapterByWWNFunc = NULL;
518*fcf3ce44SJohn Forte                 continue;
519*fcf3ce44SJohn Forte         }
520*fcf3ce44SJohn Forte 
521*fcf3ce44SJohn Forte 	(void) ((GetNumberOfTgtAdaptersFunc)());
522*fcf3ce44SJohn Forte 
523*fcf3ce44SJohn Forte 	if((status = (OpenTgtAdapterByWWNFunc)(&handle, nodeWWN))
524*fcf3ce44SJohn Forte 	    != HBA_STATUS_OK) {
525*fcf3ce44SJohn Forte 	    GetNumberOfTgtAdaptersFunc = OpenTgtAdapterByWWNFunc = NULL;
526*fcf3ce44SJohn Forte 	    continue;
527*fcf3ce44SJohn Forte 	}
528*fcf3ce44SJohn Forte 	/* OK, make a vendor non-specific handle */
529*fcf3ce44SJohn Forte 	*phandle = HBA_HANDLE_FROM_LOCAL(lib_infop->index, handle);
530*fcf3ce44SJohn Forte 	status = HBA_STATUS_OK;
531*fcf3ce44SJohn Forte 	break;
532*fcf3ce44SJohn Forte 
533*fcf3ce44SJohn Forte 	GetNumberOfTgtAdaptersFunc = OpenTgtAdapterByWWNFunc = NULL;
534*fcf3ce44SJohn Forte     }
535*fcf3ce44SJohn Forte     RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
536*fcf3ce44SJohn Forte }
537*fcf3ce44SJohn Forte 
538*fcf3ce44SJohn Forte static HBA_STATUS
539*fcf3ce44SJohn Forte HBA_NPIV_CheckLibrary(HBA_HANDLE handle,
540*fcf3ce44SJohn Forte 			HBA_LIBRARY_INFO **lib_infopp,
541*fcf3ce44SJohn Forte 			HBA_HANDLE *vendorhandle) {
542*fcf3ce44SJohn Forte 	HBA_UINT32		libraryIndex;
543*fcf3ce44SJohn Forte 	HBA_LIBRARY_INFO	*lib_infop;
544*fcf3ce44SJohn Forte 
545*fcf3ce44SJohn Forte 	if (vendorhandle == NULL) {
546*fcf3ce44SJohn Forte 		return(HBA_STATUS_ERROR_ARG);
547*fcf3ce44SJohn Forte 	}
548*fcf3ce44SJohn Forte 	if(_hbaapi_librarylist == NULL) {
549*fcf3ce44SJohn Forte 		return(HBA_STATUS_ERROR);
550*fcf3ce44SJohn Forte 	}
551*fcf3ce44SJohn Forte 	libraryIndex = LIBRARY_NUM(handle);
552*fcf3ce44SJohn Forte 
553*fcf3ce44SJohn Forte 	GRAB_MUTEX(&_hbaapi_LL_mutex);
554*fcf3ce44SJohn Forte 	for(lib_infop = _hbaapi_librarylist;
555*fcf3ce44SJohn Forte 	    lib_infop != NULL;
556*fcf3ce44SJohn Forte 	    lib_infop = lib_infop->next) {
557*fcf3ce44SJohn Forte 		if(lib_infop->index == libraryIndex) {
558*fcf3ce44SJohn Forte 			if(lib_infop->status != HBA_LIBRARY_LOADED) {
559*fcf3ce44SJohn Forte 				return HBA_STATUS_ERROR;
560*fcf3ce44SJohn Forte 			}
561*fcf3ce44SJohn Forte 			*lib_infopp = lib_infop;
562*fcf3ce44SJohn Forte 			*vendorhandle = VENDOR_HANDLE(handle);
563*fcf3ce44SJohn Forte 			/* caller will release the mutex */
564*fcf3ce44SJohn Forte 			return HBA_STATUS_OK;
565*fcf3ce44SJohn Forte 		}
566*fcf3ce44SJohn Forte 	}
567*fcf3ce44SJohn Forte 	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, HBA_STATUS_ERROR_INVALID_HANDLE);
568*fcf3ce44SJohn Forte }
569*fcf3ce44SJohn Forte #define	NPIVCHECKLIBRARY() \
570*fcf3ce44SJohn Forte 	status = HBA_NPIV_CheckLibrary(handle, &lib_infop, &vendorHandle); \
571*fcf3ce44SJohn Forte 	if(status != HBA_STATUS_OK) { \
572*fcf3ce44SJohn Forte 		return(status); \
573*fcf3ce44SJohn Forte 	}
574*fcf3ce44SJohn Forte 
575*fcf3ce44SJohn Forte HBA_STATUS
576*fcf3ce44SJohn Forte Sun_HBA_NPIVGetAdapterAttributes (
577*fcf3ce44SJohn Forte     HBA_HANDLE		handle,
578*fcf3ce44SJohn Forte     HBA_ADAPTERATTRIBUTES
579*fcf3ce44SJohn Forte 			*hbaattributes)
580*fcf3ce44SJohn Forte {
581*fcf3ce44SJohn Forte 	HBA_STATUS		status;
582*fcf3ce44SJohn Forte 	HBA_LIBRARY_INFO	*lib_infop;
583*fcf3ce44SJohn Forte 	HBA_HANDLE		vendorHandle;
584*fcf3ce44SJohn Forte 	Sun_HBANPIVGetAdapterAttributesFunc	NPIVGetAdapterAttributesFunc;
585*fcf3ce44SJohn Forte 
586*fcf3ce44SJohn Forte 	DEBUG(2, "HBA_NPIVGetAdapterAttributes", 0, 0, 0);
587*fcf3ce44SJohn Forte 
588*fcf3ce44SJohn Forte 	NPIVCHECKLIBRARY();
589*fcf3ce44SJohn Forte 	NPIVGetAdapterAttributesFunc = (Sun_HBANPIVGetAdapterAttributesFunc)
590*fcf3ce44SJohn Forte 	    dlsym(lib_infop->hLibrary, "Sun_fcNPIVGetAdapterAttributes");
591*fcf3ce44SJohn Forte 	if (NPIVGetAdapterAttributesFunc != NULL) {
592*fcf3ce44SJohn Forte 		status = ((NPIVGetAdapterAttributesFunc)(vendorHandle,
593*fcf3ce44SJohn Forte 			hbaattributes));
594*fcf3ce44SJohn Forte 	} else {
595*fcf3ce44SJohn Forte 		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
596*fcf3ce44SJohn Forte 	}
597*fcf3ce44SJohn Forte 	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
598*fcf3ce44SJohn Forte }
599*fcf3ce44SJohn Forte 
600*fcf3ce44SJohn Forte HBA_STATUS
601*fcf3ce44SJohn Forte Sun_HBA_GetNPIVPortInfo (
602*fcf3ce44SJohn Forte     HBA_HANDLE		handle,
603*fcf3ce44SJohn Forte     HBA_UINT32		portindex,
604*fcf3ce44SJohn Forte     HBA_UINT32		vportindex,
605*fcf3ce44SJohn Forte     HBA_NPIVATTRIBUTES	*attributes)
606*fcf3ce44SJohn Forte {
607*fcf3ce44SJohn Forte 	HBA_STATUS		status;
608*fcf3ce44SJohn Forte 	HBA_LIBRARY_INFO	*lib_infop;
609*fcf3ce44SJohn Forte 	HBA_HANDLE		vendorHandle;
610*fcf3ce44SJohn Forte 	Sun_HBAGetNPIVPortInfoFunc	GetNPIVPortInfoFunc;
611*fcf3ce44SJohn Forte 
612*fcf3ce44SJohn Forte 	NPIVCHECKLIBRARY();
613*fcf3ce44SJohn Forte 	GetNPIVPortInfoFunc = (Sun_HBAGetNPIVPortInfoFunc)
614*fcf3ce44SJohn Forte 		dlsym(lib_infop->hLibrary, "Sun_fcGetNPIVPortInfo");
615*fcf3ce44SJohn Forte 	if (GetNPIVPortInfoFunc != NULL) {
616*fcf3ce44SJohn Forte 		status = ((GetNPIVPortInfoFunc)(vendorHandle, portindex,
617*fcf3ce44SJohn Forte 			vportindex, attributes));
618*fcf3ce44SJohn Forte 	} else {
619*fcf3ce44SJohn Forte 		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
620*fcf3ce44SJohn Forte 	}
621*fcf3ce44SJohn Forte 	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
622*fcf3ce44SJohn Forte }
623*fcf3ce44SJohn Forte 
624*fcf3ce44SJohn Forte HBA_STATUS
625*fcf3ce44SJohn Forte Sun_HBA_DeleteNPIVPort (
626*fcf3ce44SJohn Forte     HBA_HANDLE		handle,
627*fcf3ce44SJohn Forte     HBA_UINT32		portindex,
628*fcf3ce44SJohn Forte     HBA_WWN		vportWWN)
629*fcf3ce44SJohn Forte {
630*fcf3ce44SJohn Forte 	HBA_STATUS		status;
631*fcf3ce44SJohn Forte 	HBA_LIBRARY_INFO	*lib_infop;
632*fcf3ce44SJohn Forte 	HBA_HANDLE		vendorHandle;
633*fcf3ce44SJohn Forte 	Sun_HBADeleteNPIVPortFunc	DeleteNPIVPortFunc;
634*fcf3ce44SJohn Forte 
635*fcf3ce44SJohn Forte 	NPIVCHECKLIBRARY();
636*fcf3ce44SJohn Forte 	DeleteNPIVPortFunc = (Sun_HBADeleteNPIVPortFunc)
637*fcf3ce44SJohn Forte 		dlsym(lib_infop->hLibrary, "Sun_fcDeleteNPIVPort");
638*fcf3ce44SJohn Forte 	if (DeleteNPIVPortFunc != NULL) {
639*fcf3ce44SJohn Forte 		status = ((DeleteNPIVPortFunc)(vendorHandle,
640*fcf3ce44SJohn Forte 		    portindex, vportWWN));
641*fcf3ce44SJohn Forte 	} else {
642*fcf3ce44SJohn Forte 		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
643*fcf3ce44SJohn Forte 	}
644*fcf3ce44SJohn Forte 	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
645*fcf3ce44SJohn Forte }
646*fcf3ce44SJohn Forte 
647*fcf3ce44SJohn Forte HBA_STATUS
648*fcf3ce44SJohn Forte Sun_HBA_CreateNPIVPort (
649*fcf3ce44SJohn Forte     HBA_HANDLE		handle,
650*fcf3ce44SJohn Forte     HBA_UINT32		portindex,
651*fcf3ce44SJohn Forte     HBA_WWN		vnodeWWN,
652*fcf3ce44SJohn Forte     HBA_WWN		vportWWN,
653*fcf3ce44SJohn Forte     HBA_UINT32		*vportindex)
654*fcf3ce44SJohn Forte {
655*fcf3ce44SJohn Forte 	HBA_STATUS		status;
656*fcf3ce44SJohn Forte 	HBA_LIBRARY_INFO	*lib_infop;
657*fcf3ce44SJohn Forte 	HBA_HANDLE		vendorHandle;
658*fcf3ce44SJohn Forte 	Sun_HBACreateNPIVPortFunc	CreateNPIVPortFunc;
659*fcf3ce44SJohn Forte 
660*fcf3ce44SJohn Forte 	NPIVCHECKLIBRARY();
661*fcf3ce44SJohn Forte 	CreateNPIVPortFunc = (Sun_HBACreateNPIVPortFunc)
662*fcf3ce44SJohn Forte 		dlsym(lib_infop->hLibrary, "Sun_fcCreateNPIVPort");
663*fcf3ce44SJohn Forte 	if (CreateNPIVPortFunc != NULL) {
664*fcf3ce44SJohn Forte 		status = ((CreateNPIVPortFunc)(vendorHandle,
665*fcf3ce44SJohn Forte 		    portindex, vnodeWWN, vportWWN, vportindex));
666*fcf3ce44SJohn Forte 	} else {
667*fcf3ce44SJohn Forte 		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
668*fcf3ce44SJohn Forte 	}
669*fcf3ce44SJohn Forte 	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
670*fcf3ce44SJohn Forte }
671*fcf3ce44SJohn Forte 
672*fcf3ce44SJohn Forte HBA_STATUS
673*fcf3ce44SJohn Forte Sun_HBA_GetPortNPIVAttributes (
674*fcf3ce44SJohn Forte     HBA_HANDLE		handle,
675*fcf3ce44SJohn Forte     HBA_UINT32		portindex,
676*fcf3ce44SJohn Forte     HBA_PORTNPIVATTRIBUTES	*portnpivattributes)
677*fcf3ce44SJohn Forte {
678*fcf3ce44SJohn Forte 	HBA_STATUS		status;
679*fcf3ce44SJohn Forte 	HBA_LIBRARY_INFO	*lib_infop;
680*fcf3ce44SJohn Forte 	HBA_HANDLE		vendorHandle;
681*fcf3ce44SJohn Forte 	Sun_HBAGetPortNPIVAttributesFunc	GetPortNPIVAttributesFunc;
682*fcf3ce44SJohn Forte 
683*fcf3ce44SJohn Forte 	NPIVCHECKLIBRARY();
684*fcf3ce44SJohn Forte 	GetPortNPIVAttributesFunc = (Sun_HBAGetPortNPIVAttributesFunc)
685*fcf3ce44SJohn Forte 		dlsym(lib_infop->hLibrary, "Sun_fcGetPortNPIVAttributes");
686*fcf3ce44SJohn Forte 	if (GetPortNPIVAttributesFunc != NULL) {
687*fcf3ce44SJohn Forte 		status = ((GetPortNPIVAttributesFunc)(
688*fcf3ce44SJohn Forte 		    vendorHandle, portindex, portnpivattributes));
689*fcf3ce44SJohn Forte 	} else {
690*fcf3ce44SJohn Forte 		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
691*fcf3ce44SJohn Forte 	}
692*fcf3ce44SJohn Forte 	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
693*fcf3ce44SJohn Forte }
694*fcf3ce44SJohn Forte 
695*fcf3ce44SJohn Forte HBA_STATUS
696*fcf3ce44SJohn Forte Sun_HBA_AdapterCreateWWN (
697*fcf3ce44SJohn Forte     HBA_HANDLE		handle,
698*fcf3ce44SJohn Forte     HBA_UINT32		portindex,
699*fcf3ce44SJohn Forte     HBA_WWN		*nwwn,
700*fcf3ce44SJohn Forte     HBA_WWN		*pwwn,
701*fcf3ce44SJohn Forte     HBA_WWN		*OUI,
702*fcf3ce44SJohn Forte     HBA_INT32		method)
703*fcf3ce44SJohn Forte {
704*fcf3ce44SJohn Forte 	HBA_STATUS		status;
705*fcf3ce44SJohn Forte 	HBA_LIBRARY_INFO	*lib_infop;
706*fcf3ce44SJohn Forte 	HBA_HANDLE		vendorHandle;
707*fcf3ce44SJohn Forte 	Sun_HBAAdapterCreateWWNFunc	AdapterCreateWWNFunc;
708*fcf3ce44SJohn Forte 
709*fcf3ce44SJohn Forte 	NPIVCHECKLIBRARY();
710*fcf3ce44SJohn Forte 	AdapterCreateWWNFunc = (Sun_HBAAdapterCreateWWNFunc)
711*fcf3ce44SJohn Forte 		dlsym(lib_infop->hLibrary, "Sun_fcAdapterCreateWWN");
712*fcf3ce44SJohn Forte 	if (AdapterCreateWWNFunc != NULL) {
713*fcf3ce44SJohn Forte 		status = ((AdapterCreateWWNFunc)(vendorHandle,
714*fcf3ce44SJohn Forte 		    portindex, nwwn, pwwn, OUI, method));
715*fcf3ce44SJohn Forte 	} else {
716*fcf3ce44SJohn Forte 		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
717*fcf3ce44SJohn Forte 	}
718*fcf3ce44SJohn Forte 	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
719*fcf3ce44SJohn Forte }
720*fcf3ce44SJohn Forte 
721*fcf3ce44SJohn Forte HBA_STATUS
722*fcf3ce44SJohn Forte Sun_HBA_AdapterReturnWWN (
723*fcf3ce44SJohn Forte     HBA_HANDLE		handle,
724*fcf3ce44SJohn Forte     HBA_UINT32		portindex,
725*fcf3ce44SJohn Forte     HBA_WWN		*nwwn,
726*fcf3ce44SJohn Forte     HBA_WWN		*pwwn)
727*fcf3ce44SJohn Forte {
728*fcf3ce44SJohn Forte 	HBA_STATUS		status;
729*fcf3ce44SJohn Forte 	HBA_LIBRARY_INFO	*lib_infop;
730*fcf3ce44SJohn Forte 	HBA_HANDLE		vendorHandle;
731*fcf3ce44SJohn Forte 	Sun_HBAAdapterReturnWWNFunc	AdapterReturnWWNFunc;
732*fcf3ce44SJohn Forte 
733*fcf3ce44SJohn Forte 	NPIVCHECKLIBRARY();
734*fcf3ce44SJohn Forte 	AdapterReturnWWNFunc = (Sun_HBAAdapterReturnWWNFunc)
735*fcf3ce44SJohn Forte 		dlsym(lib_infop->hLibrary, "Sun_fcAdapterReturnWWN");
736*fcf3ce44SJohn Forte 	if (AdapterReturnWWNFunc != NULL) {
737*fcf3ce44SJohn Forte 		status = ((AdapterReturnWWNFunc)(vendorHandle,
738*fcf3ce44SJohn Forte 		    portindex, nwwn, pwwn));
739*fcf3ce44SJohn Forte 	} else {
740*fcf3ce44SJohn Forte 		status = HBA_STATUS_ERROR_NOT_SUPPORTED;
741*fcf3ce44SJohn Forte 	}
742*fcf3ce44SJohn Forte 	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
743*fcf3ce44SJohn Forte }
744*fcf3ce44SJohn Forte 
745*fcf3ce44SJohn Forte typedef struct hba_npivadaptercallback_elem {
746*fcf3ce44SJohn Forte     struct hba_npivadaptercallback_elem
747*fcf3ce44SJohn Forte 			*next;
748*fcf3ce44SJohn Forte     HBA_LIBRARY_INFO	*lib_info;
749*fcf3ce44SJohn Forte     void		*userdata;
750*fcf3ce44SJohn Forte     HBA_CALLBACKHANDLE	vendorcbhandle;
751*fcf3ce44SJohn Forte     void		(*callback)();
752*fcf3ce44SJohn Forte } HBA_NPIVADAPTERCALLBACK_ELEM;
753*fcf3ce44SJohn Forte extern HBA_NPIVADAPTERCALLBACK_ELEM *_hbaapi_adapterdeviceevents_callback_list;
754*fcf3ce44SJohn Forte 
755*fcf3ce44SJohn Forte /* Adapter Device Events ********************************************************/
756*fcf3ce44SJohn Forte static void
757*fcf3ce44SJohn Forte adapterdeviceevents_callback (void *data,
758*fcf3ce44SJohn Forte     HBA_WWN	PortWWN,
759*fcf3ce44SJohn Forte     HBA_UINT32	eventType,
760*fcf3ce44SJohn Forte     HBA_UINT32	fabricPortID)
761*fcf3ce44SJohn Forte {
762*fcf3ce44SJohn Forte 	HBA_NPIVADAPTERCALLBACK_ELEM	*acbp;
763*fcf3ce44SJohn Forte 
764*fcf3ce44SJohn Forte 	DEBUG(3, "AdapterDeviceEvent, port:%s, eventType:%d fabricPortID:0X%06x",
765*fcf3ce44SJohn Forte 	    WWN2STR1(&PortWWN), eventType, fabricPortID);
766*fcf3ce44SJohn Forte 
767*fcf3ce44SJohn Forte 	GRAB_MUTEX(&_hbaapi_APE_mutex);
768*fcf3ce44SJohn Forte 
769*fcf3ce44SJohn Forte 	for(acbp = _hbaapi_adapterdeviceevents_callback_list;
770*fcf3ce44SJohn Forte 	    acbp != NULL;
771*fcf3ce44SJohn Forte 	    acbp = acbp->next) {
772*fcf3ce44SJohn Forte 		if(data == (void *)acbp) {
773*fcf3ce44SJohn Forte 			(*acbp->callback)(acbp->userdata, PortWWN, eventType, fabricPortID);
774*fcf3ce44SJohn Forte 			break;
775*fcf3ce44SJohn Forte 		}
776*fcf3ce44SJohn Forte 	}
777*fcf3ce44SJohn Forte 	RELEASE_MUTEX(&_hbaapi_APE_mutex);
778*fcf3ce44SJohn Forte }
779*fcf3ce44SJohn Forte 
780*fcf3ce44SJohn Forte HBA_STATUS
781*fcf3ce44SJohn Forte Sun_HBA_RegisterForAdapterDeviceEvents (
782*fcf3ce44SJohn Forte     void	(*callback) (
783*fcf3ce44SJohn Forte 	void		*data,
784*fcf3ce44SJohn Forte 	HBA_WWN		PortWWN,
785*fcf3ce44SJohn Forte 	HBA_UINT32	eventType,
786*fcf3ce44SJohn Forte 	HBA_UINT32	fabricPortID
787*fcf3ce44SJohn Forte 	),
788*fcf3ce44SJohn Forte     void		*userData,
789*fcf3ce44SJohn Forte     HBA_HANDLE		handle,
790*fcf3ce44SJohn Forte     HBA_WWN		PortWWN,
791*fcf3ce44SJohn Forte     HBA_CALLBACKHANDLE	*callbackHandle)
792*fcf3ce44SJohn Forte {
793*fcf3ce44SJohn Forte 	HBA_NPIVADAPTERCALLBACK_ELEM	*acbp;
794*fcf3ce44SJohn Forte 	HBA_STATUS			status;
795*fcf3ce44SJohn Forte 	HBA_LIBRARY_INFO		*lib_infop;
796*fcf3ce44SJohn Forte 	HBA_HANDLE			vendorHandle;
797*fcf3ce44SJohn Forte 	Sun_HBARegisterForAdapterDeviceEventsFunc
798*fcf3ce44SJohn Forte 					registeredfunc;
799*fcf3ce44SJohn Forte 
800*fcf3ce44SJohn Forte 	if (callbackHandle == NULL) {
801*fcf3ce44SJohn Forte 		return(HBA_STATUS_ERROR_ARG);
802*fcf3ce44SJohn Forte 	}
803*fcf3ce44SJohn Forte 
804*fcf3ce44SJohn Forte         NPIVCHECKLIBRARY();
805*fcf3ce44SJohn Forte 	registeredfunc = (Sun_HBARegisterForAdapterDeviceEventsFunc)
806*fcf3ce44SJohn Forte                 dlsym(lib_infop->hLibrary,
807*fcf3ce44SJohn Forte 		    "Sun_fcRegisterForAdapterDeviceEvents");
808*fcf3ce44SJohn Forte 	if (registeredfunc == NULL) {
809*fcf3ce44SJohn Forte 		RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, HBA_STATUS_ERROR_NOT_SUPPORTED);
810*fcf3ce44SJohn Forte 	}
811*fcf3ce44SJohn Forte 
812*fcf3ce44SJohn Forte 	acbp = (HBA_NPIVADAPTERCALLBACK_ELEM *)
813*fcf3ce44SJohn Forte 		calloc(1, sizeof(HBA_NPIVADAPTERCALLBACK_ELEM));
814*fcf3ce44SJohn Forte 
815*fcf3ce44SJohn Forte 	if(acbp == NULL) {
816*fcf3ce44SJohn Forte 		RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, HBA_STATUS_ERROR);
817*fcf3ce44SJohn Forte 	}
818*fcf3ce44SJohn Forte 
819*fcf3ce44SJohn Forte 	*callbackHandle = (HBA_CALLBACKHANDLE) acbp;
820*fcf3ce44SJohn Forte 	acbp->callback = callback;
821*fcf3ce44SJohn Forte 	acbp->userdata = userData;
822*fcf3ce44SJohn Forte 	acbp->lib_info = lib_infop;
823*fcf3ce44SJohn Forte 
824*fcf3ce44SJohn Forte 	status = (registeredfunc)(adapterdeviceevents_callback,
825*fcf3ce44SJohn Forte 		(void *)acbp,
826*fcf3ce44SJohn Forte 		vendorHandle,
827*fcf3ce44SJohn Forte 		PortWWN,
828*fcf3ce44SJohn Forte 		&acbp->vendorcbhandle);
829*fcf3ce44SJohn Forte 	if(status != HBA_STATUS_OK) {
830*fcf3ce44SJohn Forte 		free(acbp);
831*fcf3ce44SJohn Forte 		RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, status);
832*fcf3ce44SJohn Forte 	}
833*fcf3ce44SJohn Forte 
834*fcf3ce44SJohn Forte 	GRAB_MUTEX(&_hbaapi_APE_mutex);
835*fcf3ce44SJohn Forte 	acbp->next = _hbaapi_adapterdeviceevents_callback_list;
836*fcf3ce44SJohn Forte 	_hbaapi_adapterdeviceevents_callback_list = acbp;
837*fcf3ce44SJohn Forte 	RELEASE_MUTEX(&_hbaapi_APE_mutex);
838*fcf3ce44SJohn Forte 
839*fcf3ce44SJohn Forte 	RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, HBA_STATUS_OK);
840*fcf3ce44SJohn Forte }
841