17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
57c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
67c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
77c478bd9Sstevel@tonic-gate  * with the License.
87c478bd9Sstevel@tonic-gate  *
97c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
107c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
117c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
127c478bd9Sstevel@tonic-gate  * and limitations under the License.
137c478bd9Sstevel@tonic-gate  *
147c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
157c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
167c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
177c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
187c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
197c478bd9Sstevel@tonic-gate  *
207c478bd9Sstevel@tonic-gate  * CDDL HEADER END
217c478bd9Sstevel@tonic-gate  */
227c478bd9Sstevel@tonic-gate /*
237c478bd9Sstevel@tonic-gate  * Copyright (c) 1999 by Sun Microsystems, Inc.
247c478bd9Sstevel@tonic-gate  * All rights reserved.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate #include <sys/stat.h>
287c478bd9Sstevel@tonic-gate #include <fcntl.h>
297c478bd9Sstevel@tonic-gate #include <unistd.h>
307c478bd9Sstevel@tonic-gate #include <stdlib.h>
317c478bd9Sstevel@tonic-gate #include <stdio.h>
327c478bd9Sstevel@tonic-gate #include <strings.h>
337c478bd9Sstevel@tonic-gate #include <fcntl.h>
347c478bd9Sstevel@tonic-gate 
357c478bd9Sstevel@tonic-gate #include <fcode/private.h>
367c478bd9Sstevel@tonic-gate #include <fcode/log.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
397c478bd9Sstevel@tonic-gate 
407c478bd9Sstevel@tonic-gate static fc_phandle_t
fc_nodeop(common_data_t * cdp,fc_phandle_t node,char * svc)417c478bd9Sstevel@tonic-gate fc_nodeop(common_data_t *cdp, fc_phandle_t node, char *svc)
427c478bd9Sstevel@tonic-gate {
437c478bd9Sstevel@tonic-gate 	fc_cell_t hcell;
447c478bd9Sstevel@tonic-gate 	int error;
457c478bd9Sstevel@tonic-gate 
467c478bd9Sstevel@tonic-gate 	error = fc_run_priv(cdp, svc, 1, 1, fc_phandle2cell(node), &hcell);
477c478bd9Sstevel@tonic-gate 	if (error)
48*28e0ac9cSToomas Soome 		return (0);
497c478bd9Sstevel@tonic-gate 	return (fc_cell2phandle(hcell));
507c478bd9Sstevel@tonic-gate }
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate void
recurse_tree(fcode_env_t * env,device_t * d,void (* fn)(fcode_env_t *,device_t *))537c478bd9Sstevel@tonic-gate recurse_tree(fcode_env_t *env, device_t *d,
547c478bd9Sstevel@tonic-gate     void (*fn)(fcode_env_t *, device_t *))
557c478bd9Sstevel@tonic-gate {
567c478bd9Sstevel@tonic-gate 	if (d != NULL) {
577c478bd9Sstevel@tonic-gate 		device_t *p;
587c478bd9Sstevel@tonic-gate 
597c478bd9Sstevel@tonic-gate 		fn(env, d);
607c478bd9Sstevel@tonic-gate 		recurse_tree(env, d->child, fn);
617c478bd9Sstevel@tonic-gate 		recurse_tree(env, d->peer, fn);
627c478bd9Sstevel@tonic-gate 	}
637c478bd9Sstevel@tonic-gate }
647c478bd9Sstevel@tonic-gate 
657c478bd9Sstevel@tonic-gate static void
get_prom_nodeid(fcode_env_t * env,device_t * d)667c478bd9Sstevel@tonic-gate get_prom_nodeid(fcode_env_t *env, device_t *d)
677c478bd9Sstevel@tonic-gate {
687c478bd9Sstevel@tonic-gate 	common_data_t *cdp = env->private;
697c478bd9Sstevel@tonic-gate 	private_data_t *pd = d->private;
707c478bd9Sstevel@tonic-gate 	char *name;
717c478bd9Sstevel@tonic-gate 	int namelen;
727c478bd9Sstevel@tonic-gate 	char *namebuf;
737c478bd9Sstevel@tonic-gate 
747c478bd9Sstevel@tonic-gate 	if ((pd != NULL) && (pd->node)) {
757c478bd9Sstevel@tonic-gate 		if (os_get_prop_common(cdp, pd->node, "name",
767c478bd9Sstevel@tonic-gate 		    0, &namebuf, &namelen))
777c478bd9Sstevel@tonic-gate 			namebuf = "<unknown>";
787c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_UPLOAD, "Populated: %s = %p\n", namebuf,
797c478bd9Sstevel@tonic-gate 		    pd->node);
807c478bd9Sstevel@tonic-gate 		return;
817c478bd9Sstevel@tonic-gate 	}
827c478bd9Sstevel@tonic-gate 
837c478bd9Sstevel@tonic-gate 	name = get_package_name(env, d);
847c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_UPLOAD, "Node %s: %p (%p)\n", name, d, pd);
857c478bd9Sstevel@tonic-gate 	if (d->parent) {
86*28e0ac9cSToomas Soome 		private_data_t *ppd = (private_data_t *)d->parent->private;
877c478bd9Sstevel@tonic-gate 		fc_phandle_t thisnode;
887c478bd9Sstevel@tonic-gate 
897c478bd9Sstevel@tonic-gate 		if (os_get_prop_common(cdp, ppd->node, "name",
907c478bd9Sstevel@tonic-gate 		    0, &namebuf, &namelen))
917c478bd9Sstevel@tonic-gate 			namebuf = "<unknown>";
927c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_UPLOAD, "Parent: %p (%p) %s = %p\n", d->parent,
937c478bd9Sstevel@tonic-gate 		    ppd, namebuf, ppd->node);
947c478bd9Sstevel@tonic-gate 		for (thisnode = fc_nodeop(cdp, ppd->node, FC_CHILD_FCODE);
95*28e0ac9cSToomas Soome 		    thisnode != 0;
967c478bd9Sstevel@tonic-gate 		    thisnode = fc_nodeop(cdp, thisnode, FC_PEER_FCODE)) {
977c478bd9Sstevel@tonic-gate 			int status;
987c478bd9Sstevel@tonic-gate 
997c478bd9Sstevel@tonic-gate 			namebuf = "";
1007c478bd9Sstevel@tonic-gate 			namelen = 0;
1017c478bd9Sstevel@tonic-gate 			status = os_get_prop_common(cdp, thisnode, "name",
1027c478bd9Sstevel@tonic-gate 			    0, &namebuf, &namelen);
1037c478bd9Sstevel@tonic-gate 			debug_msg(DEBUG_UPLOAD, "Lookup: %p name '%s'\n"
1047c478bd9Sstevel@tonic-gate 			    " status: %d", thisnode, namebuf, status);
1057c478bd9Sstevel@tonic-gate 			if (status == 0 && strcmp(name, namebuf) == 0)
1067c478bd9Sstevel@tonic-gate 				break;
1077c478bd9Sstevel@tonic-gate 		}
1087c478bd9Sstevel@tonic-gate 		if (thisnode) {
1097c478bd9Sstevel@tonic-gate 			pd = MALLOC(sizeof (private_data_t));
1107c478bd9Sstevel@tonic-gate 			pd->common = cdp;
1117c478bd9Sstevel@tonic-gate 			pd->node = thisnode;
1127c478bd9Sstevel@tonic-gate 			pd->upload = 0;
1137c478bd9Sstevel@tonic-gate 			d->private = pd;
1147c478bd9Sstevel@tonic-gate 			add_my_handle(env, pd->node, d);
1157c478bd9Sstevel@tonic-gate 			install_property_vectors(env, d);
1167c478bd9Sstevel@tonic-gate 			debug_msg(DEBUG_UPLOAD, "Found: %p\n", thisnode);
1177c478bd9Sstevel@tonic-gate 		}
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate static void
update_nodeids(fcode_env_t * env)1227c478bd9Sstevel@tonic-gate update_nodeids(fcode_env_t *env)
1237c478bd9Sstevel@tonic-gate {
1247c478bd9Sstevel@tonic-gate 	/*
1257c478bd9Sstevel@tonic-gate 	 * We scan through the tree looking for nodes that don't have
1267c478bd9Sstevel@tonic-gate 	 * one of my structures attached, and for each of those nodes
1277c478bd9Sstevel@tonic-gate 	 * I attempt to match it with a real firmware node
1287c478bd9Sstevel@tonic-gate 	 */
1297c478bd9Sstevel@tonic-gate 	recurse_tree(env, env->root_node, get_prom_nodeid);
1307c478bd9Sstevel@tonic-gate }
1317c478bd9Sstevel@tonic-gate 
1327c478bd9Sstevel@tonic-gate static void
build_nodes(fcode_env_t * env,common_data_t * cdp,fc_phandle_t h)1337c478bd9Sstevel@tonic-gate build_nodes(fcode_env_t *env, common_data_t *cdp, fc_phandle_t h)
1347c478bd9Sstevel@tonic-gate {
1357c478bd9Sstevel@tonic-gate 	char *name;
1367c478bd9Sstevel@tonic-gate 	int len;
1377c478bd9Sstevel@tonic-gate 	int n, allocd, depth;
1387c478bd9Sstevel@tonic-gate 	fc_phandle_t p;
1397c478bd9Sstevel@tonic-gate 	device_t *current, *attach;
1407c478bd9Sstevel@tonic-gate 	private_data_t *pd;
1417c478bd9Sstevel@tonic-gate 	private_data_t **node_array;
1427c478bd9Sstevel@tonic-gate 
1437c478bd9Sstevel@tonic-gate 	/*
1447c478bd9Sstevel@tonic-gate 	 * This is not nice; new_device calls the allocate_phandle
1457c478bd9Sstevel@tonic-gate 	 * routine without exception, we need to disable the allocation
1467c478bd9Sstevel@tonic-gate 	 * while we are building the tree to the attachment point
1477c478bd9Sstevel@tonic-gate 	 * which is why the init_done variable exists.
1487c478bd9Sstevel@tonic-gate 	 */
1497c478bd9Sstevel@tonic-gate 	cdp->init_done = 0;
1507c478bd9Sstevel@tonic-gate 	node_array = NULL;
1517c478bd9Sstevel@tonic-gate 	depth = 0;
1527c478bd9Sstevel@tonic-gate 	allocd = sizeof (private_data_t *);
1537c478bd9Sstevel@tonic-gate 	do {
1547c478bd9Sstevel@tonic-gate 		node_array = REALLOC(node_array, allocd*(depth+1));
1557c478bd9Sstevel@tonic-gate 		pd = MALLOC(sizeof (private_data_t));
1567c478bd9Sstevel@tonic-gate 		pd->node = h;
1577c478bd9Sstevel@tonic-gate 		node_array[depth] = pd;
1587c478bd9Sstevel@tonic-gate 		name = NULL;
1597c478bd9Sstevel@tonic-gate 		(void) os_get_prop_common(cdp, pd->node, "name", 0, &name,
1607c478bd9Sstevel@tonic-gate 		    &len);
1617c478bd9Sstevel@tonic-gate 		if (name)
1627c478bd9Sstevel@tonic-gate 			debug_msg(DEBUG_UPLOAD, "Node: %p name: '%s'\n", h,
1637c478bd9Sstevel@tonic-gate 			    name);
1647c478bd9Sstevel@tonic-gate 		else
1657c478bd9Sstevel@tonic-gate 			log_message(MSG_ERROR, "Node: %p Unnamed node!!\n", h);
1667c478bd9Sstevel@tonic-gate 		depth++;
1677c478bd9Sstevel@tonic-gate 		h = fc_nodeop(cdp, h, FC_PARENT);
1687c478bd9Sstevel@tonic-gate 	} while (h);
1697c478bd9Sstevel@tonic-gate 
1707c478bd9Sstevel@tonic-gate 	for (n = 0; n < (depth-1); n++) {
1717c478bd9Sstevel@tonic-gate 		new_device(env);
1727c478bd9Sstevel@tonic-gate 	}
1737c478bd9Sstevel@tonic-gate 
1747c478bd9Sstevel@tonic-gate 	env->attachment_pt = current = attach = env->current_device;
1757c478bd9Sstevel@tonic-gate 
1767c478bd9Sstevel@tonic-gate 	for (n = 0; n < depth; n++) {
1777c478bd9Sstevel@tonic-gate 		pd = node_array[n];
1787c478bd9Sstevel@tonic-gate 		pd->common = cdp;
1797c478bd9Sstevel@tonic-gate 		current->private = pd;
1807c478bd9Sstevel@tonic-gate 		add_my_handle(env, pd->node, current);
1817c478bd9Sstevel@tonic-gate 		install_property_vectors(env, current);
1827c478bd9Sstevel@tonic-gate 		current = current->parent;
1837c478bd9Sstevel@tonic-gate 	}
1847c478bd9Sstevel@tonic-gate 
1857c478bd9Sstevel@tonic-gate 	for (current = attach; current != NULL; current = current->parent) {
1867c478bd9Sstevel@tonic-gate 		install_node_data(env, current);
1877c478bd9Sstevel@tonic-gate 		if (current->parent)
1887c478bd9Sstevel@tonic-gate 			finish_device(env);
1897c478bd9Sstevel@tonic-gate 	}
1907c478bd9Sstevel@tonic-gate 
1917c478bd9Sstevel@tonic-gate 	FREE(node_array);
1927c478bd9Sstevel@tonic-gate 	cdp->init_done = 2;
1937c478bd9Sstevel@tonic-gate 	update_nodeids(env);
1947c478bd9Sstevel@tonic-gate 	cdp->init_done = 1;
1957c478bd9Sstevel@tonic-gate 	cdp->first_node = 1;
1967c478bd9Sstevel@tonic-gate }
1977c478bd9Sstevel@tonic-gate 
1987c478bd9Sstevel@tonic-gate void
build_tree(fcode_env_t * env)1997c478bd9Sstevel@tonic-gate build_tree(fcode_env_t *env)
2007c478bd9Sstevel@tonic-gate {
2017c478bd9Sstevel@tonic-gate 	common_data_t *cdp = env->private;
2027c478bd9Sstevel@tonic-gate 	instance_t *ih;
2037c478bd9Sstevel@tonic-gate 
2047c478bd9Sstevel@tonic-gate 	root_node(env);
2057c478bd9Sstevel@tonic-gate 	ih = open_instance_chain(env, env->current_device, 0);
2067c478bd9Sstevel@tonic-gate 	MYSELF = ih;
2077c478bd9Sstevel@tonic-gate 	build_nodes(env, cdp, cdp->attach);
2087c478bd9Sstevel@tonic-gate 	close_instance_chain(env, ih, 0);
2097c478bd9Sstevel@tonic-gate 	MYSELF = 0;
2107c478bd9Sstevel@tonic-gate 	device_end(env);
2117c478bd9Sstevel@tonic-gate }
2127c478bd9Sstevel@tonic-gate 
2137c478bd9Sstevel@tonic-gate /*
2147c478bd9Sstevel@tonic-gate  * Installs /openprom and /packages nodes and sub-nodes.
2157c478bd9Sstevel@tonic-gate  */
2167c478bd9Sstevel@tonic-gate void
install_builtin_nodes(fcode_env_t * env)2177c478bd9Sstevel@tonic-gate install_builtin_nodes(fcode_env_t *env)
2187c478bd9Sstevel@tonic-gate {
2197c478bd9Sstevel@tonic-gate 	common_data_t *cdp = env->private;
2207c478bd9Sstevel@tonic-gate 	int saved_first_node;
2217c478bd9Sstevel@tonic-gate 	int saved_init_done;
2227c478bd9Sstevel@tonic-gate 
2237c478bd9Sstevel@tonic-gate 	if (cdp) {
2247c478bd9Sstevel@tonic-gate 		saved_first_node = cdp->first_node;
2257c478bd9Sstevel@tonic-gate 		saved_init_done = cdp->init_done;
2267c478bd9Sstevel@tonic-gate 		cdp->first_node = 0;
2277c478bd9Sstevel@tonic-gate 		cdp->init_done = 2;
2287c478bd9Sstevel@tonic-gate 		install_openprom_nodes(env);
2297c478bd9Sstevel@tonic-gate 		install_package_nodes(env);
2307c478bd9Sstevel@tonic-gate 		cdp->first_node = saved_first_node;
2317c478bd9Sstevel@tonic-gate 		cdp->init_done = saved_init_done;
2327c478bd9Sstevel@tonic-gate 	}
2337c478bd9Sstevel@tonic-gate }
2347c478bd9Sstevel@tonic-gate 
2357c478bd9Sstevel@tonic-gate 
2367c478bd9Sstevel@tonic-gate #pragma init(_init)
2377c478bd9Sstevel@tonic-gate 
2387c478bd9Sstevel@tonic-gate static void
_init(void)2397c478bd9Sstevel@tonic-gate _init(void)
2407c478bd9Sstevel@tonic-gate {
2417c478bd9Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
2427c478bd9Sstevel@tonic-gate 
2437c478bd9Sstevel@tonic-gate 	ASSERT(env);
2447c478bd9Sstevel@tonic-gate 	NOTICE;
2457c478bd9Sstevel@tonic-gate 
2467c478bd9Sstevel@tonic-gate 	FORTH(0,		"update-nodes",		update_nodeids);
2477c478bd9Sstevel@tonic-gate 	FORTH(0,		"build-tree",		build_tree);
2487c478bd9Sstevel@tonic-gate }
249