10eb822a1Scindi /*
20eb822a1Scindi  * CDDL HEADER START
30eb822a1Scindi  *
40eb822a1Scindi  * The contents of this file are subject to the terms of the
50eb822a1Scindi  * Common Development and Distribution License (the "License").
60eb822a1Scindi  * You may not use this file except in compliance with the License.
70eb822a1Scindi  *
80eb822a1Scindi  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90eb822a1Scindi  * or http://www.opensolaris.org/os/licensing.
100eb822a1Scindi  * See the License for the specific language governing permissions
110eb822a1Scindi  * and limitations under the License.
120eb822a1Scindi  *
130eb822a1Scindi  * When distributing Covered Code, include this CDDL HEADER in each
140eb822a1Scindi  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150eb822a1Scindi  * If applicable, add the following below this CDDL HEADER, with the
160eb822a1Scindi  * fields enclosed by brackets "[]" replaced with your own identifying
170eb822a1Scindi  * information: Portions Copyright [yyyy] [name of copyright owner]
180eb822a1Scindi  *
190eb822a1Scindi  * CDDL HEADER END
200eb822a1Scindi  */
210eb822a1Scindi 
220eb822a1Scindi /*
239c94f155SCheng Sean Ye  * Copyright 2009 Sun Microsystems, Inc.  All rights reserved.
240eb822a1Scindi  * Use is subject to license terms.
250eb822a1Scindi  */
260eb822a1Scindi 
270eb822a1Scindi #include <string.h>
280eb822a1Scindi #include <fm/topo_mod.h>
290eb822a1Scindi #include <fm/topo_hc.h>
300eb822a1Scindi #include <libdevinfo.h>
310eb822a1Scindi #include <limits.h>
320eb822a1Scindi #include <sys/fm/protocol.h>
330eb822a1Scindi #include <sys/param.h>
340eb822a1Scindi #include <sys/systeminfo.h>
350eb822a1Scindi #include <assert.h>
360eb822a1Scindi 
370eb822a1Scindi #include <hostbridge.h>
380eb822a1Scindi #include <pcibus.h>
390eb822a1Scindi #include <did.h>
400eb822a1Scindi #include <did_props.h>
410eb822a1Scindi #include <util.h>
420eb822a1Scindi 
430eb822a1Scindi /*
440eb822a1Scindi  * hostbridge.c
450eb822a1Scindi  *	Generic code shared by all the hostbridge enumerators
460eb822a1Scindi  */
470eb822a1Scindi static void hb_release(topo_mod_t *, tnode_t *);
480eb822a1Scindi static int hb_label(topo_mod_t *, tnode_t *, topo_version_t, nvlist_t *,
490eb822a1Scindi     nvlist_t **);
500eb822a1Scindi static int hb_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
510eb822a1Scindi     topo_instance_t, void *, void *);
520eb822a1Scindi 
530eb822a1Scindi extern int platform_hb_label(topo_mod_t *, tnode_t *, nvlist_t *, nvlist_t **);
540eb822a1Scindi extern int platform_hb_enum(topo_mod_t *, tnode_t *,
550eb822a1Scindi     const char *, topo_instance_t, topo_instance_t);
560eb822a1Scindi 
570eb822a1Scindi extern txprop_t ExHB_common_props[];
580eb822a1Scindi extern txprop_t HB_common_props[];
590eb822a1Scindi extern txprop_t RC_common_props[];
600eb822a1Scindi extern int ExHB_propcnt;
610eb822a1Scindi extern int HB_propcnt;
620eb822a1Scindi extern int RC_propcnt;
630eb822a1Scindi 
640eb822a1Scindi static int specific_hb_enum(topo_mod_t *, tnode_t *, const char *,
650eb822a1Scindi     topo_instance_t, topo_instance_t, void *);
660eb822a1Scindi 
670eb822a1Scindi static const topo_modops_t Hb_ops =
680eb822a1Scindi 	{ hb_enum, hb_release };
690eb822a1Scindi static const topo_modinfo_t Hb_info =
700eb822a1Scindi 	{ HOSTBRIDGE, FM_FMRI_SCHEME_HC, HB_ENUMR_VERS, &Hb_ops };
710eb822a1Scindi 
720eb822a1Scindi static const topo_method_t Hb_methods[] = {
730eb822a1Scindi 	{ TOPO_METH_LABEL, TOPO_METH_LABEL_DESC,
740eb822a1Scindi 	    TOPO_METH_LABEL_VERSION, TOPO_STABILITY_INTERNAL, hb_label },
750eb822a1Scindi 	{ NULL }
760eb822a1Scindi };
770eb822a1Scindi 
780eb822a1Scindi static const topo_pgroup_info_t hb_auth_pgroup = {
790eb822a1Scindi 	FM_FMRI_AUTHORITY,
800eb822a1Scindi 	TOPO_STABILITY_PRIVATE,
810eb822a1Scindi 	TOPO_STABILITY_PRIVATE,
820eb822a1Scindi 	1
830eb822a1Scindi };
840eb822a1Scindi 
850eb822a1Scindi int
_topo_init(topo_mod_t * modhdl,topo_version_t version)860eb822a1Scindi _topo_init(topo_mod_t *modhdl, topo_version_t version)
870eb822a1Scindi {
880eb822a1Scindi 	/*
890eb822a1Scindi 	 * Turn on module debugging output
900eb822a1Scindi 	 */
910eb822a1Scindi 	if (getenv("TOPOHBDBG") != NULL)
920eb822a1Scindi 		topo_mod_setdebug(modhdl);
930eb822a1Scindi 	topo_mod_dprintf(modhdl, "initializing hostbridge enumerator\n");
940eb822a1Scindi 
950eb822a1Scindi 	if (version != HB_ENUMR_VERS)
960eb822a1Scindi 		return (topo_mod_seterrno(modhdl, EMOD_VER_NEW));
970eb822a1Scindi 
980eb822a1Scindi 	if (topo_mod_register(modhdl, &Hb_info, TOPO_VERSION) < 0) {
990eb822a1Scindi 		topo_mod_dprintf(modhdl, "hostbridge registration failed: %s\n",
1000eb822a1Scindi 		    topo_mod_errmsg(modhdl));
1010eb822a1Scindi 		return (-1); /* mod errno already set */
1020eb822a1Scindi 	}
1030eb822a1Scindi 
1040eb822a1Scindi 	topo_mod_dprintf(modhdl, "Hostbridge enumr initd\n");
1050eb822a1Scindi 
1060eb822a1Scindi 	return (0);
1070eb822a1Scindi }
1080eb822a1Scindi 
1090eb822a1Scindi void
_topo_fini(topo_mod_t * modhdl)1100eb822a1Scindi _topo_fini(topo_mod_t *modhdl)
1110eb822a1Scindi {
1120eb822a1Scindi 	topo_mod_unregister(modhdl);
1130eb822a1Scindi }
1140eb822a1Scindi 
1150eb822a1Scindi static int
hb_label(topo_mod_t * mp,tnode_t * node,topo_version_t version,nvlist_t * in,nvlist_t ** out)1160eb822a1Scindi hb_label(topo_mod_t *mp, tnode_t *node, topo_version_t version,
1170eb822a1Scindi     nvlist_t *in, nvlist_t **out)
1180eb822a1Scindi {
1190eb822a1Scindi 	if (version > TOPO_METH_LABEL_VERSION)
1200eb822a1Scindi 		return (topo_mod_seterrno(mp, EMOD_VER_NEW));
1210eb822a1Scindi 	return (platform_hb_label(mp, node, in, out));
1220eb822a1Scindi }
1230eb822a1Scindi 
1240eb822a1Scindi static topo_mod_t *
pci_enumr_load(topo_mod_t * mp)1250eb822a1Scindi pci_enumr_load(topo_mod_t *mp)
1260eb822a1Scindi {
1270eb822a1Scindi 	topo_mod_t *rp = NULL;
1280eb822a1Scindi 
1290eb822a1Scindi 	if ((rp = topo_mod_load(mp, PCI_ENUM, PCI_ENUMR_VERS)) == NULL) {
1300eb822a1Scindi 		topo_mod_dprintf(mp,
1310eb822a1Scindi 		    "%s enumerator could not load %s.\n", HOSTBRIDGE, PCI_ENUM);
1320eb822a1Scindi 	}
1330eb822a1Scindi 	return (rp);
1340eb822a1Scindi }
1350eb822a1Scindi 
1360eb822a1Scindi /*ARGSUSED*/
1370eb822a1Scindi static int
hb_enum(topo_mod_t * mp,tnode_t * pn,const char * name,topo_instance_t imin,topo_instance_t imax,void * notused,void * data)1380eb822a1Scindi hb_enum(topo_mod_t *mp, tnode_t *pn, const char *name, topo_instance_t imin,
1390eb822a1Scindi     topo_instance_t imax, void *notused, void *data)
1400eb822a1Scindi {
14112cc75c8Scindi 	int rv;
1420eb822a1Scindi 	topo_mod_t *pcimod;
1430eb822a1Scindi 
1440eb822a1Scindi 	if (strcmp(name, HOSTBRIDGE) != 0) {
1450eb822a1Scindi 		topo_mod_dprintf(mp,
1460eb822a1Scindi 		    "Currently only know how to enumerate %s components.\n",
1470eb822a1Scindi 		    HOSTBRIDGE);
1480eb822a1Scindi 		return (0);
1490eb822a1Scindi 	}
1500eb822a1Scindi 	/*
1510eb822a1Scindi 	 * Load the pcibus enumerator
1520eb822a1Scindi 	 */
1530eb822a1Scindi 	if ((pcimod = pci_enumr_load(mp)) == NULL)
1540eb822a1Scindi 		return (-1);
1550eb822a1Scindi 
1560eb822a1Scindi 	/*
1570eb822a1Scindi 	 * If we're asked to enumerate a whole range of hostbridges, then
1580eb822a1Scindi 	 * we need to find them all.  If we're just asked to enumerate a
1590eb822a1Scindi 	 * single hostbridge, we expect our caller to have passed us linked
1600eb822a1Scindi 	 * did_t structures we can use to enumerate the singled out hostbridge.
1610eb822a1Scindi 	 */
1620eb822a1Scindi 	if (imin != imax) {
1630eb822a1Scindi 
1640eb822a1Scindi 		if (did_hash_init(mp) < 0) {
1650eb822a1Scindi 			topo_mod_dprintf(mp,
1660eb822a1Scindi 			    "Hash initialization for hostbridge "
1670eb822a1Scindi 			    "enumerator failed.\n");
1680eb822a1Scindi 			topo_mod_unload(pcimod);
1690eb822a1Scindi 			return (-1);
1700eb822a1Scindi 		}
17112cc75c8Scindi 		rv = platform_hb_enum(mp, pn, name, imin, imax);
1720eb822a1Scindi 		did_hash_fini(mp);
1730eb822a1Scindi 	} else {
17412cc75c8Scindi 		rv = specific_hb_enum(mp, pn, name, imin, imax, data);
1750eb822a1Scindi 	}
17612cc75c8Scindi 
17712cc75c8Scindi 	return (rv);
1780eb822a1Scindi }
1790eb822a1Scindi 
1800eb822a1Scindi /*ARGSUSED*/
1810eb822a1Scindi static void
hb_release(topo_mod_t * mp,tnode_t * node)1820eb822a1Scindi hb_release(topo_mod_t *mp, tnode_t *node)
1830eb822a1Scindi {
1840eb822a1Scindi 	topo_method_unregister_all(mp, node);
1850eb822a1Scindi }
1860eb822a1Scindi 
1870eb822a1Scindi static tnode_t *
hb_tnode_create(topo_mod_t * mod,tnode_t * parent,const char * name,topo_instance_t i,void * priv)1880eb822a1Scindi hb_tnode_create(topo_mod_t *mod, tnode_t *parent,
1890eb822a1Scindi     const char *name, topo_instance_t i, void *priv)
1900eb822a1Scindi {
1910eb822a1Scindi 	int err;
1920eb822a1Scindi 	nvlist_t *fmri;
1930eb822a1Scindi 	tnode_t *ntn;
1940eb822a1Scindi 	nvlist_t *auth = topo_mod_auth(mod, parent);
1950eb822a1Scindi 
1960eb822a1Scindi 	fmri = topo_mod_hcfmri(mod, parent, FM_HC_SCHEME_VERSION, name, i,
1970eb822a1Scindi 	    NULL, auth, NULL, NULL, NULL);
1980eb822a1Scindi 	nvlist_free(auth);
1990eb822a1Scindi 	if (fmri == NULL) {
2000eb822a1Scindi 		topo_mod_dprintf(mod,
2010eb822a1Scindi 		    "Unable to make nvlist for %s bind: %s.\n",
2020eb822a1Scindi 		    name, topo_mod_errmsg(mod));
2030eb822a1Scindi 		return (NULL);
2040eb822a1Scindi 	}
2050eb822a1Scindi 
2060eb822a1Scindi 	ntn = topo_node_bind(mod, parent, name, i, fmri);
2070eb822a1Scindi 	if (ntn == NULL) {
2080eb822a1Scindi 		topo_mod_dprintf(mod,
209*6597d6fcSRobert Mustacchi 		    "topo_node_bind (%s%" PRIu64 "/%s%" PRIu64 ") failed: %s\n",
2100eb822a1Scindi 		    topo_node_name(parent), topo_node_instance(parent),
2110eb822a1Scindi 		    name, i,
2120eb822a1Scindi 		    topo_strerror(topo_mod_errno(mod)));
2130eb822a1Scindi 		nvlist_free(fmri);
2140eb822a1Scindi 		return (NULL);
2150eb822a1Scindi 	}
2160eb822a1Scindi 	nvlist_free(fmri);
2170eb822a1Scindi 	topo_node_setspecific(ntn, priv);
2180eb822a1Scindi 
2190eb822a1Scindi 	if (topo_pgroup_create(ntn, &hb_auth_pgroup, &err) == 0) {
2200eb822a1Scindi 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
2210eb822a1Scindi 		    FM_FMRI_AUTH_PRODUCT, &err);
2229c94f155SCheng Sean Ye 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
2239c94f155SCheng Sean Ye 		    FM_FMRI_AUTH_PRODUCT_SN, &err);
2240eb822a1Scindi 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
2250eb822a1Scindi 		    FM_FMRI_AUTH_CHASSIS, &err);
2260eb822a1Scindi 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
2270eb822a1Scindi 		    FM_FMRI_AUTH_SERVER, &err);
2280eb822a1Scindi 	}
2290eb822a1Scindi 
2300eb822a1Scindi 	if (topo_method_register(mod, ntn, Hb_methods) < 0) {
2310eb822a1Scindi 		topo_mod_dprintf(mod, "topo_method_register failed: %s\n",
2320eb822a1Scindi 		    topo_strerror(topo_mod_errno(mod)));
2330eb822a1Scindi 		topo_node_unbind(ntn);
2340eb822a1Scindi 		return (NULL);
2350eb822a1Scindi 	}
2360eb822a1Scindi 	return (ntn);
2370eb822a1Scindi }
2380eb822a1Scindi 
2390eb822a1Scindi tnode_t *
pcihostbridge_declare(topo_mod_t * mod,tnode_t * parent,di_node_t din,topo_instance_t i)2400eb822a1Scindi pcihostbridge_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
2410eb822a1Scindi     topo_instance_t i)
2420eb822a1Scindi {
2430eb822a1Scindi 	did_t *pd;
2440eb822a1Scindi 	tnode_t *ntn;
2450eb822a1Scindi 
2460eb822a1Scindi 	if ((pd = did_find(mod, din)) == NULL)
2470eb822a1Scindi 		return (NULL);
2480eb822a1Scindi 	if ((ntn = hb_tnode_create(mod, parent, HOSTBRIDGE, i, din)) == NULL)
2490eb822a1Scindi 		return (NULL);
2500eb822a1Scindi 	if (did_props_set(ntn, pd, HB_common_props, HB_propcnt) < 0) {
2510eb822a1Scindi 		topo_node_unbind(ntn);
2520eb822a1Scindi 		return (NULL);
2530eb822a1Scindi 	}
2540eb822a1Scindi 	/*
2550eb822a1Scindi 	 * We expect to find pci buses beneath the hostbridge.
2560eb822a1Scindi 	 */
2570eb822a1Scindi 	if (child_range_add(mod, ntn, PCI_BUS, 0, MAX_HB_BUSES) < 0) {
2580eb822a1Scindi 		topo_node_unbind(ntn);
2590eb822a1Scindi 		return (NULL);
2600eb822a1Scindi 	}
2610eb822a1Scindi 	return (ntn);
2620eb822a1Scindi }
2630eb822a1Scindi 
2640eb822a1Scindi tnode_t *
pciexhostbridge_declare(topo_mod_t * mod,tnode_t * parent,di_node_t din,topo_instance_t hi)2650eb822a1Scindi pciexhostbridge_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
2660eb822a1Scindi     topo_instance_t hi)
2670eb822a1Scindi {
2680eb822a1Scindi 	did_t *pd;
2690eb822a1Scindi 	tnode_t *ntn;
2700eb822a1Scindi 
2710eb822a1Scindi 	if ((pd = did_find(mod, din)) == NULL)
2720eb822a1Scindi 		return (NULL);
2730eb822a1Scindi 	if ((ntn = hb_tnode_create(mod, parent, HOSTBRIDGE, hi, din)) == NULL)
2740eb822a1Scindi 		return (NULL);
2750eb822a1Scindi 	if (did_props_set(ntn, pd, ExHB_common_props, ExHB_propcnt) < 0) {
2760eb822a1Scindi 		topo_node_unbind(ntn);
2770eb822a1Scindi 		return (NULL);
2780eb822a1Scindi 	}
2790eb822a1Scindi 	/*
2800eb822a1Scindi 	 * We expect to find root complexes beneath the hostbridge.
2810eb822a1Scindi 	 */
2820eb822a1Scindi 	if (child_range_add(mod, ntn, PCIEX_ROOT, 0, MAX_HB_BUSES) < 0) {
2830eb822a1Scindi 		topo_node_unbind(ntn);
2840eb822a1Scindi 		return (NULL);
2850eb822a1Scindi 	}
2860eb822a1Scindi 	return (ntn);
2870eb822a1Scindi }
2880eb822a1Scindi 
2890eb822a1Scindi tnode_t *
pciexrc_declare(topo_mod_t * mod,tnode_t * parent,di_node_t din,topo_instance_t ri)2900eb822a1Scindi pciexrc_declare(topo_mod_t *mod, tnode_t *parent, di_node_t din,
2910eb822a1Scindi     topo_instance_t ri)
2920eb822a1Scindi {
2930eb822a1Scindi 	did_t *pd;
2940eb822a1Scindi 	tnode_t *ntn;
2950eb822a1Scindi 
2960eb822a1Scindi 	if ((pd = did_find(mod, din)) == NULL)
2970eb822a1Scindi 		return (NULL);
2980eb822a1Scindi 	did_markrc(pd);
2990eb822a1Scindi 	if ((ntn = hb_tnode_create(mod, parent, PCIEX_ROOT, ri, din)) == NULL)
3000eb822a1Scindi 		return (NULL);
3010eb822a1Scindi 	if (did_props_set(ntn, pd, RC_common_props, RC_propcnt) < 0) {
3020eb822a1Scindi 		topo_node_unbind(ntn);
3030eb822a1Scindi 		return (NULL);
3040eb822a1Scindi 	}
3050eb822a1Scindi 	/*
3060eb822a1Scindi 	 * We expect to find pci-express buses beneath a root complex
3070eb822a1Scindi 	 */
3080eb822a1Scindi 	if (child_range_add(mod, ntn, PCIEX_BUS, 0, MAX_HB_BUSES) < 0) {
3090eb822a1Scindi 		topo_node_range_destroy(ntn, PCIEX_BUS);
3100eb822a1Scindi 		return (NULL);
3110eb822a1Scindi 	}
3120eb822a1Scindi 	return (ntn);
3130eb822a1Scindi }
3140eb822a1Scindi 
3150eb822a1Scindi /*ARGSUSED*/
3160eb822a1Scindi static int
specific_hb_enum(topo_mod_t * mod,tnode_t * pn,const char * name,topo_instance_t imin,topo_instance_t imax,void * priv)3170eb822a1Scindi specific_hb_enum(topo_mod_t *mod, tnode_t *pn, const char *name,
3180eb822a1Scindi     topo_instance_t imin, topo_instance_t imax, void *priv)
3190eb822a1Scindi {
3200eb822a1Scindi 	tnode_t *hb;
3210eb822a1Scindi 	did_t *iodid = (did_t *)priv;
3220eb822a1Scindi 	did_t *didp;
3230eb822a1Scindi 	int brc = 0;
3240eb822a1Scindi 	int bus;
3250eb822a1Scindi 
3260eb822a1Scindi 	did_setspecific(mod, priv);
3270eb822a1Scindi 
3280eb822a1Scindi 	/*
3290eb822a1Scindi 	 * Find the hostbridge of interest
3300eb822a1Scindi 	 */
3310eb822a1Scindi 	didp = iodid;
3320eb822a1Scindi 	for (brc = 0; brc < imin; brc++)
3330eb822a1Scindi 		didp = did_chain_get(didp);
3340eb822a1Scindi 	assert(didp != NULL);
3350eb822a1Scindi 
3360eb822a1Scindi 	if ((hb = pcihostbridge_declare(mod, pn, did_dinode(didp), imin))
3370eb822a1Scindi 	    == NULL) {
3380eb822a1Scindi 		return (-1);
3390eb822a1Scindi 	}
3400eb822a1Scindi 	while (didp != NULL) {
3410eb822a1Scindi 		did_BDF(didp, &bus, NULL, NULL);
3420eb822a1Scindi 		if (topo_mod_enumerate(mod,
3430eb822a1Scindi 		    hb, PCI_BUS, PCI_BUS, bus, bus, didp) != 0) {
3440eb822a1Scindi 			return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
3450eb822a1Scindi 		}
3460eb822a1Scindi 		didp = did_link_get(didp);
3470eb822a1Scindi 	}
3480eb822a1Scindi 
3490eb822a1Scindi 	return (0);
3500eb822a1Scindi }
351