xref: /titanic_44/usr/src/lib/efcode/fcdriver/upload.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
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) 2000 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 void
create_node(fcode_env_t * env,device_t * d)43*7c478bd9Sstevel@tonic-gate create_node(fcode_env_t *env, device_t *d)
44*7c478bd9Sstevel@tonic-gate {
45*7c478bd9Sstevel@tonic-gate 	int error;
46*7c478bd9Sstevel@tonic-gate 	prop_t *p;
47*7c478bd9Sstevel@tonic-gate 	instance_t *ih;
48*7c478bd9Sstevel@tonic-gate 	char *service = FC_NEW_DEVICE;
49*7c478bd9Sstevel@tonic-gate 	private_data_t *pd, *ppd;
50*7c478bd9Sstevel@tonic-gate 	char *unit_str;
51*7c478bd9Sstevel@tonic-gate 
52*7c478bd9Sstevel@tonic-gate 	pd = (private_data_t *)d->private;
53*7c478bd9Sstevel@tonic-gate 	ppd = (private_data_t *)d->parent->private;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	ih = MYSELF;
56*7c478bd9Sstevel@tonic-gate 	MYSELF = open_instance_chain(env, d, 0);
57*7c478bd9Sstevel@tonic-gate 	p = lookup_package_property(env, "name", d);
58*7c478bd9Sstevel@tonic-gate 	if (p == NULL) {
59*7c478bd9Sstevel@tonic-gate 		forth_abort(env, "create_node: '%s' name prop not found\n",
60*7c478bd9Sstevel@tonic-gate 		    get_path(env, d));
61*7c478bd9Sstevel@tonic-gate 	}
62*7c478bd9Sstevel@tonic-gate 
63*7c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_UPLOAD, "Create Node: %p\n", pd->node);
64*7c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_UPLOAD, " Device Name: '%s'\n", p->data);
65*7c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_UPLOAD, " Parent     : %p\n", ppd->node);
66*7c478bd9Sstevel@tonic-gate 
67*7c478bd9Sstevel@tonic-gate 	my_unit(env);
68*7c478bd9Sstevel@tonic-gate 	(void) call_my_parent(env, "encode-unit");
69*7c478bd9Sstevel@tonic-gate 	unit_str = pop_a_duped_string(env, NULL);
70*7c478bd9Sstevel@tonic-gate 	if (unit_str == NULL) {
71*7c478bd9Sstevel@tonic-gate 		unit_str = STRDUP("");
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_UPLOAD, " Unit Addr  : '%s'\n", unit_str);
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	error = fc_run_priv(pd->common, FC_NEW_DEVICE, 4, 0,
77*7c478bd9Sstevel@tonic-gate 	    fc_phandle2cell(pd->node), fc_phandle2cell(ppd->node),
78*7c478bd9Sstevel@tonic-gate 	    fc_ptr2cell(unit_str), fc_ptr2cell(p->data));
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	FREE(unit_str);
81*7c478bd9Sstevel@tonic-gate 	close_instance_chain(env, MYSELF, 0);
82*7c478bd9Sstevel@tonic-gate 	MYSELF = ih;
83*7c478bd9Sstevel@tonic-gate 
84*7c478bd9Sstevel@tonic-gate 	if (error)
85*7c478bd9Sstevel@tonic-gate 		log_message(MSG_ERROR, "%s: parent: '%s' new: '%s'\n", service,
86*7c478bd9Sstevel@tonic-gate 		    get_path(env, d->parent), p->data);
87*7c478bd9Sstevel@tonic-gate }
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate static void
finish_node(fcode_env_t * env,device_t * d)90*7c478bd9Sstevel@tonic-gate finish_node(fcode_env_t *env, device_t *d)
91*7c478bd9Sstevel@tonic-gate {
92*7c478bd9Sstevel@tonic-gate 	private_data_t *pd;
93*7c478bd9Sstevel@tonic-gate 	int error;
94*7c478bd9Sstevel@tonic-gate 
95*7c478bd9Sstevel@tonic-gate 	pd = (private_data_t *)d->private;
96*7c478bd9Sstevel@tonic-gate 
97*7c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_UPLOAD, "Finish Node: %p\n", pd->node);
98*7c478bd9Sstevel@tonic-gate 
99*7c478bd9Sstevel@tonic-gate 	error = fc_run_priv(pd->common, FC_FINISH_DEVICE, 1, 0,
100*7c478bd9Sstevel@tonic-gate 	    fc_phandle2cell(pd->node));
101*7c478bd9Sstevel@tonic-gate 	if (error)
102*7c478bd9Sstevel@tonic-gate 		log_message(MSG_ERROR, "%s: failed\n", FC_FINISH_DEVICE);
103*7c478bd9Sstevel@tonic-gate }
104*7c478bd9Sstevel@tonic-gate 
105*7c478bd9Sstevel@tonic-gate static void
upload_properties(fcode_env_t * env,device_t * d)106*7c478bd9Sstevel@tonic-gate upload_properties(fcode_env_t *env, device_t *d)
107*7c478bd9Sstevel@tonic-gate {
108*7c478bd9Sstevel@tonic-gate 	prop_t *p;
109*7c478bd9Sstevel@tonic-gate 	private_data_t *pd;
110*7c478bd9Sstevel@tonic-gate 	int error;
111*7c478bd9Sstevel@tonic-gate 
112*7c478bd9Sstevel@tonic-gate 	pd = (private_data_t *)d->private;
113*7c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_UPLOAD, "Upload Properties: node %p upload: %d\n",
114*7c478bd9Sstevel@tonic-gate 	    pd->node, pd->upload);
115*7c478bd9Sstevel@tonic-gate 
116*7c478bd9Sstevel@tonic-gate 	if (!pd->upload)
117*7c478bd9Sstevel@tonic-gate 		return;
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate 	for (p = d->properties; p; p = p->next) {
120*7c478bd9Sstevel@tonic-gate 		DEBUGF(UPLOAD, print_property(env, p, " Upload: "));
121*7c478bd9Sstevel@tonic-gate 
122*7c478bd9Sstevel@tonic-gate 		error = fc_run_priv(pd->common, FC_CREATE_PROPERTY, 4, 0,
123*7c478bd9Sstevel@tonic-gate 		    fc_phandle2cell(pd->node), fc_int2cell(p->size),
124*7c478bd9Sstevel@tonic-gate 		    fc_ptr2cell(p->data), fc_ptr2cell(p->name));
125*7c478bd9Sstevel@tonic-gate 
126*7c478bd9Sstevel@tonic-gate 		if (error)
127*7c478bd9Sstevel@tonic-gate 			log_message(MSG_ERROR, "%s: '%s' failed\n",
128*7c478bd9Sstevel@tonic-gate 			    FC_CREATE_PROPERTY, p->name);
129*7c478bd9Sstevel@tonic-gate 	}
130*7c478bd9Sstevel@tonic-gate }
131*7c478bd9Sstevel@tonic-gate 
132*7c478bd9Sstevel@tonic-gate static void
upload_node(fcode_env_t * env,device_t * d)133*7c478bd9Sstevel@tonic-gate upload_node(fcode_env_t *env, device_t *d)
134*7c478bd9Sstevel@tonic-gate {
135*7c478bd9Sstevel@tonic-gate 	private_data_t *pd = (private_data_t *)d->private;
136*7c478bd9Sstevel@tonic-gate 
137*7c478bd9Sstevel@tonic-gate 	if (pd) {
138*7c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_UPLOAD, "Upload Node: dev: %p node: %p"
139*7c478bd9Sstevel@tonic-gate 		    " upload: %d\n", d, pd->node, pd->upload);
140*7c478bd9Sstevel@tonic-gate 		if (pd->upload) {
141*7c478bd9Sstevel@tonic-gate 			create_node(env, d);
142*7c478bd9Sstevel@tonic-gate 			upload_properties(env, d);
143*7c478bd9Sstevel@tonic-gate 			finish_node(env, d);
144*7c478bd9Sstevel@tonic-gate 		}
145*7c478bd9Sstevel@tonic-gate 	} else
146*7c478bd9Sstevel@tonic-gate 		debug_msg(DEBUG_UPLOAD, "Upload Node: dev: %p NULL private\n",
147*7c478bd9Sstevel@tonic-gate 		    d);
148*7c478bd9Sstevel@tonic-gate }
149*7c478bd9Sstevel@tonic-gate 
150*7c478bd9Sstevel@tonic-gate void
upload_nodes(fcode_env_t * env)151*7c478bd9Sstevel@tonic-gate upload_nodes(fcode_env_t *env)
152*7c478bd9Sstevel@tonic-gate {
153*7c478bd9Sstevel@tonic-gate 	debug_msg(DEBUG_UPLOAD, "Upload Nodes: Recursing Tree\n");
154*7c478bd9Sstevel@tonic-gate 	recurse_tree(env, env->root_node, upload_node);
155*7c478bd9Sstevel@tonic-gate }
156*7c478bd9Sstevel@tonic-gate 
157*7c478bd9Sstevel@tonic-gate void
validate_nodes(fcode_env_t * env)158*7c478bd9Sstevel@tonic-gate validate_nodes(fcode_env_t *env)
159*7c478bd9Sstevel@tonic-gate {
160*7c478bd9Sstevel@tonic-gate 	int error;
161*7c478bd9Sstevel@tonic-gate 	common_data_t *cdp = env->private;
162*7c478bd9Sstevel@tonic-gate 
163*7c478bd9Sstevel@tonic-gate 	error = ioctl(cdp->fcode_fd, FC_VALIDATE);
164*7c478bd9Sstevel@tonic-gate }
165*7c478bd9Sstevel@tonic-gate 
166*7c478bd9Sstevel@tonic-gate #pragma init(_init)
167*7c478bd9Sstevel@tonic-gate 
168*7c478bd9Sstevel@tonic-gate static void
_init(void)169*7c478bd9Sstevel@tonic-gate _init(void)
170*7c478bd9Sstevel@tonic-gate {
171*7c478bd9Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
172*7c478bd9Sstevel@tonic-gate 
173*7c478bd9Sstevel@tonic-gate 	ASSERT(env);
174*7c478bd9Sstevel@tonic-gate 	NOTICE;
175*7c478bd9Sstevel@tonic-gate 
176*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"upload-nodes",		upload_nodes);
177*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"validate-nodes",	validate_nodes);
178*7c478bd9Sstevel@tonic-gate }
179