ZFCP HBA API Library 1
vlib_aux.c File Reference

Auxiliary functions used in the library. More...

#include "vlib.h"

Functions

static int block_assertSize (struct block *block, const size_t size, const size_t num, const size_t grow)
 Assert that there is space for at least num elements in the block.
static void * block_addItem (struct block *block, size_t size, size_t grow)
 Add a new item to a block.
void block_free (struct block *block)
 Free the array contained in a struct block.
struct vlib_adaptergetAdapterByIndex (uint32_t index)
 Get an adapter by its index.
struct vlib_adaptergetAdapterByHandle (HBA_HANDLE handle, HBA_STATUS *status)
 Get an adapter by its handle.
struct vlib_adaptergetAdapterByDevid (devid_t devid)
 Get an adapter by its devid.
struct vlib_adaptergetAdapterByHostNo (unsigned short host)
 Get an adapter by SCSI Host number as in sysfs.
struct vlib_portgetPortByIndex (const struct vlib_adapter *adapter, const uint32_t index)
 Get a port by its index.
struct vlib_portgetPortByWWPN (const struct vlib_adapter *adapter, const wwn_t wwpn)
 Get a port by its WWPN.
struct vlib_unitgetUnitByIndex (const struct vlib_port *port, const uint32_t index)
 Get an unit by its index.
struct vlib_unitgetUnitByFcLun (const struct vlib_port *port, uint64_t fcLun)
 Get an unit by its fclun.
static struct vlib_unitgetUnitFromRepos (struct vlib_port *port, struct vlib_unit *unit)
 Check if an unit specified in an unit event is already stored in the repository.
int addUnitToRepos (struct vlib_port *port, struct vlib_unit *unit)
 Add a unit to the repository.
static struct vlib_portgetPortFromRepos (struct vlib_adapter *adapter, char *sysfs_name)
 Check if a port specified is already stored in the repository.
int addPortToRepos (struct vlib_adapter *adapter, struct vlib_port *port)
 Add a port from to the repository.
static struct vlib_adaptergetAdapterFromRepos (char *bus_dev_name)
 Check if an adapter specified in an event is already stored in the repository.
int addAdapterToRepos (struct vlib_adapter *adapter)
 Add an adapter to the repository.
int updateAdapter (struct vlib_adapter *adapter)
 Update information about ports and units of an adapter.
int revalidateAdapters (void)
 Revalidate adapters in the repository.
int findIndexByName (char *name)
 Find an adapter index by name.
HBA_HANDLE openAdapterByIndex (HBA_UINT32 index)
 Open an adapter by index.
void doCloseAdapter (struct vlib_adapter *adapter)
 Close an adapter in the repository.
void closeAllAdapters (void)
 Close all adapters in the repository.
HBA_PORTTYPE vlibCharToIntPortType (char *portType)
 Map the result of a port type string from sysfs to an int.
HBA_PORTSTATE vlibCharToIntPortState (char *portState)
 Map the result of a port state string from sysfs to an int.
HBA_PORTSPEED vlibIntToSymbolPortSpeed (int speed)
 Maps the result of a port speed int to the HBA_PORTSPEED int.
HBA_PORTSPEED vlibCharToIntPortSpeed (char *pS)
 Map the result of a port speed string to the HBA_PORTSPEED int.
int vlibCOStoFlag (int class)
 Maps the number of a class of service to its bit flag according to FC-GS-4.
HBA_COS vlibCharToIntCOS (char *s)
 Map the result of a class of service string to an int.
char * getSgDevFromPort (struct vlib_port *port)
 Get the first sg device from an adapter.
char * getAttachedWLUN (struct vlib_adapter *adapter, struct vlib_port *port)
 Try to attach the report luns wlun and return its name as in "/dev".
void detachWLUN (struct vlib_adapter *adapter, struct vlib_port *port)
 Try to detach lun 0.

Detailed Description

Auxiliary functions used in the library.

Function Documentation

◆ addAdapterToRepos()

int addAdapterToRepos ( struct vlib_adapter * adapter)

Add an adapter to the repository.

Parameters
*adapteradapter
Returns
  • -1 on error
  • 0 on success
Locks:
vlib_data.mutex must be held

If the adapter specified in the event is already stored in the repository it is marked as valid.

