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