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 static const struct topo_builtin _topo_builtins[] = {
37 	{ "cpu", cpu_init, cpu_fini },
38 	{ "dev", dev_init, dev_fini },
39 	{ "mem", mem_init, mem_fini },
40 	{ "pkg", pkg_init, pkg_fini },
41 	{ "mod", mod_init, mod_fini },
42 	{ "hc", hc_init, hc_fini },
43 	{ NULL, NULL, NULL }
44 };
45 
46 static int
47 bltin_init(topo_mod_t *mp)
48 {
49 	const topo_builtin_t *bp;
50 
51 	for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
52 		if (strcmp(mp->tm_name, bp->bltin_name) == 0)
53 			break;
54 	}
55 
56 	mp->tm_data = (void *)bp;
57 
58 	(*bp->bltin_init)(mp);
59 
60 	if (mp->tm_info == NULL) {
61 		topo_dprintf(TOPO_DBG_ERR,
62 		    "unable initialize builtin module: %s\n", bp->bltin_name);
63 		return (topo_mod_seterrno(mp, ETOPO_MOD_INIT));
64 	}
65 
66 	return (0);
67 }
68 
69 static int
70 bltin_fini(topo_mod_t *mp)
71 {
72 	topo_builtin_t *bp = mp->tm_data;
73 
74 	if (mp->tm_info != NULL) {
75 		(*bp->bltin_fini)(mp);
76 
77 	}
78 
79 	return (0);
80 }
81 
82 const topo_modops_t topo_bltin_ops = {
83 	bltin_init,
84 	bltin_fini,
85 };
86 
87 /*ARGSUSED*/
88 int
89 topo_builtin_create(topo_hdl_t *thp, const char *rootdir)
90 {
91 	const topo_builtin_t *bp;
92 	topo_mod_t *mod;
93 	ttree_t *tp;
94 	tnode_t *rnode;
95 
96 	/*
97 	 * Create a scheme-specific topo tree for all builtins
98 	 */
99 	for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
100 
101 		/*
102 		 * Load scheme-specific module
103 		 */
104 		if ((mod = topo_modhash_load(thp, bp->bltin_name,
105 		    &topo_bltin_ops)) == NULL) {
106 			topo_dprintf(TOPO_DBG_ERR, "unable to create scheme "
107 			    "tree for %s:%s\n", bp->bltin_name,
108 			    topo_hdl_errmsg(thp));
109 			return (-1);
110 		}
111 		if ((tp = topo_tree_create(thp, mod, bp->bltin_name))
112 		    == NULL) {
113 			topo_dprintf(TOPO_DBG_ERR, "unable to create scheme "
114 			    "tree for %s:%s\n", bp->bltin_name,
115 			    topo_hdl_errmsg(thp));
116 			return (-1);
117 		}
118 		topo_list_append(&thp->th_trees, tp);
119 
120 		/*
121 		 * Call the enumerator on the root of the tree, with the
122 		 * scheme name as the name to enumerate.  This will
123 		 * establish methods on the root node.
124 		 */
125 		rnode = tp->tt_root;
126 		if (topo_mod_enumerate(mod, rnode, mod->tm_name, rnode->tn_name,
127 		    rnode->tn_instance, rnode->tn_instance) < 0) {
128 			/*
129 			 * If we see a failure, note it in the handle and
130 			 * drive on
131 			 */
132 			(void) topo_hdl_seterrno(thp, ETOPO_ENUM_PARTIAL);
133 		}
134 
135 	}
136 
137 	return (0);
138 }
139