1 /* $NetBSD: sysmon_envsys_events.c,v 1.96 2010/12/15 17:17:17 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.96 2010/12/15 17:17:17 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 & edata->upropset & (PROP_CRITMAX | PROP_BATTMAX)) { 155 if (lims->sel_critmax == edata->limits.sel_critmax) { 156 DPRINTF(("%s: critmax exists\n", __func__)); 157 error = EEXIST; 158 props &= ~(PROP_CRITMAX | PROP_BATTMAX); 159 } 160 } 161 if (props & edata->upropset & (PROP_WARNMAX | PROP_BATTHIGH)) { 162 if (lims->sel_warnmax == edata->limits.sel_warnmax) { 163 DPRINTF(("%s: warnmax exists\n", __func__)); 164 error = EEXIST; 165 props &= ~(PROP_WARNMAX | PROP_BATTHIGH); 166 } 167 } 168 if (props & edata->upropset & (PROP_WARNMIN | PROP_BATTWARN)) { 169 if (lims->sel_warnmin == edata->limits.sel_warnmin) { 170 DPRINTF(("%s: warnmin exists\n", __func__)); 171 error = EEXIST; 172 props &= ~(PROP_WARNMIN | PROP_BATTWARN); 173 } 174 } 175 if (props & edata->upropset & (PROP_CRITMIN | PROP_BATTCAP)) { 176 if (lims->sel_critmin == edata->limits.sel_critmin) { 177 DPRINTF(("%s: critmin exists\n", __func__)); 178 error = EEXIST; 179 props &= ~(PROP_CRITMIN | PROP_BATTCAP); 180 } 181 } 182 break; 183 } 184 if (see == NULL) { 185 /* 186 * New event requested - allocate a sysmon_envsys event. 187 */ 188 see = kmem_zalloc(sizeof(*see), KM_SLEEP); 189 if (see == NULL) 190 return ENOMEM; 191 192 DPRINTF(("%s: dev %s sensor %s: new event\n", 193 __func__, sme->sme_name, edata->desc)); 194 195 see->see_type = crittype; 196 see->see_sme = sme; 197 see->see_edata = edata; 198 199 /* Initialize sensor type and previously-sent state */ 200 201 see->see_pes.pes_type = powertype; 202 203 switch (crittype) { 204 case PENVSYS_EVENT_LIMITS: 205 see->see_evsent = ENVSYS_SVALID; 206 break; 207 case PENVSYS_EVENT_CAPACITY: 208 see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL; 209 break; 210 case PENVSYS_EVENT_STATE_CHANGED: 211 if (edata->units == ENVSYS_BATTERY_CAPACITY) 212 see->see_evsent = ENVSYS_BATTERY_CAPACITY_NORMAL; 213 else if (edata->units == ENVSYS_DRIVE) 214 see->see_evsent = ENVSYS_DRIVE_EMPTY; 215 else 216 panic("%s: bad units for " 217 "PENVSYS_EVENT_STATE_CHANGED", __func__); 218 break; 219 case PENVSYS_EVENT_CRITICAL: 220 default: 221 see->see_evsent = 0; 222 break; 223 } 224 225 (void)strlcpy(see->see_pes.pes_dvname, sme->sme_name, 226 sizeof(see->see_pes.pes_dvname)); 227 (void)strlcpy(see->see_pes.pes_sensname, edata->desc, 228 sizeof(see->see_pes.pes_sensname)); 229 } 230 231 /* 232 * Limit operation requested. 233 */ 234 #define LIMIT_OP(k, l, p) \ 235 if (props & p) { \ 236 objkey = k; \ 237 obj = prop_dictionary_get(sdict, objkey); \ 238 if (obj != NULL && \ 239 prop_object_type(obj) != PROP_TYPE_NUMBER) { \ 240 DPRINTF(("%s: (%s) %s object no TYPE_NUMBER\n", \ 241 __func__, sme->sme_name, objkey)); \ 242 error = ENOTSUP; \ 243 } else { \ 244 edata->limits.l = lims->l; \ 245 error = sme_sensor_upint32(sdict, objkey,lims->l); \ 246 DPRINTF(("%s: (%s) event [sensor=%s type=%d] " \ 247 "(%s updated)\n", __func__, sme->sme_name, \ 248 edata->desc, crittype, objkey)); \ 249 } \ 250 if (error && error != EEXIST) \ 251 goto out; \ 252 edata->upropset |= p; \ 253 } 254 255 /* Value-based limits */ 256 LIMIT_OP("critical-max", sel_critmax, PROP_CRITMAX); 257 LIMIT_OP("warning-max", sel_warnmax, PROP_WARNMAX); 258 LIMIT_OP("warning-min", sel_warnmin, PROP_WARNMIN); 259 LIMIT_OP("critical-min", sel_critmin, PROP_CRITMIN); 260 261 /* %Capacity-based limits */ 262 LIMIT_OP("maximum-capacity", sel_critmax, PROP_BATTMAX); 263 LIMIT_OP("high-capacity", sel_warnmax, PROP_BATTHIGH); 264 LIMIT_OP("warning-capacity", sel_warnmin, PROP_BATTWARN); 265 LIMIT_OP("critical-capacity", sel_critmin, PROP_BATTCAP); 266 267 #undef LIMIT_OP 268 269 if (props & PROP_DRIVER_LIMITS) 270 edata->upropset |= PROP_DRIVER_LIMITS; 271 else 272 edata->upropset &= ~PROP_DRIVER_LIMITS; 273 274 DPRINTF(("%s: (%s) event registered (sensor=%s snum=%d type=%d " 275 "critmin=%" PRIu32 " warnmin=%" PRIu32 " warnmax=%" PRIu32 276 " critmax=%" PRIu32 " props 0x%04x)\n", __func__, 277 see->see_sme->sme_name, see->see_pes.pes_sensname, 278 edata->sensor, see->see_type, edata->limits.sel_critmin, 279 edata->limits.sel_warnmin, edata->limits.sel_warnmax, 280 edata->limits.sel_critmax, edata->upropset)); 281 /* 282 * Initialize the events framework if it wasn't initialized before. 283 */ 284 if ((sme->sme_flags & SME_CALLOUT_INITIALIZED) == 0) 285 error = sme_events_init(sme); 286 287 /* 288 * If driver requested notification, advise it of new 289 * limit values 290 */ 291 if (sme->sme_set_limits) 292 (*sme->sme_set_limits)(sme, edata, &(edata->limits), 293 &(edata->upropset)); 294 295 out: 296 if ((error == 0 || error == EEXIST) && osee == NULL) 297 LIST_INSERT_HEAD(&sme->sme_events_list, see, see_list); 298 299 mutex_exit(&sme->sme_mtx); 300 301 return error; 302 } 303 304 /* 305 * sme_event_unregister_all: 306 * 307 * + Unregisters all events associated with a sysmon envsys device. 308 */ 309 void 310 sme_event_unregister_all(struct sysmon_envsys *sme) 311 { 312 sme_event_t *see; 313 int evcounter = 0; 314 315 KASSERT(sme != NULL); 316 317 mutex_enter(&sme->sme_mtx); 318 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 319 while (see->see_flags & SEE_EVENT_WORKING) 320 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 321 322 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) 323 evcounter++; 324 } 325 326 DPRINTF(("%s: total events %d (%s)\n", __func__, 327 evcounter, sme->sme_name)); 328 329 while ((see = LIST_FIRST(&sme->sme_events_list))) { 330 if (evcounter == 0) 331 break; 332 333 if (strcmp(see->see_pes.pes_dvname, sme->sme_name) == 0) { 334 LIST_REMOVE(see, see_list); 335 DPRINTF(("%s: event %s %d removed (%s)\n", __func__, 336 see->see_pes.pes_sensname, see->see_type, 337 sme->sme_name)); 338 kmem_free(see, sizeof(*see)); 339 evcounter--; 340 } 341 } 342 343 if (LIST_EMPTY(&sme->sme_events_list)) 344 if (sme->sme_flags & SME_CALLOUT_INITIALIZED) 345 sme_events_destroy(sme); 346 mutex_exit(&sme->sme_mtx); 347 } 348 349 /* 350 * sme_event_unregister: 351 * 352 * + Unregisters an event from the specified sysmon envsys device. 353 */ 354 int 355 sme_event_unregister(struct sysmon_envsys *sme, const char *sensor, int type) 356 { 357 sme_event_t *see; 358 bool found = false; 359 360 KASSERT(sensor != NULL); 361 362 mutex_enter(&sme->sme_mtx); 363 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 364 if (strcmp(see->see_pes.pes_sensname, sensor) == 0) { 365 if (see->see_type == type) { 366 found = true; 367 break; 368 } 369 } 370 } 371 372 if (!found) { 373 mutex_exit(&sme->sme_mtx); 374 return EINVAL; 375 } 376 377 /* 378 * Wait for the event to finish its work, remove from the list 379 * and release resouces. 380 */ 381 while (see->see_flags & SEE_EVENT_WORKING) 382 cv_wait(&sme->sme_condvar, &sme->sme_mtx); 383 384 DPRINTF(("%s: removed dev=%s sensor=%s type=%d\n", 385 __func__, see->see_pes.pes_dvname, sensor, type)); 386 LIST_REMOVE(see, see_list); 387 /* 388 * So the events list is empty, we'll do the following: 389 * 390 * - stop and destroy the callout. 391 * - destroy the workqueue. 392 */ 393 if (LIST_EMPTY(&sme->sme_events_list)) 394 sme_events_destroy(sme); 395 mutex_exit(&sme->sme_mtx); 396 397 kmem_free(see, sizeof(*see)); 398 return 0; 399 } 400 401 /* 402 * sme_event_drvadd: 403 * 404 * + Registers a new event for a device that had enabled any of 405 * the monitoring flags in the driver. 406 */ 407 void 408 sme_event_drvadd(void *arg) 409 { 410 sme_event_drv_t *sed_t = arg; 411 sysmon_envsys_lim_t lims; 412 uint32_t props; 413 int error = 0; 414 415 KASSERT(sed_t != NULL); 416 417 #define SEE_REGEVENT(a, b, c) \ 418 do { \ 419 if (sed_t->sed_edata->flags & (a)) { \ 420 char str[ENVSYS_DESCLEN] = "monitoring-state-"; \ 421 \ 422 error = sme_event_register(sed_t->sed_sdict, \ 423 sed_t->sed_edata, \ 424 sed_t->sed_sme, \ 425 &lims, props, \ 426 (b), \ 427 sed_t->sed_powertype); \ 428 if (error && error != EEXIST) \ 429 printf("%s: failed to add event! " \ 430 "error=%d sensor=%s event=%s\n", \ 431 __func__, error, \ 432 sed_t->sed_edata->desc, (c)); \ 433 else { \ 434 (void)strlcat(str, (c), sizeof(str)); \ 435 prop_dictionary_set_bool(sed_t->sed_sdict, \ 436 str, \ 437 true); \ 438 } \ 439 } \ 440 } while (/* CONSTCOND */ 0) 441 442 /* 443 * If driver provides a method to retrieve its internal limit 444 * values, call it and use those returned values as initial 445 * limits for event monitoring. 446 */ 447 props = 0; 448 if (sed_t->sed_edata->flags & ENVSYS_FMONLIMITS) 449 if (sed_t->sed_sme->sme_get_limits) 450 (*sed_t->sed_sme->sme_get_limits)(sed_t->sed_sme, 451 sed_t->sed_edata, 452 &lims, &props); 453 /* 454 * If driver doesn't provide a way to "absorb" user-specified 455 * limit values, we must monitor all limits ourselves 456 */ 457 if (sed_t->sed_sme->sme_set_limits == NULL) 458 props &= ~PROP_DRIVER_LIMITS; 459 460 /* Register the events that were specified */ 461 462 SEE_REGEVENT(ENVSYS_FMONCRITICAL, 463 PENVSYS_EVENT_CRITICAL, 464 "critical"); 465 466 SEE_REGEVENT(ENVSYS_FMONSTCHANGED, 467 PENVSYS_EVENT_STATE_CHANGED, 468 "state-changed"); 469 470 SEE_REGEVENT(ENVSYS_FMONLIMITS, 471 PENVSYS_EVENT_LIMITS, 472 "hw-range-limits"); 473 474 /* 475 * we are done, free memory now. 476 */ 477 kmem_free(sed_t, sizeof(*sed_t)); 478 } 479 480 /* 481 * sme_events_init: 482 * 483 * + Initialize the events framework for this device. 484 */ 485 int 486 sme_events_init(struct sysmon_envsys *sme) 487 { 488 int error = 0; 489 uint64_t timo; 490 491 KASSERT(sme != NULL); 492 KASSERT(mutex_owned(&sme->sme_mtx)); 493 494 if (sme->sme_events_timeout) 495 timo = sme->sme_events_timeout * hz; 496 else 497 timo = SME_EVTIMO; 498 499 error = workqueue_create(&sme->sme_wq, sme->sme_name, 500 sme_events_worker, sme, PRI_NONE, IPL_SOFTCLOCK, WQ_MPSAFE); 501 if (error) 502 return error; 503 504 mutex_init(&sme->sme_callout_mtx, MUTEX_DEFAULT, IPL_SOFTCLOCK); 505 callout_init(&sme->sme_callout, CALLOUT_MPSAFE); 506 callout_setfunc(&sme->sme_callout, sme_events_check, sme); 507 callout_schedule(&sme->sme_callout, timo); 508 sme->sme_flags |= SME_CALLOUT_INITIALIZED; 509 DPRINTF(("%s: events framework initialized for '%s'\n", 510 __func__, sme->sme_name)); 511 512 return error; 513 } 514 515 /* 516 * sme_events_destroy: 517 * 518 * + Destroys the event framework for this device: callout 519 * stopped, workqueue destroyed and callout mutex destroyed. 520 */ 521 void 522 sme_events_destroy(struct sysmon_envsys *sme) 523 { 524 KASSERT(mutex_owned(&sme->sme_mtx)); 525 526 callout_stop(&sme->sme_callout); 527 workqueue_destroy(sme->sme_wq); 528 mutex_destroy(&sme->sme_callout_mtx); 529 callout_destroy(&sme->sme_callout); 530 sme->sme_flags &= ~SME_CALLOUT_INITIALIZED; 531 DPRINTF(("%s: events framework destroyed for '%s'\n", 532 __func__, sme->sme_name)); 533 } 534 535 /* 536 * sysmon_envsys_update_limits 537 * 538 * + If a driver needs to update the limits that it is providing, 539 * we need to update the dictionary data as well as the limits. 540 * This only makes sense if the driver is capable of providing 541 * its limits, and if there is a limits event-monitor. 542 */ 543 int 544 sysmon_envsys_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata) 545 { 546 int err; 547 548 sysmon_envsys_acquire(sme, false); 549 if (sme->sme_get_limits == NULL || 550 (edata->flags & ENVSYS_FMONLIMITS) == 0) 551 err = EINVAL; 552 else 553 err = sme_update_limits(sme, edata); 554 sysmon_envsys_release(sme, false); 555 556 return err; 557 } 558 559 /* 560 * sme_update_limits 561 * 562 * + Internal version of sysmon_envsys_update_limits() to be used 563 * when the device has already been sysmon_envsys_acquire()d. 564 */ 565 566 int 567 sme_update_limits(struct sysmon_envsys *sme, envsys_data_t *edata) 568 { 569 prop_dictionary_t sdict = NULL; 570 prop_array_t array = NULL; 571 sysmon_envsys_lim_t lims; 572 sme_event_t *see; 573 uint32_t props = 0; 574 575 /* Find the dictionary for this sensor */ 576 array = prop_dictionary_get(sme_propd, sme->sme_name); 577 if (array == NULL || 578 prop_object_type(array) != PROP_TYPE_ARRAY) { 579 DPRINTF(("%s: array device failed\n", __func__)); 580 return EINVAL; 581 } 582 583 sdict = prop_array_get(array, edata->sensor); 584 if (sdict == NULL) { 585 return EINVAL; 586 } 587 588 /* Find the event definition to get its powertype */ 589 LIST_FOREACH(see, &sme->sme_events_list, see_list) { 590 if (edata == see->see_edata && 591 see->see_type == PENVSYS_EVENT_LIMITS) 592 break; 593 } 594 if (see == NULL) 595 return EINVAL; 596 597 /* Update limit values from driver if possible */ 598 if (sme->sme_get_limits != NULL) 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_descr_entry *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__, see->see_sme->sme_name, edata->desc, 783 edata->sensor, 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_find_table_entry(SME_DESC_DRIVE_STATES, 823 edata->value_cur); 824 state = ENVSYS_DRIVE_ONLINE; 825 break; 826 case ENVSYS_BATTERY_CAPACITY: 827 sdt = sme_find_table_entry(SME_DESC_BATTERY_CAPACITY, 828 edata->value_cur); 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 if (sdt->type == -1) 837 break; 838 839 /* 840 * copy current state description. 841 */ 842 (void)strlcpy(see->see_pes.pes_statedesc, sdt->desc, 843 sizeof(see->see_pes.pes_statedesc)); 844 845 if (edata->value_cur == state) 846 /* 847 * state returned to normal condition 848 */ 849 sysmon_penvsys_event(&see->see_pes, 850 PENVSYS_EVENT_NORMAL); 851 else 852 /* 853 * state changed to abnormal condition 854 */ 855 sysmon_penvsys_event(&see->see_pes, see->see_type); 856 857 see->see_evsent = edata->value_cur; 858 859 /* 860 * There's no need to continue if it's a drive sensor. 861 */ 862 if (edata->units == ENVSYS_DRIVE) 863 break; 864 865 /* 866 * Check if the system is running in low power and send the 867 * event to powerd (if running) or shutdown the system 868 * otherwise. 869 */ 870 if (!sysmon_low_power && sme_event_check_low_power()) { 871 struct penvsys_state pes; 872 873 /* 874 * Stop the callout and send the 'low-power' event. 875 */ 876 sysmon_low_power = true; 877 callout_stop(&see->see_sme->sme_callout); 878 pes.pes_type = PENVSYS_TYPE_BATTERY; 879 sysmon_penvsys_event(&pes, PENVSYS_EVENT_LOW_POWER); 880 } 881 break; 882 default: 883 panic("%s: invalid event type %d", __func__, see->see_type); 884 } 885 } 886 887 /* 888 * Returns true if the system is in low power state: an AC adapter 889 * is OFF and all batteries are in LOW/CRITICAL state. 890 */ 891 static bool 892 sme_event_check_low_power(void) 893 { 894 if (!sme_acadapter_check()) 895 return false; 896 897 return sme_battery_check(); 898 } 899 900 /* 901 * Called with the sysmon_envsys device mtx held through the 902 * workqueue thread. 903 */ 904 static bool 905 sme_acadapter_check(void) 906 { 907 struct sysmon_envsys *sme; 908 envsys_data_t *edata; 909 bool dev = false, sensor = false; 910 911 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 912 if (sme->sme_class == SME_CLASS_ACADAPTER) { 913 dev = true; 914 break; 915 } 916 } 917 918 /* 919 * No AC Adapter devices were found. 920 */ 921 if (!dev) 922 return false; 923 924 /* 925 * Check if there's an AC adapter device connected. 926 */ 927 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 928 if (edata->units == ENVSYS_INDICATOR) { 929 sensor = true; 930 /* refresh current sensor */ 931 if ((sme->sme_flags & SME_DISABLE_REFRESH) == 0) 932 (*sme->sme_refresh)(sme, edata); 933 if (edata->value_cur) 934 return false; 935 } 936 } 937 938 if (!sensor) 939 return false; 940 941 /* 942 * AC adapter found and not connected. 943 */ 944 return true; 945 } 946 947 /* 948 * Called with the sysmon_envsys device mtx held through the 949 * workqueue thread. 950 */ 951 static bool 952 sme_battery_check(void) 953 { 954 struct sysmon_envsys *sme; 955 envsys_data_t *edata; 956 int batteriesfound = 0; 957 bool present, batterycap, batterycharge; 958 959 /* 960 * Check for battery devices and its state. 961 */ 962 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) { 963 if (sme->sme_class != SME_CLASS_BATTERY) 964 continue; 965 966 present = true; 967 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 968 if (edata->units == ENVSYS_INDICATOR && 969 !edata->value_cur) { 970 present = false; 971 break; 972 } 973 } 974 if (!present) 975 continue; 976 /* 977 * We've found a battery device... 978 */ 979 batteriesfound++; 980 batterycap = batterycharge = false; 981 TAILQ_FOREACH(edata, &sme->sme_sensors_list, sensors_head) { 982 if (edata->units == ENVSYS_BATTERY_CAPACITY) { 983 batterycap = true; 984 if (!sme_battery_critical(edata)) 985 return false; 986 } else if (edata->units == ENVSYS_BATTERY_CHARGE) { 987 batterycharge = true; 988 if (edata->value_cur) 989 return false; 990 } 991 } 992 if (!batterycap || !batterycharge) 993 return false; 994 } 995 996 if (!batteriesfound) 997 return false; 998 999 /* 1000 * All batteries in low/critical capacity and discharging. 1001 */ 1002 return true; 1003 } 1004 1005 static bool 1006 sme_battery_critical(envsys_data_t *edata) 1007 { 1008 if (edata->value_cur == ENVSYS_BATTERY_CAPACITY_CRITICAL || 1009 edata->value_cur == ENVSYS_BATTERY_CAPACITY_LOW) 1010 return true; 1011 1012 return false; 1013 } 1014