1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <limits.h>
32*0Sstevel@tonic-gate #include <sys/systeminfo.h>
33*0Sstevel@tonic-gate #include <pthread.h>
34*0Sstevel@tonic-gate #include <syslog.h>
35*0Sstevel@tonic-gate #include <picl.h>
36*0Sstevel@tonic-gate #include <picltree.h>
37*0Sstevel@tonic-gate #include <picldefs.h>
38*0Sstevel@tonic-gate #include <string.h>
39*0Sstevel@tonic-gate #include <strings.h>
40*0Sstevel@tonic-gate #include <sys/param.h>
41*0Sstevel@tonic-gate #include <sys/types.h>
42*0Sstevel@tonic-gate #include <sys/stat.h>
43*0Sstevel@tonic-gate #include <fcntl.h>
44*0Sstevel@tonic-gate #include <unistd.h>
45*0Sstevel@tonic-gate #include <stropts.h>
46*0Sstevel@tonic-gate #include <assert.h>
47*0Sstevel@tonic-gate #include <libnvpair.h>
48*0Sstevel@tonic-gate #include <libintl.h>
49*0Sstevel@tonic-gate #include <poll.h>
50*0Sstevel@tonic-gate #include <smclib.h>
51*0Sstevel@tonic-gate #include "piclenvmond.h"
52*0Sstevel@tonic-gate #include "picldr.h"
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate /* external functions */
55*0Sstevel@tonic-gate extern picl_errno_t env_platmod_init();
56*0Sstevel@tonic-gate extern void env_platmod_handle_event(const char *, const void *, size_t);
57*0Sstevel@tonic-gate extern picl_errno_t env_platmod_create_sensors();
58*0Sstevel@tonic-gate extern picl_errno_t env_platmod_setup_hotswap();
59*0Sstevel@tonic-gate extern picl_errno_t env_platmod_sp_monitor();
60*0Sstevel@tonic-gate extern picl_errno_t env_platmod_handle_bus_if_change(uint8_t);
61*0Sstevel@tonic-gate extern picl_errno_t env_platmod_handle_latch_open();
62*0Sstevel@tonic-gate extern void env_platmod_handle_sensor_event(void *);
63*0Sstevel@tonic-gate extern int process_platmod_sp_state_change_notif(void *);
64*0Sstevel@tonic-gate extern int process_platmod_change_cpu_node_state(void *);
65*0Sstevel@tonic-gate extern int process_platmod_change_cpci_state(void *);
66*0Sstevel@tonic-gate extern int process_platmod_async_msg_notif(void *);
67*0Sstevel@tonic-gate extern void process_platmod_sp_heartbeat(uint8_t);
68*0Sstevel@tonic-gate extern picl_errno_t env_platmod_create_hotswap_prop();
69*0Sstevel@tonic-gate extern picl_errno_t env_create_property(int ptype, int pmode,
70*0Sstevel@tonic-gate 	size_t psize, char *pname, int (*readfn)(ptree_rarg_t *, void *),
71*0Sstevel@tonic-gate 	int (*writefn)(ptree_warg_t *, const void *),
72*0Sstevel@tonic-gate 	picl_nodehdl_t nodeh, picl_prophdl_t *propp, void *vbuf);
73*0Sstevel@tonic-gate extern char *strtok_r(char *s1, const char *s2, char **lasts);
74*0Sstevel@tonic-gate 
75*0Sstevel@tonic-gate /* external variables */
76*0Sstevel@tonic-gate extern int env_debug;
77*0Sstevel@tonic-gate 
78*0Sstevel@tonic-gate static char sys_name[SYS_NMLN];
79*0Sstevel@tonic-gate static char chassisconf_name[SYS_NMLN];
80*0Sstevel@tonic-gate static boolean_t parse_config_file = B_FALSE;
81*0Sstevel@tonic-gate static int8_t alarm_check_interval = -1;
82*0Sstevel@tonic-gate static picl_nodehdl_t frutreeh = 0;
83*0Sstevel@tonic-gate static pthread_t polling_threadID;
84*0Sstevel@tonic-gate static boolean_t create_polling_thr = B_TRUE;
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate /* globals */
87*0Sstevel@tonic-gate uint8_t cpu_geo_addr = 0;
88*0Sstevel@tonic-gate picl_nodehdl_t rooth = 0, chassis_nodehdl = 0, cpu_nodehdl = 0;
89*0Sstevel@tonic-gate picl_nodehdl_t platformh = 0, sysmgmth = 0, cpu_lnodehdl = 0;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate /*
92*0Sstevel@tonic-gate  * envmond policy structure
93*0Sstevel@tonic-gate  */
94*0Sstevel@tonic-gate typedef struct _policy {
95*0Sstevel@tonic-gate 	uint8_t		interval;
96*0Sstevel@tonic-gate 	char		*pname;
97*0Sstevel@tonic-gate 	char		*argp;
98*0Sstevel@tonic-gate 	struct _policy	*nextp;
99*0Sstevel@tonic-gate } env_policy_t;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate /*
102*0Sstevel@tonic-gate  * read_policy_configuration - extract info. from the envmond.conf
103*0Sstevel@tonic-gate  */
104*0Sstevel@tonic-gate static int
env_read_policy_configuration(char * conffile,env_policy_t ** policypp)105*0Sstevel@tonic-gate env_read_policy_configuration(char *conffile, env_policy_t **policypp)
106*0Sstevel@tonic-gate {
107*0Sstevel@tonic-gate 	FILE		*fp;
108*0Sstevel@tonic-gate 	char		buf[RECORD_MAXSIZE];
109*0Sstevel@tonic-gate 	char		*token, *lasts;
110*0Sstevel@tonic-gate 	env_policy_t	*policyp;
111*0Sstevel@tonic-gate 
112*0Sstevel@tonic-gate 	if ((fp = fopen(conffile, "r")) == NULL) {
113*0Sstevel@tonic-gate 		return (-1);
114*0Sstevel@tonic-gate 	}
115*0Sstevel@tonic-gate 	while (fgets(buf, sizeof (buf), fp) != NULL) {
116*0Sstevel@tonic-gate 		if (buf[0] && (buf[0] == '#' || buf[0] == '\n')) {
117*0Sstevel@tonic-gate 			continue;
118*0Sstevel@tonic-gate 		}
119*0Sstevel@tonic-gate 		token = (char *)strtok_r(buf, RECORD_WHITESPACE, &lasts);
120*0Sstevel@tonic-gate 		if (token == NULL) {
121*0Sstevel@tonic-gate 			continue;
122*0Sstevel@tonic-gate 		}
123*0Sstevel@tonic-gate 		policyp = (env_policy_t *)malloc(sizeof (env_policy_t));
124*0Sstevel@tonic-gate 		if (policyp == NULL) {
125*0Sstevel@tonic-gate 			goto errors;
126*0Sstevel@tonic-gate 		}
127*0Sstevel@tonic-gate 		policyp->interval = (uint8_t)strtoul(token, NULL, 0);
128*0Sstevel@tonic-gate 		token = (char *)strtok_r(lasts, RECORD_WHITESPACE, &lasts);
129*0Sstevel@tonic-gate 		if (token == NULL) {
130*0Sstevel@tonic-gate 			free(policyp);
131*0Sstevel@tonic-gate 		} else {
132*0Sstevel@tonic-gate 			policyp->pname = strdup(token);
133*0Sstevel@tonic-gate 			if (NULL == policyp->pname) {
134*0Sstevel@tonic-gate 				goto errors;
135*0Sstevel@tonic-gate 			}
136*0Sstevel@tonic-gate 		}
137*0Sstevel@tonic-gate 		if (lasts) {
138*0Sstevel@tonic-gate 			policyp->argp = strdup(lasts);
139*0Sstevel@tonic-gate 			if (policyp->argp == NULL) {
140*0Sstevel@tonic-gate 				goto errors;
141*0Sstevel@tonic-gate 			}
142*0Sstevel@tonic-gate 		} else {
143*0Sstevel@tonic-gate 			policyp->argp = NULL;
144*0Sstevel@tonic-gate 		}
145*0Sstevel@tonic-gate 		policyp->nextp = *policypp;
146*0Sstevel@tonic-gate 		*policypp = policyp;
147*0Sstevel@tonic-gate 	}
148*0Sstevel@tonic-gate 	(void) fclose(fp);
149*0Sstevel@tonic-gate 	return (0);
150*0Sstevel@tonic-gate 
151*0Sstevel@tonic-gate errors:
152*0Sstevel@tonic-gate 	(void) fclose(fp);
153*0Sstevel@tonic-gate 	while (*policypp) {
154*0Sstevel@tonic-gate 		policyp = *policypp;
155*0Sstevel@tonic-gate 		*policypp = (*policypp)->nextp;
156*0Sstevel@tonic-gate 		free(policyp->pname);
157*0Sstevel@tonic-gate 		free(policyp->argp);
158*0Sstevel@tonic-gate 		free(policyp);
159*0Sstevel@tonic-gate 	}
160*0Sstevel@tonic-gate 	return (-1);
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate 
163*0Sstevel@tonic-gate /*
164*0Sstevel@tonic-gate  * supports environmental policies
165*0Sstevel@tonic-gate  */
166*0Sstevel@tonic-gate static void
env_parse_config_file()167*0Sstevel@tonic-gate env_parse_config_file()
168*0Sstevel@tonic-gate {
169*0Sstevel@tonic-gate 	char		conffile[MAXPATHLEN];
170*0Sstevel@tonic-gate 	env_policy_t	*policyp, *tmp;
171*0Sstevel@tonic-gate 	struct stat	st;
172*0Sstevel@tonic-gate 
173*0Sstevel@tonic-gate 	if (parse_config_file == B_FALSE) {
174*0Sstevel@tonic-gate 		return;
175*0Sstevel@tonic-gate 	}
176*0Sstevel@tonic-gate 	(void) snprintf(conffile, sizeof (conffile), ENV_CONFIG_FILE,
177*0Sstevel@tonic-gate 		sys_name);
178*0Sstevel@tonic-gate 	bzero(&st, sizeof (st));
179*0Sstevel@tonic-gate 	if (stat(conffile, &st) == -1) {
180*0Sstevel@tonic-gate 		return;
181*0Sstevel@tonic-gate 	}
182*0Sstevel@tonic-gate 
183*0Sstevel@tonic-gate 	policyp = NULL;
184*0Sstevel@tonic-gate 	if (env_read_policy_configuration(conffile, &policyp) == -1) {
185*0Sstevel@tonic-gate 		return;
186*0Sstevel@tonic-gate 	}
187*0Sstevel@tonic-gate 	assert(policyp);
188*0Sstevel@tonic-gate 
189*0Sstevel@tonic-gate 	while (policyp) {
190*0Sstevel@tonic-gate 		tmp = policyp;
191*0Sstevel@tonic-gate 		policyp = policyp->nextp;
192*0Sstevel@tonic-gate 		if (strcmp(tmp->pname, SERVICE_PROCESSOR) == 0) {
193*0Sstevel@tonic-gate 			alarm_check_interval = tmp->interval;
194*0Sstevel@tonic-gate 			if (env_debug & DEBUG)
195*0Sstevel@tonic-gate 				syslog(LOG_INFO, "Alarm Heartbeat frequency: "
196*0Sstevel@tonic-gate 					"%d seconds", alarm_check_interval);
197*0Sstevel@tonic-gate 		}
198*0Sstevel@tonic-gate 		free(tmp->pname);
199*0Sstevel@tonic-gate 		free(tmp->argp);
200*0Sstevel@tonic-gate 		free(tmp);
201*0Sstevel@tonic-gate 	}
202*0Sstevel@tonic-gate }
203*0Sstevel@tonic-gate 
204*0Sstevel@tonic-gate /*
205*0Sstevel@tonic-gate  * detects the presence of RTM for CPU board
206*0Sstevel@tonic-gate  */
207*0Sstevel@tonic-gate static boolean_t
is_rtm_present()208*0Sstevel@tonic-gate is_rtm_present()
209*0Sstevel@tonic-gate {
210*0Sstevel@tonic-gate 	sc_reqmsg_t	req_pkt;
211*0Sstevel@tonic-gate 	sc_rspmsg_t	rsp_pkt;
212*0Sstevel@tonic-gate 	uint8_t		size = 0;
213*0Sstevel@tonic-gate 
214*0Sstevel@tonic-gate 	req_pkt.data[0] = ENV_RTM_BUS_ID;
215*0Sstevel@tonic-gate 	req_pkt.data[1] = ENV_RTM_SLAVE_ADDR;
216*0Sstevel@tonic-gate 	req_pkt.data[2] = ENV_RTM_READ_SIZE;
217*0Sstevel@tonic-gate 	size = ENV_RTM_PKT_LEN;
218*0Sstevel@tonic-gate 
219*0Sstevel@tonic-gate 	/* initialize the request packet */
220*0Sstevel@tonic-gate 	(void) smc_init_smc_msg(&req_pkt, SMC_MASTER_RW_CMD,
221*0Sstevel@tonic-gate 		DEFAULT_SEQN, size);
222*0Sstevel@tonic-gate 
223*0Sstevel@tonic-gate 	/* make a call to smc library to send cmd */
224*0Sstevel@tonic-gate 	if (smc_send_msg(DEFAULT_FD, &req_pkt, &rsp_pkt,
225*0Sstevel@tonic-gate 		POLL_TIMEOUT) != SMC_SUCCESS) {
226*0Sstevel@tonic-gate 		return (B_FALSE);
227*0Sstevel@tonic-gate 	}
228*0Sstevel@tonic-gate 	return (B_TRUE);
229*0Sstevel@tonic-gate }
230*0Sstevel@tonic-gate 
231*0Sstevel@tonic-gate /*
232*0Sstevel@tonic-gate  * this routine does the following:
233*0Sstevel@tonic-gate  * 1. initializes the CPU geo-addr
234*0Sstevel@tonic-gate  * 2. gets the system name
235*0Sstevel@tonic-gate  * 3. create the chassis type property
236*0Sstevel@tonic-gate  * 4. creates the conf_file property
237*0Sstevel@tonic-gate  */
238*0Sstevel@tonic-gate static picl_errno_t
env_set_cpu_info()239*0Sstevel@tonic-gate env_set_cpu_info()
240*0Sstevel@tonic-gate {
241*0Sstevel@tonic-gate 	int rc = 0;
242*0Sstevel@tonic-gate 	sc_reqmsg_t	req_pkt;
243*0Sstevel@tonic-gate 	sc_rspmsg_t	rsp_pkt;
244*0Sstevel@tonic-gate 	uint8_t		size = 0;
245*0Sstevel@tonic-gate 	char 		conf_name[PICL_PROPNAMELEN_MAX];
246*0Sstevel@tonic-gate 
247*0Sstevel@tonic-gate 	/* get the geo_addr */
248*0Sstevel@tonic-gate 	/* initialize the request packet */
249*0Sstevel@tonic-gate 	(void) smc_init_smc_msg(&req_pkt, SMC_GET_GEOGRAPHICAL_ADDRESS,
250*0Sstevel@tonic-gate 		DEFAULT_SEQN, size);
251*0Sstevel@tonic-gate 
252*0Sstevel@tonic-gate 	/* make a call to smc library to send cmd */
253*0Sstevel@tonic-gate 	if (smc_send_msg(DEFAULT_FD, &req_pkt, &rsp_pkt,
254*0Sstevel@tonic-gate 		POLL_TIMEOUT) != SMC_SUCCESS) {
255*0Sstevel@tonic-gate 		return (PICL_FAILURE);
256*0Sstevel@tonic-gate 	}
257*0Sstevel@tonic-gate 	cpu_geo_addr = rsp_pkt.data[0];
258*0Sstevel@tonic-gate 
259*0Sstevel@tonic-gate 	/* get the system name */
260*0Sstevel@tonic-gate 	if (sysinfo(SI_PLATFORM, sys_name, sizeof (sys_name)) == -1) {
261*0Sstevel@tonic-gate 		return (PICL_FAILURE);
262*0Sstevel@tonic-gate 	}
263*0Sstevel@tonic-gate 	(void) strncpy(chassisconf_name, sys_name,
264*0Sstevel@tonic-gate 		sizeof (chassisconf_name));
265*0Sstevel@tonic-gate 
266*0Sstevel@tonic-gate 	/* initialize the node handles */
267*0Sstevel@tonic-gate 	if ((rc = ptree_get_root(&rooth)) != PICL_SUCCESS) {
268*0Sstevel@tonic-gate 		return (rc);
269*0Sstevel@tonic-gate 	}
270*0Sstevel@tonic-gate 
271*0Sstevel@tonic-gate 	if ((rc = ptree_get_node_by_path(FRUTREE_PATH, &frutreeh)) !=
272*0Sstevel@tonic-gate 		PICL_SUCCESS) {
273*0Sstevel@tonic-gate 		return (rc);
274*0Sstevel@tonic-gate 	}
275*0Sstevel@tonic-gate 
276*0Sstevel@tonic-gate 	if ((rc = ptree_get_node_by_path(PICL_FRUTREE_CHASSIS,
277*0Sstevel@tonic-gate 		&chassis_nodehdl)) != PICL_SUCCESS) {
278*0Sstevel@tonic-gate 		return (rc);
279*0Sstevel@tonic-gate 	}
280*0Sstevel@tonic-gate 
281*0Sstevel@tonic-gate 	/* create the chassis type property */
282*0Sstevel@tonic-gate 	if ((rc = env_create_property(PICL_PTYPE_CHARSTRING,
283*0Sstevel@tonic-gate 		PICL_READ, PICL_PROPNAMELEN_MAX, PICL_PROP_CHASSIS_TYPE,
284*0Sstevel@tonic-gate 		NULLREAD, NULLWRITE, chassis_nodehdl, (picl_prophdl_t *)NULL,
285*0Sstevel@tonic-gate 		chassisconf_name)) != PICL_SUCCESS) {
286*0Sstevel@tonic-gate 		return (rc);
287*0Sstevel@tonic-gate 	}
288*0Sstevel@tonic-gate 
289*0Sstevel@tonic-gate 	/*
290*0Sstevel@tonic-gate 	 * create dummy prop to inform frutree plugin abt conf file
291*0Sstevel@tonic-gate 	 * (rtm based or w/o rtm)
292*0Sstevel@tonic-gate 	 * frutree plugin removes this prop after reading the value
293*0Sstevel@tonic-gate 	 */
294*0Sstevel@tonic-gate 	if (is_rtm_present() == B_TRUE) {
295*0Sstevel@tonic-gate 		(void) snprintf(conf_name, sizeof (conf_name),
296*0Sstevel@tonic-gate 			"%s.RTM.conf", chassisconf_name);
297*0Sstevel@tonic-gate 	} else {
298*0Sstevel@tonic-gate 		(void) snprintf(conf_name, sizeof (conf_name),
299*0Sstevel@tonic-gate 			"%s.conf", chassisconf_name);
300*0Sstevel@tonic-gate 	}
301*0Sstevel@tonic-gate 
302*0Sstevel@tonic-gate 	if ((rc = env_create_property(PICL_PTYPE_CHARSTRING,
303*0Sstevel@tonic-gate 		PICL_READ, PICL_PROPNAMELEN_MAX, PICL_PROP_CONF_FILE, NULLREAD,
304*0Sstevel@tonic-gate 		NULLWRITE, chassis_nodehdl, (picl_prophdl_t *)NULL,
305*0Sstevel@tonic-gate 		conf_name)) != PICL_SUCCESS) {
306*0Sstevel@tonic-gate 		return (rc);
307*0Sstevel@tonic-gate 	}
308*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
309*0Sstevel@tonic-gate }
310*0Sstevel@tonic-gate 
311*0Sstevel@tonic-gate /*
312*0Sstevel@tonic-gate  * initialization
313*0Sstevel@tonic-gate  */
314*0Sstevel@tonic-gate picl_errno_t
env_init()315*0Sstevel@tonic-gate env_init()
316*0Sstevel@tonic-gate {
317*0Sstevel@tonic-gate 	picl_errno_t rc = PICL_SUCCESS;
318*0Sstevel@tonic-gate 
319*0Sstevel@tonic-gate 	if ((rc = env_set_cpu_info()) != PICL_SUCCESS) {
320*0Sstevel@tonic-gate 		return (rc);
321*0Sstevel@tonic-gate 	}
322*0Sstevel@tonic-gate 
323*0Sstevel@tonic-gate 	/* parse the configuration file */
324*0Sstevel@tonic-gate 	env_parse_config_file();
325*0Sstevel@tonic-gate 
326*0Sstevel@tonic-gate 	/*
327*0Sstevel@tonic-gate 	 * do any platform specific intialization if required
328*0Sstevel@tonic-gate 	 * IMPORTANT: must post dr_incoming resource event on
329*0Sstevel@tonic-gate 	 * chassis after doing all the reqd checks
330*0Sstevel@tonic-gate 	 */
331*0Sstevel@tonic-gate 	rc = env_platmod_init();
332*0Sstevel@tonic-gate 	return (rc);
333*0Sstevel@tonic-gate }
334*0Sstevel@tonic-gate 
335*0Sstevel@tonic-gate /*
336*0Sstevel@tonic-gate  * sets smc global enables
337*0Sstevel@tonic-gate  */
338*0Sstevel@tonic-gate static int
env_set_smc_global_enables(boolean_t ipmi_enable)339*0Sstevel@tonic-gate env_set_smc_global_enables(boolean_t ipmi_enable)
340*0Sstevel@tonic-gate {
341*0Sstevel@tonic-gate 	sc_reqmsg_t	req_pkt;
342*0Sstevel@tonic-gate 	sc_rspmsg_t	rsp_pkt;
343*0Sstevel@tonic-gate 	uint8_t		size = 0;
344*0Sstevel@tonic-gate 
345*0Sstevel@tonic-gate 	/* initialize the request packet */
346*0Sstevel@tonic-gate 	(void) smc_init_smc_msg(&req_pkt, SMC_GET_GLOBAL_ENABLES,
347*0Sstevel@tonic-gate 		DEFAULT_SEQN, size);
348*0Sstevel@tonic-gate 
349*0Sstevel@tonic-gate 	/* make a call to smc library to send cmd */
350*0Sstevel@tonic-gate 	if (smc_send_msg(DEFAULT_FD, &req_pkt, &rsp_pkt,
351*0Sstevel@tonic-gate 		POLL_TIMEOUT) != SMC_SUCCESS) {
352*0Sstevel@tonic-gate 		return (-1);
353*0Sstevel@tonic-gate 	}
354*0Sstevel@tonic-gate 
355*0Sstevel@tonic-gate 	req_pkt.data[0] = rsp_pkt.data[0];
356*0Sstevel@tonic-gate 	req_pkt.data[1] = rsp_pkt.data[1];
357*0Sstevel@tonic-gate 	if (ipmi_enable) {
358*0Sstevel@tonic-gate 		req_pkt.data[1] |= ENV_IPMI_ENABLE_MASK;
359*0Sstevel@tonic-gate 		req_pkt.data[1] &= ENV_SENSOR_ENABLE_MASK;
360*0Sstevel@tonic-gate 	} else {
361*0Sstevel@tonic-gate 		req_pkt.data[1] &= ENV_IPMI_DISABLE_MASK;
362*0Sstevel@tonic-gate 		req_pkt.data[1] |= ENV_SENSOR_DISABLE_MASK;
363*0Sstevel@tonic-gate 	}
364*0Sstevel@tonic-gate 	size = ENV_SET_GLOBAL_PKT_LEN;
365*0Sstevel@tonic-gate 	(void) smc_init_smc_msg(&req_pkt, SMC_SET_GLOBAL_ENABLES,
366*0Sstevel@tonic-gate 		DEFAULT_SEQN, size);
367*0Sstevel@tonic-gate 
368*0Sstevel@tonic-gate 	/* make a call to smc library to send cmd */
369*0Sstevel@tonic-gate 	if (smc_send_msg(DEFAULT_FD, &req_pkt, &rsp_pkt,
370*0Sstevel@tonic-gate 		POLL_TIMEOUT) != SMC_SUCCESS) {
371*0Sstevel@tonic-gate 		return (-1);
372*0Sstevel@tonic-gate 	}
373*0Sstevel@tonic-gate 	return (0);
374*0Sstevel@tonic-gate }
375*0Sstevel@tonic-gate 
376*0Sstevel@tonic-gate /*
377*0Sstevel@tonic-gate  * wrapper smc drv open
378*0Sstevel@tonic-gate  */
379*0Sstevel@tonic-gate int
env_open_smc(void)380*0Sstevel@tonic-gate env_open_smc(void)
381*0Sstevel@tonic-gate {
382*0Sstevel@tonic-gate 	int	fd;
383*0Sstevel@tonic-gate 	if ((fd = open(SMC_NODE, O_RDWR)) < 0) {
384*0Sstevel@tonic-gate 		return (-1);
385*0Sstevel@tonic-gate 	}
386*0Sstevel@tonic-gate 	return (fd);
387*0Sstevel@tonic-gate }
388*0Sstevel@tonic-gate 
389*0Sstevel@tonic-gate static picl_smc_event_t
env_handle_smc_local_event(void * res_datap)390*0Sstevel@tonic-gate env_handle_smc_local_event(void *res_datap)
391*0Sstevel@tonic-gate {
392*0Sstevel@tonic-gate 	picl_errno_t rc = PICL_SUCCESS;
393*0Sstevel@tonic-gate 	uint8_t event = SMC_LOCAL_EVENT;
394*0Sstevel@tonic-gate 	uint8_t event_data = BYTE_0(res_datap);
395*0Sstevel@tonic-gate 
396*0Sstevel@tonic-gate 	if (env_debug & EVENTS)
397*0Sstevel@tonic-gate 		syslog(LOG_INFO, "Local Event Received, data %x\n", event_data);
398*0Sstevel@tonic-gate 
399*0Sstevel@tonic-gate 	switch (event_data) {
400*0Sstevel@tonic-gate 		case SMC_LOCAL_EVENT_BRIDGE_IN_RESET :	/*FALLTHRU*/
401*0Sstevel@tonic-gate 		case SMC_LOCAL_EVENT_BRIDGE_OUT_OF_RESET :
402*0Sstevel@tonic-gate 			if ((rc = env_platmod_handle_bus_if_change(
403*0Sstevel@tonic-gate 				event_data)) != PICL_SUCCESS) {
404*0Sstevel@tonic-gate 				syslog(LOG_ERR, gettext("SUNW_envmond:Error"
405*0Sstevel@tonic-gate 					" in handling bus interface change "
406*0Sstevel@tonic-gate 					"event, error = %d"), rc);
407*0Sstevel@tonic-gate 			}
408*0Sstevel@tonic-gate 			break;
409*0Sstevel@tonic-gate 		case SMC_LOCAL_EVENT_LATCH_OPENED:
410*0Sstevel@tonic-gate 			syslog(LOG_INFO, gettext("LATCH OPEN DETECTED"));
411*0Sstevel@tonic-gate 			if ((rc = env_platmod_handle_latch_open()) !=
412*0Sstevel@tonic-gate 				PICL_SUCCESS) {
413*0Sstevel@tonic-gate 				syslog(LOG_ERR, gettext("SUNW_envmond:Error"
414*0Sstevel@tonic-gate 					" in handling latch open event, "
415*0Sstevel@tonic-gate 					"error = %d"), rc);
416*0Sstevel@tonic-gate 			}
417*0Sstevel@tonic-gate 			break;
418*0Sstevel@tonic-gate 		default:
419*0Sstevel@tonic-gate 			break;
420*0Sstevel@tonic-gate 	}
421*0Sstevel@tonic-gate 	return (event);
422*0Sstevel@tonic-gate }
423*0Sstevel@tonic-gate 
424*0Sstevel@tonic-gate static void
env_handle_async_msg_event(void * res_datap)425*0Sstevel@tonic-gate env_handle_async_msg_event(void *res_datap)
426*0Sstevel@tonic-gate {
427*0Sstevel@tonic-gate 	int rc = SMC_SUCCESS;
428*0Sstevel@tonic-gate 	uint8_t event = BYTE_6(res_datap);
429*0Sstevel@tonic-gate 
430*0Sstevel@tonic-gate 	if (env_debug & EVENTS)
431*0Sstevel@tonic-gate 		syslog(LOG_INFO, "Asynchronous Event %x Received, data %x\n",
432*0Sstevel@tonic-gate 			event, BYTE_7(res_datap));
433*0Sstevel@tonic-gate 	switch (event) {
434*0Sstevel@tonic-gate 	/*
435*0Sstevel@tonic-gate 	 * This message comes to CPU when the service processor is going offline
436*0Sstevel@tonic-gate 	 * or online.
437*0Sstevel@tonic-gate 	 */
438*0Sstevel@tonic-gate 	case EVENT_MSG_AC_STATE_CHANGE:
439*0Sstevel@tonic-gate 		if ((rc = process_platmod_sp_state_change_notif(res_datap)) !=
440*0Sstevel@tonic-gate 			SMC_SUCCESS) {
441*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("SUNW_envmond:Error in handling"
442*0Sstevel@tonic-gate 				"service processor change of state event, "
443*0Sstevel@tonic-gate 				"error = %d"), rc);
444*0Sstevel@tonic-gate 		}
445*0Sstevel@tonic-gate 		break;
446*0Sstevel@tonic-gate 	/*
447*0Sstevel@tonic-gate 	 * This message comes to CPU when service processor
448*0Sstevel@tonic-gate 	 * requests the CPU to go online or offline (shutdown).
449*0Sstevel@tonic-gate 	 */
450*0Sstevel@tonic-gate 	case EVENT_MSG_CHANGE_CPU_NODE_STATE:
451*0Sstevel@tonic-gate 		if ((rc = process_platmod_change_cpu_node_state(res_datap)) !=
452*0Sstevel@tonic-gate 			SMC_SUCCESS) {
453*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("SUNW_envmond:Error in handling"
454*0Sstevel@tonic-gate 				"cpu change of state event, error = %d"), rc);
455*0Sstevel@tonic-gate 		}
456*0Sstevel@tonic-gate 		break;
457*0Sstevel@tonic-gate 	/*
458*0Sstevel@tonic-gate 	 * This message comes to CPU(Satellite) when the
459*0Sstevel@tonic-gate 	 * other node (Host) is going online or offline.
460*0Sstevel@tonic-gate 	 */
461*0Sstevel@tonic-gate 	case EVENT_MSG_CHANGE_CPCI_STATE:
462*0Sstevel@tonic-gate 		if ((rc = process_platmod_change_cpci_state(res_datap)) !=
463*0Sstevel@tonic-gate 			SMC_SUCCESS) {
464*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("SUNW_envmond:Error in handling"
465*0Sstevel@tonic-gate 				"cpci change state event, error = %d"), rc);
466*0Sstevel@tonic-gate 		}
467*0Sstevel@tonic-gate 		break;
468*0Sstevel@tonic-gate 	/*
469*0Sstevel@tonic-gate 	 * This message comes from service processor to inform
470*0Sstevel@tonic-gate 	 * change in states for other nodes
471*0Sstevel@tonic-gate 	 */
472*0Sstevel@tonic-gate 	case EVENT_MSG_ASYNC_EVENT_NOTIFICATION:
473*0Sstevel@tonic-gate 		if ((rc = process_platmod_async_msg_notif(res_datap)) !=
474*0Sstevel@tonic-gate 			SMC_SUCCESS) {
475*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("SUNW_envmond:Error in handling"
476*0Sstevel@tonic-gate 				"async event notification, error = %d"), rc);
477*0Sstevel@tonic-gate 		}
478*0Sstevel@tonic-gate 		break;
479*0Sstevel@tonic-gate 	case MSG_GET_CPU_NODE_STATE:
480*0Sstevel@tonic-gate 		/* respond to the service processor heartbeat */
481*0Sstevel@tonic-gate 		process_platmod_sp_heartbeat(BYTE_5(res_datap));
482*0Sstevel@tonic-gate 		break;
483*0Sstevel@tonic-gate 	default:
484*0Sstevel@tonic-gate 		event = NO_EVENT;
485*0Sstevel@tonic-gate 		break;
486*0Sstevel@tonic-gate 	}
487*0Sstevel@tonic-gate }
488*0Sstevel@tonic-gate 
489*0Sstevel@tonic-gate /*ARGSUSED*/
490*0Sstevel@tonic-gate static picl_smc_event_t
env_process_smc_event(int fd,void ** datapp)491*0Sstevel@tonic-gate env_process_smc_event(int fd, void **datapp)
492*0Sstevel@tonic-gate {
493*0Sstevel@tonic-gate 	sc_rspmsg_t		rsp_msg;
494*0Sstevel@tonic-gate 	picl_smc_event_t	event;
495*0Sstevel@tonic-gate 	void			*res_datap = NULL;
496*0Sstevel@tonic-gate 
497*0Sstevel@tonic-gate 	if (read(fd, (char *)&rsp_msg, SC_MSG_MAX_SIZE) < 0) {
498*0Sstevel@tonic-gate 		return (NO_EVENT);
499*0Sstevel@tonic-gate 	}
500*0Sstevel@tonic-gate 
501*0Sstevel@tonic-gate 	if (SC_MSG_CC(&rsp_msg) != 0) {
502*0Sstevel@tonic-gate 		return (NO_EVENT);
503*0Sstevel@tonic-gate 	}
504*0Sstevel@tonic-gate 
505*0Sstevel@tonic-gate 	res_datap = SC_MSG_DATA(&rsp_msg);
506*0Sstevel@tonic-gate 	if (env_debug & EVENTS)
507*0Sstevel@tonic-gate 		syslog(LOG_INFO, "Async Msg Cmd,data0,2 = %x,%x,%x\n",
508*0Sstevel@tonic-gate 			SC_MSG_CMD(&rsp_msg), BYTE_0(res_datap),
509*0Sstevel@tonic-gate 			BYTE_2(res_datap));
510*0Sstevel@tonic-gate 
511*0Sstevel@tonic-gate 	if (SC_MSG_CMD(&rsp_msg) == SMC_SMC_LOCAL_EVENT_NOTIF) {
512*0Sstevel@tonic-gate 		event = env_handle_smc_local_event(res_datap);
513*0Sstevel@tonic-gate 	} else {	/* it must be an IPMI event */
514*0Sstevel@tonic-gate 		switch (BYTE_2(res_datap)) {
515*0Sstevel@tonic-gate 		case 0x3:
516*0Sstevel@tonic-gate 		case 0x4:
517*0Sstevel@tonic-gate 			if (env_debug & DEBUG)
518*0Sstevel@tonic-gate 				syslog(LOG_INFO, gettext("SUNW_envmond: "
519*0Sstevel@tonic-gate 					" Sensor Event Received\n"));
520*0Sstevel@tonic-gate 			/* sensor event */
521*0Sstevel@tonic-gate 			switch (BYTE_3(res_datap)) {
522*0Sstevel@tonic-gate 			case TEMPERATURE_SENSOR_TYPE:
523*0Sstevel@tonic-gate 				event = TEMPERATURE_SENSOR_EVENT;
524*0Sstevel@tonic-gate 				env_platmod_handle_sensor_event(res_datap);
525*0Sstevel@tonic-gate 				break;
526*0Sstevel@tonic-gate 			default:
527*0Sstevel@tonic-gate 				syslog(LOG_ERR, gettext("SUNW_envmond:Unknown "
528*0Sstevel@tonic-gate 				"sensor Event:%d\n"), BYTE_3(res_datap));
529*0Sstevel@tonic-gate 				event = NO_EVENT;
530*0Sstevel@tonic-gate 				break;
531*0Sstevel@tonic-gate 			}
532*0Sstevel@tonic-gate 		default:
533*0Sstevel@tonic-gate 			env_handle_async_msg_event(res_datap);
534*0Sstevel@tonic-gate 			break;
535*0Sstevel@tonic-gate 		}
536*0Sstevel@tonic-gate 	}
537*0Sstevel@tonic-gate 	return (event);
538*0Sstevel@tonic-gate }
539*0Sstevel@tonic-gate 
540*0Sstevel@tonic-gate /*
541*0Sstevel@tonic-gate  * polls SMC driver for SMC events
542*0Sstevel@tonic-gate  */
543*0Sstevel@tonic-gate /*ARGSUSED*/
544*0Sstevel@tonic-gate static void *
env_polling_thread(void * args)545*0Sstevel@tonic-gate env_polling_thread(void *args)
546*0Sstevel@tonic-gate {
547*0Sstevel@tonic-gate 	int			poll_rc;
548*0Sstevel@tonic-gate 	struct pollfd		poll_fds[1];
549*0Sstevel@tonic-gate 	void			*datap;
550*0Sstevel@tonic-gate 	int			smcfd;
551*0Sstevel@tonic-gate 	struct strioctl		strio;
552*0Sstevel@tonic-gate 	sc_cmdspec_t		set;
553*0Sstevel@tonic-gate 
554*0Sstevel@tonic-gate 	smcfd = env_open_smc();
555*0Sstevel@tonic-gate 	if (smcfd == -1) {
556*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("SUNW_envmond:Error in polling, "
557*0Sstevel@tonic-gate 			"Open of SMC drv failed"));
558*0Sstevel@tonic-gate 		create_polling_thr = B_TRUE;
559*0Sstevel@tonic-gate 		return (NULL);
560*0Sstevel@tonic-gate 	}
561*0Sstevel@tonic-gate 
562*0Sstevel@tonic-gate 	set.args[0]	= SMC_SENSOR_EVENT_ENABLE_SET;
563*0Sstevel@tonic-gate 	set.attribute	= SC_ATTR_SHARED;
564*0Sstevel@tonic-gate 	strio.ic_cmd	= SCIOC_MSG_SPEC;
565*0Sstevel@tonic-gate 	strio.ic_timout	= 0;
566*0Sstevel@tonic-gate 	strio.ic_len	= ENV_SENSOR_EV_ENABLE_PKT_LEN;
567*0Sstevel@tonic-gate 	strio.ic_dp	= (char *)&set;
568*0Sstevel@tonic-gate 	if (ioctl(smcfd, I_STR, &strio) < 0) {
569*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("SUNW_envmond:Request for "
570*0Sstevel@tonic-gate 			"Sensor events failed"));
571*0Sstevel@tonic-gate 		(void) close(smcfd);
572*0Sstevel@tonic-gate 		create_polling_thr = B_TRUE;
573*0Sstevel@tonic-gate 		return (NULL);
574*0Sstevel@tonic-gate 	}
575*0Sstevel@tonic-gate 
576*0Sstevel@tonic-gate 	/* request for async messages */
577*0Sstevel@tonic-gate 	poll_fds[0].fd		= smcfd;
578*0Sstevel@tonic-gate 	poll_fds[0].events	= POLLIN|POLLPRI;
579*0Sstevel@tonic-gate 	poll_fds[0].revents	= 0;
580*0Sstevel@tonic-gate 
581*0Sstevel@tonic-gate 	set.attribute	= SC_ATTR_SHARED;
582*0Sstevel@tonic-gate 	set.args[0]	= SMC_IPMI_RESPONSE_NOTIF;
583*0Sstevel@tonic-gate 	set.args[1]	= SMC_SMC_LOCAL_EVENT_NOTIF;
584*0Sstevel@tonic-gate 	strio.ic_cmd	= SCIOC_MSG_SPEC;
585*0Sstevel@tonic-gate 	strio.ic_timout	= 0;
586*0Sstevel@tonic-gate 	strio.ic_len	= ENV_IPMI_SMC_ENABLE_PKT_LEN;
587*0Sstevel@tonic-gate 	strio.ic_dp	= (char *)&set;
588*0Sstevel@tonic-gate 	if (ioctl(smcfd, I_STR, &strio) == -1) {
589*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("SUNW_envmond:Request for"
590*0Sstevel@tonic-gate 			"Async messages failed"));
591*0Sstevel@tonic-gate 		(void) close(smcfd);
592*0Sstevel@tonic-gate 		create_polling_thr = B_TRUE;
593*0Sstevel@tonic-gate 		return (NULL);
594*0Sstevel@tonic-gate 	}
595*0Sstevel@tonic-gate 
596*0Sstevel@tonic-gate 	/* Now wait for SMC events to come */
597*0Sstevel@tonic-gate 	for (;;) {
598*0Sstevel@tonic-gate 		poll_rc = poll(poll_fds, 1, -1); /* poll forever */
599*0Sstevel@tonic-gate 		if (poll_rc < 0) {
600*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("SUNW_envmond:Event "
601*0Sstevel@tonic-gate 				"processing halted"));
602*0Sstevel@tonic-gate 			break;
603*0Sstevel@tonic-gate 		}
604*0Sstevel@tonic-gate 		if (env_process_smc_event(smcfd, &datap) == NO_EVENT) {
605*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("SUNW_envmond:"
606*0Sstevel@tonic-gate 				"wrong event data posted from SMC"));
607*0Sstevel@tonic-gate 		}
608*0Sstevel@tonic-gate 	}
609*0Sstevel@tonic-gate 	(void) close(smcfd);
610*0Sstevel@tonic-gate 	create_polling_thr = B_TRUE;
611*0Sstevel@tonic-gate 	return (NULL);
612*0Sstevel@tonic-gate }
613*0Sstevel@tonic-gate 
614*0Sstevel@tonic-gate /*
615*0Sstevel@tonic-gate  * (to be)Called during chassis configuration. It does the following tasks.
616*0Sstevel@tonic-gate  * Set global enables on SMC
617*0Sstevel@tonic-gate  * Register for local(SMC) events and remote(IPMI) messages (State Change msgs)
618*0Sstevel@tonic-gate  * creates sensor nodes
619*0Sstevel@tonic-gate  * Initialize hotswap
620*0Sstevel@tonic-gate  * Initiallize the interaction with service processor
621*0Sstevel@tonic-gate  */
622*0Sstevel@tonic-gate static picl_errno_t
env_start_services(void)623*0Sstevel@tonic-gate env_start_services(void)
624*0Sstevel@tonic-gate {
625*0Sstevel@tonic-gate 	int rc;
626*0Sstevel@tonic-gate 	if (env_debug & DEBUG) {
627*0Sstevel@tonic-gate 		syslog(LOG_INFO, "env_start_services begin");
628*0Sstevel@tonic-gate 	}
629*0Sstevel@tonic-gate 
630*0Sstevel@tonic-gate 	/* set the SMC global enables */
631*0Sstevel@tonic-gate 	if (env_set_smc_global_enables(B_TRUE) == -1) {
632*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("SUNW_envmond:Setting SMC "
633*0Sstevel@tonic-gate 			"Globals failed"));
634*0Sstevel@tonic-gate 		return (PICL_FAILURE);
635*0Sstevel@tonic-gate 	}
636*0Sstevel@tonic-gate 
637*0Sstevel@tonic-gate 	/* start a worker thread to poll for SMC events */
638*0Sstevel@tonic-gate 	if (create_polling_thr) {
639*0Sstevel@tonic-gate 		rc = pthread_create(&polling_threadID, NULL,
640*0Sstevel@tonic-gate 			&env_polling_thread, NULL);
641*0Sstevel@tonic-gate 		if (rc != 0) {
642*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("SUNW_envmond:Error in "
643*0Sstevel@tonic-gate 				"creating polling thread"));
644*0Sstevel@tonic-gate 			return (PICL_FAILURE);
645*0Sstevel@tonic-gate 		}
646*0Sstevel@tonic-gate 		create_polling_thr = B_FALSE;
647*0Sstevel@tonic-gate 	}
648*0Sstevel@tonic-gate 
649*0Sstevel@tonic-gate 	/* create the sensor nodes */
650*0Sstevel@tonic-gate 	if ((rc = env_platmod_create_sensors()) != PICL_SUCCESS) {
651*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("SUNW_envmond:Error in creating sensor"
652*0Sstevel@tonic-gate 			" nodes, error = %d"), rc);
653*0Sstevel@tonic-gate 	}
654*0Sstevel@tonic-gate 
655*0Sstevel@tonic-gate 	/* intialize the hotswap framework */
656*0Sstevel@tonic-gate 	if ((rc = env_platmod_setup_hotswap()) != PICL_SUCCESS) {
657*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("SUNW_envmond:Error in hotswap "
658*0Sstevel@tonic-gate 			"initialization, error = %d"), rc);
659*0Sstevel@tonic-gate 	}
660*0Sstevel@tonic-gate 
661*0Sstevel@tonic-gate 	if ((rc = env_platmod_create_hotswap_prop()) != PICL_SUCCESS) {
662*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("SUNW_envmond:Error in creating "
663*0Sstevel@tonic-gate 			"hotswap prop, error = %d"), rc);
664*0Sstevel@tonic-gate 	}
665*0Sstevel@tonic-gate 
666*0Sstevel@tonic-gate 	/* intialize interaction with service processor */
667*0Sstevel@tonic-gate 	if ((rc = env_platmod_sp_monitor()) != PICL_SUCCESS) {
668*0Sstevel@tonic-gate 		syslog(LOG_ERR, gettext("SUNW_envmond:Failed to interact with"
669*0Sstevel@tonic-gate 			" service processor, error = %d"), rc);
670*0Sstevel@tonic-gate 	}
671*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
672*0Sstevel@tonic-gate }
673*0Sstevel@tonic-gate 
674*0Sstevel@tonic-gate static picl_errno_t
env_handle_chassis_configuring_event(char * state)675*0Sstevel@tonic-gate env_handle_chassis_configuring_event(char *state)
676*0Sstevel@tonic-gate {
677*0Sstevel@tonic-gate 	picl_errno_t rc = PICL_SUCCESS;
678*0Sstevel@tonic-gate 	picl_prophdl_t proph;
679*0Sstevel@tonic-gate 	picl_nodehdl_t rtm_lnodehdl = 0;
680*0Sstevel@tonic-gate 	char *cpu_name = PICL_NODE_CPU;
681*0Sstevel@tonic-gate 	char *rtm_name = PICL_NODE_RTM;
682*0Sstevel@tonic-gate 	uint64_t status_time;
683*0Sstevel@tonic-gate 
684*0Sstevel@tonic-gate 	if (strcmp(state, PICLEVENTARGVAL_CONFIGURING) != 0) {
685*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
686*0Sstevel@tonic-gate 	}
687*0Sstevel@tonic-gate 
688*0Sstevel@tonic-gate 	/* initialize cpu loc node handle */
689*0Sstevel@tonic-gate 	if (cpu_lnodehdl == 0) {
690*0Sstevel@tonic-gate 		if ((rc = ptree_find_node(chassis_nodehdl,
691*0Sstevel@tonic-gate 			PICL_PROP_NAME,	PICL_PTYPE_CHARSTRING,
692*0Sstevel@tonic-gate 			cpu_name, (strlen(cpu_name) + 1),
693*0Sstevel@tonic-gate 			&cpu_lnodehdl)) != PICL_SUCCESS) {
694*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("SUNW_envmond: failed "
695*0Sstevel@tonic-gate 			" to get CPU nodehdl, error = %d"), rc);
696*0Sstevel@tonic-gate 			return (rc);
697*0Sstevel@tonic-gate 		}
698*0Sstevel@tonic-gate 	}
699*0Sstevel@tonic-gate 
700*0Sstevel@tonic-gate 	/* create geo-addr prop under CPU location */
701*0Sstevel@tonic-gate 	if (ptree_get_prop_by_name(cpu_lnodehdl, PICL_PROP_GEO_ADDR,
702*0Sstevel@tonic-gate 		&proph) == PICL_PROPNOTFOUND) {
703*0Sstevel@tonic-gate 		if ((rc = env_create_property(PICL_PTYPE_UNSIGNED_INT,
704*0Sstevel@tonic-gate 			PICL_READ, sizeof (cpu_geo_addr),
705*0Sstevel@tonic-gate 			PICL_PROP_GEO_ADDR, NULLREAD, NULLWRITE,
706*0Sstevel@tonic-gate 			cpu_lnodehdl, &proph,
707*0Sstevel@tonic-gate 			(void *)&cpu_geo_addr)) != PICL_SUCCESS) {
708*0Sstevel@tonic-gate 			return (rc);
709*0Sstevel@tonic-gate 		}
710*0Sstevel@tonic-gate 	}
711*0Sstevel@tonic-gate 	if (ptree_get_prop_by_name(cpu_lnodehdl,
712*0Sstevel@tonic-gate 		PICL_PROP_STATUS_TIME, &proph) != PICL_SUCCESS) {
713*0Sstevel@tonic-gate 			status_time = (uint64_t)time(NULL);
714*0Sstevel@tonic-gate 			(void) env_create_property(PICL_PTYPE_TIMESTAMP,
715*0Sstevel@tonic-gate 				PICL_READ, sizeof (status_time),
716*0Sstevel@tonic-gate 				PICL_PROP_STATUS_TIME, NULLREAD, NULLWRITE,
717*0Sstevel@tonic-gate 				cpu_lnodehdl, &proph, &status_time);
718*0Sstevel@tonic-gate 	}
719*0Sstevel@tonic-gate 
720*0Sstevel@tonic-gate 	/* create geo address property for RTM node (if present) */
721*0Sstevel@tonic-gate 	(void) ptree_find_node(chassis_nodehdl,
722*0Sstevel@tonic-gate 		PICL_PROP_NAME,	PICL_PTYPE_CHARSTRING, rtm_name,
723*0Sstevel@tonic-gate 		(strlen(rtm_name) + 1), &rtm_lnodehdl);
724*0Sstevel@tonic-gate 
725*0Sstevel@tonic-gate 	if (rtm_lnodehdl == 0) {	/* RTM not present */
726*0Sstevel@tonic-gate 		return (PICL_SUCCESS);
727*0Sstevel@tonic-gate 	}
728*0Sstevel@tonic-gate 
729*0Sstevel@tonic-gate 	if (ptree_get_prop_by_name(rtm_lnodehdl, PICL_PROP_GEO_ADDR,
730*0Sstevel@tonic-gate 		&proph) == PICL_PROPNOTFOUND) {
731*0Sstevel@tonic-gate 		if ((rc = env_create_property(PICL_PTYPE_UNSIGNED_INT,
732*0Sstevel@tonic-gate 			PICL_READ, sizeof (cpu_geo_addr), PICL_PROP_GEO_ADDR,
733*0Sstevel@tonic-gate 			NULLREAD, NULLWRITE, rtm_lnodehdl, &proph,
734*0Sstevel@tonic-gate 			&cpu_geo_addr)) != PICL_SUCCESS) {
735*0Sstevel@tonic-gate 			syslog(LOG_ERR, gettext("SUNW_envmond:Failed "
736*0Sstevel@tonic-gate 				"to create CPU geo-addr, error = %d"), rc);
737*0Sstevel@tonic-gate 			return (rc);
738*0Sstevel@tonic-gate 		}
739*0Sstevel@tonic-gate 	}
740*0Sstevel@tonic-gate 	if (ptree_get_prop_by_name(rtm_lnodehdl,
741*0Sstevel@tonic-gate 		PICL_PROP_STATUS_TIME, &proph) != PICL_SUCCESS) {
742*0Sstevel@tonic-gate 		status_time = (uint64_t)time(NULL);
743*0Sstevel@tonic-gate 		(void) env_create_property(PICL_PTYPE_TIMESTAMP,
744*0Sstevel@tonic-gate 			PICL_READ, sizeof (status_time), PICL_PROP_STATUS_TIME,
745*0Sstevel@tonic-gate 			NULLREAD, NULLWRITE, rtm_lnodehdl, &proph,
746*0Sstevel@tonic-gate 			&status_time);
747*0Sstevel@tonic-gate 	}
748*0Sstevel@tonic-gate 
749*0Sstevel@tonic-gate 	/* start all the environment monitoring services */
750*0Sstevel@tonic-gate 	if ((rc = env_start_services()) != PICL_SUCCESS) {
751*0Sstevel@tonic-gate 		return (rc);
752*0Sstevel@tonic-gate 	}
753*0Sstevel@tonic-gate 	return (PICL_SUCCESS);
754*0Sstevel@tonic-gate }
755*0Sstevel@tonic-gate 
756*0Sstevel@tonic-gate /*
757*0Sstevel@tonic-gate  * routine to handle all the picl state and condition change events
758*0Sstevel@tonic-gate  */
759*0Sstevel@tonic-gate void
env_handle_event(const char * ename,const void * earg,size_t size)760*0Sstevel@tonic-gate env_handle_event(const char *ename, const void *earg, size_t size)
761*0Sstevel@tonic-gate {
762*0Sstevel@tonic-gate 	picl_nodehdl_t		nodeh = 0;
763*0Sstevel@tonic-gate 	nvlist_t		*nvlp;
764*0Sstevel@tonic-gate 	char			*value;
765*0Sstevel@tonic-gate 	boolean_t		state_event;
766*0Sstevel@tonic-gate 	char			result[PICL_PROPNAMELEN_MAX];
767*0Sstevel@tonic-gate 
768*0Sstevel@tonic-gate 	if (!ename) {
769*0Sstevel@tonic-gate 		return;
770*0Sstevel@tonic-gate 	}
771*0Sstevel@tonic-gate 	if (strcmp(ename, PICLEVENT_STATE_CHANGE) == 0) {
772*0Sstevel@tonic-gate 		state_event = B_TRUE;
773*0Sstevel@tonic-gate 	} else if (strcmp(ename, PICLEVENT_CONDITION_CHANGE) == 0) {
774*0Sstevel@tonic-gate 		state_event = B_FALSE;
775*0Sstevel@tonic-gate 	} else {
776*0Sstevel@tonic-gate 		return;
777*0Sstevel@tonic-gate 	}
778*0Sstevel@tonic-gate 
779*0Sstevel@tonic-gate 	/* unpack the nvlist and get the information */
780*0Sstevel@tonic-gate 	if (nvlist_unpack((char *)earg, size, &nvlp, NULL)) {
781*0Sstevel@tonic-gate 		return;
782*0Sstevel@tonic-gate 	}
783*0Sstevel@tonic-gate 	if (nvlist_lookup_uint64(nvlp, PICLEVENTARG_NODEHANDLE, &nodeh) == -1) {
784*0Sstevel@tonic-gate 		nvlist_free(nvlp);
785*0Sstevel@tonic-gate 		return;
786*0Sstevel@tonic-gate 	}
787*0Sstevel@tonic-gate 	if (nvlist_lookup_string(nvlp, (state_event) ?
788*0Sstevel@tonic-gate 		PICLEVENTARG_STATE :
789*0Sstevel@tonic-gate 		PICLEVENTARG_CONDITION, &value) != 0) {
790*0Sstevel@tonic-gate 		nvlist_free(nvlp);
791*0Sstevel@tonic-gate 		return;
792*0Sstevel@tonic-gate 	}
793*0Sstevel@tonic-gate 
794*0Sstevel@tonic-gate 	if (env_debug & PICLEVENTS) {
795*0Sstevel@tonic-gate 		if (ptree_get_propval_by_name(nodeh, PICL_PROP_NAME,
796*0Sstevel@tonic-gate 			result, sizeof (result)) != PICL_SUCCESS) {
797*0Sstevel@tonic-gate 			syslog(LOG_ERR, " SUNW_envmond: error in getting"
798*0Sstevel@tonic-gate 				" %s", PICL_PROP_NAME);
799*0Sstevel@tonic-gate 			nvlist_free(nvlp);
800*0Sstevel@tonic-gate 			return;
801*0Sstevel@tonic-gate 		}
802*0Sstevel@tonic-gate 		syslog(LOG_INFO, "SUNW_envmond: %s (%s) on %s",
803*0Sstevel@tonic-gate 			ename, value, result);
804*0Sstevel@tonic-gate 	}
805*0Sstevel@tonic-gate 
806*0Sstevel@tonic-gate 	if (chassis_nodehdl == 0 && state_event) {
807*0Sstevel@tonic-gate 		if (ptree_get_propval_by_name(nodeh, PICL_PROP_NAME,
808*0Sstevel@tonic-gate 			result, sizeof (result)) != PICL_SUCCESS) {
809*0Sstevel@tonic-gate 			nvlist_free(nvlp);
810*0Sstevel@tonic-gate 			return;
811*0Sstevel@tonic-gate 		}
812*0Sstevel@tonic-gate 		if (strcmp(result, PICL_NODE_CHASSIS) == 0) {
813*0Sstevel@tonic-gate 			chassis_nodehdl = nodeh;
814*0Sstevel@tonic-gate 		}
815*0Sstevel@tonic-gate 	}
816*0Sstevel@tonic-gate 	if (nodeh == chassis_nodehdl && state_event) {
817*0Sstevel@tonic-gate 		(void) env_handle_chassis_configuring_event(value);
818*0Sstevel@tonic-gate 	}
819*0Sstevel@tonic-gate 	/* do any platform specific handling that is reqd */
820*0Sstevel@tonic-gate 	env_platmod_handle_event(ename, earg, size);
821*0Sstevel@tonic-gate 	nvlist_free(nvlp);
822*0Sstevel@tonic-gate }
823