References vlib_data::adapters, block_addItem(), vlib_adapter_ident::bus_dev_name, vlib_adapter_ident::class_dev_name, vlib_adapter_ident::devid, getAdapterFromRepos(), vlib_adapter::handle, vlib_adapter_ident::host, vlib_adapter::ident, vlib_adapter::isInvalid, vlib_adapter_ident::sysfsPath, VLIB_INVALID_HANDLE, vlib_adapter_ident::wwnn, and vlib_adapter_ident::wwpn.

Referenced by addAdapterByDevPath().

◆ addPortToRepos()

int addPortToRepos ( struct vlib_adapter * adapter,
struct vlib_port * port )

Add a port from to the repository.

Parameters
*adapterto which the port data should be added, if NULL is passed the adapter should be identified by the data in the event
*portport to be added
Returns
  • -1 on error
  • 0 on success
Locks:
vlib_data.mutex must be held

If the port specified in the event is already stored in the repository it is marked as valid.

References block_addItem(), vlib_port::channel, vlib_port::did, getPortFromRepos(), vlib_port::host, vlib_port::isInvalid, vlib_port::name, vlib_adapter::ports, vlib_port::target, vlib_port::wwnn, and vlib_port::wwpn.

Referenced by addPortByName().

◆ addUnitToRepos()

int addUnitToRepos ( struct vlib_port * port,
struct vlib_unit * unit )

Add a unit to the repository.

Parameters
*portto which the unit data should be added, if NULL is passed the port should be identified by the data in the event
*unitunit to be aded
Returns
  • -1 on error
  • 0 on success
Locks:
vlib_data.mutex must be held

If the unit specified in the event is already stored in the repository it is marked as valid.

References block_addItem(), getUnitFromRepos(), vlib_unit::isInvalid, and vlib_port::units.

Referenced by sysfs_getUnitsFromPort().

◆ block_addItem()

void * block_addItem ( struct block * block,
size_t size,
size_t grow )
static

Add a new item to a block.

Parameters
*blockpointer to struct block (contains array of elements of passed size)
sizeof the contained structure
growchunk size (The array grows by chunks of size (grow * size).)
Returns
  • NULL on error
  • pointer to new item on success

If the new item does not fit in the array, the array is enlarged.

References block_assertSize(), block::data, and block::used.

Referenced by addAdapterToRepos(), addPortToRepos(), and addUnitToRepos().

◆ block_assertSize()

int block_assertSize ( struct block * block,
const size_t size,
const size_t num,
const size_t grow )
static

Assert that there is space for at least num elements in the block.

Parameters
*blockpointer to struct block (contains array of elements of passed size)
sizeof the contained structure
numminimum number of elements in the block
growchunk size (The array grows by chunks of size (grow * size).)
Returns
  • -ENOMEM if out of memory
  • total number of elements in the block

References block::allocated, block::data, and VLIB_PERROR.

Referenced by block_addItem().

◆ block_free()

void block_free ( struct block * block)

Free the array contained in a struct block.

Parameters
*blockpointer to a struct block

References block::allocated, block::data, and block::used.

Referenced by closeAllAdapters(), and doCloseAdapter().

◆ closeAllAdapters()

void closeAllAdapters ( void )

Close all adapters in the repository.

Locks:
vlib_data.mutex must be held

This function frees all allocated memory for the adapters.

References vlib_data::adapters, block_free(), doCloseAdapter(), getAdapterByIndex(), and block::used.

Referenced by HBA_FreeLibrary().

◆ detachWLUN()

void detachWLUN ( struct vlib_adapter * adapter,
struct vlib_port * port )

Try to detach lun 0.

Parameters
adapter*Pointer to an adapter
port*Pointer to a port
Note
This function issues a system call to detach lun0

References vlib_adapter_ident::bus_dev_name, vlib_unit::channel, getUnitByIndex(), vlib_unit::host, vlib_adapter::ident, REPORTLUNS_WLUN, revalidateUnits(), vlib_unit::target, and vlib_port::wwpn.

Referenced by _HBA_SendReportLUNs().

◆ doCloseAdapter()

void doCloseAdapter ( struct vlib_adapter * adapter)

Close an adapter in the repository.

Parameters
*adapterpointer to the adapter to be closed
Locks:
vlib_data.mutex must be held

