sysmon_envsys_events.c revision 1.102 1 /* $NetBSD: sysmon_envsys_events.c,v 1.102 2012/07/18 20:50:40 pgoyette Exp $ */
2
3 /*-
4 * Copyright (c) 2007, 2008 Juan Romero Pardines.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27
28 /*
29 * sysmon_envsys(9) events framework.
30 */
31
32 #include <sys/cdefs.h>
33 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.102 2012/07/18 20:50:40 pgoyette Exp $");
34
35 #include <sys/param.h>
36 #include <sys/types.h>
37 #include <sys/conf.h>
38 #include <sys/errno.h>
39 #include <sys/kernel.h>
40 #include <sys/systm.h>
41 #include <sys/proc.h>
42 #include <sys/mutex.h>
43 #include <sys/kmem.h>
44 #include <sys/callout.h>
45
46 /* #define ENVSYS_DEBUG */
47 /* #define ENVSYS_OBJECTS_DEBUG */
48
49 #include <dev/sysmon/sysmonvar.h>
50 #include <dev/sysmon/sysmon_envsysvar.h>
51
52 struct sme_sensor_event {
53 int state;
54 int event;
55 };
56
57 static const struct sme_sensor_event sme_sensor_event[] = {
58 { ENVSYS_SVALID, PENVSYS_EVENT_NORMAL },
59 { ENVSYS_SCRITOVER, PENVSYS_EVENT_CRITOVER },
60 { ENVSYS_SCRITUNDER, PENVSYS_EVENT_CRITUNDER },
61 { ENVSYS_SWARNOVER, PENVSYS_EVENT_WARNOVER },
62 { ENVSYS_SWARNUNDER, PENVSYS_EVENT_WARNUNDER },
63 { ENVSYS_BATTERY_CAPACITY_NORMAL, PENVSYS_EVENT_NORMAL },
64 { ENVSYS_BATTERY_CAPACITY_WARNING, PENVSYS_EVENT_BATT_WARN },
65 { ENVSYS_BATTERY_CAPACITY_CRITICAL, PENVSYS_EVENT_BATT_CRIT },
66 { ENVSYS_BATTERY_CAPACITY_HIGH, PENVSYS_EVENT_BATT_HIGH },
67 { ENVSYS_BATTERY_CAPACITY_MAX, PENVSYS_EVENT_BATT_MAX },
68 { -1, -1 }
69 };
70
71 static bool sysmon_low_power;
72
73 #define SME_EVTIMO (SME_EVENTS_DEFTIMEOUT * hz)
74
75 static bool sme_event_check_low_power(void);
76 static bool sme_battery_check(void);
77 static bool sme_battery_critical(envsys_data_t *);
78 static bool sme_acadapter_check(void);
79
80 static void sme_remove_event(sme_event_t *, struct sysmon_envsys *);
81
82 /*
83 * sme_event_register:
84 *
85 * + Registers a new sysmon envsys event or updates any event
86 * already in the queue.
87 */
88 int
89 sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata,
90 struct sysmon_envsys *sme, sysmon_envsys_lim_t *lims,
91 uint32_t props, int crittype, int powertype)
92 {
93 sme_event_t *see = NULL, *osee = NULL;
94 prop_object_t obj;
95 int error = 0;
96 const char *objkey;
97
98 KASSERT(sdict != NULL);
99 KASSERT(edata != NULL);
100 KASSERT(sme != NULL);
101 KASSERT(lims != NULL);
102
103 /*
104 * Some validation first for limit-checking events
105 *
106 * 1. Limits are not permitted if the units is ENVSYS_INDICATOR
107 * or ENVSYS_BATTERY_CHARGE.
108 *
109 * 2. Capacity limits are permitted only if the sensor has the
110 * ENVSYS_FPERCENT flag set and value_max is set.
111 *
112 * 3. It is not permissible for both capacity and value limits
113 * to coexist.
114 *
115 * Note that it permissible for a sensor to have value limits
116 * even if its ENVSYS_FPERCENT flag and value_max are set.
117 */
118
119 DPRINTF(("%s: units %d props 0x%04x upropset 0x%04x max_val %d"
120 " edata-flags 0x%04x\n", __func__, edata->units, props,
121 edata->upropset, edata->value_max, edata->flags));
122
123 if (props)
124 if (edata->units == ENVSYS_INDICATOR ||
125 edata->units == ENVSYS_BATTERY_CHARGE)
126 return ENOTSUP;
127
128 if ((props & PROP_CAP_LIMITS) &&
129 ((edata->value_max == 0) ||
130 !(edata->flags & ENVSYS_FPERCENT) ||
131 (props & PROP_VAL_LIMITS) ||
132 (edata->upropset & PROP_VAL_LIMITS)))
133 props = 0;
134
135 if ((props & PROP_VAL_LIMITS) && (edata->upropset & PROP_CAP_LIMITS))
136 props = 0;
137
138 /*
139 * check if the event is already on the list and return
140 * EEXIST if value provided hasn't been changed.
141 */
142 mutex_enter(&sme->sme_mtx);
143 LIST_FOREACH(osee, &sme->sme_events_list, see_list) {
144 if (strcmp(edata->desc, osee->see_pes.pes_sensname) != 0)
145 continue;
146 if (crittype != osee->see_type)
147 continue;
148
149 /*
150 * We found an existing event for this sensor. Make
151 * sure it references the correct edata
152 */
153 KASSERT(edata == osee->see_edata);
154
155 DPRINTF(("%s: dev %s sensor %s: event type %d exists\n",
156 __func__, sme->sme_name, edata->desc, crittype));
157
158 see = osee;
159 if (props & edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) {
160 if (lims->sel_critmax == edata->limits.sel_critmax) {
161 DPRINTF(("%s: critmax exists\n", __func__));
162 error = EEXIST;
163 props &= ~(PROP_CRITMAX | PROP_BATTMAX);
164 }
165 }
166 if (props & edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) {
167 if (lims->sel_warnmax == edata->limits.sel_warnmax) {
168 DPRINTF(("%s: warnmax exists\n", __func__));
169 error = EEXIST;
170 props &= ~(PROP_WARNMAX | PROP_BATTHIGH);
171 }
172 }
173 if (props & edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) {
174 if (lims->sel_warnmin == edata->limits.sel_warnmin) {
175 DPRINTF(("%s: warnmin exists\n", __func__));
176 error = EEXIST;
177 props &= ~(PROP_WARNMIN | PROP_BATTWARN);
178 }
179 }
180 if (props & edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) {
181 if (lims->sel_critmin == edata->limits.sel_critmin) {
182 DPRINTF(("%s: critmin exists\n", __func__));
183 error = EEXIST;
184 props &= ~(PROP_CRITMIN | PROP_BATTCAP);
185 }
186 }
187 break;
188 }
189 if (crittype == PENVSYS_EVENT_NULL && see != NULL) {
190 mutex_exit(&sme->sme_mtx);
191 return EEXIST;
192 }
193
194 if (see == NULL) {
195 /*
196 * New event requested - allocate a sysmon_envsys event.
197 */
198 see = kmem_zalloc(sizeof(*see), KM_SLEEP);
199 if (see == NULL)
200 return ENOMEM;
201
202 DPRINTF(("%s: dev %s sensor %s: new event\n",
203 __func__, sme->sme_name, edata->desc));
204
205 see->see_type = crittype;
206 see->see_sme = sme;
207 see->see_edata = edata;
208
209 /* Initialize sensor type and previously-sent state */
210
211 see->see_pes.pes_type = powertype;
212
213 switch (crittype) {
214 case PENVSYS_EVENT_LIMITS:
215 see->see_evsent = ENVSYS_SVALID;
216 break;
217 case PENVSYS_EVENT_CAPACITY:
218 see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL;
219 break;
220 case PENVSYS_EVENT_STATE_CHANGED:
221 if (edata->units == ENVSYS_BATTERY_CAPACITY)
222 see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL;
223 else if (edata->units == ENVSYS_DRIVE)
224 see->see_evsent = ENVSYS_DRIVE_EMPTY;
225 else
226 panic("%s: bad units for "
227 "PENVSYS_EVENT_STATE_CHANGED", __func__);
228 break;
229 case PENVSYS_EVENT_CRITICAL:
230 default:
231 see->see_evsent = 0;
232 break;
233 }
234
235 (void)strlcpy(see->see_pes.pes_dvname, sme->sme_name,
236 sizeof(see->see_pes.pes_dvname));
237 (void)strlcpy(see->see_pes.pes_sensname, edata->desc,
238 sizeof(see->see_pes.pes_sensname));
239 }
240
241 /*
242 * Limit operation requested.
243 */
244 #define LIMIT_OP(k, l, p) \
245 if (props & p) { \
246 objkey = k; \
247 obj = prop_dictionary_get(sdict, objkey); \
248 if (obj != NULL && \
249 prop_object_type(obj) != PROP_TYPE_NUMBER) { \
250 DPRINTF(("%s: (%s) %s object no TYPE_NUMBER\n", \
251 __func__, sme->sme_name, objkey)); \
252 error = ENOTSUP; \
253 } else { \
254 edata->limits.l = lims->l; \
255 error = sme_sensor_upint32(sdict, objkey,lims->l); \
256 DPRINTF(("%s: (%s) event [sensor=%s type=%d] " \
257 "(%s updated)\n", __func__, sme->sme_name, \
258 edata->desc, crittype, objkey)); \
259 } \
260 if (error && error != EEXIST) \
261 goto out; \
262 edata->upropset |= p; \
263 }
264
265 /* Value-based limits */
266 LIMIT_OP("critical-max", sel_critmax, PROP_CRITMAX);
267 LIMIT_OP("warning-max", sel_warnmax, PROP_WARNMAX);
268 LIMIT_OP("warning-min", sel_warnmin, PROP_WARNMIN);
269 LIMIT_OP("critical-min", sel_critmin, PROP_CRITMIN);
270
271 /* %Capacity-based limits */
272 LIMIT_OP("maximum-capacity", sel_critmax, PROP_BATTMAX);
273 LIMIT_OP("high-capacity", sel_warnmax, PROP_BATTHIGH);
274 LIMIT_OP("warning-capacity", sel_warnmin, PROP_BATTWARN);
275 LIMIT_OP("critical-capacity", sel_critmin, PROP_BATTCAP);
276
277 #undef LIMIT_OP
278
279 if (props & PROP_DRIVER_LIMITS)
280 edata->upropset |= PROP_DRIVER_LIMITS;
281 else
282 edata->upropset &= ~PROP_DRIVER_LIMITS;
283
284 DPRINTF(("%s: (%s) event registered (sensor=%s snum=%d type=%d "
285 "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%" PRIu32
286 " critmax=%" PRIu32 " props 0x%04x)\n", __func__,
287 see->see_sme->sme_name, see->see_pes.pes_sensname,
288 edata->sensor, see->see_type, edata->limits.sel_critmin,
289 edata->limits.sel_warnmin, edata->limits.sel_warnmax,
290 edata->limits.sel_critmax, edata->upropset));
291 /*
292 * Initialize the events framework if it wasn't initialized before.
293 */
294 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
295 error = sme_events_init(sme);
296
297 /*
298 * If driver requested notification, advise it of new
299 * limit values
300 */
301 if (sme->sme_set_limits)
302 (*sme->sme_set_limits)(sme, edata, &(edata->limits),
303 &(edata->upropset));
304
305 out:
306 if ((error == 0 || error == EEXIST) && osee == NULL)
307 LIST_INSERT_HEAD(&sme->sme_events_list, see, see_list);
308
309 mutex_exit(&sme->sme_mtx);
310
311 return error;
312 }
313
314 /*
315 * sme_event_unregister_all:
316 *
317 * + Unregisters all events associated with a sysmon envsys device.
318 */
319 void
320 sme_event_unregister_all(struct sysmon_envsys *sme)
321 {
322 sme_event_t *see;
323 int evcounter = 0;
324
325 KASSERT(sme != NULL);
326
327 mutex_enter(&sme->sme_mtx);
328 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
329 while (see->see_flags & SEE_EVENT_WORKING)
330 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
331
332 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0)
333 evcounter++;
334 }
335
336 DPRINTF(("%s: total events %d (%s)\n", __func__,
337 evcounter, sme->sme_name));
338
339 while ((see = LIST_FIRST(&sme->sme_events_list))) {
340 if (evcounter == 0)
341 break;
342
343 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) {
344 DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
345 see->see_pes.pes_sensname, see->see_type,
346 sme->sme_name));
347 sme_remove_event(see, sme);
348
349 evcounter--;
350 }
351 }
352
353 if (LIST_EMPTY(&sme->sme_events_list))
354 if (sme->sme_flags & SME_CALLOUT_INITIALIZED)
355 sme_events_destroy(sme);
356 mutex_exit(&sme->sme_mtx);
357 }
358
359 /*
360 * sme_event_unregister:
361 *
362 * + Unregisters an event from the specified sysmon envsys device.
363 */
364 int
365 sme_event_unregister(struct sysmon_envsys *sme, const char *sensor, int type)
366 {
367 sme_event_t *see;
368 bool found = false;
369
370 KASSERT(sensor != NULL);
371
372 mutex_enter(&sme->sme_mtx);
373 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
374 if (strcmp(see->see_pes.pes_sensname, sensor) == 0) {
375 if (see->see_type == type) {
376 found = true;
377 break;
378 }
379 }
380 }
381
382 if (!found) {
383 mutex_exit(&sme->sme_mtx);
384 return EINVAL;
385 }
386
387 /*
388 * Wait for the event to finish its work, remove from the list
389 * and release resouces.
390 */
391 while (see->see_flags & SEE_EVENT_WORKING)
392 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
393
394 DPRINTF(("%s: removed dev=%s sensor=%s type=%d\n",
395 __func__, see->see_pes.pes_dvname, sensor, type));
396
397 sme_remove_event(see, sme);
398
399 mutex_exit(&sme->sme_mtx);
400 return 0;
401 }
402
403 /*
404 * sme_event_unregister_sensor:
405 *
406 * + Unregisters any event associated with a specific sensor
407 * The caller must already own the sme_mtx.
408 */
409 int
410 sme_event_unregister_sensor(struct sysmon_envsys *sme, envsys_data_t *edata)
411 {
412 sme_event_t *see;
413 bool found = false;
414
415 KASSERT(mutex_owned(&sme->sme_mtx));
416 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
417 if (see->see_edata == edata) {
418 found = true;
419 break;
420 }
421 }
422 if (!found)
423 return EINVAL;
424
425 /*
426 * Wait for the event to finish its work, remove from the list
427 * and release resouces.
428 */
429 while (see->see_flags & SEE_EVENT_WORKING)
430 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
431
432 DPRINTF(("%s: removed dev=%s sensor=%s\n",
433 __func__, see->see_pes.pes_dvname, edata->desc));
434
435 sme_remove_event(see, sme);
436
437 return 0;
438 }
439
440 static void
441 sme_remove_event(sme_event_t *see, struct sysmon_envsys *sme)
442 {
443
444 KASSERT(mutex_owned(&sme->sme_mtx));
445
446 if (see->see_edata->flags & ENVSYS_FHAS_ENTROPY)
447 rnd_detach_source(&see->see_edata->rnd_src);
448 LIST_REMOVE(see, see_list);
449 /*
450 * So the events list is empty, we'll do the following:
451 *
452 * - stop and destroy the callout.
453 * - destroy the workqueue.
454 */
455 if (LIST_EMPTY(&sme->sme_events_list))
456 sme_events_destroy(sme);
457
458 kmem_free(see, sizeof(*see));
459 }
460
461 /*
462 * sme_event_drvadd:
463 *
464 * + Registers a new event for a device that had enabled any of
465 * the monitoring flags in the driver.
466 */
467 void
468 sme_event_drvadd(void *arg)
469 {
470 sme_event_drv_t *sed_t = arg;
471 sysmon_envsys_lim_t lims;
472 uint32_t props;
473 int error = 0;
474
475 KASSERT(sed_t != NULL);
476
477 #define SEE_REGEVENT(a, b, c) \
478 do { \
479 if (sed_t->sed_edata->flags & (a)) { \
480 char str[ENVSYS_DESCLEN] = "monitoring-state-"; \
481 \
482 error = sme_event_register(sed_t->sed_sdict, \
483 sed_t->sed_edata, \
484 sed_t->sed_sme, \
485 &lims, props, \
486 (b), \
487 sed_t->sed_powertype); \
488 if (error && error != EEXIST) \
489 printf("%s: failed to add event! " \
490 "error=%d sensor=%s event=%s\n", \
491 __func__, error, \
492 sed_t->sed_edata->desc, (c)); \
493 else { \
494 (void)strlcat(str, (c), sizeof(str)); \
495 prop_dictionary_set_bool(sed_t->sed_sdict, \
496 str, \
497 true); \
498 } \
499 } \
500 } while (/* CONSTCOND */ 0)
501
502 /*
503 * If driver provides a method to retrieve its internal limit
504 * values, call it and use those returned values as initial
505 * limits for event monitoring.
506 */
507 props = 0;
508 if (sed_t->sed_edata->flags & ENVSYS_FMONLIMITS)
509 if (sed_t->sed_sme->sme_get_limits)
510 (*sed_t->sed_sme->sme_get_limits)(sed_t->sed_sme,
511 sed_t->sed_edata,
512 &lims, &props);
513 /*
514 * If driver doesn't provide a way to "absorb" user-specified
515 * limit values, we must monitor all limits ourselves
516 */
517 if (sed_t->sed_sme->sme_set_limits == NULL)
518 props &= ~PROP_DRIVER_LIMITS;
519
520 /* Register the events that were specified */
521
522 SEE_REGEVENT(ENVSYS_FMONCRITICAL,
523 PENVSYS_EVENT_CRITICAL,
524 "critical");
525
526 SEE_REGEVENT(ENVSYS_FMONSTCHANGED,
527 PENVSYS_EVENT_STATE_CHANGED,
528 "state-changed");
529
530 SEE_REGEVENT(ENVSYS_FMONLIMITS,
531 PENVSYS_EVENT_LIMITS,
532 "hw-range-limits");
533
534 SEE_REGEVENT(ENVSYS_FHAS_ENTROPY,
535 PENVSYS_EVENT_NULL,
536 "refresh-event");
537
538 /*
539 * we are done, free memory now.
540 */
541 kmem_free(sed_t, sizeof(*sed_t));
542 }
543
544 /*
545 * sme_events_init:
546 *
547 * + Initialize the events framework for this device.
548 */
549 int
550 sme_events_init(struct sysmon_envsys *sme)
551 {
552 int error = 0;
553
554 KASSERT(sme != NULL);
555 KASSERT(mutex_owned(&sme->sme_mtx));
556
557 error = workqueue_create(&sme->sme_wq, sme->sme_name,
558 sme_events_worker, sme, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE);
559 if (error)
560 return error;
561
562 mutex_init(&sme->sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
563 callout_init(&sme->sme_callout, CALLOUT_MPSAFE);
564 callout_setfunc(&sme->sme_callout, sme_events_check, sme);
565 sme->sme_flags |= SME_CALLOUT_INITIALIZED;
566 sme_schedule_callout(sme);
567 DPRINTF(("%s: events framework initialized for '%s'\n",
568 __func__, sme->sme_name));
569
570 return error;
571 }
572
573 /*
574 * sme_schedule_callout
575 *
576 * (Re)-schedule the device's callout timer
577 */
578 void
579 sme_schedule_callout(struct sysmon_envsys *sme)
580 {
581 uint64_t timo;
582
583 KASSERT(sme != NULL);
584
585 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
586 return;
587
588 if (sme->sme_events_timeout)
589 timo = sme->sme_events_timeout * hz;
590 else
591 timo = SME_EVTIMO;
592
593 callout_stop(&sme->sme_callout);
594 callout_schedule(&sme->sme_callout, timo);
595 }
596
597 /*
598 * sme_events_destroy:
599 *
600 * + Destroys the event framework for this device: callout
601 * stopped, workqueue destroyed and callout mutex destroyed.
602 */
603 void
604 sme_events_destroy(struct sysmon_envsys *sme)
605 {
606 KASSERT(mutex_owned(&sme->sme_mtx));
607
608 callout_stop(&sme->sme_callout);
609 workqueue_destroy(sme->sme_wq);
610 mutex_destroy(&sme->sme_callout_mtx);
611 callout_destroy(&sme->sme_callout);
612 sme->sme_flags &= ~SME_CALLOUT_INITIALIZED;
613 DPRINTF(("%s: events framework destroyed for '%s'\n",
614 __func__, sme->sme_name));
615 }
616
617 /*
618 * sysmon_envsys_update_limits
619 *
620 * + If a driver needs to update the limits that it is providing,
621 * we need to update the dictionary data as well as the limits.
622 * This only makes sense if the driver is capable of providing
623 * its limits, and if there is a limits event-monitor.
624 */
625 int
626 sysmon_envsys_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata)
627 {
628 int err;
629
630 sysmon_envsys_acquire(sme, false);
631 if (sme->sme_get_limits == NULL ||
632 (edata->flags & ENVSYS_FMONLIMITS) == 0)
633 err = EINVAL;
634 else
635 err = sme_update_limits(sme, edata);
636 sysmon_envsys_release(sme, false);
637
638 return err;
639 }
640
641 /*
642 * sme_update_limits
643 *
644 * + Internal version of sysmon_envsys_update_limits() to be used
645 * when the device has already been sysmon_envsys_acquire()d.
646 */
647
648 int
649 sme_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata)
650 {
651 prop_dictionary_t sdict = NULL;
652 prop_array_t array = NULL;
653 sysmon_envsys_lim_t lims;
654 sme_event_t *see;
655 uint32_t props = 0;
656
657 /* Find the dictionary for this sensor */
658 array = prop_dictionary_get(sme_propd, sme->sme_name);
659 if (array == NULL ||
660 prop_object_type(array) != PROP_TYPE_ARRAY) {
661 DPRINTF(("%s: array device failed\n", __func__));
662 return EINVAL;
663 }
664
665 sdict = prop_array_get(array, edata->sensor);
666 if (sdict == NULL) {
667 return EINVAL;
668 }
669
670 /* Find the event definition to get its powertype */
671 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
672 if (edata == see->see_edata &&
673 see->see_type == PENVSYS_EVENT_LIMITS)
674 break;
675 }
676 if (see == NULL)
677 return EINVAL;
678
679 /* Update limit values from driver if possible */
680 if (sme->sme_get_limits != NULL)
681 (*sme->sme_get_limits)(sme, edata, &lims, &props);
682
683 /* Update event and dictionary */
684 sme_event_register(sdict, edata, sme, &lims, props,
685 PENVSYS_EVENT_LIMITS, see->see_pes.pes_type);
686
687 return 0;
688 }
689
690 /*
691 * sme_events_check:
692 *
693 * + Passes the events to the workqueue thread and stops
694 * the callout if the 'low-power' condition is triggered.
695 */
696 void
697 sme_events_check(void *arg)
698 {
699 struct sysmon_envsys *sme = arg;
700 sme_event_t *see;
701 uint64_t timo;
702
703 KASSERT(sme != NULL);
704
705 mutex_enter(&sme->sme_callout_mtx);
706 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
707 workqueue_enqueue(sme->sme_wq, &see->see_wk, NULL);
708 see->see_edata->flags |= ENVSYS_FNEED_REFRESH;
709 }
710 if (sme->sme_events_timeout)
711 timo = sme->sme_events_timeout * hz;
712 else
713 timo = SME_EVTIMO;
714 if (!sysmon_low_power)
715 sme_schedule_callout(sme);
716 mutex_exit(&sme->sme_callout_mtx);
717 }
718
719 /*
720 * sme_events_worker:
721 *
722 * + workqueue thread that checks if there's a critical condition
723 * and sends an event if it was triggered.
724 */
725 void
726 sme_events_worker(struct work *wk, void *arg)
727 {
728 sme_event_t *see = (void *)wk;
729 struct sysmon_envsys *sme = see->see_sme;
730 envsys_data_t *edata = see->see_edata;
731
732 KASSERT(wk == &see->see_wk);
733 KASSERT(sme != NULL || edata != NULL);
734
735 mutex_enter(&sme->sme_mtx);
736 see->see_flags |= SEE_EVENT_WORKING;
737 /*
738 * sme_events_check marks the sensors to make us refresh them here.
739 * sme_envsys_refresh_sensor will not call the driver if the driver
740 * does its own setting of the sensor value.
741 */
742 if ((edata->flags & ENVSYS_FNEED_REFRESH) != 0) {
743 /* refresh sensor in device */
744 sysmon_envsys_refresh_sensor(sme, edata);
745 edata->flags &= ~ENVSYS_FNEED_REFRESH;
746 }
747
748 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d type=%d state=%d units=%d "
749 "value_cur=%d upropset=%d\n", __func__, sme->sme_name, edata->desc,
750 edata->sensor, see->see_type, edata->state, edata->units,
751 edata->value_cur, edata->upropset));
752
753 /* skip the event if current sensor is in invalid state */
754 if (edata->state == ENVSYS_SINVALID)
755 goto out;
756
757 /*
758 * For range limits, if the driver claims responsibility for
759 * limit/range checking, just user driver-supplied status.
760 * Else calculate our own status. Note that driver must
761 * relinquish responsibility for ALL limits if there is even
762 * one limit that it cannot handle!
763 *
764 * If this is a CAPACITY monitor, but the sensor's max_value
765 * is not set, treat it as though the monitor does not exist.
766 */
767 if ((see->see_type == PENVSYS_EVENT_LIMITS ||
768 see->see_type == PENVSYS_EVENT_CAPACITY) &&
769 (edata->upropset & PROP_DRIVER_LIMITS) == 0) {
770 if ((see->see_type == PENVSYS_EVENT_CAPACITY) &&
771 (edata->value_max == 0))
772 edata->state = ENVSYS_SVALID;
773 else if ((edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) &&
774 (edata->value_cur < edata->limits.sel_critmin))
775 edata->state = ENVSYS_SCRITUNDER;
776 else if ((edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) &&
777 (edata->value_cur < edata->limits.sel_warnmin))
778 edata->state = ENVSYS_SWARNUNDER;
779 else if ((edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) &&
780 (edata->value_cur > edata->limits.sel_critmax))
781 edata->state = ENVSYS_SCRITOVER;
782 else if ((edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) &&
783 (edata->value_cur > edata->limits.sel_warnmax))
784 edata->state = ENVSYS_SWARNOVER;
785 else
786 edata->state = ENVSYS_SVALID;
787 }
788 sme_deliver_event(see);
789
790 out:
791 see->see_flags &= ~SEE_EVENT_WORKING;
792 cv_broadcast(&sme->sme_condvar);
793 mutex_exit(&sme->sme_mtx);
794 }
795
796 /*
797 * sysmon_envsys_sensor_event
798 *
799 * + Find the monitor event of a particular type for a given sensor
800 * on a device and deliver the event if one is required. If
801 * no event type is specified, deliver all events for the sensor.
802 */
803 void
804 sysmon_envsys_sensor_event(struct sysmon_envsys *sme, envsys_data_t *edata,
805 int ev_type)
806 {
807 sme_event_t *see;
808
809 mutex_enter(&sme->sme_mtx);
810 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
811 if (edata != see->see_edata)
812 continue;
813 if (ev_type == 0 ||
814 ev_type == see->see_type) {
815 sme_deliver_event(see);
816 if (ev_type != 0)
817 break;
818 }
819 }
820 mutex_exit(&sme->sme_mtx);
821 }
822
823 /*
824 * sme_deliver_event:
825 *
826 * + If new sensor state requires it, send an event to powerd
827 *
828 * Must be called with the device's sysmon mutex held
829 * see->see_sme->sme_mtx
830 */
831 void
832 sme_deliver_event(sme_event_t *see)
833 {
834 envsys_data_t *edata = see->see_edata;
835 const struct sme_descr_entry *sdt = NULL;
836 const struct sme_sensor_event *sse = sme_sensor_event;
837 int i, state = 0;
838
839 switch (see->see_type) {
840 case PENVSYS_EVENT_LIMITS:
841 case PENVSYS_EVENT_CAPACITY:
842 /*
843 * Send event if state has changed
844 */
845 if (edata->state == see->see_evsent)
846 break;
847
848 for (i = 0; sse[i].state != -1; i++)
849 if (sse[i].state == edata->state)
850 break;
851
852 if (sse[i].state == -1)
853 break;
854
855 if (edata->state == ENVSYS_SVALID)
856 sysmon_penvsys_event(&see->see_pes,
857 PENVSYS_EVENT_NORMAL);
858 else
859 sysmon_penvsys_event(&see->see_pes, sse[i].event);
860
861 see->see_evsent = edata->state;
862 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d state=%d send_ev=%d\n",
863 __func__, see->see_sme->sme_name, edata->desc,
864 edata->sensor, edata->state,
865 (edata->state == ENVSYS_SVALID) ? PENVSYS_EVENT_NORMAL :
866 sse[i].event));
867
868 break;
869
870 /*
871 * Send PENVSYS_EVENT_CRITICAL event if:
872 * State has gone from non-CRITICAL to CRITICAL,
873 * State remains CRITICAL and value has changed, or
874 * State has returned from CRITICAL to non-CRITICAL
875 */
876 case PENVSYS_EVENT_CRITICAL:
877 if (edata->state == ENVSYS_SVALID &&
878 see->see_evsent != 0) {
879 sysmon_penvsys_event(&see->see_pes,
880 PENVSYS_EVENT_NORMAL);
881 see->see_evsent = 0;
882 } else if (edata->state == ENVSYS_SCRITICAL &&
883 see->see_evsent != edata->value_cur) {
884 sysmon_penvsys_event(&see->see_pes,
885 PENVSYS_EVENT_CRITICAL);
886 see->see_evsent = edata->value_cur;
887 }
888 break;
889
890 /*
891 * if value_cur is not normal (battery) or online (drive),
892 * send the event...
893 */
894 case PENVSYS_EVENT_STATE_CHANGED:
895 /*
896 * the state has not been changed, just ignore the event.
897 */
898 if (edata->value_cur == see->see_evsent)
899 break;
900
901 switch (edata->units) {
902 case ENVSYS_DRIVE:
903 sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES,
904 edata->value_cur);
905 state = ENVSYS_DRIVE_ONLINE;
906 break;
907 case ENVSYS_BATTERY_CAPACITY:
908 sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY,
909 edata->value_cur);
910 state = ENVSYS_BATTERY_CAPACITY_NORMAL;
911 break;
912 default:
913 panic("%s: bad units for PENVSYS_EVENT_STATE_CHANGED",
914 __func__);
915 }
916
917 if (sdt->type == -1)
918 break;
919
920 /*
921 * copy current state description.
922 */
923 (void)strlcpy(see->see_pes.pes_statedesc, sdt->desc,
924 sizeof(see->see_pes.pes_statedesc));
925
926 if (edata->value_cur == state)
927 /*
928 * state returned to normal condition
929 */
930 sysmon_penvsys_event(&see->see_pes,
931 PENVSYS_EVENT_NORMAL);
932 else
933 /*
934 * state changed to abnormal condition
935 */
936 sysmon_penvsys_event(&see->see_pes, see->see_type);
937
938 see->see_evsent = edata->value_cur;
939
940 /*
941 * There's no need to continue if it's a drive sensor.
942 */
943 if (edata->units == ENVSYS_DRIVE)
944 break;
945
946 /*
947 * Check if the system is running in low power and send the
948 * event to powerd (if running) or shutdown the system
949 * otherwise.
950 */
951 if (!sysmon_low_power && sme_event_check_low_power()) {
952 struct penvsys_state pes;
953
954 /*
955 * Stop the callout and send the 'low-power' event.
956 */
957 sysmon_low_power = true;
958 callout_stop(&see->see_sme->sme_callout);
959 pes.pes_type = PENVSYS_TYPE_BATTERY;
960 sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER);
961 }
962 break;
963 case PENVSYS_EVENT_NULL:
964 break;
965 default:
966 panic("%s: invalid event type %d", __func__, see->see_type);
967 }
968 }
969
970 /*
971 * Returns true if the system is in low power state: an AC adapter
972 * is OFF and all batteries are in LOW/CRITICAL state.
973 */
974 static bool
975 sme_event_check_low_power(void)
976 {
977 if (!sme_acadapter_check())
978 return false;
979
980 return sme_battery_check();
981 }
982
983 /*
984 * Called with the sysmon_envsys device mtx held through the
985 * workqueue thread.
986 */
987 static bool
988 sme_acadapter_check(void)
989 {
990 struct sysmon_envsys *sme;
991 envsys_data_t *edata;
992 bool dev = false, sensor = false;
993
994 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
995 if (sme->sme_class == SME_CLASS_ACADAPTER) {
996 dev = true;
997 break;
998 }
999 }
1000
1001 /*
1002 * No AC Adapter devices were found.
1003 */
1004 if (!dev)
1005 return false;
1006
1007 /*
1008 * Check if there's an AC adapter device connected.
1009 */
1010 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1011 if (edata->units == ENVSYS_INDICATOR) {
1012 sensor = true;
1013 /* refresh current sensor */
1014 sysmon_envsys_refresh_sensor(sme, edata);
1015 if (edata->value_cur)
1016 return false;
1017 }
1018 }
1019
1020 if (!sensor)
1021 return false;
1022
1023 /*
1024 * AC adapter found and not connected.
1025 */
1026 return true;
1027 }
1028
1029 /*
1030 * Called with the sysmon_envsys device mtx held through the
1031 * workqueue thread.
1032 */
1033 static bool
1034 sme_battery_check(void)
1035 {
1036 struct sysmon_envsys *sme;
1037 envsys_data_t *edata;
1038 int batteriesfound = 0;
1039 bool present, batterycap, batterycharge;
1040
1041 /*
1042 * Check for battery devices and its state.
1043 */
1044 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
1045 if (sme->sme_class != SME_CLASS_BATTERY)
1046 continue;
1047
1048 present = true;
1049 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1050 if (edata->units == ENVSYS_INDICATOR &&
1051 !edata->value_cur) {
1052 present = false;
1053 break;
1054 }
1055 }
1056 if (!present)
1057 continue;
1058 /*
1059 * We've found a battery device...
1060 */
1061 batteriesfound++;
1062 batterycap = batterycharge = false;
1063 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1064 if (edata->units == ENVSYS_BATTERY_CAPACITY) {
1065 batterycap = true;
1066 if (!sme_battery_critical(edata))
1067 return false;
1068 } else if (edata->units == ENVSYS_BATTERY_CHARGE) {
1069 batterycharge = true;
1070 if (edata->value_cur)
1071 return false;
1072 }
1073 }
1074 if (!batterycap || !batterycharge)
1075 return false;
1076 }
1077
1078 if (!batteriesfound)
1079 return false;
1080
1081 /*
1082 * All batteries in low/critical capacity and discharging.
1083 */
1084 return true;
1085 }
1086
1087 static bool
1088 sme_battery_critical(envsys_data_t *edata)
1089 {
1090 if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL ||
1091 edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW)
1092 return true;
1093
1094 return false;
1095 }
1096