xref: /onnv-gate/usr/src/lib/efcode/fcdriver/build_tree.c (revision 0:68f95e015346)
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 (c) 1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate  * All rights reserved.
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 #include <sys/stat.h>
30*0Sstevel@tonic-gate #include <fcntl.h>
31*0Sstevel@tonic-gate #include <unistd.h>
32*0Sstevel@tonic-gate #include <stdlib.h>
33*0Sstevel@tonic-gate #include <stdio.h>
34*0Sstevel@tonic-gate #include <strings.h>
35*0Sstevel@tonic-gate #include <fcntl.h>
36*0Sstevel@tonic-gate 
37*0Sstevel@tonic-gate #include <fcode/private.h>
38*0Sstevel@tonic-gate #include <fcode/log.h>
39*0Sstevel@tonic-gate 
40*0Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
41*0Sstevel@tonic-gate 
42*0Sstevel@tonic-gate static fc_phandle_t
fc_nodeop(common_data_t * cdp,fc_phandle_t node,char * svc)43*0Sstevel@tonic-gate fc_nodeop(common_data_t *cdp, fc_phandle_t node, char *svc)
44*0Sstevel@tonic-gate {
45*0Sstevel@tonic-gate 	fc_cell_t hcell;
46*0Sstevel@tonic-gate 	int error;
47*0Sstevel@tonic-gate 
48*0Sstevel@tonic-gate 	error = fc_run_priv(cdp, svc, 1, 1, fc_phandle2cell(node), &hcell);
49*0Sstevel@tonic-gate 	if (error)
50*0Sstevel@tonic-gate 		return (NULL);
51*0Sstevel@tonic-gate 	return (fc_cell2phandle(hcell));
52*0Sstevel@tonic-gate }
53*0Sstevel@tonic-gate 
54*0Sstevel@tonic-gate void
recurse_tree(fcode_env_t * env,device_t * d,void (* fn)(fcode_env_t *,device_t *))55*0Sstevel@tonic-gate recurse_tree(fcode_env_t *env, device_t *d,
56*0Sstevel@tonic-gate     void (*fn)(fcode_env_t *, device_t *))
57*0Sstevel@tonic-gate {
58*0Sstevel@tonic-gate 	if (d != NULL) {
59*0Sstevel@tonic-gate 		device_t *p;
60*0Sstevel@tonic-gate 
61*0Sstevel@tonic-gate 		fn(env, d);
62*0Sstevel@tonic-gate 		recurse_tree(env, d->child, fn);
63*0Sstevel@tonic-gate 		recurse_tree(env, d->peer, fn);
64*0Sstevel@tonic-gate 	}
65*0Sstevel@tonic-gate }
66*0Sstevel@tonic-gate 
67*0Sstevel@tonic-gate static void
get_prom_nodeid(fcode_env_t * env,device_t * d)68*0Sstevel@tonic-gate get_prom_nodeid(fcode_env_t *env, device_t *d)
69*0Sstevel@tonic-gate {
70*0Sstevel@tonic-gate 	common_data_t *cdp = env->private;
71*0Sstevel@tonic-gate 	private_data_t *pd = d->private;
72*0Sstevel@tonic-gate 	char *name;
73*0Sstevel@tonic-gate 	int namelen;
74*0Sstevel@tonic-gate 	char *namebuf;
75*0Sstevel@tonic-gate 
76*0Sstevel@tonic-gate 	if ((pd != NULL) && (pd->node)) {
77*0Sstevel@tonic-gate 		if (os_get_prop_common(cdp, pd->node, "name",
78*0Sstevel@tonic-gate 		    0, &namebuf, &namelen))
79*0Sstevel@tonic-gate 			namebuf = "<unknown>";
80*0Sstevel@tonic-gate 		debug_msg(DEBUG_UPLOAD, "Populated: %s = %p\n", namebuf,
81*0Sstevel@tonic-gate 		    pd->node);
82*0Sstevel@tonic-gate 		return;
83*0Sstevel@tonic-gate 	}
84*0Sstevel@tonic-gate 
85*0Sstevel@tonic-gate 	name = get_package_name(env, d);
86*0Sstevel@tonic-gate 	debug_msg(DEBUG_UPLOAD, "Node %s: %p (%p)\n", name, d, pd);
87*0Sstevel@tonic-gate 	if (d->parent) {
88*0Sstevel@tonic-gate 		private_data_t *ppd = (private_data_t *) d->parent->private;
89*0Sstevel@tonic-gate 		fc_phandle_t thisnode;
90*0Sstevel@tonic-gate 
91*0Sstevel@tonic-gate 		if (os_get_prop_common(cdp, ppd->node, "name",
92*0Sstevel@tonic-gate 		    0, &namebuf, &namelen))
93*0Sstevel@tonic-gate 			namebuf = "<unknown>";
94*0Sstevel@tonic-gate 		debug_msg(DEBUG_UPLOAD, "Parent: %p (%p) %s = %p\n", d->parent,
95*0Sstevel@tonic-gate 		    ppd, namebuf, ppd->node);
96*0Sstevel@tonic-gate 		for (thisnode = fc_nodeop(cdp, ppd->node, FC_CHILD_FCODE);
97*0Sstevel@tonic-gate 		    thisnode != NULL;
98*0Sstevel@tonic-gate 		    thisnode = fc_nodeop(cdp, thisnode, FC_PEER_FCODE)) {
99*0Sstevel@tonic-gate 			int status;
100*0Sstevel@tonic-gate 
101*0Sstevel@tonic-gate 			namebuf = "";
102*0Sstevel@tonic-gate 			namelen = 0;
103*0Sstevel@tonic-gate 			status = os_get_prop_common(cdp, thisnode, "name",
104*0Sstevel@tonic-gate 			    0, &namebuf, &namelen);
105*0Sstevel@tonic-gate 			debug_msg(DEBUG_UPLOAD, "Lookup: %p name '%s'\n"
106*0Sstevel@tonic-gate 			    " status: %d", thisnode, namebuf, status);
107*0Sstevel@tonic-gate 			if (status == 0 && strcmp(name, namebuf) == 0)
108*0Sstevel@tonic-gate 				break;
109*0Sstevel@tonic-gate 		}
110*0Sstevel@tonic-gate 		if (thisnode) {
111*0Sstevel@tonic-gate 			pd = MALLOC(sizeof (private_data_t));
112*0Sstevel@tonic-gate 			pd->common = cdp;
113*0Sstevel@tonic-gate 			pd->node = thisnode;
114*0Sstevel@tonic-gate 			pd->upload = 0;
115*0Sstevel@tonic-gate 			d->private = pd;
116*0Sstevel@tonic-gate 			add_my_handle(env, pd->node, d);
117*0Sstevel@tonic-gate 			install_property_vectors(env, d);
118*0Sstevel@tonic-gate 			debug_msg(DEBUG_UPLOAD, "Found: %p\n", thisnode);
119*0Sstevel@tonic-gate 		}
120*0Sstevel@tonic-gate 	}
121*0Sstevel@tonic-gate }
122*0Sstevel@tonic-gate 
123*0Sstevel@tonic-gate static void
update_nodeids(fcode_env_t * env)124*0Sstevel@tonic-gate update_nodeids(fcode_env_t *env)
125*0Sstevel@tonic-gate {
126*0Sstevel@tonic-gate 	/*
127*0Sstevel@tonic-gate 	 * We scan through the tree looking for nodes that don't have
128*0Sstevel@tonic-gate 	 * one of my structures attached, and for each of those nodes
129*0Sstevel@tonic-gate 	 * I attempt to match it with a real firmware node
130*0Sstevel@tonic-gate 	 */
131*0Sstevel@tonic-gate 	recurse_tree(env, env->root_node, get_prom_nodeid);
132*0Sstevel@tonic-gate }
133*0Sstevel@tonic-gate 
134*0Sstevel@tonic-gate static void
build_nodes(fcode_env_t * env,common_data_t * cdp,fc_phandle_t h)135*0Sstevel@tonic-gate build_nodes(fcode_env_t *env, common_data_t *cdp, fc_phandle_t h)
136*0Sstevel@tonic-gate {
137*0Sstevel@tonic-gate 	char *name;
138*0Sstevel@tonic-gate 	int len;
139*0Sstevel@tonic-gate 	int n, allocd, depth;
140*0Sstevel@tonic-gate 	fc_phandle_t p;
141*0Sstevel@tonic-gate 	device_t *current, *attach;
142*0Sstevel@tonic-gate 	private_data_t *pd;
143*0Sstevel@tonic-gate 	private_data_t **node_array;
144*0Sstevel@tonic-gate 
145*0Sstevel@tonic-gate 	/*
146*0Sstevel@tonic-gate 	 * This is not nice; new_device calls the allocate_phandle
147*0Sstevel@tonic-gate 	 * routine without exception, we need to disable the allocation
148*0Sstevel@tonic-gate 	 * while we are building the tree to the attachment point
149*0Sstevel@tonic-gate 	 * which is why the init_done variable exists.
150*0Sstevel@tonic-gate 	 */
151*0Sstevel@tonic-gate 	cdp->init_done = 0;
152*0Sstevel@tonic-gate 	node_array = NULL;
153*0Sstevel@tonic-gate 	depth = 0;
154*0Sstevel@tonic-gate 	allocd = sizeof (private_data_t *);
155*0Sstevel@tonic-gate 	do {
156*0Sstevel@tonic-gate 		node_array = REALLOC(node_array, allocd*(depth+1));
157*0Sstevel@tonic-gate 		pd = MALLOC(sizeof (private_data_t));
158*0Sstevel@tonic-gate 		pd->node = h;
159*0Sstevel@tonic-gate 		node_array[depth] = pd;
160*0Sstevel@tonic-gate 		name = NULL;
161*0Sstevel@tonic-gate 		(void) os_get_prop_common(cdp, pd->node, "name", 0, &name,
162*0Sstevel@tonic-gate 		    &len);
163*0Sstevel@tonic-gate 		if (name)
164*0Sstevel@tonic-gate 			debug_msg(DEBUG_UPLOAD, "Node: %p name: '%s'\n", h,
165*0Sstevel@tonic-gate 			    name);
166*0Sstevel@tonic-gate 		else
167*0Sstevel@tonic-gate 			log_message(MSG_ERROR, "Node: %p Unnamed node!!\n", h);
168*0Sstevel@tonic-gate 		depth++;
169*0Sstevel@tonic-gate 		h = fc_nodeop(cdp, h, FC_PARENT);
170*0Sstevel@tonic-gate 	} while (h);
171*0Sstevel@tonic-gate 
172*0Sstevel@tonic-gate 	for (n = 0; n < (depth-1); n++) {
173*0Sstevel@tonic-gate 		new_device(env);
174*0Sstevel@tonic-gate 	}
175*0Sstevel@tonic-gate 
176*0Sstevel@tonic-gate 	env->attachment_pt = current = attach = env->current_device;
177*0Sstevel@tonic-gate 
178*0Sstevel@tonic-gate 	for (n = 0; n < depth; n++) {
179*0Sstevel@tonic-gate 		pd = node_array[n];
180*0Sstevel@tonic-gate 		pd->common = cdp;
181*0Sstevel@tonic-gate 		current->private = pd;
182*0Sstevel@tonic-gate 		add_my_handle(env, pd->node, current);
183*0Sstevel@tonic-gate 		install_property_vectors(env, current);
184*0Sstevel@tonic-gate 		current = current->parent;
185*0Sstevel@tonic-gate 	}
186*0Sstevel@tonic-gate 
187*0Sstevel@tonic-gate 	for (current = attach; current != NULL; current = current->parent) {
188*0Sstevel@tonic-gate 		install_node_data(env, current);
189*0Sstevel@tonic-gate 		if (current->parent)
190*0Sstevel@tonic-gate 			finish_device(env);
191*0Sstevel@tonic-gate 	}
192*0Sstevel@tonic-gate 
193*0Sstevel@tonic-gate 	FREE(node_array);
194*0Sstevel@tonic-gate 	cdp->init_done = 2;
195*0Sstevel@tonic-gate 	update_nodeids(env);
196*0Sstevel@tonic-gate 	cdp->init_done = 1;
197*0Sstevel@tonic-gate 	cdp->first_node = 1;
198*0Sstevel@tonic-gate }
199*0Sstevel@tonic-gate 
200*0Sstevel@tonic-gate void
build_tree(fcode_env_t * env)201*0Sstevel@tonic-gate build_tree(fcode_env_t *env)
202*0Sstevel@tonic-gate {
203*0Sstevel@tonic-gate 	common_data_t *cdp = env->private;
204*0Sstevel@tonic-gate 	instance_t *ih;
205*0Sstevel@tonic-gate 
206*0Sstevel@tonic-gate 	root_node(env);
207*0Sstevel@tonic-gate 	ih = open_instance_chain(env, env->current_device, 0);
208*0Sstevel@tonic-gate 	MYSELF = ih;
209*0Sstevel@tonic-gate 	build_nodes(env, cdp, cdp->attach);
210*0Sstevel@tonic-gate 	close_instance_chain(env, ih, 0);
211*0Sstevel@tonic-gate 	MYSELF = 0;
212*0Sstevel@tonic-gate 	device_end(env);
213*0Sstevel@tonic-gate }
214*0Sstevel@tonic-gate 
215*0Sstevel@tonic-gate /*
216*0Sstevel@tonic-gate  * Installs /openprom and /packages nodes and sub-nodes.
217*0Sstevel@tonic-gate  */
218*0Sstevel@tonic-gate void
install_builtin_nodes(fcode_env_t * env)219*0Sstevel@tonic-gate install_builtin_nodes(fcode_env_t *env)
220*0Sstevel@tonic-gate {
221*0Sstevel@tonic-gate 	common_data_t *cdp = env->private;
222*0Sstevel@tonic-gate 	int saved_first_node;
223*0Sstevel@tonic-gate 	int saved_init_done;
224*0Sstevel@tonic-gate 
225*0Sstevel@tonic-gate 	if (cdp) {
226*0Sstevel@tonic-gate 		saved_first_node = cdp->first_node;
227*0Sstevel@tonic-gate 		saved_init_done = cdp->init_done;
228*0Sstevel@tonic-gate 		cdp->first_node = 0;
229*0Sstevel@tonic-gate 		cdp->init_done = 2;
230*0Sstevel@tonic-gate 		install_openprom_nodes(env);
231*0Sstevel@tonic-gate 		install_package_nodes(env);
232*0Sstevel@tonic-gate 		cdp->first_node = saved_first_node;
233*0Sstevel@tonic-gate 		cdp->init_done = saved_init_done;
234*0Sstevel@tonic-gate 	}
235*0Sstevel@tonic-gate }
236*0Sstevel@tonic-gate 
237*0Sstevel@tonic-gate 
238*0Sstevel@tonic-gate #pragma init(_init)
239*0Sstevel@tonic-gate 
240*0Sstevel@tonic-gate static void
_init(void)241*0Sstevel@tonic-gate _init(void)
242*0Sstevel@tonic-gate {
243*0Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
244*0Sstevel@tonic-gate 
245*0Sstevel@tonic-gate 	ASSERT(env);
246*0Sstevel@tonic-gate 	NOTICE;
247*0Sstevel@tonic-gate 
248*0Sstevel@tonic-gate 	FORTH(0,		"update-nodes",		update_nodeids);
249*0Sstevel@tonic-gate 	FORTH(0,		"build-tree",		build_tree);
250*0Sstevel@tonic-gate }
251