This function frees all allocated memory for the ports and units of this adapter and invalidates the adapter handle.

References block_free(), getPortByIndex(), vlib_adapter::handle, vlib_adapter::ports, vlib_port::units, block::used, and VLIB_INVALID_HANDLE.

Referenced by closeAllAdapters(), HBA_CloseAdapter(), and revalidateAdapters().

◆ findIndexByName()

int findIndexByName ( char * name)

Find an adapter index by name.

Parameters
namethe name of the adapter
Returns
  • -1 on error.
  • index of adapter on success @parm Locks: vlib_data.mutex must be held
Note
If adapter.isInvalid we do not return an adapter index - although the name might be generated. At the moment this is not an issue, because currently this flag is not really set for adapters within configured within zfcp. But it might be an issue in the future, if adapters can be removed from within zfcp.

References vlib_data::adapters, getAdapterByIndex(), vlib_adapter::isInvalid, block::used, VLIB_ADAPTERNAME_LEN, and VLIB_ADAPTERNAME_PREFIX.

Referenced by HBA_OpenAdapter().

◆ getAdapterByDevid()

struct vlib_adapter * getAdapterByDevid ( devid_t devid)

Get an adapter by its devid.

Parameters
devidof the adapter
Returns
  • NULL if no such adapter exists
  • pointer to found adapter
Locks:
vlib_data.mutex must be held

References vlib_data::adapters, vlib_adapter_ident::devid, getAdapterByIndex(), vlib_adapter::ident, and block::used.

◆ getAdapterByHandle()

struct vlib_adapter * getAdapterByHandle ( HBA_HANDLE handle,
HBA_STATUS * status )

Get an adapter by its handle.

Parameters
handleof the adapter
*statuspointer to return error status code
Returns
  • NULL on error (*status contains error status code)
  • pointer to adapter structure on success
Locks:
vlib_data.mutex must be held

If NULL is returned *status contains an error status code which should be checked by the calling function. If non-NULL is returned *status is HBA_STATUS_OK. Possible error status codes are:

  • HBA_STATUS_ERROR_INVALID_HANDLE if handle is invalid
  • HBA_STATUS_ERROR_UNAVAILABLE if adapter is unavailable

References getAdapterByIndex(), vlib_adapter::handle, vlib_adapter::isInvalid, and VLIB_INVALID_HANDLE.

Referenced by _HBA_SendReadCapacity(), _HBA_SendReportLUNs(), _HBA_SendScsiInquiry(), HBA_CloseAdapter(), HBA_GetAdapterAttributes(), HBA_GetAdapterPortAttributes(), HBA_GetDiscoveredPortAttributes(), HBA_GetEventBuffer(), HBA_GetFcpTargetMapping(), HBA_GetFcpTargetMappingV2(), HBA_GetPortStatistics(), HBA_GetRNIDMgmtInfo(), HBA_RefreshInformation(), HBA_SendCTPassThru(), and HBA_SendRNID().

◆ getAdapterByHostNo()

struct vlib_adapter * getAdapterByHostNo ( unsigned short host)

Get an adapter by SCSI Host number as in sysfs.

Parameters
SCSIhost number of the adapter
Returns
  • NULL if no such adapter exists
  • pointer to found adapter
Locks:
vlib_data.mutex must be held

References vlib_data::adapters, getAdapterByIndex(), vlib_adapter_ident::host, vlib_adapter::ident, and block::used.

Referenced by sysfs_getUnitsFromPort().

◆ getAdapterByIndex()

struct vlib_adapter * getAdapterByIndex ( uint32_t index)

Get an adapter by its index.

Parameters
indexof the adapter
Returns
  • NULL if index is out of range
  • pointer to found adapter on success @parm Locks: vlib_data.mutex must be held

References vlib_data::adapters, block::data, and block::used.

Referenced by closeAllAdapters(), findIndexByName(), getAdapterByDevid(), getAdapterByHandle(), getAdapterByHostNo(), getAdapterFromRepos(), HBA_GetAdapterName(), invalidateAllAdapters(), openAdapterByIndex(), and revalidateAdapters().

◆ getAdapterFromRepos()

struct vlib_adapter * getAdapterFromRepos ( char * bus_dev_name)
static

Check if an adapter specified in an event is already stored in the repository.

