sysmon_envsys_events.c revision 1.22 1 /* $NetBSD: sysmon_envsys_events.c,v 1.22 2007/08/31 00:35:08 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.22 2007/08/31 00:35:08 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 (void)strlcat(str, (c), sizeof(str)); \
347 mutex_enter(&sme_event_mtx); \
348 prop_dictionary_set_bool(sed_t->sdict, \
349 str, \
350 true); \
351 mutex_exit(&sme_event_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 KASSERT(mutex_owned(&sme_event_init_mtx));
483
484 error = workqueue_create(&seewq, "envsysev",
485 sme_events_worker, NULL, 0, IPL_SOFTCLOCK, WQ_MPSAFE);
486 if (error)
487 goto out;
488
489 callout_init(&seeco, 0);
490 callout_setfunc(&seeco, sme_events_check, NULL);
491 callout_schedule(&seeco, SME_EVTIMO);
492 sme_events_initialized = true;
493 DPRINTF(("%s: events framework initialized\n", __func__));
494
495 out:
496 return error;
497 }
498
499 /*
500 * sme_events_destroy:
501 *
502 * + Destroys the events framework: the workqueue and the
503 * callout are stopped and destroyed because there are not
504 * events in the queue.
505 */
506 void
507 sme_events_destroy(void)
508 {
509 KASSERT(mutex_owned(&sme_event_init_mtx));
510
511 callout_stop(&seeco);
512 sme_events_initialized = false;
513 DPRINTF(("%s: events framework destroyed\n", __func__));
514 callout_destroy(&seeco);
515 workqueue_destroy(seewq);
516 }
517
518 /*
519 * sme_events_check:
520 *
521 * + Runs the work on each sysmon envsys event in our
522 * workqueue periodically with callout.
523 */
524 void
525 sme_events_check(void *arg)
526 {
527 sme_event_t *see;
528
529 LIST_FOREACH(see, &sme_events_list, see_list) {
530 DPRINTFOBJ(("%s: dev=%s sensor=%s type=%d\n",
531 __func__,
532 see->pes.pes_dvname,
533 see->pes.pes_sensname,
534 see->type));
535 workqueue_enqueue(seewq, &see->see_wk, NULL);
536 }
537 callout_schedule(&seeco, SME_EVTIMO);
538 }
539
540 /*
541 * sme_events_worker:
542 *
543 * + workqueue thread that checks if there's a critical condition
544 * and sends an event if the condition was triggered.
545 */
546 void
547 sme_events_worker(struct work *wk, void *arg)
548 {
549 const struct sme_sensor_state *esds = sme_sensor_drive_state;
550 const struct sme_sensor_event *sse = sme_sensor_event;
551 sme_event_t *see = (void *)wk;
552 struct sysmon_envsys *sme;
553 envsys_data_t *edata;
554 int i, error = 0;
555
556 KASSERT(wk == &see->see_wk);
557
558 mutex_enter(&sme_event_mtx);
559 see->see_flags |= SME_EVENT_WORKING;
560
561 /*
562 * We have to find the sme device by looking
563 * at the power envsys device name.
564 */
565 mutex_enter(&sme_list_mtx);
566 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list)
567 if (strcmp(sme->sme_name, see->pes.pes_dvname) == 0)
568 break;
569 mutex_exit(&sme_list_mtx);
570
571 KASSERT(sme != NULL);
572
573 /* get the sensor with the index specified in see->snum */
574 edata = &sme->sme_sensor_data[see->snum];
575
576 /*
577 * refresh the sensor that was marked with a critical
578 * event.
579 */
580 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
581 error = (*sme->sme_gtredata)(sme, edata);
582 if (error)
583 return;
584 }
585
586 DPRINTFOBJ(("%s: desc=%s sensor=%d units=%d value_cur=%d\n",
587 __func__, edata->desc, edata->sensor,
588 edata->units, edata->value_cur));
589
590 #define SME_SEND_NORMALEVENT() \
591 do { \
592 see->evsent = false; \
593 sysmon_penvsys_event(&see->pes, PENVSYS_EVENT_NORMAL); \
594 } while (/* CONSTCOND */ 0)
595
596 #define SME_SEND_EVENT(type) \
597 do { \
598 see->evsent = true; \
599 sysmon_penvsys_event(&see->pes, (type)); \
600 } while (/* CONSTCOND */ 0)
601
602 switch (see->type) {
603 /*
604 * if state is the same than the one that matches sse[i].state,
605 * send the event...
606 */
607 case PENVSYS_EVENT_CRITICAL:
608 case PENVSYS_EVENT_CRITUNDER:
609 case PENVSYS_EVENT_CRITOVER:
610 case PENVSYS_EVENT_WARNUNDER:
611 case PENVSYS_EVENT_WARNOVER:
612 for (i = 0; sse[i].state != -1; i++)
613 if (sse[i].event == see->type)
614 break;
615
616 if (see->evsent && edata->state == ENVSYS_SVALID)
617 SME_SEND_NORMALEVENT();
618
619 if (!see->evsent && edata->state == sse[i].state)
620 SME_SEND_EVENT(see->type);
621
622 break;
623 /*
624 * if value_cur is lower than the limit, send the event...
625 */
626 case PENVSYS_EVENT_BATT_USERCAP:
627 case PENVSYS_EVENT_USER_CRITMIN:
628 if (see->evsent && edata->value_cur > see->critval)
629 SME_SEND_NORMALEVENT();
630
631 if (!see->evsent && edata->value_cur < see->critval)
632 SME_SEND_EVENT(see->type);
633
634 break;
635 /*
636 * if value_cur is higher than the limit, send the event...
637 */
638 case PENVSYS_EVENT_USER_CRITMAX:
639 if (see->evsent && edata->value_cur < see->critval)
640 SME_SEND_NORMALEVENT();
641
642 if (!see->evsent && edata->value_cur > see->critval)
643 SME_SEND_EVENT(see->type);
644
645 break;
646 /*
647 * if value_cur is not ENVSYS_DRIVE_ONLINE, send the event...
648 */
649 case PENVSYS_EVENT_DRIVE_STCHANGED:
650 /* the state has not been changed, just ignore the event */
651 if (edata->value_cur == see->evsent)
652 break;
653
654 for (i = 0; esds[i].type != -1; i++)
655 if (esds[i].type == edata->value_cur)
656 break;
657
658 /* copy current state description */
659 (void)strlcpy(see->pes.pes_statedesc, esds[i].desc,
660 sizeof(see->pes.pes_statedesc));
661
662 /* state is ok again... send a normal event */
663 if (see->evsent && edata->value_cur == ENVSYS_DRIVE_ONLINE)
664 SME_SEND_NORMALEVENT();
665
666 /* something bad happened to the drive... send the event */
667 if (see->evsent || edata->value_cur != ENVSYS_DRIVE_ONLINE) {
668 /* save current drive state */
669 see->evsent = edata->value_cur;
670 sysmon_penvsys_event(&see->pes, see->type);
671 }
672
673 break;
674 }
675 see->see_flags &= ~SME_EVENT_WORKING;
676 cv_broadcast(&sme_event_cv);
677 mutex_exit(&sme_event_mtx);
678 }
679