1*fcf3ce44SJohn Forte /*
2*fcf3ce44SJohn Forte  * CDDL HEADER START
3*fcf3ce44SJohn Forte  *
4*fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5*fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6*fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7*fcf3ce44SJohn Forte  *
8*fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10*fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11*fcf3ce44SJohn Forte  * and limitations under the License.
12*fcf3ce44SJohn Forte  *
13*fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14*fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16*fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17*fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18*fcf3ce44SJohn Forte  *
19*fcf3ce44SJohn Forte  * CDDL HEADER END
20*fcf3ce44SJohn Forte  */
21*fcf3ce44SJohn Forte /*
22*fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23*fcf3ce44SJohn Forte  * Use is subject to license terms.
24*fcf3ce44SJohn Forte  */
25*fcf3ce44SJohn Forte 
26*fcf3ce44SJohn Forte 
27*fcf3ce44SJohn Forte 
28*fcf3ce44SJohn Forte #include "HandleNPIVPort.h"
29*fcf3ce44SJohn Forte #include "Exceptions.h"
30*fcf3ce44SJohn Forte #include "Trace.h"
31*fcf3ce44SJohn Forte #include <iostream>
32*fcf3ce44SJohn Forte #include <iomanip>
33*fcf3ce44SJohn Forte #include <sys/types.h>
34*fcf3ce44SJohn Forte #include <sys/stat.h>
35*fcf3ce44SJohn Forte #include <fcntl.h>
36*fcf3ce44SJohn Forte #include <unistd.h>
37*fcf3ce44SJohn Forte #include <stropts.h>
38*fcf3ce44SJohn Forte 
39*fcf3ce44SJohn Forte using namespace std;
40*fcf3ce44SJohn Forte 
41*fcf3ce44SJohn Forte /**
42*fcf3ce44SJohn Forte  * @memo            Construct a new HandleNPIVPort for state tracking
43*fcf3ce44SJohn Forte  * @precondition    Handle must be open
44*fcf3ce44SJohn Forte  * @param           myHandle The open handle for this HBA
45*fcf3ce44SJohn Forte  * @param           myHandlePort The open handle for this HBA Port
46*fcf3ce44SJohn Forte  * @param           myHBA The HBA for this port
47*fcf3ce44SJohn Forte  * @param           myPort The HBA Port for this npiv port
48*fcf3ce44SJohn Forte  * @param           myvPort The NPIV port to open
49*fcf3ce44SJohn Forte  * @version         1.2
50*fcf3ce44SJohn Forte  */
HandleNPIVPort(Handle * myHandle,HandlePort * myHandlePort,HBA * myHBA,HBAPort * myPort,HBANPIVPort * myvPort)51*fcf3ce44SJohn Forte HandleNPIVPort::HandleNPIVPort(Handle *myHandle, HandlePort *myHandlePort,
52*fcf3ce44SJohn Forte     HBA *myHBA, HBAPort *myPort, HBANPIVPort *myvPort) :
53*fcf3ce44SJohn Forte     handle(myHandle), handleport(myHandlePort), hba(myHBA),
54*fcf3ce44SJohn Forte     port(myPort), active(false), vport(myvPort) {
55*fcf3ce44SJohn Forte 	Trace log("HandleNPIVPort::HandleNPIVPort");
56*fcf3ce44SJohn Forte }
57*fcf3ce44SJohn Forte 
58*fcf3ce44SJohn Forte /**
59*fcf3ce44SJohn Forte  * @memo            Reset the state tracking values for stale index detection
60*fcf3ce44SJohn Forte  * @postcondition   The first subsequent call to any index based routine
61*fcf3ce44SJohn Forte  *                  will always succed.
62*fcf3ce44SJohn Forte  * @version         1.2
63*fcf3ce44SJohn Forte  */
refresh()64*fcf3ce44SJohn Forte void HandleNPIVPort::refresh() {
65*fcf3ce44SJohn Forte 	Trace log("HandleNPIVPort::refresh");
66*fcf3ce44SJohn Forte 	lock();
67*fcf3ce44SJohn Forte 	active = false;
68*fcf3ce44SJohn Forte 	unlock();
69*fcf3ce44SJohn Forte }
70*fcf3ce44SJohn Forte 
71*fcf3ce44SJohn Forte /**
72*fcf3ce44SJohn Forte  * @memo            Validate the current state of the handle port
73*fcf3ce44SJohn Forte  * @exception       StaleDataException Thrown if the state has changed
74*fcf3ce44SJohn Forte  * @param           newState The new state of the port
75*fcf3ce44SJohn Forte  * @version         1.2
76*fcf3ce44SJohn Forte  *
77*fcf3ce44SJohn Forte  * @doc             After opening a port or refreshing, no state is tracked.
78*fcf3ce44SJohn Forte  *                  The first time validate is called, the state is recorded.
79*fcf3ce44SJohn Forte  *                  Subsequent calls will verify that the state is the same.
80*fcf3ce44SJohn Forte  *                  If the state has changed, the exception will be thrown.
81*fcf3ce44SJohn Forte  */
validate(uint64_t newState)82*fcf3ce44SJohn Forte void HandleNPIVPort::validate(uint64_t newState) {
83*fcf3ce44SJohn Forte 	Trace log("HandleNPIVPort::validate");
84*fcf3ce44SJohn Forte 	log.debug("Port %016llx state %016llx",
85*fcf3ce44SJohn Forte 	    vport->getPortWWN(), newState);
86*fcf3ce44SJohn Forte 	lock();
87*fcf3ce44SJohn Forte 	if (active) {
88*fcf3ce44SJohn Forte 		if (lastState != newState) {
89*fcf3ce44SJohn Forte 			unlock();
90*fcf3ce44SJohn Forte 			throw StaleDataException();
91*fcf3ce44SJohn Forte 		}
92*fcf3ce44SJohn Forte 	} else {
93*fcf3ce44SJohn Forte 		active = true;
94*fcf3ce44SJohn Forte 		lastState = newState;
95*fcf3ce44SJohn Forte 	}
96*fcf3ce44SJohn Forte 	unlock();
97*fcf3ce44SJohn Forte }
98*fcf3ce44SJohn Forte 
99*fcf3ce44SJohn Forte /**
100*fcf3ce44SJohn Forte  * @memo            Verify this port has the stated port wwn
101*fcf3ce44SJohn Forte  * @return          TRUE if the argument matches this port
102*fcf3ce44SJohn Forte  * @return          FALSE if the argument does not match this port
103*fcf3ce44SJohn Forte  * @param           portWWN The Port WWN to compare against this port
104*fcf3ce44SJohn Forte  * @version         1.2
105*fcf3ce44SJohn Forte  */
match(uint64_t portWWN)106*fcf3ce44SJohn Forte bool HandleNPIVPort::match(uint64_t portWWN) {
107*fcf3ce44SJohn Forte 	Trace log("HandleNPIVPort::match(wwn)");
108*fcf3ce44SJohn Forte 	bool ret = false;
109*fcf3ce44SJohn Forte 	ret = (portWWN == vport->getPortWWN());
110*fcf3ce44SJohn Forte 	return (ret);
111*fcf3ce44SJohn Forte }
112*fcf3ce44SJohn Forte 
113*fcf3ce44SJohn Forte /**
114*fcf3ce44SJohn Forte  * @memo            Verify this port is the stated index
115*fcf3ce44SJohn Forte  * @return          TRUE if the argument matches this port
116*fcf3ce44SJohn Forte  * @return          FALSE if the argument does not match this port
117*fcf3ce44SJohn Forte  * @param           index The index value to compare against this port
118*fcf3ce44SJohn Forte  * @version         1.2
119*fcf3ce44SJohn Forte  */
match(int index)120*fcf3ce44SJohn Forte bool HandleNPIVPort::match(int index) {
121*fcf3ce44SJohn Forte 	Trace log("HandleNPIVPort::match(index)");
122*fcf3ce44SJohn Forte 	return (*vport == *(port->getPortByIndex(index)));
123*fcf3ce44SJohn Forte }
124*fcf3ce44SJohn Forte 
125*fcf3ce44SJohn Forte /**
126*fcf3ce44SJohn Forte  * @memo            Get attributes from this port.
127*fcf3ce44SJohn Forte  * @exception       ... underlying exceptions will be thrown
128*fcf3ce44SJohn Forte  * @return          The port attributes
129*fcf3ce44SJohn Forte  * @version         1.2
130*fcf3ce44SJohn Forte  * @see             HandlePort::validate
131*fcf3ce44SJohn Forte  *
132*fcf3ce44SJohn Forte  * @doc             This routine will perform state validation
133*fcf3ce44SJohn Forte  */
getPortAttributes()134*fcf3ce44SJohn Forte HBA_NPIVATTRIBUTES HandleNPIVPort::getPortAttributes() {
135*fcf3ce44SJohn Forte 	Trace log("HandleNPIVPort::getPortAttributes");
136*fcf3ce44SJohn Forte 	uint64_t newState;
137*fcf3ce44SJohn Forte 	HBA_NPIVATTRIBUTES attributes = vport->getPortAttributes(newState);
138*fcf3ce44SJohn Forte 	validate(newState);
139*fcf3ce44SJohn Forte 	return (attributes);
140*fcf3ce44SJohn Forte }
141*fcf3ce44SJohn Forte 
142