7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER START
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * The contents of this file are subject to the terms of the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Common Development and Distribution License (the "License").
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * You may not use this file except in compliance with the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * or http://www.opensolaris.org/os/licensing.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * See the License for the specific language governing permissions
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * and limitations under the License.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * When distributing Covered Code, include this CDDL HEADER in each
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * If applicable, add the following below this CDDL HEADER, with the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * fields enclosed by brackets "[]" replaced with your own identifying
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * information: Portions Copyright [yyyy] [name of copyright owner]
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER END
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj * Copyright 2007 Sun Microsystems, Inc. All rights reserved.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Use is subject to license terms.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#pragma ident "%Z%%M% %I% %E% SMI"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <limits.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <string.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <unistd.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <sys/param.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_error.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_tree.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_subr.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_file.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * topo_file.c
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * This file hides the details of any file manipulation to
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi * establish topology for a given enumerator.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#define TOPO_DEFAULT_FILE "maps/%s-%s-topology.xml"
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#define TOPO_COMMON_FILE "maps/%s-topology.xml"
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindistatic void
0eb822a1c0c2bea495647510b75f77f0e57633ebcinditopo_file_unload(topo_file_t *tfp)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (tfp == NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (tfp->tf_filenm != NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_mod_strfree(tfp->tf_mod, tfp->tf_filenm);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (tfp->tf_tmap != NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi tf_info_free(tfp->tf_mod, tfp->tf_tmap);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_mod_free(tfp->tf_mod, tfp, sizeof (topo_file_t));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiint
0eb822a1c0c2bea495647510b75f77f0e57633ebcinditopo_file_load(topo_mod_t *mod, tnode_t *node, const char *name,
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj const char *scheme, int pmap)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_file_t *tfp;
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi char fp[MAXNAMELEN];
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((tfp = topo_mod_zalloc(mod, sizeof (topo_file_t))) == NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return (topo_mod_seterrno(mod, ETOPO_NOMEM));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi tfp->tf_mod = mod;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (name != NULL)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi (void) snprintf(fp, MAXNAMELEN, TOPO_DEFAULT_FILE, name,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi scheme);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi else
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi (void) snprintf(fp, MAXNAMELEN, TOPO_COMMON_FILE, scheme);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((tfp->tf_filenm = topo_search_path(mod, mod->tm_rootdir, fp))
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi == NULL) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_file_unload(tfp);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return (topo_mod_seterrno(mod, ETOPO_MOD_NOENT));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((tfp->tf_tmap = topo_xml_read(mod, tfp->tf_filenm, scheme))
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi == NULL) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "failed to load topology file %s: "
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "%s\n", tfp->tf_filenm, topo_strerror(ETOPO_MOD_XRD));
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_file_unload(tfp);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return (topo_mod_seterrno(mod, ETOPO_MOD_XRD));
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj if (pmap)
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj tfp->tf_tmap->tf_flags |= TF_PROPMAP;
4557a2a1868181b517f5dfe61ba6eeba58edf4c0robj
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (topo_xml_enum(mod, tfp->tf_tmap, node) < 0) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(mod->tm_hdl, TOPO_DBG_ERR,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "Failed to enumerate topology: %s\n",
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_strerror(ETOPO_MOD_XENUM));
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_file_unload(tfp);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return (topo_mod_seterrno(mod, ETOPO_MOD_XENUM));
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_file_unload(tfp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}