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