Home | History | Annotate | only in /src/sys/dev/sysmon
History log of /src/sys/dev/sysmon
RevisionDateAuthorComments
 1.15 24-Apr-2015  pgoyette Update configuration definitions for new sysmon
 1.14 06-Sep-2012  macallan branches: 1.14.2; 1.14.16;
defflag debug #defines
 1.13 19-Oct-2010  pgoyette branches: 1.13.8;
Add a new software-only sensor device. This is intended for use as a
kernel module, and can be helpful to generate sysmon_envsys events
when creating/modifying powerd scripts. This will also be used in an
upcoming series of atf tests for sysmon_envsys itself.
 1.12 22-Jul-2010  pgoyette Convert swwdog(4) from a simple defpseudo device to a defpseudodev so
that we can attach a power management handler. The handler prevents
a suspend if the watchdog is active, to be consistent with other
watchdog drivers.

As discussed on tech-kern.
 1.11 30-Jan-2010  pooka branches: 1.11.2; 1.11.4;
Make swwdog use no-needs-count. As the manpage says, there can be only one.
 1.10 04-Sep-2007  xtraeme branches: 1.10.24;
- Remove ENVSYS_GSTRING and the genstr member from envsys_data_t.
(at least three or four persons were against it).
- Add a new sensor type: ENVSYS_BATTERY_STATE, this uses value_cur
and some predefined values in a static table, like ENVSYS_DRIVE.
- Move all static tables to sysmon_envsys_tables.c and use a function
on it to retrieve a pointer to the struct of the specified type.
- Rename the ENVSYS_FMONDRVSTCHANGED to ENVSYS_FMONSTCHANGED and make
it generic for Battery state and drive sensors (this flag enables
monitoring on these sensors when state has been changed).
- Update sysmon_penvsys_event() to report state changes on
ENVSYS_BATTERY_STATE sensors and remove other type of events, with
PENVSYS_EVENT_STATE_CHANGED they are not necessary anymore.
 1.9 02-Sep-2007  xtraeme Remove unused (and inexistent) flag.
 1.8 21-Jul-2007  xtraeme branches: 1.8.4; 1.8.6; 1.8.8;
Convert sysmon_taskqueue to use mutex(9) and condvar(9) and initialize
them in init_main.c via sysmon_task_queue_preinit().

Reviewed and ok by ad@.
 1.7 20-Jul-2007  xtraeme Move the functions to create/update objects in a dictionary into its
own file, and DO NOT MAKE THEM inline AS IT IS WRONG.

Looks like I'm very stupid and I didn't know what inline meant.
Thank you very much YAMAMOTO Takashi.
 1.6 01-Jul-2007  xtraeme branches: 1.6.2;
Imported envsys 2, a brief description of the new features:
(Part 1: API)

* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.

Tested by:

blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
 1.5 11-Dec-2005  christos branches: 1.5.30; 1.5.32;
merge ktrace-lwp.
 1.4 09-Jan-2005  smb branches: 1.4.10;
Add a software watchdog timer facility. Because this slightly
changes the "tickle" model of wdogctl(8), it was modified as well;
while I was in there, I cleaned up the argument parsing.

The code was reviewed by simonb@.
 1.3 20-Apr-2003  thorpej branches: 1.3.2;
Generic "task queue" for sysmon back-ends. This provides a thread
context for callbacks that require it in order to handle sysmon-related
events.

Adapted from the "OsdSchedule" bits of the ACPI code.
 1.2 17-Apr-2003  thorpej Start at a power management framework for sysmon. Right now we just
provide some VERY basic support for power/sleep buttons and lid switches;
if someone presses the power button, shut down the system semi-gracefully.

Eventually, we will send events for all types of button/lid events down
to a userland power management daemon, which will be able to define a
separate policy for each button/switch.
 1.1 16-Apr-2002  thorpej branches: 1.1.6; 1.1.8;
Move sysmon configuration definitions to dev/sysmon/files.sysmon.
 1.1.8.2 23-Jun-2002  jdolecek catch up with -current on kqueue branch
 1.1.8.1 16-Apr-2002  jdolecek file files.sysmon was added on branch kqueue on 2002-06-23 17:48:55 +0000
 1.1.6.2 20-Jun-2002  nathanw Catch up to -current.
 1.1.6.1 16-Apr-2002  nathanw file files.sysmon was added on branch nathanw_sa on 2002-06-20 03:46:47 +0000
 1.3.2.1 17-Jan-2005  skrll Sync with HEAD.
 1.4.10.2 27-Oct-2007  yamt sync with head.
 1.4.10.1 03-Sep-2007  yamt sync with head.
 1.5.32.1 11-Jul-2007  mjf Sync with head.
 1.5.30.3 09-Oct-2007  ad Sync with head.
 1.5.30.2 20-Aug-2007  ad Sync with HEAD.
 1.5.30.1 15-Jul-2007  ad Sync with head.
 1.6.2.3 10-Sep-2007  skrll Sync with HEAD.
 1.6.2.2 03-Sep-2007  skrll Sync with HEAD.
 1.6.2.1 15-Aug-2007  skrll Sync with HEAD.
 1.8.8.2 21-Jul-2007  xtraeme Convert sysmon_taskqueue to use mutex(9) and condvar(9) and initialize
them in init_main.c via sysmon_task_queue_preinit().

Reviewed and ok by ad@.
 1.8.8.1 21-Jul-2007  xtraeme file files.sysmon was added on branch matt-mips64 on 2007-07-21 23:15:18 +0000
 1.8.6.1 06-Nov-2007  matt sync with HEAD
 1.8.4.2 02-Oct-2007  joerg Sync with HEAD.
 1.8.4.1 03-Sep-2007  jmcneill Sync with HEAD.
 1.10.24.2 11-Aug-2010  yamt sync with head.
 1.10.24.1 11-Mar-2010  yamt sync with head
 1.11.4.1 05-Mar-2011  rmind sync with head
 1.11.2.2 22-Oct-2010  uebayasi Sync with HEAD (-D20101022).
 1.11.2.1 17-Aug-2010  uebayasi Sync with HEAD.
 1.13.8.1 30-Oct-2012  yamt sync with head
 1.14.16.1 06-Jun-2015  skrll Sync with HEAD
 1.14.2.1 03-Dec-2017  jdolecek update from HEAD
 1.20 10-Feb-2024  andvar Fix various typos in comments, log messages and documentation.
 1.19 31-Dec-2021  riastradh sysmon: Delete trailing whitespace. No functional change intended.
 1.18 11-Jun-2020  thorpej Update for proplib(3) API changes.
 1.17 08-Jun-2020  thorpej Update for proplib(3) API changes.
 1.16 16-Mar-2020  pgoyette Use the module subsystem's ability to process SYSCTL_SETUP() entries to
automate installation of sysctl nodes.

Note that there are still a number of device and pseudo-device modules
that create entries tied to individual device units, rather than to the
module itself. These are not changed.
 1.15 25-Apr-2015  pgoyette branches: 1.15.18;
Remove another debug printf() that snuck in.
 1.14 23-Apr-2015  pgoyette Update module dependencies for all the existing modules that depend on sysmon components.
 1.13 27-Aug-2012  pgoyette branches: 1.13.2; 1.13.16;
Add hw.swsensor.state variable for additional control capability.
 1.12 19-Jun-2011  pgoyette branches: 1.12.2; 1.12.8;
Use {...} to prevent premature exit from initialization function - this
lets the atf tests run again.

Move validation of current value vs min/max into the refresh routine
where it will get checked every time, not just at initialization.

While here, do a little more housekeeping:
- Retrieve numeric property value once
- Ensure that a numeric-specified sensor type is valid
- printout any unrecognized properties (DEBUG only)
 1.11 19-Jun-2011  pgoyette Use correct routine to retrieve string value from prop-dictionary
 1.10 19-Jun-2011  pgoyette Rather than requiring the user to know the internal encoding of sensor
types, allow the user to specify the type using the units description.

XXX Numeric specification is still permitted but will be removed soon.
 1.9 04-Jun-2011  pgoyette Remove ability to the value_avg since it no longer exists
 1.8 04-Jun-2011  pgoyette Enable creation of value_{max,min,avg} entries via the proplist, as well
as setting the ENVSYS_FPERCENT flag
 1.7 17-Dec-2010  pooka branches: 1.7.2; 1.7.6; 1.7.8;
use aprint
 1.6 16-Dec-2010  pgoyette Also allow specification of sensor's initial value when it is loaded,
using "-i value=<value>".

Suggested by njoly@
 1.5 11-Dec-2010  pgoyette Enhance the swsensor(4) pseudo-device's capabilities to emulate more
sensor types
 1.4 23-Oct-2010  pooka Revert 1.2 apart from incorrect comment. the devattach() routine
is of course still used by kernel&config pseudodev.

pointed out by jakllsch
 1.3 20-Oct-2010  pooka branches: 1.3.2;
Set sensor state to valid upon refresh to make things generally work.

pgoyette ok
 1.2 20-Oct-2010  pooka remove unused routine (per comment)
 1.1 19-Oct-2010  pgoyette Add a new software-only sensor device. This is intended for use as a
kernel module, and can be helpful to generate sysmon_envsys events
when creating/modifying powerd scripts. This will also be used in an
upcoming series of atf tests for sysmon_envsys itself.
 1.3.2.3 06-Nov-2010  uebayasi Sync with HEAD.
 1.3.2.2 22-Oct-2010  uebayasi Sync with HEAD (-D20101022).
 1.3.2.1 20-Oct-2010  uebayasi file swsensor.c was added on branch uebayasi-xip on 2010-10-22 07:22:17 +0000
 1.7.8.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.7.6.3 12-Jun-2011  rmind sync with head
 1.7.6.2 05-Mar-2011  rmind sync with head
 1.7.6.1 17-Dec-2010  rmind file swsensor.c was added on branch rmind-uvmplock on 2011-03-05 20:54:08 +0000
 1.7.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.12.8.1 17-Oct-2012  riz Pull up following revision(s) (requested by pgoyette in ticket #546):
sys/dev/sysmon/swsensor.c: revision 1.13
share/man/man4/swsensor.4: revision 1.11
share/man/man4/swsensor.4: revision 1.12
Add hw.swsensor.state variable for additional control capability.
Mention the new sysctl variable just created.
Drop trailing whitespace.
 1.12.2.1 30-Oct-2012  yamt sync with head
 1.13.16.1 06-Jun-2015  skrll Sync with HEAD
 1.13.2.1 03-Dec-2017  jdolecek update from HEAD
 1.15.18.1 08-Apr-2020  martin Merge changes from current as of 20200406
 1.24 07-Nov-2024  rin swwdog: Use SYSCTL_DESCR() to comply with SYSCTL_INCLUDE_DESCR option
 1.23 31-Dec-2021  riastradh branches: 1.23.10;
sysmon: Delete trailing whitespace. No functional change intended.
 1.22 16-Mar-2020  pgoyette Use the module subsystem's ability to process SYSCTL_SETUP() entries to
automate installation of sysctl nodes.

Note that there are still a number of device and pseudo-device modules
that create entries tied to individual device units, rather than to the
module itself. These are not changed.
 1.21 01-Jan-2020  thorpej - Introduce a new global kernel variable "shutting_down" to indicate that
the system is shutting down or rebooting.
- Set this global in a new function called kern_reboot(), which is currently
just a basic wrapper around cpu_reboot().
- Call kern_reboot() instead of cpu_reboot() almost everywhere; a few
places remain where it's still called directly, but those are in early
pre-main() machdep locations.

Eventually, all of the various cpu_reboot() functions should be re-factored
and common functionality moved to kern_reboot(), but that's for another day.
 1.20 27-Mar-2019  pgoyette Don't create the driver's workqueue twice!

The driver is modular, so its modcmd(MOULE_CMD_INIT) always gets called
whether or not the driver is built-in to the kernel. The modcmd init
code always calls swwdogattach() which creates the workqueue. Therefore
there's no need to also create it in swwdog_attach() (used to attach a
device instance).
 1.19 12-May-2015  pgoyette branches: 1.19.8; 1.19.18;
Finish work started in previous commit. Ensure that initialization
code is only called once for built-in variants of the module.
 1.18 12-May-2015  pgoyette Don't manipulate the autoconfig databases for built-in module.

Thanks, christos!
 1.17 24-Apr-2015  christos We only need the _cd when we are a module.
 1.16 23-Apr-2015  pgoyette Update module dependencies for all the existing modules that depend on sysmon components.
 1.15 18-Apr-2015  pgoyette Remove some debugging printf()s that were erroneously committed.
 1.14 18-Apr-2015  pgoyette Update attach/detach routines and provide MODULE interface.

Confirmed that atf tests (which run via rump) still pass, and also
confirmed that panic/reboot work in both monolithic kernel and
loaded module.
 1.13 13-Apr-2014  pgoyette branches: 1.13.4;
If we pmf_register on attach, we should pmf_deregister on detach.
 1.12 11-Nov-2010  pooka branches: 1.12.8; 1.12.18; 1.12.22; 1.12.32;
Apparently swwdog reboot hasn't worked in several years since it
tried to cpu_reboot() from a callout. Make it reboot from a workq
instead.

problem made manifest
by atf test
rumpfs unmount flush
caused issue to un-shush

tested: rump kernel (tests/dev/sysmon) and qemu
 1.11 06-Aug-2010  pooka * fix logic inversion in swwdog_reboot (and the sysctl).
* attach sysctl in constructor instead of as a link set to give it
a chance to work in a module
* teardown sysctl when driver is detached to avoid dangling pointer
 1.10 22-Jul-2010  pgoyette Convert swwdog(4) from a simple defpseudo device to a defpseudodev so
that we can attach a power management handler. The handler prevents
a suspend if the watchdog is active, to be consistent with other
watchdog drivers.

As discussed on tech-kern.
 1.9 31-Jan-2010  pooka branches: 1.9.2; 1.9.4;
print newline after timer expiry message
 1.8 30-Jan-2010  pooka Make swwdog use no-needs-count. As the manpage says, there can be only one.
 1.7 09-Jul-2007  ad branches: 1.7.32;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.6 29-Jan-2007  hubertf branches: 1.6.6; 1.6.8;
Remove more duplicate headers.
Patch by Slava Semushin <slava.semushin@gmail.com>

Again, this was tested by comparing obj files from a pristine and a patched
source tree against an i386/ALL kernel, and also for src/sbin/fsck_ffs,
src/sbin/fsdb and src/usr.sbin/makefs. Only changes in assert() line numbers
were detected in 'objdump -d' output.
 1.5 19-Dec-2006  smb Handle new gcc properly
 1.4 11-Dec-2005  christos branches: 1.4.20; 1.4.22;
merge ktrace-lwp.
 1.3 17-Oct-2005  simonb Spinkle some KNF.
 1.2 27-Feb-2005  perry branches: 1.2.4;
nuke trailing whitespace
 1.1 09-Jan-2005  smb branches: 1.1.2; 1.1.4; 1.1.6;
Add a software watchdog timer facility. Because this slightly
changes the "tickle" model of wdogctl(8), it was modified as well;
while I was in there, I cleaned up the argument parsing.

The code was reviewed by simonb@.
 1.1.6.1 19-Mar-2005  yamt sync with head. xen and whitespace. xen part is not finished.
 1.1.4.4 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.1.4.3 04-Mar-2005  skrll Sync with HEAD.

Hi Perry!
 1.1.4.2 17-Jan-2005  skrll Sync with HEAD.
 1.1.4.1 09-Jan-2005  skrll file swwdog.c was added on branch ktrace-lwp on 2005-01-17 19:31:52 +0000
 1.1.2.1 29-Apr-2005  kent sync with -current
 1.2.4.4 03-Sep-2007  yamt sync with head.
 1.2.4.3 26-Feb-2007  yamt sync with head.
 1.2.4.2 30-Dec-2006  yamt sync with head.
 1.2.4.1 21-Jun-2006  yamt sync with head.
 1.4.22.1 21-Dec-2006  yamt sync with head.
 1.4.20.2 01-Feb-2007  ad Sync with head.
 1.4.20.1 12-Jan-2007  ad Sync with head.
 1.6.8.1 11-Jul-2007  mjf Sync with head.
 1.6.6.1 01-Jul-2007  ad Adapt to callout API change.
 1.7.32.2 11-Aug-2010  yamt sync with head.
 1.7.32.1 11-Mar-2010  yamt sync with head
 1.9.4.1 05-Mar-2011  rmind sync with head
 1.9.2.1 17-Aug-2010  uebayasi Sync with HEAD.
 1.12.32.1 10-Aug-2014  tls Rebase.
 1.12.22.1 18-May-2014  rmind sync with head
 1.12.18.2 03-Dec-2017  jdolecek update from HEAD
 1.12.18.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.12.8.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.13.4.1 06-Jun-2015  skrll Sync with HEAD
 1.19.18.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.19.18.1 10-Jun-2019  christos Sync with HEAD
 1.19.8.2 17-May-2017  pgoyette Use correct variable name
 1.19.8.1 17-May-2017  pgoyette At suggestion of chuq@, modify config_attach_pseudo() to return with a
reference held on the device.

Adapt callers to expect the reference to exist, and to ensure that the
reference is released.
 1.23.10.1 02-Aug-2025  perseant Sync with HEAD
 1.32 28-Mar-2022  riastradh driver(9): devsw_detach never fails. Make it return void.

Prune a whole lotta dead branches as a result of this. (Some logic
calling this is also wrong for other reasons; devsw_detach is final
-- you should never have any reason to decide to roll it back. To be
cleaned up in subsequent commits...)

XXX kernel ABI change to devsw_detach signature requires bump
 1.31 31-Dec-2021  riastradh sysmon: Delete trailing whitespace. No functional change intended.
 1.30 26-Aug-2019  nakayama Module class of sysmon_envsys, sysmon_wdog and sysmon_power is
MODULE_CLASS_DRIVER, not MODULE_CLASS_MISC.

Fix that invoking envsys without sysmon_envsys kernel module failes with:
WARNING: module error: incompatible module class for `sysmon_envsys' (1 != 3)
 1.29 26-Apr-2019  pgoyette branches: 1.29.2;
Set the "required modules" to NULL, not to an empty string.

It really doesn't make that much difference to the code, but the output
from modstat(8) is different! (With an empty string in the MODULE() macro
modstat reports an empty string, but with a NULL in the macro, modstat
prints a '-' just like it does for other "empty" fields.)
 1.28 05-May-2015  pgoyette branches: 1.28.2; 1.28.8; 1.28.10; 1.28.18;
Optimize a bit - don't re-enter the mutex if we're just going to exit.

While here, remove some parens around a return value.
 1.27 05-May-2015  pgoyette If module_autoload() returns an error, just return that value instead
of overwriting with ENODEV.

Thanks, christos!
 1.26 04-May-2015  pgoyette If autoload of the subcomponent module fails, don't try to call its
open routine. Just return an error.

Hopefully this will fix the recently reported issues with atf tests
running on xen guest.
 1.25 29-Apr-2015  pgoyette At suggestion from riastradh@ ...

Remove auto_configure(9) goop from sysmon device. It does make things
a bit cleaner, and also reduces source code by about 10%.

Tested via QEMU with no obvious side-effects.
 1.24 25-Apr-2015  pgoyette Correctly check return status when registering with pmf
 1.23 25-Apr-2015  pgoyette Fix typo - thanks, riz@
 1.22 25-Apr-2015  pgoyette Register the sysmon pseudo-device with power management framework so we
can properly suspend the system.

Thanks, mrg, for pointing this out.
 1.21 24-Apr-2015  pgoyette With new (corrected) configuration declarations file, the cdevsw is
already included by config. So, for built-in sysmon module we don't
need to add the cdevsw to the table.
 1.20 23-Apr-2015  pgoyette Modularize sysmon and its components
 1.19 25-Jul-2014  dholland branches: 1.19.4;
Add d_discard to all struct cdevsw instances I could find.

All have been set to "nodiscard"; some should get a real implementation.
 1.18 16-Mar-2014  dholland branches: 1.18.2;
Change (mostly mechanically) every cdevsw/bdevsw I can find to use
designated initializers.

I have not built every extant kernel so I have probably broken at
least one build; however I've also found and fixed some wrong
cdevsw/bdevsw entries so even if so I think we come out ahead.
 1.17 03-Mar-2008  xtraeme branches: 1.17.38; 1.17.48; 1.17.54;
Pass D_MPSAFE to d_flag in the cdevsw struct, so that the kernel_lock
is not taken in the driver methods.

I've been running with this for some days without any effect.
 1.16 04-Jan-2008  ad branches: 1.16.2; 1.16.6;
Start detangling lock.h from intr.h. This is likely to cause short term
breakage, but the mess of dependencies has been regularly breaking the
build recently anyhow.
 1.15 04-Mar-2007  christos branches: 1.15.16; 1.15.22; 1.15.28;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.14 16-Nov-2006  christos branches: 1.14.4;
__unused removal on arguments; approved by core.
 1.13 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.12 03-Sep-2006  christos branches: 1.12.2; 1.12.4;
add missing initializers
 1.11 11-Dec-2005  christos branches: 1.11.4; 1.11.8;
merge ktrace-lwp.
 1.10 29-Jun-2003  fvdl branches: 1.10.2; 1.10.18;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.9 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.8 18-Apr-2003  thorpej * Add a generic power management event API, defined in <sys/power.h>.
Right now, only power switch state change events are supported. This
is a work-in-progress.
* Add support to sysmon for delivering power mangement events to userland.
Add poll, kqueue, and read entry points to sysmon.
* Adapt ACPI to use the new generic <sys/power.h> event types.

This provides the kernel support for a forthcoming powerd(8) which can
do nice things like gracefully shut the system down when an ACPI power
button is pressed.
 1.7 23-Oct-2002  jdolecek merge kqueue branch into -current

kqueue provides a stateful and efficient event notification framework
currently supported events include socket, file, directory, fifo,
pipe, tty and device changes, and monitoring of processes and signals

kqueue is supported by all writable filesystems in NetBSD tree
(with exception of Coda) and all device drivers supporting poll(2)

based on work done by Jonathan Lemon for FreeBSD
initial NetBSD port done by Luke Mewburn and Jason Thorpe
 1.6 06-Sep-2002  gehenna Merge the gehenna-devsw branch into the trunk.

This merge changes the device switch tables from static array to
dynamically generated by config(8).

- All device switches is defined as a constant structure in device drivers.

- The new grammer ``device-major'' is introduced to ``files''.

device-major <prefix> char <num> [block <num>] [<rules>]

- All device major numbers must be listed up in port dependent majors.<arch>
by using this grammer.

- Added the new naming convention.
The name of the device switch must be <prefix>_[bc]devsw for auto-generation
of device switch tables.

- The backward compatibility of loading block/character device
switch by LKM framework is broken. This is necessary to convert
from block/character device major to device name in runtime and vice versa.

- The restriction to assign device major by LKM is completely removed.
We don't need to reserve LKM entries for dynamic loading of device switch.

- In compile time, device major numbers list is packed into the kernel and
the LKM framework will refer it to assign device major number dynamically.
 1.5 13-Nov-2001  lukem branches: 1.5.8;
add RCSIDs
 1.4 05-Nov-2000  thorpej branches: 1.4.2; 1.4.4; 1.4.6; 1.4.8;
Split the envsys and watchdog parts of sysmon into their own
files.
 1.3 04-Nov-2000  thorpej Add a watchog timer framework to the system monitor.
 1.2 28-Jun-2000  thorpej branches: 1.2.2;
Fix a couple of brain-o's in error cases, and use PCATCH for
the sysmon mutex wait.
 1.1 24-Jun-2000  thorpej Add a clearing-house pseudo-device for system monitoring devices
such as the LM78 and VT82C686A (and eventually ACPI). Multiple
sensor devices can be hooked registered with `sysmon', and eventually
sysmon will also handle hardware (and software) watchdog timers.

Convert the `lm' and `viaenv' drivers to the new interface.
 1.2.2.3 30-Jul-2000  bouyer pull up 1.1 -> 1.2 (requested by thorpej):
Fix a couple of brain-o's in error cases, and use PCATCH for
the sysmon mutex wait.
 1.2.2.2 30-Jul-2000  bouyer Pull up (requested by thorpej):
lm_pnpbios.c 1.2->1.3
files 1.374->1.375
nslm7x.c 1.3->1.4
nslm7xvar.h 1.2->1.3
lm_isa.c 1.2->1.3
files.pci 1.96->1.97
viaenv.c 1.2->1.3
sysmon.c 1.1
sysmonvar.h 1.1

Add a clearing-house pseudo-device for system monitoring devices
such as the LM78 and VT82C686A (and eventually ACPI). Multiple
sensor devices can be hooked registered with `sysmon', and eventually
sysmon will also handle hardware (and software) watchdog timers.

Convert the `lm' and `viaenv' drivers to the new interface.
 1.2.2.1 28-Jun-2000  bouyer file sysmon.c was added on branch netbsd-1-5 on 2000-07-30 17:54:16 +0000
 1.4.8.1 18-Sep-2001  fvdl Various changes to make cloning devices possible:

* Add an extra argument (struct vnode **) to VOP_OPEN. If it is
not NULL, specfs will create a cloned (aliased) vnode during
the call, and return it there. The caller should release and
unlock the original vnode if a new vnode was returned. The
new vnode is returned locked.

* Add a flag field to the cdevsw and bdevsw structures.
DF_CLONING indicates that it wants a new vnode for each
open (XXX is there a better way? devprop?)

* If a device is cloning, always call the close entry
point for a VOP_CLOSE.


Also, rewrite cons.c to do the right thing with vnodes. Use VOPs
rather then direct device entry calls. Suggested by mycroft@

Light to moderate testing done an i386 system (arch doesn't matter
though, these are MI changes).
 1.4.6.2 10-Oct-2002  jdolecek sync kqueue with -current; this includes merge of gehenna-devsw branch,
