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