sysmon_envsys_events.c revision 1.13.2.4 1 1.13.2.4 ad /* $NetBSD: sysmon_envsys_events.c,v 1.13.2.4 2007/10/09 13:42:05 ad Exp $ */
2 1.13.2.2 ad
3 1.13.2.2 ad /*-
4 1.13.2.4 ad * Copyright (c) 2007 Juan Romero Pardines.
5 1.13.2.2 ad * All rights reserved.
6 1.13.2.2 ad *
7 1.13.2.2 ad * Redistribution and use in source and binary forms, with or without
8 1.13.2.2 ad * modification, are permitted provided that the following conditions
9 1.13.2.2 ad * are met:
10 1.13.2.2 ad * 1. Redistributions of source code must retain the above copyright
11 1.13.2.2 ad * notice, this list of conditions and the following disclaimer.
12 1.13.2.2 ad * 2. Redistributions in binary form must reproduce the above copyright
13 1.13.2.2 ad * notice, this list of conditions and the following disclaimer in the
14 1.13.2.2 ad * documentation and/or other materials provided with the distribution.
15 1.13.2.2 ad *
16 1.13.2.4 ad * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 1.13.2.4 ad * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 1.13.2.4 ad * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 1.13.2.4 ad * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 1.13.2.4 ad * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 1.13.2.4 ad * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 1.13.2.4 ad * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 1.13.2.4 ad * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 1.13.2.4 ad * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 1.13.2.4 ad * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 1.13.2.2 ad */
27 1.13.2.2 ad
28 1.13.2.2 ad /*
29 1.13.2.2 ad * sysmon_envsys(9) events framework.
30 1.13.2.2 ad */
31 1.13.2.2 ad
32 1.13.2.2 ad #include <sys/cdefs.h>
33 1.13.2.4 ad __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys_events.c,v 1.13.2.4 2007/10/09 13:42:05 ad Exp $");
34 1.13.2.2 ad
35 1.13.2.2 ad #include <sys/param.h>
36 1.13.2.2 ad #include <sys/types.h>
37 1.13.2.2 ad #include <sys/conf.h>
38 1.13.2.2 ad #include <sys/errno.h>
39 1.13.2.2 ad #include <sys/kernel.h>
40 1.13.2.2 ad #include <sys/sysctl.h>
41 1.13.2.2 ad #include <sys/systm.h>
42 1.13.2.2 ad #include <sys/proc.h>
43 1.13.2.2 ad #include <sys/mutex.h>
44 1.13.2.2 ad #include <sys/kmem.h>
45 1.13.2.2 ad #include <sys/callout.h>
46 1.13.2.2 ad
47 1.13.2.4 ad /* #define ENVSYS_DEBUG */
48 1.13.2.2 ad #include <dev/sysmon/sysmonvar.h>
49 1.13.2.2 ad #include <dev/sysmon/sysmon_envsysvar.h>
50 1.13.2.2 ad
51 1.13.2.2 ad struct sme_sensor_event {
52 1.13.2.2 ad int state;
53 1.13.2.2 ad int event;
54 1.13.2.2 ad };
55 1.13.2.2 ad
56 1.13.2.2 ad static const struct sme_sensor_event sme_sensor_event[] = {
57 1.13.2.2 ad { ENVSYS_SVALID, PENVSYS_EVENT_NORMAL },
58 1.13.2.2 ad { ENVSYS_SCRITICAL, PENVSYS_EVENT_CRITICAL },
59 1.13.2.2 ad { ENVSYS_SCRITOVER, PENVSYS_EVENT_CRITOVER },
60 1.13.2.2 ad { ENVSYS_SCRITUNDER, PENVSYS_EVENT_CRITUNDER },
61 1.13.2.2 ad { ENVSYS_SWARNOVER, PENVSYS_EVENT_WARNOVER },
62 1.13.2.2 ad { ENVSYS_SWARNUNDER, PENVSYS_EVENT_WARNUNDER },
63 1.13.2.2 ad { -1, -1 }
64 1.13.2.2 ad };
65 1.13.2.2 ad
66 1.13.2.2 ad static struct workqueue *seewq;
67 1.13.2.2 ad static struct callout seeco;
68 1.13.2.2 ad static bool sme_events_initialized = false;
69 1.13.2.4 ad kmutex_t sme_mtx, sme_event_init_mtx;
70 1.13.2.4 ad kcondvar_t sme_cv;
71 1.13.2.2 ad
72 1.13.2.2 ad /* 10 seconds of timeout for the callout */
73 1.13.2.2 ad static int sme_events_timeout = 10;
74 1.13.2.2 ad static int sme_events_timeout_sysctl(SYSCTLFN_PROTO);
75 1.13.2.2 ad #define SME_EVTIMO (sme_events_timeout * hz)
76 1.13.2.2 ad
77 1.13.2.2 ad /*
78 1.13.2.2 ad * sysctl(9) stuff to handle the refresh value in the callout
79 1.13.2.2 ad * function.
80 1.13.2.2 ad */
81 1.13.2.2 ad static int
82 1.13.2.2 ad sme_events_timeout_sysctl(SYSCTLFN_ARGS)
83 1.13.2.2 ad {
84 1.13.2.2 ad struct sysctlnode node;
85 1.13.2.2 ad int timo, error;
86 1.13.2.2 ad
87 1.13.2.2 ad node = *rnode;
88 1.13.2.2 ad timo = sme_events_timeout;
89 1.13.2.2 ad node.sysctl_data = &timo;
90 1.13.2.2 ad
91 1.13.2.2 ad error = sysctl_lookup(SYSCTLFN_CALL(&node));
92 1.13.2.4 ad if (error || !newp)
93 1.13.2.2 ad return error;
94 1.13.2.2 ad
95 1.13.2.2 ad /* min 1s */
96 1.13.2.2 ad if (timo < 1)
97 1.13.2.2 ad return EINVAL;
98 1.13.2.2 ad
99 1.13.2.2 ad sme_events_timeout = timo;
100 1.13.2.2 ad return 0;
101 1.13.2.2 ad }
102 1.13.2.2 ad
103 1.13.2.2 ad SYSCTL_SETUP(sysctl_kern_envsys_timeout_setup, "sysctl kern.envsys subtree")
104 1.13.2.2 ad {
105 1.13.2.2 ad const struct sysctlnode *node, *envsys_node;
106 1.13.2.2 ad
107 1.13.2.2 ad sysctl_createv(clog, 0, NULL, &node,
108 1.13.2.2 ad CTLFLAG_PERMANENT,
109 1.13.2.2 ad CTLTYPE_NODE, "kern", NULL,
110 1.13.2.2 ad NULL, 0, NULL, 0,
111 1.13.2.2 ad CTL_KERN, CTL_EOL);
112 1.13.2.2 ad
113 1.13.2.2 ad sysctl_createv(clog, 0, &node, &envsys_node,
114 1.13.2.2 ad 0,
115 1.13.2.2 ad CTLTYPE_NODE, "envsys", NULL,
116 1.13.2.2 ad NULL, 0, NULL, 0,
117 1.13.2.2 ad CTL_CREATE, CTL_EOL);
118 1.13.2.2 ad
119 1.13.2.2 ad sysctl_createv(clog, 0, &envsys_node, &node,
120 1.13.2.2 ad CTLFLAG_READWRITE,
121 1.13.2.2 ad CTLTYPE_INT, "refresh_value",
122 1.13.2.2 ad SYSCTL_DESCR("wait time in seconds to refresh "
123 1.13.2.2 ad "sensors being monitored"),
124 1.13.2.2 ad sme_events_timeout_sysctl, 0, &sme_events_timeout, 0,
125 1.13.2.2 ad CTL_CREATE, CTL_EOL);
126 1.13.2.2 ad }
127 1.13.2.2 ad
128 1.13.2.2 ad /*
129 1.13.2.2 ad * sme_event_register:
130 1.13.2.2 ad *
131 1.13.2.4 ad * + Registers a new sysmon envsys event or updates any event
132 1.13.2.4 ad * already in the queue.
133 1.13.2.2 ad */
134 1.13.2.2 ad int
135 1.13.2.4 ad sme_event_register(prop_dictionary_t sdict, envsys_data_t *edata,
136 1.13.2.4 ad const char *drvn, const char *objkey,
137 1.13.2.4 ad int32_t critval, int crittype, int powertype)
138 1.13.2.2 ad {
139 1.13.2.4 ad sme_event_t *see = NULL;
140 1.13.2.4 ad prop_object_t obj;
141 1.13.2.4 ad bool critvalup = false;
142 1.13.2.2 ad int error = 0;
143 1.13.2.2 ad
144 1.13.2.4 ad KASSERT(sdict != NULL || edata != NULL);
145 1.13.2.2 ad
146 1.13.2.4 ad mutex_enter(&sme_mtx);
147 1.13.2.2 ad /*
148 1.13.2.4 ad * check if the event is already on the list and return
149 1.13.2.4 ad * EEXIST if value provided hasn't been changed.
150 1.13.2.2 ad */
151 1.13.2.4 ad LIST_FOREACH(see, &sme_events_list, see_list) {
152 1.13.2.4 ad if (strcmp(edata->desc, see->pes.pes_sensname) == 0)
153 1.13.2.4 ad if (crittype == see->type) {
154 1.13.2.4 ad if (see->critval == critval) {
155 1.13.2.4 ad DPRINTF(("%s: dev=%s sensor=%s type=%d "
156 1.13.2.4 ad "(already exists)\n", __func__,
157 1.13.2.4 ad see->pes.pes_dvname,
158 1.13.2.4 ad see->pes.pes_sensname, see->type));
159 1.13.2.4 ad mutex_exit(&sme_mtx);
160 1.13.2.4 ad return EEXIST;
161 1.13.2.4 ad }
162 1.13.2.4 ad critvalup = true;
163 1.13.2.4 ad break;
164 1.13.2.2 ad }
165 1.13.2.4 ad }
166 1.13.2.4 ad
167 1.13.2.4 ad /*
168 1.13.2.4 ad * Critical condition operation requested by userland.
169 1.13.2.4 ad */
170 1.13.2.4 ad if (objkey && critval && critvalup) {
171 1.13.2.4 ad obj = prop_dictionary_get(sdict, objkey);
172 1.13.2.4 ad if (obj) {
173 1.13.2.4 ad /*
174 1.13.2.4 ad * object is already in dictionary and value
175 1.13.2.4 ad * provided is not the same than we have
176 1.13.2.4 ad * currently, update the critical value.
177 1.13.2.4 ad */
178 1.13.2.4 ad see->critval = critval;
179 1.13.2.4 ad DPRINTF(("%s: sensor=%s type=%d (critval updated)\n",
180 1.13.2.4 ad __func__, edata->desc, see->type));
181 1.13.2.4 ad error = sme_sensor_upint32(sdict, objkey, critval);
182 1.13.2.4 ad mutex_exit(&sme_mtx);
183 1.13.2.4 ad return error;
184 1.13.2.2 ad }
185 1.13.2.2 ad }
186 1.13.2.2 ad
187 1.13.2.4 ad /*
188 1.13.2.4 ad * The event is not in on the list or in a dictionary, create a new
189 1.13.2.4 ad * sme event, assign required members and update the object in
190 1.13.2.4 ad * the dictionary.
191 1.13.2.4 ad */
192 1.13.2.4 ad see = NULL;
193 1.13.2.4 ad see = kmem_zalloc(sizeof(*see), KM_NOSLEEP);
194 1.13.2.4 ad if (see == NULL) {
195 1.13.2.4 ad mutex_exit(&sme_mtx);
196 1.13.2.4 ad return ENOMEM;
197 1.13.2.4 ad }
198 1.13.2.4 ad
199 1.13.2.4 ad see->critval = critval;
200 1.13.2.4 ad see->type = crittype;
201 1.13.2.4 ad (void)strlcpy(see->pes.pes_dvname, drvn,
202 1.13.2.4 ad sizeof(see->pes.pes_dvname));
203 1.13.2.4 ad see->pes.pes_type = powertype;
204 1.13.2.4 ad (void)strlcpy(see->pes.pes_sensname, edata->desc,
205 1.13.2.4 ad sizeof(see->pes.pes_sensname));
206 1.13.2.4 ad see->snum = edata->sensor;
207 1.13.2.4 ad
208 1.13.2.4 ad LIST_INSERT_HEAD(&sme_events_list, see, see_list);
209 1.13.2.4 ad if (objkey && critval) {
210 1.13.2.4 ad error = sme_sensor_upint32(sdict, objkey, critval);
211 1.13.2.4 ad if (error) {
212 1.13.2.4 ad mutex_exit(&sme_mtx);
213 1.13.2.4 ad goto out;
214 1.13.2.4 ad }
215 1.13.2.4 ad }
216 1.13.2.4 ad DPRINTF(("%s: registering dev=%s sensor=%s snum=%d type=%d "
217 1.13.2.2 ad "critval=%" PRIu32 "\n", __func__,
218 1.13.2.2 ad see->pes.pes_dvname, see->pes.pes_sensname,
219 1.13.2.2 ad see->snum, see->type, see->critval));
220 1.13.2.2 ad /*
221 1.13.2.2 ad * Initialize the events framework if it wasn't initialized
222 1.13.2.2 ad * before.
223 1.13.2.2 ad */
224 1.13.2.2 ad mutex_enter(&sme_event_init_mtx);
225 1.13.2.4 ad mutex_exit(&sme_mtx);
226 1.13.2.2 ad if (sme_events_initialized == false)
227 1.13.2.2 ad error = sme_events_init();
228 1.13.2.2 ad mutex_exit(&sme_event_init_mtx);
229 1.13.2.4 ad out:
230 1.13.2.4 ad if (error)
231 1.13.2.4 ad kmem_free(see, sizeof(*see));
232 1.13.2.2 ad return error;
233 1.13.2.2 ad }
234 1.13.2.2 ad
235 1.13.2.2 ad /*
236 1.13.2.3 ad * sme_event_unregister_all:
237 1.13.2.3 ad *
238 1.13.2.3 ad * + Unregisters all sysmon envsys events associated with a
239 1.13.2.3 ad * sysmon envsys device.
240 1.13.2.3 ad */
241 1.13.2.3 ad void
242 1.13.2.4 ad sme_event_unregister_all(const char *sme_name)
243 1.13.2.3 ad {
244 1.13.2.3 ad sme_event_t *see;
245 1.13.2.3 ad int evcounter = 0;
246 1.13.2.3 ad
247 1.13.2.4 ad KASSERT(mutex_owned(&sme_mtx));
248 1.13.2.4 ad KASSERT(sme_name != NULL);
249 1.13.2.3 ad
250 1.13.2.3 ad LIST_FOREACH(see, &sme_events_list, see_list) {
251 1.13.2.4 ad if (strcmp(see->pes.pes_dvname, sme_name) == 0)
252 1.13.2.3 ad evcounter++;
253 1.13.2.3 ad }
254 1.13.2.3 ad
255 1.13.2.3 ad DPRINTF(("%s: total events %d (%s)\n", __func__,
256 1.13.2.4 ad evcounter, sme_name));
257 1.13.2.3 ad
258 1.13.2.4 ad while ((see = LIST_FIRST(&sme_events_list))) {
259 1.13.2.3 ad if (evcounter == 0)
260 1.13.2.3 ad break;
261 1.13.2.3 ad
262 1.13.2.4 ad if (strcmp(see->pes.pes_dvname, sme_name) == 0) {
263 1.13.2.3 ad DPRINTF(("%s: event %s %d removed (%s)\n", __func__,
264 1.13.2.4 ad see->pes.pes_sensname, see->type, sme_name));
265 1.13.2.3 ad
266 1.13.2.3 ad while (see->see_flags & SME_EVENT_WORKING)
267 1.13.2.4 ad cv_wait(&sme_cv, &sme_mtx);
268 1.13.2.3 ad
269 1.13.2.3 ad LIST_REMOVE(see, see_list);
270 1.13.2.3 ad kmem_free(see, sizeof(*see));
271 1.13.2.3 ad evcounter--;
272 1.13.2.3 ad }
273 1.13.2.3 ad }
274 1.13.2.3 ad
275 1.13.2.4 ad if (LIST_EMPTY(&sme_events_list)) {
276 1.13.2.4 ad mutex_enter(&sme_event_init_mtx);
277 1.13.2.4 ad if (sme_events_initialized)
278 1.13.2.4 ad sme_events_destroy();
279 1.13.2.4 ad mutex_exit(&sme_event_init_mtx);
280 1.13.2.4 ad }
281 1.13.2.3 ad }
282 1.13.2.3 ad
283 1.13.2.3 ad /*
284 1.13.2.2 ad * sme_event_unregister:
285 1.13.2.2 ad *
286 1.13.2.2 ad * + Unregisters a sysmon envsys event.
287 1.13.2.2 ad */
288 1.13.2.2 ad int
289 1.13.2.2 ad sme_event_unregister(const char *sensor, int type)
290 1.13.2.2 ad {
291 1.13.2.2 ad sme_event_t *see;
292 1.13.2.2 ad bool found = false;
293 1.13.2.2 ad
294 1.13.2.4 ad KASSERT(mutex_owned(&sme_mtx));
295 1.13.2.2 ad KASSERT(sensor != NULL);
296 1.13.2.2 ad
297 1.13.2.2 ad LIST_FOREACH(see, &sme_events_list, see_list) {
298 1.13.2.2 ad if (strcmp(see->pes.pes_sensname, sensor) == 0) {
299 1.13.2.2 ad if (see->type == type) {
300 1.13.2.2 ad found = true;
301 1.13.2.2 ad break;
302 1.13.2.2 ad }
303 1.13.2.2 ad }
304 1.13.2.2 ad }
305 1.13.2.2 ad
306 1.13.2.4 ad if (!found)
307 1.13.2.2 ad return EINVAL;
308 1.13.2.2 ad
309 1.13.2.3 ad while (see->see_flags & SME_EVENT_WORKING)
310 1.13.2.4 ad cv_wait(&sme_cv, &sme_mtx);
311 1.13.2.3 ad
312 1.13.2.2 ad DPRINTF(("%s: removing dev=%s sensor=%s type=%d\n",
313 1.13.2.2 ad __func__, see->pes.pes_dvname, sensor, type));
314 1.13.2.2 ad LIST_REMOVE(see, see_list);
315 1.13.2.2 ad /*
316 1.13.2.2 ad * So the events list is empty, we'll do the following:
317 1.13.2.2 ad *
318 1.13.2.2 ad * - stop and destroy the callout.
319 1.13.2.2 ad * - destroy the workqueue.
320 1.13.2.2 ad */
321 1.13.2.4 ad if (LIST_EMPTY(&sme_events_list)) {
322 1.13.2.4 ad mutex_enter(&sme_event_init_mtx);
323 1.13.2.3 ad sme_events_destroy();
324 1.13.2.4 ad mutex_exit(&sme_event_init_mtx);
325 1.13.2.4 ad }
326 1.13.2.2 ad
327 1.13.2.4 ad kmem_free(see, sizeof(*see));
328 1.13.2.2 ad return 0;
329 1.13.2.2 ad }
330 1.13.2.2 ad
331 1.13.2.2 ad /*
332 1.13.2.2 ad * sme_event_drvadd:
333 1.13.2.2 ad *
334 1.13.2.2 ad * + Adds a new sysmon envsys event for a driver if a sensor
335 1.13.2.2 ad * has set any accepted monitoring flag.
336 1.13.2.2 ad */
337 1.13.2.2 ad void
338 1.13.2.2 ad sme_event_drvadd(void *arg)
339 1.13.2.2 ad {
340 1.13.2.2 ad sme_event_drv_t *sed_t = arg;
341 1.13.2.2 ad int error = 0;
342 1.13.2.2 ad
343 1.13.2.2 ad KASSERT(sed_t != NULL);
344 1.13.2.2 ad
345 1.13.2.2 ad #define SEE_REGEVENT(a, b, c) \
346 1.13.2.2 ad do { \
347 1.13.2.2 ad if (sed_t->edata->flags & (a)) { \
348 1.13.2.4 ad char str[ENVSYS_DESCLEN] = "monitoring-state-"; \
349 1.13.2.2 ad \
350 1.13.2.4 ad error = sme_event_register(sed_t->sdict, \
351 1.13.2.2 ad sed_t->edata, \
352 1.13.2.2 ad sed_t->sme->sme_name, \
353 1.13.2.2 ad NULL, \
354 1.13.2.2 ad 0, \
355 1.13.2.2 ad (b), \
356 1.13.2.2 ad sed_t->powertype); \
357 1.13.2.2 ad if (error && error != EEXIST) \
358 1.13.2.2 ad printf("%s: failed to add event! " \
359 1.13.2.2 ad "error=%d sensor=%s event=%s\n", \
360 1.13.2.2 ad __func__, error, sed_t->edata->desc, (c)); \
361 1.13.2.2 ad else { \
362 1.13.2.2 ad (void)strlcat(str, (c), sizeof(str)); \
363 1.13.2.4 ad mutex_enter(&sme_mtx); \
364 1.13.2.2 ad prop_dictionary_set_bool(sed_t->sdict, \
365 1.13.2.2 ad str, \
366 1.13.2.2 ad true); \
367 1.13.2.4 ad mutex_exit(&sme_mtx); \
368 1.13.2.2 ad } \
369 1.13.2.2 ad } \
370 1.13.2.2 ad } while (/* CONSTCOND */ 0)
371 1.13.2.2 ad
372 1.13.2.2 ad SEE_REGEVENT(ENVSYS_FMONCRITICAL,
373 1.13.2.2 ad PENVSYS_EVENT_CRITICAL,
374 1.13.2.2 ad "critical");
375 1.13.2.2 ad
376 1.13.2.2 ad SEE_REGEVENT(ENVSYS_FMONCRITUNDER,
377 1.13.2.2 ad PENVSYS_EVENT_CRITUNDER,
378 1.13.2.2 ad "critunder");
379 1.13.2.2 ad
380 1.13.2.2 ad SEE_REGEVENT(ENVSYS_FMONCRITOVER,
381 1.13.2.2 ad PENVSYS_EVENT_CRITOVER,
382 1.13.2.2 ad "critover");
383 1.13.2.2 ad
384 1.13.2.2 ad SEE_REGEVENT(ENVSYS_FMONWARNUNDER,
385 1.13.2.2 ad PENVSYS_EVENT_WARNUNDER,
386 1.13.2.2 ad "warnunder");
387 1.13.2.2 ad
388 1.13.2.2 ad SEE_REGEVENT(ENVSYS_FMONWARNOVER,
389 1.13.2.2 ad PENVSYS_EVENT_WARNOVER,
390 1.13.2.2 ad "warnover");
391 1.13.2.2 ad
392 1.13.2.4 ad SEE_REGEVENT(ENVSYS_FMONSTCHANGED,
393 1.13.2.4 ad PENVSYS_EVENT_STATE_CHANGED,
394 1.13.2.4 ad "state-changed");
395 1.13.2.2 ad
396 1.13.2.2 ad /* we are done, free memory now */
397 1.13.2.2 ad kmem_free(sed_t, sizeof(*sed_t));
398 1.13.2.2 ad }
399 1.13.2.2 ad
400 1.13.2.2 ad /*
401 1.13.2.2 ad * sme_events_init:
402 1.13.2.2 ad *
403 1.13.2.3 ad * + Initializes the events framework.
404 1.13.2.2 ad */
405 1.13.2.2 ad int
406 1.13.2.2 ad sme_events_init(void)
407 1.13.2.2 ad {
408 1.13.2.2 ad int error;
409 1.13.2.2 ad
410 1.13.2.4 ad KASSERT(mutex_owned(&sme_event_init_mtx));
411 1.13.2.4 ad
412 1.13.2.2 ad error = workqueue_create(&seewq, "envsysev",
413 1.13.2.4 ad sme_events_worker, NULL, 0, IPL_SOFTCLOCK, WQ_MPSAFE);
414 1.13.2.2 ad if (error)
415 1.13.2.2 ad goto out;
416 1.13.2.2 ad
417 1.13.2.2 ad callout_init(&seeco, 0);
418 1.13.2.2 ad callout_setfunc(&seeco, sme_events_check, NULL);
419 1.13.2.2 ad callout_schedule(&seeco, SME_EVTIMO);
420 1.13.2.2 ad sme_events_initialized = true;
421 1.13.2.2 ad DPRINTF(("%s: events framework initialized\n", __func__));
422 1.13.2.2 ad
423 1.13.2.2 ad out:
424 1.13.2.2 ad return error;
425 1.13.2.2 ad }
426 1.13.2.2 ad
427 1.13.2.2 ad /*
428 1.13.2.3 ad * sme_events_destroy:
429 1.13.2.3 ad *
430 1.13.2.3 ad * + Destroys the events framework: the workqueue and the
431 1.13.2.3 ad * callout are stopped and destroyed because there are not
432 1.13.2.3 ad * events in the queue.
433 1.13.2.3 ad */
434 1.13.2.3 ad void
435 1.13.2.3 ad sme_events_destroy(void)
436 1.13.2.3 ad {
437 1.13.2.4 ad KASSERT(mutex_owned(&sme_event_init_mtx));
438 1.13.2.4 ad
439 1.13.2.3 ad callout_stop(&seeco);
440 1.13.2.3 ad sme_events_initialized = false;
441 1.13.2.3 ad DPRINTF(("%s: events framework destroyed\n", __func__));
442 1.13.2.4 ad callout_destroy(&seeco);
443 1.13.2.4 ad workqueue_destroy(seewq);
444 1.13.2.3 ad }
445 1.13.2.3 ad
446 1.13.2.3 ad /*
447 1.13.2.2 ad * sme_events_check:
448 1.13.2.2 ad *
449 1.13.2.2 ad * + Runs the work on each sysmon envsys event in our
450 1.13.2.2 ad * workqueue periodically with callout.
451 1.13.2.2 ad */
452 1.13.2.2 ad void
453 1.13.2.2 ad sme_events_check(void *arg)
454 1.13.2.2 ad {
455 1.13.2.2 ad sme_event_t *see;
456 1.13.2.2 ad
457 1.13.2.2 ad LIST_FOREACH(see, &sme_events_list, see_list) {
458 1.13.2.3 ad DPRINTFOBJ(("%s: dev=%s sensor=%s type=%d\n",
459 1.13.2.2 ad __func__,
460 1.13.2.2 ad see->pes.pes_dvname,
461 1.13.2.2 ad see->pes.pes_sensname,
462 1.13.2.2 ad see->type));
463 1.13.2.2 ad workqueue_enqueue(seewq, &see->see_wk, NULL);
464 1.13.2.2 ad }
465 1.13.2.2 ad callout_schedule(&seeco, SME_EVTIMO);
466 1.13.2.2 ad }
467 1.13.2.2 ad
468 1.13.2.2 ad /*
469 1.13.2.2 ad * sme_events_worker:
470 1.13.2.2 ad *
471 1.13.2.2 ad * + workqueue thread that checks if there's a critical condition
472 1.13.2.2 ad * and sends an event if the condition was triggered.
473 1.13.2.2 ad */
474 1.13.2.2 ad void
475 1.13.2.2 ad sme_events_worker(struct work *wk, void *arg)
476 1.13.2.2 ad {
477 1.13.2.4 ad const struct sme_description_table *sdt = NULL;
478 1.13.2.2 ad const struct sme_sensor_event *sse = sme_sensor_event;
479 1.13.2.2 ad sme_event_t *see = (void *)wk;
480 1.13.2.2 ad struct sysmon_envsys *sme;
481 1.13.2.2 ad envsys_data_t *edata;
482 1.13.2.4 ad int i, state, error;
483 1.13.2.2 ad
484 1.13.2.2 ad KASSERT(wk == &see->see_wk);
485 1.13.2.2 ad
486 1.13.2.4 ad state = error = 0;
487 1.13.2.4 ad
488 1.13.2.4 ad mutex_enter(&sme_mtx);
489 1.13.2.3 ad see->see_flags |= SME_EVENT_WORKING;
490 1.13.2.3 ad
491 1.13.2.2 ad /*
492 1.13.2.2 ad * We have to find the sme device by looking
493 1.13.2.2 ad * at the power envsys device name.
494 1.13.2.2 ad */
495 1.13.2.2 ad LIST_FOREACH(sme, &sysmon_envsys_list, sme_list)
496 1.13.2.2 ad if (strcmp(sme->sme_name, see->pes.pes_dvname) == 0)
497 1.13.2.2 ad break;
498 1.13.2.4 ad if (!sme)
499 1.13.2.4 ad goto out;
500 1.13.2.2 ad
501 1.13.2.2 ad /* get the sensor with the index specified in see->snum */
502 1.13.2.2 ad edata = &sme->sme_sensor_data[see->snum];
503 1.13.2.2 ad
504 1.13.2.2 ad /*
505 1.13.2.2 ad * refresh the sensor that was marked with a critical
506 1.13.2.2 ad * event.
507 1.13.2.2 ad */
508 1.13.2.2 ad if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
509 1.13.2.2 ad error = (*sme->sme_gtredata)(sme, edata);
510 1.13.2.4 ad if (error)
511 1.13.2.4 ad goto out;
512 1.13.2.2 ad }
513 1.13.2.2 ad
514 1.13.2.3 ad DPRINTFOBJ(("%s: desc=%s sensor=%d units=%d value_cur=%d\n",
515 1.13.2.2 ad __func__, edata->desc, edata->sensor,
516 1.13.2.2 ad edata->units, edata->value_cur));
517 1.13.2.2 ad
518 1.13.2.2 ad #define SME_SEND_NORMALEVENT() \
519 1.13.2.2 ad do { \
520 1.13.2.2 ad see->evsent = false; \
521 1.13.2.2 ad sysmon_penvsys_event(&see->pes, PENVSYS_EVENT_NORMAL); \
522 1.13.2.2 ad } while (/* CONSTCOND */ 0)
523 1.13.2.2 ad
524 1.13.2.2 ad #define SME_SEND_EVENT(type) \
525 1.13.2.2 ad do { \
526 1.13.2.2 ad see->evsent = true; \
527 1.13.2.2 ad sysmon_penvsys_event(&see->pes, (type)); \
528 1.13.2.2 ad } while (/* CONSTCOND */ 0)
529 1.13.2.2 ad
530 1.13.2.2 ad switch (see->type) {
531 1.13.2.2 ad /*
532 1.13.2.2 ad * if state is the same than the one that matches sse[i].state,
533 1.13.2.2 ad * send the event...
534 1.13.2.2 ad */
535 1.13.2.2 ad case PENVSYS_EVENT_CRITICAL:
536 1.13.2.2 ad case PENVSYS_EVENT_CRITUNDER:
537 1.13.2.2 ad case PENVSYS_EVENT_CRITOVER:
538 1.13.2.2 ad case PENVSYS_EVENT_WARNUNDER:
539 1.13.2.2 ad case PENVSYS_EVENT_WARNOVER:
540 1.13.2.2 ad for (i = 0; sse[i].state != -1; i++)
541 1.13.2.2 ad if (sse[i].event == see->type)
542 1.13.2.2 ad break;
543 1.13.2.2 ad
544 1.13.2.2 ad if (see->evsent && edata->state == ENVSYS_SVALID)
545 1.13.2.2 ad SME_SEND_NORMALEVENT();
546 1.13.2.2 ad
547 1.13.2.2 ad if (!see->evsent && edata->state == sse[i].state)
548 1.13.2.2 ad SME_SEND_EVENT(see->type);
549 1.13.2.2 ad
550 1.13.2.2 ad break;
551 1.13.2.2 ad /*
552 1.13.2.2 ad * if value_cur is lower than the limit, send the event...
553 1.13.2.2 ad */
554 1.13.2.2 ad case PENVSYS_EVENT_BATT_USERCAP:
555 1.13.2.2 ad case PENVSYS_EVENT_USER_CRITMIN:
556 1.13.2.2 ad if (see->evsent && edata->value_cur > see->critval)
557 1.13.2.2 ad SME_SEND_NORMALEVENT();
558 1.13.2.2 ad
559 1.13.2.2 ad if (!see->evsent && edata->value_cur < see->critval)
560 1.13.2.2 ad SME_SEND_EVENT(see->type);
561 1.13.2.2 ad
562 1.13.2.2 ad break;
563 1.13.2.2 ad /*
564 1.13.2.2 ad * if value_cur is higher than the limit, send the event...
565 1.13.2.2 ad */
566 1.13.2.2 ad case PENVSYS_EVENT_USER_CRITMAX:
567 1.13.2.2 ad if (see->evsent && edata->value_cur < see->critval)
568 1.13.2.2 ad SME_SEND_NORMALEVENT();
569 1.13.2.2 ad
570 1.13.2.2 ad if (!see->evsent && edata->value_cur > see->critval)
571 1.13.2.2 ad SME_SEND_EVENT(see->type);
572 1.13.2.2 ad
573 1.13.2.2 ad break;
574 1.13.2.2 ad /*
575 1.13.2.4 ad * if value_cur is not normal (battery) or online (drive),
576 1.13.2.4 ad * send the event...
577 1.13.2.2 ad */
578 1.13.2.4 ad case PENVSYS_EVENT_STATE_CHANGED:
579 1.13.2.2 ad /* the state has not been changed, just ignore the event */
580 1.13.2.2 ad if (edata->value_cur == see->evsent)
581 1.13.2.2 ad break;
582 1.13.2.2 ad
583 1.13.2.4 ad switch (edata->units) {
584 1.13.2.4 ad case ENVSYS_DRIVE:
585 1.13.2.4 ad sdt = sme_get_description_table(SME_DESC_DRIVE_STATES);
586 1.13.2.4 ad state = ENVSYS_DRIVE_ONLINE;
587 1.13.2.4 ad break;
588 1.13.2.4 ad case ENVSYS_BATTERY_STATE:
589 1.13.2.4 ad sdt =
590 1.13.2.4 ad sme_get_description_table(SME_DESC_BATTERY_STATES);
591 1.13.2.4 ad state = ENVSYS_BATTERY_STATE_NORMAL;
592 1.13.2.4 ad break;
593 1.13.2.4 ad default:
594 1.13.2.4 ad panic("%s: invalid units for ENVSYS_FMONSTCHANGED",
595 1.13.2.4 ad __func__);
596 1.13.2.4 ad }
597 1.13.2.4 ad
598 1.13.2.4 ad for (i = 0; sdt[i].type != -1; i++)
599 1.13.2.4 ad if (sdt[i].type == edata->value_cur)
600 1.13.2.2 ad break;
601 1.13.2.2 ad
602 1.13.2.2 ad /* copy current state description */
603 1.13.2.4 ad (void)strlcpy(see->pes.pes_statedesc, sdt[i].desc,
604 1.13.2.2 ad sizeof(see->pes.pes_statedesc));
605 1.13.2.2 ad
606 1.13.2.2 ad /* state is ok again... send a normal event */
607 1.13.2.4 ad if (see->evsent && edata->value_cur == state)
608 1.13.2.2 ad SME_SEND_NORMALEVENT();
609 1.13.2.2 ad
610 1.13.2.4 ad /* state has been changed... send event */
611 1.13.2.4 ad if (see->evsent || edata->value_cur != state) {
612 1.13.2.2 ad /* save current drive state */
613 1.13.2.2 ad see->evsent = edata->value_cur;
614 1.13.2.2 ad sysmon_penvsys_event(&see->pes, see->type);
615 1.13.2.2 ad }
616 1.13.2.2 ad
617 1.13.2.2 ad break;
618 1.13.2.2 ad }
619 1.13.2.4 ad out:
620 1.13.2.3 ad see->see_flags &= ~SME_EVENT_WORKING;
621 1.13.2.4 ad cv_broadcast(&sme_cv);
622 1.13.2.4 ad mutex_exit(&sme_mtx);
623 1.13.2.2 ad }
624