xref: /onnv-gate/usr/src/lib/sun_fc/common/HandlePort.cc (revision 7836:4e95154b5b7a)
1*7836SJohn.Forte@Sun.COM /*
2*7836SJohn.Forte@Sun.COM  * CDDL HEADER START
3*7836SJohn.Forte@Sun.COM  *
4*7836SJohn.Forte@Sun.COM  * The contents of this file are subject to the terms of the
5*7836SJohn.Forte@Sun.COM  * Common Development and Distribution License (the "License").
6*7836SJohn.Forte@Sun.COM  * You may not use this file except in compliance with the License.
7*7836SJohn.Forte@Sun.COM  *
8*7836SJohn.Forte@Sun.COM  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7836SJohn.Forte@Sun.COM  * or http://www.opensolaris.org/os/licensing.
10*7836SJohn.Forte@Sun.COM  * See the License for the specific language governing permissions
11*7836SJohn.Forte@Sun.COM  * and limitations under the License.
12*7836SJohn.Forte@Sun.COM  *
13*7836SJohn.Forte@Sun.COM  * When distributing Covered Code, include this CDDL HEADER in each
14*7836SJohn.Forte@Sun.COM  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7836SJohn.Forte@Sun.COM  * If applicable, add the following below this CDDL HEADER, with the
16*7836SJohn.Forte@Sun.COM  * fields enclosed by brackets "[]" replaced with your own identifying
17*7836SJohn.Forte@Sun.COM  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7836SJohn.Forte@Sun.COM  *
19*7836SJohn.Forte@Sun.COM  * CDDL HEADER END
20*7836SJohn.Forte@Sun.COM  */
21*7836SJohn.Forte@Sun.COM /*
22*7836SJohn.Forte@Sun.COM  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*7836SJohn.Forte@Sun.COM  * Use is subject to license terms.
24*7836SJohn.Forte@Sun.COM  */
25*7836SJohn.Forte@Sun.COM 
26*7836SJohn.Forte@Sun.COM 
27*7836SJohn.Forte@Sun.COM 
28*7836SJohn.Forte@Sun.COM #include "HandlePort.h"
29*7836SJohn.Forte@Sun.COM #include "Exceptions.h"
30*7836SJohn.Forte@Sun.COM #include "Trace.h"
31*7836SJohn.Forte@Sun.COM #include <iostream>
32*7836SJohn.Forte@Sun.COM #include <iomanip>
33*7836SJohn.Forte@Sun.COM #include <sys/types.h>
34*7836SJohn.Forte@Sun.COM #include <sys/stat.h>
35*7836SJohn.Forte@Sun.COM #include <fcntl.h>
36*7836SJohn.Forte@Sun.COM #include <unistd.h>
37*7836SJohn.Forte@Sun.COM #include <stropts.h>
38*7836SJohn.Forte@Sun.COM 
39*7836SJohn.Forte@Sun.COM 
40*7836SJohn.Forte@Sun.COM using namespace std;
41*7836SJohn.Forte@Sun.COM 
42*7836SJohn.Forte@Sun.COM /**
43*7836SJohn.Forte@Sun.COM  * @memo	    Construct a new HandlePort for state tracking
44*7836SJohn.Forte@Sun.COM  * @precondition    Handle must be open
45*7836SJohn.Forte@Sun.COM  * @param	    myHandle The open handle for this HBA
46*7836SJohn.Forte@Sun.COM  * @param	    myHBA The HBA for this port
47*7836SJohn.Forte@Sun.COM  * @param	    myPort The HBA Port to open
48*7836SJohn.Forte@Sun.COM  */
HandlePort(Handle * myHandle,HBA * myHBA,HBAPort * myPort)49*7836SJohn.Forte@Sun.COM HandlePort::HandlePort(Handle *myHandle, HBA *myHBA, HBAPort *myPort) :
50*7836SJohn.Forte@Sun.COM 	handle(myHandle), hba(myHBA), port(myPort), active(false) {
51*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::HandlePort");
52*7836SJohn.Forte@Sun.COM }
53*7836SJohn.Forte@Sun.COM 
54*7836SJohn.Forte@Sun.COM /**
55*7836SJohn.Forte@Sun.COM  * @memo	    Reset the state tracking values for stale index detection
56*7836SJohn.Forte@Sun.COM  * @postcondition   The first subsequent call to any index based routine
57*7836SJohn.Forte@Sun.COM  *		    will always succed.
58*7836SJohn.Forte@Sun.COM  */
refresh()59*7836SJohn.Forte@Sun.COM void HandlePort::refresh() {
60*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::refresh");
61*7836SJohn.Forte@Sun.COM 	lock();
62*7836SJohn.Forte@Sun.COM 	active = false;
63*7836SJohn.Forte@Sun.COM 	unlock();
64*7836SJohn.Forte@Sun.COM }
65*7836SJohn.Forte@Sun.COM 
66*7836SJohn.Forte@Sun.COM /**
67*7836SJohn.Forte@Sun.COM  * @memo	    Validate the current state of the handle port
68*7836SJohn.Forte@Sun.COM  * @exception	    StaleDataException Thrown if the state has changed
69*7836SJohn.Forte@Sun.COM  * @param	    newState The new state of the port
70*7836SJohn.Forte@Sun.COM  *
71*7836SJohn.Forte@Sun.COM  * @doc		    After opening a port or refreshing, no state is tracked.
72*7836SJohn.Forte@Sun.COM  *		    The first time validate is called, the state is recorded.
73*7836SJohn.Forte@Sun.COM  *		    Subsequent calls will verify that the state is the same.
74*7836SJohn.Forte@Sun.COM  *		    If the state has changed, the exception will be thrown.
75*7836SJohn.Forte@Sun.COM  */
validate(uint64_t newState)76*7836SJohn.Forte@Sun.COM void HandlePort::validate(uint64_t newState) {
77*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::validate");
78*7836SJohn.Forte@Sun.COM 	log.debug("Port %016llx state %016llx", port->getPortWWN(), newState);
79*7836SJohn.Forte@Sun.COM 	lock();
80*7836SJohn.Forte@Sun.COM 	if (active) {
81*7836SJohn.Forte@Sun.COM 	    if (lastState != newState) {
82*7836SJohn.Forte@Sun.COM 		unlock();
83*7836SJohn.Forte@Sun.COM 		throw StaleDataException();
84*7836SJohn.Forte@Sun.COM 	    }
85*7836SJohn.Forte@Sun.COM 	} else {
86*7836SJohn.Forte@Sun.COM 	    active = true;
87*7836SJohn.Forte@Sun.COM 	    lastState = newState;
88*7836SJohn.Forte@Sun.COM 	}
89*7836SJohn.Forte@Sun.COM 	unlock();
90*7836SJohn.Forte@Sun.COM }
91*7836SJohn.Forte@Sun.COM 
92*7836SJohn.Forte@Sun.COM /**
93*7836SJohn.Forte@Sun.COM  * @memo	    Verify this port has the stated port wwn
94*7836SJohn.Forte@Sun.COM  * @return	    TRUE if the argument matches this port
95*7836SJohn.Forte@Sun.COM  * @return	    FALSE if the argument does not match this port
96*7836SJohn.Forte@Sun.COM  * @param	    portWWN The Port WWN to compare against this port
97*7836SJohn.Forte@Sun.COM  */
match(uint64_t portWWN)98*7836SJohn.Forte@Sun.COM bool HandlePort::match(uint64_t portWWN) {
99*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::match(wwn)");
100*7836SJohn.Forte@Sun.COM 	bool ret = false;
101*7836SJohn.Forte@Sun.COM 	ret = (portWWN == port->getPortWWN());
102*7836SJohn.Forte@Sun.COM 	return (ret);
103*7836SJohn.Forte@Sun.COM }
104*7836SJohn.Forte@Sun.COM 
105*7836SJohn.Forte@Sun.COM /**
106*7836SJohn.Forte@Sun.COM  * @memo	    Verify this port is the stated index
107*7836SJohn.Forte@Sun.COM  * @return	    TRUE if the argument matches this port
108*7836SJohn.Forte@Sun.COM  * @return	    FALSE if the argument does not match this port
109*7836SJohn.Forte@Sun.COM  * @param	    index The index value to compare against this port
110*7836SJohn.Forte@Sun.COM  */
match(int index)111*7836SJohn.Forte@Sun.COM bool HandlePort::match(int index) {
112*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::match(index)");
113*7836SJohn.Forte@Sun.COM 	return (*port == *(hba->getPortByIndex(index)));
114*7836SJohn.Forte@Sun.COM }
115*7836SJohn.Forte@Sun.COM 
116*7836SJohn.Forte@Sun.COM /**
117*7836SJohn.Forte@Sun.COM  * @memo	    Get attributes from a discovered port.
118*7836SJohn.Forte@Sun.COM  * @exception	    ... underlying exceptions will be thrown
119*7836SJohn.Forte@Sun.COM  * @return	    The discovered port attributes
120*7836SJohn.Forte@Sun.COM  * @param	    wwn The node or port wwn of the discovered port
121*7836SJohn.Forte@Sun.COM  *
122*7836SJohn.Forte@Sun.COM  * @doc		    This routine will not perform any state validation
123*7836SJohn.Forte@Sun.COM  */
getDiscoveredAttributes(uint64_t wwn)124*7836SJohn.Forte@Sun.COM HBA_PORTATTRIBUTES HandlePort::getDiscoveredAttributes(uint64_t wwn) {
125*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::getDiscoveredAttributes(wwn)");
126*7836SJohn.Forte@Sun.COM 	uint64_t newState;
127*7836SJohn.Forte@Sun.COM 	HBA_PORTATTRIBUTES attributes = port->getDiscoveredAttributes(
128*7836SJohn.Forte@Sun.COM 		wwn, newState);
129*7836SJohn.Forte@Sun.COM 	// We don't validate when a WWN was used
130*7836SJohn.Forte@Sun.COM 	return (attributes);
131*7836SJohn.Forte@Sun.COM }
132*7836SJohn.Forte@Sun.COM 
133*7836SJohn.Forte@Sun.COM /**
134*7836SJohn.Forte@Sun.COM  * @memo	    Get attributes from this port.
135*7836SJohn.Forte@Sun.COM  * @exception	    ... underlying exceptions will be thrown
136*7836SJohn.Forte@Sun.COM  * @return	    The port attributes
137*7836SJohn.Forte@Sun.COM  * @see		    HandlePort::validate
138*7836SJohn.Forte@Sun.COM  *
139*7836SJohn.Forte@Sun.COM  * @doc		    This routine will perform state validation
140*7836SJohn.Forte@Sun.COM  */
getPortAttributes()141*7836SJohn.Forte@Sun.COM HBA_PORTATTRIBUTES HandlePort::getPortAttributes() {
142*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::getPortAttributes");
143*7836SJohn.Forte@Sun.COM 	uint64_t newState;
144*7836SJohn.Forte@Sun.COM 	HBA_PORTATTRIBUTES attributes = port->getPortAttributes(newState);
145*7836SJohn.Forte@Sun.COM 	validate(newState);
146*7836SJohn.Forte@Sun.COM 	return (attributes);
147*7836SJohn.Forte@Sun.COM }
148*7836SJohn.Forte@Sun.COM 
149*7836SJohn.Forte@Sun.COM /**
150*7836SJohn.Forte@Sun.COM  * @memo	    Get attributes from a discovered port.
151*7836SJohn.Forte@Sun.COM  * @exception	    ... underlying exceptions will be thrown
152*7836SJohn.Forte@Sun.COM  * @return	    The discovered port attributes
153*7836SJohn.Forte@Sun.COM  * @param	    discoveredport The index of the discovered port
154*7836SJohn.Forte@Sun.COM  * @see		    HandlePort::validate
155*7836SJohn.Forte@Sun.COM  *
156*7836SJohn.Forte@Sun.COM  * @doc		    This routine will perform state validation
157*7836SJohn.Forte@Sun.COM  */
158*7836SJohn.Forte@Sun.COM HBA_PORTATTRIBUTES
getDiscoveredAttributes(HBA_UINT32 discoveredport)159*7836SJohn.Forte@Sun.COM HandlePort::getDiscoveredAttributes(HBA_UINT32 discoveredport) {
160*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::getDiscoveredAttributes(index)");
161*7836SJohn.Forte@Sun.COM 	uint64_t newState;
162*7836SJohn.Forte@Sun.COM 	HBA_PORTATTRIBUTES attributes = port->getDiscoveredAttributes(
163*7836SJohn.Forte@Sun.COM 		discoveredport, newState);
164*7836SJohn.Forte@Sun.COM 	validate(newState);
165*7836SJohn.Forte@Sun.COM 	return (attributes);
166*7836SJohn.Forte@Sun.COM }
167*7836SJohn.Forte@Sun.COM 
getPortNPIVAttributes()168*7836SJohn.Forte@Sun.COM HBA_PORTNPIVATTRIBUTES HandlePort::getPortNPIVAttributes() {
169*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::getPortNPIVAttributes");
170*7836SJohn.Forte@Sun.COM 	uint64_t newState;
171*7836SJohn.Forte@Sun.COM 	HBA_PORTNPIVATTRIBUTES attributes = port->getPortNPIVAttributes(newState);
172*7836SJohn.Forte@Sun.COM 	validate(newState);
173*7836SJohn.Forte@Sun.COM 	return (attributes);
174*7836SJohn.Forte@Sun.COM }
175*7836SJohn.Forte@Sun.COM 
deleteNPIVPort(uint64_t vportwwn)176*7836SJohn.Forte@Sun.COM uint32_t HandlePort::deleteNPIVPort(uint64_t vportwwn) {
177*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::deleteNPIVPort");
178*7836SJohn.Forte@Sun.COM 	uint32_t ret = port->deleteNPIVPort(vportwwn);
179*7836SJohn.Forte@Sun.COM 
180*7836SJohn.Forte@Sun.COM 	return (ret);
181*7836SJohn.Forte@Sun.COM }
182*7836SJohn.Forte@Sun.COM 
createNPIVPort(uint64_t vnodewwn,uint64_t vportwwn,uint32_t vindex)183*7836SJohn.Forte@Sun.COM uint32_t HandlePort::createNPIVPort(uint64_t vnodewwn,
184*7836SJohn.Forte@Sun.COM     uint64_t vportwwn, uint32_t vindex) {
185*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::createNPIVPort");
186*7836SJohn.Forte@Sun.COM 	uint32_t vportindex;
187*7836SJohn.Forte@Sun.COM 
188*7836SJohn.Forte@Sun.COM 	vportindex = port->createNPIVPort(vnodewwn, vportwwn, vindex);
189*7836SJohn.Forte@Sun.COM 	return (vportindex);
190*7836SJohn.Forte@Sun.COM }
191*7836SJohn.Forte@Sun.COM 
getHandleNPIVPortByIndex(int index)192*7836SJohn.Forte@Sun.COM HandleNPIVPort* HandlePort::getHandleNPIVPortByIndex(int index) {
193*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::getHandleNPIVPortByIndex(int index)");
194*7836SJohn.Forte@Sun.COM 
195*7836SJohn.Forte@Sun.COM 	HBANPIVPort* vport = port->getPortByIndex(index);
196*7836SJohn.Forte@Sun.COM 	return (getHandleNPIVPort(vport->getPortWWN()));
197*7836SJohn.Forte@Sun.COM }
198*7836SJohn.Forte@Sun.COM 
getHandleNPIVPort(uint64_t wwn)199*7836SJohn.Forte@Sun.COM HandleNPIVPort* HandlePort::getHandleNPIVPort(uint64_t wwn) {
200*7836SJohn.Forte@Sun.COM 	Trace log("HandlePort::getHandleNPIVPort");
201*7836SJohn.Forte@Sun.COM 	lock();
202*7836SJohn.Forte@Sun.COM 	try {
203*7836SJohn.Forte@Sun.COM 		// Check to see if the wwn is in the map
204*7836SJohn.Forte@Sun.COM 		if (npivportHandles.find(wwn) == npivportHandles.end()) {
205*7836SJohn.Forte@Sun.COM 			// Not found, add a new one
206*7836SJohn.Forte@Sun.COM 			HBANPIVPort* vport = port->getPort(wwn);
207*7836SJohn.Forte@Sun.COM 			npivportHandles[wwn] = new HandleNPIVPort(handle, this, hba, port, vport);
208*7836SJohn.Forte@Sun.COM 		}
209*7836SJohn.Forte@Sun.COM 		HandleNPIVPort *npivportHandle = npivportHandles[wwn];
210*7836SJohn.Forte@Sun.COM 		unlock();
211*7836SJohn.Forte@Sun.COM 		return (npivportHandle);
212*7836SJohn.Forte@Sun.COM 	} catch (...) {
213*7836SJohn.Forte@Sun.COM 		unlock();
214*7836SJohn.Forte@Sun.COM 		throw;
215*7836SJohn.Forte@Sun.COM 	}
216*7836SJohn.Forte@Sun.COM }
217*7836SJohn.Forte@Sun.COM 
218