1fcf3ce44SJohn Forte /*
2fcf3ce44SJohn Forte  * CDDL HEADER START
3fcf3ce44SJohn Forte  *
4fcf3ce44SJohn Forte  * The contents of this file are subject to the terms of the
5fcf3ce44SJohn Forte  * Common Development and Distribution License (the "License").
6fcf3ce44SJohn Forte  * You may not use this file except in compliance with the License.
7fcf3ce44SJohn Forte  *
8fcf3ce44SJohn Forte  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9fcf3ce44SJohn Forte  * or http://www.opensolaris.org/os/licensing.
10fcf3ce44SJohn Forte  * See the License for the specific language governing permissions
11fcf3ce44SJohn Forte  * and limitations under the License.
12fcf3ce44SJohn Forte  *
13fcf3ce44SJohn Forte  * When distributing Covered Code, include this CDDL HEADER in each
14fcf3ce44SJohn Forte  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15fcf3ce44SJohn Forte  * If applicable, add the following below this CDDL HEADER, with the
16fcf3ce44SJohn Forte  * fields enclosed by brackets "[]" replaced with your own identifying
17fcf3ce44SJohn Forte  * information: Portions Copyright [yyyy] [name of copyright owner]
18fcf3ce44SJohn Forte  *
19fcf3ce44SJohn Forte  * CDDL HEADER END
20fcf3ce44SJohn Forte  */
21fcf3ce44SJohn Forte /*
22fcf3ce44SJohn Forte  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
23fcf3ce44SJohn Forte  * Use is subject to license terms.
24fcf3ce44SJohn Forte  */
25fcf3ce44SJohn Forte 
26fcf3ce44SJohn Forte 
27fcf3ce44SJohn Forte #include "HBANPIVPort.h"
28fcf3ce44SJohn Forte #include "Exceptions.h"
29fcf3ce44SJohn Forte #include "Trace.h"
30fcf3ce44SJohn Forte #include <iostream>
31fcf3ce44SJohn Forte #include <iomanip>
32fcf3ce44SJohn Forte #include <cerrno>
33fcf3ce44SJohn Forte #include <cstring>
34fcf3ce44SJohn Forte #include <sys/types.h>
35fcf3ce44SJohn Forte #include <sys/mkdev.h>
36fcf3ce44SJohn Forte #include <sys/stat.h>
37fcf3ce44SJohn Forte #include <fcntl.h>
38fcf3ce44SJohn Forte #include <unistd.h>
39fcf3ce44SJohn Forte #include <stropts.h>
40fcf3ce44SJohn Forte #include <dirent.h>
41fcf3ce44SJohn Forte #include <libdevinfo.h>
42fcf3ce44SJohn Forte 
43fcf3ce44SJohn Forte using namespace std;
44fcf3ce44SJohn Forte 
45fcf3ce44SJohn Forte 
46fcf3ce44SJohn Forte /**
47*9b622488SToomas Soome  * @memo            Construct a new default HBA Port
48fcf3ce44SJohn Forte  * @version         1.7
49fcf3ce44SJohn Forte  */
HBANPIVPort()50fcf3ce44SJohn Forte HBANPIVPort::HBANPIVPort() {
51fcf3ce44SJohn Forte }
52fcf3ce44SJohn Forte 
53fcf3ce44SJohn Forte /**
54fcf3ce44SJohn Forte  * @memo            Compare two HBA ports for equality
55fcf3ce44SJohn Forte  * @return          TRUE if both ports are the same
56fcf3ce44SJohn Forte  * @return          FALSE if the ports are different
57fcf3ce44SJohn Forte  * @version         1.7
58fcf3ce44SJohn Forte  *
59fcf3ce44SJohn Forte  * @doc             Comparison is based on Node WWN, Port WWN and path
60fcf3ce44SJohn Forte  */
operator ==(HBANPIVPort & comp)61fcf3ce44SJohn Forte bool HBANPIVPort::operator==(HBANPIVPort &comp) {
62fcf3ce44SJohn Forte 	return (this->getPortWWN() == comp.getPortWWN() &&
63fcf3ce44SJohn Forte 	    this->getNodeWWN() == comp.getNodeWWN());
64fcf3ce44SJohn Forte }
65fcf3ce44SJohn Forte 
66fcf3ce44SJohn Forte /*
67fcf3ce44SJohn Forte  * Finds controller path for a give device path.
68fcf3ce44SJohn Forte  *
69fcf3ce44SJohn Forte  * Return vale: controller path.
70fcf3ce44SJohn Forte  */
lookupControllerPath(string path)71fcf3ce44SJohn Forte string HBANPIVPort::lookupControllerPath(string path) {
72fcf3ce44SJohn Forte 	Trace log("lookupControllerPath");
73fcf3ce44SJohn Forte 	DIR	*dp;
74fcf3ce44SJohn Forte 	char	buf[MAXPATHLEN];
75fcf3ce44SJohn Forte 	char	node[MAXPATHLEN];
76fcf3ce44SJohn Forte 	struct dirent	**dirpp, *dirp;
77fcf3ce44SJohn Forte 	const char	dir[] = "/dev/cfg";
78fcf3ce44SJohn Forte 	ssize_t	count;
79fcf3ce44SJohn Forte 	uchar_t	*dir_buf = new uchar_t[sizeof (struct dirent) + MAXPATHLEN];
80fcf3ce44SJohn Forte 
81fcf3ce44SJohn Forte 	if ((dp = opendir(dir)) == NULL) {
82fcf3ce44SJohn Forte 		string tmp = "Unable to open ";
83fcf3ce44SJohn Forte 		tmp += dir;
84fcf3ce44SJohn Forte 		tmp += "to find controller number.";
85b3385a70SToomas Soome 		delete[] (dir_buf);
86fcf3ce44SJohn Forte 		throw IOError(tmp);
87fcf3ce44SJohn Forte 	}
88fcf3ce44SJohn Forte 
89fcf3ce44SJohn Forte 	dirp = (struct dirent *) dir_buf;
90fcf3ce44SJohn Forte 	dirpp = &dirp;
91fcf3ce44SJohn Forte 	while ((readdir_r(dp, dirp, dirpp)) == 0  && dirp != NULL) {
92fcf3ce44SJohn Forte 		if (strcmp(dirp->d_name, ".") == 0 ||
93fcf3ce44SJohn Forte 		    strcmp(dirp->d_name, "..") == 0) {
94fcf3ce44SJohn Forte 			continue;
95fcf3ce44SJohn Forte 		}
96fcf3ce44SJohn Forte 		sprintf(node, "%s/%s", dir, dirp->d_name);
97fcf3ce44SJohn Forte 		if ((count = readlink(node,buf,sizeof(buf)))) {
98fcf3ce44SJohn Forte 			buf[count] = '\0';
99fcf3ce44SJohn Forte 			if (strstr(buf, path.c_str())) {
100fcf3ce44SJohn Forte 				string cfg_path = dir;
101fcf3ce44SJohn Forte 				cfg_path += "/";
102fcf3ce44SJohn Forte 				cfg_path += dirp->d_name;
103fcf3ce44SJohn Forte 				closedir(dp);
104b3385a70SToomas Soome 				delete[] (dir_buf);
105fcf3ce44SJohn Forte 				return (cfg_path);
106fcf3ce44SJohn Forte 			}
107fcf3ce44SJohn Forte 		}
108fcf3ce44SJohn Forte 	}
109fcf3ce44SJohn Forte 
110b3385a70SToomas Soome 	closedir(dp);
111b3385a70SToomas Soome 	delete[] (dir_buf);
112fcf3ce44SJohn Forte 	throw InternalError("Unable to find controller path");
113fcf3ce44SJohn Forte }
114fcf3ce44SJohn Forte 
115