xref: /onnv-gate/usr/src/lib/fm/topo/modules/sun4v/sun4vpi/pi_ldom.c (revision 7205:e288f08b9204)
1*7205Ssd77468 /*
2*7205Ssd77468  * CDDL HEADER START
3*7205Ssd77468  *
4*7205Ssd77468  * The contents of this file are subject to the terms of the
5*7205Ssd77468  * Common Development and Distribution License (the "License").
6*7205Ssd77468  * You may not use this file except in compliance with the License.
7*7205Ssd77468  *
8*7205Ssd77468  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9*7205Ssd77468  * or http://www.opensolaris.org/os/licensing.
10*7205Ssd77468  * See the License for the specific language governing permissions
11*7205Ssd77468  * and limitations under the License.
12*7205Ssd77468  *
13*7205Ssd77468  * When distributing Covered Code, include this CDDL HEADER in each
14*7205Ssd77468  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15*7205Ssd77468  * If applicable, add the following below this CDDL HEADER, with the
16*7205Ssd77468  * fields enclosed by brackets "[]" replaced with your own identifying
17*7205Ssd77468  * information: Portions Copyright [yyyy] [name of copyright owner]
18*7205Ssd77468  *
19*7205Ssd77468  * CDDL HEADER END
20*7205Ssd77468  */
21*7205Ssd77468 
22*7205Ssd77468 /*
23*7205Ssd77468  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24*7205Ssd77468  * Use is subject to license terms.
25*7205Ssd77468  */
26*7205Ssd77468 
27*7205Ssd77468 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7205Ssd77468 
29*7205Ssd77468 /*
30*7205Ssd77468  * Open connections to the LDOM and Machine Description libraries used during
31*7205Ssd77468  * enumeration.
32*7205Ssd77468  */
33*7205Ssd77468 
34*7205Ssd77468 #include <sys/types.h>
35*7205Ssd77468 #include <string.h>
36*7205Ssd77468 #include <strings.h>
37*7205Ssd77468 #include <sys/mdesc.h>
38*7205Ssd77468 #include <sys/fm/ldom.h>
39*7205Ssd77468 #include <sys/systeminfo.h>
40*7205Ssd77468 
41*7205Ssd77468 #include "pi_impl.h"
42*7205Ssd77468 
43*7205Ssd77468 static topo_mod_t *Pi_mod;
44*7205Ssd77468 
45*7205Ssd77468 static void pi_free(void *, size_t);
46*7205Ssd77468 static void * pi_alloc(size_t);
47*7205Ssd77468 
48*7205Ssd77468 /*
49*7205Ssd77468  * Initialize a connection to the LDOM machine description interface.
50*7205Ssd77468  */
51*7205Ssd77468 int
pi_ldompri_open(topo_mod_t * mod,pi_enum_t * pip)52*7205Ssd77468 pi_ldompri_open(topo_mod_t *mod, pi_enum_t *pip)
53*7205Ssd77468 {
54*7205Ssd77468 	if (mod == NULL || pip == NULL) {
55*7205Ssd77468 		return (-1);
56*7205Ssd77468 	}
57*7205Ssd77468 
58*7205Ssd77468 	/*
59*7205Ssd77468 	 * Store the module pointer for this session.  This file-global
60*7205Ssd77468 	 * is used by the allocators called by libldom and libmdesc.
61*7205Ssd77468 	 */
62*7205Ssd77468 	Pi_mod = mod;
63*7205Ssd77468 
64*7205Ssd77468 	/* Initialize the LDOM connection */
65*7205Ssd77468 	pip->ldomp = ldom_init(pi_alloc, pi_free);
66*7205Ssd77468 	if (pip->ldomp == NULL) {
67*7205Ssd77468 		topo_mod_dprintf(mod,
68*7205Ssd77468 		    "sun4vpi failed to initialize LDOM layer.\n");
69*7205Ssd77468 		Pi_mod = NULL;
70*7205Ssd77468 		return (-1);
71*7205Ssd77468 	}
72*7205Ssd77468 
73*7205Ssd77468 	/* Initialize the machine description layer for this ldom instance */
74*7205Ssd77468 	pip->ldom_bufsize = ldom_get_core_md(pip->ldomp, &(pip->ldom_bufp));
75*7205Ssd77468 	if (pip->ldom_bufsize < 1) {
76*7205Ssd77468 		topo_mod_dprintf(mod, "ldom_get_core_md error: bufsize = %d\n",
77*7205Ssd77468 		    pip->ldom_bufsize);
78*7205Ssd77468 		ldom_fini(pip->ldomp);
79*7205Ssd77468 		Pi_mod = NULL;
80*7205Ssd77468 		return (-1);
81*7205Ssd77468 	}
82*7205Ssd77468 
83*7205Ssd77468 	/* Initialize the machine description internal layer */
84*7205Ssd77468 	pip->mdp = md_init_intern(pip->ldom_bufp, pi_alloc, pi_free);
85*7205Ssd77468 	if (pip->mdp == NULL ||
86*7205Ssd77468 	    (pip->md_nodes = md_node_count(pip->mdp)) < 1) {
87*7205Ssd77468 		topo_mod_dprintf(mod, "md_init_intern error\n");
88*7205Ssd77468 		pi_free(pip->ldom_bufp, pip->ldom_bufsize);
89*7205Ssd77468 		ldom_fini(pip->ldomp);
90*7205Ssd77468 		Pi_mod = NULL;
91*7205Ssd77468 		return (-1);
92*7205Ssd77468 	}
93*7205Ssd77468 
94*7205Ssd77468 	return (0);
95*7205Ssd77468 }
96*7205Ssd77468 
97*7205Ssd77468 
98*7205Ssd77468 /* ARGSUSED */
99*7205Ssd77468 void
pi_ldompri_close(topo_mod_t * mod,pi_enum_t * pip)100*7205Ssd77468 pi_ldompri_close(topo_mod_t *mod, pi_enum_t *pip)
101*7205Ssd77468 {
102*7205Ssd77468 	if (pip == NULL) {
103*7205Ssd77468 		return;
104*7205Ssd77468 	}
105*7205Ssd77468 
106*7205Ssd77468 	/* Close the machine description connection */
107*7205Ssd77468 	(void) md_fini(pip->mdp);
108*7205Ssd77468 
109*7205Ssd77468 	/* Close the connection to the LDOM layer */
110*7205Ssd77468 	ldom_fini(pip->ldomp);
111*7205Ssd77468 
112*7205Ssd77468 	/* Free the ldom connection data */
113*7205Ssd77468 	pi_free(pip->ldom_bufp, pip->ldom_bufsize);
114*7205Ssd77468 
115*7205Ssd77468 	/* Reset the file-global module pointer */
116*7205Ssd77468 	Pi_mod = NULL;
117*7205Ssd77468 }
118*7205Ssd77468 
119*7205Ssd77468 
120*7205Ssd77468 static void *
pi_alloc(size_t size)121*7205Ssd77468 pi_alloc(size_t size)
122*7205Ssd77468 {
123*7205Ssd77468 	if (Pi_mod == NULL) {
124*7205Ssd77468 		/* Cannot allocate memory without a module pointer */
125*7205Ssd77468 		return (NULL);
126*7205Ssd77468 	}
127*7205Ssd77468 	return (topo_mod_alloc(Pi_mod, size));
128*7205Ssd77468 }
129*7205Ssd77468 
130*7205Ssd77468 
131*7205Ssd77468 static void
pi_free(void * buf,size_t size)132*7205Ssd77468 pi_free(void *buf, size_t size)
133*7205Ssd77468 {
134*7205Ssd77468 	if (Pi_mod == NULL) {
135*7205Ssd77468 		/* Cannot free memory without a module pointer */
136*7205Ssd77468 		return;
137*7205Ssd77468 	}
138*7205Ssd77468 	topo_mod_free(Pi_mod, buf, size);
139*7205Ssd77468 }
140