xref: /illumos-gate/usr/src/cmd/picl/plugins/sun4u/psvc/psvcpolicy/psvcpolicy.c (revision 2a8bcb4efb45d99ac41c94a75c396b362c414f7f)
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
5*31925ed2Sanbui  * Common Development and Distribution License (the "License").
6*31925ed2Sanbui  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
20*31925ed2Sanbui  *
21*31925ed2Sanbui  *
22*31925ed2Sanbui  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
24*31925ed2Sanbui  *
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*
287c478bd9Sstevel@tonic-gate  * This file contains routines to support the Platform Services Plugin
297c478bd9Sstevel@tonic-gate  * These routines implement the platform independent environment monitoring
307c478bd9Sstevel@tonic-gate  * and control policies that may be invoked by a daemon thread within
317c478bd9Sstevel@tonic-gate  * the plugin
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <syslog.h>
357c478bd9Sstevel@tonic-gate #include <unistd.h>
367c478bd9Sstevel@tonic-gate #include <stdio.h>
377c478bd9Sstevel@tonic-gate #include <stdlib.h>
387c478bd9Sstevel@tonic-gate #include <libintl.h>
397c478bd9Sstevel@tonic-gate #include <errno.h>
407c478bd9Sstevel@tonic-gate #include <fcntl.h>
417c478bd9Sstevel@tonic-gate #include <strings.h>
427c478bd9Sstevel@tonic-gate #include <libintl.h>
437c478bd9Sstevel@tonic-gate #include <sys/types.h>
44298b7f4cSjfrank #include <string.h>
45298b7f4cSjfrank #include <limits.h>
467c478bd9Sstevel@tonic-gate #include <picl.h>
477c478bd9Sstevel@tonic-gate #include <picltree.h>
487c478bd9Sstevel@tonic-gate #include <sys/types.h>
497c478bd9Sstevel@tonic-gate #include <string.h>
507c478bd9Sstevel@tonic-gate #include <psvc_objects.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #define	LOWTEMP_CRITICAL_MSG		\
537c478bd9Sstevel@tonic-gate 	gettext("CRITICAL : LOW TEMPERATURE DETECTED %d, %s")
547c478bd9Sstevel@tonic-gate #define	LOWTEMP_WARNING_MSG		\
557c478bd9Sstevel@tonic-gate 	gettext("WARNING : LOW TEMPERATURE DETECTED %d, %s")
567c478bd9Sstevel@tonic-gate #define	HIGHTEMP_CRITICAL_MSG		\
577c478bd9Sstevel@tonic-gate 	gettext("CRITICAL : HIGH TEMPERATURE DETECTED %d, %s")
587c478bd9Sstevel@tonic-gate #define	HIGHTEMP_WARNING_MSG		\
597c478bd9Sstevel@tonic-gate 	gettext("WARNING : HIGH TEMPERATURE DETECTED %d, %s")
607c478bd9Sstevel@tonic-gate #define	DEVICE_INSERTED_MSG	gettext("Device %s inserted")
617c478bd9Sstevel@tonic-gate #define	DEVICE_REMOVED_MSG	gettext("Device %s removed")
627c478bd9Sstevel@tonic-gate #define	DEVICE_FAILURE_MSG		\
637c478bd9Sstevel@tonic-gate 	gettext("CRITICAL: Device %s failure detected by sensor %s\n")
647c478bd9Sstevel@tonic-gate #define	DEVICE_OK_MSG	gettext("Device %s OK")
657c478bd9Sstevel@tonic-gate #define	SECONDARY_FAN_FAIL_MSG	gettext("Secondary fan failure, device %s")
667c478bd9Sstevel@tonic-gate #define	KEYSWITCH_POS_READ_FAILED_MSG	\
677c478bd9Sstevel@tonic-gate 	gettext("Keyswitch position could not be determined")
687c478bd9Sstevel@tonic-gate #define	KEYSWITCH_POS_CHANGED_MSG gettext("Keyswitch position changed to %s")
697c478bd9Sstevel@tonic-gate #define	GET_PRESENCE_FAILED_MSG		\
707c478bd9Sstevel@tonic-gate 	gettext("Failed to get presence attribute, id = %s, errno = %d\n")
717c478bd9Sstevel@tonic-gate #define	GET_SENSOR_FAILED_MSG		\
727c478bd9Sstevel@tonic-gate 	gettext("Failed to get sensor value, id = %s, errno = %d\n")
737c478bd9Sstevel@tonic-gate #define	PS_OVER_CURRENT_MSG		\
747c478bd9Sstevel@tonic-gate 	gettext("WARNING: Power Supply overcurrent detected for %s\n")
757c478bd9Sstevel@tonic-gate #define	SET_LED_FAILED_MSG		\
767c478bd9Sstevel@tonic-gate 	gettext("Failed to set LED state, id = %s, errno = %d\n")
777c478bd9Sstevel@tonic-gate #define	SET_FANSPEED_FAILED_MSG		\
787c478bd9Sstevel@tonic-gate 	gettext("Failed to set fan speed, id = %s, errno = %d\n")
797c478bd9Sstevel@tonic-gate #define	FAN_MISSING_MSG			\
807c478bd9Sstevel@tonic-gate 	gettext("WARNING: Fan missing, id = %s\n")
817c478bd9Sstevel@tonic-gate #define	TEMP_SENSOR_FAULT		\
827c478bd9Sstevel@tonic-gate 	gettext("WARNING: Temperature Sensor %s returning faulty temp\n")
837c478bd9Sstevel@tonic-gate #define	TEMP_OFFSET	17
847c478bd9Sstevel@tonic-gate 
857c478bd9Sstevel@tonic-gate static char *shutdown_string = "shutdown -y -g 60 -i 5 \"OVERTEMP condition\"";
867c478bd9Sstevel@tonic-gate 
877c478bd9Sstevel@tonic-gate static int cpus_online = 0;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate typedef struct seg_desc {
907c478bd9Sstevel@tonic-gate 	int32_t segdesc;
917c478bd9Sstevel@tonic-gate 	int16_t segoffset;
927c478bd9Sstevel@tonic-gate 	int16_t seglength;
937c478bd9Sstevel@tonic-gate } seg_desc_t;
947c478bd9Sstevel@tonic-gate 
957c478bd9Sstevel@tonic-gate static int32_t threshold_names[] = {
967c478bd9Sstevel@tonic-gate 	PSVC_HW_LO_SHUT_ATTR,
977c478bd9Sstevel@tonic-gate 	PSVC_LO_SHUT_ATTR,
987c478bd9Sstevel@tonic-gate 	PSVC_LO_WARN_ATTR,
997c478bd9Sstevel@tonic-gate 	PSVC_NOT_USED,			/* LOW MODE which is not used */
1007c478bd9Sstevel@tonic-gate 	PSVC_OPTIMAL_TEMP_ATTR,
1017c478bd9Sstevel@tonic-gate 	PSVC_HI_WARN_ATTR,
1027c478bd9Sstevel@tonic-gate 	PSVC_HI_SHUT_ATTR,
1037c478bd9Sstevel@tonic-gate 	PSVC_HW_HI_SHUT_ATTR
1047c478bd9Sstevel@tonic-gate };
1057c478bd9Sstevel@tonic-gate 
106298b7f4cSjfrank /*
107298b7f4cSjfrank  * The I2C bus is noisy, and the state may be incorrectly reported as
108298b7f4cSjfrank  * having changed.  When the state changes, we attempt to confirm by
109298b7f4cSjfrank  * retrying.  If any retries indicate that the state has not changed, we
110298b7f4cSjfrank  * assume the state change(s) were incorrect and the state has not changed.
111298b7f4cSjfrank  * The following variables are used to store the tuneable values read in
112298b7f4cSjfrank  * from the optional i2cparam.conf file for this shared object library.
113298b7f4cSjfrank  */
114298b7f4cSjfrank static int n_read_temp = PSVC_THRESHOLD_COUNTER;
115298b7f4cSjfrank static int n_retry_keyswitch = PSVC_NUM_OF_RETRIES;
116298b7f4cSjfrank static int retry_sleep_keyswitch = 1;
117298b7f4cSjfrank static int n_retry_hotplug = PSVC_NUM_OF_RETRIES;
118298b7f4cSjfrank static int retry_sleep_hotplug = 1;
119298b7f4cSjfrank static int n_retry_fan_hotplug = PSVC_NUM_OF_RETRIES;
120298b7f4cSjfrank static int retry_sleep_fan_hotplug = 1;
121298b7f4cSjfrank static int n_retry_fan_present = PSVC_NUM_OF_RETRIES;
122298b7f4cSjfrank static int retry_sleep_fan_present = 1;
123298b7f4cSjfrank 
124298b7f4cSjfrank typedef struct {
125298b7f4cSjfrank 	int *pvar;
126298b7f4cSjfrank 	char *texttag;
127298b7f4cSjfrank } i2c_noise_param_t;
128298b7f4cSjfrank 
129298b7f4cSjfrank static i2c_noise_param_t i2cparams_sun4u[] = {
130298b7f4cSjfrank 	&n_read_temp, "n_read_temp",
131298b7f4cSjfrank 	&n_retry_keyswitch, "n_retry_keyswitch",
132298b7f4cSjfrank 	&retry_sleep_keyswitch, "retry_sleep_keyswitch",
133298b7f4cSjfrank 	&n_retry_hotplug, "n_retry_hotplug",
134298b7f4cSjfrank 	&retry_sleep_hotplug, "retry_sleep_hotplug",
135298b7f4cSjfrank 	&n_retry_fan_hotplug, "n_retry_fan_hotplug",
136298b7f4cSjfrank 	&retry_sleep_fan_hotplug, "retry_sleep_fan_hotplug",
137298b7f4cSjfrank 	&n_retry_fan_present, "n_retry_fan_present",
138298b7f4cSjfrank 	&retry_sleep_fan_present, "retry_sleep_fan_present",
139298b7f4cSjfrank 	NULL, NULL
140298b7f4cSjfrank };
141298b7f4cSjfrank 
142298b7f4cSjfrank #pragma init(i2cparams_sun4u_load)
143298b7f4cSjfrank 
144298b7f4cSjfrank static void
i2cparams_sun4u_debug(i2c_noise_param_t * pi2cparams,int usingDefaults)145298b7f4cSjfrank i2cparams_sun4u_debug(i2c_noise_param_t *pi2cparams, int usingDefaults)
146298b7f4cSjfrank {
147298b7f4cSjfrank 	char s[128];
148298b7f4cSjfrank 	i2c_noise_param_t *p;
149298b7f4cSjfrank 
150298b7f4cSjfrank 	if (!usingDefaults) {
151298b7f4cSjfrank 		(void) strncpy(s,
152298b7f4cSjfrank 		    "# Values from /usr/platform/sun4u/lib/i2cparam.conf\n",
153298b7f4cSjfrank 			sizeof (s) - 1);
154298b7f4cSjfrank 		syslog(LOG_WARNING, "%s", s);
155298b7f4cSjfrank 	} else {
156298b7f4cSjfrank 		/* no file - we're using the defaults */
157298b7f4cSjfrank 		(void) strncpy(s,
158298b7f4cSjfrank "# No /usr/platform/sun4u/lib/i2cparam.conf file, using defaults\n",
159298b7f4cSjfrank 			sizeof (s) - 1);
160298b7f4cSjfrank 	}
161298b7f4cSjfrank 	(void) fputs(s, stdout);
162298b7f4cSjfrank 	p = pi2cparams;
163298b7f4cSjfrank 	while (p->pvar != NULL) {
164298b7f4cSjfrank 		(void) snprintf(s, sizeof (s), "%s %d\n", p->texttag,
165298b7f4cSjfrank 		    *(p->pvar));
166298b7f4cSjfrank 		if (!usingDefaults)
167298b7f4cSjfrank 			syslog(LOG_WARNING, "%s", s);
168298b7f4cSjfrank 		(void) fputs(s, stdout);
169298b7f4cSjfrank 		p++;
170298b7f4cSjfrank 	}
171298b7f4cSjfrank }
172298b7f4cSjfrank 
173298b7f4cSjfrank static void
i2cparams_sun4u_load(void)174298b7f4cSjfrank i2cparams_sun4u_load(void)
175298b7f4cSjfrank {
176298b7f4cSjfrank 	FILE *fp;
177298b7f4cSjfrank 	char *filename = "/usr/platform/sun4u/lib/i2cparam.conf";
178298b7f4cSjfrank 	char s[128];
179298b7f4cSjfrank 	char var[128];
180298b7f4cSjfrank 	int val;
181298b7f4cSjfrank 	i2c_noise_param_t *p;
182298b7f4cSjfrank 
183298b7f4cSjfrank 	/* read thru the i2cparam.conf file and set variables */
184298b7f4cSjfrank 	if ((fp = fopen(filename, "r")) != NULL) {
185298b7f4cSjfrank 		while (fgets(s, sizeof (s), fp) != NULL) {
186298b7f4cSjfrank 			if (s[0] == '#') /* skip comment lines */
187298b7f4cSjfrank 				continue;
188298b7f4cSjfrank 			/* try to find a string match and get the value */
189298b7f4cSjfrank 			if (sscanf(s, "%127s %d", var, &val) != 2)
190298b7f4cSjfrank 				continue;
191298b7f4cSjfrank 			if (val < 1)
192298b7f4cSjfrank 				val = 1;  /* clamp min value */
193298b7f4cSjfrank 			p = &(i2cparams_sun4u[0]);
194298b7f4cSjfrank 			while (p->pvar != NULL) {
195298b7f4cSjfrank 				if (strncmp(p->texttag, var, sizeof (var)) ==
196298b7f4cSjfrank 				    0) {
197298b7f4cSjfrank 					*(p->pvar) = val;
198298b7f4cSjfrank 					break;
199298b7f4cSjfrank 				}
200298b7f4cSjfrank 				p++;
201298b7f4cSjfrank 			}
202298b7f4cSjfrank 		}
203298b7f4cSjfrank 		(void) fclose(fp);
204298b7f4cSjfrank 	}
205298b7f4cSjfrank 	/* output the values of the parameters */
206298b7f4cSjfrank 	i2cparams_sun4u_debug(&(i2cparams_sun4u[0]), ((fp == NULL)? 1 : 0));
207298b7f4cSjfrank }
208298b7f4cSjfrank 
209298b7f4cSjfrank 
2107c478bd9Sstevel@tonic-gate int32_t
psvc_update_thresholds_0(psvc_opaque_t hdlp,char * id)2117c478bd9Sstevel@tonic-gate psvc_update_thresholds_0(psvc_opaque_t hdlp, char *id)
2127c478bd9Sstevel@tonic-gate {
2137c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
2147c478bd9Sstevel@tonic-gate 	fru_info_t fru_data;
2157c478bd9Sstevel@tonic-gate 	char *fru, seg_name[2];
2167c478bd9Sstevel@tonic-gate 	int8_t seg_count, temp_array[8];
2177c478bd9Sstevel@tonic-gate 	int32_t match_count, i, j, seg_desc_start = 0x1806, temp_address;
2187c478bd9Sstevel@tonic-gate 	int32_t seg_found, temp;
2197c478bd9Sstevel@tonic-gate 	boolean_t present;
2207c478bd9Sstevel@tonic-gate 	seg_desc_t segment;
2217c478bd9Sstevel@tonic-gate 
2227c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &present);
2237c478bd9Sstevel@tonic-gate 	if ((status != PSVC_SUCCESS) || (present != PSVC_PRESENT))
2247c478bd9Sstevel@tonic-gate 		return (status);
2257c478bd9Sstevel@tonic-gate 
2267c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_ASSOC_MATCHES_ATTR, &match_count,
2277c478bd9Sstevel@tonic-gate 	    PSVC_FRU);
2287c478bd9Sstevel@tonic-gate 	if (status == PSVC_FAILURE)
2297c478bd9Sstevel@tonic-gate 		return (status);
2307c478bd9Sstevel@tonic-gate 
2317c478bd9Sstevel@tonic-gate 	for (i = 0; i < match_count; i++) {
2327c478bd9Sstevel@tonic-gate 		seg_found = 0;
2337c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR,
2347c478bd9Sstevel@tonic-gate 		    &fru, PSVC_FRU, i);
2357c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
2367c478bd9Sstevel@tonic-gate 			return (status);
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate 		fru_data.buf_start = 0x1805;
2397c478bd9Sstevel@tonic-gate 		fru_data.buf = (char *)&seg_count;
2407c478bd9Sstevel@tonic-gate 		fru_data.read_size = 1;
2417c478bd9Sstevel@tonic-gate 
2427c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, fru, PSVC_FRU_INFO_ATTR,
2437c478bd9Sstevel@tonic-gate 		    &fru_data);
2447c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS) {
2457c478bd9Sstevel@tonic-gate 			return (status);
2467c478bd9Sstevel@tonic-gate 		}
2477c478bd9Sstevel@tonic-gate 		for (j = 0; (j < seg_count) && (!seg_found); j++) {
2487c478bd9Sstevel@tonic-gate 			fru_data.buf_start = seg_desc_start;
2497c478bd9Sstevel@tonic-gate 			fru_data.buf = seg_name;
2507c478bd9Sstevel@tonic-gate 			fru_data.read_size = 2;
2517c478bd9Sstevel@tonic-gate 
2527c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, fru, PSVC_FRU_INFO_ATTR,
2537c478bd9Sstevel@tonic-gate 			    &fru_data);
2547c478bd9Sstevel@tonic-gate 
2557c478bd9Sstevel@tonic-gate 			seg_desc_start = seg_desc_start + 2;
2567c478bd9Sstevel@tonic-gate 			fru_data.buf_start = seg_desc_start;
2577c478bd9Sstevel@tonic-gate 			fru_data.buf = (char *)&segment;
2587c478bd9Sstevel@tonic-gate 			fru_data.read_size = sizeof (seg_desc_t);
2597c478bd9Sstevel@tonic-gate 
2607c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, fru, PSVC_FRU_INFO_ATTR,
2617c478bd9Sstevel@tonic-gate 			    &fru_data);
2627c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS) {
2637c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
2647c478bd9Sstevel@tonic-gate 				    "Failed psvc_get_attr for FRU info\n");
2657c478bd9Sstevel@tonic-gate 				return (status);
2667c478bd9Sstevel@tonic-gate 			}
2677c478bd9Sstevel@tonic-gate 			seg_desc_start = seg_desc_start + sizeof (seg_desc_t);
2687c478bd9Sstevel@tonic-gate 			if (memcmp(seg_name, "SC", 2) == 0)
2697c478bd9Sstevel@tonic-gate 				seg_found = 1;
2707c478bd9Sstevel@tonic-gate 		}
2717c478bd9Sstevel@tonic-gate 		if (seg_found) {
2727c478bd9Sstevel@tonic-gate 			temp_address = segment.segoffset + TEMP_OFFSET;
2737c478bd9Sstevel@tonic-gate 			fru_data.buf_start = temp_address;
2747c478bd9Sstevel@tonic-gate 			fru_data.buf = (char *)&temp_array;
2757c478bd9Sstevel@tonic-gate 			fru_data.read_size = sizeof (temp_array);
2767c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, fru, PSVC_FRU_INFO_ATTR,
2777c478bd9Sstevel@tonic-gate 			    &fru_data);
2787c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS) {
2797c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
2807c478bd9Sstevel@tonic-gate 				    "Failed psvc_get_attr for FRU info\n");
2817c478bd9Sstevel@tonic-gate 				return (status);
2827c478bd9Sstevel@tonic-gate 			} else {
2837c478bd9Sstevel@tonic-gate 				for (j = 0; j < sizeof (temp_array); j++) {
2847c478bd9Sstevel@tonic-gate 					if (threshold_names[j] == PSVC_NOT_USED)
2857c478bd9Sstevel@tonic-gate 						continue;
2867c478bd9Sstevel@tonic-gate 					temp = temp_array[j];
2877c478bd9Sstevel@tonic-gate 					status = psvc_set_attr(hdlp, id,
2887c478bd9Sstevel@tonic-gate 					    threshold_names[j], &temp);
2897c478bd9Sstevel@tonic-gate 					if (status != PSVC_SUCCESS) {
2907c478bd9Sstevel@tonic-gate 						return (status);
2917c478bd9Sstevel@tonic-gate 					}
2927c478bd9Sstevel@tonic-gate 				}
2937c478bd9Sstevel@tonic-gate 			}
2947c478bd9Sstevel@tonic-gate 		} else {
2957c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "No FRU Information for %s"
2967c478bd9Sstevel@tonic-gate 			    " using default temperatures\n", id);
2977c478bd9Sstevel@tonic-gate 		}
2987c478bd9Sstevel@tonic-gate 	}
2997c478bd9Sstevel@tonic-gate 	return (status);
3007c478bd9Sstevel@tonic-gate }
3017c478bd9Sstevel@tonic-gate 
3027c478bd9Sstevel@tonic-gate #define	MAX_TEMP_SENSORS	256
3037c478bd9Sstevel@tonic-gate 
3047c478bd9Sstevel@tonic-gate static int32_t
check_temp(psvc_opaque_t hdlp,char * id,int32_t silent)3057c478bd9Sstevel@tonic-gate check_temp(psvc_opaque_t hdlp, char *id, int32_t silent)
3067c478bd9Sstevel@tonic-gate {
3077c478bd9Sstevel@tonic-gate 	int32_t		lo_warn, hi_warn, lo_shut, hi_shut;
3087c478bd9Sstevel@tonic-gate 	uint64_t	features;
3097c478bd9Sstevel@tonic-gate 	int32_t		temp;
3107c478bd9Sstevel@tonic-gate 	char		previous_state[32];
3117c478bd9Sstevel@tonic-gate 	char		led_state[32];
3127c478bd9Sstevel@tonic-gate 	char		state[32];
3137c478bd9Sstevel@tonic-gate 	char		fault[32];
3147c478bd9Sstevel@tonic-gate 	char		label[32];
3157c478bd9Sstevel@tonic-gate 	boolean_t	pr;
3167c478bd9Sstevel@tonic-gate 	int32_t		status = PSVC_SUCCESS;
3177c478bd9Sstevel@tonic-gate 	int8_t		fail = 0;
318298b7f4cSjfrank 	static int	threshold_low_shut[MAX_TEMP_SENSORS] = {0};
319298b7f4cSjfrank 	static int	threshold_high_shut[MAX_TEMP_SENSORS] = {0};
320298b7f4cSjfrank 	static int	threshold_low_warn[MAX_TEMP_SENSORS] = {0};
321298b7f4cSjfrank 	static int	threshold_high_warn[MAX_TEMP_SENSORS] = {0};
3227c478bd9Sstevel@tonic-gate 	int32_t		instance;
3237c478bd9Sstevel@tonic-gate 
3247c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_INSTANCE_ATTR, &instance);
3257c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
3267c478bd9Sstevel@tonic-gate 		return (status);
3277c478bd9Sstevel@tonic-gate 
3287c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &pr);
3297c478bd9Sstevel@tonic-gate 	if ((status != PSVC_SUCCESS) || (pr != PSVC_PRESENT)) {
3307c478bd9Sstevel@tonic-gate 		return (status);
3317c478bd9Sstevel@tonic-gate 	}
3327c478bd9Sstevel@tonic-gate 
3337c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_STATE_ATTR, state);
3347c478bd9Sstevel@tonic-gate 	if (status == PSVC_FAILURE)
3357c478bd9Sstevel@tonic-gate 		return (status);
3367c478bd9Sstevel@tonic-gate 
3377c478bd9Sstevel@tonic-gate 	if ((strcmp(state, PSVC_HOTPLUGGED) == 0)) {
3387c478bd9Sstevel@tonic-gate 		return (PSVC_SUCCESS);
3397c478bd9Sstevel@tonic-gate 	}
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_FEATURES_ATTR, &features);
3427c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
3437c478bd9Sstevel@tonic-gate 		return (status);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_LO_WARN_ATTR, &lo_warn);
3467c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
3477c478bd9Sstevel@tonic-gate 		return (status);
3487c478bd9Sstevel@tonic-gate 
3497c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_LO_SHUT_ATTR, &lo_shut);
3507c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
3517c478bd9Sstevel@tonic-gate 		return (status);
3527c478bd9Sstevel@tonic-gate 
3537c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_HI_WARN_ATTR, &hi_warn);
3547c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
3557c478bd9Sstevel@tonic-gate 		return (status);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_HI_SHUT_ATTR, &hi_shut);
3587c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
3597c478bd9Sstevel@tonic-gate 		return (status);
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_SENSOR_VALUE_ATTR, &temp);
3627c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS) {
3637c478bd9Sstevel@tonic-gate 		return (status);
3647c478bd9Sstevel@tonic-gate 	}
3657c478bd9Sstevel@tonic-gate 
3667c478bd9Sstevel@tonic-gate 	/*
3677c478bd9Sstevel@tonic-gate 	 * The following code is to check to see if the temp sensor is
3687c478bd9Sstevel@tonic-gate 	 * returning a faulty reading due to it either being bad or the
3697c478bd9Sstevel@tonic-gate 	 * CPU being powered off for some reason. Is so we will alert the user
3707c478bd9Sstevel@tonic-gate 	 * and just label the sensor bad but not the WHOLE CPU module.
3717c478bd9Sstevel@tonic-gate 	 */
3727c478bd9Sstevel@tonic-gate 	if ((temp == 127) && (strcmp(state, PSVC_ERROR) != 0)) {
3737c478bd9Sstevel@tonic-gate 		status = psvc_set_attr(hdlp, id, PSVC_STATE_ATTR, PSVC_ERROR);
3747c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
3757c478bd9Sstevel@tonic-gate 			return (status);
3767c478bd9Sstevel@tonic-gate 		status = psvc_set_attr(hdlp, id, PSVC_FAULTID_ATTR,
3777c478bd9Sstevel@tonic-gate 		    PSVC_GEN_FAULT);
3787c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
3797c478bd9Sstevel@tonic-gate 			return (status);
3807c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, TEMP_SENSOR_FAULT, id);
3817c478bd9Sstevel@tonic-gate 		return (status);
3827c478bd9Sstevel@tonic-gate 	}
3837c478bd9Sstevel@tonic-gate 
3847c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_LABEL_ATTR, label);
3857c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
3867c478bd9Sstevel@tonic-gate 		return (status);
3877c478bd9Sstevel@tonic-gate 
3887c478bd9Sstevel@tonic-gate 	/*
3897c478bd9Sstevel@tonic-gate 	 * if any of the four temperature states (lo_shut, lo_warn,
3907c478bd9Sstevel@tonic-gate 	 * hi_shut, hi_warn) is detected we will not take an action
391298b7f4cSjfrank 	 * until the number of similar back-to-back readings equals
392298b7f4cSjfrank 	 * 'n_read_temp' (default is PSVC_THRESHOLD_COUNTER).
3937c478bd9Sstevel@tonic-gate 	 */
3947c478bd9Sstevel@tonic-gate 	if ((features & PSVC_LOW_SHUT) && temp < lo_shut) {
3957c478bd9Sstevel@tonic-gate 		/*
3967c478bd9Sstevel@tonic-gate 		 * once we are in one state, clear all the
3977c478bd9Sstevel@tonic-gate 		 * counters for the other three states since
3987c478bd9Sstevel@tonic-gate 		 * back-to-back readings of these other three
3997c478bd9Sstevel@tonic-gate 		 * states could not happen anymore.
4007c478bd9Sstevel@tonic-gate 		 */
4017c478bd9Sstevel@tonic-gate 		threshold_low_warn[instance] = 0;
4027c478bd9Sstevel@tonic-gate 		threshold_high_shut[instance] = 0;
4037c478bd9Sstevel@tonic-gate 		threshold_high_warn[instance] = 0;
4047c478bd9Sstevel@tonic-gate 		threshold_low_shut[instance]++;
405298b7f4cSjfrank 		if (threshold_low_shut[instance] == n_read_temp) {
4067c478bd9Sstevel@tonic-gate 			threshold_low_shut[instance] = 0;
4077c478bd9Sstevel@tonic-gate 			fail = 1;
4087c478bd9Sstevel@tonic-gate 			strcpy(state, PSVC_ERROR);
4097c478bd9Sstevel@tonic-gate 			strcpy(fault, PSVC_TEMP_LO_SHUT);
4107c478bd9Sstevel@tonic-gate 			strcpy(led_state, PSVC_LED_ON);
4117c478bd9Sstevel@tonic-gate 			if (silent == 0)
4127c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, LOWTEMP_CRITICAL_MSG,
4137c478bd9Sstevel@tonic-gate 				    temp, label);
4147c478bd9Sstevel@tonic-gate 		} else { /* Threshold for showing error not reached */
4157c478bd9Sstevel@tonic-gate 			return (PSVC_SUCCESS);
4167c478bd9Sstevel@tonic-gate 		}
4177c478bd9Sstevel@tonic-gate 	} else if ((features & PSVC_LOW_WARN) && temp < lo_warn) {
4187c478bd9Sstevel@tonic-gate 		threshold_low_shut[instance] = 0;
4197c478bd9Sstevel@tonic-gate 		threshold_high_shut[instance] = 0;
4207c478bd9Sstevel@tonic-gate 		threshold_high_warn[instance] = 0;
4217c478bd9Sstevel@tonic-gate 		threshold_low_warn[instance]++;
422298b7f4cSjfrank 		if (threshold_low_warn[instance] == n_read_temp) {
4237c478bd9Sstevel@tonic-gate 			threshold_low_warn[instance] = 0;
4247c478bd9Sstevel@tonic-gate 			fail = 1;
4257c478bd9Sstevel@tonic-gate 			strcpy(state, PSVC_ERROR);
4267c478bd9Sstevel@tonic-gate 			strcpy(fault, PSVC_TEMP_LO_WARN);
4277c478bd9Sstevel@tonic-gate 			strcpy(led_state, PSVC_LED_ON);
4287c478bd9Sstevel@tonic-gate 			if (silent == 0)
4297c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, LOWTEMP_WARNING_MSG,
4307c478bd9Sstevel@tonic-gate 				    temp, label);
4317c478bd9Sstevel@tonic-gate 		} else { /* Threshold for showing error not reached */
4327c478bd9Sstevel@tonic-gate 			return (PSVC_SUCCESS);
4337c478bd9Sstevel@tonic-gate 		}
4347c478bd9Sstevel@tonic-gate 	} else if ((features & PSVC_HIGH_SHUT) && temp > hi_shut) {
4357c478bd9Sstevel@tonic-gate 		threshold_low_warn[instance] = 0;
4367c478bd9Sstevel@tonic-gate 		threshold_low_shut[instance] = 0;
4377c478bd9Sstevel@tonic-gate 		threshold_high_warn[instance] = 0;
4387c478bd9Sstevel@tonic-gate 		threshold_high_shut[instance]++;
439298b7f4cSjfrank 		if (threshold_high_shut[instance] == n_read_temp) {
4407c478bd9Sstevel@tonic-gate 			threshold_high_shut[instance] = 0;
4417c478bd9Sstevel@tonic-gate 			fail = 1;
4427c478bd9Sstevel@tonic-gate 			strcpy(state, PSVC_ERROR);
4437c478bd9Sstevel@tonic-gate 			strcpy(fault, PSVC_TEMP_HI_SHUT);
4447c478bd9Sstevel@tonic-gate 			strcpy(led_state, PSVC_LED_ON);
4457c478bd9Sstevel@tonic-gate 			if (silent == 0)
4467c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, HIGHTEMP_CRITICAL_MSG,
4477c478bd9Sstevel@tonic-gate 				    temp, label);
4487c478bd9Sstevel@tonic-gate 		} else { /* Threshold for showing error not reached */
4497c478bd9Sstevel@tonic-gate 			return (PSVC_SUCCESS);
4507c478bd9Sstevel@tonic-gate 		}
4517c478bd9Sstevel@tonic-gate 	} else if ((features & PSVC_HIGH_WARN) && temp > hi_warn) {
4527c478bd9Sstevel@tonic-gate 		threshold_low_warn[instance] = 0;
4537c478bd9Sstevel@tonic-gate 		threshold_low_shut[instance] = 0;
4547c478bd9Sstevel@tonic-gate 		threshold_high_shut[instance] = 0;
4557c478bd9Sstevel@tonic-gate 		threshold_high_warn[instance]++;
456298b7f4cSjfrank 		if (threshold_high_warn[instance] == n_read_temp) {
4577c478bd9Sstevel@tonic-gate 			threshold_high_warn[instance] = 0;
4587c478bd9Sstevel@tonic-gate 			fail = 1;
4597c478bd9Sstevel@tonic-gate 			strcpy(state, PSVC_ERROR);
4607c478bd9Sstevel@tonic-gate 			strcpy(fault, PSVC_TEMP_HI_WARN);
4617c478bd9Sstevel@tonic-gate 			strcpy(led_state, PSVC_LED_ON);
4627c478bd9Sstevel@tonic-gate 			if (silent == 0)
4637c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, HIGHTEMP_WARNING_MSG,
4647c478bd9Sstevel@tonic-gate 				    temp, label);
4657c478bd9Sstevel@tonic-gate 		} else { /* Threshold for showing error not reached */
4667c478bd9Sstevel@tonic-gate 			return (PSVC_SUCCESS);
4677c478bd9Sstevel@tonic-gate 		}
4687c478bd9Sstevel@tonic-gate 	}
4697c478bd9Sstevel@tonic-gate 
4707c478bd9Sstevel@tonic-gate 	/*
4717c478bd9Sstevel@tonic-gate 	 * If we reached this point then that means that we are either
472298b7f4cSjfrank 	 * okay, or we have showed error n_read_temp times.
4737c478bd9Sstevel@tonic-gate 	 */
4747c478bd9Sstevel@tonic-gate 	if (fail != 1) {
4757c478bd9Sstevel@tonic-gate 		/* within limits */
4767c478bd9Sstevel@tonic-gate 		strcpy(state, PSVC_OK);
4777c478bd9Sstevel@tonic-gate 		strcpy(fault, PSVC_NO_FAULT);
4787c478bd9Sstevel@tonic-gate 		strcpy(led_state, PSVC_LED_OFF);
4797c478bd9Sstevel@tonic-gate 	}
4807c478bd9Sstevel@tonic-gate 
4817c478bd9Sstevel@tonic-gate 	status = psvc_set_attr(hdlp, id, PSVC_STATE_ATTR, state);
4827c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
4837c478bd9Sstevel@tonic-gate 		return (status);
4847c478bd9Sstevel@tonic-gate 	status = psvc_set_attr(hdlp, id, PSVC_FAULTID_ATTR, fault);
4857c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
4867c478bd9Sstevel@tonic-gate 		return (status);
4877c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_PREV_STATE_ATTR,
4887c478bd9Sstevel@tonic-gate 		previous_state);
4897c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
4907c478bd9Sstevel@tonic-gate 		return (status);
4917c478bd9Sstevel@tonic-gate 
4927c478bd9Sstevel@tonic-gate 	if (strcmp(previous_state, state) != 0) {
4937c478bd9Sstevel@tonic-gate 		char *led_id;
4947c478bd9Sstevel@tonic-gate 		int32_t led_count;
4957c478bd9Sstevel@tonic-gate 		int32_t i;
4967c478bd9Sstevel@tonic-gate 
4977c478bd9Sstevel@tonic-gate 		/* change state of fault LEDs */
4987c478bd9Sstevel@tonic-gate 		psvc_get_attr(hdlp, id, PSVC_ASSOC_MATCHES_ATTR, &led_count,
4997c478bd9Sstevel@tonic-gate 			PSVC_TS_OVERTEMP_LED);
5007c478bd9Sstevel@tonic-gate 		for (i = 0; i < led_count; ++i) {
5017c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, id,
5027c478bd9Sstevel@tonic-gate 				PSVC_ASSOC_ID_ATTR, &led_id,
5037c478bd9Sstevel@tonic-gate 				PSVC_TS_OVERTEMP_LED, i);
5047c478bd9Sstevel@tonic-gate 			if (status == PSVC_FAILURE)
5057c478bd9Sstevel@tonic-gate 				return (status);
5067c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, led_id,
5077c478bd9Sstevel@tonic-gate 				PSVC_LED_STATE_ATTR, led_state);
5087c478bd9Sstevel@tonic-gate 			if (status == PSVC_FAILURE)
5097c478bd9Sstevel@tonic-gate 				return (status);
5107c478bd9Sstevel@tonic-gate 		}
5117c478bd9Sstevel@tonic-gate 	}
5127c478bd9Sstevel@tonic-gate 
5137c478bd9Sstevel@tonic-gate 	return (PSVC_SUCCESS);
5147c478bd9Sstevel@tonic-gate }
5157c478bd9Sstevel@tonic-gate 
5167c478bd9Sstevel@tonic-gate int32_t
psvc_check_temperature_policy_0(psvc_opaque_t hdlp,char * id)5177c478bd9Sstevel@tonic-gate psvc_check_temperature_policy_0(psvc_opaque_t hdlp, char *id)
5187c478bd9Sstevel@tonic-gate {
5197c478bd9Sstevel@tonic-gate 	return (check_temp(hdlp, id, 0));
5207c478bd9Sstevel@tonic-gate }
5217c478bd9Sstevel@tonic-gate 
5227c478bd9Sstevel@tonic-gate int32_t
psvc_check_temperature_silent_policy_0(psvc_opaque_t hdlp,char * id)5237c478bd9Sstevel@tonic-gate psvc_check_temperature_silent_policy_0(psvc_opaque_t hdlp, char *id)
5247c478bd9Sstevel@tonic-gate {
5257c478bd9Sstevel@tonic-gate 	return (check_temp(hdlp, id, 1));
5267c478bd9Sstevel@tonic-gate }
5277c478bd9Sstevel@tonic-gate 
5287c478bd9Sstevel@tonic-gate int32_t
psvc_fan_enable_disable_policy_0(psvc_opaque_t hdlp,char * id)5297c478bd9Sstevel@tonic-gate psvc_fan_enable_disable_policy_0(psvc_opaque_t hdlp, char *id)
5307c478bd9Sstevel@tonic-gate {
5317c478bd9Sstevel@tonic-gate 	char state[32], previous_state[32];
5327c478bd9Sstevel@tonic-gate 	char *backup_fan;
5337c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
5347c478bd9Sstevel@tonic-gate 	uint64_t features;
5357c478bd9Sstevel@tonic-gate 	char label[32];
5367c478bd9Sstevel@tonic-gate 	boolean_t presence;
5377c478bd9Sstevel@tonic-gate 	boolean_t enable;
538298b7f4cSjfrank 	int retry;
5397c478bd9Sstevel@tonic-gate 
5407c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_FEATURES_ATTR, &features);
5417c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
5427c478bd9Sstevel@tonic-gate 		return (status);
5437c478bd9Sstevel@tonic-gate 
544298b7f4cSjfrank 	retry = 0;
545298b7f4cSjfrank 	do {
546298b7f4cSjfrank 		if (retry)
547298b7f4cSjfrank 			(void) sleep(retry_sleep_fan_present);
548298b7f4cSjfrank 
5497c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &presence);
5507c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
5517c478bd9Sstevel@tonic-gate 			return (status);
552298b7f4cSjfrank 		retry++;
553298b7f4cSjfrank 	} while ((retry < n_retry_fan_present) && (presence == PSVC_ABSENT));
5547c478bd9Sstevel@tonic-gate 
5557c478bd9Sstevel@tonic-gate 	if (presence == PSVC_ABSENT) {
5567c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_LABEL_ATTR, label);
5577c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
5587c478bd9Sstevel@tonic-gate 			return (status);
5597c478bd9Sstevel@tonic-gate 
5607c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_ENABLE_ATTR, &enable);
5617c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
5627c478bd9Sstevel@tonic-gate 			return (status);
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 		if (features & PSVC_DEV_PRIMARY) {
5657c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR,
5667c478bd9Sstevel@tonic-gate 				&backup_fan, PSVC_ALTERNATE, 0);
5677c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
5687c478bd9Sstevel@tonic-gate 				return (status);
5697c478bd9Sstevel@tonic-gate 
5707c478bd9Sstevel@tonic-gate 			enable = PSVC_DISABLED;
5717c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, id, PSVC_ENABLE_ATTR,
5727c478bd9Sstevel@tonic-gate 				&enable);
5737c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
5747c478bd9Sstevel@tonic-gate 				return (status);
5757c478bd9Sstevel@tonic-gate 
5767c478bd9Sstevel@tonic-gate 			enable = PSVC_ENABLED;
5777c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, backup_fan,
5787c478bd9Sstevel@tonic-gate 				PSVC_ENABLE_ATTR, &enable);
5797c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
5807c478bd9Sstevel@tonic-gate 				return (status);
5817c478bd9Sstevel@tonic-gate 		} else {
5827c478bd9Sstevel@tonic-gate 			enable = PSVC_DISABLED;
5837c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, id, PSVC_ENABLE_ATTR,
5847c478bd9Sstevel@tonic-gate 				&enable);
5857c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
5867c478bd9Sstevel@tonic-gate 				return (status);
5877c478bd9Sstevel@tonic-gate 		}
5887c478bd9Sstevel@tonic-gate 		return (PSVC_SUCCESS);
5897c478bd9Sstevel@tonic-gate 	}
5907c478bd9Sstevel@tonic-gate 
5917c478bd9Sstevel@tonic-gate 	/* device was present */
5927c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_STATE_ATTR, state);
5937c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
5947c478bd9Sstevel@tonic-gate 		return (status);
5957c478bd9Sstevel@tonic-gate 
5967c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_PREV_STATE_ATTR, previous_state);
5977c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
5987c478bd9Sstevel@tonic-gate 		return (status);
5997c478bd9Sstevel@tonic-gate 
6007c478bd9Sstevel@tonic-gate 	if (features & PSVC_DEV_PRIMARY) {
6017c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR,
6027c478bd9Sstevel@tonic-gate 			&backup_fan, PSVC_ALTERNATE, 0);
6037c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
6047c478bd9Sstevel@tonic-gate 			return (status);
6057c478bd9Sstevel@tonic-gate 
6067c478bd9Sstevel@tonic-gate 		if (strcmp(state, PSVC_OK) == 0) {
6077c478bd9Sstevel@tonic-gate 			enable = PSVC_ENABLED;
6087c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, id, PSVC_ENABLE_ATTR,
6097c478bd9Sstevel@tonic-gate 				&enable);
6107c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
6117c478bd9Sstevel@tonic-gate 				return (status);
6127c478bd9Sstevel@tonic-gate 
6137c478bd9Sstevel@tonic-gate 			enable = PSVC_DISABLED;
6147c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, backup_fan,
6157c478bd9Sstevel@tonic-gate 				PSVC_ENABLE_ATTR, &enable);
6167c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
6177c478bd9Sstevel@tonic-gate 				return (status);
6187c478bd9Sstevel@tonic-gate 		}
6197c478bd9Sstevel@tonic-gate 		if ((strcmp(state, PSVC_ERROR) == 0) &&
6207c478bd9Sstevel@tonic-gate 			(strcmp(previous_state, PSVC_ERROR) != 0)) {
6217c478bd9Sstevel@tonic-gate 			enable = PSVC_DISABLED;
6227c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, id, PSVC_ENABLE_ATTR,
6237c478bd9Sstevel@tonic-gate 				&enable);
6247c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
6257c478bd9Sstevel@tonic-gate 				return (status);
6267c478bd9Sstevel@tonic-gate 
6277c478bd9Sstevel@tonic-gate 			enable = PSVC_ENABLED;
6287c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, backup_fan,
6297c478bd9Sstevel@tonic-gate 				PSVC_ENABLE_ATTR, &enable);
6307c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
6317c478bd9Sstevel@tonic-gate 				return (status);
6327c478bd9Sstevel@tonic-gate 		}
6337c478bd9Sstevel@tonic-gate 	} else {
6347c478bd9Sstevel@tonic-gate 		if ((strcmp(state, PSVC_ERROR) == 0) &&
6357c478bd9Sstevel@tonic-gate 			(strcmp(previous_state, PSVC_ERROR) != 0)) {
6367c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, id, PSVC_LABEL_ATTR,
6377c478bd9Sstevel@tonic-gate 				label);
6387c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
6397c478bd9Sstevel@tonic-gate 				return (status);
6407c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, SECONDARY_FAN_FAIL_MSG, label);
6417c478bd9Sstevel@tonic-gate 		}
6427c478bd9Sstevel@tonic-gate 	}
6437c478bd9Sstevel@tonic-gate 	return (status);
6447c478bd9Sstevel@tonic-gate }
6457c478bd9Sstevel@tonic-gate 
6467c478bd9Sstevel@tonic-gate /*
6477c478bd9Sstevel@tonic-gate  * psvc_switch_fan_onoff_policy_0
6487c478bd9Sstevel@tonic-gate  * Turn a fan on if it is enabled, turn it off if it is disabled.
6497c478bd9Sstevel@tonic-gate  */
6507c478bd9Sstevel@tonic-gate int32_t
psvc_switch_fan_onoff_policy_0(psvc_opaque_t hdlp,char * id)6517c478bd9Sstevel@tonic-gate psvc_switch_fan_onoff_policy_0(psvc_opaque_t hdlp, char *id)
6527c478bd9Sstevel@tonic-gate {
6537c478bd9Sstevel@tonic-gate 	boolean_t enable;
6547c478bd9Sstevel@tonic-gate 	char *switchid;
6557c478bd9Sstevel@tonic-gate 	char state[32];
6567c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
6577c478bd9Sstevel@tonic-gate 
6587c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_ENABLE_ATTR, &enable);
6597c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
6607c478bd9Sstevel@tonic-gate 		return (status);
6617c478bd9Sstevel@tonic-gate 
6627c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR, &switchid,
6637c478bd9Sstevel@tonic-gate 		PSVC_FAN_ONOFF_SENSOR, 0);
6647c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
6657c478bd9Sstevel@tonic-gate 		return (status);
6667c478bd9Sstevel@tonic-gate 
6677c478bd9Sstevel@tonic-gate 	if (enable == PSVC_DISABLED) {
6687c478bd9Sstevel@tonic-gate 		strcpy(state, PSVC_SWITCH_OFF);
6697c478bd9Sstevel@tonic-gate 	} else {
6707c478bd9Sstevel@tonic-gate 		strcpy(state, PSVC_SWITCH_ON);
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	status = psvc_set_attr(hdlp, switchid, PSVC_SWITCH_STATE_ATTR, state);
6747c478bd9Sstevel@tonic-gate 	return (status);
6757c478bd9Sstevel@tonic-gate }
6767c478bd9Sstevel@tonic-gate 
6777c478bd9Sstevel@tonic-gate static int32_t
check_cpu_temp_fault(psvc_opaque_t hdlp,char * cpu,int32_t cpu_count)6787c478bd9Sstevel@tonic-gate check_cpu_temp_fault(psvc_opaque_t hdlp, char *cpu, int32_t cpu_count)
6797c478bd9Sstevel@tonic-gate {
6807c478bd9Sstevel@tonic-gate 	char *sensorid;
6817c478bd9Sstevel@tonic-gate 	int32_t sensor_count;
6827c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
6837c478bd9Sstevel@tonic-gate 	int32_t i;
6847c478bd9Sstevel@tonic-gate 	uint64_t features;
6857c478bd9Sstevel@tonic-gate 	char fault[32];
6867c478bd9Sstevel@tonic-gate 
6877c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, cpu, PSVC_FEATURES_ATTR, &features);
6887c478bd9Sstevel@tonic-gate 	if (status == PSVC_FAILURE)
6897c478bd9Sstevel@tonic-gate 		return (status);
6907c478bd9Sstevel@tonic-gate 
6917c478bd9Sstevel@tonic-gate 	psvc_get_attr(hdlp, cpu, PSVC_ASSOC_MATCHES_ATTR, &sensor_count,
6927c478bd9Sstevel@tonic-gate 		PSVC_DEV_TEMP_SENSOR);
6937c478bd9Sstevel@tonic-gate 	for (i = 0; i < sensor_count; ++i) {
6947c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, cpu, PSVC_ASSOC_ID_ATTR,
6957c478bd9Sstevel@tonic-gate 			&sensorid, PSVC_DEV_TEMP_SENSOR, i);
6967c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
6977c478bd9Sstevel@tonic-gate 			return (status);
6987c478bd9Sstevel@tonic-gate 
6997c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, sensorid, PSVC_FAULTID_ATTR,
7007c478bd9Sstevel@tonic-gate 			fault);
7017c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
7027c478bd9Sstevel@tonic-gate 			return (status);
7037c478bd9Sstevel@tonic-gate 
7047c478bd9Sstevel@tonic-gate 		if ((strcmp(fault, PSVC_TEMP_HI_SHUT) == 0) ||
7057c478bd9Sstevel@tonic-gate 			(strcmp(fault, PSVC_TEMP_LO_SHUT) == 0)) {
7067c478bd9Sstevel@tonic-gate 			if (cpu_count == 1 || cpus_online == 1 ||
7077c478bd9Sstevel@tonic-gate 			    !(features & PSVC_DEV_HOTPLUG)) {
7087c478bd9Sstevel@tonic-gate 				system(shutdown_string);
7097c478bd9Sstevel@tonic-gate 			} else {
7107c478bd9Sstevel@tonic-gate 				/* FIX offline cpu */
7117c478bd9Sstevel@tonic-gate 				--cpus_online;
7127c478bd9Sstevel@tonic-gate 			}
7137c478bd9Sstevel@tonic-gate 		}
7147c478bd9Sstevel@tonic-gate 	}
7157c478bd9Sstevel@tonic-gate 
7167c478bd9Sstevel@tonic-gate 	return (status);
7177c478bd9Sstevel@tonic-gate }
7187c478bd9Sstevel@tonic-gate 
7197c478bd9Sstevel@tonic-gate int32_t
psvc_shutdown_policy_0(psvc_opaque_t hdlp,char * id)7207c478bd9Sstevel@tonic-gate psvc_shutdown_policy_0(psvc_opaque_t hdlp, char *id)
7217c478bd9Sstevel@tonic-gate {
7227c478bd9Sstevel@tonic-gate 	int32_t cpu_count;
7237c478bd9Sstevel@tonic-gate 	char *cpuid;
7247c478bd9Sstevel@tonic-gate 	int32_t i;
7257c478bd9Sstevel@tonic-gate 	boolean_t present;
7267c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
7277c478bd9Sstevel@tonic-gate 
7287c478bd9Sstevel@tonic-gate 	if (cpus_online == 0) {
7297c478bd9Sstevel@tonic-gate 		/* obviously, zero isn't correct, count present cpu's */
7307c478bd9Sstevel@tonic-gate 		psvc_get_attr(hdlp, id, PSVC_ASSOC_MATCHES_ATTR, &cpu_count,
7317c478bd9Sstevel@tonic-gate 			PSVC_CPU);
7327c478bd9Sstevel@tonic-gate 		for (i = 0; i < cpu_count; ++i) {
7337c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR,
7347c478bd9Sstevel@tonic-gate 				&cpuid, PSVC_CPU, i);
7357c478bd9Sstevel@tonic-gate 			if (status == PSVC_FAILURE)
7367c478bd9Sstevel@tonic-gate 				return (status);
7377c478bd9Sstevel@tonic-gate 
7387c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, cpuid,
7397c478bd9Sstevel@tonic-gate 				PSVC_PRESENCE_ATTR, &present);
7407c478bd9Sstevel@tonic-gate 			if (status == PSVC_FAILURE && present == PSVC_PRESENT)
7417c478bd9Sstevel@tonic-gate 				return (status);
7427c478bd9Sstevel@tonic-gate 			if (present == PSVC_PRESENT)
7437c478bd9Sstevel@tonic-gate 				++cpus_online;
7447c478bd9Sstevel@tonic-gate 		}
7457c478bd9Sstevel@tonic-gate 	}
7467c478bd9Sstevel@tonic-gate 	psvc_get_attr(hdlp, id, PSVC_ASSOC_MATCHES_ATTR, &cpu_count,
7477c478bd9Sstevel@tonic-gate 		PSVC_CPU);
7487c478bd9Sstevel@tonic-gate 	for (i = 0; i < cpu_count; ++i) {
7497c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR, &cpuid,
7507c478bd9Sstevel@tonic-gate 			PSVC_CPU, i);
7517c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
7527c478bd9Sstevel@tonic-gate 			return (status);
7537c478bd9Sstevel@tonic-gate 		status = check_cpu_temp_fault(hdlp, cpuid, cpu_count);
7547c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE && errno != ENODEV)
7557c478bd9Sstevel@tonic-gate 			return (status);
7567c478bd9Sstevel@tonic-gate 	}
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 	return (PSVC_SUCCESS);
7597c478bd9Sstevel@tonic-gate }
7607c478bd9Sstevel@tonic-gate 
7617c478bd9Sstevel@tonic-gate /*
7627c478bd9Sstevel@tonic-gate  * psvc_keyswitch_position_policy_0
7637c478bd9Sstevel@tonic-gate  * Checks the state of the keyswitch sensors.
7647c478bd9Sstevel@tonic-gate  * If a keyswitch position sensor's state is on, the position
7657c478bd9Sstevel@tonic-gate  * of the key is written to syslog.  If none of the sensors
7667c478bd9Sstevel@tonic-gate  * are on (keyswitch is not at one of the detents), a message is sent
7677c478bd9Sstevel@tonic-gate  * to syslog stating that the position is unknown.
7687c478bd9Sstevel@tonic-gate  */
7697c478bd9Sstevel@tonic-gate int32_t
psvc_keyswitch_position_policy_0(psvc_opaque_t hdlp,char * id)7707c478bd9Sstevel@tonic-gate psvc_keyswitch_position_policy_0(psvc_opaque_t hdlp, char *id)
7717c478bd9Sstevel@tonic-gate {
7727c478bd9Sstevel@tonic-gate 	char position[32];
7737c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
7747c478bd9Sstevel@tonic-gate 	static int error_reported = 0;
7757c478bd9Sstevel@tonic-gate 	static char local_previous_position[32];
7767c478bd9Sstevel@tonic-gate 	static int32_t first_time = 1;
777298b7f4cSjfrank 	int retry;
7787c478bd9Sstevel@tonic-gate 
7797c478bd9Sstevel@tonic-gate 	if (first_time) {
7807c478bd9Sstevel@tonic-gate 		first_time = 0;
7817c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_STATE_ATTR,
7827c478bd9Sstevel@tonic-gate 		    local_previous_position);
7837c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
7847c478bd9Sstevel@tonic-gate 			return (status);
7857c478bd9Sstevel@tonic-gate 	}
7867c478bd9Sstevel@tonic-gate 
7877c478bd9Sstevel@tonic-gate 	retry = 0;
7887c478bd9Sstevel@tonic-gate 	do {
7897c478bd9Sstevel@tonic-gate 		if (retry)
790298b7f4cSjfrank 			(void) sleep(retry_sleep_keyswitch);
7917c478bd9Sstevel@tonic-gate 
7927c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_SWITCH_STATE_ATTR,
7937c478bd9Sstevel@tonic-gate 		    position);
7947c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
7957c478bd9Sstevel@tonic-gate 			return (status);
7967c478bd9Sstevel@tonic-gate 
7977c478bd9Sstevel@tonic-gate 		if (strcmp(position, PSVC_ERROR) == 0) {
7987c478bd9Sstevel@tonic-gate 			if ((errno == EINVAL) && (!(error_reported))) {
7997c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
8007c478bd9Sstevel@tonic-gate 				    KEYSWITCH_POS_READ_FAILED_MSG);
8017c478bd9Sstevel@tonic-gate 				error_reported = 1;
8027c478bd9Sstevel@tonic-gate 				return (PSVC_SUCCESS);
8037c478bd9Sstevel@tonic-gate 			}
8047c478bd9Sstevel@tonic-gate 		}
8057c478bd9Sstevel@tonic-gate 		retry++;
806298b7f4cSjfrank 	} while ((retry < n_retry_keyswitch) &&
8077c478bd9Sstevel@tonic-gate 	    (strcmp(position, local_previous_position) != 0));
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	status = psvc_set_attr(hdlp, id, PSVC_STATE_ATTR, position);
8107c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
8117c478bd9Sstevel@tonic-gate 		return (status);
8127c478bd9Sstevel@tonic-gate 
8137c478bd9Sstevel@tonic-gate 	if (strcmp(position, local_previous_position) != 0) {
8147c478bd9Sstevel@tonic-gate 		error_reported = 0;
8157c478bd9Sstevel@tonic-gate 		strcpy(local_previous_position, position);
8167c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, KEYSWITCH_POS_CHANGED_MSG, position);
8177c478bd9Sstevel@tonic-gate 	}
8187c478bd9Sstevel@tonic-gate 
8197c478bd9Sstevel@tonic-gate 	return (status);
8207c478bd9Sstevel@tonic-gate }
8217c478bd9Sstevel@tonic-gate 
8227c478bd9Sstevel@tonic-gate int32_t
psvc_hotplug_notifier_policy_0(psvc_opaque_t hdlp,char * id)8237c478bd9Sstevel@tonic-gate psvc_hotplug_notifier_policy_0(psvc_opaque_t hdlp, char *id)
8247c478bd9Sstevel@tonic-gate {
8257c478bd9Sstevel@tonic-gate 	boolean_t presence, previous_presence;
8267c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
8277c478bd9Sstevel@tonic-gate 	char label[32];
828298b7f4cSjfrank 	int retry;
8297c478bd9Sstevel@tonic-gate 
8307c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_PREV_PRESENCE_ATTR,
8317c478bd9Sstevel@tonic-gate 		&previous_presence);
8327c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
8337c478bd9Sstevel@tonic-gate 		return (status);
834298b7f4cSjfrank 
8357c478bd9Sstevel@tonic-gate 	retry = 0;
8367c478bd9Sstevel@tonic-gate 	do {
8377c478bd9Sstevel@tonic-gate 		if (retry)
838298b7f4cSjfrank 			(void) sleep(retry_sleep_hotplug);
8397c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &presence);
8407c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
8417c478bd9Sstevel@tonic-gate 			return (status);
8427c478bd9Sstevel@tonic-gate 		retry++;
843298b7f4cSjfrank 	} while ((retry < n_retry_hotplug) &&
8447c478bd9Sstevel@tonic-gate 	    (presence != previous_presence));
8457c478bd9Sstevel@tonic-gate 
8467c478bd9Sstevel@tonic-gate 
8477c478bd9Sstevel@tonic-gate 	if (presence != previous_presence) {
8487c478bd9Sstevel@tonic-gate 		char parent_path[256];
8497c478bd9Sstevel@tonic-gate 		picl_nodehdl_t child_node;
8507c478bd9Sstevel@tonic-gate 
8517c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_LABEL_ATTR, label);
8527c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
8537c478bd9Sstevel@tonic-gate 			return (status);
8547c478bd9Sstevel@tonic-gate 
8557c478bd9Sstevel@tonic-gate 		/* return parent path and node for an object */
8567c478bd9Sstevel@tonic-gate 		psvcplugin_lookup(id, parent_path, &child_node);
8577c478bd9Sstevel@tonic-gate 
8587c478bd9Sstevel@tonic-gate 		if (presence == PSVC_PRESENT) {
8597c478bd9Sstevel@tonic-gate 			char state[32], fault[32];
8607c478bd9Sstevel@tonic-gate 			picl_nodehdl_t parent_node;
8617c478bd9Sstevel@tonic-gate 
8627c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, DEVICE_INSERTED_MSG, label);
8637c478bd9Sstevel@tonic-gate 			strcpy(state, PSVC_OK);
8647c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, id, PSVC_STATE_ATTR,
8657c478bd9Sstevel@tonic-gate 				state);
8667c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
8677c478bd9Sstevel@tonic-gate 				return (status);
8687c478bd9Sstevel@tonic-gate 			strcpy(fault, PSVC_NO_FAULT);
8697c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, id, PSVC_FAULTID_ATTR,
8707c478bd9Sstevel@tonic-gate 				fault);
8717c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS) {
8727c478bd9Sstevel@tonic-gate 				return (status);
8737c478bd9Sstevel@tonic-gate 			}
8747c478bd9Sstevel@tonic-gate 
8757c478bd9Sstevel@tonic-gate 			status = ptree_get_node_by_path(parent_path,
8767c478bd9Sstevel@tonic-gate 				&parent_node);
8777c478bd9Sstevel@tonic-gate 			if (status != 0)
8787c478bd9Sstevel@tonic-gate 				return (PSVC_FAILURE);
8797c478bd9Sstevel@tonic-gate 			status = ptree_add_node(parent_node, child_node);
8807c478bd9Sstevel@tonic-gate 			if (status != 0)
8817c478bd9Sstevel@tonic-gate 				return (PSVC_FAILURE);
8827c478bd9Sstevel@tonic-gate 		} else {
8837c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, DEVICE_REMOVED_MSG, label);
8847c478bd9Sstevel@tonic-gate 
8857c478bd9Sstevel@tonic-gate 			ptree_delete_node(child_node);
8867c478bd9Sstevel@tonic-gate 
8877c478bd9Sstevel@tonic-gate 		}
8887c478bd9Sstevel@tonic-gate 	}
8897c478bd9Sstevel@tonic-gate 
8907c478bd9Sstevel@tonic-gate 	status = psvc_set_attr(hdlp, id, PSVC_PREV_PRESENCE_ATTR, &presence);
8917c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
8927c478bd9Sstevel@tonic-gate 		return (status);
8937c478bd9Sstevel@tonic-gate 
8947c478bd9Sstevel@tonic-gate 	return (status);
8957c478bd9Sstevel@tonic-gate }
8967c478bd9Sstevel@tonic-gate 
8977c478bd9Sstevel@tonic-gate int32_t
psvc_fan_hotplug_policy_0(psvc_opaque_t hdlp,char * id)8987c478bd9Sstevel@tonic-gate psvc_fan_hotplug_policy_0(psvc_opaque_t hdlp, char *id)
8997c478bd9Sstevel@tonic-gate {
9007c478bd9Sstevel@tonic-gate 	boolean_t presence, previous_presence;
9017c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
9027c478bd9Sstevel@tonic-gate 	char label[32];
903298b7f4cSjfrank 	int retry;
9047c478bd9Sstevel@tonic-gate 
9057c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_PREV_PRESENCE_ATTR,
9067c478bd9Sstevel@tonic-gate 		&previous_presence);
9077c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
9087c478bd9Sstevel@tonic-gate 		return (status);
9097c478bd9Sstevel@tonic-gate 
9107c478bd9Sstevel@tonic-gate 	retry = 0;
9117c478bd9Sstevel@tonic-gate 	do {
9127c478bd9Sstevel@tonic-gate 		if (retry)
913298b7f4cSjfrank 			(void) sleep(retry_sleep_fan_hotplug);
9147c478bd9Sstevel@tonic-gate 
9157c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &presence);
9167c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
9177c478bd9Sstevel@tonic-gate 			return (status);
9187c478bd9Sstevel@tonic-gate 		retry++;
919298b7f4cSjfrank 	} while ((retry < n_retry_fan_hotplug) &&
9207c478bd9Sstevel@tonic-gate 	    (presence != previous_presence));
9217c478bd9Sstevel@tonic-gate 
9227c478bd9Sstevel@tonic-gate 
9237c478bd9Sstevel@tonic-gate 	if (presence != previous_presence) {
9247c478bd9Sstevel@tonic-gate 		char parent_path[256];
9257c478bd9Sstevel@tonic-gate 		picl_nodehdl_t child_node;
9267c478bd9Sstevel@tonic-gate 
9277c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_LABEL_ATTR, label);
9287c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
9297c478bd9Sstevel@tonic-gate 			return (status);
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 		/* return parent path and node for an object */
9327c478bd9Sstevel@tonic-gate 		psvcplugin_lookup(id, parent_path, &child_node);
9337c478bd9Sstevel@tonic-gate 
9347c478bd9Sstevel@tonic-gate 		if (presence == PSVC_PRESENT) {
9357c478bd9Sstevel@tonic-gate 			char state[32], fault[32];
9367c478bd9Sstevel@tonic-gate 			char *slot_id;
9377c478bd9Sstevel@tonic-gate 			char *led_id;
9387c478bd9Sstevel@tonic-gate 			int32_t i, led_count;
9397c478bd9Sstevel@tonic-gate 			char led_state[32];
9407c478bd9Sstevel@tonic-gate 			picl_nodehdl_t parent_node;
9417c478bd9Sstevel@tonic-gate 
9427c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, DEVICE_INSERTED_MSG, label);
9437c478bd9Sstevel@tonic-gate 
9447c478bd9Sstevel@tonic-gate 			strcpy(state, PSVC_OK);
9457c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, id, PSVC_STATE_ATTR,
9467c478bd9Sstevel@tonic-gate 				state);
9477c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
9487c478bd9Sstevel@tonic-gate 				return (status);
9497c478bd9Sstevel@tonic-gate 			strcpy(fault, PSVC_NO_FAULT);
9507c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, id, PSVC_FAULTID_ATTR,
9517c478bd9Sstevel@tonic-gate 				fault);
9527c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
9537c478bd9Sstevel@tonic-gate 				return (status);
9547c478bd9Sstevel@tonic-gate 
9557c478bd9Sstevel@tonic-gate 			/* turn off fault LEDs */
9567c478bd9Sstevel@tonic-gate 			psvc_get_attr(hdlp, id, PSVC_ASSOC_MATCHES_ATTR,
9577c478bd9Sstevel@tonic-gate 				&led_count, PSVC_DEV_FAULT_LED);
9587c478bd9Sstevel@tonic-gate 			strcpy(led_state, PSVC_LED_OFF);
9597c478bd9Sstevel@tonic-gate 			for (i = 0; i < led_count; ++i) {
9607c478bd9Sstevel@tonic-gate 				status = psvc_get_attr(hdlp, id,
9617c478bd9Sstevel@tonic-gate 					PSVC_ASSOC_ID_ATTR, &led_id,
9627c478bd9Sstevel@tonic-gate 					PSVC_DEV_FAULT_LED, i);
9637c478bd9Sstevel@tonic-gate 				if (status == PSVC_FAILURE)
9647c478bd9Sstevel@tonic-gate 					return (status);
9657c478bd9Sstevel@tonic-gate 				status = psvc_set_attr(hdlp, led_id,
9667c478bd9Sstevel@tonic-gate 					PSVC_LED_STATE_ATTR, led_state);
9677c478bd9Sstevel@tonic-gate 				if (status == PSVC_FAILURE)
9687c478bd9Sstevel@tonic-gate 					return (status);
9697c478bd9Sstevel@tonic-gate 			}
9707c478bd9Sstevel@tonic-gate 
9717c478bd9Sstevel@tonic-gate 			/* turn off OK to remove LEDs */
9727c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR,
9737c478bd9Sstevel@tonic-gate 				&slot_id, PSVC_PARENT, 0);
9747c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
9757c478bd9Sstevel@tonic-gate 				return (status);
9767c478bd9Sstevel@tonic-gate 
9777c478bd9Sstevel@tonic-gate 			psvc_get_attr(hdlp, slot_id, PSVC_ASSOC_MATCHES_ATTR,
9787c478bd9Sstevel@tonic-gate 				&led_count, PSVC_SLOT_REMOVE_LED);
9797c478bd9Sstevel@tonic-gate 			strcpy(led_state, PSVC_LED_OFF);
9807c478bd9Sstevel@tonic-gate 			for (i = 0; i < led_count; ++i) {
9817c478bd9Sstevel@tonic-gate 				status = psvc_get_attr(hdlp, slot_id,
9827c478bd9Sstevel@tonic-gate 					PSVC_ASSOC_ID_ATTR, &led_id,
9837c478bd9Sstevel@tonic-gate 					PSVC_SLOT_REMOVE_LED, i);
9847c478bd9Sstevel@tonic-gate 				if (status == PSVC_FAILURE)
9857c478bd9Sstevel@tonic-gate 					return (status);
9867c478bd9Sstevel@tonic-gate 
9877c478bd9Sstevel@tonic-gate 				status = psvc_set_attr(hdlp, led_id,
9887c478bd9Sstevel@tonic-gate 					PSVC_LED_STATE_ATTR, led_state);
9897c478bd9Sstevel@tonic-gate 				if (status == PSVC_FAILURE)
9907c478bd9Sstevel@tonic-gate 					return (status);
9917c478bd9Sstevel@tonic-gate 			}
9927c478bd9Sstevel@tonic-gate 
9937c478bd9Sstevel@tonic-gate 			ptree_get_node_by_path(parent_path, &parent_node);
9947c478bd9Sstevel@tonic-gate 			ptree_add_node(parent_node, child_node);
9957c478bd9Sstevel@tonic-gate 		} else {
9967c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, DEVICE_REMOVED_MSG, label);
9977c478bd9Sstevel@tonic-gate 			ptree_delete_node(child_node);
9987c478bd9Sstevel@tonic-gate 		}
9997c478bd9Sstevel@tonic-gate 	}
10007c478bd9Sstevel@tonic-gate 
10017c478bd9Sstevel@tonic-gate 	status = psvc_set_attr(hdlp, id, PSVC_PREV_PRESENCE_ATTR, &presence);
10027c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
10037c478bd9Sstevel@tonic-gate 		return (status);
10047c478bd9Sstevel@tonic-gate 
10057c478bd9Sstevel@tonic-gate 	return (status);
10067c478bd9Sstevel@tonic-gate }
10077c478bd9Sstevel@tonic-gate 
10087c478bd9Sstevel@tonic-gate int32_t
psvc_init_led_policy_0(psvc_opaque_t hdlp,char * id)10097c478bd9Sstevel@tonic-gate psvc_init_led_policy_0(psvc_opaque_t hdlp, char *id)
10107c478bd9Sstevel@tonic-gate {
10117c478bd9Sstevel@tonic-gate 	int32_t status;
10127c478bd9Sstevel@tonic-gate 
10137c478bd9Sstevel@tonic-gate 	status = psvc_set_attr(hdlp, id, PSVC_LED_STATE_ATTR, PSVC_LED_OFF);
10147c478bd9Sstevel@tonic-gate 	return (status);
10157c478bd9Sstevel@tonic-gate }
10167c478bd9Sstevel@tonic-gate 
10177c478bd9Sstevel@tonic-gate int32_t
psvc_init_state_policy_0(psvc_opaque_t hdlp,char * id)10187c478bd9Sstevel@tonic-gate psvc_init_state_policy_0(psvc_opaque_t hdlp, char *id)
10197c478bd9Sstevel@tonic-gate {
10207c478bd9Sstevel@tonic-gate 	int32_t status;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 	status = psvc_set_attr(hdlp, id, PSVC_STATE_ATTR, PSVC_OK);
10237c478bd9Sstevel@tonic-gate 	status = psvc_set_attr(hdlp, id, PSVC_FAULTID_ATTR, PSVC_NO_FAULT);
10247c478bd9Sstevel@tonic-gate 	return (status);
10257c478bd9Sstevel@tonic-gate }
10267c478bd9Sstevel@tonic-gate 
10277c478bd9Sstevel@tonic-gate int32_t
psvc_ps_overcurrent_check_policy_0(psvc_opaque_t hdlp,char * power_supply_id)10287c478bd9Sstevel@tonic-gate psvc_ps_overcurrent_check_policy_0(psvc_opaque_t hdlp, char *power_supply_id)
10297c478bd9Sstevel@tonic-gate {
10307c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
10317c478bd9Sstevel@tonic-gate 	boolean_t present;
10327c478bd9Sstevel@tonic-gate 	char *sensor_id;
10337c478bd9Sstevel@tonic-gate 	int32_t sensor_count;
10347c478bd9Sstevel@tonic-gate 	int32_t i;
10357c478bd9Sstevel@tonic-gate 	int32_t amps, hi_warn;
10367c478bd9Sstevel@tonic-gate 
10377c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, power_supply_id, PSVC_PRESENCE_ATTR,
10387c478bd9Sstevel@tonic-gate 		&present);
10397c478bd9Sstevel@tonic-gate 	if (status == PSVC_FAILURE) {
10407c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, GET_PRESENCE_FAILED_MSG, power_supply_id,
10417c478bd9Sstevel@tonic-gate 			errno);
10427c478bd9Sstevel@tonic-gate 		return (status);
10437c478bd9Sstevel@tonic-gate 	}
10447c478bd9Sstevel@tonic-gate 
10457c478bd9Sstevel@tonic-gate 	if (present == PSVC_ABSENT) {
10467c478bd9Sstevel@tonic-gate 		errno = ENODEV;
10477c478bd9Sstevel@tonic-gate 		return (PSVC_FAILURE);
10487c478bd9Sstevel@tonic-gate 	}
10497c478bd9Sstevel@tonic-gate 
10507c478bd9Sstevel@tonic-gate 	psvc_get_attr(hdlp, power_supply_id, PSVC_ASSOC_MATCHES_ATTR,
10517c478bd9Sstevel@tonic-gate 		&sensor_count, PSVC_PS_I_SENSOR);
10527c478bd9Sstevel@tonic-gate 	for (i = 0; i < sensor_count; ++i) {
10537c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, power_supply_id,
10547c478bd9Sstevel@tonic-gate 			PSVC_ASSOC_ID_ATTR, &sensor_id, PSVC_PS_I_SENSOR, i);
10557c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
10567c478bd9Sstevel@tonic-gate 			return (status);
10577c478bd9Sstevel@tonic-gate 
10587c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, sensor_id, PSVC_HI_WARN_ATTR,
10597c478bd9Sstevel@tonic-gate 			&hi_warn);
10607c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
10617c478bd9Sstevel@tonic-gate 			return (status);
10627c478bd9Sstevel@tonic-gate 
10637c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, sensor_id,
10647c478bd9Sstevel@tonic-gate 			PSVC_SENSOR_VALUE_ATTR, &amps);
10657c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS) {
10667c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, GET_SENSOR_FAILED_MSG, sensor_id,
10677c478bd9Sstevel@tonic-gate 				errno);
10687c478bd9Sstevel@tonic-gate 			return (status);
10697c478bd9Sstevel@tonic-gate 		}
10707c478bd9Sstevel@tonic-gate 
10717c478bd9Sstevel@tonic-gate 		if (amps >= hi_warn) {
10727c478bd9Sstevel@tonic-gate 			char label[32];
10737c478bd9Sstevel@tonic-gate 
10747c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, power_supply_id,
10757c478bd9Sstevel@tonic-gate 				PSVC_LABEL_ATTR, &label);
10767c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
10777c478bd9Sstevel@tonic-gate 				return (status);
10787c478bd9Sstevel@tonic-gate 
10797c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, PS_OVER_CURRENT_MSG, label);
10807c478bd9Sstevel@tonic-gate 		}
10817c478bd9Sstevel@tonic-gate 	}
10827c478bd9Sstevel@tonic-gate 
10837c478bd9Sstevel@tonic-gate 	return (PSVC_SUCCESS);
10847c478bd9Sstevel@tonic-gate 
10857c478bd9Sstevel@tonic-gate }
10867c478bd9Sstevel@tonic-gate 
10877c478bd9Sstevel@tonic-gate int32_t
psvc_device_fail_notifier_policy_0(psvc_opaque_t hdlp,char * id)10887c478bd9Sstevel@tonic-gate psvc_device_fail_notifier_policy_0(psvc_opaque_t hdlp, char *id)
10897c478bd9Sstevel@tonic-gate {
10907c478bd9Sstevel@tonic-gate 	int32_t led_count, sensor_count;
10917c478bd9Sstevel@tonic-gate 	char *led_id, *sensor_id;
10927c478bd9Sstevel@tonic-gate 	int i;
10937c478bd9Sstevel@tonic-gate 	char state[32], fault[32], previous_state[32];
10947c478bd9Sstevel@tonic-gate 	char led_state[32];
10957c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
10967c478bd9Sstevel@tonic-gate 	boolean_t present;
10977c478bd9Sstevel@tonic-gate 
10987c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &present);
10997c478bd9Sstevel@tonic-gate 	if (status == PSVC_FAILURE)
11007c478bd9Sstevel@tonic-gate 		return (status);
11017c478bd9Sstevel@tonic-gate 
11027c478bd9Sstevel@tonic-gate 	if (present == PSVC_ABSENT) {
11037c478bd9Sstevel@tonic-gate 		errno = ENODEV;
11047c478bd9Sstevel@tonic-gate 		return (PSVC_FAILURE);
11057c478bd9Sstevel@tonic-gate 	}
11067c478bd9Sstevel@tonic-gate 
11077c478bd9Sstevel@tonic-gate 	psvc_get_attr(hdlp, id, PSVC_ASSOC_MATCHES_ATTR, &sensor_count,
11087c478bd9Sstevel@tonic-gate 		PSVC_DEV_FAULT_SENSOR);
11097c478bd9Sstevel@tonic-gate 	for (i = 0; i < sensor_count; ++i) {
11107c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR,
11117c478bd9Sstevel@tonic-gate 			&sensor_id, PSVC_DEV_FAULT_SENSOR, i);
11127c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
11137c478bd9Sstevel@tonic-gate 			return (status);
11147c478bd9Sstevel@tonic-gate 
11157c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, sensor_id,
11167c478bd9Sstevel@tonic-gate 			PSVC_SWITCH_STATE_ATTR, state);
11177c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
11187c478bd9Sstevel@tonic-gate 			return (status);
11197c478bd9Sstevel@tonic-gate 
11207c478bd9Sstevel@tonic-gate 		if (strcmp(state, PSVC_SWITCH_ON) == 0) {
11217c478bd9Sstevel@tonic-gate 			strcpy(state, PSVC_ERROR);
11227c478bd9Sstevel@tonic-gate 			strcpy(fault, PSVC_GEN_FAULT);
11237c478bd9Sstevel@tonic-gate 		} else {
11247c478bd9Sstevel@tonic-gate 			strcpy(state, PSVC_OK);
11257c478bd9Sstevel@tonic-gate 			strcpy(fault, PSVC_NO_FAULT);
11267c478bd9Sstevel@tonic-gate 		}
11277c478bd9Sstevel@tonic-gate 
11287c478bd9Sstevel@tonic-gate 		status = psvc_set_attr(hdlp, id, PSVC_STATE_ATTR, state);
11297c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
11307c478bd9Sstevel@tonic-gate 			return (status);
11317c478bd9Sstevel@tonic-gate 		status = psvc_set_attr(hdlp, id, PSVC_FAULTID_ATTR, fault);
11327c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
11337c478bd9Sstevel@tonic-gate 			return (status);
11347c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_PREV_STATE_ATTR,
11357c478bd9Sstevel@tonic-gate 			previous_state);
11367c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
11377c478bd9Sstevel@tonic-gate 			return (status);
11387c478bd9Sstevel@tonic-gate 
11397c478bd9Sstevel@tonic-gate 		if (strcmp(state, previous_state) != 0) {
11407c478bd9Sstevel@tonic-gate 			char sensor_label[32];
11417c478bd9Sstevel@tonic-gate 			char dev_label[32];
11427c478bd9Sstevel@tonic-gate 			int32_t j;
11437c478bd9Sstevel@tonic-gate 
11447c478bd9Sstevel@tonic-gate 			psvc_get_attr(hdlp, id, PSVC_LABEL_ATTR, dev_label);
11457c478bd9Sstevel@tonic-gate 			psvc_get_attr(hdlp, sensor_id, PSVC_LABEL_ATTR,
11467c478bd9Sstevel@tonic-gate 				sensor_label);
11477c478bd9Sstevel@tonic-gate 
11487c478bd9Sstevel@tonic-gate 			if (strcmp(state, PSVC_ERROR) == 0) {
11497c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, DEVICE_FAILURE_MSG, dev_label,
11507c478bd9Sstevel@tonic-gate 					sensor_label);
11517c478bd9Sstevel@tonic-gate 				strcpy(led_state, PSVC_LED_ON);
11527c478bd9Sstevel@tonic-gate 			} else {
11537c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, DEVICE_OK_MSG, dev_label);
11547c478bd9Sstevel@tonic-gate 				strcpy(led_state, PSVC_LED_OFF);
11557c478bd9Sstevel@tonic-gate 			}
11567c478bd9Sstevel@tonic-gate 
11577c478bd9Sstevel@tonic-gate 			psvc_get_attr(hdlp, id, PSVC_ASSOC_MATCHES_ATTR,
11587c478bd9Sstevel@tonic-gate 				&led_count, PSVC_DEV_FAULT_LED);
11597c478bd9Sstevel@tonic-gate 			for (j = 0; j < led_count; j++) {
11607c478bd9Sstevel@tonic-gate 				status = psvc_get_attr(hdlp, id,
11617c478bd9Sstevel@tonic-gate 					PSVC_ASSOC_ID_ATTR, &led_id,
11627c478bd9Sstevel@tonic-gate 					PSVC_DEV_FAULT_LED, j);
11637c478bd9Sstevel@tonic-gate 				if (status != PSVC_SUCCESS)
11647c478bd9Sstevel@tonic-gate 					return (status);
11657c478bd9Sstevel@tonic-gate 				status = psvc_set_attr(hdlp, led_id,
11667c478bd9Sstevel@tonic-gate 					PSVC_LED_STATE_ATTR, led_state);
11677c478bd9Sstevel@tonic-gate 				if (status != PSVC_SUCCESS) {
11687c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR, SET_LED_FAILED_MSG,
11697c478bd9Sstevel@tonic-gate 						led_id, errno);
11707c478bd9Sstevel@tonic-gate 					return (status);
11717c478bd9Sstevel@tonic-gate 				}
11727c478bd9Sstevel@tonic-gate 			}
11737c478bd9Sstevel@tonic-gate 		}
11747c478bd9Sstevel@tonic-gate 	}
11757c478bd9Sstevel@tonic-gate 
11767c478bd9Sstevel@tonic-gate 	return (PSVC_SUCCESS);
11777c478bd9Sstevel@tonic-gate }
11787c478bd9Sstevel@tonic-gate 
11797c478bd9Sstevel@tonic-gate static float
get_filtered_error(float * last_errors,int current_error)11807c478bd9Sstevel@tonic-gate get_filtered_error(float *last_errors, int current_error)
11817c478bd9Sstevel@tonic-gate {
11827c478bd9Sstevel@tonic-gate 	float error;
11837c478bd9Sstevel@tonic-gate 	float adder;
11847c478bd9Sstevel@tonic-gate 	int i = 0;
11857c478bd9Sstevel@tonic-gate 
11867c478bd9Sstevel@tonic-gate 	adder = last_errors[0];
11877c478bd9Sstevel@tonic-gate 	for (i = 1; i < PSVC_MAXERRORS; i++) {
11887c478bd9Sstevel@tonic-gate 		adder = adder + last_errors[i];
11897c478bd9Sstevel@tonic-gate 	}
11907c478bd9Sstevel@tonic-gate 	adder = adder + current_error;
11917c478bd9Sstevel@tonic-gate 	error = adder/(PSVC_MAXERRORS+1);
11927c478bd9Sstevel@tonic-gate 
11937c478bd9Sstevel@tonic-gate 	return (error);
11947c478bd9Sstevel@tonic-gate }
11957c478bd9Sstevel@tonic-gate 
11967c478bd9Sstevel@tonic-gate static int32_t
change_cpu_fans(psvc_opaque_t hdlp,char * fan_id,int32_t fan_speed)11977c478bd9Sstevel@tonic-gate change_cpu_fans(psvc_opaque_t hdlp, char *fan_id, int32_t fan_speed)
11987c478bd9Sstevel@tonic-gate {
11997c478bd9Sstevel@tonic-gate 	int err = PSVC_SUCCESS;
12007c478bd9Sstevel@tonic-gate 	int i;
12017c478bd9Sstevel@tonic-gate 	int32_t control_count;
12027c478bd9Sstevel@tonic-gate 	char *control_id;
12037c478bd9Sstevel@tonic-gate 	int32_t old_fan_speed;
12047c478bd9Sstevel@tonic-gate 
12057c478bd9Sstevel@tonic-gate 	psvc_get_attr(hdlp, fan_id, PSVC_ASSOC_MATCHES_ATTR, &control_count,
12067c478bd9Sstevel@tonic-gate 		PSVC_FAN_DRIVE_CONTROL);
12077c478bd9Sstevel@tonic-gate 	if (control_count == 0)
12087c478bd9Sstevel@tonic-gate 		return (PSVC_SUCCESS);
12097c478bd9Sstevel@tonic-gate 
12107c478bd9Sstevel@tonic-gate 	err = psvc_get_attr(hdlp, fan_id, PSVC_ASSOC_ID_ATTR, &control_id,
12117c478bd9Sstevel@tonic-gate 		PSVC_FAN_DRIVE_CONTROL, 0);
12127c478bd9Sstevel@tonic-gate 	if (err != PSVC_SUCCESS)
12137c478bd9Sstevel@tonic-gate 		return (err);
12147c478bd9Sstevel@tonic-gate 
12157c478bd9Sstevel@tonic-gate 	/*
12167c478bd9Sstevel@tonic-gate 	 * this call will return PSVC_FAILURE on the first pass,
12177c478bd9Sstevel@tonic-gate 	 * because no value has been set.
12187c478bd9Sstevel@tonic-gate 	 */
12197c478bd9Sstevel@tonic-gate 	err = psvc_get_attr(hdlp, control_id, PSVC_CONTROL_VALUE_ATTR,
12207c478bd9Sstevel@tonic-gate 		&old_fan_speed);
12217c478bd9Sstevel@tonic-gate 	if (err == PSVC_SUCCESS && old_fan_speed == fan_speed)
12227c478bd9Sstevel@tonic-gate 		return (PSVC_SUCCESS);
12237c478bd9Sstevel@tonic-gate 
12247c478bd9Sstevel@tonic-gate 	for (i = 0; i < control_count; i++) {
12257c478bd9Sstevel@tonic-gate 		err = psvc_get_attr(hdlp, fan_id, PSVC_ASSOC_ID_ATTR,
12267c478bd9Sstevel@tonic-gate 			&control_id, PSVC_FAN_DRIVE_CONTROL, i);
12277c478bd9Sstevel@tonic-gate 		if (err != PSVC_SUCCESS)
12287c478bd9Sstevel@tonic-gate 			return (err);
12297c478bd9Sstevel@tonic-gate 
12307c478bd9Sstevel@tonic-gate 		err = psvc_set_attr(hdlp, control_id, PSVC_CONTROL_VALUE_ATTR,
12317c478bd9Sstevel@tonic-gate 			&fan_speed);
12327c478bd9Sstevel@tonic-gate 		if (err == PSVC_FAILURE) {
12337c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, SET_FANSPEED_FAILED_MSG, control_id,
12347c478bd9Sstevel@tonic-gate 				errno);
12357c478bd9Sstevel@tonic-gate 			return (err);
12367c478bd9Sstevel@tonic-gate 		}
12377c478bd9Sstevel@tonic-gate 	}
12387c478bd9Sstevel@tonic-gate 	return (err);
12397c478bd9Sstevel@tonic-gate }
12407c478bd9Sstevel@tonic-gate 
12417c478bd9Sstevel@tonic-gate static int32_t
device_temp_check(psvc_opaque_t hdlp,char * fan_id,int32_t * hot_device)12427c478bd9Sstevel@tonic-gate device_temp_check(psvc_opaque_t hdlp, char *fan_id, int32_t *hot_device)
12437c478bd9Sstevel@tonic-gate {
12447c478bd9Sstevel@tonic-gate 	int i;
12457c478bd9Sstevel@tonic-gate 	int32_t err = PSVC_SUCCESS;
12467c478bd9Sstevel@tonic-gate 	char *sensor_id;
12477c478bd9Sstevel@tonic-gate 	int32_t sensor_count;
12487c478bd9Sstevel@tonic-gate 	int32_t temp;
12497c478bd9Sstevel@tonic-gate 
12507c478bd9Sstevel@tonic-gate 	*hot_device = 0;
12517c478bd9Sstevel@tonic-gate 
12527c478bd9Sstevel@tonic-gate 	psvc_get_attr(hdlp, fan_id, PSVC_ASSOC_MATCHES_ATTR, &sensor_count,
12537c478bd9Sstevel@tonic-gate 		PSVC_DEV_TEMP_SENSOR);
12547c478bd9Sstevel@tonic-gate 	for (i = 0; i < sensor_count; i++) {
12557c478bd9Sstevel@tonic-gate 		err = psvc_get_attr(hdlp, fan_id, PSVC_ASSOC_ID_ATTR,
12567c478bd9Sstevel@tonic-gate 			&sensor_id, PSVC_DEV_TEMP_SENSOR, i);
12577c478bd9Sstevel@tonic-gate 		if (err == PSVC_FAILURE)
12587c478bd9Sstevel@tonic-gate 			return (err);
12597c478bd9Sstevel@tonic-gate 		err = psvc_get_attr(hdlp, sensor_id, PSVC_SENSOR_VALUE_ATTR,
12607c478bd9Sstevel@tonic-gate 			&temp);
12617c478bd9Sstevel@tonic-gate 		if (err == PSVC_FAILURE) {
12627c478bd9Sstevel@tonic-gate 			if (errno == ENODEV) {
12637c478bd9Sstevel@tonic-gate 				temp = 0;
12647c478bd9Sstevel@tonic-gate 			} else {
1265*31925ed2Sanbui 				syslog(LOG_ERR, GET_SENSOR_FAILED_MSG,
1266*31925ed2Sanbui 				    sensor_id, errno);
12677c478bd9Sstevel@tonic-gate 				return (err);
12687c478bd9Sstevel@tonic-gate 			}
12697c478bd9Sstevel@tonic-gate 		}
12707c478bd9Sstevel@tonic-gate 
12717c478bd9Sstevel@tonic-gate 		if (*hot_device < temp)
12727c478bd9Sstevel@tonic-gate 			*hot_device = temp;
12737c478bd9Sstevel@tonic-gate 	}
12747c478bd9Sstevel@tonic-gate 	return (PSVC_SUCCESS);
12757c478bd9Sstevel@tonic-gate }
12767c478bd9Sstevel@tonic-gate 
12777c478bd9Sstevel@tonic-gate int32_t
psvc_fan_control_policy_0(psvc_opaque_t hdlp,char * fan_id)12787c478bd9Sstevel@tonic-gate psvc_fan_control_policy_0(psvc_opaque_t hdlp, char *fan_id)
12797c478bd9Sstevel@tonic-gate {
12807c478bd9Sstevel@tonic-gate 	boolean_t is_enabled;
12817c478bd9Sstevel@tonic-gate 	int32_t err = PSVC_SUCCESS;
12827c478bd9Sstevel@tonic-gate 	int16_t setpoint, hysteresis, loopgain, loopbias;
12837c478bd9Sstevel@tonic-gate 	int current_error;		/* Holds current error */
12847c478bd9Sstevel@tonic-gate 					/* Signal before signaling */
12857c478bd9Sstevel@tonic-gate 	float filtered_error;		/* Holds the filtered error signal */
12867c478bd9Sstevel@tonic-gate 	int ampout;			/* output of loop amplifier */
12877c478bd9Sstevel@tonic-gate 	int hot_device;
12887c478bd9Sstevel@tonic-gate 
12897c478bd9Sstevel@tonic-gate 	int16_t error_number;
12907c478bd9Sstevel@tonic-gate 	float last_errors[PSVC_MAXERRORS];	/* Holds the filtered error */
12917c478bd9Sstevel@tonic-gate 						/* from the last n iterations */
12927c478bd9Sstevel@tonic-gate 
12937c478bd9Sstevel@tonic-gate 	psvc_get_attr(hdlp, fan_id, PSVC_ENABLE_ATTR, &is_enabled);
12947c478bd9Sstevel@tonic-gate 	if (is_enabled == PSVC_DISABLED)
12957c478bd9Sstevel@tonic-gate 		return (PSVC_SUCCESS);
12967c478bd9Sstevel@tonic-gate 
12977c478bd9Sstevel@tonic-gate 	err = psvc_get_attr(hdlp, fan_id, PSVC_SETPOINT_ATTR, &setpoint);
12987c478bd9Sstevel@tonic-gate 	if (err != PSVC_SUCCESS)
12997c478bd9Sstevel@tonic-gate 		return (err);
13007c478bd9Sstevel@tonic-gate 
13017c478bd9Sstevel@tonic-gate 	err = psvc_get_attr(hdlp, fan_id, PSVC_HYSTERESIS_ATTR,
13027c478bd9Sstevel@tonic-gate 		&hysteresis);
13037c478bd9Sstevel@tonic-gate 	if (err != PSVC_SUCCESS)
13047c478bd9Sstevel@tonic-gate 		return (err);
13057c478bd9Sstevel@tonic-gate 
13067c478bd9Sstevel@tonic-gate 	err = psvc_get_attr(hdlp, fan_id, PSVC_LOOPGAIN_ATTR, &loopgain);
13077c478bd9Sstevel@tonic-gate 	if (err != PSVC_SUCCESS)
13087c478bd9Sstevel@tonic-gate 		return (err);
13097c478bd9Sstevel@tonic-gate 
13107c478bd9Sstevel@tonic-gate 	err = psvc_get_attr(hdlp, fan_id, PSVC_LOOPBIAS_ATTR, &loopbias);
13117c478bd9Sstevel@tonic-gate 	if (err != PSVC_SUCCESS)
13127c478bd9Sstevel@tonic-gate 		return (err);
13137c478bd9Sstevel@tonic-gate 
13147c478bd9Sstevel@tonic-gate 	err = psvc_get_attr(hdlp, fan_id, PSVC_TEMP_DIFFERENTIAL_ATTR,
13157c478bd9Sstevel@tonic-gate 		last_errors);
13167c478bd9Sstevel@tonic-gate 	if (err != PSVC_SUCCESS)
13177c478bd9Sstevel@tonic-gate 		return (err);
13187c478bd9Sstevel@tonic-gate 
13197c478bd9Sstevel@tonic-gate 	err = psvc_get_attr(hdlp, fan_id, PSVC_TEMP_DIFFERENTIAL_INDEX_ATTR,
13207c478bd9Sstevel@tonic-gate 		&error_number);
13217c478bd9Sstevel@tonic-gate 	if (err != PSVC_SUCCESS)
13227c478bd9Sstevel@tonic-gate 		return (err);
13237c478bd9Sstevel@tonic-gate 
13247c478bd9Sstevel@tonic-gate 	err = device_temp_check(hdlp, fan_id, &hot_device);
13257c478bd9Sstevel@tonic-gate 	if (err != PSVC_SUCCESS) {
13267c478bd9Sstevel@tonic-gate 		printf("psvc_fan_control failure in device_temp_check\n");
13277c478bd9Sstevel@tonic-gate 		return (err);
13287c478bd9Sstevel@tonic-gate 	}
13297c478bd9Sstevel@tonic-gate 	current_error = setpoint - hot_device;
13307c478bd9Sstevel@tonic-gate 	filtered_error = get_filtered_error(last_errors, current_error);
13317c478bd9Sstevel@tonic-gate 	if (filtered_error <= 0 || filtered_error > hysteresis) {
13327c478bd9Sstevel@tonic-gate 		ampout = (int)((filtered_error * loopgain) + loopbias);
13337c478bd9Sstevel@tonic-gate 		if (ampout < 0)
13347c478bd9Sstevel@tonic-gate 			ampout = 0;
13357c478bd9Sstevel@tonic-gate 		if (ampout > 1023)
13367c478bd9Sstevel@tonic-gate 			ampout = 1023;
13377c478bd9Sstevel@tonic-gate 		err = change_cpu_fans(hdlp, fan_id, ampout);
13387c478bd9Sstevel@tonic-gate 		if (err != PSVC_SUCCESS)
13397c478bd9Sstevel@tonic-gate 			return (err);
13407c478bd9Sstevel@tonic-gate 	}
13417c478bd9Sstevel@tonic-gate 	last_errors[error_number++] = current_error;
13427c478bd9Sstevel@tonic-gate 	if (error_number == PSVC_MAXERRORS)
13437c478bd9Sstevel@tonic-gate 		error_number = 0;
13447c478bd9Sstevel@tonic-gate 
13457c478bd9Sstevel@tonic-gate 	err = psvc_set_attr(hdlp, fan_id, PSVC_TEMP_DIFFERENTIAL_ATTR,
13467c478bd9Sstevel@tonic-gate 		last_errors);
13477c478bd9Sstevel@tonic-gate 	if (err != PSVC_SUCCESS)
13487c478bd9Sstevel@tonic-gate 		return (err);
13497c478bd9Sstevel@tonic-gate 
13507c478bd9Sstevel@tonic-gate 	err = psvc_set_attr(hdlp, fan_id, PSVC_TEMP_DIFFERENTIAL_INDEX_ATTR,
13517c478bd9Sstevel@tonic-gate 		&error_number);
13527c478bd9Sstevel@tonic-gate 	if (err != PSVC_SUCCESS)
13537c478bd9Sstevel@tonic-gate 		return (err);
13547c478bd9Sstevel@tonic-gate 
13557c478bd9Sstevel@tonic-gate 	return (PSVC_SUCCESS);
13567c478bd9Sstevel@tonic-gate }
13577c478bd9Sstevel@tonic-gate 
13587c478bd9Sstevel@tonic-gate int32_t
psvc_fan_present_policy_0(psvc_opaque_t hdlp,char * id)13597c478bd9Sstevel@tonic-gate psvc_fan_present_policy_0(psvc_opaque_t hdlp, char *id)
13607c478bd9Sstevel@tonic-gate {
13617c478bd9Sstevel@tonic-gate 	int32_t		status = PSVC_SUCCESS;
13627c478bd9Sstevel@tonic-gate 	boolean_t	presence;
13637c478bd9Sstevel@tonic-gate 	int fd;
13647c478bd9Sstevel@tonic-gate 	FILE *fp;
1365298b7f4cSjfrank 	int retry;
1366298b7f4cSjfrank 
1367298b7f4cSjfrank 	retry = 0;
1368298b7f4cSjfrank 	do {
1369298b7f4cSjfrank 		if (retry)
1370298b7f4cSjfrank 			(void) sleep(retry_sleep_fan_present);
13717c478bd9Sstevel@tonic-gate 
13727c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &presence);
13737c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
13747c478bd9Sstevel@tonic-gate 			return (status);
1375298b7f4cSjfrank 		retry++;
1376298b7f4cSjfrank 	} while ((retry < n_retry_fan_present) && (presence == PSVC_ABSENT));
13777c478bd9Sstevel@tonic-gate 
13787c478bd9Sstevel@tonic-gate 	if (presence == PSVC_ABSENT) {
13797c478bd9Sstevel@tonic-gate 		/*
13807c478bd9Sstevel@tonic-gate 		 * We make this open, write, close, call because picld
13817c478bd9Sstevel@tonic-gate 		 * starts in rcS.d while print services does not start
13827c478bd9Sstevel@tonic-gate 		 * until later (either rc2.d or rc3.d)
13837c478bd9Sstevel@tonic-gate 		 */
13847c478bd9Sstevel@tonic-gate 		fd = open("/dev/console", O_WRONLY | O_NOCTTY);
13857c478bd9Sstevel@tonic-gate 		if (fd != -1) {
13867c478bd9Sstevel@tonic-gate 			fp = fdopen(fd, "w+");
13877c478bd9Sstevel@tonic-gate 			if (fp != NULL) {
13887c478bd9Sstevel@tonic-gate 				fprintf(fp, FAN_MISSING_MSG, id);
13897c478bd9Sstevel@tonic-gate 				fclose(fp);
13907c478bd9Sstevel@tonic-gate 			}
13917c478bd9Sstevel@tonic-gate 			close(fd);
13927c478bd9Sstevel@tonic-gate 		}
13937c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, FAN_MISSING_MSG, id);
13947c478bd9Sstevel@tonic-gate 	}
13957c478bd9Sstevel@tonic-gate 	return (status);
13967c478bd9Sstevel@tonic-gate }
1397