xref: /titanic_41/usr/src/lib/efcode/fcdriver/phandle.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) 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 <stdio.h>
30*7c478bd9Sstevel@tonic-gate #include <stdlib.h>
31*7c478bd9Sstevel@tonic-gate #include <strings.h>
32*7c478bd9Sstevel@tonic-gate #include <fcode/private.h>
33*7c478bd9Sstevel@tonic-gate #include <fcode/log.h>
34*7c478bd9Sstevel@tonic-gate 
35*7c478bd9Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
36*7c478bd9Sstevel@tonic-gate 
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate static int use_os_handle = 1;
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate void
do_use_os_handles(fcode_env_t * env)41*7c478bd9Sstevel@tonic-gate do_use_os_handles(fcode_env_t *env)
42*7c478bd9Sstevel@tonic-gate {
43*7c478bd9Sstevel@tonic-gate 	use_os_handle = 1;
44*7c478bd9Sstevel@tonic-gate }
45*7c478bd9Sstevel@tonic-gate 
46*7c478bd9Sstevel@tonic-gate void
do_use_fake_handles(fcode_env_t * env)47*7c478bd9Sstevel@tonic-gate do_use_fake_handles(fcode_env_t *env)
48*7c478bd9Sstevel@tonic-gate {
49*7c478bd9Sstevel@tonic-gate 	log_message(MSG_ERROR, "WARNING: using fake phandles, test only\n");
50*7c478bd9Sstevel@tonic-gate 	use_os_handle = 0;
51*7c478bd9Sstevel@tonic-gate }
52*7c478bd9Sstevel@tonic-gate 
53*7c478bd9Sstevel@tonic-gate static int
match_nodeid(void * s,void * d)54*7c478bd9Sstevel@tonic-gate match_nodeid(void *s, void *d)
55*7c478bd9Sstevel@tonic-gate {
56*7c478bd9Sstevel@tonic-gate 	my_nodeid_t *src = s;
57*7c478bd9Sstevel@tonic-gate 	my_nodeid_t *dest = d;
58*7c478bd9Sstevel@tonic-gate 	return ((src->node) == (dest->node));
59*7c478bd9Sstevel@tonic-gate }
60*7c478bd9Sstevel@tonic-gate 
61*7c478bd9Sstevel@tonic-gate static int
match_handle(void * s,void * d)62*7c478bd9Sstevel@tonic-gate match_handle(void *s, void *d)
63*7c478bd9Sstevel@tonic-gate {
64*7c478bd9Sstevel@tonic-gate 	my_nodeid_t *src = s;
65*7c478bd9Sstevel@tonic-gate 	my_nodeid_t *dest = d;
66*7c478bd9Sstevel@tonic-gate 	return ((src->my_handle) == (dest->my_handle));
67*7c478bd9Sstevel@tonic-gate }
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate /*
70*7c478bd9Sstevel@tonic-gate  * Convert from an OS phandle to an interpreter phandle
71*7c478bd9Sstevel@tonic-gate  */
72*7c478bd9Sstevel@tonic-gate device_t *
convert_phandle(fcode_env_t * env,fstack_t d)73*7c478bd9Sstevel@tonic-gate convert_phandle(fcode_env_t *env, fstack_t d)
74*7c478bd9Sstevel@tonic-gate {
75*7c478bd9Sstevel@tonic-gate 	fc_resource_t *t;
76*7c478bd9Sstevel@tonic-gate 	common_data_t *cdp = env->private;
77*7c478bd9Sstevel@tonic-gate 	device_t *r;
78*7c478bd9Sstevel@tonic-gate 
79*7c478bd9Sstevel@tonic-gate 	if (use_os_handle) {
80*7c478bd9Sstevel@tonic-gate 		my_nodeid_t nh;
81*7c478bd9Sstevel@tonic-gate 		nh.my_handle = (fc_phandle_t) d;
82*7c478bd9Sstevel@tonic-gate 		t = find_resource(&cdp->nodeids, &nh, match_handle);
83*7c478bd9Sstevel@tonic-gate 		if (t == NULL) {
84*7c478bd9Sstevel@tonic-gate 			r = 0;
85*7c478bd9Sstevel@tonic-gate 		} else {
86*7c478bd9Sstevel@tonic-gate 			my_nodeid_t *p = (my_nodeid_t *) t->data;
87*7c478bd9Sstevel@tonic-gate 			r = (device_t *) p->node;
88*7c478bd9Sstevel@tonic-gate 		}
89*7c478bd9Sstevel@tonic-gate 	} else
90*7c478bd9Sstevel@tonic-gate 		r = (device_t *)d;
91*7c478bd9Sstevel@tonic-gate 	return (r);
92*7c478bd9Sstevel@tonic-gate }
93*7c478bd9Sstevel@tonic-gate 
94*7c478bd9Sstevel@tonic-gate /*
95*7c478bd9Sstevel@tonic-gate  * Interpreter phandle to OS phandle
96*7c478bd9Sstevel@tonic-gate  */
97*7c478bd9Sstevel@tonic-gate fstack_t
revert_phandle(fcode_env_t * env,device_t * d)98*7c478bd9Sstevel@tonic-gate revert_phandle(fcode_env_t *env, device_t *d)
99*7c478bd9Sstevel@tonic-gate {
100*7c478bd9Sstevel@tonic-gate 	fc_resource_t *t;
101*7c478bd9Sstevel@tonic-gate 	common_data_t *cdp = env->private;
102*7c478bd9Sstevel@tonic-gate 	fstack_t r;
103*7c478bd9Sstevel@tonic-gate 
104*7c478bd9Sstevel@tonic-gate 	if (use_os_handle) {
105*7c478bd9Sstevel@tonic-gate 		my_nodeid_t nh;
106*7c478bd9Sstevel@tonic-gate 		nh.node = d;
107*7c478bd9Sstevel@tonic-gate 		t = find_resource(&cdp->nodeids, &nh, match_nodeid);
108*7c478bd9Sstevel@tonic-gate 		if (t == NULL) {
109*7c478bd9Sstevel@tonic-gate 			r = 0;
110*7c478bd9Sstevel@tonic-gate 		} else {
111*7c478bd9Sstevel@tonic-gate 			my_nodeid_t *p = (my_nodeid_t *) t->data;
112*7c478bd9Sstevel@tonic-gate 			r = (fstack_t) p->my_handle;
113*7c478bd9Sstevel@tonic-gate 		}
114*7c478bd9Sstevel@tonic-gate 	} else
115*7c478bd9Sstevel@tonic-gate 		r = (fstack_t) d;
116*7c478bd9Sstevel@tonic-gate 	return (r);
117*7c478bd9Sstevel@tonic-gate }
118*7c478bd9Sstevel@tonic-gate 
119*7c478bd9Sstevel@tonic-gate void
add_my_handle(fcode_env_t * env,fc_phandle_t mh,device_t * d)120*7c478bd9Sstevel@tonic-gate add_my_handle(fcode_env_t *env, fc_phandle_t mh, device_t *d)
121*7c478bd9Sstevel@tonic-gate {
122*7c478bd9Sstevel@tonic-gate 	my_nodeid_t *nh;
123*7c478bd9Sstevel@tonic-gate 	common_data_t *cdp = env->private;
124*7c478bd9Sstevel@tonic-gate 
125*7c478bd9Sstevel@tonic-gate 	nh = MALLOC(sizeof (my_nodeid_t));
126*7c478bd9Sstevel@tonic-gate 	nh->my_handle = mh;
127*7c478bd9Sstevel@tonic-gate 	nh->node = d;
128*7c478bd9Sstevel@tonic-gate 	if (add_resource(&cdp->nodeids, nh, match_handle) == NULL) {
129*7c478bd9Sstevel@tonic-gate 		log_message(MSG_ERROR, "add_my_handle: add_resource failed\n");
130*7c478bd9Sstevel@tonic-gate 	}
131*7c478bd9Sstevel@tonic-gate }
132*7c478bd9Sstevel@tonic-gate 
133*7c478bd9Sstevel@tonic-gate void
allocate_phandle(fcode_env_t * env)134*7c478bd9Sstevel@tonic-gate allocate_phandle(fcode_env_t *env)
135*7c478bd9Sstevel@tonic-gate {
136*7c478bd9Sstevel@tonic-gate 	private_data_t *pd;
137*7c478bd9Sstevel@tonic-gate 	common_data_t *cdp;
138*7c478bd9Sstevel@tonic-gate 	int error;
139*7c478bd9Sstevel@tonic-gate 	char *service;
140*7c478bd9Sstevel@tonic-gate 	device_t *current;
141*7c478bd9Sstevel@tonic-gate 	fc_cell_t hcell;
142*7c478bd9Sstevel@tonic-gate 
143*7c478bd9Sstevel@tonic-gate 	if ((cdp = env->private) == NULL) {
144*7c478bd9Sstevel@tonic-gate 		log_message(MSG_ERROR, "allocate_phandle: NULL common\n");
145*7c478bd9Sstevel@tonic-gate 		return;
146*7c478bd9Sstevel@tonic-gate 	}
147*7c478bd9Sstevel@tonic-gate 
148*7c478bd9Sstevel@tonic-gate 	if (!cdp->init_done)
149*7c478bd9Sstevel@tonic-gate 		return;
150*7c478bd9Sstevel@tonic-gate 
151*7c478bd9Sstevel@tonic-gate 	current = MYSELF->device;
152*7c478bd9Sstevel@tonic-gate 	ASSERT(current);
153*7c478bd9Sstevel@tonic-gate 
154*7c478bd9Sstevel@tonic-gate 	if (cdp->first_node) {
155*7c478bd9Sstevel@tonic-gate 		service = FC_CONFIG_CHILD;
156*7c478bd9Sstevel@tonic-gate 		cdp->first_node = 0;
157*7c478bd9Sstevel@tonic-gate 	} else {
158*7c478bd9Sstevel@tonic-gate 		service = FC_ALLOC_PHANDLE;
159*7c478bd9Sstevel@tonic-gate 	}
160*7c478bd9Sstevel@tonic-gate 
161*7c478bd9Sstevel@tonic-gate 	pd = MALLOC(sizeof (private_data_t));
162*7c478bd9Sstevel@tonic-gate 	pd->common = cdp;
163*7c478bd9Sstevel@tonic-gate 	pd->parent = (fc_phandle_t) revert_phandle(env, current->parent);
164*7c478bd9Sstevel@tonic-gate 	pd->upload = (cdp->init_done == 1);
165*7c478bd9Sstevel@tonic-gate 	current->private = pd;
166*7c478bd9Sstevel@tonic-gate 
167*7c478bd9Sstevel@tonic-gate 	error = fc_run_priv(cdp, service, 0, 1, &hcell);
168*7c478bd9Sstevel@tonic-gate 
169*7c478bd9Sstevel@tonic-gate 	pd->node = fc_cell2phandle(hcell);
170*7c478bd9Sstevel@tonic-gate 
171*7c478bd9Sstevel@tonic-gate 	add_my_handle(env, pd->node, current);
172*7c478bd9Sstevel@tonic-gate }
173*7c478bd9Sstevel@tonic-gate 
174*7c478bd9Sstevel@tonic-gate fc_phandle_t
fc_get_ap(common_data_t * cdp)175*7c478bd9Sstevel@tonic-gate fc_get_ap(common_data_t *cdp)
176*7c478bd9Sstevel@tonic-gate {
177*7c478bd9Sstevel@tonic-gate 	fc_cell_t hcell;
178*7c478bd9Sstevel@tonic-gate 	int error;
179*7c478bd9Sstevel@tonic-gate 
180*7c478bd9Sstevel@tonic-gate 	error = fc_run_priv(cdp, FC_AP_PHANDLE, 0, 1, &hcell);
181*7c478bd9Sstevel@tonic-gate 
182*7c478bd9Sstevel@tonic-gate 	if (error)
183*7c478bd9Sstevel@tonic-gate 		exit(1);
184*7c478bd9Sstevel@tonic-gate 
185*7c478bd9Sstevel@tonic-gate 	return (fc_cell2phandle(hcell));
186*7c478bd9Sstevel@tonic-gate }
187*7c478bd9Sstevel@tonic-gate 
188*7c478bd9Sstevel@tonic-gate 
189*7c478bd9Sstevel@tonic-gate #pragma init(_init)
190*7c478bd9Sstevel@tonic-gate 
191*7c478bd9Sstevel@tonic-gate static void
_init(void)192*7c478bd9Sstevel@tonic-gate _init(void)
193*7c478bd9Sstevel@tonic-gate {
194*7c478bd9Sstevel@tonic-gate 	fcode_env_t *env = initial_env;
195*7c478bd9Sstevel@tonic-gate 
196*7c478bd9Sstevel@tonic-gate 	ASSERT(env);
197*7c478bd9Sstevel@tonic-gate 	NOTICE;
198*7c478bd9Sstevel@tonic-gate 
199*7c478bd9Sstevel@tonic-gate 	env->convert_phandle = convert_phandle;
200*7c478bd9Sstevel@tonic-gate 	env->revert_phandle = revert_phandle;
201*7c478bd9Sstevel@tonic-gate 	env->allocate_phandle = allocate_phandle;
202*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"use-os-handles",	do_use_os_handles);
203*7c478bd9Sstevel@tonic-gate 	FORTH(0,	"use-fake-handles",	do_use_fake_handles);
204*7c478bd9Sstevel@tonic-gate }
205