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 
467c478bd9Sstevel@tonic-gate #include <stdio.h>
477c478bd9Sstevel@tonic-gate #include <fcntl.h>
487c478bd9Sstevel@tonic-gate #include <unistd.h>
497c478bd9Sstevel@tonic-gate #include <syslog.h>
507c478bd9Sstevel@tonic-gate #include <stdlib.h>
517c478bd9Sstevel@tonic-gate #include <limits.h>
527c478bd9Sstevel@tonic-gate #include <sys/open.h>
537c478bd9Sstevel@tonic-gate #include <ctype.h>
547c478bd9Sstevel@tonic-gate #include <string.h>
557c478bd9Sstevel@tonic-gate #include <alloca.h>
567c478bd9Sstevel@tonic-gate #include <libintl.h>
577c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
587c478bd9Sstevel@tonic-gate #include <picl.h>
597c478bd9Sstevel@tonic-gate #include <picltree.h>
607c478bd9Sstevel@tonic-gate #include <picld_pluginutil.h>
617c478bd9Sstevel@tonic-gate #include <pthread.h>
627c478bd9Sstevel@tonic-gate #include <sys/utsname.h>
637c478bd9Sstevel@tonic-gate #include <sys/systeminfo.h>
647c478bd9Sstevel@tonic-gate #include "envd.h"
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate /*
687c478bd9Sstevel@tonic-gate  * Volatile property read/write function typedef
697c478bd9Sstevel@tonic-gate  */
707c478bd9Sstevel@tonic-gate typedef int ptree_vol_rdfunc_t(ptree_rarg_t *parg, void *buf);
717c478bd9Sstevel@tonic-gate typedef int ptree_vol_wrfunc_t(ptree_warg_t *parg, const void *buf);
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate /*
747c478bd9Sstevel@tonic-gate  * PICL Classes and standard property names
757c478bd9Sstevel@tonic-gate  */
767c478bd9Sstevel@tonic-gate #define	PICL_CLASS_CPU		"cpu"
777c478bd9Sstevel@tonic-gate #define	PICL_CLASS_FAN		"fan"
787c478bd9Sstevel@tonic-gate #define	PICL_CLASS_FAN_CONTROL	"fan-control"
797c478bd9Sstevel@tonic-gate #define	PICL_CLASS_TEMP_DEVICE	"temperature-device"
807c478bd9Sstevel@tonic-gate #define	PICL_CLASS_TEMP_SENSOR	"temperature-sensor"
817c478bd9Sstevel@tonic-gate 
827c478bd9Sstevel@tonic-gate #define	PICL_PROP_REG		"reg"
837c478bd9Sstevel@tonic-gate #define	PICL_PROP_DEVFS_PATH	"devfs-path"
847c478bd9Sstevel@tonic-gate #define	PICL_PROP_UNIT_ADDRESS	"UnitAddress"
857c478bd9Sstevel@tonic-gate #define	UNITADDR_LEN_MAX	256	/* max length for UnitAddress */
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate /*
887c478bd9Sstevel@tonic-gate  * temperature properties
897c478bd9Sstevel@tonic-gate  */
907c478bd9Sstevel@tonic-gate #define	PROP_TEMPERATURE	"Temperature"
917c478bd9Sstevel@tonic-gate #define	PROP_CPU_AMB_TEMP	"AmbientTemperature"
927c478bd9Sstevel@tonic-gate #define	PROP_CPU_DIE_TEMP	"Temperature"
937c478bd9Sstevel@tonic-gate 
947c478bd9Sstevel@tonic-gate /*
957c478bd9Sstevel@tonic-gate  * Various temperature threshold property names
967c478bd9Sstevel@tonic-gate  */
977c478bd9Sstevel@tonic-gate #define	PROP_LOW_POWER_OFF	"LowPowerOffThreshold"
987c478bd9Sstevel@tonic-gate #define	PROP_LOW_SHUTDOWN	"LowShutdownThreshold"
997c478bd9Sstevel@tonic-gate #define	PROP_LOW_WARNING	"LowWarningThreshold"
1007c478bd9Sstevel@tonic-gate #define	PROP_HIGH_POWER_OFF	"HighPowerOffThreshold"
1017c478bd9Sstevel@tonic-gate #define	PROP_HIGH_SHUTDOWN	"HighShutdownThreshold"
1027c478bd9Sstevel@tonic-gate #define	PROP_HIGH_WARNING	"HighWarningThreshold"
1037c478bd9Sstevel@tonic-gate 
1047c478bd9Sstevel@tonic-gate /*
1057c478bd9Sstevel@tonic-gate  * fan properties
1067c478bd9Sstevel@tonic-gate  */
1077c478bd9Sstevel@tonic-gate #define	PROP_FAN_SPEED		"Speed"
1087c478bd9Sstevel@tonic-gate #define	PROP_FAN_SPEED_UNIT	"SpeedUnit"
1097c478bd9Sstevel@tonic-gate #define	PROP_FAN_SPEED_UNIT_VALUE	"%"
1107c478bd9Sstevel@tonic-gate 
1117c478bd9Sstevel@tonic-gate /*
1127c478bd9Sstevel@tonic-gate  * PICL class path for CPU nodes
1137c478bd9Sstevel@tonic-gate  */
1147c478bd9Sstevel@tonic-gate #define	CPU_PLAT_PATH		"_class:/upa/cpu?ID=0"
1157c478bd9Sstevel@tonic-gate 
1167c478bd9Sstevel@tonic-gate /*
1177c478bd9Sstevel@tonic-gate  * "UnitAddress" propval for various temperature devices (platform dependent)
1187c478bd9Sstevel@tonic-gate  */
1197c478bd9Sstevel@tonic-gate #define	CPU_TEMPDEV_UNITADDR	"0,30"
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate /*
1227c478bd9Sstevel@tonic-gate  * Sensor node data structure
1237c478bd9Sstevel@tonic-gate  */
1247c478bd9Sstevel@tonic-gate typedef struct {
1257c478bd9Sstevel@tonic-gate 	char		*sensor_name;	/* sensor name */
1267c478bd9Sstevel@tonic-gate 	env_sensor_t	*sensorp;	/* sensor info */
1277c478bd9Sstevel@tonic-gate 	char		*unitaddr;	/* parent's UnitAddress propval */
1287c478bd9Sstevel@tonic-gate 	char		*sdev_node;	/* sensed device node name */
1297c478bd9Sstevel@tonic-gate 	char		*sdev_pname;	/* sensed device "temp" prop name */
1307c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;		/* sensor node handle */
1317c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;		/* "Temperature" property handle */
1327c478bd9Sstevel@tonic-gate 	picl_prophdl_t	sdev_proph;	/* property handle for sensed dev */
1337c478bd9Sstevel@tonic-gate } sensor_node_t;
1347c478bd9Sstevel@tonic-gate 
1357c478bd9Sstevel@tonic-gate 
1367c478bd9Sstevel@tonic-gate /*
1377c478bd9Sstevel@tonic-gate  * Sensor nodes array
1387c478bd9Sstevel@tonic-gate  */
1397c478bd9Sstevel@tonic-gate static sensor_node_t sensor_nodes[] = {
1407c478bd9Sstevel@tonic-gate 	{SENSOR_CPU_DIE, NULL, CPU_TEMPDEV_UNITADDR,
1417c478bd9Sstevel@tonic-gate 	    CPU_PLAT_PATH, PROP_CPU_DIE_TEMP},
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	{SENSOR_CPU_AMB, NULL, CPU_TEMPDEV_UNITADDR,
1447c478bd9Sstevel@tonic-gate 	    CPU_PLAT_PATH, PROP_CPU_AMB_TEMP},
1457c478bd9Sstevel@tonic-gate 
1467c478bd9Sstevel@tonic-gate 	{NULL, NULL, NULL, NULL, NULL}
1477c478bd9Sstevel@tonic-gate };
1487c478bd9Sstevel@tonic-gate 
1497c478bd9Sstevel@tonic-gate 
1507c478bd9Sstevel@tonic-gate /*
1517c478bd9Sstevel@tonic-gate  * Fan node data structure
1527c478bd9Sstevel@tonic-gate  */
1537c478bd9Sstevel@tonic-gate typedef struct {
1547c478bd9Sstevel@tonic-gate 	char		*fan_name;	/* fan name */
155*ada2da53SToomas Soome 	env_fan_t	*fanp;		/* fan information */
1567c478bd9Sstevel@tonic-gate 	char		*speed_unit;	/* speed unit string */
1577c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	nodeh;		/* "fan" node handle */
1587c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;		/* "Speed" property handle */
1597c478bd9Sstevel@tonic-gate } fan_node_t;
1607c478bd9Sstevel@tonic-gate 
1617c478bd9Sstevel@tonic-gate 
1627c478bd9Sstevel@tonic-gate /*
1637c478bd9Sstevel@tonic-gate  * Fan node array
1647c478bd9Sstevel@tonic-gate  */
1657c478bd9Sstevel@tonic-gate static fan_node_t fan_nodes[] =  {
1667c478bd9Sstevel@tonic-gate 	{ENV_SYSTEM_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE},
1677c478bd9Sstevel@tonic-gate 	{NULL, NULL, NULL}
1687c478bd9Sstevel@tonic-gate };
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate /*
1717c478bd9Sstevel@tonic-gate  * Miscellaneous declarations
1727c478bd9Sstevel@tonic-gate  */
1737c478bd9Sstevel@tonic-gate typedef struct node_list {
1747c478bd9Sstevel@tonic-gate 	picl_nodehdl_t		nodeh;
1757c478bd9Sstevel@tonic-gate 	struct node_list	*next;
1767c478bd9Sstevel@tonic-gate } node_list_t;
1777c478bd9Sstevel@tonic-gate 
1787c478bd9Sstevel@tonic-gate static void delete_sensor_nodes_and_props(void);
1797c478bd9Sstevel@tonic-gate static void delete_fan_nodes_and_props(void);
1807c478bd9Sstevel@tonic-gate 
1817c478bd9Sstevel@tonic-gate 
1827c478bd9Sstevel@tonic-gate 
1837c478bd9Sstevel@tonic-gate /*
1847c478bd9Sstevel@tonic-gate  * Read function for volatile "Temperature" property
1857c478bd9Sstevel@tonic-gate  */
1867c478bd9Sstevel@tonic-gate static int
get_current_temp(ptree_rarg_t * parg,void * buf)1877c478bd9Sstevel@tonic-gate get_current_temp(ptree_rarg_t *parg, void *buf)
1887c478bd9Sstevel@tonic-gate {
189*ada2da53SToomas Soome 	tempr_t		temp;
1907c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;
1917c478bd9Sstevel@tonic-gate 	sensor_node_t	*snodep;
1927c478bd9Sstevel@tonic-gate 
1937c478bd9Sstevel@tonic-gate 	/*
1947c478bd9Sstevel@tonic-gate 	 * Locate the sensor in our sensor_nodes table by matching the
1957c478bd9Sstevel@tonic-gate 	 * property handle and get its temperature.
1967c478bd9Sstevel@tonic-gate 	 */
1977c478bd9Sstevel@tonic-gate 	proph = parg->proph;
1987c478bd9Sstevel@tonic-gate 	for (snodep = &sensor_nodes[0]; snodep->sensor_name != NULL; snodep++) {
1997c478bd9Sstevel@tonic-gate 		if (snodep->proph != proph &&
2007c478bd9Sstevel@tonic-gate 		    snodep->sdev_proph != proph)
2017c478bd9Sstevel@tonic-gate 			continue;
2027c478bd9Sstevel@tonic-gate 
2037c478bd9Sstevel@tonic-gate 		if (get_temperature(snodep->sensorp, &temp) < 0)
2047c478bd9Sstevel@tonic-gate 			return (PICL_FAILURE);
2057c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&temp, sizeof (tempr_t));
2067c478bd9Sstevel@tonic-gate 		return (PICL_SUCCESS);
2077c478bd9Sstevel@tonic-gate 	}
2087c478bd9Sstevel@tonic-gate 	return (PICL_FAILURE);
2097c478bd9Sstevel@tonic-gate }
2107c478bd9Sstevel@tonic-gate 
2117c478bd9Sstevel@tonic-gate 
2127c478bd9Sstevel@tonic-gate /*
2137c478bd9Sstevel@tonic-gate  * Read function for volatile "Speed" property on "fan" class node
2147c478bd9Sstevel@tonic-gate  */
2157c478bd9Sstevel@tonic-gate static int
get_current_speed(ptree_rarg_t * parg,void * buf)2167c478bd9Sstevel@tonic-gate get_current_speed(ptree_rarg_t *parg, void *buf)
2177c478bd9Sstevel@tonic-gate {
2187c478bd9Sstevel@tonic-gate 	fanspeed_t	speed;
2197c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;
2207c478bd9Sstevel@tonic-gate 	fan_node_t	*fnodep;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	/*
2237c478bd9Sstevel@tonic-gate 	 * Locate the fan in our fan_nodes table by matching the
2247c478bd9Sstevel@tonic-gate 	 * property handle and get fan speed.
2257c478bd9Sstevel@tonic-gate 	 */
2267c478bd9Sstevel@tonic-gate 	proph = parg->proph;
2277c478bd9Sstevel@tonic-gate 	for (fnodep = &fan_nodes[0]; fnodep->fan_name != NULL; fnodep++) {
2287c478bd9Sstevel@tonic-gate 		if (fnodep->proph != proph)
2297c478bd9Sstevel@tonic-gate 			continue;
2307c478bd9Sstevel@tonic-gate 		if (get_fan_speed(fnodep->fanp, &speed) < 0)
2317c478bd9Sstevel@tonic-gate 			return (PICL_FAILURE);
2327c478bd9Sstevel@tonic-gate 
2337c478bd9Sstevel@tonic-gate 		(void) memcpy(buf, (caddr_t)&speed, sizeof (speed));
2347c478bd9Sstevel@tonic-gate 		return (PICL_SUCCESS);
2357c478bd9Sstevel@tonic-gate 	}
2367c478bd9Sstevel@tonic-gate 	return (PICL_FAILURE);
2377c478bd9Sstevel@tonic-gate }
2387c478bd9Sstevel@tonic-gate 
2397c478bd9Sstevel@tonic-gate 
2407c478bd9Sstevel@tonic-gate static node_list_t *
add_node_to_list(picl_nodehdl_t nodeh,node_list_t * listp)2417c478bd9Sstevel@tonic-gate add_node_to_list(picl_nodehdl_t nodeh, node_list_t *listp)
2427c478bd9Sstevel@tonic-gate {
2437c478bd9Sstevel@tonic-gate 	node_list_t	*el;
2447c478bd9Sstevel@tonic-gate 	node_list_t	*tmp;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	el = malloc(sizeof (node_list_t));
2477c478bd9Sstevel@tonic-gate 	if (el == NULL)
2487c478bd9Sstevel@tonic-gate 		return (listp);
2497c478bd9Sstevel@tonic-gate 	el->nodeh = nodeh;
2507c478bd9Sstevel@tonic-gate 	el->next = NULL;
2517c478bd9Sstevel@tonic-gate 	if (listp == NULL) {
2527c478bd9Sstevel@tonic-gate 		listp = el;
2537c478bd9Sstevel@tonic-gate 		return (listp);
2547c478bd9Sstevel@tonic-gate 	}
2557c478bd9Sstevel@tonic-gate 
2567c478bd9Sstevel@tonic-gate 	/*
2577c478bd9Sstevel@tonic-gate 	 * append to the end to preserve the order found
2587c478bd9Sstevel@tonic-gate 	 */
2597c478bd9Sstevel@tonic-gate 	tmp = listp;
2607c478bd9Sstevel@tonic-gate 	while (tmp->next != NULL)
2617c478bd9Sstevel@tonic-gate 		tmp = tmp->next;
2627c478bd9Sstevel@tonic-gate 
2637c478bd9Sstevel@tonic-gate 	tmp->next = el;
2647c478bd9Sstevel@tonic-gate 	return (listp);
2657c478bd9Sstevel@tonic-gate }
2667c478bd9Sstevel@tonic-gate 
2677c478bd9Sstevel@tonic-gate 
2687c478bd9Sstevel@tonic-gate 
2697c478bd9Sstevel@tonic-gate /*
2707c478bd9Sstevel@tonic-gate  * Get a list of nodes of the specified classname under nodeh
2717c478bd9Sstevel@tonic-gate  * Once a node of the specified class is found, it's children are not
2727c478bd9Sstevel@tonic-gate  * searched.
2737c478bd9Sstevel@tonic-gate  */
2747c478bd9Sstevel@tonic-gate static node_list_t *
get_node_list_by_class(picl_nodehdl_t nodeh,const char * classname,node_list_t * listp)2757c478bd9Sstevel@tonic-gate get_node_list_by_class(picl_nodehdl_t nodeh, const char *classname,
276*ada2da53SToomas Soome     node_list_t *listp)
2777c478bd9Sstevel@tonic-gate {
2787c478bd9Sstevel@tonic-gate 	int		err;
2797c478bd9Sstevel@tonic-gate 	char		clname[PICL_CLASSNAMELEN_MAX+1];
2807c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	chdh;
2817c478bd9Sstevel@tonic-gate 
2827c478bd9Sstevel@tonic-gate 	/*
2837c478bd9Sstevel@tonic-gate 	 * go through the children
2847c478bd9Sstevel@tonic-gate 	 */
2857c478bd9Sstevel@tonic-gate 	err = ptree_get_propval_by_name(nodeh, PICL_PROP_CHILD, &chdh,
2867c478bd9Sstevel@tonic-gate 	    sizeof (picl_nodehdl_t));
2877c478bd9Sstevel@tonic-gate 
2887c478bd9Sstevel@tonic-gate 	while (err == PICL_SUCCESS) {
2897c478bd9Sstevel@tonic-gate 		err = ptree_get_propval_by_name(chdh, PICL_PROP_CLASSNAME,
2907c478bd9Sstevel@tonic-gate 		    clname, strlen(classname) + 1);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 		if ((err == PICL_SUCCESS) && (strcmp(clname, classname) == 0))
2937c478bd9Sstevel@tonic-gate 			listp = add_node_to_list(chdh, listp);
2947c478bd9Sstevel@tonic-gate 		else
2957c478bd9Sstevel@tonic-gate 			listp = get_node_list_by_class(chdh, classname, listp);
2967c478bd9Sstevel@tonic-gate 
2977c478bd9Sstevel@tonic-gate 		err = ptree_get_propval_by_name(chdh, PICL_PROP_PEER, &chdh,
2987c478bd9Sstevel@tonic-gate 		    sizeof (picl_nodehdl_t));
2997c478bd9Sstevel@tonic-gate 	}
3007c478bd9Sstevel@tonic-gate 	return (listp);
3017c478bd9Sstevel@tonic-gate }
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate /*
3057c478bd9Sstevel@tonic-gate  * Free memory allocated to build the specified node list.
3067c478bd9Sstevel@tonic-gate  */
3077c478bd9Sstevel@tonic-gate static void
free_node_list(node_list_t * listp)3087c478bd9Sstevel@tonic-gate free_node_list(node_list_t *listp)
3097c478bd9Sstevel@tonic-gate {
3107c478bd9Sstevel@tonic-gate 	node_list_t	*next;
3117c478bd9Sstevel@tonic-gate 
3127c478bd9Sstevel@tonic-gate 	for (; listp != NULL; listp = next) {
3137c478bd9Sstevel@tonic-gate 		next = listp->next;
3147c478bd9Sstevel@tonic-gate 		free(listp);
3157c478bd9Sstevel@tonic-gate 	}
3167c478bd9Sstevel@tonic-gate }
3177c478bd9Sstevel@tonic-gate 
3187c478bd9Sstevel@tonic-gate /*
3197c478bd9Sstevel@tonic-gate  * Get PICL_PTYPE_CHARSTRING "UnitAddress" property
3207c478bd9Sstevel@tonic-gate  */
3217c478bd9Sstevel@tonic-gate static int
get_unit_address_prop(picl_nodehdl_t nodeh,void * buf,size_t len)3227c478bd9Sstevel@tonic-gate get_unit_address_prop(picl_nodehdl_t nodeh, void *buf, size_t len)
3237c478bd9Sstevel@tonic-gate {
3247c478bd9Sstevel@tonic-gate 	int			err;
3257c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
3267c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	pinfo;
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	err = ptree_get_prop_by_name(nodeh, PICL_PROP_UNIT_ADDRESS, &proph);
3297c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS)
3307c478bd9Sstevel@tonic-gate 		err = ptree_get_propinfo(proph, &pinfo);
3317c478bd9Sstevel@tonic-gate 
3327c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
3337c478bd9Sstevel@tonic-gate 		return (err);
3347c478bd9Sstevel@tonic-gate 
3357c478bd9Sstevel@tonic-gate 	if (pinfo.piclinfo.type != PICL_PTYPE_CHARSTRING ||
3367c478bd9Sstevel@tonic-gate 	    pinfo.piclinfo.size > len)
3377c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
3387c478bd9Sstevel@tonic-gate 
3397c478bd9Sstevel@tonic-gate 	err = ptree_get_propval(proph, buf, pinfo.piclinfo.size);
3407c478bd9Sstevel@tonic-gate 	return (err);
3417c478bd9Sstevel@tonic-gate }
3427c478bd9Sstevel@tonic-gate 
3437c478bd9Sstevel@tonic-gate 
3447c478bd9Sstevel@tonic-gate /*
3457c478bd9Sstevel@tonic-gate  * Create and add the specified regular property
3467c478bd9Sstevel@tonic-gate  */
3477c478bd9Sstevel@tonic-gate 
3487c478bd9Sstevel@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)3497c478bd9Sstevel@tonic-gate add_regular_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
3507c478bd9Sstevel@tonic-gate     int size, void *valbuf, picl_prophdl_t *prophp)
3517c478bd9Sstevel@tonic-gate {
3527c478bd9Sstevel@tonic-gate 	int			err;
3537c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
3547c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
3557c478bd9Sstevel@tonic-gate 
3567c478bd9Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
3577c478bd9Sstevel@tonic-gate 	    type, access, size, name, NULL, NULL);
3587c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
3597c478bd9Sstevel@tonic-gate 		return (err);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(nodeh, &propinfo, valbuf, &proph);
3627c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS && prophp)
3637c478bd9Sstevel@tonic-gate 		*prophp = proph;
3647c478bd9Sstevel@tonic-gate 	return (err);
3657c478bd9Sstevel@tonic-gate }
3667c478bd9Sstevel@tonic-gate 
3677c478bd9Sstevel@tonic-gate 
3687c478bd9Sstevel@tonic-gate /*
3697c478bd9Sstevel@tonic-gate  * Create and add the specified volatile property
3707c478bd9Sstevel@tonic-gate  */
3717c478bd9Sstevel@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)3727c478bd9Sstevel@tonic-gate add_volatile_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
3737c478bd9Sstevel@tonic-gate     int size, ptree_vol_rdfunc_t *rdfunc, ptree_vol_wrfunc_t *wrfunc,
3747c478bd9Sstevel@tonic-gate     picl_prophdl_t *prophp)
3757c478bd9Sstevel@tonic-gate {
3767c478bd9Sstevel@tonic-gate 	int			err;
3777c478bd9Sstevel@tonic-gate 	ptree_propinfo_t	propinfo;
3787c478bd9Sstevel@tonic-gate 	picl_prophdl_t		proph;
3797c478bd9Sstevel@tonic-gate 
3807c478bd9Sstevel@tonic-gate 	err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
3817c478bd9Sstevel@tonic-gate 	    type, (access|PICL_VOLATILE), size, name, rdfunc, wrfunc);
3827c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
3837c478bd9Sstevel@tonic-gate 		return (err);
3847c478bd9Sstevel@tonic-gate 
3857c478bd9Sstevel@tonic-gate 	err = ptree_create_and_add_prop(nodeh, &propinfo, NULL, &proph);
3867c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS && prophp)
3877c478bd9Sstevel@tonic-gate 		*prophp = proph;
3887c478bd9Sstevel@tonic-gate 	return (err);
3897c478bd9Sstevel@tonic-gate }
3907c478bd9Sstevel@tonic-gate 
3917c478bd9Sstevel@tonic-gate /*
3927c478bd9Sstevel@tonic-gate  * Add temperature threshold properties
3937c478bd9Sstevel@tonic-gate  */
3947c478bd9Sstevel@tonic-gate static void
add_sensor_thresh_props(picl_nodehdl_t nodeh,sensor_thresh_t * threshp)3957c478bd9Sstevel@tonic-gate add_sensor_thresh_props(picl_nodehdl_t nodeh, sensor_thresh_t *threshp)
3967c478bd9Sstevel@tonic-gate {
3977c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;
3987c478bd9Sstevel@tonic-gate 
3997c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_LOW_POWER_OFF,
4007c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4017c478bd9Sstevel@tonic-gate 	    sizeof (threshp->low_power_off),
4027c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->low_power_off), &proph);
4037c478bd9Sstevel@tonic-gate 
4047c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_LOW_SHUTDOWN,
4057c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4067c478bd9Sstevel@tonic-gate 	    sizeof (threshp->low_shutdown),
4077c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->low_shutdown), &proph);
4087c478bd9Sstevel@tonic-gate 
4097c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_LOW_WARNING,
4107c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4117c478bd9Sstevel@tonic-gate 	    sizeof (threshp->low_warning),
4127c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->low_warning), &proph);
4137c478bd9Sstevel@tonic-gate 
4147c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_HIGH_WARNING,
4157c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4167c478bd9Sstevel@tonic-gate 	    sizeof (threshp->high_warning),
4177c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->high_warning), &proph);
4187c478bd9Sstevel@tonic-gate 
4197c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_HIGH_SHUTDOWN,
4207c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4217c478bd9Sstevel@tonic-gate 	    sizeof (threshp->high_shutdown),
4227c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->high_shutdown), &proph);
4237c478bd9Sstevel@tonic-gate 
4247c478bd9Sstevel@tonic-gate 	(void) add_regular_prop(nodeh, PROP_HIGH_POWER_OFF,
4257c478bd9Sstevel@tonic-gate 	    PICL_PTYPE_INT, PICL_READ,
4267c478bd9Sstevel@tonic-gate 	    sizeof (threshp->high_power_off),
4277c478bd9Sstevel@tonic-gate 	    (void *)&(threshp->high_power_off), &proph);
4287c478bd9Sstevel@tonic-gate }
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 
4317c478bd9Sstevel@tonic-gate /*
4327c478bd9Sstevel@tonic-gate  * Lookup "temperature-device" class nodes and create "temperature-sensor"
4337c478bd9Sstevel@tonic-gate  * class nodes and relevant properties under those nodes.
4347c478bd9Sstevel@tonic-gate  *
4357c478bd9Sstevel@tonic-gate  * For each entry in sensor_nodes[] array, do the following:
4367c478bd9Sstevel@tonic-gate  *	- Create specified (cpu-die or cpu-ambient) "temperautre-sensor" class
4377c478bd9Sstevel@tonic-gate  *	  node.
4387c478bd9Sstevel@tonic-gate  *	- Create "devfs-path" property under this node.
4397c478bd9Sstevel@tonic-gate  *	- Create "Temperature" volatile property under this node.
4407c478bd9Sstevel@tonic-gate  *	- Create various temperature threshold properties under this node.
4417c478bd9Sstevel@tonic-gate  *	- Create specified ("Temperature" or "AmbientTemperature") volatile
4427c478bd9Sstevel@tonic-gate  *	  temperature property under specified sdev_node node.
4437c478bd9Sstevel@tonic-gate  */
4447c478bd9Sstevel@tonic-gate 
4457c478bd9Sstevel@tonic-gate static int
add_sensor_nodes_and_props(picl_nodehdl_t plath)4467c478bd9Sstevel@tonic-gate add_sensor_nodes_and_props(picl_nodehdl_t plath)
4477c478bd9Sstevel@tonic-gate {
4487c478bd9Sstevel@tonic-gate 	int		err;
4497c478bd9Sstevel@tonic-gate 	char		*pname, *nodename, *refnode, *devfs_path;
4507c478bd9Sstevel@tonic-gate 	node_list_t	*node_list, *listp;
4517c478bd9Sstevel@tonic-gate 	sensor_node_t	*snodep;
4527c478bd9Sstevel@tonic-gate 	sensor_thresh_t *threshp;
4537c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	nodeh, refnodeh, cnodeh;
4547c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;
4557c478bd9Sstevel@tonic-gate 	char		unitaddr[UNITADDR_LEN_MAX];
4567c478bd9Sstevel@tonic-gate 	env_sensor_t	*sensorp;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	node_list =
4597c478bd9Sstevel@tonic-gate 	    get_node_list_by_class(plath, PICL_CLASS_TEMP_DEVICE, NULL);
4607c478bd9Sstevel@tonic-gate 
4617c478bd9Sstevel@tonic-gate 	if (node_list == NULL)
4627c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
4637c478bd9Sstevel@tonic-gate 
4647c478bd9Sstevel@tonic-gate 	for (listp = node_list; listp != NULL; listp = listp->next) {
4657c478bd9Sstevel@tonic-gate 		/*
4667c478bd9Sstevel@tonic-gate 		 * Get "reg" property. Skip if no "reg" property found.
4677c478bd9Sstevel@tonic-gate 		 */
4687c478bd9Sstevel@tonic-gate 		nodeh = listp->nodeh;
4697c478bd9Sstevel@tonic-gate 		err = get_unit_address_prop(nodeh, (void *)unitaddr,
4707c478bd9Sstevel@tonic-gate 		    sizeof (unitaddr));
4717c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS)
4727c478bd9Sstevel@tonic-gate 			continue;
4737c478bd9Sstevel@tonic-gate 
4747c478bd9Sstevel@tonic-gate 		for (snodep = sensor_nodes; snodep->sensor_name != NULL;
4757c478bd9Sstevel@tonic-gate 		    snodep++) {
4767c478bd9Sstevel@tonic-gate 
4777c478bd9Sstevel@tonic-gate 			/* Match "UnitAddress" property */
4787c478bd9Sstevel@tonic-gate 			if (strcasecmp(unitaddr, snodep->unitaddr) != 0)
4797c478bd9Sstevel@tonic-gate 				continue;
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 			/*
4827c478bd9Sstevel@tonic-gate 			 * Skip if already initialized or no sensor info
4837c478bd9Sstevel@tonic-gate 			 */
4847c478bd9Sstevel@tonic-gate 			sensorp = snodep->sensorp;
485*ada2da53SToomas Soome 			if (snodep->nodeh != 0 || sensorp == NULL)
4867c478bd9Sstevel@tonic-gate 				continue;
4877c478bd9Sstevel@tonic-gate 
4887c478bd9Sstevel@tonic-gate 			/*
4897c478bd9Sstevel@tonic-gate 			 * Create temperature-sensor node
4907c478bd9Sstevel@tonic-gate 			 */
4917c478bd9Sstevel@tonic-gate 			nodename = snodep->sensor_name;
4927c478bd9Sstevel@tonic-gate 			err = ptree_create_and_add_node(nodeh, nodename,
4937c478bd9Sstevel@tonic-gate 			    PICL_CLASS_TEMP_SENSOR, &cnodeh);
4947c478bd9Sstevel@tonic-gate 			if (env_debug)
4957c478bd9Sstevel@tonic-gate 				envd_log(LOG_INFO,
4967c478bd9Sstevel@tonic-gate 				    "Creating PICL sensor node '%s' err:%d\n",
4977c478bd9Sstevel@tonic-gate 				    nodename, err);
4987c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
4997c478bd9Sstevel@tonic-gate 				break;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 			/* save node handle */
5027c478bd9Sstevel@tonic-gate 			snodep->nodeh = cnodeh;
5037c478bd9Sstevel@tonic-gate 
5047c478bd9Sstevel@tonic-gate 			/*
5057c478bd9Sstevel@tonic-gate 			 * Add "devfs_path" property in child node
5067c478bd9Sstevel@tonic-gate 			 */
5077c478bd9Sstevel@tonic-gate 			devfs_path = sensorp->devfs_path;
5087c478bd9Sstevel@tonic-gate 			pname = PICL_PROP_DEVFS_PATH;
5097c478bd9Sstevel@tonic-gate 			err = add_regular_prop(cnodeh, pname,
5107c478bd9Sstevel@tonic-gate 			    PICL_PTYPE_CHARSTRING, PICL_READ,
5117c478bd9Sstevel@tonic-gate 			    strlen(devfs_path)+1, (void *)devfs_path, &proph);
5127c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
5137c478bd9Sstevel@tonic-gate 				break;
5147c478bd9Sstevel@tonic-gate 
5157c478bd9Sstevel@tonic-gate 			/*
5167c478bd9Sstevel@tonic-gate 			 * Now add volatile "temperature" volatile property
5177c478bd9Sstevel@tonic-gate 			 * in this "temperature-sensor" class node.
5187c478bd9Sstevel@tonic-gate 			 */
5197c478bd9Sstevel@tonic-gate 			pname = PROP_TEMPERATURE;
5207c478bd9Sstevel@tonic-gate 			err = add_volatile_prop(cnodeh, pname,
5217c478bd9Sstevel@tonic-gate 			    PICL_PTYPE_INT, PICL_READ, sizeof (tempr_t),
5227c478bd9Sstevel@tonic-gate 			    get_current_temp, NULL, &proph);
5237c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
5247c478bd9Sstevel@tonic-gate 				break;
5257c478bd9Sstevel@tonic-gate 
5267c478bd9Sstevel@tonic-gate 			/* Save prop handle */
5277c478bd9Sstevel@tonic-gate 			snodep->proph = proph;
5287c478bd9Sstevel@tonic-gate 
5297c478bd9Sstevel@tonic-gate 			/*
5307c478bd9Sstevel@tonic-gate 			 * Add threshold related properties
5317c478bd9Sstevel@tonic-gate 			 */
5327c478bd9Sstevel@tonic-gate 			threshp = sensorp->temp_thresh;
5337c478bd9Sstevel@tonic-gate 			if (threshp != NULL)
5347c478bd9Sstevel@tonic-gate 				add_sensor_thresh_props(cnodeh, threshp);
5357c478bd9Sstevel@tonic-gate 
5367c478bd9Sstevel@tonic-gate 			/*
5377c478bd9Sstevel@tonic-gate 			 * Finally create property in the sensed device
5387c478bd9Sstevel@tonic-gate 			 * (if one specified)
5397c478bd9Sstevel@tonic-gate 			 */
5407c478bd9Sstevel@tonic-gate 			refnode =  snodep->sdev_node;
5417c478bd9Sstevel@tonic-gate 			pname =  snodep->sdev_pname;
5427c478bd9Sstevel@tonic-gate 			if (refnode == NULL || pname == NULL)
5437c478bd9Sstevel@tonic-gate 				continue;
5447c478bd9Sstevel@tonic-gate 
5457c478bd9Sstevel@tonic-gate 			err = ptree_get_node_by_path(refnode, &refnodeh);
5467c478bd9Sstevel@tonic-gate 			if (err == PICL_SUCCESS) {
5477c478bd9Sstevel@tonic-gate 				err = add_volatile_prop(refnodeh, pname,
5487c478bd9Sstevel@tonic-gate 				    PICL_PTYPE_INT, PICL_READ,
5497c478bd9Sstevel@tonic-gate 				    sizeof (tempr_t), get_current_temp,
5507c478bd9Sstevel@tonic-gate 				    NULL, &proph);
5517c478bd9Sstevel@tonic-gate 			}
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
5547c478bd9Sstevel@tonic-gate 				break;
5557c478bd9Sstevel@tonic-gate 
5567c478bd9Sstevel@tonic-gate 			/* Save prop handle */
5577c478bd9Sstevel@tonic-gate 			snodep->sdev_proph = proph;
5587c478bd9Sstevel@tonic-gate 		}
5597c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
5607c478bd9Sstevel@tonic-gate 			delete_sensor_nodes_and_props();
5617c478bd9Sstevel@tonic-gate 			free_node_list(node_list);
5627c478bd9Sstevel@tonic-gate 			if (env_debug)
5637c478bd9Sstevel@tonic-gate 				envd_log(LOG_INFO,
5647c478bd9Sstevel@tonic-gate 				    "Can't create prop/node for sensor '%s'\n",
5657c478bd9Sstevel@tonic-gate 				    nodename);
5667c478bd9Sstevel@tonic-gate 			return (err);
5677c478bd9Sstevel@tonic-gate 		}
5687c478bd9Sstevel@tonic-gate 	}
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 	free_node_list(node_list);
5717c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
5727c478bd9Sstevel@tonic-gate }
5737c478bd9Sstevel@tonic-gate 
5747c478bd9Sstevel@tonic-gate /*
5757c478bd9Sstevel@tonic-gate  * Delete all sensor nodes and related properties created by the
5767c478bd9Sstevel@tonic-gate  * add_sensor_prop() for each sensor node in the PICL tree.
5777c478bd9Sstevel@tonic-gate  */
5787c478bd9Sstevel@tonic-gate static void
delete_sensor_nodes_and_props(void)5797c478bd9Sstevel@tonic-gate delete_sensor_nodes_and_props(void)
5807c478bd9Sstevel@tonic-gate {
5817c478bd9Sstevel@tonic-gate 	sensor_node_t	*snodep;
5827c478bd9Sstevel@tonic-gate 
5837c478bd9Sstevel@tonic-gate 	/*
5847c478bd9Sstevel@tonic-gate 	 * Delete/destroy any property created in the sensed device
5857c478bd9Sstevel@tonic-gate 	 * as well as the sensor node and all properties under it.
5867c478bd9Sstevel@tonic-gate 	 * Note that deleiing/destroying a node deletes/destroys
5877c478bd9Sstevel@tonic-gate 	 * all properties within that node.
5887c478bd9Sstevel@tonic-gate 	 */
5897c478bd9Sstevel@tonic-gate 
5907c478bd9Sstevel@tonic-gate 	for (snodep = sensor_nodes; snodep->sensor_name != NULL; snodep++) {
591*ada2da53SToomas Soome 		if (snodep->sdev_proph != 0) {
5927c478bd9Sstevel@tonic-gate 			(void) ptree_delete_prop(snodep->sdev_proph);
5937c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_prop(snodep->sdev_proph);
594*ada2da53SToomas Soome 			snodep->sdev_proph = 0;
5957c478bd9Sstevel@tonic-gate 		}
5967c478bd9Sstevel@tonic-gate 
597*ada2da53SToomas Soome 		if (snodep->nodeh != 0) {
5987c478bd9Sstevel@tonic-gate 			/* delete node and all properties under it */
5997c478bd9Sstevel@tonic-gate 			(void) ptree_delete_node(snodep->nodeh);
6007c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_node(snodep->nodeh);
601*ada2da53SToomas Soome 			snodep->nodeh = 0;
602*ada2da53SToomas Soome 			snodep->proph = 0;
6037c478bd9Sstevel@tonic-gate 		}
6047c478bd9Sstevel@tonic-gate 	}
6057c478bd9Sstevel@tonic-gate }
6067c478bd9Sstevel@tonic-gate 
6077c478bd9Sstevel@tonic-gate 
6087c478bd9Sstevel@tonic-gate /*
6097c478bd9Sstevel@tonic-gate  * Lookup "fan-control" class node and create "fan" class nodes and
6107c478bd9Sstevel@tonic-gate  * relevant properties under those nodes.
6117c478bd9Sstevel@tonic-gate  *
6127c478bd9Sstevel@tonic-gate  * For each entry in fan_nodes[] array, do the following:
6137c478bd9Sstevel@tonic-gate  *	- Create specified "fan" class node.
6147c478bd9Sstevel@tonic-gate  *	- Create "devfs-path" property under "fan" class node
6157c478bd9Sstevel@tonic-gate  *	- Create "Speed" volatile propery under "fan" class node.
6167c478bd9Sstevel@tonic-gate  *	- Create "SpeedUnit" property under "fan" class node.
6177c478bd9Sstevel@tonic-gate  */
6187c478bd9Sstevel@tonic-gate 
6197c478bd9Sstevel@tonic-gate static int
add_fan_nodes_and_props(picl_nodehdl_t plath)6207c478bd9Sstevel@tonic-gate add_fan_nodes_and_props(picl_nodehdl_t plath)
6217c478bd9Sstevel@tonic-gate {
6227c478bd9Sstevel@tonic-gate 	int		err;
6237c478bd9Sstevel@tonic-gate 	char		*pname, *nodename, *devfs_path;
6247c478bd9Sstevel@tonic-gate 	env_fan_t	*fanp;
6257c478bd9Sstevel@tonic-gate 	fan_node_t	*fnodep;
6267c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	nodeh, cnodeh;
6277c478bd9Sstevel@tonic-gate 	picl_prophdl_t	proph;
6287c478bd9Sstevel@tonic-gate 	node_list_t	*node_list, *listp;
6297c478bd9Sstevel@tonic-gate 
6307c478bd9Sstevel@tonic-gate 	node_list =
6317c478bd9Sstevel@tonic-gate 	    get_node_list_by_class(plath, PICL_CLASS_FAN_CONTROL, NULL);
6327c478bd9Sstevel@tonic-gate 
6337c478bd9Sstevel@tonic-gate 	if (node_list == NULL)
6347c478bd9Sstevel@tonic-gate 		return (PICL_FAILURE);
6357c478bd9Sstevel@tonic-gate 
6367c478bd9Sstevel@tonic-gate 	for (listp = node_list; listp != NULL; listp = listp->next) {
6377c478bd9Sstevel@tonic-gate 		/*
6387c478bd9Sstevel@tonic-gate 		 * Add various fan nodes and properties
6397c478bd9Sstevel@tonic-gate 		 */
6407c478bd9Sstevel@tonic-gate 		nodeh = listp->nodeh;
6417c478bd9Sstevel@tonic-gate 		err = PICL_SUCCESS;
6427c478bd9Sstevel@tonic-gate 		for (fnodep = fan_nodes; fnodep->fan_name != NULL; fnodep++) {
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 			/* Skip if already initialized or no fan info */
645*ada2da53SToomas Soome 			if (fnodep->nodeh != 0 || fnodep->fanp == NULL)
6467c478bd9Sstevel@tonic-gate 				continue;
6477c478bd9Sstevel@tonic-gate 
6487c478bd9Sstevel@tonic-gate 			/*
6497c478bd9Sstevel@tonic-gate 			 * Create "fan" class node and save node handle
6507c478bd9Sstevel@tonic-gate 			 */
6517c478bd9Sstevel@tonic-gate 			nodename = fnodep->fan_name;
6527c478bd9Sstevel@tonic-gate 			err = ptree_create_and_add_node(nodeh, nodename,
6537c478bd9Sstevel@tonic-gate 			    PICL_CLASS_FAN, &cnodeh);
6547c478bd9Sstevel@tonic-gate 			if (env_debug)
6557c478bd9Sstevel@tonic-gate 				envd_log(LOG_INFO,
6567c478bd9Sstevel@tonic-gate 				    "Creating PICL fan node '%s' err:%d\n",
6577c478bd9Sstevel@tonic-gate 				    nodename, err);
6587c478bd9Sstevel@tonic-gate 
6597c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
6607c478bd9Sstevel@tonic-gate 				break;
6617c478bd9Sstevel@tonic-gate 			fnodep->nodeh = cnodeh;
6627c478bd9Sstevel@tonic-gate 
6637c478bd9Sstevel@tonic-gate 			/*
6647c478bd9Sstevel@tonic-gate 			 * Add "devfs_path" property in child node
6657c478bd9Sstevel@tonic-gate 			 */
6667c478bd9Sstevel@tonic-gate 			fanp = fnodep->fanp;
6677c478bd9Sstevel@tonic-gate 			devfs_path  = fanp->devfs_path;
6687c478bd9Sstevel@tonic-gate 			pname = PICL_PROP_DEVFS_PATH;
6697c478bd9Sstevel@tonic-gate 			err = add_regular_prop(cnodeh, pname,
6707c478bd9Sstevel@tonic-gate 			    PICL_PTYPE_CHARSTRING, PICL_READ,
6717c478bd9Sstevel@tonic-gate 			    strlen(devfs_path)+1, (void *)devfs_path, &proph);
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
6747c478bd9Sstevel@tonic-gate 				break;
6757c478bd9Sstevel@tonic-gate 
6767c478bd9Sstevel@tonic-gate 			/*
6777c478bd9Sstevel@tonic-gate 			 * Add "Speed" volatile property in this "fan"
6787c478bd9Sstevel@tonic-gate 			 * class node and save prop handle.
6797c478bd9Sstevel@tonic-gate 			 */
6807c478bd9Sstevel@tonic-gate 			pname = PROP_FAN_SPEED;
6817c478bd9Sstevel@tonic-gate 			err = add_volatile_prop(cnodeh, pname, PICL_PTYPE_INT,
6827c478bd9Sstevel@tonic-gate 			    PICL_READ, sizeof (fanspeed_t), get_current_speed,
6837c478bd9Sstevel@tonic-gate 			    NULL, &proph);
6847c478bd9Sstevel@tonic-gate 
6857c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
6867c478bd9Sstevel@tonic-gate 				break;
6877c478bd9Sstevel@tonic-gate 			fnodep->proph = proph;
6887c478bd9Sstevel@tonic-gate 
6897c478bd9Sstevel@tonic-gate 			/*
6907c478bd9Sstevel@tonic-gate 			 * Add other "fan" class properties
6917c478bd9Sstevel@tonic-gate 			 */
692*ada2da53SToomas Soome 			pname = PROP_FAN_SPEED_UNIT;
6937c478bd9Sstevel@tonic-gate 			err = add_regular_prop(cnodeh, pname,
6947c478bd9Sstevel@tonic-gate 			    PICL_PTYPE_CHARSTRING, PICL_READ,
6957c478bd9Sstevel@tonic-gate 			    strlen(fnodep->speed_unit)+1,
6967c478bd9Sstevel@tonic-gate 			    (void *)fnodep->speed_unit, &proph);
6977c478bd9Sstevel@tonic-gate 
6987c478bd9Sstevel@tonic-gate 			if (err != PICL_SUCCESS)
6997c478bd9Sstevel@tonic-gate 				break;
7007c478bd9Sstevel@tonic-gate 		}
7017c478bd9Sstevel@tonic-gate 		if (err != PICL_SUCCESS) {
7027c478bd9Sstevel@tonic-gate 			delete_fan_nodes_and_props();
7037c478bd9Sstevel@tonic-gate 			free_node_list(node_list);
7047c478bd9Sstevel@tonic-gate 			if (env_debug)
7057c478bd9Sstevel@tonic-gate 				envd_log(LOG_WARNING,
7067c478bd9Sstevel@tonic-gate 				    "Can't create prop/node for fan '%s'\n",
7077c478bd9Sstevel@tonic-gate 				    nodename);
7087c478bd9Sstevel@tonic-gate 			return (err);
7097c478bd9Sstevel@tonic-gate 		}
7107c478bd9Sstevel@tonic-gate 	}
7117c478bd9Sstevel@tonic-gate 
7127c478bd9Sstevel@tonic-gate 	free_node_list(node_list);
7137c478bd9Sstevel@tonic-gate 	return (PICL_SUCCESS);
7147c478bd9Sstevel@tonic-gate }
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 
7177c478bd9Sstevel@tonic-gate /*
7187c478bd9Sstevel@tonic-gate  * Delete all fan nodes and related properties created by the
7197c478bd9Sstevel@tonic-gate  * add_fan_props() for each fan node in the PICL tree.
7207c478bd9Sstevel@tonic-gate  */
7217c478bd9Sstevel@tonic-gate static void
delete_fan_nodes_and_props(void)7227c478bd9Sstevel@tonic-gate delete_fan_nodes_and_props(void)
7237c478bd9Sstevel@tonic-gate {
7247c478bd9Sstevel@tonic-gate 	fan_node_t	*fnodep;
7257c478bd9Sstevel@tonic-gate 
7267c478bd9Sstevel@tonic-gate 	/*
7277c478bd9Sstevel@tonic-gate 	 * Delete/destroy fan node and all properties under it.
7287c478bd9Sstevel@tonic-gate 	 * Note that deleiing/destroying a node deletes/destroys
7297c478bd9Sstevel@tonic-gate 	 * all properties within that node.
7307c478bd9Sstevel@tonic-gate 	 */
7317c478bd9Sstevel@tonic-gate 
7327c478bd9Sstevel@tonic-gate 	for (fnodep = fan_nodes; fnodep->fan_name != NULL; fnodep++) {
733*ada2da53SToomas Soome 		if (fnodep->nodeh != 0) {
7347c478bd9Sstevel@tonic-gate 			(void) ptree_delete_node(fnodep->nodeh);
7357c478bd9Sstevel@tonic-gate 			(void) ptree_destroy_node(fnodep->nodeh);
736*ada2da53SToomas Soome 			fnodep->nodeh = 0;
7377c478bd9Sstevel@tonic-gate 		}
7387c478bd9Sstevel@tonic-gate 	}
7397c478bd9Sstevel@tonic-gate }
7407c478bd9Sstevel@tonic-gate 
7417c478bd9Sstevel@tonic-gate /*
7427c478bd9Sstevel@tonic-gate  * Find the ENVMODEL_CONF_FILE file.
7437c478bd9Sstevel@tonic-gate  */
7447c478bd9Sstevel@tonic-gate static int
get_envmodel_conf_file(char * outfilename)7457c478bd9Sstevel@tonic-gate get_envmodel_conf_file(char *outfilename)
7467c478bd9Sstevel@tonic-gate {
7477c478bd9Sstevel@tonic-gate 	char	nmbuf[SYS_NMLN];
7487c478bd9Sstevel@tonic-gate 	char    pname[PATH_MAX];
7497c478bd9Sstevel@tonic-gate 
7507c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
7517c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
7527c478bd9Sstevel@tonic-gate 		(void) strlcat(pname, ENVMODEL_CONF_FILE, PATH_MAX);
7537c478bd9Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
7547c478bd9Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
7557c478bd9Sstevel@tonic-gate 			return (0);
7567c478bd9Sstevel@tonic-gate 		}
7577c478bd9Sstevel@tonic-gate 	}
7587c478bd9Sstevel@tonic-gate 
7597c478bd9Sstevel@tonic-gate 	if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
7607c478bd9Sstevel@tonic-gate 		(void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
7617c478bd9Sstevel@tonic-gate 		(void) strlcat(pname, ENVMODEL_CONF_FILE, PATH_MAX);
7627c478bd9Sstevel@tonic-gate 		if (access(pname, R_OK) == 0) {
7637c478bd9Sstevel@tonic-gate 			(void) strlcpy(outfilename, pname, PATH_MAX);
7647c478bd9Sstevel@tonic-gate 			return (0);
7657c478bd9Sstevel@tonic-gate 		}
7667c478bd9Sstevel@tonic-gate 	}
7677c478bd9Sstevel@tonic-gate 
7687c478bd9Sstevel@tonic-gate 	(void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
769*ada2da53SToomas Soome 	    ENVMODEL_CONF_FILE);
7707c478bd9Sstevel@tonic-gate 
7717c478bd9Sstevel@tonic-gate 	if (access(pname, R_OK) == 0) {
7727c478bd9Sstevel@tonic-gate 		(void) strlcpy(outfilename, pname, PATH_MAX);
7737c478bd9Sstevel@tonic-gate 		return (0);
7747c478bd9Sstevel@tonic-gate 	}
7757c478bd9Sstevel@tonic-gate 
7767c478bd9Sstevel@tonic-gate 	return (-1);
7777c478bd9Sstevel@tonic-gate }
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate void
env_picl_setup(void)7807c478bd9Sstevel@tonic-gate env_picl_setup(void)
7817c478bd9Sstevel@tonic-gate {
7827c478bd9Sstevel@tonic-gate 	int		err;
7837c478bd9Sstevel@tonic-gate 	sensor_node_t	*snodep;
7847c478bd9Sstevel@tonic-gate 	fan_node_t	*fnodep;
7857c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	plath;
7867c478bd9Sstevel@tonic-gate 	char		fullfilename[PATH_MAX];
7877c478bd9Sstevel@tonic-gate 	picl_nodehdl_t	rooth;
7887c478bd9Sstevel@tonic-gate 
7897c478bd9Sstevel@tonic-gate 	/*
7907c478bd9Sstevel@tonic-gate 	 * Initialize sensorp and other fields in the sensor_nodes[] array
7917c478bd9Sstevel@tonic-gate 	 */
7927c478bd9Sstevel@tonic-gate 	for (snodep = sensor_nodes; snodep->sensor_name != NULL; snodep++) {
7937c478bd9Sstevel@tonic-gate 		snodep->sensorp = sensor_lookup(snodep->sensor_name);
794*ada2da53SToomas Soome 		snodep->nodeh = 0;
795*ada2da53SToomas Soome 		snodep->proph = 0;
796*ada2da53SToomas Soome 		snodep->sdev_proph = 0;
7977c478bd9Sstevel@tonic-gate 	}
7987c478bd9Sstevel@tonic-gate 
7997c478bd9Sstevel@tonic-gate 	/*
8007c478bd9Sstevel@tonic-gate 	 * Initialize fanp and other fields in the fan_nodes[] array
8017c478bd9Sstevel@tonic-gate 	 */
8027c478bd9Sstevel@tonic-gate 	for (fnodep = fan_nodes; fnodep->fan_name != NULL; fnodep++) {
8037c478bd9Sstevel@tonic-gate 		fnodep->fanp = fan_lookup(fnodep->fan_name);
804*ada2da53SToomas Soome 		fnodep->nodeh = 0;
805*ada2da53SToomas Soome 		fnodep->proph = 0;
8067c478bd9Sstevel@tonic-gate 	}
8077c478bd9Sstevel@tonic-gate 
8087c478bd9Sstevel@tonic-gate 	/*
8097c478bd9Sstevel@tonic-gate 	 * Get platform handle and populate PICL tree with environmental
8107c478bd9Sstevel@tonic-gate 	 * nodes and properties
8117c478bd9Sstevel@tonic-gate 	 */
8127c478bd9Sstevel@tonic-gate 	err = ptree_get_node_by_path("/platform", &plath);
8137c478bd9Sstevel@tonic-gate 
8147c478bd9Sstevel@tonic-gate 	if (err == PICL_SUCCESS) {
8157c478bd9Sstevel@tonic-gate 		err = add_sensor_nodes_and_props(plath);
8167c478bd9Sstevel@tonic-gate 		if (err == PICL_SUCCESS)
8177c478bd9Sstevel@tonic-gate 			err = add_fan_nodes_and_props(plath);
8187c478bd9Sstevel@tonic-gate 	}
8197c478bd9Sstevel@tonic-gate 
8207c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS) {
8217c478bd9Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
8227c478bd9Sstevel@tonic-gate 		return;
8237c478bd9Sstevel@tonic-gate 	}
8247c478bd9Sstevel@tonic-gate 
8257c478bd9Sstevel@tonic-gate 	/*
8267c478bd9Sstevel@tonic-gate 	 * Parse the envmodel.conf file and populate the PICL tree
8277c478bd9Sstevel@tonic-gate 	 */
8287c478bd9Sstevel@tonic-gate 	if (get_envmodel_conf_file(fullfilename) < 0)
8297c478bd9Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
8307c478bd9Sstevel@tonic-gate 	if (ptree_get_root(&rooth) != PICL_SUCCESS)
8317c478bd9Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
8327c478bd9Sstevel@tonic-gate 	err = picld_pluginutil_parse_config_file(rooth, fullfilename);
8337c478bd9Sstevel@tonic-gate 
8347c478bd9Sstevel@tonic-gate 	if (err != PICL_SUCCESS)
8357c478bd9Sstevel@tonic-gate 		envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
8367c478bd9Sstevel@tonic-gate }
837