17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1999-2001 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This file contains code for setting up environmental related nodes
297c478bd9Sstevel@tonic-gate  * and properties in the PICL tree.
307c478bd9Sstevel@tonic-gate  *
317c478bd9Sstevel@tonic-gate  * For each temperature-device class node, it does the following:
327c478bd9Sstevel@tonic-gate  *	- Create cpu and cpu-ambient temperautre-sensor class nodes.
337c478bd9Sstevel@tonic-gate  *	- Create "devfs-path" property under each temperature-sensor class node
347c478bd9Sstevel@tonic-gate  *	- Create "Temperature" volatile property under these nodes.
357c478bd9Sstevel@tonic-gate  *	- Create various temperature threshold properties under each node.
367c478bd9Sstevel@tonic-gate  *	- Create "Temperature" and "AmbientTemperature" volatile properties
377c478bd9Sstevel@tonic-gate  *	  under corresponding "cpu" class node.
387c478bd9Sstevel@tonic-gate  *
397c478bd9Sstevel@tonic-gate  * For the "fan-control" node, it does the following:
407c478bd9Sstevel@tonic-gate  *	- Create system-fan node
417c478bd9Sstevel@tonic-gate  *	- Create "devfs-path" property under "fan" class node
427c478bd9Sstevel@tonic-gate  *	- Create "Speed" volatile propery under each node.
437c478bd9Sstevel@tonic-gate  *	- Create "SpeedUnit" property under each node.
447c478bd9Sstevel@tonic-gate  *
457c478bd9Sstevel@tonic-gate  * Access to sensor/fan properties is protected by the envpicl_rwlock
467c478bd9Sstevel@tonic-gate  * readers/writer lock. This lock is held as a reader while trying to
477c478bd9Sstevel@tonic-gate  * access any volatile sensor/fan property, and held as a writer lock
487c478bd9Sstevel@tonic-gate  * while trying to create or destroy sensor/fan nodes and properties.
497c478bd9Sstevel@tonic-gate  */
507c478bd9Sstevel@tonic-gate 
517c478bd9Sstevel@tonic-gate #include <stdio.h>
527c478bd9Sstevel@tonic-gate #include <fcntl.h>
537c478bd9Sstevel@tonic-gate #include <unistd.h>
547c478bd9Sstevel@tonic-gate #include <syslog.h>
557c478bd9Sstevel@tonic-gate #include <stdlib.h>
567c478bd9Sstevel@tonic-gate #include <limits.h>
577c478bd9Sstevel@tonic-gate #include <sys/open.h>
587c478bd9Sstevel@tonic-gate #include <ctype.h>
597c478bd9Sstevel@tonic-gate #include <string.h>
607c478bd9Sstevel@tonic-gate #include <alloca.h>
617c478bd9Sstevel@tonic-gate #include <libintl.h>
627c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
637c478bd9Sstevel@tonic-gate #include <picl.h>
647c478bd9Sstevel@tonic-gate #include <picltree.h>
657c478bd9Sstevel@tonic-gate #include <picld_pluginutil.h>
667c478bd9Sstevel@tonic-gate #include <pthread.h>
677c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
687c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
697c478bd9Sstevel@tonic-gate #include "picldefs.h"
707c478bd9Sstevel@tonic-gate #include "envd.h"
717c478bd9Sstevel@tonic-gate 
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * Volatile property read/write function typedef
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate typedef int ptree_vol_rdfunc_t(ptree_rarg_t *parg, void *buf);
777c478bd9Sstevel@tonic-gate typedef int ptree_vol_wrfunc_t(ptree_warg_t *parg, const void *buf);
787c478bd9Sstevel@tonic-gate 
797c478bd9Sstevel@tonic-gate #define	PROP_FAN_SPEED_UNIT_VALUE	"%"
807c478bd9Sstevel@tonic-gate 
817c478bd9Sstevel@tonic-gate /*
827c478bd9Sstevel@tonic-gate  * PICL class path for CPU nodes
837c478bd9Sstevel@tonic-gate  */
847c478bd9Sstevel@tonic-gate #define	CPU0_PLAT_PATH		"_class:/gptwo/cpu?ID=0"
857c478bd9Sstevel@tonic-gate #define	CPU1_PLAT_PATH		"_class:/gptwo/cpu?ID=1"
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * "UnitAddress" propval for various temperature devices (platform dependent)
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate #define	CPU0_TEMPDEV_UNITADDR	"0,30"
917c478bd9Sstevel@tonic-gate #define	CPU1_TEMPDEV_UNITADDR	"0,98"
927c478bd9Sstevel@tonic-gate 
937c478bd9Sstevel@tonic-gate /*
947c478bd9Sstevel@tonic-gate  * Sensor node data structure
957c478bd9Sstevel@tonic-gate  */
967c478bd9Sstevel@tonic-gate typedef struct {
977c478bd9Sstevel@tonic-gate 	char		*sensor_name;	/* sensor name */
987c478bd9Sstevel@tonic-gate 	env_sensor_t	*sensorp;	/* sensor info */
997c478bd9Sstevel@tonic-gate 	char		*unitaddr;	/* parent's UnitAddress propval */
1007c478bd9Sstevel@tonic-gate 	char		*sdev_node;	/* sensed device node name */
1017c478bd9Sstevel@tonic-gate 	char		*sdev_pname;	/* sensed device "temp" prop name */
1027c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;		/* sensor node handle */
1037c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;		/* "Temperature" property handle */
1047c478bd9Sstevel@tonic-gate 	picl_prophdl_t	target_proph;	/* "TargetTemp" property handle */
1057c478bd9Sstevel@tonic-gate 	picl_prophdl_t	sdev_proph;	/* property handle for sensed dev */
1067c478bd9Sstevel@tonic-gate } sensor_node_t;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 
1097c478bd9Sstevel@tonic-gate /*
1107c478bd9Sstevel@tonic-gate  * Sensor nodes array
1117c478bd9Sstevel@tonic-gate  */
1127c478bd9Sstevel@tonic-gate static sensor_node_t sensor_nodes[] = {
1137c478bd9Sstevel@tonic-gate 	{SENSOR_CPU0_DIE, NULL, CPU0_TEMPDEV_UNITADDR,
1147c478bd9Sstevel@tonic-gate 	    CPU0_PLAT_PATH, PICL_PROP_CPU_DIE_TEMP},
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate 	{SENSOR_CPU0_AMB, NULL, CPU0_TEMPDEV_UNITADDR,
1177c478bd9Sstevel@tonic-gate 	    CPU0_PLAT_PATH, PICL_PROP_CPU_AMB_TEMP},
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate 	{SENSOR_CPU1_DIE, NULL, CPU1_TEMPDEV_UNITADDR,
1207c478bd9Sstevel@tonic-gate 	    CPU1_PLAT_PATH, PICL_PROP_CPU_DIE_TEMP},
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate 	{SENSOR_CPU1_AMB, NULL, CPU1_TEMPDEV_UNITADDR,
1237c478bd9Sstevel@tonic-gate 	    CPU1_PLAT_PATH, PICL_PROP_CPU_AMB_TEMP},
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate 	{NULL, NULL, NULL, NULL, NULL}
1267c478bd9Sstevel@tonic-gate };
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate 
1297c478bd9Sstevel@tonic-gate /*
1307c478bd9Sstevel@tonic-gate  * Fan node data structure
1317c478bd9Sstevel@tonic-gate  */
1327c478bd9Sstevel@tonic-gate typedef struct {
1337c478bd9Sstevel@tonic-gate 	char		*fan_name;	/* fan name */
134*ada2da53SToomas Soome 	env_fan_t	*fanp;		/* fan information */
1357c478bd9Sstevel@tonic-gate 	char		*speed_unit;	/* speed unit string */
1367c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;		/* "fan" node handle */
1377c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;		/* "Speed" property handle */
1387c478bd9Sstevel@tonic-gate } fan_node_t;
1397c478bd9Sstevel@tonic-gate 
1407c478bd9Sstevel@tonic-gate 
1417c478bd9Sstevel@tonic-gate /*
1427c478bd9Sstevel@tonic-gate  * Fan node array
1437c478bd9Sstevel@tonic-gate  */
1447c478bd9Sstevel@tonic-gate static fan_node_t fan_nodes[] =  {
1457c478bd9Sstevel@tonic-gate 	{ENV_SYSTEM_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE},
1467c478bd9Sstevel@tonic-gate 	{ENV_CPU_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE},
1477c478bd9Sstevel@tonic-gate 	{ENV_PSUPPLY_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE},
1487c478bd9Sstevel@tonic-gate 	{NULL, NULL, NULL}
1497c478bd9Sstevel@tonic-gate };
1507c478bd9Sstevel@tonic-gate 
1517c478bd9Sstevel@tonic-gate 
1527c478bd9Sstevel@tonic-gate /*
1537c478bd9Sstevel@tonic-gate  * Miscellaneous declarations
1547c478bd9Sstevel@tonic-gate  */
1557c478bd9Sstevel@tonic-gate typedef struct node_list {
1567c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		nodeh;
1577c478bd9Sstevel@tonic-gate 	struct node_list	*next;
1587c478bd9Sstevel@tonic-gate } node_list_t;
1597c478bd9Sstevel@tonic-gate 
1607c478bd9Sstevel@tonic-gate static void delete_sensor_nodes_and_props(void);
1617c478bd9Sstevel@tonic-gate static void delete_fan_nodes_and_props(void);
1627c478bd9Sstevel@tonic-gate static pthread_rwlock_t	envpicl_rwlock = PTHREAD_RWLOCK_INITIALIZER;
1637c478bd9Sstevel@tonic-gate 
1647c478bd9Sstevel@tonic-gate 
1657c478bd9Sstevel@tonic-gate /*
1667c478bd9Sstevel@tonic-gate  * Read function for volatile "Temperature" property
1677c478bd9Sstevel@tonic-gate  */
1687c478bd9Sstevel@tonic-gate static int
get_current_target_temp(ptree_rarg_t * parg,void * buf)1697c478bd9Sstevel@tonic-gate get_current_target_temp(ptree_rarg_t *parg, void *buf)
1707c478bd9Sstevel@tonic-gate {
1717c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;
1727c478bd9Sstevel@tonic-gate 	sensor_node_t	*snodep;
1737c478bd9Sstevel@tonic-gate 	env_sensor_t	*sensorp;
1747c478bd9Sstevel@tonic-gate 
1757c478bd9Sstevel@tonic-gate 	/*
1767c478bd9Sstevel@tonic-gate 	 * Locate the sensor in our sensor_nodes table by matching the
1777c478bd9Sstevel@tonic-gate 	 * property handle and get its temperature.
1787c478bd9Sstevel@tonic-gate 	 */
1797c478bd9Sstevel@tonic-gate 	proph = parg->proph;
1807c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_rdlock(&envpicl_rwlock);
1817c478bd9Sstevel@tonic-gate 	for (snodep = &sensor_nodes[0]; snodep->sensor_name != NULL;
1827c478bd9Sstevel@tonic-gate 	    snodep++) {
1837c478bd9Sstevel@tonic-gate 		if (snodep->target_proph != proph)
1847c478bd9Sstevel@tonic-gate 			continue;
1857c478bd9Sstevel@tonic-gate 
1867c478bd9Sstevel@tonic-gate 		if ((sensorp = snodep->sensorp) == NULL)
1877c478bd9Sstevel@tonic-gate 			break;
1887c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&sensorp->target_temp,
1897c478bd9Sstevel@tonic-gate 		    sizeof (sensorp->target_temp));
1907c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&envpicl_rwlock);
1917c478bd9Sstevel@tonic-gate 		return (PICL_SUCCESS);
1927c478bd9Sstevel@tonic-gate 	}
1937c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&envpicl_rwlock);
1947c478bd9Sstevel@tonic-gate 	return (PICL_FAILURE);
1957c478bd9Sstevel@tonic-gate }
1967c478bd9Sstevel@tonic-gate 
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate /*
1997c478bd9Sstevel@tonic-gate  * Read function for volatile "Temperature" property
2007c478bd9Sstevel@tonic-gate  */
2017c478bd9Sstevel@tonic-gate static int
get_current_temp(ptree_rarg_t * parg,void * buf)2027c478bd9Sstevel@tonic-gate get_current_temp(ptree_rarg_t *parg, void *buf)
2037c478bd9Sstevel@tonic-gate {
204*ada2da53SToomas Soome 	tempr_t		temp;
2057c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;
2067c478bd9Sstevel@tonic-gate 	sensor_node_t	*snodep;
2077c478bd9Sstevel@tonic-gate 
2087c478bd9Sstevel@tonic-gate 	/*
2097c478bd9Sstevel@tonic-gate 	 * Locate the sensor in our sensor_nodes table by matching the
2107c478bd9Sstevel@tonic-gate 	 * property handle and get its temperature.
2117c478bd9Sstevel@tonic-gate 	 */
2127c478bd9Sstevel@tonic-gate 	proph = parg->proph;
2137c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_rdlock(&envpicl_rwlock);
2147c478bd9Sstevel@tonic-gate 	for (snodep = &sensor_nodes[0]; snodep->sensor_name != NULL;
2157c478bd9Sstevel@tonic-gate 	    snodep++) {
2167c478bd9Sstevel@tonic-gate 		if (snodep->proph != proph &&
2177c478bd9Sstevel@tonic-gate 		    snodep->sdev_proph != proph)
2187c478bd9Sstevel@tonic-gate 			continue;
2197c478bd9Sstevel@tonic-gate 
2207c478bd9Sstevel@tonic-gate 		if (get_temperature(snodep->sensorp, &temp) < 0)
2217c478bd9Sstevel@tonic-gate 			break;
2227c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&temp, sizeof (tempr_t));
2237c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&envpicl_rwlock);
2247c478bd9Sstevel@tonic-gate 		return (PICL_SUCCESS);
2257c478bd9Sstevel@tonic-gate 	}
2267c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&envpicl_rwlock);
2277c478bd9Sstevel@tonic-gate 	return (PICL_FAILURE);
2287c478bd9Sstevel@tonic-gate }
2297c478bd9Sstevel@tonic-gate 
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate /*
2327c478bd9Sstevel@tonic-gate  * Read function for volatile "Speed" property on "fan" class node
2337c478bd9Sstevel@tonic-gate  */
2347c478bd9Sstevel@tonic-gate static int
get_current_speed(ptree_rarg_t * parg,void * buf)2357c478bd9Sstevel@tonic-gate get_current_speed(ptree_rarg_t *parg, void *buf)
2367c478bd9Sstevel@tonic-gate {
2377c478bd9Sstevel@tonic-gate 	fanspeed_t	speed;
2387c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;
2397c478bd9Sstevel@tonic-gate 	fan_node_t	*fnodep;
2407c478bd9Sstevel@tonic-gate 
2417c478bd9Sstevel@tonic-gate 	/*
2427c478bd9Sstevel@tonic-gate 	 * Locate the fan in our fan_nodes table by matching the
2437c478bd9Sstevel@tonic-gate 	 * property handle and get fan speed.
2447c478bd9Sstevel@tonic-gate 	 */
2457c478bd9Sstevel@tonic-gate 	proph = parg->proph;
2467c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_rdlock(&envpicl_rwlock);
2477c478bd9Sstevel@tonic-gate 	for (fnodep = &fan_nodes[0]; fnodep->fan_name != NULL; fnodep++) {
2487c478bd9Sstevel@tonic-gate 		if (fnodep->proph != proph)
2497c478bd9Sstevel@tonic-gate 			continue;
2507c478bd9Sstevel@tonic-gate 		if (get_fan_speed(fnodep->fanp, &speed) < 0)
2517c478bd9Sstevel@tonic-gate 			break;
2527c478bd9Sstevel@tonic-gate 		speed = (fanspeed_t)(speed * 100/fnodep->fanp->speed_max);
2537c478bd9Sstevel@tonic-gate 
2547c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&speed, sizeof (speed));
2557c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&envpicl_rwlock);
2567c478bd9Sstevel@tonic-gate 		return (PICL_SUCCESS);
2577c478bd9Sstevel@tonic-gate 	}
2587c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&envpicl_rwlock);
2597c478bd9Sstevel@tonic-gate 	return (PICL_FAILURE);
2607c478bd9Sstevel@tonic-gate }
2617c478bd9Sstevel@tonic-gate 
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate static node_list_t *
add_node_to_list(picl_nodehdl_t nodeh,node_list_t * listp)2647c478bd9Sstevel@tonic-gate add_node_to_list(picl_nodehdl_t nodeh, node_list_t *listp)
2657c478bd9Sstevel@tonic-gate {
2667c478bd9Sstevel@tonic-gate 	node_list_t	*el;
2677c478bd9Sstevel@tonic-gate 	node_list_t	*tmp;
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate 	el = malloc(sizeof (node_list_t));
2707c478bd9Sstevel@tonic-gate 	if (el == NULL)
2717c478bd9Sstevel@tonic-gate 		return (listp);
2727c478bd9Sstevel@tonic-gate 	el->nodeh = nodeh;
2737c478bd9Sstevel@tonic-gate 	el->next = NULL;
2747c478bd9Sstevel@tonic-gate 	if (listp == NULL) {
2757c478bd9Sstevel@tonic-gate 		listp = el;
2767c478bd9Sstevel@tonic-gate 		return (listp);
2777c478bd9Sstevel@tonic-gate 	}
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	/*
2807c478bd9Sstevel@tonic-gate 	 * append to the end to preserve the order found
2817c478bd9Sstevel@tonic-gate 	 */
2827c478bd9Sstevel@tonic-gate 	tmp = listp;
2837c478bd9Sstevel@tonic-gate 	while (tmp->next != NULL)
2847c478bd9Sstevel@tonic-gate 		tmp = tmp->next;
2857c478bd9Sstevel@tonic-gate 
2867c478bd9Sstevel@tonic-gate 	tmp->next = el;
2877c478bd9Sstevel@tonic-gate 	return (listp);
2887c478bd9Sstevel@tonic-gate }
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate /*
2937c478bd9Sstevel@tonic-gate  * Get a list of nodes of the specified classname under nodeh
2947c478bd9Sstevel@tonic-gate  * Once a node of the specified class is found, it's children are not
2957c478bd9Sstevel@tonic-gate  * searched.
2967c478bd9Sstevel@tonic-gate  */
2977c478bd9Sstevel@tonic-gate static node_list_t *
get_node_list_by_class(picl_nodehdl_t nodeh,const char * classname,node_list_t * listp)2987c478bd9Sstevel@tonic-gate get_node_list_by_class(picl_nodehdl_t nodeh, const char *classname,
299*ada2da53SToomas Soome     node_list_t *listp)
3007c478bd9Sstevel@tonic-gate {
3017c478bd9Sstevel@tonic-gate 	int		err;
3027c478bd9Sstevel@tonic-gate 	char		clname[PICL_CLASSNAMELEN_MAX+1];
3037c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	chdh;
3047c478bd9Sstevel@tonic-gate 
3057c478bd9Sstevel@tonic-gate 	/*
3067c478bd9Sstevel@tonic-gate 	 * go through the children
3077c478bd9Sstevel@tonic-gate 	 */
3087c478bd9Sstevel@tonic-gate 	err = ptree_get_propval_by_name(nodeh, PICL_PROP_CHILD, &chdh,
3097c478bd9Sstevel@tonic-gate 	    sizeof (picl_nodehdl_t));
3107c478bd9Sstevel@tonic-gate 
3117c478bd9Sstevel@tonic-gate 	while (err == PICL_SUCCESS) {
3127c478bd9Sstevel@tonic-gate 		err = ptree_get_propval_by_name(chdh, PICL_PROP_CLASSNAME,
3137c478bd9Sstevel@tonic-gate 		    clname, strlen(classname) + 1);
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 		if ((err == PICL_SUCCESS) && (strcmp(clname, classname) == 0))
3167c478bd9Sstevel@tonic-gate 			listp = add_node_to_list(chdh, listp);
3177c478bd9Sstevel@tonic-gate 		else
3187c478bd9Sstevel@tonic-gate 			listp = get_node_list_by_class(chdh, classname, listp);
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 		err = ptree_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
3217c478bd9Sstevel@tonic-gate 		    sizeof (picl_nodehdl_t));
3227c478bd9Sstevel@tonic-gate 	}
3237c478bd9Sstevel@tonic-gate 	return (listp);
3247c478bd9Sstevel@tonic-gate }
3257c478bd9Sstevel@tonic-gate 
3267c478bd9Sstevel@tonic-gate 
3277c478bd9Sstevel@tonic-gate /*
3287c478bd9Sstevel@tonic-gate  * Free memory allocated to build the specified node list.
3297c478bd9Sstevel@tonic-gate  */
3307c478bd9Sstevel@tonic-gate static void
free_node_list(node_list_t * listp)3317c478bd9Sstevel@tonic-gate free_node_list(node_list_t *listp)
3327c478bd9Sstevel@tonic-gate {
3337c478bd9Sstevel@tonic-gate 	node_list_t	*next;
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	for (; listp != NULL; listp = next) {
3367c478bd9Sstevel@tonic-gate 		next = listp->next;
3377c478bd9Sstevel@tonic-gate 		free(listp);
3387c478bd9Sstevel@tonic-gate 	}
3397c478bd9Sstevel@tonic-gate }
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate /*
3427c478bd9Sstevel@tonic-gate  * Get PICL_PTYPE_CHARSTRING "UnitAddress" property
3437c478bd9Sstevel@tonic-gate  */
3447c478bd9Sstevel@tonic-gate static int
get_unit_address_prop(picl_nodehdl_t nodeh,void * buf,size_t len)3457c478bd9Sstevel@tonic-gate get_unit_address_prop(picl_nodehdl_t nodeh, void *buf, size_t len)
3467c478bd9Sstevel@tonic-gate {
3477c478bd9Sstevel@tonic-gate 	int			err;
3487c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
3497c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
3507c478bd9Sstevel@tonic-gate 
3517c478bd9Sstevel@tonic-gate 	err = ptree_get_prop_by_name(nodeh, PICL_PROP_UNIT_ADDRESS, &proph);
3527c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS)
3537c478bd9Sstevel@tonic-gate 		err = ptree_get_propinfo(proph, &pinfo);
3547c478bd9Sstevel@tonic-gate 
3557c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
3567c478bd9Sstevel@tonic-gate 		return (err);
3577c478bd9Sstevel@tonic-gate 
3587c478bd9Sstevel@tonic-gate 	if (pinfo.piclinfo.type != PICL_PTYPE_CHARSTRING ||
3597c478bd9Sstevel@tonic-gate 	    pinfo.piclinfo.size > len)
3607c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
3617c478bd9Sstevel@tonic-gate 
3627c478bd9Sstevel@tonic-gate 	err = ptree_get_propval(proph, buf, pinfo.piclinfo.size);
3637c478bd9Sstevel@tonic-gate 	return (err);
3647c478bd9Sstevel@tonic-gate }
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate /*
3687c478bd9Sstevel@tonic-gate  * Create and add the specified regular property
3697c478bd9Sstevel@tonic-gate  */
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@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)3727c478bd9Sstevel@tonic-gate add_regular_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
3737c478bd9Sstevel@tonic-gate     int size, void *valbuf, picl_prophdl_t *prophp)
3747c478bd9Sstevel@tonic-gate {
3757c478bd9Sstevel@tonic-gate 	int			err;
3767c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
3777c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
3807c478bd9Sstevel@tonic-gate 	    type, access, size, name, NULL, NULL);
3817c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
3827c478bd9Sstevel@tonic-gate 		return (err);
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(nodeh, &propinfo, valbuf, &proph);
3857c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS && prophp)
3867c478bd9Sstevel@tonic-gate 		*prophp = proph;
3877c478bd9Sstevel@tonic-gate 	return (err);
3887c478bd9Sstevel@tonic-gate }
3897c478bd9Sstevel@tonic-gate 
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * Create and add the specified volatile property
3937c478bd9Sstevel@tonic-gate  */
3947c478bd9Sstevel@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)3957c478bd9Sstevel@tonic-gate add_volatile_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
3967c478bd9Sstevel@tonic-gate     int size, ptree_vol_rdfunc_t *rdfunc, ptree_vol_wrfunc_t *wrfunc,
3977c478bd9Sstevel@tonic-gate     picl_prophdl_t *prophp)
3987c478bd9Sstevel@tonic-gate {
3997c478bd9Sstevel@tonic-gate 	int			err;
4007c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
4017c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
4027c478bd9Sstevel@tonic-gate 
4037c478bd9Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
4047c478bd9Sstevel@tonic-gate 	    type, (access|PICL_VOLATILE), size, name, rdfunc, wrfunc);
4057c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
4067c478bd9Sstevel@tonic-gate 		return (err);
4077c478bd9Sstevel@tonic-gate 
4087c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(nodeh, &propinfo, NULL, &proph);
4097c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS && prophp)
4107c478bd9Sstevel@tonic-gate 		*prophp = proph;
4117c478bd9Sstevel@tonic-gate 	return (err);
4127c478bd9Sstevel@tonic-gate }
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate /*
4157c478bd9Sstevel@tonic-gate  * Add temperature threshold properties
4167c478bd9Sstevel@tonic-gate  */
4177c478bd9Sstevel@tonic-gate static void
add_sensor_thresh_props(picl_nodehdl_t nodeh,sensor_thresh_t * threshp)4187c478bd9Sstevel@tonic-gate add_sensor_thresh_props(picl_nodehdl_t nodeh, sensor_thresh_t *threshp)
4197c478bd9Sstevel@tonic-gate {
4207c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_LOW_POWER_OFF,
4237c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4247c478bd9Sstevel@tonic-gate 	    sizeof (threshp->low_power_off),
4257c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->low_power_off), &proph);
4267c478bd9Sstevel@tonic-gate 
4277c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_LOW_SHUTDOWN,
4287c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4297c478bd9Sstevel@tonic-gate 	    sizeof (threshp->low_shutdown),
4307c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->low_shutdown), &proph);
4317c478bd9Sstevel@tonic-gate 
4327c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_LOW_WARNING,
4337c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4347c478bd9Sstevel@tonic-gate 	    sizeof (threshp->low_warning),
4357c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->low_warning), &proph);
4367c478bd9Sstevel@tonic-gate 
4377c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_HIGH_WARNING,
4387c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4397c478bd9Sstevel@tonic-gate 	    sizeof (threshp->high_warning),
4407c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->high_warning), &proph);
4417c478bd9Sstevel@tonic-gate 
4427c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_HIGH_SHUTDOWN,
4437c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4447c478bd9Sstevel@tonic-gate 	    sizeof (threshp->high_shutdown),
4457c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->high_shutdown), &proph);
4467c478bd9Sstevel@tonic-gate 
4477c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PICL_PROP_HIGH_POWER_OFF,
4487c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4497c478bd9Sstevel@tonic-gate 	    sizeof (threshp->high_power_off),
4507c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->high_power_off), &proph);
4517c478bd9Sstevel@tonic-gate }
4527c478bd9Sstevel@tonic-gate 
4537c478bd9Sstevel@tonic-gate 
4547c478bd9Sstevel@tonic-gate /*
4557c478bd9Sstevel@tonic-gate  * Lookup "temperature-device" class nodes and create "temperature-sensor"
4567c478bd9Sstevel@tonic-gate  * class nodes and relevant properties under those nodes.
4577c478bd9Sstevel@tonic-gate  *
4587c478bd9Sstevel@tonic-gate  * For each entry in sensor_nodes[] array, do the following:
4597c478bd9Sstevel@tonic-gate  *	- Create specified (cpu-die or cpu-ambient) "temperautre-sensor" class
4607c478bd9Sstevel@tonic-gate  *	  node.
4617c478bd9Sstevel@tonic-gate  *	- Create "devfs-path" property under this node.
4627c478bd9Sstevel@tonic-gate  *	- Create "Temperature" volatile property under this node.
4637c478bd9Sstevel@tonic-gate  *	- Create various temperature threshold properties under this node.
4647c478bd9Sstevel@tonic-gate  *	- Create specified ("Temperature" or "AmbientTemperature") volatile
4657c478bd9Sstevel@tonic-gate  *	  temperature property under specified sdev_node node.
4667c478bd9Sstevel@tonic-gate  */
4677c478bd9Sstevel@tonic-gate 
4687c478bd9Sstevel@tonic-gate static int
add_sensor_nodes_and_props(picl_nodehdl_t plath)4697c478bd9Sstevel@tonic-gate add_sensor_nodes_and_props(picl_nodehdl_t plath)
4707c478bd9Sstevel@tonic-gate {
4717c478bd9Sstevel@tonic-gate 	int		err;
4727c478bd9Sstevel@tonic-gate 	char		*pname, *nodename, *refnode, *devfs_path;
4737c478bd9Sstevel@tonic-gate 	node_list_t	*node_list, *listp;
4747c478bd9Sstevel@tonic-gate 	sensor_node_t	*snodep;
4757c478bd9Sstevel@tonic-gate 	sensor_thresh_t *threshp;
4767c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	nodeh, refnodeh, cnodeh;
4777c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;
4787c478bd9Sstevel@tonic-gate 	char		unitaddr[PICL_UNITADDR_LEN_MAX];
4797c478bd9Sstevel@tonic-gate 	env_sensor_t	*sensorp;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	node_list =
4827c478bd9Sstevel@tonic-gate 	    get_node_list_by_class(plath, PICL_CLASS_TEMPERATURE_DEVICE, NULL);
4837c478bd9Sstevel@tonic-gate 
4847c478bd9Sstevel@tonic-gate 	if (node_list == NULL)
4857c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
4867c478bd9Sstevel@tonic-gate 
4877c478bd9Sstevel@tonic-gate 	for (listp = node_list; listp != NULL; listp = listp->next) {
4887c478bd9Sstevel@tonic-gate 		/*
4897c478bd9Sstevel@tonic-gate 		 * Get "reg" property. Skip if no "reg" property found.
4907c478bd9Sstevel@tonic-gate 		 */
4917c478bd9Sstevel@tonic-gate 		nodeh = listp->nodeh;
4927c478bd9Sstevel@tonic-gate 		err = get_unit_address_prop(nodeh, (void *)unitaddr,
4937c478bd9Sstevel@tonic-gate 		    sizeof (unitaddr));
4947c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
4957c478bd9Sstevel@tonic-gate 			continue;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 		for (snodep = sensor_nodes; snodep->sensor_name != NULL;
4987c478bd9Sstevel@tonic-gate 		    snodep++) {
4997c478bd9Sstevel@tonic-gate 
5007c478bd9Sstevel@tonic-gate 			/* Match "UnitAddress" property */
5017c478bd9Sstevel@tonic-gate 			if (strcasecmp(unitaddr, snodep->unitaddr) != 0)
5027c478bd9Sstevel@tonic-gate 				continue;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 			/*
5057c478bd9Sstevel@tonic-gate 			 * Skip if already initialized or no sensor info
5067c478bd9Sstevel@tonic-gate 			 */
5077c478bd9Sstevel@tonic-gate 			sensorp = snodep->sensorp;
508*ada2da53SToomas Soome 			if (snodep->nodeh != 0 || sensorp == NULL)
5097c478bd9Sstevel@tonic-gate 				continue;
5107c478bd9Sstevel@tonic-gate 
5117c478bd9Sstevel@tonic-gate 			/*
5127c478bd9Sstevel@tonic-gate 			 * Create temperature-sensor node
5137c478bd9Sstevel@tonic-gate 			 */
5147c478bd9Sstevel@tonic-gate 			nodename = snodep->sensor_name;
5157c478bd9Sstevel@tonic-gate 			err = ptree_create_and_add_node(nodeh, nodename,
5167c478bd9Sstevel@tonic-gate 			    PICL_CLASS_TEMPERATURE_SENSOR, &cnodeh);
5177c478bd9Sstevel@tonic-gate 			if (env_debug)
5187c478bd9Sstevel@tonic-gate 				envd_log(LOG_INFO,
5197c478bd9Sstevel@tonic-gate 				    "Creating PICL sensor node '%s' err:%d\n",
5207c478bd9Sstevel@tonic-gate 				    nodename, err);
5217c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
5227c478bd9Sstevel@tonic-gate 				break;
5237c478bd9Sstevel@tonic-gate 
5247c478bd9Sstevel@tonic-gate 			/* save node handle */
5257c478bd9Sstevel@tonic-gate 			snodep->nodeh = cnodeh;
5267c478bd9Sstevel@tonic-gate 
5277c478bd9Sstevel@tonic-gate 			/*
5287c478bd9Sstevel@tonic-gate 			 * Add "devfs_path" property in child node
5297c478bd9Sstevel@tonic-gate 			 */
5307c478bd9Sstevel@tonic-gate 			devfs_path = sensorp->devfs_path;
5317c478bd9Sstevel@tonic-gate 			pname = PICL_PROP_DEVFS_PATH;
5327c478bd9Sstevel@tonic-gate 			err = add_regular_prop(cnodeh, pname,
5337c478bd9Sstevel@tonic-gate 			    PICL_PTYPE_CHARSTRING, PICL_READ,
5347c478bd9Sstevel@tonic-gate 			    strlen(devfs_path)+1, (void *)devfs_path, &proph);
5357c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
5367c478bd9Sstevel@tonic-gate 				break;
5377c478bd9Sstevel@tonic-gate 
5387c478bd9Sstevel@tonic-gate 			/*
5397c478bd9Sstevel@tonic-gate 			 * Now add volatile "temperature" volatile property
5407c478bd9Sstevel@tonic-gate 			 * in this "temperature-sensor" class node.
5417c478bd9Sstevel@tonic-gate 			 */
5427c478bd9Sstevel@tonic-gate 			pname = PICL_PROP_TEMPERATURE;
5437c478bd9Sstevel@tonic-gate 			err = add_volatile_prop(cnodeh, pname,
5447c478bd9Sstevel@tonic-gate 			    PICL_PTYPE_INT, PICL_READ, sizeof (tempr_t),
5457c478bd9Sstevel@tonic-gate 			    get_current_temp, NULL, &proph);
5467c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
5477c478bd9Sstevel@tonic-gate 				break;
5487c478bd9Sstevel@tonic-gate 
5497c478bd9Sstevel@tonic-gate 			/* Save prop handle */
5507c478bd9Sstevel@tonic-gate 			snodep->proph = proph;
5517c478bd9Sstevel@tonic-gate 
5527c478bd9Sstevel@tonic-gate 			/*
5537c478bd9Sstevel@tonic-gate 			 * Add threshold related properties
5547c478bd9Sstevel@tonic-gate 			 */
5557c478bd9Sstevel@tonic-gate 			threshp = sensorp->temp_thresh;
5567c478bd9Sstevel@tonic-gate 			if (threshp && threshp->policy_type ==
5577c478bd9Sstevel@tonic-gate 			    POLICY_TARGET_TEMP) {
5587c478bd9Sstevel@tonic-gate 				/*
5597c478bd9Sstevel@tonic-gate 				 * Add volatile "TargetTemperature" property
5607c478bd9Sstevel@tonic-gate 				 */
5617c478bd9Sstevel@tonic-gate 				pname = PICL_PROP_TARGET_TEMPERATURE;
5627c478bd9Sstevel@tonic-gate 				err = add_volatile_prop(cnodeh, pname,
5637c478bd9Sstevel@tonic-gate 				    PICL_PTYPE_INT, PICL_READ,
5647c478bd9Sstevel@tonic-gate 				    sizeof (sensorp->target_temp),
5657c478bd9Sstevel@tonic-gate 				    get_current_target_temp, NULL, &proph);
5667c478bd9Sstevel@tonic-gate 				if (err != PICL_SUCCESS)
5677c478bd9Sstevel@tonic-gate 					break;
5687c478bd9Sstevel@tonic-gate 				snodep->target_proph = proph;
5697c478bd9Sstevel@tonic-gate 			}
5707c478bd9Sstevel@tonic-gate 
5717c478bd9Sstevel@tonic-gate 			if (threshp != NULL)
5727c478bd9Sstevel@tonic-gate 				add_sensor_thresh_props(cnodeh, threshp);
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate 			/*
5757c478bd9Sstevel@tonic-gate 			 * Finally create property in the sensed device
5767c478bd9Sstevel@tonic-gate 			 * (if one specified)
5777c478bd9Sstevel@tonic-gate 			 */
5787c478bd9Sstevel@tonic-gate 			refnode =  snodep->sdev_node;
5797c478bd9Sstevel@tonic-gate 			pname =  snodep->sdev_pname;
5807c478bd9Sstevel@tonic-gate 			if (refnode == NULL || pname == NULL)
5817c478bd9Sstevel@tonic-gate 				continue;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 			err = ptree_get_node_by_path(refnode, &refnodeh);
5847c478bd9Sstevel@tonic-gate 			if (err == PICL_SUCCESS) {
5857c478bd9Sstevel@tonic-gate 				err = add_volatile_prop(refnodeh, pname,
5867c478bd9Sstevel@tonic-gate 				    PICL_PTYPE_INT, PICL_READ,
5877c478bd9Sstevel@tonic-gate 				    sizeof (tempr_t), get_current_temp,
5887c478bd9Sstevel@tonic-gate 				    NULL, &proph);
5897c478bd9Sstevel@tonic-gate 			}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
5927c478bd9Sstevel@tonic-gate 				break;
5937c478bd9Sstevel@tonic-gate 
5947c478bd9Sstevel@tonic-gate 			/* Save prop handle */
5957c478bd9Sstevel@tonic-gate 			snodep->sdev_proph = proph;
5967c478bd9Sstevel@tonic-gate 		}
5977c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
5987c478bd9Sstevel@tonic-gate 			delete_sensor_nodes_and_props();
5997c478bd9Sstevel@tonic-gate 			free_node_list(node_list);
6007c478bd9Sstevel@tonic-gate 			if (env_debug)
6017c478bd9Sstevel@tonic-gate 				envd_log(LOG_INFO,
6027c478bd9Sstevel@tonic-gate 				    "Can't create prop/node for sensor '%s'\n",
6037c478bd9Sstevel@tonic-gate 				    nodename);
6047c478bd9Sstevel@tonic-gate 			return (err);
6057c478bd9Sstevel@tonic-gate 		}
6067c478bd9Sstevel@tonic-gate 	}
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate 	free_node_list(node_list);
6097c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
6107c478bd9Sstevel@tonic-gate }
6117c478bd9Sstevel@tonic-gate 
6127c478bd9Sstevel@tonic-gate /*
6137c478bd9Sstevel@tonic-gate  * Delete all sensor nodes and related properties created by the
6147c478bd9Sstevel@tonic-gate  * add_sensor_prop() for each sensor node in the PICL tree.
6157c478bd9Sstevel@tonic-gate  */
6167c478bd9Sstevel@tonic-gate static void
delete_sensor_nodes_and_props(void)6177c478bd9Sstevel@tonic-gate delete_sensor_nodes_and_props(void)
6187c478bd9Sstevel@tonic-gate {
6197c478bd9Sstevel@tonic-gate 	sensor_node_t	*snodep;
6207c478bd9Sstevel@tonic-gate 
6217c478bd9Sstevel@tonic-gate 	/*
6227c478bd9Sstevel@tonic-gate 	 * Delete/destroy any property created in the sensed device
6237c478bd9Sstevel@tonic-gate 	 * as well as the sensor node and all properties under it.
6247c478bd9Sstevel@tonic-gate 	 * Note that deleiing/destroying a node deletes/destroys
6257c478bd9Sstevel@tonic-gate 	 * all properties within that node.
6267c478bd9Sstevel@tonic-gate 	 */
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	for (snodep = sensor_nodes; snodep->sensor_name != NULL; snodep++) {
629*ada2da53SToomas Soome 		if (snodep->sdev_proph != 0) {
6307c478bd9Sstevel@tonic-gate 			(void) ptree_delete_prop(snodep->sdev_proph);
6317c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_prop(snodep->sdev_proph);
632*ada2da53SToomas Soome 			snodep->sdev_proph = 0;
6337c478bd9Sstevel@tonic-gate 		}
6347c478bd9Sstevel@tonic-gate 
635*ada2da53SToomas Soome 		if (snodep->nodeh != 0) {
6367c478bd9Sstevel@tonic-gate 			/* delete node and all properties under it */
6377c478bd9Sstevel@tonic-gate 			(void) ptree_delete_node(snodep->nodeh);
6387c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_node(snodep->nodeh);
639*ada2da53SToomas Soome 			snodep->nodeh = 0;
640*ada2da53SToomas Soome 			snodep->proph = 0;
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate }
6447c478bd9Sstevel@tonic-gate 
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate /*
6477c478bd9Sstevel@tonic-gate  * Lookup "fan-control" class node and create "fan" class nodes and
6487c478bd9Sstevel@tonic-gate  * relevant properties under those nodes.
6497c478bd9Sstevel@tonic-gate  *
6507c478bd9Sstevel@tonic-gate  * For each entry in fan_nodes[] array, do the following:
6517c478bd9Sstevel@tonic-gate  *	- Create specified "fan" class node.
6527c478bd9Sstevel@tonic-gate  *	- Create "devfs-path" property under "fan" class node
6537c478bd9Sstevel@tonic-gate  *	- Create "Speed" volatile propery under "fan" class node.
6547c478bd9Sstevel@tonic-gate  *	- Create "SpeedUnit" property under "fan" class node.
6557c478bd9Sstevel@tonic-gate  */
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate static int
add_fan_nodes_and_props(picl_nodehdl_t plath)6587c478bd9Sstevel@tonic-gate add_fan_nodes_and_props(picl_nodehdl_t plath)
6597c478bd9Sstevel@tonic-gate {
6607c478bd9Sstevel@tonic-gate 	int		err;
6617c478bd9Sstevel@tonic-gate 	char		*pname, *nodename, *devfs_path;
6627c478bd9Sstevel@tonic-gate 	env_fan_t	*fanp;
6637c478bd9Sstevel@tonic-gate 	fan_node_t	*fnodep;
6647c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	nodeh, cnodeh;
6657c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;
6667c478bd9Sstevel@tonic-gate 	node_list_t	*node_list, *listp;
6677c478bd9Sstevel@tonic-gate 
6687c478bd9Sstevel@tonic-gate 	node_list =
6697c478bd9Sstevel@tonic-gate 	    get_node_list_by_class(plath, PICL_CLASS_FAN_CONTROL, NULL);
6707c478bd9Sstevel@tonic-gate 
6717c478bd9Sstevel@tonic-gate 	if (node_list == NULL)
6727c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
6737c478bd9Sstevel@tonic-gate 
6747c478bd9Sstevel@tonic-gate 	for (listp = node_list; listp != NULL; listp = listp->next) {
6757c478bd9Sstevel@tonic-gate 		/*
6767c478bd9Sstevel@tonic-gate 		 * Add various fan nodes and properties
6777c478bd9Sstevel@tonic-gate 		 */
6787c478bd9Sstevel@tonic-gate 		nodeh = listp->nodeh;
6797c478bd9Sstevel@tonic-gate 		err = PICL_SUCCESS;
6807c478bd9Sstevel@tonic-gate 		for (fnodep = fan_nodes; fnodep->fan_name != NULL; fnodep++) {
6817c478bd9Sstevel@tonic-gate 
6827c478bd9Sstevel@tonic-gate 			/* Skip if already initialized or no fan info */
683*ada2da53SToomas Soome 			if (fnodep->nodeh != 0 || fnodep->fanp == NULL)
6847c478bd9Sstevel@tonic-gate 				continue;
6857c478bd9Sstevel@tonic-gate 
6867c478bd9Sstevel@tonic-gate 			/*
6877c478bd9Sstevel@tonic-gate 			 * Create "fan" class node and save node handle
6887c478bd9Sstevel@tonic-gate 			 */
6897c478bd9Sstevel@tonic-gate 			nodename = fnodep->fan_name;
6907c478bd9Sstevel@tonic-gate 			err = ptree_create_and_add_node(nodeh, nodename,
6917c478bd9Sstevel@tonic-gate 			    PICL_CLASS_FAN, &cnodeh);
6927c478bd9Sstevel@tonic-gate 			if (env_debug)
6937c478bd9Sstevel@tonic-gate 				envd_log(LOG_INFO,
6947c478bd9Sstevel@tonic-gate 				    "Creating PICL fan node '%s' err:%d\n",
6957c478bd9Sstevel@tonic-gate 				    nodename, err);
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
6987c478bd9Sstevel@tonic-gate 				break;
6997c478bd9Sstevel@tonic-gate 			fnodep->nodeh = cnodeh;
7007c478bd9Sstevel@tonic-gate 
7017c478bd9Sstevel@tonic-gate 			/*
7027c478bd9Sstevel@tonic-gate 			 * Add "devfs_path" property in child node
7037c478bd9Sstevel@tonic-gate 			 */
7047c478bd9Sstevel@tonic-gate 			fanp = fnodep->fanp;
7057c478bd9Sstevel@tonic-gate 			devfs_path  = fanp->devfs_path;
7067c478bd9Sstevel@tonic-gate 			pname = PICL_PROP_DEVFS_PATH;
7077c478bd9Sstevel@tonic-gate 			err = add_regular_prop(cnodeh, pname,
7087c478bd9Sstevel@tonic-gate 			    PICL_PTYPE_CHARSTRING, PICL_READ,
7097c478bd9Sstevel@tonic-gate 			    strlen(devfs_path)+1, (void *)devfs_path, &proph);
7107c478bd9Sstevel@tonic-gate 
7117c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
7127c478bd9Sstevel@tonic-gate 				break;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 			/*
7157c478bd9Sstevel@tonic-gate 			 * Add "Speed" volatile property in this "fan"
7167c478bd9Sstevel@tonic-gate 			 * class node and save prop handle.
7177c478bd9Sstevel@tonic-gate 			 */
7187c478bd9Sstevel@tonic-gate 			pname = PICL_PROP_FAN_SPEED;
7197c478bd9Sstevel@tonic-gate 			err = add_volatile_prop(cnodeh, pname, PICL_PTYPE_INT,
7207c478bd9Sstevel@tonic-gate 			    PICL_READ, sizeof (fanspeed_t), get_current_speed,
7217c478bd9Sstevel@tonic-gate 			    NULL, &proph);
7227c478bd9Sstevel@tonic-gate 
7237c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
7247c478bd9Sstevel@tonic-gate 				break;
7257c478bd9Sstevel@tonic-gate 			fnodep->proph = proph;
7267c478bd9Sstevel@tonic-gate 
7277c478bd9Sstevel@tonic-gate 			/*
7287c478bd9Sstevel@tonic-gate 			 * Add other "fan" class properties
7297c478bd9Sstevel@tonic-gate 			 */
7307c478bd9Sstevel@tonic-gate 			pname = PICL_PROP_FAN_SPEED_UNIT;
7317c478bd9Sstevel@tonic-gate 			err = add_regular_prop(cnodeh, pname,
732*ada2da53SToomas Soome 			    PICL_PTYPE_CHARSTRING, PICL_READ,
733*ada2da53SToomas Soome 			    strlen(fnodep->speed_unit)+1,
734*ada2da53SToomas Soome 			    (void *)fnodep->speed_unit, &proph);
7357c478bd9Sstevel@tonic-gate 
7367c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
7377c478bd9Sstevel@tonic-gate 				break;
7387c478bd9Sstevel@tonic-gate 		}
7397c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
7407c478bd9Sstevel@tonic-gate 			delete_fan_nodes_and_props();
7417c478bd9Sstevel@tonic-gate 			free_node_list(node_list);
7427c478bd9Sstevel@tonic-gate 			if (env_debug)
7437c478bd9Sstevel@tonic-gate 				envd_log(LOG_WARNING,
7447c478bd9Sstevel@tonic-gate 				    "Can't create prop/node for fan '%s'\n",
7457c478bd9Sstevel@tonic-gate 				    nodename);
7467c478bd9Sstevel@tonic-gate 			return (err);
7477c478bd9Sstevel@tonic-gate 		}
7487c478bd9Sstevel@tonic-gate 	}
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	free_node_list(node_list);
7517c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
7527c478bd9Sstevel@tonic-gate }
7537c478bd9Sstevel@tonic-gate 
7547c478bd9Sstevel@tonic-gate 
7557c478bd9Sstevel@tonic-gate /*
7567c478bd9Sstevel@tonic-gate  * Delete all fan nodes and related properties created by the
7577c478bd9Sstevel@tonic-gate  * add_fan_props() for each fan node in the PICL tree.
7587c478bd9Sstevel@tonic-gate  */
7597c478bd9Sstevel@tonic-gate static void
delete_fan_nodes_and_props(void)7607c478bd9Sstevel@tonic-gate delete_fan_nodes_and_props(void)
7617c478bd9Sstevel@tonic-gate {
7627c478bd9Sstevel@tonic-gate 	fan_node_t	*fnodep;
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 	/*
7657c478bd9Sstevel@tonic-gate 	 * Delete/destroy fan node and all properties under it.
7667c478bd9Sstevel@tonic-gate 	 * Note that deleiing/destroying a node deletes/destroys
7677c478bd9Sstevel@tonic-gate 	 * all properties within that node.
7687c478bd9Sstevel@tonic-gate 	 */
7697c478bd9Sstevel@tonic-gate 
7707c478bd9Sstevel@tonic-gate 	for (fnodep = fan_nodes; fnodep->fan_name != NULL; fnodep++) {
771*ada2da53SToomas Soome 		if (fnodep->nodeh != 0) {
7727c478bd9Sstevel@tonic-gate 			(void) ptree_delete_node(fnodep->nodeh);
7737c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_node(fnodep->nodeh);
774*ada2da53SToomas Soome 			fnodep->nodeh = 0;
7757c478bd9Sstevel@tonic-gate 		}
7767c478bd9Sstevel@tonic-gate 	}
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate /*
7807c478bd9Sstevel@tonic-gate  * Find the ENVMODEL_CONF_FILE file.
7817c478bd9Sstevel@tonic-gate  */
7827c478bd9Sstevel@tonic-gate static int
get_envmodel_conf_file(char * outfilename)7837c478bd9Sstevel@tonic-gate get_envmodel_conf_file(char *outfilename)
7847c478bd9Sstevel@tonic-gate {
7857c478bd9Sstevel@tonic-gate 	char	nmbuf[SYS_NMLN];
7867c478bd9Sstevel@tonic-gate 	char    pname[PATH_MAX];
7877c478bd9Sstevel@tonic-gate 
7887c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
7897c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
7907c478bd9Sstevel@tonic-gate 		(void) strlcat(pname, ENVMODEL_CONF_FILE, PATH_MAX);
7917c478bd9Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
7927c478bd9Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
7937c478bd9Sstevel@tonic-gate 			return (0);
7947c478bd9Sstevel@tonic-gate 		}
7957c478bd9Sstevel@tonic-gate 	}
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
7987c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
7997c478bd9Sstevel@tonic-gate 		(void) strlcat(pname, ENVMODEL_CONF_FILE, PATH_MAX);
8007c478bd9Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
8017c478bd9Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
8027c478bd9Sstevel@tonic-gate 			return (0);
8037c478bd9Sstevel@tonic-gate 		}
8047c478bd9Sstevel@tonic-gate 	}
8057c478bd9Sstevel@tonic-gate 
8067c478bd9Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
807*ada2da53SToomas Soome 	    ENVMODEL_CONF_FILE);
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	if (access(pname, R_OK) == 0) {
8107c478bd9Sstevel@tonic-gate 		(void) strlcpy(outfilename, pname, PATH_MAX);
8117c478bd9Sstevel@tonic-gate 		return (0);
8127c478bd9Sstevel@tonic-gate 	}
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	return (-1);
8157c478bd9Sstevel@tonic-gate }
8167c478bd9Sstevel@tonic-gate 
8177c478bd9Sstevel@tonic-gate /* Delete all sensor/fan nodes and any properties created by this plugin */
8187c478bd9Sstevel@tonic-gate void
env_picl_destroy(void)8197c478bd9Sstevel@tonic-gate env_picl_destroy(void)
8207c478bd9Sstevel@tonic-gate {
8217c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_wrlock(&envpicl_rwlock);
8227c478bd9Sstevel@tonic-gate 	delete_fan_nodes_and_props();
8237c478bd9Sstevel@tonic-gate 	delete_sensor_nodes_and_props();
8247c478bd9Sstevel@tonic-gate 	(void) pthread_rwlock_unlock(&envpicl_rwlock);
8257c478bd9Sstevel@tonic-gate }
8267c478bd9Sstevel@tonic-gate 
8277c478bd9Sstevel@tonic-gate void
env_picl_setup(void)8287c478bd9Sstevel@tonic-gate env_picl_setup(void)
8297c478bd9Sstevel@tonic-gate {
8307c478bd9Sstevel@tonic-gate 	int		err;
8317c478bd9Sstevel@tonic-gate 	sensor_node_t	*snodep;
8327c478bd9Sstevel@tonic-gate 	fan_node_t	*fnodep;
8337c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	plath;
8347c478bd9Sstevel@tonic-gate 	char		fullfilename[PATH_MAX];
8357c478bd9Sstevel@tonic-gate 	picl_nodehdl_t  rooth;
8367c478bd9Sstevel@tonic-gate 
8377c478bd9Sstevel@tonic-gate 
8387c478bd9Sstevel@tonic-gate 	/*
8397c478bd9Sstevel@tonic-gate 	 * Initialize sensorp and other fields in the sensor_nodes[] array
8407c478bd9Sstevel@tonic-gate 	 */
8417c478bd9Sstevel@tonic-gate 	for (snodep = sensor_nodes; snodep->sensor_name != NULL; snodep++) {
8427c478bd9Sstevel@tonic-gate 		snodep->sensorp = sensor_lookup(snodep->sensor_name);
843*ada2da53SToomas Soome 		snodep->nodeh = 0;
844*ada2da53SToomas Soome 		snodep->proph = 0;
845*ada2da53SToomas Soome 		snodep->target_proph = 0;
846*ada2da53SToomas Soome 		snodep->sdev_proph = 0;
8477c478bd9Sstevel@tonic-gate 	}
8487c478bd9Sstevel@tonic-gate 
8497c478bd9Sstevel@tonic-gate 	/*
8507c478bd9Sstevel@tonic-gate 	 * Initialize fanp and other fields in the fan_nodes[] array
8517c478bd9Sstevel@tonic-gate 	 */
8527c478bd9Sstevel@tonic-gate 	for (fnodep = fan_nodes; fnodep->fan_name != NULL; fnodep++) {
8537c478bd9Sstevel@tonic-gate 		fnodep->fanp = fan_lookup(fnodep->fan_name);
854*ada2da53SToomas Soome 		fnodep->nodeh = 0;
855*ada2da53SToomas Soome 		fnodep->proph = 0;
8567c478bd9Sstevel@tonic-gate 	}
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 	/*
8597c478bd9Sstevel@tonic-gate 	 * Get platform handle and populate PICL tree with environmental
8607c478bd9Sstevel@tonic-gate 	 * nodes and properties
8617c478bd9Sstevel@tonic-gate 	 */
8627c478bd9Sstevel@tonic-gate 	err = ptree_get_node_by_path("/platform", &plath);
8637c478bd9Sstevel@tonic-gate 
8647c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS) {
8657c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_wrlock(&envpicl_rwlock);
8667c478bd9Sstevel@tonic-gate 		err = add_sensor_nodes_and_props(plath);
8677c478bd9Sstevel@tonic-gate 		if (err == PICL_SUCCESS)
8687c478bd9Sstevel@tonic-gate 			err = add_fan_nodes_and_props(plath);
8697c478bd9Sstevel@tonic-gate 
8707c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
8717c478bd9Sstevel@tonic-gate 			delete_sensor_nodes_and_props();
8727c478bd9Sstevel@tonic-gate 		(void) pthread_rwlock_unlock(&envpicl_rwlock);
8737c478bd9Sstevel@tonic-gate 	}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
8767c478bd9Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
8777c478bd9Sstevel@tonic-gate 		return;
8787c478bd9Sstevel@tonic-gate 	}
8797c478bd9Sstevel@tonic-gate 
8807c478bd9Sstevel@tonic-gate 	/*
8817c478bd9Sstevel@tonic-gate 	 * Parse the envmodel.conf file and populate the PICL tree
8827c478bd9Sstevel@tonic-gate 	 */
8837c478bd9Sstevel@tonic-gate 	if (get_envmodel_conf_file(fullfilename) < 0)
8847c478bd9Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
8857c478bd9Sstevel@tonic-gate 	if (ptree_get_root(&rooth) != PICL_SUCCESS)
8867c478bd9Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
8877c478bd9Sstevel@tonic-gate 	err = picld_pluginutil_parse_config_file(rooth, fullfilename);
8887c478bd9Sstevel@tonic-gate 
8897c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
8907c478bd9Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
8917c478bd9Sstevel@tonic-gate }
892