xref: /onnv-gate/usr/src/cmd/picl/plugins/sun4v/mdesc/mdescplugin.c (revision 0:68f95e015346)
1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2005 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * The MDESC picl plugin serves 2 different functionalities.
31*0Sstevel@tonic-gate  * --The first is to look up certain CPU properties in the MDESC an to add
32*0Sstevel@tonic-gate  * these properties in the already created CPU PICL nodes in the /platform
33*0Sstevel@tonic-gate  * section of the tree.
34*0Sstevel@tonic-gate  * --The second functionality is to create a /disk_discovery section of the
35*0Sstevel@tonic-gate  * PICL tree which will have a disk node created for each disk node in the
36*0Sstevel@tonic-gate  * machine description.
37*0Sstevel@tonic-gate  */
38*0Sstevel@tonic-gate 
39*0Sstevel@tonic-gate #include "mdescplugin.h"
40*0Sstevel@tonic-gate 
41*0Sstevel@tonic-gate #pragma init(mdescplugin_register)	/* place in .init section */
42*0Sstevel@tonic-gate 
43*0Sstevel@tonic-gate picl_nodehdl_t	root_node;
44*0Sstevel@tonic-gate md_t		*mdp;
45*0Sstevel@tonic-gate mde_cookie_t	rootnode;
46*0Sstevel@tonic-gate 
47*0Sstevel@tonic-gate void mdescplugin_init(void);
48*0Sstevel@tonic-gate void mdescplugin_fini(void);
49*0Sstevel@tonic-gate 
50*0Sstevel@tonic-gate extern int add_cpu_prop(picl_nodehdl_t node, void *args);
51*0Sstevel@tonic-gate extern int disk_discovery(void);
52*0Sstevel@tonic-gate extern md_t *mdesc_devinit(void);
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate picld_plugin_reg_t mdescplugin_reg = {
55*0Sstevel@tonic-gate 	PICLD_PLUGIN_VERSION_1,
56*0Sstevel@tonic-gate 	PICLD_PLUGIN_CRITICAL,
57*0Sstevel@tonic-gate 	"mdesc_plugin",
58*0Sstevel@tonic-gate 	mdescplugin_init,
59*0Sstevel@tonic-gate 	mdescplugin_fini
60*0Sstevel@tonic-gate };
61*0Sstevel@tonic-gate 
62*0Sstevel@tonic-gate #define	DISK_FOUND 0x00
63*0Sstevel@tonic-gate #define	DISK_NOT_FOUND 0x01
64*0Sstevel@tonic-gate 
65*0Sstevel@tonic-gate typedef struct disk_lookup {
66*0Sstevel@tonic-gate 	char *path;
67*0Sstevel@tonic-gate 	picl_nodehdl_t disk;
68*0Sstevel@tonic-gate 	int result;
69*0Sstevel@tonic-gate } disk_lookup_t;
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate int
72*0Sstevel@tonic-gate find_disk(picl_nodehdl_t node, void *args)
73*0Sstevel@tonic-gate {
74*0Sstevel@tonic-gate 	disk_lookup_t *lookup  = (disk_lookup_t *)args;
75*0Sstevel@tonic-gate 	int status;
76*0Sstevel@tonic-gate 	char path[PICL_PROPNAMELEN_MAX];
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate 	status = ptree_get_propval_by_name(node, "Path", (void *)&path,
79*0Sstevel@tonic-gate 	    PICL_PROPNAMELEN_MAX);
80*0Sstevel@tonic-gate 	if (status != PICL_SUCCESS) {
81*0Sstevel@tonic-gate 		return (PICL_WALK_CONTINUE);
82*0Sstevel@tonic-gate 	}
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate 	if (strcmp(path, lookup->path) == 0) {
85*0Sstevel@tonic-gate 		lookup->disk = node;
86*0Sstevel@tonic-gate 		lookup->result = DISK_FOUND;
87*0Sstevel@tonic-gate 		return (PICL_WALK_TERMINATE);
88*0Sstevel@tonic-gate 	}
89*0Sstevel@tonic-gate 
90*0Sstevel@tonic-gate 	return (PICL_WALK_CONTINUE);
91*0Sstevel@tonic-gate }
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate /*
94*0Sstevel@tonic-gate  * Discovery event handler
95*0Sstevel@tonic-gate  * respond to the picl events:
96*0Sstevel@tonic-gate  *      PICLEVENT_SYSEVENT_DEVICE_ADDED
97*0Sstevel@tonic-gate  *      PICLEVENT_SYSEVENT_DEVICE_REMOVED
98*0Sstevel@tonic-gate  */
99*0Sstevel@tonic-gate static void
100*0Sstevel@tonic-gate dsc_handler(const char *ename, const void *earg, size_t size, void *cookie)
101*0Sstevel@tonic-gate {
102*0Sstevel@tonic-gate 	nvlist_t	*nvlp = NULL;
103*0Sstevel@tonic-gate 	char		*path;
104*0Sstevel@tonic-gate 	disk_lookup_t	lookup;
105*0Sstevel@tonic-gate 	int		status;
106*0Sstevel@tonic-gate 
107*0Sstevel@tonic-gate 	/*
108*0Sstevel@tonic-gate 	 * retrieve the device's physical path from the event arg
109*0Sstevel@tonic-gate 	 * and determine which disk (if any) we are working with
110*0Sstevel@tonic-gate 	 */
111*0Sstevel@tonic-gate 	if (nvlist_unpack((char *)earg, size, &nvlp, NULL))
112*0Sstevel@tonic-gate 		return;
113*0Sstevel@tonic-gate 	if (nvlist_lookup_string(nvlp, "devfs-path", &path))
114*0Sstevel@tonic-gate 		return;
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate 	lookup.path = strdup(path);
117*0Sstevel@tonic-gate 	lookup.disk = NULL;
118*0Sstevel@tonic-gate 	lookup.result = DISK_NOT_FOUND;
119*0Sstevel@tonic-gate 
120*0Sstevel@tonic-gate 	status = ptree_walk_tree_by_class(root_node, "disk", (void *)&lookup,
121*0Sstevel@tonic-gate 	    find_disk);
122*0Sstevel@tonic-gate 	if (status != PICL_SUCCESS) {
123*0Sstevel@tonic-gate 		return;
124*0Sstevel@tonic-gate 	}
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	if (lookup.result == DISK_FOUND) {
127*0Sstevel@tonic-gate 		if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_ADDED) == 0)
128*0Sstevel@tonic-gate 			ptree_update_propval_by_name(lookup.disk, "State",
129*0Sstevel@tonic-gate 			    (void *)strdup(CONFIGURED), PICL_PROPNAMELEN_MAX);
130*0Sstevel@tonic-gate 		else if (strcmp(ename, PICLEVENT_SYSEVENT_DEVICE_REMOVED) == 0)
131*0Sstevel@tonic-gate 			ptree_update_propval_by_name(lookup.disk, "State",
132*0Sstevel@tonic-gate 			    (void *)strdup(UNCONFIGURED), PICL_PROPNAMELEN_MAX);
133*0Sstevel@tonic-gate 	}
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	nvlist_free(nvlp);
136*0Sstevel@tonic-gate }
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate void
139*0Sstevel@tonic-gate mdescplugin_init(void)
140*0Sstevel@tonic-gate {
141*0Sstevel@tonic-gate 	int		status;
142*0Sstevel@tonic-gate 
143*0Sstevel@tonic-gate 	status = ptree_get_root(&root_node);
144*0Sstevel@tonic-gate 	if (status != PICL_SUCCESS) {
145*0Sstevel@tonic-gate 		return;
146*0Sstevel@tonic-gate 	}
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	mdp = mdesc_devinit();
149*0Sstevel@tonic-gate 	if (mdp == NULL)
150*0Sstevel@tonic-gate 		return;
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate 	rootnode = md_root_node(mdp);
153*0Sstevel@tonic-gate 
154*0Sstevel@tonic-gate 	/*
155*0Sstevel@tonic-gate 	 * This is the start of the CPU property augmentation code.
156*0Sstevel@tonic-gate 	 * add_cpu_prop and the rest of the CPU code lives in cpu_prop_update.c
157*0Sstevel@tonic-gate 	 */
158*0Sstevel@tonic-gate 	status = ptree_walk_tree_by_class(root_node, "cpu", NULL, add_cpu_prop);
159*0Sstevel@tonic-gate 	if (status != PICL_SUCCESS) {
160*0Sstevel@tonic-gate 		return;
161*0Sstevel@tonic-gate 	}
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 	(void) disk_discovery();
164*0Sstevel@tonic-gate 
165*0Sstevel@tonic-gate 	/*
166*0Sstevel@tonic-gate 	 * register dsc_handler for both "sysevent-device-added" and
167*0Sstevel@tonic-gate 	 * and for "sysevent-device-removed" PICL events
168*0Sstevel@tonic-gate 	 */
169*0Sstevel@tonic-gate 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
170*0Sstevel@tonic-gate 	    dsc_handler, NULL);
171*0Sstevel@tonic-gate 	(void) ptree_register_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
172*0Sstevel@tonic-gate 	    dsc_handler, NULL);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 	(void) md_fini(mdp);
175*0Sstevel@tonic-gate }
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate void
178*0Sstevel@tonic-gate mdescplugin_fini(void)
179*0Sstevel@tonic-gate {
180*0Sstevel@tonic-gate 	/* unregister the event handler */
181*0Sstevel@tonic-gate 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_ADDED,
182*0Sstevel@tonic-gate 	    dsc_handler, NULL);
183*0Sstevel@tonic-gate 	(void) ptree_unregister_handler(PICLEVENT_SYSEVENT_DEVICE_REMOVED,
184*0Sstevel@tonic-gate 	    dsc_handler, NULL);
185*0Sstevel@tonic-gate }
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate void
188*0Sstevel@tonic-gate mdescplugin_register(void)
189*0Sstevel@tonic-gate {
190*0Sstevel@tonic-gate 	picld_plugin_register(&mdescplugin_reg);
191*0Sstevel@tonic-gate }
192