merge of i386 MP branch, and part of autoconf rototil work
 1.4.6.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.4.4.3 11-Nov-2002  nathanw Catch up to -current
 1.4.4.2 17-Sep-2002  nathanw Catch up to -current.
 1.4.4.1 14-Nov-2001  nathanw Catch up to -current.
 1.4.2.3 22-Nov-2000  bouyer Sync with HEAD.
 1.4.2.2 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.
 1.4.2.1 05-Nov-2000  bouyer file sysmon.c was added on branch thorpej_scsipi on 2000-11-20 11:43:12 +0000
 1.5.8.1 16-May-2002  gehenna Add the character device switch.
 1.10.18.5 17-Mar-2008  yamt sync with head.
 1.10.18.4 21-Jan-2008  yamt sync with head
 1.10.18.3 03-Sep-2007  yamt sync with head.
 1.10.18.2 30-Dec-2006  yamt sync with head.
 1.10.18.1 21-Jun-2006  yamt sync with head.
 1.10.2.3 21-Sep-2004  skrll Fix the sync with head I botched.
 1.10.2.2 18-Sep-2004  skrll Sync with HEAD.
 1.10.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.11.8.1 03-Sep-2006  yamt sync with head.
 1.11.4.1 09-Sep-2006  rpaulo sync with head
 1.12.4.2 10-Dec-2006  yamt sync with head.
 1.12.4.1 22-Oct-2006  yamt sync with head
 1.12.2.1 18-Nov-2006  ad Sync with head.
 1.14.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.15.28.1 08-Jan-2008  bouyer Sync with HEAD
 1.15.22.1 18-Feb-2008  mjf Sync with HEAD.
 1.15.16.2 23-Mar-2008  matt sync with HEAD
 1.15.16.1 09-Jan-2008  matt sync with HEAD
 1.16.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.16.2.1 24-Mar-2008  keiichi sync with head.
 1.17.54.1 18-May-2014  rmind sync with head
 1.17.48.2 03-Dec-2017  jdolecek update from HEAD
 1.17.48.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.17.38.1 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.18.2.1 10-Aug-2014  tls Rebase.
 1.19.4.1 06-Jun-2015  skrll Sync with HEAD
 1.28.18.2 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.28.18.1 10-Jun-2019  christos Sync with HEAD
 1.28.10.1 26-Aug-2019  martin Pull up following revision(s) (requested by nakayama in ticket #1344):

sys/dev/sysmon/sysmon.c: revision 1.30

Module class of sysmon_envsys, sysmon_wdog and sysmon_power is
MODULE_CLASS_DRIVER, not MODULE_CLASS_MISC.

Fix that invoking envsys without sysmon_envsys kernel module failes with:
WARNING: module error: incompatible module class for `sysmon_envsys' (1 != 3)
 1.28.8.2 29-Apr-2017  pgoyette Remove explicit inclusion of <sys/localcount.h> since there is no
explicit usage of localcounts here. <sys/conf.h> will take care of
including as needed.
 1.28.8.1 27-Apr-2017  pgoyette Restore all work from the former pgoyette-localcount branch (which is
now abandoned doe to cvs merge botch).

The branch now builds, and installs via anita. There are still some
problems (cgd is non-functional and all atf tests time-out) but they
will get resolved soon.
 1.28.2.3 26-Jul-2016  pgoyette Rename LOCALCOUNT_INITIALIZER to DEVSW_MODULE_INIT. This better describes
what we're doing, and why.
 1.28.2.2 19-Jul-2016  pgoyette Instead of repeatedly typing the conditional initialization of the
.d_localcount members in the various {b,c}devsw, define an initializer
macro and use it. This also removes the need for defining new symbols
for each 'struct localcount'.

As suggested by riastradh@
 1.28.2.1 18-Jul-2016  pgoyette Rump drivers are always installed via devsw_attach() so we need to
always allocate a 'struct localcount' for these drivers whenever they
are built as modules.
 1.29.2.1 26-Aug-2019  martin Pull up following revision(s) (requested by nakayama in ticket #118):

sys/dev/sysmon/sysmon.c: revision 1.30

Module class of sysmon_envsys, sysmon_wdog and sysmon_power is
MODULE_CLASS_DRIVER, not MODULE_CLASS_MISC.

Fix that invoking envsys without sysmon_envsys kernel module failes with:
WARNING: module error: incompatible module class for `sysmon_envsys' (1 != 3)
 1.151 20-May-2022  andvar s/appropiate/appropriate/ in comments and readme file.
 1.150 31-Dec-2021  riastradh sysmon(9): Fix callout/thread synchronization.

Callout may ONLY take sme_work_mtx, at IPL_SOFTCLOCK; MUST NOT touch
sme_mtx at IPL_NONE. All state the callout needs is serialized by
sme_work_mtx now:

- calls to sme_schedule_callout
- calls to sme_schedule_halt
- struct sysmon_envsys::sme_events_timeout
- struct sysmon_envsys::sme_events_list
- struct sysmon_envsys::sme_callout_state
- struct envsys_data::flags
=> yes, this is a little silly -- used for ENVSYS_FNEED_REFRESH
=> should maybe separate the static driver-defined features from
the state flags needed by sysmon_envsys but not important now

Sleeping under sme_work_mtx (except on other adaptive locks at
IPL_SOFTCLOCK) is forbidden. Calling out to the driver under
sme_work_mtx is forbidden.

This should properly fix:

https://mail-index.netbsd.org/tech-kern/2015/10/14/msg019511.html
PR kern/56592
 1.149 31-Dec-2021  riastradh sysmon: Delete trailing whitespace. No functional change intended.
 1.148 07-Sep-2021  andvar s/aquire/acquire/ in comments, also one typo fix acqure->acquire.
 1.147 11-Jun-2020  thorpej Update for proplib(3) API changes.
 1.146 08-Jun-2020  thorpej Update for proplib(3) API changes.
 1.145 01-Jun-2020  riastradh Don't queue sysmon refresh until the rndsource is attached.

Using the rndsource, as refreshing the sensors will do, is not
allowed until _after_ rnd_source_attach.

XXX pullup-7
XXX pullup-8
XXX pullup-9
 1.144 26-Mar-2019  bad branches: 1.144.4;
in sysmon_envsys_unregister(): loop over sme_sensors_list with TAILQ_FIRST
instead of TAILQ_FOREACH.

sysmon_envsys_sensor_detach() removes the sensor from sme_sensors_list.
Hence using TAILQ_FOREACH is not safe.

Suggested by thorpej and jmcneill. Thanks!
 1.143 26-May-2018  thorpej branches: 1.143.2;
Avoid dereferencing NULL if we attempt to look up an known unit type.
 1.142 05-May-2018  pgoyette Defend against some table-lookup-not-found errors.
 1.141 11-Sep-2017  pgoyette branches: 1.141.2;
Improve tracking of the state of an event's callout, and protect all
queries or modifications of the state with the sme_mtx mutex.

Detach the rndsrc before re-attaching it (with potentially new values).

Clean up some lock-ordering issues.

And a couple of KNF issues for good measure!

Should address PR kern/52533

XXX Pullup-8 along with the previous fixes from msaitoh@
XXX http://mail-index.netbsd.org/source-changes/2017/09/06/msg088028.html
~
~
 1.140 06-Sep-2017  msaitoh Fixes a problem that some driver(e.g. acpitz(4) or coretemp(5)) which
use sysmon_envsys sleep waiting at "rndsrc" when "drvctl -d".
Don't call rnd_detach_source() in sme_remove_event() which is called
from sme_event_unregister_all(). Instead, call rnd_detach_source() in
sysmon_envsys_sensor_detach() and call sysmon_envsys_sensor_detach()
before sme_event_unregister_sensor(). Each sensor(envsys_data) has each
rnd_src, but some sme_events point to the same rnd_src in a sensor.
Calling rnd_detach_souce() twice with the same rnd_src brokes a reference
count in rnd_src. OK'd by pgoyette@.
 1.139 14-Dec-2015  pgoyette branches: 1.139.10;
sysmon's components need to be MODULE_CLASS_DRIVER so they will get
initialized before we configure/initialize any devices that interact
with them.

Thanks, marty!
 1.138 13-Dec-2015  jdc Note the sensor number in the error output. Useful for drivers adding
multiple sensors.
 1.137 25-Apr-2015  pgoyette Handle early initialization requirements - thanks martin@ and others
 1.136 25-Apr-2015  christos make things boot again, from martin.
 1.135 25-Apr-2015  pgoyette Use tabs for indentation.
 1.134 25-Apr-2015  pgoyette Release the mutex in the error path.
 1.133 24-Apr-2015  pgoyette The sysmon_envsys module also depends on sysmon_power (for delivery of
sensor events to userland).
 1.132 24-Apr-2015  pgoyette Remove a left-over debugging printf()
 1.131 23-Apr-2015  pgoyette Modularize sysmon and its components
 1.130 13-Apr-2015  riastradh Convert sys/dev to use <sys/rndsource.h>.
 1.129 14-Mar-2015  hannken Add a counter of busy events and stop enqueueing more work if a device is busy.
Protect this counter with a new short time lock "sme_work_mtx" and
keep "sme_mtx" as long time lock.

Removes a deadlock where an active event holds "sme_mtx", the callout
"sme_events_check" blocks on "sme_mtx" and callout processing stops.
 1.128 23-Nov-2014  ozaki-r branches: 1.128.2;
Pull workqueue_destroy out of sme->sme_mtx

workqueue_destroy may sleep so we shouldn't run it with holding a mutex.

Requested by riastradh@.
 1.127 10-Aug-2014  tls branches: 1.127.2;
Merge tls-earlyentropy branch into HEAD.
 1.126 29-Nov-2012  msaitoh branches: 1.126.10;
Remove trailing white space(s) from name of random source.
And also, changind space(s) in the middle with '-'.
PR#47232.
 1.125 06-Sep-2012  pgoyette branches: 1.125.2;
Remove (commented-out) definitions for debugging - thanks to macallan@
for defflagging these.
 1.124 27-Aug-2012  pgoyette Fix indentation. No code change.
 1.123 27-Aug-2012  pgoyette Simplify some logic - no functional change intended.

While here, clean up a couple of comments.
 1.122 19-Jul-2012  pgoyette Make sure to re-add the sensor as an entropy source after user request
to reset (via envstat -S)
 1.121 16-Jul-2012  pgoyette Extend previous changes so that even sensors which don't use a refresh()
callback to update the value can be polled to provide rnd(4) entropy.
 1.120 15-Jul-2012  pgoyette If a sensor is flagged as capable of providing rnd(4) with entropy,
hook the sensor into rnd subsystem, and make sure we periodically
refresh the sensor whether or not it is being actively monitored.
 1.119 15-Jul-2012  pgoyette When unregistering a sensor device, make sure we unregister and delete
all the associated events.
 1.118 18-Feb-2012  matt Don't export sysmon_envsys_list as a common.
 1.117 01-Aug-2011  mbalmer branches: 1.117.2; 1.117.6; 1.117.8;
According to the Oxford Dictionary of Etymology, a wether is a male sheep
or ram.
 1.116 19-Jun-2011  martin Instead of crashing, print a usable error message when a driver eroneously
does not properly initialize sensor state.
 1.115 15-Jun-2011  pgoyette Previous fix broke the setting of current value for Indicator and
Battery-Charge sensors. Fix this.
 1.114 08-Jun-2011  pgoyette Factor out some duplicated code to simplify maintenance. Reduces
the source file by ~100 lines, and amd64 object file shrinks by
~650 bytes.

No functional change intended.
 1.113 04-Jun-2011  pgoyette Since nothing actually records or maintains an average sensor value,
remove value_avg from the prop_dict. We can't completely remove it
because there's one driver that uses this field for its own private
purposes, so for now we just rename the member.
 1.112 30-Dec-2010  pgoyette branches: 1.112.2; 1.112.6;
When the user updates the sensor device's refresh timer, reset the
callout immediately rather than waiting for the previous timer to
expire.
 1.111 16-Dec-2010  njoly Be sure to always initialize props variable to a sensible value.
Fix a problem with envstat -S call which was installing bogus/random
limits.

ok pgoyette.
 1.110 15-Dec-2010  pgoyette Extract searching of description tables into a single function, rather
than duplicating the code every time. Minor reduction in code size
(about 1200 bytes on amd64), no change in functionality.
 1.109 11-Dec-2010  pgoyette Handle removal of limit properties correctly, even for sensors that
provide their own internal limit(s).
 1.108 08-Dec-2010  pgoyette When removing properties (as in envstat -S), clear the flags that track
whether the properties actually exist.

When setting new limit values, don't complain/ignore about duplicate
values if the associated flag bits (indicating that the limit is valid)
are not set.

These two fixes together should fix a problem reported in private Email
by njoly@ a couple months ago. Issue replicated and solution tested
using my recent swsensor pseudo-device running inside qemu world!
 1.107 11-Apr-2010  pgoyette Use a typedef to define the prototype of the per-sensor callback.

Update manpage for the prototype, and fix a fubar'd Cross-reference.
 1.106 10-Apr-2010  pgoyette When removing sensor properties with envstat -S, ensure that drivers
which could have modified hardware state are informed so that original
state can be restored.

Welcome to 5.99.27
 1.105 03-Apr-2010  pgoyette When unregistering a device, in addition to reducing the global count
of sensors, we need to adjust the sme_fsensor values for some devices.
Failure to do this leaves a sparesly-populated number-space and can
subsequently result in overlapping sensors number assignments. (This
only affects the compatability-mode API, but still needs to be fixed.)
 1.104 01-Apr-2010  pgoyette Permit creation of an event-monitor for alarm limits even if the limits
are not currently set (and, in case of battery capacity sensors, if there
is no value_max). Ensure that such an event-monitor does not trigger the
delivery of any actual events.

Provide a mechanism for sensors to set their limits at time other than
system startup (for example, when a battery is inserted).

This allows us to boot a system with a battery missing, install the
battery some time later, and automatically monitor it without requiring
any user intervention to create the event-monitor. (The actual changes
for battery sensors to use this new capability will come later.)
 1.103 27-Mar-2010  pgoyette Constify the args to sysmon_envsys_foreach_sensor()'s callback function.
 1.102 27-Mar-2010  pgoyette Fix the locking protocol in sysmon_envsys_foreach_sensor(), and add an
additional argument to control whether or not the sensors are refreshed
before invoking the callback routine.
 1.101 26-Mar-2010  pgoyette Make this work the way the preceeding comment blocks says it works, and
break out of the while{ } when the callback returns false, not when it
returns true!
 1.100 26-Mar-2010  pgoyette Add a routine to iterate over all the sensors on the system. This will
be needed to update acpiapm_get_powstat() which is currently slightly
broken due to recent rearrangement of acpi_bat(4)'s sensors. (This
approach is in lieu of exporting proplists between kernel entities, and
avoids exposing some internal sysmon_envsys details.)
 1.99 26-Mar-2010  pgoyette Export the sensor device's class.
 1.98 24-Mar-2010  pgoyette Do the initial refresh before creating the event monitors, in case the
event monitors require some of the data loaded by the refresh. For
example, the initial refresh might load sensor limits and max_value in
addition to the cur_value.
 1.97 14-Mar-2010  pgoyette branches: 1.97.2;
Retire the 'monitor' member of the envsys_data_t. It was only used in
one place, and functioned as a logical OR of the ENVSYS_FMON* flag bits.
 1.96 15-Feb-2010  pgoyette For symmetry, allow warning and critical upper-limits for capacity
sensors.
 1.95 14-Feb-2010  pgoyette Remove the flags member of the limits structure. Its value was only
meaningful for a few limited function calls; all persitent storage
of limit-present flags is maintained in the edata->upropset member.
 1.94 05-Feb-2010  pgoyette branches: 1.94.2;
One more callback of sme_refresh() routine needs to check first that
there is a routine to be called!
 1.93 31-Jan-2010  martin Fix an outdated comment, noticed by Paul Goyette.
 1.92 31-Jan-2010  martin Add a convenience function to find the maximum value currently reported
by a set (selected via a passed callback predicate) of sensors.
This provides an easy way to query the current temperature of a thermal
zone, for example, from within the kernel - assuming the caller knows
the topology.
 1.91 30-Jan-2010  pgoyette Validate limit-based monitoring in one place, so we can apply the
constraints to both driver-requested and user-requested monitoring.

While here, clean up some of the debugging DPRINTFs.
 1.90 10-Jul-2009  pgoyette Remove some code (bracketed by #ifdef NOTYET / #endif) that should not
have been committed in the first place.
 1.89 08-Jul-2009  pgoyette Remove driver name from debug message. Not all drivers have set this
early enough, and we can easily figure out the driver name from some
subsequent debug messages.
 1.88 13-Jun-2009  pgoyette Add capability to send sensor limit values to the driver so they can
be programmed into device registers. This way we can let the hardware
help us out instead of having to always compare the value against each
limit. (Driver updates for some sensors to take advantage of this
capability will be forthcoming.)
 1.87 08-Jun-2009  pgoyette General clean-up and some restructuring of event handling. This is a
precursor to letting sensor drivers actually exchange limits/thresholds
with user-land.
 1.86 03-Jun-2009  pgoyette Don't allocate the private list-threading structure unless we're going
to use it. Otherwise we end up leaking little bits of memory for each
sensor that is not monitored at time of initial registration.
 1.85 22-Aug-2008  pgoyette branches: 1.85.8;
1. provide for warning-min and warning-max alarm levels in addition to
critical-min and critical-max; also, a battery warning-capacity is
added in addition to a critical-capacity.

2. usr.sbin/envstat is modified to introduce a -W command line switch to
display the warning-* values instead of the critical-* values, and
envstat(8) and envsys.conf(5) man pages are updated appropriately.

3. Treat user-defined limits as a single continuum and generate a single
event regardless of how many boundaries a change in sensor value
crosses; ditto for driver-defined limits.

Fixes my PR/39021
Fixes my PR/39022

OK'd by christos@ bouyer@ cube@
 1.84 03-Jun-2008  jmcneill branches: 1.84.4;
Add SME_POLL_ONLY flag, which informs sme to only refresh the sensor when
it is polled (and not from GTREDATA).
 1.83 02-Apr-2008  xtraeme branches: 1.83.2; 1.83.4; 1.83.6;
Initialize and destroy the callout in sme_events_init() and
sme_events_destroy() respectively. And don't stop twice the callout
when calling sysmon_envsys_unregister().
 1.82 01-Apr-2008  xtraeme Introduce per-device locking/synchronization and maintain only a
global mutex for the linked list of devices and the global proplib
dictionary.

This has improved locking contention a lot when multiple devices with
multiple monitoring events are running:

New:

0.30 35 0.33 sme_global_mtx sysmonioctl_envsys+28b
0.10 10 0.11 00000000cd97feac sysmon_envsys_acquire+4c
0.08 6 0.09 00000000cd97feac sme_update_dictionary+24f
0.01 4 0.01 00000000cd97feac sme_events_worker+2f
0.01 10 0.01 00000000cd97fe2c sysmon_envsys_acquire+4c
0.00 1 0.00 00000000cd97fe2c sysmon_envsys_release+3b
28.38 94 9.16 sme_global_mtx sysmonioctl_envsys+28b
4.54 74 1.47 00000000cd97fe2c sysmon_envsys_acquire+4c
0.06 3 0.02 00000000cd97fe2c sysmon_envsys_release+3b
0.03 1 0.01 00000000cd97fe2c sme_events_worker+2f
1.40 19 0.45 00000000cd97bee4 sysmon_envsys_acquire+4c

Old:

4.25 313 4.74 sme_mtx <all>
3.12 185 3.49 sme_mtx sme_events_worker+21
1.12 128 1.25 sme_mtx sysmonioctl_envsys+29b
34.75 1423 59.52 sme_mtx <all>
22.08 477 37.82 sme_mtx sysmonioctl_envsys+29b
12.67 946 21.70 sme_mtx sme_events_worker+21
 1.81 23-Mar-2008  xtraeme Fix some problems reported by <drochner> a while ago on tech-kern:

sme_events_worker: skip sensors with invalid state.
sme_acadapter_check:

* use a boolean to check if an SME_CLASS_ACADAPTER was found.
* Always refresh sensor data before checking value_cur.

Introduce the SME_INIT_REFRESH flag. If a device sets this it will mean
that after interrupts are enabled in the autoconf(9) process, sensor
data will be refreshed once.
 1.80 02-Feb-2008  xtraeme branches: 1.80.6;
Destroy the callout via sysmon_envsys_destroy() now that it's
initialized in sysmon_envsys_create().
 1.79 02-Jan-2008  xtraeme - Initialize the callout with CALLOUT_MPSAFE, like it was done before
in sysmon_envsys_events.c.
- Separate sme_event_check_low_power() into sme_battery_check(),
sme_acadapter_check() and sme_battery_critical() (no functional changes).
 1.78 02-Jan-2008  dyoung In sysmon_envsys_create(), initialize the callout structure. Stops
panics with a DIAGNOSTIC kernel.

In sysmon_envsys_find_40(), wait until an envsys is not busy before
settings its busy flag, in the pattern of sysmon_envsys_find().
 1.77 02-Jan-2008  dyoung Use LIST_FOREACH().
 1.76 02-Jan-2008  dyoung Use TAILQ_FOREACH().
 1.75 05-Dec-2007  ad branches: 1.75.4;
MUTEX_SPIN -> MUTEX_DEFAULT.
 1.74 20-Nov-2007  xtraeme branches: 1.74.2;
Update a comment.
 1.73 20-Nov-2007  xtraeme After comments from Andrew Doran:

- The mutex for the callout handler must run at IPL_SOFTCLOCK.
- Just stop the callout in sysmon_envsys_unregister() and don't wait
for the callout to finish.
 1.72 16-Nov-2007  xtraeme Extend the envsys2 API (one more time, sorry) as defined in:

http://mail-index.netbsd.org/tech-kern/2007/11/09/0001.html

sysmon_envsys_create() and sysmon_envsys_destroy() were added to
create/destroy sysmon_envsys objects (and its TAILQ/LIST for sensors/events).

sysmon_envsys_sensor_attach() and sysmon_envsys_sensor_detach() were
added to attach/detach sensors to a specified sysmon_envsys device.

The events framework is now per device and configurable via the
ENVSYS_SETDICTIONARY ioctl or /etc/envsys.conf and envstat(8).

Update all users and documentation to reflect these changes.
 1.71 03-Nov-2007  xtraeme branches: 1.71.2;
- Rename ENVSYS_BATTERY_STATE units to ENVSYS_BATTERY_CAPACITY and
introduce ENVSYS_BATTERY_CHARGE, which is the same than an Indicator and
it's used to know if the battery is currently charging or discharging.

- Require two sensors at least for SME_CLASS_BATTERY to make the low-power
condition work: a ENVSYS_BATTERY_CAPACITY plus ENVSYS_BATTERY_CHARGE.

- Simplify sme_event_check_lowpower() and make it check the required
sensors in the SME_CLASS_ACADAPTER and SME_CLASS_BATTERY classes.
If the acadapter is not ready, trust the state returned by the battery
device.

Based on suggestion from joerg@.
 1.70 02-Nov-2007  plunky do not embed acpi device names in the sensor names
fake it in the compat code for the old envsys API
 1.69 07-Oct-2007  xtraeme branches: 1.69.2;
New changes to support the new configuration file format for
envstat(8) and the envsys framework:

- Modify the ENVSYS_SETDICTIONARY ioctl to support the following
plist structure:

<dict>
<key>foo0</key>
<array>
<dict>
<key>index</key>
<string>sensor0</string>
<key>description</key>
<string>cpu temp</string>
...
Another property for this sensor
...
</dict>
...
Another dictionary for other sensor
...
</array>
...
Another device as above
...
</dict>

Multiple devices with multiple sensors can now be specified, that means
that to set the properties only one copyin(9) is needed now.

- Added the ENVSYS_REMOVEPROPS ioctl, that accepts a boolean object
"envsys-remove-props" and when set to true, all properties that were
set previously by ENVSYS_SETDICTIONARY will be removed. That means that
you can now set multiple critical limits, descriptions and all they
will be removed or changed to its default value (for rfact and
description objects).

- Added the 'index' and 'allow-rfact' objects into the sensor dictionaries,
for better interactivity with userland. To know the position of the
sensor and to know if sensor allows to change the rfact.

- Misc cosmetic changes for consistency.

- Use a two clause license for all my code.
 1.68 24-Sep-2007  xtraeme sme_add_sensor_dictionary: don't forget to release the dictionary when
an error ocurrs. This is the last memleak in the code, let me know if
you find more and I'll pay you a beer.
 1.67 09-Sep-2007  xtraeme branches: 1.67.2;
Change the DPRINTFs in sysmon_envsys_destroy_plist() to DPRINTFOBJs
(second level of verbosity).
 1.66 08-Sep-2007  xtraeme sysmon_envsys_register: don't forget to clear SME_FLAG_BUSY when
registration succeeds.
 1.65 08-Sep-2007  xtraeme Use DPRINTF for a debugging printf.
 1.64 08-Sep-2007  xtraeme Remove an unused variable.
 1.63 08-Sep-2007  xtraeme Fix another memleak that only happened when using
sysmon_envsys_unregister(): in sysmon_envsys_destroy_plist() I wasn't
removing the _objects_ from the dictionary, instead I was calling
prop_object_release() with the keysym returned by
prop_object_iterator_next()... obviously this wasn't correct.
 1.62 08-Sep-2007  xtraeme Don't forget to free sme_evdrv in non error case too. Noticed and
reviewed by rmind.
 1.61 08-Sep-2007  xtraeme Use only one single condvar(9) for devices and events, they are protected
by the same mutex (sme_mtx) and there's no point in using two of them.

Also add a comment mentioning some locking notes.

Reviewed and ok by rmind.
 1.60 08-Sep-2007  xtraeme Disable debug, committed accidentally in previous.
 1.59 08-Sep-2007  xtraeme sysmon_envsys_register: only call sme_event_unregister_all when any
other error than EEXIST is returned.
 1.58 08-Sep-2007  xtraeme - Use only one global mutex for all tasks with devices & events.
- sysmon_envsys_register: use a SLIST to maintain allocated sme_event_drv_t
structs for later use in sysmon_task_queue_sched(). This avoids a
locking error acquiring/dropping the mutex multiple times.

Suggested by rmind.
 1.57 08-Sep-2007  xtraeme Remove another COMPAT_40 block, found by rmind@.
 1.56 08-Sep-2007  xtraeme - Build unconditionally compatibility code, there's no point to have it
disabled, because some drivers depend in the old code yet.
- Use a new mutex for sysmon_envsys_next_sensor_index (used in
compatibility code), otherwise bad things happen with LKMs. Thanks
to this, the hack for LKMs has been removed.
- Check in advance if the driver doesn't exist already on the list
before adding the sensors in the dictionary.
- Don't forget to call sme_event_unregister_all() if
sysmon_envsys_unregister() fails after adding the array into the global
dictionary or when creating sensors.
- Modify and add some DPRINTFs.

The lm(4) lkm works without known problems registering and unregistering
it multiple times.
 1.55 04-Sep-2007  xtraeme - Remove ENVSYS_GSTRING and the genstr member from envsys_data_t.
(at least three or four persons were against it).
- Add a new sensor type: ENVSYS_BATTERY_STATE, this uses value_cur
and some predefined values in a static table, like ENVSYS_DRIVE.
- Move all static tables to sysmon_envsys_tables.c and use a function
on it to retrieve a pointer to the struct of the specified type.
- Rename the ENVSYS_FMONDRVSTCHANGED to ENVSYS_FMONSTCHANGED and make
it generic for Battery state and drive sensors (this flag enables
monitoring on these sensors when state has been changed).
- Update sysmon_penvsys_event() to report state changes on
ENVSYS_BATTERY_STATE sensors and remove other type of events, with
PENVSYS_EVENT_STATE_CHANGED they are not necessary anymore.
 1.54 02-Sep-2007  xtraeme Add a new sensor type to sysmon_envsys(9): ENVSYS_GSTRING.

ENVSYS_GSTRING (aka Generic String) uses the genstr member in
the envsys_data_t struct to add a generic string that envstat(8)
will show as value or state.

It's like the ENVSYS_DRIVER, but doesn't use value_cur. Below
is the dictionary created on these sensors:

<dict>
<key>description</key>
<string>acpibat0 charge state</string>
<key>generic-state-string</key>
<string>NORMAL</string>
<key>monitoring-supported</key>
<false/>
<key>state</key>
<string>valid</string>
<key>type</key>
<string>Generic string</string>
</dict>

Note that it's limited to 32 chars, but we can grow it if needed.

envstat(8) will print ENVSYS_GSTRING sensors as:

$ envstat -dacpibat0 -s"acpibat0 charge state"
acpibat0 charge state: NORMAL
$
 1.53 01-Sep-2007  xtraeme - Fix a memleak in ENVSYS_SETDICTIONARY if there wasn't any error
(thanks god for KMEMSTATS).
- sysmon_envsys_register: add all objects in the dictionary without any
lock, at this point the sme device hasn't been added into the list
and it's safe.
- Add sysmon_envsys_destroy_plist(prop_array_t) that destroys all objects
associated with a device and use it on sysmon_envsys_unregister() and
sysmon_envsys_register() if there's any error.

Thanks to Mindaugas Rasiukevicius (rmind@) for the great comments/ideas.
 1.52 31-Aug-2007  xtraeme - sme_unregister_all: there's no need to use a struct sysmon_envsys as
argument, just pass sme->sme_name to it.
- sysmon_envsys_register: drop the array in all cases, not just when
there's an error. The reference is stored in the dictionary anyway...
- Update some comments.
 1.51 31-Aug-2007  xtraeme Merge sme_event_add() into sme_event_register()... there's no need
to have two different functions for this.
 1.50 31-Aug-2007  xtraeme Add a few KASSERT(mutex_owned(&foo_lock)) as suggested by Andrew Doran.
 1.49 30-Aug-2007  xtraeme More misc changes for sysmon_envsys(9):

- Add the SLIST for sensor descriptions and sme_uniqsensors into the
struct sysmon_envsys (it's per device now).
- Use only one common struct with three members for the static tables
(there's no need to have different structs just for them).
- While initializing/destroying the events framework, use the
strategy specified by Andrew Doran in:
http://mail-index.netbsd.org/tech-kern/2007/06/21/0025.html).
 1.48 30-Aug-2007  xtraeme Simplify sysmon_envsys_register() and fix an obvious error in
previous.
 1.47 30-Aug-2007  xtraeme Some changes to improve locking on sysmon_envsys(9):

- Remove sme_mtx, a global lock (sme_list_mtx) is used to access to
the sysmon envsys device.
- Allocate memory with KM_NOSLEEP rather than KM_SLEEP if there's a
mutex held, to avoid sleeping.
- Remove sysmon_envsys_createplist() and add the logic into
sysmon_envsys_register().
- sysmon_envsys_register: allocate the array and dictionaries required
in advance for a device before the locking and adding the objects
into the array happens.
- Rename sme_make_dictionary() to sme_add_sensor_dictionary() and pass
to it the dictionary on which the objects will be stored for a sensor.
- Improve locking here and there.

Thanks to Mindaugas Rasiukevicius and Andrew Doran for comments.
 1.46 05-Aug-2007  xtraeme branches: 1.46.2; 1.46.4;
In ENVSYS_SETDICTIONARY if sysmon_envsys_find() returns NULL, do not
call sysmon_envsys_release with a NULL argument.
 1.45 29-Jul-2007  xtraeme branches: 1.45.4;
- Do not call sme_gtredata in sysmon_envsys_createplist, as in some
systems the interrupts are not configured and causes unexpected behaviour
in acpi_bat and other drivers, reported by cube@.
- Update the 'type' object in sme_update_dictionary(), so that it can
report the correct units type in acpibat(4).
 1.44 27-Jul-2007  xtraeme Change locking stuff in sysmon_envsys_register() and
sysmon_envsys_createplist().
 1.43 23-Jul-2007  xtraeme - ENVSYS_SETDICTIONARY: use sysmon_envsys_release() if there's an error.
- sme_events_worker: use sme_list_mtx when accessing to the sysmon_envsys
linked list.
- Improve the comments in sysmon_envsysvar.h about the mutexes and condvar.
 1.42 23-Jul-2007  xtraeme - sysmon_envsys_createplist: do not return the error of sme->sme_gtredata
in all cases to the caller, just continue and show a debug printf if it
fails, because it's not a critical error.

- Improve some debugging printfs.

Fixes another part of yamt@'s PR kern/36673, that I forgot to commit
yesterday.
 1.41 22-Jul-2007  xtraeme Fix the last point reported by yamt in PR kern/36673:

- There must be three mandatory objects in a sensor to be valid and be
enabled on its dictionary: units, state and desc. If any of these
objects fails for some reason, its dictionary won't created in the
array and sensor will be marked with ENVSYS_FNOTVALID in edata->flags.

That means there won't never be a partial dictionary.

- Mark all invalid sensors and sensors with duplicate description with
ENVSYS_FNOTVALID and remove ENVSYS_FDUPDESC. The former is the generic
way to say that a sensor does not obey the rules and must be not
created.

- sysmon_envsys_createplist: if the array does not contain any object,
release the array and return the error.

And with all that stuff I can go to holidays and be happy.
 1.40 21-Jul-2007  xtraeme - Do not touch sme_nsensors when sensors with duplicate description are
found, rather maintain a global counter for all sensors that are unique,
and use this global to get the correct index in the array.
- Use prop_array_set() rather than prop_array_add() for adding dictionaries
into our global array. Thanks to yamt@.
- Overwrite the sensor index in the first sensor of a device, to avoid
a problem with LKMs where the first sensor gets the index of all edata
structures allocated in kernel.
- Add missing proto for sme_register_sensorname().

All them fix another part of yamt@'s PR.
 1.39 21-Jul-2007  xtraeme Use the mutex that belongs to the specific task and do not mix them
blindly.

- sme_list_mtx to access to the linked lists sme_list/sme_names_list.
- sme_mtx to access to the data in the sysmon envsys device.
 1.38 21-Jul-2007  xtraeme sme_update_dictionary: if there's a sensor with duplicate description,
there's no need to decrease 'i' again, just sme->sme_nsensors is enough.

Reported by yamt@.
 1.37 21-Jul-2007  xtraeme Simplify previous.
 1.36 21-Jul-2007  xtraeme Skip sensor with duplicate descriptions in the COMPAT_40 case, verified
with envstat(8) from netbsd-4 and sys/lkm/misc/envsys2.

Fixes another part of kern/36673 by yamt@.
 1.35 21-Jul-2007  xtraeme - Add sme_events_destroy() to stop/destroy the callout and workqueue if
there are no more events.
- Add sme_event_unregister_all() to remove all events associated with
a device and use it in sysmon_envsys_unregister() to fix a "use-after-free".
- Check return error of sme_sensor_upfoo() in sme_make_dictionary() and
return it rather than 0 all the time.
- Add more KASSERTS.
 1.34 21-Jul-2007  xtraeme Some fixes for kern/36673 by yamt@:

- sme_register_sensorname: there's no need to handle empty list case.
- 'obj' argument in sme_sensor_upstring and similar functions is useless:
removed the obj argument from the functions, it's handled in the function
itself now.
- check for more errors in sme_make_dictionary() and
sysmon_envsys_createplist().
 1.33 21-Jul-2007  xtraeme Re-add a strlcpy() to copy the new description in edata->desc, if the
object in dictionary was updated succesfully.
 1.32 20-Jul-2007  xtraeme Add another missing break if updating the drive-state object fails.
 1.31 20-Jul-2007  xtraeme Add a missing break if error when setting a new description.
 1.30 20-Jul-2007  xtraeme Replace the macros to create/update objects in dictionary with three
inline functions, suggested by yamt@.

This also gets rid of a few gotos.
 1.29 19-Jul-2007  xtraeme sysmon_envsys_unregister: do not kmem_free(9) with the mutex held.
 1.28 19-Jul-2007  xtraeme sme_register_sensorname: don't forget to kmem_free(9) before returning
EEXIST.
 1.27 19-Jul-2007  xtraeme Maintain a SLIST per device to check for unique descriptions and do not
create any dictionary if it's duplicated. Only the first sensor that has
a unique description will be added.

Remove all elements from the list at sysmon_envsys_unregister() time and
free resources associated with it.
 1.26 18-Jul-2007  xtraeme - More changes related to locking.
- Add a condvar(9) to protect deletions on the list if there's an event
in the worker thread.
 1.25 18-Jul-2007  xtraeme sme_make_dictionary: if prop_array_add() fails, don't forget to release
the dictionary. Pointed out by yamt@.
 1.24 17-Jul-2007  xtraeme branches: 1.24.2;
Add more debugging printfs to catch sensors with duplicate description,
and some cosmetic ones.
 1.23 17-Jul-2007  xtraeme Skip sensors with the same description on the same device at attach time,
if the description of it is not unique, mark the sensor with
ENVSYS_FDUPDESC, return EEXIST and continue.

Also, when changing a description, check that the new description is not
assigned by any other sensor, otherwise return EEXIST.

Note that these restrictions are only per device. Fixes PR kern/36660.
 1.22 17-Jul-2007  xtraeme Only allow to use ENVSYS_SETDICTIONARY if the user has write permission
in the /dev/sysmon device.

Use O_RDONLY for ENVSYS_GETDICTIONARY and O_RDWR for ENVSYS_SETDICTIONARY
in envstat(8).

Fixes PR kern/36661 by YAMAMOTO Takashi.
 1.21 16-Jul-2007  xtraeme There's no reason why integer sensors were disabled for monitoring,
so enable it. This will allow to set a min/max limit for the
macppc/battery.c driver, which uses an integer sensor to report the
battery capacity.

Indicator/Integer sensors will share the /etc/power/scripts/sensor_indicator
script to report the events.
 1.20 04-Jul-2007  xtraeme Fix the old ENVSYS_GTREDATA ioctl if there are sensors with invalid
state. Enable all sensors by default as valid and current valid data and
if there is one that is in invalid state, mark current data as not valid.

This matches the behaviour with the old API. Also, this fixes the problem
with ipmi(4) reporting bogus values with sensors in invalid state using
the old envstat(8).

Note that this only happened when using the old envstat(8).
 1.19 02-Jul-2007  xtraeme Add PENVSYS_TYPE_INDICATOR to handle events on Indicator sensors.

ipmi(4) uses this for the Intrussion and Power Supply sensors.
 1.18 01-Jul-2007  xtraeme Imported envsys 2, a brief description of the new features:
(Part 1: API)

* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.

Tested by:

blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
 1.17 04-Mar-2007  christos branches: 1.17.2; 1.17.4;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.16 19-Feb-2007  ad Back out previous for now. There isn't currently an easy way to set up
shared state before these get attached or used.

From xtraeme@.
 1.15 19-Feb-2007  xtraeme Replace SYSMON_ENVSYS_{LOCK,UNLOCK} with mutex_{enter,exit}.
 1.14 18-Feb-2007  xtraeme * Replace simple_locks and exclusive locks with adaptive mutexes.
* Replace ltsleep/wakeup pairs with condition variables
(cv_init/cv_wait/cv_broadcast).

Reviewed by ad@.
 1.13 16-Nov-2006  christos branches: 1.13.4;
__unused removal on arguments; approved by core.
 1.12 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.11 08-Jul-2006  christos branches: 1.11.4; 1.11.6;
expose the ioctl internal function now used by the acpi_apm code.
 1.10 20-Mar-2006  lukem branches: 1.10.4;
Don't attempt to call (*sme->sme_gtredata)() or (*sme->sme_streinfo)()
if those members are NULL.
As discussed on tech-kern in October/November 2005.
 1.9 11-Dec-2005  christos branches: 1.9.4; 1.9.6; 1.9.8; 1.9.10; 1.9.12;
merge ktrace-lwp.
 1.8 11-Aug-2003  yamt branches: 1.8.16;
do wakeup() only when someone is waiting for us.
 1.7 11-Aug-2003  yamt introduce SME_FLAG_BUSY and set it during operations
instead of keeping sysmon_envsys_list_slock spinlock held
because some drivers might sleep in sysmon_envsys ops,

XXX sysmon_envsys_lock is now redundant
 1.6 29-Jun-2003  fvdl branches: 1.6.2;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.5 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.4 31-Dec-2002  explorer add envsys support to acpi A/C adapter and batteries. This is still a work
in progress, much like the rest of ACPI.
 1.3 03-Jan-2002  jdolecek The device close routine is called when _last_ descriptor to the
device file is closed, rather on every descriptor close. Instead of
getting the exclusive lock on open and releasing on close, get
the lock only when needed in sysmonioctl_envsys().
Fixes kern/14368 by Anthony Mallet. Okayed by Jason Thorpe.

While here, also inline sysmon_envsys_init() to sysmonopen_envsys(),
and g/c sysmonioctl_wdog() prototype.
 1.2 13-Nov-2001  lukem add RCSIDs
 1.1 05-Nov-2000  thorpej branches: 1.1.2; 1.1.4; 1.1.6; 1.1.8;
Split the envsys and watchdog parts of sysmon into their own
files.
 1.1.8.1 18-Sep-2001  fvdl Various changes to make cloning devices possible:

* Add an extra argument (struct vnode **) to VOP_OPEN. If it is
not NULL, specfs will create a cloned (aliased) vnode during
the call, and return it there. The caller should release and
unlock the original vnode if a new vnode was returned. The
new vnode is returned locked.

* Add a flag field to the cdevsw and bdevsw structures.
DF_CLONING indicates that it wants a new vnode for each
open (XXX is there a better way? devprop?)

* If a device is cloning, always call the close entry
point for a VOP_CLOSE.


Also, rewrite cons.c to do the right thing with vnodes. Use VOPs
rather then direct device entry calls. Suggested by mycroft@

Light to moderate testing done an i386 system (arch doesn't matter
though, these are MI changes).
 1.1.6.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.1.4.4 03-Jan-2003  thorpej Sync with HEAD.
 1.1.4.3 28-Feb-2002  nathanw Catch up to -current.
 1.1.4.2 11-Jan-2002  nathanw More catchup.
 1.1.4.1 14-Nov-2001  nathanw Catch up to -current.
 1.1.2.2 22-Nov-2000  bouyer Sync with HEAD.
 1.1.2.1 05-Nov-2000  bouyer file sysmon_envsys.c was added on branch thorpej_scsipi on 2000-11-22 16:04:54 +0000
 1.6.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.6.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.6.2.2 03-Aug-2004  skrll Sync with HEAD
 1.6.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.8.16.9 24-Mar-2008  yamt sync with head.
 1.8.16.8 04-Feb-2008  yamt sync with head.
 1.8.16.7 21-Jan-2008  yamt sync with head
 1.8.16.6 07-Dec-2007  yamt sync with head
 1.8.16.5 15-Nov-2007  yamt sync with head.
 1.8.16.4 27-Oct-2007  yamt sync with head.
 1.8.16.3 03-Sep-2007  yamt sync with head.
 1.8.16.2 30-Dec-2006  yamt sync with head.
 1.8.16.1 21-Jun-2006  yamt sync with head.
 1.9.12.1 28-Mar-2006  tron Merge 2006-03-28 NetBSD-current into the "peter-altq" branch.
 1.9.10.1 19-Apr-2006  elad sync with head.
 1.9.8.2 11-Aug-2006  yamt sync with head
 1.9.8.1 01-Apr-2006  yamt sync with head.
 1.9.6.1 22-Apr-2006  simonb Sync with head.
 1.9.4.1 09-Sep-2006  rpaulo sync with head
 1.10.4.1 13-Jul-2006  gdamore Merge from HEAD.
 1.11.6.2 10-Dec-2006  yamt sync with head.
 1.11.6.1 22-Oct-2006  yamt sync with head
 1.11.4.1 18-Nov-2006  ad Sync with head.
 1.13.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.17.4.1 11-Jul-2007  mjf Sync with head.
 1.17.2.4 09-Oct-2007  ad Sync with head.
 1.17.2.3 20-Aug-2007  ad Sync with HEAD.
 1.17.2.2 15-Jul-2007  ad Sync with head.
 1.17.2.1 15-Jul-2007  ad Sync with head.
 1.24.2.3 10-Sep-2007  skrll Sync with HEAD.
 1.24.2.2 03-Sep-2007  skrll Sync with HEAD.
 1.24.2.1 15-Aug-2007  skrll Sync with HEAD.
 1.45.4.7 09-Dec-2007  jmcneill Sync with HEAD.
 1.45.4.6 21-Nov-2007  joerg Sync with HEAD.
 1.45.4.5 04-Nov-2007  jmcneill Sync with HEAD.
 1.45.4.4 07-Oct-2007  joerg Sync with HEAD.
 1.45.4.3 02-Oct-2007  joerg Sync with HEAD.
 1.45.4.2 03-Sep-2007  jmcneill Sync with HEAD.
 1.45.4.1 09-Aug-2007  jmcneill Sync with HEAD.
 1.46.4.2 05-Aug-2007  xtraeme In ENVSYS_SETDICTIONARY if sysmon_envsys_find() returns NULL, do not
call sysmon_envsys_release with a NULL argument.
 1.46.4.1 05-Aug-2007  xtraeme file sysmon_envsys.c was added on branch matt-mips64 on 2007-08-05 23:16:26 +0000
 1.46.2.3 23-Mar-2008  matt sync with HEAD
 1.46.2.2 09-Jan-2008  matt sync with HEAD
 1.46.2.1 06-Nov-2007  matt sync with HEAD
 1.67.2.2 14-Oct-2007  yamt sync with head.
 1.67.2.1 06-Oct-2007  yamt sync with head.
 1.69.2.3 21-Nov-2007  bouyer Sync with HEAD
 1.69.2.2 18-Nov-2007  bouyer Sync with HEAD
 1.69.2.1 13-Nov-2007  bouyer Sync with HEAD
 1.71.2.3 18-Feb-2008  mjf Sync with HEAD.
 1.71.2.2 08-Dec-2007  mjf Sync with HEAD.
 1.71.2.1 19-Nov-2007  mjf Sync with HEAD.
 1.74.2.1 08-Dec-2007  ad Sync with head.
 1.75.4.1 02-Jan-2008  bouyer Sync with HEAD
 1.80.6.3 28-Sep-2008  mjf Sync with HEAD.
 1.80.6.2 05-Jun-2008  mjf Sync with HEAD.

Also fix build.
 1.80.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.83.6.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.83.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.83.4.5 11-Aug-2010  yamt sync with head.
 1.83.4.4 11-Mar-2010  yamt sync with head
 1.83.4.3 18-Jul-2009  yamt sync with head.
 1.83.4.2 20-Jun-2009  yamt sync with head
 1.83.4.1 04-May-2009  yamt sync with head.
 1.83.2.1 04-Jun-2008  yamt sync with head
 1.84.4.1 19-Oct-2008  haad Sync with HEAD.
 1.85.8.1 23-Jul-2009  jym Sync with HEAD.
 1.94.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.97.2.3 12-Jun-2011  rmind sync with head
 1.97.2.2 05-Mar-2011  rmind sync with head
 1.97.2.1 30-May-2010  rmind sync with head
 1.112.6.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.112.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.117.8.2 19-Oct-2012  riz Remove stray CR characters introduced in ticket #544. From pgoyette.
 1.117.8.1 17-Oct-2012  riz Pull up following revision(s) (requested by pgoyette in ticket #544):
sys/dev/sysmon/sysmon_envsysvar.h: revision 1.42
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.99
sys/dev/sysmon/sysmon_envsys.c: revision 1.119
When unregistering a sensor device, make sure we unregister and delete
all the associated events.
 1.117.6.1 18-Feb-2012  mrg merge to -current.
 1.117.2.3 16-Jan-2013  yamt sync with (a bit old) head
 1.117.2.2 30-Oct-2012  yamt sync with head
 1.117.2.1 17-Apr-2012  yamt sync with head
 1.125.2.3 03-Dec-2017  jdolecek update from HEAD
 1.125.2.2 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.125.2.1 25-Feb-2013  tls resync with head
 1.126.10.1 07-Apr-2014  tls Be a little more clear and consistent about harvesting entropy from devices:

1) deprecate RND_FLAG_NO_ESTIMATE

2) define RND_FLAG_COLLECT_TIME, RND_FLAG_COLLECT_VALUE

3) define RND_FLAG_ESTIMATE_TIME, RND_FLAG_ESTIMATE_VALUE

4) define RND_FLAG_DEFAULT: RND_FLAG_COLLECT_TIME|
RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_TIME

