8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / picl / plugins / sun4u / enchilada / envd / piclenvsetup.c
blob56dcf642242eb6d57449d6742be68b97338aac54
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License, Version 1.0 only
6 * (the "License"). You may not use this file except in compliance
7 * with the License.
9 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10 * or http://www.opensolaris.org/os/licensing.
11 * See the License for the specific language governing permissions
12 * and limitations under the License.
14 * When distributing Covered Code, include this CDDL HEADER in each
15 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16 * If applicable, add the following below this CDDL HEADER, with the
17 * fields enclosed by brackets "[]" replaced with your own identifying
18 * information: Portions Copyright [yyyy] [name of copyright owner]
20 * CDDL HEADER END
23 * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 #pragma ident "%Z%%M% %I% %E% SMI"
30 * This file contains code for setting up environmental related nodes
31 * and properties in the PICL tree.
35 #include <stdio.h>
36 #include <fcntl.h>
37 #include <unistd.h>
38 #include <syslog.h>
39 #include <stdlib.h>
40 #include <limits.h>
41 #include <errno.h>
42 #include <sys/open.h>
43 #include <ctype.h>
44 #include <string.h>
45 #include <alloca.h>
46 #include <libintl.h>
47 #include <sys/systeminfo.h>
48 #include <picl.h>
49 #include <picltree.h>
50 #include <picld_pluginutil.h>
51 #include <pthread.h>
52 #include <sys/utsname.h>
53 #include <sys/systeminfo.h>
54 #include "picldefs.h"
55 #include "envd.h"
58 * Volatile property read/write function typedef
60 typedef int ptree_vol_rdfunc_t(ptree_rarg_t *parg, void *buf);
61 typedef int ptree_vol_wrfunc_t(ptree_warg_t *parg, const void *buf);
63 extern int monitor_disk_temp;
64 extern sensor_ctrl_blk_t sensor_ctrl[];
65 extern fan_ctrl_blk_t fan_ctrl[];
66 extern env_tuneable_t tuneables[];
67 extern int errno;
68 extern int ntuneables;
69 #define PROP_FAN_SPEED_UNIT_VALUE "rpm"
74 * Sensor node data structure
76 typedef struct {
77 char *parent_path; /* parent path */
78 char *sensor_name; /* sensor name */
79 env_sensor_t *sensorp; /* sensor info */
80 picl_nodehdl_t nodeh; /* sensor node handle */
81 picl_prophdl_t proph; /* "Temperature" property handle */
82 picl_prophdl_t target_proph; /* "TargetTemp" property handle */
83 } sensor_node_t;
87 * Sensor nodes array
89 static sensor_node_t sensor_nodes[] = {
90 {"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,58",
91 SENSOR_CPU0_DIE, NULL, NULL, NULL, NULL},
93 {"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,58",
94 SENSOR_CPU1_DIE, NULL, NULL, NULL, NULL},
96 {"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,58",
97 SENSOR_INT_AMB_0, NULL, NULL, NULL, NULL},
99 {"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
100 SENSOR_SYS_OUT, NULL, NULL, NULL, NULL},
102 {"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
103 SENSOR_SYS_IN, NULL, NULL, NULL, NULL},
105 {"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
106 SENSOR_INT_AMB_1, NULL, NULL, NULL, NULL},
109 #define N_SENSOR_NODES (sizeof (sensor_nodes)/sizeof (sensor_nodes[0]))
113 * Fan node data structure
115 typedef struct {
116 char *parent_path; /* parent node path */
117 char *fan_name; /* fan name */
118 env_fan_t *fanp; /* fan information */
119 char *speed_unit; /* speed unit string */
120 picl_nodehdl_t nodeh; /* "fan" node handle */
121 picl_prophdl_t proph; /* "Speed" property handle */
122 } fan_node_t;
126 * Fan node array
128 static fan_node_t fan_nodes[] = {
129 {"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,58",
130 ENV_CPU0_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
132 {"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,58",
133 ENV_CPU1_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
135 {"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
136 ENV_SYSTEM_OUT_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
138 {"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,5c",
139 ENV_SYSTEM_INTAKE_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
141 {"/platform/pci@1e,600000/isa@7/i2c@0,320/hardware-monitor@0,52",
142 ENV_DIMM_FAN, NULL, PROP_FAN_SPEED_UNIT_VALUE, NULL, NULL},
145 #define N_FAN_NODES (sizeof (fan_nodes)/sizeof (fan_nodes[0]))
148 * Disk node data structure
150 typedef struct {
151 char *parent_path; /* parent node path */
152 char *disk_name; /* disk name */
153 env_disk_t *diskp; /* disk information */
154 picl_nodehdl_t nodeh; /* "disk" node handle */
155 picl_prophdl_t proph; /* "Temperature" property handle */
156 } disk_node_t;
159 * Disk node array
161 static disk_node_t disk_nodes[] = {
162 {DISK0_NODE_PATH, ENV_DISK0, NULL, NULL, NULL},
163 {DISK1_NODE_PATH, ENV_DISK1, NULL, NULL, NULL},
165 #define N_DISK_NODES (sizeof (disk_nodes)/sizeof (disk_nodes[0]))
168 * Miscellaneous declarations
170 static void delete_sensor_nodes_and_props(void);
171 static void delete_disk_nodes_and_props(void);
172 static void delete_fan_nodes_and_props(void);
176 * Read function for volatile "Temperature" property
178 static int
179 get_current_temp(ptree_rarg_t *parg, void *buf)
181 tempr_t temp;
182 picl_prophdl_t proph;
183 sensor_node_t *snodep;
184 int i;
187 * Locate the sensor in our sensor_nodes table by matching the
188 * property handle and get its temperature.
190 proph = parg->proph;
191 for (i = 0; i < N_SENSOR_NODES; ++i) {
192 snodep = &sensor_nodes[i];
193 if (snodep->proph != proph)
194 continue;
196 if (get_temperature(snodep->sensorp, &temp) < 0)
197 break;
198 (void) memcpy(buf, (caddr_t)&temp, sizeof (tempr_t));
199 return (PICL_SUCCESS);
201 return (PICL_FAILURE);
205 * Read function for volatile "Temperature" property
207 static int
208 get_disk_temp(ptree_rarg_t *parg, void *buf)
210 tempr_t temp;
211 picl_prophdl_t proph;
212 disk_node_t *dnodep;
213 int i;
216 * Locate the sensor in our sensor_nodes table by matching the
217 * property handle and get its temperature.
219 proph = parg->proph;
220 for (i = 0; i < N_DISK_NODES; ++i) {
221 dnodep = &disk_nodes[i];
222 if (dnodep->proph != proph)
223 continue;
225 if (disk_temperature(dnodep->diskp, &temp) < 0)
226 break;
227 (void) memcpy(buf, (caddr_t)&temp, sizeof (tempr_t));
228 return (PICL_SUCCESS);
230 return (PICL_FAILURE);
234 * Read function for volatile "Speed" property on "fan" class node
236 static int
237 set_current_speed(ptree_warg_t *parg, const void *buf)
239 fanspeed_t speed;
240 picl_prophdl_t proph;
241 fan_node_t *fnodep;
242 int i, ret;
245 * Locate the fan in our fan_nodes table by matching the
246 * property handle and get fan speed.
248 proph = parg->proph;
249 for (i = 0; i < N_FAN_NODES; ++i) {
250 fnodep = &fan_nodes[i];
251 if (fnodep->proph != proph)
252 continue;
253 if (fnodep->fanp->fd == -1)
254 continue;
256 (void) memcpy((caddr_t)&speed, buf, sizeof (speed));
258 ret = set_fan_speed(fnodep->fanp, speed);
260 if (ret < 0) {
261 if (ret == -1 && errno == EBUSY)
262 return (PICL_NOTWRITABLE);
263 if (ret == -2)
264 return (PICL_INVALIDARG);
265 break;
269 return (PICL_SUCCESS);
271 return (PICL_FAILURE);
276 * Read function for volatile "Speed" property on "fan" class node
278 static int
279 get_current_speed(ptree_rarg_t *parg, void *buf)
281 fanspeed_t speed;
282 picl_prophdl_t proph;
283 fan_node_t *fnodep;
284 int i;
287 * Locate the fan in our fan_nodes table by matching the
288 * property handle and get fan speed.
290 proph = parg->proph;
291 for (i = 0; i < N_FAN_NODES; ++i) {
292 fnodep = &fan_nodes[i];
293 if (fnodep->proph != proph)
294 continue;
295 if (fnodep->fanp->fd == -1)
296 continue;
297 if (get_fan_speed(fnodep->fanp, &speed) < 0)
298 break;
300 (void) memcpy(buf, (caddr_t)&speed, sizeof (speed));
301 return (PICL_SUCCESS);
303 return (PICL_FAILURE);
307 * Create and add the specified regular property
310 static int
311 add_regular_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
312 int size, void *valbuf, picl_prophdl_t *prophp)
314 int err;
315 ptree_propinfo_t propinfo;
316 picl_prophdl_t proph;
318 err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
319 type, access, size, name, NULL, NULL);
320 if (err != PICL_SUCCESS)
321 return (err);
323 err = ptree_create_and_add_prop(nodeh, &propinfo, valbuf, &proph);
324 if (err == PICL_SUCCESS && prophp)
325 *prophp = proph;
326 return (err);
331 * Create and add the specified volatile property
333 static int
334 add_volatile_prop(picl_nodehdl_t nodeh, char *name, int type, int access,
335 int size, ptree_vol_rdfunc_t *rdfunc, ptree_vol_wrfunc_t *wrfunc,
336 picl_prophdl_t *prophp)
338 int err;
339 ptree_propinfo_t propinfo;
340 picl_prophdl_t proph;
342 err = ptree_init_propinfo(&propinfo, PTREE_PROPINFO_VERSION,
343 type, (access|PICL_VOLATILE), size, name, rdfunc, wrfunc);
344 if (err != PICL_SUCCESS)
345 return (err);
347 err = ptree_create_and_add_prop(nodeh, &propinfo, NULL, &proph);
348 if (err == PICL_SUCCESS && prophp)
349 *prophp = proph;
350 return (err);
354 * Add temperature threshold properties
356 static void
357 add_sensor_thresh_props(picl_nodehdl_t nodeh, sensor_ctrl_blk_t *threshp)
359 picl_prophdl_t proph;
361 (void) add_regular_prop(nodeh, PICL_PROP_LOW_POWER_OFF,
362 PICL_PTYPE_INT, PICL_READ,
363 sizeof (threshp->low_power_off),
364 (void *)&(threshp->low_power_off), &proph);
366 (void) add_regular_prop(nodeh, PICL_PROP_LOW_SHUTDOWN,
367 PICL_PTYPE_INT, PICL_READ,
368 sizeof (threshp->low_shutdown),
369 (void *)&(threshp->low_shutdown), &proph);
371 (void) add_regular_prop(nodeh, PICL_PROP_LOW_WARNING,
372 PICL_PTYPE_INT, PICL_READ,
373 sizeof (threshp->low_warning),
374 (void *)&(threshp->low_warning), &proph);
376 (void) add_regular_prop(nodeh, PICL_PROP_HIGH_WARNING,
377 PICL_PTYPE_INT, PICL_READ,
378 sizeof (threshp->high_warning),
379 (void *)&(threshp->high_warning), &proph);
381 (void) add_regular_prop(nodeh, PICL_PROP_HIGH_SHUTDOWN,
382 PICL_PTYPE_INT, PICL_READ,
383 sizeof (threshp->high_shutdown),
384 (void *)&(threshp->high_shutdown), &proph);
386 (void) add_regular_prop(nodeh, PICL_PROP_HIGH_POWER_OFF,
387 PICL_PTYPE_INT, PICL_READ,
388 sizeof (threshp->high_power_off),
389 (void *)&(threshp->high_power_off), &proph);
394 * Go through the sensor_nodes array and create those nodes
395 * and the Temperature property to report the temperature.
397 static int
398 add_sensor_nodes_and_props()
400 int err;
401 char *pname, *nodename, *devfs_path;
402 sensor_node_t *snodep;
403 sensor_ctrl_blk_t *threshp;
404 picl_nodehdl_t nodeh, cnodeh;
405 picl_prophdl_t proph;
406 env_sensor_t *sensorp;
407 int i;
409 for (i = 0; i < N_SENSOR_NODES; ++i) {
410 snodep = &sensor_nodes[i];
412 * Get the parent nodeh
414 err = ptree_get_node_by_path(snodep->parent_path, &nodeh);
415 if (err != PICL_SUCCESS)
416 continue;
417 sensorp = snodep->sensorp;
418 if (sensorp->present == B_FALSE)
419 continue;
421 * Create temperature-sensor node
423 nodename = snodep->sensor_name;
424 err = ptree_create_and_add_node(nodeh, nodename,
425 PICL_CLASS_TEMPERATURE_SENSOR, &cnodeh);
426 if (env_debug)
427 envd_log(LOG_INFO,
428 "Creating PICL sensor node '%s' err:%d\n",
429 nodename, err);
430 if (err != PICL_SUCCESS)
431 break;
433 /* save node handle */
434 snodep->nodeh = cnodeh;
437 * Add "devfs_path" property in child node
439 devfs_path = sensorp->devfs_path;
440 pname = PICL_PROP_DEVFS_PATH;
441 err = add_regular_prop(cnodeh, pname,
442 PICL_PTYPE_CHARSTRING, PICL_READ,
443 strlen(devfs_path)+1, (void *)devfs_path, &proph);
444 if (err != PICL_SUCCESS)
445 break;
448 * Now add volatile "temperature" volatile property
449 * in this "temperature-sensor" class node.
451 pname = PICL_PROP_TEMPERATURE;
452 err = add_volatile_prop(cnodeh, pname,
453 PICL_PTYPE_INT, PICL_READ, sizeof (tempr_t),
454 get_current_temp, NULL, &proph);
455 if (err != PICL_SUCCESS)
456 break;
458 /* Save prop handle */
459 snodep->proph = proph;
462 * Add threshold related properties
464 threshp = sensorp->es_ptr;
466 if (threshp != NULL)
467 add_sensor_thresh_props(cnodeh, threshp);
470 if (err != PICL_SUCCESS) {
471 delete_sensor_nodes_and_props();
472 if (env_debug)
473 envd_log(LOG_INFO,
474 "Can't create prop/node for sensor '%s'\n",
475 nodename);
476 return (err);
478 return (PICL_SUCCESS);
482 * Delete all sensor nodes and related properties created by the
483 * add_sensor_prop() for each sensor node in the PICL tree.
485 static void
486 delete_sensor_nodes_and_props(void)
488 sensor_node_t *snodep;
489 int i;
492 * Delete/destroy any property created in the sensed device
493 * as well as the sensor node and all properties under it.
494 * Note that deleiing/destroying a node deletes/destroys
495 * all properties within that node.
498 for (i = 0; i < N_SENSOR_NODES; ++i) {
499 snodep = &sensor_nodes[i];
500 if (snodep->nodeh != NULL) {
501 /* delete node and all properties under it */
502 (void) ptree_delete_node(snodep->nodeh);
503 (void) ptree_destroy_node(snodep->nodeh);
504 snodep->nodeh = NULL;
505 snodep->proph = NULL;
511 * Go through the disk_nodes array and create those nodes
512 * and the Temperature property to report the temperature.
514 static int
515 add_disk_nodes_and_props()
517 int err;
518 char *pname, *nodename, *devfs_path;
519 disk_node_t *dnodep;
520 picl_nodehdl_t nodeh, cnodeh;
521 picl_prophdl_t proph;
522 env_disk_t *diskp;
523 int i;
525 for (i = 0; i < N_DISK_NODES; ++i) {
526 dnodep = &disk_nodes[i];
528 * Get the parent nodeh
530 err = ptree_get_node_by_path(dnodep->parent_path, &nodeh);
531 if (err != PICL_SUCCESS) {
532 if (env_debug)
533 envd_log(LOG_ERR, "failed to get node for path %s\n",
534 dnodep->parent_path);
535 err = PICL_SUCCESS;
536 continue;
538 diskp = dnodep->diskp;
539 if (diskp->present == B_FALSE)
540 continue;
542 * Create temperature-sensor node
544 nodename = dnodep->disk_name;
545 err = ptree_create_and_add_node(nodeh, nodename,
546 PICL_CLASS_TEMPERATURE_SENSOR, &cnodeh);
547 if (env_debug)
548 envd_log(LOG_ERR,
549 "Creating PICL disk node '%s' err:%d\n",
550 nodename, err);
551 if (err != PICL_SUCCESS)
552 break;
554 /* save node handle */
555 dnodep->nodeh = cnodeh;
558 * Add "devfs_path" property in child node
560 devfs_path = diskp->devfs_path;
561 pname = PICL_PROP_DEVFS_PATH;
562 err = add_regular_prop(cnodeh, pname,
563 PICL_PTYPE_CHARSTRING, PICL_READ,
564 strlen(devfs_path)+1, (void *)devfs_path, &proph);
565 if (err != PICL_SUCCESS)
566 break;
569 * Now add volatile "temperature" volatile property
570 * in this "temperature-sensor" class node.
572 pname = PICL_PROP_TEMPERATURE;
573 err = add_volatile_prop(cnodeh, pname,
574 PICL_PTYPE_INT, PICL_READ, sizeof (tempr_t),
575 get_disk_temp, NULL, &proph);
576 if (err != PICL_SUCCESS)
577 break;
579 /* Save prop handle */
580 dnodep->proph = proph;
583 * Add threshold related properties
586 (void) add_regular_prop(cnodeh, PICL_PROP_LOW_SHUTDOWN,
587 PICL_PTYPE_INT, PICL_READ,
588 sizeof (diskp->low_shutdown),
589 (void *)&(diskp->low_shutdown), &proph);
591 (void) add_regular_prop(cnodeh, PICL_PROP_LOW_WARNING,
592 PICL_PTYPE_INT, PICL_READ,
593 sizeof (diskp->low_warning),
594 (void *)&(diskp->low_warning), &proph);
596 (void) add_regular_prop(cnodeh, PICL_PROP_HIGH_WARNING,
597 PICL_PTYPE_INT, PICL_READ,
598 sizeof (diskp->high_warning),
599 (void *)&(diskp->high_warning), &proph);
601 (void) add_regular_prop(cnodeh, PICL_PROP_HIGH_SHUTDOWN,
602 PICL_PTYPE_INT, PICL_READ,
603 sizeof (diskp->high_shutdown),
604 (void *)&(diskp->high_shutdown), &proph);
607 if (err != PICL_SUCCESS) {
608 delete_disk_nodes_and_props();
609 if (env_debug)
610 envd_log(LOG_INFO,
611 "Can't create prop/node for disk '%s'\n",
612 nodename);
613 return (err);
615 return (PICL_SUCCESS);
619 * Delete all disk nodes and related properties created by the
620 * add_disk_props() for each disk node in the PICL tree.
622 static void
623 delete_disk_nodes_and_props(void)
625 disk_node_t *dnodep;
626 int i;
629 * Delete/destroy disk node and all properties under it.
630 * Note that deleting/destroying a node deletes/destroys
631 * all properties within that node.
634 for (i = 0; i < N_DISK_NODES; ++i) {
635 dnodep = &disk_nodes[i];
636 if (dnodep->nodeh != NULL) {
637 (void) ptree_delete_node(dnodep->nodeh);
638 (void) ptree_destroy_node(dnodep->nodeh);
639 dnodep->nodeh = NULL;
640 dnodep->proph = NULL;
646 * For each entry in fan_nodes[] array, do the following:
647 * - Create specified "fan" class node.
648 * - Create "Speed" volatile propery under "fan" class node.
649 * - Create "SpeedUnit" property under "fan" class node.
651 static int
652 add_fan_nodes_and_props()
654 int err;
655 char *pname, *nodename, *devfs_path;
656 env_fan_t *fanp;
657 fan_node_t *fnodep;
658 picl_nodehdl_t nodeh, cnodeh;
659 picl_prophdl_t proph;
660 int i;
662 for (i = 0; i < N_FAN_NODES; ++i) {
664 * Add various fan nodes and properties
666 fnodep = &fan_nodes[i];
667 if (fnodep->fanp->present == B_FALSE)
668 continue;
670 * get parent nodeh
672 err = ptree_get_node_by_path(fnodep->parent_path, &nodeh);
673 if (err != PICL_SUCCESS) {
674 if (env_debug)
675 envd_log(LOG_ERR,
676 "node for %s NOT FOUND.\n", fnodep->parent_path);
677 err = PICL_SUCCESS;
678 continue;
681 * Create "fan" class node and save node handle
683 nodename = fnodep->fan_name;
684 err = ptree_create_and_add_node(nodeh, nodename,
685 PICL_CLASS_FAN, &cnodeh);
686 if (env_debug)
687 envd_log(LOG_ERR,
688 "Creating PICL fan node '%s' err:%d\n",
689 nodename, err);
691 if (err != PICL_SUCCESS)
692 break;
693 fnodep->nodeh = cnodeh;
696 * Add "devfs_path" property in child node
698 fanp = fnodep->fanp;
699 devfs_path = fanp->devfs_path;
700 pname = PICL_PROP_DEVFS_PATH;
701 err = add_regular_prop(cnodeh, pname,
702 PICL_PTYPE_CHARSTRING, PICL_READ,
703 strlen(devfs_path)+1, (void *)devfs_path, &proph);
705 if (err != PICL_SUCCESS)
707 break;
710 * Add "Speed" volatile property in this "fan"
711 * class node and save prop handle.
713 pname = PICL_PROP_FAN_SPEED;
714 if (fanp->id == DIMM_FAN_ID) {
716 * We do not permit setting of DIMM FAN speeds.
718 err = add_volatile_prop(cnodeh, pname, PICL_PTYPE_INT,
719 PICL_READ, sizeof (fanspeed_t),
720 get_current_speed,
721 NULL, &proph);
722 } else {
723 err = add_volatile_prop(cnodeh, pname, PICL_PTYPE_INT,
724 PICL_READ|PICL_WRITE, sizeof (fanspeed_t),
725 get_current_speed,
726 set_current_speed, &proph);
729 if (err != PICL_SUCCESS)
730 break;
731 fnodep->proph = proph;
734 * Add other "fan" class properties
736 pname = PICL_PROP_FAN_SPEED_UNIT;
737 err = add_regular_prop(cnodeh, pname,
738 PICL_PTYPE_CHARSTRING, PICL_READ,
739 strlen(fnodep->speed_unit)+1,
740 (void *)fnodep->speed_unit, &proph);
742 if (err != PICL_SUCCESS)
743 break;
745 if (err != PICL_SUCCESS) {
746 delete_fan_nodes_and_props();
747 if (env_debug)
748 envd_log(LOG_WARNING,
749 "Can't create prop/node for fan '%s'\n",
750 nodename);
751 return (err);
753 return (PICL_SUCCESS);
758 * Delete all fan nodes and related properties created by the
759 * add_fan_props() for each fan node in the PICL tree.
761 static void
762 delete_fan_nodes_and_props(void)
764 fan_node_t *fnodep;
765 int i;
768 * Delete/destroy fan node and all properties under it.
769 * Note that deleting/destroying a node deletes/destroys
770 * all properties within that node.
773 for (i = 0; i < N_FAN_NODES; ++i) {
774 fnodep = &fan_nodes[i];
775 if (fnodep->nodeh != NULL) {
776 (void) ptree_delete_node(fnodep->nodeh);
777 (void) ptree_destroy_node(fnodep->nodeh);
778 fnodep->nodeh = NULL;
783 * Tuneables publishing functions
785 static int
786 copy_persistent_tuneable(env_tuneable_t *tune, char *buf)
789 switch (tune->type) {
790 case PICL_PTYPE_INT : {
791 (void) memcpy((int *)tune->value,
792 buf, tune->nbytes);
793 break;
795 case PICL_PTYPE_CHARSTRING : {
796 (void) memcpy((caddr_t)tune->value,
797 buf, tune->nbytes);
798 break;
800 default : {
801 return (PICL_FAILURE);
804 return (PICL_SUCCESS);
807 static void
808 env_parse_tunables(picl_nodehdl_t rooth)
810 char nmbuf[SYS_NMLN];
811 char pname[PATH_MAX];
813 if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
814 (void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
815 (void) strlcat(pname, TUNABLE_CONF_FILE, PATH_MAX);
816 if (access(pname, R_OK) == 0) {
817 (void) picld_pluginutil_parse_config_file(rooth, pname);
818 return;
824 env_picl_setup_tuneables(void)
826 int err;
827 int i;
828 picl_nodehdl_t nodeh;
829 picl_nodehdl_t rooth;
830 picl_prophdl_t proph;
831 env_tuneable_t *tuneablep;
832 char read_buf[BUFSIZ];
834 if (ptree_get_root(&rooth) != PICL_SUCCESS) {
835 return (PICL_FAILURE);
837 err = ptree_create_and_add_node(rooth, PICL_PLUGINS_NODE,
838 PICL_CLASS_PICL, &nodeh);
839 if (err != PICL_SUCCESS)
840 return (PICL_FAILURE);
841 err = ptree_create_and_add_node(nodeh, PICL_ENVIRONMENTAL_NODE,
842 PICL_CLASS_PICL, &nodeh);
843 if (err != PICL_SUCCESS) {
844 return (PICL_FAILURE);
848 * Parse the conf file
850 env_parse_tunables(rooth);
851 for (i = 0; i < ntuneables; i++) {
852 tuneablep = &tuneables[i];
853 err = ptree_get_propval_by_name(nodeh, tuneablep->name,
854 read_buf, tuneablep->nbytes);
856 if (err != PICL_SUCCESS) {
858 * Add volitle functions to environmental node
860 err = add_volatile_prop(nodeh, tuneablep->name,
861 tuneablep->type,
862 PICL_READ|PICL_WRITE, tuneablep->nbytes,
863 tuneablep->rfunc,
864 tuneablep->wfunc, &proph);
866 tuneablep->proph = proph;
867 } else {
869 * property is persistent
871 (void) copy_persistent_tuneable(tuneablep,
872 read_buf);
876 return (PICL_SUCCESS);
880 * Find the ENVMODEL_CONF_FILE file.
882 static int
883 get_envmodel_conf_file(char *outfilename)
885 char nmbuf[SYS_NMLN];
886 char pname[PATH_MAX];
888 if (sysinfo(SI_PLATFORM, nmbuf, sizeof (nmbuf)) != -1) {
889 (void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
890 (void) strlcat(pname, ENV_CONF_FILE, PATH_MAX);
891 if (access(pname, R_OK) == 0) {
892 (void) strlcpy(outfilename, pname, PATH_MAX);
893 return (0);
897 if (sysinfo(SI_MACHINE, nmbuf, sizeof (nmbuf)) != -1) {
898 (void) snprintf(pname, PATH_MAX, PICLD_PLAT_PLUGIN_DIRF, nmbuf);
899 (void) strlcat(pname, ENV_CONF_FILE, PATH_MAX);
900 if (access(pname, R_OK) == 0) {
901 (void) strlcpy(outfilename, pname, PATH_MAX);
902 return (0);
906 (void) snprintf(pname, PATH_MAX, "%s/%s", PICLD_COMMON_PLUGIN_DIR,
907 ENV_CONF_FILE);
909 if (access(pname, R_OK) == 0) {
910 (void) strlcpy(outfilename, pname, PATH_MAX);
911 return (0);
914 return (-1);
917 /* Delete all sensor/fan nodes and any properties created by this plugin */
918 void
919 env_picl_destroy(void)
921 delete_fan_nodes_and_props();
922 delete_sensor_nodes_and_props();
923 delete_disk_nodes_and_props();
926 void
927 env_picl_setup(void)
929 int err;
930 sensor_node_t *snodep;
931 fan_node_t *fnodep;
932 disk_node_t *dnodep;
933 picl_nodehdl_t plath;
934 char fullfilename[PATH_MAX];
935 picl_nodehdl_t rooth;
936 int i;
940 * Initialize sensorp and other fields in the sensor_nodes[] array
943 for (i = 0; i < N_SENSOR_NODES; ++i) {
944 snodep = &sensor_nodes[i];
945 snodep->sensorp = sensor_lookup(snodep->sensor_name);
946 snodep->nodeh = NULL;
947 snodep->proph = NULL;
948 snodep->target_proph = NULL;
952 * Initialize fanp and other fields in the fan_nodes[] array
954 for (i = 0; i < N_FAN_NODES; ++i) {
955 fnodep = &fan_nodes[i];
956 fnodep->fanp = fan_lookup(fnodep->fan_name);
957 fnodep->nodeh = NULL;
958 fnodep->proph = NULL;
962 * Initialize diskp and other fields in the disk_nodes[] array
964 for (i = 0; i < N_DISK_NODES; ++i) {
965 dnodep = &disk_nodes[i];
966 dnodep->diskp = disk_lookup(dnodep->disk_name);
967 dnodep->nodeh = NULL;
968 dnodep->proph = NULL;
972 * Get platform handle and populate PICL tree with environmental
973 * nodes and properties
975 err = ptree_get_node_by_path("/platform", &plath);
977 if (err == PICL_SUCCESS)
978 err = add_sensor_nodes_and_props();
979 if (err == PICL_SUCCESS)
980 err = add_fan_nodes_and_props();
981 if ((err == PICL_SUCCESS) && (monitor_disk_temp))
982 err = add_disk_nodes_and_props();
985 * We can safely call delete_xxx_nodes_and_props even
986 * if nodes were not added.
989 if (err != PICL_SUCCESS) {
990 delete_fan_nodes_and_props();
991 delete_disk_nodes_and_props();
992 delete_sensor_nodes_and_props();
993 envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
994 return;
998 * Parse the envmodel.conf file and populate the PICL tree
1000 if (get_envmodel_conf_file(fullfilename) < 0)
1001 envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
1002 if (ptree_get_root(&rooth) != PICL_SUCCESS)
1003 envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);
1004 err = picld_pluginutil_parse_config_file(rooth, fullfilename);
1006 if (err != PICL_SUCCESS)
1007 envd_log(LOG_CRIT, ENVD_PICL_SETUP_FAILED);