xref: /onnv-gate/usr/src/lib/sun_fc/common/HBAPort.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 "HBAPort.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 <cerrno>
34*7836SJohn.Forte@Sun.COM #include <cstring>
35*7836SJohn.Forte@Sun.COM #include <sys/types.h>
36*7836SJohn.Forte@Sun.COM #include <sys/mkdev.h>
37*7836SJohn.Forte@Sun.COM #include <sys/stat.h>
38*7836SJohn.Forte@Sun.COM #include <fcntl.h>
39*7836SJohn.Forte@Sun.COM #include <unistd.h>
40*7836SJohn.Forte@Sun.COM #include <stropts.h>
41*7836SJohn.Forte@Sun.COM #include <dirent.h>
42*7836SJohn.Forte@Sun.COM #include <libdevinfo.h>
43*7836SJohn.Forte@Sun.COM 
44*7836SJohn.Forte@Sun.COM using namespace std;
45*7836SJohn.Forte@Sun.COM 
46*7836SJohn.Forte@Sun.COM /**
47*7836SJohn.Forte@Sun.COM  * Standard definition for general topology lookup (See T11 FC-FS)
48*7836SJohn.Forte@Sun.COM  */
49*7836SJohn.Forte@Sun.COM const int HBAPort::RNID_GENERAL_TOPOLOGY_DATA_FORMAT = 0xDF;
50*7836SJohn.Forte@Sun.COM const uint8_t HBAPort::HBA_NPIV_PORT_MAX = UCHAR_MAX;
51*7836SJohn.Forte@Sun.COM 
52*7836SJohn.Forte@Sun.COM /**
53*7836SJohn.Forte@Sun.COM  * @memo	    Construct a new deafult HBA Port
54*7836SJohn.Forte@Sun.COM  */
HBAPort()55*7836SJohn.Forte@Sun.COM HBAPort::HBAPort() {
56*7836SJohn.Forte@Sun.COM }
57*7836SJohn.Forte@Sun.COM 
58*7836SJohn.Forte@Sun.COM /**
59*7836SJohn.Forte@Sun.COM  * @memo	    Compare two HBA ports for equality
60*7836SJohn.Forte@Sun.COM  * @return	    TRUE if both ports are the same
61*7836SJohn.Forte@Sun.COM  * @return	    FALSE if the ports are different
62*7836SJohn.Forte@Sun.COM  *
63*7836SJohn.Forte@Sun.COM  * @doc		    Comparison is based on Node WWN, Port WWN and path
64*7836SJohn.Forte@Sun.COM  */
operator ==(HBAPort & comp)65*7836SJohn.Forte@Sun.COM bool HBAPort::operator==(HBAPort &comp) {
66*7836SJohn.Forte@Sun.COM 	return (this->getPortWWN() == comp.getPortWWN() &&
67*7836SJohn.Forte@Sun.COM 		this->getNodeWWN() == comp.getNodeWWN() &&
68*7836SJohn.Forte@Sun.COM 		this->getPath() == comp.getPath());
69*7836SJohn.Forte@Sun.COM }
70*7836SJohn.Forte@Sun.COM 
71*7836SJohn.Forte@Sun.COM /**
72*7836SJohn.Forte@Sun.COM  * @memo	    Validate that the port is still present in the system
73*7836SJohn.Forte@Sun.COM  * @exception	    UnavailableException if the port is not present
74*7836SJohn.Forte@Sun.COM  *
75*7836SJohn.Forte@Sun.COM  * @doc		    If the port is still present on the system, the routine
76*7836SJohn.Forte@Sun.COM  *		    will return normally.  If the port is not present
77*7836SJohn.Forte@Sun.COM  *		    an exception will be thrown.
78*7836SJohn.Forte@Sun.COM  */
validatePresent()79*7836SJohn.Forte@Sun.COM void HBAPort::validatePresent() {
80*7836SJohn.Forte@Sun.COM 	Trace log("HBAPort::validatePresent");
81*7836SJohn.Forte@Sun.COM 	string path = getPath();
82*7836SJohn.Forte@Sun.COM 	struct stat sbuf;
83*7836SJohn.Forte@Sun.COM 	if (stat(path.c_str(), &sbuf) == -1) {
84*7836SJohn.Forte@Sun.COM 	    if (errno == ENOENT) {
85*7836SJohn.Forte@Sun.COM 		throw UnavailableException();
86*7836SJohn.Forte@Sun.COM 	    } else {
87*7836SJohn.Forte@Sun.COM 		log.debug("Unable to stat %s: %s", path.c_str(),
88*7836SJohn.Forte@Sun.COM 			strerror(errno));
89*7836SJohn.Forte@Sun.COM 		throw InternalError();
90*7836SJohn.Forte@Sun.COM 	    }
91*7836SJohn.Forte@Sun.COM 	}
92*7836SJohn.Forte@Sun.COM }
93*7836SJohn.Forte@Sun.COM 
94*7836SJohn.Forte@Sun.COM 
95*7836SJohn.Forte@Sun.COM /*
96*7836SJohn.Forte@Sun.COM  * structure for di_devlink_walk
97*7836SJohn.Forte@Sun.COM  */
98*7836SJohn.Forte@Sun.COM typedef struct walk_devlink {
99*7836SJohn.Forte@Sun.COM 	char *path;
100*7836SJohn.Forte@Sun.COM 	size_t len;
101*7836SJohn.Forte@Sun.COM 	char **linkpp;
102*7836SJohn.Forte@Sun.COM } walk_devlink_t;
103*7836SJohn.Forte@Sun.COM 
104*7836SJohn.Forte@Sun.COM /**
105*7836SJohn.Forte@Sun.COM  * @memo	    callback funtion for di_devlink_walk
106*7836SJohn.Forte@Sun.COM  * @postcondition   Find matching /dev link for the given path argument.
107*7836SJohn.Forte@Sun.COM  * @param	    devlink element and callback function argument.
108*7836SJohn.Forte@Sun.COM  *
109*7836SJohn.Forte@Sun.COM  * @doc		    The input path is expected to not have "/devices".
110*7836SJohn.Forte@Sun.COM  */
111*7836SJohn.Forte@Sun.COM extern "C" int
get_devlink(di_devlink_t devlink,void * arg)112*7836SJohn.Forte@Sun.COM get_devlink(di_devlink_t devlink, void *arg) {
113*7836SJohn.Forte@Sun.COM 	Trace log("get_devlink");
114*7836SJohn.Forte@Sun.COM 	walk_devlink_t *warg = (walk_devlink_t *)arg;
115*7836SJohn.Forte@Sun.COM 
116*7836SJohn.Forte@Sun.COM 	/*
117*7836SJohn.Forte@Sun.COM 	 * When path is specified, it doesn't have minor
118*7836SJohn.Forte@Sun.COM 	 * name. Therefore, the ../.. prefixes needs to be stripped.
119*7836SJohn.Forte@Sun.COM 	 */
120*7836SJohn.Forte@Sun.COM 	if (warg->path) {
121*7836SJohn.Forte@Sun.COM 		// di_devlink_content contains /devices
122*7836SJohn.Forte@Sun.COM 		char *content = (char *)di_devlink_content(devlink);
123*7836SJohn.Forte@Sun.COM 		char *start = strstr(content, "/devices");
124*7836SJohn.Forte@Sun.COM 
125*7836SJohn.Forte@Sun.COM 		if (start == NULL ||
126*7836SJohn.Forte@Sun.COM 		    strncmp(start, warg->path, warg->len) != 0 ||
127*7836SJohn.Forte@Sun.COM 		    // make it sure the device path has minor name
128*7836SJohn.Forte@Sun.COM 		    start[warg->len] != ':')
129*7836SJohn.Forte@Sun.COM 			return (DI_WALK_CONTINUE);
130*7836SJohn.Forte@Sun.COM 	}
131*7836SJohn.Forte@Sun.COM 
132*7836SJohn.Forte@Sun.COM 	*(warg->linkpp) = strdup(di_devlink_path(devlink));
133*7836SJohn.Forte@Sun.COM 	return (DI_WALK_TERMINATE);
134*7836SJohn.Forte@Sun.COM }
135*7836SJohn.Forte@Sun.COM 
136*7836SJohn.Forte@Sun.COM /**
137*7836SJohn.Forte@Sun.COM  * @memo	    Convert /devices paths to /dev sym-link paths.
138*7836SJohn.Forte@Sun.COM  * @postcondition   The mapping buffer OSDeviceName paths will be
139*7836SJohn.Forte@Sun.COM  *		    converted to short names.
140*7836SJohn.Forte@Sun.COM  * @param	    mappings The target mappings data to convert to
141*7836SJohn.Forte@Sun.COM  *		    short names
142*7836SJohn.Forte@Sun.COM  *
143*7836SJohn.Forte@Sun.COM  * @doc		    If no link
144*7836SJohn.Forte@Sun.COM  * is found, the long path is left as is.
145*7836SJohn.Forte@Sun.COM  * Note: The NumberOfEntries field MUST not be greater than the size
146*7836SJohn.Forte@Sun.COM  * of the array passed in.
147*7836SJohn.Forte@Sun.COM  */
convertToShortNames(PHBA_FCPTARGETMAPPINGV2 mappings)148*7836SJohn.Forte@Sun.COM void HBAPort::convertToShortNames(PHBA_FCPTARGETMAPPINGV2 mappings) {
149*7836SJohn.Forte@Sun.COM 	Trace log("HBAPort::convertToShortNames");
150*7836SJohn.Forte@Sun.COM 	di_devlink_handle_t hdl;
151*7836SJohn.Forte@Sun.COM 	walk_devlink_t warg;
152*7836SJohn.Forte@Sun.COM 	char *minor_path, *devlinkp;
153*7836SJohn.Forte@Sun.COM 
154*7836SJohn.Forte@Sun.COM 	if ((hdl = di_devlink_init(NULL, 0)) == NULL) {
155*7836SJohn.Forte@Sun.COM 	    log.internalError("di_devlink_init failed. Errno:%d", errno);
156*7836SJohn.Forte@Sun.COM 	    // no need to check further, just return here.
157*7836SJohn.Forte@Sun.COM 	    return;
158*7836SJohn.Forte@Sun.COM 	}
159*7836SJohn.Forte@Sun.COM 
160*7836SJohn.Forte@Sun.COM 	for (int j = 0; j < mappings->NumberOfEntries; j++) {
161*7836SJohn.Forte@Sun.COM 	    if (strchr(mappings->entry[j].ScsiId.OSDeviceName, ':')) {
162*7836SJohn.Forte@Sun.COM 		// search link for minor node
163*7836SJohn.Forte@Sun.COM 		minor_path = mappings->entry[j].ScsiId.OSDeviceName;
164*7836SJohn.Forte@Sun.COM 		if (strstr(minor_path, "/devices") != NULL) {
165*7836SJohn.Forte@Sun.COM 		    minor_path = mappings->entry[j].ScsiId.OSDeviceName +
166*7836SJohn.Forte@Sun.COM 			strlen("/devices");
167*7836SJohn.Forte@Sun.COM 		} else {
168*7836SJohn.Forte@Sun.COM 		    minor_path = mappings->entry[j].ScsiId.OSDeviceName;
169*7836SJohn.Forte@Sun.COM 		}
170*7836SJohn.Forte@Sun.COM 		warg.path = NULL;
171*7836SJohn.Forte@Sun.COM 	    } else {
172*7836SJohn.Forte@Sun.COM 		minor_path = NULL;
173*7836SJohn.Forte@Sun.COM 		if (strstr(mappings->entry[j].ScsiId.OSDeviceName,
174*7836SJohn.Forte@Sun.COM 		    "/devices") != NULL) {
175*7836SJohn.Forte@Sun.COM 		    warg.len = strlen (mappings->entry[j].ScsiId.OSDeviceName) -
176*7836SJohn.Forte@Sun.COM 			    strlen ("/devices");
177*7836SJohn.Forte@Sun.COM 		    warg.path = mappings->entry[j].ScsiId.OSDeviceName +
178*7836SJohn.Forte@Sun.COM 			    strlen ("/devices");
179*7836SJohn.Forte@Sun.COM 		} else {
180*7836SJohn.Forte@Sun.COM 		    warg.len = strlen(mappings->entry[j].ScsiId.OSDeviceName);
181*7836SJohn.Forte@Sun.COM 		    warg.path = mappings->entry[j].ScsiId.OSDeviceName;
182*7836SJohn.Forte@Sun.COM 		}
183*7836SJohn.Forte@Sun.COM 	    }
184*7836SJohn.Forte@Sun.COM 
185*7836SJohn.Forte@Sun.COM 	    devlinkp = NULL;
186*7836SJohn.Forte@Sun.COM 	    warg.linkpp = &devlinkp;
187*7836SJohn.Forte@Sun.COM 	    (void) di_devlink_walk(hdl, NULL, minor_path, DI_PRIMARY_LINK,
188*7836SJohn.Forte@Sun.COM 		(void *)&warg, get_devlink);
189*7836SJohn.Forte@Sun.COM 
190*7836SJohn.Forte@Sun.COM 	    if (devlinkp != NULL) {
191*7836SJohn.Forte@Sun.COM 		snprintf(mappings->entry[j].ScsiId.OSDeviceName,
192*7836SJohn.Forte@Sun.COM 		    sizeof (mappings->entry[j].ScsiId.OSDeviceName),
193*7836SJohn.Forte@Sun.COM 		    "%s", devlinkp);
194*7836SJohn.Forte@Sun.COM 		free(devlinkp);
195*7836SJohn.Forte@Sun.COM 	    } // else leave OSDeviceName alone.
196*7836SJohn.Forte@Sun.COM 
197*7836SJohn.Forte@Sun.COM 	}
198*7836SJohn.Forte@Sun.COM 
199*7836SJohn.Forte@Sun.COM 	di_devlink_fini(&hdl);
200*7836SJohn.Forte@Sun.COM 
201*7836SJohn.Forte@Sun.COM }
202*7836SJohn.Forte@Sun.COM 
203*7836SJohn.Forte@Sun.COM /*
204*7836SJohn.Forte@Sun.COM  * Finds controller path for a give device path.
205*7836SJohn.Forte@Sun.COM  *
206*7836SJohn.Forte@Sun.COM  * Return vale: controller path.
207*7836SJohn.Forte@Sun.COM  */
lookupControllerPath(string path)208*7836SJohn.Forte@Sun.COM string HBAPort::lookupControllerPath(string path) {
209*7836SJohn.Forte@Sun.COM 	Trace log("lookupControllerPath");
210*7836SJohn.Forte@Sun.COM 	DIR	    *dp;
211*7836SJohn.Forte@Sun.COM 	char    buf[MAXPATHLEN];
212*7836SJohn.Forte@Sun.COM 	char    node[MAXPATHLEN];
213*7836SJohn.Forte@Sun.COM 	struct dirent **dirpp, *dirp;
214*7836SJohn.Forte@Sun.COM 	const char    dir[] = "/dev/cfg";
215*7836SJohn.Forte@Sun.COM 	ssize_t	    count;
216*7836SJohn.Forte@Sun.COM 	uchar_t *dir_buf = new uchar_t[sizeof (struct dirent) + MAXPATHLEN];
217*7836SJohn.Forte@Sun.COM 
218*7836SJohn.Forte@Sun.COM 	if ((dp = opendir(dir)) == NULL) {
219*7836SJohn.Forte@Sun.COM 	    string tmp = "Unable to open ";
220*7836SJohn.Forte@Sun.COM 	    tmp += dir;
221*7836SJohn.Forte@Sun.COM 	    tmp += "to find controller number.";
222*7836SJohn.Forte@Sun.COM 	    delete (dir_buf);
223*7836SJohn.Forte@Sun.COM 	    throw IOError(tmp);
224*7836SJohn.Forte@Sun.COM 	}
225*7836SJohn.Forte@Sun.COM 
226*7836SJohn.Forte@Sun.COM 	dirp = (struct dirent *) dir_buf;
227*7836SJohn.Forte@Sun.COM 	dirpp = &dirp;
228*7836SJohn.Forte@Sun.COM 	while ((readdir_r(dp, dirp, dirpp)) == 0  && dirp != NULL) {
229*7836SJohn.Forte@Sun.COM 	    if (strcmp(dirp->d_name, ".") == 0 ||
230*7836SJohn.Forte@Sun.COM 		    strcmp(dirp->d_name, "..") == 0) {
231*7836SJohn.Forte@Sun.COM 		continue;
232*7836SJohn.Forte@Sun.COM 	    }
233*7836SJohn.Forte@Sun.COM 	    sprintf(node, "%s/%s", dir, dirp->d_name);
234*7836SJohn.Forte@Sun.COM 	    if ((count = readlink(node,buf,sizeof(buf)))) {
235*7836SJohn.Forte@Sun.COM 		buf[count] = '\0';
236*7836SJohn.Forte@Sun.COM 		if (strstr(buf, path.c_str())) {
237*7836SJohn.Forte@Sun.COM 		    string cfg_path = dir;
238*7836SJohn.Forte@Sun.COM 		    cfg_path += "/";
239*7836SJohn.Forte@Sun.COM 		    cfg_path += dirp->d_name;
240*7836SJohn.Forte@Sun.COM 		    closedir(dp);
241*7836SJohn.Forte@Sun.COM 		    delete (dir_buf);
242*7836SJohn.Forte@Sun.COM 		    return (cfg_path);
243*7836SJohn.Forte@Sun.COM 		}
244*7836SJohn.Forte@Sun.COM 	    }
245*7836SJohn.Forte@Sun.COM 	}
246*7836SJohn.Forte@Sun.COM 
247*7836SJohn.Forte@Sun.COM 	closedir(dp);
248*7836SJohn.Forte@Sun.COM 	delete (dir_buf);
249*7836SJohn.Forte@Sun.COM 	throw InternalError("Unable to find controller path");
250*7836SJohn.Forte@Sun.COM }
251*7836SJohn.Forte@Sun.COM 
addPort(HBANPIVPort * port)252*7836SJohn.Forte@Sun.COM void HBAPort::addPort(HBANPIVPort *port) {
253*7836SJohn.Forte@Sun.COM 	Trace log("HBAPort::addPort");
254*7836SJohn.Forte@Sun.COM 	lock();
255*7836SJohn.Forte@Sun.COM 	// support hba with up to UCHAR_MAX number of ports.
256*7836SJohn.Forte@Sun.COM 	if (npivportsByIndex.size() + 1 > HBA_NPIV_PORT_MAX) {
257*7836SJohn.Forte@Sun.COM 		unlock();
258*7836SJohn.Forte@Sun.COM 		throw InternalError("HBA NPIV Port count exceeds max number of ports");
259*7836SJohn.Forte@Sun.COM 	}
260*7836SJohn.Forte@Sun.COM 
261*7836SJohn.Forte@Sun.COM 	try {
262*7836SJohn.Forte@Sun.COM 		npivportsByWWN[port->getPortWWN()] = port;
263*7836SJohn.Forte@Sun.COM 		npivportsByIndex.insert(npivportsByIndex.end(), port);
264*7836SJohn.Forte@Sun.COM 		unlock();
265*7836SJohn.Forte@Sun.COM 	} catch (...) {
266*7836SJohn.Forte@Sun.COM 		unlock();
267*7836SJohn.Forte@Sun.COM 		throw;
268*7836SJohn.Forte@Sun.COM 	}
269*7836SJohn.Forte@Sun.COM }
270*7836SJohn.Forte@Sun.COM 
getPort(uint64_t wwn)271*7836SJohn.Forte@Sun.COM HBANPIVPort* HBAPort::getPort(uint64_t wwn) {
272*7836SJohn.Forte@Sun.COM 	Trace log("HBAPort::getPort");
273*7836SJohn.Forte@Sun.COM 	HBANPIVPort *port = NULL;
274*7836SJohn.Forte@Sun.COM 
275*7836SJohn.Forte@Sun.COM 	lock();
276*7836SJohn.Forte@Sun.COM 	try {
277*7836SJohn.Forte@Sun.COM 		if (npivportsByWWN.find(wwn) == npivportsByWWN.end()) {
278*7836SJohn.Forte@Sun.COM 			throw IllegalWWNException();
279*7836SJohn.Forte@Sun.COM 		}
280*7836SJohn.Forte@Sun.COM 		port = npivportsByWWN[wwn];
281*7836SJohn.Forte@Sun.COM 		unlock();
282*7836SJohn.Forte@Sun.COM 		return (port);
283*7836SJohn.Forte@Sun.COM 	} catch (...) {
284*7836SJohn.Forte@Sun.COM 		unlock();
285*7836SJohn.Forte@Sun.COM 		throw;
286*7836SJohn.Forte@Sun.COM 	}
287*7836SJohn.Forte@Sun.COM }
288*7836SJohn.Forte@Sun.COM 
getPortByIndex(int index)289*7836SJohn.Forte@Sun.COM HBANPIVPort* HBAPort::getPortByIndex(int index) {
290*7836SJohn.Forte@Sun.COM 	Trace log("HBAPort::getPortByIndex");
291*7836SJohn.Forte@Sun.COM 	lock();
292*7836SJohn.Forte@Sun.COM 	try {
293*7836SJohn.Forte@Sun.COM 		if (index >= npivportsByIndex.size() || index < 0) {
294*7836SJohn.Forte@Sun.COM 			throw IllegalIndexException();
295*7836SJohn.Forte@Sun.COM 		}
296*7836SJohn.Forte@Sun.COM 		HBANPIVPort *tmp = npivportsByIndex[index];
297*7836SJohn.Forte@Sun.COM 		unlock();
298*7836SJohn.Forte@Sun.COM 		return (tmp);
299*7836SJohn.Forte@Sun.COM 	} catch (...) {
300*7836SJohn.Forte@Sun.COM 		unlock();
301*7836SJohn.Forte@Sun.COM 		throw;
302*7836SJohn.Forte@Sun.COM 	}
303*7836SJohn.Forte@Sun.COM }
304*7836SJohn.Forte@Sun.COM 
305