Parameters
*bus_dev_namename of adapter as in the sysfs bus dev name
Returns
  • pointer to found adapter
  • NULL if adapter is not yet in the repository
Locks:
vlib_data.mutex must be held

References vlib_data::adapters, vlib_adapter_ident::bus_dev_name, getAdapterByIndex(), vlib_adapter::ident, and block::used.

Referenced by addAdapterToRepos().

◆ getAttachedWLUN()

char * getAttachedWLUN ( struct vlib_adapter * adapter,
struct vlib_port * port )

Try to attach the report luns wlun and return its name as in "/dev".

Parameters
adapter*Pointer to an adapter
port*Pointer to a port
Note
This function issues a system call to attach lun0

References vlib_adapter_ident::bus_dev_name, getSgDevFromPort(), vlib_adapter::ident, REPORTLUNS_WLUN, and vlib_port::wwpn.

Referenced by _HBA_SendReportLUNs().

◆ getPortByIndex()

struct vlib_port * getPortByIndex ( const struct vlib_adapter * adapter,
const uint32_t index )

Get a port by its index.

Parameters
*adapterto which the port belongs
indexof the port
Returns
  • NULL if index is out of range
  • pointer to found port on success
Locks:
vlib_Data.mutex must be held

References block::data, vlib_adapter::ports, and block::used.

Referenced by doCloseAdapter(), getPortByWWPN(), getPortFromRepos(), HBA_GetDiscoveredPortAttributes(), HBA_GetFcpTargetMapping(), and updateAdapter().

◆ getPortByWWPN()

struct vlib_port * getPortByWWPN ( const struct vlib_adapter * adapter,
const wwn_t wwpn )

Get a port by its WWPN.

Parameters
*adapterto which the port belongs
wwpnof the port
Returns
  • NULL if no port with such a WWPN exists
  • pointer to found port on success
Locks:
vlib_data.mutex must be held

References getPortByIndex(), vlib_adapter::ports, block::used, and vlib_port::wwpn.

Referenced by _HBA_SendReadCapacity(), _HBA_SendReportLUNs(), and _HBA_SendScsiInquiry().

◆ getPortFromRepos()

struct vlib_port * getPortFromRepos ( struct vlib_adapter * adapter,
char * sysfs_name )
static

Check if a port specified is already stored in the repository.

Parameters
*adapterto which this port belongs
*portthe new port for whose existence is checked
Returns
  • NULL if no such port exists for the adapter in the repository

References getPortByIndex(), vlib_port::name, vlib_adapter::ports, and block::used.

Referenced by addPortToRepos().

◆ getSgDevFromPort()

char * getSgDevFromPort ( struct vlib_port * port)

Get the first sg device from an adapter.

Parameters
port*Pointer to a port
Note
This function looks for the first lun belonging to the first port of the adapter and returns its sg device

References getUnitByIndex(), revalidateUnits(), and vlib_unit::sg_dev.

Referenced by _HBA_SendReportLUNs(), and getAttachedWLUN().

◆ getUnitByFcLun()

struct vlib_unit * getUnitByFcLun ( const struct vlib_port * port,
uint64_t fcLun )

Get an unit by its fclun.

Parameters
*portto which the unit belongs
indexof the unit
Returns
  • NULL if index is out of range
  • pointer to found unit on success
Locks:
vlib_data.mutex must be held

References vlib_unit::fcLun, getUnitByIndex(), vlib_port::units, and block::used.

Referenced by _HBA_SendReadCapacity(), and _HBA_SendScsiInquiry().

◆ getUnitByIndex()

struct vlib_unit * getUnitByIndex ( const struct vlib_port * port,
const uint32_t index )

Get an unit by its index.

Parameters
*portto which the unit belongs
indexof the unit
Returns
  • NULL if index is out of range
  • pointer to found unit on success
Locks:
vlib_data.mutex must be held

References block::data, vlib_port::units, and block::used.

Referenced by detachWLUN(), getSgDevFromPort(), getUnitByFcLun(), getUnitFromRepos(), and HBA_GetFcpTargetMapping().

◆ getUnitFromRepos()

struct vlib_unit * getUnitFromRepos ( struct vlib_port * port,
struct vlib_unit * unit )
static

Check if an unit specified in an unit event is already stored in the repository.

