sysmon_envsys.c revision 1.51 1 /* $NetBSD: sysmon_envsys.c,v 1.51 2007/08/31 10:13:27 xtraeme Exp $ */
2
3 /*-
4 * Copyright (c) 2007 The NetBSD Foundation, Inc.
5 * All rights reserved.
6 *
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Juan Romero Pardines.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by Juan Romero Pardines
21 * for the NetBSD Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*-
40 * Copyright (c) 2000 Zembu Labs, Inc.
41 * All rights reserved.
42 *
43 * Author: Jason R. Thorpe <thorpej (at) zembu.com>
44 *
45 * Redistribution and use in source and binary forms, with or without
46 * modification, are permitted provided that the following conditions
47 * are met:
48 * 1. Redistributions of source code must retain the above copyright
49 * notice, this list of conditions and the following disclaimer.
50 * 2. Redistributions in binary form must reproduce the above copyright
51 * notice, this list of conditions and the following disclaimer in the
52 * documentation and/or other materials provided with the distribution.
53 * 3. All advertising materials mentioning features or use of this software
54 * must display the following acknowledgement:
55 * This product includes software developed by Zembu Labs, Inc.
56 * 4. Neither the name of Zembu Labs nor the names of its employees may
57 * be used to endorse or promote products derived from this software
58 * without specific prior written permission.
59 *
60 * THIS SOFTWARE IS PROVIDED BY ZEMBU LABS, INC. ``AS IS'' AND ANY EXPRESS
61 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WAR-
62 * RANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DIS-
63 * CLAIMED. IN NO EVENT SHALL ZEMBU LABS BE LIABLE FOR ANY DIRECT, INDIRECT,
64 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
65 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
66 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
67 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
68 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
69 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
70 */
71
72 /*
73 * Environmental sensor framework for sysmon, exported to userland
74 * with proplib(3).
75 */
76
77 #include <sys/cdefs.h>
78 __KERNEL_RCSID(0, "$NetBSD: sysmon_envsys.c,v 1.51 2007/08/31 10:13:27 xtraeme Exp $");
79
80 #include <sys/param.h>
81 #include <sys/types.h>
82 #include <sys/conf.h>
83 #include <sys/errno.h>
84 #include <sys/fcntl.h>
85 #include <sys/kernel.h>
86 #include <sys/systm.h>
87 #include <sys/proc.h>
88 #include <sys/mutex.h>
89 #include <sys/kmem.h>
90
91 #include <dev/sysmon/sysmonvar.h>
92 #include <dev/sysmon/sysmon_envsysvar.h>
93 #include <dev/sysmon/sysmon_taskq.h>
94
95 #include "opt_compat_netbsd.h"
96
97 struct sme_sensor_type {
98 int type;
99 int crittype;
100 const char *desc;
101 };
102
103 static const struct sme_sensor_type sme_sensor_type[] = {
104 { ENVSYS_STEMP, PENVSYS_TYPE_TEMP, "Temperature" },
105 { ENVSYS_SFANRPM, PENVSYS_TYPE_FAN, "Fan" },
106 { ENVSYS_SVOLTS_AC, PENVSYS_TYPE_VOLTAGE, "Voltage AC" },
107 { ENVSYS_SVOLTS_DC, PENVSYS_TYPE_VOLTAGE, "Voltage DC" },
108 { ENVSYS_SOHMS, PENVSYS_TYPE_RESISTANCE,"Ohms" },
109 { ENVSYS_SWATTS, PENVSYS_TYPE_POWER, "Watts" },
110 { ENVSYS_SAMPS, PENVSYS_TYPE_POWER, "Ampere" },
111 { ENVSYS_SWATTHOUR, PENVSYS_TYPE_BATTERY, "Watt hour" },
112 { ENVSYS_SAMPHOUR, PENVSYS_TYPE_BATTERY, "Ampere hour" },
113 { ENVSYS_INDICATOR, PENVSYS_TYPE_INDICATOR, "Indicator" },
114 { ENVSYS_INTEGER, PENVSYS_TYPE_INDICATOR, "Integer" },
115 { ENVSYS_DRIVE, PENVSYS_TYPE_DRIVE, "Drive" },
116 { -1, -1, "unknown" }
117 };
118
119 static const struct sme_sensor_type sme_sensor_state[] = {
120 { ENVSYS_SVALID, -1, "valid" },
121 { ENVSYS_SINVALID, -1, "invalid" },
122 { ENVSYS_SCRITICAL, -1, "critical" },
123 { ENVSYS_SCRITUNDER, -1, "critical-under" },
124 { ENVSYS_SCRITOVER, -1, "critical-over" },
125 { ENVSYS_SWARNUNDER, -1, "warning-under" },
126 { ENVSYS_SWARNOVER, -1, "warning-over" },
127 { -1, -1, "unknown" }
128 };
129
130 static const struct sme_sensor_type sme_sensor_drive_state[] = {
131 { ENVSYS_DRIVE_EMPTY, -1, "drive state is unknown" },
132 { ENVSYS_DRIVE_READY, -1, "drive is ready" },
133 { ENVSYS_DRIVE_POWERUP, -1, "drive is powering up" },
134 { ENVSYS_DRIVE_ONLINE, -1, "drive is online" },
135 { ENVSYS_DRIVE_IDLE, -1, "drive is idle" },
136 { ENVSYS_DRIVE_ACTIVE, -1, "drive is active" },
137 { ENVSYS_DRIVE_REBUILD, -1, "drive is rebuilding" },
138 { ENVSYS_DRIVE_POWERDOWN, -1, "drive is powering down" },
139 { ENVSYS_DRIVE_FAIL, -1, "drive failed" },
140 { ENVSYS_DRIVE_PFAIL, -1, "drive degraded" },
141 { -1, -1, "unknown" }
142 };
143
144 static prop_dictionary_t sme_propd;
145 static kcondvar_t sme_list_cv;
146
147 kmutex_t sme_list_mtx, sme_event_mtx, sme_event_init_mtx;
148 kcondvar_t sme_event_cv;
149
150 #ifdef COMPAT_40
151 static u_int sysmon_envsys_next_sensor_index = 0;
152 static struct sysmon_envsys *sysmon_envsys_find_40(u_int);
153 #endif
154
155 static void sysmon_envsys_release(struct sysmon_envsys *);
156 static int sme_register_sensorname(struct sysmon_envsys *, envsys_data_t *);
157
158 /*
159 * sysmon_envsys_init:
160 *
161 * + Initialize global mutexes, dictionary and the linked lists.
162 */
163 void
164 sysmon_envsys_init(void)
165 {
166 LIST_INIT(&sysmon_envsys_list);
167 LIST_INIT(&sme_events_list);
168 mutex_init(&sme_list_mtx, MUTEX_DRIVER, IPL_NONE);
169 mutex_init(&sme_event_mtx, MUTEX_DRIVER, IPL_NONE);
170 mutex_init(&sme_event_init_mtx, MUTEX_DRIVER, IPL_NONE);
171 cv_init(&sme_list_cv, "smefind");
172 cv_init(&sme_event_cv, "smeevent");
173 sme_propd = prop_dictionary_create();
174 }
175
176 /*
177 * sysmonopen_envsys:
178 *
179 * + Open the system monitor device.
180 */
181 int
182 sysmonopen_envsys(dev_t dev, int flag, int mode, struct lwp *l)
183 {
184 return 0;
185 }
186
187 /*
188 * sysmonclose_envsys:
189 *
190 * + Close the system monitor device.
191 */
192 int
193 sysmonclose_envsys(dev_t dev, int flag, int mode, struct lwp *l)
194 {
195 /* Nothing to do */
196 return 0;
197 }
198
199 /*
200 * sysmonioctl_envsys:
201 *
202 * + Perform an envsys control request.
203 */
204 int
205 sysmonioctl_envsys(dev_t dev, u_long cmd, void *data, int flag, struct lwp *l)
206 {
207 struct sysmon_envsys *sme = NULL;
208 int error = 0;
209 #ifdef COMPAT_40
210 u_int oidx;
211 #endif
212
213 switch (cmd) {
214 case ENVSYS_GETDICTIONARY:
215 {
216 struct plistref *plist = (struct plistref *)data;
217
218 /*
219 * Update all sysmon envsys devices dictionaries with
220 * new data if it's different than we have currently
221 * in the dictionary.
222 */
223 mutex_enter(&sme_list_mtx);
224 LIST_FOREACH(sme, &sysmon_envsys_list, sme_list) {
225 sme->sme_flags |= SME_FLAG_BUSY;
226 error = sme_update_dictionary(sme);
227 if (error) {
228 DPRINTF(("%s: sme_update_dictionary, "
229 "error=%d\n", __func__, error));
230 sme->sme_flags &= ~SME_FLAG_BUSY;
231 mutex_exit(&sme_list_mtx);
232 return error;
233 }
234 sme->sme_flags &= ~SME_FLAG_BUSY;
235 }
236 mutex_exit(&sme_list_mtx);
237 /*
238 * Copy global dictionary to userland.
239 */
240 error = prop_dictionary_copyout_ioctl(plist, cmd, sme_propd);
241 break;
242 }
243 case ENVSYS_SETDICTIONARY:
244 {
245 const struct plistref *plist = (const struct plistref *)data;
246 prop_dictionary_t udict;
247 prop_object_t obj;
248 const char *devname = NULL;
249
250 if ((flag & FWRITE) == 0)
251 return EPERM;
252
253 /*
254 * Get dictionary from userland.
255 */
256 error = prop_dictionary_copyin_ioctl(plist, cmd, &udict);
257 if (error) {
258 DPRINTF(("%s: copyin_ioctl error=%d\n",
259 __func__, error));
260 break;
261 }
262
263 /*
264 * Parse "driver-name" key to obtain the driver we
265 * are searching for.
266 */
267 obj = prop_dictionary_get(udict, "driver-name");
268 if (obj == NULL || prop_object_type(obj) != PROP_TYPE_STRING) {
269 DPRINTF(("%s: driver-name failed\n", __func__));
270 prop_object_release(udict);
271 error = EINVAL;
272 break;
273 }
274
275 /* driver name */
276 devname = prop_string_cstring_nocopy(obj);
277
278 /* find the correct sme device */
279 sme = sysmon_envsys_find(devname);
280 if (sme == NULL) {
281 DPRINTF(("%s: NULL sme\n", __func__));
282 prop_object_release(udict);
283 error = EINVAL;
284 break;
285 }
286
287 /*
288 * Find the correct array object with the string supplied
289 * by the userland dictionary.
290 */
291 obj = prop_dictionary_get(sme_propd, devname);
292 if (prop_object_type(obj) != PROP_TYPE_ARRAY) {
293 DPRINTF(("%s: array device failed\n", __func__));
294 sysmon_envsys_release(sme);
295 prop_object_release(udict);
296 error = EINVAL;
297 break;
298 }
299
300 /* do the real work now */
301 error = sme_userset_dictionary(sme, udict, obj);
302 sysmon_envsys_release(sme);
303 break;
304 }
305 /*
306 * Compatibility functions with the old interface, only
307 * implemented ENVSYS_GTREDATA and ENVSYS_GTREINFO; enough
308 * to make old applications work.
309 */
310 #ifdef COMPAT_40
311 case ENVSYS_GTREDATA:
312 {
313 struct envsys_tre_data *tred = (void *)data;
314 envsys_data_t *edata = NULL;
315
316 tred->validflags = 0;
317
318 sme = sysmon_envsys_find_40(tred->sensor);
319 if (sme == NULL)
320 break;
321
322 mutex_enter(&sme_list_mtx);
323 oidx = tred->sensor;
324 tred->sensor = SME_SENSOR_IDX(sme, tred->sensor);
325
326 DPRINTFOBJ(("%s: sensor=%d oidx=%d dev=%s nsensors=%d\n",
327 __func__, tred->sensor, oidx, sme->sme_name,
328 sme->sme_nsensors));
329
330 edata = &sme->sme_sensor_data[tred->sensor];
331
332 if (tred->sensor < sme->sme_nsensors) {
333 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
334 error = (*sme->sme_gtredata)(sme, edata);
335 if (error) {
336 DPRINTF(("%s: sme_gtredata failed\n",
337 __func__));
338 mutex_exit(&sme_list_mtx);
339 return error;
340 }
341 }
342
343 /* copy required values to the old interface */
344 tred->sensor = edata->sensor;
345 tred->cur.data_us = edata->value_cur;
346 tred->cur.data_s = edata->value_cur;
347 tred->max.data_us = edata->value_max;
348 tred->max.data_s = edata->value_max;
349 tred->min.data_us = edata->value_min;
350 tred->min.data_s = edata->value_min;
351 tred->avg.data_us = edata->value_avg;
352 tred->avg.data_s = edata->value_avg;
353 tred->units = edata->units;
354
355 tred->validflags |= ENVSYS_FVALID;
356 tred->validflags |= ENVSYS_FCURVALID;
357
358 if (edata->flags & ENVSYS_FPERCENT) {
359 tred->validflags |= ENVSYS_FMAXVALID;
360 tred->validflags |= ENVSYS_FFRACVALID;
361 }
362
363 if (edata->state == ENVSYS_SINVALID ||
364 edata->flags & ENVSYS_FNOTVALID) {
365 tred->validflags &= ~ENVSYS_FCURVALID;
366 tred->cur.data_us = tred->cur.data_s = 0;
367 }
368
369 DPRINTFOBJ(("%s: sensor=%s tred->cur.data_s=%d\n",
370 __func__, edata->desc, tred->cur.data_s));
371 DPRINTFOBJ(("%s: tred->validflags=%d tred->units=%d"
372 " tred->sensor=%d\n", __func__, tred->validflags,
373 tred->units, tred->sensor));
374 }
375 tred->sensor = oidx;
376 mutex_exit(&sme_list_mtx);
377
378 break;
379 }
380 case ENVSYS_GTREINFO:
381 {
382 struct envsys_basic_info *binfo = (void *)data;
383 envsys_data_t *edata = NULL;
384
385 binfo->validflags = 0;
386
387 sme = sysmon_envsys_find_40(binfo->sensor);
388 if (sme == NULL)
389 break;
390
391 mutex_enter(&sme_list_mtx);
392 oidx = binfo->sensor;
393 binfo->sensor = SME_SENSOR_IDX(sme, binfo->sensor);
394
395 edata = &sme->sme_sensor_data[binfo->sensor];
396
397 binfo->validflags |= ENVSYS_FVALID;
398
399 if (binfo->sensor < sme->sme_nsensors) {
400 binfo->units = edata->units;
401 (void)strlcpy(binfo->desc, edata->desc,
402 sizeof(binfo->desc));
403 }
404
405 DPRINTFOBJ(("%s: binfo->units=%d binfo->validflags=%d\n",
406 __func__, binfo->units, binfo->validflags));
407 DPRINTFOBJ(("%s: binfo->desc=%s binfo->sensor=%d\n",
408 __func__, binfo->desc, binfo->sensor));
409
410 binfo->sensor = oidx;
411 mutex_exit(&sme_list_mtx);
412
413 break;
414 }
415 #endif /* COMPAT_40 */
416 default:
417 error = ENOTTY;
418 break;
419 }
420
421 return error;
422 }
423
424 /*
425 * sysmon_envsys_register:
426 *
427 * + Register an envsys device.
428 * + Create device dictionary.
429 */
430 int
431 sysmon_envsys_register(struct sysmon_envsys *sme)
432 {
433 struct sme_dictionary {
434 SLIST_ENTRY(sme_dictionary) sme_dicts;
435 prop_dictionary_t dict;
436 size_t idx;
437 };
438 struct sysmon_envsys *lsme;
439 SLIST_HEAD(, sme_dictionary) sme_dict_list;
440 struct sme_dictionary *sd = NULL;
441 prop_array_t array;
442 envsys_data_t *edata = NULL;
443 int i, error = 0;
444
445 KASSERT(sme != NULL);
446 KASSERT(sme->sme_name != NULL);
447 KASSERT(sme->sme_sensor_data != NULL);
448
449 /*
450 * sme_nsensors is mandatory...
451 */
452 if (!sme->sme_nsensors)
453 return EINVAL;
454
455 /*
456 * sanity check: if SME_DISABLE_GTREDATA is not set,
457 * the sme_gtredata function callback must be non NULL.
458 */
459 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
460 if (sme->sme_gtredata == NULL)
461 return EINVAL;
462 }
463
464 /* create the sysmon envsys device array. */
465 array = prop_array_create();
466 if (array == NULL)
467 return ENOMEM;
468
469 /*
470 * Initialize the singly linked list to allocate N dictionaries.
471 */
472 SLIST_INIT(&sme_dict_list);
473 for (i = 0; i < sme->sme_nsensors; i++) {
474 sd = kmem_zalloc(sizeof(*sd), KM_SLEEP);
475 sd->dict = prop_dictionary_create();
476 if (sd->dict == NULL) {
477 kmem_free(sd, sizeof(*sd));
478 error = ENOMEM;
479 goto out2;
480 }
481 sd->idx = i;
482 SLIST_INSERT_HEAD(&sme_dict_list, sd, sme_dicts);
483 DPRINTF(("%s: creating dictionary [%d]\n", __func__, i));
484 }
485
486
487 /*
488 * Check if requested sysmon_envsys device is valid
489 * and does not exist already in the list.
490 */
491 mutex_enter(&sme_list_mtx);
492 sme->sme_flags |= SME_FLAG_BUSY;
493 LIST_FOREACH(lsme, &sysmon_envsys_list, sme_list) {
494 if (strcmp(lsme->sme_name, sme->sme_name) == 0) {
495 error = EEXIST;
496 goto out;
497 }
498 }
499 /*
500 * Initialize the singly linked list for sensor descriptions.
501 */
502 SLIST_INIT(&sme->sme_names_list);
503 /*
504 * Iterate over all sensors and create a dictionary per sensor,
505 * checking firstly if sensor description is unique.
506 */
507 for (i = 0; i < sme->sme_nsensors; i++) {
508 /*
509 * XXX:
510 *
511 * workaround for LKMs. First sensor used in a LKM
512 * gets the index sensor from all edata structs
513 * allocated in kernel. It is unknown to me why this
514 * value is not 0 when using a LKM.
515 *
516 * For now overwrite its index to 0.
517 */
518 if (i == 0)
519 sme->sme_sensor_data[0].sensor = 0;
520
521 edata = &sme->sme_sensor_data[i];
522 /*
523 * Check if sensor description is unique.
524 */
525 if (sme_register_sensorname(sme, edata))
526 continue;
527
528 SLIST_FOREACH(sd, &sme_dict_list, sme_dicts)
529 if (sd->idx == i)
530 break;
531 /*
532 * Create all objects in sensor's dictionary.
533 */
534 sme_add_sensor_dictionary(sme, array, sd->dict, edata);
535 }
536
537 /*
538 * If the array does not contain any object (sensor), there's
539 * no need to attach the driver.
540 */
541 if (prop_array_count(array) == 0) {
542 error = EINVAL;
543 DPRINTF(("%s: empty array for '%s'\n", __func__,
544 sme->sme_name));
545 goto out;
546 }
547
548 /*
549 * Add the array into the global dictionary for the driver.
550 *
551 * <dict>
552 * <key>foo0</key>
553 * <array>
554 * ...
555 */
556 if (!prop_dictionary_set(sme_propd, sme->sme_name, array)) {
557 DPRINTF(("%s: prop_dictionary_set for '%s'\n", __func__,
558 sme->sme_name));
559 error = EINVAL;
560 goto out;
561 }
562
563 /*
564 * Add the device into the list.
565 */
566 #ifdef COMPAT_40
567 sme->sme_fsensor = sysmon_envsys_next_sensor_index;
568 sysmon_envsys_next_sensor_index += sme->sme_nsensors;
569 #endif
570 LIST_INSERT_HEAD(&sysmon_envsys_list, sme, sme_list);
571
572 out:
573 sme->sme_flags &= ~SME_FLAG_BUSY;
574 sme->sme_uniqsensors = 0;
575 mutex_exit(&sme_list_mtx);
576 /*
577 * Remove all items from the singly linked list, we don't
578 * need them anymore.
579 */
580 out2:
581 while ((sd = SLIST_FIRST(&sme_dict_list)) != NULL) {
582 SLIST_REMOVE_HEAD(&sme_dict_list, sme_dicts);
583 prop_object_release(sd->dict);
584 kmem_free(sd, sizeof(*sd));
585 }
586 if (error) {
587 DPRINTF(("%s: failed to register '%s' (%d)\n",
588 __func__, sme->sme_name, error));
589 prop_object_release(array);
590 }
591 return error;
592 }
593
594 /*
595 * sysmon_envsys_unregister:
596 *
597 * + Unregister an envsys device.
598 * + Unregister all events associated with this device.
599 * + Remove device dictionary.
600 */
601 void
602 sysmon_envsys_unregister(struct sysmon_envsys *sme)
603 {
604 struct sme_sensor_names *snames;
605
606 KASSERT(sme != NULL);
607
608 mutex_enter(&sme_list_mtx);
609 while (sme->sme_flags & SME_FLAG_BUSY) {
610 sme->sme_flags |= SME_FLAG_WANTED;
611 cv_wait(&sme_list_cv, &sme_list_mtx);
612 }
613 LIST_REMOVE(sme, sme_list);
614 /*
615 * Remove all sensor descriptions from the singly linked list.
616 */
617 while (!SLIST_EMPTY(&sme->sme_names_list)) {
618 snames = SLIST_FIRST(&sme->sme_names_list);
619 SLIST_REMOVE_HEAD(&sme->sme_names_list, sme_names);
620 kmem_free(snames, sizeof(*snames));
621 }
622 mutex_exit(&sme_list_mtx);
623 /*
624 * Unregister all events associated with this device.
625 */
626 sme_event_unregister_all(sme);
627 /*
628 * Remove the device from the global dictionary.
629 */
630 prop_dictionary_remove(sme_propd, sme->sme_name);
631 }
632
633 /*
634 * sysmon_envsys_find:
635 *
636 * + Find an envsys device.
637 */
638 struct sysmon_envsys *
639 sysmon_envsys_find(const char *name)
640 {
641 struct sysmon_envsys *sme;
642
643 mutex_enter(&sme_list_mtx);
644 again:
645 for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
646 sme = LIST_NEXT(sme, sme_list)) {
647 if (strcmp(sme->sme_name, name) == 0) {
648 if (sme->sme_flags & SME_FLAG_BUSY) {
649 sme->sme_flags |= SME_FLAG_WANTED;
650 cv_wait(&sme_list_cv, &sme_list_mtx);
651 goto again;
652 }
653 sme->sme_flags |= SME_FLAG_BUSY;
654 break;
655 }
656 }
657 mutex_exit(&sme_list_mtx);
658 return sme;
659 }
660
661 /*
662 * sysmon_envsys_release:
663 *
664 * + Release an envsys device.
665 */
666 void
667 sysmon_envsys_release(struct sysmon_envsys *sme)
668 {
669 mutex_enter(&sme_list_mtx);
670 if (sme->sme_flags & SME_FLAG_WANTED)
671 cv_broadcast(&sme_list_cv);
672 sme->sme_flags &= ~(SME_FLAG_BUSY | SME_FLAG_WANTED);
673 mutex_exit(&sme_list_mtx);
674 }
675
676 /* compatibility function */
677 #ifdef COMPAT_40
678 struct sysmon_envsys *
679 sysmon_envsys_find_40(u_int idx)
680 {
681 struct sysmon_envsys *sme;
682
683 mutex_enter(&sme_list_mtx);
684 for (sme = LIST_FIRST(&sysmon_envsys_list); sme != NULL;
685 sme = LIST_NEXT(sme, sme_list)) {
686 if (idx >= sme->sme_fsensor &&
687 idx < (sme->sme_fsensor + sme->sme_nsensors))
688 break;
689 }
690 mutex_exit(&sme_list_mtx);
691 return sme;
692 }
693 #endif
694
695 /*
696 * sme_register_sensorname:
697 *
698 * + Registers a sensor name into the list maintained per device.
699 */
700 static int
701 sme_register_sensorname(struct sysmon_envsys *sme, envsys_data_t *edata)
702 {
703 struct sme_sensor_names *snames, *snames2 = NULL;
704
705 KASSERT(edata != NULL);
706 KASSERT(mutex_owned(&sme_list_mtx));
707
708 SLIST_FOREACH(snames2, &sme->sme_names_list, sme_names) {
709 /*
710 * Match sensors with empty and duplicate description.
711 */
712 if (strlen(edata->desc) == 0 ||
713 strcmp(snames2->desc, edata->desc) == 0)
714 if (snames2->assigned) {
715 edata->flags |= ENVSYS_FNOTVALID;
716 DPRINTF(("%s: duplicate name "
717 "(%s)\n", __func__, edata->desc));
718 return EEXIST;
719 }
720 }
721
722 snames = kmem_zalloc(sizeof(*snames), KM_NOSLEEP);
723 if (snames == NULL)
724 return ENOMEM;
725
726 snames->assigned = true;
727 (void)strlcpy(snames->desc, edata->desc, sizeof(snames->desc));
728 DPRINTF(("%s: registering sensor name=%s\n", __func__, edata->desc));
729 SLIST_INSERT_HEAD(&sme->sme_names_list, snames, sme_names);
730 sme->sme_uniqsensors++;
731
732 return 0;
733 }
734
735 /*
736 * sme_make_dictionary:
737 *
738 * + Create sensor's dictionary in device's array.
739 */
740 void
741 sme_add_sensor_dictionary(struct sysmon_envsys *sme, prop_array_t array,
742 prop_dictionary_t dict, envsys_data_t *edata)
743 {
744 const struct sme_sensor_type *est = sme_sensor_type;
745 const struct sme_sensor_type *ess = sme_sensor_state;
746 const struct sme_sensor_type *esds = sme_sensor_drive_state;
747 sme_event_drv_t *sme_evdrv_t = NULL;
748 int i, j;
749
750 i = j = 0;
751
752 KASSERT(mutex_owned(&sme_list_mtx));
753
754 /* find the correct unit for this sensor. */
755 for (i = 0; est[i].type != -1; i++)
756 if (est[i].type == edata->units)
757 break;
758
759 if (strcmp(est[i].desc, "unknown") == 0) {
760 DPRINTF(("%s: invalid units type for sensor=%d\n",
761 __func__, edata->sensor));
762 goto invalidate_sensor;
763 }
764
765 /*
766 * ...
767 * <key>type</key>
768 * <string>foo</string>
769 * <key>description</key>
770 * <string>blah blah</string>
771 * ...
772 */
773 if (sme_sensor_upstring(dict, "type", est[i].desc))
774 goto invalidate_sensor;
775
776 if (strlen(edata->desc) == 0) {
777 DPRINTF(("%s: invalid description for sensor=%d\n",
778 __func__, edata->sensor));
779 goto invalidate_sensor;
780 }
781
782 if (sme_sensor_upstring(dict, "description", edata->desc))
783 goto invalidate_sensor;
784
785 /*
786 * Add sensor's state description.
787 *
788 * ...
789 * <key>state</key>
790 * <string>valid</string>
791 * ...
792 */
793 for (j = 0; ess[j].type != -1; j++)
794 if (ess[j].type == edata->state)
795 break;
796
797 if (strcmp(ess[j].desc, "unknown") == 0) {
798 DPRINTF(("%s: invalid state for sensor=%d\n",
799 __func__, edata->sensor));
800 goto invalidate_sensor;
801 }
802
803 DPRINTF(("%s: sensor desc=%s type=%d state=%d\n",
804 __func__, edata->desc, edata->units, edata->state));
805
806 if (sme_sensor_upstring(dict, "state", ess[j].desc))
807 goto invalidate_sensor;
808
809 /*
810 * add the percentage boolean object:
811 *
812 * ...
813 * <key>want-percentage</key>
814 * <true/>
815 * ...
816 */
817 if (edata->flags & ENVSYS_FPERCENT)
818 if (sme_sensor_upbool(dict, "want-percentage", true))
819 return;
820
821 /*
822 * Add the monitoring boolean object:
823 *
824 * ...
825 * <key>monitoring-supported</key>
826 * <true/>
827 * ...
828 *
829 * always false on Drive and Indicator types, they
830 * cannot be monitored.
831 *
832 */
833 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
834 (edata->units == ENVSYS_INDICATOR) ||
835 (edata->units == ENVSYS_DRIVE)) {
836 if (sme_sensor_upbool(dict, "monitoring-supported", false))
837 return;
838 } else {
839 if (sme_sensor_upbool(dict, "monitoring-supported", true))
840 return;
841 }
842
843 /*
844 * Add the drive-state object for drive sensors:
845 *
846 * ...
847 * <key>drive-state</key>
848 * <string>drive is online</string>
849 * ...
850 */
851 if (edata->units == ENVSYS_DRIVE) {
852 for (j = 0; esds[j].type != -1; j++)
853 if (esds[j].type == edata->value_cur)
854 break;
855
856 if (sme_sensor_upstring(dict, "drive-state", esds[j].desc))
857 return;
858 }
859
860 /* if sensor is enabled, add the following properties... */
861 if (edata->state == ENVSYS_SVALID) {
862 /*
863 * ...
864 * <key>rpms</key>
865 * <integer>2500</integer>
866 * <key>rfact</key>
867 * <integer>10000</integer>
868 * <key>cur-value</key>
869 * <integer>1250</integer>
870 * <key>min-value</key>
871 * <integer>800</integer>
872 * <key>max-value</integer>
873 * <integer>3000</integer>
874 * <key>avg-value</integer>
875 * <integer>1400</integer>
876 * </dict>
877 */
878 if (edata->units == ENVSYS_SFANRPM)
879 if (sme_sensor_upuint32(dict, "rpms", edata->rpms))
880 return;
881
882 if (edata->units == ENVSYS_SVOLTS_AC ||
883 edata->units == ENVSYS_SVOLTS_DC)
884 if (sme_sensor_upint32(dict, "rfact", edata->rfact))
885 return;
886
887 if (sme_sensor_upint32(dict, "cur-value", edata->value_cur))
888 return;
889
890 if (edata->flags & ENVSYS_FVALID_MIN) {
891 if (sme_sensor_upint32(dict,
892 "min-value",
893 edata->value_min))
894 return;
895 }
896
897 if (edata->flags & ENVSYS_FVALID_MAX) {
898 if (sme_sensor_upint32(dict,
899 "max-value",
900 edata->value_max))
901 return;
902 }
903
904 if (edata->flags & ENVSYS_FVALID_AVG) {
905 if (sme_sensor_upint32(dict,
906 "avg-value",
907 edata->value_avg))
908 return;
909 }
910 }
911
912 /*
913 * ...
914 * </array>
915 *
916 * Add the dictionary into the array.
917 *
918 */
919
920 if (!prop_array_set(array, sme->sme_uniqsensors - 1, dict)) {
921 DPRINTF(("%s: prop_array_add\n", __func__));
922 goto invalidate_sensor;
923 }
924
925 /*
926 * Add a new event if a monitoring flag was set.
927 */
928 if (edata->monitor) {
929 sme_evdrv_t = kmem_zalloc(sizeof(*sme_evdrv_t), KM_NOSLEEP);
930 if (sme_evdrv_t == NULL) {
931 DPRINTF(("%s: edata->monitor failed\n", __func__));
932 return;
933 }
934
935 sme_evdrv_t->sdict = dict;
936 sme_evdrv_t->edata = edata;
937 sme_evdrv_t->sme = sme;
938 sme_evdrv_t->powertype = est[i].crittype;
939
940 sysmon_task_queue_init();
941 sysmon_task_queue_sched(0, sme_event_drvadd, sme_evdrv_t);
942 }
943
944 return;
945
946 invalidate_sensor:
947 edata->flags |= ENVSYS_FNOTVALID;
948 }
949
950 /*
951 * sme_update_dictionary:
952 *
953 * + Update per-sensor dictionaries with new values if there were
954 * changes, otherwise the object in dictionary is untouched.
955 * + Send a critical event if any sensor is in a critical condition.
956 */
957 int
958 sme_update_dictionary(struct sysmon_envsys *sme)
959 {
960 const struct sme_sensor_type *est = sme_sensor_type;
961 const struct sme_sensor_type *ess = sme_sensor_state;
962 const struct sme_sensor_type *esds = sme_sensor_drive_state;
963 envsys_data_t *edata = NULL;
964 prop_object_t array, dict;
965 int i, j, error, invalid;
966
967 KASSERT(mutex_owned(&sme_list_mtx));
968
969 error = invalid = 0;
970 array = dict = NULL;
971
972 /* retrieve the array of dictionaries in device. */
973 array = prop_dictionary_get(sme_propd, sme->sme_name);
974 if (prop_object_type(array) != PROP_TYPE_ARRAY) {
975 DPRINTF(("%s: not an array (%s)\n", __func__, sme->sme_name));
976 return EINVAL;
977 }
978
979 /*
980 * - iterate over all sensors.
981 * - fetch new data.
982 * - check if data in dictionary is different than new data.
983 * - update dictionary if there were changes.
984 */
985 for (i = 0; i < sme->sme_nsensors; i++) {
986 edata = &sme->sme_sensor_data[i];
987 /* skip invalid sensors */
988 if (edata->flags & ENVSYS_FNOTVALID) {
989 invalid++;
990 continue;
991 }
992
993 /*
994 * refresh sensor data via sme_gtredata only if the
995 * flag is not set.
996 */
997 if ((sme->sme_flags & SME_DISABLE_GTREDATA) == 0) {
998 error = (*sme->sme_gtredata)(sme, edata);
999 if (error) {
1000 DPRINTF(("%s: gtredata[%d] failed\n",
1001 __func__, i));
1002 return error;
1003 }
1004 }
1005
1006 /* retrieve sensor's dictionary. */
1007 dict = prop_array_get(array, i - invalid);
1008 if (prop_object_type(dict) != PROP_TYPE_DICTIONARY) {
1009 DPRINTF(("%s: not a dictionary (%d:%s)\n",
1010 __func__, edata->sensor, sme->sme_name));
1011 return EINVAL;
1012 }
1013
1014 for (j = 0; ess[j].type != -1; j++)
1015 if (ess[j].type == edata->state)
1016 break;
1017
1018 DPRINTFOBJ(("%s: state=%s type=%d flags=%d "
1019 "units=%d sensor=%d\n", __func__, ess[j].desc,
1020 ess[j].type, edata->flags, edata->units, edata->sensor));
1021
1022 /* update sensor state */
1023 error = sme_sensor_upstring(dict, "state", ess[j].desc);
1024 if (error)
1025 break;
1026
1027 for (j = 0; est[j].type != -1; j++)
1028 if (est[j].type == edata->units)
1029 break;
1030
1031 /* update sensor type */
1032 error = sme_sensor_upstring(dict, "type", est[j].desc);
1033 if (error)
1034 break;
1035
1036 /* update sensor current value */
1037 error = sme_sensor_upint32(dict,
1038 "cur-value",
1039 edata->value_cur);
1040 if (error)
1041 break;
1042
1043 /*
1044 * Integer and Indicator types do not the following
1045 * values, so skip them.
1046 */
1047 if (edata->units == ENVSYS_INTEGER ||
1048 edata->units == ENVSYS_INDICATOR)
1049 continue;
1050
1051 /* update sensor flags */
1052 if (edata->flags & ENVSYS_FPERCENT) {
1053 error = sme_sensor_upbool(dict,
1054 "want-percentage",
1055 true);
1056 if (error)
1057 break;
1058 }
1059
1060 if (edata->flags & ENVSYS_FVALID_MAX) {
1061 error = sme_sensor_upint32(dict,
1062 "max-value",
1063 edata->value_max);
1064 if (error)
1065 break;
1066 }
1067
1068 if (edata->flags & ENVSYS_FVALID_MIN) {
1069 error = sme_sensor_upint32(dict,
1070 "min-value",
1071 edata->value_min);
1072 if (error)
1073 break;
1074 }
1075
1076 if (edata->flags & ENVSYS_FVALID_AVG) {
1077 error = sme_sensor_upint32(dict,
1078 "avg-value",
1079 edata->value_avg);
1080 if (error)
1081 break;
1082 }
1083
1084 /* update 'rpms' only in ENVSYS_SFANRPM. */
1085 if (edata->units == ENVSYS_SFANRPM) {
1086 error = sme_sensor_upuint32(dict,
1087 "rpms",
1088 edata->rpms);
1089 if (error)
1090 break;
1091 }
1092
1093 /* update 'rfact' only in ENVSYS_SVOLTS_[AD]C. */
1094 if (edata->units == ENVSYS_SVOLTS_AC ||
1095 edata->units == ENVSYS_SVOLTS_DC) {
1096 error = sme_sensor_upint32(dict,
1097 "rfact",
1098 edata->rfact);
1099 if (error)
1100 break;
1101 }
1102
1103 /* update 'drive-state' only in ENVSYS_DRIVE. */
1104 if (edata->units == ENVSYS_DRIVE) {
1105 for (j = 0; esds[j].type != -1; j++)
1106 if (esds[j].type == edata->value_cur)
1107 break;
1108
1109 error = sme_sensor_upstring(dict,
1110 "drive-state",
1111 esds[j].desc);
1112 if (error)
1113 break;
1114 }
1115 }
1116
1117 return error;
1118 }
1119
1120 /*
1121 * sme_userset_dictionary:
1122 *
1123 * + Parses the userland dictionary and run the appropiate
1124 * tasks that were requested.
1125 */
1126 int
1127 sme_userset_dictionary(struct sysmon_envsys *sme, prop_dictionary_t udict,
1128 prop_array_t array)
1129 {
1130 const struct sme_sensor_type *sst = sme_sensor_type;
1131 envsys_data_t *edata, *nedata;
1132 prop_dictionary_t dict;
1133 prop_object_t obj, obj1, obj2;
1134 int32_t critval;
1135 int i, invalid, error;
1136 const char *blah, *sname;
1137 bool targetfound = false;
1138
1139 error = invalid = 0;
1140 blah = sname = NULL;
1141
1142 /* get sensor's name from userland dictionary. */
1143 obj = prop_dictionary_get(udict, "sensor-name");
1144 if (prop_object_type(obj) != PROP_TYPE_STRING) {
1145 DPRINTF(("%s: sensor-name failed\n", __func__));
1146 return EINVAL;
1147 }
1148
1149 /* iterate over the sensors to find the right one */
1150 for (i = 0; i < sme->sme_nsensors; i++) {
1151 edata = &sme->sme_sensor_data[i];
1152 /* skip sensors with duplicate description */
1153 if (edata->flags & ENVSYS_FNOTVALID) {
1154 invalid++;
1155 continue;
1156 }
1157
1158 dict = prop_array_get(array, i - invalid);
1159 obj1 = prop_dictionary_get(dict, "description");
1160
1161 /* is it our sensor? */
1162 if (!prop_string_equals(obj1, obj))
1163 continue;
1164
1165 /*
1166 * Check if a new description operation was
1167 * requested by the user and set new description.
1168 */
1169 if ((obj2 = prop_dictionary_get(udict, "new-description"))) {
1170 targetfound = true;
1171 blah = prop_string_cstring_nocopy(obj2);
1172
1173 for (i = 0; i < sme->sme_nsensors; i++) {
1174 if (i == edata->sensor)
1175 continue;
1176
1177 nedata = &sme->sme_sensor_data[i];
1178 if (strcmp(blah, nedata->desc) == 0) {
1179 error = EEXIST;
1180 break;
1181 }
1182 }
1183
1184 if (error)
1185 break;
1186
1187 error = sme_sensor_upstring(dict,
1188 "description",
1189 blah);
1190 if (!error)
1191 (void)strlcpy(edata->desc,
1192 blah,
1193 sizeof(edata->desc));
1194
1195 break;
1196 }
1197
1198 /* did the user want to remove a critical capacity limit? */
1199 obj2 = prop_dictionary_get(udict, "remove-critical-cap");
1200 if (obj2 != NULL) {
1201 targetfound = true;
1202 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1203 (edata->flags & ENVSYS_FPERCENT) == 0) {
1204 error = ENOTSUP;
1205 break;
1206 }
1207
1208 sname = prop_string_cstring_nocopy(obj);
1209 error = sme_event_unregister(sname,
1210 PENVSYS_EVENT_BATT_USERCAP);
1211 if (error)
1212 break;
1213
1214 prop_dictionary_remove(dict, "critical-capacity");
1215 break;
1216 }
1217
1218 /* did the user want to remove a critical min limit? */
1219 obj2 = prop_dictionary_get(udict, "remove-cmin-limit");
1220 if (obj2 != NULL) {
1221 targetfound = true;
1222 sname = prop_string_cstring_nocopy(obj);
1223 error = sme_event_unregister(sname,
1224 PENVSYS_EVENT_USER_CRITMIN);
1225 if (error)
1226 break;
1227
1228 prop_dictionary_remove(dict, "critical-min-limit");
1229 break;
1230 }
1231
1232 /* did the user want to remove a critical max limit? */
1233 obj2 = prop_dictionary_get(udict, "remove-cmax-limit");
1234 if (obj2 != NULL) {
1235 targetfound = true;
1236 sname = prop_string_cstring_nocopy(obj);
1237 error = sme_event_unregister(sname,
1238 PENVSYS_EVENT_USER_CRITMAX);
1239 if (error)
1240 break;
1241
1242 prop_dictionary_remove(dict, "critical-max-limit");
1243 break;
1244 }
1245
1246 /* did the user want to change rfact? */
1247 obj2 = prop_dictionary_get(udict, "new-rfact");
1248 if (obj2 != NULL) {
1249 targetfound = true;
1250 if (edata->flags & ENVSYS_FCHANGERFACT)
1251 edata->rfact = prop_number_integer_value(obj2);
1252 else
1253 error = ENOTSUP;
1254
1255 break;
1256 }
1257
1258 for (i = 0; sst[i].type != -1; i++)
1259 if (sst[i].type == edata->units)
1260 break;
1261
1262 /* did the user want to set a critical capacity event? */
1263 obj2 = prop_dictionary_get(udict, "critical-capacity");
1264 if (obj2 != NULL) {
1265 targetfound = true;
1266 if ((edata->flags & ENVSYS_FMONNOTSUPP) ||
1267 (edata->flags & ENVSYS_FPERCENT) == 0) {
1268 error = ENOTSUP;
1269 break;
1270 }
1271
1272 critval = prop_number_integer_value(obj2);
1273 error = sme_event_register(dict,
1274 edata,
1275 sme->sme_name,
1276 "critical-capacity",
1277 critval,
1278 PENVSYS_EVENT_BATT_USERCAP,
1279 sst[i].crittype);
1280 break;
1281 }
1282
1283 /* did the user want to set a critical max event? */
1284 obj2 = prop_dictionary_get(udict, "critical-max-limit");
1285 if (obj2 != NULL) {
1286 targetfound = true;
1287 if (edata->units == ENVSYS_INDICATOR ||
1288 edata->flags & ENVSYS_FMONNOTSUPP) {
1289 error = ENOTSUP;
1290 break;
1291 }
1292
1293 critval = prop_number_integer_value(obj2);
1294 error = sme_event_register(dict,
1295 edata,
1296 sme->sme_name,
1297 "critical-max-limit",
1298 critval,
1299 PENVSYS_EVENT_USER_CRITMAX,
1300 sst[i].crittype);
1301 break;
1302 }
1303
1304 /* did the user want to set a critical min event? */
1305 obj2 = prop_dictionary_get(udict, "critical-min-limit");
1306 if (obj2 != NULL) {
1307 targetfound = true;
1308 if (edata->units == ENVSYS_INDICATOR ||
1309 edata->flags & ENVSYS_FMONNOTSUPP) {
1310 error = ENOTSUP;
1311 break;
1312 }
1313
1314 critval = prop_number_integer_value(obj2);
1315 error = sme_event_register(dict,
1316 edata,
1317 sme->sme_name,
1318 "critical-min-limit",
1319 critval,
1320 PENVSYS_EVENT_USER_CRITMIN,
1321 sst[i].crittype);
1322 break;
1323 }
1324 }
1325
1326 /* invalid target? return the error */
1327 if (!targetfound)
1328 error = EINVAL;
1329
1330 return error;
1331 }
1332