1 /* $NetBSD: sysmon_envsys_events.c,v 1.92 2010/04/10 19:01: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.92 2010/04/10 19:01: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 /* 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 if (sme->sme_get_limits == NULL || 550 (edata->flags & ENVSYS_FMONLIMITS) == 0) 551 return EINVAL; 552 553 sysmon_envsys_acquire(sme, false); 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 /* Get new limit values */ 599 (*sme->sme_get_limits)(sme, edata, &lims, &props); 600 601 /* Update event and dictionary */ 602 sme_event_register(sdict, edata, sme, &lims, props, 603 PENVSYS_EVENT_LIMITS, see->see_pes.pes_type); 604 605 return 0; 606 } 607 608 /* 609 * sme_events_check: 610 * 611 * + Passes the events to the workqueue thread and stops 612 * the callout if the 'low-power' condition is triggered. 613 */ 614 void 615 sme_events_check(void *arg) 616 { 617 struct sysmon_envsys *sme = arg; 618 sme_event_t *see; 619 uint64_t timo; 620 621 KASSERT(sme != NULL); 622 623 mutex_enter(&sme->sme_callout_mtx); 624 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 625 workqueue_enqueue(sme->sme_wq, &see->see_wk, NULL); 626 see->see_edata->flags |= ENVSYS_FNEED_REFRESH; 627 } 628 if (sme->sme_events_timeout) 629 timo = sme->sme_events_timeout * hz; 630 else 631 timo = SME_EVTIMO; 632 if (!sysmon_low_power) 633 callout_schedule(&sme->sme_callout, timo); 634 mutex_exit(&sme->sme_callout_mtx); 635 } 636 637 /* 638 * sme_events_worker: 639 * 640 * + workqueue thread that checks if there's a critical condition 641 * and sends an event if it was triggered. 642 */ 643 void 644 sme_events_worker(struct work *wk, void *arg) 645 { 646 sme_event_t *see = (void *)wk; 647 struct sysmon_envsys *sme = see->see_sme; 648 envsys_data_t *edata = see->see_edata; 649 650 KASSERT(wk == &see->see_wk); 651 KASSERT(sme != NULL || edata != NULL); 652 653 mutex_enter(&sme->sme_mtx); 654 see->see_flags |= SEE_EVENT_WORKING; 655 /* 656 * sme_events_check marks the sensors to make us refresh them here. 657 * Don't refresh if the driver uses its own method for refreshing. 658 */ 659 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) { 660 if ((edata->flags & ENVSYS_FNEED_REFRESH) != 0) { 661 /* refresh sensor in device */ 662 (*sme->sme_refresh)(sme, edata); 663 edata->flags &= ~ENVSYS_FNEED_REFRESH; 664 } 665 } 666 667 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d type=%d state=%d units=%d " 668 "value_cur=%d upropset=%d\n", __func__, sme->sme_name, edata->desc, 669 edata->sensor, see->see_type, edata->state, edata->units, 670 edata->value_cur, edata->upropset)); 671 672 /* skip the event if current sensor is in invalid state */ 673 if (edata->state == ENVSYS_SINVALID) 674 goto out; 675 676 /* 677 * For range limits, if the driver claims responsibility for 678 * limit/range checking, just user driver-supplied status. 679 * Else calculate our own status. Note that driver must 680 * relinquish responsibility for ALL limits if there is even 681 * one limit that it cannot handle! 682 * 683 * If this is a CAPACITY monitor, but the sensor's max_value 684 * is not set, treat it as though the monitor does not exist. 685 */ 686 if ((see->see_type == PENVSYS_EVENT_LIMITS || 687 see->see_type == PENVSYS_EVENT_CAPACITY) && 688 (edata->upropset & PROP_DRIVER_LIMITS) == 0) { 689 if ((see->see_type == PENVSYS_EVENT_CAPACITY) && 690 (edata->value_max == 0)) 691 edata->state = ENVSYS_SVALID; 692 else if ((edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) && 693 (edata->value_cur < edata->limits.sel_critmin)) 694 edata->state = ENVSYS_SCRITUNDER; 695 else if ((edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) && 696 (edata->value_cur < edata->limits.sel_warnmin)) 697 edata->state = ENVSYS_SWARNUNDER; 698 else if ((edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) && 699 (edata->value_cur > edata->limits.sel_critmax)) 700 edata->state = ENVSYS_SCRITOVER; 701 else if ((edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) && 702 (edata->value_cur > edata->limits.sel_warnmax)) 703 edata->state = ENVSYS_SWARNOVER; 704 else 705 edata->state = ENVSYS_SVALID; 706 } 707 sme_deliver_event(see); 708 709 out: 710 see->see_flags &= ~SEE_EVENT_WORKING; 711 cv_broadcast(&sme->sme_condvar); 712 mutex_exit(&sme->sme_mtx); 713 } 714 715 /* 716 * sysmon_envsys_sensor_event 717 * 718 * + Find the monitor event of a particular type for a given sensor 719 * on a device and deliver the event if one is required. If 720 * no event type is specified, deliver all events for the sensor. 721 */ 722 void 723 sysmon_envsys_sensor_event(struct sysmon_envsys *sme, envsys_data_t *edata, 724 int ev_type) 725 { 726 sme_event_t *see; 727 728 mutex_enter(&sme->sme_mtx); 729 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 730 if (edata != see->see_edata) 731 continue; 732 if (ev_type == 0 || 733 ev_type == see->see_type) { 734 sme_deliver_event(see); 735 if (ev_type != 0) 736 break; 737 } 738 } 739 mutex_exit(&sme->sme_mtx); 740 } 741 742 /* 743 * sme_deliver_event: 744 * 745 * + If new sensor state requires it, send an event to powerd 746 * 747 * Must be called with the device's sysmon mutex held 748 * see->see_sme->sme_mtx 749 */ 750 void 751 sme_deliver_event(sme_event_t *see) 752 { 753 envsys_data_t *edata = see->see_edata; 754 const struct sme_description_table *sdt = NULL; 755 const struct sme_sensor_event *sse = sme_sensor_event; 756 int i, state = 0; 757 758 switch (see->see_type) { 759 case PENVSYS_EVENT_LIMITS: 760 case PENVSYS_EVENT_CAPACITY: 761 /* 762 * Send event if state has changed 763 */ 764 if (edata->state == see->see_evsent) 765 break; 766 767 for (i = 0; sse[i].state != -1; i++) 768 if (sse[i].state == edata->state) 769 break; 770 771 if (sse[i].state == -1) 772 break; 773 774 if (edata->state == ENVSYS_SVALID) 775 sysmon_penvsys_event(&see->see_pes, 776 PENVSYS_EVENT_NORMAL); 777 else 778 sysmon_penvsys_event(&see->see_pes, sse[i].event); 779 780 see->see_evsent = edata->state; 781 DPRINTFOBJ(("%s: (%s) desc=%s sensor=%d state=%d send_ev=%d\n", 782 __func__, sme->sme_name, edata->desc, edata->sensor, 783 edata->state, 784 (edata->state == ENVSYS_SVALID) ? PENVSYS_EVENT_NORMAL : 785 sse[i].event)); 786 787 break; 788 789 /* 790 * Send PENVSYS_EVENT_CRITICAL event if: 791 * State has gone from non-CRITICAL to CRITICAL, 792 * State remains CRITICAL and value has changed, or 793 * State has returned from CRITICAL to non-CRITICAL 794 */ 795 case PENVSYS_EVENT_CRITICAL: 796 if (edata->state == ENVSYS_SVALID && 797 see->see_evsent != 0) { 798 sysmon_penvsys_event(&see->see_pes, 799 PENVSYS_EVENT_NORMAL); 800 see->see_evsent = 0; 801 } else if (edata->state == ENVSYS_SCRITICAL && 802 see->see_evsent != edata->value_cur) { 803 sysmon_penvsys_event(&see->see_pes, 804 PENVSYS_EVENT_CRITICAL); 805 see->see_evsent = edata->value_cur; 806 } 807 break; 808 809 /* 810 * if value_cur is not normal (battery) or online (drive), 811 * send the event... 812 */ 813 case PENVSYS_EVENT_STATE_CHANGED: 814 /* 815 * the state has not been changed, just ignore the event. 816 */ 817 if (edata->value_cur == see->see_evsent) 818 break; 819 820 switch (edata->units) { 821 case ENVSYS_DRIVE: 822 sdt = sme_get_description_table(SME_DESC_DRIVE_STATES); 823 state = ENVSYS_DRIVE_ONLINE; 824 break; 825 case ENVSYS_BATTERY_CAPACITY: 826 sdt = sme_get_description_table( 827 SME_DESC_BATTERY_CAPACITY); 828 state = ENVSYS_BATTERY_CAPACITY_NORMAL; 829 break; 830 default: 831 panic("%s: bad units for PENVSYS_EVENT_STATE_CHANGED", 832 __func__); 833 } 834 835 for (i = 0; sdt[i].type != -1; i++) 836 if (sdt[i].type == edata->value_cur) 837 break; 838 839 if (sdt[i].type == -1) 840 break; 841 842 /* 843 * copy current state description. 844 */ 845 (void)strlcpy(see->see_pes.pes_statedesc, sdt[i].desc, 846 sizeof(see->see_pes.pes_statedesc)); 847 848 if (edata->value_cur == state) 849 /* 850 * state returned to normal condition 851 */ 852 sysmon_penvsys_event(&see->see_pes, 853 PENVSYS_EVENT_NORMAL); 854 else 855 /* 856 * state changed to abnormal condition 857 */ 858 sysmon_penvsys_event(&see->see_pes, see->see_type); 859 860 see->see_evsent = edata->value_cur; 861 862 /* 863 * There's no need to continue if it's a drive sensor. 864 */ 865 if (edata->units == ENVSYS_DRIVE) 866 break; 867 868 /* 869 * Check if the system is running in low power and send the 870 * event to powerd (if running) or shutdown the system 871 * otherwise. 872 */ 873 if (!sysmon_low_power && sme_event_check_low_power()) { 874 struct penvsys_state pes; 875 876 /* 877 * Stop the callout and send the 'low-power' event. 878 */ 879 sysmon_low_power = true; 880 callout_stop(&see->see_sme->sme_callout); 881 pes.pes_type = PENVSYS_TYPE_BATTERY; 882 sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER); 883 } 884 break; 885 default: 886 panic("%s: invalid event type %d", __func__, see->see_type); 887 } 888 } 889 890 /* 891 * Returns true if the system is in low power state: an AC adapter 892 * is OFF and all batteries are in LOW/CRITICAL state. 893 */ 894 static bool 895 sme_event_check_low_power(void) 896 { 897 if (!sme_acadapter_check()) 898 return false; 899 900 return sme_battery_check(); 901 } 902 903 /* 904 * Called with the sysmon_envsys device mtx held through the 905 * workqueue thread. 906 */ 907 static bool 908 sme_acadapter_check(void) 909 { 910 struct sysmon_envsys *sme; 911 envsys_data_t *edata; 912 bool dev = false, sensor = false; 913 914 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 915 if (sme->sme_class == SME_CLASS_ACADAPTER) { 916 dev = true; 917 break; 918 } 919 } 920 921 /* 922 * No AC Adapter devices were found. 923 */ 924 if (!dev) 925 return false; 926 927 /* 928 * Check if there's an AC adapter device connected. 929 */ 930 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 931 if (edata->units == ENVSYS_INDICATOR) { 932 sensor = true; 933 /* refresh current sensor */ 934 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) 935 (*sme->sme_refresh)(sme, edata); 936 if (edata->value_cur) 937 return false; 938 } 939 } 940 941 if (!sensor) 942 return false; 943 944 /* 945 * AC adapter found and not connected. 946 */ 947 return true; 948 } 949 950 /* 951 * Called with the sysmon_envsys device mtx held through the 952 * workqueue thread. 953 */ 954 static bool 955 sme_battery_check(void) 956 { 957 struct sysmon_envsys *sme; 958 envsys_data_t *edata; 959 int batteriesfound = 0; 960 bool present, batterycap, batterycharge; 961 962 /* 963 * Check for battery devices and its state. 964 */ 965 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 966 if (sme->sme_class != SME_CLASS_BATTERY) 967 continue; 968 969 present = true; 970 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 971 if (edata->units == ENVSYS_INDICATOR && 972 !edata->value_cur) { 973 present = false; 974 break; 975 } 976 } 977 if (!present) 978 continue; 979 /* 980 * We've found a battery device... 981 */ 982 batteriesfound++; 983 batterycap = batterycharge = false; 984 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 985 if (edata->units == ENVSYS_BATTERY_CAPACITY) { 986 batterycap = true; 987 if (!sme_battery_critical(edata)) 988 return false; 989 } else if (edata->units == ENVSYS_BATTERY_CHARGE) { 990 batterycharge = true; 991 if (edata->value_cur) 992 return false; 993 } 994 } 995 if (!batterycap || !batterycharge) 996 return false; 997 } 998 999 if (!batteriesfound) 1000 return false; 1001 1002 /* 1003 * All batteries in low/critical capacity and discharging. 1004 */ 1005 return true; 1006 } 1007 1008 static bool 1009 sme_battery_critical(envsys_data_t *edata) 1010 { 1011 if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL || 1012 edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW) 1013 return true; 1014 1015 return false; 1016 } 1017