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
55eb92cf2Sdhain  * Common Development and Distribution License (the "License").
65eb92cf2Sdhain  * 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
207c478bd9Sstevel@tonic-gate  */
217c478bd9Sstevel@tonic-gate /*
228a88157cSvb  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
237c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
247c478bd9Sstevel@tonic-gate  */
257c478bd9Sstevel@tonic-gate 
267c478bd9Sstevel@tonic-gate /*
277c478bd9Sstevel@tonic-gate  * Daktari platform platform specific environment monitoring policies
287c478bd9Sstevel@tonic-gate  */
297c478bd9Sstevel@tonic-gate #include	<poll.h>
307c478bd9Sstevel@tonic-gate #include	<syslog.h>
317c478bd9Sstevel@tonic-gate #include	<unistd.h>
327c478bd9Sstevel@tonic-gate #include	<stdio.h>
337c478bd9Sstevel@tonic-gate #include	<stdlib.h>
347c478bd9Sstevel@tonic-gate #include	<errno.h>
357c478bd9Sstevel@tonic-gate #include	<fcntl.h>
367c478bd9Sstevel@tonic-gate #include	<strings.h>
377c478bd9Sstevel@tonic-gate #include	<libintl.h>
387c478bd9Sstevel@tonic-gate #include	<sys/types.h>
397c478bd9Sstevel@tonic-gate #include	<sys/param.h>
407c478bd9Sstevel@tonic-gate #include	<config_admin.h>
417c478bd9Sstevel@tonic-gate #include	<libdevice.h>
427c478bd9Sstevel@tonic-gate #include	<picl.h>
437c478bd9Sstevel@tonic-gate #include	<picltree.h>
447c478bd9Sstevel@tonic-gate #include	<psvc_objects.h>
457c478bd9Sstevel@tonic-gate #include	<sys/i2c/clients/i2c_client.h>
467c478bd9Sstevel@tonic-gate #include	<sys/daktari.h>
477c478bd9Sstevel@tonic-gate #include	<sys/hpc3130_events.h>
487c478bd9Sstevel@tonic-gate #include	<assert.h>
49298b7f4cSjfrank #include	<limits.h>
50298b7f4cSjfrank #include	<sys/systeminfo.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate /*LINTLIBRARY*/
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate /* resides in libcfgadm */
557c478bd9Sstevel@tonic-gate extern cfga_err_t config_change_state(cfga_cmd_t, int, char *const *,
567c478bd9Sstevel@tonic-gate 	const char *, struct cfga_confirm *, struct cfga_msg *, char **,
577c478bd9Sstevel@tonic-gate 	cfga_flags_t);
587c478bd9Sstevel@tonic-gate /* Local Routine */
597c478bd9Sstevel@tonic-gate static int32_t update_gen_fault_led(psvc_opaque_t, char *);
607c478bd9Sstevel@tonic-gate static void shutdown_routine(void);
617c478bd9Sstevel@tonic-gate static int32_t update_thresholds(psvc_opaque_t hdlp, char *id, int offset);
627c478bd9Sstevel@tonic-gate 
637c478bd9Sstevel@tonic-gate 
647c478bd9Sstevel@tonic-gate #ifdef DEBUG
657c478bd9Sstevel@tonic-gate 
667c478bd9Sstevel@tonic-gate static int dak_policy_debug = 0;
677c478bd9Sstevel@tonic-gate 
687c478bd9Sstevel@tonic-gate #define	D1SYS_ERR(ARGS) if (dak_policy_debug & 0x1) syslog ARGS;
697c478bd9Sstevel@tonic-gate #define	D2SYS_ERR(ARGS) if (dak_policy_debug & 0x2) syslog ARGS;
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #else
727c478bd9Sstevel@tonic-gate 
737c478bd9Sstevel@tonic-gate #define	D1SYS_ERR(ARGS)
747c478bd9Sstevel@tonic-gate #define	D2SYS_ERR(ARGS)
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #endif
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #define	I2C_PATH	"/devices/pci@9,700000/ebus@1/i2c@1,30"
79159cf8a6Swesolows #define	I2C_NODE	I2C_PATH ":devctl"
80159cf8a6Swesolows #define	PCF8574		I2C_PATH "/ioexp@0,%x:pcf8574"
81159cf8a6Swesolows #define	PCF8591		I2C_PATH "/adio@0,%x:port_0"
82159cf8a6Swesolows #define	FRU		I2C_PATH "/fru@0,%x:fru"
83159cf8a6Swesolows #define	HPC3130_DEV	I2C_PATH "/hotplug-controller@0,%2x:port_%1x"
847c478bd9Sstevel@tonic-gate #define	GEN_FAULT_LED	"FSP_GEN_FAULT_LED"
857c478bd9Sstevel@tonic-gate #define	EMPTY_STRING	"EMPTY"
867c478bd9Sstevel@tonic-gate #define	DEVICE_FAILURE_MSG	gettext("WARNING: Device %s failure detected")
877c478bd9Sstevel@tonic-gate #define	DEVICE_INSERTED_MSG	gettext("Device %s inserted")
887c478bd9Sstevel@tonic-gate #define	DEVICE_REMOVED_MSG	gettext("Device %s removed")
897c478bd9Sstevel@tonic-gate #define	PS_UNPLUGGED_MSG	gettext("Device %s unplugged")
907c478bd9Sstevel@tonic-gate #define	PS_PLUGGED_MSG		gettext("Device %s Plugged in")
917c478bd9Sstevel@tonic-gate #define	DEVICE_OK_MSG		gettext("Device %s OK")
927c478bd9Sstevel@tonic-gate #define	SET_LED_FAILED_MSG		\
937c478bd9Sstevel@tonic-gate 	gettext("Failed to set LED state, id = %s, errno = %d\n")
947c478bd9Sstevel@tonic-gate #define	GET_PRESENCE_FAILED_MSG		\
957c478bd9Sstevel@tonic-gate 	gettext("Failed to get presence attribute, id = %s, errno = %d\n")
967c478bd9Sstevel@tonic-gate #define	GET_SENSOR_FAILED_MSG		\
977c478bd9Sstevel@tonic-gate 	gettext("Failed to get sensor value, id = %s, errno = %d\n")
987c478bd9Sstevel@tonic-gate #define	ADD_PS_MSG			\
99*338c2f03SToomas Soome     gettext("WARNING: Only 1 Power Supply in system. ADD a 2nd Power Supply.\n")
1007c478bd9Sstevel@tonic-gate #define	REMOVE_LOAD_MSG			\
1017c478bd9Sstevel@tonic-gate 	gettext("WARNING: Power Supply at 95%% current. Remove some load.\n")
1027c478bd9Sstevel@tonic-gate #define	PS_OVER_CURRENT_MSG		\
1037c478bd9Sstevel@tonic-gate 	gettext("WARNING: Power Supply overcurrent detected\n")
1048a88157cSvb #define	PS_UNDER_CURRENT_MSG		\
1058a88157cSvb 	gettext("WARNING: PS%d Undercurrent on one or more DC lines\n")
1067c478bd9Sstevel@tonic-gate #define	DEVICE_UNKNOWN_MSG	gettext("Unknown device %s instance %d\n")
1077c478bd9Sstevel@tonic-gate #define	DEVICE_HANDLE_FAIL_MSG		\
1087c478bd9Sstevel@tonic-gate 	gettext("Failed to get device handle for %s, errno = %d\n")
1097c478bd9Sstevel@tonic-gate #define	DEVTREE_NODE_CREATE_FAILED	\
1107c478bd9Sstevel@tonic-gate 	gettext("psvc PICL plugin: Failed to create node for %s, errno = %d")
1117c478bd9Sstevel@tonic-gate #define	DEVTREE_NODE_DELETE_FAILED	\
1127c478bd9Sstevel@tonic-gate 	gettext("psvc PICL plugin: Failed to delete node for %s, errno = %d")
1137c478bd9Sstevel@tonic-gate #define	DISK_FAULT_MSG		gettext("%s: Error Reported\n")
1147c478bd9Sstevel@tonic-gate #define	DISK_OK_MSG		gettext("%s: Error Cleared\n")
1157c478bd9Sstevel@tonic-gate #define	SET_FANSPEED_FAILED_MSG		\
1167c478bd9Sstevel@tonic-gate 	gettext("Failed to set fan speed, id = %s, errno = %d\n")
1177c478bd9Sstevel@tonic-gate #define	GET_ATTR_FRU_FAILED_MSG	gettext("Failed psvc_get_attr for FRU info\n")
1187c478bd9Sstevel@tonic-gate #define	NO_FRU_INFO_MSG			\
1197c478bd9Sstevel@tonic-gate 	gettext("No FRU Information for %s using default module card\n")
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate #define	DAKTARI_MAX_PS	3
1227c478bd9Sstevel@tonic-gate #define	DAK_MAX_PS_I_SENSORS 4
1237c478bd9Sstevel@tonic-gate #define	DAK_MAX_DISKS	12
1247c478bd9Sstevel@tonic-gate #define	DAK_MAX_CPU_MOD	4
1257c478bd9Sstevel@tonic-gate #define	DAK_MAX_FAULT_SENSORS 3
1267c478bd9Sstevel@tonic-gate #define	DAK_MAX_FANS 10
1277c478bd9Sstevel@tonic-gate 
1287c478bd9Sstevel@tonic-gate static int co_ps = 0;
1297c478bd9Sstevel@tonic-gate static char *shutdown_string = "shutdown -y -g 60 -i 5 \"OVERTEMP condition\"";
1307c478bd9Sstevel@tonic-gate 
1317c478bd9Sstevel@tonic-gate typedef struct i2c_hp {
1327c478bd9Sstevel@tonic-gate 	int32_t		addr[2];
1337c478bd9Sstevel@tonic-gate 	char		name[256];
1347c478bd9Sstevel@tonic-gate 	char		compatible[256];
1357c478bd9Sstevel@tonic-gate } i2c_hp_t;
1367c478bd9Sstevel@tonic-gate 
1377c478bd9Sstevel@tonic-gate typedef struct seg_desc {
1387c478bd9Sstevel@tonic-gate 	int32_t segdesc;
1397c478bd9Sstevel@tonic-gate 	int16_t segoffset;
1407c478bd9Sstevel@tonic-gate 	int16_t seglength;
1417c478bd9Sstevel@tonic-gate } seg_desc_t;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate static int32_t threshold_names[] = {
1447c478bd9Sstevel@tonic-gate 	PSVC_HW_LO_SHUT_ATTR,
1457c478bd9Sstevel@tonic-gate 	PSVC_LO_SHUT_ATTR,
1467c478bd9Sstevel@tonic-gate 	PSVC_LO_WARN_ATTR,
1477c478bd9Sstevel@tonic-gate 	PSVC_NOT_USED,			/* LOW MODE which is not used */
1487c478bd9Sstevel@tonic-gate 	PSVC_OPTIMAL_TEMP_ATTR,
1497c478bd9Sstevel@tonic-gate 	PSVC_HI_WARN_ATTR,
1507c478bd9Sstevel@tonic-gate 	PSVC_HI_SHUT_ATTR,
1517c478bd9Sstevel@tonic-gate 	PSVC_HW_HI_SHUT_ATTR
1527c478bd9Sstevel@tonic-gate };
1537c478bd9Sstevel@tonic-gate 
154298b7f4cSjfrank /*
155298b7f4cSjfrank  * The I2C bus is noisy, and the state may be incorrectly reported as
156298b7f4cSjfrank  * having changed.  When the state changes, we attempt to confirm by
157298b7f4cSjfrank  * retrying.  If any retries indicate that the state has not changed, we
158298b7f4cSjfrank  * assume the state change(s) were incorrect and the state has not changed.
159298b7f4cSjfrank  * The following variables are used to store the tuneable values read in
160298b7f4cSjfrank  * from the optional i2cparam.conf file for this shared object library.
161298b7f4cSjfrank  */
162298b7f4cSjfrank static int n_retry_pshp_status = PSVC_NUM_OF_RETRIES;
163298b7f4cSjfrank static int retry_sleep_pshp_status = 1;
164298b7f4cSjfrank static int n_read_overcurrent = PSVC_THRESHOLD_COUNTER;
1658a88157cSvb static int n_read_undercurrent = PSVC_THRESHOLD_COUNTER;
166298b7f4cSjfrank static int n_retry_devicefail = PSVC_NUM_OF_RETRIES;
167298b7f4cSjfrank static int retry_sleep_devicefail = 1;
168298b7f4cSjfrank static int n_read_fanfault = PSVC_THRESHOLD_COUNTER;
169298b7f4cSjfrank static int n_retry_pshp = PSVC_NUM_OF_RETRIES;
170298b7f4cSjfrank static int retry_sleep_pshp = 1;
171298b7f4cSjfrank static int n_retry_diskfault = PSVC_NUM_OF_RETRIES;
172298b7f4cSjfrank static int retry_sleep_diskfault = 1;
173298b7f4cSjfrank static int n_retry_temp_shutdown = PSVC_NUM_OF_RETRIES;
174298b7f4cSjfrank static int retry_sleep_temp_shutdown = 1;
175298b7f4cSjfrank 
176298b7f4cSjfrank typedef struct {
177298b7f4cSjfrank 	int *pvar;
178298b7f4cSjfrank 	char *texttag;
179298b7f4cSjfrank } i2c_noise_param_t;
180298b7f4cSjfrank 
181298b7f4cSjfrank static i2c_noise_param_t i2cparams[] = {
182298b7f4cSjfrank 	&n_retry_pshp_status, "n_retry_pshp_status",
183298b7f4cSjfrank 	&retry_sleep_pshp_status, "retry_sleep_pshp_status",
184298b7f4cSjfrank 	&n_read_overcurrent, "n_read_overcurrent",
1858a88157cSvb 	&n_read_undercurrent, "n_read_undercurrent",
186298b7f4cSjfrank 	&n_retry_devicefail, "n_retry_devicefail",
187298b7f4cSjfrank 	&retry_sleep_devicefail, "retry_sleep_devicefail",
188298b7f4cSjfrank 	&n_read_fanfault, "n_read_fanfault",
189298b7f4cSjfrank 	&n_retry_pshp, "n_retry_pshp",
190298b7f4cSjfrank 	&retry_sleep_pshp, "retry_sleep_pshp",
191298b7f4cSjfrank 	&n_retry_diskfault, "n_retry_diskfault",
192298b7f4cSjfrank 	&retry_sleep_diskfault, "retry_sleep_diskfault",
193298b7f4cSjfrank 	&n_retry_temp_shutdown, "n_retry_temp_shutdown",
194298b7f4cSjfrank 	&retry_sleep_temp_shutdown, "retry_sleep_temp_shutdown",
195298b7f4cSjfrank 	NULL, NULL
196298b7f4cSjfrank };
197298b7f4cSjfrank 
198298b7f4cSjfrank #pragma init(i2cparams_load)
199298b7f4cSjfrank 
200298b7f4cSjfrank static void
i2cparams_debug(i2c_noise_param_t * pi2cparams,char * platform,int usingDefaults)201298b7f4cSjfrank i2cparams_debug(i2c_noise_param_t *pi2cparams, char *platform,
202*338c2f03SToomas Soome     int usingDefaults)
203298b7f4cSjfrank {
204298b7f4cSjfrank 	char s[128];
205298b7f4cSjfrank 	i2c_noise_param_t *p;
206298b7f4cSjfrank 
207298b7f4cSjfrank 	if (!usingDefaults) {
208298b7f4cSjfrank 		(void) snprintf(s, sizeof (s),
209298b7f4cSjfrank 		    "# Values from /usr/platform/%s/lib/i2cparam.conf\n",
210298b7f4cSjfrank 		    platform);
211298b7f4cSjfrank 		syslog(LOG_WARNING, "%s", s);
212298b7f4cSjfrank 	} else {
213298b7f4cSjfrank 		/* no file - we're using the defaults */
214298b7f4cSjfrank 		(void) snprintf(s, sizeof (s),
215298b7f4cSjfrank "# No /usr/platform/%s/lib/i2cparam.conf file, using defaults\n",
216298b7f4cSjfrank 		    platform);
217298b7f4cSjfrank 	}
218298b7f4cSjfrank 	(void) fputs(s, stdout);
219298b7f4cSjfrank 	p = pi2cparams;
220298b7f4cSjfrank 	while (p->pvar != NULL) {
221298b7f4cSjfrank 		(void) snprintf(s, sizeof (s), "%s %d\n", p->texttag,
222298b7f4cSjfrank 		    *(p->pvar));
223298b7f4cSjfrank 		if (!usingDefaults)
224298b7f4cSjfrank 			syslog(LOG_WARNING, "%s", s);
225298b7f4cSjfrank 		(void) fputs(s, stdout);
226298b7f4cSjfrank 		p++;
227298b7f4cSjfrank 	}
228298b7f4cSjfrank }
229298b7f4cSjfrank 
230298b7f4cSjfrank static void
i2cparams_load(void)231298b7f4cSjfrank i2cparams_load(void)
232298b7f4cSjfrank {
233298b7f4cSjfrank 	FILE *fp;
234298b7f4cSjfrank 	char filename[PATH_MAX];
235298b7f4cSjfrank 	char platform[64];
236298b7f4cSjfrank 	char s[128];
237298b7f4cSjfrank 	char var[128];
238298b7f4cSjfrank 	int val;
239298b7f4cSjfrank 	i2c_noise_param_t *p;
240298b7f4cSjfrank 
241298b7f4cSjfrank 	if (sysinfo(SI_PLATFORM, platform, sizeof (platform)) == -1) {
242298b7f4cSjfrank 		syslog(LOG_ERR, "sysinfo error %s\n", strerror(errno));
243298b7f4cSjfrank 		return;
244298b7f4cSjfrank 	}
245298b7f4cSjfrank 	(void) snprintf(filename, sizeof (filename),
246298b7f4cSjfrank 	    "/usr/platform/%s/lib/i2cparam.conf", platform);
247298b7f4cSjfrank 	/* read thru the i2cparam.conf file and set variables */
248298b7f4cSjfrank 	if ((fp = fopen(filename, "r")) != NULL) {
249298b7f4cSjfrank 		while (fgets(s, sizeof (s), fp) != NULL) {
250298b7f4cSjfrank 			if (s[0] == '#') /* skip comment lines */
251298b7f4cSjfrank 				continue;
252298b7f4cSjfrank 			/* try to find a string match and get the value */
253298b7f4cSjfrank 			if (sscanf(s, "%127s %d", var, &val) != 2)
254298b7f4cSjfrank 				continue;
255298b7f4cSjfrank 			if (val < 1)
256298b7f4cSjfrank 				val = 1;  /* clamp min value */
257298b7f4cSjfrank 			p = &(i2cparams[0]);
258298b7f4cSjfrank 			while (p->pvar != NULL) {
259298b7f4cSjfrank 				if (strncmp(p->texttag, var, sizeof (var)) ==
260298b7f4cSjfrank 				    0) {
261298b7f4cSjfrank 					*(p->pvar) = val;
262298b7f4cSjfrank 					break;
263298b7f4cSjfrank 				}
264298b7f4cSjfrank 				p++;
265298b7f4cSjfrank 			}
266298b7f4cSjfrank 		}
267298b7f4cSjfrank 		(void) fclose(fp);
268298b7f4cSjfrank 	}
269298b7f4cSjfrank 	/* output the values of the parameters */
270298b7f4cSjfrank 	i2cparams_debug(&(i2cparams[0]), platform, ((fp == NULL)? 1 : 0));
271298b7f4cSjfrank }
272298b7f4cSjfrank 
2737c478bd9Sstevel@tonic-gate int32_t
psvc_MB_update_thresholds_0(psvc_opaque_t hdlp,char * id,int offset)2747c478bd9Sstevel@tonic-gate psvc_MB_update_thresholds_0(psvc_opaque_t hdlp, char *id, int offset)
2757c478bd9Sstevel@tonic-gate {
2767c478bd9Sstevel@tonic-gate 	int IO_offset = 0xd;
2777c478bd9Sstevel@tonic-gate 	int32_t err;
2787c478bd9Sstevel@tonic-gate 
2797c478bd9Sstevel@tonic-gate 	err = update_thresholds(hdlp, id, IO_offset);
2807c478bd9Sstevel@tonic-gate 
2817c478bd9Sstevel@tonic-gate 	return (err);
2827c478bd9Sstevel@tonic-gate }
2837c478bd9Sstevel@tonic-gate 
2847c478bd9Sstevel@tonic-gate int32_t
psvc_IO_update_thresholds_0(psvc_opaque_t hdlp,char * id,int offset)2857c478bd9Sstevel@tonic-gate psvc_IO_update_thresholds_0(psvc_opaque_t hdlp, char *id, int offset)
2867c478bd9Sstevel@tonic-gate {
2877c478bd9Sstevel@tonic-gate 	int IO_offset = 0x8;
2887c478bd9Sstevel@tonic-gate 	int32_t err;
2897c478bd9Sstevel@tonic-gate 
2907c478bd9Sstevel@tonic-gate 	err = update_thresholds(hdlp, id, IO_offset);
2917c478bd9Sstevel@tonic-gate 
2927c478bd9Sstevel@tonic-gate 	return (err);
2937c478bd9Sstevel@tonic-gate }
2947c478bd9Sstevel@tonic-gate 
2957c478bd9Sstevel@tonic-gate int32_t
psvc_DBP_update_thresholds_0(psvc_opaque_t hdlp,char * id,int offset)2967c478bd9Sstevel@tonic-gate psvc_DBP_update_thresholds_0(psvc_opaque_t hdlp, char *id, int offset)
2977c478bd9Sstevel@tonic-gate {
2987c478bd9Sstevel@tonic-gate 	int IO_offset = 0x7;
2997c478bd9Sstevel@tonic-gate 	int32_t err;
3007c478bd9Sstevel@tonic-gate 
3017c478bd9Sstevel@tonic-gate 	err = update_thresholds(hdlp, id, IO_offset);
3027c478bd9Sstevel@tonic-gate 
3037c478bd9Sstevel@tonic-gate 	return (err);
3047c478bd9Sstevel@tonic-gate }
3057c478bd9Sstevel@tonic-gate 
3067c478bd9Sstevel@tonic-gate /*
3077c478bd9Sstevel@tonic-gate  * used to determine if a change of state occured. valid when states
3087c478bd9Sstevel@tonic-gate  * are strings.
3097c478bd9Sstevel@tonic-gate  */
3107c478bd9Sstevel@tonic-gate static int8_t
change_of_state_str(char * state1,char * check1,char * state2,char * check2)3117c478bd9Sstevel@tonic-gate change_of_state_str(char *state1, char *check1, char *state2, char *check2)
3127c478bd9Sstevel@tonic-gate {
3137c478bd9Sstevel@tonic-gate 	int change = 0;
3147c478bd9Sstevel@tonic-gate 
3157c478bd9Sstevel@tonic-gate 	if ((strcmp(state1, check1) == 0) && (strcmp(state2, check2) != 0))
3167c478bd9Sstevel@tonic-gate 		change = 1;
3177c478bd9Sstevel@tonic-gate 	if ((strcmp(state1, check1) != 0) && (strcmp(state2, check2) == 0))
3187c478bd9Sstevel@tonic-gate 		change = 1;
3197c478bd9Sstevel@tonic-gate 
3207c478bd9Sstevel@tonic-gate 	return (change);
3217c478bd9Sstevel@tonic-gate }
3227c478bd9Sstevel@tonic-gate 
3237c478bd9Sstevel@tonic-gate /*
3247c478bd9Sstevel@tonic-gate  * Update thresholds tries to read the temperature thresholds from the FRU
3257c478bd9Sstevel@tonic-gate  * SEEproms and then updates the thresholds in the object by overriding the
3267c478bd9Sstevel@tonic-gate  * hardcoded thresholds.  For Daktari it is an Error if the FRU does not
3277c478bd9Sstevel@tonic-gate  * contain the segment that had the temperature thresholds.
3287c478bd9Sstevel@tonic-gate  */
3297c478bd9Sstevel@tonic-gate static int32_t
update_thresholds(psvc_opaque_t hdlp,char * id,int offset)3307c478bd9Sstevel@tonic-gate update_thresholds(psvc_opaque_t hdlp, char *id, int offset)
3317c478bd9Sstevel@tonic-gate {
3327c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
3337c478bd9Sstevel@tonic-gate 	fru_info_t fru_data;
3347c478bd9Sstevel@tonic-gate 	char *fru, seg_name[2];
3357c478bd9Sstevel@tonic-gate 	int8_t seg_count, temp_array[8];
3367c478bd9Sstevel@tonic-gate 	int32_t match_count, i, j, seg_desc_start = 0x1806, temp_address;
3377c478bd9Sstevel@tonic-gate 	int32_t seg_found, temp;
3387c478bd9Sstevel@tonic-gate 	boolean_t present;
3397c478bd9Sstevel@tonic-gate 	seg_desc_t segment;
3407c478bd9Sstevel@tonic-gate 
3417c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &present);
3427c478bd9Sstevel@tonic-gate 	if ((status != PSVC_SUCCESS) || (present != PSVC_PRESENT))
3437c478bd9Sstevel@tonic-gate 		return (status);
3447c478bd9Sstevel@tonic-gate 
3457c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_ASSOC_MATCHES_ATTR, &match_count,
3467c478bd9Sstevel@tonic-gate 	    PSVC_FRU);
3477c478bd9Sstevel@tonic-gate 	if (status == PSVC_FAILURE)
3487c478bd9Sstevel@tonic-gate 		return (status);
3497c478bd9Sstevel@tonic-gate 
3507c478bd9Sstevel@tonic-gate 	for (i = 0; i < match_count; i++) {
3517c478bd9Sstevel@tonic-gate 		seg_found = 0;
3527c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR,
3537c478bd9Sstevel@tonic-gate 		    &fru, PSVC_FRU, i);
3547c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
3557c478bd9Sstevel@tonic-gate 			return (status);
3567c478bd9Sstevel@tonic-gate 
3577c478bd9Sstevel@tonic-gate 		fru_data.buf_start = 0x1805;
3587c478bd9Sstevel@tonic-gate 		fru_data.buf = (char *)&seg_count;
3597c478bd9Sstevel@tonic-gate 		fru_data.read_size = 1;
3607c478bd9Sstevel@tonic-gate 
3617c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, fru, PSVC_FRU_INFO_ATTR,
3627c478bd9Sstevel@tonic-gate 		    &fru_data);
3637c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS) {
3647c478bd9Sstevel@tonic-gate 			return (status);
3657c478bd9Sstevel@tonic-gate 		}
3667c478bd9Sstevel@tonic-gate 		for (j = 0; (j < seg_count) && (!seg_found); j++) {
3677c478bd9Sstevel@tonic-gate 			fru_data.buf_start = seg_desc_start;
3687c478bd9Sstevel@tonic-gate 			fru_data.buf = seg_name;
3697c478bd9Sstevel@tonic-gate 			fru_data.read_size = 2;
3707c478bd9Sstevel@tonic-gate 
3717c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, fru, PSVC_FRU_INFO_ATTR,
3727c478bd9Sstevel@tonic-gate 			    &fru_data);
3737c478bd9Sstevel@tonic-gate 
3747c478bd9Sstevel@tonic-gate 			seg_desc_start = seg_desc_start + 2;
3757c478bd9Sstevel@tonic-gate 			fru_data.buf_start = seg_desc_start;
3767c478bd9Sstevel@tonic-gate 			fru_data.buf = (char *)&segment;
3777c478bd9Sstevel@tonic-gate 			fru_data.read_size = sizeof (seg_desc_t);
3787c478bd9Sstevel@tonic-gate 
3797c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, fru, PSVC_FRU_INFO_ATTR,
3807c478bd9Sstevel@tonic-gate 			    &fru_data);
3817c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS) {
3827c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
3837c478bd9Sstevel@tonic-gate 				    "Failed psvc_get_attr for FRU info\n");
3847c478bd9Sstevel@tonic-gate 				return (status);
3857c478bd9Sstevel@tonic-gate 			}
3867c478bd9Sstevel@tonic-gate 			seg_desc_start = seg_desc_start + sizeof (seg_desc_t);
3877c478bd9Sstevel@tonic-gate 			if (memcmp(seg_name, "SC", 2) == 0)
3887c478bd9Sstevel@tonic-gate 				seg_found = 1;
3897c478bd9Sstevel@tonic-gate 		}
3907c478bd9Sstevel@tonic-gate 		if (seg_found) {
3917c478bd9Sstevel@tonic-gate 			temp_address = segment.segoffset + offset;
3927c478bd9Sstevel@tonic-gate 			fru_data.buf_start = temp_address;
3937c478bd9Sstevel@tonic-gate 			fru_data.buf = (char *)&temp_array;
3947c478bd9Sstevel@tonic-gate 			fru_data.read_size = sizeof (temp_array);
3957c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, fru, PSVC_FRU_INFO_ATTR,
3967c478bd9Sstevel@tonic-gate 			    &fru_data);
3977c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS) {
3987c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
3997c478bd9Sstevel@tonic-gate 				    "Failed psvc_get_attr for FRU info\n");
4007c478bd9Sstevel@tonic-gate 				return (status);
4017c478bd9Sstevel@tonic-gate 			} else {
4027c478bd9Sstevel@tonic-gate 				for (j = 0; j < sizeof (temp_array); j++) {
4037c478bd9Sstevel@tonic-gate 					if (threshold_names[j] ==
4047c478bd9Sstevel@tonic-gate 					    PSVC_NOT_USED)
4057c478bd9Sstevel@tonic-gate 						continue;
4067c478bd9Sstevel@tonic-gate 					temp = temp_array[j];
4077c478bd9Sstevel@tonic-gate 					status = psvc_set_attr(hdlp, id,
4087c478bd9Sstevel@tonic-gate 					    threshold_names[j], &temp);
4097c478bd9Sstevel@tonic-gate 					if (status != PSVC_SUCCESS) {
4107c478bd9Sstevel@tonic-gate 						return (status);
4117c478bd9Sstevel@tonic-gate 					}
4127c478bd9Sstevel@tonic-gate 				}
4137c478bd9Sstevel@tonic-gate 			}
4147c478bd9Sstevel@tonic-gate 		} else {
4157c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "No FRU Information for %s"
4167c478bd9Sstevel@tonic-gate 			    " using default temperatures\n", id);
4177c478bd9Sstevel@tonic-gate 		}
4187c478bd9Sstevel@tonic-gate 	}
4197c478bd9Sstevel@tonic-gate 	return (status);
4207c478bd9Sstevel@tonic-gate }
4217c478bd9Sstevel@tonic-gate 
4227c478bd9Sstevel@tonic-gate int32_t
psvc_fan_init_speed_0(psvc_opaque_t hdlp,char * id)4237c478bd9Sstevel@tonic-gate psvc_fan_init_speed_0(psvc_opaque_t hdlp, char *id)
4247c478bd9Sstevel@tonic-gate {
4257c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
4267c478bd9Sstevel@tonic-gate 	boolean_t present;
4277c478bd9Sstevel@tonic-gate 	char *control_id;
4287c478bd9Sstevel@tonic-gate 	int32_t init_speed = 0;
4297c478bd9Sstevel@tonic-gate 
4307c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &present);
4317c478bd9Sstevel@tonic-gate 	if ((status != PSVC_SUCCESS) || (present != PSVC_PRESENT))
4327c478bd9Sstevel@tonic-gate 		return (status);
4337c478bd9Sstevel@tonic-gate 
4347c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR, &control_id,
4355eb92cf2Sdhain 	    PSVC_FAN_DRIVE_CONTROL, 0);
4367c478bd9Sstevel@tonic-gate 	if (status != PSVC_SUCCESS)
4377c478bd9Sstevel@tonic-gate 		return (status);
4387c478bd9Sstevel@tonic-gate 
4397c478bd9Sstevel@tonic-gate 	status = psvc_set_attr(hdlp, control_id, PSVC_CONTROL_VALUE_ATTR,
4407c478bd9Sstevel@tonic-gate 	    &init_speed);
4417c478bd9Sstevel@tonic-gate 	if (status == PSVC_FAILURE) {
4427c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, SET_FANSPEED_FAILED_MSG, control_id, errno);
4437c478bd9Sstevel@tonic-gate 		return (status);
4447c478bd9Sstevel@tonic-gate 	}
4457c478bd9Sstevel@tonic-gate 
4467c478bd9Sstevel@tonic-gate 	return (status);
4477c478bd9Sstevel@tonic-gate }
4487c478bd9Sstevel@tonic-gate 
4497c478bd9Sstevel@tonic-gate int32_t
psvc_update_setpoint_0(psvc_opaque_t hdlp,char * id)4507c478bd9Sstevel@tonic-gate psvc_update_setpoint_0(psvc_opaque_t hdlp, char *id)
4517c478bd9Sstevel@tonic-gate {
4527c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
4537c478bd9Sstevel@tonic-gate 	char *temp_sensor;
4547c478bd9Sstevel@tonic-gate 	int32_t match_count, i, temp;
4557c478bd9Sstevel@tonic-gate 	int16_t lowest_temp = 500;
4567c478bd9Sstevel@tonic-gate 	boolean_t present;
4577c478bd9Sstevel@tonic-gate 
4587c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &present);
4597c478bd9Sstevel@tonic-gate 	if ((status != PSVC_SUCCESS) || (present != PSVC_PRESENT))
4607c478bd9Sstevel@tonic-gate 		return (status);
4617c478bd9Sstevel@tonic-gate 
4627c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_ASSOC_MATCHES_ATTR, &match_count,
4637c478bd9Sstevel@tonic-gate 	    PSVC_DEV_TEMP_SENSOR);
4647c478bd9Sstevel@tonic-gate 	if (status == PSVC_FAILURE)
4657c478bd9Sstevel@tonic-gate 		return (status);
4667c478bd9Sstevel@tonic-gate 
4677c478bd9Sstevel@tonic-gate 	for (i = 0; i < match_count; i++) {
4687c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR,
4697c478bd9Sstevel@tonic-gate 		    &temp_sensor, PSVC_DEV_TEMP_SENSOR, i);
4707c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
4717c478bd9Sstevel@tonic-gate 			return (status);
4727c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, temp_sensor,
4737c478bd9Sstevel@tonic-gate 		    PSVC_OPTIMAL_TEMP_ATTR, &temp);
4747c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS) {
4757c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, "Failed to get Optimal temp for %s\n",
4767c478bd9Sstevel@tonic-gate 			    temp_sensor);
4777c478bd9Sstevel@tonic-gate 			return (status);
4787c478bd9Sstevel@tonic-gate 		}
4797c478bd9Sstevel@tonic-gate 		if (temp < lowest_temp)
4807c478bd9Sstevel@tonic-gate 			lowest_temp = temp;
4817c478bd9Sstevel@tonic-gate 	}
4827c478bd9Sstevel@tonic-gate 	status = psvc_set_attr(hdlp, id, PSVC_SETPOINT_ATTR, &lowest_temp);
4837c478bd9Sstevel@tonic-gate 	if (status == PSVC_FAILURE) {
4847c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, "Failed to change setpoint for %s\n", id);
4857c478bd9Sstevel@tonic-gate 		return (status);
4867c478bd9Sstevel@tonic-gate 	}
4877c478bd9Sstevel@tonic-gate 	return (status);
4887c478bd9Sstevel@tonic-gate }
4897c478bd9Sstevel@tonic-gate 
4907c478bd9Sstevel@tonic-gate int32_t
psvc_remove_missing_nodes_0(psvc_opaque_t hdlp,char * id)4917c478bd9Sstevel@tonic-gate psvc_remove_missing_nodes_0(psvc_opaque_t hdlp, char *id)
4927c478bd9Sstevel@tonic-gate {
4937c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
4947c478bd9Sstevel@tonic-gate 	char state[32];
4957c478bd9Sstevel@tonic-gate 	char *physical_dev;
4967c478bd9Sstevel@tonic-gate 	int32_t i, device_count;
4977c478bd9Sstevel@tonic-gate 	char parent_path[256];
4987c478bd9Sstevel@tonic-gate 	picl_nodehdl_t child_node;
4997c478bd9Sstevel@tonic-gate 	boolean_t present;
5007c478bd9Sstevel@tonic-gate 
5017c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_ASSOC_MATCHES_ATTR,
5027c478bd9Sstevel@tonic-gate 	    &device_count, PSVC_PHYSICAL_DEVICE);
5037c478bd9Sstevel@tonic-gate 	if (status == PSVC_FAILURE)
5047c478bd9Sstevel@tonic-gate 		return (status);
5057c478bd9Sstevel@tonic-gate 
5067c478bd9Sstevel@tonic-gate 	for (i = 0; i < device_count; i++) {
5077c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR,
5087c478bd9Sstevel@tonic-gate 		    &physical_dev, PSVC_PHYSICAL_DEVICE, i);
5097c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
5107c478bd9Sstevel@tonic-gate 			return (status);
5117c478bd9Sstevel@tonic-gate 		if (strncmp(physical_dev, "LTC1427", 7) == 0)
5127c478bd9Sstevel@tonic-gate 			continue;
5137c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, physical_dev,
5147c478bd9Sstevel@tonic-gate 		    PSVC_PROBE_RESULT_ATTR, state);
5157c478bd9Sstevel@tonic-gate 		if (status != PSVC_SUCCESS)
5167c478bd9Sstevel@tonic-gate 			continue;
5177c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, physical_dev, PSVC_PRESENCE_ATTR,
5187c478bd9Sstevel@tonic-gate 		    &present);
5197c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE) {
5207c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, GET_PRESENCE_FAILED_MSG, physical_dev,
5217c478bd9Sstevel@tonic-gate 			    errno);
5227c478bd9Sstevel@tonic-gate 			return (status);
5237c478bd9Sstevel@tonic-gate 		}
5247c478bd9Sstevel@tonic-gate 
5257c478bd9Sstevel@tonic-gate 		if ((strcmp(state, PSVC_ERROR) == 0) &&
5267c478bd9Sstevel@tonic-gate 		    (present == PSVC_PRESENT)) {
5277c478bd9Sstevel@tonic-gate 			/* convert name to node, and parent path */
5287c478bd9Sstevel@tonic-gate 			psvcplugin_lookup(physical_dev, parent_path,
5297c478bd9Sstevel@tonic-gate 			    &child_node);
5307c478bd9Sstevel@tonic-gate 			/* Device removed */
5317c478bd9Sstevel@tonic-gate 			ptree_delete_node(child_node);
5327c478bd9Sstevel@tonic-gate 		}
5337c478bd9Sstevel@tonic-gate 	}
5347c478bd9Sstevel@tonic-gate 	return (status);
5357c478bd9Sstevel@tonic-gate }
5367c478bd9Sstevel@tonic-gate 
5377c478bd9Sstevel@tonic-gate int32_t
psvc_check_ps_hotplug_status_0(psvc_opaque_t hdlp,char * id)5387c478bd9Sstevel@tonic-gate psvc_check_ps_hotplug_status_0(psvc_opaque_t hdlp, char *id)
5397c478bd9Sstevel@tonic-gate {
5407c478bd9Sstevel@tonic-gate 	char		fail_valid_switch_id[PICL_PROPNAMELEN_MAX];
5417c478bd9Sstevel@tonic-gate 	int32_t		status = PSVC_SUCCESS;
5427c478bd9Sstevel@tonic-gate 	char		valid_switch_state[32];
5437c478bd9Sstevel@tonic-gate 	char		state[32], fault[32];
5447c478bd9Sstevel@tonic-gate 	int32_t		led_count, j;
5457c478bd9Sstevel@tonic-gate 	char		*led_id;
5467c478bd9Sstevel@tonic-gate 	char		led_state[32];
5477c478bd9Sstevel@tonic-gate 	boolean_t	present;
5487c478bd9Sstevel@tonic-gate 	static int8_t	hotplug_failed_count = 0;
5495eb92cf2Sdhain 	static int	unplugged_ps = 0;
550298b7f4cSjfrank 	int	retry;
5515eb92cf2Sdhain 	char		*unplugged_id;
5527c478bd9Sstevel@tonic-gate 
5537c478bd9Sstevel@tonic-gate 	status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &present);
5547c478bd9Sstevel@tonic-gate 	if (status == PSVC_FAILURE) {
5557c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, GET_PRESENCE_FAILED_MSG, id, errno);
5567c478bd9Sstevel@tonic-gate 		return (status);
5577c478bd9Sstevel@tonic-gate 	}
5587c478bd9Sstevel@tonic-gate 
5597c478bd9Sstevel@tonic-gate 	if (present == PSVC_ABSENT) {
5607c478bd9Sstevel@tonic-gate 		errno = ENODEV;
5617c478bd9Sstevel@tonic-gate 		return (PSVC_FAILURE);
5627c478bd9Sstevel@tonic-gate 	}
5637c478bd9Sstevel@tonic-gate 
5647c478bd9Sstevel@tonic-gate 	snprintf(fail_valid_switch_id, sizeof (fail_valid_switch_id), "%s%s",
5657c478bd9Sstevel@tonic-gate 	    id, "_SENSOR_VALID_SWITCH");
5667c478bd9Sstevel@tonic-gate 
5677c478bd9Sstevel@tonic-gate 	retry = 0;
5687c478bd9Sstevel@tonic-gate 	do {
5697c478bd9Sstevel@tonic-gate 		if (retry)
570298b7f4cSjfrank 			(void) sleep(retry_sleep_pshp_status);
5717c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, fail_valid_switch_id,
5727c478bd9Sstevel@tonic-gate 		    PSVC_STATE_ATTR, valid_switch_state);
5737c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE) {
5747c478bd9Sstevel@tonic-gate 			if (hotplug_failed_count == 0) {
5757c478bd9Sstevel@tonic-gate 				/*
5767c478bd9Sstevel@tonic-gate 				 * First time the get_attr call failed
5777c478bd9Sstevel@tonic-gate 				 * set count so that if we fail again
5787c478bd9Sstevel@tonic-gate 				 * we will know
5797c478bd9Sstevel@tonic-gate 				 */
5807c478bd9Sstevel@tonic-gate 				hotplug_failed_count = 1;
5817c478bd9Sstevel@tonic-gate 				/*
5827c478bd9Sstevel@tonic-gate 				 * We probably failed because the power
5837c478bd9Sstevel@tonic-gate 				 * supply was just insterted or removed
5847c478bd9Sstevel@tonic-gate 				 * before the get_attr call. We then
5857c478bd9Sstevel@tonic-gate 				 * return from this policy successfully
5867c478bd9Sstevel@tonic-gate 				 * knowing it will be run again shortly
5877c478bd9Sstevel@tonic-gate 				 * with the right PS state.
5887c478bd9Sstevel@tonic-gate 				 */
5897c478bd9Sstevel@tonic-gate 				return (PSVC_SUCCESS);
5907c478bd9Sstevel@tonic-gate 			} else {
5917c478bd9Sstevel@tonic-gate 				/*
5927c478bd9Sstevel@tonic-gate 				 * We have failed before and so this
5937c478bd9Sstevel@tonic-gate 				 * we will consider a hardware problem
5947c478bd9Sstevel@tonic-gate 				 * and it should be reported
5957c478bd9Sstevel@tonic-gate 				 */
5967c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR,
5977c478bd9Sstevel@tonic-gate 				    "Failed getting %s State: ",
5987c478bd9Sstevel@tonic-gate 				    "ps_hotplug_status_0\n",
5997c478bd9Sstevel@tonic-gate 				    fail_valid_switch_id);
6007c478bd9Sstevel@tonic-gate 				return (status);
6017c478bd9Sstevel@tonic-gate 			}
6027c478bd9Sstevel@tonic-gate 		}
6037c478bd9Sstevel@tonic-gate 		/*
6047c478bd9Sstevel@tonic-gate 		 * Because we have successfully gotten a value from
6057c478bd9Sstevel@tonic-gate 		 * the i2c device on the PS we will set the
6067c478bd9Sstevel@tonic-gate 		 * failed_count to 0
6077c478bd9Sstevel@tonic-gate 		 */
6087c478bd9Sstevel@tonic-gate 		hotplug_failed_count = 0;
6097c478bd9Sstevel@tonic-gate 
6107c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_STATE_ATTR, state);
6117c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
6127c478bd9Sstevel@tonic-gate 			return (status);
6137c478bd9Sstevel@tonic-gate 		retry++;
6147c478bd9Sstevel@tonic-gate 		/*
6157c478bd9Sstevel@tonic-gate 		 * check to see if we need to retry. the conditions are:
6167c478bd9Sstevel@tonic-gate 		 *
6177c478bd9Sstevel@tonic-gate 		 * valid_switch_state	state			retry
6187c478bd9Sstevel@tonic-gate 		 * --------------------------------------------------
6197c478bd9Sstevel@tonic-gate 		 *	PSVC_OFF	!PSVC_HOTPLUGGED	yes
6207c478bd9Sstevel@tonic-gate 		 *	PSVC_ON		PSVC_HOTPLUGGED		yes
6217c478bd9Sstevel@tonic-gate 		 *	PSVC_OFF	PSVC_HOTPLUGGED		no
6227c478bd9Sstevel@tonic-gate 		 *	PSVC_ON		!PSVC_HOTPLUGGED	no
6237c478bd9Sstevel@tonic-gate 		 */
624298b7f4cSjfrank 	} while ((retry < n_retry_pshp_status) &&
6257c478bd9Sstevel@tonic-gate 	    change_of_state_str(valid_switch_state, PSVC_OFF,
6267c478bd9Sstevel@tonic-gate 	    state, PSVC_HOTPLUGGED));
6277c478bd9Sstevel@tonic-gate 
6287c478bd9Sstevel@tonic-gate 	if ((strcmp(valid_switch_state, PSVC_OFF) == 0) &&
6295eb92cf2Sdhain 	    (strcmp(state, PSVC_HOTPLUGGED) != 0)) {
6307c478bd9Sstevel@tonic-gate 		strcpy(state, PSVC_HOTPLUGGED);
6317c478bd9Sstevel@tonic-gate 		strcpy(fault, PSVC_NO_FAULT);
6327c478bd9Sstevel@tonic-gate 		strcpy(led_state, PSVC_LED_OFF);
6337c478bd9Sstevel@tonic-gate 		status = psvc_set_attr(hdlp, id, PSVC_STATE_ATTR,
6345eb92cf2Sdhain 		    state);
6357c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
6367c478bd9Sstevel@tonic-gate 			return (status);
6377c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, id, PSVC_ASSOC_MATCHES_ATTR,
6385eb92cf2Sdhain 		    &led_count, PSVC_DEV_FAULT_LED);
6397c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
6407c478bd9Sstevel@tonic-gate 			return (status);
6417c478bd9Sstevel@tonic-gate 
6427c478bd9Sstevel@tonic-gate 		for (j = 0; j < led_count; j++) {
6437c478bd9Sstevel@tonic-gate 
6447c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR,
6455eb92cf2Sdhain 			    &led_id, PSVC_DEV_FAULT_LED, j);
6467c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
6477c478bd9Sstevel@tonic-gate 				return (status);
6487c478bd9Sstevel@tonic-gate 
6497c478bd9Sstevel@tonic-gate 			status = psvc_set_attr(hdlp, led_id,
6505eb92cf2Sdhain 			    PSVC_LED_STATE_ATTR, led_state);
6517c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS) {
6527c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, SET_LED_FAILED_MSG, led_id,
6537c478bd9Sstevel@tonic-gate 				    errno);
6547c478bd9Sstevel@tonic-gate 				return (status);
6557c478bd9Sstevel@tonic-gate 			}
6567c478bd9Sstevel@tonic-gate 
6577c478bd9Sstevel@tonic-gate 		}
6585eb92cf2Sdhain 		strcpy(led_state, PSVC_LED_ON);
6595eb92cf2Sdhain 		status = psvc_set_attr(hdlp, "FSP_POWER_FAULT_LED",
6605eb92cf2Sdhain 		    PSVC_LED_STATE_ATTR, led_state);
6615eb92cf2Sdhain 		if (status != PSVC_SUCCESS) {
6625eb92cf2Sdhain 			syslog(LOG_ERR, SET_LED_FAILED_MSG, led_id, errno);
6635eb92cf2Sdhain 			return (status);
6645eb92cf2Sdhain 		}
6655eb92cf2Sdhain 		unplugged_id = id + 2;
6665eb92cf2Sdhain 		unplugged_ps = unplugged_ps | (1 << (int)strtol(unplugged_id,
6675eb92cf2Sdhain 		    (char **)NULL, 10));
6685eb92cf2Sdhain 		status = update_gen_fault_led(hdlp, GEN_FAULT_LED);
6697c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, PS_UNPLUGGED_MSG, id);
6707c478bd9Sstevel@tonic-gate 		return (status);
6717c478bd9Sstevel@tonic-gate 	}
6727c478bd9Sstevel@tonic-gate 
6737c478bd9Sstevel@tonic-gate 	if ((strcmp(valid_switch_state, PSVC_ON) == 0) &&
6745eb92cf2Sdhain 	    (strcmp(state, PSVC_HOTPLUGGED) == 0)) {
6757c478bd9Sstevel@tonic-gate 		strcpy(state, PSVC_OK);
6767c478bd9Sstevel@tonic-gate 		strcpy(fault, PSVC_NO_FAULT);
6777c478bd9Sstevel@tonic-gate 		status = psvc_set_attr(hdlp, id, PSVC_STATE_ATTR, state);
6787c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
6797c478bd9Sstevel@tonic-gate 			return (status);
6805eb92cf2Sdhain 		unplugged_id = id + 2;
6815eb92cf2Sdhain 		unplugged_ps = unplugged_ps ^ (1 << (int)strtol(unplugged_id,
6825eb92cf2Sdhain 		    (char **)NULL, 10));
6835eb92cf2Sdhain 		if (unplugged_ps == 0) {
6845eb92cf2Sdhain 			strcpy(led_state, PSVC_LED_OFF);
6855eb92cf2Sdhain 			status = psvc_set_attr(hdlp, "FSP_POWER_FAULT_LED",
6865eb92cf2Sdhain 			    PSVC_LED_STATE_ATTR, led_state);
6875eb92cf2Sdhain 			if (status != PSVC_SUCCESS) {
6885eb92cf2Sdhain 				syslog(LOG_ERR, SET_LED_FAILED_MSG, led_id,
6895eb92cf2Sdhain 				    errno);
6905eb92cf2Sdhain 				return (status);
6915eb92cf2Sdhain 			}
6925eb92cf2Sdhain 			status = update_gen_fault_led(hdlp, GEN_FAULT_LED);
6935eb92cf2Sdhain 		}
6947c478bd9Sstevel@tonic-gate 		syslog(LOG_ERR, PS_PLUGGED_MSG, id);
6957c478bd9Sstevel@tonic-gate 	}
6967c478bd9Sstevel@tonic-gate 
6977c478bd9Sstevel@tonic-gate 	return (status);
6987c478bd9Sstevel@tonic-gate }
6997c478bd9Sstevel@tonic-gate 
7007c478bd9Sstevel@tonic-gate int32_t
psvc_ps_overcurrent_check_policy_0(psvc_opaque_t hdlp,char * system)7017c478bd9Sstevel@tonic-gate psvc_ps_overcurrent_check_policy_0(psvc_opaque_t hdlp, char *system)
7027c478bd9Sstevel@tonic-gate {
7037c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
7047c478bd9Sstevel@tonic-gate 	boolean_t present;
7057c478bd9Sstevel@tonic-gate 	static char *sensor_id[DAKTARI_MAX_PS][DAK_MAX_PS_I_SENSORS];
706360e6f5eSmathue 	static char *power_supply_id[DAKTARI_MAX_PS] = {NULL};
7077c478bd9Sstevel@tonic-gate 	int32_t i, j;
7087c478bd9Sstevel@tonic-gate 	int32_t amps, oc_flag = 0, ps_present = 0;
7097c478bd9Sstevel@tonic-gate 	static int32_t hi_warn[DAKTARI_MAX_PS][DAK_MAX_PS_I_SENSORS];
7107c478bd9Sstevel@tonic-gate 	char state[32];
7117c478bd9Sstevel@tonic-gate 	static int8_t overcurrent_failed_check = 0;
712298b7f4cSjfrank 	static int threshold_counter = 0;
7137c478bd9Sstevel@tonic-gate 
7147c478bd9Sstevel@tonic-gate 	if (power_supply_id[0] == NULL) {
7157c478bd9Sstevel@tonic-gate 		for (i = 0; i < DAKTARI_MAX_PS; i++) {
7167c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, system,
7177c478bd9Sstevel@tonic-gate 			    PSVC_ASSOC_ID_ATTR, &(power_supply_id[i]),
7187c478bd9Sstevel@tonic-gate 			    PSVC_PS, i);
7197c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
7207c478bd9Sstevel@tonic-gate 				return (status);
7217c478bd9Sstevel@tonic-gate 			for (j = 0; j < DAK_MAX_PS_I_SENSORS; ++j) {
7227c478bd9Sstevel@tonic-gate 				status = psvc_get_attr(hdlp,
7237c478bd9Sstevel@tonic-gate 				    power_supply_id[i], PSVC_ASSOC_ID_ATTR,
7247c478bd9Sstevel@tonic-gate 				    &(sensor_id[i][j]), PSVC_PS_I_SENSOR, j);
7257c478bd9Sstevel@tonic-gate 				if (status != PSVC_SUCCESS)
7267c478bd9Sstevel@tonic-gate 					return (status);
7277c478bd9Sstevel@tonic-gate 				status = psvc_get_attr(hdlp, sensor_id[i][j],
7287c478bd9Sstevel@tonic-gate 				    PSVC_HI_WARN_ATTR, &(hi_warn[i][j]));
7297c478bd9Sstevel@tonic-gate 				if (status != PSVC_SUCCESS)
7307c478bd9Sstevel@tonic-gate 					return (status);
7317c478bd9Sstevel@tonic-gate 			}
7327c478bd9Sstevel@tonic-gate 		}
7337c478bd9Sstevel@tonic-gate 	}
7347c478bd9Sstevel@tonic-gate 
7357c478bd9Sstevel@tonic-gate 	for (i = 0; i < DAKTARI_MAX_PS; i++) {
7367c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, power_supply_id[i],
7375eb92cf2Sdhain 		    PSVC_PRESENCE_ATTR, &present);
7387c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE) {
7397c478bd9Sstevel@tonic-gate 			syslog(LOG_ERR, GET_PRESENCE_FAILED_MSG,
7405eb92cf2Sdhain 			    power_supply_id[i], errno);
7417c478bd9Sstevel@tonic-gate 			return (status);
7427c478bd9Sstevel@tonic-gate 		}
7437c478bd9Sstevel@tonic-gate 
7447c478bd9Sstevel@tonic-gate 		if (present == PSVC_ABSENT) {
7457c478bd9Sstevel@tonic-gate 			continue;
7467c478bd9Sstevel@tonic-gate 		}
7477c478bd9Sstevel@tonic-gate 
7487c478bd9Sstevel@tonic-gate 		status = psvc_check_ps_hotplug_status_0(hdlp,
7497c478bd9Sstevel@tonic-gate 		    power_supply_id[i]);
7507c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
7517c478bd9Sstevel@tonic-gate 			return (status);
7527c478bd9Sstevel@tonic-gate 
7537c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, power_supply_id[i],
7547c478bd9Sstevel@tonic-gate 		    PSVC_STATE_ATTR, state);
7557c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
7567c478bd9Sstevel@tonic-gate 			return (status);
7577c478bd9Sstevel@tonic-gate 
7587c478bd9Sstevel@tonic-gate 		if (strcmp(state, PSVC_HOTPLUGGED) == 0) {
7597c478bd9Sstevel@tonic-gate 			continue;
7607c478bd9Sstevel@tonic-gate 		} else {
7617c478bd9Sstevel@tonic-gate 			ps_present++;
7627c478bd9Sstevel@tonic-gate 		}
7637c478bd9Sstevel@tonic-gate 
7647c478bd9Sstevel@tonic-gate 		for (j = 0; j < DAK_MAX_PS_I_SENSORS; ++j) {
7657c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, sensor_id[i][j],
7665eb92cf2Sdhain 			    PSVC_SENSOR_VALUE_ATTR, &amps);
7677c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS) {
7687c478bd9Sstevel@tonic-gate 				if (overcurrent_failed_check == 0) {
7697c478bd9Sstevel@tonic-gate 					/*
7707c478bd9Sstevel@tonic-gate 					 * First time the get_attr call
7717c478bd9Sstevel@tonic-gate 					 * failed  set count so that if we
7727c478bd9Sstevel@tonic-gate 					 * fail again we will know
7737c478bd9Sstevel@tonic-gate 					 */
7747c478bd9Sstevel@tonic-gate 					overcurrent_failed_check = 1;
7757c478bd9Sstevel@tonic-gate 					/*
7767c478bd9Sstevel@tonic-gate 					 * We probably failed because the power
7777c478bd9Sstevel@tonic-gate 					 * supply was just insterted or removed
7787c478bd9Sstevel@tonic-gate 					 * before the get_attr call. We then
7797c478bd9Sstevel@tonic-gate 					 * return from this policy successfully
7807c478bd9Sstevel@tonic-gate 					 * knowing it will be run again shortly
7817c478bd9Sstevel@tonic-gate 					 * with the right PS state.
7827c478bd9Sstevel@tonic-gate 					 */
7837c478bd9Sstevel@tonic-gate 					return (PSVC_SUCCESS);
7847c478bd9Sstevel@tonic-gate 				} else {
7857c478bd9Sstevel@tonic-gate 					/*
7867c478bd9Sstevel@tonic-gate 					 * We have failed before and so this we
7877c478bd9Sstevel@tonic-gate 					 * will consider a hardware problem and
7887c478bd9Sstevel@tonic-gate 					 * it should be reported.
7897c478bd9Sstevel@tonic-gate 					 */
7907c478bd9Sstevel@tonic-gate 					syslog(LOG_ERR,
7917c478bd9Sstevel@tonic-gate 					    "Failed getting %s sensor value",
7927c478bd9Sstevel@tonic-gate 					    sensor_id[i][j]);
7937c478bd9Sstevel@tonic-gate 					return (status);
7947c478bd9Sstevel@tonic-gate 				}
7957c478bd9Sstevel@tonic-gate 			}
7967c478bd9Sstevel@tonic-gate 			/*
7977c478bd9Sstevel@tonic-gate 			 * Because we have successfully gotten a value from the
7987c478bd9Sstevel@tonic-gate 			 * i2c device on the PS we will set the failed_count
7997c478bd9Sstevel@tonic-gate 			 * to 0.
8007c478bd9Sstevel@tonic-gate 			 */
8017c478bd9Sstevel@tonic-gate 			overcurrent_failed_check = 0;
8027c478bd9Sstevel@tonic-gate 
8037c478bd9Sstevel@tonic-gate 			if (amps >= hi_warn[i][j]) {
8047c478bd9Sstevel@tonic-gate 				oc_flag = 1;
8057c478bd9Sstevel@tonic-gate 			}
8067c478bd9Sstevel@tonic-gate 		}
8077c478bd9Sstevel@tonic-gate 	}
8087c478bd9Sstevel@tonic-gate 
8097c478bd9Sstevel@tonic-gate 	if (oc_flag) {
8107c478bd9Sstevel@tonic-gate 		/*
8117c478bd9Sstevel@tonic-gate 		 * Because we observed an overcurrent
8127c478bd9Sstevel@tonic-gate 		 * condition, we increment threshold_counter.
8137c478bd9Sstevel@tonic-gate 		 * Once threshold_counter reaches the value
814298b7f4cSjfrank 		 * of n_read_overcurrent we log the event.
8157c478bd9Sstevel@tonic-gate 		 */
8167c478bd9Sstevel@tonic-gate 		threshold_counter++;
817298b7f4cSjfrank 		if (threshold_counter == n_read_overcurrent) {
8187c478bd9Sstevel@tonic-gate 			threshold_counter = 0;
8197c478bd9Sstevel@tonic-gate 			if (ps_present == 1) {
8207c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, PS_OVER_CURRENT_MSG);
8217c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, ADD_PS_MSG);
8227c478bd9Sstevel@tonic-gate 			} else {
8237c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, PS_OVER_CURRENT_MSG);
8247c478bd9Sstevel@tonic-gate 				syslog(LOG_ERR, REMOVE_LOAD_MSG);
8257c478bd9Sstevel@tonic-gate 			}
8267c478bd9Sstevel@tonic-gate 		}
8277c478bd9Sstevel@tonic-gate 	} else {
8287c478bd9Sstevel@tonic-gate 		threshold_counter = 0;
8297c478bd9Sstevel@tonic-gate 	}
8307c478bd9Sstevel@tonic-gate 
8317c478bd9Sstevel@tonic-gate 	return (PSVC_SUCCESS);
8327c478bd9Sstevel@tonic-gate }
8337c478bd9Sstevel@tonic-gate 
8348a88157cSvb int32_t
psvc_ps_undercurrent_check(psvc_opaque_t hdlp,char * id,int32_t * uc_flag)8358a88157cSvb psvc_ps_undercurrent_check(psvc_opaque_t hdlp, char *id, int32_t *uc_flag)
8368a88157cSvb {
8378a88157cSvb 	int32_t status = PSVC_SUCCESS;
8388a88157cSvb 	boolean_t present;
8398a88157cSvb 	static char *sensor_id[DAK_MAX_PS_I_SENSORS];
8408a88157cSvb 	int32_t j;
8418a88157cSvb 	int32_t amps;
8428a88157cSvb 	static int32_t lo_warn[DAK_MAX_PS_I_SENSORS];
8438a88157cSvb 	static int8_t undercurrent_failed_check = 0;
8448a88157cSvb 
8458a88157cSvb 	status = psvc_get_attr(hdlp, id, PSVC_PRESENCE_ATTR, &present);
8468a88157cSvb 	if (status == PSVC_FAILURE) {
8478a88157cSvb 		syslog(LOG_ERR, GET_PRESENCE_FAILED_MSG, id, errno);
8488a88157cSvb 		return (status);
8498a88157cSvb 	}
8508a88157cSvb 
8518a88157cSvb 	if (present == PSVC_ABSENT) {
8528a88157cSvb 		errno = ENODEV;
8538a88157cSvb 		return (PSVC_FAILURE);
8548a88157cSvb 	}
8558a88157cSvb 
8568a88157cSvb 	for (j = 0; j < DAK_MAX_PS_I_SENSORS; ++j) {
8578a88157cSvb 		status = psvc_get_attr(hdlp, id, PSVC_ASSOC_ID_ATTR,
8588a88157cSvb 		    &(sensor_id[j]), PSVC_PS_I_SENSOR, j);
8598a88157cSvb 		if (status != PSVC_SUCCESS)
8608a88157cSvb 			return (status);
8618a88157cSvb 		status = psvc_get_attr(hdlp, sensor_id[j],
8628a88157cSvb 		    PSVC_LO_WARN_ATTR, &(lo_warn[j]));
8638a88157cSvb 		if (status != PSVC_SUCCESS)
8648a88157cSvb 			return (status);
8658a88157cSvb 	}
8668a88157cSvb 
8678a88157cSvb 	*uc_flag = 0;
8688a88157cSvb 	for (j = 0; j < DAK_MAX_PS_I_SENSORS; ++j) {
8698a88157cSvb 		status = psvc_get_attr(hdlp, sensor_id[j],
8708a88157cSvb 		    PSVC_SENSOR_VALUE_ATTR, &amps);
8718a88157cSvb 		if (status != PSVC_SUCCESS) {
8728a88157cSvb 			if (undercurrent_failed_check == 0) {
8738a88157cSvb 				/*
8748a88157cSvb 				 * First time the get_attr call
8758a88157cSvb 				 * failed  set count so that if we
8768a88157cSvb 				 * fail again we will know.
8778a88157cSvb 				 */
8788a88157cSvb 				undercurrent_failed_check = 1;
8798a88157cSvb 				/*
8808a88157cSvb 				 * We probably failed because the power
8818a88157cSvb 				 * supply was just inserted or removed
8828a88157cSvb 				 * before the get_attr call. We then
8838a88157cSvb 				 * return from this policy successfully
8848a88157cSvb 				 * knowing it will be run again shortly
8858a88157cSvb 				 * with the right PS state.
8868a88157cSvb 				 */
8878a88157cSvb 				return (PSVC_SUCCESS);
8888a88157cSvb 			} else {
8898a88157cSvb 				/*
8908a88157cSvb 				 * Repeated failures are logged.
8918a88157cSvb 				 */
8928a88157cSvb 				syslog(LOG_ERR,
8938a88157cSvb 				    "Failed getting %s sensor value",
8948a88157cSvb 				    sensor_id[j]);
8958a88157cSvb 				return (status);
8968a88157cSvb 			}
8978a88157cSvb 		}
8988a88157cSvb 		/*
8998a88157cSvb 		 * Because we have successfully gotten a value from the
9008a88157cSvb 		 * i2c device on the PS we will set the failed_count
9018a88157cSvb 		 * to 0.
9028a88157cSvb 		 */
9038a88157cSvb 		undercurrent_failed_check = 0;
9048a88157cSvb 
9058a88157cSvb 		if (amps <= lo_warn[j]) {
9068a88157cSvb 			*uc_flag = 1;
9078a88157cSvb 			return (PSVC_SUCCESS);
9088a88157cSvb 		}
9098a88157cSvb 	}
9108a88157cSvb 
9118a88157cSvb 	return (PSVC_SUCCESS);
9128a88157cSvb }
9138a88157cSvb 
9147c478bd9Sstevel@tonic-gate int32_t
psvc_ps_device_fail_notifier_policy_0(psvc_opaque_t hdlp,char * system)9157c478bd9Sstevel@tonic-gate psvc_ps_device_fail_notifier_policy_0(psvc_opaque_t hdlp, char *system)
9167c478bd9Sstevel@tonic-gate {
917360e6f5eSmathue 	static char *ps_id[DAKTARI_MAX_PS] = {NULL};
9187c478bd9Sstevel@tonic-gate 	static char *sensor_id[DAKTARI_MAX_PS][DAK_MAX_FAULT_SENSORS];
9197c478bd9Sstevel@tonic-gate 	char *led_id = "FSP_POWER_FAULT_LED";
9208a88157cSvb 	int i, j, uc_flag;
9217c478bd9Sstevel@tonic-gate 	char state[32], fault[32], previous_state[32], past_state[32];
9227c478bd9Sstevel@tonic-gate 	char led_state[32];
9237c478bd9Sstevel@tonic-gate 	char bad_sensors[DAK_MAX_FAULT_SENSORS][256];
9248a88157cSvb 	static int threshold_counter[DAKTARI_MAX_PS];
9257c478bd9Sstevel@tonic-gate 	int32_t status = PSVC_SUCCESS;
9267c478bd9Sstevel@tonic-gate 	boolean_t present;
9277c478bd9Sstevel@tonic-gate 	int fail_state;
9287c478bd9Sstevel@tonic-gate 	static int8_t device_fail_failed_check = 0;
929298b7f4cSjfrank 	int retry, should_retry;
9307c478bd9Sstevel@tonic-gate 
9317c478bd9Sstevel@tonic-gate 	if (ps_id[0] == NULL) {
9327c478bd9Sstevel@tonic-gate 		for (i = 0; i < DAKTARI_MAX_PS; i++) {
9337c478bd9Sstevel@tonic-gate 			status = psvc_get_attr(hdlp, system,
9347c478bd9Sstevel@tonic-gate 			    PSVC_ASSOC_ID_ATTR, &(ps_id[i]), PSVC_PS, i);
9357c478bd9Sstevel@tonic-gate 			if (status != PSVC_SUCCESS)
9367c478bd9Sstevel@tonic-gate 				return (status);
9377c478bd9Sstevel@tonic-gate 			for (j = 0; j < DAK_MAX_FAULT_SENSORS; j++) {
9387c478bd9Sstevel@tonic-gate 				status = psvc_get_attr(hdlp, ps_id[i],
9397c478bd9Sstevel@tonic-gate 				    PSVC_ASSOC_ID_ATTR, &(sensor_id[i][j]),
9407c478bd9Sstevel@tonic-gate 				    PSVC_DEV_FAULT_SENSOR, j);
9417c478bd9Sstevel@tonic-gate 				if (status != PSVC_SUCCESS)
9427c478bd9Sstevel@tonic-gate 					return (status);
9437c478bd9Sstevel@tonic-gate 			}
9447c478bd9Sstevel@tonic-gate 		}
9457c478bd9Sstevel@tonic-gate 	}
9467c478bd9Sstevel@tonic-gate 
9477c478bd9Sstevel@tonic-gate 	for (i = 0; i < DAKTARI_MAX_PS; i++) {
9487c478bd9Sstevel@tonic-gate 		fail_state = 0;
9497c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, ps_id[i], PSVC_PRESENCE_ATTR,
9507c478bd9Sstevel@tonic-gate 		    &present);
9517c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
9527c478bd9Sstevel@tonic-gate 			return (status);
9537c478bd9Sstevel@tonic-gate 
9547c478bd9Sstevel@tonic-gate 		if (present == PSVC_ABSENT) {
9557c478bd9Sstevel@tonic-gate 			errno = ENODEV;
9567c478bd9Sstevel@tonic-gate 			return (PSVC_FAILURE);
9577c478bd9Sstevel@tonic-gate 		}
9587c478bd9Sstevel@tonic-gate 
9597c478bd9Sstevel@tonic-gate 		status = psvc_check_ps_hotplug_status_0(hdlp, ps_id[i]);
9607c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
9617c478bd9Sstevel@tonic-gate 			return (status);
9627c478bd9Sstevel@tonic-gate 
9637c478bd9Sstevel@tonic-gate 		status = psvc_get_attr(hdlp, ps_id[i], PSVC_STATE_ATTR,
9647c478bd9Sstevel@tonic-gate 		    past_state);
9657c478bd9Sstevel@tonic-gate 		if (status == PSVC_FAILURE)
9667c478bd9Sstevel@tonic-gate 			return (status);
9677c478bd9Sstevel@tonic-gate 
9687c478bd9Sstevel@tonic-gate 		if (strcmp(past_state, PSVC_HOTPLUGGED) == 0) {
9697c478bd9Sstevel@tonic-gate 			return (PICL_SUCCESS);
9707c478bd9Sstevel@tonic-gate 		}
9717c478bd9Sstevel@tonic-gate 
9727c478bd9Sstevel@tonic-gate 		retry = 0;
9737c478bd9Sstevel@tonic-gate 		do {
9747c478bd9Sstevel@tonic-gate 			if (retry)
975298b7f4cSjfrank 				(void) sleep(retry_sleep_devicefail);
9767c478bd9Sstevel@tonic-gate 			fail_state = 0;
9777c478bd9Sstevel@tonic-gate 			should_retry = 0;
9787c478bd9Sstevel@tonic-gate 			for (j = 0; j < DAK_MAX_FAULT_SENSORS; ++j) {
9797c478bd9Sstevel@tonic-gate 				status = psvc_get_attr(hdlp, sensor_id[i][j],
9807c478bd9Sstevel@tonic-gate 				    PSVC_SWITCH_STATE_ATTR, state);
9817c478bd9Sstevel@tonic-gate 				if (status != PSVC_SUCCESS) {
9827c478bd9Sstevel@tonic-gate 					if (device_fail_failed_check == 0) {
9837c478bd9Sstevel@tonic-gate 						/*
9847c478bd9Sstevel@tonic-gate 						 * First time the get_attr call
9857c478bd9Sstevel@tonic-gate 						 * failed  set count so that
9867c478bd9Sstevel@tonic-gate 						 * if we fail again we will know
9877c478bd9Sstevel@tonic-gate 						 */
9887c478bd9Sstevel@tonic-gate 						device_fail_failed_check = 1;
9897c478bd9Sstevel@tonic-gate 						/*
9907c478bd9Sstevel@tonic-gate 						 * We probably failed because
9917c478bd9Sstevel@tonic-gate 						 * the power supply was just
9927c478bd9Sstevel@tonic-gate 						 * insterted or removed before
9937c478bd9Sstevel@tonic-gate 						 * the get_attr call. We then
9947c478bd9Sstevel@tonic-gate 						 * return from this policy
9957c478bd9Sstevel@tonic-gate 						 * successfully knowing it will
9967c478bd9Sstevel@tonic-gate 						 * be run again shortly
9977c478bd9Sstevel@tonic-gate 						 * with the right PS state.
9987c478bd9Sstevel@tonic-gate 						 */
9997c478bd9Sstevel@tonic-gate 						return (PSVC_SUCCESS);
10007c478bd9Sstevel@tonic-gate 					} else {
10017c478bd9Sstevel@tonic-gate 						/*
10027c478bd9Sstevel@tonic-gate 						 * We have failed before and
10037c478bd9Sstevel@tonic-gate 						 * so this we will consider a
10047c478bd9Sstevel@tonic-gate 						 * hardware problem and
10057c478bd9Sstevel@tonic-gate 						 * it should be reported.
10067c478bd9Sstevel@tonic-gate 						 */
10077c478bd9Sstevel@tonic-gate 						syslog(LOG_ERR, "Failed in "
10087c478bd9Sstevel@tonic-gate 						    "getting sensor state for "
10097c478bd9Sstevel@tonic-gate 						    "%s\n", sensor_id[i][j]);
10107c478bd9Sstevel@tonic-gate 
10117c478bd9Sstevel@tonic-gate 						return (status);
10127c478bd9Sstevel@tonic-gate 					}
10137c478bd9Sstevel@tonic-gate 				}
10147c478bd9Sstevel@tonic-gate 
10157c478bd9Sstevel@tonic-gate 				/*
10167c478bd9Sstevel@tonic-gate 				 * Because we have successfully gotten
10177c478bd9Sstevel@tonic-gate 				 * a value from the i2c device on the
10187c478bd9Sstevel@tonic-gate 				 * PS we will set the failed_count to 0.
10197c478bd9Sstevel@tonic-gate 				 */
10207c478bd9Sstevel@tonic-gate 				device_fail_failed_check = 0;
10217c478bd9Sstevel@tonic-gate 
10227c478bd9Sstevel@tonic-gate 				/*
10237c478bd9Sstevel@tonic-gate 				 * If we find that the sensor is on we
10247c478bd9Sstevel@tonic-gate 				 * fill in the name of the sensor in
10257c478bd9Sstevel@tonic-gate 				 * the bad_sensor array. If the sensor
10267c478bd9Sstevel@tonic-gate 				 * is off we use EMPTY_STRING as a check
10277c478bd9Sstevel@tonic-gate 				 * later on as to when NOT to print out
10287c478bd9Sstevel@tonic-gate 				 * what is in bad_sensor[].
10297c478bd9Sstevel@tonic-gate 				 */
10307c478bd9Sstevel@tonic-gate 				if (strcmp(state, PSVC_SWITCH_ON) == 0) {
10317c478bd9Sstevel@tonic-gate 					fail_state++;
10327c478bd9Sstevel@tonic-gate 					strlcpy(bad_sensors[j], sensor_id[i][j],
10337c478bd9Sstevel@tonic-gate 					    sizeof (bad_sensors[j]));
10347c478bd9Sstevel@tonic-gate 				} else {
10357c478bd9Sstevel@tonic-gate 					strcpy(bad_sensors[j], EMPTY_STRING);
10367c478bd9Sstevel@tonic-gate 				}
10377c478bd9Sstevel@tonic-gate 			}
10387c478bd9Sstevel@tonic-gate 			retry++;
10397c478bd9Sstevel@tonic-gate 			/*
10407c478bd9Sstevel@tonic-gate 			 * check to see if we need to retry. the conditions are:
10417c478bd9Sstevel@tonic-gate 			 *
10427c478bd9Sstevel@tonic-gate 			 * fail_state		past_state		retry
10437c478bd9Sstevel@tonic-gate 			 * --------------------------------------------------
10447c478bd9Sstevel@tonic-gate 			 *	+		PSVC_OK			yes
10457c478bd9Sstevel@tonic-gate 			 *	0		PSVC_ERROR		yes
10467c478bd9Sstevel@tonic-gate 			 *	+		PSVC_ERROR		no
10477c478bd9Sstevel@tonic-gate 			 *	0		PSVC_OK			no
1048