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