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