7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * CDDL HEADER START
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi *
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * The contents of this file are subject to the terms of the
74a31ce69a88d851777ca840d31ec610ce9ecab3timh * Common Development and Distribution License (the "License").
74a31ce69a88d851777ca840d31ec610ce9ecab3timh * 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/*
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <pthread.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_module.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_string.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_builtin.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_error.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi#include <topo_subr.h>
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <cpu.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <hc.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <dev.h>
95efa359b507db290a2484b8f615822b1e06096fEric Schrock#include <fmd.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <mem.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <mod.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi#include <pkg.h>
95efa359b507db290a2484b8f615822b1e06096fEric Schrock#include <svc.h>
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby#include <sw.h>
95efa359b507db290a2484b8f615822b1e06096fEric Schrock#include <zfs.h>
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic const struct topo_builtin _topo_builtins[] = {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi { "cpu", CPU_VERSION, cpu_init, cpu_fini },
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi { "dev", DEV_VERSION, dev_init, dev_fini },
95efa359b507db290a2484b8f615822b1e06096fEric Schrock { "fmd", FMD_VERSION, fmd_init, fmd_fini },
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi { "mem", MEM_VERSION, mem_init, mem_fini },
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi { "pkg", PKG_VERSION, pkg_init, pkg_fini },
95efa359b507db290a2484b8f615822b1e06096fEric Schrock { "svc", SVC_VERSION, svc_init, svc_fini },
f6e214c7418f43af38bd8c3a557e3d0a1d311cfaGavin Maltby { "sw", SW_VERSION, sw_init, sw_fini },
95efa359b507db290a2484b8f615822b1e06096fEric Schrock { "zfs", ZFS_VERSION, zfs_init, zfs_fini },
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi { "mod", MOD_VERSION, mod_init, mod_fini },
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi { "hc", HC_VERSION, hc_init, hc_fini }, /* hc must go last */
9dd0f810214fdc8e1af881a9a5c4b6927629ff9ecindi { NULL, 0, NULL, NULL }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi};
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic int
0eb822a1c0c2bea495647510b75f77f0e57633ebcindibltin_init(topo_mod_t *mp, topo_version_t version)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi const topo_builtin_t *bp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (strcmp(mp->tm_name, bp->bltin_name) == 0)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi break;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi mp->tm_data = (void *)bp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((*bp->bltin_init)(mp, version) != 0 || mp->tm_info == NULL) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if (mp->tm_errno == 0)
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi (void) topo_mod_seterrno(mp, ETOPO_MOD_INIT);
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(mp->tm_hdl, TOPO_DBG_ERR,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "unable initialize builtin module: %s: %s\n",
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi bp->bltin_name, topo_mod_errmsg(mp));
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi return (-1);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindistatic int
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindibltin_fini(topo_mod_t *mp)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_builtin_t *bp = mp->tm_data;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (mp->tm_info != NULL) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (*bp->bltin_fini)(mp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
0eb822a1c0c2bea495647510b75f77f0e57633ebcindiconst topo_imodops_t topo_bltin_ops = {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi bltin_init,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi bltin_fini,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi};
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi/*ARGSUSED*/
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindiint
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecinditopo_builtin_create(topo_hdl_t *thp, const char *rootdir)
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi{
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi const topo_builtin_t *bp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_mod_t *mod;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi ttree_t *tp;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi tnode_t *rnode;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Create a scheme-specific topo tree for all builtins
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Load scheme-specific module
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi if ((mod = topo_modhash_load(thp, bp->bltin_name, NULL,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi &topo_bltin_ops, bp->bltin_version)) == NULL) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(thp, TOPO_DBG_ERR,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "unable to create scheme "
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "tree for %s:%s\n", bp->bltin_name,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_hdl_errmsg(thp));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (-1);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if ((tp = topo_tree_create(thp, mod, bp->bltin_name))
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi == NULL) {
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi topo_dprintf(thp, TOPO_DBG_ERR,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi "unable to create scheme "
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi "tree for %s:%s\n", bp->bltin_name,
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_hdl_errmsg(thp));
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (-1);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi topo_list_append(&thp->th_trees, tp);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * Call the enumerator on the root of the tree, with the
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * scheme name as the name to enumerate. This will
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * establish methods on the root node.
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi rnode = tp->tt_root;
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi if (topo_mod_enumerate(mod, rnode, mod->tm_name, rnode->tn_name,
0eb822a1c0c2bea495647510b75f77f0e57633ebcindi rnode->tn_instance, rnode->tn_instance, NULL) < 0) {
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi /*
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * If we see a failure, note it in the handle and
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi * drive on
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi */
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi (void) topo_hdl_seterrno(thp, ETOPO_ENUM_PARTIAL);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi }
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi return (0);
7aec1d6e253b21f9e9b7ef68b4d81ab9859b51fecindi}