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