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 2006 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 	topo_mod_t *pcimod;
144 
145 	if (strcmp(name, HOSTBRIDGE) != 0) {
146 		topo_mod_dprintf(mp,
147 		    "Currently only know how to enumerate %s components.\n",
148 		    HOSTBRIDGE);
149 		return (0);
150 	}
151 	/*
152 	 * Load the pcibus enumerator
153 	 */
154 	if ((pcimod = pci_enumr_load(mp)) == NULL)
155 		return (-1);
156 
157 	/*
158 	 * If we're asked to enumerate a whole range of hostbridges, then
159 	 * we need to find them all.  If we're just asked to enumerate a
160 	 * single hostbridge, we expect our caller to have passed us linked
161 	 * did_t structures we can use to enumerate the singled out hostbridge.
162 	 */
163 	if (imin != imax) {
164 		int rv;
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 		if ((rv = platform_hb_enum(mp, pn, name, imin, imax)) < 0)
174 			topo_mod_seterrno(mp, EMOD_PARTIAL_ENUM);
175 		did_hash_fini(mp);
176 		return (rv);
177 	} else {
178 		return (specific_hb_enum(mp, pn, name, imin, imax,
179 		    data));
180 	}
181 }
182 
183 /*ARGSUSED*/
184 static void
185 hb_release(topo_mod_t *mp, tnode_t *node)
186 {
187 	topo_method_unregister_all(mp, node);
188 
189 	/*
190 	 * node private data (did_t) for this node is destroyed in
191 	 * did_hash_destroy()
192 	 */
193 
194 }
195 
196 static tnode_t *
197 hb_tnode_create(topo_mod_t *mod, tnode_t *parent,
198     const char *name, topo_instance_t i, void *priv)
199 {
200 	int err;
201 	nvlist_t *fmri;
202 	tnode_t *ntn;
203 	nvlist_t *auth = topo_mod_auth(mod, parent);
204 
205 	fmri = topo_mod_hcfmri(mod, parent, FM_HC_SCHEME_VERSION, name, i,
206 	    NULL, auth, NULL, NULL, NULL);
207 	nvlist_free(auth);
208 	if (fmri == NULL) {
209 		topo_mod_dprintf(mod,
210 		    "Unable to make nvlist for %s bind: %s.\n",
211 		    name, topo_mod_errmsg(mod));
212 		return (NULL);
213 	}
214 
215 	ntn = topo_node_bind(mod, parent, name, i, fmri);
216 	if (ntn == NULL) {
217 		topo_mod_dprintf(mod,
218 		    "topo_node_bind (%s%d/%s%d) failed: %s\n",
219 		    topo_node_name(parent), topo_node_instance(parent),
220 		    name, i,
221 		    topo_strerror(topo_mod_errno(mod)));
222 		nvlist_free(fmri);
223 		return (NULL);
224 	}
225 	nvlist_free(fmri);
226 	topo_node_setspecific(ntn, priv);
227 
228 	if (topo_pgroup_create(ntn, &hb_auth_pgroup, &err) == 0) {
229 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
230 		    FM_FMRI_AUTH_PRODUCT, &err);
231 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
232 		    FM_FMRI_AUTH_CHASSIS, &err);
233 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
234 		    FM_FMRI_AUTH_SERVER, &err);
235 	}
236 
237 	if (topo_method_register(mod, ntn, Hb_methods) < 0) {
238 		topo_mod_dprintf(mod, "topo_method_register failed: %s\n",
239 		    topo_strerror(topo_mod_errno(mod)));
240 		topo_node_unbind(ntn);
241 		return (NULL);
242 	}
243 	return (ntn);
244 }
245 
246 tnode_t *
247 pcihostbridge_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
248     topo_instance_t i)
249 {
250 	did_t *pd;
251 	tnode_t *ntn;
252 
253 	if ((pd = did_find(mod, din)) == NULL)
254 		return (NULL);
255 	if ((ntn = hb_tnode_create(mod, parent, HOSTBRIDGE, i, din)) == NULL)
256 		return (NULL);
257 	if (did_props_set(ntn, pd, HB_common_props, HB_propcnt) < 0) {
258 		topo_node_unbind(ntn);
259 		return (NULL);
260 	}
261 	/*
262 	 * We expect to find pci buses beneath the hostbridge.
263 	 */
264 	if (child_range_add(mod, ntn, PCI_BUS, 0, MAX_HB_BUSES) < 0) {
265 		topo_node_unbind(ntn);
266 		return (NULL);
267 	}
268 	return (ntn);
269 }
270 
271 tnode_t *
272 pciexhostbridge_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
273     topo_instance_t hi)
274 {
275 	did_t *pd;
276 	tnode_t *ntn;
277 
278 	if ((pd = did_find(mod, din)) == NULL)
279 		return (NULL);
280 	if ((ntn = hb_tnode_create(mod, parent, HOSTBRIDGE, hi, din)) == NULL)
281 		return (NULL);
282 	if (did_props_set(ntn, pd, ExHB_common_props, ExHB_propcnt) < 0) {
283 		topo_node_unbind(ntn);
284 		return (NULL);
285 	}
286 	/*
287 	 * We expect to find root complexes beneath the hostbridge.
288 	 */
289 	if (child_range_add(mod, ntn, PCIEX_ROOT, 0, MAX_HB_BUSES) < 0) {
290 		topo_node_unbind(ntn);
291 		return (NULL);
292 	}
293 	return (ntn);
294 }
295 
296 tnode_t *
297 pciexrc_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
298     topo_instance_t ri)
299 {
300 	did_t *pd;
301 	tnode_t *ntn;
302 
303 	if ((pd = did_find(mod, din)) == NULL)
304 		return (NULL);
305 	did_markrc(pd);
306 	if ((ntn = hb_tnode_create(mod, parent, PCIEX_ROOT, ri, din)) == NULL)
307 		return (NULL);
308 	if (did_props_set(ntn, pd, RC_common_props, RC_propcnt) < 0) {
309 		topo_node_unbind(ntn);
310 		return (NULL);
311 	}
312 	/*
313 	 * We expect to find pci-express buses beneath a root complex
314 	 */
315 	if (child_range_add(mod, ntn, PCIEX_BUS, 0, MAX_HB_BUSES) < 0) {
316 		topo_node_range_destroy(ntn, PCIEX_BUS);
317 		return (NULL);
318 	}
319 	return (ntn);
320 }
321 
322 /*ARGSUSED*/
323 static int
324 specific_hb_enum(topo_mod_t *mod, tnode_t *pn, const char *name,
325     topo_instance_t imin, topo_instance_t imax, void *priv)
326 {
327 	tnode_t *hb;
328 	did_t *iodid = (did_t *)priv;
329 	did_t *didp;
330 	int brc = 0;
331 	int bus;
332 
333 	did_setspecific(mod, priv);
334 
335 	/*
336 	 * Find the hostbridge of interest
337 	 */
338 	didp = iodid;
339 	for (brc = 0; brc < imin; brc++)
340 		didp = did_chain_get(didp);
341 	assert(didp != NULL);
342 
343 	if ((hb = pcihostbridge_declare(mod, pn, did_dinode(didp), imin))
344 	    == NULL) {
345 		return (-1);
346 	}
347 	while (didp != NULL) {
348 		did_BDF(didp, &bus, NULL, NULL);
349 		if (topo_mod_enumerate(mod,
350 		    hb, PCI_BUS, PCI_BUS, bus, bus, didp) != 0) {
351 			return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
352 		}
353 		didp = did_link_get(didp);
354 	}
355 
356 	return (0);
357 }
358