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