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