2N/A/*
2N/A * CDDL HEADER START
2N/A *
2N/A * The contents of this file are subject to the terms of the
2N/A * Common Development and Distribution License (the "License").
2N/A * You may not use this file except in compliance with the License.
2N/A *
2N/A * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
2N/A * or http://www.opensolaris.org/os/licensing.
2N/A * See the License for the specific language governing permissions
2N/A * and limitations under the License.
2N/A *
2N/A * When distributing Covered Code, include this CDDL HEADER in each
2N/A * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
2N/A * If applicable, add the following below this CDDL HEADER, with the
2N/A * fields enclosed by brackets "[]" replaced with your own identifying
2N/A * information: Portions Copyright [yyyy] [name of copyright owner]
2N/A *
2N/A * CDDL HEADER END
2N/A */
2N/A
2N/A/*
2N/A * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A#include <sun_sas.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <fcntl.h>
2N/A#include <unistd.h>
2N/A#include <dirent.h>
2N/A#include <libdevinfo.h>
2N/A
2N/A/*
2N/A * structure for di_devlink_walk
2N/A */
2N/Atypedef struct walk_devlink {
2N/A char *path;
2N/A size_t len;
2N/A char **linkpp;
2N/A} walk_devlink_t;
2N/A
2N/A/*
2N/A * callback funtion for di_devlink_walk
2N/A * Find matching /dev link for the given path argument.
2N/A * devlink element and callback function argument.
2N/A * The input path is expected to not have "/devices".
2N/A */
2N/Astatic int
2N/Aget_devlink(di_devlink_t devlink, void *arg)
2N/A{
2N/A const char ROUTINE[] = "get_devlink";
2N/A walk_devlink_t *warg = (walk_devlink_t *)arg;
2N/A
2N/A /*
2N/A * When path is specified, it doesn't have minor
2N/A * name. Therefore, the ../.. prefixes needs to be stripped.
2N/A */
2N/A if (warg->path) {
2N/A char *content = (char *)di_devlink_content(devlink);
2N/A char *start = strstr(content, "/devices");
2N/A
2N/A if (start == NULL ||
2N/A strncmp(start, warg->path, warg->len) != 0 ||
2N/A /* make it sure the device path has minor name */
2N/A start[warg->len] != ':') {
2N/A return (DI_WALK_CONTINUE);
2N/A }
2N/A }
2N/A
2N/A *(warg->linkpp) = strdup(di_devlink_path(devlink));
2N/A log(LOG_DEBUG, ROUTINE, "Walk terminate");
2N/A return (DI_WALK_TERMINATE);
2N/A}
2N/A
2N/A/*
2N/A * Convert /devices paths to /dev sym-link paths.
2N/A * The mapping buffer OSDeviceName paths will be
2N/A * converted to short names.
2N/A * mappings The target mappings data to convert to short names
2N/A *
2N/A * If no link is found, the long path is left as is.
2N/A * Note: The NumberOfEntries field MUST not be greater than the size
2N/A * of the array passed in.
2N/A */
2N/Avoid
2N/AconvertDevpathToDevlink(PSMHBA_TARGETMAPPING mappings)
2N/A{
2N/A const char ROUTINE[] = "convertDevpathToLink";
2N/A di_devlink_handle_t hdl;
2N/A walk_devlink_t warg;
2N/A int j;
2N/A char *minor_path, *devlinkp;
2N/A
2N/A if ((hdl = di_devlink_init(NULL, 0)) == NULL) {
2N/A log(LOG_DEBUG, ROUTINE, "di_devlink failed: errno:%d",
2N/A strerror(errno));
2N/A return;
2N/A }
2N/A
2N/A for (j = 0; j < mappings->NumberOfEntries; j++) {
2N/A if (strchr(mappings->entry[j].ScsiId.OSDeviceName, ':')) {
2N/A /* search link for minor node */
2N/A minor_path = mappings->entry[j].ScsiId.OSDeviceName;
2N/A if (strstr(minor_path, "/devices") != NULL) {
2N/A minor_path = mappings->entry[j].ScsiId.
2N/A OSDeviceName + strlen("/devices");
2N/A }
2N/A warg.path = NULL;
2N/A } else {
2N/A minor_path = NULL;
2N/A if (strstr(mappings->entry[j].ScsiId.OSDeviceName,
2N/A "/devices") != NULL) {
2N/A warg.len = strlen(mappings->entry[j].ScsiId.
2N/A OSDeviceName) - strlen("/devices");
2N/A warg.path = mappings->entry[j].
2N/A ScsiId.OSDeviceName + strlen("/devices");
2N/A } else {
2N/A warg.len = strlen(mappings->entry[j].ScsiId.
2N/A OSDeviceName);
2N/A warg.path = mappings->entry[j].ScsiId.
2N/A OSDeviceName;
2N/A }
2N/A }
2N/A
2N/A devlinkp = NULL;
2N/A warg.linkpp = &devlinkp;
2N/A (void) di_devlink_walk(hdl, NULL, minor_path, DI_PRIMARY_LINK,
2N/A (void *)&warg, get_devlink);
2N/A
2N/A if (devlinkp != NULL) {
2N/A (void) snprintf(mappings->entry[j].ScsiId.OSDeviceName,
2N/A sizeof (mappings->entry[j].ScsiId.OSDeviceName),
2N/A "%s", devlinkp);
2N/A free(devlinkp);
2N/A }
2N/A
2N/A }
2N/A
2N/A (void) di_devlink_fini(&hdl);
2N/A}
2N/A
2N/A/*
2N/A * Finds controller path for a give device path.
2N/A *
2N/A * Return value: /dev link for dir and minor name.
2N/A */
2N/Astatic HBA_STATUS
2N/AlookupLink(char *path, char *link, const char *dir, const char *mname)
2N/A{
2N/A const char ROUTINE[] = "lookupLink";
2N/A DIR *dp;
2N/A char buf[MAXPATHLEN];
2N/A char node[MAXPATHLEN];
2N/A char *charptr;
2N/A struct dirent *newdirp, *dirp;
2N/A ssize_t count;
2N/A int dirplen;
2N/A char *subpath;
2N/A char tmpPath[MAXPATHLEN];
2N/A
2N/A if ((dp = opendir(dir)) == NULL) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "Unable to open %s to find controller number.", dir);
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A
2N/A if (link == NULL) {
2N/A log(LOG_DEBUG, ROUTINE,
2N/A "Invalid argument for storing the link.");
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A
2N/A /*
2N/A * dirplen is large enough to fit the largest path-
2N/A * struct dirent includes one byte (the terminator)
2N/A * so we don't add 1 to the calculation here.
2N/A */
2N/A dirplen = pathconf(dir, _PC_NAME_MAX);
2N/A dirplen = ((dirplen <= 0) ? MAXNAMELEN : dirplen) +
2N/A sizeof (struct dirent);
2N/A dirp = (struct dirent *)malloc(dirplen);
2N/A if (dirp == NULL) {
2N/A OUT_OF_MEMORY(ROUTINE);
2N/A return (HBA_STATUS_ERROR);
2N/A }
2N/A
2N/A while ((readdir_r(dp, dirp, &newdirp)) == 0 && newdirp != NULL) {
2N/A if (strcmp(dirp->d_name, ".") == 0 ||
2N/A strcmp(dirp->d_name, "..") == 0) {
2N/A continue;
2N/A }
2N/A /*
2N/A * set to another pointer since dirp->d_name length is 1
2N/A * that will store only the first char 'c' from the name.
2N/A */
2N/A charptr = dirp->d_name;
2N/A (void) snprintf(node, strlen(charptr) + strlen(dir) + 2,
2N/A "%s/%s", dir, charptr);
2N/A if (count = readlink(node, buf, sizeof (buf))) {
2N/A subpath = NULL;
2N/A subpath = strstr(buf, path);
2N/A buf[count] = '\0';
2N/A if (subpath != NULL) {
2N/A (void) strlcpy(tmpPath, path, MAXPATHLEN);
2N/A (void) strlcat(tmpPath, mname, MAXPATHLEN);
2N/A /*
2N/A * if device path has substring of path
2N/A * and exactally matching with :scsi suffix
2N/A */
2N/A if (strcmp(subpath, tmpPath) == 0) {
2N/A (void) strlcpy(link, node, MAXPATHLEN);
2N/A (void) closedir(dp);
2N/A S_FREE(dirp);
2N/A return (HBA_STATUS_OK);
2N/A }
2N/A }
2N/A }
2N/A }
2N/A
2N/A (void) closedir(dp);
2N/A S_FREE(dirp);
2N/A return (HBA_STATUS_ERROR);
2N/A}
2N/A
2N/A/*
2N/A * Finds controller path for a give device path.
2N/A *
2N/A * Return vale:i smp devlink.
2N/A */
2N/AHBA_STATUS
2N/AlookupControllerLink(char *path, char *link)
2N/A{
2N/A const char dir[] = "/dev/cfg";
2N/A const char mname[] = ":scsi";
2N/A return (lookupLink(path, link, dir, mname));
2N/A}
2N/A
2N/A/*
2N/A * Finds smp devlink for a give smp path.
2N/A *
2N/A * Return vale: smp devlink.
2N/A */
2N/AHBA_STATUS
2N/AlookupSMPLink(char *path, char *link)
2N/A{
2N/A const char dir[] = "/dev/smp";
2N/A const char mname[] = ":smp";
2N/A return (lookupLink(path, link, dir, mname));
2N/A}