xref: /titanic_41/usr/src/cmd/picl/plugins/sun4v/mdesc/mdescplugin.c (revision 40db2e2b777b79f3dd0d6d9629593a07f86b9c0a)
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 	packed_nvl = NULL;
246 	if (nvlist_pack(nvl, &packed_nvl, &nvl_size, NV_ENCODE_NATIVE,
247 	    0) != 0) {
248 		free(nvl);
249 		return;
250 	}
251 	if ((status = ptree_post_event(PICLEVENT_CPU_STATE_CHANGE,
252 	    packed_nvl, nvl_size, mdesc_ev_completion_handler)) !=
253 	    PICL_SUCCESS) {
254 		free(nvl);
255 		syslog(LOG_WARNING,
256 		    "signal_devtree: can't post cpu event: %d\n", status);
257 	}
258 }
259 
260 void
261 mdescplugin_init(void)
262 {
263 	int		status;
264 
265 	status = ptree_get_root(&root_node);
266 	if (status != PICL_SUCCESS) {
267 		return;
268 	}
269 
270 	mdp = mdesc_devinit();
271 	if (mdp == NULL)
272 		return;
273 
274 	rootnode = md_root_node(mdp);
275 
276 	/*
277 	 * This is the start of the CPU property augmentation code.
278 	 * add_cpu_prop and the rest of the CPU code lives in cpu_prop_update.c
279 	 */
280 	status = ptree_walk_tree_by_class(root_node, "cpu", NULL, add_cpu_prop);
281 	if (status != PICL_SUCCESS) {
282 		return;
283 	}
284 
285 	signal_devtree();
286 
287 	(void) disk_discovery();
288 
289 	/*
290 	 * register dsc_handler for both "sysevent-device-added" and
291 	 * and for "sysevent-device-removed" PICL events
292 	 */
293 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
294 	    dsc_handler, NULL);
295 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
296 	    dsc_handler, NULL);
297 	(void) ptree_register_handler(PICLEVENT_DR_AP_STATE_CHANGE,
298 	    dr_handler, NULL);
299 
300 	mdesc_devfini(mdp);
301 }
302 
303 void
304 mdescplugin_fini(void)
305 {
306 	/* unregister the event handler */
307 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
308 	    dsc_handler, NULL);
309 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
310 	    dsc_handler, NULL);
311 	(void) ptree_unregister_handler(PICLEVENT_DR_AP_STATE_CHANGE,
312 	    dr_handler, NULL);
313 }
314 
315 void
316 mdescplugin_register(void)
317 {
318 	picld_plugin_register(&mdescplugin_reg);
319 }
320