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