dmake: do not set MAKEFLAGS=k
[unleashed/tickless.git] / usr / src / cmd / picl / plugins / sun4v / mdesc / mdescplugin.c
blobfa3e985b002253a64a91f19c3f73f04ec995725c
1 /*
2 * CDDL HEADER START
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.
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.
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]
19 * CDDL HEADER END
23 * Copyright 2009 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
28 * The MDESC picl plugin serves 2 different functionalities.
29 * --The first is to look up certain CPU properties in the MDESC an to add
30 * these properties in the already created CPU PICL nodes in the /platform
31 * section of the tree.
32 * --The second functionality is to create a /disk_discovery section of the
33 * PICL tree which will have a disk node created for each disk node in the
34 * machine description.
37 #include "mdescplugin.h"
38 #include <libnvpair.h>
40 #pragma init(mdescplugin_register) /* place in .init section */
42 picl_nodehdl_t root_node;
43 md_t *mdp;
44 mde_cookie_t rootnode;
46 void mdescplugin_init(void);
47 void mdescplugin_fini(void);
48 static void signal_devtree(void);
50 extern int add_cpu_prop(picl_nodehdl_t node, void *args);
51 extern int disk_discovery(void);
52 extern md_t *mdesc_devinit(void);
53 extern void mdesc_devfini(md_t *mdp);
54 extern int update_devices(char *dev, int op);
56 picld_plugin_reg_t mdescplugin_reg = {
57 PICLD_PLUGIN_VERSION_1,
58 PICLD_PLUGIN_CRITICAL,
59 "mdesc_plugin",
60 mdescplugin_init,
61 mdescplugin_fini
64 #define DISK_FOUND 0x00
65 #define DISK_NOT_FOUND 0x01
67 typedef struct disk_lookup {
68 char *path;
69 picl_nodehdl_t disk;
70 int result;
71 } disk_lookup_t;
73 int
74 find_disk(picl_nodehdl_t node, void *args)
76 disk_lookup_t *lookup = (disk_lookup_t *)args;
77 int status;
78 char path[PICL_PROPNAMELEN_MAX];
80 status = ptree_get_propval_by_name(node, "Path", (void *)&path,
81 PICL_PROPNAMELEN_MAX);
82 if (status != PICL_SUCCESS) {
83 return (PICL_WALK_CONTINUE);
86 if (strcmp(path, lookup->path) == 0) {
87 lookup->disk = node;
88 lookup->result = DISK_FOUND;
89 return (PICL_WALK_TERMINATE);
92 return (PICL_WALK_CONTINUE);
96 * DR event handler
97 * respond to the picl events:
98 * PICLEVENT_DR_AP_STATE_CHANGE
100 static void
101 dr_handler(const char *ename, const void *earg, size_t size, void *cookie)
103 nvlist_t *nvlp = NULL;
104 char *dtype;
105 char *ap_id;
106 char *hint;
109 if (strcmp(ename, PICLEVENT_DR_AP_STATE_CHANGE) != 0) {
110 return;
113 if (nvlist_unpack((char *)earg, size, &nvlp, NULL)) {
114 return;
117 if (nvlist_lookup_string(nvlp, PICLEVENTARG_DATA_TYPE, &dtype)) {
118 nvlist_free(nvlp);
119 return;
122 if (strcmp(dtype, PICLEVENTARG_PICLEVENT_DATA) != 0) {
123 nvlist_free(nvlp);
124 return;
127 if (nvlist_lookup_string(nvlp, PICLEVENTARG_AP_ID, &ap_id)) {
128 nvlist_free(nvlp);
129 return;
132 if (nvlist_lookup_string(nvlp, PICLEVENTARG_HINT, &hint)) {
133 nvlist_free(nvlp);
134 return;
137 mdp = mdesc_devinit();
138 if (mdp == NULL) {
139 nvlist_free(nvlp);
140 return;
143 rootnode = md_root_node(mdp);
145 if (strcmp(hint, DR_HINT_INSERT) == 0)
146 (void) update_devices(ap_id, DEV_ADD);
147 else if (strcmp(hint, DR_HINT_REMOVE) == 0)
148 (void) update_devices(ap_id, DEV_REMOVE);
150 mdesc_devfini(mdp);
151 nvlist_free(nvlp);
154 * Signal the devtree plugin to add more cpu properties.
156 signal_devtree();
160 * Discovery event handler
161 * respond to the picl events:
162 * PICLEVENT_SYSEVENT_DEVICE_ADDED
163 * PICLEVENT_SYSEVENT_DEVICE_REMOVED
165 static void
166 dsc_handler(const char *ename, const void *earg, size_t size, void *cookie)
168 nvlist_t *nvlp = NULL;
169 char *path;
170 disk_lookup_t lookup;
171 int status;
174 * retrieve the device's physical path from the event arg
175 * and determine which disk (if any) we are working with
177 if (nvlist_unpack((char *)earg, size, &nvlp, NULL))
178 return;
179 if (nvlist_lookup_string(nvlp, "devfs-path", &path))
180 return;
182 lookup.path = strdup(path);
183 lookup.disk = NULL;
184 lookup.result = DISK_NOT_FOUND;
186 status = ptree_walk_tree_by_class(root_node, "disk", (void *)&lookup,
187 find_disk);
188 if (status != PICL_SUCCESS) {
189 return;
192 if (lookup.result == DISK_FOUND) {
193 if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_ADDED) == 0)
194 ptree_update_propval_by_name(lookup.disk, "State",
195 (void *)strdup(CONFIGURED), PICL_PROPNAMELEN_MAX);
196 else if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_REMOVED) == 0)
197 ptree_update_propval_by_name(lookup.disk, "State",
198 (void *)strdup(UNCONFIGURED), PICL_PROPNAMELEN_MAX);
201 nvlist_free(nvlp);
204 /*ARGSUSED*/
205 static void
206 mdesc_ev_completion_handler(char *ename, void *earg, size_t size)
208 free(earg);
211 static void
212 signal_devtree(void)
214 nvlist_t *nvl;
215 char *packed_nvl;
216 size_t nvl_size;
217 int status;
219 if (nvlist_alloc(&nvl, NV_UNIQUE_NAME_TYPE, NULL) != 0)
220 return;
223 * Right now (Aug. 2007) snowbird is the only other platform
224 * which uses this event. Since that's a sun4u platform and
225 * this is sun4v we do not have to worry about possible confusion
226 * or interference between the two by grabbing this event for
227 * our own use here. This event is consumed by the devtree
228 * plug-in. The event signals the plug-in to re-run its
229 * cpu initialization function, which will cause it to add
230 * additional information to the cpu devtree nodes (particularly,
231 * the administrative state of the cpus.)
233 if (nvlist_add_string(nvl, PICLEVENTARG_EVENT_NAME,
234 PICLEVENT_CPU_STATE_CHANGE) != 0) {
235 free(nvl);
236 return;
240 * The devtree plug-in needs to see a devfs path argument for
241 * any event it considers. We supply one here which is essentially
242 * a dummy since it is not processed by the devtree plug-in for
243 * this event.
245 if (nvlist_add_string(nvl, PICLEVENTARG_DEVFS_PATH, "/cpu") != 0) {
246 free(nvl);
247 return;
249 packed_nvl = NULL;
250 if (nvlist_pack(nvl, &packed_nvl, &nvl_size, NV_ENCODE_NATIVE,
251 0) != 0) {
252 free(nvl);
253 return;
255 if ((status = ptree_post_event(PICLEVENT_CPU_STATE_CHANGE,
256 packed_nvl, nvl_size, mdesc_ev_completion_handler)) !=
257 PICL_SUCCESS) {
258 free(nvl);
259 syslog(LOG_WARNING,
260 "signal_devtree: can't post cpu event: %d\n", status);
264 void
265 mdescplugin_init(void)
267 int status;
269 status = ptree_get_root(&root_node);
270 if (status != PICL_SUCCESS) {
271 return;
274 mdp = mdesc_devinit();
275 if (mdp == NULL)
276 return;
279 * update the cpu configuration in case the snapshot cache used by the
280 * devtree plugin is out of date.
282 (void) update_devices(OBP_CPU, DEV_ADD);
283 (void) update_devices(OBP_CPU, DEV_REMOVE);
285 rootnode = md_root_node(mdp);
288 * This is the start of the CPU property augmentation code.
289 * add_cpu_prop and the rest of the CPU code lives in cpu_prop_update.c
291 status = ptree_walk_tree_by_class(root_node, "cpu", NULL, add_cpu_prop);
292 if (status != PICL_SUCCESS) {
293 return;
296 signal_devtree();
298 (void) disk_discovery();
301 * register dsc_handler for both "sysevent-device-added" and
302 * and for "sysevent-device-removed" PICL events
304 (void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
305 dsc_handler, NULL);
306 (void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
307 dsc_handler, NULL);
308 (void) ptree_register_handler(PICLEVENT_DR_AP_STATE_CHANGE,
309 dr_handler, NULL);
311 mdesc_devfini(mdp);
314 void
315 mdescplugin_fini(void)
317 /* unregister the event handler */
318 (void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
319 dsc_handler, NULL);
320 (void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
321 dsc_handler, NULL);
322 (void) ptree_unregister_handler(PICLEVENT_DR_AP_STATE_CHANGE,
323 dr_handler, NULL);
326 void
327 mdescplugin_register(void)
329 picld_plugin_register(&mdescplugin_reg);