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