xref: /illumos-gate/usr/src/lib/fm/topo/modules/common/hostbridge/hostbridge.c (revision 6e375c8351497b82ffa4f33cbf61d712999b4605)
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 /*
23  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <string.h>
30 #include <fm/topo_mod.h>
31 #include <fm/topo_hc.h>
32 #include <libdevinfo.h>
33 #include <limits.h>
34 #include <sys/fm/protocol.h>
35 #include <sys/param.h>
36 #include <sys/systeminfo.h>
37 #include <assert.h>
38 
39 #include <hostbridge.h>
40 #include <pcibus.h>
41 #include <did.h>
42 #include <did_props.h>
43 #include <util.h>
44 
45 /*
46  * hostbridge.c
47  *	Generic code shared by all the hostbridge enumerators
48  */
49 static void hb_release(topo_mod_t *, tnode_t *);
50 static int hb_label(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
51     nvlist_t **);
52 static int hb_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
53     topo_instance_t, void *, void *);
54 
55 extern int platform_hb_label(topo_mod_t *, tnode_t *, nvlist_t *, nvlist_t **);
56 extern int platform_hb_enum(topo_mod_t *, tnode_t *,
57     const char *, topo_instance_t, topo_instance_t);
58 
59 extern txprop_t ExHB_common_props[];
60 extern txprop_t HB_common_props[];
61 extern txprop_t RC_common_props[];
62 extern int ExHB_propcnt;
63 extern int HB_propcnt;
64 extern int RC_propcnt;
65 
66 static int specific_hb_enum(topo_mod_t *, tnode_t *, const char *,
67     topo_instance_t, topo_instance_t, void *);
68 
69 static const topo_modops_t Hb_ops =
70 	{ hb_enum, hb_release };
71 static const topo_modinfo_t Hb_info =
72 	{ HOSTBRIDGE, FM_FMRI_SCHEME_HC, HB_ENUMR_VERS, &Hb_ops };
73 
74 static const topo_method_t Hb_methods[] = {
75 	{ TOPO_METH_LABEL, TOPO_METH_LABEL_DESC,
76 	    TOPO_METH_LABEL_VERSION, TOPO_STABILITY_INTERNAL, hb_label },
77 	{ NULL }
78 };
79 
80 static const topo_pgroup_info_t hb_auth_pgroup = {
81 	FM_FMRI_AUTHORITY,
82 	TOPO_STABILITY_PRIVATE,
83 	TOPO_STABILITY_PRIVATE,
84 	1
85 };
86 
87 int
88 _topo_init(topo_mod_t *modhdl, topo_version_t version)
89 {
90 	/*
91 	 * Turn on module debugging output
92 	 */
93 	if (getenv("TOPOHBDBG") != NULL)
94 		topo_mod_setdebug(modhdl);
95 	topo_mod_dprintf(modhdl, "initializing hostbridge enumerator\n");
96 
97 	if (version != HB_ENUMR_VERS)
98 		return (topo_mod_seterrno(modhdl, EMOD_VER_NEW));
99 
100 	if (topo_mod_register(modhdl, &Hb_info, TOPO_VERSION) < 0) {
101 		topo_mod_dprintf(modhdl, "hostbridge registration failed: %s\n",
102 		    topo_mod_errmsg(modhdl));
103 		return (-1); /* mod errno already set */
104 	}
105 
106 	topo_mod_dprintf(modhdl, "Hostbridge enumr initd\n");
107 
108 	return (0);
109 }
110 
111 void
112 _topo_fini(topo_mod_t *modhdl)
113 {
114 	topo_mod_unregister(modhdl);
115 }
116 
117 static int
118 hb_label(topo_mod_t *mp, tnode_t *node, topo_version_t version,
119     nvlist_t *in, nvlist_t **out)
120 {
121 	if (version > TOPO_METH_LABEL_VERSION)
122 		return (topo_mod_seterrno(mp, EMOD_VER_NEW));
123 	return (platform_hb_label(mp, node, in, out));
124 }
125 
126 static topo_mod_t *
127 pci_enumr_load(topo_mod_t *mp)
128 {
129 	topo_mod_t *rp = NULL;
130 
131 	if ((rp = topo_mod_load(mp, PCI_ENUM, PCI_ENUMR_VERS)) == NULL) {
132 		topo_mod_dprintf(mp,
133 		    "%s enumerator could not load %s.\n", HOSTBRIDGE, PCI_ENUM);
134 	}
135 	return (rp);
136 }
137 
138 /*ARGSUSED*/
139 static int
140 hb_enum(topo_mod_t *mp, tnode_t *pn, const char *name, topo_instance_t imin,
141     topo_instance_t imax, void *notused, void *data)
142 {
143 	int rv;
144 	topo_mod_t *pcimod;
145 
146 	if (strcmp(name, HOSTBRIDGE) != 0) {
147 		topo_mod_dprintf(mp,
148 		    "Currently only know how to enumerate %s components.\n",
149 		    HOSTBRIDGE);
150 		return (0);
151 	}
152 	/*
153 	 * Load the pcibus enumerator
154 	 */
155 	if ((pcimod = pci_enumr_load(mp)) == NULL)
156 		return (-1);
157 
158 	/*
159 	 * If we're asked to enumerate a whole range of hostbridges, then
160 	 * we need to find them all.  If we're just asked to enumerate a
161 	 * single hostbridge, we expect our caller to have passed us linked
162 	 * did_t structures we can use to enumerate the singled out hostbridge.
163 	 */
164 	if (imin != imax) {
165 
166 		if (did_hash_init(mp) < 0) {
167 			topo_mod_dprintf(mp,
168 			    "Hash initialization for hostbridge "
169 			    "enumerator failed.\n");
170 			topo_mod_unload(pcimod);
171 			return (-1);
172 		}
173 		rv = platform_hb_enum(mp, pn, name, imin, imax);
174 		did_hash_fini(mp);
175 	} else {
176 		rv = specific_hb_enum(mp, pn, name, imin, imax, data);
177 	}
178 
179 	return (rv);
180 }
181 
182 /*ARGSUSED*/
183 static void
184 hb_release(topo_mod_t *mp, tnode_t *node)
185 {
186 	topo_method_unregister_all(mp, node);
187 }
188 
189 static tnode_t *
190 hb_tnode_create(topo_mod_t *mod, tnode_t *parent,
191     const char *name, topo_instance_t i, void *priv)
192 {
193 	int err;
194 	nvlist_t *fmri;
195 	tnode_t *ntn;
196 	nvlist_t *auth = topo_mod_auth(mod, parent);
197 
198 	fmri = topo_mod_hcfmri(mod, parent, FM_HC_SCHEME_VERSION, name, i,
199 	    NULL, auth, NULL, NULL, NULL);
200 	nvlist_free(auth);
201 	if (fmri == NULL) {
202 		topo_mod_dprintf(mod,
203 		    "Unable to make nvlist for %s bind: %s.\n",
204 		    name, topo_mod_errmsg(mod));
205 		return (NULL);
206 	}
207 
208 	ntn = topo_node_bind(mod, parent, name, i, fmri);
209 	if (ntn == NULL) {
210 		topo_mod_dprintf(mod,
211 		    "topo_node_bind (%s%d/%s%d) failed: %s\n",
212 		    topo_node_name(parent), topo_node_instance(parent),
213 		    name, i,
214 		    topo_strerror(topo_mod_errno(mod)));
215 		nvlist_free(fmri);
216 		return (NULL);
217 	}
218 	nvlist_free(fmri);
219 	topo_node_setspecific(ntn, priv);
220 
221 	if (topo_pgroup_create(ntn, &hb_auth_pgroup, &err) == 0) {
222 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
223 		    FM_FMRI_AUTH_PRODUCT, &err);
224 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
225 		    FM_FMRI_AUTH_CHASSIS, &err);
226 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
227 		    FM_FMRI_AUTH_SERVER, &err);
228 	}
229 
230 	if (topo_method_register(mod, ntn, Hb_methods) < 0) {
231 		topo_mod_dprintf(mod, "topo_method_register failed: %s\n",
232 		    topo_strerror(topo_mod_errno(mod)));
233 		topo_node_unbind(ntn);
234 		return (NULL);
235 	}
236 	return (ntn);
237 }
238 
239 tnode_t *
240 pcihostbridge_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
241     topo_instance_t i)
242 {
243 	did_t *pd;
244 	tnode_t *ntn;
245 
246 	if ((pd = did_find(mod, din)) == NULL)
247 		return (NULL);
248 	if ((ntn = hb_tnode_create(mod, parent, HOSTBRIDGE, i, din)) == NULL)
249 		return (NULL);
250 	if (did_props_set(ntn, pd, HB_common_props, HB_propcnt) < 0) {
251 		topo_node_unbind(ntn);
252 		return (NULL);
253 	}
254 	/*
255 	 * We expect to find pci buses beneath the hostbridge.
256 	 */
257 	if (child_range_add(mod, ntn, PCI_BUS, 0, MAX_HB_BUSES) < 0) {
258 		topo_node_unbind(ntn);
259 		return (NULL);
260 	}
261 	return (ntn);
262 }
263 
264 tnode_t *
265 pciexhostbridge_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
266     topo_instance_t hi)
267 {
268 	did_t *pd;
269 	tnode_t *ntn;
270 
271 	if ((pd = did_find(mod, din)) == NULL)
272 		return (NULL);
273 	if ((ntn = hb_tnode_create(mod, parent, HOSTBRIDGE, hi, din)) == NULL)
274 		return (NULL);
275 	if (did_props_set(ntn, pd, ExHB_common_props, ExHB_propcnt) < 0) {
276 		topo_node_unbind(ntn);
277 		return (NULL);
278 	}
279 	/*
280 	 * We expect to find root complexes beneath the hostbridge.
281 	 */
282 	if (child_range_add(mod, ntn, PCIEX_ROOT, 0, MAX_HB_BUSES) < 0) {
283 		topo_node_unbind(ntn);
284 		return (NULL);
285 	}
286 	return (ntn);
287 }
288 
289 tnode_t *
290 pciexrc_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
291     topo_instance_t ri)
292 {
293 	did_t *pd;
294 	tnode_t *ntn;
295 
296 	if ((pd = did_find(mod, din)) == NULL)
297 		return (NULL);
298 	did_markrc(pd);
299 	if ((ntn = hb_tnode_create(mod, parent, PCIEX_ROOT, ri, din)) == NULL)
300 		return (NULL);
301 	if (did_props_set(ntn, pd, RC_common_props, RC_propcnt) < 0) {
302 		topo_node_unbind(ntn);
303 		return (NULL);
304 	}
305 	/*
306 	 * We expect to find pci-express buses beneath a root complex
307 	 */
308 	if (child_range_add(mod, ntn, PCIEX_BUS, 0, MAX_HB_BUSES) < 0) {
309 		topo_node_range_destroy(ntn, PCIEX_BUS);
310 		return (NULL);
311 	}
312 	return (ntn);
313 }
314 
315 /*ARGSUSED*/
316 static int
317 specific_hb_enum(topo_mod_t *mod, tnode_t *pn, const char *name,
318     topo_instance_t imin, topo_instance_t imax, void *priv)
319 {
320 	tnode_t *hb;
321 	did_t *iodid = (did_t *)priv;
322 	did_t *didp;
323 	int brc = 0;
324 	int bus;
325 
326 	did_setspecific(mod, priv);
327 
328 	/*
329 	 * Find the hostbridge of interest
330 	 */
331 	didp = iodid;
332 	for (brc = 0; brc < imin; brc++)
333 		didp = did_chain_get(didp);
334 	assert(didp != NULL);
335 
336 	if ((hb = pcihostbridge_declare(mod, pn, did_dinode(didp), imin))
337 	    == NULL) {
338 		return (-1);
339 	}
340 	while (didp != NULL) {
341 		did_BDF(didp, &bus, NULL, NULL);
342 		if (topo_mod_enumerate(mod,
343 		    hb, PCI_BUS, PCI_BUS, bus, bus, didp) != 0) {
344 			return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
345 		}
346 		didp = did_link_get(didp);
347 	}
348 
349 	return (0);
350 }
351