Home | History | Annotate | Line # | Download | only in sysmon
sysmon_envsys_events.c revision 1.19.4.6
      1  1.19.4.6     joerg /* $NetBSD: sysmon_envsys_events.c,v 1.19.4.6 2007/11/21 21:55:49 joerg Exp $ */
      2       1.1   xtraeme 
      3       1.1   xtraeme /*-
      4  1.19.4.3     joerg  * Copyright (c) 2007 Juan Romero Pardines.
      5       1.1   xtraeme  * All rights reserved.
      6       1.1   xtraeme  *
      7       1.1   xtraeme  * Redistribution and use in source and binary forms, with or without
      8       1.1   xtraeme  * modification, are permitted provided that the following conditions
      9       1.1   xtraeme  * are met:
     10       1.1   xtraeme  * 1. Redistributions of source code must retain the above copyright
     11       1.1   xtraeme  *    notice, this list of conditions and the following disclaimer.
     12       1.1   xtraeme  * 2. Redistributions in binary form must reproduce the above copyright
     13       1.1   xtraeme  *    notice, this list of conditions and the following disclaimer in the
     14       1.1   xtraeme  *    documentation and/or other materials provided with the distribution.
     15       1.1   xtraeme  *
     16  1.19.4.3     joerg  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     17  1.19.4.3     joerg  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     18  1.19.4.3     joerg  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     19  1.19.4.3     joerg  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  1.19.4.3     joerg  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     21  1.19.4.3     joerg  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     22  1.19.4.3     joerg  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     23  1.19.4.3     joerg  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     24  1.19.4.3     joerg  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     25  1.19.4.3     joerg  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     26       1.1   xtraeme  */
     27       1.1   xtraeme 
     28       1.1   xtraeme /*
     29       1.1   xtraeme  * sysmon_envsys(9) events framework.
     30       1.1   xtraeme  */
     31       1.1   xtraeme 
     32       1.1   xtraeme #include <sys/cdefs.h>
     33  1.19.4.6     joerg __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.19.4.6 2007/11/21 21:55:49 joerg Exp $");
     34       1.1   xtraeme 
     35       1.1   xtraeme #include <sys/param.h>
     36       1.1   xtraeme #include <sys/types.h>
     37       1.1   xtraeme #include <sys/conf.h>
     38       1.1   xtraeme #include <sys/errno.h>
     39       1.1   xtraeme #include <sys/kernel.h>
     40       1.1   xtraeme #include <sys/systm.h>
     41       1.1   xtraeme #include <sys/proc.h>
     42       1.1   xtraeme #include <sys/mutex.h>
     43       1.1   xtraeme #include <sys/kmem.h>
     44       1.1   xtraeme #include <sys/callout.h>
     45       1.1   xtraeme 
     46  1.19.4.2     joerg /* #define ENVSYS_DEBUG */
     47       1.1   xtraeme #include <dev/sysmon/sysmonvar.h>
     48       1.1   xtraeme #include <dev/sysmon/sysmon_envsysvar.h>
     49       1.1   xtraeme 
     50      1.10   xtraeme struct sme_sensor_event {
     51      1.10   xtraeme 	int		state;
     52      1.10   xtraeme 	int		event;
     53      1.10   xtraeme };
     54      1.10   xtraeme 
     55      1.10   xtraeme static const struct sme_sensor_event sme_sensor_event[] = {
     56      1.10   xtraeme 	{ ENVSYS_SVALID, 	PENVSYS_EVENT_NORMAL },
     57      1.10   xtraeme 	{ ENVSYS_SCRITICAL, 	PENVSYS_EVENT_CRITICAL },
     58      1.10   xtraeme 	{ ENVSYS_SCRITOVER, 	PENVSYS_EVENT_CRITOVER },
     59      1.10   xtraeme 	{ ENVSYS_SCRITUNDER, 	PENVSYS_EVENT_CRITUNDER },
     60      1.10   xtraeme 	{ ENVSYS_SWARNOVER, 	PENVSYS_EVENT_WARNOVER },
     61      1.10   xtraeme 	{ ENVSYS_SWARNUNDER,	PENVSYS_EVENT_WARNUNDER },
     62      1.10   xtraeme 	{ -1, 			-1 }
     63      1.10   xtraeme };
     64      1.10   xtraeme 
     65  1.19.4.6     joerg kmutex_t sme_mtx, sme_events_mtx, sme_callout_mtx;
     66  1.19.4.2     joerg kcondvar_t sme_cv;
     67  1.19.4.6     joerg static bool sysmon_low_power = false;
     68       1.1   xtraeme 
     69  1.19.4.6     joerg #define SME_EVTIMO	(SME_EVENTS_DEFTIMEOUT * hz)
     70       1.3   xtraeme 
     71  1.19.4.5  jmcneill static bool sme_event_check_low_power(void);
     72  1.19.4.5  jmcneill static bool sme_battery_critical(envsys_data_t *);
     73  1.19.4.4     joerg 
     74       1.3   xtraeme /*
     75       1.1   xtraeme  * sme_event_register:
     76       1.1   xtraeme  *
     77  1.19.4.1  jmcneill  * 	+ Registers a new sysmon envsys event or updates any event
     78  1.19.4.1  jmcneill  * 	  already in the queue.
     79       1.1   xtraeme  */
     80       1.1   xtraeme int
     81  1.19.4.1  jmcneill sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata,
     82  1.19.4.6     joerg 		   struct sysmon_envsys *sme, const char *objkey,
     83  1.19.4.1  jmcneill 		   int32_t critval, int crittype, int powertype)
     84       1.1   xtraeme {
     85  1.19.4.1  jmcneill 	sme_event_t *see = NULL;
     86  1.19.4.1  jmcneill 	prop_object_t obj;
     87  1.19.4.1  jmcneill 	bool critvalup = false;
     88       1.1   xtraeme 	int error = 0;
     89       1.1   xtraeme 
     90  1.19.4.6     joerg 	KASSERT(sdict != NULL || edata != NULL || sme != NULL);
     91  1.19.4.6     joerg 	KASSERT(mutex_owned(&sme_mtx));
     92       1.1   xtraeme 
     93       1.1   xtraeme 	/*
     94  1.19.4.1  jmcneill 	 * check if the event is already on the list and return
     95  1.19.4.1  jmcneill 	 * EEXIST if value provided hasn't been changed.
     96       1.1   xtraeme 	 */
     97  1.19.4.6     joerg 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
     98  1.19.4.6     joerg 		if (strcmp(edata->desc, see->see_pes.pes_sensname) == 0)
     99  1.19.4.6     joerg 			if (crittype == see->see_type) {
    100  1.19.4.6     joerg 				if (see->see_critval == critval) {
    101  1.19.4.1  jmcneill 					DPRINTF(("%s: dev=%s sensor=%s type=%d "
    102  1.19.4.1  jmcneill 				    	    "(already exists)\n", __func__,
    103  1.19.4.6     joerg 				    	    see->see_pes.pes_dvname,
    104  1.19.4.6     joerg 				    	    see->see_pes.pes_sensname,
    105  1.19.4.6     joerg 					    see->see_type));
    106  1.19.4.1  jmcneill 					return EEXIST;
    107  1.19.4.1  jmcneill 				}
    108  1.19.4.1  jmcneill 				critvalup = true;
    109  1.19.4.1  jmcneill 				break;
    110       1.1   xtraeme 			}
    111  1.19.4.1  jmcneill 	}
    112  1.19.4.1  jmcneill 
    113  1.19.4.1  jmcneill 	/*
    114  1.19.4.1  jmcneill 	 * Critical condition operation requested by userland.
    115  1.19.4.1  jmcneill 	 */
    116  1.19.4.1  jmcneill 	if (objkey && critval && critvalup) {
    117  1.19.4.1  jmcneill 		obj = prop_dictionary_get(sdict, objkey);
    118  1.19.4.6     joerg 		if (obj && prop_object_type(obj) == PROP_TYPE_NUMBER) {
    119  1.19.4.1  jmcneill 			/*
    120  1.19.4.1  jmcneill 			 * object is already in dictionary and value
    121  1.19.4.1  jmcneill 			 * provided is not the same than we have
    122  1.19.4.1  jmcneill 			 * currently,  update the critical value.
    123  1.19.4.1  jmcneill 			 */
    124  1.19.4.6     joerg 			see->see_critval = critval;
    125  1.19.4.6     joerg 			DPRINTF(("%s: (%s) sensor=%s type=%d "
    126  1.19.4.6     joerg 			    "(critval updated)\n", __func__, sme->sme_name,
    127  1.19.4.6     joerg 			    edata->desc, see->see_type));
    128  1.19.4.1  jmcneill 			error = sme_sensor_upint32(sdict, objkey, critval);
    129  1.19.4.1  jmcneill 			return error;
    130       1.1   xtraeme 		}
    131       1.1   xtraeme 	}
    132       1.1   xtraeme 
    133  1.19.4.1  jmcneill 	/*
    134  1.19.4.1  jmcneill 	 * The event is not in on the list or in a dictionary, create a new
    135  1.19.4.1  jmcneill 	 * sme event, assign required members and update the object in
    136  1.19.4.1  jmcneill 	 * the dictionary.
    137  1.19.4.1  jmcneill 	 */
    138  1.19.4.1  jmcneill 	see = kmem_zalloc(sizeof(*see), KM_NOSLEEP);
    139  1.19.4.6     joerg 	if (see == NULL)
    140  1.19.4.1  jmcneill 		return ENOMEM;
    141  1.19.4.1  jmcneill 
    142  1.19.4.6     joerg 	see->see_edata = edata;
    143  1.19.4.6     joerg 	see->see_critval = critval;
    144  1.19.4.6     joerg 	see->see_type = crittype;
    145  1.19.4.6     joerg 	see->see_sme = sme;
    146  1.19.4.6     joerg 	(void)strlcpy(see->see_pes.pes_dvname, sme->sme_name,
    147  1.19.4.6     joerg 	    sizeof(see->see_pes.pes_dvname));
    148  1.19.4.6     joerg 	see->see_pes.pes_type = powertype;
    149  1.19.4.6     joerg 	(void)strlcpy(see->see_pes.pes_sensname, edata->desc,
    150  1.19.4.6     joerg 	    sizeof(see->see_pes.pes_sensname));
    151  1.19.4.1  jmcneill 
    152  1.19.4.6     joerg 	LIST_INSERT_HEAD(&sme->sme_events_list, see, see_list);
    153  1.19.4.1  jmcneill 	if (objkey && critval) {
    154  1.19.4.1  jmcneill 		error = sme_sensor_upint32(sdict, objkey, critval);
    155  1.19.4.6     joerg 		if (error)
    156  1.19.4.1  jmcneill 			goto out;
    157  1.19.4.1  jmcneill 	}
    158  1.19.4.6     joerg 	DPRINTF(("%s: (%s) registering sensor=%s snum=%d type=%d "
    159       1.1   xtraeme 	    "critval=%" PRIu32 "\n", __func__,
    160  1.19.4.6     joerg 	    see->see_sme->sme_name, see->see_pes.pes_sensname,
    161  1.19.4.6     joerg 	    see->see_edata->sensor, see->see_type, see->see_critval));
    162       1.1   xtraeme 	/*
    163  1.19.4.6     joerg 	 * Initialize the events framework if it wasn't initialized before.
    164       1.1   xtraeme 	 */
    165  1.19.4.6     joerg 	mutex_enter(&sme_events_mtx);
    166  1.19.4.6     joerg 	if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
    167  1.19.4.6     joerg 		error = sme_events_init(sme);
    168  1.19.4.6     joerg 	mutex_exit(&sme_events_mtx);
    169  1.19.4.1  jmcneill out:
    170  1.19.4.1  jmcneill 	if (error)
    171  1.19.4.1  jmcneill 		kmem_free(see, sizeof(*see));
    172       1.1   xtraeme 	return error;
    173       1.1   xtraeme }
    174       1.1   xtraeme 
    175       1.1   xtraeme /*
    176      1.18   xtraeme  * sme_event_unregister_all:
    177      1.18   xtraeme  *
    178      1.18   xtraeme  * 	+ Unregisters all sysmon envsys events associated with a
    179      1.18   xtraeme  * 	  sysmon envsys device.
    180      1.18   xtraeme  */
    181      1.18   xtraeme void
    182  1.19.4.6     joerg sme_event_unregister_all(struct sysmon_envsys *sme)
    183      1.18   xtraeme {
    184      1.18   xtraeme 	sme_event_t *see;
    185      1.18   xtraeme 	int evcounter = 0;
    186      1.18   xtraeme 
    187  1.19.4.2     joerg 	KASSERT(mutex_owned(&sme_mtx));
    188  1.19.4.6     joerg 	KASSERT(sme != NULL);
    189      1.18   xtraeme 
    190  1.19.4.6     joerg 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    191  1.19.4.6     joerg 		if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0)
    192      1.18   xtraeme 			evcounter++;
    193      1.18   xtraeme 	}
    194      1.18   xtraeme 
    195      1.18   xtraeme 	DPRINTF(("%s: total events %d (%s)\n", __func__,
    196  1.19.4.6     joerg 	    evcounter, sme->sme_name));
    197      1.18   xtraeme 
    198  1.19.4.6     joerg 	while ((see = LIST_FIRST(&sme->sme_events_list))) {
    199      1.18   xtraeme 		if (evcounter == 0)
    200      1.18   xtraeme 			break;
    201      1.18   xtraeme 
    202  1.19.4.6     joerg 		if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) {
    203      1.18   xtraeme 			DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
    204  1.19.4.6     joerg 			    see->see_pes.pes_sensname, see->see_type,
    205  1.19.4.6     joerg 			    sme->sme_name));
    206      1.18   xtraeme 
    207      1.18   xtraeme 			while (see->see_flags & SME_EVENT_WORKING)
    208  1.19.4.2     joerg 				cv_wait(&sme_cv, &sme_mtx);
    209      1.18   xtraeme 
    210      1.18   xtraeme 			LIST_REMOVE(see, see_list);
    211      1.18   xtraeme 			kmem_free(see, sizeof(*see));
    212      1.18   xtraeme 			evcounter--;
    213      1.18   xtraeme 		}
    214      1.18   xtraeme 	}
    215      1.18   xtraeme 
    216  1.19.4.6     joerg 	if (LIST_EMPTY(&sme->sme_events_list)) {
    217  1.19.4.6     joerg 		mutex_enter(&sme_events_mtx);
    218  1.19.4.6     joerg 		if (sme->sme_flags & SME_CALLOUT_INITIALIZED)
    219  1.19.4.6     joerg 			sme_events_destroy(sme);
    220  1.19.4.6     joerg 		mutex_exit(&sme_events_mtx);
    221  1.19.4.1  jmcneill 	}
    222      1.18   xtraeme }
    223      1.18   xtraeme 
    224      1.18   xtraeme /*
    225       1.1   xtraeme  * sme_event_unregister:
    226       1.1   xtraeme  *
    227  1.19.4.6     joerg  * 	+ Unregisters an event from the specified sysmon envsys device.
    228       1.1   xtraeme  */
    229       1.1   xtraeme int
    230  1.19.4.6     joerg sme_event_unregister(struct sysmon_envsys *sme, const char *sensor, int type)
    231       1.1   xtraeme {
    232       1.1   xtraeme 	sme_event_t *see;
    233       1.1   xtraeme 	bool found = false;
    234       1.1   xtraeme 
    235  1.19.4.3     joerg 	KASSERT(mutex_owned(&sme_mtx));
    236       1.1   xtraeme 	KASSERT(sensor != NULL);
    237       1.1   xtraeme 
    238  1.19.4.6     joerg 	LIST_FOREACH(see, &sme->sme_events_list, see_list) {
    239  1.19.4.6     joerg 		if (strcmp(see->see_pes.pes_sensname, sensor) == 0) {
    240  1.19.4.6     joerg 			if (see->see_type == type) {
    241       1.1   xtraeme 				found = true;
    242       1.1   xtraeme 				break;
    243       1.1   xtraeme 			}
    244       1.1   xtraeme 		}
    245       1.1   xtraeme 	}
    246       1.1   xtraeme 
    247  1.19.4.3     joerg 	if (!found)
    248       1.1   xtraeme 		return EINVAL;
    249       1.1   xtraeme 
    250      1.15   xtraeme 	while (see->see_flags & SME_EVENT_WORKING)
    251  1.19.4.2     joerg 		cv_wait(&sme_cv, &sme_mtx);
    252      1.15   xtraeme 
    253       1.1   xtraeme 	DPRINTF(("%s: removing dev=%s sensor=%s type=%d\n",
    254  1.19.4.6     joerg 	    __func__, see->see_pes.pes_dvname, sensor, type));
    255       1.1   xtraeme 	LIST_REMOVE(see, see_list);
    256       1.1   xtraeme 	/*
    257       1.1   xtraeme 	 * So the events list is empty, we'll do the following:
    258       1.1   xtraeme 	 *
    259       1.9   xtraeme 	 * 	- stop and destroy the callout.
    260       1.1   xtraeme 	 * 	- destroy the workqueue.
    261       1.1   xtraeme 	 */
    262  1.19.4.6     joerg 	if (LIST_EMPTY(&sme->sme_events_list)) {
    263  1.19.4.6     joerg 		mutex_enter(&sme_events_mtx);
    264  1.19.4.6     joerg 		sme_events_destroy(sme);
    265  1.19.4.6     joerg 		mutex_exit(&sme_events_mtx);
    266  1.19.4.1  jmcneill 	}
    267       1.1   xtraeme 
    268  1.19.4.1  jmcneill 	kmem_free(see, sizeof(*see));
    269       1.1   xtraeme 	return 0;
    270       1.1   xtraeme }
    271       1.1   xtraeme 
    272       1.1   xtraeme /*
    273       1.1   xtraeme  * sme_event_drvadd:
    274       1.1   xtraeme  *
    275  1.19.4.6     joerg  * 	+ Registers a new event for a device that had enabled any of
    276  1.19.4.6     joerg  * 	  the monitoring flags in the driver.
    277       1.1   xtraeme  */
    278       1.1   xtraeme void
    279       1.1   xtraeme sme_event_drvadd(void *arg)
    280       1.1   xtraeme {
    281       1.1   xtraeme 	sme_event_drv_t *sed_t = arg;
    282       1.2   xtraeme 	int error = 0;
    283       1.1   xtraeme 
    284       1.1   xtraeme 	KASSERT(sed_t != NULL);
    285       1.1   xtraeme 
    286       1.1   xtraeme #define SEE_REGEVENT(a, b, c)						\
    287       1.1   xtraeme do {									\
    288  1.19.4.6     joerg 	if (sed_t->sed_edata->flags & (a)) {				\
    289  1.19.4.3     joerg 		char str[ENVSYS_DESCLEN] = "monitoring-state-";		\
    290       1.1   xtraeme 									\
    291  1.19.4.6     joerg 		sysmon_envsys_acquire(sed_t->sed_sme);			\
    292  1.19.4.6     joerg 		error = sme_event_register(sed_t->sed_sdict,		\
    293  1.19.4.6     joerg 				      sed_t->sed_edata,			\
    294  1.19.4.6     joerg 				      sed_t->sed_sme,			\
    295       1.2   xtraeme 				      NULL,				\
    296       1.2   xtraeme 				      0,				\
    297       1.2   xtraeme 				      (b),				\
    298  1.19.4.6     joerg 				      sed_t->sed_powertype);		\
    299       1.2   xtraeme 		if (error && error != EEXIST)				\
    300       1.2   xtraeme 			printf("%s: failed to add event! "		\
    301       1.2   xtraeme 			    "error=%d sensor=%s event=%s\n",		\
    302  1.19.4.6     joerg 			    __func__, error,				\
    303  1.19.4.6     joerg 			    sed_t->sed_edata->desc, (c));		\
    304       1.2   xtraeme 		else {							\
    305       1.2   xtraeme 			(void)strlcat(str, (c), sizeof(str));		\
    306  1.19.4.6     joerg 			prop_dictionary_set_bool(sed_t->sed_sdict,	\
    307       1.2   xtraeme 						 str,			\
    308       1.2   xtraeme 						 true);			\
    309       1.2   xtraeme 		}							\
    310  1.19.4.6     joerg 		sysmon_envsys_release(sed_t->sed_sme);			\
    311       1.1   xtraeme 	}								\
    312       1.1   xtraeme } while (/* CONSTCOND */ 0)
    313       1.1   xtraeme 
    314  1.19.4.6     joerg 	mutex_enter(&sme_mtx);
    315       1.1   xtraeme 	SEE_REGEVENT(ENVSYS_FMONCRITICAL,
    316       1.1   xtraeme 		     PENVSYS_EVENT_CRITICAL,
    317       1.1   xtraeme 		     "critical");
    318       1.1   xtraeme 
    319       1.1   xtraeme 	SEE_REGEVENT(ENVSYS_FMONCRITUNDER,
    320       1.1   xtraeme 		     PENVSYS_EVENT_CRITUNDER,
    321       1.1   xtraeme 		     "critunder");
    322       1.1   xtraeme 
    323       1.1   xtraeme 	SEE_REGEVENT(ENVSYS_FMONCRITOVER,
    324       1.1   xtraeme 		     PENVSYS_EVENT_CRITOVER,
    325       1.1   xtraeme 		     "critover");
    326       1.1   xtraeme 
    327       1.1   xtraeme 	SEE_REGEVENT(ENVSYS_FMONWARNUNDER,
    328       1.1   xtraeme 		     PENVSYS_EVENT_WARNUNDER,
    329       1.1   xtraeme 		     "warnunder");
    330       1.1   xtraeme 
    331       1.1   xtraeme 	SEE_REGEVENT(ENVSYS_FMONWARNOVER,
    332       1.1   xtraeme 		     PENVSYS_EVENT_WARNOVER,
    333       1.1   xtraeme 		     "warnover");
    334       1.1   xtraeme 
    335  1.19.4.2     joerg 	SEE_REGEVENT(ENVSYS_FMONSTCHANGED,
    336  1.19.4.2     joerg 		     PENVSYS_EVENT_STATE_CHANGED,
    337  1.19.4.2     joerg 		     "state-changed");
    338  1.19.4.6     joerg 	mutex_exit(&sme_mtx);
    339       1.1   xtraeme 
    340  1.19.4.6     joerg 	/*
    341  1.19.4.6     joerg 	 * we are done, free memory now.
    342  1.19.4.6     joerg 	 */
    343       1.1   xtraeme 	kmem_free(sed_t, sizeof(*sed_t));
    344       1.1   xtraeme }
    345       1.1   xtraeme 
    346       1.1   xtraeme /*
    347       1.1   xtraeme  * sme_events_init:
    348       1.1   xtraeme  *
    349  1.19.4.6     joerg  * 	+ Initialize the events framework for this device.
    350       1.1   xtraeme  */
    351       1.1   xtraeme int
    352  1.19.4.6     joerg sme_events_init(struct sysmon_envsys *sme)
    353       1.1   xtraeme {
    354       1.1   xtraeme 	int error;
    355  1.19.4.6     joerg 	uint64_t timo;
    356  1.19.4.6     joerg 
    357  1.19.4.6     joerg 	KASSERT(sme != NULL);
    358  1.19.4.6     joerg 	KASSERT(mutex_owned(&sme_events_mtx));
    359       1.1   xtraeme 
    360  1.19.4.6     joerg 	if (sme->sme_events_timeout)
    361  1.19.4.6     joerg 		timo = sme->sme_events_timeout * hz;
    362  1.19.4.6     joerg 	else
    363  1.19.4.6     joerg 		timo = SME_EVTIMO;
    364  1.19.4.1  jmcneill 
    365  1.19.4.6     joerg 	error = workqueue_create(&sme->sme_wq, sme->sme_name,
    366  1.19.4.6     joerg 	    sme_events_worker, sme, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE);
    367       1.1   xtraeme 	if (error)
    368       1.1   xtraeme 		goto out;
    369       1.1   xtraeme 
    370  1.19.4.6     joerg 	callout_init(&sme->sme_callout, CALLOUT_MPSAFE);
    371  1.19.4.6     joerg 	callout_setfunc(&sme->sme_callout, sme_events_check, sme);
    372  1.19.4.6     joerg 	callout_schedule(&sme->sme_callout, timo);
    373  1.19.4.6     joerg 	sme->sme_flags |= SME_CALLOUT_INITIALIZED;
    374  1.19.4.6     joerg 	DPRINTF(("%s: events framework initialized for '%s'\n",
    375  1.19.4.6     joerg 	    __func__, sme->sme_name));
    376       1.1   xtraeme 
    377       1.1   xtraeme out:
    378       1.1   xtraeme 	return error;
    379       1.1   xtraeme }
    380       1.1   xtraeme 
    381       1.1   xtraeme /*
    382      1.18   xtraeme  * sme_events_destroy:
    383      1.18   xtraeme  *
    384  1.19.4.6     joerg  * 	+ Destroys the events framework for this device: the workqueue and the
    385  1.19.4.6     joerg  * 	  callout are stopped/destroyed because the queue is empty.
    386      1.18   xtraeme  */
    387      1.18   xtraeme void
    388  1.19.4.6     joerg sme_events_destroy(struct sysmon_envsys *sme)
    389      1.18   xtraeme {
    390  1.19.4.6     joerg 	KASSERT(mutex_owned(&sme_events_mtx));
    391  1.19.4.1  jmcneill 
    392  1.19.4.6     joerg 	callout_stop(&sme->sme_callout);
    393  1.19.4.6     joerg 	sme->sme_flags &= ~SME_CALLOUT_INITIALIZED;
    394  1.19.4.6     joerg 	DPRINTF(("%s: events framework destroyed for '%s'\n",
    395  1.19.4.6     joerg 	    __func__, sme->sme_name));
    396  1.19.4.6     joerg 	callout_destroy(&sme->sme_callout);
    397  1.19.4.6     joerg 	workqueue_destroy(sme->sme_wq);
    398      1.18   xtraeme }
    399      1.18   xtraeme 
    400      1.18   xtraeme /*
    401       1.1   xtraeme  * sme_events_check:
    402       1.1   xtraeme  *
    403  1.19.4.6     joerg  * 	+ Runs the work on the passed sysmon envsys device for all
    404  1.19.4.6     joerg  * 	  registered events.
    405       1.1   xtraeme  */
    406       1.1   xtraeme void
    407       1.1   xtraeme sme_events_check(void *arg)
    408       1.1   xtraeme {
    409  1.19.4.6     joerg 	struct sysmon_envsys *sme = arg;
    410       1.1   xtraeme 	sme_event_t *see;
    411  1.19.4.6     joerg 	uint64_t timo;
    412  1.19.4.6     joerg 
    413  1.19.4.6     joerg 	KASSERT(sme != NULL);
    414  1.19.4.6     joerg 
    415  1.19.4.6     joerg 	mutex_enter(&sme_callout_mtx);
    416  1.19.4.6     joerg 
    417  1.19.4.6     joerg 	LIST_FOREACH(see, &sme->sme_events_list, see_list)
    418  1.19.4.6     joerg 		workqueue_enqueue(sme->sme_wq, &see->see_wk, NULL);
    419       1.1   xtraeme 
    420  1.19.4.4     joerg 	/*
    421  1.19.4.4     joerg 	 * Now that the events list was checked, reset the refresh value.
    422  1.19.4.4     joerg 	 */
    423  1.19.4.6     joerg 	LIST_FOREACH(see, &sme->sme_events_list, see_list)
    424  1.19.4.4     joerg 		see->see_flags &= ~SME_EVENT_REFRESHED;
    425  1.19.4.4     joerg 
    426  1.19.4.6     joerg 	if (sme->sme_events_timeout)
    427  1.19.4.6     joerg 		timo = sme->sme_events_timeout * hz;
    428  1.19.4.6     joerg 	else
    429  1.19.4.6     joerg 		timo = SME_EVTIMO;
    430  1.19.4.6     joerg 
    431  1.19.4.4     joerg 	if (!sysmon_low_power)
    432  1.19.4.6     joerg 		callout_schedule(&sme->sme_callout, timo);
    433  1.19.4.6     joerg 
    434  1.19.4.6     joerg 	mutex_exit(&sme_callout_mtx);
    435       1.1   xtraeme }
    436       1.1   xtraeme 
    437       1.1   xtraeme /*
    438       1.1   xtraeme  * sme_events_worker:
    439       1.1   xtraeme  *
    440       1.1   xtraeme  * 	+ workqueue thread that checks if there's a critical condition
    441       1.1   xtraeme  * 	  and sends an event if the condition was triggered.
    442       1.1   xtraeme  */
    443       1.1   xtraeme void
    444       1.1   xtraeme sme_events_worker(struct work *wk, void *arg)
    445       1.1   xtraeme {
    446  1.19.4.2     joerg 	const struct sme_description_table *sdt = NULL;
    447      1.10   xtraeme 	const struct sme_sensor_event *sse = sme_sensor_event;
    448       1.1   xtraeme 	sme_event_t *see = (void *)wk;
    449       1.1   xtraeme 	struct sysmon_envsys *sme;
    450  1.19.4.2     joerg 	int i, state, error;
    451       1.1   xtraeme 
    452       1.1   xtraeme 	KASSERT(wk == &see->see_wk);
    453  1.19.4.6     joerg 	KASSERT(see != NULL);
    454       1.1   xtraeme 
    455  1.19.4.2     joerg 	state = error = 0;
    456  1.19.4.2     joerg 
    457  1.19.4.2     joerg 	mutex_enter(&sme_mtx);
    458      1.15   xtraeme 	see->see_flags |= SME_EVENT_WORKING;
    459  1.19.4.6     joerg 	sme = see->see_sme;
    460       1.1   xtraeme 
    461       1.1   xtraeme 	/*
    462  1.19.4.4     joerg 	 * refresh the sensor that was marked with a critical event
    463  1.19.4.4     joerg 	 * only if it wasn't refreshed before or if the driver doesn't
    464  1.19.4.4     joerg 	 * use its own method for refreshing.
    465       1.1   xtraeme 	 */
    466  1.19.4.6     joerg 	if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) {
    467  1.19.4.4     joerg 		if ((see->see_flags & SME_EVENT_REFRESHED) == 0) {
    468  1.19.4.6     joerg 			(*sme->sme_refresh)(sme, see->see_edata);
    469  1.19.4.4     joerg 			see->see_flags |= SME_EVENT_REFRESHED;
    470  1.19.4.4     joerg 		}
    471       1.1   xtraeme 	}
    472       1.1   xtraeme 
    473  1.19.4.6     joerg 	DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d units=%d value_cur=%d\n",
    474  1.19.4.6     joerg 	    __func__, sme->sme_name, see->see_edata->desc,
    475  1.19.4.6     joerg 	    see->see_edata->sensor,
    476  1.19.4.6     joerg 	    see->see_edata->units, see->see_edata->value_cur));
    477       1.1   xtraeme 
    478      1.10   xtraeme #define SME_SEND_NORMALEVENT()						\
    479      1.10   xtraeme do {									\
    480  1.19.4.6     joerg 	see->see_evsent = false;					\
    481  1.19.4.6     joerg 	sysmon_penvsys_event(&see->see_pes, PENVSYS_EVENT_NORMAL);	\
    482      1.10   xtraeme } while (/* CONSTCOND */ 0)
    483      1.10   xtraeme 
    484      1.10   xtraeme #define SME_SEND_EVENT(type)						\
    485       1.1   xtraeme do {									\
    486  1.19.4.6     joerg 	see->see_evsent = true;						\
    487  1.19.4.6     joerg 	sysmon_penvsys_event(&see->see_pes, (type));			\
    488       1.1   xtraeme } while (/* CONSTCOND */ 0)
    489       1.1   xtraeme 
    490  1.19.4.4     joerg 
    491  1.19.4.6     joerg 	switch (see->see_type) {
    492      1.10   xtraeme 	/*
    493      1.10   xtraeme 	 * if state is the same than the one that matches sse[i].state,
    494      1.10   xtraeme 	 * send the event...
    495      1.10   xtraeme 	 */
    496       1.1   xtraeme 	case PENVSYS_EVENT_CRITICAL:
    497       1.1   xtraeme 	case PENVSYS_EVENT_CRITUNDER:
    498       1.1   xtraeme 	case PENVSYS_EVENT_CRITOVER:
    499      1.10   xtraeme 	case PENVSYS_EVENT_WARNUNDER:
    500      1.10   xtraeme 	case PENVSYS_EVENT_WARNOVER:
    501      1.10   xtraeme 		for (i = 0; sse[i].state != -1; i++)
    502  1.19.4.6     joerg 			if (sse[i].event == see->see_type)
    503      1.10   xtraeme 				break;
    504       1.1   xtraeme 
    505  1.19.4.6     joerg 		if (see->see_evsent && see->see_edata->state == ENVSYS_SVALID)
    506      1.10   xtraeme 			SME_SEND_NORMALEVENT();
    507       1.1   xtraeme 
    508  1.19.4.6     joerg 		if (!see->see_evsent && see->see_edata->state == sse[i].state)
    509  1.19.4.6     joerg 			SME_SEND_EVENT(see->see_type);
    510       1.1   xtraeme 
    511       1.1   xtraeme 		break;
    512      1.10   xtraeme 	/*
    513      1.10   xtraeme 	 * if value_cur is lower than the limit, send the event...
    514      1.10   xtraeme 	 */
    515       1.1   xtraeme 	case PENVSYS_EVENT_BATT_USERCAP:
    516      1.10   xtraeme 	case PENVSYS_EVENT_USER_CRITMIN:
    517  1.19.4.6     joerg 		if (see->see_evsent &&
    518  1.19.4.6     joerg 		    see->see_edata->value_cur > see->see_critval)
    519      1.10   xtraeme 			SME_SEND_NORMALEVENT();
    520       1.1   xtraeme 
    521  1.19.4.6     joerg 		if (!see->see_evsent &&
    522  1.19.4.6     joerg 		    see->see_edata->value_cur < see->see_critval)
    523  1.19.4.6     joerg 			SME_SEND_EVENT(see->see_type);
    524       1.1   xtraeme 
    525       1.1   xtraeme 		break;
    526      1.10   xtraeme 	/*
    527      1.10   xtraeme 	 * if value_cur is higher than the limit, send the event...
    528      1.10   xtraeme 	 */
    529       1.1   xtraeme 	case PENVSYS_EVENT_USER_CRITMAX:
    530  1.19.4.6     joerg 		if (see->see_evsent &&
    531  1.19.4.6     joerg 		    see->see_edata->value_cur < see->see_critval)
    532      1.10   xtraeme 			SME_SEND_NORMALEVENT();
    533       1.1   xtraeme 
    534  1.19.4.6     joerg 		if (!see->see_evsent &&
    535  1.19.4.6     joerg 		    see->see_edata->value_cur > see->see_critval)
    536  1.19.4.6     joerg 			SME_SEND_EVENT(see->see_type);
    537       1.1   xtraeme 
    538       1.1   xtraeme 		break;
    539      1.10   xtraeme 	/*
    540  1.19.4.2     joerg 	 * if value_cur is not normal (battery) or online (drive),
    541  1.19.4.2     joerg 	 * send the event...
    542      1.10   xtraeme 	 */
    543  1.19.4.2     joerg 	case PENVSYS_EVENT_STATE_CHANGED:
    544  1.19.4.6     joerg 		/*
    545  1.19.4.6     joerg 		 * the state has not been changed, just ignore the event.
    546  1.19.4.6     joerg 		 */
    547  1.19.4.6     joerg 		if (see->see_edata->value_cur == see->see_evsent)
    548       1.1   xtraeme 			break;
    549       1.1   xtraeme 
    550  1.19.4.6     joerg 		switch (see->see_edata->units) {
    551  1.19.4.2     joerg 		case ENVSYS_DRIVE:
    552  1.19.4.2     joerg 			sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
    553  1.19.4.2     joerg 			state = ENVSYS_DRIVE_ONLINE;
    554  1.19.4.2     joerg 			break;
    555  1.19.4.5  jmcneill 		case ENVSYS_BATTERY_CAPACITY:
    556  1.19.4.2     joerg 			sdt =
    557  1.19.4.5  jmcneill 			    sme_get_description_table(SME_DESC_BATTERY_CAPACITY);
    558  1.19.4.5  jmcneill 			state = ENVSYS_BATTERY_CAPACITY_NORMAL;
    559  1.19.4.2     joerg 			break;
    560  1.19.4.2     joerg 		default:
    561  1.19.4.2     joerg 			panic("%s: invalid units for ENVSYS_FMONSTCHANGED",
    562  1.19.4.2     joerg 			    __func__);
    563  1.19.4.2     joerg 		}
    564  1.19.4.2     joerg 
    565  1.19.4.2     joerg 		for (i = 0; sdt[i].type != -1; i++)
    566  1.19.4.6     joerg 			if (sdt[i].type == see->see_edata->value_cur)
    567       1.1   xtraeme 				break;
    568       1.1   xtraeme 
    569  1.19.4.6     joerg 		/*
    570  1.19.4.6     joerg 		 * copy current state description.
    571  1.19.4.6     joerg 		 */
    572  1.19.4.6     joerg 		(void)strlcpy(see->see_pes.pes_statedesc, sdt[i].desc,
    573  1.19.4.6     joerg 		    sizeof(see->see_pes.pes_statedesc));
    574       1.1   xtraeme 
    575  1.19.4.6     joerg 		/*
    576  1.19.4.6     joerg 		 * state is ok again... send a normal event.
    577  1.19.4.6     joerg 		 */
    578  1.19.4.6     joerg 		if (see->see_evsent && see->see_edata->value_cur == state)
    579      1.10   xtraeme 			SME_SEND_NORMALEVENT();
    580       1.1   xtraeme 
    581  1.19.4.6     joerg 		/*
    582  1.19.4.6     joerg 		 * state has been changed... send event.
    583  1.19.4.6     joerg 		 */
    584  1.19.4.6     joerg 		if (see->see_evsent || see->see_edata->value_cur != state) {
    585  1.19.4.6     joerg 			/*
    586  1.19.4.6     joerg 			 * save current drive state.
    587  1.19.4.6     joerg 			 */
    588  1.19.4.6     joerg 			see->see_evsent = see->see_edata->value_cur;
    589  1.19.4.6     joerg 			sysmon_penvsys_event(&see->see_pes, see->see_type);
    590       1.1   xtraeme 		}
    591       1.1   xtraeme 
    592  1.19.4.4     joerg 		/*
    593  1.19.4.4     joerg 		 * Check if the system is running in low power and send the
    594  1.19.4.4     joerg 		 * event to powerd (if running) or shutdown the system
    595  1.19.4.4     joerg 		 * otherwise.
    596  1.19.4.4     joerg 		 */
    597  1.19.4.4     joerg 		if (!sysmon_low_power && sme_event_check_low_power()) {
    598  1.19.4.4     joerg 			struct penvsys_state pes;
    599  1.19.4.4     joerg 
    600  1.19.4.6     joerg 			/*
    601  1.19.4.6     joerg 			 * Stop the callout and send the 'low-power' event.
    602  1.19.4.6     joerg 			 */
    603  1.19.4.6     joerg 			sysmon_low_power = true;
    604  1.19.4.6     joerg 			callout_stop(&sme->sme_callout);
    605  1.19.4.4     joerg 			pes.pes_type = PENVSYS_TYPE_BATTERY;
    606  1.19.4.4     joerg 			sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER);
    607  1.19.4.4     joerg 		}
    608       1.1   xtraeme 		break;
    609       1.1   xtraeme 	}
    610  1.19.4.6     joerg 
    611      1.15   xtraeme 	see->see_flags &= ~SME_EVENT_WORKING;
    612  1.19.4.2     joerg 	cv_broadcast(&sme_cv);
    613  1.19.4.2     joerg 	mutex_exit(&sme_mtx);
    614       1.1   xtraeme }
    615  1.19.4.4     joerg 
    616  1.19.4.5  jmcneill static bool
    617  1.19.4.4     joerg sme_event_check_low_power(void)
    618  1.19.4.4     joerg {
    619  1.19.4.4     joerg 	struct sysmon_envsys *sme;
    620  1.19.4.4     joerg 	envsys_data_t *edata;
    621  1.19.4.5  jmcneill 	bool battery, batterycap, batterycharge;
    622  1.19.4.4     joerg 
    623  1.19.4.4     joerg 	KASSERT(mutex_owned(&sme_mtx));
    624  1.19.4.4     joerg 
    625  1.19.4.5  jmcneill 	battery = batterycap = batterycharge = false;
    626  1.19.4.5  jmcneill 
    627  1.19.4.4     joerg 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list)
    628  1.19.4.4     joerg 		if (sme->sme_class == SME_CLASS_ACADAPTER)
    629  1.19.4.4     joerg 			break;
    630  1.19.4.4     joerg 
    631  1.19.4.4     joerg 	/*
    632  1.19.4.4     joerg 	 * No AC Adapter devices were found, do nothing.
    633  1.19.4.4     joerg 	 */
    634  1.19.4.4     joerg 	if (!sme)
    635  1.19.4.5  jmcneill 		return false;
    636  1.19.4.4     joerg 
    637  1.19.4.5  jmcneill 	/*
    638  1.19.4.5  jmcneill 	 * If there's an AC Adapter connected, there's no need
    639  1.19.4.5  jmcneill 	 * to continue...
    640  1.19.4.5  jmcneill 	 */
    641  1.19.4.6     joerg 	TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    642  1.19.4.4     joerg 		if (edata->units == ENVSYS_INDICATOR) {
    643  1.19.4.5  jmcneill 			if (edata->value_cur)
    644  1.19.4.5  jmcneill 				return false;
    645  1.19.4.4     joerg 		}
    646  1.19.4.4     joerg 	}
    647  1.19.4.5  jmcneill 
    648  1.19.4.4     joerg 	/*
    649  1.19.4.5  jmcneill 	 * Check for battery devices and its state.
    650  1.19.4.4     joerg 	 */
    651  1.19.4.4     joerg 	LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
    652  1.19.4.5  jmcneill 		if (sme->sme_class != SME_CLASS_BATTERY)
    653  1.19.4.5  jmcneill 			continue;
    654  1.19.4.5  jmcneill 
    655  1.19.4.5  jmcneill 		/*
    656  1.19.4.5  jmcneill 		 * We've found a battery device...
    657  1.19.4.5  jmcneill 		 */
    658  1.19.4.5  jmcneill 		battery = true;
    659  1.19.4.6     joerg 		TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
    660  1.19.4.5  jmcneill 			if (edata->units == ENVSYS_BATTERY_CAPACITY) {
    661  1.19.4.5  jmcneill 				batterycap = true;
    662  1.19.4.5  jmcneill 				if (!sme_battery_critical(edata))
    663  1.19.4.5  jmcneill 					return false;
    664  1.19.4.5  jmcneill 			} else if (edata->units == ENVSYS_BATTERY_CHARGE) {
    665  1.19.4.5  jmcneill 				batterycharge = true;
    666  1.19.4.5  jmcneill 				if (edata->value_cur)
    667  1.19.4.5  jmcneill 					return false;
    668  1.19.4.4     joerg 			}
    669  1.19.4.4     joerg 		}
    670  1.19.4.4     joerg 	}
    671  1.19.4.5  jmcneill 	if (!battery || !batterycap || !batterycharge)
    672  1.19.4.5  jmcneill 		return false;
    673  1.19.4.4     joerg 
    674  1.19.4.4     joerg 	/*
    675  1.19.4.5  jmcneill 	 * All batteries in low/critical capacity and discharging.
    676  1.19.4.4     joerg 	 */
    677  1.19.4.5  jmcneill 	return true;
    678  1.19.4.5  jmcneill }
    679  1.19.4.4     joerg 
    680  1.19.4.5  jmcneill static bool
    681  1.19.4.5  jmcneill sme_battery_critical(envsys_data_t *edata)
    682  1.19.4.5  jmcneill {
    683  1.19.4.5  jmcneill 	if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL ||
    684  1.19.4.5  jmcneill 	    edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW)
    685  1.19.4.5  jmcneill 		return true;
    686  1.19.4.5  jmcneill 
    687  1.19.4.5  jmcneill 	return false;
    688  1.19.4.4     joerg }
    689