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