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