5) Make entropy harvesting from environmental sensors a little more generic
and remove it from individual sensor drivers.

6) Remove individual open-coded delta-estimators for values from a few
places in the tree (uvm, environmental drivers).

7) 0 -> RND_FLAG_DEFAULT, actually gather entropy from various drivers
that had stubbed out code, other minor cleanups.
 1.127.2.4 07-Jun-2020  martin Pull up following revision(s) (requested by riastradh in ticket #1733):

sys/dev/sysmon/sysmon_envsys.c: revision 1.145

Don't queue sysmon refresh until the rndsource is attached.

Using the rndsource, as refreshing the sensors will do, is not
allowed until _after_ rnd_source_attach.

XXX pullup-7
XXX pullup-8
XXX pullup-9
 1.127.2.3 24-Oct-2017  snj Fix fallout from ticket #1511:
It's rnd_detach_source, not rnd_detach_sources.
 1.127.2.2 23-Oct-2017  snj Apply patch (requested by pgoyette in ticket #1511):
Detach the rndsrc before re-attaching it.
 1.127.2.1 06-Apr-2015  snj branches: 1.127.2.1.2; 1.127.2.1.6;
Pull up following revision(s) (requested by hannken in ticket #666):
sys/dev/sysmon/sysmon_envsys.c: revision 1.129
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.114
sys/dev/sysmon/sysmonvar.h: revision 1.46
Add a counter of busy events and stop enqueueing more work if a device =
is busy.
Protect this counter with a new short time lock "sme_work_mtx" and
keep "sme_mtx" as long time lock.
Removes a deadlock where an active event holds "sme_mtx", the callout
"sme_events_check" blocks on "sme_mtx" and callout processing stops.
 1.127.2.1.6.2 24-Oct-2017  snj Fix fallout from ticket #1511:
It's rnd_detach_source, not rnd_detach_sources.
 1.127.2.1.6.1 23-Oct-2017  snj Apply patch (requested by pgoyette in ticket #1511):
Detach the rndsrc before re-attaching it.
 1.127.2.1.2.2 24-Oct-2017  snj Fix fallout from ticket #1511:
It's rnd_detach_source, not rnd_detach_sources.
 1.127.2.1.2.1 23-Oct-2017  snj Apply patch (requested by pgoyette in ticket #1511):
Detach the rndsrc before re-attaching it.
 1.128.2.3 27-Dec-2015  skrll Sync with HEAD (as of 26th Dec)
 1.128.2.2 06-Jun-2015  skrll Sync with HEAD
 1.128.2.1 06-Apr-2015  skrll Sync with HEAD
 1.139.10.2 07-Jun-2020  martin Pull up following revision(s) (requested by riastradh in ticket #1555):

sys/dev/sysmon/sysmon_envsys.c: revision 1.145

Don't queue sysmon refresh until the rndsource is attached.

Using the rndsource, as refreshing the sensors will do, is not
allowed until _after_ rnd_source_attach.

XXX pullup-7
XXX pullup-8
XXX pullup-9
 1.139.10.1 23-Sep-2017  snj Pull up following revision(s) (requested by pgoyette in ticket #281):
sys/dev/sysmon/sysmon_envsys.c: 1.140-1.141
sys/dev/sysmon/sysmon_envsys_events.c: 1.120-1.121
sys/dev/sysmon/sysmonvar.h: 1.50
Fixes a problem that some driver(e.g. acpitz(4) or coretemp(5)) which
use sysmon_envsys sleep waiting at "rndsrc" when "drvctl -d".
Don't call rnd_detach_source() in sme_remove_event() which is called
from sme_event_unregister_all(). Instead, call rnd_detach_source() in
sysmon_envsys_sensor_detach() and call sysmon_envsys_sensor_detach()
before sme_event_unregister_sensor(). Each sensor(envsys_data) has each
rnd_src, but some sme_events point to the same rnd_src in a sensor.
Calling rnd_detach_souce() twice with the same rnd_src brokes a reference
count in rnd_src. OK'd by pgoyette@.
--
Improve tracking of the state of an event's callout, and protect all
queries or modifications of the state with the sme_mtx mutex.
Detach the rndsrc before re-attaching it (with potentially new values).
Clean up some lock-ordering issues.
And a couple of KNF issues for good measure!
Should address PR kern/52533
 1.141.2.2 25-Jun-2018  pgoyette Sync with HEAD
 1.141.2.1 21-May-2018  pgoyette Sync with HEAD
 1.143.2.1 10-Jun-2019  christos Sync with HEAD
 1.144.4.1 07-Jun-2020  martin Pull up following revision(s) (requested by riastradh in ticket #946):

sys/dev/sysmon/sysmon_envsys.c: revision 1.145

Don't queue sysmon refresh until the rndsource is attached.

Using the rndsource, as refreshing the sensors will do, is not
allowed until _after_ rnd_source_attach.

XXX pullup-7
XXX pullup-8
XXX pullup-9
 1.123 31-Dec-2021  riastradh sysmon(9): Fix callout/thread synchronization.

Callout may ONLY take sme_work_mtx, at IPL_SOFTCLOCK; MUST NOT touch
sme_mtx at IPL_NONE. All state the callout needs is serialized by
sme_work_mtx now:

- calls to sme_schedule_callout
- calls to sme_schedule_halt
- struct sysmon_envsys::sme_events_timeout
- struct sysmon_envsys::sme_events_list
- struct sysmon_envsys::sme_callout_state
- struct envsys_data::flags
=> yes, this is a little silly -- used for ENVSYS_FNEED_REFRESH
=> should maybe separate the static driver-defined features from
the state flags needed by sysmon_envsys but not important now

Sleeping under sme_work_mtx (except on other adaptive locks at
IPL_SOFTCLOCK) is forbidden. Calling out to the driver under
sme_work_mtx is forbidden.

This should properly fix:

https://mail-index.netbsd.org/tech-kern/2015/10/14/msg019511.html
PR kern/56592
 1.122 31-Dec-2021  riastradh sysmon: Delete trailing whitespace. No functional change intended.
 1.121 11-Sep-2017  pgoyette Improve tracking of the state of an event's callout, and protect all
queries or modifications of the state with the sme_mtx mutex.

Detach the rndsrc before re-attaching it (with potentially new values).

Clean up some lock-ordering issues.

And a couple of KNF issues for good measure!

Should address PR kern/52533

XXX Pullup-8 along with the previous fixes from msaitoh@
XXX http://mail-index.netbsd.org/source-changes/2017/09/06/msg088028.html
~
~
 1.120 06-Sep-2017  msaitoh Fixes a problem that some driver(e.g. acpitz(4) or coretemp(5)) which
use sysmon_envsys sleep waiting at "rndsrc" when "drvctl -d".
Don't call rnd_detach_source() in sme_remove_event() which is called
from sme_event_unregister_all(). Instead, call rnd_detach_source() in
sysmon_envsys_sensor_detach() and call sysmon_envsys_sensor_detach()
before sme_event_unregister_sensor(). Each sensor(envsys_data) has each
rnd_src, but some sme_events point to the same rnd_src in a sensor.
Calling rnd_detach_souce() twice with the same rnd_src brokes a reference
count in rnd_src. OK'd by pgoyette@.
 1.119 01-Jun-2017  chs branches: 1.119.2;
remove checks for failure after memory allocation calls that cannot fail:

kmem_alloc() with KM_SLEEP
kmem_zalloc() with KM_SLEEP
percpu_alloc()
pserialize_create()
psref_class_create()

all of these paths include an assertion that the allocation has not failed,
so callers should not assert that again.
 1.118 15-Oct-2015  bouyer As proposed in
https://mail-index.netbsd.org/tech-kern/2015/10/14/msg019511.html
don't sleep on sme->sme_mtx in the callout but use mutex_tryenter()
and just reschedule the callout if we can't get the mutex now.
This fixes a deadlock which can happen if the backed wants to
sleep with timeout (e.g. cv_timedwait()) as the backed is called with
sme->sme_mtx held.

This is a stopgap measure for netbsd-7; sysmon should be changed to not
sleep (or call a backend which will sleep) with mutexes held.
 1.117 23-Jun-2015  pgoyette Separate the two conditions for the KASSERT, so we can tell which
one occurred.
 1.116 23-Jun-2015  pgoyette Fix the KASSERT - we want to make sure that _both_ pointers are non-NULL,
n ot just that one or the other is non-NULL!
 1.115 18-Apr-2015  mlelstv use unsigned type for flag bits.
 1.114 14-Mar-2015  hannken Add a counter of busy events and stop enqueueing more work if a device is busy.
Protect this counter with a new short time lock "sme_work_mtx" and
keep "sme_mtx" as long time lock.

Removes a deadlock where an active event holds "sme_mtx", the callout
"sme_events_check" blocks on "sme_mtx" and callout processing stops.
 1.113 23-Nov-2014  ozaki-r branches: 1.113.2;
Pull workqueue_destroy out of sme->sme_mtx

workqueue_destroy may sleep so we shouldn't run it with holding a mutex.

Requested by riastradh@.
 1.112 22-Nov-2014  ozaki-r Replace callout_stop with callout_halt

In order to call callout_destroy for a callout safely, we have to ensure
the function of the callout is not running and pending. To do so, we should
use callout_halt, not callout_stop.

In this case, we need to pass an interlock to callout_halt to wait for
the callout complete. And also we make sure that SME_CALLOUT_INITIALIZED
is unset before calling callout_halt to prevent the callout from calling
callout_schedule. This is the same as what we did in sys/netinet6/mld6.c@1.61.

Reviewed by riastradh@.
 1.111 22-Nov-2014  ozaki-r Kill sme_callout_mtx and use sme_mtx instead

We can use sme_mtx for the callout as well. Actually we should do so
because sme_events_list and some other data that are touched in the
callout should be protected by sme_mtx, not sme_callout_mtx.

Discussed with riastradh@ in http://mail-index.netbsd.org/tech-kern/2014/11/11/msg017956.html
 1.110 15-Sep-2013  martin branches: 1.110.4;
Remove unused variable
 1.109 23-Jan-2013  mbalmer branches: 1.109.2;
Fix spelling, grammar, typos.
 1.108 14-Dec-2012  pgoyette Constify
 1.107 11-Dec-2012  pgoyette Replace a couple of many-line #define with equivalent code loops.

No functional change intended, and atf tests (using swsensor(4)) still
pass 100%
 1.106 31-Oct-2012  macallan in sme_battery_check():
- don't assume that all batteries have exactly one ENVSYS_INDICATOR
- check capacity and charge sensors for ENVSYS_SVALID before using them
now this works on macppc
 1.105 06-Sep-2012  pgoyette branches: 1.105.2;
Remove (commented-out) definitions for debugging - thanks to macallan@
for defflagging these.
 1.104 27-Aug-2012  pgoyette 1. Enable use of FMONSTCHANGED events for INDICATOR sensors
2. Update handling of FMONCRITICAL event reporting. The state
transition does not require a corresponding change in value.

With these changes, you can now have an INDICATOR sensor that
reports the presence or absence of a device, and (if the device
is present) separately monitor it for proper functioning.

Should address the concerns expressed recently with the commit
of changes to wmi(4) BBU handling.
 1.103 19-Jul-2012  pgoyette If previously there was only a dummy event entry (to force refresh for
entropy gathering), allow it to be updated for the current request to
add a real entry.
 1.102 18-Jul-2012  pgoyette Release the mutex before taking a quick exit.
 1.101 16-Jul-2012  pgoyette Extend previous changes so that even sensors which don't use a refresh()
callback to update the value can be polled to provide rnd(4) entropy.
 1.100 15-Jul-2012  pgoyette If a sensor is flagged as capable of providing rnd(4) with entropy,
hook the sensor into rnd subsystem, and make sure we periodically
refresh the sensor whether or not it is being actively monitored.
 1.99 15-Jul-2012  pgoyette When unregistering a sensor device, make sure we unregister and delete
all the associated events.
 1.98 08-Jun-2011  pgoyette branches: 1.98.2; 1.98.8;
Restrict limit monitoring for Indicator sensors as well as for
Battery-charge sensors.
 1.97 30-Dec-2010  pgoyette branches: 1.97.6;
When the user updates the sensor device's refresh timer, reset the
callout immediately rather than waiting for the previous timer to
expire.
 1.96 15-Dec-2010  pgoyette Extract searching of description tables into a single function, rather
than duplicating the code every time. Minor reduction in code size
(about 1200 bytes on amd64), no change in functionality.
 1.95 08-Dec-2010  pgoyette When removing properties (as in envstat -S), clear the flags that track
whether the properties actually exist.

When setting new limit values, don't complain/ignore about duplicate
values if the associated flag bits (indicating that the limit is valid)
are not set.

These two fixes together should fix a problem reported in private Email
by njoly@ a couple months ago. Issue replicated and solution tested
using my recent swsensor pseudo-device running inside qemu world!
 1.94 06-Dec-2010  pgoyette Make this compile when "options ENVSYS_OBJECTS_DEBUG" is defined.
 1.93 22-Sep-2010  pgoyette 1. Don't try to call the driver's get_limits routine if it doesn't have
one. Fixes a panic reported by njoly@ in private Email

2. Acquire the device before checking whether or not the get_limits
routine exists.

XXX There's still a bug in here somewhere that prevents setting of
XXX limit values after using 'envstat -S'
 1.92 10-Apr-2010  pgoyette When removing sensor properties with envstat -S, ensure that drivers
which could have modified hardware state are informed so that original
state can be restored.

Welcome to 5.99.27
 1.91 01-Apr-2010  pgoyette Permit creation of an event-monitor for alarm limits even if the limits
are not currently set (and, in case of battery capacity sensors, if there
is no value_max). Ensure that such an event-monitor does not trigger the
delivery of any actual events.

Provide a mechanism for sensors to set their limits at time other than
system startup (for example, when a battery is inserted).

This allows us to boot a system with a battery missing, install the
battery some time later, and automatically monitor it without requiring
any user intervention to create the event-monitor. (The actual changes
for battery sensors to use this new capability will come later.)
 1.90 24-Mar-2010  njoly Fix ENVSYS_DEBUG build.
 1.89 24-Mar-2010  pgoyette In sysmon_envsys_sensor_event(), permit delivery of all events for the
sensor if no event type is specified.
 1.88 23-Mar-2010  pgoyette Additional info in DPRINTF to help debugging efforts
 1.87 19-Mar-2010  pgoyette Modify previous commit. The routine to deliver events is intended to be
exported to device drivers, so rename it and move the declaration to
<dev/sysmon/sysmonvar.h>
 1.86 19-Mar-2010  pgoyette Separate event delivery from polling and limit evaluation in
sme_events_worker().

Provide a wrapper that can be called from sensor drivers' interrupt
routines to find and deliver a specific event.

XXX Actually updating sensor drivers (and their parent devices) to
XXX have interrupts is a future enhancement. This is simply an
XXX enabler.
 1.85 18-Feb-2010  pgoyette branches: 1.85.2;
If we're going to valdate things, at least do it correctly.
 1.84 15-Feb-2010  pgoyette For symmetry, allow warning and critical upper-limits for capacity
sensors.
 1.83 14-Feb-2010  pgoyette Replace large amount of replicated code with a macro to improve readability.
 1.82 14-Feb-2010  pgoyette Remove the flags member of the limits structure. Its value was only
meaningful for a few limited function calls; all persitent storage
of limit-present flags is maintained in the edata->upropset member.
 1.81 14-Feb-2010  pgoyette Correct one more instance of checking flags in the wrong place.
 1.80 13-Feb-2010  pgoyette When checking current sensor value vs limits, check the correct set of
flags to determine which limits are present.
 1.79 05-Feb-2010  jruoho branches: 1.79.2;
Fix a typo in one DPRINTF (s/edata->des/edata->desc/).
 1.78 04-Feb-2010  pgoyette If the AC adapter is flagged with SME_DISABLE_REFRESH, don't try to call
its refresh() callback. Just assume that its value has been properly
updated by other means.
 1.77 30-Jan-2010  pgoyette Validate limit-based monitoring in one place, so we can apply the
constraints to both driver-requested and user-requested monitoring.

While here, clean up some of the debugging DPRINTFs.
 1.76 26-Jan-2010  pgoyette Move the limits out of the event structure and into the main sensor.

Use the limits_flags only for calls to the {get,set}_limits() callbacks
and merge the results into upropset, rather than trying to maintain both
values (error prone).
 1.75 18-Jan-2010  pgoyette If no limits are exceeded, make sure we set state to SVALID
 1.74 02-Jan-2010  pgoyette Flags for existence of limit values need to be propagated to the
event data. Otherwise, they're not processed.

XXX This needs to be rethought. While fixing it this way makes things
XXX "work", we really shouldn't need to replicate these flags. The
XXX limits and their flags should be kept as part of the sensor data,
XXX and not part of the event-monitor data. I'll work on this in the
XXX near future.
 1.73 01-Jan-2010  pgoyette Don't infer a sensor driver's ability to handle limit checking itself.
 1.72 23-Dec-2009  pgoyette Check for value exceeding crit-max limit before checking against warn-max
limit. Otherwise we'll never notice if we exceed crit-max (assuming that
crit-max is at least as large as warn-max).
 1.71 10-Jul-2009  pgoyette Document usage of PROP_DRIVER_LIMITS flag and set it correctly.

Use flag bits to determine validity of limit values, rather than
assuming that invalid/not-present values are set to zero.
 1.70 08-Jul-2009  pgoyette Initialize lims.sel_flags before using it. Prevents spurious limit
events being created for devices that don't provide internal limits.

Thanks to Mattias for finding this.
 1.69 08-Jul-2009  pgoyette Clean up a couple of debug statements that got missed previously.
 1.68 14-Jun-2009  pgoyette Ooops - {get,set}_limits() need to be sensor-specific. Pass an extra
argument to them to identify the individual sensor being processed.
 1.67 13-Jun-2009  pgoyette Add capability to send sensor limit values to the driver so they can
be programmed into device registers. This way we can let the hardware
help us out instead of having to always compare the value against each
limit. (Driver updates for some sensors to take advantage of this
capability will be forthcoming.)
 1.66 08-Jun-2009  pgoyette Correct typo in last so we can #undef the correct macro.
 1.65 08-Jun-2009  pgoyette General clean-up and some restructuring of event handling. This is a
precursor to letting sensor drivers actually exchange limits/thresholds
with user-land.
 1.64 01-Jun-2009  pgoyette Since we no longer have individual events for each sensor value limit,
we don't need individual flag bits. Clean up extra bit definitions.
Bump kernel version - welcome to 5.99.13
 1.63 07-Apr-2009  dyoung Fix spelling: mak -> make.
 1.62 11-Nov-2008  pgoyette branches: 1.62.4;
Fix typo in previous, and wrap a panic with #ifdef DIAGNOSTIC
 1.61 11-Nov-2008  pgoyette Initialize event's "previous sent state" based on the event monitor type,
regardless of sensor type. Fixes spurious "device: normal state on
'sensor-name' messages (reported by ad@) which interfere with sysinst.
 1.60 04-Nov-2008  pgoyette Fix cut&paste error in prev
 1.59 04-Nov-2008  pgoyette Restore previous behavior concerning CRITICAL events. Send an event
whenever state enters or exits CRITICAL, or whenever value of sensor
changes while the state is CRITICAL.
 1.58 07-Sep-2008  pgoyette branches: 1.58.2; 1.58.4;
Only report new sensor state values if the new value is valid for the
particular event being monitored. This avoids duplicate reporting for
sensors that monitor both a normal/critical state and value-within-limits
state as reported by Simon Burge on current-users.

While we're here, clean-up some stair-stepping if's.

OK garbled@
 1.57 04-Sep-2008  pgoyette Initialize event's sent-state appropriately to avoid unnecessary start-up
time messages that report a "transition" to the Normal state.
 1.56 22-Aug-2008  pgoyette 1. provide for warning-min and warning-max alarm levels in addition to
critical-min and critical-max; also, a battery warning-capacity is
added in addition to a critical-capacity.

2. usr.sbin/envstat is modified to introduce a -W command line switch to
display the warning-* values instead of the critical-* values, and
envstat(8) and envsys.conf(5) man pages are updated appropriately.

3. Treat user-defined limits as a single continuum and generate a single
event regardless of how many boundaries a change in sensor value
crosses; ditto for driver-defined limits.

Fixes my PR/39021
Fixes my PR/39022

OK'd by christos@ bouyer@ cube@
 1.55 11-Jun-2008  drochner branches: 1.55.2;
in sme_battery_check(), explicitely check whether the battery is
present before using the values read, and clean up the logics
a bit to make it more understandable
 1.54 02-Apr-2008  xtraeme branches: 1.54.2; 1.54.4; 1.54.6; 1.54.8;
Initialize and destroy the callout in sme_events_init() and
sme_events_destroy() respectively. And don't stop twice the callout
when calling sysmon_envsys_unregister().
 1.53 01-Apr-2008  xtraeme Also fix a memleak when the event is already in dictionary
and has been updated.
 1.52 01-Apr-2008  rmind sme_event_register: fix a memory leaks in two error paths.
 1.51 01-Apr-2008  xtraeme Introduce per-device locking/synchronization and maintain only a
global mutex for the linked list of devices and the global proplib
dictionary.

This has improved locking contention a lot when multiple devices with
multiple monitoring events are running:

New:

0.30 35 0.33 sme_global_mtx sysmonioctl_envsys+28b
0.10 10 0.11 00000000cd97feac sysmon_envsys_acquire+4c
0.08 6 0.09 00000000cd97feac sme_update_dictionary+24f
0.01 4 0.01 00000000cd97feac sme_events_worker+2f
0.01 10 0.01 00000000cd97fe2c sysmon_envsys_acquire+4c
0.00 1 0.00 00000000cd97fe2c sysmon_envsys_release+3b
28.38 94 9.16 sme_global_mtx sysmonioctl_envsys+28b
4.54 74 1.47 00000000cd97fe2c sysmon_envsys_acquire+4c
0.06 3 0.02 00000000cd97fe2c sysmon_envsys_release+3b
0.03 1 0.01 00000000cd97fe2c sme_events_worker+2f
1.40 19 0.45 00000000cd97bee4 sysmon_envsys_acquire+4c

Old:

4.25 313 4.74 sme_mtx <all>
3.12 185 3.49 sme_mtx sme_events_worker+21
1.12 128 1.25 sme_mtx sysmonioctl_envsys+29b
34.75 1423 59.52 sme_mtx <all>
22.08 477 37.82 sme_mtx sysmonioctl_envsys+29b
12.67 946 21.70 sme_mtx sme_events_worker+21
 1.50 23-Mar-2008  xtraeme Fix some problems reported by <drochner> a while ago on tech-kern:

sme_events_worker: skip sensors with invalid state.
sme_acadapter_check:

* use a boolean to check if an SME_CLASS_ACADAPTER was found.
* Always refresh sensor data before checking value_cur.

Introduce the SME_INIT_REFRESH flag. If a device sets this it will mean
that after interrupts are enabled in the autoconf(9) process, sensor
data will be refreshed once.
 1.49 29-Feb-2008  xtraeme Only check sysmon_low_power if the event comes from
ENVSYS_BATTERY_CAPACITY sensors.
 1.48 02-Feb-2008  xtraeme branches: 1.48.2; 1.48.6;
Destroy the callout via sysmon_envsys_destroy() now that it's
initialized in sysmon_envsys_create().
 1.47 02-Jan-2008  xtraeme - Initialize the callout with CALLOUT_MPSAFE, like it was done before
in sysmon_envsys_events.c.
- Separate sme_event_check_low_power() into sme_battery_check(),
sme_acadapter_check() and sme_battery_critical() (no functional changes).
 1.46 20-Nov-2007  xtraeme branches: 1.46.6;
After comments from Andrew Doran:

- The mutex for the callout handler must run at IPL_SOFTCLOCK.
- Just stop the callout in sysmon_envsys_unregister() and don't wait
for the callout to finish.
 1.45 16-Nov-2007  xtraeme Extend the envsys2 API (one more time, sorry) as defined in:

http://mail-index.netbsd.org/tech-kern/2007/11/09/0001.html

sysmon_envsys_create() and sysmon_envsys_destroy() were added to
create/destroy sysmon_envsys objects (and its TAILQ/LIST for sensors/events).

sysmon_envsys_sensor_attach() and sysmon_envsys_sensor_detach() were
added to attach/detach sensors to a specified sysmon_envsys device.

The events framework is now per device and configurable via the
ENVSYS_SETDICTIONARY ioctl or /etc/envsys.conf and envstat(8).

Update all users and documentation to reflect these changes.
 1.44 03-Nov-2007  xtraeme branches: 1.44.2;
- Rename ENVSYS_BATTERY_STATE units to ENVSYS_BATTERY_CAPACITY and
introduce ENVSYS_BATTERY_CHARGE, which is the same than an Indicator and
it's used to know if the battery is currently charging or discharging.

- Require two sensors at least for SME_CLASS_BATTERY to make the low-power
condition work: a ENVSYS_BATTERY_CAPACITY plus ENVSYS_BATTERY_CHARGE.

- Simplify sme_event_check_lowpower() and make it check the required
sensors in the SME_CLASS_ACADAPTER and SME_CLASS_BATTERY classes.
If the acadapter is not ready, trust the state returned by the battery
device.

Based on suggestion from joerg@.
 1.43 23-Oct-2007  xtraeme Use a flag for see_flags to know when the sensor has been refreshed
rather than using a boolean. Suggested by rmind.
 1.42 20-Oct-2007  xtraeme performance fix: in the workqueue handler only refresh the sensor once,
not multiple times for the same sensor and clear its state after all
events were processed.
 1.41 13-Oct-2007  xtraeme branches: 1.41.2;
sme_event_drvadd: there's no need to acquire the mutex only when
setting the object on its dictionary.
 1.40 13-Oct-2007  xtraeme Use PRI_NONE for the priority argument in workqueue_create(9).
 1.39 11-Oct-2007  xtraeme Check for the low power condition only when a new battery state sensor
changed its state, there's no need to check this in all cases.
 1.38 10-Oct-2007  xtraeme Fix the problems described in:
http://mail-index.netbsd.org/current-users/2007/07/16/0012.html

- Introduce sme_class into the sysmon_envsys struct to specify a
class; currently there are two classes: SME_CLASS_ACADAPTER and
SME_CLASS_BATTERY.

- Add a new envsys event: PENVSYS_EVENT_LOW_POWER that is reached when
all SME_CLASS_BATTERY devices are in CRITICAL/LOW state and there's not
any SME_CLASS_ACADAPTER connected.

- Add the 'low-power' event into the sensor_battery script that will
shutdown the system gracefully via 'shutdown -p'. If powerd(8) is
not running, cpu_reboot(9) with RB_POWERDOWN is used.

- Make acpiacad(4) a SME_CLASS_ACADAPTER device and acpibat(4) a
SME_CLASS_BATTERY device.

Update the documentation accordingly to these changes.
 1.37 07-Oct-2007  xtraeme sme_event_unregister: remove a goto that is not needed anymore.
 1.36 07-Oct-2007  xtraeme New changes to support the new configuration file format for
envstat(8) and the envsys framework:

- Modify the ENVSYS_SETDICTIONARY ioctl to support the following
plist structure:

<dict>
<key>foo0</key>
<array>
<dict>
<key>index</key>
<string>sensor0</string>
<key>description</key>
<string>cpu temp</string>
...
Another property for this sensor
...
</dict>
...
Another dictionary for other sensor
...
</array>
...
Another device as above
...
</dict>

Multiple devices with multiple sensors can now be specified, that means
that to set the properties only one copyin(9) is needed now.

- Added the ENVSYS_REMOVEPROPS ioctl, that accepts a boolean object
"envsys-remove-props" and when set to true, all properties that were
set previously by ENVSYS_SETDICTIONARY will be removed. That means that
you can now set multiple critical limits, descriptions and all they
will be removed or changed to its default value (for rfact and
description objects).

- Added the 'index' and 'allow-rfact' objects into the sensor dictionaries,
for better interactivity with userland. To know the position of the
sensor and to know if sensor allows to change the rfact.

- Misc cosmetic changes for consistency.

- Use a two clause license for all my code.
 1.35 08-Sep-2007  xtraeme branches: 1.35.2;
Use panic(9) for previous. To make pooka and rmind happy.
 1.34 08-Sep-2007  xtraeme sme_event_worker: in PENVSYS_EVENT_STATE_CHANGED, only
ENVSYS_DRIVE or ENVSYS_BATTERY_STATE sensors are allowed for now.

Other type of sensors will be ignored. Added this to make
Coverity Prevent happy.
 1.33 08-Sep-2007  xtraeme Use only one single condvar(9) for devices and events, they are protected
by the same mutex (sme_mtx) and there's no point in using two of them.

Also add a comment mentioning some locking notes.

Reviewed and ok by rmind.
 1.32 08-Sep-2007  xtraeme Disable debug, committed accidentally in previous.
 1.31 08-Sep-2007  xtraeme - Use only one global mutex for all tasks with devices & events.
- sysmon_envsys_register: use a SLIST to maintain allocated sme_event_drv_t
structs for later use in sysmon_task_queue_sched(). This avoids a
locking error acquiring/dropping the mutex multiple times.

Suggested by rmind.
 1.30 08-Sep-2007  xtraeme Fix previous.
 1.29 07-Sep-2007  xtraeme - sme_event_unregister_all: check if sme_events_initialized is set
before calling sme_events_destroy().
- sme_event_worker: remove a KASSERT and always check if returned sme
is NULL and do not continue the task.
 1.28 04-Sep-2007  xtraeme - Remove ENVSYS_GSTRING and the genstr member from envsys_data_t.
(at least three or four persons were against it).
- Add a new sensor type: ENVSYS_BATTERY_STATE, this uses value_cur
and some predefined values in a static table, like ENVSYS_DRIVE.
- Move all static tables to sysmon_envsys_tables.c and use a function
on it to retrieve a pointer to the struct of the specified type.
- Rename the ENVSYS_FMONDRVSTCHANGED to ENVSYS_FMONSTCHANGED and make
it generic for Battery state and drive sensors (this flag enables
monitoring on these sensors when state has been changed).
- Update sysmon_penvsys_event() to report state changes on
ENVSYS_BATTERY_STATE sensors and remove other type of events, with
PENVSYS_EVENT_STATE_CHANGED they are not necessary anymore.
 1.27 01-Sep-2007  xtraeme branches: 1.27.2;
sme_event_register: don't forget to add the object in dictionary when
a critical value is added via ENVSYS_SETDICTIONARY.
 1.26 01-Sep-2007  xtraeme sme_event_register: sme_sensor_upint32() was being called even when
adding events via sme_event_drvadd(), fix it.
 1.25 31-Aug-2007  xtraeme - sme_unregister_all: there's no need to use a struct sysmon_envsys as
argument, just pass sme->sme_name to it.
- sysmon_envsys_register: drop the array in all cases, not just when
there's an error. The reference is stored in the dictionary anyway...
- Update some comments.
 1.24 31-Aug-2007  xtraeme Merge sme_event_add() into sme_event_register()... there's no need
to have two different functions for this.
 1.23 31-Aug-2007  xtraeme Simplify sme_event_add() and fix a problem when updating critical
values.
 1.22 31-Aug-2007  xtraeme Add a few KASSERT(mutex_owned(&foo_lock)) as suggested by Andrew Doran.
 1.21 30-Aug-2007  xtraeme More misc changes for sysmon_envsys(9):

- Add the SLIST for sensor descriptions and sme_uniqsensors into the
struct sysmon_envsys (it's per device now).
- Use only one common struct with three members for the static tables
(there's no need to have different structs just for them).
- While initializing/destroying the events framework, use the
strategy specified by Andrew Doran in:
http://mail-index.netbsd.org/tech-kern/2007/06/21/0025.html).
 1.20 30-Aug-2007  xtraeme Some changes to improve locking on sysmon_envsys(9):

- Remove sme_mtx, a global lock (sme_list_mtx) is used to access to
the sysmon envsys device.
- Allocate memory with KM_NOSLEEP rather than KM_SLEEP if there's a
mutex held, to avoid sleeping.
- Remove sysmon_envsys_createplist() and add the logic into
sysmon_envsys_register().
- sysmon_envsys_register: allocate the array and dictionaries required
in advance for a device before the locking and adding the objects
into the array happens.
- Rename sme_make_dictionary() to sme_add_sensor_dictionary() and pass
to it the dictionary on which the objects will be stored for a sensor.
- Improve locking here and there.

Thanks to Mindaugas Rasiukevicius and Andrew Doran for comments.
 1.19 23-Jul-2007  xtraeme branches: 1.19.4; 1.19.6; 1.19.8;
- ENVSYS_SETDICTIONARY: use sysmon_envsys_release() if there's an error.
- sme_events_worker: use sme_list_mtx when accessing to the sysmon_envsys
linked list.
- Improve the comments in sysmon_envsysvar.h about the mutexes and condvar.
 1.18 21-Jul-2007  xtraeme - Add sme_events_destroy() to stop/destroy the callout and workqueue if
there are no more events.
- Add sme_event_unregister_all() to remove all events associated with
a device and use it in sysmon_envsys_unregister() to fix a "use-after-free".
- Check return error of sme_sensor_upfoo() in sme_make_dictionary() and
return it rather than 0 all the time.
- Add more KASSERTS.
 1.17 21-Jul-2007  xtraeme Some fixes for kern/36673 by yamt@:

- sme_register_sensorname: there's no need to handle empty list case.
- 'obj' argument in sme_sensor_upstring and similar functions is useless:
removed the obj argument from the functions, it's handled in the function
itself now.
- check for more errors in sme_make_dictionary() and
sysmon_envsys_createplist().
 1.16 20-Jul-2007  xtraeme Replace the macros to create/update objects in dictionary with three
inline functions, suggested by yamt@.

This also gets rid of a few gotos.
 1.15 18-Jul-2007  xtraeme - More changes related to locking.
- Add a condvar(9) to protect deletions on the list if there's an event
in the worker thread.
 1.14 17-Jul-2007  xtraeme branches: 1.14.2;
Add more debugging printfs to catch sensors with duplicate description,
and some cosmetic ones.
 1.13 14-Jul-2007  xtraeme branches: 1.13.2;
sme_events_worker: there's no need to use an extra int that gets
the value of see->snum to get the index of sme_sensor_data, just use
see->snum directly.
 1.12 13-Jul-2007  xtraeme sme_event_add: if the event is already on the list, update the
critical value only if new value is different than the one it has.

While I'm here, improve the debugging printf for this.
 1.11 13-Jul-2007  xtraeme sme_event_add: if sme_event_register() fails return the error of this
function and not EINVAL.
 1.10 13-Jul-2007  xtraeme sme_events_worker: simplify the code that sends the events.
 1.9 13-Jul-2007  xtraeme sme_event_unregister: if the events list is empty, just after the
callout is stopped, use callout_destroy().
 1.8 12-Jul-2007  rmind Implementation of per-CPU work-queues support for workqueue(9) interface.
WQ_PERCPU flag for workqueue and additional argument for workqueue_enqueue()
to assign a CPU might be used. Notes:
- For now, the list is used for workqueue_queue, which is non-optimal,
and will be changed with array, where index would be CPU ID.
- The data structures should be changed to be cache-friendly.

Reviewed by: <yamt>, <tech-kern>
 1.7 10-Jul-2007  yamt branches: 1.7.2;
sme_events_init: create seewq with IPL_SOFTCLOCK as it's accessed by a callout.
 1.6 09-Jul-2007  ad Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.5 04-Jul-2007  xtraeme Remove the max limit in the refresh value for the refresh_value sysctl,
use whatever you want within the limits of an integer.

Suggested by garbled@ and freza@.
 1.4 03-Jul-2007  xtraeme Erm, fix previous: 6min != 5min...
 1.3 03-Jul-2007  xtraeme Make the refresh value of the callout function (that monitors the sensors)
configurable via sysctl. By default it uses 10 seconds, but it can be changed
between 1 second and 5 minutes.

$ sysctl -d kern.envsys
kern.envsys.refresh_value: wait time in seconds to refresh sensors being monitored
$
 1.2 02-Jul-2007  xtraeme sme_event_drvadd:

- Check the error returned by sme_event_add() and print details if
returned value is not EEXIST.
- If sme_event_add() returned an error, don't add the monitoring-* object
into the dictionary.
 1.1 01-Jul-2007  xtraeme Imported envsys 2, a brief description of the new features:
(Part 1: API)

* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.

Tested by:

blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
 1.7.2.2 11-Jul-2007  mjf Sync with head.
 1.7.2.1 10-Jul-2007  mjf file sysmon_envsys_events.c was added on branch mjf-ufs-trans on 2007-07-11 20:08:24 +0000
 1.13.2.6 23-Oct-2007  ad Sync with head.
 1.13.2.5 12-Oct-2007  ad Sync with head.
 1.13.2.4 09-Oct-2007  ad Sync with head.
 1.13.2.3 20-Aug-2007  ad Sync with HEAD.
 1.13.2.2 15-Jul-2007  ad Sync with head.
 1.13.2.1 14-Jul-2007  ad file sysmon_envsys_events.c was added on branch vmlocking on 2007-07-15 13:21:45 +0000
 1.14.2.3 10-Sep-2007  skrll Sync with HEAD.
 1.14.2.2 03-Sep-2007  skrll Sync with HEAD.
 1.14.2.1 15-Aug-2007  skrll Sync with HEAD.
 1.19.8.2 23-Jul-2007  xtraeme - ENVSYS_SETDICTIONARY: use sysmon_envsys_release() if there's an error.
- sme_events_worker: use sme_list_mtx when accessing to the sysmon_envsys
linked list.
- Improve the comments in sysmon_envsysvar.h about the mutexes and condvar.
 1.19.8.1 23-Jul-2007  xtraeme file sysmon_envsys_events.c was added on branch matt-mips64 on 2007-07-23 17:51:18 +0000
 1.19.6.3 23-Mar-2008  matt sync with HEAD
 1.19.6.2 09-Jan-2008  matt sync with HEAD
 1.19.6.1 06-Nov-2007  matt sync with HEAD
 1.19.4.6 21-Nov-2007  joerg Sync with HEAD.
 1.19.4.5 04-Nov-2007  jmcneill Sync with HEAD.
 1.19.4.4 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.19.4.3 07-Oct-2007  joerg Sync with HEAD.
 1.19.4.2 02-Oct-2007  joerg Sync with HEAD.
 1.19.4.1 03-Sep-2007  jmcneill Sync with HEAD.
 1.27.2.9 24-Mar-2008  yamt sync with head.
 1.27.2.8 17-Mar-2008  yamt sync with head.
 1.27.2.7 04-Feb-2008  yamt sync with head.
 1.27.2.6 21-Jan-2008  yamt sync with head
 1.27.2.5 07-Dec-2007  yamt sync with head
 1.27.2.4 15-Nov-2007  yamt sync with head.
 1.27.2.3 27-Oct-2007  yamt sync with head.
 1.27.2.2 03-Sep-2007  yamt sync with head.
 1.27.2.1 01-Sep-2007  yamt file sysmon_envsys_events.c was added on branch yamt-lazymbuf on 2007-09-03 14:38:50 +0000
 1.35.2.1 14-Oct-2007  yamt sync with head.
 1.41.2.4 21-Nov-2007  bouyer Sync with HEAD
 1.41.2.3 18-Nov-2007  bouyer Sync with HEAD
 1.41.2.2 13-Nov-2007  bouyer Sync with HEAD
 1.41.2.1 25-Oct-2007  bouyer Sync with HEAD.
 1.44.2.3 18-Feb-2008  mjf Sync with HEAD.
 1.44.2.2 08-Dec-2007  mjf Sync with HEAD.
 1.44.2.1 19-Nov-2007  mjf Sync with HEAD.
 1.46.6.1 02-Jan-2008  bouyer Sync with HEAD
 1.48.6.4 17-Jan-2009  mjf Sync with HEAD.
 1.48.6.3 28-Sep-2008  mjf Sync with HEAD.
 1.48.6.2 29-Jun-2008  mjf Sync with HEAD.
 1.48.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.48.2.1 24-Mar-2008  keiichi sync with head.
 1.54.8.1 18-Jun-2008  simonb Sync with head.
 1.54.6.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.54.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.54.4.6 09-Oct-2010  yamt sync with head
 1.54.4.5 11-Aug-2010  yamt sync with head.
 1.54.4.4 11-Mar-2010  yamt sync with head
 1.54.4.3 18-Jul-2009  yamt sync with head.
 1.54.4.2 20-Jun-2009  yamt sync with head
 1.54.4.1 04-May-2009  yamt sync with head.
 1.54.2.1 17-Jun-2008  yamt sync with head.
 1.55.2.2 13-Dec-2008  haad Update haad-dm branch to haad-dm-base2.
 1.55.2.1 19-Oct-2008  haad Sync with HEAD.
 1.58.4.5 07-Jan-2011  riz Pull up following revision(s) (requested by pgoyette in ticket #1505):
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.94
Make this compile when "options ENVSYS_OBJECTS_DEBUG" is defined.
 1.58.4.4 13-Nov-2008  snj Pull up following revision(s) (requested by tls in ticket #49):
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.62
Fix typo in previous, and wrap a panic with #ifdef DIAGNOSTIC
 1.58.4.3 13-Nov-2008  snj Pull up following revision(s) (requested by tls in ticket #49):
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.61
Initialize event's "previous sent state" based on the event monitor type,
regardless of sensor type. Fixes spurious "device: normal state on
'sensor-name' messages (reported by ad@) which interfere with sysinst.
 1.58.4.2 06-Nov-2008  snj Pull up following revision(s) (requested by pgoyette in ticket #15):
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.60
Fix cut&paste error in prev
 1.58.4.1 06-Nov-2008  snj Pull up following revision(s) (requested by pgoyette in ticket #15):
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.59
Restore previous behavior concerning CRITICAL events. Send an event
whenever state enters or exits CRITICAL, or whenever value of sensor
changes while the state is CRITICAL.
 1.58.2.2 28-Apr-2009  skrll Sync with HEAD.
 1.58.2.1 19-Jan-2009  skrll Sync with HEAD.
 1.62.4.2 23-Jul-2009  jym Sync with HEAD.
 1.62.4.1 13-May-2009  jym Sync with HEAD.

Commit is split, to avoid a "too many arguments" protocol error.
 1.79.2.2 22-Oct-2010  uebayasi Sync with HEAD (-D20101022).
 1.79.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.85.2.3 12-Jun-2011  rmind sync with head
 1.85.2.2 05-Mar-2011  rmind sync with head
 1.85.2.1 30-May-2010  rmind sync with head
 1.97.6.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.98.8.4 07-Dec-2014  martin Pull up following revision(s) (requested by ozaki-r in ticket #1201):
sys/kern/kern_ktrace.c: revision 1.166
sys/dev/isa/aps.c: revision 1.16
sys/dev/sysmon/sysmonvar.h: revision 1.45
sys/dev/ir/irframe_tty.c: revision 1.60
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.111-1.112 (patch)
sys/dev/pci/pccbb.c: revision 1.207
sys/dev/wscons/wskbd.c: revision 1.135
sys/dev/usb/ohci.c: revision 1.254
sys/net/if_ecosubr.c: revision 1.41
sys/dev/pcmcia/btbc.c: revision 1.17
sys/arch/x86/x86/via_padlock.c: revision 1.23
sys/dev/sdmmc/sdmmc.c: revision 1.23 (patch)
sys/dev/bluetooth/btkbd.c: revision 1.17
sys/dev/bluetooth/bcsp.c: revision 1.25
sys/arch/x86/pci/fwhrng.c: revision 1.8
sys/dev/ic/nslm7x.c: revision 1.61
share/man/man9/callout.9: revision 1.28 (patch)

Replace callout_stop with callout_halt and ensure the callout
is not running before destroying it.
 1.98.8.3 19-Oct-2012  riz Remove stray CR characters introduced in ticket #544. From pgoyette.
 1.98.8.2 17-Oct-2012  riz Pull up following revision(s) (requested by pgoyette in ticket #545):
etc/powerd/scripts/sensor_indicator: revision 1.5
sys/dev/sysmon/sysmon_envsys_tables.c: revision 1.11
sys/sys/envsys.h: revision 1.32
sys/dev/sysmon/sysmon_envsysvar.h: revision 1.43
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.104
Provide names for the values associated with INDICATOR sensors.
1. Enable use of FMONSTCHANGED events for INDICATOR sensors
2. Update handling of FMONCRITICAL event reporting. The state
transition does not require a corresponding change in value.
With these changes, you can now have an INDICATOR sensor that
reports the presence or absence of a device, and (if the device
is present) separately monitor it for proper functioning.
Should address the concerns expressed recently with the commit
of changes to wmi(4) BBU handling.
Handle any reports of state changes for INDICATORs
 1.98.8.1 17-Oct-2012  riz Pull up following revision(s) (requested by pgoyette in ticket #544):
sys/dev/sysmon/sysmon_envsysvar.h: revision 1.42
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.99
sys/dev/sysmon/sysmon_envsys.c: revision 1.119
When unregistering a sensor device, make sure we unregister and delete
all the associated events.
 1.98.2.4 22-May-2014  yamt sync with head.

for a reference, the tree before this commit was tagged
as yamt-pagecache-tag8.

this commit was splitted into small chunks to avoid
a limitation of cvs. ("Protocol error: too many arguments")
 1.98.2.3 23-Jan-2013  yamt sync with head
 1.98.2.2 16-Jan-2013  yamt sync with (a bit old) head
 1.98.2.1 30-Oct-2012  yamt sync with head
 1.105.2.4 03-Dec-2017  jdolecek update from HEAD
 1.105.2.3 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.105.2.2 25-Feb-2013  tls resync with head
 1.105.2.1 20-Nov-2012  tls Resync to 2012-11-19 00:00:00 UTC
 1.109.2.1 18-May-2014  rmind sync with head
 1.110.4.2 06-Apr-2015  snj Pull up following revision(s) (requested by hannken in ticket #666):
sys/dev/sysmon/sysmon_envsys.c: revision 1.129
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.114
sys/dev/sysmon/sysmonvar.h: revision 1.46
Add a counter of busy events and stop enqueueing more work if a device =
is busy.
Protect this counter with a new short time lock "sme_work_mtx" and
keep "sme_mtx" as long time lock.
Removes a deadlock where an active event holds "sme_mtx", the callout
"sme_events_check" blocks on "sme_mtx" and callout processing stops.
 1.110.4.1 01-Dec-2014  martin Pull up following revision(s) (requested by ozaki-r in ticket #279):
sys/kern/kern_ktrace.c: revision 1.166
sys/dev/isa/aps.c: revision 1.16
sys/dev/sysmon/sysmonvar.h: revision 1.45
sys/dev/ir/irframe_tty.c: revision 1.60
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.111
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.112
sys/dev/pci/pccbb.c: revision 1.207
sys/dev/wscons/wskbd.c: revision 1.135
sys/dev/usb/ohci.c: revision 1.254
sys/net/if_ecosubr.c: revision 1.41
sys/dev/pcmcia/btbc.c: revision 1.17
sys/arch/x86/x86/via_padlock.c: revision 1.23
sys/dev/sdmmc/sdmmc.c: revision 1.23
sys/dev/bluetooth/btkbd.c: revision 1.17
sys/dev/bluetooth/bcsp.c: revision 1.25
sys/arch/x86/pci/fwhrng.c: revision 1.8
sys/dev/ic/nslm7x.c: revision 1.61
share/man/man9/callout.9: revision 1.28
Replace callout_stop with callout_halt
In order to call callout_destroy for a callout safely, we have to ensure
the function of the callout is not running and pending. To do so, we should
use callout_halt, not callout_stop.
Discussed with martin@ and riastradh@.
Make it clear that we should use not callout_stop but callout_halt
before callout_destroy
Replace callout_stop with callout_halt
In order to call callout_destroy for a callout safely, we have to ensure
the function of the callout is not running and pending. To do so, we should
use callout_halt, not callout_stop.
In this case, we need to pass an interlock to callout_halt to wait for
the callout complete.
Reviewed by riastradh@.
Kill sme_callout_mtx and use sme_mtx instead
We can use sme_mtx for the callout as well. Actually we should do so
because sme_events_list and some other data that are touched in the
callout should be protected by sme_mtx, not sme_callout_mtx.
Discussed with riastradh@ in
http://mail-index.netbsd.org/tech-kern/2014/11/11/msg017956.html
Replace callout_stop with callout_halt
In order to call callout_destroy for a callout safely, we have to ensure
the function of the callout is not running and pending. To do so, we should
use callout_halt, not callout_stop.
In this case, we need to pass an interlock to callout_halt to wait for
the callout complete. And also we make sure that SME_CALLOUT_INITIALIZED
is unset before calling callout_halt to prevent the callout from calling
callout_schedule. This is the same as what we did in sys/netinet6/mld6.c@1.61.
Reviewed by riastradh@.
 1.113.2.5 28-Aug-2017  skrll Sync with HEAD
 1.113.2.4 27-Dec-2015  skrll Sync with HEAD (as of 26th Dec)
 1.113.2.3 22-Sep-2015  skrll Sync with HEAD
 1.113.2.2 06-Jun-2015  skrll Sync with HEAD
 1.113.2.1 06-Apr-2015  skrll Sync with HEAD
 1.119.2.1 23-Sep-2017  snj Pull up following revision(s) (requested by pgoyette in ticket #281):
sys/dev/sysmon/sysmon_envsys.c: 1.140-1.141
sys/dev/sysmon/sysmon_envsys_events.c: 1.120-1.121
sys/dev/sysmon/sysmonvar.h: 1.50
Fixes a problem that some driver(e.g. acpitz(4) or coretemp(5)) which
use sysmon_envsys sleep waiting at "rndsrc" when "drvctl -d".
Don't call rnd_detach_source() in sme_remove_event() which is called
from sme_event_unregister_all(). Instead, call rnd_detach_source() in
sysmon_envsys_sensor_detach() and call sysmon_envsys_sensor_detach()
before sme_event_unregister_sensor(). Each sensor(envsys_data) has each
rnd_src, but some sme_events point to the same rnd_src in a sensor.
Calling rnd_detach_souce() twice with the same rnd_src brokes a reference
count in rnd_src. OK'd by pgoyette@.
--
Improve tracking of the state of an event's callout, and protect all
queries or modifications of the state with the sme_mtx mutex.
Detach the rndsrc before re-attaching it (with potentially new values).
Clean up some lock-ordering issues.
And a couple of KNF issues for good measure!
Should address PR kern/52533
 1.14 21-Nov-2022  brad A driver for the Bosch BMP280 / BME280 temperature, humidity and
atmospheric pressure sensor. This is an inexpensive to moderately
expensive chip available from a large number of places. The driver
supports all aspects of the two chips, except for the repeating read
mode which would allow for sub-second queries, such as fall detection
or perhaps even as an altimeter. This driver also only supports the
I2C interface and not the SPI interface.

The BME280, the one with humidity, is not fully tested at this point,
awaiting upon a breakout board and may not show proper humidity.
 1.13 27-May-2018  thorpej Add support for light sensors that report Illuminance in lux.
 1.12 18-May-2014  kardel branches: 1.12.26;
add HYT-221/271/939 humidity/temperature I2C sensor
extend envsys(4) framework by %rH (relative humidity)
 1.11 27-Aug-2012  pgoyette branches: 1.11.2; 1.11.4; 1.11.12;
1. Enable use of FMONSTCHANGED events for INDICATOR sensors
2. Update handling of FMONCRITICAL event reporting. The state
transition does not require a corresponding change in value.

With these changes, you can now have an INDICATOR sensor that
reports the presence or absence of a device, and (if the device
is present) separately monitor it for proper functioning.

Should address the concerns expressed recently with the commit
of changes to wmi(4) BBU handling.
 1.10 19-Jun-2011  nonaka branches: 1.10.2; 1.10.8;
Pass table_id to sme_find_table().
 1.9 19-Jun-2011  nonaka fix typo.
 1.8 19-Jun-2011  christos simplify and don't deref NULL.
 1.7 19-Jun-2011  pgoyette Add routine to lookup description tables by description instead of by
type.
 1.6 15-Dec-2010  pgoyette branches: 1.6.6;
Extract searching of description tables into a single function, rather
than duplicating the code every time. Minor reduction in code size
(about 1200 bytes on amd64), no change in functionality.
 1.5 28-Feb-2008  xtraeme branches: 1.5.26;
Add three new values for ENVSYS_DRIVE units:

* ENVSYS_DRIVE_OFFLINE, ENVSYS_DRIVE_CHECK and ENVSYS_DRIVE_BUILD
 1.4 07-Dec-2007  xtraeme branches: 1.4.8; 1.4.12;
Add ENVSYS_DRIVE_MIGRATING for ENVSYS_DRIVE sensors, this is for RAID
volumes that are migrating such as when you change the stripe size.

While I'm here use the same string than we had in the old framework to
report status "online" vs "drive is online", because the sensor might be
a RAID volume and not just a drive.
 1.3 03-Nov-2007  xtraeme branches: 1.3.2; 1.3.4; 1.3.6;
- Rename ENVSYS_BATTERY_STATE units to ENVSYS_BATTERY_CAPACITY and
introduce ENVSYS_BATTERY_CHARGE, which is the same than an Indicator and
it's used to know if the battery is currently charging or discharging.

- Require two sensors at least for SME_CLASS_BATTERY to make the low-power
condition work: a ENVSYS_BATTERY_CAPACITY plus ENVSYS_BATTERY_CHARGE.

- Simplify sme_event_check_lowpower() and make it check the required
sensors in the SME_CLASS_ACADAPTER and SME_CLASS_BATTERY classes.
If the acadapter is not ready, trust the state returned by the battery
device.

Based on suggestion from joerg@.
 1.2 07-Oct-2007  xtraeme branches: 1.2.2; 1.2.4; 1.2.6;
New changes to support the new configuration file format for
envstat(8) and the envsys framework:

- Modify the ENVSYS_SETDICTIONARY ioctl to support the following
plist structure:

<dict>
<key>foo0</key>
<array>
<dict>
<key>index</key>
<string>sensor0</string>
<key>description</key>
<string>cpu temp</string>
...
Another property for this sensor
...
</dict>
...
Another dictionary for other sensor
...
</array>
...
Another device as above
...
</dict>

Multiple devices with multiple sensors can now be specified, that means
that to set the properties only one copyin(9) is needed now.

- Added the ENVSYS_REMOVEPROPS ioctl, that accepts a boolean object
"envsys-remove-props" and when set to true, all properties that were
set previously by ENVSYS_SETDICTIONARY will be removed. That means that
you can now set multiple critical limits, descriptions and all they
will be removed or changed to its default value (for rfact and
description objects).

- Added the 'index' and 'allow-rfact' objects into the sensor dictionaries,
for better interactivity with userland. To know the position of the
sensor and to know if sensor allows to change the rfact.

- Misc cosmetic changes for consistency.

- Use a two clause license for all my code.
 1.1 04-Sep-2007  xtraeme branches: 1.1.2; 1.1.4; 1.1.6;
- Remove ENVSYS_GSTRING and the genstr member from envsys_data_t.
(at least three or four persons were against it).
- Add a new sensor type: ENVSYS_BATTERY_STATE, this uses value_cur
and some predefined values in a static table, like ENVSYS_DRIVE.
- Move all static tables to sysmon_envsys_tables.c and use a function
on it to retrieve a pointer to the struct of the specified type.
- Rename the ENVSYS_FMONDRVSTCHANGED to ENVSYS_FMONSTCHANGED and make
it generic for Battery state and drive sensors (this flag enables
monitoring on these sensors when state has been changed).
- Update sysmon_penvsys_event() to report state changes on
ENVSYS_BATTERY_STATE sensors and remove other type of events, with
PENVSYS_EVENT_STATE_CHANGED they are not necessary anymore.
 1.1.6.5 09-Dec-2007  jmcneill Sync with HEAD.
 1.1.6.4 04-Nov-2007  jmcneill Sync with HEAD.
 1.1.6.3 07-Oct-2007  joerg Sync with HEAD.
 1.1.6.2 02-Oct-2007  joerg Sync with HEAD.
 1.1.6.1 04-Sep-2007  joerg file sysmon_envsys_tables.c was added on branch jmcneill-pm on 2007-10-02 18:28:41 +0000
 1.1.4.1 14-Oct-2007  yamt sync with head.
 1.1.2.2 10-Sep-2007  skrll Sync with HEAD.
 1.1.2.1 04-Sep-2007  skrll file sysmon_envsys_tables.c was added on branch nick-csl-alignment on 2007-09-10 10:55:23 +0000
 1.2.6.5 17-Mar-2008  yamt sync with head.
 1.2.6.4 07-Dec-2007  yamt sync with head
 1.2.6.3 15-Nov-2007  yamt sync with head.
 1.2.6.2 27-Oct-2007  yamt sync with head.
 1.2.6.1 07-Oct-2007  yamt file sysmon_envsys_tables.c was added on branch yamt-lazymbuf on 2007-10-27 11:34:21 +0000
 1.2.4.1 13-Nov-2007  bouyer Sync with HEAD
 1.2.2.2 09-Oct-2007  ad Sync with head.
 1.2.2.1 07-Oct-2007  ad file sysmon_envsys_tables.c was added on branch vmlocking on 2007-10-09 13:42:06 +0000
 1.3.6.1 08-Dec-2007  ad Sync with head.
 1.3.4.4 23-Mar-2008  matt sync with HEAD
 1.3.4.3 09-Jan-2008  matt sync with HEAD
 1.3.4.2 06-Nov-2007  matt sync with HEAD
 1.3.4.1 03-Nov-2007  matt file sysmon_envsys_tables.c was added on branch matt-armv6 on 2007-11-06 23:30:20 +0000
 1.3.2.1 08-Dec-2007  mjf Sync with HEAD.
 1.4.12.1 03-Apr-2008  mjf Sync with HEAD.
 1.4.8.1 24-Mar-2008  keiichi sync with head.
 1.5.26.1 05-Mar-2011  rmind sync with head
 1.6.6.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.10.8.1 17-Oct-2012  riz Pull up following revision(s) (requested by pgoyette in ticket #545):
etc/powerd/scripts/sensor_indicator: revision 1.5
sys/dev/sysmon/sysmon_envsys_tables.c: revision 1.11
sys/sys/envsys.h: revision 1.32
sys/dev/sysmon/sysmon_envsysvar.h: revision 1.43
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.104
Provide names for the values associated with INDICATOR sensors.
1. Enable use of FMONSTCHANGED events for INDICATOR sensors
2. Update handling of FMONCRITICAL event reporting. The state
transition does not require a corresponding change in value.
With these changes, you can now have an INDICATOR sensor that
reports the presence or absence of a device, and (if the device
is present) separately monitor it for proper functioning.
Should address the concerns expressed recently with the commit
of changes to wmi(4) BBU handling.
Handle any reports of state changes for INDICATORs
 1.10.2.1 30-Oct-2012  yamt sync with head
 1.11.12.1 10-Aug-2014  tls Rebase.
 1.11.4.1 18-May-2014  rmind sync with head
 1.11.2.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.12.26.1 25-Jun-2018  pgoyette Sync with HEAD
 1.7 11-Jun-2020  thorpej Update for proplib(3) API changes.
 1.6 08-Jun-2020  thorpej Update for proplib(3) API changes.
 1.5 16-Nov-2007  xtraeme Extend the envsys2 API (one more time, sorry) as defined in:

http://mail-index.netbsd.org/tech-kern/2007/11/09/0001.html

sysmon_envsys_create() and sysmon_envsys_destroy() were added to
create/destroy sysmon_envsys objects (and its TAILQ/LIST for sensors/events).

sysmon_envsys_sensor_attach() and sysmon_envsys_sensor_detach() were
added to attach/detach sensors to a specified sysmon_envsys device.

The events framework is now per device and configurable via the
ENVSYS_SETDICTIONARY ioctl or /etc/envsys.conf and envstat(8).

Update all users and documentation to reflect these changes.
 1.4 07-Oct-2007  xtraeme branches: 1.4.2; 1.4.4;
New changes to support the new configuration file format for
envstat(8) and the envsys framework:

- Modify the ENVSYS_SETDICTIONARY ioctl to support the following
plist structure:

<dict>
<key>foo0</key>
<array>
<dict>
<key>index</key>
<string>sensor0</string>
<key>description</key>
<string>cpu temp</string>
...
Another property for this sensor
...
</dict>
...
Another dictionary for other sensor
...
</array>
...
Another device as above
...
</dict>

Multiple devices with multiple sensors can now be specified, that means
that to set the properties only one copyin(9) is needed now.

- Added the ENVSYS_REMOVEPROPS ioctl, that accepts a boolean object
"envsys-remove-props" and when set to true, all properties that were
set previously by ENVSYS_SETDICTIONARY will be removed. That means that
you can now set multiple critical limits, descriptions and all they
will be removed or changed to its default value (for rfact and
description objects).

- Added the 'index' and 'allow-rfact' objects into the sensor dictionaries,
for better interactivity with userland. To know the position of the
sensor and to know if sensor allows to change the rfact.

- Misc cosmetic changes for consistency.

- Use a two clause license for all my code.
 1.3 24-Sep-2007  plunky do not use _nocopy, this string is part of another object that
will be released shortly..
 1.2 21-Jul-2007  xtraeme branches: 1.2.4; 1.2.6; 1.2.8; 1.2.10; 1.2.12; 1.2.14; 1.2.16;
Some fixes for kern/36673 by yamt@:

- sme_register_sensorname: there's no need to handle empty list case.
- 'obj' argument in sme_sensor_upstring and similar functions is useless:
removed the obj argument from the functions, it's handled in the function
itself now.
- check for more errors in sme_make_dictionary() and
sysmon_envsys_createplist().
 1.1 20-Jul-2007  xtraeme Move the functions to create/update objects in a dictionary into its
own file, and DO NOT MAKE THEM inline AS IT IS WRONG.

Looks like I'm very stupid and I didn't know what inline meant.
Thank you very much YAMAMOTO Takashi.
 1.2.16.2 21-Jul-2007  xtraeme Some fixes for kern/36673 by yamt@:

- sme_register_sensorname: there's no need to handle empty list case.
- 'obj' argument in sme_sensor_upstring and similar functions is useless:
removed the obj argument from the functions, it's handled in the function
itself now.
- check for more errors in sme_make_dictionary() and
sysmon_envsys_createplist().
 1.2.16.1 21-Jul-2007  xtraeme file sysmon_envsys_util.c was added on branch matt-mips64 on 2007-07-21 12:11:28 +0000
 1.2.14.2 14-Oct-2007  yamt sync with head.
 1.2.14.1 06-Oct-2007  yamt sync with head.
 1.2.12.4 07-Dec-2007  yamt sync with head
 1.2.12.3 27-Oct-2007  yamt sync with head.
 1.2.12.2 03-Sep-2007  yamt sync with head.
 1.2.12.1 21-Jul-2007  yamt file sysmon_envsys_util.c was added on branch yamt-lazymbuf on 2007-09-03 14:38:50 +0000
 1.2.10.2 09-Jan-2008  matt sync with HEAD
 1.2.10.1 06-Nov-2007  matt sync with HEAD
 1.2.8.3 09-Oct-2007  ad Sync with head.
 1.2.8.2 20-Aug-2007  ad Sync with HEAD.
 1.2.8.1 21-Jul-2007  ad file sysmon_envsys_util.c was added on branch vmlocking on 2007-08-20 22:07:05 +0000
 1.2.6.2 15-Aug-2007  skrll Sync with HEAD.
 1.2.6.1 21-Jul-2007  skrll file sysmon_envsys_util.c was added on branch nick-csl-alignment on 2007-08-15 13:48:46 +0000
 1.2.4.3 21-Nov-2007  joerg Sync with HEAD.
 1.2.4.2 07-Oct-2007  joerg Sync with HEAD.
 1.2.4.1 02-Oct-2007  joerg Sync with HEAD.
 1.4.4.1 19-Nov-2007  mjf Sync with HEAD.
 1.4.2.1 18-Nov-2007  bouyer Sync with HEAD
 1.48 31-Dec-2021  riastradh sysmon: Delete trailing whitespace. No functional change intended.
 1.47 23-Nov-2014  ozaki-r Pull workqueue_destroy out of sme->sme_mtx

workqueue_destroy may sleep so we shouldn't run it with holding a mutex.

Requested by riastradh@.
 1.46 14-Dec-2012  pgoyette The sensor limit values were moved out of the event structure and into the
per-sensor structure a long time ago. Garbage collect the old structure
member which is now unused.
 1.45 06-Sep-2012  macallan branches: 1.45.2;
opt_envsys.h needs #ifdef _KERNEL_OPT
( thanks martin@ )
 1.44 06-Sep-2012  macallan defflag debug #defines
 1.43 27-Aug-2012  pgoyette 1. Enable use of FMONSTCHANGED events for INDICATOR sensors
2. Update handling of FMONCRITICAL event reporting. The state
transition does not require a corresponding change in value.

With these changes, you can now have an INDICATOR sensor that
reports the presence or absence of a device, and (if the device
is present) separately monitor it for proper functioning.

Should address the concerns expressed recently with the commit
of changes to wmi(4) BBU handling.
 1.42 15-Jul-2012  pgoyette When unregistering a sensor device, make sure we unregister and delete
all the associated events.
 1.41 18-Feb-2012  matt Don't export sysmon_envsys_list as a common.
 1.40 19-Jun-2011  pgoyette branches: 1.40.2; 1.40.6; 1.40.8;
Add routine to lookup description tables by description instead of by
type.
 1.39 08-Jun-2011  pgoyette Factor out some duplicated code to simplify maintenance. Reduces
the source file by ~100 lines, and amd64 object file shrinks by
~650 bytes.

No functional change intended.
 1.38 30-Dec-2010  pgoyette branches: 1.38.6;
When the user updates the sensor device's refresh timer, reset the
callout immediately rather than waiting for the previous timer to
expire.
 1.37 15-Dec-2010  pgoyette Extract searching of description tables into a single function, rather
than duplicating the code every time. Minor reduction in code size
(about 1200 bytes on amd64), no change in functionality.
 1.36 10-Apr-2010  pgoyette When removing sensor properties with envstat -S, ensure that drivers
which could have modified hardware state are informed so that original
state can be restored.

Welcome to 5.99.27
 1.35 01-Apr-2010  pgoyette Permit creation of an event-monitor for alarm limits even if the limits
are not currently set (and, in case of battery capacity sensors, if there
is no value_max). Ensure that such an event-monitor does not trigger the
delivery of any actual events.

Provide a mechanism for sensors to set their limits at time other than
system startup (for example, when a battery is inserted).

This allows us to boot a system with a battery missing, install the
battery some time later, and automatically monitor it without requiring
any user intervention to create the event-monitor. (The actual changes
for battery sensors to use this new capability will come later.)
 1.34 19-Mar-2010  pgoyette Modify previous commit. The routine to deliver events is intended to be
exported to device drivers, so rename it and move the declaration to
<dev/sysmon/sysmonvar.h>
 1.33 19-Mar-2010  pgoyette Separate event delivery from polling and limit evaluation in
sme_events_worker().

Provide a wrapper that can be called from sensor drivers' interrupt
routines to find and deliver a specific event.

XXX Actually updating sensor drivers (and their parent devices) to
XXX have interrupts is a future enhancement. This is simply an
XXX enabler.
 1.32 14-Mar-2010  pgoyette branches: 1.32.2;
Retire the 'monitor' member of the envsys_data_t. It was only used in
one place, and functioned as a logical OR of the ENVSYS_FMON* flag bits.
 1.31 14-Feb-2010  pgoyette Remove the flags member of the limits structure. Its value was only
meaningful for a few limited function calls; all persitent storage
of limit-present flags is maintained in the edata->upropset member.
 1.30 13-Jun-2009  pgoyette branches: 1.30.2;
Add capability to send sensor limit values to the driver so they can
be programmed into device registers. This way we can let the hardware
help us out instead of having to always compare the value against each
limit. (Driver updates for some sensors to take advantage of this
capability will be forthcoming.)
 1.29 08-Jun-2009  pgoyette General clean-up and some restructuring of event handling. This is a
precursor to letting sensor drivers actually exchange limits/thresholds
with user-land.
 1.28 22-Aug-2008  pgoyette branches: 1.28.8;
1. provide for warning-min and warning-max alarm levels in addition to
critical-min and critical-max; also, a battery warning-capacity is
added in addition to a critical-capacity.

2. usr.sbin/envstat is modified to introduce a -W command line switch to
display the warning-* values instead of the critical-* values, and
envstat(8) and envsys.conf(5) man pages are updated appropriately.

3. Treat user-defined limits as a single continuum and generate a single
event regardless of how many boundaries a change in sensor value
crosses; ditto for driver-defined limits.

Fixes my PR/39021
Fixes my PR/39022

OK'd by christos@ bouyer@ cube@
 1.27 01-Apr-2008  xtraeme branches: 1.27.4; 1.27.6; 1.27.10;
Introduce per-device locking/synchronization and maintain only a
global mutex for the linked list of devices and the global proplib
dictionary.

This has improved locking contention a lot when multiple devices with
multiple monitoring events are running:

New:

0.30 35 0.33 sme_global_mtx sysmonioctl_envsys+28b
0.10 10 0.11 00000000cd97feac sysmon_envsys_acquire+4c
0.08 6 0.09 00000000cd97feac sme_update_dictionary+24f
0.01 4 0.01 00000000cd97feac sme_events_worker+2f
0.01 10 0.01 00000000cd97fe2c sysmon_envsys_acquire+4c
0.00 1 0.00 00000000cd97fe2c sysmon_envsys_release+3b
28.38 94 9.16 sme_global_mtx sysmonioctl_envsys+28b
4.54 74 1.47 00000000cd97fe2c sysmon_envsys_acquire+4c
0.06 3 0.02 00000000cd97fe2c sysmon_envsys_release+3b
0.03 1 0.01 00000000cd97fe2c sme_events_worker+2f
1.40 19 0.45 00000000cd97bee4 sysmon_envsys_acquire+4c

Old:

4.25 313 4.74 sme_mtx <all>
3.12 185 3.49 sme_mtx sme_events_worker+21
1.12 128 1.25 sme_mtx sysmonioctl_envsys+29b
34.75 1423 59.52 sme_mtx <all>
22.08 477 37.82 sme_mtx sysmonioctl_envsys+29b
12.67 946 21.70 sme_mtx sme_events_worker+21
 1.26 20-Nov-2007  xtraeme branches: 1.26.14;
After comments from Andrew Doran:

- The mutex for the callout handler must run at IPL_SOFTCLOCK.
- Just stop the callout in sysmon_envsys_unregister() and don't wait
for the callout to finish.
 1.25 16-Nov-2007  xtraeme Remove duplicate sme_events_list.
 1.24 16-Nov-2007  xtraeme Extend the envsys2 API (one more time, sorry) as defined in:

http://mail-index.netbsd.org/tech-kern/2007/11/09/0001.html

sysmon_envsys_create() and sysmon_envsys_destroy() were added to
create/destroy sysmon_envsys objects (and its TAILQ/LIST for sensors/events).

sysmon_envsys_sensor_attach() and sysmon_envsys_sensor_detach() were
added to attach/detach sensors to a specified sysmon_envsys device.

The events framework is now per device and configurable via the
ENVSYS_SETDICTIONARY ioctl or /etc/envsys.conf and envstat(8).

Update all users and documentation to reflect these changes.
 1.23 03-Nov-2007  xtraeme branches: 1.23.2;
- Rename ENVSYS_BATTERY_STATE units to ENVSYS_BATTERY_CAPACITY and
introduce ENVSYS_BATTERY_CHARGE, which is the same than an Indicator and
it's used to know if the battery is currently charging or discharging.

- Require two sensors at least for SME_CLASS_BATTERY to make the low-power
condition work: a ENVSYS_BATTERY_CAPACITY plus ENVSYS_BATTERY_CHARGE.

- Simplify sme_event_check_lowpower() and make it check the required
sensors in the SME_CLASS_ACADAPTER and SME_CLASS_BATTERY classes.
If the acadapter is not ready, trust the state returned by the battery
device.

Based on suggestion from joerg@.
 1.22 23-Oct-2007  xtraeme Use a flag for see_flags to know when the sensor has been refreshed
rather than using a boolean. Suggested by rmind.
 1.21 20-Oct-2007  xtraeme performance fix: in the workqueue handler only refresh the sensor once,
not multiple times for the same sensor and clear its state after all
events were processed.
 1.20 07-Oct-2007  xtraeme branches: 1.20.2;
New changes to support the new configuration file format for
envstat(8) and the envsys framework:

- Modify the ENVSYS_SETDICTIONARY ioctl to support the following
plist structure:

<dict>
<key>foo0</key>
<array>
<dict>
<key>index</key>
<string>sensor0</string>
<key>description</key>
<string>cpu temp</string>
...
Another property for this sensor
...
</dict>
...
Another dictionary for other sensor
...
</array>
...
Another device as above
...
</dict>

Multiple devices with multiple sensors can now be specified, that means
that to set the properties only one copyin(9) is needed now.

- Added the ENVSYS_REMOVEPROPS ioctl, that accepts a boolean object
"envsys-remove-props" and when set to true, all properties that were
set previously by ENVSYS_SETDICTIONARY will be removed. That means that
you can now set multiple critical limits, descriptions and all they
will be removed or changed to its default value (for rfact and
description objects).

- Added the 'index' and 'allow-rfact' objects into the sensor dictionaries,
for better interactivity with userland. To know the position of the
sensor and to know if sensor allows to change the rfact.

- Misc cosmetic changes for consistency.

- Use a two clause license for all my code.
 1.19 08-Sep-2007  xtraeme branches: 1.19.2;
Use only one single condvar(9) for devices and events, they are protected
by the same mutex (sme_mtx) and there's no point in using two of them.

Also add a comment mentioning some locking notes.

Reviewed and ok by rmind.
 1.18 08-Sep-2007  xtraeme - Use only one global mutex for all tasks with devices & events.
- sysmon_envsys_register: use a SLIST to maintain allocated sme_event_drv_t
structs for later use in sysmon_task_queue_sched(). This avoids a
locking error acquiring/dropping the mutex multiple times.

Suggested by rmind.
 1.17 04-Sep-2007  xtraeme - Remove ENVSYS_GSTRING and the genstr member from envsys_data_t.
(at least three or four persons were against it).
- Add a new sensor type: ENVSYS_BATTERY_STATE, this uses value_cur
and some predefined values in a static table, like ENVSYS_DRIVE.
- Move all static tables to sysmon_envsys_tables.c and use a function
on it to retrieve a pointer to the struct of the specified type.
- Rename the ENVSYS_FMONDRVSTCHANGED to ENVSYS_FMONSTCHANGED and make
it generic for Battery state and drive sensors (this flag enables
monitoring on these sensors when state has been changed).
- Update sysmon_penvsys_event() to report state changes on
ENVSYS_BATTERY_STATE sensors and remove other type of events, with
PENVSYS_EVENT_STATE_CHANGED they are not necessary anymore.
 1.16 31-Aug-2007  xtraeme branches: 1.16.2;
- sme_unregister_all: there's no need to use a struct sysmon_envsys as
argument, just pass sme->sme_name to it.
- sysmon_envsys_register: drop the array in all cases, not just when
there's an error. The reference is stored in the dictionary anyway...
- Update some comments.
 1.15 31-Aug-2007  xtraeme Merge sme_event_add() into sme_event_register()... there's no need
to have two different functions for this.
 1.14 30-Aug-2007  xtraeme More misc changes for sysmon_envsys(9):

- Add the SLIST for sensor descriptions and sme_uniqsensors into the
struct sysmon_envsys (it's per device now).
- Use only one common struct with three members for the static tables
(there's no need to have different structs just for them).
- While initializing/destroying the events framework, use the
strategy specified by Andrew Doran in:
http://mail-index.netbsd.org/tech-kern/2007/06/21/0025.html).
 1.13 30-Aug-2007  xtraeme Some changes to improve locking on sysmon_envsys(9):

- Remove sme_mtx, a global lock (sme_list_mtx) is used to access to
the sysmon envsys device.
- Allocate memory with KM_NOSLEEP rather than KM_SLEEP if there's a
mutex held, to avoid sleeping.
- Remove sysmon_envsys_createplist() and add the logic into
sysmon_envsys_register().
- sysmon_envsys_register: allocate the array and dictionaries required
in advance for a device before the locking and adding the objects
into the array happens.
- Rename sme_make_dictionary() to sme_add_sensor_dictionary() and pass
to it the dictionary on which the objects will be stored for a sensor.
- Improve locking here and there.

Thanks to Mindaugas Rasiukevicius and Andrew Doran for comments.
 1.12 23-Jul-2007  xtraeme branches: 1.12.4; 1.12.6; 1.12.8;
- ENVSYS_SETDICTIONARY: use sysmon_envsys_release() if there's an error.
- sme_events_worker: use sme_list_mtx when accessing to the sysmon_envsys
linked list.
- Improve the comments in sysmon_envsysvar.h about the mutexes and condvar.
 1.11 22-Jul-2007  xtraeme Fix the last point reported by yamt in PR kern/36673:

- There must be three mandatory objects in a sensor to be valid and be
enabled on its dictionary: units, state and desc. If any of these
objects fails for some reason, its dictionary won't created in the
array and sensor will be marked with ENVSYS_FNOTVALID in edata->flags.

That means there won't never be a partial dictionary.

- Mark all invalid sensors and sensors with duplicate description with
ENVSYS_FNOTVALID and remove ENVSYS_FDUPDESC. The former is the generic
way to say that a sensor does not obey the rules and must be not
created.

- sysmon_envsys_createplist: if the array does not contain any object,
release the array and return the error.

And with all that stuff I can go to holidays and be happy.
 1.10 21-Jul-2007  xtraeme - Add sme_events_destroy() to stop/destroy the callout and workqueue if
there are no more events.
- Add sme_event_unregister_all() to remove all events associated with
a device and use it in sysmon_envsys_unregister() to fix a "use-after-free".
- Check return error of sme_sensor_upfoo() in sme_make_dictionary() and
return it rather than 0 all the time.
- Add more KASSERTS.
 1.9 21-Jul-2007  xtraeme Some fixes for kern/36673 by yamt@:

- sme_register_sensorname: there's no need to handle empty list case.
- 'obj' argument in sme_sensor_upstring and similar functions is useless:
removed the obj argument from the functions, it's handled in the function
itself now.
- check for more errors in sme_make_dictionary() and
sysmon_envsys_createplist().
 1.8 20-Jul-2007  xtraeme Move the functions to create/update objects in a dictionary into its
own file, and DO NOT MAKE THEM inline AS IT IS WRONG.

Looks like I'm very stupid and I didn't know what inline meant.
Thank you very much YAMAMOTO Takashi.
 1.7 20-Jul-2007  xtraeme Replace the macros to create/update objects in dictionary with three
inline functions, suggested by yamt@.

This also gets rid of a few gotos.
 1.6 19-Jul-2007  xtraeme Apply some indent to recent changes.
 1.5 18-Jul-2007  xtraeme - More changes related to locking.
- Add a condvar(9) to protect deletions on the list if there's an event
in the worker thread.
 1.4 18-Jul-2007  xtraeme - Cleanup sme_event_t: just use the struct work without any union and
remove the TAILQ.
- If prop_dictionary_set_* fails, do not release the object. The function
calling it will release the object. Pointed out by yamt@.
 1.3 05-Jul-2007  xtraeme branches: 1.3.2; 1.3.4; 1.3.6;
SENSOR_UPTYPE: accept values of 0 for any object, so that the value is
showed in its dictionary.
 1.2 03-Jul-2007  xtraeme Make the refresh value of the callout function (that monitors the sensors)
configurable via sysctl. By default it uses 10 seconds, but it can be changed
between 1 second and 5 minutes.

$ sysctl -d kern.envsys
kern.envsys.refresh_value: wait time in seconds to refresh sensors being monitored
$
 1.1 01-Jul-2007  xtraeme Imported envsys 2, a brief description of the new features:
(Part 1: API)

* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.

Tested by:

blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
 1.3.6.3 10-Sep-2007  skrll Sync with HEAD.
 1.3.6.2 03-Sep-2007  skrll Sync with HEAD.
 1.3.6.1 15-Aug-2007  skrll Sync with HEAD.
 1.3.4.4 09-Oct-2007  ad Sync with head.
 1.3.4.3 20-Aug-2007  ad Sync with HEAD.
 1.3.4.2 15-Jul-2007  ad Sync with head.
 1.3.4.1 05-Jul-2007  ad file sysmon_envsysvar.h was added on branch vmlocking on 2007-07-15 13:21:45 +0000
 1.3.2.2 11-Jul-2007  mjf Sync with head.
 1.3.2.1 05-Jul-2007  mjf file sysmon_envsysvar.h was added on branch mjf-ufs-trans on 2007-07-11 20:08:25 +0000
 1.12.8.2 23-Jul-2007  xtraeme - ENVSYS_SETDICTIONARY: use sysmon_envsys_release() if there's an error.
- sme_events_worker: use sme_list_mtx when accessing to the sysmon_envsys
linked list.
- Improve the comments in sysmon_envsysvar.h about the mutexes and condvar.
 1.12.8.1 23-Jul-2007  xtraeme file sysmon_envsysvar.h was added on branch matt-mips64 on 2007-07-23 17:51:18 +0000
 1.12.6.2 09-Jan-2008  matt sync with HEAD
 1.12.6.1 06-Nov-2007  matt sync with HEAD
 1.12.4.6 21-Nov-2007  joerg Sync with HEAD.
 1.12.4.5 04-Nov-2007  jmcneill Sync with HEAD.
 1.12.4.4 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.12.4.3 07-Oct-2007  joerg Sync with HEAD.
 1.12.4.2 02-Oct-2007  joerg Sync with HEAD.
 1.12.4.1 03-Sep-2007  jmcneill Sync with HEAD.
 1.16.2.5 07-Dec-2007  yamt sync with head
 1.16.2.4 15-Nov-2007  yamt sync with head.
 1.16.2.3 27-Oct-2007  yamt sync with head.
 1.16.2.2 03-Sep-2007  yamt sync with head.
 1.16.2.1 31-Aug-2007  yamt file sysmon_envsysvar.h was added on branch yamt-lazymbuf on 2007-09-03 14:38:51 +0000
 1.19.2.1 14-Oct-2007  yamt sync with head.
 1.20.2.4 21-Nov-2007  bouyer Sync with HEAD
 1.20.2.3 18-Nov-2007  bouyer Sync with HEAD
 1.20.2.2 13-Nov-2007  bouyer Sync with HEAD
 1.20.2.1 25-Oct-2007  bouyer Sync with HEAD.
 1.23.2.2 08-Dec-2007  mjf Sync with HEAD.
 1.23.2.1 19-Nov-2007  mjf Sync with HEAD.
 1.26.14.2 28-Sep-2008  mjf Sync with HEAD.
 1.26.14.1 03-Apr-2008  mjf Sync with HEAD.
 1.27.10.1 19-Oct-2008  haad Sync with HEAD.
 1.27.6.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.27.4.4 11-Aug-2010  yamt sync with head.
 1.27.4.3 11-Mar-2010  yamt sync with head
 1.27.4.2 20-Jun-2009  yamt sync with head
 1.27.4.1 04-May-2009  yamt sync with head.
 1.28.8.1 23-Jul-2009  jym Sync with HEAD.
 1.30.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.32.2.3 12-Jun-2011  rmind sync with head
 1.32.2.2 05-Mar-2011  rmind sync with head
 1.32.2.1 30-May-2010  rmind sync with head
 1.38.6.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.40.8.3 19-Oct-2012  riz Remove stray CR characters introduced in ticket #544. From pgoyette.
 1.40.8.2 17-Oct-2012  riz Pull up following revision(s) (requested by pgoyette in ticket #545):
etc/powerd/scripts/sensor_indicator: revision 1.5
sys/dev/sysmon/sysmon_envsys_tables.c: revision 1.11
sys/sys/envsys.h: revision 1.32
sys/dev/sysmon/sysmon_envsysvar.h: revision 1.43
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.104
Provide names for the values associated with INDICATOR sensors.
1. Enable use of FMONSTCHANGED events for INDICATOR sensors
2. Update handling of FMONCRITICAL event reporting. The state
transition does not require a corresponding change in value.
With these changes, you can now have an INDICATOR sensor that
reports the presence or absence of a device, and (if the device
is present) separately monitor it for proper functioning.
Should address the concerns expressed recently with the commit
of changes to wmi(4) BBU handling.
Handle any reports of state changes for INDICATORs
 1.40.8.1 17-Oct-2012  riz Pull up following revision(s) (requested by pgoyette in ticket #544):
sys/dev/sysmon/sysmon_envsysvar.h: revision 1.42
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.99
sys/dev/sysmon/sysmon_envsys.c: revision 1.119
When unregistering a sensor device, make sure we unregister and delete
all the associated events.
 1.40.6.1 18-Feb-2012  mrg merge to -current.
 1.40.2.3 23-Jan-2013  yamt sync with head
 1.40.2.2 30-Oct-2012  yamt sync with head
 1.40.2.1 17-Apr-2012  yamt sync with head
 1.45.2.2 03-Dec-2017  jdolecek update from HEAD
 1.45.2.1 25-Feb-2013  tls resync with head
 1.69 31-Dec-2021  riastradh sysmon: Delete trailing whitespace. No functional change intended.
 1.68 26-Sep-2021  thorpej - Call selnotify() with sysmon_power_event_queue_mtx held, passing the
correct hints. Adjust filt_sysmon_power_read() accordingly (assert
that the mutex is held iff NOTE_SUBMIT).
- Mark sysmon_power_read_filtops as MPSAFE.
- Use seltrue_filtops rather than rolling our own with filt_seltrue.
 1.67 26-Sep-2021  thorpej Change the kqueue filterops::f_isfd field to filterops::f_flags, and
define a flag FILTEROP_ISFD that has the meaning of the prior f_isfd.
Field and flag name aligned with OpenBSD.

This does not constitute a functional or ABI change, as the field location
and size, and the value placed in that field, are the same as the previous
code, but we're bumping __NetBSD_Version__ so 3rd-party module source code
can adapt, as needed.

NetBSD 9.99.89
 1.66 18-Dec-2020  thorpej Use sel{remove,record}_knote().
 1.65 30-Oct-2020  christos branches: 1.65.2;
fix indentation
 1.64 11-Jun-2020  thorpej Update for proplib(3) API changes.
 1.63 08-Jun-2020  thorpej Update for proplib(3) API changes.
 1.62 01-Jan-2020  thorpej - Introduce a new global kernel variable "shutting_down" to indicate that
the system is shutting down or rebooting.
- Set this global in a new function called kern_reboot(), which is currently
just a basic wrapper around cpu_reboot().
- Call kern_reboot() instead of cpu_reboot() almost everywhere; a few
places remain where it's still called directly, but those are in early
pre-main() machdep locations.

Eventually, all of the various cpu_reboot() functions should be re-factored
and common functionality moved to kern_reboot(), but that's for another day.
 1.61 20-Aug-2019  maxv Fix info leak, not all of 'pev' is initialized.
 1.60 01-Mar-2019  pgoyette branches: 1.60.4;
Rename the MODULE_*_HOOK() macros to MODULE_HOOK_*() as briefly
discussed on irc.

NFCI intended.

Ride the earlier kernel bump - it;s getting crowded.
 1.59 27-Jan-2019  pgoyette Merge the [pgoyette-compat] branch
 1.58 25-Oct-2017  maya branches: 1.58.2; 1.58.4;
Use C99 initializer for filterops

Mostly done with spatch with touchups for indentation

@@
expression a;
identifier b,c,d;
identifier p;
@@
const struct filterops p =
- { a, b, c, d
+ {
+ .f_isfd = a,
+ .f_attach = b,
+ .f_detach = c,
+ .f_event = d,
};
 1.57 14-Dec-2015  pgoyette branches: 1.57.10;
sysmon's components need to be MODULE_CLASS_DRIVER so they will get
initialized before we configure/initialize any devices that interact
with them.

Thanks, marty!
 1.56 24-Aug-2015  pooka to garnish, dust with _KERNEL_OPT
 1.55 25-Apr-2015  pgoyette Handle early initialization requirements - thanks martin@ and others
 1.54 23-Apr-2015  pgoyette Modularize sysmon and its components
 1.53 13-Apr-2015  riastradh Convert sys/dev to use <sys/rndsource.h>.
 1.52 04-Apr-2015  christos another missing free dict.
 1.51 04-Apr-2015  christos don't forget to free the dictionary.
 1.50 04-Apr-2015  riastradh Free ped if we can't hand it to the power daemon.

Found by Brainy, reported by maxv@.
 1.49 06-Jan-2015  bouyer As proposed on tech-net@, introduce a new switch type, PSWITCH_TYPE_RADIO,
to be used to report to userland hardware radio switch changes.
powerd(8) will call a "radio_button" script to handle the event.
This script can e.g. start or stop wpa_supplicant.
Update wpi(4) to report PSWITCH_TYPE_RADIO events to sysmon.
 1.48 21-Nov-2014  joerg branches: 1.48.2;
Explicitly compare to NULL to show clang that the bool conversion is
intentional, even if it is always true in case of the arrays.
 1.47 10-Aug-2014  tls branches: 1.47.2;
Merge tls-earlyentropy branch into HEAD.
 1.46 02-Feb-2012  tls branches: 1.46.6; 1.46.20;
Entropy-pool implementation move and cleanup.

1) Move core entropy-pool code and source/sink/sample management code
to sys/kern from sys/dev.

2) Remove use of NRND as test for presence of entropy-pool code throughout
source tree.

3) Remove use of RND_ENABLED in device drivers as microoptimization to
avoid expensive operations on disabled entropy sources; make the
rnd_add calls do this directly so all callers benefit.

4) Fix bug in recent rnd_add_data()/rnd_add_uint32() changes that might
have lead to slight entropy overestimation for some sources.

5) Add new source types for environmental sensors, power sensors, VM
system events, and skew between clocks, with a sample implementation
for each.

ok releng to go in before the branch due to the difficulty of later
pullup (widespread #ifdef removal and moved files). Tested with release
builds on amd64 and evbarm and live testing on amd64.
 1.45 22-Jul-2011  jakllsch branches: 1.45.2; 1.45.6;
Ensure events come off the list in the same order they went on.
Hotkey press/release events now happen in the correct order.
 1.44 11-Mar-2010  jruoho Fix comment typos (mangement, managmenet).
 1.43 15-Feb-2010  pgoyette For symmetry, allow warning and critical upper-limits for capacity
sensors.
 1.42 06-Nov-2009  jakllsch branches: 1.42.2;
Fix POWER_IOC_GET_TYPE.

The longest string in use with this seems to have been "acpi",
so size_t covered that, but on 32-bit ports omited the NUL.
 1.41 08-Jun-2009  pgoyette General clean-up and some restructuring of event handling. This is a
precursor to letting sensor drivers actually exchange limits/thresholds
with user-land.
 1.40 05-Sep-2008  gmcgarry branches: 1.40.8;
Rearrange sysmonread_power() to have a single exit point, which simplifies
the locking and fixes -Wunreachable-code warning.
 1.39 22-Aug-2008  pgoyette 1. provide for warning-min and warning-max alarm levels in addition to
critical-min and critical-max; also, a battery warning-capacity is
added in addition to a critical-capacity.

2. usr.sbin/envstat is modified to introduce a -W command line switch to
display the warning-* values instead of the critical-* values, and
envstat(8) and envsys.conf(5) man pages are updated appropriately.

3. Treat user-defined limits as a single continuum and generate a single
event regardless of how many boundaries a change in sensor value
crosses; ditto for driver-defined limits.

Fixes my PR/39021
Fixes my PR/39022

OK'd by christos@ bouyer@ cube@
 1.38 10-May-2008  jmcneill branches: 1.38.4;
Use aprint when reporting AC adapter status.
 1.37 01-Mar-2008  rmind branches: 1.37.2; 1.37.4; 1.37.6;
Welcome to 4.99.55:

- Add a lot of missing selinit() and seldestroy() calls.

- Merge selwakeup() and selnotify() calls into a single selnotify().

- Add an additional 'events' argument to selnotify() call. It will
indicate which event (POLL_IN, POLL_OUT, etc) happen. If unknown,
zero may be used.

Note: please pass appropriate value of 'events' where possible.
Proposed on: <tech-kern>
 1.36 29-Feb-2008  xtraeme Added a missing break that was bothering me.
 1.35 22-Dec-2007  jmcneill branches: 1.35.2; 1.35.6;
Handle hotkey events using /etc/powerd/scripts/hotkey_button, ok xtraeme
 1.34 09-Dec-2007  martin branches: 1.34.2;
Add missing <sys/device.h> include.
 1.33 09-Dec-2007  jmcneill Merge jmcneill-pm branch.
 1.32 05-Dec-2007  pooka branches: 1.32.2;
Do not "return 1" from kqfilter for errors. That value is passed
directly to the userland caller and results in a mysterious EPERM.
Instead, return EINVAL or something else sensible depending on the
case.
 1.31 05-Dec-2007  ad Match the docs: MUTEX_DRIVER/SPIN are now only for porting code written
for Solaris.
 1.30 10-Nov-2007  xtraeme branches: 1.30.2;
Add another flag that is set only when the event was enqueued
(and its dictionary created) successfully and use it in the
POWER_EVENT_RECVDICT ioctl to check if the dictionary is ready before
calling prop_dictionary_copyout_ioctl().

This fixes a rare condition when too many events are enqueued and
there wasn't time to create the dictionary, so prop_dictionary_copyout_ioctl()
fails with a NULL pointer dereference.
 1.29 10-Oct-2007  xtraeme branches: 1.29.2; 1.29.4;
Fix the problems described in:
http://mail-index.netbsd.org/current-users/2007/07/16/0012.html

- Introduce sme_class into the sysmon_envsys struct to specify a
class; currently there are two classes: SME_CLASS_ACADAPTER and
SME_CLASS_BATTERY.

- Add a new envsys event: PENVSYS_EVENT_LOW_POWER that is reached when
all SME_CLASS_BATTERY devices are in CRITICAL/LOW state and there's not
any SME_CLASS_ACADAPTER connected.

- Add the 'low-power' event into the sensor_battery script that will
shutdown the system gracefully via 'shutdown -p'. If powerd(8) is
not running, cpu_reboot(9) with RB_POWERDOWN is used.

- Make acpiacad(4) a SME_CLASS_ACADAPTER device and acpibat(4) a
SME_CLASS_BATTERY device.

Update the documentation accordingly to these changes.
 1.28 06-Oct-2007  xtraeme Forgot to change the kmem_zalloc(9) calls to use KM_NOSLEEP in previous,
as was suggested by rmind.
 1.27 06-Oct-2007  xtraeme Fix a bug that happened when two or more events were received almost
at the same time and the code accessed to an unexistent dictionary:

- Rather than using a global dictionary, use a singly linked list to
access to the dictionaries, one per event.
- When the dictionary has been sent to userland, destroy it and remove
it from the list.

With that change it is possible to receive multiple events at the
same time without panics; again thanks to rmind@ for comments and help
with locking.

Reviewed and ok by rmind.
 1.26 06-Oct-2007  xtraeme - sysmon_power_daemon_task: check if pev is valid before use.
- Add two more KASSERTS in strategic places.
 1.25 25-Sep-2007  xtraeme - Don't use _nocopy in prop_dictionary_set_cstring, the string is part
of another object that will be released shortly.
- Fix a memleak: add sysmon_power_destroy_dictionary() and remove
all objects that are currently in the dictionary and finally release
the dictionary.
 1.24 04-Sep-2007  xtraeme branches: 1.24.2;
- Remove ENVSYS_GSTRING and the genstr member from envsys_data_t.
(at least three or four persons were against it).
- Add a new sensor type: ENVSYS_BATTERY_STATE, this uses value_cur
and some predefined values in a static table, like ENVSYS_DRIVE.
- Move all static tables to sysmon_envsys_tables.c and use a function
on it to retrieve a pointer to the struct of the specified type.
- Rename the ENVSYS_FMONDRVSTCHANGED to ENVSYS_FMONSTCHANGED and make
it generic for Battery state and drive sensors (this flag enables
monitoring on these sensors when state has been changed).
- Update sysmon_penvsys_event() to report state changes on
ENVSYS_BATTERY_STATE sensors and remove other type of events, with
PENVSYS_EVENT_STATE_CHANGED they are not necessary anymore.
 1.23 03-Sep-2007  xtraeme Add some KASSERT(mutex_owned(&foo_lock)).
 1.22 17-Aug-2007  pavel branches: 1.22.2;
Add missing #include <prop/proplib.h>, should fix build failure reported
by Juergen Hannken-Illjes. The code apparently relied on proplib.h being
included indirectly through proc.h -> event.h -> ioctl.h -> dkio.h.
 1.21 07-Aug-2007  xtraeme The acpilid(4) powerd script is "lid_switch" not "lid_button"... found
by jmcneill@.
 1.20 15-Jul-2007  ad branches: 1.20.2; 1.20.6;
The power daemon is now an LWP.
 1.19 02-Jul-2007  xtraeme Add PENVSYS_TYPE_INDICATOR to handle events on Indicator sensors.

ipmi(4) uses this for the Intrussion and Power Supply sensors.
 1.18 01-Jul-2007  xtraeme Imported envsys 2, a brief description of the new features:
(Part 1: API)

* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.

Tested by:

blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
 1.17 04-Mar-2007  christos branches: 1.17.2; 1.17.4;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.16 19-Feb-2007  ad Back out previous for now. There isn't currently an easy way to set up
shared state before these get attached or used.

From xtraeme@.
 1.15 18-Feb-2007  xtraeme * Replace simple_locks and exclusive locks with adaptive mutexes.
* Replace ltsleep/wakeup pairs with condition variables
(cv_init/cv_wait/cv_broadcast).

Reviewed by ad@.
 1.14 16-Nov-2006  christos branches: 1.14.4;
__unused removal on arguments; approved by core.
 1.13 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.12 08-Jun-2006  freza branches: 1.12.6; 1.12.8;
Advance queue indices properly instead of silently rewriting
sysmon_power_event_queue[0] all the time.

OK by thorpej@
 1.11 11-Dec-2005  christos branches: 1.11.4; 1.11.8; 1.11.14;
merge ktrace-lwp.
 1.10 22-May-2004  cube branches: 1.10.10; 1.10.12;
According to acpi_acad.c and powerd(8), PRESSED event for AC adapter device
means it gets connected. It feels odd to be told the AC adapter went
offline right after plugging it in.
 1.9 03-May-2004  kochi Add AC adapter event type.
 1.8 14-Jul-2003  lukem branches: 1.8.4;
add missing __KERNEL_RCSID()
 1.7 29-Jun-2003  fvdl branches: 1.7.2;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.6 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.5 19-May-2003  kochi Fix a locking problem, which closes kern/21607.
ok'd by Jason Thorpe.
 1.4 11-May-2003  fvdl Fix format / number of argument mismatch for a printf.
 1.3 20-Apr-2003  thorpej Add a "reset button" type.
 1.2 18-Apr-2003  thorpej * Add a generic power management event API, defined in <sys/power.h>.
Right now, only power switch state change events are supported. This
is a work-in-progress.
* Add support to sysmon for delivering power mangement events to userland.
Add poll, kqueue, and read entry points to sysmon.
* Adapt ACPI to use the new generic <sys/power.h> event types.

This provides the kernel support for a forthcoming powerd(8) which can
do nice things like gracefully shut the system down when an ACPI power
button is pressed.
 1.1 17-Apr-2003  thorpej Start at a power management framework for sysmon. Right now we just
provide some VERY basic support for power/sleep buttons and lid switches;
if someone presses the power button, shut down the system semi-gracefully.

Eventually, we will send events for all types of button/lid events down
to a userland power management daemon, which will be able to define a
separate policy for each button/switch.
 1.7.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.7.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.7.2.2 03-Aug-2004  skrll Sync with HEAD
 1.7.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.8.4.1 03-Aug-2006  tron Pull up following revision(s) (requested by freza in ticket #10642):
sys/dev/sysmon/sysmon_power.c: revision 1.12 via patch
Advance queue indices properly instead of silently rewriting
sysmon_power_event_queue[0] all the time.
OK by thorpej@
 1.10.12.8 17-Mar-2008  yamt sync with head.
 1.10.12.7 21-Jan-2008  yamt sync with head
 1.10.12.6 07-Dec-2007  yamt sync with head
 1.10.12.5 15-Nov-2007  yamt sync with head.
 1.10.12.4 27-Oct-2007  yamt sync with head.
 1.10.12.3 03-Sep-2007  yamt sync with head.
 1.10.12.2 30-Dec-2006  yamt sync with head.
 1.10.12.1 21-Jun-2006  yamt sync with head.
 1.10.10.1 13-Jun-2006  tron Pull up following revision(s) (requested by freza in ticket #1369):
sys/dev/sysmon/sysmon_power.c: revision 1.12
Advance queue indices properly instead of silently rewriting
sysmon_power_event_queue[0] all the time.
OK by thorpej@
 1.11.14.1 19-Jun-2006  chap Sync with head.
 1.11.8.1 26-Jun-2006  yamt sync with head.
 1.11.4.1 09-Sep-2006  rpaulo sync with head
 1.12.8.2 10-Dec-2006  yamt sync with head.
 1.12.8.1 22-Oct-2006  yamt sync with head
 1.12.6.1 18-Nov-2006  ad Sync with head.
 1.14.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.17.4.1 11-Jul-2007  mjf Sync with head.
 1.17.2.9 12-Oct-2007  ad Sync with head.
 1.17.2.8 09-Oct-2007  ad Sync with head.
 1.17.2.7 09-Oct-2007  ad Sync with head.
 1.17.2.6 20-Aug-2007  ad Sync with HEAD.
 1.17.2.5 15-Jul-2007  ad Sync with head.
 1.17.2.4 15-Jul-2007  ad Sync with head.
 1.17.2.3 09-Jul-2007  ad Make sysmon_power_daemon of type lwp_t *.
 1.17.2.2 05-Apr-2007  ad Compile fixes.
 1.17.2.1 21-Mar-2007  ad GC the simplelock/spinlock debugging stuff.
 1.20.6.10 09-Dec-2007  jmcneill Sync with HEAD.
 1.20.6.9 08-Dec-2007  jmcneill Rename pnp(9) -> pmf(9), as requested by many.
 1.20.6.8 11-Nov-2007  joerg Sync with HEAD.
 1.20.6.7 06-Nov-2007  joerg Refactor PNP API:
- Make suspend/resume directly a device functionality. It consists of
three layers (class logic, device logic, bus logic), all of them being
optional. This replaces D0/D3 transitions.
- device_is_active returns true if the device was not disabled and was
not suspended (even partially), device_is_enabled returns true if the
device was enabled.
- Change pnp_global_transition into pnp_system_suspend and
pnp_system_resume. Before running any suspend/resume handlers, check
that all currently attached devices support power management and bail
out otherwise. The latter is not done for the shutdown/panic case.
- Make the former bus-specific generic network handlers a class handler.
- Make PNP message like volume up/down/toogle PNP events. Each device
can register what events they are interested in and whether the handler
should be global or not.
- Introduce device_active API for devices to mark themselve in use from
either the system or the device. Use this to implement the idle handling
for audio and input devices. This is intended to replace most ad-hoc
watchdogs as well.
- Fix somes situations in which audio resume would lose mixer settings.
- Make USB host controllers better deal with suspend in the light of
shared interrupts.
- Flush filesystem cache on suspend.
- Flush disk caches on suspend. Put ATA disks into standby on suspend as
well.
- Adopt drivers to use the new PNP API.
- Fix a critical bug in the generic cardbus layer that made D0->D3
break.
- Fix ral(4) to set if_stop.
- Convert cbb(4) to the new PNP API.
- Apply the PCI Express SCI fix on resume again.
 1.20.6.6 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.20.6.5 07-Oct-2007  joerg Sync with HEAD.
 1.20.6.4 02-Oct-2007  joerg Sync with HEAD.
 1.20.6.3 03-Sep-2007  jmcneill Sync with HEAD.
 1.20.6.2 09-Aug-2007  jmcneill Sync with HEAD.
 1.20.6.1 03-Aug-2007  jmcneill Pull in power management changes from private branch.
 1.20.2.3 10-Sep-2007  skrll Sync with HEAD.
 1.20.2.2 03-Sep-2007  skrll Sync with HEAD.
 1.20.2.1 15-Aug-2007  skrll Sync with HEAD.
 1.22.2.3 23-Mar-2008  matt sync with HEAD
 1.22.2.2 09-Jan-2008  matt sync with HEAD
 1.22.2.1 06-Nov-2007  matt sync with HEAD
 1.24.2.2 14-Oct-2007  yamt sync with head.
 1.24.2.1 06-Oct-2007  yamt sync with head.
 1.29.4.3 27-Dec-2007  mjf Sync with HEAD.
 1.29.4.2 08-Dec-2007  mjf Sync with HEAD.
 1.29.4.1 19-Nov-2007  mjf Sync with HEAD.
 1.29.2.1 13-Nov-2007  bouyer Sync with HEAD
 1.30.2.2 26-Dec-2007  ad Sync with head.
 1.30.2.1 08-Dec-2007  ad Sync with head.
 1.32.2.1 11-Dec-2007  yamt sync with head.
 1.34.2.1 02-Jan-2008  bouyer Sync with HEAD
 1.35.6.3 28-Sep-2008  mjf Sync with HEAD.
 1.35.6.2 02-Jun-2008  mjf Sync with HEAD.
 1.35.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.35.2.1 24-Mar-2008  keiichi sync with head.
 1.37.6.2 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.37.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.37.4.5 11-Aug-2010  yamt sync with head.
 1.37.4.4 11-Mar-2010  yamt sync with head
 1.37.4.3 20-Jun-2009  yamt sync with head
 1.37.4.2 04-May-2009  yamt sync with head.
 1.37.4.1 16-May-2008  yamt sync with head.
 1.37.2.1 18-May-2008  yamt sync with head.
 1.38.4.1 19-Oct-2008  haad Sync with HEAD.
 1.40.8.1 23-Jul-2009  jym Sync with HEAD.
 1.42.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.45.6.1 18-Feb-2012  mrg merge to -current.
 1.45.2.1 17-Apr-2012  yamt sync with head
 1.46.20.1 07-Apr-2014  tls Be a little more clear and consistent about harvesting entropy from devices:

1) deprecate RND_FLAG_NO_ESTIMATE

2) define RND_FLAG_COLLECT_TIME, RND_FLAG_COLLECT_VALUE

3) define RND_FLAG_ESTIMATE_TIME, RND_FLAG_ESTIMATE_VALUE

4) define RND_FLAG_DEFAULT: RND_FLAG_COLLECT_TIME|
RND_FLAG_COLLECT_VALUE|RND_FLAG_ESTIMATE_TIME

5) Make entropy harvesting from environmental sensors a little more generic
and remove it from individual sensor drivers.

6) Remove individual open-coded delta-estimators for values from a few
places in the tree (uvm, environmental drivers).

7) 0 -> RND_FLAG_DEFAULT, actually gather entropy from various drivers
that had stubbed out code, other minor cleanups.
 1.46.6.2 03-Dec-2017  jdolecek update from HEAD
 1.46.6.1 20-Aug-2014  tls Rebase to HEAD as of a few days ago.
 1.47.2.2 16-May-2015  snj Pull up following revision(s) (requested by maxv in ticket #761):
sys/arch/sgimips/dev/scn.c: revision 1.8
sys/arch/sgimips/mace/macekbc.c: revision 1.8
sys/dev/hpc/hpcfb.c: revision 1.59
sys/dev/i2c/i2c.c: revision 1.47
sys/dev/ic/mfi.c: revision 1.57
sys/dev/if_ndis/if_ndis_pci.c: revision 1.21
sys/dev/sysmon/sysmon_power.c: revisions 1.50-1.52
sys/dev/usb/umass_isdata.c: revision 1.31
fix double free, found by Brainy.
--
Free cmd on error if we allocated it.
Found by Brainy, reported by maxv@.
--
don't leak rl (but there are other leaks), found by Brainy.
--
Free ped if we can't hand it to the power daemon.
Found by Brainy, reported by maxv@.
--
don't forget to free the dictionary.
--
another missing free dict.
--
fix leak, found by Brainy.
--
don't malloc a tiny, fixed size buffer to scribble into, then not use it
and never free it either
found by Brainy
--
malloc() -> kmem_alloc() for private data, also kmem_free() them if we
don't finish attaching for whatever reason
found by Brainy
 1.47.2.1 12-Jan-2015  snj Pull up following revision(s) (requested by bouyer in ticket #415):
share/man/man9/sysmon_pswitch.9: revision 1.6
sys/dev/pci/if_wpi.c: revision 1.70, 1.71
sys/dev/pci/if_wpivar.h: revision 1.19
sys/dev/sysmon/sysmon_power.c: revision 1.49
sys/sys/power.h: revision 1.20
As proposed on tech-net@, introduce a new switch type, PSWITCH_TYPE_RADIO,
to be used to report to userland hardware radio switch changes.
powerd(8) will call a "radio_button" script to handle the event.
This script can e.g. start or stop wpa_supplicant.
Update wpi(4) to report PSWITCH_TYPE_RADIO events to sysmon.
--
Take sc_rsw_mtx before calling wpi_getrfkill() from wpi_init(),
Problem reported and patch tested by chris at chriswareham.net
 1.48.2.4 27-Dec-2015  skrll Sync with HEAD (as of 26th Dec)
 1.48.2.3 22-Sep-2015  skrll Sync with HEAD
 1.48.2.2 06-Jun-2015  skrll Sync with HEAD
 1.48.2.1 06-Apr-2015  skrll Sync with HEAD
 1.57.10.1 10-Sep-2019  martin Pull up following revision(s) (requested by maxv in ticket #1371):

sys/dev/sysmon/sysmon_power.c: revision 1.61

Fix info leak, not all of 'pev' is initialized.
 1.58.4.3 13-Apr-2020  martin Mostly merge changes from HEAD upto 20200411
 1.58.4.2 08-Apr-2020  martin Merge changes from current as of 20200406
 1.58.4.1 10-Jun-2019  christos Sync with HEAD
 1.58.2.9 22-Jan-2019  pgoyette Convert the MODULE_{,VOID_}HOOK_CALL macros to do everything in-line
rather than defining an intermediate hook##call function. Almost
all of the hooks are called only once, and although we lose the
ability of doing things like

if (MODULE_HOOK_CALL(...) == 0) ...

we simplify things quite a bit. With this change, we no longer need
to have both declaration and definition macros, and the definition
no longer needs to have both prototype argument list and a "real"
argument list.

FWIW, the above if now needs to written as

int ret;

MODULE_HOOK_CALL(..., ret);
if (ret == 0) ...

with appropriate use of braces {}.
 1.58.2.8 21-Jan-2019  pgoyette No need to declare the hook_call() function for void hooks. So
remove and simplify.
 1.58.2.7 14-Jan-2019  pgoyette Create a variant of the HOOK macros that handles hook routines of
type void, and use them where appropriate.
 1.58.2.6 13-Jan-2019  pgoyette Remove the HOOK2 versions of the MODULE_HOOK macros. There were
only a few uses, and using them led to some lack of clarity in the
code. Instead, we now use two separate hooks, with names that
make it clear(er) what we're doing.

This also positions us to start unraveling some of the rtsock_50
mess, which will need (at least) five hooks.
 1.58.2.5 29-Sep-2018  pgoyette In MODULE_HOOK_CALL_DECL we don't need to provide the actual argument
list for calling the hook function, nor do we need to provide the
default value (for when the hook has not been set).
 1.58.2.4 18-Sep-2018  pgoyette The COMPAT_HOOK macros were renamed to MODULE_HOOK, adjust all callers
 1.58.2.3 18-Sep-2018  pgoyette Split the COMPAT_CALL_HOOK to separate the declaration from the
implementation. Some hooks are called from multiple source files,
and the old method resulted in duplicate implementations.

Implement MP-safe hooks for the usb_subr_30 code. Pass the helper
functions as arguments to the compat code so it does not have to
determine if the kernel contains usb code.
 1.58.2.2 17-Sep-2018  pgoyette Adapt (most of) the indirect function pointers to the new MP-safe
mechanism. Still remaining are the compat_netbsd32 stuff, and
some usb subroutines.
 1.58.2.1 27-Mar-2018  pgoyette Create a compat_40 module
 1.60.4.1 01-Sep-2019  martin Pull up following revision(s) (requested by maxv in ticket #128):

sys/dev/sysmon/sysmon_power.c: revision 1.61

Fix info leak, not all of 'pev' is initialized.
 1.65.2.1 03-Jan-2021  thorpej Sync w/ HEAD.
 1.23 31-Dec-2021  riastradh sysmon(9): Fix mistake in sysmon_task_queue_barrier.

Need to allocate the task structure with malloc(9), not on the stack;
the task thread will free it with free(9). Forgot to squash this
into previous commit.
 1.22 31-Dec-2021  riastradh sysmon(9): New sysmon_task_queue_barrier(pri) function.

This waits for the completion of all tasks at priority pri or lower
that are currently queued at the time of the call.
 1.21 31-Dec-2021  riastradh sysmon: Delete trailing whitespace. No functional change intended.
 1.20 08-Feb-2018  dholland Typos.
 1.19 28-Apr-2015  martin sysmon_task_queue_sched needs to RUN_ONCE(tq_preinit) as well, it can
be called from sysmon_envsys_register() early.
 1.18 27-Apr-2015  pgoyette Replace a home-grown run-once implementation with the real RUN_ONCE()
 1.17 24-Apr-2015  pgoyette This really does need to return an error code, so change the
sysmon_task_queue_fini() function to have a non-void type.
 1.16 24-Apr-2015  agc It's a function returning void; don't return an error code.
 1.15 23-Apr-2015  pgoyette Modularize sysmon and its components
 1.14 05-Sep-2008  gmcgarry branches: 1.14.38; 1.14.58;
Rearrange code in the sysmon queue execution thread to simplify locking logic.
Removes unreachable code warning.
 1.13 06-Apr-2008  xtraeme branches: 1.13.4; 1.13.6; 1.13.10;
Pass KTHREAD_MPSAFE to kthread_create(). I've been testing this for
some weeks in both UP and SMP without any issue so far.
 1.12 04-Jan-2008  ad branches: 1.12.6;
Start detangling lock.h from intr.h. This is likely to cause short term
breakage, but the mess of dependencies has been regularly breaking the
build recently anyhow.
 1.11 05-Dec-2007  ad branches: 1.11.4;
Match the docs: MUTEX_DRIVER/SPIN are now only for porting code written
for Solaris.
 1.10 21-Jul-2007  xtraeme branches: 1.10.4; 1.10.6; 1.10.12; 1.10.14; 1.10.16;
Convert sysmon_taskqueue to use mutex(9) and condvar(9) and initialize
them in init_main.c via sysmon_task_queue_preinit().

Reviewed and ok by ad@.
 1.9 09-Jul-2007  ad branches: 1.9.2;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.8 27-Jun-2007  xtraeme Use aprint_debug rather than printf for the annoying message
"WARNING: Callback scheduled before sysmon task queue thread present.".
 1.7 16-Nov-2006  christos branches: 1.7.8; 1.7.10;
__unused removal on arguments; approved by core.
 1.6 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.5 11-Dec-2005  christos branches: 1.5.20; 1.5.22;
merge ktrace-lwp.
 1.4 29-May-2005  christos branches: 1.4.2;
- add const
- avoid variable shadowing.
 1.3 06-Sep-2003  christos Rename the thread command name so that it fits on top's display.
 1.2 14-Jul-2003  lukem add missing __KERNEL_RCSID()
 1.1 20-Apr-2003  thorpej branches: 1.1.2;
Generic "task queue" for sysmon back-ends. This provides a thread
context for callbacks that require it in order to handle sysmon-related
events.

Adapted from the "OsdSchedule" bits of the ACPI code.
 1.1.2.4 10-Nov-2005  skrll Sync with HEAD. Here we go again...
 1.1.2.3 21-Sep-2004  skrll Fix the sync with head I botched.
 1.1.2.2 18-Sep-2004  skrll Sync with HEAD.
 1.1.2.1 03-Aug-2004  skrll Sync with HEAD
 1.4.2.3 21-Jan-2008  yamt sync with head
 1.4.2.2 07-Dec-2007  yamt sync with head
 1.4.2.1 03-Sep-2007  yamt sync with head.
 1.5.22.2 10-Dec-2006  yamt sync with head.
 1.5.22.1 22-Oct-2006  yamt sync with head
 1.5.20.1 18-Nov-2006  ad Sync with head.
 1.7.10.1 11-Jul-2007  mjf Sync with head.
 1.7.8.5 20-Aug-2007  ad Sync with HEAD.
 1.7.8.4 15-Jul-2007  ad Sync with head.
 1.7.8.3 13-May-2007  ad - Pass the error number and residual count to biodone(), and let it handle
setting error indicators. Prepare to eliminate B_ERROR.
- Add a flag argument to brelse() to be set into the buf's flags, instead
of doing it directly. Typically used to set B_INVAL.
- Add a "struct cpu_info *" argument to kthread_create(), to be used to
create bound threads. Change "bool mpsafe" to "int flags".
- Allow exit of LWPs in the IDL state when (l != curlwp).
- More locking fixes & conversion to the new API.
 1.7.8.2 10-Apr-2007  ad Nuke the deferred kthread creation stuff, as it's no longer needed.
Pointed out by thorpej@.
 1.7.8.1 09-Apr-2007  ad - Add two new arguments to kthread_create1: pri_t pri, bool mpsafe.
- Fork kthreads off proc0 as new LWPs, not new processes.
 1.9.2.1 15-Aug-2007  skrll Sync with HEAD.
 1.10.16.2 21-Jul-2007  xtraeme Convert sysmon_taskqueue to use mutex(9) and condvar(9) and initialize
them in init_main.c via sysmon_task_queue_preinit().

Reviewed and ok by ad@.
 1.10.16.1 21-Jul-2007  xtraeme file sysmon_taskq.c was added on branch matt-mips64 on 2007-07-21 23:15:17 +0000
 1.10.14.1 08-Dec-2007  ad Sync with head.
 1.10.12.2 18-Feb-2008  mjf Sync with HEAD.
 1.10.12.1 08-Dec-2007  mjf Sync with HEAD.
 1.10.6.1 09-Jan-2008  matt sync with HEAD
 1.10.4.1 09-Dec-2007  jmcneill Sync with HEAD.
 1.11.4.1 08-Jan-2008  bouyer Sync with HEAD
 1.12.6.2 28-Sep-2008  mjf Sync with HEAD.
 1.12.6.1 02-Jun-2008  mjf Sync with HEAD.
 1.13.10.1 19-Oct-2008  haad Sync with HEAD.
 1.13.6.1 18-Sep-2008  wrstuden Sync with wrstuden-revivesa-base-2.
 1.13.4.1 04-May-2009  yamt sync with head.
 1.14.58.1 06-Jun-2015  skrll Sync with HEAD
 1.14.38.1 03-Dec-2017  jdolecek update from HEAD
 1.5 31-Dec-2021  riastradh sysmon(9): New sysmon_task_queue_barrier(pri) function.

This waits for the completion of all tasks at priority pri or lower
that are currently queued at the time of the call.
 1.4 27-Apr-2015  pgoyette Replace a home-grown run-once implementation with the real RUN_ONCE()
 1.3 24-Apr-2015  pgoyette This really does need to return an error code, so change the
sysmon_task_queue_fini() function to have a non-void type.
 1.2 21-Jul-2007  xtraeme branches: 1.2.38; 1.2.76; 1.2.96;
Convert sysmon_taskqueue to use mutex(9) and condvar(9) and initialize
them in init_main.c via sysmon_task_queue_preinit().

Reviewed and ok by ad@.
 1.1 20-Apr-2003  thorpej branches: 1.1.18; 1.1.60; 1.1.70;
Generic "task queue" for sysmon back-ends. This provides a thread
context for callbacks that require it in order to handle sysmon-related
events.

Adapted from the "OsdSchedule" bits of the ACPI code.
 1.1.70.1 15-Aug-2007  skrll Sync with HEAD.
 1.1.60.1 20-Aug-2007  ad Sync with HEAD.
 1.1.18.1 03-Sep-2007  yamt sync with head.
 1.2.96.1 06-Jun-2015  skrll Sync with HEAD
 1.2.76.1 03-Dec-2017  jdolecek update from HEAD
 1.2.38.2 21-Jul-2007  xtraeme Convert sysmon_taskqueue to use mutex(9) and condvar(9) and initialize
them in init_main.c via sysmon_task_queue_preinit().

Reviewed and ok by ad@.
 1.2.38.1 21-Jul-2007  xtraeme file sysmon_taskq.h was added on branch matt-mips64 on 2007-07-21 23:15:18 +0000
 1.30 31-Dec-2021  riastradh sysmon: Delete trailing whitespace. No functional change intended.
 1.29 14-Dec-2015  pgoyette sysmon's components need to be MODULE_CLASS_DRIVER so they will get
initialized before we configure/initialize any devices that interact
with them.

Thanks, marty!
 1.28 05-Jun-2015  matt Move callout_init to wdog_preinit
 1.27 25-Apr-2015  pgoyette Handle early initialization requirements - thanks martin@ and others
 1.26 23-Apr-2015  pgoyette Modularize sysmon and its components
 1.25 04-Jan-2011  matt branches: 1.25.18; 1.25.36;
add support for autostarting watchdogs (wdog was started by firmware
and can't be disabled). Add critical pool hooks for kernel tickled
watchdogs.
 1.24 16-Dec-2007  dyoung branches: 1.24.28; 1.24.32; 1.24.36;
In sysmon_wdog_unregister(), do not return until all of the watchdog
timer's users are gone. A signal cancels the unregister:
sysmon_wdog_unregister() leaves the watchdog registered.

The only user of sysmon_wdog_unregister() that I can find is in
elansc(4), so this looks like a safe change to make.
 1.23 15-Dec-2007  dyoung Use LIST_FOREACH().
 1.22 05-Dec-2007  ad branches: 1.22.4;
MUTEX_SPIN -> MUTEX_DEFAULT.
 1.21 02-Sep-2007  xtraeme branches: 1.21.6; 1.21.8;
Add <sys/mutex.h> rather than <sys/lock.h>.
 1.20 02-Sep-2007  xtraeme Convert the sysmon watchdog framework to use mutex(9) rather than
simple_locks and initialize them on init_main via sysmon_wdog_init().

All the sysmon code now is cleaned up and doesn't use old style locking.
 1.19 09-Jul-2007  ad branches: 1.19.2; 1.19.6; 1.19.8;
Merge some of the less invasive changes from the vmlocking branch:

- kthread, callout, devsw API changes
- select()/poll() improvements
- miscellaneous MT safety improvements
 1.18 04-Mar-2007  christos branches: 1.18.2; 1.18.4;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.17 19-Feb-2007  ad Back out previous for now. There isn't currently an easy way to set up
shared state before these get attached or used.

From xtraeme@.
 1.16 19-Feb-2007  xtraeme * sysmon_wdog_mtx needs to run at IPL_SOFTCLOCK.
* replace SYSMON_WDOG_{LOCK,UNLOCK} macros with mutex_{enter,exit}

reminded by ad@.
 1.15 18-Feb-2007  xtraeme Replace simple_locks with mutexes, reviewed by ad@.
 1.14 16-Nov-2006  christos branches: 1.14.4;
__unused removal on arguments; approved by core.
 1.13 12-Oct-2006  christos - sprinkle __unused on function decls.
- fix a couple of unused bugs
- no more -Wno-unused for i386
 1.12 11-Dec-2005  christos branches: 1.12.20; 1.12.22;
merge ktrace-lwp.
 1.11 12-Jan-2005  drochner branches: 1.11.10;
apply an "& WDOG_MODE_MASK" so that user mode watchdogs started with "-A"
are terminated properly
 1.10 09-Jan-2005  smb Add a software watchdog timer facility. Because this slightly
changes the "tickle" model of wdogctl(8), it was modified as well;
while I was in there, I cleaned up the argument parsing.

The code was reviewed by simonb@.
 1.9 30-Oct-2003  simonb Remove some assigned-to but otherwise unused variables.
 1.8 29-Jun-2003  fvdl branches: 1.8.2;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.7 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.6 30-Dec-2002  explorer remove redundant defines
 1.5 06-Oct-2002  kristerw Correct possible return of uninitialized error variable.
 1.4 31-Jul-2002  simonb Fix WDOGIOC_GTICKLER for userland ticklers.
 1.3 13-Nov-2001  lukem branches: 1.3.8;
add RCSIDs
 1.2 30-Jan-2001  thorpej branches: 1.2.2; 1.2.4; 1.2.6;
Make sure to clear sysmon_armed_wdog once the watchdog timer
is disarmed.
 1.1 05-Nov-2000  thorpej branches: 1.1.2;
Split the envsys and watchdog parts of sysmon into their own
files.
 1.1.2.3 11-Feb-2001  bouyer Sync with HEAD.
 1.1.2.2 22-Nov-2000  bouyer Sync with HEAD.
 1.1.2.1 05-Nov-2000  bouyer file sysmon_wdog.c was added on branch thorpej_scsipi on 2000-11-22 16:04:54 +0000
 1.2.6.1 18-Sep-2001  fvdl Various changes to make cloning devices possible:

* Add an extra argument (struct vnode **) to VOP_OPEN. If it is
not NULL, specfs will create a cloned (aliased) vnode during
the call, and return it there. The caller should release and
unlock the original vnode if a new vnode was returned. The
new vnode is returned locked.

* Add a flag field to the cdevsw and bdevsw structures.
DF_CLONING indicates that it wants a new vnode for each
open (XXX is there a better way? devprop?)

* If a device is cloning, always call the close entry
point for a VOP_CLOSE.


Also, rewrite cons.c to do the right thing with vnodes. Use VOPs
rather then direct device entry calls. Suggested by mycroft@

Light to moderate testing done an i386 system (arch doesn't matter
though, these are MI changes).
 1.2.4.2 06-Sep-2002  jdolecek sync kqueue branch with HEAD
 1.2.4.1 10-Jan-2002  thorpej Sync kqueue branch with -current.
 1.2.2.4 03-Jan-2003  thorpej Sync with HEAD.
 1.2.2.3 18-Oct-2002  nathanw Catch up to -current.
 1.2.2.2 01-Aug-2002  nathanw Catch up to -current.
 1.2.2.1 14-Nov-2001  nathanw Catch up to -current.
 1.3.8.1 29-Aug-2002  gehenna catch up with -current.
 1.8.2.5 17-Jan-2005  skrll Sync with HEAD.
 1.8.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.8.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.8.2.2 03-Aug-2004  skrll Sync with HEAD
 1.8.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.11.10.5 21-Jan-2008  yamt sync with head
 1.11.10.4 07-Dec-2007  yamt sync with head
 1.11.10.3 03-Sep-2007  yamt sync with head.
 1.11.10.2 30-Dec-2006  yamt sync with head.
 1.11.10.1 21-Jun-2006  yamt sync with head.
 1.12.22.2 10-Dec-2006  yamt sync with head.
 1.12.22.1 22-Oct-2006  yamt sync with head
 1.12.20.1 18-Nov-2006  ad Sync with head.
 1.14.4.1 12-Mar-2007  rmind Sync with HEAD.
 1.18.4.1 11-Jul-2007  mjf Sync with head.
 1.18.2.2 09-Oct-2007  ad Sync with head.
 1.18.2.1 01-Jul-2007  ad Adapt to callout API change.
 1.19.8.2 09-Jan-2008  matt sync with HEAD
 1.19.8.1 06-Nov-2007  matt sync with HEAD
 1.19.6.2 09-Dec-2007  jmcneill Sync with HEAD.
 1.19.6.1 03-Sep-2007  jmcneill Sync with HEAD.
 1.19.2.1 03-Sep-2007  skrll Sync with HEAD.
 1.21.8.2 26-Dec-2007  ad Sync with head.
 1.21.8.1 08-Dec-2007  ad Sync with head.
 1.21.6.2 27-Dec-2007  mjf Sync with HEAD.
 1.21.6.1 08-Dec-2007  mjf Sync with HEAD.
 1.22.4.1 02-Jan-2008  bouyer Sync with HEAD
 1.24.36.1 07-Jan-2011  matt Allow kernel ticklers to turn themselves on at boot.
 1.24.32.1 05-Mar-2011  rmind sync with head
 1.24.28.2 15-Feb-2014  matt move sysmon_wdog_setmode prototype to sysmonvar.h
 1.24.28.1 13-May-2010  cliff moveed callout_init call from sysmonopen_wdog to sysmon_wdog_init
 1.25.36.2 27-Dec-2015  skrll Sync with HEAD (as of 26th Dec)
 1.25.36.1 06-Jun-2015  skrll Sync with HEAD
 1.25.18.1 03-Dec-2017  jdolecek update from HEAD
 1.4 23-Apr-2015  pgoyette Modularize sysmon and its components
 1.3 18-Apr-2003  thorpej branches: 1.3.150; 1.3.170;
* Add a generic power management event API, defined in <sys/power.h>.
Right now, only power switch state change events are supported. This
is a work-in-progress.
* Add support to sysmon for delivering power mangement events to userland.
Add poll, kqueue, and read entry points to sysmon.
* Adapt ACPI to use the new generic <sys/power.h> event types.

This provides the kernel support for a forthcoming powerd(8) which can
do nice things like gracefully shut the system down when an ACPI power
button is pressed.
 1.2 31-Dec-2002  thorpej Nothing cares about NSYSMON anymore; delete it.
 1.1 05-Nov-2000  thorpej branches: 1.1.2; 1.1.4;
Split the envsys and watchdog parts of sysmon into their own
files.
 1.1.4.1 03-Jan-2003  thorpej Sync with HEAD.
 1.1.2.2 22-Nov-2000  bouyer Sync with HEAD.
 1.1.2.1 05-Nov-2000  bouyer file sysmonconf.h was added on branch thorpej_scsipi on 2000-11-22 16:04:55 +0000
 1.3.170.1 06-Jun-2015  skrll Sync with HEAD
 1.3.150.1 03-Dec-2017  jdolecek update from HEAD
 1.53 31-Dec-2021  riastradh sysmon(9): Document lock order.
 1.52 31-Dec-2021  riastradh sysmon(9): Fix callout/thread synchronization.

Callout may ONLY take sme_work_mtx, at IPL_SOFTCLOCK; MUST NOT touch
sme_mtx at IPL_NONE. All state the callout needs is serialized by
sme_work_mtx now:

- calls to sme_schedule_callout
- calls to sme_schedule_halt
- struct sysmon_envsys::sme_events_timeout
- struct sysmon_envsys::sme_events_list
- struct sysmon_envsys::sme_callout_state
- struct envsys_data::flags
=> yes, this is a little silly -- used for ENVSYS_FNEED_REFRESH
=> should maybe separate the static driver-defined features from
the state flags needed by sysmon_envsys but not important now

Sleeping under sme_work_mtx (except on other adaptive locks at
IPL_SOFTCLOCK) is forbidden. Calling out to the driver under
sme_work_mtx is forbidden.

This should properly fix:

https://mail-index.netbsd.org/tech-kern/2015/10/14/msg019511.html
PR kern/56592
 1.51 31-Dec-2021  riastradh sysmon: Delete trailing whitespace. No functional change intended.
 1.50 11-Sep-2017  pgoyette Improve tracking of the state of an event's callout, and protect all
queries or modifications of the state with the sme_mtx mutex.

Detach the rndsrc before re-attaching it (with potentially new values).

Clean up some lock-ordering issues.

And a couple of KNF issues for good measure!

Should address PR kern/52533

XXX Pullup-8 along with the previous fixes from msaitoh@
XXX http://mail-index.netbsd.org/source-changes/2017/09/06/msg088028.html
~
~
 1.49 23-Apr-2015  pgoyette branches: 1.49.10;
Modularize sysmon and its components
 1.48 18-Apr-2015  mlelstv use unsigned type for flag bits.
 1.47 13-Apr-2015  riastradh Convert sys/dev to use <sys/rndsource.h>.
 1.46 14-Mar-2015  hannken Add a counter of busy events and stop enqueueing more work if a device is busy.
Protect this counter with a new short time lock "sme_work_mtx" and
keep "sme_mtx" as long time lock.

Removes a deadlock where an active event holds "sme_mtx", the callout
"sme_events_check" blocks on "sme_mtx" and callout processing stops.
 1.45 22-Nov-2014  ozaki-r branches: 1.45.2;
Kill sme_callout_mtx and use sme_mtx instead

We can use sme_mtx for the callout as well. Actually we should do so
because sme_events_list and some other data that are touched in the
callout should be protected by sme_mtx, not sme_callout_mtx.

Discussed with riastradh@ in http://mail-index.netbsd.org/tech-kern/2014/11/11/msg017956.html
 1.44 11-Dec-2012  pgoyette branches: 1.44.12;
Replace a couple of many-line #define with equivalent code loops.

No functional change intended, and atf tests (using swsensor(4)) still
pass 100%
 1.43 16-Jul-2012  pgoyette branches: 1.43.2;
Extend previous changes so that even sensors which don't use a refresh()
callback to update the value can be polled to provide rnd(4) entropy.
 1.42 15-Jul-2012  pgoyette If a sensor is flagged as capable of providing rnd(4) with entropy,
hook the sensor into rnd subsystem, and make sure we periodically
refresh the sensor whether or not it is being actively monitored.
 1.41 04-Jun-2011  pgoyette branches: 1.41.2; 1.41.8;
Since nothing actually records or maintains an average sensor value,
remove value_avg from the prop_dict. We can't completely remove it
because there's one driver that uses this field for its own private
purposes, so for now we just rename the member.
 1.40 04-Jan-2011  matt branches: 1.40.2; 1.40.6;
add support for autostarting watchdogs (wdog was started by firmware
and can't be disabled). Add critical pool hooks for kernel tickled
watchdogs.
 1.39 11-Apr-2010  pgoyette Use a typedef to define the prototype of the per-sensor callback.

Update manpage for the prototype, and fix a fubar'd Cross-reference.
 1.38 01-Apr-2010  pgoyette Permit creation of an event-monitor for alarm limits even if the limits
are not currently set (and, in case of battery capacity sensors, if there
is no value_max). Ensure that such an event-monitor does not trigger the
delivery of any actual events.

Provide a mechanism for sensors to set their limits at time other than
system startup (for example, when a battery is inserted).

This allows us to boot a system with a battery missing, install the
battery some time later, and automatically monitor it without requiring
any user intervention to create the event-monitor. (The actual changes
for battery sensors to use this new capability will come later.)
 1.37 27-Mar-2010  pgoyette Constify the args to sysmon_envsys_foreach_sensor()'s callback function.
 1.36 27-Mar-2010  pgoyette Fix the locking protocol in sysmon_envsys_foreach_sensor(), and add an
additional argument to control whether or not the sensors are refreshed
before invoking the callback routine.
 1.35 26-Mar-2010  pgoyette Add a routine to iterate over all the sensors on the system. This will
be needed to update acpiapm_get_powstat() which is currently slightly
broken due to recent rearrangement of acpi_bat(4)'s sensors. (This
approach is in lieu of exporting proplists between kernel entities, and
avoids exposing some internal sysmon_envsys details.)
 1.34 19-Mar-2010  pgoyette Modify previous commit. The routine to deliver events is intended to be
exported to device drivers, so rename it and move the declaration to
<dev/sysmon/sysmonvar.h>
 1.33 14-Mar-2010  pgoyette branches: 1.33.2;
Retire the 'monitor' member of the envsys_data_t. It was only used in
one place, and functioned as a logical OR of the ENVSYS_FMON* flag bits.
 1.32 28-Feb-2010  pgoyette Move definitions of internal data structures and flag bits to avoid
exposing them to user-land.
 1.31 14-Feb-2010  pgoyette Remove the flags member of the limits structure. Its value was only
meaningful for a few limited function calls; all persitent storage
of limit-present flags is maintained in the edata->upropset member.
 1.30 31-Jan-2010  martin branches: 1.30.2;
Add a convenience function to find the maximum value currently reported
by a set (selected via a passed callback predicate) of sensors.
This provides an easy way to query the current temperature of a thermal
zone, for example, from within the kernel - assuming the caller knows
the topology.
 1.29 14-Jun-2009  pgoyette Ooops - {get,set}_limits() need to be sensor-specific. Pass an extra
argument to them to identify the individual sensor being processed.
 1.28 13-Jun-2009  pgoyette Add capability to send sensor limit values to the driver so they can
be programmed into device registers. This way we can let the hardware
help us out instead of having to always compare the value against each
limit. (Driver updates for some sensors to take advantage of this
capability will be forthcoming.)
 1.27 03-Jun-2008  jmcneill branches: 1.27.12; 1.27.16; 1.27.20;
Add SME_POLL_ONLY flag, which informs sme to only refresh the sensor when
it is polled (and not from GTREDATA).
 1.26 04-Apr-2008  hannken branches: 1.26.2; 1.26.4; 1.26.6;
Update includes. Builds again on sparc64.

Ok: Juan Romero Pardines <xtraeme@netbsd.org>
 1.25 01-Apr-2008  xtraeme Introduce per-device locking/synchronization and maintain only a
global mutex for the linked list of devices and the global proplib
dictionary.

This has improved locking contention a lot when multiple devices with
multiple monitoring events are running:

New:

0.30 35 0.33 sme_global_mtx sysmonioctl_envsys+28b
0.10 10 0.11 00000000cd97feac sysmon_envsys_acquire+4c
0.08 6 0.09 00000000cd97feac sme_update_dictionary+24f
0.01 4 0.01 00000000cd97feac sme_events_worker+2f
0.01 10 0.01 00000000cd97fe2c sysmon_envsys_acquire+4c
0.00 1 0.00 00000000cd97fe2c sysmon_envsys_release+3b
28.38 94 9.16 sme_global_mtx sysmonioctl_envsys+28b
4.54 74 1.47 00000000cd97fe2c sysmon_envsys_acquire+4c
0.06 3 0.02 00000000cd97fe2c sysmon_envsys_release+3b
0.03 1 0.01 00000000cd97fe2c sme_events_worker+2f
1.40 19 0.45 00000000cd97bee4 sysmon_envsys_acquire+4c

Old:

4.25 313 4.74 sme_mtx <all>
3.12 185 3.49 sme_mtx sme_events_worker+21
1.12 128 1.25 sme_mtx sysmonioctl_envsys+29b
34.75 1423 59.52 sme_mtx <all>
22.08 477 37.82 sme_mtx sysmonioctl_envsys+29b
12.67 946 21.70 sme_mtx sme_events_worker+21
 1.24 23-Mar-2008  xtraeme Fix some problems reported by <drochner> a while ago on tech-kern:

sme_events_worker: skip sensors with invalid state.
sme_acadapter_check:

* use a boolean to check if an SME_CLASS_ACADAPTER was found.
* Always refresh sensor data before checking value_cur.

Introduce the SME_INIT_REFRESH flag. If a device sets this it will mean
that after interrupts are enabled in the autoconf(9) process, sensor
data will be refreshed once.
 1.23 16-Dec-2007  dyoung branches: 1.23.6;
In sysmon_wdog_unregister(), do not return until all of the watchdog
timer's users are gone. A signal cancels the unregister:
sysmon_wdog_unregister() leaves the watchdog registered.

The only user of sysmon_wdog_unregister() that I can find is in
elansc(4), so this looks like a safe change to make.
 1.22 20-Nov-2007  xtraeme branches: 1.22.2; 1.22.6;
After comments from Andrew Doran:

- The mutex for the callout handler must run at IPL_SOFTCLOCK.
- Just stop the callout in sysmon_envsys_unregister() and don't wait
for the callout to finish.
 1.21 16-Nov-2007  xtraeme Extend the envsys2 API (one more time, sorry) as defined in:

http://mail-index.netbsd.org/tech-kern/2007/11/09/0001.html

sysmon_envsys_create() and sysmon_envsys_destroy() were added to
create/destroy sysmon_envsys objects (and its TAILQ/LIST for sensors/events).

sysmon_envsys_sensor_attach() and sysmon_envsys_sensor_detach() were
added to attach/detach sensors to a specified sysmon_envsys device.

The events framework is now per device and configurable via the
ENVSYS_SETDICTIONARY ioctl or /etc/envsys.conf and envstat(8).

Update all users and documentation to reflect these changes.
 1.20 03-Nov-2007  xtraeme branches: 1.20.2;
Remove sysmon_envsys_find() proto, it's defined on sysmon_envsysvar.h.
 1.19 10-Oct-2007  xtraeme branches: 1.19.2;
Fix the problems described in:
http://mail-index.netbsd.org/current-users/2007/07/16/0012.html

- Introduce sme_class into the sysmon_envsys struct to specify a
class; currently there are two classes: SME_CLASS_ACADAPTER and
SME_CLASS_BATTERY.

- Add a new envsys event: PENVSYS_EVENT_LOW_POWER that is reached when
all SME_CLASS_BATTERY devices are in CRITICAL/LOW state and there's not
any SME_CLASS_ACADAPTER connected.

- Add the 'low-power' event into the sensor_battery script that will
shutdown the system gracefully via 'shutdown -p'. If powerd(8) is
not running, cpu_reboot(9) with RB_POWERDOWN is used.

- Make acpiacad(4) a SME_CLASS_ACADAPTER device and acpibat(4) a
SME_CLASS_BATTERY device.

Update the documentation accordingly to these changes.
 1.18 08-Sep-2007  xtraeme branches: 1.18.2;
- Build unconditionally compatibility code, there's no point to have it
disabled, because some drivers depend in the old code yet.
- Use a new mutex for sysmon_envsys_next_sensor_index (used in
compatibility code), otherwise bad things happen with LKMs. Thanks
to this, the hack for LKMs has been removed.
- Check in advance if the driver doesn't exist already on the list
before adding the sensors in the dictionary.
- Don't forget to call sme_event_unregister_all() if
sysmon_envsys_unregister() fails after adding the array into the global
dictionary or when creating sensors.
- Modify and add some DPRINTFs.

The lm(4) lkm works without known problems registering and unregistering
it multiple times.
 1.17 02-Sep-2007  xtraeme Convert the sysmon watchdog framework to use mutex(9) rather than
simple_locks and initialize them on init_main via sysmon_wdog_init().

All the sysmon code now is cleaned up and doesn't use old style locking.
 1.16 30-Aug-2007  xtraeme More misc changes for sysmon_envsys(9):

- Add the SLIST for sensor descriptions and sme_uniqsensors into the
struct sysmon_envsys (it's per device now).
- Use only one common struct with three members for the static tables
(there's no need to have different structs just for them).
- While initializing/destroying the events framework, use the
strategy specified by Andrew Doran in:
http://mail-index.netbsd.org/tech-kern/2007/06/21/0025.html).

(forgot in previous)
 1.15 01-Jul-2007  xtraeme branches: 1.15.2; 1.15.6; 1.15.8;
Imported envsys 2, a brief description of the new features:
(Part 1: API)

* Support for detachable sensors.
* Cleaned up the API for simplicity and efficiency.
* Ability to send capacity/critical/warning events to powerd(8).
* Adapted all the code to the new locking order.
* Compatibility with the old envsys API: the ENVSYS_GTREINFO
and ENVSYS_GTREDATA ioctl(2)s are supported.
* Added support for a 'dictionary based communication channel' between
sysmon_power(9) and powerd(8), that means there is no 32 bytes event
size restriction anymore.
* Binary compatibility with old envstat(8) and powerd(8) via COMPAT_40.
* All drivers with the n^2 gtredata bug were fixed, PR kern/36226.

Tested by:

blymn: smsc(4).
bouyer: ipmi(4), mfi(4).
kefren: ug(4).
njoly: viaenv(4), adt7463.c.
riz: owtemp(4).
xtraeme: acpiacad(4), acpibat(4), acpitz(4), aiboost(4), it(4), lm(4).
 1.14 08-May-2007  xtraeme Remove duplicate sysmonioctl_envsys() prototype.
 1.13 04-Mar-2007  christos branches: 1.13.2; 1.13.4;
Kill caddr_t; there will be some MI fallout, but it will be fixed shortly.
 1.12 08-Jul-2006  christos branches: 1.12.10;
expose the ioctl internal function now used by the acpi_apm code.
 1.11 11-Dec-2005  christos branches: 1.11.4; 1.11.8; 1.11.16;
merge ktrace-lwp.
 1.10 11-Aug-2003  yamt branches: 1.10.16;
do wakeup() only when someone is waiting for us.
 1.9 11-Aug-2003  yamt introduce SME_FLAG_BUSY and set it during operations
instead of keeping sysmon_envsys_list_slock spinlock held
because some drivers might sleep in sysmon_envsys ops,

XXX sysmon_envsys_lock is now redundant
 1.8 29-Jun-2003  fvdl branches: 1.8.2;
Back out the lwp/ktrace changes. They contained a lot of colateral damage,
and need to be examined and discussed more.
 1.7 28-Jun-2003  darrenr Pass lwp pointers throughtout the kernel, as required, so that the lwpid can
be inserted into ktrace records. The general change has been to replace
"struct proc *" with "struct lwp *" in various function prototypes, pass
the lwp through and use l_proc to get the process pointer when needed.

Bump the kernel rev up to 1.6V
 1.6 18-Apr-2003  thorpej * Add a generic power management event API, defined in <sys/power.h>.
Right now, only power switch state change events are supported. This
is a work-in-progress.
* Add support to sysmon for delivering power mangement events to userland.
Add poll, kqueue, and read entry points to sysmon.
* Adapt ACPI to use the new generic <sys/power.h> event types.

This provides the kernel support for a forthcoming powerd(8) which can
do nice things like gracefully shut the system down when an ACPI power
button is pressed.
 1.5 17-Apr-2003  thorpej Start at a power management framework for sysmon. Right now we just
provide some VERY basic support for power/sleep buttons and lid switches;
if someone presses the power button, shut down the system semi-gracefully.

Eventually, we will send events for all types of button/lid events down
to a userland power management daemon, which will be able to define a
separate policy for each button/switch.
 1.4 10-Apr-2003  thorpej Group things in this file a bit more logically.
 1.3 05-Nov-2000  thorpej branches: 1.3.2; 1.3.8;
Split the envsys and watchdog parts of sysmon into their own
files.
 1.2 04-Nov-2000  thorpej Add a watchog timer framework to the system monitor.
 1.1 24-Jun-2000  thorpej branches: 1.1.2;
Add a clearing-house pseudo-device for system monitoring devices
such as the LM78 and VT82C686A (and eventually ACPI). Multiple
sensor devices can be hooked registered with `sysmon', and eventually
sysmon will also handle hardware (and software) watchdog timers.

Convert the `lm' and `viaenv' drivers to the new interface.
 1.1.2.2 30-Jul-2000  bouyer Pull up (requested by thorpej):
lm_pnpbios.c 1.2->1.3
files 1.374->1.375
nslm7x.c 1.3->1.4
nslm7xvar.h 1.2->1.3
lm_isa.c 1.2->1.3
files.pci 1.96->1.97
viaenv.c 1.2->1.3
sysmon.c 1.1
sysmonvar.h 1.1

Add a clearing-house pseudo-device for system monitoring devices
such as the LM78 and VT82C686A (and eventually ACPI). Multiple
sensor devices can be hooked registered with `sysmon', and eventually
sysmon will also handle hardware (and software) watchdog timers.

Convert the `lm' and `viaenv' drivers to the new interface.
 1.1.2.1 24-Jun-2000  bouyer file sysmonvar.h was added on branch netbsd-1-5 on 2000-07-30 17:54:16 +0000
 1.3.8.1 18-Sep-2001  fvdl Various changes to make cloning devices possible:

* Add an extra argument (struct vnode **) to VOP_OPEN. If it is
not NULL, specfs will create a cloned (aliased) vnode during
the call, and return it there. The caller should release and
unlock the original vnode if a new vnode was returned. The
new vnode is returned locked.

* Add a flag field to the cdevsw and bdevsw structures.
DF_CLONING indicates that it wants a new vnode for each
open (XXX is there a better way? devprop?)

* If a device is cloning, always call the close entry
point for a VOP_CLOSE.


Also, rewrite cons.c to do the right thing with vnodes. Use VOPs
rather then direct device entry calls. Suggested by mycroft@

Light to moderate testing done an i386 system (arch doesn't matter
though, these are MI changes).
 1.3.2.3 22-Nov-2000  bouyer Sync with HEAD.
 1.3.2.2 20-Nov-2000  bouyer Update thorpej_scsipi to -current as of a month ago
A i386 GENERIC kernel compiles without the siop, ahc and bha drivers
(will be updated later). i386 IDE/ATAPI and ncr work, as well as
sparc/esp_sbus. alpha should work as well (untested yet).
siop, ahc and bha will be updated once I've updated the branch to current
-current, as well as machine-dependant code.
 1.3.2.1 05-Nov-2000  bouyer file sysmonvar.h was added on branch thorpej_scsipi on 2000-11-20 11:43:12 +0000
 1.8.2.4 21-Sep-2004  skrll Fix the sync with head I botched.
 1.8.2.3 18-Sep-2004  skrll Sync with HEAD.
 1.8.2.2 03-Aug-2004  skrll Sync with HEAD
 1.8.2.1 02-Jul-2003  darrenr Apply the aborted ktrace-lwp changes to a specific branch. This is just for
others to review, I'm concerned that patch fuziness may have resulted in some
errant code being generated but I'll look at that later by comparing the diff
from the base to the branch with the file I attempt to apply to it. This will,
at the very least, put the changes in a better context for others to review
them and attempt to tinker with removing passing of 'struct lwp' through
the kernel.
 1.10.16.8 24-Mar-2008  yamt sync with head.
 1.10.16.7 21-Jan-2008  yamt sync with head
 1.10.16.6 07-Dec-2007  yamt sync with head
 1.10.16.5 15-Nov-2007  yamt sync with head.
 1.10.16.4 27-Oct-2007  yamt sync with head.
 1.10.16.3 03-Sep-2007  yamt sync with head.
 1.10.16.2 30-Dec-2006  yamt sync with head.
 1.10.16.1 21-Jun-2006  yamt sync with head.
 1.11.16.1 13-Jul-2006  gdamore Merge from HEAD.
 1.11.8.1 11-Aug-2006  yamt sync with head
 1.11.4.1 09-Sep-2006  rpaulo sync with head
 1.12.10.2 17-May-2007  yamt sync with head.
 1.12.10.1 12-Mar-2007  rmind Sync with HEAD.
 1.13.4.1 11-Jul-2007  mjf Sync with head.
 1.13.2.4 12-Oct-2007  ad Sync with head.
 1.13.2.3 09-Oct-2007  ad Sync with head.
 1.13.2.2 15-Jul-2007  ad Sync with head.
 1.13.2.1 27-May-2007  ad Sync with head.
 1.15.8.2 09-Jan-2008  matt sync with HEAD
 1.15.8.1 06-Nov-2007  matt sync with HEAD
 1.15.6.5 21-Nov-2007  joerg Sync with HEAD.
 1.15.6.4 04-Nov-2007  jmcneill Sync with HEAD.
 1.15.6.3 26-Oct-2007  joerg Sync with HEAD.

Follow the merge of pmap.c on i386 and amd64 and move
pmap_init_tmp_pgtbl into arch/x86/x86/pmap.c. Modify the ACPI wakeup
code to restore CR4 before jumping back into kernel space as the large
page option might cover that.
 1.15.6.2 02-Oct-2007  joerg Sync with HEAD.
 1.15.6.1 03-Sep-2007  jmcneill Sync with HEAD.
 1.15.2.2 10-Sep-2007  skrll Sync with HEAD.
 1.15.2.1 03-Sep-2007  skrll Sync with HEAD.
 1.18.2.1 14-Oct-2007  yamt sync with head.
 1.19.2.3 21-Nov-2007  bouyer Sync with HEAD
 1.19.2.2 18-Nov-2007  bouyer Sync with HEAD
 1.19.2.1 13-Nov-2007  bouyer Sync with HEAD
 1.20.2.3 27-Dec-2007  mjf Sync with HEAD.
 1.20.2.2 08-Dec-2007  mjf Sync with HEAD.
 1.20.2.1 19-Nov-2007  mjf Sync with HEAD.
 1.22.6.1 02-Jan-2008  bouyer Sync with HEAD
 1.22.2.1 26-Dec-2007  ad Sync with head.
 1.23.6.3 05-Jun-2008  mjf Sync with HEAD.

Also fix build.
 1.23.6.2 02-Jun-2008  mjf Sync with HEAD.
 1.23.6.1 03-Apr-2008  mjf Sync with HEAD.
 1.26.6.1 23-Jun-2008  wrstuden Sync w/ -current. 34 merge conflicts to follow.
 1.26.4.4 11-Aug-2010  yamt sync with head.
 1.26.4.3 11-Mar-2010  yamt sync with head
 1.26.4.2 20-Jun-2009  yamt sync with head
 1.26.4.1 04-May-2009  yamt sync with head.
 1.26.2.1 04-Jun-2008  yamt sync with head
 1.27.20.1 07-Jan-2011  matt Allow kernel ticklers to turn themselves on at boot.
 1.27.16.1 15-Feb-2014  matt move sysmon_wdog_setmode prototype to sysmonvar.h
 1.27.12.1 23-Jul-2009  jym Sync with HEAD.
 1.30.2.1 30-Apr-2010  uebayasi Sync with HEAD.
 1.33.2.3 12-Jun-2011  rmind sync with head
 1.33.2.2 05-Mar-2011  rmind sync with head
 1.33.2.1 30-May-2010  rmind sync with head
 1.40.6.1 23-Jun-2011  cherry Catchup with rmind-uvmplock merge.
 1.40.2.1 06-Jun-2011  jruoho Sync with HEAD.
 1.41.8.1 07-Dec-2014  martin Pull up following revision(s) (requested by ozaki-r in ticket #1201):
sys/kern/kern_ktrace.c: revision 1.166
sys/dev/isa/aps.c: revision 1.16
sys/dev/sysmon/sysmonvar.h: revision 1.45
sys/dev/ir/irframe_tty.c: revision 1.60
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.111-1.112 (patch)
sys/dev/pci/pccbb.c: revision 1.207
sys/dev/wscons/wskbd.c: revision 1.135
sys/dev/usb/ohci.c: revision 1.254
sys/net/if_ecosubr.c: revision 1.41
sys/dev/pcmcia/btbc.c: revision 1.17
sys/arch/x86/x86/via_padlock.c: revision 1.23
sys/dev/sdmmc/sdmmc.c: revision 1.23 (patch)
sys/dev/bluetooth/btkbd.c: revision 1.17
sys/dev/bluetooth/bcsp.c: revision 1.25
sys/arch/x86/pci/fwhrng.c: revision 1.8
sys/dev/ic/nslm7x.c: revision 1.61
share/man/man9/callout.9: revision 1.28 (patch)

Replace callout_stop with callout_halt and ensure the callout
is not running before destroying it.
 1.41.2.2 16-Jan-2013  yamt sync with (a bit old) head
 1.41.2.1 30-Oct-2012  yamt sync with head
 1.43.2.2 03-Dec-2017  jdolecek update from HEAD
 1.43.2.1 25-Feb-2013  tls resync with head
 1.44.12.2 06-Apr-2015  snj Pull up following revision(s) (requested by hannken in ticket #666):
sys/dev/sysmon/sysmon_envsys.c: revision 1.129
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.114
sys/dev/sysmon/sysmonvar.h: revision 1.46
Add a counter of busy events and stop enqueueing more work if a device =
is busy.
Protect this counter with a new short time lock "sme_work_mtx" and
keep "sme_mtx" as long time lock.
Removes a deadlock where an active event holds "sme_mtx", the callout
"sme_events_check" blocks on "sme_mtx" and callout processing stops.
 1.44.12.1 01-Dec-2014  martin Pull up following revision(s) (requested by ozaki-r in ticket #279):
sys/kern/kern_ktrace.c: revision 1.166
sys/dev/isa/aps.c: revision 1.16
sys/dev/sysmon/sysmonvar.h: revision 1.45
sys/dev/ir/irframe_tty.c: revision 1.60
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.111
sys/dev/sysmon/sysmon_envsys_events.c: revision 1.112
sys/dev/pci/pccbb.c: revision 1.207
sys/dev/wscons/wskbd.c: revision 1.135
sys/dev/usb/ohci.c: revision 1.254
sys/net/if_ecosubr.c: revision 1.41
sys/dev/pcmcia/btbc.c: revision 1.17
sys/arch/x86/x86/via_padlock.c: revision 1.23
sys/dev/sdmmc/sdmmc.c: revision 1.23
sys/dev/bluetooth/btkbd.c: revision 1.17
sys/dev/bluetooth/bcsp.c: revision 1.25
sys/arch/x86/pci/fwhrng.c: revision 1.8
sys/dev/ic/nslm7x.c: revision 1.61
share/man/man9/callout.9: revision 1.28
Replace callout_stop with callout_halt
In order to call callout_destroy for a callout safely, we have to ensure
the function of the callout is not running and pending. To do so, we should
use callout_halt, not callout_stop.
Discussed with martin@ and riastradh@.
Make it clear that we should use not callout_stop but callout_halt
before callout_destroy
Replace callout_stop with callout_halt
In order to call callout_destroy for a callout safely, we have to ensure
the function of the callout is not running and pending. To do so, we should
use callout_halt, not callout_stop.
In this case, we need to pass an interlock to callout_halt to wait for
the callout complete.
Reviewed by riastradh@.
Kill sme_callout_mtx and use sme_mtx instead
We can use sme_mtx for the callout as well. Actually we should do so
because sme_events_list and some other data that are touched in the
callout should be protected by sme_mtx, not sme_callout_mtx.
Discussed with riastradh@ in
http://mail-index.netbsd.org/tech-kern/2014/11/11/msg017956.html
Replace callout_stop with callout_halt
In order to call callout_destroy for a callout safely, we have to ensure
the function of the callout is not running and pending. To do so, we should
use callout_halt, not callout_stop.
In this case, we need to pass an interlock to callout_halt to wait for
the callout complete. And also we make sure that SME_CALLOUT_INITIALIZED
is unset before calling callout_halt to prevent the callout from calling
callout_schedule. This is the same as what we did in sys/netinet6/mld6.c@1.61.
Reviewed by riastradh@.
 1.45.2.2 06-Jun-2015  skrll Sync with HEAD
 1.45.2.1 06-Apr-2015  skrll Sync with HEAD
 1.49.10.1 23-Sep-2017  snj Pull up following revision(s) (requested by pgoyette in ticket #281):
sys/dev/sysmon/sysmon_envsys.c: 1.140-1.141
sys/dev/sysmon/sysmon_envsys_events.c: 1.120-1.121
sys/dev/sysmon/sysmonvar.h: 1.50
Fixes a problem that some driver(e.g. acpitz(4) or coretemp(5)) which
use sysmon_envsys sleep waiting at "rndsrc" when "drvctl -d".
Don't call rnd_detach_source() in sme_remove_event() which is called
from sme_event_unregister_all(). Instead, call rnd_detach_source() in
sysmon_envsys_sensor_detach() and call sysmon_envsys_sensor_detach()
before sme_event_unregister_sensor(). Each sensor(envsys_data) has each
rnd_src, but some sme_events point to the same rnd_src in a sensor.
Calling rnd_detach_souce() twice with the same rnd_src brokes a reference
count in rnd_src. OK'd by pgoyette@.
--
Improve tracking of the state of an event's callout, and protect all
queries or modifications of the state with the sme_mtx mutex.
Detach the rndsrc before re-attaching it (with potentially new values).
Clean up some lock-ordering issues.
And a couple of KNF issues for good measure!
Should address PR kern/52533

RSS XML Feed