xref: /onnv-gate/usr/src/lib/sun_fc/common/HBANPIVPort.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 #include "HBANPIVPort.h"
28*7836SJohn.Forte@Sun.COM #include "Exceptions.h"
29*7836SJohn.Forte@Sun.COM #include "Trace.h"
30*7836SJohn.Forte@Sun.COM #include <iostream>
31*7836SJohn.Forte@Sun.COM #include <iomanip>
32*7836SJohn.Forte@Sun.COM #include <cerrno>
33*7836SJohn.Forte@Sun.COM #include <cstring>
34*7836SJohn.Forte@Sun.COM #include <sys/types.h>
35*7836SJohn.Forte@Sun.COM #include <sys/mkdev.h>
36*7836SJohn.Forte@Sun.COM #include <sys/stat.h>
37*7836SJohn.Forte@Sun.COM #include <fcntl.h>
38*7836SJohn.Forte@Sun.COM #include <unistd.h>
39*7836SJohn.Forte@Sun.COM #include <stropts.h>
40*7836SJohn.Forte@Sun.COM #include <dirent.h>
41*7836SJohn.Forte@Sun.COM #include <libdevinfo.h>
42*7836SJohn.Forte@Sun.COM 
43*7836SJohn.Forte@Sun.COM using namespace std;
44*7836SJohn.Forte@Sun.COM 
45*7836SJohn.Forte@Sun.COM 
46*7836SJohn.Forte@Sun.COM /**
47*7836SJohn.Forte@Sun.COM  * @memo            Construct a new deafult HBA Port
48*7836SJohn.Forte@Sun.COM  * @version         1.7
49*7836SJohn.Forte@Sun.COM  */
HBANPIVPort()50*7836SJohn.Forte@Sun.COM HBANPIVPort::HBANPIVPort() {
51*7836SJohn.Forte@Sun.COM }
52*7836SJohn.Forte@Sun.COM 
53*7836SJohn.Forte@Sun.COM /**
54*7836SJohn.Forte@Sun.COM  * @memo            Compare two HBA ports for equality
55*7836SJohn.Forte@Sun.COM  * @return          TRUE if both ports are the same
56*7836SJohn.Forte@Sun.COM  * @return          FALSE if the ports are different
57*7836SJohn.Forte@Sun.COM  * @version         1.7
58*7836SJohn.Forte@Sun.COM  *
59*7836SJohn.Forte@Sun.COM  * @doc             Comparison is based on Node WWN, Port WWN and path
60*7836SJohn.Forte@Sun.COM  */
operator ==(HBANPIVPort & comp)61*7836SJohn.Forte@Sun.COM bool HBANPIVPort::operator==(HBANPIVPort &comp) {
62*7836SJohn.Forte@Sun.COM 	return (this->getPortWWN() == comp.getPortWWN() &&
63*7836SJohn.Forte@Sun.COM 	    this->getNodeWWN() == comp.getNodeWWN());
64*7836SJohn.Forte@Sun.COM }
65*7836SJohn.Forte@Sun.COM 
66*7836SJohn.Forte@Sun.COM /*
67*7836SJohn.Forte@Sun.COM  * Finds controller path for a give device path.
68*7836SJohn.Forte@Sun.COM  *
69*7836SJohn.Forte@Sun.COM  * Return vale: controller path.
70*7836SJohn.Forte@Sun.COM  */
lookupControllerPath(string path)71*7836SJohn.Forte@Sun.COM string HBANPIVPort::lookupControllerPath(string path) {
72*7836SJohn.Forte@Sun.COM 	Trace log("lookupControllerPath");
73*7836SJohn.Forte@Sun.COM 	DIR	*dp;
74*7836SJohn.Forte@Sun.COM 	char	buf[MAXPATHLEN];
75*7836SJohn.Forte@Sun.COM 	char	node[MAXPATHLEN];
76*7836SJohn.Forte@Sun.COM 	struct dirent	**dirpp, *dirp;
77*7836SJohn.Forte@Sun.COM 	const char	dir[] = "/dev/cfg";
78*7836SJohn.Forte@Sun.COM 	ssize_t	count;
79*7836SJohn.Forte@Sun.COM 	uchar_t	*dir_buf = new uchar_t[sizeof (struct dirent) + MAXPATHLEN];
80*7836SJohn.Forte@Sun.COM 
81*7836SJohn.Forte@Sun.COM 	if ((dp = opendir(dir)) == NULL) {
82*7836SJohn.Forte@Sun.COM 		string tmp = "Unable to open ";
83*7836SJohn.Forte@Sun.COM 		tmp += dir;
84*7836SJohn.Forte@Sun.COM 		tmp += "to find controller number.";
85*7836SJohn.Forte@Sun.COM 		delete (dir_buf);
86*7836SJohn.Forte@Sun.COM 		throw IOError(tmp);
87*7836SJohn.Forte@Sun.COM 	}
88*7836SJohn.Forte@Sun.COM 
89*7836SJohn.Forte@Sun.COM 	dirp = (struct dirent *) dir_buf;
90*7836SJohn.Forte@Sun.COM 	dirpp = &dirp;
91*7836SJohn.Forte@Sun.COM 	while ((readdir_r(dp, dirp, dirpp)) == 0  && dirp != NULL) {
92*7836SJohn.Forte@Sun.COM 		if (strcmp(dirp->d_name, ".") == 0 ||
93*7836SJohn.Forte@Sun.COM 		    strcmp(dirp->d_name, "..") == 0) {
94*7836SJohn.Forte@Sun.COM 			continue;
95*7836SJohn.Forte@Sun.COM 		}
96*7836SJohn.Forte@Sun.COM 		sprintf(node, "%s/%s", dir, dirp->d_name);
97*7836SJohn.Forte@Sun.COM 		if ((count = readlink(node,buf,sizeof(buf)))) {
98*7836SJohn.Forte@Sun.COM 			buf[count] = '\0';
99*7836SJohn.Forte@Sun.COM 			if (strstr(buf, path.c_str())) {
100*7836SJohn.Forte@Sun.COM 				string cfg_path = dir;
101*7836SJohn.Forte@Sun.COM 				cfg_path += "/";
102*7836SJohn.Forte@Sun.COM 				cfg_path += dirp->d_name;
103*7836SJohn.Forte@Sun.COM 				closedir(dp);
104*7836SJohn.Forte@Sun.COM 				delete (dir_buf);
105*7836SJohn.Forte@Sun.COM 				return (cfg_path);
106*7836SJohn.Forte@Sun.COM 			}
107*7836SJohn.Forte@Sun.COM 		}
108*7836SJohn.Forte@Sun.COM 	}
109*7836SJohn.Forte@Sun.COM 
110*7836SJohn.Forte@Sun.COM  	closedir(dp);
111*7836SJohn.Forte@Sun.COM 	delete (dir_buf);
112*7836SJohn.Forte@Sun.COM 	throw InternalError("Unable to find controller path");
113*7836SJohn.Forte@Sun.COM }
114*7836SJohn.Forte@Sun.COM 
115