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