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 (c) 2000 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
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 * This file contains the environmental daemon module.
31*0Sstevel@tonic-gate */
32*0Sstevel@tonic-gate
33*0Sstevel@tonic-gate
34*0Sstevel@tonic-gate /*
35*0Sstevel@tonic-gate * Grover system contains one temperature device, MAX1617, which consists
36*0Sstevel@tonic-gate * of two sensors: CPU die and CPU ambient. Each sensor is represented
37*0Sstevel@tonic-gate * as a different minor device and the current temperature is read via an
38*0Sstevel@tonic-gate * I2C_GET_TEMPERATURE ioctl call to the max1617 driver. Additionally, the
39*0Sstevel@tonic-gate * MAX1617 device supports both a low and high temperature limit, which
40*0Sstevel@tonic-gate * can trigger an alert condition, causing power supply to turn off.
41*0Sstevel@tonic-gate *
42*0Sstevel@tonic-gate * The environmental daemon defines the following thresholds per sensor:
43*0Sstevel@tonic-gate *
44*0Sstevel@tonic-gate * high_power_off high hard shutdown
45*0Sstevel@tonic-gate * high_shutdown high soft shutdown limit
46*0Sstevel@tonic-gate * high_warning high warning limit
47*0Sstevel@tonic-gate * low_warning low warning limit
48*0Sstevel@tonic-gate * low_shutdown low soft shutdown limit
49*0Sstevel@tonic-gate * low_power_off low hard shutdown limit
50*0Sstevel@tonic-gate *
51*0Sstevel@tonic-gate * Except for the low_power_off and high_power_off limits, all other threshold
52*0Sstevel@tonic-gate * values can be changed via "piclenvd.conf" configuration file.
53*0Sstevel@tonic-gate *
54*0Sstevel@tonic-gate * Environmental monitoring is done by the "envthr" thread. It periodically
55*0Sstevel@tonic-gate * monitors both CPU die and CPU ambient temperatures and takes appropriate
56*0Sstevel@tonic-gate * action depending upon the current temperature and threshold values for
57*0Sstevel@tonic-gate * that sensor. If the temperature reaches the high_shutdown limit or the
58*0Sstevel@tonic-gate * low_shutdown limit, and remains there for over shutdown_interval seconds,
59*0Sstevel@tonic-gate * it forces a graceful system shutdown via tuneable shutdown_cmd string
60*0Sstevel@tonic-gate * variable. Otherwise, if the temperature reaches the high_warning limit
61*0Sstevel@tonic-gate * or the low_warning limit, it logs and prints a message on the console.
62*0Sstevel@tonic-gate * This message will be printed at most at "warning_interval" seconds
63*0Sstevel@tonic-gate * interval, which is also a tuneable variable.
64*0Sstevel@tonic-gate *
65*0Sstevel@tonic-gate * Grover system also contains a fan, known as system fan, which can be turned
66*0Sstevel@tonic-gate * ON or OFF under software control. However, its speed is automatically
67*0Sstevel@tonic-gate * controlled by the hardware based upon the ambient temperature. When in EStar
68*0Sstevel@tonic-gate * mode (i.e. lowest power state), the environmental daemon will turn off this
69*0Sstevel@tonic-gate * fan provided the CPU die and ambient temperature is below the high warning
70*0Sstevel@tonic-gate * limits.
71*0Sstevel@tonic-gate *
72*0Sstevel@tonic-gate * The power state monitoring is done by the "pmthr" thread. It uses the
73*0Sstevel@tonic-gate * PM_GET_STATE_CHANGE and PM_GET_STATE_CHANGE_WAIT ioctl commands to pick
74*0Sstevel@tonic-gate * up any power state change events. It processes all queued power state
75*0Sstevel@tonic-gate * change events and determines the curret lowest power state and saves it
76*0Sstevel@tonic-gate * in cur_lpstate variable. Whenever this state changes from the previous
77*0Sstevel@tonic-gate * lowest power state (saved in prev_lpstate), it wakes up the "envtrh"
78*0Sstevel@tonic-gate * thread.
79*0Sstevel@tonic-gate *
80*0Sstevel@tonic-gate * The "lpstate_lock" mutex and "lpstate_cond" condition variables are used
81*0Sstevel@tonic-gate * to communicate power state change events from the "pmthr" to the "envthr"
82*0Sstevel@tonic-gate * thread. The "envthr" thread uses the pthread_cond_timedwait() interface
83*0Sstevel@tonic-gate * to wait for any power state change notifications. The "pmthr" uses the
84*0Sstevel@tonic-gate * pthread_signal() interface to wake up the "envthr" thread.
85*0Sstevel@tonic-gate */
86*0Sstevel@tonic-gate
87*0Sstevel@tonic-gate #include <stdio.h>
88*0Sstevel@tonic-gate #include <stdlib.h>
89*0Sstevel@tonic-gate #include <string.h>
90*0Sstevel@tonic-gate #include <stdarg.h>
91*0Sstevel@tonic-gate #include <unistd.h>
92*0Sstevel@tonic-gate #include <limits.h>
93*0Sstevel@tonic-gate #include <syslog.h>
94*0Sstevel@tonic-gate #include <errno.h>
95*0Sstevel@tonic-gate #include <fcntl.h>
96*0Sstevel@tonic-gate #include <picl.h>
97*0Sstevel@tonic-gate #include <picltree.h>
98*0Sstevel@tonic-gate #include <pthread.h>
99*0Sstevel@tonic-gate #include <sys/pm.h>
100*0Sstevel@tonic-gate #include <sys/open.h>
101*0Sstevel@tonic-gate #include <sys/time.h>
102*0Sstevel@tonic-gate #include <sys/utsname.h>
103*0Sstevel@tonic-gate #include <sys/systeminfo.h>
104*0Sstevel@tonic-gate #include <sys/i2c/clients/max1617.h>
105*0Sstevel@tonic-gate #include <sys/i2c/clients/i2c_client.h>
106*0Sstevel@tonic-gate #include "envd.h"
107*0Sstevel@tonic-gate
108*0Sstevel@tonic-gate
109*0Sstevel@tonic-gate /*
110*0Sstevel@tonic-gate * PICL plguin
111*0Sstevel@tonic-gate */
112*0Sstevel@tonic-gate static void piclenvd_register(void);
113*0Sstevel@tonic-gate static void piclenvd_init(void);
114*0Sstevel@tonic-gate static void piclenvd_fini(void);
115*0Sstevel@tonic-gate extern void env_picl_setup();
116*0Sstevel@tonic-gate
117*0Sstevel@tonic-gate #pragma init(piclenvd_register)
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate static picld_plugin_reg_t my_reg_info = {
120*0Sstevel@tonic-gate PICLD_PLUGIN_VERSION_1,
121*0Sstevel@tonic-gate PICLD_PLUGIN_CRITICAL,
122*0Sstevel@tonic-gate "SUNW_piclenvd",
123*0Sstevel@tonic-gate piclenvd_init,
124*0Sstevel@tonic-gate piclenvd_fini,
125*0Sstevel@tonic-gate };
126*0Sstevel@tonic-gate
127*0Sstevel@tonic-gate /*
128*0Sstevel@tonic-gate * tuneable variables
129*0Sstevel@tonic-gate */
130*0Sstevel@tonic-gate int env_debug;
131*0Sstevel@tonic-gate static int sensor_poll_interval = SENSOR_POLL_INTERVAL;
132*0Sstevel@tonic-gate static int warning_interval = WARNING_INTERVAL;
133*0Sstevel@tonic-gate static int shutdown_interval = SHUTDOWN_INTERVAL;
134*0Sstevel@tonic-gate static char shutdown_cmd[128] = SHUTDOWN_CMD;
135*0Sstevel@tonic-gate static int monitor_temperature = 0;
136*0Sstevel@tonic-gate
137*0Sstevel@tonic-gate static sensor_thresh_t cpu_die_thresh = {
138*0Sstevel@tonic-gate CPU_DIE_LOW_POWER_OFF, CPU_DIE_HIGH_POWER_OFF,
139*0Sstevel@tonic-gate CPU_DIE_LOW_SHUTDOWN, CPU_DIE_HIGH_SHUTDOWN,
140*0Sstevel@tonic-gate CPU_DIE_LOW_WARNING, CPU_DIE_HIGH_WARNING,
141*0Sstevel@tonic-gate CPU_DIE_TARGET_TEMP
142*0Sstevel@tonic-gate };
143*0Sstevel@tonic-gate
144*0Sstevel@tonic-gate static sensor_thresh_t cpu_amb_thresh = {
145*0Sstevel@tonic-gate CPU_AMB_LOW_POWER_OFF, CPU_AMB_HIGH_POWER_OFF,
146*0Sstevel@tonic-gate CPU_AMB_LOW_SHUTDOWN, CPU_AMB_HIGH_SHUTDOWN,
147*0Sstevel@tonic-gate CPU_AMB_LOW_WARNING, CPU_AMB_HIGH_WARNING,
148*0Sstevel@tonic-gate CPU_AMB_TARGET_TEMP
149*0Sstevel@tonic-gate };
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate /*
152*0Sstevel@tonic-gate * Temperature sensors
153*0Sstevel@tonic-gate */
154*0Sstevel@tonic-gate
155*0Sstevel@tonic-gate static env_sensor_t cpu_die_sensor =
156*0Sstevel@tonic-gate { SENSOR_CPU_DIE, CPU_DIE_SENSOR_DEVFS, &cpu_die_thresh};
157*0Sstevel@tonic-gate
158*0Sstevel@tonic-gate static env_sensor_t cpu_amb_sensor =
159*0Sstevel@tonic-gate { SENSOR_CPU_AMB, CPU_AMB_SENSOR_DEVFS, &cpu_amb_thresh};
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate
162*0Sstevel@tonic-gate static env_sensor_t *envd_sensors[] = {
163*0Sstevel@tonic-gate &cpu_die_sensor,
164*0Sstevel@tonic-gate &cpu_amb_sensor,
165*0Sstevel@tonic-gate NULL
166*0Sstevel@tonic-gate };
167*0Sstevel@tonic-gate
168*0Sstevel@tonic-gate /*
169*0Sstevel@tonic-gate * Fan devices
170*0Sstevel@tonic-gate */
171*0Sstevel@tonic-gate static env_fan_t envd_system_fan = {
172*0Sstevel@tonic-gate ENV_SYSTEM_FAN, ENV_SYSTEM_FAN_DEVFS,
173*0Sstevel@tonic-gate SYSTEM_FAN_SPEED_MIN, SYSTEM_FAN_SPEED_MAX,
174*0Sstevel@tonic-gate };
175*0Sstevel@tonic-gate
176*0Sstevel@tonic-gate static env_fan_t *envd_fans[] = {
177*0Sstevel@tonic-gate &envd_system_fan,
178*0Sstevel@tonic-gate NULL
179*0Sstevel@tonic-gate };
180*0Sstevel@tonic-gate
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate /*
183*0Sstevel@tonic-gate * Environmental thread variables
184*0Sstevel@tonic-gate */
185*0Sstevel@tonic-gate static boolean_t envd_inited = B_FALSE;
186*0Sstevel@tonic-gate static boolean_t system_shutdown_started;
187*0Sstevel@tonic-gate static boolean_t envthr_created; /* envthr created */
188*0Sstevel@tonic-gate static pthread_t envthr_tid; /* envthr thread ID */
189*0Sstevel@tonic-gate static pthread_attr_t thr_attr;
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate /*
192*0Sstevel@tonic-gate * Power management thread (pmthr) variables
193*0Sstevel@tonic-gate */
194*0Sstevel@tonic-gate static pthread_t pmthr_tid; /* pmthr thread ID */
195*0Sstevel@tonic-gate static int pmthr_created; /* pmthr created */
196*0Sstevel@tonic-gate static int pm_fd; /* PM device file descriptor */
197*0Sstevel@tonic-gate static int cur_lpstate; /* cur low power state */
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gate static pthread_mutex_t lpstate_lock; /* low power state lock */
200*0Sstevel@tonic-gate static pthread_cond_t lpstate_cond; /* low power state condvar */
201*0Sstevel@tonic-gate
202*0Sstevel@tonic-gate
203*0Sstevel@tonic-gate /*
204*0Sstevel@tonic-gate * Tuneable variables data structure/array
205*0Sstevel@tonic-gate */
206*0Sstevel@tonic-gate
207*0Sstevel@tonic-gate typedef struct {
208*0Sstevel@tonic-gate char *name; /* keyword */
209*0Sstevel@tonic-gate void *addr; /* memory (variable) address */
210*0Sstevel@tonic-gate int type; /* keyword type */
211*0Sstevel@tonic-gate int size; /* variable size */
212*0Sstevel@tonic-gate } env_tuneable_t;
213*0Sstevel@tonic-gate
214*0Sstevel@tonic-gate /* keyword types */
215*0Sstevel@tonic-gate #define KTYPE_INT 1 /* signed int */
216*0Sstevel@tonic-gate #define KTYPE_STRING 2 /* string in double quotes */
217*0Sstevel@tonic-gate
218*0Sstevel@tonic-gate static env_tuneable_t env_tuneables[] = {
219*0Sstevel@tonic-gate {"cpu_amb_low_shutdown", &cpu_amb_thresh.low_shutdown, KTYPE_INT,
220*0Sstevel@tonic-gate sizeof (tempr_t)},
221*0Sstevel@tonic-gate {"cpu_amb_low_warning", &cpu_amb_thresh.low_warning, KTYPE_INT,
222*0Sstevel@tonic-gate sizeof (tempr_t)},
223*0Sstevel@tonic-gate {"cpu_amb_target_temp", &cpu_amb_thresh.target_temp, KTYPE_INT,
224*0Sstevel@tonic-gate sizeof (tempr_t)},
225*0Sstevel@tonic-gate {"cpu_amb_high_shutdown", &cpu_amb_thresh.high_shutdown, KTYPE_INT,
226*0Sstevel@tonic-gate sizeof (tempr_t)},
227*0Sstevel@tonic-gate {"cpu_amb_high_warning", &cpu_amb_thresh.high_warning, KTYPE_INT,
228*0Sstevel@tonic-gate sizeof (tempr_t)},
229*0Sstevel@tonic-gate {"cpu_die_low_shutdown", &cpu_die_thresh.low_shutdown, KTYPE_INT,
230*0Sstevel@tonic-gate sizeof (tempr_t)},
231*0Sstevel@tonic-gate {"cpu_die_low_warning", &cpu_die_thresh.low_warning, KTYPE_INT,
232*0Sstevel@tonic-gate sizeof (tempr_t)},
233*0Sstevel@tonic-gate {"cpu_die_target_temp", &cpu_die_thresh.target_temp, KTYPE_INT,
234*0Sstevel@tonic-gate sizeof (tempr_t)},
235*0Sstevel@tonic-gate {"cpu_die_high_shutdown", &cpu_die_thresh.high_shutdown, KTYPE_INT,
236*0Sstevel@tonic-gate sizeof (tempr_t)},
237*0Sstevel@tonic-gate {"cpu_die_high_warning", &cpu_die_thresh.high_warning, KTYPE_INT,
238*0Sstevel@tonic-gate sizeof (tempr_t)},
239*0Sstevel@tonic-gate {"sensor_poll_interval", &sensor_poll_interval, KTYPE_INT,
240*0Sstevel@tonic-gate sizeof (sensor_poll_interval)},
241*0Sstevel@tonic-gate {"monitor_temperature", &monitor_temperature, KTYPE_INT,
242*0Sstevel@tonic-gate sizeof (monitor_temperature)},
243*0Sstevel@tonic-gate {"warning_interval", &warning_interval, KTYPE_INT,
244*0Sstevel@tonic-gate sizeof (warning_interval)},
245*0Sstevel@tonic-gate {"shutdown_interval", &shutdown_interval, KTYPE_INT,
246*0Sstevel@tonic-gate sizeof (shutdown_interval)},
247*0Sstevel@tonic-gate {"shutdown_cmd", &shutdown_cmd[0], KTYPE_STRING, sizeof (shutdown_cmd)},
248*0Sstevel@tonic-gate {"env_debug", &env_debug, KTYPE_INT, sizeof (env_debug)},
249*0Sstevel@tonic-gate { NULL, NULL, 0, 0}
250*0Sstevel@tonic-gate };
251*0Sstevel@tonic-gate
252*0Sstevel@tonic-gate /*
253*0Sstevel@tonic-gate * Lookup fan and return a pointer to env_fan_t data structure.
254*0Sstevel@tonic-gate */
255*0Sstevel@tonic-gate env_fan_t *
fan_lookup(char * name)256*0Sstevel@tonic-gate fan_lookup(char *name)
257*0Sstevel@tonic-gate {
258*0Sstevel@tonic-gate int i;
259*0Sstevel@tonic-gate env_fan_t *fanp;
260*0Sstevel@tonic-gate
261*0Sstevel@tonic-gate for (i = 0; (fanp = envd_fans[i]) != NULL; i++) {
262*0Sstevel@tonic-gate if (strcmp(fanp->name, name) == 0)
263*0Sstevel@tonic-gate return (fanp);
264*0Sstevel@tonic-gate }
265*0Sstevel@tonic-gate return (NULL);
266*0Sstevel@tonic-gate }
267*0Sstevel@tonic-gate
268*0Sstevel@tonic-gate /*
269*0Sstevel@tonic-gate * Lookup sensor and return a pointer to env_sensor_t data structure.
270*0Sstevel@tonic-gate */
271*0Sstevel@tonic-gate env_sensor_t *
sensor_lookup(char * name)272*0Sstevel@tonic-gate sensor_lookup(char *name)
273*0Sstevel@tonic-gate {
274*0Sstevel@tonic-gate int i;
275*0Sstevel@tonic-gate env_sensor_t *sensorp;
276*0Sstevel@tonic-gate
277*0Sstevel@tonic-gate for (i = 0; (sensorp = envd_sensors[i]) != NULL; i++) {
278*0Sstevel@tonic-gate if (strcmp(sensorp->name, name) == 0)
279*0Sstevel@tonic-gate return (sensorp);
280*0Sstevel@tonic-gate }
281*0Sstevel@tonic-gate return (NULL);
282*0Sstevel@tonic-gate }
283*0Sstevel@tonic-gate
284*0Sstevel@tonic-gate /*
285*0Sstevel@tonic-gate * Get current temperature
286*0Sstevel@tonic-gate * Returns -1 on error, 0 if successful
287*0Sstevel@tonic-gate */
288*0Sstevel@tonic-gate int
get_temperature(env_sensor_t * sensorp,tempr_t * temp)289*0Sstevel@tonic-gate get_temperature(env_sensor_t *sensorp, tempr_t *temp)
290*0Sstevel@tonic-gate {
291*0Sstevel@tonic-gate int fd = sensorp->fd;
292*0Sstevel@tonic-gate int retval = 0;
293*0Sstevel@tonic-gate
294*0Sstevel@tonic-gate if (fd == -1)
295*0Sstevel@tonic-gate retval = -1;
296*0Sstevel@tonic-gate else if (ioctl(fd, I2C_GET_TEMPERATURE, temp) == -1) {
297*0Sstevel@tonic-gate retval = -1;
298*0Sstevel@tonic-gate if (sensorp->error == 0) {
299*0Sstevel@tonic-gate sensorp->error = 1;
300*0Sstevel@tonic-gate envd_log(LOG_WARNING, ENV_SENSOR_ACCESS_FAIL,
301*0Sstevel@tonic-gate sensorp->name, errno, strerror(errno));
302*0Sstevel@tonic-gate }
303*0Sstevel@tonic-gate } else if (sensorp->error != 0) {
304*0Sstevel@tonic-gate sensorp->error = 0;
305*0Sstevel@tonic-gate envd_log(LOG_WARNING, ENV_SENSOR_ACCESS_OK, sensorp->name);
306*0Sstevel@tonic-gate }
307*0Sstevel@tonic-gate
308*0Sstevel@tonic-gate return (retval);
309*0Sstevel@tonic-gate }
310*0Sstevel@tonic-gate
311*0Sstevel@tonic-gate /*
312*0Sstevel@tonic-gate * Get current fan speed
313*0Sstevel@tonic-gate * Returns -1 on error, 0 if successful
314*0Sstevel@tonic-gate */
315*0Sstevel@tonic-gate int
get_fan_speed(env_fan_t * fanp,fanspeed_t * fanspeedp)316*0Sstevel@tonic-gate get_fan_speed(env_fan_t *fanp, fanspeed_t *fanspeedp)
317*0Sstevel@tonic-gate {
318*0Sstevel@tonic-gate int fan_fd;
319*0Sstevel@tonic-gate int retval = 0;
320*0Sstevel@tonic-gate
321*0Sstevel@tonic-gate fan_fd = fanp->fd;
322*0Sstevel@tonic-gate if (fan_fd == -1 || read(fan_fd, fanspeedp, sizeof (fanspeed_t)) !=
323*0Sstevel@tonic-gate sizeof (fanspeed_t))
324*0Sstevel@tonic-gate retval = -1;
325*0Sstevel@tonic-gate return (retval);
326*0Sstevel@tonic-gate }
327*0Sstevel@tonic-gate
328*0Sstevel@tonic-gate /*
329*0Sstevel@tonic-gate * Set fan speed
330*0Sstevel@tonic-gate * Returns -1 on error, 0 if successful
331*0Sstevel@tonic-gate */
332*0Sstevel@tonic-gate static int
set_fan_speed(env_fan_t * fanp,fanspeed_t fanspeed)333*0Sstevel@tonic-gate set_fan_speed(env_fan_t *fanp, fanspeed_t fanspeed)
334*0Sstevel@tonic-gate {
335*0Sstevel@tonic-gate int fan_fd;
336*0Sstevel@tonic-gate int retval = 0;
337*0Sstevel@tonic-gate
338*0Sstevel@tonic-gate fan_fd = fanp->fd;
339*0Sstevel@tonic-gate if (fan_fd == -1 || write(fan_fd, &fanspeed, sizeof (fanspeed)) !=
340*0Sstevel@tonic-gate sizeof (fanspeed_t))
341*0Sstevel@tonic-gate retval = -1;
342*0Sstevel@tonic-gate return (retval);
343*0Sstevel@tonic-gate }
344*0Sstevel@tonic-gate
345*0Sstevel@tonic-gate
346*0Sstevel@tonic-gate /*
347*0Sstevel@tonic-gate * close all fan devices
348*0Sstevel@tonic-gate */
349*0Sstevel@tonic-gate static void
envd_close_fans(void)350*0Sstevel@tonic-gate envd_close_fans(void)
351*0Sstevel@tonic-gate {
352*0Sstevel@tonic-gate int i;
353*0Sstevel@tonic-gate env_fan_t *fanp;
354*0Sstevel@tonic-gate
355*0Sstevel@tonic-gate for (i = 0; (fanp = envd_fans[i]) != NULL; i++) {
356*0Sstevel@tonic-gate if (fanp->fd != -1) {
357*0Sstevel@tonic-gate (void) close(fanp->fd);
358*0Sstevel@tonic-gate fanp->fd = -1;
359*0Sstevel@tonic-gate }
360*0Sstevel@tonic-gate }
361*0Sstevel@tonic-gate }
362*0Sstevel@tonic-gate
363*0Sstevel@tonic-gate /*
364*0Sstevel@tonic-gate * Close sensor devices
365*0Sstevel@tonic-gate */
366*0Sstevel@tonic-gate static void
envd_close_sensors(void)367*0Sstevel@tonic-gate envd_close_sensors(void)
368*0Sstevel@tonic-gate {
369*0Sstevel@tonic-gate int i;
370*0Sstevel@tonic-gate env_sensor_t *sensorp;
371*0Sstevel@tonic-gate
372*0Sstevel@tonic-gate for (i = 0; (sensorp = envd_sensors[i]) != NULL; i++) {
373*0Sstevel@tonic-gate if (sensorp->fd != -1) {
374*0Sstevel@tonic-gate (void) close(sensorp->fd);
375*0Sstevel@tonic-gate sensorp->fd = -1;
376*0Sstevel@tonic-gate }
377*0Sstevel@tonic-gate }
378*0Sstevel@tonic-gate }
379*0Sstevel@tonic-gate
380*0Sstevel@tonic-gate /*
381*0Sstevel@tonic-gate * Close PM device
382*0Sstevel@tonic-gate */
383*0Sstevel@tonic-gate static void
envd_close_pm(void)384*0Sstevel@tonic-gate envd_close_pm(void)
385*0Sstevel@tonic-gate {
386*0Sstevel@tonic-gate if (pm_fd != -1) {
387*0Sstevel@tonic-gate (void) close(pm_fd);
388*0Sstevel@tonic-gate pm_fd = -1;
389*0Sstevel@tonic-gate }
390*0Sstevel@tonic-gate }
391*0Sstevel@tonic-gate
392*0Sstevel@tonic-gate /*
393*0Sstevel@tonic-gate * Open fan devices and initialize per fan data structure.
394*0Sstevel@tonic-gate * Returns #fans found.
395*0Sstevel@tonic-gate */
396*0Sstevel@tonic-gate static int
envd_setup_fans(void)397*0Sstevel@tonic-gate envd_setup_fans(void)
398*0Sstevel@tonic-gate {
399*0Sstevel@tonic-gate int i, fd;
400*0Sstevel@tonic-gate fanspeed_t speed;
401*0Sstevel@tonic-gate env_fan_t *fanp;
402*0Sstevel@tonic-gate char path[FILENAME_MAX];
403*0Sstevel@tonic-gate int fancnt = 0;
404*0Sstevel@tonic-gate
405*0Sstevel@tonic-gate for (i = 0; (fanp = envd_fans[i]) != NULL; i++) {
406*0Sstevel@tonic-gate fanp->fd = -1;
407*0Sstevel@tonic-gate fanp->cur_speed = 0;
408*0Sstevel@tonic-gate fanp->prev_speed = 0;
409*0Sstevel@tonic-gate
410*0Sstevel@tonic-gate (void) strcpy(path, "/devices");
411*0Sstevel@tonic-gate (void) strlcat(path, fanp->devfs_path, sizeof (path));
412*0Sstevel@tonic-gate fd = open(path, O_RDWR);
413*0Sstevel@tonic-gate if (fd == -1) {
414*0Sstevel@tonic-gate envd_log(LOG_WARNING, ENV_FAN_OPEN_FAIL, fanp->name,
415*0Sstevel@tonic-gate fanp->devfs_path, errno, strerror(errno));
416*0Sstevel@tonic-gate fanp->present = B_FALSE;
417*0Sstevel@tonic-gate continue;
418*0Sstevel@tonic-gate }
419*0Sstevel@tonic-gate fanp->fd = fd;
420*0Sstevel@tonic-gate fanp->present = B_TRUE;
421*0Sstevel@tonic-gate fancnt++;
422*0Sstevel@tonic-gate
423*0Sstevel@tonic-gate /*
424*0Sstevel@tonic-gate * Set cur_speed/prev_speed to current fan speed
425*0Sstevel@tonic-gate */
426*0Sstevel@tonic-gate if (get_fan_speed(fanp, &speed) == -1) {
427*0Sstevel@tonic-gate /*
428*0Sstevel@tonic-gate * The Fan driver does not know the current fan speed.
429*0Sstevel@tonic-gate * Initialize it to 50% of the max speed and reread
430*0Sstevel@tonic-gate * to get the current speed.
431*0Sstevel@tonic-gate */
432*0Sstevel@tonic-gate speed = fanp->speed_max/2;
433*0Sstevel@tonic-gate (void) set_fan_speed(fanp, speed);
434*0Sstevel@tonic-gate if (get_fan_speed(fanp, &speed) == -1)
435*0Sstevel@tonic-gate continue;
436*0Sstevel@tonic-gate }
437*0Sstevel@tonic-gate fanp->cur_speed = speed;
438*0Sstevel@tonic-gate fanp->prev_speed = speed;
439*0Sstevel@tonic-gate }
440*0Sstevel@tonic-gate return (fancnt);
441*0Sstevel@tonic-gate }
442*0Sstevel@tonic-gate
443*0Sstevel@tonic-gate /*
444*0Sstevel@tonic-gate * Open temperature sensor devices and initialize per sensor data structure.
445*0Sstevel@tonic-gate * Returns #sensors found.
446*0Sstevel@tonic-gate */
447*0Sstevel@tonic-gate static int
envd_setup_sensors(void)448*0Sstevel@tonic-gate envd_setup_sensors(void)
449*0Sstevel@tonic-gate {
450*0Sstevel@tonic-gate int i;
451*0Sstevel@tonic-gate tempr_t temp;
452*0Sstevel@tonic-gate env_sensor_t *sensorp;
453*0Sstevel@tonic-gate char path[FILENAME_MAX];
454*0Sstevel@tonic-gate int sensorcnt = 0;
455*0Sstevel@tonic-gate sensor_thresh_t *threshp;
456*0Sstevel@tonic-gate
457*0Sstevel@tonic-gate for (i = 0; (sensorp = envd_sensors[i]) != NULL; i++) {
458*0Sstevel@tonic-gate sensorp->fd = -1;
459*0Sstevel@tonic-gate sensorp->shutdown_initiated = B_FALSE;
460*0Sstevel@tonic-gate sensorp->warning_tstamp = 0;
461*0Sstevel@tonic-gate sensorp->shutdown_tstamp = 0;
462*0Sstevel@tonic-gate threshp = sensorp->temp_thresh;
463*0Sstevel@tonic-gate sensorp->cur_temp = threshp->target_temp;
464*0Sstevel@tonic-gate sensorp->error = 0;
465*0Sstevel@tonic-gate
466*0Sstevel@tonic-gate (void) strcpy(path, "/devices");
467*0Sstevel@tonic-gate (void) strlcat(path, sensorp->devfs_path, sizeof (path));
468*0Sstevel@tonic-gate sensorp->fd = open(path, O_RDWR);
469*0Sstevel@tonic-gate if (sensorp->fd == -1) {
470*0Sstevel@tonic-gate envd_log(LOG_WARNING, ENV_SENSOR_OPEN_FAIL,
471*0Sstevel@tonic-gate sensorp->name, sensorp->devfs_path, errno,
472*0Sstevel@tonic-gate strerror(errno));
473*0Sstevel@tonic-gate sensorp->present = B_FALSE;
474*0Sstevel@tonic-gate continue;
475*0Sstevel@tonic-gate }
476*0Sstevel@tonic-gate sensorp->present = B_TRUE;
477*0Sstevel@tonic-gate sensorcnt++;
478*0Sstevel@tonic-gate
479*0Sstevel@tonic-gate if (monitor_temperature) {
480*0Sstevel@tonic-gate /*
481*0Sstevel@tonic-gate * Set low_power_off and high_power_off limits
482*0Sstevel@tonic-gate */
483*0Sstevel@tonic-gate (void) ioctl(sensorp->fd, MAX1617_SET_LOW_LIMIT,
484*0Sstevel@tonic-gate &threshp->low_power_off);
485*0Sstevel@tonic-gate (void) ioctl(sensorp->fd, MAX1617_SET_HIGH_LIMIT,
486*0Sstevel@tonic-gate &threshp->high_power_off);
487*0Sstevel@tonic-gate }
488*0Sstevel@tonic-gate
489*0Sstevel@tonic-gate /*
490*0Sstevel@tonic-gate * Set cur_temp field to the current temperature value
491*0Sstevel@tonic-gate */
492*0Sstevel@tonic-gate if (get_temperature(sensorp, &temp) == 0) {
493*0Sstevel@tonic-gate sensorp->cur_temp = temp;
494*0Sstevel@tonic-gate }
495*0Sstevel@tonic-gate }
496*0Sstevel@tonic-gate return (sensorcnt);
497*0Sstevel@tonic-gate }
498*0Sstevel@tonic-gate
499*0Sstevel@tonic-gate /*
500*0Sstevel@tonic-gate * Read all temperature sensors and take appropriate action based
501*0Sstevel@tonic-gate * upon temperature threshols associated with each sensor. Possible
502*0Sstevel@tonic-gate * actions are:
503*0Sstevel@tonic-gate *
504*0Sstevel@tonic-gate * temperature > high_shutdown
505*0Sstevel@tonic-gate * temperature < low_shutdown
506*0Sstevel@tonic-gate * Gracefully shutdown the system and log/print a message
507*0Sstevel@tonic-gate * on the system console provided the temperature has been
508*0Sstevel@tonic-gate * in shutdown range for "shutdown_interval" seconds.
509*0Sstevel@tonic-gate *
510*0Sstevel@tonic-gate * high_warning < temperature <= high_shutdown
511*0Sstevel@tonic-gate * low_warning > temperature >= low_shutdown
512*0Sstevel@tonic-gate * Log/print a warning message on the system console at most
513*0Sstevel@tonic-gate * once every "warning_interval" seconds.
514*0Sstevel@tonic-gate *
515*0Sstevel@tonic-gate * Note that the current temperature is recorded in the "cur_temp" field
516*0Sstevel@tonic-gate * within each env_sensor_t structure.
517*0Sstevel@tonic-gate */
518*0Sstevel@tonic-gate static void
monitor_sensors(void)519*0Sstevel@tonic-gate monitor_sensors(void)
520*0Sstevel@tonic-gate {
521*0Sstevel@tonic-gate tempr_t temp;
522*0Sstevel@tonic-gate int i;
523*0Sstevel@tonic-gate env_sensor_t *sensorp;
524*0Sstevel@tonic-gate sensor_thresh_t *threshp;
525*0Sstevel@tonic-gate struct timeval ct;
526*0Sstevel@tonic-gate char msgbuf[BUFSIZ];
527*0Sstevel@tonic-gate char syscmd[BUFSIZ];
528*0Sstevel@tonic-gate
529*0Sstevel@tonic-gate for (i = 0; (sensorp = envd_sensors[i]) != NULL; i++) {
530*0Sstevel@tonic-gate if (get_temperature(sensorp, &temp) < 0)
531*0Sstevel@tonic-gate continue;
532*0Sstevel@tonic-gate
533*0Sstevel@tonic-gate sensorp->cur_temp = temp;
534*0Sstevel@tonic-gate
535*0Sstevel@tonic-gate if (env_debug)
536*0Sstevel@tonic-gate envd_log(LOG_INFO,
537*0Sstevel@tonic-gate "sensor: %-13s temp cur:%3d target:%3d\n",
538*0Sstevel@tonic-gate sensorp->name, temp,
539*0Sstevel@tonic-gate sensorp->temp_thresh->target_temp);
540*0Sstevel@tonic-gate
541*0Sstevel@tonic-gate if (!monitor_temperature)
542*0Sstevel@tonic-gate continue;
543*0Sstevel@tonic-gate
544*0Sstevel@tonic-gate /*
545*0Sstevel@tonic-gate * If this sensor already triggered system shutdown, don't
546*0Sstevel@tonic-gate * log any more shutdown/warning messages for it.
547*0Sstevel@tonic-gate */
548*0Sstevel@tonic-gate if (sensorp->shutdown_initiated)
549*0Sstevel@tonic-gate continue;
550*0Sstevel@tonic-gate
551*0Sstevel@tonic-gate /*
552*0Sstevel@tonic-gate * Check for the temperature in warning and shutdown range
553*0Sstevel@tonic-gate * and take appropriate action.
554*0Sstevel@tonic-gate */
555*0Sstevel@tonic-gate threshp = sensorp->temp_thresh;
556*0Sstevel@tonic-gate if (TEMP_IN_WARNING_RANGE(temp, threshp)) {
557*0Sstevel@tonic-gate /*
558*0Sstevel@tonic-gate * Log warning message at most once every
559*0Sstevel@tonic-gate * warning_interval seconds.
560*0Sstevel@tonic-gate */
561*0Sstevel@tonic-gate (void) gettimeofday(&ct, NULL);
562*0Sstevel@tonic-gate if ((ct.tv_sec - sensorp->warning_tstamp) >=
563*0Sstevel@tonic-gate warning_interval) {
564*0Sstevel@tonic-gate envd_log(LOG_WARNING, ENV_WARNING_MSG,
565*0Sstevel@tonic-gate sensorp->name, temp,
566*0Sstevel@tonic-gate threshp->low_warning,
567*0Sstevel@tonic-gate threshp->high_warning);
568*0Sstevel@tonic-gate sensorp->warning_tstamp = ct.tv_sec;
569*0Sstevel@tonic-gate }
570*0Sstevel@tonic-gate }
571*0Sstevel@tonic-gate
572*0Sstevel@tonic-gate if (TEMP_IN_SHUTDOWN_RANGE(temp, threshp)) {
573*0Sstevel@tonic-gate (void) gettimeofday(&ct, NULL);
574*0Sstevel@tonic-gate if (sensorp->shutdown_tstamp == 0)
575*0Sstevel@tonic-gate sensorp->shutdown_tstamp = ct.tv_sec;
576*0Sstevel@tonic-gate
577*0Sstevel@tonic-gate /*
578*0Sstevel@tonic-gate * Shutdown the system if the temperature remains
579*0Sstevel@tonic-gate * in the shutdown range for over shutdown_interval
580*0Sstevel@tonic-gate * seconds.
581*0Sstevel@tonic-gate */
582*0Sstevel@tonic-gate if ((ct.tv_sec - sensorp->shutdown_tstamp) >=
583*0Sstevel@tonic-gate shutdown_interval) {
584*0Sstevel@tonic-gate /* log error */
585*0Sstevel@tonic-gate sensorp->shutdown_initiated = B_TRUE;
586*0Sstevel@tonic-gate (void) snprintf(msgbuf, sizeof (msgbuf),
587*0Sstevel@tonic-gate ENV_SHUTDOWN_MSG, sensorp->name,
588*0Sstevel@tonic-gate temp, threshp->low_shutdown,
589*0Sstevel@tonic-gate threshp->high_shutdown);
590*0Sstevel@tonic-gate envd_log(LOG_CRIT, msgbuf);
591*0Sstevel@tonic-gate
592*0Sstevel@tonic-gate /* shutdown the system (only once) */
593*0Sstevel@tonic-gate if (system_shutdown_started == B_FALSE) {
594*0Sstevel@tonic-gate (void) snprintf(syscmd, sizeof (syscmd),
595*0Sstevel@tonic-gate "%s \"%s\"", shutdown_cmd, msgbuf);
596*0Sstevel@tonic-gate envd_log(LOG_CRIT, syscmd);
597*0Sstevel@tonic-gate system_shutdown_started = B_TRUE;
598*0Sstevel@tonic-gate (void) system(syscmd);
599*0Sstevel@tonic-gate }
600*0Sstevel@tonic-gate }
601*0Sstevel@tonic-gate } else if (sensorp->shutdown_tstamp != 0)
602*0Sstevel@tonic-gate sensorp->shutdown_tstamp = 0;
603*0Sstevel@tonic-gate }
604*0Sstevel@tonic-gate }
605*0Sstevel@tonic-gate
606*0Sstevel@tonic-gate
607*0Sstevel@tonic-gate /*
608*0Sstevel@tonic-gate * This is the environment thread, which monitors the current temperature
609*0Sstevel@tonic-gate * and power managed state and controls system fan speed. Temperature is
610*0Sstevel@tonic-gate * polled every sensor-poll_interval seconds duration.
611*0Sstevel@tonic-gate */
612*0Sstevel@tonic-gate static void *
envthr(void * args)613*0Sstevel@tonic-gate envthr(void *args)
614*0Sstevel@tonic-gate {
615*0Sstevel@tonic-gate int err;
616*0Sstevel@tonic-gate fanspeed_t fan_speed;
617*0Sstevel@tonic-gate struct timeval ct;
618*0Sstevel@tonic-gate struct timespec to;
619*0Sstevel@tonic-gate env_fan_t *pmfanp = &envd_system_fan;
620*0Sstevel@tonic-gate tempr_t cpu_amb_temp, cpu_die_temp;
621*0Sstevel@tonic-gate tempr_t cpu_amb_warning, cpu_die_warning;
622*0Sstevel@tonic-gate
623*0Sstevel@tonic-gate (void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
624*0Sstevel@tonic-gate (void) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
625*0Sstevel@tonic-gate
626*0Sstevel@tonic-gate cpu_amb_warning = cpu_amb_sensor.temp_thresh->high_warning;
627*0Sstevel@tonic-gate cpu_die_warning = cpu_die_sensor.temp_thresh->high_warning;
628*0Sstevel@tonic-gate
629*0Sstevel@tonic-gate for (;;) {
630*0Sstevel@tonic-gate (void) gettimeofday(&ct, NULL);
631*0Sstevel@tonic-gate
632*0Sstevel@tonic-gate /*
633*0Sstevel@tonic-gate * Monitor current temperature for all sensors
634*0Sstevel@tonic-gate * (current temperature is recorded in the "cur_temp"
635*0Sstevel@tonic-gate * field within each sensor data structure)
636*0Sstevel@tonic-gate */
637*0Sstevel@tonic-gate monitor_sensors();
638*0Sstevel@tonic-gate
639*0Sstevel@tonic-gate cpu_amb_temp = cpu_amb_sensor.cur_temp;
640*0Sstevel@tonic-gate cpu_die_temp = cpu_die_sensor.cur_temp;
641*0Sstevel@tonic-gate
642*0Sstevel@tonic-gate /*
643*0Sstevel@tonic-gate * Process any PM state change events while waiting until
644*0Sstevel@tonic-gate * time to poll sensors again (i.e. sensor_poll_interval
645*0Sstevel@tonic-gate * seconds from the last time).
646*0Sstevel@tonic-gate */
647*0Sstevel@tonic-gate to.tv_sec = ct.tv_sec + sensor_poll_interval;
648*0Sstevel@tonic-gate to.tv_nsec = 0;
649*0Sstevel@tonic-gate for (;;) {
650*0Sstevel@tonic-gate /*
651*0Sstevel@tonic-gate * Turn off system fan if in lowest power state
652*0Sstevel@tonic-gate * and both CPU die and ambient temperatures are
653*0Sstevel@tonic-gate * below corresponding high warning temperatures.
654*0Sstevel@tonic-gate */
655*0Sstevel@tonic-gate fan_speed = pmfanp->speed_max;
656*0Sstevel@tonic-gate if (cur_lpstate && cpu_amb_temp < cpu_amb_warning &&
657*0Sstevel@tonic-gate cpu_die_temp < cpu_die_warning)
658*0Sstevel@tonic-gate fan_speed = pmfanp->speed_min;
659*0Sstevel@tonic-gate
660*0Sstevel@tonic-gate if (env_debug)
661*0Sstevel@tonic-gate envd_log(LOG_INFO,
662*0Sstevel@tonic-gate "fan: %-16s speed cur:%3d new:%3d "
663*0Sstevel@tonic-gate "low-power:%d\n", pmfanp->name,
664*0Sstevel@tonic-gate (uint_t)pmfanp->cur_speed,
665*0Sstevel@tonic-gate (uint_t)fan_speed, cur_lpstate);
666*0Sstevel@tonic-gate
667*0Sstevel@tonic-gate if (fan_speed != pmfanp->cur_speed &&
668*0Sstevel@tonic-gate set_fan_speed(pmfanp, fan_speed) == 0)
669*0Sstevel@tonic-gate pmfanp->cur_speed = fan_speed;
670*0Sstevel@tonic-gate
671*0Sstevel@tonic-gate /* wait for power state change or time to poll */
672*0Sstevel@tonic-gate pthread_mutex_lock(&lpstate_lock);
673*0Sstevel@tonic-gate err = pthread_cond_timedwait(&lpstate_cond,
674*0Sstevel@tonic-gate &lpstate_lock, &to);
675*0Sstevel@tonic-gate pthread_mutex_unlock(&lpstate_lock);
676*0Sstevel@tonic-gate if (err == ETIMEDOUT)
677*0Sstevel@tonic-gate break;
678*0Sstevel@tonic-gate }
679*0Sstevel@tonic-gate }
680*0Sstevel@tonic-gate /*NOTREACHED*/
681*0Sstevel@tonic-gate return (NULL);
682*0Sstevel@tonic-gate }
683*0Sstevel@tonic-gate
684*0Sstevel@tonic-gate /*
685*0Sstevel@tonic-gate * This is the power management thread, which monitors all power state
686*0Sstevel@tonic-gate * change events and wakes up the "envthr" thread when the system enters
687*0Sstevel@tonic-gate * or exits the lowest power state.
688*0Sstevel@tonic-gate */
689*0Sstevel@tonic-gate static void *
pmthr(void * args)690*0Sstevel@tonic-gate pmthr(void *args)
691*0Sstevel@tonic-gate {
692*0Sstevel@tonic-gate pm_state_change_t pmstate;
693*0Sstevel@tonic-gate char physpath[PATH_MAX];
694*0Sstevel@tonic-gate int prev_lpstate;
695*0Sstevel@tonic-gate
696*0Sstevel@tonic-gate (void) pthread_setcancelstate(PTHREAD_CANCEL_ENABLE, NULL);
697*0Sstevel@tonic-gate (void) pthread_setcanceltype(PTHREAD_CANCEL_ASYNCHRONOUS, NULL);
698*0Sstevel@tonic-gate
699*0Sstevel@tonic-gate pmstate.physpath = physpath;
700*0Sstevel@tonic-gate pmstate.size = sizeof (physpath);
701*0Sstevel@tonic-gate cur_lpstate = 0;
702*0Sstevel@tonic-gate prev_lpstate = 0;
703*0Sstevel@tonic-gate
704*0Sstevel@tonic-gate for (;;) {
705*0Sstevel@tonic-gate /*
706*0Sstevel@tonic-gate * Get PM state change events to check if the system
707*0Sstevel@tonic-gate * is in lowest power state and wake up the "envthr"
708*0Sstevel@tonic-gate * thread when the power state changes.
709*0Sstevel@tonic-gate *
710*0Sstevel@tonic-gate * To minimize polling, we use the blocking interface
711*0Sstevel@tonic-gate * to get the power state change event here.
712*0Sstevel@tonic-gate */
713*0Sstevel@tonic-gate if (ioctl(pm_fd, PM_GET_STATE_CHANGE_WAIT, &pmstate) != 0) {
714*0Sstevel@tonic-gate if (errno != EINTR)
715*0Sstevel@tonic-gate break;
716*0Sstevel@tonic-gate continue;
717*0Sstevel@tonic-gate }
718*0Sstevel@tonic-gate
719*0Sstevel@tonic-gate /*
720*0Sstevel@tonic-gate * Extract the lowest power state from the last queued
721*0Sstevel@tonic-gate * state change events. We pick up queued state change
722*0Sstevel@tonic-gate * events using the non-blocking interface and wake up
723*0Sstevel@tonic-gate * the "envthr" thread only after consuming all the
724*0Sstevel@tonic-gate * state change events queued at that time.
725*0Sstevel@tonic-gate */
726*0Sstevel@tonic-gate do {
727*0Sstevel@tonic-gate if (env_debug > 1) {
728*0Sstevel@tonic-gate envd_log(LOG_INFO,
729*0Sstevel@tonic-gate "pmstate event:0x%x flags:%x comp:%d "
730*0Sstevel@tonic-gate "oldval:%d newval:%d path:%s\n",
731*0Sstevel@tonic-gate pmstate.event, pmstate.flags,
732*0Sstevel@tonic-gate pmstate.component, pmstate.old_level,
733*0Sstevel@tonic-gate pmstate.new_level, pmstate.physpath);
734*0Sstevel@tonic-gate }
735*0Sstevel@tonic-gate cur_lpstate =
736*0Sstevel@tonic-gate (pmstate.flags & PSC_ALL_LOWEST) ? 1 : 0;
737*0Sstevel@tonic-gate } while (ioctl(pm_fd, PM_GET_STATE_CHANGE, &pmstate) == 0);
738*0Sstevel@tonic-gate
739*0Sstevel@tonic-gate if (cur_lpstate != prev_lpstate) {
740*0Sstevel@tonic-gate prev_lpstate = cur_lpstate;
741*0Sstevel@tonic-gate pthread_mutex_lock(&lpstate_lock);
742*0Sstevel@tonic-gate pthread_cond_signal(&lpstate_cond);
743*0Sstevel@tonic-gate pthread_mutex_unlock(&lpstate_lock);
744*0Sstevel@tonic-gate }
745*0Sstevel@tonic-gate }
746*0Sstevel@tonic-gate
747*0Sstevel@tonic-gate /*
748*0Sstevel@tonic-gate * We won't be able to monitor lowest power state any longer,
749*0Sstevel@tonic-gate * hence reset it and wakeup the "envthr".
750*0Sstevel@tonic-gate */
751*0Sstevel@tonic-gate if (cur_lpstate != 0) {
752*0Sstevel@tonic-gate prev_lpstate = cur_lpstate;
753*0Sstevel@tonic-gate cur_lpstate = 0;
754*0Sstevel@tonic-gate pthread_mutex_lock(&lpstate_lock);
755*0Sstevel@tonic-gate pthread_cond_signal(&lpstate_cond);
756*0Sstevel@tonic-gate pthread_mutex_unlock(&lpstate_lock);
757*0Sstevel@tonic-gate }
758*0Sstevel@tonic-gate envd_log(LOG_ERR, PM_THREAD_EXITING, errno, strerror(errno));
759*0Sstevel@tonic-gate return (NULL);
760*0Sstevel@tonic-gate }
761*0Sstevel@tonic-gate
762*0Sstevel@tonic-gate
763*0Sstevel@tonic-gate /*
764*0Sstevel@tonic-gate * Parse string value (handling escaped double quotes and other characters)
765*0Sstevel@tonic-gate * and return string end pointer.
766*0Sstevel@tonic-gate */
767*0Sstevel@tonic-gate
768*0Sstevel@tonic-gate static char *
parse_string_val(char * buf)769*0Sstevel@tonic-gate parse_string_val(char *buf)
770*0Sstevel@tonic-gate {
771*0Sstevel@tonic-gate char *p, c;
772*0Sstevel@tonic-gate
773*0Sstevel@tonic-gate if (buf[0] != '"')
774*0Sstevel@tonic-gate return (NULL);
775*0Sstevel@tonic-gate
776*0Sstevel@tonic-gate for (p = buf+1; (c = *p) != '\0'; p++)
777*0Sstevel@tonic-gate if (c == '"' || (c == '\\' && *++p == '\0'))
778*0Sstevel@tonic-gate break;
779*0Sstevel@tonic-gate
780*0Sstevel@tonic-gate return ((*p == '"') ? p : NULL);
781*0Sstevel@tonic-gate }
782*0Sstevel@tonic-gate
783*0Sstevel@tonic-gate
784*0Sstevel@tonic-gate /*
785*0Sstevel@tonic-gate * Process configuration file
786*0Sstevel@tonic-gate */
787*0Sstevel@tonic-gate static void
process_env_conf_file(void)788*0Sstevel@tonic-gate process_env_conf_file(void)
789*0Sstevel@tonic-gate {
790*0Sstevel@tonic-gate int line, len, val, toklen;
791*0Sstevel@tonic-gate char buf[BUFSIZ];
792*0Sstevel@tonic-gate FILE *fp;
793*0Sstevel@tonic-gate env_tuneable_t *tunep;
794*0Sstevel@tonic-gate char nmbuf[SYS_NMLN];
795*0Sstevel@tonic-gate char fname[PATH_MAX];
796*0Sstevel@tonic-gate char *tok, *valuep, *strend;
797*0Sstevel@tonic-gate char tokdel[] = " \t\n\r";
798*0Sstevel@tonic-gate int skip_line = 0;
799*0Sstevel@tonic-gate
800*0Sstevel@tonic-gate if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) == -1)
801*0Sstevel@tonic-gate return;
802*0Sstevel@tonic-gate
803*0Sstevel@tonic-gate (void) snprintf(fname, sizeof (fname), PICLD_PLAT_PLUGIN_DIRF, nmbuf);
804*0Sstevel@tonic-gate (void) strlcat(fname, ENV_CONF_FILE, sizeof (fname));
805*0Sstevel@tonic-gate fp = fopen(fname, "r");
806*0Sstevel@tonic-gate if (fp == NULL)
807*0Sstevel@tonic-gate return;
808*0Sstevel@tonic-gate
809*0Sstevel@tonic-gate /*
810*0Sstevel@tonic-gate * Blank lines or lines starting with "#" or "*" in the first
811*0Sstevel@tonic-gate * column are ignored. All other lines are assumed to contain
812*0Sstevel@tonic-gate * input in the following format:
813*0Sstevel@tonic-gate *
814*0Sstevel@tonic-gate * keyword value
815*0Sstevel@tonic-gate *
816*0Sstevel@tonic-gate * where the "value" can be a signed integer or string (in
817*0Sstevel@tonic-gate * double quotes) depending upon the keyword.
818*0Sstevel@tonic-gate */
819*0Sstevel@tonic-gate
820*0Sstevel@tonic-gate for (line = 1; fgets(buf, sizeof (buf), fp) != NULL; line++) {
821*0Sstevel@tonic-gate len = strlen(buf);
822*0Sstevel@tonic-gate if (len <= 0)
823*0Sstevel@tonic-gate continue;
824*0Sstevel@tonic-gate
825*0Sstevel@tonic-gate /* skip long lines */
826*0Sstevel@tonic-gate if (buf[len-1] != '\n') {
827*0Sstevel@tonic-gate skip_line = 1;
828*0Sstevel@tonic-gate continue;
829*0Sstevel@tonic-gate } else if (skip_line) {
830*0Sstevel@tonic-gate skip_line = 0;
831*0Sstevel@tonic-gate continue;
832*0Sstevel@tonic-gate } else
833*0Sstevel@tonic-gate buf[len-1] = '\0';
834*0Sstevel@tonic-gate
835*0Sstevel@tonic-gate /* skip comments */
836*0Sstevel@tonic-gate if (buf[0] == '*' || buf[0] == '#')
837*0Sstevel@tonic-gate continue;
838*0Sstevel@tonic-gate
839*0Sstevel@tonic-gate /*
840*0Sstevel@tonic-gate * Skip over white space to get the keyword
841*0Sstevel@tonic-gate */
842*0Sstevel@tonic-gate tok = buf + strspn(buf, tokdel);
843*0Sstevel@tonic-gate if (*tok == '\0')
844*0Sstevel@tonic-gate continue; /* blank line */
845*0Sstevel@tonic-gate
846*0Sstevel@tonic-gate toklen = strcspn(tok, tokdel);
847*0Sstevel@tonic-gate tok[toklen] = '\0';
848*0Sstevel@tonic-gate
849*0Sstevel@tonic-gate /* Get possible location for value (within current line) */
850*0Sstevel@tonic-gate valuep = tok + toklen + 1;
851*0Sstevel@tonic-gate if (valuep > buf+len)
852*0Sstevel@tonic-gate valuep = buf + len;
853*0Sstevel@tonic-gate
854*0Sstevel@tonic-gate /*
855*0Sstevel@tonic-gate * Lookup the keyword and process value accordingly
856*0Sstevel@tonic-gate */
857*0Sstevel@tonic-gate for (tunep = &env_tuneables[0]; tunep->name != NULL; tunep++) {
858*0Sstevel@tonic-gate if (strcmp(tunep->name, tok) != 0)
859*0Sstevel@tonic-gate continue;
860*0Sstevel@tonic-gate
861*0Sstevel@tonic-gate switch (tunep->type) {
862*0Sstevel@tonic-gate case KTYPE_INT:
863*0Sstevel@tonic-gate errno = 0;
864*0Sstevel@tonic-gate val = strtol(valuep, &valuep, 0);
865*0Sstevel@tonic-gate
866*0Sstevel@tonic-gate /* Check for invalid value or extra tokens */
867*0Sstevel@tonic-gate if (errno != 0 || strtok(valuep, tokdel)) {
868*0Sstevel@tonic-gate envd_log(LOG_INFO,
869*0Sstevel@tonic-gate ENV_CONF_INT_EXPECTED,
870*0Sstevel@tonic-gate fname, line, tok);
871*0Sstevel@tonic-gate break;
872*0Sstevel@tonic-gate }
873*0Sstevel@tonic-gate
874*0Sstevel@tonic-gate /* Update only if value within range */
875*0Sstevel@tonic-gate if (tunep->size == sizeof (int8_t) &&
876*0Sstevel@tonic-gate val == (int8_t)val)
877*0Sstevel@tonic-gate *(int8_t *)tunep->addr = (int8_t)val;
878*0Sstevel@tonic-gate else if (tunep->size == sizeof (short) &&
879*0Sstevel@tonic-gate val == (short)val)
880*0Sstevel@tonic-gate *(short *)tunep->addr = (short)val;
881*0Sstevel@tonic-gate else if (tunep->size == sizeof (int))
882*0Sstevel@tonic-gate *(int *)tunep->addr = (int)val;
883*0Sstevel@tonic-gate else {
884*0Sstevel@tonic-gate envd_log(LOG_INFO,
885*0Sstevel@tonic-gate ENV_CONF_INT_EXPECTED,
886*0Sstevel@tonic-gate fname, line, tok);
887*0Sstevel@tonic-gate break;
888*0Sstevel@tonic-gate }
889*0Sstevel@tonic-gate if (env_debug)
890*0Sstevel@tonic-gate envd_log(LOG_INFO, "SUNW_piclenvd: "
891*0Sstevel@tonic-gate "file:%s line:%d %s = %d\n",
892*0Sstevel@tonic-gate fname, line, tok, val);
893*0Sstevel@tonic-gate break;
894*0Sstevel@tonic-gate
895*0Sstevel@tonic-gate case KTYPE_STRING:
896*0Sstevel@tonic-gate /*
897*0Sstevel@tonic-gate * String value must be within double quotes.
898*0Sstevel@tonic-gate * Skip over initial white spaces before
899*0Sstevel@tonic-gate * looking for value.
900*0Sstevel@tonic-gate */
901*0Sstevel@tonic-gate valuep += strspn(valuep, tokdel);
902*0Sstevel@tonic-gate strend = parse_string_val(valuep);
903*0Sstevel@tonic-gate
904*0Sstevel@tonic-gate if (strend == NULL || *valuep != '"' ||
905*0Sstevel@tonic-gate strtok(strend+1, tokdel) != NULL ||
906*0Sstevel@tonic-gate (strend-valuep) > tunep->size) {
907*0Sstevel@tonic-gate envd_log(LOG_INFO,
908*0Sstevel@tonic-gate ENV_CONF_STRING_EXPECTED,
909*0Sstevel@tonic-gate fname, line, tok,
910*0Sstevel@tonic-gate tunep->size);
911*0Sstevel@tonic-gate break;
912*0Sstevel@tonic-gate }
913*0Sstevel@tonic-gate *strend = '\0';
914*0Sstevel@tonic-gate if (env_debug)
915*0Sstevel@tonic-gate envd_log(LOG_INFO, "piclenvd: file:%s"
916*0Sstevel@tonic-gate " line:%d %s = \"%s\"\n",
917*0Sstevel@tonic-gate fname, line, tok, valuep+1);
918*0Sstevel@tonic-gate (void) strcpy(tunep->addr, (caddr_t)valuep+1);
919*0Sstevel@tonic-gate break;
920*0Sstevel@tonic-gate
921*0Sstevel@tonic-gate default:
922*0Sstevel@tonic-gate envd_log(LOG_INFO,
923*0Sstevel@tonic-gate ENV_CONF_UNSUPPORTED_TYPE,
924*0Sstevel@tonic-gate fname, line,
925*0Sstevel@tonic-gate tunep->type, tunep->name);
926*0Sstevel@tonic-gate }
927*0Sstevel@tonic-gate break;
928*0Sstevel@tonic-gate }
929*0Sstevel@tonic-gate
930*0Sstevel@tonic-gate if (tunep->name == NULL)
931*0Sstevel@tonic-gate envd_log(LOG_INFO, ENV_CONF_UNSUPPORTED_KEYWORD,
932*0Sstevel@tonic-gate fname, line, tok);
933*0Sstevel@tonic-gate }
934*0Sstevel@tonic-gate (void) fclose(fp);
935*0Sstevel@tonic-gate }
936*0Sstevel@tonic-gate
937*0Sstevel@tonic-gate /*
938*0Sstevel@tonic-gate * Setup envrionmental daemon state and start threads to monitor
939*0Sstevel@tonic-gate * temperature and power management state.
940*0Sstevel@tonic-gate * Returns -1 on error, 0 if successful.
941*0Sstevel@tonic-gate */
942*0Sstevel@tonic-gate
943*0Sstevel@tonic-gate static int
envd_setup(void)944*0Sstevel@tonic-gate envd_setup(void)
945*0Sstevel@tonic-gate {
946*0Sstevel@tonic-gate if (envd_inited == B_FALSE) {
947*0Sstevel@tonic-gate /*
948*0Sstevel@tonic-gate * Initialize global state
949*0Sstevel@tonic-gate */
950*0Sstevel@tonic-gate system_shutdown_started = B_FALSE;
951*0Sstevel@tonic-gate envthr_created = B_FALSE;
952*0Sstevel@tonic-gate pmthr_created = B_FALSE;
953*0Sstevel@tonic-gate
954*0Sstevel@tonic-gate if (pthread_attr_init(&thr_attr) != 0 ||
955*0Sstevel@tonic-gate pthread_attr_setscope(&thr_attr, PTHREAD_SCOPE_SYSTEM) != 0)
956*0Sstevel@tonic-gate return (-1);
957*0Sstevel@tonic-gate
958*0Sstevel@tonic-gate if (pthread_mutex_init(&lpstate_lock, NULL) != 0 ||
959*0Sstevel@tonic-gate pthread_cond_init(&lpstate_cond, NULL) != 0)
960*0Sstevel@tonic-gate return (-1);
961*0Sstevel@tonic-gate
962*0Sstevel@tonic-gate /*
963*0Sstevel@tonic-gate * Process tuneable parameters
964*0Sstevel@tonic-gate */
965*0Sstevel@tonic-gate process_env_conf_file();
966*0Sstevel@tonic-gate
967*0Sstevel@tonic-gate /*
968*0Sstevel@tonic-gate * Setup temperature sensors and fail if we can't open
969*0Sstevel@tonic-gate * at least one sensor.
970*0Sstevel@tonic-gate */
971*0Sstevel@tonic-gate if (envd_setup_sensors() <= 0)
972*0Sstevel@tonic-gate return (-1);
973*0Sstevel@tonic-gate
974*0Sstevel@tonic-gate /*
975*0Sstevel@tonic-gate * Setup fan device (don't fail even if we can't access
976*0Sstevel@tonic-gate * the fan as we can still monitor temeperature.
977*0Sstevel@tonic-gate */
978*0Sstevel@tonic-gate (void) envd_setup_fans();
979*0Sstevel@tonic-gate
980*0Sstevel@tonic-gate /*
981*0Sstevel@tonic-gate * Create a thread to monitor temperature and control fan
982*0Sstevel@tonic-gate * speed.
983*0Sstevel@tonic-gate */
984*0Sstevel@tonic-gate if (envthr_created == B_FALSE && pthread_create(&envthr_tid,
985*0Sstevel@tonic-gate &thr_attr, envthr, (void *)NULL) != 0) {
986*0Sstevel@tonic-gate envd_close_fans();
987*0Sstevel@tonic-gate envd_close_sensors();
988*0Sstevel@tonic-gate envd_log(LOG_CRIT, ENV_THREAD_CREATE_FAILED);
989*0Sstevel@tonic-gate return (-1);
990*0Sstevel@tonic-gate }
991*0Sstevel@tonic-gate envthr_created = B_TRUE;
992*0Sstevel@tonic-gate }
993*0Sstevel@tonic-gate envd_inited = B_TRUE;
994*0Sstevel@tonic-gate
995*0Sstevel@tonic-gate /*
996*0Sstevel@tonic-gate * Create a thread to monitor PM state
997*0Sstevel@tonic-gate */
998*0Sstevel@tonic-gate if (pmthr_created == B_FALSE) {
999*0Sstevel@tonic-gate pm_fd = open(PM_DEVICE, O_RDONLY);
1000*0Sstevel@tonic-gate if (pm_fd == -1 || pthread_create(&pmthr_tid, &thr_attr,
1001*0Sstevel@tonic-gate pmthr, (void *)NULL) != 0) {
1002*0Sstevel@tonic-gate envd_close_pm();
1003*0Sstevel@tonic-gate envd_log(LOG_CRIT, PM_THREAD_CREATE_FAILED);
1004*0Sstevel@tonic-gate } else
1005*0Sstevel@tonic-gate pmthr_created = B_TRUE;
1006*0Sstevel@tonic-gate }
1007*0Sstevel@tonic-gate return (0);
1008*0Sstevel@tonic-gate }
1009*0Sstevel@tonic-gate
1010*0Sstevel@tonic-gate
1011*0Sstevel@tonic-gate static void
piclenvd_register(void)1012*0Sstevel@tonic-gate piclenvd_register(void)
1013*0Sstevel@tonic-gate {
1014*0Sstevel@tonic-gate picld_plugin_register(&my_reg_info);
1015*0Sstevel@tonic-gate }
1016*0Sstevel@tonic-gate
1017*0Sstevel@tonic-gate static void
piclenvd_init(void)1018*0Sstevel@tonic-gate piclenvd_init(void)
1019*0Sstevel@tonic-gate {
1020*0Sstevel@tonic-gate /*
1021*0Sstevel@tonic-gate * Start environmental daemon/threads
1022*0Sstevel@tonic-gate */
1023*0Sstevel@tonic-gate if (envd_setup() != 0) {
1024*0Sstevel@tonic-gate envd_log(LOG_CRIT, ENVD_PLUGIN_INIT_FAILED);
1025*0Sstevel@tonic-gate return;
1026*0Sstevel@tonic-gate }
1027*0Sstevel@tonic-gate
1028*0Sstevel@tonic-gate /*
1029*0Sstevel@tonic-gate * Now setup/populate PICL tree
1030*0Sstevel@tonic-gate */
1031*0Sstevel@tonic-gate env_picl_setup();
1032*0Sstevel@tonic-gate }
1033*0Sstevel@tonic-gate
1034*0Sstevel@tonic-gate static void
piclenvd_fini(void)1035*0Sstevel@tonic-gate piclenvd_fini(void)
1036*0Sstevel@tonic-gate {
1037*0Sstevel@tonic-gate void *exitval;
1038*0Sstevel@tonic-gate
1039*0Sstevel@tonic-gate /*
1040*0Sstevel@tonic-gate * Kill both "envthr" and "pmthr" threads.
1041*0Sstevel@tonic-gate */
1042*0Sstevel@tonic-gate if (envthr_created) {
1043*0Sstevel@tonic-gate (void) pthread_cancel(envthr_tid);
1044*0Sstevel@tonic-gate (void) pthread_join(envthr_tid, &exitval);
1045*0Sstevel@tonic-gate envthr_created = B_FALSE;
1046*0Sstevel@tonic-gate }
1047*0Sstevel@tonic-gate
1048*0Sstevel@tonic-gate if (pmthr_created) {
1049*0Sstevel@tonic-gate (void) pthread_cancel(pmthr_tid);
1050*0Sstevel@tonic-gate (void) pthread_join(pmthr_tid, &exitval);
1051*0Sstevel@tonic-gate pmthr_created = B_FALSE;
1052*0Sstevel@tonic-gate }
1053*0Sstevel@tonic-gate
1054*0Sstevel@tonic-gate /*
1055*0Sstevel@tonic-gate * close all sensors, fans and the power management device
1056*0Sstevel@tonic-gate */
1057*0Sstevel@tonic-gate envd_close_pm();
1058*0Sstevel@tonic-gate envd_close_fans();
1059*0Sstevel@tonic-gate envd_close_sensors();
1060*0Sstevel@tonic-gate envd_inited = B_FALSE;
1061*0Sstevel@tonic-gate }
1062*0Sstevel@tonic-gate
1063*0Sstevel@tonic-gate /*VARARGS2*/
1064*0Sstevel@tonic-gate void
envd_log(int pri,const char * fmt,...)1065*0Sstevel@tonic-gate envd_log(int pri, const char *fmt, ...)
1066*0Sstevel@tonic-gate {
1067*0Sstevel@tonic-gate va_list ap;
1068*0Sstevel@tonic-gate
1069*0Sstevel@tonic-gate va_start(ap, fmt);
1070*0Sstevel@tonic-gate vsyslog(pri, fmt, ap);
1071*0Sstevel@tonic-gate va_end(ap);
1072*0Sstevel@tonic-gate }
1073