xref: /titanic_44/usr/src/cmd/picl/plugins/sun4v/mdesc/mdescplugin.c (revision dd5829d1456ba00e6f704e6a88e7eaae608e3c1b)
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 /*
30  * The MDESC picl plugin serves 2 different functionalities.
31  * --The first is to look up certain CPU properties in the MDESC an to add
32  * these properties in the already created CPU PICL nodes in the /platform
33  * section of the tree.
34  * --The second functionality is to create a /disk_discovery section of the
35  * PICL tree which will have a disk node created for each disk node in the
36  * machine description.
37  */
38 
39 #include "mdescplugin.h"
40 #include <libnvpair.h>
41 
42 #pragma init(mdescplugin_register)	/* place in .init section */
43 
44 picl_nodehdl_t	root_node;
45 md_t		*mdp;
46 mde_cookie_t	rootnode;
47 
48 void mdescplugin_init(void);
49 void mdescplugin_fini(void);
50 
51 extern int add_cpu_prop(picl_nodehdl_t node, void *args);
52 extern int disk_discovery(void);
53 extern md_t *mdesc_devinit(void);
54 extern void mdesc_devfini(md_t *mdp);
55 extern int update_devices(char *dev, int op);
56 
57 picld_plugin_reg_t mdescplugin_reg = {
58 	PICLD_PLUGIN_VERSION_1,
59 	PICLD_PLUGIN_CRITICAL,
60 	"mdesc_plugin",
61 	mdescplugin_init,
62 	mdescplugin_fini
63 };
64 
65 #define	DISK_FOUND 0x00
66 #define	DISK_NOT_FOUND 0x01
67 
68 typedef struct disk_lookup {
69 	char *path;
70 	picl_nodehdl_t disk;
71 	int result;
72 } disk_lookup_t;
73 
74 int
75 find_disk(picl_nodehdl_t node, void *args)
76 {
77 	disk_lookup_t *lookup  = (disk_lookup_t *)args;
78 	int status;
79 	char path[PICL_PROPNAMELEN_MAX];
80 
81 	status = ptree_get_propval_by_name(node, "Path", (void *)&path,
82 	    PICL_PROPNAMELEN_MAX);
83 	if (status != PICL_SUCCESS) {
84 		return (PICL_WALK_CONTINUE);
85 	}
86 
87 	if (strcmp(path, lookup->path) == 0) {
88 		lookup->disk = node;
89 		lookup->result = DISK_FOUND;
90 		return (PICL_WALK_TERMINATE);
91 	}
92 
93 	return (PICL_WALK_CONTINUE);
94 }
95 
96 /*
97  * DR event handler
98  * respond to the picl events:
99  *      PICLEVENT_DR_AP_STATE_CHANGE
100  */
101 static void
102 dr_handler(const char *ename, const void *earg, size_t size, void *cookie)
103 {
104 	nvlist_t	*nvlp = NULL;
105 	char		*dtype;
106 	char		*ap_id;
107 	char		*hint;
108 
109 
110 	if (strcmp(ename, PICLEVENT_DR_AP_STATE_CHANGE) != 0) {
111 		return;
112 	}
113 
114 	if (nvlist_unpack((char *)earg, size, &nvlp, NULL)) {
115 		return;
116 	}
117 
118 	if (nvlist_lookup_string(nvlp, PICLEVENTARG_DATA_TYPE, &dtype)) {
119 		nvlist_free(nvlp);
120 		return;
121 	}
122 
123 	if (strcmp(dtype, PICLEVENTARG_PICLEVENT_DATA) != 0) {
124 		nvlist_free(nvlp);
125 		return;
126 	}
127 
128 	if (nvlist_lookup_string(nvlp, PICLEVENTARG_AP_ID, &ap_id)) {
129 		nvlist_free(nvlp);
130 		return;
131 	}
132 
133 	if (nvlist_lookup_string(nvlp, PICLEVENTARG_HINT, &hint)) {
134 		nvlist_free(nvlp);
135 		return;
136 	}
137 
138 	mdp = mdesc_devinit();
139 	if (mdp == NULL) {
140 		nvlist_free(nvlp);
141 		return;
142 	}
143 
144 	rootnode = md_root_node(mdp);
145 
146 	if (strcmp(hint, DR_HINT_INSERT) == 0)
147 		(void) update_devices(ap_id, DEV_ADD);
148 	else if (strcmp(hint, DR_HINT_REMOVE) == 0)
149 		(void) update_devices(ap_id, DEV_REMOVE);
150 
151 	mdesc_devfini(mdp);
152 	nvlist_free(nvlp);
153 }
154 
155 /*
156  * Discovery event handler
157  * respond to the picl events:
158  *      PICLEVENT_SYSEVENT_DEVICE_ADDED
159  *      PICLEVENT_SYSEVENT_DEVICE_REMOVED
160  */
161 static void
162 dsc_handler(const char *ename, const void *earg, size_t size, void *cookie)
163 {
164 	nvlist_t	*nvlp = NULL;
165 	char		*path;
166 	disk_lookup_t	lookup;
167 	int		status;
168 
169 	/*
170 	 * retrieve the device's physical path from the event arg
171 	 * and determine which disk (if any) we are working with
172 	 */
173 	if (nvlist_unpack((char *)earg, size, &nvlp, NULL))
174 		return;
175 	if (nvlist_lookup_string(nvlp, "devfs-path", &path))
176 		return;
177 
178 	lookup.path = strdup(path);
179 	lookup.disk = NULL;
180 	lookup.result = DISK_NOT_FOUND;
181 
182 	status = ptree_walk_tree_by_class(root_node, "disk", (void *)&lookup,
183 	    find_disk);
184 	if (status != PICL_SUCCESS) {
185 		return;
186 	}
187 
188 	if (lookup.result == DISK_FOUND) {
189 		if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_ADDED) == 0)
190 			ptree_update_propval_by_name(lookup.disk, "State",
191 			    (void *)strdup(CONFIGURED), PICL_PROPNAMELEN_MAX);
192 		else if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_REMOVED) == 0)
193 			ptree_update_propval_by_name(lookup.disk, "State",
194 			    (void *)strdup(UNCONFIGURED), PICL_PROPNAMELEN_MAX);
195 	}
196 
197 	nvlist_free(nvlp);
198 }
199 
200 /*ARGSUSED*/
201 static void
202 mdesc_ev_completion_handler(char *ename, void *earg, size_t size)
203 {
204 	free(earg);
205 }
206 
207 static void
208 signal_devtree(void)
209 {
210 	nvlist_t *nvl;
211 	char *packed_nvl;
212 	size_t nvl_size;
213 	int status;
214 
215 	if (nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, NULL) != 0)
216 		return;
217 
218 	/*
219 	 * Right now (Aug. 2007) snowbird is the only other platform
220 	 * which uses this event.  Since that's a sun4u platform and
221 	 * this is sun4v we do not have to worry about possible confusion
222 	 * or interference between the two by grabbing this event for
223 	 * our own use here.  This event is consumed by the devtree
224 	 * plug-in.  The event signals the plug-in to re-run its
225 	 * cpu initialization function, which will cause it to add
226 	 * additional information to the cpu devtree nodes (particularly,
227 	 * the administrative state of the cpus.)
228 	 */
229 	if (nvlist_add_string(nvl, PICLEVENTARG_EVENT_NAME,
230 	    PICLEVENT_CPU_STATE_CHANGE) != 0) {
231 		free(nvl);
232 		return;
233 	}
234 
235 	/*
236 	 * The devtree plug-in needs to see a devfs path argument for
237 	 * any event it considers.  We supply one here which is essentially
238 	 * a dummy since it is not processed by the devtree plug-in for
239 	 * this event.
240 	 */
241 	if (nvlist_add_string(nvl, PICLEVENTARG_DEVFS_PATH, "/cpu") != 0) {
242 		free(nvl);
243 		return;
244 	}
245 	if (nvlist_pack(nvl, &packed_nvl, &nvl_size, NV_ENCODE_NATIVE,
246 	    NULL) != 0) {
247 		free(nvl);
248 		return;
249 	}
250 	if ((status = ptree_post_event(PICLEVENT_CPU_STATE_CHANGE,
251 	    packed_nvl, nvl_size, mdesc_ev_completion_handler)) !=
252 	    PICL_SUCCESS) {
253 		free(nvl);
254 		syslog(LOG_WARNING,
255 		    "signal_devtree: can't post cpu event: %d\n", status);
256 	}
257 }
258 
259 void
260 mdescplugin_init(void)
261 {
262 	int		status;
263 
264 	status = ptree_get_root(&root_node);
265 	if (status != PICL_SUCCESS) {
266 		return;
267 	}
268 
269 	mdp = mdesc_devinit();
270 	if (mdp == NULL)
271 		return;
272 
273 	rootnode = md_root_node(mdp);
274 
275 	/*
276 	 * This is the start of the CPU property augmentation code.
277 	 * add_cpu_prop and the rest of the CPU code lives in cpu_prop_update.c
278 	 */
279 	status = ptree_walk_tree_by_class(root_node, "cpu", NULL, add_cpu_prop);
280 	if (status != PICL_SUCCESS) {
281 		return;
282 	}
283 
284 	signal_devtree();
285 
286 	(void) disk_discovery();
287 
288 	/*
289 	 * register dsc_handler for both "sysevent-device-added" and
290 	 * and for "sysevent-device-removed" PICL events
291 	 */
292 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
293 	    dsc_handler, NULL);
294 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
295 	    dsc_handler, NULL);
296 	(void) ptree_register_handler(PICLEVENT_DR_AP_STATE_CHANGE,
297 	    dr_handler, NULL);
298 
299 	mdesc_devfini(mdp);
300 }
301 
302 void
303 mdescplugin_fini(void)
304 {
305 	/* unregister the event handler */
306 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
307 	    dsc_handler, NULL);
308 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
309 	    dsc_handler, NULL);
310 	(void) ptree_unregister_handler(PICLEVENT_DR_AP_STATE_CHANGE,
311 	    dr_handler, NULL);
312 }
313 
314 void
315 mdescplugin_register(void)
316 {
317 	picld_plugin_register(&mdescplugin_reg);
318 }
319