sysmon_envsys_events.c revision 1.36 1 /* $NetBSD: sysmon_envsys_events.c,v 1.36 2007/10/07 04:11:15 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.36 2007/10/07 04:11:15 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 goto out;
326 }
327
328 out:
329 kmem_free(see, sizeof(*see));
330 return 0;
331 }
332
333 /*
334 * sme_event_drvadd:
335 *
336 * + Adds a new sysmon envsys event for a driver if a sensor
337 * has set any accepted monitoring flag.
338 */
339 void
340 sme_event_drvadd(void *arg)
341 {
342 sme_event_drv_t *sed_t = arg;
343 int error = 0;
344
345 KASSERT(sed_t != NULL);
346
347 #define SEE_REGEVENT(a, b, c) \
348 do { \
349 if (sed_t->edata->flags & (a)) { \
350 char str[ENVSYS_DESCLEN] = "monitoring-state-"; \
351 \
352 error = sme_event_register(sed_t->sdict, \
353 sed_t->edata, \
354 sed_t->sme->sme_name, \
355 NULL, \
356 0, \
357 (b), \
358 sed_t->powertype); \
359 if (error && error != EEXIST) \
360 printf("%s: failed to add event! " \
361 "error=%d sensor=%s event=%s\n", \
362 __func__, error, sed_t->edata->desc, (c)); \
363 else { \
364 (void)strlcat(str, (c), sizeof(str)); \
365 mutex_enter(&sme_mtx); \
366 prop_dictionary_set_bool(sed_t->sdict, \
367 str, \
368 true); \
369 mutex_exit(&sme_mtx); \
370 } \
371 } \
372 } while (/* CONSTCOND */ 0)
373
374 SEE_REGEVENT(ENVSYS_FMONCRITICAL,
375 PENVSYS_EVENT_CRITICAL,
376 "critical");
377
378 SEE_REGEVENT(ENVSYS_FMONCRITUNDER,
379 PENVSYS_EVENT_CRITUNDER,
380 "critunder");
381
382 SEE_REGEVENT(ENVSYS_FMONCRITOVER,
383 PENVSYS_EVENT_CRITOVER,
384 "critover");
385
386 SEE_REGEVENT(ENVSYS_FMONWARNUNDER,
387 PENVSYS_EVENT_WARNUNDER,
388 "warnunder");
389
390 SEE_REGEVENT(ENVSYS_FMONWARNOVER,
391 PENVSYS_EVENT_WARNOVER,
392 "warnover");
393
394 SEE_REGEVENT(ENVSYS_FMONSTCHANGED,
395 PENVSYS_EVENT_STATE_CHANGED,
396 "state-changed");
397
398 /* we are done, free memory now */
399 kmem_free(sed_t, sizeof(*sed_t));
400 }
401
402 /*
403 * sme_events_init:
404 *
405 * + Initializes the events framework.
406 */
407 int
408 sme_events_init(void)
409 {
410 int error;
411
412 KASSERT(mutex_owned(&sme_event_init_mtx));
413
414 error = workqueue_create(&seewq, "envsysev",
415 sme_events_worker, NULL, 0, IPL_SOFTCLOCK, WQ_MPSAFE);
416 if (error)
417 goto out;
418
419 callout_init(&seeco, 0);
420 callout_setfunc(&seeco, sme_events_check, NULL);
421 callout_schedule(&seeco, SME_EVTIMO);
422 sme_events_initialized = true;
423 DPRINTF(("%s: events framework initialized\n", __func__));
424
425 out:
426 return error;
427 }
428
429 /*
430 * sme_events_destroy:
431 *
432 * + Destroys the events framework: the workqueue and the
433 * callout are stopped and destroyed because there are not
434 * events in the queue.
435 */
436 void
437 sme_events_destroy(void)
438 {
439 KASSERT(mutex_owned(&sme_event_init_mtx));
440
441 callout_stop(&seeco);
442 sme_events_initialized = false;
443 DPRINTF(("%s: events framework destroyed\n", __func__));
444 callout_destroy(&seeco);
445 workqueue_destroy(seewq);
446 }
447
448 /*
449 * sme_events_check:
450 *
451 * + Runs the work on each sysmon envsys event in our
452 * workqueue periodically with callout.
453 */
454 void
455 sme_events_check(void *arg)
456 {
457 sme_event_t *see;
458
459 LIST_FOREACH(see, &sme_events_list, see_list) {
460 DPRINTFOBJ(("%s: dev=%s sensor=%s type=%d\n",
461 __func__,
462 see->pes.pes_dvname,
463 see->pes.pes_sensname,
464 see->type));
465 workqueue_enqueue(seewq, &see->see_wk, NULL);
466 }
467 callout_schedule(&seeco, SME_EVTIMO);
468 }
469
470 /*
471 * sme_events_worker:
472 *
473 * + workqueue thread that checks if there's a critical condition
474 * and sends an event if the condition was triggered.
475 */
476 void
477 sme_events_worker(struct work *wk, void *arg)
478 {
479 const struct sme_description_table *sdt = NULL;
480 const struct sme_sensor_event *sse = sme_sensor_event;
481 sme_event_t *see = (void *)wk;
482 struct sysmon_envsys *sme;
483 envsys_data_t *edata;
484 int i, state, error;
485
486 KASSERT(wk == &see->see_wk);
487
488 state = error = 0;
489
490 mutex_enter(&sme_mtx);
491 see->see_flags |= SME_EVENT_WORKING;
492
493 /*
494 * We have to find the sme device by looking
495 * at the power envsys device name.
496 */
497 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list)
498 if (strcmp(sme->sme_name, see->pes.pes_dvname) == 0)
499 break;
500 if (!sme)
501 goto out;
502
503 /* get the sensor with the index specified in see->snum */
504 edata = &sme->sme_sensor_data[see->snum];
505
506 /*
507 * refresh the sensor that was marked with a critical
508 * event.
509 */
510 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
511 error = (*sme->sme_gtredata)(sme, edata);
512 if (error)
513 goto out;
514 }
515
516 DPRINTFOBJ(("%s: desc=%s sensor=%d units=%d value_cur=%d\n",
517 __func__, edata->desc, edata->sensor,
518 edata->units, edata->value_cur));
519
520 #define SME_SEND_NORMALEVENT() \
521 do { \
522 see->evsent = false; \
523 sysmon_penvsys_event(&see->pes, PENVSYS_EVENT_NORMAL); \
524 } while (/* CONSTCOND */ 0)
525
526 #define SME_SEND_EVENT(type) \
527 do { \
528 see->evsent = true; \
529 sysmon_penvsys_event(&see->pes, (type)); \
530 } while (/* CONSTCOND */ 0)
531
532 switch (see->type) {
533 /*
534 * if state is the same than the one that matches sse[i].state,
535 * send the event...
536 */
537 case PENVSYS_EVENT_CRITICAL:
538 case PENVSYS_EVENT_CRITUNDER:
539 case PENVSYS_EVENT_CRITOVER:
540 case PENVSYS_EVENT_WARNUNDER:
541 case PENVSYS_EVENT_WARNOVER:
542 for (i = 0; sse[i].state != -1; i++)
543 if (sse[i].event == see->type)
544 break;
545
546 if (see->evsent && edata->state == ENVSYS_SVALID)
547 SME_SEND_NORMALEVENT();
548
549 if (!see->evsent && edata->state == sse[i].state)
550 SME_SEND_EVENT(see->type);
551
552 break;
553 /*
554 * if value_cur is lower than the limit, send the event...
555 */
556 case PENVSYS_EVENT_BATT_USERCAP:
557 case PENVSYS_EVENT_USER_CRITMIN:
558 if (see->evsent && edata->value_cur > see->critval)
559 SME_SEND_NORMALEVENT();
560
561 if (!see->evsent && edata->value_cur < see->critval)
562 SME_SEND_EVENT(see->type);
563
564 break;
565 /*
566 * if value_cur is higher than the limit, send the event...
567 */
568 case PENVSYS_EVENT_USER_CRITMAX:
569 if (see->evsent && edata->value_cur < see->critval)
570 SME_SEND_NORMALEVENT();
571
572 if (!see->evsent && edata->value_cur > see->critval)
573 SME_SEND_EVENT(see->type);
574
575 break;
576 /*
577 * if value_cur is not normal (battery) or online (drive),
578 * send the event...
579 */
580 case PENVSYS_EVENT_STATE_CHANGED:
581 /* the state has not been changed, just ignore the event */
582 if (edata->value_cur == see->evsent)
583 break;
584
585 switch (edata->units) {
586 case ENVSYS_DRIVE:
587 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
588 state = ENVSYS_DRIVE_ONLINE;
589 break;
590 case ENVSYS_BATTERY_STATE:
591 sdt =
592 sme_get_description_table(SME_DESC_BATTERY_STATES);
593 state = ENVSYS_BATTERY_STATE_NORMAL;
594 break;
595 default:
596 panic("%s: invalid units for ENVSYS_FMONSTCHANGED",
597 __func__);
598 }
599
600 for (i = 0; sdt[i].type != -1; i++)
601 if (sdt[i].type == edata->value_cur)
602 break;
603
604 /* copy current state description */
605 (void)strlcpy(see->pes.pes_statedesc, sdt[i].desc,
606 sizeof(see->pes.pes_statedesc));
607
608 /* state is ok again... send a normal event */
609 if (see->evsent && edata->value_cur == state)
610 SME_SEND_NORMALEVENT();
611
612 /* state has been changed... send event */
613 if (see->evsent || edata->value_cur != state) {
614 /* save current drive state */
615 see->evsent = edata->value_cur;
616 sysmon_penvsys_event(&see->pes, see->type);
617 }
618
619 break;
620 }
621 out:
622 see->see_flags &= ~SME_EVENT_WORKING;
623 cv_broadcast(&sme_cv);
624 mutex_exit(&sme_mtx);
625 }
626