sysmon_envsys_events.c revision 1.19 1 /* $NetBSD: sysmon_envsys_events.c,v 1.19 2007/07/23 17:51:17 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.19 2007/07/23 17:51:17 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/sysctl.h>
52 #include <sys/systm.h>
53 #include <sys/proc.h>
54 #include <sys/mutex.h>
55 #include <sys/kmem.h>
56 #include <sys/callout.h>
57
58 #include <dev/sysmon/sysmonvar.h>
59 #include <dev/sysmon/sysmon_envsysvar.h>
60
61 struct sme_sensor_state {
62 int type;
63 const char *desc;
64 };
65
66 struct sme_sensor_event {
67 int state;
68 int event;
69 };
70
71 static const struct sme_sensor_state sme_sensor_drive_state[] = {
72 { ENVSYS_DRIVE_EMPTY, "drive state is unknown" },
73 { ENVSYS_DRIVE_READY, "drive is ready" },
74 { ENVSYS_DRIVE_POWERUP, "drive is powering up" },
75 { ENVSYS_DRIVE_ONLINE, "drive is online" },
76 { ENVSYS_DRIVE_IDLE, "drive is idle" },
77 { ENVSYS_DRIVE_ACTIVE, "drive is active" },
78 { ENVSYS_DRIVE_REBUILD, "drive is rebuilding" },
79 { ENVSYS_DRIVE_POWERDOWN, "drive is powering down" },
80 { ENVSYS_DRIVE_FAIL, "drive failed" },
81 { ENVSYS_DRIVE_PFAIL, "drive degraded" },
82 { -1, "unknown" }
83 };
84
85 static const struct sme_sensor_event sme_sensor_event[] = {
86 { ENVSYS_SVALID, PENVSYS_EVENT_NORMAL },
87 { ENVSYS_SCRITICAL, PENVSYS_EVENT_CRITICAL },
88 { ENVSYS_SCRITOVER, PENVSYS_EVENT_CRITOVER },
89 { ENVSYS_SCRITUNDER, PENVSYS_EVENT_CRITUNDER },
90 { ENVSYS_SWARNOVER, PENVSYS_EVENT_WARNOVER },
91 { ENVSYS_SWARNUNDER, PENVSYS_EVENT_WARNUNDER },
92 { -1, -1 }
93 };
94
95 static struct workqueue *seewq;
96 static struct callout seeco;
97 static bool sme_events_initialized = false;
98 kmutex_t sme_mtx, sme_list_mtx, sme_event_mtx, sme_event_init_mtx;
99 kcondvar_t sme_event_cv;
100
101 /* 10 seconds of timeout for the callout */
102 static int sme_events_timeout = 10;
103 static int sme_events_timeout_sysctl(SYSCTLFN_PROTO);
104 #define SME_EVTIMO (sme_events_timeout * hz)
105
106 /*
107 * sysctl(9) stuff to handle the refresh value in the callout
108 * function.
109 */
110 static int
111 sme_events_timeout_sysctl(SYSCTLFN_ARGS)
112 {
113 struct sysctlnode node;
114 int timo, error;
115
116 node = *rnode;
117 timo = sme_events_timeout;
118 node.sysctl_data = &timo;
119
120 error = sysctl_lookup(SYSCTLFN_CALL(&node));
121 if (error || newp == NULL)
122 return error;
123
124 /* min 1s */
125 if (timo < 1)
126 return EINVAL;
127
128 sme_events_timeout = timo;
129 return 0;
130 }
131
132 SYSCTL_SETUP(sysctl_kern_envsys_timeout_setup, "sysctl kern.envsys subtree")
133 {
134 const struct sysctlnode *node, *envsys_node;
135
136 sysctl_createv(clog, 0, NULL, &node,
137 CTLFLAG_PERMANENT,
138 CTLTYPE_NODE, "kern", NULL,
139 NULL, 0, NULL, 0,
140 CTL_KERN, CTL_EOL);
141
142 sysctl_createv(clog, 0, &node, &envsys_node,
143 0,
144 CTLTYPE_NODE, "envsys", NULL,
145 NULL, 0, NULL, 0,
146 CTL_CREATE, CTL_EOL);
147
148 sysctl_createv(clog, 0, &envsys_node, &node,
149 CTLFLAG_READWRITE,
150 CTLTYPE_INT, "refresh_value",
151 SYSCTL_DESCR("wait time in seconds to refresh "
152 "sensors being monitored"),
153 sme_events_timeout_sysctl, 0, &sme_events_timeout, 0,
154 CTL_CREATE, CTL_EOL);
155 }
156
157
158 /*
159 * sme_event_register:
160 *
161 * + Registers a sysmon envsys event.
162 * + Creates a new sysmon envsys event.
163 */
164 int
165 sme_event_register(sme_event_t *see)
166 {
167 sme_event_t *lsee;
168 int error = 0;
169
170 KASSERT(see != NULL);
171
172 mutex_enter(&sme_event_mtx);
173 /*
174 * Ensure that we don't add events for the same sensor
175 * and with the same type.
176 */
177 LIST_FOREACH(lsee, &sme_events_list, see_list) {
178 if (strcmp(lsee->pes.pes_sensname,
179 see->pes.pes_sensname) == 0) {
180 if (lsee->type == see->type) {
181 DPRINTF(("%s: dev=%s sensor=%s type=%d "
182 "(already exists)\n", __func__,
183 see->pes.pes_dvname,
184 see->pes.pes_sensname, see->type));
185 mutex_exit(&sme_event_mtx);
186 return EEXIST;
187 }
188 }
189 }
190
191 DPRINTF(("%s: dev=%s sensor=%s snum=%d type=%d "
192 "critval=%" PRIu32 "\n", __func__,
193 see->pes.pes_dvname, see->pes.pes_sensname,
194 see->snum, see->type, see->critval));
195
196 LIST_INSERT_HEAD(&sme_events_list, see, see_list);
197 /*
198 * Initialize the events framework if it wasn't initialized
199 * before.
200 */
201 mutex_enter(&sme_event_init_mtx);
202 if (sme_events_initialized == false)
203 error = sme_events_init();
204 mutex_exit(&sme_event_init_mtx);
205
206 mutex_exit(&sme_event_mtx);
207 return error;
208 }
209
210 /*
211 * sme_event_unregister_all:
212 *
213 * + Unregisters all sysmon envsys events associated with a
214 * sysmon envsys device.
215 */
216 void
217 sme_event_unregister_all(struct sysmon_envsys *sme)
218 {
219 sme_event_t *see;
220 int evcounter = 0;
221
222 KASSERT(sme != NULL);
223
224 mutex_enter(&sme_event_mtx);
225 LIST_FOREACH(see, &sme_events_list, see_list) {
226 if (strcmp(see->pes.pes_dvname, sme->sme_name) == 0)
227 evcounter++;
228 }
229
230 DPRINTF(("%s: total events %d (%s)\n", __func__,
231 evcounter, sme->sme_name));
232
233 while ((see = LIST_FIRST(&sme_events_list)) != NULL) {
234 if (evcounter == 0)
235 break;
236
237 if (strcmp(see->pes.pes_dvname, sme->sme_name) == 0) {
238 DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
239 see->pes.pes_sensname, see->type, sme->sme_name));
240
241 while (see->see_flags & SME_EVENT_WORKING)
242 cv_wait(&sme_event_cv, &sme_event_mtx);
243
244 LIST_REMOVE(see, see_list);
245 mutex_exit(&sme_event_mtx);
246 kmem_free(see, sizeof(*see));
247 mutex_enter(&sme_event_mtx);
248 evcounter--;
249 }
250 }
251
252 if (LIST_EMPTY(&sme_events_list))
253 sme_events_destroy();
254
255 mutex_exit(&sme_event_mtx);
256 }
257
258 /*
259 * sme_event_unregister:
260 *
261 * + Unregisters a sysmon envsys event.
262 */
263 int
264 sme_event_unregister(const char *sensor, int type)
265 {
266 sme_event_t *see;
267 bool found = false;
268
269 KASSERT(sensor != NULL);
270
271 mutex_enter(&sme_event_mtx);
272 LIST_FOREACH(see, &sme_events_list, see_list) {
273 if (strcmp(see->pes.pes_sensname, sensor) == 0) {
274 if (see->type == type) {
275 found = true;
276 break;
277 }
278 }
279 }
280
281 if (!found) {
282 mutex_exit(&sme_event_mtx);
283 return EINVAL;
284 }
285
286 while (see->see_flags & SME_EVENT_WORKING)
287 cv_wait(&sme_event_cv, &sme_event_mtx);
288
289 DPRINTF(("%s: removing dev=%s sensor=%s type=%d\n",
290 __func__, see->pes.pes_dvname, sensor, type));
291 LIST_REMOVE(see, see_list);
292 mutex_exit(&sme_event_mtx);
293 kmem_free(see, sizeof(*see));
294
295 /*
296 * So the events list is empty, we'll do the following:
297 *
298 * - stop and destroy the callout.
299 * - destroy the workqueue.
300 */
301 mutex_enter(&sme_event_mtx);
302 if (LIST_EMPTY(&sme_events_list))
303 sme_events_destroy();
304 mutex_exit(&sme_event_mtx);
305
306 return 0;
307 }
308
309 /*
310 * sme_event_drvadd:
311 *
312 * + Adds a new sysmon envsys event for a driver if a sensor
313 * has set any accepted monitoring flag.
314 */
315 void
316 sme_event_drvadd(void *arg)
317 {
318 sme_event_drv_t *sed_t = arg;
319 int error = 0;
320
321 KASSERT(sed_t != NULL);
322
323 #define SEE_REGEVENT(a, b, c) \
324 do { \
325 if (sed_t->edata->flags & (a)) { \
326 char str[32] = "monitoring-state-"; \
327 \
328 error = sme_event_add(sed_t->sdict, \
329 sed_t->edata, \
330 sed_t->sme->sme_name, \
331 NULL, \
332 0, \
333 (b), \
334 sed_t->powertype); \
335 if (error && error != EEXIST) \
336 printf("%s: failed to add event! " \
337 "error=%d sensor=%s event=%s\n", \
338 __func__, error, sed_t->edata->desc, (c)); \
339 else { \
340 mutex_enter(&sme_mtx); \
341 (void)strlcat(str, (c), sizeof(str)); \
342 prop_dictionary_set_bool(sed_t->sdict, \
343 str, \
344 true); \
345 mutex_exit(&sme_mtx); \
346 } \
347 } \
348 } while (/* CONSTCOND */ 0)
349
350 SEE_REGEVENT(ENVSYS_FMONCRITICAL,
351 PENVSYS_EVENT_CRITICAL,
352 "critical");
353
354 SEE_REGEVENT(ENVSYS_FMONCRITUNDER,
355 PENVSYS_EVENT_CRITUNDER,
356 "critunder");
357
358 SEE_REGEVENT(ENVSYS_FMONCRITOVER,
359 PENVSYS_EVENT_CRITOVER,
360 "critover");
361
362 SEE_REGEVENT(ENVSYS_FMONWARNUNDER,
363 PENVSYS_EVENT_WARNUNDER,
364 "warnunder");
365
366 SEE_REGEVENT(ENVSYS_FMONWARNOVER,
367 PENVSYS_EVENT_WARNOVER,
368 "warnover");
369
370 SEE_REGEVENT(ENVSYS_FMONDRVSTATE,
371 PENVSYS_EVENT_DRIVE_STCHANGED,
372 "drvstchanged");
373
374 /* we are done, free memory now */
375 kmem_free(sed_t, sizeof(*sed_t));
376 }
377
378 /*
379 * sme_event_add:
380 *
381 * + Initializes or updates a sysmon envsys event.
382 */
383 int
384 sme_event_add(prop_dictionary_t sdict, envsys_data_t *edata,
385 const char *drvn, const char *objkey,
386 int32_t critval, int crittype, int powertype)
387 {
388 sme_event_t *see = NULL;
389 prop_object_t obj;
390 int error = 0;
391
392 KASSERT(sdict != NULL || edata != NULL);
393
394 mutex_enter(&sme_event_mtx);
395 /* critical condition set via userland */
396 if (objkey && critval) {
397 obj = prop_dictionary_get(sdict, objkey);
398 if (obj != NULL) {
399 /*
400 * object is already in dictionary, update
401 * the critical value.
402 */
403 LIST_FOREACH(see, &sme_events_list, see_list) {
404 if (strcmp(edata->desc,
405 see->pes.pes_sensname) == 0)
406 if (crittype == see->type)
407 break;
408 }
409
410 if (see->critval != critval) {
411 see->critval = critval;
412 DPRINTF(("%s: sensor=%s type=%d "
413 "(critval updated)\n", __func__,
414 edata->desc, see->type));
415 }
416
417 mutex_exit(&sme_event_mtx);
418 goto out;
419 }
420 }
421
422 if (LIST_EMPTY(&sme_events_list)) {
423 mutex_exit(&sme_event_mtx);
424 goto register_event;
425 }
426
427 /* check if the event is already on the list */
428 LIST_FOREACH(see, &sme_events_list, see_list) {
429 if (strcmp(edata->desc, see->pes.pes_sensname) == 0)
430 if (crittype == see->type) {
431 mutex_exit(&sme_event_mtx);
432 error = EEXIST;
433 goto out;
434 }
435 }
436 mutex_exit(&sme_event_mtx);
437
438 /*
439 * object is not in dictionary, create a new
440 * sme event and assign required members.
441 */
442 register_event:
443 see = kmem_zalloc(sizeof(*see), KM_SLEEP);
444
445 mutex_enter(&sme_event_mtx);
446 see->critval = critval;
447 see->type = crittype;
448 (void)strlcpy(see->pes.pes_dvname, drvn,
449 sizeof(see->pes.pes_dvname));
450 see->pes.pes_type = powertype;
451 (void)strlcpy(see->pes.pes_sensname, edata->desc,
452 sizeof(see->pes.pes_sensname));
453 see->snum = edata->sensor;
454 mutex_exit(&sme_event_mtx);
455
456 error = sme_event_register(see);
457 if (error)
458 kmem_free(see, sizeof(*see));
459
460 out:
461 /* update the object in the dictionary */
462 if (objkey && critval) {
463 mutex_enter(&sme_event_mtx);
464 error = sme_sensor_upint32(sdict, objkey, critval);
465 mutex_exit(&sme_event_mtx);
466 }
467
468 return error;
469 }
470
471 /*
472 * sme_events_init:
473 *
474 * + Initializes the events framework.
475 */
476 int
477 sme_events_init(void)
478 {
479 int error;
480
481 error = workqueue_create(&seewq, "envsysev",
482 sme_events_worker, NULL, 0, IPL_SOFTCLOCK, 0);
483 if (error)
484 goto out;
485
486 callout_init(&seeco, 0);
487 callout_setfunc(&seeco, sme_events_check, NULL);
488 callout_schedule(&seeco, SME_EVTIMO);
489 sme_events_initialized = true;
490 DPRINTF(("%s: events framework initialized\n", __func__));
491
492 out:
493 return error;
494 }
495
496 /*
497 * sme_events_destroy:
498 *
499 * + Destroys the events framework: the workqueue and the
500 * callout are stopped and destroyed because there are not
501 * events in the queue.
502 */
503 void
504 sme_events_destroy(void)
505 {
506 mutex_enter(&sme_event_init_mtx);
507 callout_stop(&seeco);
508 callout_destroy(&seeco);
509 workqueue_destroy(seewq);
510 sme_events_initialized = false;
511 DPRINTF(("%s: events framework destroyed\n", __func__));
512 mutex_exit(&sme_event_init_mtx);
513 }
514
515 /*
516 * sme_events_check:
517 *
518 * + Runs the work on each sysmon envsys event in our
519 * workqueue periodically with callout.
520 */
521 void
522 sme_events_check(void *arg)
523 {
524 sme_event_t *see;
525
526 LIST_FOREACH(see, &sme_events_list, see_list) {
527 DPRINTFOBJ(("%s: dev=%s sensor=%s type=%d\n",
528 __func__,
529 see->pes.pes_dvname,
530 see->pes.pes_sensname,
531 see->type));
532 workqueue_enqueue(seewq, &see->see_wk, NULL);
533 }
534 callout_schedule(&seeco, SME_EVTIMO);
535 }
536
537 /*
538 * sme_events_worker:
539 *
540 * + workqueue thread that checks if there's a critical condition
541 * and sends an event if the condition was triggered.
542 */
543 void
544 sme_events_worker(struct work *wk, void *arg)
545 {
546 const struct sme_sensor_state *esds = sme_sensor_drive_state;
547 const struct sme_sensor_event *sse = sme_sensor_event;
548 sme_event_t *see = (void *)wk;
549 struct sysmon_envsys *sme;
550 envsys_data_t *edata;
551 int i, error = 0;
552
553 KASSERT(wk == &see->see_wk);
554
555 mutex_enter(&sme_event_mtx);
556 see->see_flags |= SME_EVENT_WORKING;
557
558 /*
559 * We have to find the sme device by looking
560 * at the power envsys device name.
561 */
562 mutex_enter(&sme_list_mtx);
563 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list)
564 if (strcmp(sme->sme_name, see->pes.pes_dvname) == 0)
565 break;
566 mutex_exit(&sme_list_mtx);
567
568 KASSERT(sme != NULL);
569
570 mutex_enter(&sme_mtx);
571 /* get the sensor with the index specified in see->snum */
572 edata = &sme->sme_sensor_data[see->snum];
573
574 /*
575 * refresh the sensor that was marked with a critical
576 * event.
577 */
578 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
579 error = (*sme->sme_gtredata)(sme, edata);
580 if (error) {
581 mutex_exit(&sme_mtx);
582 mutex_exit(&sme_event_mtx);
583 return;
584 }
585 }
586 mutex_exit(&sme_mtx);
587
588 DPRINTFOBJ(("%s: desc=%s sensor=%d units=%d value_cur=%d\n",
589 __func__, edata->desc, edata->sensor,
590 edata->units, edata->value_cur));
591
592 #define SME_SEND_NORMALEVENT() \
593 do { \
594 see->evsent = false; \
595 sysmon_penvsys_event(&see->pes, PENVSYS_EVENT_NORMAL); \
596 } while (/* CONSTCOND */ 0)
597
598 #define SME_SEND_EVENT(type) \
599 do { \
600 see->evsent = true; \
601 sysmon_penvsys_event(&see->pes, (type)); \
602 } while (/* CONSTCOND */ 0)
603
604 switch (see->type) {
605 /*
606 * if state is the same than the one that matches sse[i].state,
607 * send the event...
608 */
609 case PENVSYS_EVENT_CRITICAL:
610 case PENVSYS_EVENT_CRITUNDER:
611 case PENVSYS_EVENT_CRITOVER:
612 case PENVSYS_EVENT_WARNUNDER:
613 case PENVSYS_EVENT_WARNOVER:
614 for (i = 0; sse[i].state != -1; i++)
615 if (sse[i].event == see->type)
616 break;
617
618 if (see->evsent && edata->state == ENVSYS_SVALID)
619 SME_SEND_NORMALEVENT();
620
621 if (!see->evsent && edata->state == sse[i].state)
622 SME_SEND_EVENT(see->type);
623
624 break;
625 /*
626 * if value_cur is lower than the limit, send the event...
627 */
628 case PENVSYS_EVENT_BATT_USERCAP:
629 case PENVSYS_EVENT_USER_CRITMIN:
630 if (see->evsent && edata->value_cur > see->critval)
631 SME_SEND_NORMALEVENT();
632
633 if (!see->evsent && edata->value_cur < see->critval)
634 SME_SEND_EVENT(see->type);
635
636 break;
637 /*
638 * if value_cur is higher than the limit, send the event...
639 */
640 case PENVSYS_EVENT_USER_CRITMAX:
641 if (see->evsent && edata->value_cur < see->critval)
642 SME_SEND_NORMALEVENT();
643
644 if (!see->evsent && edata->value_cur > see->critval)
645 SME_SEND_EVENT(see->type);
646
647 break;
648 /*
649 * if value_cur is not ENVSYS_DRIVE_ONLINE, send the event...
650 */
651 case PENVSYS_EVENT_DRIVE_STCHANGED:
652 /* the state has not been changed, just ignore the event */
653 if (edata->value_cur == see->evsent)
654 break;
655
656 for (i = 0; esds[i].type != -1; i++)
657 if (esds[i].type == edata->value_cur)
658 break;
659
660 /* copy current state description */
661 (void)strlcpy(see->pes.pes_statedesc, esds[i].desc,
662 sizeof(see->pes.pes_statedesc));
663
664 /* state is ok again... send a normal event */
665 if (see->evsent && edata->value_cur == ENVSYS_DRIVE_ONLINE)
666 SME_SEND_NORMALEVENT();
667
668 /* something bad happened to the drive... send the event */
669 if (see->evsent || edata->value_cur != ENVSYS_DRIVE_ONLINE) {
670 /* save current drive state */
671 see->evsent = edata->value_cur;
672 sysmon_penvsys_event(&see->pes, see->type);
673 }
674
675 break;
676 }
677
678 see->see_flags &= ~SME_EVENT_WORKING;
679 cv_broadcast(&sme_event_cv);
680 mutex_exit(&sme_event_mtx);
681 }
682