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 2006 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 
26 #pragma ident	"%Z%%M%	%I%	%E% SMI"
27 
28 #include <pthread.h>
29 
30 #include <topo_module.h>
31 #include <topo_string.h>
32 #include <topo_builtin.h>
33 #include <topo_error.h>
34 #include <topo_subr.h>
35 
36 #include <cpu.h>
37 #include <hc.h>
38 #include <dev.h>
39 #include <mem.h>
40 #include <mod.h>
41 #include <pkg.h>
42 
43 static const struct topo_builtin _topo_builtins[] = {
44 	{ "cpu", CPU_VERSION, cpu_init, cpu_fini },
45 	{ "dev", DEV_VERSION, dev_init, dev_fini },
46 	{ "mem", MEM_VERSION, mem_init, mem_fini },
47 	{ "pkg", PKG_VERSION, pkg_init, pkg_fini },
48 	{ "mod", MOD_VERSION, mod_init, mod_fini },
49 	{ "hc", HC_VERSION, hc_init, hc_fini },		/* hc must go last */
50 	{ NULL, NULL, NULL }
51 };
52 
53 static int
54 bltin_init(topo_mod_t *mp, topo_version_t version)
55 {
56 	const topo_builtin_t *bp;
57 
58 	for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
59 		if (strcmp(mp->tm_name, bp->bltin_name) == 0)
60 			break;
61 	}
62 
63 	mp->tm_data = (void *)bp;
64 
65 	if ((*bp->bltin_init)(mp, version) != 0 || mp->tm_info == NULL) {
66 		if (mp->tm_errno == 0)
67 			(void) topo_mod_seterrno(mp, ETOPO_MOD_INIT);
68 		topo_dprintf(mp->tm_hdl, TOPO_DBG_ERR,
69 		    "unable initialize builtin module: %s: %s\n",
70 		    bp->bltin_name, topo_mod_errmsg(mp));
71 		return (-1);
72 	}
73 
74 	return (0);
75 }
76 
77 static int
78 bltin_fini(topo_mod_t *mp)
79 {
80 	topo_builtin_t *bp = mp->tm_data;
81 
82 	if (mp->tm_info != NULL) {
83 		(*bp->bltin_fini)(mp);
84 
85 	}
86 
87 	return (0);
88 }
89 
90 const topo_imodops_t topo_bltin_ops = {
91 	bltin_init,
92 	bltin_fini,
93 };
94 
95 /*ARGSUSED*/
96 int
97 topo_builtin_create(topo_hdl_t *thp, const char *rootdir)
98 {
99 	const topo_builtin_t *bp;
100 	topo_mod_t *mod;
101 	ttree_t *tp;
102 	tnode_t *rnode;
103 
104 	/*
105 	 * Create a scheme-specific topo tree for all builtins
106 	 */
107 	for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
108 
109 		/*
110 		 * Load scheme-specific module
111 		 */
112 		if ((mod = topo_modhash_load(thp, bp->bltin_name, NULL,
113 		    &topo_bltin_ops, bp->bltin_version)) == NULL) {
114 			topo_dprintf(thp, TOPO_DBG_ERR,
115 			    "unable to create scheme "
116 			    "tree for %s:%s\n", bp->bltin_name,
117 			    topo_hdl_errmsg(thp));
118 			return (-1);
119 		}
120 		if ((tp = topo_tree_create(thp, mod, bp->bltin_name))
121 		    == NULL) {
122 			topo_dprintf(thp, TOPO_DBG_ERR,
123 			    "unable to create scheme "
124 			    "tree for %s:%s\n", bp->bltin_name,
125 			    topo_hdl_errmsg(thp));
126 			return (-1);
127 		}
128 		topo_list_append(&thp->th_trees, tp);
129 
130 		/*
131 		 * Call the enumerator on the root of the tree, with the
132 		 * scheme name as the name to enumerate.  This will
133 		 * establish methods on the root node.
134 		 */
135 		rnode = tp->tt_root;
136 		if (topo_mod_enumerate(mod, rnode, mod->tm_name, rnode->tn_name,
137 		    rnode->tn_instance, rnode->tn_instance, NULL) < 0) {
138 			/*
139 			 * If we see a failure, note it in the handle and
140 			 * drive on
141 			 */
142 			(void) topo_hdl_seterrno(thp, ETOPO_ENUM_PARTIAL);
143 		}
144 
145 	}
146 
147 	return (0);
148 }
149