sysmon_envsys_events.c revision 1.101 1 /* $NetBSD: sysmon_envsys_events.c,v 1.101 2012/07/16 13:55:01 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.101 2012/07/16 13:55:01 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 * sme_envsys_refresh_sensor will not call the driver if the driver
738 * does its own setting of the sensor value.
739 */
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 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d type=%d state=%d units=%d "
747 "value_cur=%d upropset=%d\n", __func__, sme->sme_name, edata->desc,
748 edata->sensor, see->see_type, edata->state, edata->units,
749 edata->value_cur, edata->upropset));
750
751 /* skip the event if current sensor is in invalid state */
752 if (edata->state == ENVSYS_SINVALID)
753 goto out;
754
755 /*
756 * For range limits, if the driver claims responsibility for
757 * limit/range checking, just user driver-supplied status.
758 * Else calculate our own status. Note that driver must
759 * relinquish responsibility for ALL limits if there is even
760 * one limit that it cannot handle!
761 *
762 * If this is a CAPACITY monitor, but the sensor's max_value
763 * is not set, treat it as though the monitor does not exist.
764 */
765 if ((see->see_type == PENVSYS_EVENT_LIMITS ||
766 see->see_type == PENVSYS_EVENT_CAPACITY) &&
767 (edata->upropset & PROP_DRIVER_LIMITS) == 0) {
768 if ((see->see_type == PENVSYS_EVENT_CAPACITY) &&
769 (edata->value_max == 0))
770 edata->state = ENVSYS_SVALID;
771 else if ((edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) &&
772 (edata->value_cur < edata->limits.sel_critmin))
773 edata->state = ENVSYS_SCRITUNDER;
774 else if ((edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) &&
775 (edata->value_cur < edata->limits.sel_warnmin))
776 edata->state = ENVSYS_SWARNUNDER;
777 else if ((edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) &&
778 (edata->value_cur > edata->limits.sel_critmax))
779 edata->state = ENVSYS_SCRITOVER;
780 else if ((edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) &&
781 (edata->value_cur > edata->limits.sel_warnmax))
782 edata->state = ENVSYS_SWARNOVER;
783 else
784 edata->state = ENVSYS_SVALID;
785 }
786 sme_deliver_event(see);
787
788 out:
789 see->see_flags &= ~SEE_EVENT_WORKING;
790 cv_broadcast(&sme->sme_condvar);
791 mutex_exit(&sme->sme_mtx);
792 }
793
794 /*
795 * sysmon_envsys_sensor_event
796 *
797 * + Find the monitor event of a particular type for a given sensor
798 * on a device and deliver the event if one is required. If
799 * no event type is specified, deliver all events for the sensor.
800 */
801 void
802 sysmon_envsys_sensor_event(struct sysmon_envsys *sme, envsys_data_t *edata,
803 int ev_type)
804 {
805 sme_event_t *see;
806
807 mutex_enter(&sme->sme_mtx);
808 LIST_FOREACH(see, &sme->sme_events_list, see_list) {
809 if (edata != see->see_edata)
810 continue;
811 if (ev_type == 0 ||
812 ev_type == see->see_type) {
813 sme_deliver_event(see);
814 if (ev_type != 0)
815 break;
816 }
817 }
818 mutex_exit(&sme->sme_mtx);
819 }
820
821 /*
822 * sme_deliver_event:
823 *
824 * + If new sensor state requires it, send an event to powerd
825 *
826 * Must be called with the device's sysmon mutex held
827 * see->see_sme->sme_mtx
828 */
829 void
830 sme_deliver_event(sme_event_t *see)
831 {
832 envsys_data_t *edata = see->see_edata;
833 const struct sme_descr_entry *sdt = NULL;
834 const struct sme_sensor_event *sse = sme_sensor_event;
835 int i, state = 0;
836
837 switch (see->see_type) {
838 case PENVSYS_EVENT_LIMITS:
839 case PENVSYS_EVENT_CAPACITY:
840 /*
841 * Send event if state has changed
842 */
843 if (edata->state == see->see_evsent)
844 break;
845
846 for (i = 0; sse[i].state != -1; i++)
847 if (sse[i].state == edata->state)
848 break;
849
850 if (sse[i].state == -1)
851 break;
852
853 if (edata->state == ENVSYS_SVALID)
854 sysmon_penvsys_event(&see->see_pes,
855 PENVSYS_EVENT_NORMAL);
856 else
857 sysmon_penvsys_event(&see->see_pes, sse[i].event);
858
859 see->see_evsent = edata->state;
860 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d state=%d send_ev=%d\n",
861 __func__, see->see_sme->sme_name, edata->desc,
862 edata->sensor, edata->state,
863 (edata->state == ENVSYS_SVALID) ? PENVSYS_EVENT_NORMAL :
864 sse[i].event));
865
866 break;
867
868 /*
869 * Send PENVSYS_EVENT_CRITICAL event if:
870 * State has gone from non-CRITICAL to CRITICAL,
871 * State remains CRITICAL and value has changed, or
872 * State has returned from CRITICAL to non-CRITICAL
873 */
874 case PENVSYS_EVENT_CRITICAL:
875 if (edata->state == ENVSYS_SVALID &&
876 see->see_evsent != 0) {
877 sysmon_penvsys_event(&see->see_pes,
878 PENVSYS_EVENT_NORMAL);
879 see->see_evsent = 0;
880 } else if (edata->state == ENVSYS_SCRITICAL &&
881 see->see_evsent != edata->value_cur) {
882 sysmon_penvsys_event(&see->see_pes,
883 PENVSYS_EVENT_CRITICAL);
884 see->see_evsent = edata->value_cur;
885 }
886 break;
887
888 /*
889 * if value_cur is not normal (battery) or online (drive),
890 * send the event...
891 */
892 case PENVSYS_EVENT_STATE_CHANGED:
893 /*
894 * the state has not been changed, just ignore the event.
895 */
896 if (edata->value_cur == see->see_evsent)
897 break;
898
899 switch (edata->units) {
900 case ENVSYS_DRIVE:
901 sdt = sme_find_table_entry(SME_DESC_DRIVE_STATES,
902 edata->value_cur);
903 state = ENVSYS_DRIVE_ONLINE;
904 break;
905 case ENVSYS_BATTERY_CAPACITY:
906 sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY,
907 edata->value_cur);
908 state = ENVSYS_BATTERY_CAPACITY_NORMAL;
909 break;
910 default:
911 panic("%s: bad units for PENVSYS_EVENT_STATE_CHANGED",
912 __func__);
913 }
914
915 if (sdt->type == -1)
916 break;
917
918 /*
919 * copy current state description.
920 */
921 (void)strlcpy(see->see_pes.pes_statedesc, sdt->desc,
922 sizeof(see->see_pes.pes_statedesc));
923
924 if (edata->value_cur == state)
925 /*
926 * state returned to normal condition
927 */
928 sysmon_penvsys_event(&see->see_pes,
929 PENVSYS_EVENT_NORMAL);
930 else
931 /*
932 * state changed to abnormal condition
933 */
934 sysmon_penvsys_event(&see->see_pes, see->see_type);
935
936 see->see_evsent = edata->value_cur;
937
938 /*
939 * There's no need to continue if it's a drive sensor.
940 */
941 if (edata->units == ENVSYS_DRIVE)
942 break;
943
944 /*
945 * Check if the system is running in low power and send the
946 * event to powerd (if running) or shutdown the system
947 * otherwise.
948 */
949 if (!sysmon_low_power && sme_event_check_low_power()) {
950 struct penvsys_state pes;
951
952 /*
953 * Stop the callout and send the 'low-power' event.
954 */
955 sysmon_low_power = true;
956 callout_stop(&see->see_sme->sme_callout);
957 pes.pes_type = PENVSYS_TYPE_BATTERY;
958 sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER);
959 }
960 break;
961 case PENVSYS_EVENT_NULL:
962 break;
963 default:
964 panic("%s: invalid event type %d", __func__, see->see_type);
965 }
966 }
967
968 /*
969 * Returns true if the system is in low power state: an AC adapter
970 * is OFF and all batteries are in LOW/CRITICAL state.
971 */
972 static bool
973 sme_event_check_low_power(void)
974 {
975 if (!sme_acadapter_check())
976 return false;
977
978 return sme_battery_check();
979 }
980
981 /*
982 * Called with the sysmon_envsys device mtx held through the
983 * workqueue thread.
984 */
985 static bool
986 sme_acadapter_check(void)
987 {
988 struct sysmon_envsys *sme;
989 envsys_data_t *edata;
990 bool dev = false, sensor = false;
991
992 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
993 if (sme->sme_class == SME_CLASS_ACADAPTER) {
994 dev = true;
995 break;
996 }
997 }
998
999 /*
1000 * No AC Adapter devices were found.
1001 */
1002 if (!dev)
1003 return false;
1004
1005 /*
1006 * Check if there's an AC adapter device connected.
1007 */
1008 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1009 if (edata->units == ENVSYS_INDICATOR) {
1010 sensor = true;
1011 /* refresh current sensor */
1012 sysmon_envsys_refresh_sensor(sme, edata);
1013 if (edata->value_cur)
1014 return false;
1015 }
1016 }
1017
1018 if (!sensor)
1019 return false;
1020
1021 /*
1022 * AC adapter found and not connected.
1023 */
1024 return true;
1025 }
1026
1027 /*
1028 * Called with the sysmon_envsys device mtx held through the
1029 * workqueue thread.
1030 */
1031 static bool
1032 sme_battery_check(void)
1033 {
1034 struct sysmon_envsys *sme;
1035 envsys_data_t *edata;
1036 int batteriesfound = 0;
1037 bool present, batterycap, batterycharge;
1038
1039 /*
1040 * Check for battery devices and its state.
1041 */
1042 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
1043 if (sme->sme_class != SME_CLASS_BATTERY)
1044 continue;
1045
1046 present = true;
1047 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1048 if (edata->units == ENVSYS_INDICATOR &&
1049 !edata->value_cur) {
1050 present = false;
1051 break;
1052 }
1053 }
1054 if (!present)
1055 continue;
1056 /*
1057 * We've found a battery device...
1058 */
1059 batteriesfound++;
1060 batterycap = batterycharge = false;
1061 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) {
1062 if (edata->units == ENVSYS_BATTERY_CAPACITY) {
1063 batterycap = true;
1064 if (!sme_battery_critical(edata))
1065 return false;
1066 } else if (edata->units == ENVSYS_BATTERY_CHARGE) {
1067 batterycharge = true;
1068 if (edata->value_cur)
1069 return false;
1070 }
1071 }
1072 if (!batterycap || !batterycharge)
1073 return false;
1074 }
1075
1076 if (!batteriesfound)
1077 return false;
1078
1079 /*
1080 * All batteries in low/critical capacity and discharging.
1081 */
1082 return true;
1083 }
1084
1085 static bool
1086 sme_battery_critical(envsys_data_t *edata)
1087 {
1088 if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL ||
1089 edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW)
1090 return true;
1091
1092 return false;
1093 }
1094