xref: /onnv-gate/usr/src/cmd/picl/plugins/sun4u/taco/envd/piclenvsetup.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 2004 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  * This file contains code for setting up environmental related nodes
31*0Sstevel@tonic-gate  * and properties in the PICL tree.
32*0Sstevel@tonic-gate  *
33*0Sstevel@tonic-gate  */
34*0Sstevel@tonic-gate 
35*0Sstevel@tonic-gate #include <stdio.h>
36*0Sstevel@tonic-gate #include <fcntl.h>
37*0Sstevel@tonic-gate #include <unistd.h>
38*0Sstevel@tonic-gate #include <syslog.h>
39*0Sstevel@tonic-gate #include <stdlib.h>
40*0Sstevel@tonic-gate #include <errno.h>
41*0Sstevel@tonic-gate #include <limits.h>
42*0Sstevel@tonic-gate #include <sys/open.h>
43*0Sstevel@tonic-gate #include <ctype.h>
44*0Sstevel@tonic-gate #include <string.h>
45*0Sstevel@tonic-gate #include <alloca.h>
46*0Sstevel@tonic-gate #include <libintl.h>
47*0Sstevel@tonic-gate #include <sys/systeminfo.h>
48*0Sstevel@tonic-gate #include <picl.h>
49*0Sstevel@tonic-gate #include <picltree.h>
50*0Sstevel@tonic-gate #include <picld_pluginutil.h>
51*0Sstevel@tonic-gate #include <pthread.h>
52*0Sstevel@tonic-gate #include <sys/utsname.h>
53*0Sstevel@tonic-gate #include <sys/systeminfo.h>
54*0Sstevel@tonic-gate #include "picldefs.h"
55*0Sstevel@tonic-gate #include "envd.h"
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate /*
58*0Sstevel@tonic-gate  * Volatile property read/write function typedef
59*0Sstevel@tonic-gate  */
60*0Sstevel@tonic-gate typedef int ptree_vol_rdfunc_t(ptree_rarg_t *parg, void *buf);
61*0Sstevel@tonic-gate typedef int ptree_vol_wrfunc_t(ptree_warg_t *parg, const void *buf);
62*0Sstevel@tonic-gate 
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate extern sensor_ctrl_blk_t sensor_ctrl[];
65*0Sstevel@tonic-gate extern fan_ctrl_blk_t fan_ctrl[];
66*0Sstevel@tonic-gate extern env_tuneable_t	tuneables[];
67*0Sstevel@tonic-gate extern int ntuneables;
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate #define	PROP_FAN_SPEED_UNIT_VALUE	"rpm"
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate /*
72*0Sstevel@tonic-gate  * Sensor node data structure
73*0Sstevel@tonic-gate  */
74*0Sstevel@tonic-gate typedef struct {
75*0Sstevel@tonic-gate 	char		*parent_path;	/* parent path */
76*0Sstevel@tonic-gate 	char		*sensor_name;	/* sensor name */
77*0Sstevel@tonic-gate 	env_sensor_t	*sensorp;	/* sensor info */
78*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;		/* sensor node handle */
79*0Sstevel@tonic-gate 	picl_prophdl_t	proph;		/* "Temperature" property handle */
80*0Sstevel@tonic-gate 	picl_prophdl_t	target_proph;	/* "TargetTemp" property handle */
81*0Sstevel@tonic-gate } sensor_node_t;
82*0Sstevel@tonic-gate 
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate /*
85*0Sstevel@tonic-gate  * Sensor nodes array
86*0Sstevel@tonic-gate  */
87*0Sstevel@tonic-gate static sensor_node_t sensor_nodes[] = {
88*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
89*0Sstevel@tonic-gate 	SENSOR_CPU_DIE, NULL,
90*0Sstevel@tonic-gate 	NULL, NULL, NULL},
91*0Sstevel@tonic-gate 
92*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
93*0Sstevel@tonic-gate 	SENSOR_INT_AMB, NULL,
94*0Sstevel@tonic-gate 	NULL, NULL, NULL},
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
97*0Sstevel@tonic-gate 	SENSOR_SYS_IN, NULL,
98*0Sstevel@tonic-gate 	NULL, NULL, NULL}
99*0Sstevel@tonic-gate };
100*0Sstevel@tonic-gate #define	NSENSORS	(sizeof (sensor_nodes)/sizeof (sensor_nodes[0]))
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 
103*0Sstevel@tonic-gate /*
104*0Sstevel@tonic-gate  * Fan node data structure
105*0Sstevel@tonic-gate  */
106*0Sstevel@tonic-gate typedef struct {
107*0Sstevel@tonic-gate 	char		*parent_path;	/* parent node path */
108*0Sstevel@tonic-gate 	char		*fan_name;	/* fan name */
109*0Sstevel@tonic-gate 	env_fan_t 	*fanp;		/* fan information */
110*0Sstevel@tonic-gate 	char		*speed_unit;	/* speed unit string */
111*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;		/* "fan" node handle */
112*0Sstevel@tonic-gate 	picl_prophdl_t	proph;		/* "Speed" property handle */
113*0Sstevel@tonic-gate } fan_node_t;
114*0Sstevel@tonic-gate 
115*0Sstevel@tonic-gate 
116*0Sstevel@tonic-gate /*
117*0Sstevel@tonic-gate  * Fan node array
118*0Sstevel@tonic-gate  */
119*0Sstevel@tonic-gate static fan_node_t fan_nodes[] =  {
120*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
121*0Sstevel@tonic-gate 	ENV_CPU_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
124*0Sstevel@tonic-gate 	ENV_SYSTEM_INTAKE_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
125*0Sstevel@tonic-gate 
126*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
127*0Sstevel@tonic-gate 	ENV_SYSTEM_OUT_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL}
128*0Sstevel@tonic-gate };
129*0Sstevel@tonic-gate #define	NFANS	(sizeof (fan_nodes)/sizeof (fan_nodes[0]))
130*0Sstevel@tonic-gate 
131*0Sstevel@tonic-gate /*
132*0Sstevel@tonic-gate  * Miscellaneous declarations
133*0Sstevel@tonic-gate  */
134*0Sstevel@tonic-gate static void delete_sensor_nodes_and_props(void);
135*0Sstevel@tonic-gate static void delete_fan_nodes_and_props(void);
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate /*
139*0Sstevel@tonic-gate  * Read function for volatile "Temperature" property
140*0Sstevel@tonic-gate  */
141*0Sstevel@tonic-gate static int
get_current_temp(ptree_rarg_t * parg,void * buf)142*0Sstevel@tonic-gate get_current_temp(ptree_rarg_t *parg, void *buf)
143*0Sstevel@tonic-gate {
144*0Sstevel@tonic-gate 	tempr_t 	temp;
145*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
146*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
147*0Sstevel@tonic-gate 	int		i;
148*0Sstevel@tonic-gate 
149*0Sstevel@tonic-gate 	/*
150*0Sstevel@tonic-gate 	 * Locate the sensor in our sensor_nodes table by matching the
151*0Sstevel@tonic-gate 	 * property handle and get its temperature.
152*0Sstevel@tonic-gate 	 */
153*0Sstevel@tonic-gate 	proph = parg->proph;
154*0Sstevel@tonic-gate 	for (i = 0; i < NSENSORS; ++i) {
155*0Sstevel@tonic-gate 		snodep = &sensor_nodes[i];
156*0Sstevel@tonic-gate 		if (snodep->proph != proph)
157*0Sstevel@tonic-gate 			continue;
158*0Sstevel@tonic-gate 
159*0Sstevel@tonic-gate 		if (get_temperature(snodep->sensorp, &temp) < 0)
160*0Sstevel@tonic-gate 			break;
161*0Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&temp, sizeof (tempr_t));
162*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
163*0Sstevel@tonic-gate 	}
164*0Sstevel@tonic-gate 	return (PICL_FAILURE);
165*0Sstevel@tonic-gate }
166*0Sstevel@tonic-gate /*
167*0Sstevel@tonic-gate  * Read function for volatile "Speed" property on "fan" class node
168*0Sstevel@tonic-gate  */
169*0Sstevel@tonic-gate static int
set_current_speed(ptree_warg_t * parg,const void * buf)170*0Sstevel@tonic-gate set_current_speed(ptree_warg_t *parg, const void *buf)
171*0Sstevel@tonic-gate {
172*0Sstevel@tonic-gate 	fanspeed_t	speed;
173*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
174*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
175*0Sstevel@tonic-gate 	int		i, ret;
176*0Sstevel@tonic-gate 
177*0Sstevel@tonic-gate 	/*
178*0Sstevel@tonic-gate 	 * Locate the fan in our fan_nodes table by matching the
179*0Sstevel@tonic-gate 	 * property handle and get fan speed.
180*0Sstevel@tonic-gate 	 */
181*0Sstevel@tonic-gate 	proph = parg->proph;
182*0Sstevel@tonic-gate 	for (i = 0; i < NFANS; ++i) {
183*0Sstevel@tonic-gate 		fnodep = &fan_nodes[i];
184*0Sstevel@tonic-gate 		if (fnodep->proph != proph)
185*0Sstevel@tonic-gate 			continue;
186*0Sstevel@tonic-gate 		if (fnodep->fanp->fd == -1)
187*0Sstevel@tonic-gate 			continue;
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 		(void) memcpy((caddr_t)&speed, buf, sizeof (speed));
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		ret = set_fan_speed(fnodep->fanp, speed);
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 		if (ret < 0) {
194*0Sstevel@tonic-gate 			if (ret == -1 && errno == EBUSY)
195*0Sstevel@tonic-gate 				return (PICL_NOTWRITABLE);
196*0Sstevel@tonic-gate 			if (ret == -2)
197*0Sstevel@tonic-gate 				return (PICL_INVALIDARG);
198*0Sstevel@tonic-gate 			break;
199*0Sstevel@tonic-gate 		}
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
202*0Sstevel@tonic-gate 	}
203*0Sstevel@tonic-gate 	return (PICL_FAILURE);
204*0Sstevel@tonic-gate }
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate /*
208*0Sstevel@tonic-gate  * Read function for volatile "Speed" property on "fan" class node
209*0Sstevel@tonic-gate  */
210*0Sstevel@tonic-gate static int
get_current_speed(ptree_rarg_t * parg,void * buf)211*0Sstevel@tonic-gate get_current_speed(ptree_rarg_t *parg, void *buf)
212*0Sstevel@tonic-gate {
213*0Sstevel@tonic-gate 	fanspeed_t	speed;
214*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
215*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
216*0Sstevel@tonic-gate 	int		i;
217*0Sstevel@tonic-gate 
218*0Sstevel@tonic-gate 	/*
219*0Sstevel@tonic-gate 	 * Locate the fan in our fan_nodes table by matching the
220*0Sstevel@tonic-gate 	 * property handle and get fan speed.
221*0Sstevel@tonic-gate 	 */
222*0Sstevel@tonic-gate 	proph = parg->proph;
223*0Sstevel@tonic-gate 	for (i = 0; i < NFANS; ++i) {
224*0Sstevel@tonic-gate 		fnodep = &fan_nodes[i];
225*0Sstevel@tonic-gate 		if (fnodep->proph != proph)
226*0Sstevel@tonic-gate 			continue;
227*0Sstevel@tonic-gate 		if (fnodep->fanp->fd == -1)
228*0Sstevel@tonic-gate 			continue;
229*0Sstevel@tonic-gate 		if (get_fan_speed(fnodep->fanp, &speed) < 0)
230*0Sstevel@tonic-gate 			break;
231*0Sstevel@tonic-gate 
232*0Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&speed, sizeof (speed));
233*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate 	return (PICL_FAILURE);
236*0Sstevel@tonic-gate }
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate /*
239*0Sstevel@tonic-gate  * Create and add the specified regular property
240*0Sstevel@tonic-gate  */
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate static int
add_regular_prop(picl_nodehdl_t nodeh,char * name,int type,int access,int size,void * valbuf,picl_prophdl_t * prophp)243*0Sstevel@tonic-gate add_regular_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
244*0Sstevel@tonic-gate     int size, void *valbuf, picl_prophdl_t *prophp)
245*0Sstevel@tonic-gate {
246*0Sstevel@tonic-gate 	int			err;
247*0Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
248*0Sstevel@tonic-gate 	picl_prophdl_t		proph;
249*0Sstevel@tonic-gate 
250*0Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
251*0Sstevel@tonic-gate 	    type, access, size, name, NULL, NULL);
252*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
253*0Sstevel@tonic-gate 		return (err);
254*0Sstevel@tonic-gate 
255*0Sstevel@tonic-gate 	err = ptree_create_and_add_prop(nodeh, &propinfo, valbuf, &proph);
256*0Sstevel@tonic-gate 	if (err == PICL_SUCCESS && prophp)
257*0Sstevel@tonic-gate 		*prophp = proph;
258*0Sstevel@tonic-gate 	return (err);
259*0Sstevel@tonic-gate }
260*0Sstevel@tonic-gate 
261*0Sstevel@tonic-gate 
262*0Sstevel@tonic-gate /*
263*0Sstevel@tonic-gate  * Create and add the specified volatile property
264*0Sstevel@tonic-gate  */
265*0Sstevel@tonic-gate static int
add_volatile_prop(picl_nodehdl_t nodeh,char * name,int type,int access,int size,ptree_vol_rdfunc_t * rdfunc,ptree_vol_wrfunc_t * wrfunc,picl_prophdl_t * prophp)266*0Sstevel@tonic-gate add_volatile_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
267*0Sstevel@tonic-gate     int size, ptree_vol_rdfunc_t *rdfunc, ptree_vol_wrfunc_t *wrfunc,
268*0Sstevel@tonic-gate     picl_prophdl_t *prophp)
269*0Sstevel@tonic-gate {
270*0Sstevel@tonic-gate 	int			err;
271*0Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
272*0Sstevel@tonic-gate 	picl_prophdl_t		proph;
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
275*0Sstevel@tonic-gate 	    type, (access|PICL_VOLATILE), size, name, rdfunc, wrfunc);
276*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
277*0Sstevel@tonic-gate 		return (err);
278*0Sstevel@tonic-gate 
279*0Sstevel@tonic-gate 	err = ptree_create_and_add_prop(nodeh, &propinfo, NULL, &proph);
280*0Sstevel@tonic-gate 	if (err == PICL_SUCCESS && prophp)
281*0Sstevel@tonic-gate 		*prophp = proph;
282*0Sstevel@tonic-gate 	return (err);
283*0Sstevel@tonic-gate }
284*0Sstevel@tonic-gate 
285*0Sstevel@tonic-gate /*
286*0Sstevel@tonic-gate  * Add temperature threshold properties
287*0Sstevel@tonic-gate  */
288*0Sstevel@tonic-gate static void
add_sensor_thresh_props(picl_nodehdl_t nodeh,sensor_ctrl_blk_t * threshp)289*0Sstevel@tonic-gate add_sensor_thresh_props(picl_nodehdl_t nodeh, sensor_ctrl_blk_t *threshp)
290*0Sstevel@tonic-gate {
291*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
292*0Sstevel@tonic-gate 
293*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_LOW_POWER_OFF,
294*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
295*0Sstevel@tonic-gate 	    sizeof (threshp->low_power_off),
296*0Sstevel@tonic-gate 	    (void *)&(threshp->low_power_off), &proph);
297*0Sstevel@tonic-gate 
298*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_LOW_SHUTDOWN,
299*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
300*0Sstevel@tonic-gate 	    sizeof (threshp->low_shutdown),
301*0Sstevel@tonic-gate 	    (void *)&(threshp->low_shutdown), &proph);
302*0Sstevel@tonic-gate 
303*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_LOW_WARNING,
304*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
305*0Sstevel@tonic-gate 	    sizeof (threshp->low_warning),
306*0Sstevel@tonic-gate 	    (void *)&(threshp->low_warning), &proph);
307*0Sstevel@tonic-gate 
308*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_HIGH_WARNING,
309*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
310*0Sstevel@tonic-gate 	    sizeof (threshp->high_warning),
311*0Sstevel@tonic-gate 	    (void *)&(threshp->high_warning), &proph);
312*0Sstevel@tonic-gate 
313*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_HIGH_SHUTDOWN,
314*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
315*0Sstevel@tonic-gate 	    sizeof (threshp->high_shutdown),
316*0Sstevel@tonic-gate 	    (void *)&(threshp->high_shutdown), &proph);
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_HIGH_POWER_OFF,
319*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
320*0Sstevel@tonic-gate 	    sizeof (threshp->high_power_off),
321*0Sstevel@tonic-gate 	    (void *)&(threshp->high_power_off), &proph);
322*0Sstevel@tonic-gate }
323*0Sstevel@tonic-gate 
324*0Sstevel@tonic-gate /*
325*0Sstevel@tonic-gate  * Go through the sensor_nodes array and create those nodes
326*0Sstevel@tonic-gate  * and the Temperature property to report the temperature.
327*0Sstevel@tonic-gate  */
328*0Sstevel@tonic-gate static int
add_sensor_nodes_and_props(void)329*0Sstevel@tonic-gate add_sensor_nodes_and_props(void)
330*0Sstevel@tonic-gate {
331*0Sstevel@tonic-gate 	int		err;
332*0Sstevel@tonic-gate 	char		*pname, *nodename, *devfs_path;
333*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
334*0Sstevel@tonic-gate 	sensor_ctrl_blk_t *threshp;
335*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh, cnodeh;
336*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
337*0Sstevel@tonic-gate 	env_sensor_t	*sensorp;
338*0Sstevel@tonic-gate 	int		i;
339*0Sstevel@tonic-gate 
340*0Sstevel@tonic-gate 	for (i = 0; i < NSENSORS; ++i) {
341*0Sstevel@tonic-gate 		snodep = &sensor_nodes[i];
342*0Sstevel@tonic-gate 		/*
343*0Sstevel@tonic-gate 		 * Get the parent nodeh
344*0Sstevel@tonic-gate 		 */
345*0Sstevel@tonic-gate 		err = ptree_get_node_by_path(snodep->parent_path, &nodeh);
346*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
347*0Sstevel@tonic-gate 			continue;
348*0Sstevel@tonic-gate 		sensorp = snodep->sensorp;
349*0Sstevel@tonic-gate 
350*0Sstevel@tonic-gate 		/*
351*0Sstevel@tonic-gate 		 * Create temperature-sensor node
352*0Sstevel@tonic-gate 		 */
353*0Sstevel@tonic-gate 		nodename = snodep->sensor_name;
354*0Sstevel@tonic-gate 		err = ptree_create_and_add_node(nodeh, nodename,
355*0Sstevel@tonic-gate 		    PICL_CLASS_TEMPERATURE_SENSOR, &cnodeh);
356*0Sstevel@tonic-gate 		if (env_debug)
357*0Sstevel@tonic-gate 			envd_log(LOG_INFO,
358*0Sstevel@tonic-gate 			    "Creating PICL sensor node '%s' err:%d\n",
359*0Sstevel@tonic-gate 			    nodename, err);
360*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
361*0Sstevel@tonic-gate 			break;
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 		/* save node handle */
364*0Sstevel@tonic-gate 		snodep->nodeh = cnodeh;
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 		/*
367*0Sstevel@tonic-gate 		 * Add "devfs_path" property in child node
368*0Sstevel@tonic-gate 		 */
369*0Sstevel@tonic-gate 		devfs_path = sensorp->devfs_path;
370*0Sstevel@tonic-gate 		pname = PICL_PROP_DEVFS_PATH;
371*0Sstevel@tonic-gate 		err = add_regular_prop(cnodeh, pname,
372*0Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ,
373*0Sstevel@tonic-gate 		    strlen(devfs_path)+1, (void *)devfs_path, &proph);
374*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
375*0Sstevel@tonic-gate 			break;
376*0Sstevel@tonic-gate 
377*0Sstevel@tonic-gate 		/*
378*0Sstevel@tonic-gate 		 * Now add volatile "temperature" volatile property
379*0Sstevel@tonic-gate 		 * in this "temperature-sensor" class node.
380*0Sstevel@tonic-gate 		 */
381*0Sstevel@tonic-gate 		pname = PICL_PROP_TEMPERATURE;
382*0Sstevel@tonic-gate 		err = add_volatile_prop(cnodeh, pname,
383*0Sstevel@tonic-gate 		    PICL_PTYPE_INT, PICL_READ, sizeof (tempr_t),
384*0Sstevel@tonic-gate 		    get_current_temp, NULL, &proph);
385*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
386*0Sstevel@tonic-gate 			break;
387*0Sstevel@tonic-gate 
388*0Sstevel@tonic-gate 		/* Save prop handle */
389*0Sstevel@tonic-gate 		snodep->proph = proph;
390*0Sstevel@tonic-gate 
391*0Sstevel@tonic-gate 		/*
392*0Sstevel@tonic-gate 		 * Add threshold related properties
393*0Sstevel@tonic-gate 		 */
394*0Sstevel@tonic-gate 		threshp = sensorp->es_ptr;
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 		if (threshp != NULL)
397*0Sstevel@tonic-gate 			add_sensor_thresh_props(cnodeh, threshp);
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	}
400*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
401*0Sstevel@tonic-gate 		delete_sensor_nodes_and_props();
402*0Sstevel@tonic-gate 		if (env_debug)
403*0Sstevel@tonic-gate 			envd_log(LOG_INFO,
404*0Sstevel@tonic-gate 			    "Can't create prop/node for sensor '%s'\n",
405*0Sstevel@tonic-gate 			    nodename);
406*0Sstevel@tonic-gate 		return (err);
407*0Sstevel@tonic-gate 	}
408*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
409*0Sstevel@tonic-gate }
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate /*
412*0Sstevel@tonic-gate  * Delete all sensor nodes and related properties created by the
413*0Sstevel@tonic-gate  * add_sensor_prop() for each sensor node in the PICL tree.
414*0Sstevel@tonic-gate  */
415*0Sstevel@tonic-gate static void
delete_sensor_nodes_and_props(void)416*0Sstevel@tonic-gate delete_sensor_nodes_and_props(void)
417*0Sstevel@tonic-gate {
418*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
419*0Sstevel@tonic-gate 	int		i;
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	/*
422*0Sstevel@tonic-gate 	 * Delete/destroy any property created in the sensed device
423*0Sstevel@tonic-gate 	 * as well as the sensor node and all properties under it.
424*0Sstevel@tonic-gate 	 * Note that deleiing/destroying a node deletes/destroys
425*0Sstevel@tonic-gate 	 * all properties within that node.
426*0Sstevel@tonic-gate 	 */
427*0Sstevel@tonic-gate 
428*0Sstevel@tonic-gate 	for (i = 0; i < NSENSORS; ++i) {
429*0Sstevel@tonic-gate 		snodep = &sensor_nodes[i];
430*0Sstevel@tonic-gate 		if (snodep->nodeh != NULL) {
431*0Sstevel@tonic-gate 			/* delete node and all properties under it */
432*0Sstevel@tonic-gate 			(void) ptree_delete_node(snodep->nodeh);
433*0Sstevel@tonic-gate 			(void) ptree_destroy_node(snodep->nodeh);
434*0Sstevel@tonic-gate 			snodep->nodeh = NULL;
435*0Sstevel@tonic-gate 			snodep->proph = NULL;
436*0Sstevel@tonic-gate 		}
437*0Sstevel@tonic-gate 	}
438*0Sstevel@tonic-gate }
439*0Sstevel@tonic-gate 
440*0Sstevel@tonic-gate 
441*0Sstevel@tonic-gate /*
442*0Sstevel@tonic-gate  * For each entry in fan_nodes[] array, do the following:
443*0Sstevel@tonic-gate  *	- Create specified "fan" class node.
444*0Sstevel@tonic-gate  *	- Create "Speed" volatile propery under "fan" class node.
445*0Sstevel@tonic-gate  *	- Create "SpeedUnit" property under "fan" class node.
446*0Sstevel@tonic-gate  */
447*0Sstevel@tonic-gate static int
add_fan_nodes_and_props()448*0Sstevel@tonic-gate add_fan_nodes_and_props()
449*0Sstevel@tonic-gate {
450*0Sstevel@tonic-gate 	int		err;
451*0Sstevel@tonic-gate 	char		*pname, *nodename, *devfs_path;
452*0Sstevel@tonic-gate 	env_fan_t	*fanp;
453*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
454*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh, cnodeh;
455*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
456*0Sstevel@tonic-gate 	int		i;
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 
459*0Sstevel@tonic-gate 	for (i = 0; i < NFANS; ++i) {
460*0Sstevel@tonic-gate 		/*
461*0Sstevel@tonic-gate 		 * Add various fan nodes and properties
462*0Sstevel@tonic-gate 		 */
463*0Sstevel@tonic-gate 		fnodep = &fan_nodes[i];
464*0Sstevel@tonic-gate 		/*
465*0Sstevel@tonic-gate 		 * get parent nodeh
466*0Sstevel@tonic-gate 		 */
467*0Sstevel@tonic-gate 		err = ptree_get_node_by_path(fnodep->parent_path, &nodeh);
468*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
469*0Sstevel@tonic-gate 			continue;
470*0Sstevel@tonic-gate 
471*0Sstevel@tonic-gate 		/*
472*0Sstevel@tonic-gate 		 * Create "fan" class node and save node handle
473*0Sstevel@tonic-gate 		 */
474*0Sstevel@tonic-gate 		nodename = fnodep->fan_name;
475*0Sstevel@tonic-gate 		err = ptree_create_and_add_node(nodeh, nodename,
476*0Sstevel@tonic-gate 		    PICL_CLASS_FAN, &cnodeh);
477*0Sstevel@tonic-gate 		if (env_debug)
478*0Sstevel@tonic-gate 			envd_log(LOG_INFO,
479*0Sstevel@tonic-gate 			    "Creating PICL fan node '%s' err:%d\n",
480*0Sstevel@tonic-gate 			    nodename, err);
481*0Sstevel@tonic-gate 
482*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
483*0Sstevel@tonic-gate 			break;
484*0Sstevel@tonic-gate 		fnodep->nodeh = cnodeh;
485*0Sstevel@tonic-gate 
486*0Sstevel@tonic-gate 		/*
487*0Sstevel@tonic-gate 		 * Add "devfs_path" property in child node
488*0Sstevel@tonic-gate 		 */
489*0Sstevel@tonic-gate 		fanp = fnodep->fanp;
490*0Sstevel@tonic-gate 		devfs_path  = fanp->devfs_path;
491*0Sstevel@tonic-gate 		pname = PICL_PROP_DEVFS_PATH;
492*0Sstevel@tonic-gate 		err = add_regular_prop(cnodeh, pname,
493*0Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ,
494*0Sstevel@tonic-gate 		    strlen(devfs_path)+1, (void *)devfs_path, &proph);
495*0Sstevel@tonic-gate 
496*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 			break;
499*0Sstevel@tonic-gate 
500*0Sstevel@tonic-gate 		/*
501*0Sstevel@tonic-gate 		 * Add "Speed" volatile property in this "fan"
502*0Sstevel@tonic-gate 		 * class node and save prop handle.
503*0Sstevel@tonic-gate 		 */
504*0Sstevel@tonic-gate 		pname = PICL_PROP_FAN_SPEED;
505*0Sstevel@tonic-gate 		err = add_volatile_prop(cnodeh, pname, PICL_PTYPE_INT,
506*0Sstevel@tonic-gate 		    PICL_READ|PICL_WRITE, sizeof (fanspeed_t),
507*0Sstevel@tonic-gate 		    get_current_speed,
508*0Sstevel@tonic-gate 		    set_current_speed, &proph);
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
511*0Sstevel@tonic-gate 			break;
512*0Sstevel@tonic-gate 		fnodep->proph = proph;
513*0Sstevel@tonic-gate 
514*0Sstevel@tonic-gate 		/*
515*0Sstevel@tonic-gate 		 * Add other "fan" class properties
516*0Sstevel@tonic-gate 		 */
517*0Sstevel@tonic-gate 		pname = PICL_PROP_FAN_SPEED_UNIT;
518*0Sstevel@tonic-gate 		err = add_regular_prop(cnodeh, pname,
519*0Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ,
520*0Sstevel@tonic-gate 		    strlen(fnodep->speed_unit)+1,
521*0Sstevel@tonic-gate 		    (void *)fnodep->speed_unit, &proph);
522*0Sstevel@tonic-gate 
523*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
524*0Sstevel@tonic-gate 			break;
525*0Sstevel@tonic-gate 	}
526*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
527*0Sstevel@tonic-gate 		delete_fan_nodes_and_props();
528*0Sstevel@tonic-gate 		if (env_debug)
529*0Sstevel@tonic-gate 			envd_log(LOG_WARNING,
530*0Sstevel@tonic-gate 			    "Can't create prop/node for fan '%s'\n",
531*0Sstevel@tonic-gate 			    nodename);
532*0Sstevel@tonic-gate 		return (err);
533*0Sstevel@tonic-gate 	}
534*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
535*0Sstevel@tonic-gate }
536*0Sstevel@tonic-gate 
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate /*
539*0Sstevel@tonic-gate  * Delete all fan nodes and related properties created by the
540*0Sstevel@tonic-gate  * add_fan_props() for each fan node in the PICL tree.
541*0Sstevel@tonic-gate  */
542*0Sstevel@tonic-gate static void
delete_fan_nodes_and_props(void)543*0Sstevel@tonic-gate delete_fan_nodes_and_props(void)
544*0Sstevel@tonic-gate {
545*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
546*0Sstevel@tonic-gate 	int		i;
547*0Sstevel@tonic-gate 
548*0Sstevel@tonic-gate 	/*
549*0Sstevel@tonic-gate 	 * Delete/destroy fan node and all properties under it.
550*0Sstevel@tonic-gate 	 * Note that deleiing/destroying a node deletes/destroys
551*0Sstevel@tonic-gate 	 * all properties within that node.
552*0Sstevel@tonic-gate 	 */
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	for (i = 0; i < NFANS; ++i) {
555*0Sstevel@tonic-gate 		fnodep = &fan_nodes[i];
556*0Sstevel@tonic-gate 		if (fnodep->nodeh != NULL) {
557*0Sstevel@tonic-gate 			(void) ptree_delete_node(fnodep->nodeh);
558*0Sstevel@tonic-gate 			(void) ptree_destroy_node(fnodep->nodeh);
559*0Sstevel@tonic-gate 			fnodep->nodeh = NULL;
560*0Sstevel@tonic-gate 		}
561*0Sstevel@tonic-gate 	}
562*0Sstevel@tonic-gate }
563*0Sstevel@tonic-gate 
564*0Sstevel@tonic-gate /*
565*0Sstevel@tonic-gate  * Tuneables publishing functions
566*0Sstevel@tonic-gate  */
567*0Sstevel@tonic-gate static int
copy_persistent_tuneable(env_tuneable_t * tune,char * buf)568*0Sstevel@tonic-gate copy_persistent_tuneable(env_tuneable_t *tune, char *buf)
569*0Sstevel@tonic-gate {
570*0Sstevel@tonic-gate 
571*0Sstevel@tonic-gate 	switch (tune->type) {
572*0Sstevel@tonic-gate 
573*0Sstevel@tonic-gate 	case PICL_PTYPE_INT : {
574*0Sstevel@tonic-gate 		(void) memcpy((int *)tune->value,
575*0Sstevel@tonic-gate 		    buf, tune->nbytes);
576*0Sstevel@tonic-gate 		break;
577*0Sstevel@tonic-gate 	}
578*0Sstevel@tonic-gate 	case PICL_PTYPE_CHARSTRING : {
579*0Sstevel@tonic-gate 		(void) memcpy((caddr_t)tune->value,
580*0Sstevel@tonic-gate 		    buf, tune->nbytes);
581*0Sstevel@tonic-gate 		break;
582*0Sstevel@tonic-gate 	}
583*0Sstevel@tonic-gate 	default	: {
584*0Sstevel@tonic-gate 		return (PICL_FAILURE);
585*0Sstevel@tonic-gate 	}
586*0Sstevel@tonic-gate 	}
587*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
588*0Sstevel@tonic-gate }
589*0Sstevel@tonic-gate 
590*0Sstevel@tonic-gate static void
env_parse_tunables(picl_nodehdl_t rooth)591*0Sstevel@tonic-gate env_parse_tunables(picl_nodehdl_t rooth)
592*0Sstevel@tonic-gate {
593*0Sstevel@tonic-gate 	char	nmbuf[SYS_NMLN];
594*0Sstevel@tonic-gate 	char    pname[PATH_MAX];
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
597*0Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
598*0Sstevel@tonic-gate 		(void) strlcat(pname, TUNABLE_CONF_FILE, PATH_MAX);
599*0Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
600*0Sstevel@tonic-gate 			(void) picld_pluginutil_parse_config_file(rooth, pname);
601*0Sstevel@tonic-gate 			return;
602*0Sstevel@tonic-gate 		}
603*0Sstevel@tonic-gate 	}
604*0Sstevel@tonic-gate }
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate int
env_picl_setup_tuneables(void)607*0Sstevel@tonic-gate env_picl_setup_tuneables(void)
608*0Sstevel@tonic-gate {
609*0Sstevel@tonic-gate 	int		err;
610*0Sstevel@tonic-gate 	int		i;
611*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;
612*0Sstevel@tonic-gate 	picl_nodehdl_t	rooth;
613*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
614*0Sstevel@tonic-gate 	env_tuneable_t	*tuneablep;
615*0Sstevel@tonic-gate 	char		read_buf[BUFSIZ];
616*0Sstevel@tonic-gate 
617*0Sstevel@tonic-gate 	if (ptree_get_root(&rooth) != PICL_SUCCESS) {
618*0Sstevel@tonic-gate 		return (PICL_FAILURE);
619*0Sstevel@tonic-gate 	}
620*0Sstevel@tonic-gate 	err = ptree_create_and_add_node(rooth, PICL_PLUGINS_NODE,
621*0Sstevel@tonic-gate 	    PICL_CLASS_PICL, &nodeh);
622*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
623*0Sstevel@tonic-gate 		return (PICL_FAILURE);
624*0Sstevel@tonic-gate 	err = ptree_create_and_add_node(nodeh, PICL_ENVIRONMENTAL_NODE,
625*0Sstevel@tonic-gate 	    PICL_CLASS_PICL, &nodeh);
626*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
627*0Sstevel@tonic-gate 		return (PICL_FAILURE);
628*0Sstevel@tonic-gate 	}
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 	/*
631*0Sstevel@tonic-gate 	 * Parse the conf file
632*0Sstevel@tonic-gate 	 */
633*0Sstevel@tonic-gate 	env_parse_tunables(rooth);
634*0Sstevel@tonic-gate 	for (i = 0; i < ntuneables; i++) {
635*0Sstevel@tonic-gate 		tuneablep = &tuneables[i];
636*0Sstevel@tonic-gate 		err = ptree_get_propval_by_name(nodeh, tuneablep->name,
637*0Sstevel@tonic-gate 		    read_buf, tuneablep->nbytes);
638*0Sstevel@tonic-gate 
639*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
640*0Sstevel@tonic-gate 			/*
641*0Sstevel@tonic-gate 			 * Add volitle functions to environmental node
642*0Sstevel@tonic-gate 			 */
643*0Sstevel@tonic-gate 			err = add_volatile_prop(nodeh, tuneablep->name,
644*0Sstevel@tonic-gate 			    tuneablep->type,
645*0Sstevel@tonic-gate 			    PICL_READ|PICL_WRITE, tuneablep->nbytes,
646*0Sstevel@tonic-gate 			    tuneablep->rfunc,
647*0Sstevel@tonic-gate 			    tuneablep->wfunc, &proph);
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 			tuneablep->proph = proph;
650*0Sstevel@tonic-gate 		} else {
651*0Sstevel@tonic-gate 			/*
652*0Sstevel@tonic-gate 			 * property is persistent
653*0Sstevel@tonic-gate 			 */
654*0Sstevel@tonic-gate 			(void) copy_persistent_tuneable(tuneablep,
655*0Sstevel@tonic-gate 			    read_buf);
656*0Sstevel@tonic-gate 		}
657*0Sstevel@tonic-gate 	}
658*0Sstevel@tonic-gate 
659*0Sstevel@tonic-gate 	return	(PICL_SUCCESS);
660*0Sstevel@tonic-gate }
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate /*
663*0Sstevel@tonic-gate  * Find the ENVMODEL_CONF_FILE file.
664*0Sstevel@tonic-gate  */
665*0Sstevel@tonic-gate static int
get_envmodel_conf_file(char * outfilename)666*0Sstevel@tonic-gate get_envmodel_conf_file(char *outfilename)
667*0Sstevel@tonic-gate {
668*0Sstevel@tonic-gate 	char	nmbuf[SYS_NMLN];
669*0Sstevel@tonic-gate 	char    pname[PATH_MAX];
670*0Sstevel@tonic-gate 
671*0Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
672*0Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
673*0Sstevel@tonic-gate 		(void) strlcat(pname, ENV_CONF_FILE, PATH_MAX);
674*0Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
675*0Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
676*0Sstevel@tonic-gate 			return (0);
677*0Sstevel@tonic-gate 		}
678*0Sstevel@tonic-gate 	}
679*0Sstevel@tonic-gate 
680*0Sstevel@tonic-gate 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
681*0Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
682*0Sstevel@tonic-gate 		(void) strlcat(pname, ENV_CONF_FILE, PATH_MAX);
683*0Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
684*0Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
685*0Sstevel@tonic-gate 			return (0);
686*0Sstevel@tonic-gate 		}
687*0Sstevel@tonic-gate 	}
688*0Sstevel@tonic-gate 
689*0Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
690*0Sstevel@tonic-gate 	    ENV_CONF_FILE);
691*0Sstevel@tonic-gate 
692*0Sstevel@tonic-gate 	if (access(pname, R_OK) == 0) {
693*0Sstevel@tonic-gate 		(void) strlcpy(outfilename, pname, PATH_MAX);
694*0Sstevel@tonic-gate 		return (0);
695*0Sstevel@tonic-gate 	}
696*0Sstevel@tonic-gate 
697*0Sstevel@tonic-gate 	return (-1);
698*0Sstevel@tonic-gate }
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate /* Delete all sensor/fan nodes and any properties created by this plugin */
701*0Sstevel@tonic-gate void
env_picl_destroy(void)702*0Sstevel@tonic-gate env_picl_destroy(void)
703*0Sstevel@tonic-gate {
704*0Sstevel@tonic-gate 	delete_fan_nodes_and_props();
705*0Sstevel@tonic-gate 	delete_sensor_nodes_and_props();
706*0Sstevel@tonic-gate }
707*0Sstevel@tonic-gate 
708*0Sstevel@tonic-gate void
env_picl_setup(void)709*0Sstevel@tonic-gate env_picl_setup(void)
710*0Sstevel@tonic-gate {
711*0Sstevel@tonic-gate 	int		err;
712*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
713*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
714*0Sstevel@tonic-gate 	char		fullfilename[PATH_MAX];
715*0Sstevel@tonic-gate 	picl_nodehdl_t  rooth;
716*0Sstevel@tonic-gate 	int		i;
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate 	/*
720*0Sstevel@tonic-gate 	 * Initialize sensorp and other fields in the sensor_nodes[] array
721*0Sstevel@tonic-gate 	 */
722*0Sstevel@tonic-gate 
723*0Sstevel@tonic-gate 	for (i = 0; i < NSENSORS; ++i) {
724*0Sstevel@tonic-gate 		snodep = &sensor_nodes[i];
725*0Sstevel@tonic-gate 		snodep->sensorp = sensor_lookup(snodep->sensor_name);
726*0Sstevel@tonic-gate 		snodep->nodeh = NULL;
727*0Sstevel@tonic-gate 		snodep->proph = NULL;
728*0Sstevel@tonic-gate 		snodep->target_proph = NULL;
729*0Sstevel@tonic-gate 	}
730*0Sstevel@tonic-gate 
731*0Sstevel@tonic-gate 	/*
732*0Sstevel@tonic-gate 	 * Initialize fanp and other fields in the fan_nodes[] array
733*0Sstevel@tonic-gate 	 */
734*0Sstevel@tonic-gate 	for (i = 0; i < NFANS; ++i) {
735*0Sstevel@tonic-gate 		fnodep = &fan_nodes[i];
736*0Sstevel@tonic-gate 		fnodep->fanp = fan_lookup(fnodep->fan_name);
737*0Sstevel@tonic-gate 		fnodep->nodeh = NULL;
738*0Sstevel@tonic-gate 		fnodep->proph = NULL;
739*0Sstevel@tonic-gate 	}
740*0Sstevel@tonic-gate 
741*0Sstevel@tonic-gate 	/*
742*0Sstevel@tonic-gate 	 * Get platform handle and populate PICL tree with environmental
743*0Sstevel@tonic-gate 	 * nodes and properties
744*0Sstevel@tonic-gate 	 */
745*0Sstevel@tonic-gate 
746*0Sstevel@tonic-gate 	err = add_sensor_nodes_and_props();
747*0Sstevel@tonic-gate 
748*0Sstevel@tonic-gate 	if (err == PICL_SUCCESS)
749*0Sstevel@tonic-gate 		err = add_fan_nodes_and_props();
750*0Sstevel@tonic-gate 
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
753*0Sstevel@tonic-gate 		delete_sensor_nodes_and_props();
754*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
755*0Sstevel@tonic-gate 		return;
756*0Sstevel@tonic-gate 	}
757*0Sstevel@tonic-gate 
758*0Sstevel@tonic-gate 	/*
759*0Sstevel@tonic-gate 	 * Parse the envmodel.conf file and populate the PICL tree
760*0Sstevel@tonic-gate 	 */
761*0Sstevel@tonic-gate 	if (get_envmodel_conf_file(fullfilename) < 0)
762*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
763*0Sstevel@tonic-gate 	if (ptree_get_root(&rooth) != PICL_SUCCESS)
764*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
765*0Sstevel@tonic-gate 	err = picld_pluginutil_parse_config_file(rooth, fullfilename);
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
768*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
769*0Sstevel@tonic-gate }
770