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