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