sysmon_envsys_events.c revision 1.21 1 /* $NetBSD: sysmon_envsys_events.c,v 1.21 2007/08/30 23:44:32 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.21 2007/08/30 23:44:32 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_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 mutex_exit(&sme_event_mtx);
203 if (sme_events_initialized == false)
204 error = sme_events_init();
205 mutex_exit(&sme_event_init_mtx);
206
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 kmem_free(see, sizeof(*see));
246 evcounter--;
247 }
248 }
249
250 if (LIST_EMPTY(&sme_events_list)) {
251 mutex_enter(&sme_event_init_mtx);
252 mutex_exit(&sme_event_mtx);
253 sme_events_destroy();
254 mutex_exit(&sme_event_init_mtx);
255 return;
256 }
257
258 mutex_exit(&sme_event_mtx);
259 }
260
261 /*
262 * sme_event_unregister:
263 *
264 * + Unregisters a sysmon envsys event.
265 */
266 int
267 sme_event_unregister(const char *sensor, int type)
268 {
269 sme_event_t *see;
270 bool found = false;
271
272 KASSERT(sensor != NULL);
273
274 mutex_enter(&sme_event_mtx);
275 LIST_FOREACH(see, &sme_events_list, see_list) {
276 if (strcmp(see->pes.pes_sensname, sensor) == 0) {
277 if (see->type == type) {
278 found = true;
279 break;
280 }
281 }
282 }
283
284 if (!found) {
285 mutex_exit(&sme_event_mtx);
286 return EINVAL;
287 }
288
289 while (see->see_flags & SME_EVENT_WORKING)
290 cv_wait(&sme_event_cv, &sme_event_mtx);
291
292 DPRINTF(("%s: removing dev=%s sensor=%s type=%d\n",
293 __func__, see->pes.pes_dvname, sensor, type));
294 LIST_REMOVE(see, see_list);
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 if (LIST_EMPTY(&sme_events_list)) {
302 mutex_enter(&sme_event_init_mtx);
303 mutex_exit(&sme_event_mtx);
304 sme_events_destroy();
305 mutex_exit(&sme_event_init_mtx);
306 goto out;
307 }
308 mutex_exit(&sme_event_mtx);
309
310 out:
311 kmem_free(see, sizeof(*see));
312 return 0;
313 }
314
315 /*
316 * sme_event_drvadd:
317 *
318 * + Adds a new sysmon envsys event for a driver if a sensor
319 * has set any accepted monitoring flag.
320 */
321 void
322 sme_event_drvadd(void *arg)
323 {
324 sme_event_drv_t *sed_t = arg;
325 int error = 0;
326
327 KASSERT(sed_t != NULL);
328
329 #define SEE_REGEVENT(a, b, c) \
330 do { \
331 if (sed_t->edata->flags & (a)) { \
332 char str[32] = "monitoring-state-"; \
333 \
334 error = sme_event_add(sed_t->sdict, \
335 sed_t->edata, \
336 sed_t->sme->sme_name, \
337 NULL, \
338 0, \
339 (b), \
340 sed_t->powertype); \
341 if (error && error != EEXIST) \
342 printf("%s: failed to add event! " \
343 "error=%d sensor=%s event=%s\n", \
344 __func__, error, sed_t->edata->desc, (c)); \
345 else { \
346 mutex_enter(&sme_list_mtx); \
347 (void)strlcat(str, (c), sizeof(str)); \
348 prop_dictionary_set_bool(sed_t->sdict, \
349 str, \
350 true); \
351 mutex_exit(&sme_list_mtx); \
352 } \
353 } \
354 } while (/* CONSTCOND */ 0)
355
356 SEE_REGEVENT(ENVSYS_FMONCRITICAL,
357 PENVSYS_EVENT_CRITICAL,
358 "critical");
359
360 SEE_REGEVENT(ENVSYS_FMONCRITUNDER,
361 PENVSYS_EVENT_CRITUNDER,
362 "critunder");
363
364 SEE_REGEVENT(ENVSYS_FMONCRITOVER,
365 PENVSYS_EVENT_CRITOVER,
366 "critover");
367
368 SEE_REGEVENT(ENVSYS_FMONWARNUNDER,
369 PENVSYS_EVENT_WARNUNDER,
370 "warnunder");
371
372 SEE_REGEVENT(ENVSYS_FMONWARNOVER,
373 PENVSYS_EVENT_WARNOVER,
374 "warnover");
375
376 SEE_REGEVENT(ENVSYS_FMONDRVSTATE,
377 PENVSYS_EVENT_DRIVE_STCHANGED,
378 "drvstchanged");
379
380 /* we are done, free memory now */
381 kmem_free(sed_t, sizeof(*sed_t));
382 }
383
384 /*
385 * sme_event_add:
386 *
387 * + Initializes or updates a sysmon envsys event.
388 */
389 int
390 sme_event_add(prop_dictionary_t sdict, envsys_data_t *edata,
391 const char *drvn, const char *objkey,
392 int32_t critval, int crittype, int powertype)
393 {
394 sme_event_t *see = NULL;
395 prop_object_t obj;
396 int error = 0;
397
398 KASSERT(sdict != NULL || edata != NULL);
399
400 mutex_enter(&sme_event_mtx);
401 /* critical condition set via userland */
402 if (objkey && critval) {
403 obj = prop_dictionary_get(sdict, objkey);
404 if (obj != NULL) {
405 /*
406 * object is already in dictionary, update
407 * the critical value.
408 */
409 LIST_FOREACH(see, &sme_events_list, see_list) {
410 if (strcmp(edata->desc,
411 see->pes.pes_sensname) == 0)
412 if (crittype == see->type)
413 break;
414 }
415 if (see->critval != critval) {
416 see->critval = critval;
417 DPRINTF(("%s: sensor=%s type=%d "
418 "(critval updated)\n", __func__,
419 edata->desc, see->type));
420 }
421 goto out;
422 }
423 }
424
425 if (LIST_EMPTY(&sme_events_list))
426 goto register_event;
427
428 /* check if the event is already on the list */
429 LIST_FOREACH(see, &sme_events_list, see_list) {
430 if (strcmp(edata->desc, see->pes.pes_sensname) == 0)
431 if (crittype == see->type) {
432 mutex_exit(&sme_event_mtx);
433 return EEXIST;
434 }
435 }
436
437 /*
438 * object is not in dictionary, create a new
439 * sme event and assign required members.
440 */
441 register_event:
442 see = kmem_zalloc(sizeof(*see), KM_NOSLEEP);
443 if (see == NULL) {
444 mutex_exit(&sme_event_mtx);
445 return ENOMEM;
446 }
447
448 see->critval = critval;
449 see->type = crittype;
450 (void)strlcpy(see->pes.pes_dvname, drvn,
451 sizeof(see->pes.pes_dvname));
452 see->pes.pes_type = powertype;
453 (void)strlcpy(see->pes.pes_sensname, edata->desc,
454 sizeof(see->pes.pes_sensname));
455 see->snum = edata->sensor;
456 out:
457 /* update the object in the dictionary */
458 if (objkey && critval) {
459 error = sme_sensor_upint32(sdict, objkey, critval);
460 if (error) {
461 mutex_exit(&sme_event_mtx);
462 return error;
463 }
464 }
465 mutex_exit(&sme_event_mtx);
466 error = sme_event_register(see);
467 if (error)
468 kmem_free(see, sizeof(*see));
469 return error;
470 }
471
472 /*
473 * sme_events_init:
474 *
475 * + Initializes the events framework.
476 */
477 int
478 sme_events_init(void)
479 {
480 int error;
481
482 error = workqueue_create(&seewq, "envsysev",
483 sme_events_worker, NULL, 0, IPL_SOFTCLOCK, WQ_MPSAFE);
484 if (error)
485 goto out;
486
487 callout_init(&seeco, 0);
488 callout_setfunc(&seeco, sme_events_check, NULL);
489 callout_schedule(&seeco, SME_EVTIMO);
490 sme_events_initialized = true;
491 DPRINTF(("%s: events framework initialized\n", __func__));
492
493 out:
494 return error;
495 }
496
497 /*
498 * sme_events_destroy:
499 *
500 * + Destroys the events framework: the workqueue and the
501 * callout are stopped and destroyed because there are not
502 * events in the queue.
503 */
504 void
505 sme_events_destroy(void)
506 {
507 callout_stop(&seeco);
508 sme_events_initialized = false;
509 DPRINTF(("%s: events framework destroyed\n", __func__));
510 callout_destroy(&seeco);
511 workqueue_destroy(seewq);
512 }
513
514 /*
515 * sme_events_check:
516 *
517 * + Runs the work on each sysmon envsys event in our
518 * workqueue periodically with callout.
519 */
520 void
521 sme_events_check(void *arg)
522 {
523 sme_event_t *see;
524
525 LIST_FOREACH(see, &sme_events_list, see_list) {
526 DPRINTFOBJ(("%s: dev=%s sensor=%s type=%d\n",
527 __func__,
528 see->pes.pes_dvname,
529 see->pes.pes_sensname,
530 see->type));
531 workqueue_enqueue(seewq, &see->see_wk, NULL);
532 }
533 callout_schedule(&seeco, SME_EVTIMO);
534 }
535
536 /*
537 * sme_events_worker:
538 *
539 * + workqueue thread that checks if there's a critical condition
540 * and sends an event if the condition was triggered.
541 */
542 void
543 sme_events_worker(struct work *wk, void *arg)
544 {
545 const struct sme_sensor_state *esds = sme_sensor_drive_state;
546 const struct sme_sensor_event *sse = sme_sensor_event;
547 sme_event_t *see = (void *)wk;
548 struct sysmon_envsys *sme;
549 envsys_data_t *edata;
550 int i, error = 0;
551
552 KASSERT(wk == &see->see_wk);
553
554 mutex_enter(&sme_event_mtx);
555 see->see_flags |= SME_EVENT_WORKING;
556
557 /*
558 * We have to find the sme device by looking
559 * at the power envsys device name.
560 */
561 mutex_enter(&sme_list_mtx);
562 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list)
563 if (strcmp(sme->sme_name, see->pes.pes_dvname) == 0)
564 break;
565 mutex_exit(&sme_list_mtx);
566
567 KASSERT(sme != NULL);
568
569 /* get the sensor with the index specified in see->snum */
570 edata = &sme->sme_sensor_data[see->snum];
571
572 /*
573 * refresh the sensor that was marked with a critical
574 * event.
575 */
576 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
577 error = (*sme->sme_gtredata)(sme, edata);
578 if (error)
579 return;
580 }
581
582 DPRINTFOBJ(("%s: desc=%s sensor=%d units=%d value_cur=%d\n",
583 __func__, edata->desc, edata->sensor,
584 edata->units, edata->value_cur));
585
586 #define SME_SEND_NORMALEVENT() \
587 do { \
588 see->evsent = false; \
589 sysmon_penvsys_event(&see->pes, PENVSYS_EVENT_NORMAL); \
590 } while (/* CONSTCOND */ 0)
591
592 #define SME_SEND_EVENT(type) \
593 do { \
594 see->evsent = true; \
595 sysmon_penvsys_event(&see->pes, (type)); \
596 } while (/* CONSTCOND */ 0)
597
598 switch (see->type) {
599 /*
600 * if state is the same than the one that matches sse[i].state,
601 * send the event...
602 */
603 case PENVSYS_EVENT_CRITICAL:
604 case PENVSYS_EVENT_CRITUNDER:
605 case PENVSYS_EVENT_CRITOVER:
606 case PENVSYS_EVENT_WARNUNDER:
607 case PENVSYS_EVENT_WARNOVER:
608 for (i = 0; sse[i].state != -1; i++)
609 if (sse[i].event == see->type)
610 break;
611
612 if (see->evsent && edata->state == ENVSYS_SVALID)
613 SME_SEND_NORMALEVENT();
614
615 if (!see->evsent && edata->state == sse[i].state)
616 SME_SEND_EVENT(see->type);
617
618 break;
619 /*
620 * if value_cur is lower than the limit, send the event...
621 */
622 case PENVSYS_EVENT_BATT_USERCAP:
623 case PENVSYS_EVENT_USER_CRITMIN:
624 if (see->evsent && edata->value_cur > see->critval)
625 SME_SEND_NORMALEVENT();
626
627 if (!see->evsent && edata->value_cur < see->critval)
628 SME_SEND_EVENT(see->type);
629
630 break;
631 /*
632 * if value_cur is higher than the limit, send the event...
633 */
634 case PENVSYS_EVENT_USER_CRITMAX:
635 if (see->evsent && edata->value_cur < see->critval)
636 SME_SEND_NORMALEVENT();
637
638 if (!see->evsent && edata->value_cur > see->critval)
639 SME_SEND_EVENT(see->type);
640
641 break;
642 /*
643 * if value_cur is not ENVSYS_DRIVE_ONLINE, send the event...
644 */
645 case PENVSYS_EVENT_DRIVE_STCHANGED:
646 /* the state has not been changed, just ignore the event */
647 if (edata->value_cur == see->evsent)
648 break;
649
650 for (i = 0; esds[i].type != -1; i++)
651 if (esds[i].type == edata->value_cur)
652 break;
653
654 /* copy current state description */
655 (void)strlcpy(see->pes.pes_statedesc, esds[i].desc,
656 sizeof(see->pes.pes_statedesc));
657
658 /* state is ok again... send a normal event */
659 if (see->evsent && edata->value_cur == ENVSYS_DRIVE_ONLINE)
660 SME_SEND_NORMALEVENT();
661
662 /* something bad happened to the drive... send the event */
663 if (see->evsent || edata->value_cur != ENVSYS_DRIVE_ONLINE) {
664 /* save current drive state */
665 see->evsent = edata->value_cur;
666 sysmon_penvsys_event(&see->pes, see->type);
667 }
668
669 break;
670 }
671 see->see_flags &= ~SME_EVENT_WORKING;
672 cv_broadcast(&sme_event_cv);
673 mutex_exit(&sme_event_mtx);
674 }
675