1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate  * CDDL HEADER START
3*0Sstevel@tonic-gate  *
4*0Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*0Sstevel@tonic-gate  * with the License.
8*0Sstevel@tonic-gate  *
9*0Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate  * and limitations under the License.
13*0Sstevel@tonic-gate  *
14*0Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate  *
20*0Sstevel@tonic-gate  * CDDL HEADER END
21*0Sstevel@tonic-gate  */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
24*0Sstevel@tonic-gate  * Use is subject to license terms.
25*0Sstevel@tonic-gate  */
26*0Sstevel@tonic-gate 
27*0Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*0Sstevel@tonic-gate 
29*0Sstevel@tonic-gate /*
30*0Sstevel@tonic-gate  * PICL Littleneck platform plug-in to create environment tree nodes.
31*0Sstevel@tonic-gate  */
32*0Sstevel@tonic-gate #define	_POSIX_PRIORITY_SCHEDULING 1
33*0Sstevel@tonic-gate 
34*0Sstevel@tonic-gate #include <picl.h>
35*0Sstevel@tonic-gate #include <picltree.h>
36*0Sstevel@tonic-gate #include <stdio.h>
37*0Sstevel@tonic-gate #include <time.h>
38*0Sstevel@tonic-gate #include <fcntl.h>
39*0Sstevel@tonic-gate #include <unistd.h>
40*0Sstevel@tonic-gate #include <stdlib.h>
41*0Sstevel@tonic-gate #include <stdio.h>
42*0Sstevel@tonic-gate #include <libintl.h>
43*0Sstevel@tonic-gate #include <limits.h>
44*0Sstevel@tonic-gate #include <ctype.h>
45*0Sstevel@tonic-gate #include <errno.h>
46*0Sstevel@tonic-gate #include <semaphore.h>
47*0Sstevel@tonic-gate #include <syslog.h>
48*0Sstevel@tonic-gate #include <string.h>
49*0Sstevel@tonic-gate #include <sys/types.h>
50*0Sstevel@tonic-gate #include <sys/systeminfo.h>
51*0Sstevel@tonic-gate #include <psvc_objects.h>
52*0Sstevel@tonic-gate 
53*0Sstevel@tonic-gate static psvc_opaque_t hdlp;
54*0Sstevel@tonic-gate 
55*0Sstevel@tonic-gate #define	PSVC_PLUGIN_VERSION	PICLD_PLUGIN_VERSION_1
56*0Sstevel@tonic-gate 
57*0Sstevel@tonic-gate #pragma init(psvc_psr_plugin_register)	/* place in .init section */
58*0Sstevel@tonic-gate 
59*0Sstevel@tonic-gate typedef struct {
60*0Sstevel@tonic-gate 	char	name[32];
61*0Sstevel@tonic-gate 	picl_nodehdl_t  node;
62*0Sstevel@tonic-gate } picl_psvc_t;
63*0Sstevel@tonic-gate 
64*0Sstevel@tonic-gate extern struct handle {
65*0Sstevel@tonic-gate 	uint32_t	obj_count;
66*0Sstevel@tonic-gate 	picl_psvc_t *objects;
67*0Sstevel@tonic-gate 	FILE *fp;
68*0Sstevel@tonic-gate } psvc_hdl;
69*0Sstevel@tonic-gate 
70*0Sstevel@tonic-gate void psvc_psr_plugin_init(void);
71*0Sstevel@tonic-gate void psvc_psr_plugin_fini(void);
72*0Sstevel@tonic-gate 
73*0Sstevel@tonic-gate picld_plugin_reg_t psvc_psr_reg = {
74*0Sstevel@tonic-gate 	PSVC_PLUGIN_VERSION,
75*0Sstevel@tonic-gate 	PICLD_PLUGIN_CRITICAL,
76*0Sstevel@tonic-gate 	"PSVC_PSR",
77*0Sstevel@tonic-gate 	psvc_psr_plugin_init,
78*0Sstevel@tonic-gate 	psvc_psr_plugin_fini
79*0Sstevel@tonic-gate };
80*0Sstevel@tonic-gate 
81*0Sstevel@tonic-gate #define	PSVC_INIT_ERR		gettext("%s: Error in psvc_init(): %s\n")
82*0Sstevel@tonic-gate #define	PTREE_DELETE_NODE_ERR	gettext("%s: ptree_delete_node() failed: %s\n")
83*0Sstevel@tonic-gate #define	PTREE_GET_NODE_ERR			\
84*0Sstevel@tonic-gate 	gettext("%s: ptree_get_node_by_path() failed: %s\n")
85*0Sstevel@tonic-gate 
86*0Sstevel@tonic-gate extern int ptree_get_node_by_path(const char *, picl_nodehdl_t *);
87*0Sstevel@tonic-gate 
88*0Sstevel@tonic-gate struct node_file {
89*0Sstevel@tonic-gate 	char	path[256];
90*0Sstevel@tonic-gate 	char	file[256];
91*0Sstevel@tonic-gate } dev_pr_info[] = {
92*0Sstevel@tonic-gate {"/SYSTEM/CPU0_MOD_CARD",
93*0Sstevel@tonic-gate 	"/devices/pci@8,700000/ebus@5/i2c@1,30/temperature@0,30:die_temp"},
94*0Sstevel@tonic-gate {"/SYSTEM/CPU1_MOD_CARD",
95*0Sstevel@tonic-gate 	"/devices/pci@8,700000/ebus@5/i2c@1,30/temperature@0,98:die_temp"},
96*0Sstevel@tonic-gate {"/SYSTEM/AT24C64_A0_1",
97*0Sstevel@tonic-gate 	"/devices/pci@8,700000/ebus@5/i2c@1,2e/dimm@1,a0:dimm"},
98*0Sstevel@tonic-gate {"/SYSTEM/AT24C64_A2_1",
99*0Sstevel@tonic-gate 	"/devices/pci@8,700000/ebus@5/i2c@1,2e/dimm@1,a2:dimm"},
100*0Sstevel@tonic-gate {"/SYSTEM/AT24C64_A4_1",
101*0Sstevel@tonic-gate 	"/devices/pci@8,700000/ebus@5/i2c@1,2e/dimm@1,a4:dimm"},
102*0Sstevel@tonic-gate {"/SYSTEM/AT24C64_A6_1",
103*0Sstevel@tonic-gate 	"/devices/pci@8,700000/ebus@5/i2c@1,2e/dimm@1,a6:dimm"},
104*0Sstevel@tonic-gate {"/SYSTEM/AT24C64_A8_1",
105*0Sstevel@tonic-gate 	"/devices/pci@8,700000/ebus@5/i2c@1,2e/dimm@1,a8:dimm"},
106*0Sstevel@tonic-gate {"/SYSTEM/AT24C64_AA_1",
107*0Sstevel@tonic-gate 	"/devices/pci@8,700000/ebus@5/i2c@1,2e/dimm@1,aa:dimm"},
108*0Sstevel@tonic-gate {"/SYSTEM/AT24C64_AC_1",
109*0Sstevel@tonic-gate 	"/devices/pci@8,700000/ebus@5/i2c@1,2e/dimm@1,ac:dimm"},
110*0Sstevel@tonic-gate {"/SYSTEM/AT24C64_AE_1",
111*0Sstevel@tonic-gate 	"/devices/pci@8,700000/ebus@5/i2c@1,2e/dimm@1,ae:dimm"}
112*0Sstevel@tonic-gate };
113*0Sstevel@tonic-gate #define	DEV_PR_COUNT (sizeof (dev_pr_info) / sizeof (struct node_file))
114*0Sstevel@tonic-gate 
init_err(char * fmt,char * arg1,char * arg2)115*0Sstevel@tonic-gate static void init_err(char *fmt, char *arg1, char *arg2)
116*0Sstevel@tonic-gate {
117*0Sstevel@tonic-gate 	char msg[256];
118*0Sstevel@tonic-gate 
119*0Sstevel@tonic-gate 	sprintf(msg, fmt, arg1, arg2);
120*0Sstevel@tonic-gate 	syslog(LOG_ERR, msg);
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate void
psvc_psr_plugin_init(void)124*0Sstevel@tonic-gate psvc_psr_plugin_init(void)
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate 	char *funcname = "psvc_plugin_init";
127*0Sstevel@tonic-gate 	int32_t i;
128*0Sstevel@tonic-gate 	int err;
129*0Sstevel@tonic-gate 	boolean_t present;
130*0Sstevel@tonic-gate 	/*
131*0Sstevel@tonic-gate 	 * So the volatile read/write routines can retrieve data from
132*0Sstevel@tonic-gate 	 * psvc or picl
133*0Sstevel@tonic-gate 	 */
134*0Sstevel@tonic-gate 	err = psvc_init(&hdlp);
135*0Sstevel@tonic-gate 	if (err != 0) {
136*0Sstevel@tonic-gate 		init_err(PSVC_INIT_ERR, funcname, strerror(errno));
137*0Sstevel@tonic-gate 
138*0Sstevel@tonic-gate 	}
139*0Sstevel@tonic-gate 
140*0Sstevel@tonic-gate 	/*
141*0Sstevel@tonic-gate 	 * Remove nodes whose devices aren't present from the picl tree.
142*0Sstevel@tonic-gate 	 */
143*0Sstevel@tonic-gate 	for (i = 0; i < psvc_hdl.obj_count; ++i) {
144*0Sstevel@tonic-gate 		picl_psvc_t *objp;
145*0Sstevel@tonic-gate 		uint64_t features;
146*0Sstevel@tonic-gate 		objp = &psvc_hdl.objects[i];
147*0Sstevel@tonic-gate 
148*0Sstevel@tonic-gate 		err = psvc_get_attr(hdlp, objp->name, PSVC_PRESENCE_ATTR,
149*0Sstevel@tonic-gate 			&present);
150*0Sstevel@tonic-gate 		if (err != PSVC_SUCCESS)
151*0Sstevel@tonic-gate 			continue;
152*0Sstevel@tonic-gate 		err = psvc_get_attr(hdlp, objp->name, PSVC_FEATURES_ATTR,
153*0Sstevel@tonic-gate 			&features);
154*0Sstevel@tonic-gate 		if (err != PSVC_SUCCESS)
155*0Sstevel@tonic-gate 			continue;
156*0Sstevel@tonic-gate 		if ((features & (PSVC_DEV_HOTPLUG | PSVC_DEV_OPTION)) &&
157*0Sstevel@tonic-gate 			(present == PSVC_ABSENT)) {
158*0Sstevel@tonic-gate 			err = ptree_delete_node(objp->node);
159*0Sstevel@tonic-gate 			if (err != 0) {
160*0Sstevel@tonic-gate 				init_err(PTREE_DELETE_NODE_ERR, funcname,
161*0Sstevel@tonic-gate 					picl_strerror(err));
162*0Sstevel@tonic-gate 				return;
163*0Sstevel@tonic-gate 			}
164*0Sstevel@tonic-gate 		}
165*0Sstevel@tonic-gate 	}
166*0Sstevel@tonic-gate 
167*0Sstevel@tonic-gate 	/*
168*0Sstevel@tonic-gate 	 * Remove PICL device nodes if their /devices file isn't present or
169*0Sstevel@tonic-gate 	 * if the device file is present but the open returns ENXIO
170*0Sstevel@tonic-gate 	 * which indicates that the node file doesn't represent a device
171*0Sstevel@tonic-gate 	 * tree node and is probably a relic from some previous boot config
172*0Sstevel@tonic-gate 	 */
173*0Sstevel@tonic-gate 	for (i = 0; i < DEV_PR_COUNT; ++i) {
174*0Sstevel@tonic-gate 		picl_nodehdl_t	dev_pr_node;
175*0Sstevel@tonic-gate 		int fd;
176*0Sstevel@tonic-gate 		fd = open(dev_pr_info[i].file, O_RDONLY);
177*0Sstevel@tonic-gate 		if (fd != -1) {
178*0Sstevel@tonic-gate 			close(fd);
179*0Sstevel@tonic-gate 			continue;
180*0Sstevel@tonic-gate 		}
181*0Sstevel@tonic-gate 		if ((errno != ENOENT) && (errno != ENXIO))
182*0Sstevel@tonic-gate 			continue;
183*0Sstevel@tonic-gate 
184*0Sstevel@tonic-gate 		err = ptree_get_node_by_path(dev_pr_info[i].path, &dev_pr_node);
185*0Sstevel@tonic-gate 		if (err != 0) {
186*0Sstevel@tonic-gate 			init_err(PTREE_GET_NODE_ERR, funcname,
187*0Sstevel@tonic-gate 				picl_strerror(err));
188*0Sstevel@tonic-gate 			return;
189*0Sstevel@tonic-gate 		}
190*0Sstevel@tonic-gate 
191*0Sstevel@tonic-gate 		err = ptree_delete_node(dev_pr_node);
192*0Sstevel@tonic-gate 		if (err != 0) {
193*0Sstevel@tonic-gate 			init_err(PTREE_DELETE_NODE_ERR, funcname,
194*0Sstevel@tonic-gate 				picl_strerror(err));
195*0Sstevel@tonic-gate 			return;
196*0Sstevel@tonic-gate 		}
197*0Sstevel@tonic-gate 	}
198*0Sstevel@tonic-gate 	free(psvc_hdl.objects);
199*0Sstevel@tonic-gate }
200*0Sstevel@tonic-gate 
201*0Sstevel@tonic-gate void
psvc_psr_plugin_fini(void)202*0Sstevel@tonic-gate psvc_psr_plugin_fini(void)
203*0Sstevel@tonic-gate {
204*0Sstevel@tonic-gate 	psvc_fini(hdlp);
205*0Sstevel@tonic-gate }
206*0Sstevel@tonic-gate 
207*0Sstevel@tonic-gate void
psvc_psr_plugin_register(void)208*0Sstevel@tonic-gate psvc_psr_plugin_register(void)
209*0Sstevel@tonic-gate {
210*0Sstevel@tonic-gate 	picld_plugin_register(&psvc_psr_reg);
211*0Sstevel@tonic-gate }
212