HBAAPILIB-sun.c revision a79493184c9332129c9c91500069322f6f3fafdd
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*************************************************************************
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Description
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * HBAAPILIB-sun.c - Implements the Sun Extention for Target mode
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * FCHBA discovery
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the SNIA Public License
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Version 1.0 (the "License"); you may not use this file except in
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * compliance with the License. You may obtain a copy of the License at
bff3dadc2c9a6711b6e359fc39d0170de218be50duo liu - Sun Microsystems - Beijing China * http://www.snia.org/English/Resources/Code/OpenSource.html
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Software distributed under the License is distributed on an "AS IS"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the License for the specific language governing rights and limitations
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *************************************************************************
a79493184c9332129c9c91500069322f6f3fafddReed * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
bff3dadc2c9a6711b6e359fc39d0170de218be50duo liu - Sun Microsystems - Beijing China * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Next define forces entry points in the dll to be exported
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See hbaapi.h to see what it does.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * LIBRARY_NUM is a shortcut to figure out which library we need to call.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The top 16 bits of handle are the library index
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * VENDOR_HANDLE turns a global library handle into a vendor specific handle,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * with all upper 16 bits set to 0
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte openlog("HBAAPI", LOG_PID|LOG_ODELAY ,LOG_USER);\
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(_hbaapi_debug_fd, (STR ## "\n"), (A1), (A2), (A3));\
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else /* WIN32*/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte openlog("HBAAPI", LOG_PID|LOG_ODELAY ,LOG_USER);\
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _hbaapi_debug_fd = fopen("/tmp/HBAAPI.log", "a");\
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(_hbaapi_debug_fd, (STR ## "\n"), (A1), (A2), (A3));\
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif /* WIN32*/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else /* Not both USESYSLOG and USELOGFILE */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte openlog("HBAAPI", LOG_PID|LOG_ODELAY ,LOG_USER);\
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif /* USESYSLOG */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#else /* WIN32 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _hbaapi_debug_fd = fopen("/tmp/HBAAPI.log", "a");\
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte fprintf(_hbaapi_debug_fd, (STR) ## "\n", (A1), (A2), (A3));\
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif /* WIN32 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif /* USELOGFILE */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif /* Not both USELOGFILE and USESYSLOG */
a79493184c9332129c9c91500069322f6f3fafddReed * When multiple mutex's are grabed, they must be always be grabbed in
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the same order, or deadlock can result. There are three levels
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * of mutex's involved in this API. If LL_mutex is grabbed, always grap
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * it first. If AL_mutex is grabbed, it may not be grabbed before
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * LL_mutex. If grabbed in a multi grab sequence, the mutex's protecting
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the callback lists must always be grabbed last and release before calling
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * a vendor specific library function that might invoke a callback function
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * on the same thread.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define RELEASE_MUTEX_RETURN(M,RET) release_mutex(M); return(RET)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define RELEASE_MUTEX_RETURN(m, RET) LeaveCriticalSection(m); return(RET)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * HBA_LIBRARY_STATUS and HBA_LIBRARY_INFO are redefined here.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Avoid any change in the common code.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef enum {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef struct hba_library_info {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte HBA_ENTRYPOINTSV2 functionTable; /* Function pointers */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte HBA_LIBRARY_STATUS status; /* info on this library */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Function type def fop Sun extentions.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef HBA_UINT32 (* Sun_HBAGetNumberOfTgtAdaptersFunc)();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef HBA_STATUS (* Sun_HBAGetTgtAdapterNameFunc)(HBA_UINT32, char *);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef HBA_HANDLE (* Sun_HBAOpenTgtAdapterFunc)(char *);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef HBA_STATUS (* Sun_HBAOpenTgtAdapterByWWNFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef HBA_STATUS (* Sun_HBANPIVGetAdapterAttributesFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (HBA_HANDLE, HBA_UINT32, HBA_UINT32, HBA_NPIVATTRIBUTES *);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (HBA_HANDLE, HBA_UINT32, HBA_WWN, HBA_WWN, HBA_UINT32 *);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef HBA_STATUS (* Sun_HBAAdapterReturnWWNFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef HBA_STATUS (* Sun_HBAAdapterCreateWWNFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (HBA_HANDLE, HBA_UINT32, HBA_WWN *, HBA_WWN *, HBA_WWN *,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef HBA_STATUS (* Sun_HBAGetPortNPIVAttributesFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (HBA_HANDLE, HBA_UINT32, HBA_PORTNPIVATTRIBUTES *);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortetypedef HBA_STATUS (* Sun_HBARegisterForAdapterDeviceEventsFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (void (*)(void *, HBA_WWN, HBA_UINT32, HBA_UINT32),
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte void *, HBA_HANDLE, HBA_WWN, HBA_CALLBACKHANDLE *);
a79493184c9332129c9c91500069322f6f3fafddReedtypedef HBA_STATUS (* Sun_HBADoForceLipFunc)(HBA_HANDLE, int *);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Individual adapter (hba) information
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Same as hbaadapter with different structure name.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte HBA_STATUS GNstatus; /* status from GetTgtAdapterNameFunc */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Make the list as an array with max size 16
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortepthread_mutex_t _hbaapi_tgtAL_mutex = PTHREAD_MUTEX_INITIALIZER;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common library internal. Mutex handling
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DEBUG(0, "pthread_mutex_lock returned %d", ret, 0, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DEBUG(0, "pthread_mutex_unlock returned %d", ret, 0, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The API used to use fixed size tables as its primary data structure.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Indexing from 1 to N identified each adapters. Now the adapters are
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * on a linked list. There is a unique "index" foreach each adapter.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Adapters always keep their index, even if they are removed from the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * hardware. The only time the indexing is reset is on HBA_FreeLibrary
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte GRAB_MUTEX(&_hbaapi_LL_mutex); /* pay attention to order */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte GetNumberOfTgtAdaptersFunc = (Sun_HBAGetNumberOfTgtAdaptersFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcGetNumberOfTgtAdapters");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte GetTgtAdapterNameFunc = (Sun_HBAGetTgtAdapterNameFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcGetTgtAdapterName");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte GetNumberOfTgtAdaptersFunc = GetTgtAdapterNameFunc = NULL;
a79493184c9332129c9c91500069322f6f3fafddReed DEBUG(1, "HBAAPI: number of target mode adapters for %s = %d\n",
a79493184c9332129c9c91500069322f6f3fafddReed DEBUG(1, "HBAAPI: number of target mode_adapters for %s = %d\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (j = 0; j < num_adapters; j++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte status = (GetTgtAdapterNameFunc)(j, (char *)&adaptername);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * check for duplicates, really, this may just be a second
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * call to this function
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * ??? how do we know when a name becomes stale?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if(strcmp(adaptername, adapt_infop->name) == 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* already got this one */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "HBA_GetNumberOfAdapters: calloc failed on sizeof:%d\n",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if((adapt_infop->GNstatus = status) == HBA_STATUS_OK) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lib_infop->LibraryPath, _hbaapi_total_tgtadapter_count);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte adapt_infop->index = _hbaapi_total_tgtadapter_count;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte GetNumberOfTgtAdaptersFunc = GetTgtAdapterNameFunc = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, _hbaapi_total_tgtadapter_count);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The adapter index is from old code, but we have
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * to support it. Go down the list looking for
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the adapter
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DEBUG(2, "GetAdapterName for index:%d ->%s", adapterindex, adaptername, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (strcmp(adaptername, adapt_infop->name) != 0) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcOpenTgtAdapter");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* retrieve the vendor handle */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* or this with the library index to get the common handle */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte handle = HBA_HANDLE_FROM_LOCAL(lib_infop->index, handle);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * This function ignores the list of known adapters and instead tries
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * each vendors open function to see if one of them
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * can open an adapter when referenced with a particular WWN
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteSun_HBA_OpenTgtAdapterByWWN(HBA_HANDLE *phandle, HBA_WWN nodeWWN)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DEBUG(2, "OpenAdapterByWWN: %s", WWN2STR1(&nodeWWN), 0, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte GetNumberOfTgtAdaptersFunc = (Sun_HBAGetNumberOfTgtAdaptersFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcGetNumberOfTgtAdapters");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte OpenTgtAdapterByWWNFunc = (Sun_HBAOpenTgtAdapterByWWNFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcOpenTgtAdapterByWWN");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte GetNumberOfTgtAdaptersFunc = OpenTgtAdapterByWWNFunc = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if((status = (OpenTgtAdapterByWWNFunc)(&handle, nodeWWN))
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte GetNumberOfTgtAdaptersFunc = OpenTgtAdapterByWWNFunc = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* OK, make a vendor non-specific handle */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *phandle = HBA_HANDLE_FROM_LOCAL(lib_infop->index, handle);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte GetNumberOfTgtAdaptersFunc = OpenTgtAdapterByWWNFunc = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* caller will release the mutex */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, HBA_STATUS_ERROR_INVALID_HANDLE);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte status = HBA_NPIV_CheckLibrary(handle, &lib_infop, &vendorHandle); \
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Sun_HBANPIVGetAdapterAttributesFunc NPIVGetAdapterAttributesFunc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DEBUG(2, "HBA_NPIVGetAdapterAttributes", 0, 0, 0);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte NPIVGetAdapterAttributesFunc = (Sun_HBANPIVGetAdapterAttributesFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcNPIVGetAdapterAttributes");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte status = ((NPIVGetAdapterAttributesFunc)(vendorHandle,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte GetNPIVPortInfoFunc = (Sun_HBAGetNPIVPortInfoFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcGetNPIVPortInfo");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte status = ((GetNPIVPortInfoFunc)(vendorHandle, portindex,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcDeleteNPIVPort");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcCreateNPIVPort");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Sun_HBAGetPortNPIVAttributesFunc GetPortNPIVAttributesFunc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte GetPortNPIVAttributesFunc = (Sun_HBAGetPortNPIVAttributesFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcGetPortNPIVAttributes");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Sun_HBAAdapterCreateWWNFunc AdapterCreateWWNFunc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte AdapterCreateWWNFunc = (Sun_HBAAdapterCreateWWNFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcAdapterCreateWWN");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Sun_HBAAdapterReturnWWNFunc AdapterReturnWWNFunc;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte AdapterReturnWWNFunc = (Sun_HBAAdapterReturnWWNFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte dlsym(lib_infop->hLibrary, "Sun_fcAdapterReturnWWN");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteextern HBA_NPIVADAPTERCALLBACK_ELEM *_hbaapi_adapterdeviceevents_callback_list;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/* Adapter Device Events ********************************************************/
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte DEBUG(3, "AdapterDeviceEvent, port:%s, eventType:%d fabricPortID:0X%06x",
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for(acbp = _hbaapi_adapterdeviceevents_callback_list;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte (*acbp->callback)(acbp->userdata, PortWWN, eventType, fabricPortID);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte registeredfunc = (Sun_HBARegisterForAdapterDeviceEventsFunc)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte "Sun_fcRegisterForAdapterDeviceEvents");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, HBA_STATUS_ERROR_NOT_SUPPORTED);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte RELEASE_MUTEX_RETURN(&_hbaapi_LL_mutex, HBA_STATUS_ERROR);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte status = (registeredfunc)(adapterdeviceevents_callback,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte acbp->next = _hbaapi_adapterdeviceevents_callback_list;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte _hbaapi_adapterdeviceevents_callback_list = acbp;