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 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
2N/A * Use is subject to license terms.
2N/A */
2N/A
2N/A/*
2N/A * Implements auxiliary routines declared in pcp_utils.h to facilitate
2N/A * finding the appropriate communication transport & device path for a
2N/A * given service. This supports the transition from the legacy service channel
2N/A * transport (glvc) to the logical domain channel (vldc) transport native
2N/A * to platforms running Logical Domains (LDoms).
2N/A */
2N/A
2N/A#include <fcntl.h>
2N/A#include <sys/types.h>
2N/A#include <sys/stat.h>
2N/A#include <strings.h>
2N/A#include <stdlib.h>
2N/A#include <libgen.h>
2N/A#include <unistd.h>
2N/A#include <stdio.h>
2N/A#include <libdevinfo.h>
2N/A
2N/A#include "pcp_utils.h"
2N/A
2N/Atypedef enum { false = 0, true = 1 } bool_t;
2N/A
2N/A#define SERVICE_PREFIX "SUNW,sun4v-"
2N/A#define DEVICES_DIR "/devices"
2N/A#define GLVC ":glvc"
2N/A#define VCHAN "virtual-channel@"
2N/A#define VCHAN_C "virtual-channel-client@"
2N/A
2N/A/*
2N/A * The mechanism to relate a service to a device path is different for
2N/A * vldc and glvc, due to the way the device pathnames are encoded:
2N/A * Sample service: sunvts
2N/A * Service Name: SUNW,sun4v-sunvts
2N/A * GLVC device path:
2N/A * "/devices/virtual-devices@100/sunvts@a:glvc"
2N/A * VLDC device path:
2N/A * "/devices/virtual-devices@100/channel-devices@200/virtual-channel@3:sunvts"
2N/A *
2N/A * As VLDC is the communication mechanism used in an LDoms environment, it is
2N/A * the preferred channel, and its existence is checked for first.
2N/A */
2N/A
2N/A/*
2N/A * Extract from dev_path the "service name" portion.
2N/A * For vldc, the service corresponds to the minor name of the device path
2N/A * (e.g. virtual-channel@3:sunvts for sunvts). If service is non-NULL, it must
2N/A * match the extracted service name for the function to succeed.
2N/A * The service name is returned in match (if non-NULL), and the function
2N/A * itself returns true on success; false on failure.
2N/A */
2N/Astatic bool_t
2N/Aget_vldc_svc_name(char *dev_path, char *service, char **match)
2N/A{
2N/A bool_t ret = false;
2N/A char *pathname = strdup(dev_path);
2N/A char *devname, *s;
2N/A
2N/A if (NULL == pathname)
2N/A return (false);
2N/A
2N/A devname = basename(pathname);
2N/A s = strrchr(devname, ':');
2N/A
2N/A if (s++ == NULL) {
2N/A goto end;
2N/A }
2N/A
2N/A if ((strncmp(devname, VCHAN, strlen(VCHAN)) == 0) ||
2N/A (strncmp(devname, VCHAN_C, strlen(VCHAN_C)) == 0)) {
2N/A /*
2N/A * If in addition, a service string is specified to
2N/A * be matched, do a comparison
2N/A */
2N/A if (service != NULL) {
2N/A if (strcmp(s, service) == 0) {
2N/A if (match)
2N/A *match = strdup(s);
2N/A ret = true;
2N/A goto end;
2N/A } else {
2N/A ret = false;
2N/A goto end;
2N/A }
2N/A } else if (match) {
2N/A *match = strdup(s);
2N/A }
2N/A
2N/A ret = true;
2N/A goto end;
2N/A }
2N/Aend:
2N/A
2N/A free(pathname);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * Extract from dev_path the "service name" portion.
2N/A * For glvc, the service corresponds to the node name of the device path
2N/A * (e.g. sunvts@a:glvc for sunvts). If service is non-NULL, it must
2N/A * match the extracted service name for the function to succeed.
2N/A * The service name is returned in match (if non-NULL), and the function
2N/A * itself returns true on success; false on failure.
2N/A */
2N/Astatic bool_t
2N/Aget_glvc_svc_name(char *dev_path, char *service, char **match)
2N/A{
2N/A bool_t ret = true;
2N/A char *pathname = strdup(dev_path);
2N/A char *devname, *substr, *t;
2N/A int len;
2N/A
2N/A if (NULL == pathname)
2N/A return (false);
2N/A
2N/A devname = basename(pathname);
2N/A substr = strstr(devname, GLVC);
2N/A
2N/A if (!((substr != NULL) && (strcmp(substr, GLVC) == 0))) {
2N/A ret = false;
2N/A goto end;
2N/A }
2N/A
2N/A if ((t = strrchr(devname, '@')) == NULL) {
2N/A ret = false;
2N/A goto end;
2N/A }
2N/A
2N/A len = t - devname;
2N/A
2N/A /*
2N/A * If a service string is specified, check if there
2N/A * is a match
2N/A */
2N/A if ((service != NULL) && (strncmp(devname, service, len) != 0))
2N/A ret = false;
2N/A
2N/A if ((ret == true) && (match != NULL)) {
2N/A *match = calloc(len + 1, 1);
2N/A if (*match)
2N/A (void) strncpy(*match, devname, len);
2N/A }
2N/A
2N/Aend:
2N/A free(pathname);
2N/A return (ret);
2N/A}
2N/A
2N/A/*
2N/A * This routine accepts either a prefixed service name or a legacy full
2N/A * pathname (which might not even exist in the filesystem), and in either case
2N/A * returns a canonical service name. If the parameter is neither a service
2N/A * name (i.e. with a "SUNW,sun4v-" prefix), nor a path to a legacy glvc or
2N/A * vldc device, NULL is returned.
2N/A */
2N/Achar *
2N/Aplatsvc_extract_svc_name(char *devname)
2N/A{
2N/A char *sname = NULL;
2N/A char *vldc_path, *glvc_path;
2N/A
2N/A /*
2N/A * First check whether a service name
2N/A */
2N/A if (strncmp(devname, SERVICE_PREFIX, strlen(SERVICE_PREFIX)) == 0) {
2N/A sname = strdup(devname + strlen(SERVICE_PREFIX));
2N/A return (sname);
2N/A }
2N/A
2N/A /*
2N/A * Not a service name, check if it's a valid pathname
2N/A */
2N/A if (!(devname[0] == '/' || devname[0] == '.')) {
2N/A return (NULL);
2N/A }
2N/A
2N/A /*
2N/A * Ideally, we should only check for a valid glvc pathname,
2N/A * requiring all vldc access to be only via service names. But
2N/A * to prevent a flag day with code that's already passing in
2N/A * vldc full pathnames (e.g. sunMC), we allow them here.
2N/A */
2N/A if (get_vldc_svc_name(devname, NULL, &vldc_path) == true) {
2N/A return (vldc_path);
2N/A } else if (get_glvc_svc_name(devname, NULL, &glvc_path) == true) {
2N/A return (glvc_path);
2N/A }
2N/A
2N/A return (NULL);
2N/A}
2N/A
2N/A/*
2N/A * Walk all "service" device nodes to find the one with the
2N/A * matching glvc minor name
2N/A */
2N/Astatic char *
2N/Asvc_name_to_glvc_dev_path(char *service)
2N/A{
2N/A di_node_t root_node, service_node;
2N/A char *glvc_path;
2N/A char *minor_name;
2N/A di_minor_t minor;
2N/A char *dev_path = NULL;
2N/A
2N/A if (service == NULL)
2N/A return (NULL);
2N/A
2N/A /* Ensure that the 'glvc' driver is loaded */
2N/A root_node = di_init_driver("glvc", DINFOCPYALL);
2N/A if (root_node == DI_NODE_NIL) {
2N/A return (dev_path);
2N/A }
2N/A
2N/A service_node = di_drv_first_node("glvc", root_node);
2N/A
2N/A while (service_node != DI_NODE_NIL) {
2N/A /* Make sure node name matches service name */
2N/A if (strcmp(service, di_node_name(service_node)) == 0) {
2N/A /* Walk minor nodes */
2N/A minor = di_minor_next(service_node, DI_NODE_NIL);
2N/A
2N/A while (minor != DI_NODE_NIL) {
2N/A glvc_path = di_devfs_minor_path(minor);
2N/A minor_name = di_minor_name(minor);
2N/A
2N/A if (strcmp(minor_name, "glvc") == 0) {
2N/A dev_path = malloc(strlen(glvc_path) +
2N/A strlen(DEVICES_DIR) + 1);
2N/A (void) strcpy(dev_path, DEVICES_DIR);
2N/A (void) strcat(dev_path, glvc_path);
2N/A di_devfs_path_free(glvc_path);
2N/A break;
2N/A }
2N/A
2N/A di_devfs_path_free(glvc_path);
2N/A minor = di_minor_next(service_node, minor);
2N/A }
2N/A }
2N/A if (dev_path != NULL)
2N/A break;
2N/A
2N/A service_node = di_drv_next_node(service_node);
2N/A }
2N/A
2N/A di_fini(root_node);
2N/A return (dev_path);
2N/A}
2N/A
2N/A/*
2N/A * Walk all vldc device nodes to find the one with the
2N/A * matching minor name
2N/A */
2N/Astatic char *
2N/Asvc_name_to_vldc_dev_path(char *service)
2N/A{
2N/A di_node_t root_node, vldc_node;
2N/A char *vldc_path;
2N/A char *minor_name;
2N/A di_minor_t minor;
2N/A char *dev_path = NULL;
2N/A
2N/A /* Ensure that the 'vldc' driver is loaded */
2N/A root_node = di_init_driver("vldc", DINFOCPYALL);
2N/A if (root_node == DI_NODE_NIL) {
2N/A return (dev_path);
2N/A }
2N/A
2N/A vldc_node = di_drv_first_node("vldc", root_node);
2N/A
2N/A while (vldc_node != DI_NODE_NIL) {
2N/A /* Walk minor nodes */
2N/A minor = di_minor_next(vldc_node, DI_NODE_NIL);
2N/A
2N/A while (minor != DI_NODE_NIL) {
2N/A vldc_path = di_devfs_minor_path(minor);
2N/A minor_name = di_minor_name(minor);
2N/A
2N/A if (strcmp(minor_name, service) == 0) {
2N/A dev_path = malloc(strlen(vldc_path) +
2N/A strlen(DEVICES_DIR) + 1);
2N/A (void) strcpy(dev_path, DEVICES_DIR);
2N/A (void) strcat(dev_path, vldc_path);
2N/A di_devfs_path_free(vldc_path);
2N/A break;
2N/A }
2N/A
2N/A di_devfs_path_free(vldc_path);
2N/A minor = di_minor_next(vldc_node, minor);
2N/A }
2N/A if (dev_path != NULL)
2N/A break;
2N/A
2N/A vldc_node = di_drv_next_node(vldc_node);
2N/A }
2N/A
2N/A di_fini(root_node);
2N/A return (dev_path);
2N/A}
2N/A
2N/A/*
2N/A * Given a service name or a full legacy pathname, return
2N/A * the full pathname to the appropriate vldc or glvc device.
2N/A */
2N/Achar *
2N/Aplatsvc_name_to_path(char *svc_or_path, pcp_xport_t *type)
2N/A{
2N/A char *pathn_p;
2N/A char *service;
2N/A
2N/A if ((service = platsvc_extract_svc_name(svc_or_path)) == NULL)
2N/A return (NULL);
2N/A
2N/A /*
2N/A * First lookup vldc nodes
2N/A */
2N/A pathn_p = svc_name_to_vldc_dev_path(service);
2N/A if (pathn_p != NULL) {
2N/A *type = VLDC_STREAMING;
2N/A } else {
2N/A /*
2N/A * If no vldc, try to find a glvc node
2N/A */
2N/A pathn_p = svc_name_to_glvc_dev_path(service);
2N/A if (pathn_p != NULL) {
2N/A *type = GLVC_NON_STREAM;
2N/A }
2N/A }
2N/A
2N/A free(service);
2N/A return (pathn_p);
2N/A}