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 "priplugin.h"
30 
31 #pragma init(priplugin_register)	/* place in .init section */
32 
33 picl_nodehdl_t	root_node;
34 md_t		*mdp;
35 mde_cookie_t	rootnode;
36 
37 void priplugin_init(void);
38 void priplugin_fini(void);
39 
40 picld_plugin_reg_t priplugin_reg = {
41 	PICLD_PLUGIN_VERSION_1,
42 	PICLD_PLUGIN_CRITICAL,
43 	"pri_plugin",
44 	priplugin_init,
45 	priplugin_fini
46 };
47 
48 void
49 set_prop_info(ptree_propinfo_t *propinfo, int size, char *name, int type)
50 {
51 	propinfo->version = PICLD_PLUGIN_VERSION_1;
52 	propinfo->read = NULL;
53 	propinfo->write = NULL;
54 	propinfo->piclinfo.type = type;
55 	propinfo->piclinfo.accessmode = PICL_READ;
56 	propinfo->piclinfo.size = size;
57 	(void) strlcpy(propinfo->piclinfo.name, name,
58 	    sizeof (propinfo->piclinfo.name));
59 }
60 
61 boolean_t
62 prop_exists(picl_nodehdl_t node, char *name)
63 {
64 	int status;
65 	picl_prophdl_t proph;
66 
67 	status = ptree_get_prop_by_name(node, name, &proph);
68 	if (status == PICL_SUCCESS)
69 		return (B_TRUE);
70 	else
71 		return (B_FALSE);
72 }
73 
74 void
75 add_md_prop(picl_nodehdl_t node, int size, char *name, void* value, int type)
76 {
77 	ptree_propinfo_t propinfo;
78 	picl_prophdl_t proph;
79 
80 	if (!prop_exists(node, name)) {
81 		set_prop_info(&propinfo, size, name, type);
82 
83 		(void) ptree_create_and_add_prop(node, &propinfo,
84 		    value, &proph);
85 	}
86 }
87 
88 void
89 priplugin_init(void)
90 {
91 	int status;
92 
93 	pri_debug(LOG_NOTICE, "priplugin: entered\n");
94 	status = ptree_get_root(&root_node);
95 	if (status != PICL_SUCCESS) {
96 		pri_debug(LOG_NOTICE, "priplugin: can't get picl root node\n");
97 		return;
98 	}
99 
100 	mdp = pri_devinit();
101 	if (mdp == NULL) {
102 		pri_debug(LOG_NOTICE, "priplugin: cannot init pri: %d\n",
103 		    errno);
104 		return;
105 	}
106 
107 	rootnode = md_root_node(mdp);
108 
109 	pri_debug(LOG_NOTICE, "priplugin: have root picl and PRI nodes\n");
110 
111 	status = ptree_walk_tree_by_class(root_node, "memory",
112 	    "memory-segments", add_mem_prop);
113 	if (status != PICL_SUCCESS) {
114 		pri_debug(LOG_NOTICE, "pri: memory-segments walk failed\n");
115 	} else
116 		pri_debug(LOG_NOTICE, "pri: success walking memory node\n");
117 
118 	io_dev_addlabel();
119 
120 	pri_devfini(mdp);
121 }
122 
123 void
124 priplugin_fini(void)
125 {
126 }
127 
128 void
129 priplugin_register(void)
130 {
131 	picld_plugin_register(&priplugin_reg);
132 }
133 
134 /*VARARGS2*/
135 void
136 pri_debug(int level, char *fmt, ...)
137 {
138 #if (PRI_DEBUG != 0)
139 	va_list	ap;
140 
141 	va_start(ap, fmt);
142 	vsyslog(level, fmt, ap);
143 	va_end(ap);
144 #endif
145 }
146