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