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