1*10652SHyon.Kim@Sun.COM /*
2*10652SHyon.Kim@Sun.COM * CDDL HEADER START
3*10652SHyon.Kim@Sun.COM *
4*10652SHyon.Kim@Sun.COM * The contents of this file are subject to the terms of the
5*10652SHyon.Kim@Sun.COM * Common Development and Distribution License (the "License").
6*10652SHyon.Kim@Sun.COM * You may not use this file except in compliance with the License.
7*10652SHyon.Kim@Sun.COM *
8*10652SHyon.Kim@Sun.COM * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*10652SHyon.Kim@Sun.COM * or http://www.opensolaris.org/os/licensing.
10*10652SHyon.Kim@Sun.COM * See the License for the specific language governing permissions
11*10652SHyon.Kim@Sun.COM * and limitations under the License.
12*10652SHyon.Kim@Sun.COM *
13*10652SHyon.Kim@Sun.COM * When distributing Covered Code, include this CDDL HEADER in each
14*10652SHyon.Kim@Sun.COM * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*10652SHyon.Kim@Sun.COM * If applicable, add the following below this CDDL HEADER, with the
16*10652SHyon.Kim@Sun.COM * fields enclosed by brackets "[]" replaced with your own identifying
17*10652SHyon.Kim@Sun.COM * information: Portions Copyright [yyyy] [name of copyright owner]
18*10652SHyon.Kim@Sun.COM *
19*10652SHyon.Kim@Sun.COM * CDDL HEADER END
20*10652SHyon.Kim@Sun.COM */
21*10652SHyon.Kim@Sun.COM
22*10652SHyon.Kim@Sun.COM /*
23*10652SHyon.Kim@Sun.COM * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24*10652SHyon.Kim@Sun.COM * Use is subject to license terms.
25*10652SHyon.Kim@Sun.COM */
26*10652SHyon.Kim@Sun.COM
27*10652SHyon.Kim@Sun.COM #include <sun_sas.h>
28*10652SHyon.Kim@Sun.COM #include <sys/types.h>
29*10652SHyon.Kim@Sun.COM #include <sys/stat.h>
30*10652SHyon.Kim@Sun.COM #include <fcntl.h>
31*10652SHyon.Kim@Sun.COM #include <unistd.h>
32*10652SHyon.Kim@Sun.COM #include <dirent.h>
33*10652SHyon.Kim@Sun.COM #include <libdevinfo.h>
34*10652SHyon.Kim@Sun.COM
35*10652SHyon.Kim@Sun.COM /*
36*10652SHyon.Kim@Sun.COM * structure for di_devlink_walk
37*10652SHyon.Kim@Sun.COM */
38*10652SHyon.Kim@Sun.COM typedef struct walk_devlink {
39*10652SHyon.Kim@Sun.COM char *path;
40*10652SHyon.Kim@Sun.COM size_t len;
41*10652SHyon.Kim@Sun.COM char **linkpp;
42*10652SHyon.Kim@Sun.COM } walk_devlink_t;
43*10652SHyon.Kim@Sun.COM
44*10652SHyon.Kim@Sun.COM /*
45*10652SHyon.Kim@Sun.COM * callback funtion for di_devlink_walk
46*10652SHyon.Kim@Sun.COM * Find matching /dev link for the given path argument.
47*10652SHyon.Kim@Sun.COM * devlink element and callback function argument.
48*10652SHyon.Kim@Sun.COM * The input path is expected to not have "/devices".
49*10652SHyon.Kim@Sun.COM */
50*10652SHyon.Kim@Sun.COM static int
get_devlink(di_devlink_t devlink,void * arg)51*10652SHyon.Kim@Sun.COM get_devlink(di_devlink_t devlink, void *arg)
52*10652SHyon.Kim@Sun.COM {
53*10652SHyon.Kim@Sun.COM const char ROUTINE[] = "get_devlink";
54*10652SHyon.Kim@Sun.COM walk_devlink_t *warg = (walk_devlink_t *)arg;
55*10652SHyon.Kim@Sun.COM
56*10652SHyon.Kim@Sun.COM /*
57*10652SHyon.Kim@Sun.COM * When path is specified, it doesn't have minor
58*10652SHyon.Kim@Sun.COM * name. Therefore, the ../.. prefixes needs to be stripped.
59*10652SHyon.Kim@Sun.COM */
60*10652SHyon.Kim@Sun.COM if (warg->path) {
61*10652SHyon.Kim@Sun.COM char *content = (char *)di_devlink_content(devlink);
62*10652SHyon.Kim@Sun.COM char *start = strstr(content, "/devices");
63*10652SHyon.Kim@Sun.COM
64*10652SHyon.Kim@Sun.COM if (start == NULL ||
65*10652SHyon.Kim@Sun.COM strncmp(start, warg->path, warg->len) != 0 ||
66*10652SHyon.Kim@Sun.COM /* make it sure the device path has minor name */
67*10652SHyon.Kim@Sun.COM start[warg->len] != ':') {
68*10652SHyon.Kim@Sun.COM return (DI_WALK_CONTINUE);
69*10652SHyon.Kim@Sun.COM }
70*10652SHyon.Kim@Sun.COM }
71*10652SHyon.Kim@Sun.COM
72*10652SHyon.Kim@Sun.COM *(warg->linkpp) = strdup(di_devlink_path(devlink));
73*10652SHyon.Kim@Sun.COM log(LOG_DEBUG, ROUTINE, "Walk terminate");
74*10652SHyon.Kim@Sun.COM return (DI_WALK_TERMINATE);
75*10652SHyon.Kim@Sun.COM }
76*10652SHyon.Kim@Sun.COM
77*10652SHyon.Kim@Sun.COM /*
78*10652SHyon.Kim@Sun.COM * Convert /devices paths to /dev sym-link paths.
79*10652SHyon.Kim@Sun.COM * The mapping buffer OSDeviceName paths will be
80*10652SHyon.Kim@Sun.COM * converted to short names.
81*10652SHyon.Kim@Sun.COM * mappings The target mappings data to convert to short names
82*10652SHyon.Kim@Sun.COM *
83*10652SHyon.Kim@Sun.COM * If no link is found, the long path is left as is.
84*10652SHyon.Kim@Sun.COM * Note: The NumberOfEntries field MUST not be greater than the size
85*10652SHyon.Kim@Sun.COM * of the array passed in.
86*10652SHyon.Kim@Sun.COM */
87*10652SHyon.Kim@Sun.COM void
convertDevpathToDevlink(PSMHBA_TARGETMAPPING mappings)88*10652SHyon.Kim@Sun.COM convertDevpathToDevlink(PSMHBA_TARGETMAPPING mappings)
89*10652SHyon.Kim@Sun.COM {
90*10652SHyon.Kim@Sun.COM const char ROUTINE[] = "convertDevpathToLink";
91*10652SHyon.Kim@Sun.COM di_devlink_handle_t hdl;
92*10652SHyon.Kim@Sun.COM walk_devlink_t warg;
93*10652SHyon.Kim@Sun.COM int j;
94*10652SHyon.Kim@Sun.COM char *minor_path, *devlinkp;
95*10652SHyon.Kim@Sun.COM
96*10652SHyon.Kim@Sun.COM if ((hdl = di_devlink_init(NULL, 0)) == NULL) {
97*10652SHyon.Kim@Sun.COM log(LOG_DEBUG, ROUTINE, "di_devlink failed: errno:%d",
98*10652SHyon.Kim@Sun.COM strerror(errno));
99*10652SHyon.Kim@Sun.COM return;
100*10652SHyon.Kim@Sun.COM }
101*10652SHyon.Kim@Sun.COM
102*10652SHyon.Kim@Sun.COM for (j = 0; j < mappings->NumberOfEntries; j++) {
103*10652SHyon.Kim@Sun.COM if (strchr(mappings->entry[j].ScsiId.OSDeviceName, ':')) {
104*10652SHyon.Kim@Sun.COM /* search link for minor node */
105*10652SHyon.Kim@Sun.COM minor_path = mappings->entry[j].ScsiId.OSDeviceName;
106*10652SHyon.Kim@Sun.COM if (strstr(minor_path, "/devices") != NULL) {
107*10652SHyon.Kim@Sun.COM minor_path = mappings->entry[j].ScsiId.
108*10652SHyon.Kim@Sun.COM OSDeviceName + strlen("/devices");
109*10652SHyon.Kim@Sun.COM }
110*10652SHyon.Kim@Sun.COM warg.path = NULL;
111*10652SHyon.Kim@Sun.COM } else {
112*10652SHyon.Kim@Sun.COM minor_path = NULL;
113*10652SHyon.Kim@Sun.COM if (strstr(mappings->entry[j].ScsiId.OSDeviceName,
114*10652SHyon.Kim@Sun.COM "/devices") != NULL) {
115*10652SHyon.Kim@Sun.COM warg.len = strlen(mappings->entry[j].ScsiId.
116*10652SHyon.Kim@Sun.COM OSDeviceName) - strlen("/devices");
117*10652SHyon.Kim@Sun.COM warg.path = mappings->entry[j].
118*10652SHyon.Kim@Sun.COM ScsiId.OSDeviceName + strlen("/devices");
119*10652SHyon.Kim@Sun.COM } else {
120*10652SHyon.Kim@Sun.COM warg.len = strlen(mappings->entry[j].ScsiId.
121*10652SHyon.Kim@Sun.COM OSDeviceName);
122*10652SHyon.Kim@Sun.COM warg.path = mappings->entry[j].ScsiId.
123*10652SHyon.Kim@Sun.COM OSDeviceName;
124*10652SHyon.Kim@Sun.COM }
125*10652SHyon.Kim@Sun.COM }
126*10652SHyon.Kim@Sun.COM
127*10652SHyon.Kim@Sun.COM devlinkp = NULL;
128*10652SHyon.Kim@Sun.COM warg.linkpp = &devlinkp;
129*10652SHyon.Kim@Sun.COM (void) di_devlink_walk(hdl, NULL, minor_path, DI_PRIMARY_LINK,
130*10652SHyon.Kim@Sun.COM (void *)&warg, get_devlink);
131*10652SHyon.Kim@Sun.COM
132*10652SHyon.Kim@Sun.COM if (devlinkp != NULL) {
133*10652SHyon.Kim@Sun.COM (void) snprintf(mappings->entry[j].ScsiId.OSDeviceName,
134*10652SHyon.Kim@Sun.COM sizeof (mappings->entry[j].ScsiId.OSDeviceName),
135*10652SHyon.Kim@Sun.COM "%s", devlinkp);
136*10652SHyon.Kim@Sun.COM free(devlinkp);
137*10652SHyon.Kim@Sun.COM }
138*10652SHyon.Kim@Sun.COM
139*10652SHyon.Kim@Sun.COM }
140*10652SHyon.Kim@Sun.COM
141*10652SHyon.Kim@Sun.COM (void) di_devlink_fini(&hdl);
142*10652SHyon.Kim@Sun.COM }
143*10652SHyon.Kim@Sun.COM
144*10652SHyon.Kim@Sun.COM /*
145*10652SHyon.Kim@Sun.COM * Finds controller path for a give device path.
146*10652SHyon.Kim@Sun.COM *
147*10652SHyon.Kim@Sun.COM * Return value: /dev link for dir and minor name.
148*10652SHyon.Kim@Sun.COM */
149*10652SHyon.Kim@Sun.COM static HBA_STATUS
lookupLink(char * path,char * link,const char * dir,const char * mname)150*10652SHyon.Kim@Sun.COM lookupLink(char *path, char *link, const char *dir, const char *mname)
151*10652SHyon.Kim@Sun.COM {
152*10652SHyon.Kim@Sun.COM const char ROUTINE[] = "lookupLink";
153*10652SHyon.Kim@Sun.COM DIR *dp;
154*10652SHyon.Kim@Sun.COM char buf[MAXPATHLEN];
155*10652SHyon.Kim@Sun.COM char node[MAXPATHLEN];
156*10652SHyon.Kim@Sun.COM char *charptr;
157*10652SHyon.Kim@Sun.COM struct dirent *newdirp, *dirp;
158*10652SHyon.Kim@Sun.COM ssize_t count;
159*10652SHyon.Kim@Sun.COM int dirplen;
160*10652SHyon.Kim@Sun.COM char *subpath;
161*10652SHyon.Kim@Sun.COM char tmpPath[MAXPATHLEN];
162*10652SHyon.Kim@Sun.COM
163*10652SHyon.Kim@Sun.COM if ((dp = opendir(dir)) == NULL) {
164*10652SHyon.Kim@Sun.COM log(LOG_DEBUG, ROUTINE,
165*10652SHyon.Kim@Sun.COM "Unable to open %s to find controller number.", dir);
166*10652SHyon.Kim@Sun.COM return (HBA_STATUS_ERROR);
167*10652SHyon.Kim@Sun.COM }
168*10652SHyon.Kim@Sun.COM
169*10652SHyon.Kim@Sun.COM if (link == NULL) {
170*10652SHyon.Kim@Sun.COM log(LOG_DEBUG, ROUTINE,
171*10652SHyon.Kim@Sun.COM "Invalid argument for storing the link.");
172*10652SHyon.Kim@Sun.COM return (HBA_STATUS_ERROR);
173*10652SHyon.Kim@Sun.COM }
174*10652SHyon.Kim@Sun.COM
175*10652SHyon.Kim@Sun.COM /*
176*10652SHyon.Kim@Sun.COM * dirplen is large enough to fit the largest path-
177*10652SHyon.Kim@Sun.COM * struct dirent includes one byte (the terminator)
178*10652SHyon.Kim@Sun.COM * so we don't add 1 to the calculation here.
179*10652SHyon.Kim@Sun.COM */
180*10652SHyon.Kim@Sun.COM dirplen = pathconf(dir, _PC_NAME_MAX);
181*10652SHyon.Kim@Sun.COM dirplen = ((dirplen <= 0) ? MAXNAMELEN : dirplen) +
182*10652SHyon.Kim@Sun.COM sizeof (struct dirent);
183*10652SHyon.Kim@Sun.COM dirp = (struct dirent *)malloc(dirplen);
184*10652SHyon.Kim@Sun.COM if (dirp == NULL) {
185*10652SHyon.Kim@Sun.COM OUT_OF_MEMORY(ROUTINE);
186*10652SHyon.Kim@Sun.COM return (HBA_STATUS_ERROR);
187*10652SHyon.Kim@Sun.COM }
188*10652SHyon.Kim@Sun.COM
189*10652SHyon.Kim@Sun.COM while ((readdir_r(dp, dirp, &newdirp)) == 0 && newdirp != NULL) {
190*10652SHyon.Kim@Sun.COM if (strcmp(dirp->d_name, ".") == 0 ||
191*10652SHyon.Kim@Sun.COM strcmp(dirp->d_name, "..") == 0) {
192*10652SHyon.Kim@Sun.COM continue;
193*10652SHyon.Kim@Sun.COM }
194*10652SHyon.Kim@Sun.COM /*
195*10652SHyon.Kim@Sun.COM * set to another pointer since dirp->d_name length is 1
196*10652SHyon.Kim@Sun.COM * that will store only the first char 'c' from the name.
197*10652SHyon.Kim@Sun.COM */
198*10652SHyon.Kim@Sun.COM charptr = dirp->d_name;
199*10652SHyon.Kim@Sun.COM (void) snprintf(node, strlen(charptr) + strlen(dir) + 2,
200*10652SHyon.Kim@Sun.COM "%s/%s", dir, charptr);
201*10652SHyon.Kim@Sun.COM if (count = readlink(node, buf, sizeof (buf))) {
202*10652SHyon.Kim@Sun.COM subpath = NULL;
203*10652SHyon.Kim@Sun.COM subpath = strstr(buf, path);
204*10652SHyon.Kim@Sun.COM buf[count] = '\0';
205*10652SHyon.Kim@Sun.COM if (subpath != NULL) {
206*10652SHyon.Kim@Sun.COM (void) strlcpy(tmpPath, path, MAXPATHLEN);
207*10652SHyon.Kim@Sun.COM (void) strlcat(tmpPath, mname, MAXPATHLEN);
208*10652SHyon.Kim@Sun.COM /*
209*10652SHyon.Kim@Sun.COM * if device path has substring of path
210*10652SHyon.Kim@Sun.COM * and exactally matching with :scsi suffix
211*10652SHyon.Kim@Sun.COM */
212*10652SHyon.Kim@Sun.COM if (strcmp(subpath, tmpPath) == 0) {
213*10652SHyon.Kim@Sun.COM (void) strlcpy(link, node, MAXPATHLEN);
214*10652SHyon.Kim@Sun.COM (void) closedir(dp);
215*10652SHyon.Kim@Sun.COM S_FREE(dirp);
216*10652SHyon.Kim@Sun.COM return (HBA_STATUS_OK);
217*10652SHyon.Kim@Sun.COM }
218*10652SHyon.Kim@Sun.COM }
219*10652SHyon.Kim@Sun.COM }
220*10652SHyon.Kim@Sun.COM }
221*10652SHyon.Kim@Sun.COM
222*10652SHyon.Kim@Sun.COM (void) closedir(dp);
223*10652SHyon.Kim@Sun.COM S_FREE(dirp);
224*10652SHyon.Kim@Sun.COM return (HBA_STATUS_ERROR);
225*10652SHyon.Kim@Sun.COM }
226*10652SHyon.Kim@Sun.COM
227*10652SHyon.Kim@Sun.COM /*
228*10652SHyon.Kim@Sun.COM * Finds controller path for a give device path.
229*10652SHyon.Kim@Sun.COM *
230*10652SHyon.Kim@Sun.COM * Return vale:i smp devlink.
231*10652SHyon.Kim@Sun.COM */
232*10652SHyon.Kim@Sun.COM HBA_STATUS
lookupControllerLink(char * path,char * link)233*10652SHyon.Kim@Sun.COM lookupControllerLink(char *path, char *link)
234*10652SHyon.Kim@Sun.COM {
235*10652SHyon.Kim@Sun.COM const char dir[] = "/dev/cfg";
236*10652SHyon.Kim@Sun.COM const char mname[] = ":scsi";
237*10652SHyon.Kim@Sun.COM return (lookupLink(path, link, dir, mname));
238*10652SHyon.Kim@Sun.COM }
239*10652SHyon.Kim@Sun.COM
240*10652SHyon.Kim@Sun.COM /*
241*10652SHyon.Kim@Sun.COM * Finds smp devlink for a give smp path.
242*10652SHyon.Kim@Sun.COM *
243*10652SHyon.Kim@Sun.COM * Return vale: smp devlink.
244*10652SHyon.Kim@Sun.COM */
245*10652SHyon.Kim@Sun.COM HBA_STATUS
lookupSMPLink(char * path,char * link)246*10652SHyon.Kim@Sun.COM lookupSMPLink(char *path, char *link)
247*10652SHyon.Kim@Sun.COM {
248*10652SHyon.Kim@Sun.COM const char dir[] = "/dev/smp";
249*10652SHyon.Kim@Sun.COM const char mname[] = ":smp";
250*10652SHyon.Kim@Sun.COM return (lookupLink(path, link, dir, mname));
251*10652SHyon.Kim@Sun.COM }
252