2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A
2N/A
2N/A#include <TgtFCHBAPort.h>
2N/A#include <Exceptions.h>
2N/A#include <Trace.h>
2N/A#include <sun_fc.h>
2N/A#include <iostream>
2N/A#include <iomanip>
2N/A#include <sys/types.h>
2N/A#include <sys/mkdev.h>
2N/A#include <sys/stat.h>
2N/A#include <fcntl.h>
2N/A#include <unistd.h>
2N/A#include <stropts.h>
2N/A#include <dirent.h>
2N/A#include <sys/fibre-channel/fc.h>
2N/A#include <sys/fctio.h>
2N/A#include <sys/fibre-channel/impl/fc_error.h>
2N/A#include <sys/fibre-channel/fc_appif.h>
2N/A#include <sys/scsi/generic/commands.h>
2N/A#include <sys/scsi/impl/commands.h>
2N/A#include <sys/scsi/impl/sense.h>
2N/A#include <sys/scsi/generic/inquiry.h>
2N/A#include <sys/scsi/generic/status.h>
2N/A#include <errno.h>
2N/A
2N/A
2N/Ausing namespace std;
2N/A
2N/Aconst int TgtFCHBAPort::MAX_FCTIO_MSG_LEN = 256;
2N/Aconst string TgtFCHBAPort::FCT_DRIVER_PATH = "/devices/pseudo/fct@0:admin";
2N/A
2N/A/*
2N/A * Interpret the error code in the fctio_t structure
2N/A *
2N/A * message must be at least MAX_FCTIO_MSG_LEN in length.
2N/A */
2N/Avoid
2N/ATgtFCHBAPort::transportError(uint32_t fctio_errno, char *message) {
2N/A Trace log("transportError");
2N/A string fcioErrorString;
2N/A if (message == NULL) {
2N/A log.internalError("NULL routine argument");
2N/A return;
2N/A }
2N/A switch (fctio_errno) {
2N/A case (uint32_t)FC_FAILURE:
2N/A fcioErrorString = "general failure";
2N/A break;
2N/A case (uint32_t)FC_FAILURE_SILENT:
2N/A fcioErrorString = "general failure but fail silently";
2N/A break;
2N/A case FC_SUCCESS:
2N/A fcioErrorString = "successful completion";
2N/A break;
2N/A case FC_CAP_ERROR:
2N/A fcioErrorString = "FCA capability error";
2N/A break;
2N/A case FC_CAP_FOUND:
2N/A fcioErrorString = "FCA capability unsettable";
2N/A break;
2N/A case FC_CAP_SETTABLE:
2N/A fcioErrorString = "FCA capability settable";
2N/A break;
2N/A case FC_UNBOUND:
2N/A fcioErrorString = "unbound stuff";
2N/A break;
2N/A case FC_NOMEM:
2N/A fcioErrorString = "allocation error";
2N/A break;
2N/A case FC_BADPACKET:
2N/A fcioErrorString = "invalid packet specified/supplied";
2N/A break;
2N/A case FC_OFFLINE:
2N/A fcioErrorString = "I/O resource unavailable";
2N/A break;
2N/A case FC_OLDPORT:
2N/A fcioErrorString = "operation on non-loop port";
2N/A break;
2N/A case FC_NO_MAP:
2N/A fcioErrorString = "requested map unavailable";
2N/A break;
2N/A case FC_TRANSPORT_ERROR:
2N/A fcioErrorString = "unable to transport I/O";
2N/A break;
2N/A case FC_ELS_FREJECT:
2N/A fcioErrorString = "ELS rejected by a Fabric";
2N/A break;
2N/A case FC_ELS_PREJECT:
2N/A fcioErrorString = "ELS rejected by an N_port";
2N/A break;
2N/A case FC_ELS_BAD:
2N/A fcioErrorString = "ELS rejected by FCA/fctl";
2N/A break;
2N/A case FC_ELS_MALFORMED:
2N/A fcioErrorString = "poorly formed ELS request";
2N/A break;
2N/A case FC_TOOMANY:
2N/A fcioErrorString = "resource request too large";
2N/A break;
2N/A case FC_UB_BADTOKEN:
2N/A fcioErrorString = "invalid unsolicited buffer token";
2N/A break;
2N/A case FC_UB_ERROR:
2N/A fcioErrorString = "invalid unsol buf request";
2N/A break;
2N/A case FC_UB_BUSY:
2N/A fcioErrorString = "buffer already in use";
2N/A break;
2N/A case FC_BADULP:
2N/A fcioErrorString = "Unknown ulp";
2N/A break;
2N/A case FC_BADTYPE:
2N/A fcioErrorString = "ULP not registered to handle this FC4 type";
2N/A break;
2N/A case FC_UNCLAIMED:
2N/A fcioErrorString = "request or data not claimed";
2N/A break;
2N/A case FC_ULP_SAMEMODULE:
2N/A fcioErrorString = "module already in use";
2N/A break;
2N/A case FC_ULP_SAMETYPE:
2N/A fcioErrorString = "FC4 module already in use";
2N/A break;
2N/A case FC_ABORTED:
2N/A fcioErrorString = "request aborted";
2N/A break;
2N/A case FC_ABORT_FAILED:
2N/A fcioErrorString = "abort request failed";
2N/A break;
2N/A case FC_BADEXCHANGE:
2N/A fcioErrorString = "exchange doesn�t exist";
2N/A break;
2N/A case FC_BADWWN:
2N/A fcioErrorString = "WWN not recognized";
2N/A break;
2N/A case FC_BADDEV:
2N/A fcioErrorString = "device unrecognized";
2N/A break;
2N/A case FC_BADCMD:
2N/A fcioErrorString = "invalid command issued";
2N/A break;
2N/A case FC_BADOBJECT:
2N/A fcioErrorString = "invalid object requested";
2N/A break;
2N/A case FC_BADPORT:
2N/A fcioErrorString = "invalid port specified";
2N/A break;
2N/A case FC_NOTTHISPORT:
2N/A fcioErrorString = "resource not at this port";
2N/A break;
2N/A case FC_PREJECT:
2N/A fcioErrorString = "reject at remote N_Port";
2N/A break;
2N/A case FC_FREJECT:
2N/A fcioErrorString = "reject at remote Fabric";
2N/A break;
2N/A case FC_PBUSY:
2N/A fcioErrorString = "remote N_Port busy";
2N/A break;
2N/A case FC_FBUSY:
2N/A fcioErrorString = "remote Fabric busy";
2N/A break;
2N/A case FC_ALREADY:
2N/A fcioErrorString = "already logged in";
2N/A break;
2N/A case FC_LOGINREQ:
2N/A fcioErrorString = "login required";
2N/A break;
2N/A case FC_RESETFAIL:
2N/A fcioErrorString = "reset failed";
2N/A break;
2N/A case FC_INVALID_REQUEST:
2N/A fcioErrorString = "request is invalid";
2N/A break;
2N/A case FC_OUTOFBOUNDS:
2N/A fcioErrorString = "port number is out of bounds";
2N/A break;
2N/A case FC_TRAN_BUSY:
2N/A fcioErrorString = "command transport busy";
2N/A break;
2N/A case FC_STATEC_BUSY:
2N/A fcioErrorString = "port driver currently busy";
2N/A break;
2N/A case FC_DEVICE_BUSY:
2N/A fcioErrorString = "transport working on this device";
2N/A break;
2N/A case FC_DEVICE_NOT_TGT:
2N/A fcioErrorString = "device is not a SCSI target";
2N/A break;
2N/A default:
2N/A snprintf(message, MAX_FCTIO_MSG_LEN, "Unknown error code 0x%x",
2N/A fctio_errno);
2N/A return;
2N/A }
2N/A snprintf(message, MAX_FCTIO_MSG_LEN, "%s", fcioErrorString.c_str());
2N/A}
2N/A
2N/ATgtFCHBAPort::TgtFCHBAPort(string thePath) : HBAPort() {
2N/A Trace log("TgtFCHBAPort::TgtFCHBAPort");
2N/A log.debug("Initializing HBA port %s", path.c_str());
2N/A path = thePath;
2N/A
2N/A // This routine is not index based, so we can discard stateChange
2N/A uint64_t tmp;
2N/A HBA_PORTATTRIBUTES attrs = getPortAttributes(tmp);
2N/A memcpy(&tmp, &attrs.PortWWN, 8);
2N/A portWWN = ntohll(tmp);
2N/A memcpy(&tmp, &attrs.NodeWWN, 8);
2N/A nodeWWN = ntohll(tmp);
2N/A
2N/A // For reference, here's how to dump WWN's through C++ streams.
2N/A // cout << "\tPort WWN: " << hex << setfill('0') << setw(16) << portWWN
2N/A // << endl;
2N/A // cout << "\tNode WWN: " << hex << setfill('0') << setw(16) << nodeWWN
2N/A // << endl;
2N/A}
2N/A
2N/AHBA_PORTATTRIBUTES TgtFCHBAPort::getPortAttributes(uint64_t &stateChange) {
2N/A Trace log("TgtFCHBAPort::getPortAttributes");
2N/A
2N/A HBA_PORTATTRIBUTES attributes;
2N/A fctio_t fctio;
2N/A fc_tgt_hba_port_attributes_t attrs;
2N/A
2N/A memset(&fctio, 0, sizeof (fctio));
2N/A memset(&attributes, 0, sizeof (attributes));
2N/A
2N/A uint64_t portwwn = 0;
2N/A try {
2N/A string::size_type offset = path.find_last_of(".");
2N/A if (offset >= 0) {
2N/A string portwwnString = path.substr(offset+1);
2N/A portwwn = strtoull(portwwnString.c_str(), NULL, 16);
2N/A }
2N/A } catch (...) {
2N/A throw BadArgumentException();
2N/A }
2N/A
2N/A uint64_t en_wwn = htonll(portwwn);
2N/A
2N/A fctio.fctio_cmd = FCTIO_GET_ADAPTER_PORT_ATTRIBUTES;
2N/A fctio.fctio_ilen = 8;
2N/A fctio.fctio_ibuf = (uint64_t)(uintptr_t)&en_wwn;
2N/A fctio.fctio_xfer = FCTIO_XFER_READ;
2N/A fctio.fctio_olen = (uint32_t)(sizeof (attrs));
2N/A fctio.fctio_obuf = (uint64_t)(uintptr_t)&attrs;
2N/A
2N/A fct_ioctl(FCTIO_CMD, &fctio);
2N/A
2N/A stateChange = attrs.lastChange;
2N/A
2N/A attributes.PortFcId = attrs.PortFcId;
2N/A attributes.PortType = attrs.PortType;
2N/A attributes.PortState = attrs.PortState;
2N/A attributes.PortSupportedClassofService = attrs.PortSupportedClassofService;
2N/A attributes.PortSupportedSpeed = attrs.PortSupportedSpeed;
2N/A attributes.PortSpeed = attrs.PortSpeed;
2N/A attributes.PortMaxFrameSize = attrs.PortMaxFrameSize;
2N/A attributes.NumberofDiscoveredPorts = attrs.NumberofDiscoveredPorts;
2N/A memcpy(&attributes.NodeWWN, &attrs.NodeWWN, 8);
2N/A memcpy(&attributes.PortWWN, &attrs.PortWWN, 8);
2N/A memcpy(&attributes.FabricName, &attrs.FabricName, 8);
2N/A memcpy(&attributes.PortSupportedFc4Types, &attrs.PortSupportedFc4Types, 32);
2N/A memcpy(&attributes.PortActiveFc4Types, &attrs.PortActiveFc4Types, 32);
2N/A memcpy(&attributes.PortSymbolicName, &attrs.PortSymbolicName, 256);
2N/A
2N/A strncpy((char *)attributes.OSDeviceName, "Not Applicable", 15);
2N/A return (attributes);
2N/A}
2N/A
2N/AHBA_PORTATTRIBUTES TgtFCHBAPort::getDiscoveredAttributes(
2N/A HBA_UINT32 discoveredport, uint64_t &stateChange) {
2N/A Trace log("TgtFCHBAPort::getDiscoverdAttributes(i)");
2N/A
2N/A HBA_PORTATTRIBUTES attributes;
2N/A fctio_t fctio;
2N/A fc_tgt_hba_port_attributes_t attrs;
2N/A
2N/A memset(&fctio, 0, sizeof (fctio));
2N/A memset(&attributes, 0, sizeof (attributes));
2N/A
2N/A uint64_t portwwn = 0;
2N/A try {
2N/A string::size_type offset = path.find_last_of(".");
2N/A if (offset >= 0) {
2N/A string portwwnString = path.substr(offset+1);
2N/A portwwn = strtoull(portwwnString.c_str(), NULL, 16);
2N/A }
2N/A } catch (...) {
2N/A throw BadArgumentException();
2N/A }
2N/A
2N/A uint64_t en_wwn = htonll(portwwn);
2N/A
2N/A fctio.fctio_cmd = FCTIO_GET_DISCOVERED_PORT_ATTRIBUTES;
2N/A fctio.fctio_ilen = 8;
2N/A fctio.fctio_ibuf = (uint64_t)(uintptr_t)&en_wwn;
2N/A fctio.fctio_xfer = FCTIO_XFER_READ;
2N/A fctio.fctio_olen = (uint32_t)(sizeof (attrs));
2N/A fctio.fctio_obuf = (uint64_t)(uintptr_t)&attrs;
2N/A fctio.fctio_alen = (uint32_t)(sizeof (discoveredport));
2N/A fctio.fctio_abuf = (uint64_t)(uintptr_t)&discoveredport;
2N/A
2N/A fct_ioctl(FCTIO_CMD, &fctio);
2N/A
2N/A stateChange = attrs.lastChange;
2N/A
2N/A attributes.PortFcId = attrs.PortFcId;
2N/A attributes.PortType = attrs.PortType;
2N/A attributes.PortState = attrs.PortState;
2N/A attributes.PortSupportedClassofService = attrs.PortSupportedClassofService;
2N/A attributes.PortSupportedSpeed = attrs.PortSupportedSpeed;
2N/A attributes.PortSpeed = attrs.PortSpeed;
2N/A attributes.PortMaxFrameSize = attrs.PortMaxFrameSize;
2N/A attributes.NumberofDiscoveredPorts = attrs.NumberofDiscoveredPorts;
2N/A memcpy(&attributes.NodeWWN, &attrs.NodeWWN, 8);
2N/A memcpy(&attributes.PortWWN, &attrs.PortWWN, 8);
2N/A memcpy(&attributes.FabricName, &attrs.FabricName, 8);
2N/A memcpy(&attributes.PortSupportedFc4Types, &attrs.PortSupportedFc4Types, 32);
2N/A memcpy(&attributes.PortActiveFc4Types, &attrs.PortActiveFc4Types, 32);
2N/A memcpy(&attributes.PortSymbolicName, &attrs.PortSymbolicName, 256);
2N/A
2N/A
2N/A return (attributes);
2N/A}
2N/A
2N/AHBA_PORTATTRIBUTES TgtFCHBAPort::getDiscoveredAttributes(
2N/A uint64_t wwn, uint64_t &stateChange) {
2N/A Trace log("TgtFCHBAPort::getDiscoverdAttributes(p)");
2N/A
2N/A HBA_PORTATTRIBUTES attributes;
2N/A fctio_t fctio;
2N/A fc_tgt_hba_port_attributes_t attrs;
2N/A
2N/A memset(&fctio, 0, sizeof (fctio));
2N/A memset(&attributes, 0, sizeof (attributes));
2N/A
2N/A uint64_t en_wwn = htonll(wwn);
2N/A
2N/A fctio.fctio_cmd = FCTIO_GET_PORT_ATTRIBUTES;
2N/A fctio.fctio_olen = (uint32_t)(sizeof (attrs));
2N/A fctio.fctio_xfer = FCTIO_XFER_READ;
2N/A fctio.fctio_obuf = (uint64_t)(uintptr_t)&attrs;
2N/A fctio.fctio_ilen = (uint32_t)(sizeof (wwn));
2N/A fctio.fctio_ibuf = (uint64_t)(uintptr_t)&en_wwn;
2N/A
2N/A fct_ioctl(FCTIO_CMD, &fctio);
2N/A
2N/A stateChange = attrs.lastChange;
2N/A
2N/A attributes.PortFcId = attrs.PortFcId;
2N/A attributes.PortType = attrs.PortType;
2N/A attributes.PortState = attrs.PortState;
2N/A attributes.PortSupportedClassofService = attrs.PortSupportedClassofService;
2N/A attributes.PortSupportedSpeed = attrs.PortSupportedSpeed;
2N/A attributes.PortSpeed = attrs.PortSpeed;
2N/A attributes.PortMaxFrameSize = attrs.PortMaxFrameSize;
2N/A attributes.NumberofDiscoveredPorts = attrs.NumberofDiscoveredPorts;
2N/A memcpy(&attributes.NodeWWN, &attrs.NodeWWN, 8);
2N/A memcpy(&attributes.PortWWN, &attrs.PortWWN, 8);
2N/A memcpy(&attributes.FabricName, &attrs.FabricName, 8);
2N/A memcpy(&attributes.PortSupportedFc4Types, &attrs.PortSupportedFc4Types, 32);
2N/A memcpy(&attributes.PortActiveFc4Types, &attrs.PortActiveFc4Types, 32);
2N/A memcpy(&attributes.PortSymbolicName, &attrs.PortSymbolicName, 256);
2N/A
2N/A
2N/A return (attributes);
2N/A}
2N/A
2N/Avoid TgtFCHBAPort::sendRLS(uint64_t destWWN,
2N/A void *pRspBuffer,
2N/A HBA_UINT32 *pRspBufferSize) {
2N/A Trace log("FCHBAPort::sendRLS");
2N/A
2N/A fctio_t fctio;
2N/A // fc_hba_adapter_port_stats_t fc_port_stat;
2N/A uint64_t en_portWWN;
2N/A uint64_t DestPortID;
2N/A
2N/A // Validate the arguments
2N/A if (pRspBuffer == NULL ||
2N/A pRspBufferSize == NULL) {
2N/A log.userError("NULL hba");
2N/A throw BadArgumentException();
2N/A }
2N/A
2N/A // check to see if we are sending RLS to the HBA
2N/A HBA_PORTATTRIBUTES attrs;
2N/A uint64_t tmp;
2N/A portWWN = getPortWWN();
2N/A en_portWWN = htonll(portWWN);
2N/A
2N/A /* The destWWN is either the adapter port or a discovered port. */
2N/A memset(&fctio, 0, sizeof (fctio));
2N/A fctio.fctio_cmd = FCTIO_GET_LINK_STATUS;
2N/A fctio.fctio_ibuf = (uint64_t)(uintptr_t)&en_portWWN;
2N/A fctio.fctio_ilen = (uint32_t)(sizeof (en_portWWN));
2N/A if (portWWN != destWWN) {
2N/A attrs = getDiscoveredAttributes(destWWN, tmp);
2N/A DestPortID = (uint64_t)attrs.PortFcId;
2N/A fctio.fctio_abuf = (uint64_t)(uintptr_t)&DestPortID;
2N/A fctio.fctio_alen = (uint32_t)(sizeof (DestPortID));
2N/A }
2N/A fctio.fctio_xfer = FCTIO_XFER_READ;
2N/A fctio.fctio_flags = 0;
2N/A fctio.fctio_obuf = (uint64_t)(uintptr_t)new uchar_t[*pRspBufferSize];
2N/A fctio.fctio_olen = *pRspBufferSize;
2N/A
2N/A if (fctio.fctio_obuf == NULL) {
2N/A log.noMemory();
2N/A throw InternalError();
2N/A }
2N/A
2N/A fct_ioctl(FCTIO_CMD, &fctio);
2N/A memcpy(pRspBuffer, (uchar_t *)(uintptr_t)fctio.fctio_obuf,
2N/A *pRspBufferSize);
2N/A if (fctio.fctio_obuf != NULL) {
2N/A delete((uchar_t *)(uintptr_t)fctio.fctio_obuf);
2N/A }
2N/A}
2N/A
2N/A/**
2N/A * @memo Validate that the port is still present in the system
2N/A * @exception UnavailableException if the port is not present
2N/A * @version 1.7
2N/A *
2N/A * @doc If the port is still present on the system, the routine
2N/A * will return normally. If the port is not present
2N/A * an exception will be thrown.
2N/A */
2N/Avoid TgtFCHBAPort::validatePresent() {
2N/A Trace log("TgtFCHBAPort::validatePresent");
2N/A // We already got the adapter list through the ioctl
2N/A // so calling it again to validate it is too expensive.
2N/A}
2N/A
2N/Avoid TgtFCHBAPort::fct_ioctl(int cmd, fctio_t *fctio) {
2N/A Trace log("TgtFCHBAPort::fct_ioctl");
2N/A char fcioErrorString[MAX_FCTIO_MSG_LEN] = "";
2N/A int fd = HBA::_open(FCT_DRIVER_PATH, O_NDELAY | O_RDONLY);
2N/A try {
2N/A HBA::_ioctl(fd, cmd, (uchar_t *)fctio);
2N/A close(fd);
2N/A if (fctio->fctio_errno) {
2N/A throw IOError("IOCTL transport failure");
2N/A }
2N/A } catch (...) {
2N/A close(fd);
2N/A transportError(fctio->fctio_errno, fcioErrorString);
2N/A log.genericIOError("ioctl (0x%x) failed. Transport: \"%s\"", cmd,
2N/A fcioErrorString);
2N/A switch (fctio->fctio_errno) {
2N/A case FC_BADWWN:
2N/A throw IllegalWWNException();
2N/A case FC_BADPORT:
2N/A throw IllegalWWNException();
2N/A case FC_OUTOFBOUNDS:
2N/A throw IllegalIndexException();
2N/A case FC_PBUSY:
2N/A case FC_FBUSY:
2N/A case FC_TRAN_BUSY:
2N/A case FC_STATEC_BUSY:
2N/A case FC_DEVICE_BUSY:
2N/A throw BusyException();
2N/A case FC_SUCCESS:
2N/A default:
2N/A throw;
2N/A }
2N/A }
2N/A}