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 2006 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 
41 #pragma init(mdescplugin_register)	/* place in .init section */
42 
43 picl_nodehdl_t	root_node;
44 md_t		*mdp;
45 mde_cookie_t	rootnode;
46 
47 void mdescplugin_init(void);
48 void mdescplugin_fini(void);
49 
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);
55 
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
62 };
63 
64 #define	DISK_FOUND 0x00
65 #define	DISK_NOT_FOUND 0x01
66 
67 typedef struct disk_lookup {
68 	char *path;
69 	picl_nodehdl_t disk;
70 	int result;
71 } disk_lookup_t;
72 
73 int
74 find_disk(picl_nodehdl_t node, void *args)
75 {
76 	disk_lookup_t *lookup  = (disk_lookup_t *)args;
77 	int status;
78 	char path[PICL_PROPNAMELEN_MAX];
79 
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);
84 	}
85 
86 	if (strcmp(path, lookup->path) == 0) {
87 		lookup->disk = node;
88 		lookup->result = DISK_FOUND;
89 		return (PICL_WALK_TERMINATE);
90 	}
91 
92 	return (PICL_WALK_CONTINUE);
93 }
94 
95 /*
96  * DR event handler
97  * respond to the picl events:
98  *      PICLEVENT_DR_AP_STATE_CHANGE
99  */
100 static void
101 dr_handler(const char *ename, const void *earg, size_t size, void *cookie)
102 {
103 	nvlist_t	*nvlp = NULL;
104 	char		*dtype;
105 	char		*ap_id;
106 	char		*hint;
107 
108 
109 	if (strcmp(ename, PICLEVENT_DR_AP_STATE_CHANGE) != 0) {
110 		return;
111 	}
112 
113 	if (nvlist_unpack((char *)earg, size, &nvlp, NULL)) {
114 		return;
115 	}
116 
117 	if (nvlist_lookup_string(nvlp, PICLEVENTARG_DATA_TYPE, &dtype)) {
118 		nvlist_free(nvlp);
119 		return;
120 	}
121 
122 	if (strcmp(dtype, PICLEVENTARG_PICLEVENT_DATA) != 0) {
123 		nvlist_free(nvlp);
124 		return;
125 	}
126 
127 	if (nvlist_lookup_string(nvlp, PICLEVENTARG_AP_ID, &ap_id)) {
128 		nvlist_free(nvlp);
129 		return;
130 	}
131 
132 	if (nvlist_lookup_string(nvlp, PICLEVENTARG_HINT, &hint)) {
133 		nvlist_free(nvlp);
134 		return;
135 	}
136 
137 	mdp = mdesc_devinit();
138 	if (mdp == NULL) {
139 		nvlist_free(nvlp);
140 		return;
141 	}
142 
143 	rootnode = md_root_node(mdp);
144 
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);
149 
150 	mdesc_devfini(mdp);
151 	nvlist_free(nvlp);
152 }
153 
154 /*
155  * Discovery event handler
156  * respond to the picl events:
157  *      PICLEVENT_SYSEVENT_DEVICE_ADDED
158  *      PICLEVENT_SYSEVENT_DEVICE_REMOVED
159  */
160 static void
161 dsc_handler(const char *ename, const void *earg, size_t size, void *cookie)
162 {
163 	nvlist_t	*nvlp = NULL;
164 	char		*path;
165 	disk_lookup_t	lookup;
166 	int		status;
167 
168 	/*
169 	 * retrieve the device's physical path from the event arg
170 	 * and determine which disk (if any) we are working with
171 	 */
172 	if (nvlist_unpack((char *)earg, size, &nvlp, NULL))
173 		return;
174 	if (nvlist_lookup_string(nvlp, "devfs-path", &path))
175 		return;
176 
177 	lookup.path = strdup(path);
178 	lookup.disk = NULL;
179 	lookup.result = DISK_NOT_FOUND;
180 
181 	status = ptree_walk_tree_by_class(root_node, "disk", (void *)&lookup,
182 	    find_disk);
183 	if (status != PICL_SUCCESS) {
184 		return;
185 	}
186 
187 	if (lookup.result == DISK_FOUND) {
188 		if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_ADDED) == 0)
189 			ptree_update_propval_by_name(lookup.disk, "State",
190 			    (void *)strdup(CONFIGURED), PICL_PROPNAMELEN_MAX);
191 		else if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_REMOVED) == 0)
192 			ptree_update_propval_by_name(lookup.disk, "State",
193 			    (void *)strdup(UNCONFIGURED), PICL_PROPNAMELEN_MAX);
194 	}
195 
196 	nvlist_free(nvlp);
197 }
198 
199 void
200 mdescplugin_init(void)
201 {
202 	int		status;
203 
204 	status = ptree_get_root(&root_node);
205 	if (status != PICL_SUCCESS) {
206 		return;
207 	}
208 
209 	mdp = mdesc_devinit();
210 	if (mdp == NULL)
211 		return;
212 
213 	rootnode = md_root_node(mdp);
214 
215 	/*
216 	 * This is the start of the CPU property augmentation code.
217 	 * add_cpu_prop and the rest of the CPU code lives in cpu_prop_update.c
218 	 */
219 	status = ptree_walk_tree_by_class(root_node, "cpu", NULL, add_cpu_prop);
220 	if (status != PICL_SUCCESS) {
221 		return;
222 	}
223 
224 	(void) disk_discovery();
225 
226 	/*
227 	 * register dsc_handler for both "sysevent-device-added" and
228 	 * and for "sysevent-device-removed" PICL events
229 	 */
230 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
231 	    dsc_handler, NULL);
232 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
233 	    dsc_handler, NULL);
234 	(void) ptree_register_handler(PICLEVENT_DR_AP_STATE_CHANGE,
235 	    dr_handler, NULL);
236 
237 	mdesc_devfini(mdp);
238 }
239 
240 void
241 mdescplugin_fini(void)
242 {
243 	/* unregister the event handler */
244 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
245 	    dsc_handler, NULL);
246 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
247 	    dsc_handler, NULL);
248 	(void) ptree_unregister_handler(PICLEVENT_DR_AP_STATE_CHANGE,
249 	    dr_handler, NULL);
250 }
251 
252 void
253 mdescplugin_register(void)
254 {
255 	picld_plugin_register(&mdescplugin_reg);
256 }
257