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