sysmon_envsys_events.c revision 1.100 1 /* $NetBSD: sysmon_envsys_events.c,v 1.100 2012/07/15 18:33:07 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.100 2012/07/15 18:33:07 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 return EEXIST;
191
192 if (see == NULL) {
193 /*
194 * New event requested - allocate a sysmon_envsys event.
195 */
196 see = kmem_zalloc(sizeof(*see), KM_SLEEP);
197 if (see == NULL)
198 return ENOMEM;
199
200 DPRINTF(("%s: dev %s sensor %s: new event\n",
201 __func__, sme->sme_name, edata->desc));
202
203 see->see_type = crittype;
204 see->see_sme = sme;
205 see->see_edata = edata;
206
207 /* Initialize sensor type and previously-sent state */
208
209 see->see_pes.pes_type = powertype;
210
211 switch (crittype) {
212 case PENVSYS_EVENT_LIMITS:
213 see->see_evsent = ENVSYS_SVALID;
214 break;
215 case PENVSYS_EVENT_CAPACITY:
216 see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL;
217 break;
218 case PENVSYS_EVENT_STATE_CHANGED:
219 if (edata->units == ENVSYS_BATTERY_CAPACITY)
220 see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL;
221 else if (edata->units == ENVSYS_DRIVE)
222 see->see_evsent = ENVSYS_DRIVE_EMPTY;
223 else
224 panic("%s: bad units for "
225 "PENVSYS_EVENT_STATE_CHANGED", __func__);
226 break;
227 case PENVSYS_EVENT_CRITICAL:
228 default:
229 see->see_evsent = 0;
230 break;
231 }
232
233 (void)strlcpy(see->see_pes.pes_dvname, sme->sme_name,
234 sizeof(see->see_pes.pes_dvname));
235 (void)strlcpy(see->see_pes.pes_sensname, edata->desc,
236 sizeof(see->see_pes.pes_sensname));
237 }
238
239 /*
240 * Limit operation requested.
241 */
242 #define LIMIT_OP(k, l, p) \
243 if (props & p) { \
244 objkey = k; \
245 obj = prop_dictionary_get(sdict, objkey); \
246 if (obj != NULL && \
247 prop_object_type(obj) != PROP_TYPE_NUMBER) { \
248 DPRINTF(("%s: (%s) %s object no TYPE_NUMBER\n", \
249 __func__, sme->sme_name, objkey)); \
250 error = ENOTSUP; \
251 } else { \
252 edata->limits.l = lims->l; \
253 error = sme_sensor_upint32(sdict, objkey,lims->l); \
254 DPRINTF(("%s: (%s) event [sensor=%s type=%d] " \
255 "(%s updated)\n", __func__, sme->sme_name, \
256 edata->desc, crittype, objkey)); \
257 } \
258 if (error && error != EEXIST) \
259 goto out; \
260 edata->upropset |= p; \
261 }
262
263 /* Value-based limits */
264 LIMIT_OP("critical-max", sel_critmax, PROP_CRITMAX);
265 LIMIT_OP("warning-max", sel_warnmax, PROP_WARNMAX);
266 LIMIT_OP("warning-min", sel_warnmin, PROP_WARNMIN);
267 LIMIT_OP("critical-min", sel_critmin, PROP_CRITMIN);
268
269 /* %Capacity-based limits */
270 LIMIT_OP("maximum-capacity", sel_critmax, PROP_BATTMAX);
271 LIMIT_OP("high-capacity", sel_warnmax, PROP_BATTHIGH);
272 LIMIT_OP("warning-capacity", sel_warnmin, PROP_BATTWARN);
273 LIMIT_OP("critical-capacity", sel_critmin, PROP_BATTCAP);
274
275 #undef LIMIT_OP
276
277 if (props & PROP_DRIVER_LIMITS)
278 edata->upropset |= PROP_DRIVER_LIMITS;
279 else
280 edata->upropset &= ~PROP_DRIVER_LIMITS;
281
282 DPRINTF(("%s: (%s) event registered (sensor=%s snum=%d type=%d "
283 "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%" PRIu32
284 " critmax=%" PRIu32 " props 0x%04x)\n", __func__,
285 see->see_sme->sme_name, see->see_pes.pes_sensname,
286 edata->sensor, see->see_type, edata->limits.sel_critmin,
287 edata->limits.sel_warnmin, edata->limits.sel_warnmax,
288 edata->limits.sel_critmax, edata->upropset));
289 /*
290 * Initialize the events framework if it wasn't initialized before.
291 */
292 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
293 error = sme_events_init(sme);
294
295 /*
296 * If driver requested notification, advise it of new
297 * limit values
298 */
299 if (sme->sme_set_limits)
300 (*sme->sme_set_limits)(sme, edata, &(edata->limits),
301 &(edata->upropset));
302
303 out:
304 if ((error == 0 || error == EEXIST) && osee == NULL)
305 LIST_INSERT_HEAD(&sme->sme_events_list, see, see_list);
306
307 mutex_exit(&sme->sme_mtx);
308
309 return error;
310 }
311
312 /*
313 * sme_event_unregister_all:
314 *
315 * + Unregisters all events associated with a sysmon envsys device.
316 */
317 void
318 sme_event_unregister_all(struct sysmon_envsys *sme)
319 {
320 sme_event_t *see;
321 int evcounter = 0;
322
323 KASSERT(sme != NULL);
324
325 mutex_enter(&sme->sme_mtx);
326 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
327 while (see->see_flags & SEE_EVENT_WORKING)
328 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
329
330 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0)
331 evcounter++;
332 }
333
334 DPRINTF(("%s: total events %d (%s)\n", __func__,
335 evcounter, sme->sme_name));
336
337 while ((see = LIST_FIRST(&sme->sme_events_list))) {
338 if (evcounter == 0)
339 break;
340
341 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) {
342 DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
343 see->see_pes.pes_sensname, see->see_type,
344 sme->sme_name));
345 sme_remove_event(see, sme);
346
347 evcounter--;
348 }
349 }
350
351 if (LIST_EMPTY(&sme->sme_events_list))
352 if (sme->sme_flags & SME_CALLOUT_INITIALIZED)
353 sme_events_destroy(sme);
354 mutex_exit(&sme->sme_mtx);
355 }
356
357 /*
358 * sme_event_unregister:
359 *
360 * + Unregisters an event from the specified sysmon envsys device.
361 */
362 int
363 sme_event_unregister(struct sysmon_envsys *sme, const char *sensor, int type)
364 {
365 sme_event_t *see;
366 bool found = false;
367
368 KASSERT(sensor != NULL);
369
370 mutex_enter(&sme->sme_mtx);
371 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
372 if (strcmp(see->see_pes.pes_sensname, sensor) == 0) {
373 if (see->see_type == type) {
374 found = true;
375 break;
376 }
377 }
378 }
379
380 if (!found) {
381 mutex_exit(&sme->sme_mtx);
382 return EINVAL;
383 }
384
385 /*
386 * Wait for the event to finish its work, remove from the list
387 * and release resouces.
388 */
389 while (see->see_flags & SEE_EVENT_WORKING)
390 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
391
392 DPRINTF(("%s: removed dev=%s sensor=%s type=%d\n",
393 __func__, see->see_pes.pes_dvname, sensor, type));
394
395 sme_remove_event(see, sme);
396
397 mutex_exit(&sme->sme_mtx);
398 return 0;
399 }
400
401 /*
402 * sme_event_unregister_sensor:
403 *
404 * + Unregisters any event associated with a specific sensor
405 * The caller must already own the sme_mtx.
406 */
407 int
408 sme_event_unregister_sensor(struct sysmon_envsys *sme, envsys_data_t *edata)
409 {
410 sme_event_t *see;
411 bool found = false;
412
413 KASSERT(mutex_owned(&sme->sme_mtx));
414 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
415 if (see->see_edata == edata) {
416 found = true;
417 break;
418 }
419 }
420 if (!found)
421 return EINVAL;
422
423 /*
424 * Wait for the event to finish its work, remove from the list
425 * and release resouces.
426 */
427 while (see->see_flags & SEE_EVENT_WORKING)
428 cv_wait(&sme->sme_condvar, &sme->sme_mtx);
429
430 DPRINTF(("%s: removed dev=%s sensor=%s\n",
431 __func__, see->see_pes.pes_dvname, edata->desc));
432
433 sme_remove_event(see, sme);
434
435 return 0;
436 }
437
438 static void
439 sme_remove_event(sme_event_t *see, struct sysmon_envsys *sme)
440 {
441
442 KASSERT(mutex_owned(&sme->sme_mtx));
443
444 if (see->see_edata->flags & ENVSYS_FHAS_ENTROPY)
445 rnd_detach_source(&see->see_edata->rnd_src);
446 LIST_REMOVE(see, see_list);
447 /*
448 * So the events list is empty, we'll do the following:
449 *
450 * - stop and destroy the callout.
451 * - destroy the workqueue.
452 */
453 if (LIST_EMPTY(&sme->sme_events_list))
454 sme_events_destroy(sme);
455
456 kmem_free(see, sizeof(*see));
457 }
458
459 /*
460 * sme_event_drvadd:
461 *
462 * + Registers a new event for a device that had enabled any of
463 * the monitoring flags in the driver.
464 */
465 void
466 sme_event_drvadd(void *arg)
467 {
468 sme_event_drv_t *sed_t = arg;
469 sysmon_envsys_lim_t lims;
470 uint32_t props;
471 int error = 0;
472
473 KASSERT(sed_t != NULL);
474
475 #define SEE_REGEVENT(a, b, c) \
476 do { \
477 if (sed_t->sed_edata->flags & (a)) { \
478 char str[ENVSYS_DESCLEN] = "monitoring-state-"; \
479 \
480 error = sme_event_register(sed_t->sed_sdict, \
481 sed_t->sed_edata, \
482 sed_t->sed_sme, \
483 &lims, props, \
484 (b), \
485 sed_t->sed_powertype); \
486 if (error && error != EEXIST) \
487 printf("%s: failed to add event! " \
488 "error=%d sensor=%s event=%s\n", \
489 __func__, error, \
490 sed_t->sed_edata->desc, (c)); \
491 else { \
492 (void)strlcat(str, (c), sizeof(str)); \
493 prop_dictionary_set_bool(sed_t->sed_sdict, \
494 str, \
495 true); \
496 } \
497 } \
498 } while (/* CONSTCOND */ 0)
499
500 /*
501 * If driver provides a method to retrieve its internal limit
502 * values, call it and use those returned values as initial
503 * limits for event monitoring.
504 */
505 props = 0;
506 if (sed_t->sed_edata->flags & ENVSYS_FMONLIMITS)
507 if (sed_t->sed_sme->sme_get_limits)
508 (*sed_t->sed_sme->sme_get_limits)(sed_t->sed_sme,
509 sed_t->sed_edata,
510 &lims, &props);
511 /*
512 * If driver doesn't provide a way to "absorb" user-specified
513 * limit values, we must monitor all limits ourselves
514 */
515 if (sed_t->sed_sme->sme_set_limits == NULL)
516 props &= ~PROP_DRIVER_LIMITS;
517
518 /* Register the events that were specified */
519
520 SEE_REGEVENT(ENVSYS_FMONCRITICAL,
521 PENVSYS_EVENT_CRITICAL,
522 "critical");
523
524 SEE_REGEVENT(ENVSYS_FMONSTCHANGED,
525 PENVSYS_EVENT_STATE_CHANGED,
526 "state-changed");
527
528 SEE_REGEVENT(ENVSYS_FMONLIMITS,
529 PENVSYS_EVENT_LIMITS,
530 "hw-range-limits");
531
532 SEE_REGEVENT(ENVSYS_FHAS_ENTROPY,
533 PENVSYS_EVENT_NULL,
534 "refresh-event");
535
536 /*
537 * we are done, free memory now.
538 */
539 kmem_free(sed_t, sizeof(*sed_t));
540 }
541
542 /*
543 * sme_events_init:
544 *
545 * + Initialize the events framework for this device.
546 */
547 int
548 sme_events_init(struct sysmon_envsys *sme)
549 {
550 int error = 0;
551
552 KASSERT(sme != NULL);
553 KASSERT(mutex_owned(&sme->sme_mtx));
554
555 error = workqueue_create(&sme->sme_wq, sme->sme_name,
556 sme_events_worker, sme, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE);
557 if (error)
558 return error;
559
560 mutex_init(&sme->sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK);
561 callout_init(&sme->sme_callout, CALLOUT_MPSAFE);
562 callout_setfunc(&sme->sme_callout, sme_events_check, sme);
563 sme->sme_flags |= SME_CALLOUT_INITIALIZED;
564 sme_schedule_callout(sme);
565 DPRINTF(("%s: events framework initialized for '%s'\n",
566 __func__, sme->sme_name));
567
568 return error;
569 }
570
571 /*
572 * sme_schedule_callout
573 *
574 * (Re)-schedule the device's callout timer
575 */
576 void
577 sme_schedule_callout(struct sysmon_envsys *sme)
578 {
579 uint64_t timo;
580
581 KASSERT(sme != NULL);
582
583 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0)
584 return;
585
586 if (sme->sme_events_timeout)
587 timo = sme->sme_events_timeout * hz;
588 else
589 timo = SME_EVTIMO;
590
591 callout_stop(&sme->sme_callout);
592 callout_schedule(&sme->sme_callout, timo);
593 }
594
595 /*
596 * sme_events_destroy:
597 *
598 * + Destroys the event framework for this device: callout
599 * stopped, workqueue destroyed and callout mutex destroyed.
600 */
601 void
602 sme_events_destroy(struct sysmon_envsys *sme)
603 {
604 KASSERT(mutex_owned(&sme->sme_mtx));
605
606 callout_stop(&sme->sme_callout);
607 workqueue_destroy(sme->sme_wq);
608 mutex_destroy(&sme->sme_callout_mtx);
609 callout_destroy(&sme->sme_callout);
610 sme->sme_flags &= ~SME_CALLOUT_INITIALIZED;
611 DPRINTF(("%s: events framework destroyed for '%s'\n",
612 __func__, sme->sme_name));
613 }
614
615 /*
616 * sysmon_envsys_update_limits
617 *
618 * + If a driver needs to update the limits that it is providing,
619 * we need to update the dictionary data as well as the limits.
620 * This only makes sense if the driver is capable of providing
621 * its limits, and if there is a limits event-monitor.
622 */
623 int
624 sysmon_envsys_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata)
625 {
626 int err;
627
628 sysmon_envsys_acquire(sme, false);
629 if (sme->sme_get_limits == NULL ||
630 (edata->flags & ENVSYS_FMONLIMITS) == 0)
631 err = EINVAL;
632 else
633 err = sme_update_limits(sme, edata);
634 sysmon_envsys_release(sme, false);
635
636 return err;
637 }
638
639 /*
640 * sme_update_limits
641 *
642 * + Internal version of sysmon_envsys_update_limits() to be used
643 * when the device has already been sysmon_envsys_acquire()d.
644 */
645
646 int
647 sme_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata)
648 {
649 prop_dictionary_t sdict = NULL;
650 prop_array_t array = NULL;
651 sysmon_envsys_lim_t lims;
652 sme_event_t *see;
653 uint32_t props = 0;
654
655 /* Find the dictionary for this sensor */
656 array = prop_dictionary_get(sme_propd, sme->sme_name);
657 if (array == NULL ||
658 prop_object_type(array) != PROP_TYPE_ARRAY) {
659 DPRINTF(("%s: array device failed\n", __func__));
660 return EINVAL;
661 }
662
663 sdict = prop_array_get(array, edata->sensor);
664 if (sdict == NULL) {
665 return EINVAL;
666 }
667
668 /* Find the event definition to get its powertype */
669 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
670 if (edata == see->see_edata &&
671 see->see_type == PENVSYS_EVENT_LIMITS)
672 break;
673 }
674 if (see == NULL)
675 return EINVAL;
676
677 /* Update limit values from driver if possible */
678 if (sme->sme_get_limits != NULL)
679 (*sme->sme_get_limits)(sme, edata, &lims, &props);
680
681 /* Update event and dictionary */
682 sme_event_register(sdict, edata, sme, &lims, props,
683 PENVSYS_EVENT_LIMITS, see->see_pes.pes_type);
684
685 return 0;
686 }
687
688 /*
689 * sme_events_check:
690 *
691 * + Passes the events to the workqueue thread and stops
692 * the callout if the 'low-power' condition is triggered.
693 */
694 void
695 sme_events_check(void *arg)
696 {
697 struct sysmon_envsys *sme = arg;
698 sme_event_t *see;
699 uint64_t timo;
700
701 KASSERT(sme != NULL);
702
703 mutex_enter(&sme->sme_callout_mtx);
704 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
705 workqueue_enqueue(sme->sme_wq, &see->see_wk, NULL);
706 see->see_edata->flags |= ENVSYS_FNEED_REFRESH;
707 }
708 if (sme->sme_events_timeout)
709 timo = sme->sme_events_timeout * hz;
710 else
711 timo = SME_EVTIMO;
712 if (!sysmon_low_power)
713 sme_schedule_callout(sme);
714 mutex_exit(&sme->sme_callout_mtx);
715 }
716
717 /*
718 * sme_events_worker:
719 *
720 * + workqueue thread that checks if there's a critical condition
721 * and sends an event if it was triggered.
722 */
723 void
724 sme_events_worker(struct work *wk, void *arg)
725 {
726 sme_event_t *see = (void *)wk;
727 struct sysmon_envsys *sme = see->see_sme;
728 envsys_data_t *edata = see->see_edata;
729
730 KASSERT(wk == &see->see_wk);
731 KASSERT(sme != NULL || edata != NULL);
732
733 mutex_enter(&sme->sme_mtx);
734 see->see_flags |= SEE_EVENT_WORKING;
735 /*
736 * sme_events_check marks the sensors to make us refresh them here.
737 * Don't refresh if the driver uses its own method for refreshing.
738 */
739 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) {
740 if ((edata->flags & ENVSYS_FNEED_REFRESH) != 0) {
741 /* refresh sensor in device */
742 sysmon_envsys_refresh_sensor(sme, edata);
743 edata->flags &= ~ENVSYS_FNEED_REFRESH;
744 }
745 }
746
747 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d type=%d state=%d units=%d "
748 "value_cur=%d upropset=%d\n", __func__, sme->sme_name, edata->desc,
749 edata->sensor, see->see_type, edata->state, edata->units,
750 edata->value_cur, edata->upropset));
751
752 /* skip the event if current sensor is in invalid state */
753 if (edata->state == ENVSYS_SINVALID)
754 goto out;
755
756 /*
757 * For range limits, if the driver claims responsibility for
758 * limit/range checking, just user driver-supplied status.
759 * Else calculate our own status. Note that driver must
760 * relinquish responsibility for ALL limits if there is even
761 * one limit that it cannot handle!
762 *
763 * If this is a CAPACITY monitor, but the sensor's max_value
764 * is not set, treat it as though the monitor does not exist.
765 */
766 if ((see->see_type == PENVSYS_EVENT_LIMITS ||
767 see->see_type == PENVSYS_EVENT_CAPACITY) &&
768 (edata->upropset & PROP_DRIVER_LIMITS) == 0) {
769 if ((see->see_type == PENVSYS_EVENT_CAPACITY) &&
770 (edata->value_max == 0))
771 edata->state = ENVSYS_SVALID;
772 else if ((edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) &&
773 (edata->value_cur < edata->limits.sel_critmin))
774 edata->state = ENVSYS_SCRITUNDER;
775 else if ((edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) &&
776 (edata->value_cur < edata->limits.sel_warnmin))
777 edata->state = ENVSYS_SWARNUNDER;
778 else if ((edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) &&
779 (edata->value_cur > edata->limits.sel_critmax))
780 edata->state = ENVSYS_SCRITOVER;
781 else if ((edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) &&
782 (edata->value_cur > edata->limits.sel_warnmax))
783 edata->state = ENVSYS_SWARNOVER;
784 else
785 edata->state = ENVSYS_SVALID;
786 }
787 sme_deliver_event(see);
788
789 out:
790 see->see_flags &= ~SEE_EVENT_WORKING;
791 cv_broadcast(&sme->sme_condvar);
792 mutex_exit(&sme->sme_mtx);
793 }
794
795 /*
796 * sysmon_envsys_sensor_event
797 *
798 * + Find the monitor event of a particular type for a given sensor
799 * on a device and deliver the event if one is required. If
800 * no event type is specified, deliver all events for the sensor.
801 */
802 void
803 sysmon_envsys_sensor_event(struct sysmon_envsys *sme, envsys_data_t *edata,
804 int ev_type)
805 {
806 sme_event_t *see;
807
808 mutex_enter(&sme->sme_mtx);
809 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
810 if (edata != see->see_edata)
811 continue;
812 if (ev_type == 0 ||
813 ev_type == see->see_type) {
814 sme_deliver_event(see);
815 if (ev_type != 0)
816 break;
817 }
818 }
819 mutex_exit(&sme->sme_mtx);
820 }
821
822 /*
823 * sme_deliver_event:
824 *
825 * + If new sensor state requires it, send an event to powerd
826 *
827 * Must be called with the device's sysmon mutex held
828 * see->see_sme->sme_mtx
829 */
830 void
831 sme_deliver_event(sme_event_t *see)
832 {
833 envsys_data_t *edata = see->see_edata;
834 const struct sme_descr_entry *sdt = NULL;
835 const struct sme_sensor_event *sse = sme_sensor_event;
836 int i, state = 0;
837
838 switch (see->see_type) {
839 case PENVSYS_EVENT_LIMITS:
840 case PENVSYS_EVENT_CAPACITY:
841 /*
842 * Send event if state has changed
843 */
844 if (edata->state == see->see_evsent)
845 break;
846
847 for (i = 0; sse[i].state != -1; i++)
848 if (sse[i].state == edata->state)
849 break;
850
851 if (sse[i].state == -1)
852 break;
853
854 if (edata->state == ENVSYS_SVALID)
855 sysmon_penvsys_event(&see->see_pes,
856 PENVSYS_EVENT_NORMAL);
857 else
858 sysmon_penvsys_event(&see->see_pes, sse[i].event);
859
860 see->see_evsent = edata->state;
861 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d state=%d send_ev=%d\n",
862 __func__, see->see_sme->sme_name, edata->desc,
863 edata->sensor, edata->state,
864 (edata->state == ENVSYS_SVALID) ? PENVSYS_EVENT_NORMAL :
865 sse[i].event));
866
867 break;
868
869 /*
870 * Send PENVSYS_EVENT_CRITICAL event if:
871 * State has gone from non-CRITICAL to CRITICAL,
872 * State remains CRITICAL and value has changed, or
873 * State has returned from CRITICAL to non-CRITICAL
874 */
875 case PENVSYS_EVENT_CRITICAL:
876 if (edata->state == ENVSYS_SVALID &&
877 see->see_evsent != 0) {
878 sysmon_penvsys_event(&see->see_pes,
879 PENVSYS_EVENT_NORMAL);
880 see->see_evsent = 0;
881 } else if (edata->state == ENVSYS_SCRITICAL &&
882 see->see_evsent != edata->value_cur) {
883 sysmon_penvsys_event(&see->see_pes,
884 PENVSYS_EVENT_CRITICAL);
885 see->see_evsent = edata->value_cur;
886 }
887 break;
888
889 /*
890 * if value_cur is not normal (battery) or online (drive),
891 * send the event...
892 */
893 case PENVSYS_EVENT_STATE_CHANGED:
894 /*
895 * the state has not been changed, just ignore the event.
896 */
897 if (edata->value_cur == see->see_evsent)
898 break;
899
900 switch (edata->units) {
901 case ENVSYS_DRIVE:
902 sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES,
903 edata->value_cur);
904 state = ENVSYS_DRIVE_ONLINE;
905 break;
906 case ENVSYS_BATTERY_CAPACITY:
907 sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY,
908 edata->value_cur);
909 state = ENVSYS_BATTERY_CAPACITY_NORMAL;
910 break;
911 default:
912 panic("%s: bad units for PENVSYS_EVENT_STATE_CHANGED",
913 __func__);
914 }
915
916 if (sdt->type == -1)
917 break;
918
919 /*
920 * copy current state description.
921 */
922 (void)strlcpy(see->see_pes.pes_statedesc, sdt->desc,
923 sizeof(see->see_pes.pes_statedesc));
924
925 if (edata->value_cur == state)
926 /*
927 * state returned to normal condition
928 */
929 sysmon_penvsys_event(&see->see_pes,
930 PENVSYS_EVENT_NORMAL);
931 else
932 /*
933 * state changed to abnormal condition
934 */
935 sysmon_penvsys_event(&see->see_pes, see->see_type);
936
937 see->see_evsent = edata->value_cur;
938
939 /*
940 * There's no need to continue if it's a drive sensor.
941 */
942 if (edata->units == ENVSYS_DRIVE)
943 break;
944
945 /*
946 * Check if the system is running in low power and send the
947 * event to powerd (if running) or shutdown the system
948 * otherwise.
949 */
950 if (!sysmon_low_power && sme_event_check_low_power()) {
951 struct penvsys_state pes;
952
953 /*
954 * Stop the callout and send the 'low-power' event.
955 */
956 sysmon_low_power = true;
957 callout_stop(&see->see_sme->sme_callout);
958 pes.pes_type = PENVSYS_TYPE_BATTERY;
959 sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER);
960 }
961 break;
962 case PENVSYS_EVENT_NULL:
963 break;
964 default:
965 panic("%s: invalid event type %d", __func__, see->see_type);
966 }
967 }
968
969 /*
970 * Returns true if the system is in low power state: an AC adapter
971 * is OFF and all batteries are in LOW/CRITICAL state.
972 */
973 static bool
974 sme_event_check_low_power(void)
975 {
976 if (!sme_acadapter_check())
977 return false;
978
979 return sme_battery_check();
980 }
981
982 /*
983 * Called with the sysmon_envsys device mtx held through the
984 * workqueue thread.
985 */
986 static bool
987 sme_acadapter_check(void)
988 {
989 struct sysmon_envsys *sme;
990 envsys_data_t *edata;
991 bool dev = false, sensor = false;
992
993 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
994 if (sme->sme_class == SME_CLASS_ACADAPTER) {
995 dev = true;
996 break;
997 }
998 }
999
1000 /*
1001 * No AC Adapter devices were found.
1002 */
1003 if (!dev)
1004 return false;
1005
1006 /*
1007 * Check if there's an AC adapter device connected.
1008 */
1009 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1010 if (edata->units == ENVSYS_INDICATOR) {
1011 sensor = true;
1012 /* refresh current sensor */
1013 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0)
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