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