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 /*
30*0Sstevel@tonic-gate * Routines in this file are used to manage CPU temperature sensor
31*0Sstevel@tonic-gate */
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <unistd.h>
35*0Sstevel@tonic-gate #include <smclib.h>
36*0Sstevel@tonic-gate #include <libintl.h>
37*0Sstevel@tonic-gate #include <syslog.h>
38*0Sstevel@tonic-gate #include <pthread.h>
39*0Sstevel@tonic-gate #include <string.h>
40*0Sstevel@tonic-gate #include <strings.h>
41*0Sstevel@tonic-gate #include <picl.h>
42*0Sstevel@tonic-gate #include <picltree.h>
43*0Sstevel@tonic-gate #include <picldefs.h>
44*0Sstevel@tonic-gate #include <pthread.h>
45*0Sstevel@tonic-gate #include <errno.h>
46*0Sstevel@tonic-gate #include <stropts.h>
47*0Sstevel@tonic-gate #include "piclenvmond.h"
48*0Sstevel@tonic-gate #include "piclsensors.h"
49*0Sstevel@tonic-gate
50*0Sstevel@tonic-gate #define NULLREAD (int (*)(ptree_rarg_t *, void *))0
51*0Sstevel@tonic-gate #define NULLWRITE (int (*)(ptree_warg_t *, const void *))0
52*0Sstevel@tonic-gate #define POLL_TIMEOUT 5000
53*0Sstevel@tonic-gate #define BUF_SIZE 50
54*0Sstevel@tonic-gate
55*0Sstevel@tonic-gate /* packet lengths */
56*0Sstevel@tonic-gate #define ENV_GET_THRESHOLD_PKT_LEN 1
57*0Sstevel@tonic-gate #define ENV_SET_THRESHOLD_PKT_LEN 8
58*0Sstevel@tonic-gate #define ENV_READ_SENSOR_PKT_LEN 1
59*0Sstevel@tonic-gate #define ENV_SENSOR_EVENT_ENABLE_PKT_LEN 2
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate /* req pkt data */
62*0Sstevel@tonic-gate #define ENV_SENSOR_EVENT_ENABLE_MASK 0x80
63*0Sstevel@tonic-gate
64*0Sstevel@tonic-gate /* ptree wrapper to create property */
65*0Sstevel@tonic-gate extern picl_errno_t env_create_property(int ptype, int pmode,
66*0Sstevel@tonic-gate size_t psize, char *pname, int (*readfn)(ptree_rarg_t *, void *),
67*0Sstevel@tonic-gate int (*writefn)(ptree_warg_t *, const void *),
68*0Sstevel@tonic-gate picl_nodehdl_t nodeh, picl_prophdl_t *propp, void *vbuf);
69*0Sstevel@tonic-gate extern int post_sensor_event(picl_nodehdl_t, char *, uint8_t);
70*0Sstevel@tonic-gate extern int env_open_smc(void);
71*0Sstevel@tonic-gate extern int env_debug;
72*0Sstevel@tonic-gate
73*0Sstevel@tonic-gate /* globals */
74*0Sstevel@tonic-gate int sensor_fd = -1;
75*0Sstevel@tonic-gate picl_nodehdl_t sensorh = 0;
76*0Sstevel@tonic-gate pthread_t env_temp_thr_tid;
77*0Sstevel@tonic-gate
78*0Sstevel@tonic-gate /* local vars */
79*0Sstevel@tonic-gate static env_temp_sensor_t temp_sensor;
80*0Sstevel@tonic-gate static pthread_mutex_t sensor_mutex = PTHREAD_MUTEX_INITIALIZER;
81*0Sstevel@tonic-gate static pthread_mutex_t env_temp_monitor_mutex = PTHREAD_MUTEX_INITIALIZER;
82*0Sstevel@tonic-gate static pthread_cond_t env_temp_monitor_cv = PTHREAD_COND_INITIALIZER;
83*0Sstevel@tonic-gate static env_temp_threshold_t env_curr_state = NORMAL_THRESHOLD;
84*0Sstevel@tonic-gate static char *env_thresholds[] = {
85*0Sstevel@tonic-gate PICL_PROP_LOW_WARNING,
86*0Sstevel@tonic-gate PICL_PROP_LOW_SHUTDOWN,
87*0Sstevel@tonic-gate PICL_PROP_LOW_POWER_OFF,
88*0Sstevel@tonic-gate PICL_PROP_HIGH_WARNING,
89*0Sstevel@tonic-gate PICL_PROP_HIGH_SHUTDOWN,
90*0Sstevel@tonic-gate PICL_PROP_HIGH_POWER_OFF
91*0Sstevel@tonic-gate };
92*0Sstevel@tonic-gate static int cpu_sensor_geo_addr = 0;
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate /* local func prototypes */
95*0Sstevel@tonic-gate static void *env_temp_monitor(void *args);
96*0Sstevel@tonic-gate
97*0Sstevel@tonic-gate /*
98*0Sstevel@tonic-gate * Reads the threshold value from hardware
99*0Sstevel@tonic-gate */
100*0Sstevel@tonic-gate static picl_errno_t
env_get_temp_threshold(int sensor_no,int threshold_no,int8_t * threshold_reading)101*0Sstevel@tonic-gate env_get_temp_threshold(int sensor_no, int threshold_no,
102*0Sstevel@tonic-gate int8_t *threshold_reading)
103*0Sstevel@tonic-gate {
104*0Sstevel@tonic-gate sc_reqmsg_t req_pkt;
105*0Sstevel@tonic-gate sc_rspmsg_t rsp_pkt;
106*0Sstevel@tonic-gate smc_errno_t rc = SMC_SUCCESS;
107*0Sstevel@tonic-gate uint8_t size = 0;
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate if (threshold_no < 1 || threshold_no > 6) {
110*0Sstevel@tonic-gate return (PICL_INVALIDARG);
111*0Sstevel@tonic-gate }
112*0Sstevel@tonic-gate
113*0Sstevel@tonic-gate req_pkt.data[0] = sensor_no;
114*0Sstevel@tonic-gate size = ENV_GET_THRESHOLD_PKT_LEN;
115*0Sstevel@tonic-gate /* initialize the request packet */
116*0Sstevel@tonic-gate (void) smc_init_smc_msg(&req_pkt, SMC_SENSOR_THRESHOLD_GET,
117*0Sstevel@tonic-gate DEFAULT_SEQN, size);
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate /* make a call to smc library to send cmd */
120*0Sstevel@tonic-gate if ((rc = smc_send_msg(DEFAULT_FD, &req_pkt, &rsp_pkt,
121*0Sstevel@tonic-gate POLL_TIMEOUT)) != SMC_SUCCESS) {
122*0Sstevel@tonic-gate syslog(LOG_ERR, SMC_GET_SENSOR_THRES_FAILED,
123*0Sstevel@tonic-gate sensor_no, rc);
124*0Sstevel@tonic-gate return (PICL_FAILURE);
125*0Sstevel@tonic-gate }
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate switch (threshold_no) {
128*0Sstevel@tonic-gate case LOW_WARNING_THRESHOLD:
129*0Sstevel@tonic-gate if (LOW_WARNING_BIT(rsp_pkt.data[0])) {
130*0Sstevel@tonic-gate *threshold_reading = rsp_pkt.data[1];
131*0Sstevel@tonic-gate } else {
132*0Sstevel@tonic-gate return (PICL_PERMDENIED);
133*0Sstevel@tonic-gate }
134*0Sstevel@tonic-gate break;
135*0Sstevel@tonic-gate case LOW_SHUTDOWN_THRESHOLD:
136*0Sstevel@tonic-gate if (LOW_SHUTDOWN_BIT(rsp_pkt.data[0])) {
137*0Sstevel@tonic-gate *threshold_reading = rsp_pkt.data[2];
138*0Sstevel@tonic-gate } else {
139*0Sstevel@tonic-gate return (PICL_PERMDENIED);
140*0Sstevel@tonic-gate }
141*0Sstevel@tonic-gate break;
142*0Sstevel@tonic-gate case LOW_POWEROFF_THRESHOLD:
143*0Sstevel@tonic-gate if (LOW_POWEROFF_BIT(rsp_pkt.data[0])) {
144*0Sstevel@tonic-gate *threshold_reading = rsp_pkt.data[3];
145*0Sstevel@tonic-gate } else {
146*0Sstevel@tonic-gate return (PICL_PERMDENIED);
147*0Sstevel@tonic-gate }
148*0Sstevel@tonic-gate break;
149*0Sstevel@tonic-gate case HIGH_WARNING_THRESHOLD:
150*0Sstevel@tonic-gate if (HIGH_WARNING_BIT(rsp_pkt.data[0])) {
151*0Sstevel@tonic-gate *threshold_reading = rsp_pkt.data[4];
152*0Sstevel@tonic-gate } else {
153*0Sstevel@tonic-gate return (PICL_PERMDENIED);
154*0Sstevel@tonic-gate }
155*0Sstevel@tonic-gate break;
156*0Sstevel@tonic-gate case HIGH_SHUTDOWN_THRESHOLD:
157*0Sstevel@tonic-gate if (HIGH_SHUTDOWN_BIT(rsp_pkt.data[0])) {
158*0Sstevel@tonic-gate *threshold_reading = rsp_pkt.data[5];
159*0Sstevel@tonic-gate } else {
160*0Sstevel@tonic-gate return (PICL_PERMDENIED);
161*0Sstevel@tonic-gate }
162*0Sstevel@tonic-gate break;
163*0Sstevel@tonic-gate case HIGH_POWEROFF_THRESHOLD:
164*0Sstevel@tonic-gate if (HIGH_POWEROFF_BIT(rsp_pkt.data[0])) {
165*0Sstevel@tonic-gate *threshold_reading = rsp_pkt.data[6];
166*0Sstevel@tonic-gate } else {
167*0Sstevel@tonic-gate return (PICL_PERMDENIED);
168*0Sstevel@tonic-gate }
169*0Sstevel@tonic-gate break;
170*0Sstevel@tonic-gate default:
171*0Sstevel@tonic-gate return (PICL_INVALIDARG);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate return (PICL_SUCCESS);
174*0Sstevel@tonic-gate }
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate /*
177*0Sstevel@tonic-gate * Sets the threshold temperature specified in given sensor number
178*0Sstevel@tonic-gate */
179*0Sstevel@tonic-gate static picl_errno_t
env_set_temp_threshold(int sensor_no,int threshold_no,int8_t set_value)180*0Sstevel@tonic-gate env_set_temp_threshold(int sensor_no, int threshold_no,
181*0Sstevel@tonic-gate int8_t set_value)
182*0Sstevel@tonic-gate {
183*0Sstevel@tonic-gate sc_reqmsg_t req_pkt;
184*0Sstevel@tonic-gate sc_rspmsg_t rsp_pkt;
185*0Sstevel@tonic-gate smc_errno_t rc;
186*0Sstevel@tonic-gate uint8_t size = 0;
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate if (threshold_no < 1 || threshold_no > 6) {
189*0Sstevel@tonic-gate return (PICL_INVALIDARG);
190*0Sstevel@tonic-gate }
191*0Sstevel@tonic-gate
192*0Sstevel@tonic-gate req_pkt.data[0] = (int8_t)sensor_no;
193*0Sstevel@tonic-gate req_pkt.data[1] = 0x01 << (threshold_no - 1); /* set the bit mask */
194*0Sstevel@tonic-gate req_pkt.data[1 + threshold_no] = set_value;
195*0Sstevel@tonic-gate size = ENV_SET_THRESHOLD_PKT_LEN;
196*0Sstevel@tonic-gate
197*0Sstevel@tonic-gate /* initialize the request packet */
198*0Sstevel@tonic-gate (void) smc_init_smc_msg(&req_pkt, SMC_SENSOR_THRESHOLD_SET,
199*0Sstevel@tonic-gate DEFAULT_SEQN, size);
200*0Sstevel@tonic-gate
201*0Sstevel@tonic-gate /* make a call to smc library to send cmd */
202*0Sstevel@tonic-gate if ((rc = smc_send_msg(sensor_fd, &req_pkt, &rsp_pkt,
203*0Sstevel@tonic-gate POLL_TIMEOUT)) != SMC_SUCCESS) {
204*0Sstevel@tonic-gate syslog(LOG_ERR, SMC_SET_SENSOR_THRES_FAILED,
205*0Sstevel@tonic-gate sensor_no, rc);
206*0Sstevel@tonic-gate return (PICL_FAILURE);
207*0Sstevel@tonic-gate }
208*0Sstevel@tonic-gate return (PICL_SUCCESS);
209*0Sstevel@tonic-gate }
210*0Sstevel@tonic-gate
211*0Sstevel@tonic-gate /*
212*0Sstevel@tonic-gate * returns the sensor reading of the SMC sensor specified in sensor_no
213*0Sstevel@tonic-gate */
214*0Sstevel@tonic-gate static picl_errno_t
env_get_sensor_reading(uint8_t sensor_no,int8_t * sensor_reading)215*0Sstevel@tonic-gate env_get_sensor_reading(uint8_t sensor_no, int8_t *sensor_reading)
216*0Sstevel@tonic-gate {
217*0Sstevel@tonic-gate sc_reqmsg_t req_pkt;
218*0Sstevel@tonic-gate sc_rspmsg_t rsp_pkt;
219*0Sstevel@tonic-gate smc_errno_t rc = SMC_SUCCESS;
220*0Sstevel@tonic-gate uint8_t size = 0;
221*0Sstevel@tonic-gate
222*0Sstevel@tonic-gate req_pkt.data[0] = sensor_no;
223*0Sstevel@tonic-gate /* initialize the request packet */
224*0Sstevel@tonic-gate size = ENV_READ_SENSOR_PKT_LEN;
225*0Sstevel@tonic-gate (void) smc_init_smc_msg(&req_pkt, SMC_SENSOR_READING_GET,
226*0Sstevel@tonic-gate DEFAULT_SEQN, size);
227*0Sstevel@tonic-gate
228*0Sstevel@tonic-gate /* make a call to smc library to send cmd */
229*0Sstevel@tonic-gate if ((rc = smc_send_msg(DEFAULT_FD, &req_pkt, &rsp_pkt,
230*0Sstevel@tonic-gate POLL_TIMEOUT)) != SMC_SUCCESS) {
231*0Sstevel@tonic-gate syslog(LOG_ERR, SMC_GET_SENSOR_READING_FAILED,
232*0Sstevel@tonic-gate sensor_no, rc);
233*0Sstevel@tonic-gate return (PICL_FAILURE);
234*0Sstevel@tonic-gate }
235*0Sstevel@tonic-gate *sensor_reading = rsp_pkt.data[0];
236*0Sstevel@tonic-gate return (PICL_SUCCESS);
237*0Sstevel@tonic-gate }
238*0Sstevel@tonic-gate
239*0Sstevel@tonic-gate /*
240*0Sstevel@tonic-gate * volatile call back function to read the current temparature
241*0Sstevel@tonic-gate */
242*0Sstevel@tonic-gate static int
get_curr_temp(ptree_rarg_t * argp,void * bufp)243*0Sstevel@tonic-gate get_curr_temp(ptree_rarg_t *argp, void *bufp)
244*0Sstevel@tonic-gate {
245*0Sstevel@tonic-gate uint8_t sensor_no;
246*0Sstevel@tonic-gate int8_t sensor_reading;
247*0Sstevel@tonic-gate picl_errno_t rc;
248*0Sstevel@tonic-gate
249*0Sstevel@tonic-gate if ((rc = ptree_get_propval_by_name(argp->nodeh,
250*0Sstevel@tonic-gate PICL_PROP_GEO_ADDR, &sensor_no, sizeof (sensor_no))) !=
251*0Sstevel@tonic-gate PICL_SUCCESS) {
252*0Sstevel@tonic-gate return (rc);
253*0Sstevel@tonic-gate }
254*0Sstevel@tonic-gate
255*0Sstevel@tonic-gate /* read the temp from SMC f/w */
256*0Sstevel@tonic-gate if ((rc = env_get_sensor_reading(sensor_no, &sensor_reading)) !=
257*0Sstevel@tonic-gate PICL_SUCCESS) {
258*0Sstevel@tonic-gate return (rc);
259*0Sstevel@tonic-gate }
260*0Sstevel@tonic-gate *(int8_t *)bufp = sensor_reading;
261*0Sstevel@tonic-gate
262*0Sstevel@tonic-gate /* update the internal cache */
263*0Sstevel@tonic-gate (void) pthread_mutex_lock(&sensor_mutex);
264*0Sstevel@tonic-gate temp_sensor.curr_temp = sensor_reading;
265*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
266*0Sstevel@tonic-gate
267*0Sstevel@tonic-gate return (PICL_SUCCESS);
268*0Sstevel@tonic-gate }
269*0Sstevel@tonic-gate
270*0Sstevel@tonic-gate /*
271*0Sstevel@tonic-gate * volatile function that returns the state of sensor
272*0Sstevel@tonic-gate */
273*0Sstevel@tonic-gate static int
get_sensor_condition(ptree_rarg_t * argp,void * bufp)274*0Sstevel@tonic-gate get_sensor_condition(ptree_rarg_t *argp, void *bufp)
275*0Sstevel@tonic-gate {
276*0Sstevel@tonic-gate uint8_t sensor_no;
277*0Sstevel@tonic-gate picl_errno_t rc = PICL_SUCCESS;
278*0Sstevel@tonic-gate int8_t sensor_reading;
279*0Sstevel@tonic-gate
280*0Sstevel@tonic-gate if ((rc = ptree_get_propval_by_name(argp->nodeh,
281*0Sstevel@tonic-gate PICL_PROP_GEO_ADDR, &sensor_no, sizeof (sensor_no))) !=
282*0Sstevel@tonic-gate PICL_SUCCESS) {
283*0Sstevel@tonic-gate return (rc);
284*0Sstevel@tonic-gate }
285*0Sstevel@tonic-gate
286*0Sstevel@tonic-gate /* read the curr temp from SMC f/w */
287*0Sstevel@tonic-gate if ((rc = env_get_sensor_reading(sensor_no, &sensor_reading)) !=
288*0Sstevel@tonic-gate PICL_SUCCESS) {
289*0Sstevel@tonic-gate (void) pthread_mutex_lock(&sensor_mutex);
290*0Sstevel@tonic-gate (void) strncpy(temp_sensor.state, PICLEVENTARGVAL_UNKNOWN,
291*0Sstevel@tonic-gate sizeof (temp_sensor.state));
292*0Sstevel@tonic-gate (void) strncpy((char *)bufp, PICLEVENTARGVAL_UNKNOWN,
293*0Sstevel@tonic-gate PICL_PROPNAMELEN_MAX);
294*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
295*0Sstevel@tonic-gate return (PICL_SUCCESS);
296*0Sstevel@tonic-gate }
297*0Sstevel@tonic-gate
298*0Sstevel@tonic-gate (void) pthread_mutex_lock(&sensor_mutex);
299*0Sstevel@tonic-gate
300*0Sstevel@tonic-gate if (sensor_reading > temp_sensor.hi_shutdown ||
301*0Sstevel@tonic-gate sensor_reading < temp_sensor.lo_shutdown)
302*0Sstevel@tonic-gate (void) strncpy(temp_sensor.state,
303*0Sstevel@tonic-gate PICLEVENTARGVAL_SENSOR_COND_SHUTDOWN,
304*0Sstevel@tonic-gate sizeof (temp_sensor.state));
305*0Sstevel@tonic-gate else if (sensor_reading > temp_sensor.hi_warning ||
306*0Sstevel@tonic-gate sensor_reading < temp_sensor.lo_warning)
307*0Sstevel@tonic-gate (void) strncpy(temp_sensor.state,
308*0Sstevel@tonic-gate PICLEVENTARGVAL_SENSOR_COND_WARNING,
309*0Sstevel@tonic-gate sizeof (temp_sensor.state));
310*0Sstevel@tonic-gate else
311*0Sstevel@tonic-gate (void) strncpy(temp_sensor.state, PICLEVENTARGVAL_OK,
312*0Sstevel@tonic-gate sizeof (temp_sensor.state));
313*0Sstevel@tonic-gate (void) strncpy((char *)bufp, temp_sensor.state,
314*0Sstevel@tonic-gate PICL_PROPNAMELEN_MAX);
315*0Sstevel@tonic-gate
316*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
317*0Sstevel@tonic-gate return (PICL_SUCCESS);
318*0Sstevel@tonic-gate }
319*0Sstevel@tonic-gate
320*0Sstevel@tonic-gate /*
321*0Sstevel@tonic-gate * volatile property to read sensor thresholds
322*0Sstevel@tonic-gate */
323*0Sstevel@tonic-gate static int
get_sensor_thr(ptree_rarg_t * argp,void * bufp)324*0Sstevel@tonic-gate get_sensor_thr(ptree_rarg_t *argp, void *bufp)
325*0Sstevel@tonic-gate {
326*0Sstevel@tonic-gate picl_errno_t rc = PICL_SUCCESS;
327*0Sstevel@tonic-gate ptree_propinfo_t pi;
328*0Sstevel@tonic-gate char prop_name[PICL_PROPNAMELEN_MAX];
329*0Sstevel@tonic-gate
330*0Sstevel@tonic-gate if ((rc = ptree_get_propinfo(argp->proph, &pi)) != PICL_SUCCESS) {
331*0Sstevel@tonic-gate return (rc);
332*0Sstevel@tonic-gate }
333*0Sstevel@tonic-gate (void) strncpy(prop_name, pi.piclinfo.name, sizeof (prop_name));
334*0Sstevel@tonic-gate
335*0Sstevel@tonic-gate (void) pthread_mutex_lock(&sensor_mutex);
336*0Sstevel@tonic-gate
337*0Sstevel@tonic-gate if (strcmp(prop_name, PICL_PROP_LOW_WARNING) == 0) {
338*0Sstevel@tonic-gate *(int8_t *)bufp = temp_sensor.lo_warning;
339*0Sstevel@tonic-gate } else if (strcmp(prop_name, PICL_PROP_LOW_SHUTDOWN) == 0) {
340*0Sstevel@tonic-gate *(int8_t *)bufp = temp_sensor.lo_shutdown;
341*0Sstevel@tonic-gate } else if (strcmp(prop_name, PICL_PROP_LOW_POWER_OFF) == 0) {
342*0Sstevel@tonic-gate *(int8_t *)bufp = temp_sensor.lo_poweroff;
343*0Sstevel@tonic-gate } else if (strcmp(prop_name, PICL_PROP_HIGH_WARNING) == 0) {
344*0Sstevel@tonic-gate *(int8_t *)bufp = temp_sensor.hi_warning;
345*0Sstevel@tonic-gate } else if (strcmp(prop_name, PICL_PROP_HIGH_SHUTDOWN) == 0) {
346*0Sstevel@tonic-gate *(int8_t *)bufp = temp_sensor.hi_shutdown;
347*0Sstevel@tonic-gate } else if (strcmp(prop_name, PICL_PROP_HIGH_POWER_OFF) == 0) {
348*0Sstevel@tonic-gate *(int8_t *)bufp = temp_sensor.hi_poweroff;
349*0Sstevel@tonic-gate } else {
350*0Sstevel@tonic-gate rc = PICL_INVALIDARG;
351*0Sstevel@tonic-gate }
352*0Sstevel@tonic-gate
353*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
354*0Sstevel@tonic-gate return (rc);
355*0Sstevel@tonic-gate }
356*0Sstevel@tonic-gate
357*0Sstevel@tonic-gate /*
358*0Sstevel@tonic-gate * volatile callback function to set the temp thresholds
359*0Sstevel@tonic-gate */
360*0Sstevel@tonic-gate static int
set_sensor_thr(ptree_warg_t * argp,const void * bufp)361*0Sstevel@tonic-gate set_sensor_thr(ptree_warg_t *argp, const void *bufp)
362*0Sstevel@tonic-gate {
363*0Sstevel@tonic-gate picl_errno_t rc = PICL_SUCCESS;
364*0Sstevel@tonic-gate ptree_propinfo_t pi;
365*0Sstevel@tonic-gate int threshold_no = 0;
366*0Sstevel@tonic-gate int8_t temp = *(int8_t *)bufp;
367*0Sstevel@tonic-gate char cmd[BUF_SIZE];
368*0Sstevel@tonic-gate char prop_name[PICL_PROPNAMELEN_MAX];
369*0Sstevel@tonic-gate
370*0Sstevel@tonic-gate if ((rc = ptree_get_propinfo(argp->proph, &pi)) != PICL_SUCCESS) {
371*0Sstevel@tonic-gate return (rc);
372*0Sstevel@tonic-gate }
373*0Sstevel@tonic-gate (void) strncpy(prop_name, pi.piclinfo.name, sizeof (prop_name));
374*0Sstevel@tonic-gate cmd[0] = '\0';
375*0Sstevel@tonic-gate
376*0Sstevel@tonic-gate (void) pthread_mutex_lock(&sensor_mutex);
377*0Sstevel@tonic-gate
378*0Sstevel@tonic-gate if (strcmp(prop_name, PICL_PROP_LOW_WARNING) == 0) {
379*0Sstevel@tonic-gate /* warning cannot be less than shutdown threshold */
380*0Sstevel@tonic-gate if (temp <= temp_sensor.lo_shutdown) {
381*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
382*0Sstevel@tonic-gate return (PICL_INVALIDARG);
383*0Sstevel@tonic-gate }
384*0Sstevel@tonic-gate threshold_no = LOW_WARNING_THRESHOLD;
385*0Sstevel@tonic-gate } else if (strcmp(prop_name, PICL_PROP_LOW_SHUTDOWN) == 0) {
386*0Sstevel@tonic-gate /* shutdown cannot be greater than warning threshold */
387*0Sstevel@tonic-gate if (temp >= temp_sensor.lo_warning) {
388*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
389*0Sstevel@tonic-gate return (PICL_INVALIDARG);
390*0Sstevel@tonic-gate }
391*0Sstevel@tonic-gate threshold_no = LOW_SHUTDOWN_THRESHOLD;
392*0Sstevel@tonic-gate } else if (strcmp(prop_name, PICL_PROP_LOW_POWER_OFF) == 0) {
393*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
394*0Sstevel@tonic-gate return (PICL_PERMDENIED);
395*0Sstevel@tonic-gate } else if (strcmp(prop_name, PICL_PROP_HIGH_WARNING) == 0) {
396*0Sstevel@tonic-gate if ((temp + 5) > temp_sensor.hi_shutdown) {
397*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
398*0Sstevel@tonic-gate return (PICL_INVALIDARG);
399*0Sstevel@tonic-gate }
400*0Sstevel@tonic-gate /* change the OBP nvram property */
401*0Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd),
402*0Sstevel@tonic-gate EEPROM_WARNING_CMD, temp);
403*0Sstevel@tonic-gate threshold_no = HIGH_WARNING_THRESHOLD;
404*0Sstevel@tonic-gate } else if (strcmp(prop_name, PICL_PROP_HIGH_SHUTDOWN) == 0) {
405*0Sstevel@tonic-gate if ((temp - 5) < temp_sensor.hi_warning) {
406*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
407*0Sstevel@tonic-gate return (PICL_INVALIDARG);
408*0Sstevel@tonic-gate }
409*0Sstevel@tonic-gate /* change the OBP nvram property */
410*0Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd),
411*0Sstevel@tonic-gate EEPROM_SHUTDOWN_CMD, temp);
412*0Sstevel@tonic-gate threshold_no = HIGH_SHUTDOWN_THRESHOLD;
413*0Sstevel@tonic-gate } else if (strcmp(prop_name, PICL_PROP_HIGH_POWER_OFF) == 0) {
414*0Sstevel@tonic-gate if (temp > MAX_POWEROFF_TEMP ||
415*0Sstevel@tonic-gate (temp - 5) < temp_sensor.hi_shutdown) {
416*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
417*0Sstevel@tonic-gate return (PICL_INVALIDARG);
418*0Sstevel@tonic-gate }
419*0Sstevel@tonic-gate /* change the OBP nvram property */
420*0Sstevel@tonic-gate threshold_no = HIGH_POWEROFF_THRESHOLD;
421*0Sstevel@tonic-gate (void) snprintf(cmd, sizeof (cmd),
422*0Sstevel@tonic-gate EEPROM_POWEROFF_CMD, temp);
423*0Sstevel@tonic-gate } else {
424*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
425*0Sstevel@tonic-gate return (PICL_INVALIDARG);
426*0Sstevel@tonic-gate }
427*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
428*0Sstevel@tonic-gate
429*0Sstevel@tonic-gate if ((rc = env_set_temp_threshold(cpu_sensor_geo_addr,
430*0Sstevel@tonic-gate threshold_no, temp)) != PICL_SUCCESS) {
431*0Sstevel@tonic-gate return (rc);
432*0Sstevel@tonic-gate }
433*0Sstevel@tonic-gate
434*0Sstevel@tonic-gate (void) pthread_mutex_lock(&sensor_mutex);
435*0Sstevel@tonic-gate switch (threshold_no) {
436*0Sstevel@tonic-gate case LOW_WARNING_THRESHOLD:
437*0Sstevel@tonic-gate temp_sensor.lo_warning = temp;
438*0Sstevel@tonic-gate break;
439*0Sstevel@tonic-gate case LOW_SHUTDOWN_THRESHOLD:
440*0Sstevel@tonic-gate temp_sensor.lo_shutdown = temp;
441*0Sstevel@tonic-gate break;
442*0Sstevel@tonic-gate case LOW_POWEROFF_THRESHOLD:
443*0Sstevel@tonic-gate temp_sensor.lo_poweroff = temp;
444*0Sstevel@tonic-gate break;
445*0Sstevel@tonic-gate case HIGH_WARNING_THRESHOLD:
446*0Sstevel@tonic-gate temp_sensor.hi_warning = temp;
447*0Sstevel@tonic-gate break;
448*0Sstevel@tonic-gate case HIGH_SHUTDOWN_THRESHOLD:
449*0Sstevel@tonic-gate temp_sensor.hi_shutdown = temp;
450*0Sstevel@tonic-gate break;
451*0Sstevel@tonic-gate case HIGH_POWEROFF_THRESHOLD:
452*0Sstevel@tonic-gate temp_sensor.hi_poweroff = temp;
453*0Sstevel@tonic-gate break;
454*0Sstevel@tonic-gate }
455*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
456*0Sstevel@tonic-gate
457*0Sstevel@tonic-gate /* execute the cmd to change OBP nvram property */
458*0Sstevel@tonic-gate if (cmd[0]) {
459*0Sstevel@tonic-gate (void) pclose(popen(cmd, "w"));
460*0Sstevel@tonic-gate }
461*0Sstevel@tonic-gate return (PICL_SUCCESS);
462*0Sstevel@tonic-gate }
463*0Sstevel@tonic-gate
464*0Sstevel@tonic-gate /*
465*0Sstevel@tonic-gate * this routine reads the hardware state and initialises the internal
466*0Sstevel@tonic-gate * cache for temperature thresholds
467*0Sstevel@tonic-gate */
468*0Sstevel@tonic-gate static picl_errno_t
env_init_temp_sensor_values(int sensor_no,env_temp_sensor_t * sensor)469*0Sstevel@tonic-gate env_init_temp_sensor_values(int sensor_no, env_temp_sensor_t *sensor)
470*0Sstevel@tonic-gate {
471*0Sstevel@tonic-gate if (env_get_sensor_reading(sensor_no, &sensor->curr_temp) !=
472*0Sstevel@tonic-gate PICL_SUCCESS) {
473*0Sstevel@tonic-gate return (PICL_FAILURE);
474*0Sstevel@tonic-gate }
475*0Sstevel@tonic-gate
476*0Sstevel@tonic-gate if (env_get_temp_threshold(sensor_no, LOW_WARNING_THRESHOLD,
477*0Sstevel@tonic-gate &sensor->lo_warning) != PICL_SUCCESS) {
478*0Sstevel@tonic-gate syslog(LOG_ERR, SMC_GET_LWT_FAILED);
479*0Sstevel@tonic-gate return (PICL_FAILURE);
480*0Sstevel@tonic-gate }
481*0Sstevel@tonic-gate
482*0Sstevel@tonic-gate if (env_get_temp_threshold(sensor_no, LOW_SHUTDOWN_THRESHOLD,
483*0Sstevel@tonic-gate &sensor->lo_shutdown) != PICL_SUCCESS) {
484*0Sstevel@tonic-gate syslog(LOG_ERR, SMC_GET_LST_FAILED);
485*0Sstevel@tonic-gate return (PICL_FAILURE);
486*0Sstevel@tonic-gate }
487*0Sstevel@tonic-gate
488*0Sstevel@tonic-gate if (env_get_temp_threshold(sensor_no, LOW_POWEROFF_THRESHOLD,
489*0Sstevel@tonic-gate &sensor->lo_poweroff) != PICL_SUCCESS) {
490*0Sstevel@tonic-gate syslog(LOG_ERR, SMC_GET_LPT_FAILED);
491*0Sstevel@tonic-gate return (PICL_FAILURE);
492*0Sstevel@tonic-gate }
493*0Sstevel@tonic-gate
494*0Sstevel@tonic-gate if (env_get_temp_threshold(sensor_no, HIGH_WARNING_THRESHOLD,
495*0Sstevel@tonic-gate &sensor->hi_warning) != PICL_SUCCESS) {
496*0Sstevel@tonic-gate syslog(LOG_ERR, SMC_SET_LWT_FAILED);
497*0Sstevel@tonic-gate return (PICL_FAILURE);
498*0Sstevel@tonic-gate }
499*0Sstevel@tonic-gate
500*0Sstevel@tonic-gate if (env_get_temp_threshold(sensor_no, HIGH_SHUTDOWN_THRESHOLD,
501*0Sstevel@tonic-gate &sensor->hi_shutdown) != PICL_SUCCESS) {
502*0Sstevel@tonic-gate syslog(LOG_ERR, SMC_SET_LST_FAILED);
503*0Sstevel@tonic-gate return (PICL_FAILURE);
504*0Sstevel@tonic-gate }
505*0Sstevel@tonic-gate
506*0Sstevel@tonic-gate if (env_get_temp_threshold(sensor_no, HIGH_POWEROFF_THRESHOLD,
507*0Sstevel@tonic-gate &sensor->hi_poweroff) != PICL_SUCCESS) {
508*0Sstevel@tonic-gate syslog(LOG_ERR, SMC_SET_LPT_FAILED);
509*0Sstevel@tonic-gate return (PICL_FAILURE);
510*0Sstevel@tonic-gate }
511*0Sstevel@tonic-gate
512*0Sstevel@tonic-gate if (sensor->curr_temp > sensor->hi_shutdown ||
513*0Sstevel@tonic-gate sensor->curr_temp < sensor->lo_shutdown) {
514*0Sstevel@tonic-gate (void) strncpy(sensor->state,
515*0Sstevel@tonic-gate PICLEVENTARGVAL_SENSOR_COND_SHUTDOWN,
516*0Sstevel@tonic-gate sizeof (sensor->state));
517*0Sstevel@tonic-gate } else if (sensor->curr_temp > sensor->hi_warning ||
518*0Sstevel@tonic-gate sensor->curr_temp < sensor->lo_warning) {
519*0Sstevel@tonic-gate (void) strncpy(sensor->state,
520*0Sstevel@tonic-gate PICLEVENTARGVAL_SENSOR_COND_WARNING,
521*0Sstevel@tonic-gate sizeof (sensor->state));
522*0Sstevel@tonic-gate } else {
523*0Sstevel@tonic-gate (void) strncpy(sensor->state, PICLEVENTARGVAL_OK,
524*0Sstevel@tonic-gate sizeof (sensor->state));
525*0Sstevel@tonic-gate }
526*0Sstevel@tonic-gate return (PICL_SUCCESS);
527*0Sstevel@tonic-gate }
528*0Sstevel@tonic-gate
529*0Sstevel@tonic-gate /*
530*0Sstevel@tonic-gate * sensor_event_enable_set: enables or disables Event Message generation
531*0Sstevel@tonic-gate * from a sensor specified by sensor_no
532*0Sstevel@tonic-gate */
533*0Sstevel@tonic-gate static int
sensor_event_enable_set(uint8_t sensor_no,boolean_t enable)534*0Sstevel@tonic-gate sensor_event_enable_set(uint8_t sensor_no, boolean_t enable)
535*0Sstevel@tonic-gate {
536*0Sstevel@tonic-gate smc_errno_t rc = SMC_SUCCESS;
537*0Sstevel@tonic-gate sc_reqmsg_t req_pkt;
538*0Sstevel@tonic-gate sc_rspmsg_t rsp_pkt;
539*0Sstevel@tonic-gate uint8_t size = 0;
540*0Sstevel@tonic-gate
541*0Sstevel@tonic-gate req_pkt.data[0] = sensor_no;
542*0Sstevel@tonic-gate req_pkt.data[1] = 0;
543*0Sstevel@tonic-gate if (enable) {
544*0Sstevel@tonic-gate req_pkt.data[1] |= ENV_SENSOR_EVENT_ENABLE_MASK;
545*0Sstevel@tonic-gate }
546*0Sstevel@tonic-gate size = ENV_SENSOR_EVENT_ENABLE_PKT_LEN;
547*0Sstevel@tonic-gate
548*0Sstevel@tonic-gate (void) smc_init_smc_msg(&req_pkt, SMC_SENSOR_EVENT_ENABLE_SET,
549*0Sstevel@tonic-gate DEFAULT_SEQN, size);
550*0Sstevel@tonic-gate
551*0Sstevel@tonic-gate /* make a call to smc library to send cmd */
552*0Sstevel@tonic-gate if ((rc = smc_send_msg(DEFAULT_FD, &req_pkt, &rsp_pkt,
553*0Sstevel@tonic-gate POLL_TIMEOUT)) != SMC_SUCCESS) {
554*0Sstevel@tonic-gate syslog(LOG_ERR, SMC_ENABLE_SENSOR_EVENT_FAILED, rc);
555*0Sstevel@tonic-gate return (PICL_FAILURE);
556*0Sstevel@tonic-gate }
557*0Sstevel@tonic-gate return (PICL_SUCCESS);
558*0Sstevel@tonic-gate }
559*0Sstevel@tonic-gate
560*0Sstevel@tonic-gate /*
561*0Sstevel@tonic-gate * creates temperature sensor node and all of its properties
562*0Sstevel@tonic-gate */
563*0Sstevel@tonic-gate picl_errno_t
env_create_temp_sensor_node(picl_nodehdl_t parenth,uint8_t sensor_no)564*0Sstevel@tonic-gate env_create_temp_sensor_node(picl_nodehdl_t parenth, uint8_t sensor_no)
565*0Sstevel@tonic-gate {
566*0Sstevel@tonic-gate int i = 0;
567*0Sstevel@tonic-gate picl_errno_t rc = PICL_SUCCESS;
568*0Sstevel@tonic-gate int8_t sensor_reading = 0;
569*0Sstevel@tonic-gate struct strioctl strio;
570*0Sstevel@tonic-gate sc_cmdspec_t set;
571*0Sstevel@tonic-gate
572*0Sstevel@tonic-gate sensor_fd = env_open_smc();
573*0Sstevel@tonic-gate if (sensor_fd < 0) {
574*0Sstevel@tonic-gate syslog(LOG_ERR, gettext("SUNW_envmond:Error in "
575*0Sstevel@tonic-gate "opening SMC(failed to create sensor nodes)"));
576*0Sstevel@tonic-gate return (PICL_FAILURE);
577*0Sstevel@tonic-gate }
578*0Sstevel@tonic-gate
579*0Sstevel@tonic-gate /* grab exclusive access to set the thresholds */
580*0Sstevel@tonic-gate set.args[0] = SMC_SENSOR_THRESHOLD_SET;
581*0Sstevel@tonic-gate set.attribute = SC_ATTR_EXCLUSIVE;
582*0Sstevel@tonic-gate strio.ic_cmd = SCIOC_MSG_SPEC;
583*0Sstevel@tonic-gate strio.ic_timout = 0;
584*0Sstevel@tonic-gate strio.ic_len = 2;
585*0Sstevel@tonic-gate strio.ic_dp = (char *)&set;
586*0Sstevel@tonic-gate if (ioctl(sensor_fd, I_STR, &strio) < 0) {
587*0Sstevel@tonic-gate syslog(LOG_ERR, SMC_GET_EXCLUSIVE_ERR);
588*0Sstevel@tonic-gate (void) close(sensor_fd);
589*0Sstevel@tonic-gate return (PICL_FAILURE);
590*0Sstevel@tonic-gate }
591*0Sstevel@tonic-gate
592*0Sstevel@tonic-gate cpu_sensor_geo_addr = sensor_no;
593*0Sstevel@tonic-gate /* create temperature sensor node */
594*0Sstevel@tonic-gate if ((rc = ptree_create_and_add_node(parenth, CPU_SENSOR,
595*0Sstevel@tonic-gate PICL_CLASS_TEMPERATURE_SENSOR, &sensorh)) !=
596*0Sstevel@tonic-gate PICL_SUCCESS) {
597*0Sstevel@tonic-gate (void) close(sensor_fd);
598*0Sstevel@tonic-gate return (rc);
599*0Sstevel@tonic-gate }
600*0Sstevel@tonic-gate
601*0Sstevel@tonic-gate /* create Label prop. */
602*0Sstevel@tonic-gate if ((rc = env_create_property(PICL_PTYPE_CHARSTRING, PICL_READ,
603*0Sstevel@tonic-gate PICL_PROPNAMELEN_MAX, PICL_PROP_LABEL, NULLREAD,
604*0Sstevel@tonic-gate NULLWRITE, sensorh, (picl_prophdl_t *)NULL,
605*0Sstevel@tonic-gate (char *)PICL_PROPVAL_LABEL_AMBIENT)) != PICL_SUCCESS) {
606*0Sstevel@tonic-gate (void) ptree_delete_node(sensorh);
607*0Sstevel@tonic-gate (void) ptree_destroy_node(sensorh);
608*0Sstevel@tonic-gate (void) close(sensor_fd);
609*0Sstevel@tonic-gate return (rc);
610*0Sstevel@tonic-gate }
611*0Sstevel@tonic-gate
612*0Sstevel@tonic-gate /* create the geo-addr property */
613*0Sstevel@tonic-gate if ((rc = env_create_property(PICL_PTYPE_UNSIGNED_INT,
614*0Sstevel@tonic-gate PICL_READ, sizeof (sensor_no), PICL_PROP_GEO_ADDR,
615*0Sstevel@tonic-gate NULLREAD, NULLWRITE, sensorh, (picl_prophdl_t *)NULL,
616*0Sstevel@tonic-gate &sensor_no)) != PICL_SUCCESS) {
617*0Sstevel@tonic-gate (void) ptree_delete_node(sensorh);
618*0Sstevel@tonic-gate (void) ptree_destroy_node(sensorh);
619*0Sstevel@tonic-gate (void) close(sensor_fd);
620*0Sstevel@tonic-gate return (rc);
621*0Sstevel@tonic-gate }
622*0Sstevel@tonic-gate
623*0Sstevel@tonic-gate /* read the current temp from hardware */
624*0Sstevel@tonic-gate if (env_get_sensor_reading(sensor_no, &sensor_reading) !=
625*0Sstevel@tonic-gate PICL_SUCCESS) {
626*0Sstevel@tonic-gate (void) ptree_delete_node(sensorh);
627*0Sstevel@tonic-gate (void) ptree_destroy_node(sensorh);
628*0Sstevel@tonic-gate (void) close(sensor_fd);
629*0Sstevel@tonic-gate return (PICL_FAILURE);
630*0Sstevel@tonic-gate }
631*0Sstevel@tonic-gate
632*0Sstevel@tonic-gate /* create temperature prop. */
633*0Sstevel@tonic-gate if ((rc = env_create_property(PICL_PTYPE_INT,
634*0Sstevel@tonic-gate PICL_READ + PICL_VOLATILE, sizeof (sensor_reading),
635*0Sstevel@tonic-gate PICL_PROP_TEMPERATURE, get_curr_temp,
636*0Sstevel@tonic-gate NULLWRITE, sensorh, (picl_prophdl_t *)NULL,
637*0Sstevel@tonic-gate &sensor_reading)) != PICL_SUCCESS) {
638*0Sstevel@tonic-gate (void) ptree_delete_node(sensorh);
639*0Sstevel@tonic-gate (void) ptree_destroy_node(sensorh);
640*0Sstevel@tonic-gate (void) close(sensor_fd);
641*0Sstevel@tonic-gate return (rc);
642*0Sstevel@tonic-gate }
643*0Sstevel@tonic-gate
644*0Sstevel@tonic-gate /* create the threshold properties */
645*0Sstevel@tonic-gate for (i = 0; i < NUM_OF_THRESHOLDS; i++) {
646*0Sstevel@tonic-gate if ((rc = env_create_property(PICL_PTYPE_INT,
647*0Sstevel@tonic-gate PICL_READ + PICL_WRITE + PICL_VOLATILE,
648*0Sstevel@tonic-gate sizeof (uint8_t), env_thresholds[i],
649*0Sstevel@tonic-gate get_sensor_thr, set_sensor_thr,
650*0Sstevel@tonic-gate sensorh, (picl_prophdl_t *)NULL,
651*0Sstevel@tonic-gate (void *)NULL)) != PICL_SUCCESS) {
652*0Sstevel@tonic-gate (void) ptree_delete_node(sensorh);
653*0Sstevel@tonic-gate (void) ptree_destroy_node(sensorh);
654*0Sstevel@tonic-gate (void) close(sensor_fd);
655*0Sstevel@tonic-gate return (rc);
656*0Sstevel@tonic-gate }
657*0Sstevel@tonic-gate }
658*0Sstevel@tonic-gate
659*0Sstevel@tonic-gate /* intialise the internal cache */
660*0Sstevel@tonic-gate if (env_init_temp_sensor_values(cpu_sensor_geo_addr,
661*0Sstevel@tonic-gate &temp_sensor) != PICL_SUCCESS) {
662*0Sstevel@tonic-gate (void) ptree_delete_node(sensorh);
663*0Sstevel@tonic-gate (void) ptree_destroy_node(sensorh);
664*0Sstevel@tonic-gate (void) close(sensor_fd);
665*0Sstevel@tonic-gate return (PICL_FAILURE);
666*0Sstevel@tonic-gate }
667*0Sstevel@tonic-gate
668*0Sstevel@tonic-gate /* create STATE prop. */
669*0Sstevel@tonic-gate if ((rc = env_create_property(PICL_PTYPE_CHARSTRING,
670*0Sstevel@tonic-gate PICL_READ + PICL_VOLATILE, PICL_PROPNAMELEN_MAX,
671*0Sstevel@tonic-gate PICL_PROP_CONDITION, get_sensor_condition, NULLWRITE,
672*0Sstevel@tonic-gate sensorh, (picl_prophdl_t *)NULL,
673*0Sstevel@tonic-gate temp_sensor.state)) != PICL_SUCCESS) {
674*0Sstevel@tonic-gate (void) ptree_delete_node(sensorh);
675*0Sstevel@tonic-gate (void) ptree_destroy_node(sensorh);
676*0Sstevel@tonic-gate (void) close(sensor_fd);
677*0Sstevel@tonic-gate return (rc);
678*0Sstevel@tonic-gate }
679*0Sstevel@tonic-gate
680*0Sstevel@tonic-gate /* start temperature monitoring thread */
681*0Sstevel@tonic-gate if (pthread_create(&env_temp_thr_tid, NULL,
682*0Sstevel@tonic-gate &env_temp_monitor, NULL) != 0) {
683*0Sstevel@tonic-gate syslog(LOG_ERR, gettext("SUNW_envmond:Error in "
684*0Sstevel@tonic-gate "creating temperature monitor thread"));
685*0Sstevel@tonic-gate }
686*0Sstevel@tonic-gate
687*0Sstevel@tonic-gate /* enable sensor-event */
688*0Sstevel@tonic-gate (void) sensor_event_enable_set(sensor_no, B_TRUE);
689*0Sstevel@tonic-gate return (PICL_SUCCESS);
690*0Sstevel@tonic-gate }
691*0Sstevel@tonic-gate
692*0Sstevel@tonic-gate /*
693*0Sstevel@tonic-gate * handles the sensor events (post corresponding Condition picl event)
694*0Sstevel@tonic-gate */
695*0Sstevel@tonic-gate void
env_handle_sensor_event(void * res_datap)696*0Sstevel@tonic-gate env_handle_sensor_event(void *res_datap)
697*0Sstevel@tonic-gate {
698*0Sstevel@tonic-gate uint8_t offset;
699*0Sstevel@tonic-gate char sensor_cond[BUF_SIZE];
700*0Sstevel@tonic-gate
701*0Sstevel@tonic-gate if (BYTE_4(res_datap) != cpu_sensor_geo_addr) {
702*0Sstevel@tonic-gate return;
703*0Sstevel@tonic-gate }
704*0Sstevel@tonic-gate
705*0Sstevel@tonic-gate if (BYTE_5(res_datap) != THRESHOLD_TYPE) {
706*0Sstevel@tonic-gate return;
707*0Sstevel@tonic-gate }
708*0Sstevel@tonic-gate
709*0Sstevel@tonic-gate if (env_debug & DEBUG) {
710*0Sstevel@tonic-gate syslog(LOG_INFO, "Temperature = %d\n", BYTE_7(res_datap));
711*0Sstevel@tonic-gate syslog(LOG_INFO,
712*0Sstevel@tonic-gate "Threshold changed to %d\n", BYTE_8(res_datap));
713*0Sstevel@tonic-gate }
714*0Sstevel@tonic-gate
715*0Sstevel@tonic-gate /* Threshold event */
716*0Sstevel@tonic-gate offset = BYTE_6(res_datap) & 0x0F; /* first 4 bits */
717*0Sstevel@tonic-gate switch (offset) {
718*0Sstevel@tonic-gate case 0:
719*0Sstevel@tonic-gate (void) pthread_mutex_lock(&env_temp_monitor_mutex);
720*0Sstevel@tonic-gate if (env_curr_state == LOW_WARNING_THRESHOLD) {
721*0Sstevel@tonic-gate (void) pthread_cond_signal(&env_temp_monitor_cv);
722*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&env_temp_monitor_mutex);
723*0Sstevel@tonic-gate return;
724*0Sstevel@tonic-gate }
725*0Sstevel@tonic-gate env_curr_state = LOW_WARNING_THRESHOLD;
726*0Sstevel@tonic-gate (void) pthread_cond_signal(&env_temp_monitor_cv);
727*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&env_temp_monitor_mutex);
728*0Sstevel@tonic-gate (void) strncpy(sensor_cond,
729*0Sstevel@tonic-gate PICLEVENTARGVAL_SENSOR_COND_WARNING,
730*0Sstevel@tonic-gate sizeof (sensor_cond));
731*0Sstevel@tonic-gate syslog(LOG_CRIT, gettext("SUNW_envmond:current temperature (%d)"
732*0Sstevel@tonic-gate " is below lower warning temperature (%d).\n"),
733*0Sstevel@tonic-gate BYTE_7(res_datap), BYTE_8(res_datap));
734*0Sstevel@tonic-gate break;
735*0Sstevel@tonic-gate case 2:
736*0Sstevel@tonic-gate (void) pthread_mutex_lock(&env_temp_monitor_mutex);
737*0Sstevel@tonic-gate if (env_curr_state == LOW_SHUTDOWN_THRESHOLD) {
738*0Sstevel@tonic-gate (void) pthread_cond_signal(&env_temp_monitor_cv);
739*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&env_temp_monitor_mutex);
740*0Sstevel@tonic-gate return;
741*0Sstevel@tonic-gate }
742*0Sstevel@tonic-gate env_curr_state = LOW_SHUTDOWN_THRESHOLD;
743*0Sstevel@tonic-gate (void) pthread_cond_signal(&env_temp_monitor_cv);
744*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&env_temp_monitor_mutex);
745*0Sstevel@tonic-gate (void) strncpy(sensor_cond,
746*0Sstevel@tonic-gate PICLEVENTARGVAL_SENSOR_COND_SHUTDOWN,
747*0Sstevel@tonic-gate sizeof (sensor_cond));
748*0Sstevel@tonic-gate syslog(LOG_CRIT, gettext("SUNW_envmond:current temperature (%d)"
749*0Sstevel@tonic-gate " is below lower critical temperature (%d).\n"),
750*0Sstevel@tonic-gate BYTE_7(res_datap), BYTE_8(res_datap));
751*0Sstevel@tonic-gate break;
752*0Sstevel@tonic-gate case 7:
753*0Sstevel@tonic-gate (void) pthread_mutex_lock(&env_temp_monitor_mutex);
754*0Sstevel@tonic-gate if (env_curr_state == HIGH_WARNING_THRESHOLD) {
755*0Sstevel@tonic-gate (void) pthread_cond_signal(&env_temp_monitor_cv);
756*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&env_temp_monitor_mutex);
757*0Sstevel@tonic-gate return;
758*0Sstevel@tonic-gate }
759*0Sstevel@tonic-gate env_curr_state = HIGH_WARNING_THRESHOLD;
760*0Sstevel@tonic-gate (void) pthread_cond_signal(&env_temp_monitor_cv);
761*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&env_temp_monitor_mutex);
762*0Sstevel@tonic-gate (void) strncpy(sensor_cond,
763*0Sstevel@tonic-gate PICLEVENTARGVAL_SENSOR_COND_WARNING,
764*0Sstevel@tonic-gate sizeof (sensor_cond));
765*0Sstevel@tonic-gate syslog(LOG_CRIT, gettext("SUNW_envmond:current temperature (%d)"
766*0Sstevel@tonic-gate " exceeds upper warning temperature (%d).\n"),
767*0Sstevel@tonic-gate BYTE_7(res_datap), BYTE_8(res_datap));
768*0Sstevel@tonic-gate break;
769*0Sstevel@tonic-gate case 9:
770*0Sstevel@tonic-gate (void) pthread_mutex_lock(&env_temp_monitor_mutex);
771*0Sstevel@tonic-gate if (env_curr_state == HIGH_SHUTDOWN_THRESHOLD) {
772*0Sstevel@tonic-gate (void) pthread_cond_signal(&env_temp_monitor_cv);
773*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&env_temp_monitor_mutex);
774*0Sstevel@tonic-gate return;
775*0Sstevel@tonic-gate }
776*0Sstevel@tonic-gate env_curr_state = HIGH_SHUTDOWN_THRESHOLD;
777*0Sstevel@tonic-gate (void) pthread_cond_signal(&env_temp_monitor_cv);
778*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&env_temp_monitor_mutex);
779*0Sstevel@tonic-gate (void) strncpy(sensor_cond,
780*0Sstevel@tonic-gate PICLEVENTARGVAL_SENSOR_COND_SHUTDOWN,
781*0Sstevel@tonic-gate sizeof (sensor_cond));
782*0Sstevel@tonic-gate syslog(LOG_CRIT, gettext("SUNW_envmond:current temperature (%d)"
783*0Sstevel@tonic-gate " exceeds upper critical temperature (%d).\n"),
784*0Sstevel@tonic-gate BYTE_7(res_datap), BYTE_8(res_datap));
785*0Sstevel@tonic-gate break;
786*0Sstevel@tonic-gate default:
787*0Sstevel@tonic-gate (void) strncpy(sensor_cond, PICLEVENTARGVAL_UNKNOWN,
788*0Sstevel@tonic-gate sizeof (sensor_cond));
789*0Sstevel@tonic-gate break;
790*0Sstevel@tonic-gate }
791*0Sstevel@tonic-gate
792*0Sstevel@tonic-gate if (post_sensor_event(sensorh, sensor_cond, NO_COND_TIMEDWAIT)
793*0Sstevel@tonic-gate != PICL_SUCCESS) {
794*0Sstevel@tonic-gate syslog(LOG_ERR, gettext("SUNW_envmond:Error in posting "
795*0Sstevel@tonic-gate "%s event"), PICLEVENT_CONDITION_CHANGE);
796*0Sstevel@tonic-gate }
797*0Sstevel@tonic-gate }
798*0Sstevel@tonic-gate
799*0Sstevel@tonic-gate /*
800*0Sstevel@tonic-gate * this thread monitors the temperature when the current temperature
801*0Sstevel@tonic-gate * raises above high warning threshold
802*0Sstevel@tonic-gate */
803*0Sstevel@tonic-gate /*ARGSUSED*/
804*0Sstevel@tonic-gate static void *
env_temp_monitor(void * args)805*0Sstevel@tonic-gate env_temp_monitor(void *args)
806*0Sstevel@tonic-gate {
807*0Sstevel@tonic-gate int ret;
808*0Sstevel@tonic-gate timespec_t to;
809*0Sstevel@tonic-gate int8_t sensor_reading;
810*0Sstevel@tonic-gate char sensor_cond[BUF_SIZE];
811*0Sstevel@tonic-gate
812*0Sstevel@tonic-gate (void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
813*0Sstevel@tonic-gate (void) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
814*0Sstevel@tonic-gate
815*0Sstevel@tonic-gate for (;;) {
816*0Sstevel@tonic-gate (void) pthread_mutex_lock(&env_temp_monitor_mutex);
817*0Sstevel@tonic-gate if (env_curr_state == NORMAL_THRESHOLD) {
818*0Sstevel@tonic-gate pthread_cond_wait(&env_temp_monitor_cv,
819*0Sstevel@tonic-gate &env_temp_monitor_mutex);
820*0Sstevel@tonic-gate }
821*0Sstevel@tonic-gate
822*0Sstevel@tonic-gate /* check until temp drops below warning threshold */
823*0Sstevel@tonic-gate to.tv_sec = ENV_TEMP_MONITOR_TIME;
824*0Sstevel@tonic-gate to.tv_nsec = 0;
825*0Sstevel@tonic-gate ret = pthread_cond_reltimedwait_np(&env_temp_monitor_cv,
826*0Sstevel@tonic-gate &env_temp_monitor_mutex, &to);
827*0Sstevel@tonic-gate if (ret != ETIMEDOUT) {
828*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&env_temp_monitor_mutex);
829*0Sstevel@tonic-gate continue;
830*0Sstevel@tonic-gate }
831*0Sstevel@tonic-gate
832*0Sstevel@tonic-gate /* read the present temperature */
833*0Sstevel@tonic-gate if (env_get_sensor_reading(cpu_sensor_geo_addr,
834*0Sstevel@tonic-gate &sensor_reading) != PICL_SUCCESS) {
835*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&env_temp_monitor_mutex);
836*0Sstevel@tonic-gate continue;
837*0Sstevel@tonic-gate }
838*0Sstevel@tonic-gate
839*0Sstevel@tonic-gate (void) pthread_mutex_lock(&sensor_mutex);
840*0Sstevel@tonic-gate if (sensor_reading < temp_sensor.hi_warning &&
841*0Sstevel@tonic-gate sensor_reading > temp_sensor.lo_warning) {
842*0Sstevel@tonic-gate /* temperature is ok now */
843*0Sstevel@tonic-gate (void) strncpy(sensor_cond, PICLEVENTARGVAL_OK,
844*0Sstevel@tonic-gate sizeof (sensor_cond));
845*0Sstevel@tonic-gate env_curr_state = NORMAL_THRESHOLD;
846*0Sstevel@tonic-gate }
847*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&sensor_mutex);
848*0Sstevel@tonic-gate
849*0Sstevel@tonic-gate if (env_curr_state == NORMAL_THRESHOLD) {
850*0Sstevel@tonic-gate syslog(LOG_NOTICE, gettext("SUNW_envmond:Current "
851*0Sstevel@tonic-gate "temperature is ok now"));
852*0Sstevel@tonic-gate if (post_sensor_event(sensorh, sensor_cond,
853*0Sstevel@tonic-gate NO_COND_TIMEDWAIT) != PICL_SUCCESS) {
854*0Sstevel@tonic-gate syslog(LOG_ERR, gettext("SUNW_envmond:Error in"
855*0Sstevel@tonic-gate " posting %s event"),
856*0Sstevel@tonic-gate PICLEVENT_CONDITION_CHANGE);
857*0Sstevel@tonic-gate }
858*0Sstevel@tonic-gate }
859*0Sstevel@tonic-gate (void) pthread_mutex_unlock(&env_temp_monitor_mutex);
860*0Sstevel@tonic-gate }
861*0Sstevel@tonic-gate /*NOTREACHED*/
862*0Sstevel@tonic-gate return (NULL);
863*0Sstevel@tonic-gate }
864