xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_builtin.c (revision 826ac02a0def83e0a41b29321470d299c7389aab)
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 (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
23  */
24 
25 #include <pthread.h>
26 
27 #include <topo_module.h>
28 #include <topo_string.h>
29 #include <topo_builtin.h>
30 #include <topo_error.h>
31 #include <topo_subr.h>
32 
33 #include <cpu.h>
34 #include <hc.h>
35 #include <dev.h>
36 #include <fmd.h>
37 #include <mem.h>
38 #include <mod.h>
39 #include <pkg.h>
40 #include <svc.h>
41 #include <sw.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 	{ "sw", SW_VERSION, sw_init, sw_fini },
52 	{ "zfs", ZFS_VERSION, zfs_init, zfs_fini },
53 	{ "mod", MOD_VERSION, mod_init, mod_fini },
54 	{ "hc", HC_VERSION, hc_init, hc_fini },		/* hc must go last */
55 	{ NULL, 0, NULL, NULL }
56 };
57 
58 static int
59 bltin_init(topo_mod_t *mp, topo_version_t version)
60 {
61 	const topo_builtin_t *bp;
62 
63 	for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
64 		if (strcmp(mp->tm_name, bp->bltin_name) == 0)
65 			break;
66 	}
67 
68 	mp->tm_data = (void *)bp;
69 
70 	if ((*bp->bltin_init)(mp, version) != 0 || mp->tm_info == NULL) {
71 		if (mp->tm_errno == 0)
72 			(void) topo_mod_seterrno(mp, ETOPO_MOD_INIT);
73 		topo_dprintf(mp->tm_hdl, TOPO_DBG_ERR,
74 		    "unable initialize builtin module: %s: %s\n",
75 		    bp->bltin_name, topo_mod_errmsg(mp));
76 		return (-1);
77 	}
78 
79 	return (0);
80 }
81 
82 static int
83 bltin_fini(topo_mod_t *mp)
84 {
85 	topo_builtin_t *bp = mp->tm_data;
86 
87 	if (mp->tm_info != NULL) {
88 		(*bp->bltin_fini)(mp);
89 
90 	}
91 
92 	return (0);
93 }
94 
95 const topo_imodops_t topo_bltin_ops = {
96 	bltin_init,
97 	bltin_fini,
98 };
99 
100 /*ARGSUSED*/
101 int
102 topo_builtin_create(topo_hdl_t *thp, const char *rootdir)
103 {
104 	const topo_builtin_t *bp;
105 	topo_mod_t *mod;
106 	ttree_t *tp;
107 	tnode_t *rnode;
108 
109 	/*
110 	 * Create a scheme-specific topo tree for all builtins
111 	 */
112 	for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
113 
114 		/*
115 		 * Load scheme-specific module
116 		 */
117 		if ((mod = topo_modhash_load(thp, bp->bltin_name, NULL,
118 		    &topo_bltin_ops, bp->bltin_version)) == NULL) {
119 			topo_dprintf(thp, TOPO_DBG_ERR,
120 			    "unable to create scheme "
121 			    "tree for %s:%s\n", bp->bltin_name,
122 			    topo_hdl_errmsg(thp));
123 			return (-1);
124 		}
125 		if ((tp = topo_tree_create(thp, mod, bp->bltin_name))
126 		    == NULL) {
127 			topo_dprintf(thp, TOPO_DBG_ERR,
128 			    "unable to create scheme "
129 			    "tree for %s:%s\n", bp->bltin_name,
130 			    topo_hdl_errmsg(thp));
131 			return (-1);
132 		}
133 		topo_list_append(&thp->th_trees, tp);
134 
135 		/*
136 		 * Call the enumerator on the root of the tree, with the
137 		 * scheme name as the name to enumerate.  This will
138 		 * establish methods on the root node.
139 		 */
140 		rnode = tp->tt_root;
141 		if (topo_mod_enumerate(mod, rnode, mod->tm_name, rnode->tn_name,
142 		    rnode->tn_instance, rnode->tn_instance, NULL) < 0) {
143 			/*
144 			 * If we see a failure, note it in the handle and
145 			 * drive on
146 			 */
147 			(void) topo_hdl_seterrno(thp, ETOPO_ENUM_PARTIAL);
148 		}
149 
150 	}
151 
152 	return (0);
153 }
154