xref: /titanic_44/usr/src/lib/efcode/fcdriver/upload.c (revision fcf3ce441efd61da9bb2884968af01cb7c1452cc)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright (c) 2000 by Sun Microsystems, Inc.
24  * All rights reserved.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 #include <sys/stat.h>
30 #include <fcntl.h>
31 #include <unistd.h>
32 #include <stdlib.h>
33 #include <stdio.h>
34 #include <strings.h>
35 #include <fcntl.h>
36 
37 #include <fcode/private.h>
38 #include <fcode/log.h>
39 
40 #include <fcdriver/fcdriver.h>
41 
42 static void
43 create_node(fcode_env_t *env, device_t *d)
44 {
45 	int error;
46 	prop_t *p;
47 	instance_t *ih;
48 	char *service = FC_NEW_DEVICE;
49 	private_data_t *pd, *ppd;
50 	char *unit_str;
51 
52 	pd = (private_data_t *)d->private;
53 	ppd = (private_data_t *)d->parent->private;
54 
55 	ih = MYSELF;
56 	MYSELF = open_instance_chain(env, d, 0);
57 	p = lookup_package_property(env, "name", d);
58 	if (p == NULL) {
59 		forth_abort(env, "create_node: '%s' name prop not found\n",
60 		    get_path(env, d));
61 	}
62 
63 	debug_msg(DEBUG_UPLOAD, "Create Node: %p\n", pd->node);
64 	debug_msg(DEBUG_UPLOAD, " Device Name: '%s'\n", p->data);
65 	debug_msg(DEBUG_UPLOAD, " Parent     : %p\n", ppd->node);
66 
67 	my_unit(env);
68 	(void) call_my_parent(env, "encode-unit");
69 	unit_str = pop_a_duped_string(env, NULL);
70 	if (unit_str == NULL) {
71 		unit_str = STRDUP("");
72 	}
73 
74 	debug_msg(DEBUG_UPLOAD, " Unit Addr  : '%s'\n", unit_str);
75 
76 	error = fc_run_priv(pd->common, FC_NEW_DEVICE, 4, 0,
77 	    fc_phandle2cell(pd->node), fc_phandle2cell(ppd->node),
78 	    fc_ptr2cell(unit_str), fc_ptr2cell(p->data));
79 
80 	FREE(unit_str);
81 	close_instance_chain(env, MYSELF, 0);
82 	MYSELF = ih;
83 
84 	if (error)
85 		log_message(MSG_ERROR, "%s: parent: '%s' new: '%s'\n", service,
86 		    get_path(env, d->parent), p->data);
87 }
88 
89 static void
90 finish_node(fcode_env_t *env, device_t *d)
91 {
92 	private_data_t *pd;
93 	int error;
94 
95 	pd = (private_data_t *)d->private;
96 
97 	debug_msg(DEBUG_UPLOAD, "Finish Node: %p\n", pd->node);
98 
99 	error = fc_run_priv(pd->common, FC_FINISH_DEVICE, 1, 0,
100 	    fc_phandle2cell(pd->node));
101 	if (error)
102 		log_message(MSG_ERROR, "%s: failed\n", FC_FINISH_DEVICE);
103 }
104 
105 static void
106 upload_properties(fcode_env_t *env, device_t *d)
107 {
108 	prop_t *p;
109 	private_data_t *pd;
110 	int error;
111 
112 	pd = (private_data_t *)d->private;
113 	debug_msg(DEBUG_UPLOAD, "Upload Properties: node %p upload: %d\n",
114 	    pd->node, pd->upload);
115 
116 	if (!pd->upload)
117 		return;
118 
119 	for (p = d->properties; p; p = p->next) {
120 		DEBUGF(UPLOAD, print_property(env, p, " Upload: "));
121 
122 		error = fc_run_priv(pd->common, FC_CREATE_PROPERTY, 4, 0,
123 		    fc_phandle2cell(pd->node), fc_int2cell(p->size),
124 		    fc_ptr2cell(p->data), fc_ptr2cell(p->name));
125 
126 		if (error)
127 			log_message(MSG_ERROR, "%s: '%s' failed\n",
128 			    FC_CREATE_PROPERTY, p->name);
129 	}
130 }
131 
132 static void
133 upload_node(fcode_env_t *env, device_t *d)
134 {
135 	private_data_t *pd = (private_data_t *)d->private;
136 
137 	if (pd) {
138 		debug_msg(DEBUG_UPLOAD, "Upload Node: dev: %p node: %p"
139 		    " upload: %d\n", d, pd->node, pd->upload);
140 		if (pd->upload) {
141 			create_node(env, d);
142 			upload_properties(env, d);
143 			finish_node(env, d);
144 		}
145 	} else
146 		debug_msg(DEBUG_UPLOAD, "Upload Node: dev: %p NULL private\n",
147 		    d);
148 }
149 
150 void
151 upload_nodes(fcode_env_t *env)
152 {
153 	debug_msg(DEBUG_UPLOAD, "Upload Nodes: Recursing Tree\n");
154 	recurse_tree(env, env->root_node, upload_node);
155 }
156 
157 void
158 validate_nodes(fcode_env_t *env)
159 {
160 	int error;
161 	common_data_t *cdp = env->private;
162 
163 	error = ioctl(cdp->fcode_fd, FC_VALIDATE);
164 }
165 
166 #pragma init(_init)
167 
168 static void
169 _init(void)
170 {
171 	fcode_env_t *env = initial_env;
172 
173 	ASSERT(env);
174 	NOTICE;
175 
176 	FORTH(0,	"upload-nodes",		upload_nodes);
177 	FORTH(0,	"validate-nodes",	validate_nodes);
178 }
179