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 <limits.h>
41*0Sstevel@tonic-gate #include <errno.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 extern int monitor_disk_temp;
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 errno;
68*0Sstevel@tonic-gate extern	int	ntuneables;
69*0Sstevel@tonic-gate #define	PROP_FAN_SPEED_UNIT_VALUE	"rpm"
70*0Sstevel@tonic-gate 
71*0Sstevel@tonic-gate 
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate /*
74*0Sstevel@tonic-gate  * Sensor node data structure
75*0Sstevel@tonic-gate  */
76*0Sstevel@tonic-gate typedef struct {
77*0Sstevel@tonic-gate 	char		*parent_path;	/* parent path */
78*0Sstevel@tonic-gate 	char		*sensor_name;	/* sensor name */
79*0Sstevel@tonic-gate 	env_sensor_t	*sensorp;	/* sensor info */
80*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;		/* sensor node handle */
81*0Sstevel@tonic-gate 	picl_prophdl_t	proph;		/* "Temperature" property handle */
82*0Sstevel@tonic-gate 	picl_prophdl_t	target_proph;	/* "TargetTemp" property handle */
83*0Sstevel@tonic-gate } sensor_node_t;
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /*
87*0Sstevel@tonic-gate  * Sensor nodes array
88*0Sstevel@tonic-gate  */
89*0Sstevel@tonic-gate static sensor_node_t sensor_nodes[] = {
90*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,58",
91*0Sstevel@tonic-gate 	SENSOR_CPU0_DIE, NULL, NULL, NULL, NULL},
92*0Sstevel@tonic-gate 
93*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,58",
94*0Sstevel@tonic-gate 	SENSOR_CPU1_DIE, NULL, NULL, NULL, NULL},
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,58",
97*0Sstevel@tonic-gate 	SENSOR_INT_AMB_0, NULL, NULL, NULL, NULL},
98*0Sstevel@tonic-gate 
99*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
100*0Sstevel@tonic-gate 	SENSOR_SYS_OUT, NULL, NULL, NULL, NULL},
101*0Sstevel@tonic-gate 
102*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
103*0Sstevel@tonic-gate 	SENSOR_SYS_IN, NULL, NULL, NULL, NULL},
104*0Sstevel@tonic-gate 
105*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
106*0Sstevel@tonic-gate 	SENSOR_INT_AMB_1, NULL, NULL, NULL, NULL},
107*0Sstevel@tonic-gate 
108*0Sstevel@tonic-gate };
109*0Sstevel@tonic-gate #define	N_SENSOR_NODES	(sizeof (sensor_nodes)/sizeof (sensor_nodes[0]))
110*0Sstevel@tonic-gate 
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate /*
113*0Sstevel@tonic-gate  * Fan node data structure
114*0Sstevel@tonic-gate  */
115*0Sstevel@tonic-gate typedef struct {
116*0Sstevel@tonic-gate 	char		*parent_path;	/* parent node path */
117*0Sstevel@tonic-gate 	char		*fan_name;	/* fan name */
118*0Sstevel@tonic-gate 	env_fan_t 	*fanp;		/* fan information */
119*0Sstevel@tonic-gate 	char		*speed_unit;	/* speed unit string */
120*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;		/* "fan" node handle */
121*0Sstevel@tonic-gate 	picl_prophdl_t	proph;		/* "Speed" property handle */
122*0Sstevel@tonic-gate } fan_node_t;
123*0Sstevel@tonic-gate 
124*0Sstevel@tonic-gate 
125*0Sstevel@tonic-gate /*
126*0Sstevel@tonic-gate  * Fan node array
127*0Sstevel@tonic-gate  */
128*0Sstevel@tonic-gate static fan_node_t fan_nodes[] =  {
129*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,58",
130*0Sstevel@tonic-gate 	ENV_CPU0_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
131*0Sstevel@tonic-gate 
132*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,58",
133*0Sstevel@tonic-gate 	ENV_CPU1_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
134*0Sstevel@tonic-gate 
135*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
136*0Sstevel@tonic-gate 	ENV_SYSTEM_OUT_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
139*0Sstevel@tonic-gate 	ENV_SYSTEM_INTAKE_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
140*0Sstevel@tonic-gate 
141*0Sstevel@tonic-gate 	{"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,52",
142*0Sstevel@tonic-gate 	ENV_DIMM_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
143*0Sstevel@tonic-gate 
144*0Sstevel@tonic-gate };
145*0Sstevel@tonic-gate #define	N_FAN_NODES	(sizeof (fan_nodes)/sizeof (fan_nodes[0]))
146*0Sstevel@tonic-gate 
147*0Sstevel@tonic-gate /*
148*0Sstevel@tonic-gate  * Disk node data structure
149*0Sstevel@tonic-gate  */
150*0Sstevel@tonic-gate typedef struct {
151*0Sstevel@tonic-gate 	char		*parent_path;	/* parent node path */
152*0Sstevel@tonic-gate 	char		*disk_name;	/* disk name */
153*0Sstevel@tonic-gate 	env_disk_t 	*diskp;		/* disk information */
154*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;		/* "disk" node handle */
155*0Sstevel@tonic-gate 	picl_prophdl_t	proph;		/* "Temperature" property handle */
156*0Sstevel@tonic-gate } disk_node_t;
157*0Sstevel@tonic-gate 
158*0Sstevel@tonic-gate /*
159*0Sstevel@tonic-gate  * Disk node array
160*0Sstevel@tonic-gate  */
161*0Sstevel@tonic-gate static disk_node_t disk_nodes[] =  {
162*0Sstevel@tonic-gate 	{DISK0_NODE_PATH, ENV_DISK0, NULL, NULL, NULL},
163*0Sstevel@tonic-gate 	{DISK1_NODE_PATH, ENV_DISK1, NULL, NULL, NULL},
164*0Sstevel@tonic-gate };
165*0Sstevel@tonic-gate #define	N_DISK_NODES	(sizeof (disk_nodes)/sizeof (disk_nodes[0]))
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate /*
168*0Sstevel@tonic-gate  * Miscellaneous declarations
169*0Sstevel@tonic-gate  */
170*0Sstevel@tonic-gate static void delete_sensor_nodes_and_props(void);
171*0Sstevel@tonic-gate static void delete_disk_nodes_and_props(void);
172*0Sstevel@tonic-gate static void delete_fan_nodes_and_props(void);
173*0Sstevel@tonic-gate 
174*0Sstevel@tonic-gate 
175*0Sstevel@tonic-gate /*
176*0Sstevel@tonic-gate  * Read function for volatile "Temperature" property
177*0Sstevel@tonic-gate  */
178*0Sstevel@tonic-gate static int
get_current_temp(ptree_rarg_t * parg,void * buf)179*0Sstevel@tonic-gate get_current_temp(ptree_rarg_t *parg, void *buf)
180*0Sstevel@tonic-gate {
181*0Sstevel@tonic-gate 	tempr_t 	temp;
182*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
183*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
184*0Sstevel@tonic-gate 	int		i;
185*0Sstevel@tonic-gate 
186*0Sstevel@tonic-gate 	/*
187*0Sstevel@tonic-gate 	 * Locate the sensor in our sensor_nodes table by matching the
188*0Sstevel@tonic-gate 	 * property handle and get its temperature.
189*0Sstevel@tonic-gate 	 */
190*0Sstevel@tonic-gate 	proph = parg->proph;
191*0Sstevel@tonic-gate 	for (i = 0; i < N_SENSOR_NODES; ++i) {
192*0Sstevel@tonic-gate 		snodep = &sensor_nodes[i];
193*0Sstevel@tonic-gate 		if (snodep->proph != proph)
194*0Sstevel@tonic-gate 			continue;
195*0Sstevel@tonic-gate 
196*0Sstevel@tonic-gate 		if (get_temperature(snodep->sensorp, &temp) < 0)
197*0Sstevel@tonic-gate 			break;
198*0Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&temp, sizeof (tempr_t));
199*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
200*0Sstevel@tonic-gate 	}
201*0Sstevel@tonic-gate 	return (PICL_FAILURE);
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate /*
205*0Sstevel@tonic-gate  * Read function for volatile "Temperature" property
206*0Sstevel@tonic-gate  */
207*0Sstevel@tonic-gate static int
get_disk_temp(ptree_rarg_t * parg,void * buf)208*0Sstevel@tonic-gate get_disk_temp(ptree_rarg_t *parg, void *buf)
209*0Sstevel@tonic-gate {
210*0Sstevel@tonic-gate 	tempr_t 	temp;
211*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
212*0Sstevel@tonic-gate 	disk_node_t	*dnodep;
213*0Sstevel@tonic-gate 	int		i;
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate 	/*
216*0Sstevel@tonic-gate 	 * Locate the sensor in our sensor_nodes table by matching the
217*0Sstevel@tonic-gate 	 * property handle and get its temperature.
218*0Sstevel@tonic-gate 	 */
219*0Sstevel@tonic-gate 	proph = parg->proph;
220*0Sstevel@tonic-gate 	for (i = 0; i < N_DISK_NODES; ++i) {
221*0Sstevel@tonic-gate 		dnodep = &disk_nodes[i];
222*0Sstevel@tonic-gate 		if (dnodep->proph != proph)
223*0Sstevel@tonic-gate 			continue;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 		if (disk_temperature(dnodep->diskp, &temp) < 0)
226*0Sstevel@tonic-gate 			break;
227*0Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&temp, sizeof (tempr_t));
228*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
229*0Sstevel@tonic-gate 	}
230*0Sstevel@tonic-gate 	return (PICL_FAILURE);
231*0Sstevel@tonic-gate }
232*0Sstevel@tonic-gate 
233*0Sstevel@tonic-gate /*
234*0Sstevel@tonic-gate  * Read function for volatile "Speed" property on "fan" class node
235*0Sstevel@tonic-gate  */
236*0Sstevel@tonic-gate static int
set_current_speed(ptree_warg_t * parg,const void * buf)237*0Sstevel@tonic-gate set_current_speed(ptree_warg_t *parg, const void *buf)
238*0Sstevel@tonic-gate {
239*0Sstevel@tonic-gate 	fanspeed_t	speed;
240*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
241*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
242*0Sstevel@tonic-gate 	int		i, ret;
243*0Sstevel@tonic-gate 
244*0Sstevel@tonic-gate 	/*
245*0Sstevel@tonic-gate 	 * Locate the fan in our fan_nodes table by matching the
246*0Sstevel@tonic-gate 	 * property handle and get fan speed.
247*0Sstevel@tonic-gate 	 */
248*0Sstevel@tonic-gate 	proph = parg->proph;
249*0Sstevel@tonic-gate 	for (i = 0; i < N_FAN_NODES; ++i) {
250*0Sstevel@tonic-gate 		fnodep = &fan_nodes[i];
251*0Sstevel@tonic-gate 		if (fnodep->proph != proph)
252*0Sstevel@tonic-gate 			continue;
253*0Sstevel@tonic-gate 		if (fnodep->fanp->fd == -1)
254*0Sstevel@tonic-gate 			continue;
255*0Sstevel@tonic-gate 
256*0Sstevel@tonic-gate 		(void) memcpy((caddr_t)&speed, buf, sizeof (speed));
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 		ret = set_fan_speed(fnodep->fanp, speed);
259*0Sstevel@tonic-gate 
260*0Sstevel@tonic-gate 		if (ret < 0) {
261*0Sstevel@tonic-gate 			if (ret == -1 && errno == EBUSY)
262*0Sstevel@tonic-gate 				return (PICL_NOTWRITABLE);
263*0Sstevel@tonic-gate 			if (ret == -2)
264*0Sstevel@tonic-gate 				return (PICL_INVALIDARG);
265*0Sstevel@tonic-gate 			break;
266*0Sstevel@tonic-gate 		}
267*0Sstevel@tonic-gate 
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
270*0Sstevel@tonic-gate 	}
271*0Sstevel@tonic-gate 	return (PICL_FAILURE);
272*0Sstevel@tonic-gate }
273*0Sstevel@tonic-gate 
274*0Sstevel@tonic-gate 
275*0Sstevel@tonic-gate /*
276*0Sstevel@tonic-gate  * Read function for volatile "Speed" property on "fan" class node
277*0Sstevel@tonic-gate  */
278*0Sstevel@tonic-gate static int
get_current_speed(ptree_rarg_t * parg,void * buf)279*0Sstevel@tonic-gate get_current_speed(ptree_rarg_t *parg, void *buf)
280*0Sstevel@tonic-gate {
281*0Sstevel@tonic-gate 	fanspeed_t	speed;
282*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
283*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
284*0Sstevel@tonic-gate 	int		i;
285*0Sstevel@tonic-gate 
286*0Sstevel@tonic-gate 	/*
287*0Sstevel@tonic-gate 	 * Locate the fan in our fan_nodes table by matching the
288*0Sstevel@tonic-gate 	 * property handle and get fan speed.
289*0Sstevel@tonic-gate 	 */
290*0Sstevel@tonic-gate 	proph = parg->proph;
291*0Sstevel@tonic-gate 	for (i = 0; i < N_FAN_NODES; ++i) {
292*0Sstevel@tonic-gate 		fnodep = &fan_nodes[i];
293*0Sstevel@tonic-gate 		if (fnodep->proph != proph)
294*0Sstevel@tonic-gate 			continue;
295*0Sstevel@tonic-gate 		if (fnodep->fanp->fd == -1)
296*0Sstevel@tonic-gate 			continue;
297*0Sstevel@tonic-gate 		if (get_fan_speed(fnodep->fanp, &speed) < 0)
298*0Sstevel@tonic-gate 			break;
299*0Sstevel@tonic-gate 
300*0Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&speed, sizeof (speed));
301*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
302*0Sstevel@tonic-gate 	}
303*0Sstevel@tonic-gate 	return (PICL_FAILURE);
304*0Sstevel@tonic-gate }
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate /*
307*0Sstevel@tonic-gate  * Create and add the specified regular property
308*0Sstevel@tonic-gate  */
309*0Sstevel@tonic-gate 
310*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)311*0Sstevel@tonic-gate add_regular_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
312*0Sstevel@tonic-gate     int size, void *valbuf, picl_prophdl_t *prophp)
313*0Sstevel@tonic-gate {
314*0Sstevel@tonic-gate 	int			err;
315*0Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
316*0Sstevel@tonic-gate 	picl_prophdl_t		proph;
317*0Sstevel@tonic-gate 
318*0Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
319*0Sstevel@tonic-gate 	    type, access, size, name, NULL, NULL);
320*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
321*0Sstevel@tonic-gate 		return (err);
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	err = ptree_create_and_add_prop(nodeh, &propinfo, valbuf, &proph);
324*0Sstevel@tonic-gate 	if (err == PICL_SUCCESS && prophp)
325*0Sstevel@tonic-gate 		*prophp = proph;
326*0Sstevel@tonic-gate 	return (err);
327*0Sstevel@tonic-gate }
328*0Sstevel@tonic-gate 
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate /*
331*0Sstevel@tonic-gate  * Create and add the specified volatile property
332*0Sstevel@tonic-gate  */
333*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)334*0Sstevel@tonic-gate add_volatile_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
335*0Sstevel@tonic-gate     int size, ptree_vol_rdfunc_t *rdfunc, ptree_vol_wrfunc_t *wrfunc,
336*0Sstevel@tonic-gate     picl_prophdl_t *prophp)
337*0Sstevel@tonic-gate {
338*0Sstevel@tonic-gate 	int			err;
339*0Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
340*0Sstevel@tonic-gate 	picl_prophdl_t		proph;
341*0Sstevel@tonic-gate 
342*0Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
343*0Sstevel@tonic-gate 	    type, (access|PICL_VOLATILE), size, name, rdfunc, wrfunc);
344*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
345*0Sstevel@tonic-gate 		return (err);
346*0Sstevel@tonic-gate 
347*0Sstevel@tonic-gate 	err = ptree_create_and_add_prop(nodeh, &propinfo, NULL, &proph);
348*0Sstevel@tonic-gate 	if (err == PICL_SUCCESS && prophp)
349*0Sstevel@tonic-gate 		*prophp = proph;
350*0Sstevel@tonic-gate 	return (err);
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate 
353*0Sstevel@tonic-gate /*
354*0Sstevel@tonic-gate  * Add temperature threshold properties
355*0Sstevel@tonic-gate  */
356*0Sstevel@tonic-gate static void
add_sensor_thresh_props(picl_nodehdl_t nodeh,sensor_ctrl_blk_t * threshp)357*0Sstevel@tonic-gate add_sensor_thresh_props(picl_nodehdl_t nodeh, sensor_ctrl_blk_t *threshp)
358*0Sstevel@tonic-gate {
359*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
360*0Sstevel@tonic-gate 
361*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_LOW_POWER_OFF,
362*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
363*0Sstevel@tonic-gate 	    sizeof (threshp->low_power_off),
364*0Sstevel@tonic-gate 	    (void *)&(threshp->low_power_off), &proph);
365*0Sstevel@tonic-gate 
366*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_LOW_SHUTDOWN,
367*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
368*0Sstevel@tonic-gate 	    sizeof (threshp->low_shutdown),
369*0Sstevel@tonic-gate 	    (void *)&(threshp->low_shutdown), &proph);
370*0Sstevel@tonic-gate 
371*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_LOW_WARNING,
372*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
373*0Sstevel@tonic-gate 	    sizeof (threshp->low_warning),
374*0Sstevel@tonic-gate 	    (void *)&(threshp->low_warning), &proph);
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_HIGH_WARNING,
377*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
378*0Sstevel@tonic-gate 	    sizeof (threshp->high_warning),
379*0Sstevel@tonic-gate 	    (void *)&(threshp->high_warning), &proph);
380*0Sstevel@tonic-gate 
381*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_HIGH_SHUTDOWN,
382*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
383*0Sstevel@tonic-gate 	    sizeof (threshp->high_shutdown),
384*0Sstevel@tonic-gate 	    (void *)&(threshp->high_shutdown), &proph);
385*0Sstevel@tonic-gate 
386*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_HIGH_POWER_OFF,
387*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
388*0Sstevel@tonic-gate 	    sizeof (threshp->high_power_off),
389*0Sstevel@tonic-gate 	    (void *)&(threshp->high_power_off), &proph);
390*0Sstevel@tonic-gate }
391*0Sstevel@tonic-gate 
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate /*
394*0Sstevel@tonic-gate  * Go through the sensor_nodes array and create those nodes
395*0Sstevel@tonic-gate  * and the Temperature property to report the temperature.
396*0Sstevel@tonic-gate  */
397*0Sstevel@tonic-gate static int
add_sensor_nodes_and_props()398*0Sstevel@tonic-gate add_sensor_nodes_and_props()
399*0Sstevel@tonic-gate {
400*0Sstevel@tonic-gate 	int		err;
401*0Sstevel@tonic-gate 	char		*pname, *nodename, *devfs_path;
402*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
403*0Sstevel@tonic-gate 	sensor_ctrl_blk_t *threshp;
404*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh, cnodeh;
405*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
406*0Sstevel@tonic-gate 	env_sensor_t	*sensorp;
407*0Sstevel@tonic-gate 	int		i;
408*0Sstevel@tonic-gate 
409*0Sstevel@tonic-gate 	for (i = 0; i < N_SENSOR_NODES; ++i) {
410*0Sstevel@tonic-gate 		snodep = &sensor_nodes[i];
411*0Sstevel@tonic-gate 		/*
412*0Sstevel@tonic-gate 		 * Get the parent nodeh
413*0Sstevel@tonic-gate 		 */
414*0Sstevel@tonic-gate 		err = ptree_get_node_by_path(snodep->parent_path, &nodeh);
415*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
416*0Sstevel@tonic-gate 			continue;
417*0Sstevel@tonic-gate 		sensorp = snodep->sensorp;
418*0Sstevel@tonic-gate 		if (sensorp->present == B_FALSE)
419*0Sstevel@tonic-gate 			continue;
420*0Sstevel@tonic-gate 		/*
421*0Sstevel@tonic-gate 		 * Create temperature-sensor node
422*0Sstevel@tonic-gate 		 */
423*0Sstevel@tonic-gate 		nodename = snodep->sensor_name;
424*0Sstevel@tonic-gate 		err = ptree_create_and_add_node(nodeh, nodename,
425*0Sstevel@tonic-gate 		    PICL_CLASS_TEMPERATURE_SENSOR, &cnodeh);
426*0Sstevel@tonic-gate 		if (env_debug)
427*0Sstevel@tonic-gate 			envd_log(LOG_INFO,
428*0Sstevel@tonic-gate 			    "Creating PICL sensor node '%s' err:%d\n",
429*0Sstevel@tonic-gate 			    nodename, err);
430*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
431*0Sstevel@tonic-gate 			break;
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate 		/* save node handle */
434*0Sstevel@tonic-gate 		snodep->nodeh = cnodeh;
435*0Sstevel@tonic-gate 
436*0Sstevel@tonic-gate 		/*
437*0Sstevel@tonic-gate 		 * Add "devfs_path" property in child node
438*0Sstevel@tonic-gate 		 */
439*0Sstevel@tonic-gate 		devfs_path = sensorp->devfs_path;
440*0Sstevel@tonic-gate 		pname = PICL_PROP_DEVFS_PATH;
441*0Sstevel@tonic-gate 		err = add_regular_prop(cnodeh, pname,
442*0Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ,
443*0Sstevel@tonic-gate 		    strlen(devfs_path)+1, (void *)devfs_path, &proph);
444*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
445*0Sstevel@tonic-gate 			break;
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate 		/*
448*0Sstevel@tonic-gate 		 * Now add volatile "temperature" volatile property
449*0Sstevel@tonic-gate 		 * in this "temperature-sensor" class node.
450*0Sstevel@tonic-gate 		 */
451*0Sstevel@tonic-gate 		pname = PICL_PROP_TEMPERATURE;
452*0Sstevel@tonic-gate 		err = add_volatile_prop(cnodeh, pname,
453*0Sstevel@tonic-gate 		    PICL_PTYPE_INT, PICL_READ, sizeof (tempr_t),
454*0Sstevel@tonic-gate 		    get_current_temp, NULL, &proph);
455*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
456*0Sstevel@tonic-gate 			break;
457*0Sstevel@tonic-gate 
458*0Sstevel@tonic-gate 		/* Save prop handle */
459*0Sstevel@tonic-gate 		snodep->proph = proph;
460*0Sstevel@tonic-gate 
461*0Sstevel@tonic-gate 		/*
462*0Sstevel@tonic-gate 		 * Add threshold related properties
463*0Sstevel@tonic-gate 		 */
464*0Sstevel@tonic-gate 		threshp = sensorp->es_ptr;
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 		if (threshp != NULL)
467*0Sstevel@tonic-gate 			add_sensor_thresh_props(cnodeh, threshp);
468*0Sstevel@tonic-gate 
469*0Sstevel@tonic-gate 	}
470*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
471*0Sstevel@tonic-gate 		delete_sensor_nodes_and_props();
472*0Sstevel@tonic-gate 		if (env_debug)
473*0Sstevel@tonic-gate 			envd_log(LOG_INFO,
474*0Sstevel@tonic-gate 			    "Can't create prop/node for sensor '%s'\n",
475*0Sstevel@tonic-gate 			    nodename);
476*0Sstevel@tonic-gate 		return (err);
477*0Sstevel@tonic-gate 	}
478*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
479*0Sstevel@tonic-gate }
480*0Sstevel@tonic-gate 
481*0Sstevel@tonic-gate /*
482*0Sstevel@tonic-gate  * Delete all sensor nodes and related properties created by the
483*0Sstevel@tonic-gate  * add_sensor_prop() for each sensor node in the PICL tree.
484*0Sstevel@tonic-gate  */
485*0Sstevel@tonic-gate static void
delete_sensor_nodes_and_props(void)486*0Sstevel@tonic-gate delete_sensor_nodes_and_props(void)
487*0Sstevel@tonic-gate {
488*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
489*0Sstevel@tonic-gate 	int		i;
490*0Sstevel@tonic-gate 
491*0Sstevel@tonic-gate 	/*
492*0Sstevel@tonic-gate 	 * Delete/destroy any property created in the sensed device
493*0Sstevel@tonic-gate 	 * as well as the sensor node and all properties under it.
494*0Sstevel@tonic-gate 	 * Note that deleiing/destroying a node deletes/destroys
495*0Sstevel@tonic-gate 	 * all properties within that node.
496*0Sstevel@tonic-gate 	 */
497*0Sstevel@tonic-gate 
498*0Sstevel@tonic-gate 	for (i = 0; i < N_SENSOR_NODES; ++i) {
499*0Sstevel@tonic-gate 		snodep = &sensor_nodes[i];
500*0Sstevel@tonic-gate 		if (snodep->nodeh != NULL) {
501*0Sstevel@tonic-gate 			/* delete node and all properties under it */
502*0Sstevel@tonic-gate 			(void) ptree_delete_node(snodep->nodeh);
503*0Sstevel@tonic-gate 			(void) ptree_destroy_node(snodep->nodeh);
504*0Sstevel@tonic-gate 			snodep->nodeh = NULL;
505*0Sstevel@tonic-gate 			snodep->proph = NULL;
506*0Sstevel@tonic-gate 		}
507*0Sstevel@tonic-gate 	}
508*0Sstevel@tonic-gate }
509*0Sstevel@tonic-gate 
510*0Sstevel@tonic-gate /*
511*0Sstevel@tonic-gate  * Go through the disk_nodes array and create those nodes
512*0Sstevel@tonic-gate  * and the Temperature property to report the temperature.
513*0Sstevel@tonic-gate  */
514*0Sstevel@tonic-gate static int
add_disk_nodes_and_props()515*0Sstevel@tonic-gate add_disk_nodes_and_props()
516*0Sstevel@tonic-gate {
517*0Sstevel@tonic-gate 	int		err;
518*0Sstevel@tonic-gate 	char		*pname, *nodename, *devfs_path;
519*0Sstevel@tonic-gate 	disk_node_t	*dnodep;
520*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh, cnodeh;
521*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
522*0Sstevel@tonic-gate 	env_disk_t	*diskp;
523*0Sstevel@tonic-gate 	int		i;
524*0Sstevel@tonic-gate 
525*0Sstevel@tonic-gate 	for (i = 0; i < N_DISK_NODES; ++i) {
526*0Sstevel@tonic-gate 		dnodep = &disk_nodes[i];
527*0Sstevel@tonic-gate 		/*
528*0Sstevel@tonic-gate 		 * Get the parent nodeh
529*0Sstevel@tonic-gate 		 */
530*0Sstevel@tonic-gate 		err = ptree_get_node_by_path(dnodep->parent_path, &nodeh);
531*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
532*0Sstevel@tonic-gate 		    if (env_debug)
533*0Sstevel@tonic-gate 			envd_log(LOG_ERR, "failed to get node for path %s\n",
534*0Sstevel@tonic-gate 				dnodep->parent_path);
535*0Sstevel@tonic-gate 		    err = PICL_SUCCESS;
536*0Sstevel@tonic-gate 		    continue;
537*0Sstevel@tonic-gate 		}
538*0Sstevel@tonic-gate 		diskp = dnodep->diskp;
539*0Sstevel@tonic-gate 		if (diskp->present == B_FALSE)
540*0Sstevel@tonic-gate 			continue;
541*0Sstevel@tonic-gate 		/*
542*0Sstevel@tonic-gate 		 * Create temperature-sensor node
543*0Sstevel@tonic-gate 		 */
544*0Sstevel@tonic-gate 		nodename = dnodep->disk_name;
545*0Sstevel@tonic-gate 		err = ptree_create_and_add_node(nodeh, nodename,
546*0Sstevel@tonic-gate 		    PICL_CLASS_TEMPERATURE_SENSOR, &cnodeh);
547*0Sstevel@tonic-gate 		if (env_debug)
548*0Sstevel@tonic-gate 			envd_log(LOG_ERR,
549*0Sstevel@tonic-gate 			    "Creating PICL disk node '%s' err:%d\n",
550*0Sstevel@tonic-gate 			    nodename, err);
551*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
552*0Sstevel@tonic-gate 			break;
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 		/* save node handle */
555*0Sstevel@tonic-gate 		dnodep->nodeh = cnodeh;
556*0Sstevel@tonic-gate 
557*0Sstevel@tonic-gate 		/*
558*0Sstevel@tonic-gate 		 * Add "devfs_path" property in child node
559*0Sstevel@tonic-gate 		 */
560*0Sstevel@tonic-gate 		devfs_path = diskp->devfs_path;
561*0Sstevel@tonic-gate 		pname = PICL_PROP_DEVFS_PATH;
562*0Sstevel@tonic-gate 		err = add_regular_prop(cnodeh, pname,
563*0Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ,
564*0Sstevel@tonic-gate 		    strlen(devfs_path)+1, (void *)devfs_path, &proph);
565*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
566*0Sstevel@tonic-gate 			break;
567*0Sstevel@tonic-gate 
568*0Sstevel@tonic-gate 		/*
569*0Sstevel@tonic-gate 		 * Now add volatile "temperature" volatile property
570*0Sstevel@tonic-gate 		 * in this "temperature-sensor" class node.
571*0Sstevel@tonic-gate 		 */
572*0Sstevel@tonic-gate 		pname = PICL_PROP_TEMPERATURE;
573*0Sstevel@tonic-gate 		err = add_volatile_prop(cnodeh, pname,
574*0Sstevel@tonic-gate 		    PICL_PTYPE_INT, PICL_READ, sizeof (tempr_t),
575*0Sstevel@tonic-gate 		    get_disk_temp, NULL, &proph);
576*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
577*0Sstevel@tonic-gate 			break;
578*0Sstevel@tonic-gate 
579*0Sstevel@tonic-gate 		/* Save prop handle */
580*0Sstevel@tonic-gate 		dnodep->proph = proph;
581*0Sstevel@tonic-gate 
582*0Sstevel@tonic-gate 		/*
583*0Sstevel@tonic-gate 		 * Add threshold related properties
584*0Sstevel@tonic-gate 		 */
585*0Sstevel@tonic-gate 
586*0Sstevel@tonic-gate 		(void) add_regular_prop(cnodeh, PICL_PROP_LOW_SHUTDOWN,
587*0Sstevel@tonic-gate 			PICL_PTYPE_INT, PICL_READ,
588*0Sstevel@tonic-gate 			sizeof (diskp->low_shutdown),
589*0Sstevel@tonic-gate 			(void *)&(diskp->low_shutdown), &proph);
590*0Sstevel@tonic-gate 
591*0Sstevel@tonic-gate 		(void) add_regular_prop(cnodeh, PICL_PROP_LOW_WARNING,
592*0Sstevel@tonic-gate 			PICL_PTYPE_INT, PICL_READ,
593*0Sstevel@tonic-gate 			sizeof (diskp->low_warning),
594*0Sstevel@tonic-gate 			(void *)&(diskp->low_warning), &proph);
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 		(void) add_regular_prop(cnodeh, PICL_PROP_HIGH_WARNING,
597*0Sstevel@tonic-gate 			PICL_PTYPE_INT, PICL_READ,
598*0Sstevel@tonic-gate 			sizeof (diskp->high_warning),
599*0Sstevel@tonic-gate 			(void *)&(diskp->high_warning), &proph);
600*0Sstevel@tonic-gate 
601*0Sstevel@tonic-gate 		(void) add_regular_prop(cnodeh, PICL_PROP_HIGH_SHUTDOWN,
602*0Sstevel@tonic-gate 			PICL_PTYPE_INT, PICL_READ,
603*0Sstevel@tonic-gate 			sizeof (diskp->high_shutdown),
604*0Sstevel@tonic-gate 			(void *)&(diskp->high_shutdown), &proph);
605*0Sstevel@tonic-gate 
606*0Sstevel@tonic-gate 	}
607*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
608*0Sstevel@tonic-gate 		delete_disk_nodes_and_props();
609*0Sstevel@tonic-gate 		if (env_debug)
610*0Sstevel@tonic-gate 			envd_log(LOG_INFO,
611*0Sstevel@tonic-gate 			    "Can't create prop/node for disk '%s'\n",
612*0Sstevel@tonic-gate 			    nodename);
613*0Sstevel@tonic-gate 		return (err);
614*0Sstevel@tonic-gate 	}
615*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
616*0Sstevel@tonic-gate }
617*0Sstevel@tonic-gate 
618*0Sstevel@tonic-gate /*
619*0Sstevel@tonic-gate  * Delete all disk nodes and related properties created by the
620*0Sstevel@tonic-gate  * add_disk_props() for each disk node in the PICL tree.
621*0Sstevel@tonic-gate  */
622*0Sstevel@tonic-gate static void
delete_disk_nodes_and_props(void)623*0Sstevel@tonic-gate delete_disk_nodes_and_props(void)
624*0Sstevel@tonic-gate {
625*0Sstevel@tonic-gate 	disk_node_t	*dnodep;
626*0Sstevel@tonic-gate 	int		i;
627*0Sstevel@tonic-gate 
628*0Sstevel@tonic-gate 	/*
629*0Sstevel@tonic-gate 	 * Delete/destroy disk node and all properties under it.
630*0Sstevel@tonic-gate 	 * Note that deleting/destroying a node deletes/destroys
631*0Sstevel@tonic-gate 	 * all properties within that node.
632*0Sstevel@tonic-gate 	 */
633*0Sstevel@tonic-gate 
634*0Sstevel@tonic-gate 	for (i = 0; i < N_DISK_NODES; ++i) {
635*0Sstevel@tonic-gate 		dnodep = &disk_nodes[i];
636*0Sstevel@tonic-gate 		if (dnodep->nodeh != NULL) {
637*0Sstevel@tonic-gate 			(void) ptree_delete_node(dnodep->nodeh);
638*0Sstevel@tonic-gate 			(void) ptree_destroy_node(dnodep->nodeh);
639*0Sstevel@tonic-gate 			dnodep->nodeh = NULL;
640*0Sstevel@tonic-gate 			dnodep->proph = NULL;
641*0Sstevel@tonic-gate 		}
642*0Sstevel@tonic-gate 	}
643*0Sstevel@tonic-gate }
644*0Sstevel@tonic-gate 
645*0Sstevel@tonic-gate /*
646*0Sstevel@tonic-gate  * For each entry in fan_nodes[] array, do the following:
647*0Sstevel@tonic-gate  *	- Create specified "fan" class node.
648*0Sstevel@tonic-gate  *	- Create "Speed" volatile propery under "fan" class node.
649*0Sstevel@tonic-gate  *	- Create "SpeedUnit" property under "fan" class node.
650*0Sstevel@tonic-gate  */
651*0Sstevel@tonic-gate static int
add_fan_nodes_and_props()652*0Sstevel@tonic-gate add_fan_nodes_and_props()
653*0Sstevel@tonic-gate {
654*0Sstevel@tonic-gate 	int		err;
655*0Sstevel@tonic-gate 	char		*pname, *nodename, *devfs_path;
656*0Sstevel@tonic-gate 	env_fan_t	*fanp;
657*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
658*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh, cnodeh;
659*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
660*0Sstevel@tonic-gate 	int		i;
661*0Sstevel@tonic-gate 
662*0Sstevel@tonic-gate 	for (i = 0; i < N_FAN_NODES; ++i) {
663*0Sstevel@tonic-gate 		/*
664*0Sstevel@tonic-gate 		 * Add various fan nodes and properties
665*0Sstevel@tonic-gate 		 */
666*0Sstevel@tonic-gate 		fnodep = &fan_nodes[i];
667*0Sstevel@tonic-gate 		if (fnodep->fanp->present == B_FALSE)
668*0Sstevel@tonic-gate 			continue;
669*0Sstevel@tonic-gate 		/*
670*0Sstevel@tonic-gate 		 * get parent nodeh
671*0Sstevel@tonic-gate 		 */
672*0Sstevel@tonic-gate 		err = ptree_get_node_by_path(fnodep->parent_path, &nodeh);
673*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
674*0Sstevel@tonic-gate 			if (env_debug)
675*0Sstevel@tonic-gate 				envd_log(LOG_ERR,
676*0Sstevel@tonic-gate 		"node for %s NOT FOUND.\n", fnodep->parent_path);
677*0Sstevel@tonic-gate 			err = PICL_SUCCESS;
678*0Sstevel@tonic-gate 			continue;
679*0Sstevel@tonic-gate 		}
680*0Sstevel@tonic-gate 		/*
681*0Sstevel@tonic-gate 		 * Create "fan" class node and save node handle
682*0Sstevel@tonic-gate 		 */
683*0Sstevel@tonic-gate 		nodename = fnodep->fan_name;
684*0Sstevel@tonic-gate 		err = ptree_create_and_add_node(nodeh, nodename,
685*0Sstevel@tonic-gate 		    PICL_CLASS_FAN, &cnodeh);
686*0Sstevel@tonic-gate 		if (env_debug)
687*0Sstevel@tonic-gate 			envd_log(LOG_ERR,
688*0Sstevel@tonic-gate 			    "Creating PICL fan node '%s' err:%d\n",
689*0Sstevel@tonic-gate 			    nodename, err);
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
692*0Sstevel@tonic-gate 			break;
693*0Sstevel@tonic-gate 		fnodep->nodeh = cnodeh;
694*0Sstevel@tonic-gate 
695*0Sstevel@tonic-gate 		/*
696*0Sstevel@tonic-gate 		 * Add "devfs_path" property in child node
697*0Sstevel@tonic-gate 		 */
698*0Sstevel@tonic-gate 		fanp = fnodep->fanp;
699*0Sstevel@tonic-gate 		devfs_path  = fanp->devfs_path;
700*0Sstevel@tonic-gate 		pname = PICL_PROP_DEVFS_PATH;
701*0Sstevel@tonic-gate 		err = add_regular_prop(cnodeh, pname,
702*0Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ,
703*0Sstevel@tonic-gate 		    strlen(devfs_path)+1, (void *)devfs_path, &proph);
704*0Sstevel@tonic-gate 
705*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
706*0Sstevel@tonic-gate 
707*0Sstevel@tonic-gate 			break;
708*0Sstevel@tonic-gate 
709*0Sstevel@tonic-gate 		/*
710*0Sstevel@tonic-gate 		 * Add "Speed" volatile property in this "fan"
711*0Sstevel@tonic-gate 		 * class node and save prop handle.
712*0Sstevel@tonic-gate 		 */
713*0Sstevel@tonic-gate 		pname = PICL_PROP_FAN_SPEED;
714*0Sstevel@tonic-gate 		if (fanp->id == DIMM_FAN_ID) {
715*0Sstevel@tonic-gate 			/*
716*0Sstevel@tonic-gate 			 * We do not permit setting of DIMM FAN speeds.
717*0Sstevel@tonic-gate 			 */
718*0Sstevel@tonic-gate 			err = add_volatile_prop(cnodeh, pname, PICL_PTYPE_INT,
719*0Sstevel@tonic-gate 				PICL_READ, sizeof (fanspeed_t),
720*0Sstevel@tonic-gate 				get_current_speed,
721*0Sstevel@tonic-gate 				NULL, &proph);
722*0Sstevel@tonic-gate 		} else {
723*0Sstevel@tonic-gate 			err = add_volatile_prop(cnodeh, pname, PICL_PTYPE_INT,
724*0Sstevel@tonic-gate 				PICL_READ|PICL_WRITE, sizeof (fanspeed_t),
725*0Sstevel@tonic-gate 				get_current_speed,
726*0Sstevel@tonic-gate 				set_current_speed, &proph);
727*0Sstevel@tonic-gate 		}
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
730*0Sstevel@tonic-gate 			break;
731*0Sstevel@tonic-gate 		fnodep->proph = proph;
732*0Sstevel@tonic-gate 
733*0Sstevel@tonic-gate 		/*
734*0Sstevel@tonic-gate 		 * Add other "fan" class properties
735*0Sstevel@tonic-gate 		 */
736*0Sstevel@tonic-gate 		pname = PICL_PROP_FAN_SPEED_UNIT;
737*0Sstevel@tonic-gate 		err = add_regular_prop(cnodeh, pname,
738*0Sstevel@tonic-gate 		    PICL_PTYPE_CHARSTRING, PICL_READ,
739*0Sstevel@tonic-gate 		    strlen(fnodep->speed_unit)+1,
740*0Sstevel@tonic-gate 		    (void *)fnodep->speed_unit, &proph);
741*0Sstevel@tonic-gate 
742*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
743*0Sstevel@tonic-gate 			break;
744*0Sstevel@tonic-gate 	}
745*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
746*0Sstevel@tonic-gate 		delete_fan_nodes_and_props();
747*0Sstevel@tonic-gate 		if (env_debug)
748*0Sstevel@tonic-gate 			envd_log(LOG_WARNING,
749*0Sstevel@tonic-gate 			    "Can't create prop/node for fan '%s'\n",
750*0Sstevel@tonic-gate 			    nodename);
751*0Sstevel@tonic-gate 		return (err);
752*0Sstevel@tonic-gate 	}
753*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
754*0Sstevel@tonic-gate }
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate 
757*0Sstevel@tonic-gate /*
758*0Sstevel@tonic-gate  * Delete all fan nodes and related properties created by the
759*0Sstevel@tonic-gate  * add_fan_props() for each fan node in the PICL tree.
760*0Sstevel@tonic-gate  */
761*0Sstevel@tonic-gate static void
delete_fan_nodes_and_props(void)762*0Sstevel@tonic-gate delete_fan_nodes_and_props(void)
763*0Sstevel@tonic-gate {
764*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
765*0Sstevel@tonic-gate 	int		i;
766*0Sstevel@tonic-gate 
767*0Sstevel@tonic-gate 	/*
768*0Sstevel@tonic-gate 	 * Delete/destroy fan node and all properties under it.
769*0Sstevel@tonic-gate 	 * Note that deleting/destroying a node deletes/destroys
770*0Sstevel@tonic-gate 	 * all properties within that node.
771*0Sstevel@tonic-gate 	 */
772*0Sstevel@tonic-gate 
773*0Sstevel@tonic-gate 	for (i = 0; i < N_FAN_NODES; ++i) {
774*0Sstevel@tonic-gate 		fnodep = &fan_nodes[i];
775*0Sstevel@tonic-gate 		if (fnodep->nodeh != NULL) {
776*0Sstevel@tonic-gate 			(void) ptree_delete_node(fnodep->nodeh);
777*0Sstevel@tonic-gate 			(void) ptree_destroy_node(fnodep->nodeh);
778*0Sstevel@tonic-gate 			fnodep->nodeh = NULL;
779*0Sstevel@tonic-gate 		}
780*0Sstevel@tonic-gate 	}
781*0Sstevel@tonic-gate }
782*0Sstevel@tonic-gate /*
783*0Sstevel@tonic-gate  * Tuneables publishing functions
784*0Sstevel@tonic-gate  */
785*0Sstevel@tonic-gate static int
copy_persistent_tuneable(env_tuneable_t * tune,char * buf)786*0Sstevel@tonic-gate copy_persistent_tuneable(env_tuneable_t *tune, char *buf)
787*0Sstevel@tonic-gate {
788*0Sstevel@tonic-gate 
789*0Sstevel@tonic-gate 	switch (tune->type) {
790*0Sstevel@tonic-gate 	case PICL_PTYPE_INT : {
791*0Sstevel@tonic-gate 		(void) memcpy((int *)tune->value,
792*0Sstevel@tonic-gate 		    buf, tune->nbytes);
793*0Sstevel@tonic-gate 		break;
794*0Sstevel@tonic-gate 	}
795*0Sstevel@tonic-gate 	case PICL_PTYPE_CHARSTRING : {
796*0Sstevel@tonic-gate 		(void) memcpy((caddr_t)tune->value,
797*0Sstevel@tonic-gate 		    buf, tune->nbytes);
798*0Sstevel@tonic-gate 		break;
799*0Sstevel@tonic-gate 	}
800*0Sstevel@tonic-gate 	default	: {
801*0Sstevel@tonic-gate 		return (PICL_FAILURE);
802*0Sstevel@tonic-gate 	}
803*0Sstevel@tonic-gate 	}
804*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
805*0Sstevel@tonic-gate }
806*0Sstevel@tonic-gate 
807*0Sstevel@tonic-gate static void
env_parse_tunables(picl_nodehdl_t rooth)808*0Sstevel@tonic-gate env_parse_tunables(picl_nodehdl_t rooth)
809*0Sstevel@tonic-gate {
810*0Sstevel@tonic-gate 	char	nmbuf[SYS_NMLN];
811*0Sstevel@tonic-gate 	char    pname[PATH_MAX];
812*0Sstevel@tonic-gate 
813*0Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
814*0Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
815*0Sstevel@tonic-gate 		(void) strlcat(pname, TUNABLE_CONF_FILE, PATH_MAX);
816*0Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
817*0Sstevel@tonic-gate 			(void) picld_pluginutil_parse_config_file(rooth, pname);
818*0Sstevel@tonic-gate 			return;
819*0Sstevel@tonic-gate 		}
820*0Sstevel@tonic-gate 	}
821*0Sstevel@tonic-gate }
822*0Sstevel@tonic-gate 
823*0Sstevel@tonic-gate int
env_picl_setup_tuneables(void)824*0Sstevel@tonic-gate env_picl_setup_tuneables(void)
825*0Sstevel@tonic-gate {
826*0Sstevel@tonic-gate 	int		err;
827*0Sstevel@tonic-gate 	int		i;
828*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;
829*0Sstevel@tonic-gate 	picl_nodehdl_t	rooth;
830*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
831*0Sstevel@tonic-gate 	env_tuneable_t	*tuneablep;
832*0Sstevel@tonic-gate 	char		read_buf[BUFSIZ];
833*0Sstevel@tonic-gate 
834*0Sstevel@tonic-gate 	if (ptree_get_root(&rooth) != PICL_SUCCESS) {
835*0Sstevel@tonic-gate 		return (PICL_FAILURE);
836*0Sstevel@tonic-gate 	}
837*0Sstevel@tonic-gate 	err = ptree_create_and_add_node(rooth, PICL_PLUGINS_NODE,
838*0Sstevel@tonic-gate 	    PICL_CLASS_PICL, &nodeh);
839*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
840*0Sstevel@tonic-gate 		return (PICL_FAILURE);
841*0Sstevel@tonic-gate 	err = ptree_create_and_add_node(nodeh, PICL_ENVIRONMENTAL_NODE,
842*0Sstevel@tonic-gate 	    PICL_CLASS_PICL, &nodeh);
843*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
844*0Sstevel@tonic-gate 		return (PICL_FAILURE);
845*0Sstevel@tonic-gate 	}
846*0Sstevel@tonic-gate 
847*0Sstevel@tonic-gate 	/*
848*0Sstevel@tonic-gate 	 * Parse the conf file
849*0Sstevel@tonic-gate 	 */
850*0Sstevel@tonic-gate 	env_parse_tunables(rooth);
851*0Sstevel@tonic-gate 	for (i = 0; i < ntuneables; i++) {
852*0Sstevel@tonic-gate 		tuneablep = &tuneables[i];
853*0Sstevel@tonic-gate 		err = ptree_get_propval_by_name(nodeh, tuneablep->name,
854*0Sstevel@tonic-gate 		    read_buf, tuneablep->nbytes);
855*0Sstevel@tonic-gate 
856*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
857*0Sstevel@tonic-gate 			/*
858*0Sstevel@tonic-gate 			 * Add volitle functions to environmental node
859*0Sstevel@tonic-gate 			 */
860*0Sstevel@tonic-gate 			err = add_volatile_prop(nodeh, tuneablep->name,
861*0Sstevel@tonic-gate 			    tuneablep->type,
862*0Sstevel@tonic-gate 			    PICL_READ|PICL_WRITE, tuneablep->nbytes,
863*0Sstevel@tonic-gate 			    tuneablep->rfunc,
864*0Sstevel@tonic-gate 			    tuneablep->wfunc, &proph);
865*0Sstevel@tonic-gate 
866*0Sstevel@tonic-gate 			tuneablep->proph = proph;
867*0Sstevel@tonic-gate 		} else {
868*0Sstevel@tonic-gate 			/*
869*0Sstevel@tonic-gate 			 * property is persistent
870*0Sstevel@tonic-gate 			 */
871*0Sstevel@tonic-gate 			(void) copy_persistent_tuneable(tuneablep,
872*0Sstevel@tonic-gate 			    read_buf);
873*0Sstevel@tonic-gate 		}
874*0Sstevel@tonic-gate 	}
875*0Sstevel@tonic-gate 
876*0Sstevel@tonic-gate 	return	(PICL_SUCCESS);
877*0Sstevel@tonic-gate }
878*0Sstevel@tonic-gate 
879*0Sstevel@tonic-gate /*
880*0Sstevel@tonic-gate  * Find the ENVMODEL_CONF_FILE file.
881*0Sstevel@tonic-gate  */
882*0Sstevel@tonic-gate static int
get_envmodel_conf_file(char * outfilename)883*0Sstevel@tonic-gate get_envmodel_conf_file(char *outfilename)
884*0Sstevel@tonic-gate {
885*0Sstevel@tonic-gate 	char	nmbuf[SYS_NMLN];
886*0Sstevel@tonic-gate 	char    pname[PATH_MAX];
887*0Sstevel@tonic-gate 
888*0Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
889*0Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
890*0Sstevel@tonic-gate 		(void) strlcat(pname, ENV_CONF_FILE, PATH_MAX);
891*0Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
892*0Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
893*0Sstevel@tonic-gate 			return (0);
894*0Sstevel@tonic-gate 		}
895*0Sstevel@tonic-gate 	}
896*0Sstevel@tonic-gate 
897*0Sstevel@tonic-gate 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
898*0Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
899*0Sstevel@tonic-gate 		(void) strlcat(pname, ENV_CONF_FILE, PATH_MAX);
900*0Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
901*0Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
902*0Sstevel@tonic-gate 			return (0);
903*0Sstevel@tonic-gate 		}
904*0Sstevel@tonic-gate 	}
905*0Sstevel@tonic-gate 
906*0Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
907*0Sstevel@tonic-gate 	    ENV_CONF_FILE);
908*0Sstevel@tonic-gate 
909*0Sstevel@tonic-gate 	if (access(pname, R_OK) == 0) {
910*0Sstevel@tonic-gate 		(void) strlcpy(outfilename, pname, PATH_MAX);
911*0Sstevel@tonic-gate 		return (0);
912*0Sstevel@tonic-gate 	}
913*0Sstevel@tonic-gate 
914*0Sstevel@tonic-gate 	return (-1);
915*0Sstevel@tonic-gate }
916*0Sstevel@tonic-gate 
917*0Sstevel@tonic-gate /* Delete all sensor/fan nodes and any properties created by this plugin */
918*0Sstevel@tonic-gate void
env_picl_destroy(void)919*0Sstevel@tonic-gate env_picl_destroy(void)
920*0Sstevel@tonic-gate {
921*0Sstevel@tonic-gate 	delete_fan_nodes_and_props();
922*0Sstevel@tonic-gate 	delete_sensor_nodes_and_props();
923*0Sstevel@tonic-gate 	delete_disk_nodes_and_props();
924*0Sstevel@tonic-gate }
925*0Sstevel@tonic-gate 
926*0Sstevel@tonic-gate void
env_picl_setup(void)927*0Sstevel@tonic-gate env_picl_setup(void)
928*0Sstevel@tonic-gate {
929*0Sstevel@tonic-gate 	int		err;
930*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
931*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
932*0Sstevel@tonic-gate 	disk_node_t	*dnodep;
933*0Sstevel@tonic-gate 	picl_nodehdl_t	plath;
934*0Sstevel@tonic-gate 	char		fullfilename[PATH_MAX];
935*0Sstevel@tonic-gate 	picl_nodehdl_t  rooth;
936*0Sstevel@tonic-gate 	int		i;
937*0Sstevel@tonic-gate 
938*0Sstevel@tonic-gate 
939*0Sstevel@tonic-gate 	/*
940*0Sstevel@tonic-gate 	 * Initialize sensorp and other fields in the sensor_nodes[] array
941*0Sstevel@tonic-gate 	 */
942*0Sstevel@tonic-gate 
943*0Sstevel@tonic-gate 	for (i = 0; i < N_SENSOR_NODES; ++i) {
944*0Sstevel@tonic-gate 		snodep = &sensor_nodes[i];
945*0Sstevel@tonic-gate 		snodep->sensorp = sensor_lookup(snodep->sensor_name);
946*0Sstevel@tonic-gate 		snodep->nodeh = NULL;
947*0Sstevel@tonic-gate 		snodep->proph = NULL;
948*0Sstevel@tonic-gate 		snodep->target_proph = NULL;
949*0Sstevel@tonic-gate 	}
950*0Sstevel@tonic-gate 
951*0Sstevel@tonic-gate 	/*
952*0Sstevel@tonic-gate 	 * Initialize fanp and other fields in the fan_nodes[] array
953*0Sstevel@tonic-gate 	 */
954*0Sstevel@tonic-gate 	for (i = 0; i < N_FAN_NODES; ++i) {
955*0Sstevel@tonic-gate 		fnodep = &fan_nodes[i];
956*0Sstevel@tonic-gate 		fnodep->fanp = fan_lookup(fnodep->fan_name);
957*0Sstevel@tonic-gate 		fnodep->nodeh = NULL;
958*0Sstevel@tonic-gate 		fnodep->proph = NULL;
959*0Sstevel@tonic-gate 	}
960*0Sstevel@tonic-gate 
961*0Sstevel@tonic-gate 	/*
962*0Sstevel@tonic-gate 	 * Initialize diskp and other fields in the disk_nodes[] array
963*0Sstevel@tonic-gate 	 */
964*0Sstevel@tonic-gate 	for (i = 0; i < N_DISK_NODES; ++i) {
965*0Sstevel@tonic-gate 		dnodep = &disk_nodes[i];
966*0Sstevel@tonic-gate 		dnodep->diskp = disk_lookup(dnodep->disk_name);
967*0Sstevel@tonic-gate 		dnodep->nodeh = NULL;
968*0Sstevel@tonic-gate 		dnodep->proph = NULL;
969*0Sstevel@tonic-gate 	}
970*0Sstevel@tonic-gate 
971*0Sstevel@tonic-gate 	/*
972*0Sstevel@tonic-gate 	 * Get platform handle and populate PICL tree with environmental
973*0Sstevel@tonic-gate 	 * nodes and properties
974*0Sstevel@tonic-gate 	 */
975*0Sstevel@tonic-gate 	err = ptree_get_node_by_path("/platform", &plath);
976*0Sstevel@tonic-gate 
977*0Sstevel@tonic-gate 	if (err == PICL_SUCCESS)
978*0Sstevel@tonic-gate 		err = add_sensor_nodes_and_props();
979*0Sstevel@tonic-gate 	if (err == PICL_SUCCESS)
980*0Sstevel@tonic-gate 		err = add_fan_nodes_and_props();
981*0Sstevel@tonic-gate 	if ((err == PICL_SUCCESS) && (monitor_disk_temp))
982*0Sstevel@tonic-gate 		err = add_disk_nodes_and_props();
983*0Sstevel@tonic-gate 
984*0Sstevel@tonic-gate 	/*
985*0Sstevel@tonic-gate 	 * We can safely call delete_xxx_nodes_and_props even
986*0Sstevel@tonic-gate 	 * if nodes were not added.
987*0Sstevel@tonic-gate 	 */
988*0Sstevel@tonic-gate 
989*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
990*0Sstevel@tonic-gate 		delete_fan_nodes_and_props();
991*0Sstevel@tonic-gate 		delete_disk_nodes_and_props();
992*0Sstevel@tonic-gate 		delete_sensor_nodes_and_props();
993*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
994*0Sstevel@tonic-gate 		return;
995*0Sstevel@tonic-gate 	}
996*0Sstevel@tonic-gate 
997*0Sstevel@tonic-gate 	/*
998*0Sstevel@tonic-gate 	 * Parse the envmodel.conf file and populate the PICL tree
999*0Sstevel@tonic-gate 	 */
1000*0Sstevel@tonic-gate 	if (get_envmodel_conf_file(fullfilename) < 0)
1001*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
1002*0Sstevel@tonic-gate 	if (ptree_get_root(&rooth) != PICL_SUCCESS)
1003*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
1004*0Sstevel@tonic-gate 	err = picld_pluginutil_parse_config_file(rooth, fullfilename);
1005*0Sstevel@tonic-gate 
1006*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
1007*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
1008*0Sstevel@tonic-gate }
1009