fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER START
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * The contents of this file are subject to the terms of the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Common Development and Distribution License (the "License").
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You may not use this file except in compliance with the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * or http://www.opensolaris.org/os/licensing.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * See the License for the specific language governing permissions
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and limitations under the License.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * When distributing Covered Code, include this CDDL HEADER in each
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * If applicable, add the following below this CDDL HEADER, with the
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * fields enclosed by brackets "[]" replaced with your own identifying
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * information: Portions Copyright [yyyy] [name of copyright owner]
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * CDDL HEADER END
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/*
a79493184c9332129c9c91500069322f6f3fafddReed * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Use is subject to license terms.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "Handle.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "Exceptions.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include "Trace.h"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <libdevinfo.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <iostream>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <iomanip>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/types.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <sys/stat.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <fcntl.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <unistd.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#include <stropts.h>
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define MAX_INIT_HANDLE_ID 0x7fff
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#define MAX_TGT_HANDLE_ID 0xffff
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forteusing namespace std;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Global lock for list of Handles
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortepthread_mutex_t Handle::staticLock = PTHREAD_MUTEX_INITIALIZER;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Tracking for the previous handle we have opened
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHBA_HANDLE Handle::prevOpen = 0;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Tracking for the previous target HBA handle we have opened
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHBA_HANDLE Handle::prevTgtOpen = 0x8000;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Global map from HBA_HANDLE to Handle pointers (our global list)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortemap<HBA_HANDLE, Handle*> Handle::openHandles;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Create a new open handle for a specified HBA
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @precondition HBA port(s) must be loaded
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @postcondition An open handle will be present in the global tracking list
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and must be closed at some point to prevent leakage. If no
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * handle could be assigned (the track list is full), an
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * exception will be thrown. Scope for valid ids in the track
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * list is [1, MAX_INIT_HANDLE_ID].
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param myhba The HBA to open a handle for
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHandle::Handle(HBA *myhba) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte map<HBA_HANDLE, Handle*>::iterator mapend;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::Handle");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte modeVal = INITIATOR;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mapend = openHandles.end();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Start the search for a free id from the previously assigned one */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte id = prevOpen + 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (id != prevOpen) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Exceeds the max valid value, continue the search from 1 */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (id > MAX_INIT_HANDLE_ID)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte id = 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (openHandles.find(id) == mapend) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* the id is not in use */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte id ++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (id == prevOpen) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* no usable id for now */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw TryAgainException();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte prevOpen = id;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hba = myhba;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte openHandles[id] = this;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Create a new open handle for a specified HBA
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @precondition HBA port(s) must be loaded
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @postcondition An open handle will be present in the global tracking list
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * and must be closed at some point to prevent leakage. If no
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * handle could be assigned (the track list is full), an
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * exception will be thrown. Scope for valid ids in the track
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * list is [0x8000, MAX_TGT_HANDLE_ID].
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param myhba The HBA to open a handle for
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * m The mode of HBA to open handle for
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#if 0
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte// appears unused
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHandle::Handle(HBA *myhba, MODE m) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte map<HBA_HANDLE, Handle*>::iterator mapend;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::Handle");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte modeVal = m;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // if initiator mode call constructor for initiator.
a79493184c9332129c9c91500069322f6f3fafddReed if (m == INITIATOR) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Handle(myhba, TARGET);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte mapend = openHandles.end();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* Start the search for a free id from the previously assigned one */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte id = prevTgtOpen + 1;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte while (id != prevTgtOpen) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /*
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Exceeds the max valid target id value,
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * continue the search from 1.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (id > MAX_TGT_HANDLE_ID)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte id = 0x8001;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (openHandles.find(id) == mapend) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* the id is not in use */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte break;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte id ++;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (id == prevTgtOpen) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte /* no usable id for now */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw TryAgainException();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte prevTgtOpen = id;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hba = myhba;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte openHandles[id] = this;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte#endif
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Free up the handle (aka, close it)
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @postcondition This handle will be removed from the global list
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @exception ... underlying exceptions will be thrown
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHandle::~Handle() {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::~Handle");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // Remove this handle from the global list
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte try {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte openHandles.erase(openHandles.find(getHandle()));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } catch (...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // Now nuke all internal dynamic allocations
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte typedef map<uint64_t, HandlePort *>::const_iterator CI;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte try {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (CI port = portHandles.begin(); port != portHandles.end();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte port++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte delete port->second;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte portHandles.clear();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } catch (...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Locate a handle in the global list of open handles
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @precondition The requested handle must already be open
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @exception InvalidHandleException Thrown if the id does not match
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * an open handle
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @return The open Handle
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param id The id of the handle to fetch
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @doc The HBA API uses a simple integer type to represent
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * an open Handle, but we use an instance of the Handle
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * class. This interface allows a caller to quickly convert
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * from the API integer value to related the Handle instance.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHandle* Handle::findHandle(HBA_HANDLE id) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::findHandle(id)");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Handle *tmp = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte try {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (openHandles.find(id) == openHandles.end()) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw InvalidHandleException();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tmp = openHandles[id];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (tmp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } catch (...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Find an open handle based on Node or Port WWN
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @precondition The given HBA must already be open
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @exception IllegalWWNException Thrown if no matching open Handle found
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @return The open handle matching the wwn argument
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param wwn The Node or Port WWN of the HBA whos open handle
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * is requested.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHandle* Handle::findHandle(uint64_t wwn) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::findHandle(wwn)");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Handle *tmp = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte try {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (int i = 0; i < openHandles.size(); i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tmp = openHandles[i];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (tmp->getHBA()->containsWWN(wwn)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (tmp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tmp = NULL;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } catch (...) { tmp = NULL; }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock(&staticLock);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (tmp == NULL) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw IllegalWWNException();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (tmp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Refresh underlying index values
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @postcondition All HandlePorts will be reset and prior index values
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * will be undefined.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @exception ... underlying exceptions will be thrown
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @doc A number of APIs in the standard interface require
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * the use of index values for identifying what "thing"
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * to operate on. When dynamic reconfiguration occurs
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * these indexes may become inconsistent. This routine
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * is called to reset the indexes and signify that the caller
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * no longer holds or will refer to any old indexes.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid Handle::refresh() {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::refresh");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte try {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte typedef map<uint64_t, HandlePort *>::const_iterator CI;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (CI port = portHandles.begin(); port != portHandles.end();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte port++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte port->second->refresh();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } catch (...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Close the specified handle
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @precondition The handle must be open
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @postcondition The handle will be closed and should be discarded.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param id The handle to close
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortevoid Handle::closeHandle(HBA_HANDLE id) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::closeHandle");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Handle *myHandle = findHandle(id);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte delete myHandle;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Get the integer value for return to the API
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @exception ... underlying exceptions will be thrown
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @return The integer value representing the handle
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @doc The HBA API uses integer values to represent handles.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * Call this routine to convert a Handle instance into
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * its representative integer value.
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHBA_HANDLE Handle::getHandle() {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::getHandle");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte HBA_HANDLE tmp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte try {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte tmp = (HBA_HANDLE) id;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (tmp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } catch (...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Compare two handles for equality
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @return TRUE if the handles are the same
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @return FALSE if the handles are different
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Fortebool Handle::operator==(Handle comp) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::operator==");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (this->id == comp.id);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Get the underlying Handle port based on index
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @return The Handle port for the given port index
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param index The index of the desired port
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHandlePort* Handle::getHandlePortByIndex(int index) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::getHandlePortByIndex");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte HBAPort* port = hba->getPortByIndex(index);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (getHandlePort(port->getPortWWN()));
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Get the underlying Handle port based on Port wwn
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @exception IllegalWWNException thrown if the wwn is not found
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @return The handle port for the specified WWN
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @param wwn The Port WWN of the HBA port
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHandlePort* Handle::getHandlePort(uint64_t wwn) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::getHandlePort");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte try {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // Check to see if the wwn is in the map
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (portHandles.find(wwn) == portHandles.end()) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // Not found, add a new one
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte HBAPort* port = hba->getPort(wwn);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte portHandles[wwn] = new HandlePort(this, hba, port);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte HandlePort *portHandle = portHandles[wwn];
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (portHandle);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } catch (...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Get the HBA attributes from the underlying HBA
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @see HBA::getHBAAttributes
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHBA_ADAPTERATTRIBUTES Handle::getHBAAttributes() {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::getHBAAttributes");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte try {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte HBA_ADAPTERATTRIBUTES attributes = hba->getHBAAttributes();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (attributes);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } catch (...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
a79493184c9332129c9c91500069322f6f3fafddReed/**
a79493184c9332129c9c91500069322f6f3fafddReed * @memo Do FORCELIP
a79493184c9332129c9c91500069322f6f3fafddReed *
a79493184c9332129c9c91500069322f6f3fafddReed * @see HBA::doForceLip
a79493184c9332129c9c91500069322f6f3fafddReed */
a79493184c9332129c9c91500069322f6f3fafddReedint Handle::doForceLip() {
a79493184c9332129c9c91500069322f6f3fafddReed Trace log("Handle::doForceLip");
a79493184c9332129c9c91500069322f6f3fafddReed lock();
a79493184c9332129c9c91500069322f6f3fafddReed try {
a79493184c9332129c9c91500069322f6f3fafddReed int rval = hba->doForceLip();
a79493184c9332129c9c91500069322f6f3fafddReed unlock();
a79493184c9332129c9c91500069322f6f3fafddReed return (rval);
a79493184c9332129c9c91500069322f6f3fafddReed } catch (...) {
a79493184c9332129c9c91500069322f6f3fafddReed unlock();
a79493184c9332129c9c91500069322f6f3fafddReed throw;
a79493184c9332129c9c91500069322f6f3fafddReed }
a79493184c9332129c9c91500069322f6f3fafddReed}
a79493184c9332129c9c91500069322f6f3fafddReed
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHBA_ADAPTERATTRIBUTES Handle::npivGetHBAAttributes() {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::npivGetHBAAttributes");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte try {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte HBA_ADAPTERATTRIBUTES attributes = hba->npivGetHBAAttributes();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (attributes);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } catch (...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte/**
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @memo Get the HBA port attributes from the HBA
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @see HBAPort::getPortAttributes
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @see HBAPort::getDisoveredAttributes
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * @doc This routine will return either HBA port
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte * attributes, or discovered port attributes
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte *
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte */
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn ForteHBA_PORTATTRIBUTES Handle::getPortAttributes(uint64_t wwn) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte Trace log("Handle::getPortAttributes");
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte uint64_t tmp;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte HBA_PORTATTRIBUTES attributes;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte lock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte try {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // Is this a WWN for one of the adapter ports?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte if (hba->containsWWN(wwn)) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte attributes = hba->getPort(wwn)->getPortAttributes(tmp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (attributes);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } else { // Is this a target we know about?
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // Loop through all ports and look for the first match
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte for (int i = 0; i < hba->getNumberOfPorts(); i++) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte try {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte attributes =
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte hba->getPortByIndex(i)->getDiscoveredAttributes(
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte wwn, tmp);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte return (attributes);
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } catch (HBAException &e) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte continue;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte // If we get to here, then we don't see this WWN on this HBA
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw IllegalWWNException();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte } catch (...) {
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte unlock();
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte throw;
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte }
fcf3ce441efd61da9bb2884968af01cb7c1452ccJohn Forte}