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