xref: /illumos-gate/usr/src/lib/fm/topo/libtopo/common/topo_builtin.c (revision a92282e44f968185a6bba094d1e5fece2da819cf)
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  * Copyright 2020 Joyent, Inc.
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 <sw.h>
43 #include <zfs.h>
44 
45 static const struct topo_builtin _topo_builtins[] = {
46 	{ "cpu", CPU_VERSION, cpu_init, cpu_fini, TOPO_BLTIN_TYPE_TREE },
47 	{ "dev", DEV_VERSION, dev_init, dev_fini, TOPO_BLTIN_TYPE_TREE },
48 	{ "fmd", FMD_VERSION, fmd_init, fmd_fini, TOPO_BLTIN_TYPE_TREE },
49 	{ "mem", MEM_VERSION, mem_init, mem_fini, TOPO_BLTIN_TYPE_TREE },
50 	{ "pkg", PKG_VERSION, pkg_init, pkg_fini, TOPO_BLTIN_TYPE_TREE },
51 	{ "svc", SVC_VERSION, svc_init, svc_fini, TOPO_BLTIN_TYPE_TREE },
52 	{ "sw", SW_VERSION, sw_init, sw_fini, TOPO_BLTIN_TYPE_TREE },
53 	{ "zfs", ZFS_VERSION, zfs_init, zfs_fini, TOPO_BLTIN_TYPE_TREE },
54 	{ "mod", MOD_VERSION, mod_init, mod_fini, TOPO_BLTIN_TYPE_TREE },
55 	/* hc must go last */
56 	{ "hc", HC_VERSION, hc_init, hc_fini, TOPO_BLTIN_TYPE_TREE },
57 	{ NULL, 0, NULL, NULL, 0 }
58 };
59 
60 static int
61 bltin_init(topo_mod_t *mp, topo_version_t version)
62 {
63 	const topo_builtin_t *bp;
64 
65 	for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
66 		if (strcmp(mp->tm_name, bp->bltin_name) == 0)
67 			break;
68 	}
69 
70 	mp->tm_data = (void *)bp;
71 
72 	if ((*bp->bltin_init)(mp, version) != 0 || mp->tm_info == NULL) {
73 		if (mp->tm_errno == 0)
74 			(void) topo_mod_seterrno(mp, ETOPO_MOD_INIT);
75 		topo_dprintf(mp->tm_hdl, TOPO_DBG_ERR,
76 		    "unable initialize builtin module: %s: %s\n",
77 		    bp->bltin_name, topo_mod_errmsg(mp));
78 		return (-1);
79 	}
80 
81 	return (0);
82 }
83 
84 static int
85 bltin_fini(topo_mod_t *mp)
86 {
87 	topo_builtin_t *bp = mp->tm_data;
88 
89 	if (mp->tm_info != NULL) {
90 		(*bp->bltin_fini)(mp);
91 
92 	}
93 
94 	return (0);
95 }
96 
97 const topo_imodops_t topo_bltin_ops = {
98 	bltin_init,
99 	bltin_fini,
100 };
101 
102 /*ARGSUSED*/
103 int
104 topo_builtin_create(topo_hdl_t *thp, const char *rootdir)
105 {
106 	const topo_builtin_t *bp;
107 	topo_mod_t *mod;
108 	ttree_t *tp;
109 	tnode_t *rnode;
110 	topo_digraph_t *tdg;
111 
112 	/*
113 	 * Create a scheme-specific topo tree for all builtins
114 	 */
115 	for (bp = _topo_builtins; bp->bltin_name != NULL; bp++) {
116 
117 		/*
118 		 * Load scheme-specific module
119 		 */
120 		if ((mod = topo_modhash_load(thp, bp->bltin_name, NULL,
121 		    &topo_bltin_ops, bp->bltin_version)) == NULL) {
122 			topo_dprintf(thp, TOPO_DBG_ERR,
123 			    "unable to create scheme "
124 			    "topology for %s:%s\n", bp->bltin_name,
125 			    topo_hdl_errmsg(thp));
126 			return (-1);
127 		}
128 		switch (bp->bltin_type) {
129 		case TOPO_BLTIN_TYPE_TREE:
130 			if ((tp = topo_tree_create(thp, mod, bp->bltin_name))
131 			    == NULL) {
132 				topo_dprintf(thp, TOPO_DBG_ERR, "unable to "
133 				    "create scheme tree for %s:%s\n",
134 				    bp->bltin_name, topo_hdl_errmsg(thp));
135 				return (-1);
136 			}
137 			topo_list_append(&thp->th_trees, tp);
138 
139 			rnode = tp->tt_root;
140 			break;
141 		case TOPO_BLTIN_TYPE_DIGRAPH:
142 			if ((tdg = topo_digraph_new(thp, mod, bp->bltin_name))
143 			    == NULL) {
144 				topo_dprintf(thp, TOPO_DBG_ERR, "unable to "
145 				    "create scheme digraph for %s:%s\n",
146 				    bp->bltin_name, topo_hdl_errmsg(thp));
147 				return (-1);
148 			}
149 			topo_list_append(&thp->th_digraphs, tdg);
150 
151 			rnode = tdg->tdg_rootnode;
152 			break;
153 		default:
154 			topo_dprintf(thp, TOPO_DBG_ERR, "unexpected topology "
155 			    "type: %u", bp->bltin_type);
156 			return (-1);
157 		}
158 		if (topo_mod_enumerate(mod, rnode, mod->tm_name,
159 		    rnode->tn_name, rnode->tn_instance, rnode->tn_instance,
160 		    NULL) < 0) {
161 			/*
162 			 * If we see a failure, note it in the handle and drive
163 			 * on
164 			 */
165 			(void) topo_hdl_seterrno(thp, ETOPO_ENUM_PARTIAL);
166 		}
167 	}
168 
169 	return (0);
170 }
171