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