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 (c) 1999-2001 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
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  * For each temperature-device class node, it does the following:
34*0Sstevel@tonic-gate  *	- Create cpu and cpu-ambient temperautre-sensor class nodes.
35*0Sstevel@tonic-gate  *	- Create "devfs-path" property under each temperature-sensor class node
36*0Sstevel@tonic-gate  *	- Create "Temperature" volatile property under these nodes.
37*0Sstevel@tonic-gate  *	- Create various temperature threshold properties under each node.
38*0Sstevel@tonic-gate  *	- Create "Temperature" and "AmbientTemperature" volatile properties
39*0Sstevel@tonic-gate  *	  under corresponding "cpu" class node.
40*0Sstevel@tonic-gate  *
41*0Sstevel@tonic-gate  * For the "fan-control" node, it does the following:
42*0Sstevel@tonic-gate  *	- Create system-fan node
43*0Sstevel@tonic-gate  *	- Create "devfs-path" property under "fan" class node
44*0Sstevel@tonic-gate  *	- Create "Speed" volatile propery under each node.
45*0Sstevel@tonic-gate  *	- Create "SpeedUnit" property under each node.
46*0Sstevel@tonic-gate  */
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate #include <stdio.h>
49*0Sstevel@tonic-gate #include <fcntl.h>
50*0Sstevel@tonic-gate #include <unistd.h>
51*0Sstevel@tonic-gate #include <syslog.h>
52*0Sstevel@tonic-gate #include <stdlib.h>
53*0Sstevel@tonic-gate #include <limits.h>
54*0Sstevel@tonic-gate #include <sys/open.h>
55*0Sstevel@tonic-gate #include <ctype.h>
56*0Sstevel@tonic-gate #include <string.h>
57*0Sstevel@tonic-gate #include <alloca.h>
58*0Sstevel@tonic-gate #include <libintl.h>
59*0Sstevel@tonic-gate #include <sys/systeminfo.h>
60*0Sstevel@tonic-gate #include <picl.h>
61*0Sstevel@tonic-gate #include <picltree.h>
62*0Sstevel@tonic-gate #include <picld_pluginutil.h>
63*0Sstevel@tonic-gate #include <pthread.h>
64*0Sstevel@tonic-gate #include <sys/utsname.h>
65*0Sstevel@tonic-gate #include <sys/systeminfo.h>
66*0Sstevel@tonic-gate #include "envd.h"
67*0Sstevel@tonic-gate 
68*0Sstevel@tonic-gate 
69*0Sstevel@tonic-gate /*
70*0Sstevel@tonic-gate  * Volatile property read/write function typedef
71*0Sstevel@tonic-gate  */
72*0Sstevel@tonic-gate typedef int ptree_vol_rdfunc_t(ptree_rarg_t *parg, void *buf);
73*0Sstevel@tonic-gate typedef int ptree_vol_wrfunc_t(ptree_warg_t *parg, const void *buf);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /*
76*0Sstevel@tonic-gate  * PICL Classes and standard property names
77*0Sstevel@tonic-gate  */
78*0Sstevel@tonic-gate #define	PICL_CLASS_CPU		"cpu"
79*0Sstevel@tonic-gate #define	PICL_CLASS_FAN		"fan"
80*0Sstevel@tonic-gate #define	PICL_CLASS_FAN_CONTROL	"fan-control"
81*0Sstevel@tonic-gate #define	PICL_CLASS_TEMP_DEVICE	"temperature-device"
82*0Sstevel@tonic-gate #define	PICL_CLASS_TEMP_SENSOR	"temperature-sensor"
83*0Sstevel@tonic-gate 
84*0Sstevel@tonic-gate #define	PICL_PROP_REG		"reg"
85*0Sstevel@tonic-gate #define	PICL_PROP_DEVFS_PATH	"devfs-path"
86*0Sstevel@tonic-gate #define	PICL_PROP_UNIT_ADDRESS	"UnitAddress"
87*0Sstevel@tonic-gate #define	UNITADDR_LEN_MAX	256	/* max length for UnitAddress */
88*0Sstevel@tonic-gate 
89*0Sstevel@tonic-gate /*
90*0Sstevel@tonic-gate  * temperature properties
91*0Sstevel@tonic-gate  */
92*0Sstevel@tonic-gate #define	PROP_TEMPERATURE	"Temperature"
93*0Sstevel@tonic-gate #define	PROP_CPU_AMB_TEMP	"AmbientTemperature"
94*0Sstevel@tonic-gate #define	PROP_CPU_DIE_TEMP	"Temperature"
95*0Sstevel@tonic-gate 
96*0Sstevel@tonic-gate /*
97*0Sstevel@tonic-gate  * Various temperature threshold property names
98*0Sstevel@tonic-gate  */
99*0Sstevel@tonic-gate #define	PROP_LOW_POWER_OFF	"LowPowerOffThreshold"
100*0Sstevel@tonic-gate #define	PROP_LOW_SHUTDOWN	"LowShutdownThreshold"
101*0Sstevel@tonic-gate #define	PROP_LOW_WARNING	"LowWarningThreshold"
102*0Sstevel@tonic-gate #define	PROP_HIGH_POWER_OFF	"HighPowerOffThreshold"
103*0Sstevel@tonic-gate #define	PROP_HIGH_SHUTDOWN	"HighShutdownThreshold"
104*0Sstevel@tonic-gate #define	PROP_HIGH_WARNING	"HighWarningThreshold"
105*0Sstevel@tonic-gate 
106*0Sstevel@tonic-gate /*
107*0Sstevel@tonic-gate  * fan properties
108*0Sstevel@tonic-gate  */
109*0Sstevel@tonic-gate #define	PROP_FAN_SPEED		"Speed"
110*0Sstevel@tonic-gate #define	PROP_FAN_SPEED_UNIT	"SpeedUnit"
111*0Sstevel@tonic-gate #define	PROP_FAN_SPEED_UNIT_VALUE	"%"
112*0Sstevel@tonic-gate 
113*0Sstevel@tonic-gate /*
114*0Sstevel@tonic-gate  * PICL class path for CPU nodes
115*0Sstevel@tonic-gate  */
116*0Sstevel@tonic-gate #define	CPU_PLAT_PATH		"_class:/upa/cpu?ID=0"
117*0Sstevel@tonic-gate 
118*0Sstevel@tonic-gate /*
119*0Sstevel@tonic-gate  * "UnitAddress" propval for various temperature devices (platform dependent)
120*0Sstevel@tonic-gate  */
121*0Sstevel@tonic-gate #define	CPU_TEMPDEV_UNITADDR	"0,30"
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate /*
124*0Sstevel@tonic-gate  * Sensor node data structure
125*0Sstevel@tonic-gate  */
126*0Sstevel@tonic-gate typedef struct {
127*0Sstevel@tonic-gate 	char		*sensor_name;	/* sensor name */
128*0Sstevel@tonic-gate 	env_sensor_t	*sensorp;	/* sensor info */
129*0Sstevel@tonic-gate 	char		*unitaddr;	/* parent's UnitAddress propval */
130*0Sstevel@tonic-gate 	char		*sdev_node;	/* sensed device node name */
131*0Sstevel@tonic-gate 	char		*sdev_pname;	/* sensed device "temp" prop name */
132*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;		/* sensor node handle */
133*0Sstevel@tonic-gate 	picl_prophdl_t	proph;		/* "Temperature" property handle */
134*0Sstevel@tonic-gate 	picl_prophdl_t	sdev_proph;	/* property handle for sensed dev */
135*0Sstevel@tonic-gate } sensor_node_t;
136*0Sstevel@tonic-gate 
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate /*
139*0Sstevel@tonic-gate  * Sensor nodes array
140*0Sstevel@tonic-gate  */
141*0Sstevel@tonic-gate static sensor_node_t sensor_nodes[] = {
142*0Sstevel@tonic-gate 	{SENSOR_CPU_DIE, NULL, CPU_TEMPDEV_UNITADDR,
143*0Sstevel@tonic-gate 	    CPU_PLAT_PATH, PROP_CPU_DIE_TEMP},
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	{SENSOR_CPU_AMB, NULL, CPU_TEMPDEV_UNITADDR,
146*0Sstevel@tonic-gate 	    CPU_PLAT_PATH, PROP_CPU_AMB_TEMP},
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 	{NULL, NULL, NULL, NULL, NULL}
149*0Sstevel@tonic-gate };
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate 
152*0Sstevel@tonic-gate /*
153*0Sstevel@tonic-gate  * Fan node data structure
154*0Sstevel@tonic-gate  */
155*0Sstevel@tonic-gate typedef struct {
156*0Sstevel@tonic-gate 	char		*fan_name;	/* fan name */
157*0Sstevel@tonic-gate 	env_fan_t 	*fanp;		/* fan information */
158*0Sstevel@tonic-gate 	char		*speed_unit;	/* speed unit string */
159*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;		/* "fan" node handle */
160*0Sstevel@tonic-gate 	picl_prophdl_t	proph;		/* "Speed" property handle */
161*0Sstevel@tonic-gate } fan_node_t;
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate 
164*0Sstevel@tonic-gate /*
165*0Sstevel@tonic-gate  * Fan node array
166*0Sstevel@tonic-gate  */
167*0Sstevel@tonic-gate static fan_node_t fan_nodes[] =  {
168*0Sstevel@tonic-gate 	{ENV_SYSTEM_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE},
169*0Sstevel@tonic-gate 	{NULL, NULL, NULL}
170*0Sstevel@tonic-gate };
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate /*
173*0Sstevel@tonic-gate  * Miscellaneous declarations
174*0Sstevel@tonic-gate  */
175*0Sstevel@tonic-gate typedef struct node_list {
176*0Sstevel@tonic-gate 	picl_nodehdl_t		nodeh;
177*0Sstevel@tonic-gate 	struct node_list	*next;
178*0Sstevel@tonic-gate } node_list_t;
179*0Sstevel@tonic-gate 
180*0Sstevel@tonic-gate static void delete_sensor_nodes_and_props(void);
181*0Sstevel@tonic-gate static void delete_fan_nodes_and_props(void);
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 
185*0Sstevel@tonic-gate /*
186*0Sstevel@tonic-gate  * Read function for volatile "Temperature" property
187*0Sstevel@tonic-gate  */
188*0Sstevel@tonic-gate static int
get_current_temp(ptree_rarg_t * parg,void * buf)189*0Sstevel@tonic-gate get_current_temp(ptree_rarg_t *parg, void *buf)
190*0Sstevel@tonic-gate {
191*0Sstevel@tonic-gate 	tempr_t 	temp;
192*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
193*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
194*0Sstevel@tonic-gate 
195*0Sstevel@tonic-gate 	/*
196*0Sstevel@tonic-gate 	 * Locate the sensor in our sensor_nodes table by matching the
197*0Sstevel@tonic-gate 	 * property handle and get its temperature.
198*0Sstevel@tonic-gate 	 */
199*0Sstevel@tonic-gate 	proph = parg->proph;
200*0Sstevel@tonic-gate 	for (snodep = &sensor_nodes[0]; snodep->sensor_name != NULL; snodep++) {
201*0Sstevel@tonic-gate 		if (snodep->proph != proph &&
202*0Sstevel@tonic-gate 		    snodep->sdev_proph != proph)
203*0Sstevel@tonic-gate 			continue;
204*0Sstevel@tonic-gate 
205*0Sstevel@tonic-gate 		if (get_temperature(snodep->sensorp, &temp) < 0)
206*0Sstevel@tonic-gate 			return (PICL_FAILURE);
207*0Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&temp, sizeof (tempr_t));
208*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
209*0Sstevel@tonic-gate 	}
210*0Sstevel@tonic-gate 	return (PICL_FAILURE);
211*0Sstevel@tonic-gate }
212*0Sstevel@tonic-gate 
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate /*
215*0Sstevel@tonic-gate  * Read function for volatile "Speed" property on "fan" class node
216*0Sstevel@tonic-gate  */
217*0Sstevel@tonic-gate static int
get_current_speed(ptree_rarg_t * parg,void * buf)218*0Sstevel@tonic-gate get_current_speed(ptree_rarg_t *parg, void *buf)
219*0Sstevel@tonic-gate {
220*0Sstevel@tonic-gate 	fanspeed_t	speed;
221*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
222*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
223*0Sstevel@tonic-gate 
224*0Sstevel@tonic-gate 	/*
225*0Sstevel@tonic-gate 	 * Locate the fan in our fan_nodes table by matching the
226*0Sstevel@tonic-gate 	 * property handle and get fan speed.
227*0Sstevel@tonic-gate 	 */
228*0Sstevel@tonic-gate 	proph = parg->proph;
229*0Sstevel@tonic-gate 	for (fnodep = &fan_nodes[0]; fnodep->fan_name != NULL; fnodep++) {
230*0Sstevel@tonic-gate 		if (fnodep->proph != proph)
231*0Sstevel@tonic-gate 			continue;
232*0Sstevel@tonic-gate 		if (get_fan_speed(fnodep->fanp, &speed) < 0)
233*0Sstevel@tonic-gate 			return (PICL_FAILURE);
234*0Sstevel@tonic-gate 
235*0Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&speed, sizeof (speed));
236*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
237*0Sstevel@tonic-gate 	}
238*0Sstevel@tonic-gate 	return (PICL_FAILURE);
239*0Sstevel@tonic-gate }
240*0Sstevel@tonic-gate 
241*0Sstevel@tonic-gate 
242*0Sstevel@tonic-gate static node_list_t *
add_node_to_list(picl_nodehdl_t nodeh,node_list_t * listp)243*0Sstevel@tonic-gate add_node_to_list(picl_nodehdl_t nodeh, node_list_t *listp)
244*0Sstevel@tonic-gate {
245*0Sstevel@tonic-gate 	node_list_t	*el;
246*0Sstevel@tonic-gate 	node_list_t	*tmp;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	el = malloc(sizeof (node_list_t));
249*0Sstevel@tonic-gate 	if (el == NULL)
250*0Sstevel@tonic-gate 		return (listp);
251*0Sstevel@tonic-gate 	el->nodeh = nodeh;
252*0Sstevel@tonic-gate 	el->next = NULL;
253*0Sstevel@tonic-gate 	if (listp == NULL) {
254*0Sstevel@tonic-gate 		listp = el;
255*0Sstevel@tonic-gate 		return (listp);
256*0Sstevel@tonic-gate 	}
257*0Sstevel@tonic-gate 
258*0Sstevel@tonic-gate 	/*
259*0Sstevel@tonic-gate 	 * append to the end to preserve the order found
260*0Sstevel@tonic-gate 	 */
261*0Sstevel@tonic-gate 	tmp = listp;
262*0Sstevel@tonic-gate 	while (tmp->next != NULL)
263*0Sstevel@tonic-gate 		tmp = tmp->next;
264*0Sstevel@tonic-gate 
265*0Sstevel@tonic-gate 	tmp->next = el;
266*0Sstevel@tonic-gate 	return (listp);
267*0Sstevel@tonic-gate }
268*0Sstevel@tonic-gate 
269*0Sstevel@tonic-gate 
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate /*
272*0Sstevel@tonic-gate  * Get a list of nodes of the specified classname under nodeh
273*0Sstevel@tonic-gate  * Once a node of the specified class is found, it's children are not
274*0Sstevel@tonic-gate  * searched.
275*0Sstevel@tonic-gate  */
276*0Sstevel@tonic-gate static node_list_t *
get_node_list_by_class(picl_nodehdl_t nodeh,const char * classname,node_list_t * listp)277*0Sstevel@tonic-gate get_node_list_by_class(picl_nodehdl_t nodeh, const char *classname,
278*0Sstevel@tonic-gate 	node_list_t *listp)
279*0Sstevel@tonic-gate {
280*0Sstevel@tonic-gate 	int		err;
281*0Sstevel@tonic-gate 	char		clname[PICL_CLASSNAMELEN_MAX+1];
282*0Sstevel@tonic-gate 	picl_nodehdl_t	chdh;
283*0Sstevel@tonic-gate 
284*0Sstevel@tonic-gate 	/*
285*0Sstevel@tonic-gate 	 * go through the children
286*0Sstevel@tonic-gate 	 */
287*0Sstevel@tonic-gate 	err = ptree_get_propval_by_name(nodeh, PICL_PROP_CHILD, &chdh,
288*0Sstevel@tonic-gate 	    sizeof (picl_nodehdl_t));
289*0Sstevel@tonic-gate 
290*0Sstevel@tonic-gate 	while (err == PICL_SUCCESS) {
291*0Sstevel@tonic-gate 		err = ptree_get_propval_by_name(chdh, PICL_PROP_CLASSNAME,
292*0Sstevel@tonic-gate 		    clname, strlen(classname) + 1);
293*0Sstevel@tonic-gate 
294*0Sstevel@tonic-gate 		if ((err == PICL_SUCCESS) && (strcmp(clname, classname) == 0))
295*0Sstevel@tonic-gate 			listp = add_node_to_list(chdh, listp);
296*0Sstevel@tonic-gate 		else
297*0Sstevel@tonic-gate 			listp = get_node_list_by_class(chdh, classname, listp);
298*0Sstevel@tonic-gate 
299*0Sstevel@tonic-gate 		err = ptree_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
300*0Sstevel@tonic-gate 		    sizeof (picl_nodehdl_t));
301*0Sstevel@tonic-gate 	}
302*0Sstevel@tonic-gate 	return (listp);
303*0Sstevel@tonic-gate }
304*0Sstevel@tonic-gate 
305*0Sstevel@tonic-gate 
306*0Sstevel@tonic-gate /*
307*0Sstevel@tonic-gate  * Free memory allocated to build the specified node list.
308*0Sstevel@tonic-gate  */
309*0Sstevel@tonic-gate static void
free_node_list(node_list_t * listp)310*0Sstevel@tonic-gate free_node_list(node_list_t *listp)
311*0Sstevel@tonic-gate {
312*0Sstevel@tonic-gate 	node_list_t	*next;
313*0Sstevel@tonic-gate 
314*0Sstevel@tonic-gate 	for (; listp != NULL; listp = next) {
315*0Sstevel@tonic-gate 		next = listp->next;
316*0Sstevel@tonic-gate 		free(listp);
317*0Sstevel@tonic-gate 	}
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate 
320*0Sstevel@tonic-gate /*
321*0Sstevel@tonic-gate  * Get PICL_PTYPE_CHARSTRING "UnitAddress" property
322*0Sstevel@tonic-gate  */
323*0Sstevel@tonic-gate static int
get_unit_address_prop(picl_nodehdl_t nodeh,void * buf,size_t len)324*0Sstevel@tonic-gate get_unit_address_prop(picl_nodehdl_t nodeh, void *buf, size_t len)
325*0Sstevel@tonic-gate {
326*0Sstevel@tonic-gate 	int			err;
327*0Sstevel@tonic-gate 	picl_prophdl_t		proph;
328*0Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
329*0Sstevel@tonic-gate 
330*0Sstevel@tonic-gate 	err = ptree_get_prop_by_name(nodeh, PICL_PROP_UNIT_ADDRESS, &proph);
331*0Sstevel@tonic-gate 	if (err == PICL_SUCCESS)
332*0Sstevel@tonic-gate 		err = ptree_get_propinfo(proph, &pinfo);
333*0Sstevel@tonic-gate 
334*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
335*0Sstevel@tonic-gate 		return (err);
336*0Sstevel@tonic-gate 
337*0Sstevel@tonic-gate 	if (pinfo.piclinfo.type != PICL_PTYPE_CHARSTRING ||
338*0Sstevel@tonic-gate 	    pinfo.piclinfo.size > len)
339*0Sstevel@tonic-gate 		return (PICL_FAILURE);
340*0Sstevel@tonic-gate 
341*0Sstevel@tonic-gate 	err = ptree_get_propval(proph, buf, pinfo.piclinfo.size);
342*0Sstevel@tonic-gate 	return (err);
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 
346*0Sstevel@tonic-gate /*
347*0Sstevel@tonic-gate  * Create and add the specified regular property
348*0Sstevel@tonic-gate  */
349*0Sstevel@tonic-gate 
350*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)351*0Sstevel@tonic-gate add_regular_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
352*0Sstevel@tonic-gate     int size, void *valbuf, picl_prophdl_t *prophp)
353*0Sstevel@tonic-gate {
354*0Sstevel@tonic-gate 	int			err;
355*0Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
356*0Sstevel@tonic-gate 	picl_prophdl_t		proph;
357*0Sstevel@tonic-gate 
358*0Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
359*0Sstevel@tonic-gate 	    type, access, size, name, NULL, NULL);
360*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
361*0Sstevel@tonic-gate 		return (err);
362*0Sstevel@tonic-gate 
363*0Sstevel@tonic-gate 	err = ptree_create_and_add_prop(nodeh, &propinfo, valbuf, &proph);
364*0Sstevel@tonic-gate 	if (err == PICL_SUCCESS && prophp)
365*0Sstevel@tonic-gate 		*prophp = proph;
366*0Sstevel@tonic-gate 	return (err);
367*0Sstevel@tonic-gate }
368*0Sstevel@tonic-gate 
369*0Sstevel@tonic-gate 
370*0Sstevel@tonic-gate /*
371*0Sstevel@tonic-gate  * Create and add the specified volatile property
372*0Sstevel@tonic-gate  */
373*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)374*0Sstevel@tonic-gate add_volatile_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
375*0Sstevel@tonic-gate     int size, ptree_vol_rdfunc_t *rdfunc, ptree_vol_wrfunc_t *wrfunc,
376*0Sstevel@tonic-gate     picl_prophdl_t *prophp)
377*0Sstevel@tonic-gate {
378*0Sstevel@tonic-gate 	int			err;
379*0Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
380*0Sstevel@tonic-gate 	picl_prophdl_t		proph;
381*0Sstevel@tonic-gate 
382*0Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
383*0Sstevel@tonic-gate 	    type, (access|PICL_VOLATILE), size, name, rdfunc, wrfunc);
384*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
385*0Sstevel@tonic-gate 		return (err);
386*0Sstevel@tonic-gate 
387*0Sstevel@tonic-gate 	err = ptree_create_and_add_prop(nodeh, &propinfo, NULL, &proph);
388*0Sstevel@tonic-gate 	if (err == PICL_SUCCESS && prophp)
389*0Sstevel@tonic-gate 		*prophp = proph;
390*0Sstevel@tonic-gate 	return (err);
391*0Sstevel@tonic-gate }
392*0Sstevel@tonic-gate 
393*0Sstevel@tonic-gate /*
394*0Sstevel@tonic-gate  * Add temperature threshold properties
395*0Sstevel@tonic-gate  */
396*0Sstevel@tonic-gate static void
add_sensor_thresh_props(picl_nodehdl_t nodeh,sensor_thresh_t * threshp)397*0Sstevel@tonic-gate add_sensor_thresh_props(picl_nodehdl_t nodeh, sensor_thresh_t *threshp)
398*0Sstevel@tonic-gate {
399*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
400*0Sstevel@tonic-gate 
401*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_LOW_POWER_OFF,
402*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
403*0Sstevel@tonic-gate 	    sizeof (threshp->low_power_off),
404*0Sstevel@tonic-gate 	    (void *)&(threshp->low_power_off), &proph);
405*0Sstevel@tonic-gate 
406*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_LOW_SHUTDOWN,
407*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
408*0Sstevel@tonic-gate 	    sizeof (threshp->low_shutdown),
409*0Sstevel@tonic-gate 	    (void *)&(threshp->low_shutdown), &proph);
410*0Sstevel@tonic-gate 
411*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_LOW_WARNING,
412*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
413*0Sstevel@tonic-gate 	    sizeof (threshp->low_warning),
414*0Sstevel@tonic-gate 	    (void *)&(threshp->low_warning), &proph);
415*0Sstevel@tonic-gate 
416*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_HIGH_WARNING,
417*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
418*0Sstevel@tonic-gate 	    sizeof (threshp->high_warning),
419*0Sstevel@tonic-gate 	    (void *)&(threshp->high_warning), &proph);
420*0Sstevel@tonic-gate 
421*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_HIGH_SHUTDOWN,
422*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
423*0Sstevel@tonic-gate 	    sizeof (threshp->high_shutdown),
424*0Sstevel@tonic-gate 	    (void *)&(threshp->high_shutdown), &proph);
425*0Sstevel@tonic-gate 
426*0Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_HIGH_POWER_OFF,
427*0Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
428*0Sstevel@tonic-gate 	    sizeof (threshp->high_power_off),
429*0Sstevel@tonic-gate 	    (void *)&(threshp->high_power_off), &proph);
430*0Sstevel@tonic-gate }
431*0Sstevel@tonic-gate 
432*0Sstevel@tonic-gate 
433*0Sstevel@tonic-gate /*
434*0Sstevel@tonic-gate  * Lookup "temperature-device" class nodes and create "temperature-sensor"
435*0Sstevel@tonic-gate  * class nodes and relevant properties under those nodes.
436*0Sstevel@tonic-gate  *
437*0Sstevel@tonic-gate  * For each entry in sensor_nodes[] array, do the following:
438*0Sstevel@tonic-gate  *	- Create specified (cpu-die or cpu-ambient) "temperautre-sensor" class
439*0Sstevel@tonic-gate  *	  node.
440*0Sstevel@tonic-gate  *	- Create "devfs-path" property under this node.
441*0Sstevel@tonic-gate  *	- Create "Temperature" volatile property under this node.
442*0Sstevel@tonic-gate  *	- Create various temperature threshold properties under this node.
443*0Sstevel@tonic-gate  *	- Create specified ("Temperature" or "AmbientTemperature") volatile
444*0Sstevel@tonic-gate  *	  temperature property under specified sdev_node node.
445*0Sstevel@tonic-gate  */
446*0Sstevel@tonic-gate 
447*0Sstevel@tonic-gate static int
add_sensor_nodes_and_props(picl_nodehdl_t plath)448*0Sstevel@tonic-gate add_sensor_nodes_and_props(picl_nodehdl_t plath)
449*0Sstevel@tonic-gate {
450*0Sstevel@tonic-gate 	int		err;
451*0Sstevel@tonic-gate 	char		*pname, *nodename, *refnode, *devfs_path;
452*0Sstevel@tonic-gate 	node_list_t	*node_list, *listp;
453*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
454*0Sstevel@tonic-gate 	sensor_thresh_t *threshp;
455*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh, refnodeh, cnodeh;
456*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
457*0Sstevel@tonic-gate 	char		unitaddr[UNITADDR_LEN_MAX];
458*0Sstevel@tonic-gate 	env_sensor_t	*sensorp;
459*0Sstevel@tonic-gate 
460*0Sstevel@tonic-gate 	node_list =
461*0Sstevel@tonic-gate 	    get_node_list_by_class(plath, PICL_CLASS_TEMP_DEVICE, NULL);
462*0Sstevel@tonic-gate 
463*0Sstevel@tonic-gate 	if (node_list == NULL)
464*0Sstevel@tonic-gate 		return (PICL_FAILURE);
465*0Sstevel@tonic-gate 
466*0Sstevel@tonic-gate 	for (listp = node_list; listp != NULL; listp = listp->next) {
467*0Sstevel@tonic-gate 		/*
468*0Sstevel@tonic-gate 		 * Get "reg" property. Skip if no "reg" property found.
469*0Sstevel@tonic-gate 		 */
470*0Sstevel@tonic-gate 		nodeh = listp->nodeh;
471*0Sstevel@tonic-gate 		err = get_unit_address_prop(nodeh, (void *)unitaddr,
472*0Sstevel@tonic-gate 		    sizeof (unitaddr));
473*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
474*0Sstevel@tonic-gate 			continue;
475*0Sstevel@tonic-gate 
476*0Sstevel@tonic-gate 		for (snodep = sensor_nodes; snodep->sensor_name != NULL;
477*0Sstevel@tonic-gate 		    snodep++) {
478*0Sstevel@tonic-gate 
479*0Sstevel@tonic-gate 			/* Match "UnitAddress" property */
480*0Sstevel@tonic-gate 			if (strcasecmp(unitaddr, snodep->unitaddr) != 0)
481*0Sstevel@tonic-gate 				continue;
482*0Sstevel@tonic-gate 
483*0Sstevel@tonic-gate 			/*
484*0Sstevel@tonic-gate 			 * Skip if already initialized or no sensor info
485*0Sstevel@tonic-gate 			 */
486*0Sstevel@tonic-gate 			sensorp = snodep->sensorp;
487*0Sstevel@tonic-gate 			if (snodep->nodeh != NULL || sensorp == NULL)
488*0Sstevel@tonic-gate 				continue;
489*0Sstevel@tonic-gate 
490*0Sstevel@tonic-gate 			/*
491*0Sstevel@tonic-gate 			 * Create temperature-sensor node
492*0Sstevel@tonic-gate 			 */
493*0Sstevel@tonic-gate 			nodename = snodep->sensor_name;
494*0Sstevel@tonic-gate 			err = ptree_create_and_add_node(nodeh, nodename,
495*0Sstevel@tonic-gate 			    PICL_CLASS_TEMP_SENSOR, &cnodeh);
496*0Sstevel@tonic-gate 			if (env_debug)
497*0Sstevel@tonic-gate 				envd_log(LOG_INFO,
498*0Sstevel@tonic-gate 				    "Creating PICL sensor node '%s' err:%d\n",
499*0Sstevel@tonic-gate 				    nodename, err);
500*0Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
501*0Sstevel@tonic-gate 				break;
502*0Sstevel@tonic-gate 
503*0Sstevel@tonic-gate 			/* save node handle */
504*0Sstevel@tonic-gate 			snodep->nodeh = cnodeh;
505*0Sstevel@tonic-gate 
506*0Sstevel@tonic-gate 			/*
507*0Sstevel@tonic-gate 			 * Add "devfs_path" property in child node
508*0Sstevel@tonic-gate 			 */
509*0Sstevel@tonic-gate 			devfs_path = sensorp->devfs_path;
510*0Sstevel@tonic-gate 			pname = PICL_PROP_DEVFS_PATH;
511*0Sstevel@tonic-gate 			err = add_regular_prop(cnodeh, pname,
512*0Sstevel@tonic-gate 			    PICL_PTYPE_CHARSTRING, PICL_READ,
513*0Sstevel@tonic-gate 			    strlen(devfs_path)+1, (void *)devfs_path, &proph);
514*0Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
515*0Sstevel@tonic-gate 				break;
516*0Sstevel@tonic-gate 
517*0Sstevel@tonic-gate 			/*
518*0Sstevel@tonic-gate 			 * Now add volatile "temperature" volatile property
519*0Sstevel@tonic-gate 			 * in this "temperature-sensor" class node.
520*0Sstevel@tonic-gate 			 */
521*0Sstevel@tonic-gate 			pname = PROP_TEMPERATURE;
522*0Sstevel@tonic-gate 			err = add_volatile_prop(cnodeh, pname,
523*0Sstevel@tonic-gate 			    PICL_PTYPE_INT, PICL_READ, sizeof (tempr_t),
524*0Sstevel@tonic-gate 			    get_current_temp, NULL, &proph);
525*0Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
526*0Sstevel@tonic-gate 				break;
527*0Sstevel@tonic-gate 
528*0Sstevel@tonic-gate 			/* Save prop handle */
529*0Sstevel@tonic-gate 			snodep->proph = proph;
530*0Sstevel@tonic-gate 
531*0Sstevel@tonic-gate 			/*
532*0Sstevel@tonic-gate 			 * Add threshold related properties
533*0Sstevel@tonic-gate 			 */
534*0Sstevel@tonic-gate 			threshp = sensorp->temp_thresh;
535*0Sstevel@tonic-gate 			if (threshp != NULL)
536*0Sstevel@tonic-gate 				add_sensor_thresh_props(cnodeh, threshp);
537*0Sstevel@tonic-gate 
538*0Sstevel@tonic-gate 			/*
539*0Sstevel@tonic-gate 			 * Finally create property in the sensed device
540*0Sstevel@tonic-gate 			 * (if one specified)
541*0Sstevel@tonic-gate 			 */
542*0Sstevel@tonic-gate 			refnode =  snodep->sdev_node;
543*0Sstevel@tonic-gate 			pname =  snodep->sdev_pname;
544*0Sstevel@tonic-gate 			if (refnode == NULL || pname == NULL)
545*0Sstevel@tonic-gate 				continue;
546*0Sstevel@tonic-gate 
547*0Sstevel@tonic-gate 			err = ptree_get_node_by_path(refnode, &refnodeh);
548*0Sstevel@tonic-gate 			if (err == PICL_SUCCESS) {
549*0Sstevel@tonic-gate 				err = add_volatile_prop(refnodeh, pname,
550*0Sstevel@tonic-gate 				    PICL_PTYPE_INT, PICL_READ,
551*0Sstevel@tonic-gate 				    sizeof (tempr_t), get_current_temp,
552*0Sstevel@tonic-gate 				    NULL, &proph);
553*0Sstevel@tonic-gate 			}
554*0Sstevel@tonic-gate 
555*0Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
556*0Sstevel@tonic-gate 				break;
557*0Sstevel@tonic-gate 
558*0Sstevel@tonic-gate 			/* Save prop handle */
559*0Sstevel@tonic-gate 			snodep->sdev_proph = proph;
560*0Sstevel@tonic-gate 		}
561*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
562*0Sstevel@tonic-gate 			delete_sensor_nodes_and_props();
563*0Sstevel@tonic-gate 			free_node_list(node_list);
564*0Sstevel@tonic-gate 			if (env_debug)
565*0Sstevel@tonic-gate 				envd_log(LOG_INFO,
566*0Sstevel@tonic-gate 				    "Can't create prop/node for sensor '%s'\n",
567*0Sstevel@tonic-gate 				    nodename);
568*0Sstevel@tonic-gate 			return (err);
569*0Sstevel@tonic-gate 		}
570*0Sstevel@tonic-gate 	}
571*0Sstevel@tonic-gate 
572*0Sstevel@tonic-gate 	free_node_list(node_list);
573*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
574*0Sstevel@tonic-gate }
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate /*
577*0Sstevel@tonic-gate  * Delete all sensor nodes and related properties created by the
578*0Sstevel@tonic-gate  * add_sensor_prop() for each sensor node in the PICL tree.
579*0Sstevel@tonic-gate  */
580*0Sstevel@tonic-gate static void
delete_sensor_nodes_and_props(void)581*0Sstevel@tonic-gate delete_sensor_nodes_and_props(void)
582*0Sstevel@tonic-gate {
583*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
584*0Sstevel@tonic-gate 
585*0Sstevel@tonic-gate 	/*
586*0Sstevel@tonic-gate 	 * Delete/destroy any property created in the sensed device
587*0Sstevel@tonic-gate 	 * as well as the sensor node and all properties under it.
588*0Sstevel@tonic-gate 	 * Note that deleiing/destroying a node deletes/destroys
589*0Sstevel@tonic-gate 	 * all properties within that node.
590*0Sstevel@tonic-gate 	 */
591*0Sstevel@tonic-gate 
592*0Sstevel@tonic-gate 	for (snodep = sensor_nodes; snodep->sensor_name != NULL; snodep++) {
593*0Sstevel@tonic-gate 		if (snodep->sdev_proph != NULL) {
594*0Sstevel@tonic-gate 			(void) ptree_delete_prop(snodep->sdev_proph);
595*0Sstevel@tonic-gate 			(void) ptree_destroy_prop(snodep->sdev_proph);
596*0Sstevel@tonic-gate 			snodep->sdev_proph = NULL;
597*0Sstevel@tonic-gate 		}
598*0Sstevel@tonic-gate 
599*0Sstevel@tonic-gate 		if (snodep->nodeh != NULL) {
600*0Sstevel@tonic-gate 			/* delete node and all properties under it */
601*0Sstevel@tonic-gate 			(void) ptree_delete_node(snodep->nodeh);
602*0Sstevel@tonic-gate 			(void) ptree_destroy_node(snodep->nodeh);
603*0Sstevel@tonic-gate 			snodep->nodeh = NULL;
604*0Sstevel@tonic-gate 			snodep->proph = NULL;
605*0Sstevel@tonic-gate 		}
606*0Sstevel@tonic-gate 	}
607*0Sstevel@tonic-gate }
608*0Sstevel@tonic-gate 
609*0Sstevel@tonic-gate 
610*0Sstevel@tonic-gate /*
611*0Sstevel@tonic-gate  * Lookup "fan-control" class node and create "fan" class nodes and
612*0Sstevel@tonic-gate  * relevant properties under those nodes.
613*0Sstevel@tonic-gate  *
614*0Sstevel@tonic-gate  * For each entry in fan_nodes[] array, do the following:
615*0Sstevel@tonic-gate  *	- Create specified "fan" class node.
616*0Sstevel@tonic-gate  *	- Create "devfs-path" property under "fan" class node
617*0Sstevel@tonic-gate  *	- Create "Speed" volatile propery under "fan" class node.
618*0Sstevel@tonic-gate  *	- Create "SpeedUnit" property under "fan" class node.
619*0Sstevel@tonic-gate  */
620*0Sstevel@tonic-gate 
621*0Sstevel@tonic-gate static int
add_fan_nodes_and_props(picl_nodehdl_t plath)622*0Sstevel@tonic-gate add_fan_nodes_and_props(picl_nodehdl_t plath)
623*0Sstevel@tonic-gate {
624*0Sstevel@tonic-gate 	int		err;
625*0Sstevel@tonic-gate 	char		*pname, *nodename, *devfs_path;
626*0Sstevel@tonic-gate 	env_fan_t	*fanp;
627*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
628*0Sstevel@tonic-gate 	picl_nodehdl_t	nodeh, cnodeh;
629*0Sstevel@tonic-gate 	picl_prophdl_t	proph;
630*0Sstevel@tonic-gate 	node_list_t	*node_list, *listp;
631*0Sstevel@tonic-gate 
632*0Sstevel@tonic-gate 	node_list =
633*0Sstevel@tonic-gate 	    get_node_list_by_class(plath, PICL_CLASS_FAN_CONTROL, NULL);
634*0Sstevel@tonic-gate 
635*0Sstevel@tonic-gate 	if (node_list == NULL)
636*0Sstevel@tonic-gate 		return (PICL_FAILURE);
637*0Sstevel@tonic-gate 
638*0Sstevel@tonic-gate 	for (listp = node_list; listp != NULL; listp = listp->next) {
639*0Sstevel@tonic-gate 		/*
640*0Sstevel@tonic-gate 		 * Add various fan nodes and properties
641*0Sstevel@tonic-gate 		 */
642*0Sstevel@tonic-gate 		nodeh = listp->nodeh;
643*0Sstevel@tonic-gate 		err = PICL_SUCCESS;
644*0Sstevel@tonic-gate 		for (fnodep = fan_nodes; fnodep->fan_name != NULL; fnodep++) {
645*0Sstevel@tonic-gate 
646*0Sstevel@tonic-gate 			/* Skip if already initialized or no fan info */
647*0Sstevel@tonic-gate 			if (fnodep->nodeh != NULL || fnodep->fanp == NULL)
648*0Sstevel@tonic-gate 				continue;
649*0Sstevel@tonic-gate 
650*0Sstevel@tonic-gate 			/*
651*0Sstevel@tonic-gate 			 * Create "fan" class node and save node handle
652*0Sstevel@tonic-gate 			 */
653*0Sstevel@tonic-gate 			nodename = fnodep->fan_name;
654*0Sstevel@tonic-gate 			err = ptree_create_and_add_node(nodeh, nodename,
655*0Sstevel@tonic-gate 			    PICL_CLASS_FAN, &cnodeh);
656*0Sstevel@tonic-gate 			if (env_debug)
657*0Sstevel@tonic-gate 				envd_log(LOG_INFO,
658*0Sstevel@tonic-gate 				    "Creating PICL fan node '%s' err:%d\n",
659*0Sstevel@tonic-gate 				    nodename, err);
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
662*0Sstevel@tonic-gate 				break;
663*0Sstevel@tonic-gate 			fnodep->nodeh = cnodeh;
664*0Sstevel@tonic-gate 
665*0Sstevel@tonic-gate 			/*
666*0Sstevel@tonic-gate 			 * Add "devfs_path" property in child node
667*0Sstevel@tonic-gate 			 */
668*0Sstevel@tonic-gate 			fanp = fnodep->fanp;
669*0Sstevel@tonic-gate 			devfs_path  = fanp->devfs_path;
670*0Sstevel@tonic-gate 			pname = PICL_PROP_DEVFS_PATH;
671*0Sstevel@tonic-gate 			err = add_regular_prop(cnodeh, pname,
672*0Sstevel@tonic-gate 			    PICL_PTYPE_CHARSTRING, PICL_READ,
673*0Sstevel@tonic-gate 			    strlen(devfs_path)+1, (void *)devfs_path, &proph);
674*0Sstevel@tonic-gate 
675*0Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
676*0Sstevel@tonic-gate 				break;
677*0Sstevel@tonic-gate 
678*0Sstevel@tonic-gate 			/*
679*0Sstevel@tonic-gate 			 * Add "Speed" volatile property in this "fan"
680*0Sstevel@tonic-gate 			 * class node and save prop handle.
681*0Sstevel@tonic-gate 			 */
682*0Sstevel@tonic-gate 			pname = PROP_FAN_SPEED;
683*0Sstevel@tonic-gate 			err = add_volatile_prop(cnodeh, pname, PICL_PTYPE_INT,
684*0Sstevel@tonic-gate 			    PICL_READ, sizeof (fanspeed_t), get_current_speed,
685*0Sstevel@tonic-gate 			    NULL, &proph);
686*0Sstevel@tonic-gate 
687*0Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
688*0Sstevel@tonic-gate 				break;
689*0Sstevel@tonic-gate 			fnodep->proph = proph;
690*0Sstevel@tonic-gate 
691*0Sstevel@tonic-gate 			/*
692*0Sstevel@tonic-gate 			 * Add other "fan" class properties
693*0Sstevel@tonic-gate 			 */
694*0Sstevel@tonic-gate 			pname = PROP_FAN_SPEED_UNIT,
695*0Sstevel@tonic-gate 			err = add_regular_prop(cnodeh, pname,
696*0Sstevel@tonic-gate 			    PICL_PTYPE_CHARSTRING, PICL_READ,
697*0Sstevel@tonic-gate 			    strlen(fnodep->speed_unit)+1,
698*0Sstevel@tonic-gate 			    (void *)fnodep->speed_unit, &proph);
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
701*0Sstevel@tonic-gate 				break;
702*0Sstevel@tonic-gate 		}
703*0Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
704*0Sstevel@tonic-gate 			delete_fan_nodes_and_props();
705*0Sstevel@tonic-gate 			free_node_list(node_list);
706*0Sstevel@tonic-gate 			if (env_debug)
707*0Sstevel@tonic-gate 				envd_log(LOG_WARNING,
708*0Sstevel@tonic-gate 				    "Can't create prop/node for fan '%s'\n",
709*0Sstevel@tonic-gate 				    nodename);
710*0Sstevel@tonic-gate 			return (err);
711*0Sstevel@tonic-gate 		}
712*0Sstevel@tonic-gate 	}
713*0Sstevel@tonic-gate 
714*0Sstevel@tonic-gate 	free_node_list(node_list);
715*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
716*0Sstevel@tonic-gate }
717*0Sstevel@tonic-gate 
718*0Sstevel@tonic-gate 
719*0Sstevel@tonic-gate /*
720*0Sstevel@tonic-gate  * Delete all fan nodes and related properties created by the
721*0Sstevel@tonic-gate  * add_fan_props() for each fan node in the PICL tree.
722*0Sstevel@tonic-gate  */
723*0Sstevel@tonic-gate static void
delete_fan_nodes_and_props(void)724*0Sstevel@tonic-gate delete_fan_nodes_and_props(void)
725*0Sstevel@tonic-gate {
726*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
727*0Sstevel@tonic-gate 
728*0Sstevel@tonic-gate 	/*
729*0Sstevel@tonic-gate 	 * Delete/destroy fan node and all properties under it.
730*0Sstevel@tonic-gate 	 * Note that deleiing/destroying a node deletes/destroys
731*0Sstevel@tonic-gate 	 * all properties within that node.
732*0Sstevel@tonic-gate 	 */
733*0Sstevel@tonic-gate 
734*0Sstevel@tonic-gate 	for (fnodep = fan_nodes; fnodep->fan_name != NULL; fnodep++) {
735*0Sstevel@tonic-gate 		if (fnodep->nodeh != NULL) {
736*0Sstevel@tonic-gate 			(void) ptree_delete_node(fnodep->nodeh);
737*0Sstevel@tonic-gate 			(void) ptree_destroy_node(fnodep->nodeh);
738*0Sstevel@tonic-gate 			fnodep->nodeh = NULL;
739*0Sstevel@tonic-gate 		}
740*0Sstevel@tonic-gate 	}
741*0Sstevel@tonic-gate }
742*0Sstevel@tonic-gate 
743*0Sstevel@tonic-gate /*
744*0Sstevel@tonic-gate  * Find the ENVMODEL_CONF_FILE file.
745*0Sstevel@tonic-gate  */
746*0Sstevel@tonic-gate static int
get_envmodel_conf_file(char * outfilename)747*0Sstevel@tonic-gate get_envmodel_conf_file(char *outfilename)
748*0Sstevel@tonic-gate {
749*0Sstevel@tonic-gate 	char	nmbuf[SYS_NMLN];
750*0Sstevel@tonic-gate 	char    pname[PATH_MAX];
751*0Sstevel@tonic-gate 
752*0Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
753*0Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
754*0Sstevel@tonic-gate 		(void) strlcat(pname, ENVMODEL_CONF_FILE, PATH_MAX);
755*0Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
756*0Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
757*0Sstevel@tonic-gate 			return (0);
758*0Sstevel@tonic-gate 		}
759*0Sstevel@tonic-gate 	}
760*0Sstevel@tonic-gate 
761*0Sstevel@tonic-gate 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
762*0Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
763*0Sstevel@tonic-gate 		(void) strlcat(pname, ENVMODEL_CONF_FILE, PATH_MAX);
764*0Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
765*0Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
766*0Sstevel@tonic-gate 			return (0);
767*0Sstevel@tonic-gate 		}
768*0Sstevel@tonic-gate 	}
769*0Sstevel@tonic-gate 
770*0Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
771*0Sstevel@tonic-gate 		ENVMODEL_CONF_FILE);
772*0Sstevel@tonic-gate 
773*0Sstevel@tonic-gate 	if (access(pname, R_OK) == 0) {
774*0Sstevel@tonic-gate 		(void) strlcpy(outfilename, pname, PATH_MAX);
775*0Sstevel@tonic-gate 		return (0);
776*0Sstevel@tonic-gate 	}
777*0Sstevel@tonic-gate 
778*0Sstevel@tonic-gate 	return (-1);
779*0Sstevel@tonic-gate }
780*0Sstevel@tonic-gate 
781*0Sstevel@tonic-gate void
env_picl_setup(void)782*0Sstevel@tonic-gate env_picl_setup(void)
783*0Sstevel@tonic-gate {
784*0Sstevel@tonic-gate 	int		err;
785*0Sstevel@tonic-gate 	sensor_node_t	*snodep;
786*0Sstevel@tonic-gate 	fan_node_t	*fnodep;
787*0Sstevel@tonic-gate 	picl_nodehdl_t	plath;
788*0Sstevel@tonic-gate 	char		fullfilename[PATH_MAX];
789*0Sstevel@tonic-gate 	picl_nodehdl_t	rooth;
790*0Sstevel@tonic-gate 
791*0Sstevel@tonic-gate 	/*
792*0Sstevel@tonic-gate 	 * Initialize sensorp and other fields in the sensor_nodes[] array
793*0Sstevel@tonic-gate 	 */
794*0Sstevel@tonic-gate 	for (snodep = sensor_nodes; snodep->sensor_name != NULL; snodep++) {
795*0Sstevel@tonic-gate 		snodep->sensorp = sensor_lookup(snodep->sensor_name);
796*0Sstevel@tonic-gate 		snodep->nodeh = NULL;
797*0Sstevel@tonic-gate 		snodep->proph = NULL;
798*0Sstevel@tonic-gate 		snodep->sdev_proph = NULL;
799*0Sstevel@tonic-gate 	}
800*0Sstevel@tonic-gate 
801*0Sstevel@tonic-gate 	/*
802*0Sstevel@tonic-gate 	 * Initialize fanp and other fields in the fan_nodes[] array
803*0Sstevel@tonic-gate 	 */
804*0Sstevel@tonic-gate 	for (fnodep = fan_nodes; fnodep->fan_name != NULL; fnodep++) {
805*0Sstevel@tonic-gate 		fnodep->fanp = fan_lookup(fnodep->fan_name);
806*0Sstevel@tonic-gate 		fnodep->nodeh = NULL;
807*0Sstevel@tonic-gate 		fnodep->proph = NULL;
808*0Sstevel@tonic-gate 	}
809*0Sstevel@tonic-gate 
810*0Sstevel@tonic-gate 	/*
811*0Sstevel@tonic-gate 	 * Get platform handle and populate PICL tree with environmental
812*0Sstevel@tonic-gate 	 * nodes and properties
813*0Sstevel@tonic-gate 	 */
814*0Sstevel@tonic-gate 	err = ptree_get_node_by_path("/platform", &plath);
815*0Sstevel@tonic-gate 
816*0Sstevel@tonic-gate 	if (err == PICL_SUCCESS) {
817*0Sstevel@tonic-gate 		err = add_sensor_nodes_and_props(plath);
818*0Sstevel@tonic-gate 		if (err == PICL_SUCCESS)
819*0Sstevel@tonic-gate 			err = add_fan_nodes_and_props(plath);
820*0Sstevel@tonic-gate 	}
821*0Sstevel@tonic-gate 
822*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
823*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
824*0Sstevel@tonic-gate 		return;
825*0Sstevel@tonic-gate 	}
826*0Sstevel@tonic-gate 
827*0Sstevel@tonic-gate 	/*
828*0Sstevel@tonic-gate 	 * Parse the envmodel.conf file and populate the PICL tree
829*0Sstevel@tonic-gate 	 */
830*0Sstevel@tonic-gate 	if (get_envmodel_conf_file(fullfilename) < 0)
831*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
832*0Sstevel@tonic-gate 	if (ptree_get_root(&rooth) != PICL_SUCCESS)
833*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
834*0Sstevel@tonic-gate 	err = picld_pluginutil_parse_config_file(rooth, fullfilename);
835*0Sstevel@tonic-gate 
836*0Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
837*0Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
838*0Sstevel@tonic-gate }
839