sysmon_envsys_events.c revision 1.24 1 /* $NetBSD: sysmon_envsys_events.c,v 1.24 2007/08/31 10:13:27 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.24 2007/08/31 10:13:27 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_list_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 * sme_event_register:
159 *
160 * + Registers a new sysmon envsys event or updates any event
161 * already in the queue.
162 */
163 int
164 sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata,
165 const char *drvn, const char *objkey,
166 int32_t critval, int crittype, int powertype)
167 {
168 sme_event_t *see = NULL;
169 prop_object_t obj;
170 bool critvalup = false;
171 int error = 0;
172
173 KASSERT(sdict != NULL || edata != NULL);
174
175 mutex_enter(&sme_event_mtx);
176 /*
177 * check if the event is already on the list and return
178 * EEXIST if value provided hasn't been changed.
179 */
180 LIST_FOREACH(see, &sme_events_list, see_list) {
181 if (strcmp(edata->desc, see->pes.pes_sensname) == 0)
182 if (crittype == see->type) {
183 if (see->critval == critval) {
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 mutex_exit(&sme_event_mtx);
189 return EEXIST;
190 }
191 critvalup = true;
192 break;
193 }
194 }
195
196 /*
197 * Critical condition operation requested by userland.
198 */
199 if (objkey && critval && critvalup) {
200 obj = prop_dictionary_get(sdict, objkey);
201 if (obj != NULL) {
202 /*
203 * object is already in dictionary and value
204 * provided is not the same than we have
205 * currently, update the critical value.
206 */
207 see->critval = critval;
208 DPRINTF(("%s: sensor=%s type=%d (critval updated)\n",
209 __func__, edata->desc, see->type));
210 error = sme_sensor_upint32(sdict, objkey, critval);
211 mutex_exit(&sme_event_mtx);
212 return error;
213 }
214 }
215
216 /*
217 * The event is not in on the list or in a dictionary, create a new
218 * sme event, assign required members and update the object in
219 * the dictionary.
220 */
221 see = NULL;
222 see = kmem_zalloc(sizeof(*see), KM_NOSLEEP);
223 if (see == NULL) {
224 mutex_exit(&sme_event_mtx);
225 return ENOMEM;
226 }
227
228 see->critval = critval;
229 see->type = crittype;
230 (void)strlcpy(see->pes.pes_dvname, drvn,
231 sizeof(see->pes.pes_dvname));
232 see->pes.pes_type = powertype;
233 (void)strlcpy(see->pes.pes_sensname, edata->desc,
234 sizeof(see->pes.pes_sensname));
235 see->snum = edata->sensor;
236 error = sme_sensor_upint32(sdict, objkey, critval);
237 if (error) {
238 mutex_exit(&sme_event_mtx);
239 return error;
240 }
241
242 DPRINTF(("%s: registering dev=%s sensor=%s snum=%d type=%d "
243 "critval=%" PRIu32 "\n", __func__,
244 see->pes.pes_dvname, see->pes.pes_sensname,
245 see->snum, see->type, see->critval));
246
247 LIST_INSERT_HEAD(&sme_events_list, see, see_list);
248 /*
249 * Initialize the events framework if it wasn't initialized
250 * before.
251 */
252 mutex_enter(&sme_event_init_mtx);
253 mutex_exit(&sme_event_mtx);
254 if (sme_events_initialized == false)
255 error = sme_events_init();
256 mutex_exit(&sme_event_init_mtx);
257 if (error)
258 kmem_free(see, sizeof(*see));
259 return error;
260 }
261
262 /*
263 * sme_event_unregister_all:
264 *
265 * + Unregisters all sysmon envsys events associated with a
266 * sysmon envsys device.
267 */
268 void
269 sme_event_unregister_all(struct sysmon_envsys *sme)
270 {
271 sme_event_t *see;
272 int evcounter = 0;
273
274 KASSERT(sme != NULL);
275
276 mutex_enter(&sme_event_mtx);
277 LIST_FOREACH(see, &sme_events_list, see_list) {
278 if (strcmp(see->pes.pes_dvname, sme->sme_name) == 0)
279 evcounter++;
280 }
281
282 DPRINTF(("%s: total events %d (%s)\n", __func__,
283 evcounter, sme->sme_name));
284
285 while ((see = LIST_FIRST(&sme_events_list)) != NULL) {
286 if (evcounter == 0)
287 break;
288
289 if (strcmp(see->pes.pes_dvname, sme->sme_name) == 0) {
290 DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
291 see->pes.pes_sensname, see->type, sme->sme_name));
292
293 while (see->see_flags & SME_EVENT_WORKING)
294 cv_wait(&sme_event_cv, &sme_event_mtx);
295
296 LIST_REMOVE(see, see_list);
297 kmem_free(see, sizeof(*see));
298 evcounter--;
299 }
300 }
301
302 if (LIST_EMPTY(&sme_events_list)) {
303 mutex_enter(&sme_event_init_mtx);
304 mutex_exit(&sme_event_mtx);
305 sme_events_destroy();
306 mutex_exit(&sme_event_init_mtx);
307 return;
308 }
309
310 mutex_exit(&sme_event_mtx);
311 }
312
313 /*
314 * sme_event_unregister:
315 *
316 * + Unregisters a sysmon envsys event.
317 */
318 int
319 sme_event_unregister(const char *sensor, int type)
320 {
321 sme_event_t *see;
322 bool found = false;
323
324 KASSERT(sensor != NULL);
325
326 mutex_enter(&sme_event_mtx);
327 LIST_FOREACH(see, &sme_events_list, see_list) {
328 if (strcmp(see->pes.pes_sensname, sensor) == 0) {
329 if (see->type == type) {
330 found = true;
331 break;
332 }
333 }
334 }
335
336 if (!found) {
337 mutex_exit(&sme_event_mtx);
338 return EINVAL;
339 }
340
341 while (see->see_flags & SME_EVENT_WORKING)
342 cv_wait(&sme_event_cv, &sme_event_mtx);
343
344 DPRINTF(("%s: removing dev=%s sensor=%s type=%d\n",
345 __func__, see->pes.pes_dvname, sensor, type));
346 LIST_REMOVE(see, see_list);
347 /*
348 * So the events list is empty, we'll do the following:
349 *
350 * - stop and destroy the callout.
351 * - destroy the workqueue.
352 */
353 if (LIST_EMPTY(&sme_events_list)) {
354 mutex_enter(&sme_event_init_mtx);
355 mutex_exit(&sme_event_mtx);
356 sme_events_destroy();
357 mutex_exit(&sme_event_init_mtx);
358 goto out;
359 }
360 mutex_exit(&sme_event_mtx);
361
362 out:
363 kmem_free(see, sizeof(*see));
364 return 0;
365 }
366
367 /*
368 * sme_event_drvadd:
369 *
370 * + Adds a new sysmon envsys event for a driver if a sensor
371 * has set any accepted monitoring flag.
372 */
373 void
374 sme_event_drvadd(void *arg)
375 {
376 sme_event_drv_t *sed_t = arg;
377 int error = 0;
378
379 KASSERT(sed_t != NULL);
380
381 #define SEE_REGEVENT(a, b, c) \
382 do { \
383 if (sed_t->edata->flags & (a)) { \
384 char str[32] = "monitoring-state-"; \
385 \
386 error = sme_event_register(sed_t->sdict, \
387 sed_t->edata, \
388 sed_t->sme->sme_name, \
389 NULL, \
390 0, \
391 (b), \
392 sed_t->powertype); \
393 if (error && error != EEXIST) \
394 printf("%s: failed to add event! " \
395 "error=%d sensor=%s event=%s\n", \
396 __func__, error, sed_t->edata->desc, (c)); \
397 else { \
398 (void)strlcat(str, (c), sizeof(str)); \
399 mutex_enter(&sme_event_mtx); \
400 prop_dictionary_set_bool(sed_t->sdict, \
401 str, \
402 true); \
403 mutex_exit(&sme_event_mtx); \
404 } \
405 } \
406 } while (/* CONSTCOND */ 0)
407
408 SEE_REGEVENT(ENVSYS_FMONCRITICAL,
409 PENVSYS_EVENT_CRITICAL,
410 "critical");
411
412 SEE_REGEVENT(ENVSYS_FMONCRITUNDER,
413 PENVSYS_EVENT_CRITUNDER,
414 "critunder");
415
416 SEE_REGEVENT(ENVSYS_FMONCRITOVER,
417 PENVSYS_EVENT_CRITOVER,
418 "critover");
419
420 SEE_REGEVENT(ENVSYS_FMONWARNUNDER,
421 PENVSYS_EVENT_WARNUNDER,
422 "warnunder");
423
424 SEE_REGEVENT(ENVSYS_FMONWARNOVER,
425 PENVSYS_EVENT_WARNOVER,
426 "warnover");
427
428 SEE_REGEVENT(ENVSYS_FMONDRVSTATE,
429 PENVSYS_EVENT_DRIVE_STCHANGED,
430 "drvstchanged");
431
432 /* we are done, free memory now */
433 kmem_free(sed_t, sizeof(*sed_t));
434 }
435
436 /*
437 * sme_events_init:
438 *
439 * + Initializes the events framework.
440 */
441 int
442 sme_events_init(void)
443 {
444 int error;
445
446 KASSERT(mutex_owned(&sme_event_init_mtx));
447
448 error = workqueue_create(&seewq, "envsysev",
449 sme_events_worker, NULL, 0, IPL_SOFTCLOCK, WQ_MPSAFE);
450 if (error)
451 goto out;
452
453 callout_init(&seeco, 0);
454 callout_setfunc(&seeco, sme_events_check, NULL);
455 callout_schedule(&seeco, SME_EVTIMO);
456 sme_events_initialized = true;
457 DPRINTF(("%s: events framework initialized\n", __func__));
458
459 out:
460 return error;
461 }
462
463 /*
464 * sme_events_destroy:
465 *
466 * + Destroys the events framework: the workqueue and the
467 * callout are stopped and destroyed because there are not
468 * events in the queue.
469 */
470 void
471 sme_events_destroy(void)
472 {
473 KASSERT(mutex_owned(&sme_event_init_mtx));
474
475 callout_stop(&seeco);
476 sme_events_initialized = false;
477 DPRINTF(("%s: events framework destroyed\n", __func__));
478 callout_destroy(&seeco);
479 workqueue_destroy(seewq);
480 }
481
482 /*
483 * sme_events_check:
484 *
485 * + Runs the work on each sysmon envsys event in our
486 * workqueue periodically with callout.
487 */
488 void
489 sme_events_check(void *arg)
490 {
491 sme_event_t *see;
492
493 LIST_FOREACH(see, &sme_events_list, see_list) {
494 DPRINTFOBJ(("%s: dev=%s sensor=%s type=%d\n",
495 __func__,
496 see->pes.pes_dvname,
497 see->pes.pes_sensname,
498 see->type));
499 workqueue_enqueue(seewq, &see->see_wk, NULL);
500 }
501 callout_schedule(&seeco, SME_EVTIMO);
502 }
503
504 /*
505 * sme_events_worker:
506 *
507 * + workqueue thread that checks if there's a critical condition
508 * and sends an event if the condition was triggered.
509 */
510 void
511 sme_events_worker(struct work *wk, void *arg)
512 {
513 const struct sme_sensor_state *esds = sme_sensor_drive_state;
514 const struct sme_sensor_event *sse = sme_sensor_event;
515 sme_event_t *see = (void *)wk;
516 struct sysmon_envsys *sme;
517 envsys_data_t *edata;
518 int i, error = 0;
519
520 KASSERT(wk == &see->see_wk);
521
522 mutex_enter(&sme_event_mtx);
523 see->see_flags |= SME_EVENT_WORKING;
524
525 /*
526 * We have to find the sme device by looking
527 * at the power envsys device name.
528 */
529 mutex_enter(&sme_list_mtx);
530 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list)
531 if (strcmp(sme->sme_name, see->pes.pes_dvname) == 0)
532 break;
533 mutex_exit(&sme_list_mtx);
534
535 KASSERT(sme != NULL);
536
537 /* get the sensor with the index specified in see->snum */
538 edata = &sme->sme_sensor_data[see->snum];
539
540 /*
541 * refresh the sensor that was marked with a critical
542 * event.
543 */
544 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
545 error = (*sme->sme_gtredata)(sme, edata);
546 if (error)
547 return;
548 }
549
550 DPRINTFOBJ(("%s: desc=%s sensor=%d units=%d value_cur=%d\n",
551 __func__, edata->desc, edata->sensor,
552 edata->units, edata->value_cur));
553
554 #define SME_SEND_NORMALEVENT() \
555 do { \
556 see->evsent = false; \
557 sysmon_penvsys_event(&see->pes, PENVSYS_EVENT_NORMAL); \
558 } while (/* CONSTCOND */ 0)
559
560 #define SME_SEND_EVENT(type) \
561 do { \
562 see->evsent = true; \
563 sysmon_penvsys_event(&see->pes, (type)); \
564 } while (/* CONSTCOND */ 0)
565
566 switch (see->type) {
567 /*
568 * if state is the same than the one that matches sse[i].state,
569 * send the event...
570 */
571 case PENVSYS_EVENT_CRITICAL:
572 case PENVSYS_EVENT_CRITUNDER:
573 case PENVSYS_EVENT_CRITOVER:
574 case PENVSYS_EVENT_WARNUNDER:
575 case PENVSYS_EVENT_WARNOVER:
576 for (i = 0; sse[i].state != -1; i++)
577 if (sse[i].event == see->type)
578 break;
579
580 if (see->evsent && edata->state == ENVSYS_SVALID)
581 SME_SEND_NORMALEVENT();
582
583 if (!see->evsent && edata->state == sse[i].state)
584 SME_SEND_EVENT(see->type);
585
586 break;
587 /*
588 * if value_cur is lower than the limit, send the event...
589 */
590 case PENVSYS_EVENT_BATT_USERCAP:
591 case PENVSYS_EVENT_USER_CRITMIN:
592 if (see->evsent && edata->value_cur > see->critval)
593 SME_SEND_NORMALEVENT();
594
595 if (!see->evsent && edata->value_cur < see->critval)
596 SME_SEND_EVENT(see->type);
597
598 break;
599 /*
600 * if value_cur is higher than the limit, send the event...
601 */
602 case PENVSYS_EVENT_USER_CRITMAX:
603 if (see->evsent && edata->value_cur < see->critval)
604 SME_SEND_NORMALEVENT();
605
606 if (!see->evsent && edata->value_cur > see->critval)
607 SME_SEND_EVENT(see->type);
608
609 break;
610 /*
611 * if value_cur is not ENVSYS_DRIVE_ONLINE, send the event...
612 */
613 case PENVSYS_EVENT_DRIVE_STCHANGED:
614 /* the state has not been changed, just ignore the event */
615 if (edata->value_cur == see->evsent)
616 break;
617
618 for (i = 0; esds[i].type != -1; i++)
619 if (esds[i].type == edata->value_cur)
620 break;
621
622 /* copy current state description */
623 (void)strlcpy(see->pes.pes_statedesc, esds[i].desc,
624 sizeof(see->pes.pes_statedesc));
625
626 /* state is ok again... send a normal event */
627 if (see->evsent && edata->value_cur == ENVSYS_DRIVE_ONLINE)
628 SME_SEND_NORMALEVENT();
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, see->type);
635 }
636
637 break;
638 }
639 see->see_flags &= ~SME_EVENT_WORKING;
640 cv_broadcast(&sme_event_cv);
641 mutex_exit(&sme_event_mtx);
642 }
643