Parameters
*portto which this unit belongs
*eventunit add event
Returns
  • NULL if no such unit exists for the port in the repository
  • pointer to found unit

References vlib_unit::fcLun, getUnitByIndex(), vlib_port::units, and block::used.

Referenced by addUnitToRepos().

◆ openAdapterByIndex()

HBA_HANDLE openAdapterByIndex ( HBA_UINT32 index)

Open an adapter by index.

Parameters
indexof the adapter
Returns
  • VLIB_INVALID_HANDLE if index is invalid
  • handle to the adapter on success
Locks:
vlib_data.mutex must be held

If compiled as a vendor library, we shall only use the lower 16 Bit of the handle.

References getAdapterByIndex(), vlib_adapter::handle, and VLIB_INVALID_HANDLE.

Referenced by HBA_OpenAdapter().

◆ revalidateAdapters()

int revalidateAdapters ( void )

Revalidate adapters in the repository.

Returns
  • -1 on error
  • 0 on success
Locks:
vlib_data.mutex must be held

Port and unit configuration data is only updated if it was already generated before. Generation of port and unit configuration information is triggered in HBA_GetAdapterPortAttributes() and HBA_GetFcpTargetMapping(), resp.

References vlib_data::adapters, block::allocated, doCloseAdapter(), getAdapterByIndex(), vlib_adapter::isInvalid, vlib_adapter::ports, updateAdapter(), and block::used.

Referenced by sysfs_createAndReadConfigAdapter().

◆ updateAdapter()

int updateAdapter ( struct vlib_adapter * adapter)

Update information about ports and units of an adapter.

Parameters
*adapterto be updated
Returns
  • -1 on error
  • 0 on success
Locks:
vlib_data.mutex must be held
Note
Additionally this function triggers creation of unit configuration for this adapter (see getUnitsFromPort()).

References getPortByIndex(), vlib_adapter::isInvalid, vlib_adapter::ports, sysfs_createAndReadConfigPorts(), sysfs_getUnitsFromPort(), and block::used.

Referenced by HBA_RefreshInformation(), and revalidateAdapters().

◆ vlibCharToIntCOS()

HBA_COS vlibCharToIntCOS ( char * s)

Map the result of a class of service string to an int.

Parameters
char*containing the class of service string
Note
This function maps the class of service from sysfs which is in the form "Class2, Class3" to its integer representation via bit flags

References vlibCOStoFlag().

Referenced by getPortAttributes().

◆ vlibCharToIntPortSpeed()

HBA_PORTSPEED vlibCharToIntPortSpeed ( char * pS)

Map the result of a port speed string to the HBA_PORTSPEED int.

Parameters
char*containing the port speed string
Note
This function maps the port speed from the sysfs (which is in the form "1 Gbit, 2 Gbit" to an int storing a flag for each port speed

References vlibIntToSymbolPortSpeed().

Referenced by getPortAttributes().

◆ vlibCharToIntPortState()

HBA_PORTSTATE vlibCharToIntPortState ( char * portState)

Map the result of a port state string from sysfs to an int.

Parameters
char*containing the port state
Note
This function maps the port states from the sysfs attribute (which are strings) to their number as defined in hbaapi.h

Referenced by getPortAttributes().

◆ vlibCharToIntPortType()

HBA_PORTTYPE vlibCharToIntPortType ( char * portType)

Map the result of a port type string from sysfs to an int.

Parameters
char*containing the port type
Note
This function maps the port types from the sysfs attribute (which are strings) to their number as defined in hbaapi.h

Referenced by getPortAttributes().

◆ vlibCOStoFlag()

int vlibCOStoFlag ( int class)

Maps the number of a class of service to its bit flag according to FC-GS-4.

Parameters
intholding the class number
Note
Maps the number of a class of service to its bit flag according to FC-GS-4

Referenced by vlibCharToIntCOS().

◆ vlibIntToSymbolPortSpeed()

HBA_PORTSPEED vlibIntToSymbolPortSpeed ( int speed)

Maps the result of a port speed int to the HBA_PORTSPEED int.

Parameters
intholding the port speed
Note
This function maps the port speed in integer form (e.g. 1) to the HBA API flag (e.g. HBA_PORTSPEED_1GBIT)

Referenced by vlibCharToIntPortSpeed().