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