xref: /onnv-gate/usr/src/lib/fm/topo/modules/sun4v/cpuboard/cpuboard.c (revision 12731:89d0ecf088f0)
15430Ssd77468 /*
25430Ssd77468  * CDDL HEADER START
35430Ssd77468  *
45430Ssd77468  * The contents of this file are subject to the terms of the
55430Ssd77468  * Common Development and Distribution License (the "License").
65430Ssd77468  * You may not use this file except in compliance with the License.
75430Ssd77468  *
85430Ssd77468  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
95430Ssd77468  * or http://www.opensolaris.org/os/licensing.
105430Ssd77468  * See the License for the specific language governing permissions
115430Ssd77468  * and limitations under the License.
125430Ssd77468  *
135430Ssd77468  * When distributing Covered Code, include this CDDL HEADER in each
145430Ssd77468  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
155430Ssd77468  * If applicable, add the following below this CDDL HEADER, with the
165430Ssd77468  * fields enclosed by brackets "[]" replaced with your own identifying
175430Ssd77468  * information: Portions Copyright [yyyy] [name of copyright owner]
185430Ssd77468  *
195430Ssd77468  * CDDL HEADER END
205430Ssd77468  */
215430Ssd77468 
225430Ssd77468 /*
23*12731STrang.Do@Sun.COM  * Copyright (c) 2007, 2010, Oracle and/or its affiliates. All rights reserved.
245430Ssd77468  */
255430Ssd77468 
265430Ssd77468 #include <stdlib.h>
275430Ssd77468 #include <string.h>
285430Ssd77468 #include <strings.h>
295430Ssd77468 #include <fm/topo_mod.h>
305430Ssd77468 #include <fm/topo_hc.h>
315430Ssd77468 #include <libdevinfo.h>
325430Ssd77468 #include <limits.h>
335430Ssd77468 #include <sys/fm/protocol.h>
345430Ssd77468 #include <sys/param.h>
355430Ssd77468 #include <sys/systeminfo.h>
365430Ssd77468 #include <assert.h>
375430Ssd77468 #include <sys/utsname.h>
385430Ssd77468 #include <sys/systeminfo.h>
395430Ssd77468 #include <fm/fmd_fmri.h>
405430Ssd77468 #include <sys/types.h>
415430Ssd77468 #include <sys/mdesc.h>
425430Ssd77468 #include <sys/fm/ldom.h>
435430Ssd77468 
445430Ssd77468 #include "cpuboard_topo.h"
455430Ssd77468 
465430Ssd77468 /*
475430Ssd77468  * cpuboard.c
485430Ssd77468  *	sun4v specific cpuboard enumerator
495430Ssd77468  */
505430Ssd77468 
515430Ssd77468 #ifdef __cplusplus
525430Ssd77468 extern "C" {
535430Ssd77468 #endif
545430Ssd77468 
555430Ssd77468 #define	CPUBOARD_VERSION	TOPO_VERSION
565430Ssd77468 
575430Ssd77468 /* Until future PRI changes, make connection between cpuboard id and RC */
585430Ssd77468 char *cpub_rcs[] = { CPUBOARD0_RC, CPUBOARD1_RC, CPUBOARD2_RC, CPUBOARD3_RC };
595430Ssd77468 
605430Ssd77468 static int cpuboard_enum(topo_mod_t *, tnode_t *, const char *, topo_instance_t,
615430Ssd77468 		    topo_instance_t, void *, void *);
625430Ssd77468 
635430Ssd77468 static const topo_modops_t cpuboard_ops =
645430Ssd77468 	{ cpuboard_enum, NULL };
655430Ssd77468 
665430Ssd77468 const topo_modinfo_t cpuboard_info =
675430Ssd77468 	{CPUBOARD, FM_FMRI_SCHEME_HC, CPUBOARD_VERSION, &cpuboard_ops};
685430Ssd77468 
695430Ssd77468 static const topo_pgroup_info_t cpuboard_auth_pgroup =
705430Ssd77468 	{ FM_FMRI_AUTHORITY, TOPO_STABILITY_PRIVATE,
715430Ssd77468 	    TOPO_STABILITY_PRIVATE, 1 };
725430Ssd77468 
735430Ssd77468 static topo_mod_t *cpuboard_mod_hdl = NULL;
745430Ssd77468 
755430Ssd77468 static void *
cpuboard_topo_alloc(size_t size)765430Ssd77468 cpuboard_topo_alloc(size_t size)
775430Ssd77468 {
785430Ssd77468 	assert(cpuboard_mod_hdl != NULL);
795430Ssd77468 	return (topo_mod_alloc(cpuboard_mod_hdl, size));
805430Ssd77468 }
815430Ssd77468 
825430Ssd77468 static void
cpuboard_topo_free(void * data,size_t size)835430Ssd77468 cpuboard_topo_free(void *data, size_t size)
845430Ssd77468 {
855430Ssd77468 	assert(cpuboard_mod_hdl != NULL);
865430Ssd77468 	topo_mod_free(cpuboard_mod_hdl, data, size);
875430Ssd77468 }
885430Ssd77468 
895430Ssd77468 static int
cpuboard_get_pri_info(topo_mod_t * mod,cpuboard_contents_t cpubs[])905430Ssd77468 cpuboard_get_pri_info(topo_mod_t *mod, cpuboard_contents_t cpubs[])
915430Ssd77468 {
925430Ssd77468 	char isa[MAXNAMELEN];
935430Ssd77468 	md_t *mdp;
945430Ssd77468 	mde_cookie_t *listp;
955430Ssd77468 	uint64_t *bufp;
965430Ssd77468 	ssize_t bufsize = 0;
975430Ssd77468 	int  ncomp, num_nodes, i, len;
985430Ssd77468 	char *pstr = NULL;
995430Ssd77468 	char *sn = NULL, *pn = NULL;
1005430Ssd77468 	char *dn = NULL;
1017850SVuong.Nguyen@Sun.COM 	uint32_t type = 0;
1025430Ssd77468 	ldom_hdl_t *lhp;
1035430Ssd77468 	uint64_t id;
1046314Sgk73471 	int cpuboards_found = 0;
1055430Ssd77468 
1065430Ssd77468 	lhp = ldom_init(cpuboard_topo_alloc, cpuboard_topo_free);
1075430Ssd77468 	if (lhp == NULL) {
1085430Ssd77468 		topo_mod_dprintf(mod, "ldom_init failed\n");
1096314Sgk73471 		return (0);
1105430Ssd77468 	}
1115430Ssd77468 
1125430Ssd77468 	(void) sysinfo(SI_MACHINE, isa, MAXNAMELEN);
1135430Ssd77468 	if (strcmp(isa, "sun4v") != 0) {
1145430Ssd77468 		topo_mod_dprintf(mod, "not sun4v architecture%s\n", isa);
1155430Ssd77468 		ldom_fini(lhp);
1166314Sgk73471 		return (0);
1175430Ssd77468 	}
1185430Ssd77468 
1197850SVuong.Nguyen@Sun.COM 	(void) ldom_get_type(lhp, &type);
1207850SVuong.Nguyen@Sun.COM 	if ((type & LDOM_TYPE_CONTROL) != 0) {
1217850SVuong.Nguyen@Sun.COM 		bufsize = ldom_get_core_md(lhp, &bufp);
1227850SVuong.Nguyen@Sun.COM 	} else {
1237850SVuong.Nguyen@Sun.COM 		bufsize = ldom_get_local_md(lhp, &bufp);
1247850SVuong.Nguyen@Sun.COM 	}
1257850SVuong.Nguyen@Sun.COM 	if (bufsize < 1) {
1267850SVuong.Nguyen@Sun.COM 		topo_mod_dprintf(mod, "Failed to get pri/md, bufsize=%d\n",
1275430Ssd77468 		    bufsize);
1285430Ssd77468 		ldom_fini(lhp);
1296314Sgk73471 		return (0);
1305430Ssd77468 	}
1317850SVuong.Nguyen@Sun.COM 	topo_mod_dprintf(mod, "pri/md bufsize=%d\n", bufsize);
1325430Ssd77468 
1335430Ssd77468 	if ((mdp = md_init_intern(bufp, cpuboard_topo_alloc,
1345430Ssd77468 	    cpuboard_topo_free)) == NULL ||
1355430Ssd77468 	    (num_nodes = md_node_count(mdp)) < 1) {
1365430Ssd77468 		topo_mod_dprintf(mod, "md_init_intern error\n");
1375430Ssd77468 		cpuboard_topo_free(bufp, (size_t)bufsize);
1385430Ssd77468 		ldom_fini(lhp);
1396314Sgk73471 		return (0);
1405430Ssd77468 	}
1415430Ssd77468 	topo_mod_dprintf(mod, "num_nodes=%d\n", num_nodes);
1425430Ssd77468 
1435430Ssd77468 	if ((listp = (mde_cookie_t *)cpuboard_topo_alloc(
1445430Ssd77468 	    sizeof (mde_cookie_t) * num_nodes)) == NULL) {
1455430Ssd77468 		topo_mod_dprintf(mod, "alloc listp error\n");
1465430Ssd77468 		cpuboard_topo_free(bufp, (size_t)bufsize);
1475430Ssd77468 		(void) md_fini(mdp);
1485430Ssd77468 		ldom_fini(lhp);
1496314Sgk73471 		return (0);
1505430Ssd77468 	}
1515430Ssd77468 	ncomp = md_scan_dag(mdp, MDE_INVAL_ELEM_COOKIE,
1525430Ssd77468 	    md_find_name(mdp, "component"),
1535430Ssd77468 	    md_find_name(mdp, "fwd"), listp);
1545430Ssd77468 	topo_mod_dprintf(mod, "ncomp=%d\n", ncomp);
1555430Ssd77468 	if (ncomp <= 0) {
1565430Ssd77468 		cpuboard_topo_free(listp, sizeof (mde_cookie_t) * num_nodes);
1575430Ssd77468 		cpuboard_topo_free(bufp, (size_t)bufsize);
1585430Ssd77468 		(void) md_fini(mdp);
1595430Ssd77468 		ldom_fini(lhp);
1606314Sgk73471 		return (0);
1615430Ssd77468 	}
1625430Ssd77468 	for (i = 0; i < ncomp; i++) {
1635430Ssd77468 		/*
1645430Ssd77468 		 * PRI nodes are still named "cpu-board", but the canonical
1655430Ssd77468 		 * names are "cpuboard".
1665430Ssd77468 		 */
1675430Ssd77468 		if (md_get_prop_str(mdp, listp[i], "type", &pstr) == 0 &&
1685430Ssd77468 		    pstr != NULL && strcmp(pstr, "cpu-board") == 0) {
1695430Ssd77468 			if (md_get_prop_val(mdp, listp[i], "id", &id) < 0) {
1705430Ssd77468 				topo_mod_dprintf(mod, "cpuboard_get_pri_info: "
1715430Ssd77468 				    "id md_get_prop_val() failed. (%d: %s)\n",
1725430Ssd77468 				    errno, strerror(errno));
1735430Ssd77468 				continue;
1745430Ssd77468 			}
1755430Ssd77468 			if ((id >= CPUBOARD_MAX) || cpubs[id].present) {
17611583SSurya.Prakki@Sun.COM 				(void) topo_mod_seterrno(mod, EMOD_NVL_INVAL);
1775430Ssd77468 				topo_mod_dprintf(mod, "cpuboard_get_pri_info: "
1785430Ssd77468 				    "id %llx out of range. (%d: %s)\n",
1795430Ssd77468 				    id, errno, strerror(errno));
1805430Ssd77468 				continue;
1815430Ssd77468 			}
1825430Ssd77468 			cpubs[id].present = 1;
1836314Sgk73471 			cpuboards_found++;
1845430Ssd77468 
1855430Ssd77468 			topo_mod_dprintf(mod, "got cpu-board: %llx\n", id);
1865430Ssd77468 
1875430Ssd77468 			sn = pn = dn = NULL;
1885430Ssd77468 
1895430Ssd77468 			(void) md_get_prop_str(mdp, listp[i],
1905430Ssd77468 			    "serial_number", &sn);
1915430Ssd77468 			cpubs[id].sn = topo_mod_strdup(mod, sn);
1925430Ssd77468 
1935430Ssd77468 			(void) md_get_prop_str(mdp, listp[i],
1945430Ssd77468 			    "part_number", &pn);
1955430Ssd77468 
1965430Ssd77468 			(void) md_get_prop_str(mdp, listp[i],
1975430Ssd77468 			    "dash_number", &dn);
1985430Ssd77468 			len = (pn ? strlen(pn) : 0) + (dn ? strlen(dn) : 0) + 1;
1995430Ssd77468 			pstr = cpuboard_topo_alloc(len);
2005430Ssd77468 			(void) snprintf(pstr, len, "%s%s",
2015430Ssd77468 			    pn ? pn : "", dn ? dn : "");
2025430Ssd77468 			cpubs[id].pn = topo_mod_strdup(mod, pstr);
2035430Ssd77468 			cpuboard_topo_free(pstr, len);
2045430Ssd77468 		}
2055430Ssd77468 	}
2065430Ssd77468 	cpuboard_topo_free(listp, sizeof (mde_cookie_t) * num_nodes);
2075430Ssd77468 	cpuboard_topo_free(bufp, (size_t)bufsize);
2085430Ssd77468 	(void) md_fini(mdp);
2095430Ssd77468 	ldom_fini(lhp);
2105430Ssd77468 
2116314Sgk73471 	return (cpuboards_found);
2125430Ssd77468 }
2135430Ssd77468 
2145430Ssd77468 /*ARGSUSED*/
2155430Ssd77468 void
_topo_init(topo_mod_t * mod,topo_version_t version)2165430Ssd77468 _topo_init(topo_mod_t *mod, topo_version_t version)
2175430Ssd77468 {
2185430Ssd77468 	/*
2195430Ssd77468 	 * Turn on module debugging output
2205430Ssd77468 	 */
2215430Ssd77468 	if (getenv("TOPOCPUBOARDDBG") != NULL) {
2225430Ssd77468 		topo_mod_setdebug(mod);
2235430Ssd77468 	}
2245430Ssd77468 	topo_mod_dprintf(mod, "initializing cpuboard enumerator\n");
2255430Ssd77468 
2265430Ssd77468 	if (topo_mod_register(mod, &cpuboard_info, TOPO_VERSION) < 0) {
2275430Ssd77468 		topo_mod_dprintf(mod, "cpuboard registration failed: %s\n",
2285430Ssd77468 		    topo_mod_errmsg(mod));
2295430Ssd77468 		return; /* mod errno already set */
2305430Ssd77468 	}
2315430Ssd77468 	topo_mod_dprintf(mod, "cpuboard enumr initd\n");
2325430Ssd77468 }
2335430Ssd77468 
2345430Ssd77468 void
_topo_fini(topo_mod_t * mod)2355430Ssd77468 _topo_fini(topo_mod_t *mod)
2365430Ssd77468 {
2375430Ssd77468 	topo_mod_unregister(mod);
2385430Ssd77468 }
2395430Ssd77468 
2405430Ssd77468 static tnode_t *
cpuboard_tnode_create(topo_mod_t * mod,tnode_t * parent,const char * name,topo_instance_t i,void * priv,cpuboard_contents_t * cpubc)2415430Ssd77468 cpuboard_tnode_create(topo_mod_t *mod, tnode_t *parent,
2425430Ssd77468     const char *name, topo_instance_t i, void *priv, cpuboard_contents_t *cpubc)
2435430Ssd77468 {
2445430Ssd77468 	int err;
2455430Ssd77468 	nvlist_t *fmri;
2465430Ssd77468 	tnode_t *ntn;
2475430Ssd77468 	nvlist_t *auth = topo_mod_auth(mod, parent);
2485430Ssd77468 
2495430Ssd77468 	fmri = topo_mod_hcfmri(mod, parent, FM_HC_SCHEME_VERSION, name, i,
2505430Ssd77468 	    NULL, auth, cpubc->pn, NULL, cpubc->sn);
2515430Ssd77468 	nvlist_free(auth);
2525430Ssd77468 
2535430Ssd77468 	if (fmri == NULL) {
2545430Ssd77468 		topo_mod_dprintf(mod,
2555430Ssd77468 		    "Unable to make nvlist for %s bind: %s.\n",
2565430Ssd77468 		    name, topo_mod_errmsg(mod));
2575430Ssd77468 		return (NULL);
2585430Ssd77468 	}
2595430Ssd77468 
2605430Ssd77468 	ntn = topo_node_bind(mod, parent, name, i, fmri);
2615430Ssd77468 	if (ntn == NULL) {
2625430Ssd77468 		topo_mod_dprintf(mod,
2635430Ssd77468 		    "topo_node_bind (%s%d/%s%d) failed: %s\n",
2645430Ssd77468 		    topo_node_name(parent), topo_node_instance(parent),
2655430Ssd77468 		    name, i,
2665430Ssd77468 		    topo_strerror(topo_mod_errno(mod)));
2675430Ssd77468 		nvlist_free(fmri);
2685430Ssd77468 		return (NULL);
2695430Ssd77468 	}
2705430Ssd77468 	topo_mod_dprintf(mod,
2715430Ssd77468 	    "cpuboard_tnode_create: topo_node_bind (%s%d/%s%d) created!\n",
2725430Ssd77468 	    topo_node_name(parent), topo_node_instance(parent), name, i);
2735430Ssd77468 	nvlist_free(fmri);
2745430Ssd77468 	topo_node_setspecific(ntn, priv);
2755430Ssd77468 
2765430Ssd77468 	if (topo_pgroup_create(ntn, &cpuboard_auth_pgroup, &err) == 0) {
2775430Ssd77468 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
2785430Ssd77468 		    FM_FMRI_AUTH_PRODUCT, &err);
2795430Ssd77468 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
28010462SSean.Ye@Sun.COM 		    FM_FMRI_AUTH_PRODUCT_SN, &err);
28110462SSean.Ye@Sun.COM 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
2825430Ssd77468 		    FM_FMRI_AUTH_CHASSIS, &err);
2835430Ssd77468 		(void) topo_prop_inherit(ntn, FM_FMRI_AUTHORITY,
2845430Ssd77468 		    FM_FMRI_AUTH_SERVER, &err);
2855430Ssd77468 	}
2865430Ssd77468 
2875430Ssd77468 	return (ntn);
2885430Ssd77468 }
2895430Ssd77468 
2905430Ssd77468 static int
cpuboard_fru_set(topo_mod_t * mp,tnode_t * tn)2915430Ssd77468 cpuboard_fru_set(topo_mod_t *mp, tnode_t *tn)
2925430Ssd77468 {
2935430Ssd77468 	nvlist_t *fmri;
2945430Ssd77468 	int err, e;
2955430Ssd77468 
2965430Ssd77468 	if (topo_node_resource(tn, &fmri, &err) < 0 ||
2975430Ssd77468 	    fmri == NULL) {
2985430Ssd77468 		topo_mod_dprintf(mp, "FRU_fmri_set error: %s\n",
2995430Ssd77468 		    topo_strerror(topo_mod_errno(mp)));
3005430Ssd77468 		return (topo_mod_seterrno(mp, err));
3015430Ssd77468 	}
3025430Ssd77468 	e = topo_node_fru_set(tn, fmri, 0, &err);
3035430Ssd77468 	nvlist_free(fmri);
3045430Ssd77468 	if (e < 0)
3055430Ssd77468 		return (topo_mod_seterrno(mp, err));
3065430Ssd77468 	return (0);
3075430Ssd77468 }
3085430Ssd77468 
3095430Ssd77468 static int
cpuboard_label_set(topo_mod_t * mod,tnode_t * parent,tnode_t * node,topo_instance_t n)3105430Ssd77468 cpuboard_label_set(topo_mod_t *mod, tnode_t *parent, tnode_t *node,
3115430Ssd77468 	topo_instance_t n)
3125430Ssd77468 {
3135430Ssd77468 	char *label = NULL;
3145430Ssd77468 	char *plabel = NULL;
3155430Ssd77468 	const char *cpuboard_label = "/CPU";
3165430Ssd77468 	int err, len;
3175430Ssd77468 
3185430Ssd77468 	if (topo_node_label(parent, &plabel, &err) != 0 ||
3195430Ssd77468 	    plabel == NULL) {
3205430Ssd77468 		return (-1);
3215430Ssd77468 	}
3225430Ssd77468 
3235430Ssd77468 	len = strlen(plabel) + strlen(cpuboard_label) + 2;
3245430Ssd77468 	label = topo_mod_alloc(mod, len);
3255430Ssd77468 	(void) snprintf(label, len, "%s%s%d", plabel, cpuboard_label, n);
3265430Ssd77468 	topo_mod_strfree(mod, plabel);
3275430Ssd77468 
3285430Ssd77468 	if (label != NULL) {
3295430Ssd77468 		if (topo_prop_set_string(node, TOPO_PGROUP_PROTOCOL,
3305430Ssd77468 		    TOPO_PROP_LABEL, TOPO_PROP_IMMUTABLE, label,
3315430Ssd77468 		    &err) != 0) {
3325430Ssd77468 			topo_mod_strfree(mod, label);
3335430Ssd77468 			return (topo_mod_seterrno(mod, err));
3345430Ssd77468 		}
3355430Ssd77468 	}
3365430Ssd77468 	topo_mod_free(mod, label, len);
3375430Ssd77468 	return (0);
3385430Ssd77468 }
3395430Ssd77468 
3405430Ssd77468 
3415430Ssd77468 /*ARGSUSED*/
3425430Ssd77468 static tnode_t *
cpuboard_declare(tnode_t * parent,const char * name,topo_instance_t i,void * priv,topo_mod_t * mod,cpuboard_contents_t * cpubc)3435430Ssd77468 cpuboard_declare(tnode_t *parent, const char *name, topo_instance_t i,
3445430Ssd77468 	void *priv, topo_mod_t *mod, cpuboard_contents_t *cpubc)
3455430Ssd77468 {
3465430Ssd77468 	tnode_t *ntn;
3475430Ssd77468 	nvlist_t *fmri = NULL;
3485430Ssd77468 	int err;
3495430Ssd77468 
3505430Ssd77468 	if ((ntn = cpuboard_tnode_create(mod, parent, name, i, priv,
3515430Ssd77468 	    cpubc)) == NULL) {
3525430Ssd77468 		topo_mod_dprintf(mod, "%s ntn = NULL\n", name);
3535430Ssd77468 		return (NULL);
3545430Ssd77468 	}
3555430Ssd77468 
3565430Ssd77468 	(void) cpuboard_fru_set(mod, ntn);
3575430Ssd77468 
3585430Ssd77468 	(void) cpuboard_label_set(mod, parent, ntn, i);
3595430Ssd77468 
3605430Ssd77468 	/* set ASRU to resource fmri */
3615430Ssd77468 	if (topo_prop_get_fmri(ntn, TOPO_PGROUP_PROTOCOL,
3625430Ssd77468 	    TOPO_PROP_RESOURCE, &fmri, &err) == 0)
3635430Ssd77468 		(void) topo_node_asru_set(ntn, fmri, 0, &err);
3645430Ssd77468 	nvlist_free(fmri);
3655430Ssd77468 
3665430Ssd77468 	return (ntn);
3675430Ssd77468 }
3685430Ssd77468 
3695430Ssd77468 static int
chip_instantiate(tnode_t * parent,const char * name,topo_mod_t * mod,topo_instance_t inst)3705430Ssd77468 chip_instantiate(tnode_t *parent, const char *name, topo_mod_t *mod,
3715430Ssd77468     topo_instance_t inst)
3725430Ssd77468 {
3735430Ssd77468 	if (strcmp(name, CPUBOARD) != 0) {
3745430Ssd77468 		topo_mod_dprintf(mod,
3755430Ssd77468 		    "Currently only know how to enumerate %s components.\n",
3765430Ssd77468 		    CPUBOARD);
3775430Ssd77468 		return (0);
3785430Ssd77468 	}
3795430Ssd77468 	topo_mod_dprintf(mod,
3805430Ssd77468 	    "Calling chip_enum for inst: %lx\n", inst);
3815430Ssd77468 	if (topo_mod_enumerate(mod,
3825430Ssd77468 	    parent, CHIP, CHIP, inst, inst, NULL) != 0) {
3835430Ssd77468 		return (topo_mod_seterrno(mod, EMOD_PARTIAL_ENUM));
3845430Ssd77468 	}
3855430Ssd77468 	return (0);
3865430Ssd77468 }
3875430Ssd77468 
3885430Ssd77468 static topo_mod_t *
chip_enum_load(topo_mod_t * mp)3895430Ssd77468 chip_enum_load(topo_mod_t *mp)
3905430Ssd77468 {
3915430Ssd77468 	topo_mod_t *rp = NULL;
3925430Ssd77468 
3935430Ssd77468 	topo_mod_dprintf(mp, "chip_enum_load: %s\n", CPUBOARD);
3945430Ssd77468 	if ((rp = topo_mod_load(mp, CHIP, TOPO_VERSION)) == NULL) {
3955430Ssd77468 		topo_mod_dprintf(mp,
3965430Ssd77468 		    "%s enumerator could not load %s enum. (%d: %s)\n",
3975430Ssd77468 		    CPUBOARD, CHIP, errno, strerror(errno));
3985430Ssd77468 	}
3995430Ssd77468 	topo_mod_dprintf(mp, "chip_enum_load(EXIT): %s, rp=%p\n", CPUBOARD, rp);
4005430Ssd77468 	return (rp);
4015430Ssd77468 }
4025430Ssd77468 
4035430Ssd77468 static di_node_t
cpuboard_findrc(topo_mod_t * mod,uint64_t id)4045430Ssd77468 cpuboard_findrc(topo_mod_t *mod, uint64_t id)
4055430Ssd77468 {
4065430Ssd77468 	di_node_t devtree;
4075430Ssd77468 	di_node_t dnode;
4085430Ssd77468 
4095430Ssd77468 	if ((devtree = topo_mod_devinfo(mod)) == DI_NODE_NIL) {
4105430Ssd77468 		topo_mod_dprintf(mod, "devinfo init failed.");
4115430Ssd77468 		return (NULL);
4125430Ssd77468 	}
4135430Ssd77468 	dnode = di_drv_first_node(CPUBOARD_PX_DRV, devtree);
4145430Ssd77468 	while (dnode != DI_NODE_NIL) {
4155430Ssd77468 		char *path;
4165430Ssd77468 
4175430Ssd77468 		if ((path = di_devfs_path(dnode)) == NULL) {
4185430Ssd77468 			topo_mod_dprintf(mod, "cpuboard_findrc: "
4195430Ssd77468 			    "NULL di_devfs_path.\n");
4205430Ssd77468 			(void) topo_mod_seterrno(mod, ETOPO_PROP_NOENT);
4215430Ssd77468 			return (NULL);
4225430Ssd77468 		}
4235430Ssd77468 		topo_mod_dprintf(mod, "cpuboard_findrc: "
4245430Ssd77468 		    "got px %d, node named: %s, path: %s\n",
4255430Ssd77468 		    di_instance(dnode), di_node_name(dnode), path);
4265430Ssd77468 
4275430Ssd77468 		if (strcmp(cpub_rcs[id], path) == 0) {
4285430Ssd77468 			di_devfs_path_free(path);
4295430Ssd77468 			return (dnode);
4305430Ssd77468 		}
4315430Ssd77468 
4325430Ssd77468 		di_devfs_path_free(path);
4335430Ssd77468 
4345430Ssd77468 		dnode = di_drv_next_node(dnode);
4355430Ssd77468 	}
4365430Ssd77468 	return (NULL);
4375430Ssd77468 }
4385430Ssd77468 
439*12731STrang.Do@Sun.COM static void
cpuboard_free_pri_info(topo_mod_t * mod,cpuboard_contents_t cpuboard_list[],topo_instance_t min,topo_instance_t max)440*12731STrang.Do@Sun.COM cpuboard_free_pri_info(topo_mod_t *mod, cpuboard_contents_t cpuboard_list[],
441*12731STrang.Do@Sun.COM     topo_instance_t min, topo_instance_t max)
442*12731STrang.Do@Sun.COM {
443*12731STrang.Do@Sun.COM 	int i;
444*12731STrang.Do@Sun.COM 
445*12731STrang.Do@Sun.COM 	for (i = min; i <= max; i++) {
446*12731STrang.Do@Sun.COM 		if (cpuboard_list[i].present == 0)
447*12731STrang.Do@Sun.COM 			continue;
448*12731STrang.Do@Sun.COM 		if (cpuboard_list[i].sn != NULL)
449*12731STrang.Do@Sun.COM 			topo_mod_strfree(mod, cpuboard_list[i].sn);
450*12731STrang.Do@Sun.COM 		if (cpuboard_list[i].pn != NULL)
451*12731STrang.Do@Sun.COM 			topo_mod_strfree(mod, cpuboard_list[i].pn);
452*12731STrang.Do@Sun.COM 	}
453*12731STrang.Do@Sun.COM }
454*12731STrang.Do@Sun.COM 
4555430Ssd77468 /*ARGSUSED*/
4565430Ssd77468 static int
cpuboard_enum(topo_mod_t * mod,tnode_t * parent,const char * name,topo_instance_t min,topo_instance_t max,void * arg,void * notused)4575430Ssd77468 cpuboard_enum(topo_mod_t *mod, tnode_t *parent, const char *name,
4585430Ssd77468 	topo_instance_t min, topo_instance_t max, void *arg, void *notused)
4595430Ssd77468 {
4605430Ssd77468 	tnode_t *cpuboardn;
4615430Ssd77468 	topo_instance_t i = 0;
4625430Ssd77468 	cpuboard_contents_t cpuboard_list[CPUBOARD_MAX];
4635430Ssd77468 
4645430Ssd77468 	if (strcmp(name, CPUBOARD) != 0) {
4655430Ssd77468 		topo_mod_dprintf(mod,
4665430Ssd77468 		    "Currently only know how to enumerate %s components.\n",
4675430Ssd77468 		    CPUBOARD);
4685430Ssd77468 		return (-1);
4695430Ssd77468 	}
4705430Ssd77468 	/* Make sure we don't exceed CPUBOARD_MAX */
4715430Ssd77468 	if (max >= CPUBOARD_MAX) {
4725430Ssd77468 		max = CPUBOARD_MAX;
4735430Ssd77468 	}
4745430Ssd77468 
4755430Ssd77468 	bzero(cpuboard_list, sizeof (cpuboard_list));
4765430Ssd77468 
4775430Ssd77468 	/* Scan PRI for cpu-boards. */
4785430Ssd77468 	cpuboard_mod_hdl = mod;
4796314Sgk73471 	if (cpuboard_get_pri_info(mod, cpuboard_list) == 0) {
4806314Sgk73471 		int cpuboards_found = 0;
4816314Sgk73471 		/*
4826314Sgk73471 		 * if no PRI available (i.e. not in Control Domain),
4836314Sgk73471 		 * use px driver to determine cpuboard presence.
4846314Sgk73471 		 * NOTE: with this approach there will be no
4856314Sgk73471 		 * identity information - no SN nor PN.
4866314Sgk73471 		 */
4876314Sgk73471 		bzero(cpuboard_list, sizeof (cpuboard_list));
4886314Sgk73471 		for (i = min; i <= max; i++) {
4896314Sgk73471 			if (cpuboard_findrc(mod, i) != NULL) {
4906314Sgk73471 				cpuboard_list[i].present = 1;
4916314Sgk73471 				cpuboards_found++;
4926314Sgk73471 			}
4936314Sgk73471 		}
4946314Sgk73471 		if (cpuboards_found == 0) {
4956314Sgk73471 			topo_mod_dprintf(mod, "No cpuboards found.\n");
4966314Sgk73471 			return (-1);
4976314Sgk73471 		}
4986314Sgk73471 	}
4995430Ssd77468 
500*12731STrang.Do@Sun.COM 	if (chip_enum_load(mod) == NULL) {
501*12731STrang.Do@Sun.COM 		cpuboard_free_pri_info(mod, cpuboard_list, min, max);
5025430Ssd77468 		return (-1);
503*12731STrang.Do@Sun.COM 	}
5045430Ssd77468 
5055430Ssd77468 	for (i = min; i <= max; i++) {
5065430Ssd77468 		if (cpuboard_list[i].present == 0)
5075430Ssd77468 			continue;
5085430Ssd77468 
5095430Ssd77468 		cpuboardn = cpuboard_declare(parent, name, i,
5105430Ssd77468 		    NULL, mod, &cpuboard_list[i]);
5115430Ssd77468 		if (cpuboardn == NULL) {
5125430Ssd77468 			topo_mod_dprintf(mod,
5135430Ssd77468 			    "Enumeration of cpuboard failed: %s\n",
5145430Ssd77468 			    topo_strerror(topo_mod_errno(mod)));
515*12731STrang.Do@Sun.COM 			cpuboard_free_pri_info(mod, cpuboard_list, min, max);
5165430Ssd77468 			return (-1); /* mod_errno already set */
5175430Ssd77468 		}
5185430Ssd77468 		if (topo_node_range_create(mod, cpuboardn, CHIP, 0,
5195430Ssd77468 		    CHIP_MAX) < 0) {
5205430Ssd77468 			topo_node_unbind(cpuboardn);
5215430Ssd77468 			topo_mod_dprintf(mod, "topo_node_range_create CHIP "
5225430Ssd77468 			    "failed: %s\n", topo_strerror(topo_mod_errno(mod)));
523*12731STrang.Do@Sun.COM 			cpuboard_free_pri_info(mod, cpuboard_list, min, max);
5245430Ssd77468 			return (-1); /* mod_errno already set */
5255430Ssd77468 		}
5265430Ssd77468 		if (chip_instantiate(cpuboardn, CPUBOARD, mod, i) < 0) {
5275430Ssd77468 			topo_mod_dprintf(mod, "Enumeration of chip "
5285430Ssd77468 			    "failed %s\n",
5295430Ssd77468 			    topo_strerror(topo_mod_errno(mod)));
530*12731STrang.Do@Sun.COM 			cpuboard_free_pri_info(mod, cpuboard_list, min, max);
5315430Ssd77468 			return (-1);
5325430Ssd77468 		}
5335430Ssd77468 		if (topo_node_range_create(mod, cpuboardn, HOSTBRIDGE, 0,
5345430Ssd77468 		    HOSTBRIDGE_MAX) < 0) {
5355430Ssd77468 			topo_node_unbind(cpuboardn);
5365430Ssd77468 			topo_mod_dprintf(mod, "topo_node_range_create: "
5375430Ssd77468 			    "HOSTBRIDGE failed: %s\n",
5385430Ssd77468 			    topo_strerror(topo_mod_errno(mod)));
539*12731STrang.Do@Sun.COM 			cpuboard_free_pri_info(mod, cpuboard_list, min, max);
5405430Ssd77468 			return (-1);
5415430Ssd77468 		}
5427850SVuong.Nguyen@Sun.COM 		if (cpuboard_hb_enum(mod, cpuboard_findrc(mod, i), cpub_rcs[i],
5435430Ssd77468 		    cpuboardn, i) < 0) {
5445430Ssd77468 			topo_node_unbind(cpuboardn);
5455430Ssd77468 			topo_mod_dprintf(mod, "cpuboard_hb_enum: "
5465430Ssd77468 			    "failed: %s\n",
5475430Ssd77468 			    topo_strerror(topo_mod_errno(mod)));
548*12731STrang.Do@Sun.COM 			cpuboard_free_pri_info(mod, cpuboard_list, min, max);
5495430Ssd77468 			return (-1);
5505430Ssd77468 		}
5515430Ssd77468 	}
552*12731STrang.Do@Sun.COM 	cpuboard_free_pri_info(mod, cpuboard_list, min, max);
5535430Ssd77468 	return (0);
5545430Ssd77468 }
555