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