xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_builtin.c (revision 46b592853d0f4f11781b6b0a7533f267c6aee132)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 /*
22  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #include <pthread.h>
27 
28 #include <topo_module.h>
29 #include <topo_string.h>
30 #include <topo_builtin.h>
31 #include <topo_error.h>
32 #include <topo_subr.h>
33 
34 #include <cpu.h>
35 #include <hc.h>
36 #include <dev.h>
37 #include <fmd.h>
38 #include <mem.h>
39 #include <mod.h>
40 #include <pkg.h>
41 #include <svc.h>
42 #include <zfs.h>
43 
44 static const struct topo_builtin _topo_builtins[] = {
45 	{ "cpu", CPU_VERSION, cpu_init, cpu_fini },
46 	{ "dev", DEV_VERSION, dev_init, dev_fini },
47 	{ "fmd", FMD_VERSION, fmd_init, fmd_fini },
48 	{ "mem", MEM_VERSION, mem_init, mem_fini },
49 	{ "pkg", PKG_VERSION, pkg_init, pkg_fini },
50 	{ "svc", SVC_VERSION, svc_init, svc_fini },
51 	{ "zfs", ZFS_VERSION, zfs_init, zfs_fini },
52 	{ "mod", MOD_VERSION, mod_init, mod_fini },
53 	{ "hc", HC_VERSION, hc_init, hc_fini },		/* hc must go last */
54 	{ NULL, 0, NULL, NULL }
55 };
56 
57 static int
58 bltin_init(topo_mod_t *mp, topo_version_t version)
59 {
60 	const topo_builtin_t *bp;
61 
62 	for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
63 		if (strcmp(mp->tm_name, bp->bltin_name) == 0)
64 			break;
65 	}
66 
67 	mp->tm_data = (void *)bp;
68 
69 	if ((*bp->bltin_init)(mp, version) != 0 || mp->tm_info == NULL) {
70 		if (mp->tm_errno == 0)
71 			(void) topo_mod_seterrno(mp, ETOPO_MOD_INIT);
72 		topo_dprintf(mp->tm_hdl, TOPO_DBG_ERR,
73 		    "unable initialize builtin module: %s: %s\n",
74 		    bp->bltin_name, topo_mod_errmsg(mp));
75 		return (-1);
76 	}
77 
78 	return (0);
79 }
80 
81 static int
82 bltin_fini(topo_mod_t *mp)
83 {
84 	topo_builtin_t *bp = mp->tm_data;
85 
86 	if (mp->tm_info != NULL) {
87 		(*bp->bltin_fini)(mp);
88 
89 	}
90 
91 	return (0);
92 }
93 
94 const topo_imodops_t topo_bltin_ops = {
95 	bltin_init,
96 	bltin_fini,
97 };
98 
99 /*ARGSUSED*/
100 int
101 topo_builtin_create(topo_hdl_t *thp, const char *rootdir)
102 {
103 	const topo_builtin_t *bp;
104 	topo_mod_t *mod;
105 	ttree_t *tp;
106 	tnode_t *rnode;
107 
108 	/*
109 	 * Create a scheme-specific topo tree for all builtins
110 	 */
111 	for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
112 
113 		/*
114 		 * Load scheme-specific module
115 		 */
116 		if ((mod = topo_modhash_load(thp, bp->bltin_name, NULL,
117 		    &topo_bltin_ops, bp->bltin_version)) == NULL) {
118 			topo_dprintf(thp, TOPO_DBG_ERR,
119 			    "unable to create scheme "
120 			    "tree for %s:%s\n", bp->bltin_name,
121 			    topo_hdl_errmsg(thp));
122 			return (-1);
123 		}
124 		if ((tp = topo_tree_create(thp, mod, bp->bltin_name))
125 		    == NULL) {
126 			topo_dprintf(thp, TOPO_DBG_ERR,
127 			    "unable to create scheme "
128 			    "tree for %s:%s\n", bp->bltin_name,
129 			    topo_hdl_errmsg(thp));
130 			return (-1);
131 		}
132 		topo_list_append(&thp->th_trees, tp);
133 
134 		/*
135 		 * Call the enumerator on the root of the tree, with the
136 		 * scheme name as the name to enumerate.  This will
137 		 * establish methods on the root node.
138 		 */
139 		rnode = tp->tt_root;
140 		if (topo_mod_enumerate(mod, rnode, mod->tm_name, rnode->tn_name,
141 		    rnode->tn_instance, rnode->tn_instance, NULL) < 0) {
142 			/*
143 			 * If we see a failure, note it in the handle and
144 			 * drive on
145 			 */
146 			(void) topo_hdl_seterrno(thp, ETOPO_ENUM_PARTIAL);
147 		}
148 
149 	}
150 
151 	return (0);
152 }
153