1*0Sstevel@tonic-gate /*
2*0Sstevel@tonic-gate * CDDL HEADER START
3*0Sstevel@tonic-gate *
4*0Sstevel@tonic-gate * The contents of this file are subject to the terms of the
5*0Sstevel@tonic-gate * Common Development and Distribution License, Version 1.0 only
6*0Sstevel@tonic-gate * (the "License"). You may not use this file except in compliance
7*0Sstevel@tonic-gate * with the License.
8*0Sstevel@tonic-gate *
9*0Sstevel@tonic-gate * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*0Sstevel@tonic-gate * or http://www.opensolaris.org/os/licensing.
11*0Sstevel@tonic-gate * See the License for the specific language governing permissions
12*0Sstevel@tonic-gate * and limitations under the License.
13*0Sstevel@tonic-gate *
14*0Sstevel@tonic-gate * When distributing Covered Code, include this CDDL HEADER in each
15*0Sstevel@tonic-gate * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*0Sstevel@tonic-gate * If applicable, add the following below this CDDL HEADER, with the
17*0Sstevel@tonic-gate * fields enclosed by brackets "[]" replaced with your own identifying
18*0Sstevel@tonic-gate * information: Portions Copyright [yyyy] [name of copyright owner]
19*0Sstevel@tonic-gate *
20*0Sstevel@tonic-gate * CDDL HEADER END
21*0Sstevel@tonic-gate */
22*0Sstevel@tonic-gate /*
23*0Sstevel@tonic-gate * Copyright (c) 1999 by Sun Microsystems, Inc.
24*0Sstevel@tonic-gate * All rights reserved.
25*0Sstevel@tonic-gate */
26*0Sstevel@tonic-gate
27*0Sstevel@tonic-gate #pragma ident "%Z%%M% %I% %E% SMI"
28*0Sstevel@tonic-gate
29*0Sstevel@tonic-gate #include <stdio.h>
30*0Sstevel@tonic-gate #include <stdlib.h>
31*0Sstevel@tonic-gate #include <strings.h>
32*0Sstevel@tonic-gate #include <fcode/private.h>
33*0Sstevel@tonic-gate #include <fcode/log.h>
34*0Sstevel@tonic-gate
35*0Sstevel@tonic-gate #include <fcdriver/fcdriver.h>
36*0Sstevel@tonic-gate
37*0Sstevel@tonic-gate
38*0Sstevel@tonic-gate static int use_os_handle = 1;
39*0Sstevel@tonic-gate
40*0Sstevel@tonic-gate void
do_use_os_handles(fcode_env_t * env)41*0Sstevel@tonic-gate do_use_os_handles(fcode_env_t *env)
42*0Sstevel@tonic-gate {
43*0Sstevel@tonic-gate use_os_handle = 1;
44*0Sstevel@tonic-gate }
45*0Sstevel@tonic-gate
46*0Sstevel@tonic-gate void
do_use_fake_handles(fcode_env_t * env)47*0Sstevel@tonic-gate do_use_fake_handles(fcode_env_t *env)
48*0Sstevel@tonic-gate {
49*0Sstevel@tonic-gate log_message(MSG_ERROR, "WARNING: using fake phandles, test only\n");
50*0Sstevel@tonic-gate use_os_handle = 0;
51*0Sstevel@tonic-gate }
52*0Sstevel@tonic-gate
53*0Sstevel@tonic-gate static int
match_nodeid(void * s,void * d)54*0Sstevel@tonic-gate match_nodeid(void *s, void *d)
55*0Sstevel@tonic-gate {
56*0Sstevel@tonic-gate my_nodeid_t *src = s;
57*0Sstevel@tonic-gate my_nodeid_t *dest = d;
58*0Sstevel@tonic-gate return ((src->node) == (dest->node));
59*0Sstevel@tonic-gate }
60*0Sstevel@tonic-gate
61*0Sstevel@tonic-gate static int
match_handle(void * s,void * d)62*0Sstevel@tonic-gate match_handle(void *s, void *d)
63*0Sstevel@tonic-gate {
64*0Sstevel@tonic-gate my_nodeid_t *src = s;
65*0Sstevel@tonic-gate my_nodeid_t *dest = d;
66*0Sstevel@tonic-gate return ((src->my_handle) == (dest->my_handle));
67*0Sstevel@tonic-gate }
68*0Sstevel@tonic-gate
69*0Sstevel@tonic-gate /*
70*0Sstevel@tonic-gate * Convert from an OS phandle to an interpreter phandle
71*0Sstevel@tonic-gate */
72*0Sstevel@tonic-gate device_t *
convert_phandle(fcode_env_t * env,fstack_t d)73*0Sstevel@tonic-gate convert_phandle(fcode_env_t *env, fstack_t d)
74*0Sstevel@tonic-gate {
75*0Sstevel@tonic-gate fc_resource_t *t;
76*0Sstevel@tonic-gate common_data_t *cdp = env->private;
77*0Sstevel@tonic-gate device_t *r;
78*0Sstevel@tonic-gate
79*0Sstevel@tonic-gate if (use_os_handle) {
80*0Sstevel@tonic-gate my_nodeid_t nh;
81*0Sstevel@tonic-gate nh.my_handle = (fc_phandle_t) d;
82*0Sstevel@tonic-gate t = find_resource(&cdp->nodeids, &nh, match_handle);
83*0Sstevel@tonic-gate if (t == NULL) {
84*0Sstevel@tonic-gate r = 0;
85*0Sstevel@tonic-gate } else {
86*0Sstevel@tonic-gate my_nodeid_t *p = (my_nodeid_t *) t->data;
87*0Sstevel@tonic-gate r = (device_t *) p->node;
88*0Sstevel@tonic-gate }
89*0Sstevel@tonic-gate } else
90*0Sstevel@tonic-gate r = (device_t *)d;
91*0Sstevel@tonic-gate return (r);
92*0Sstevel@tonic-gate }
93*0Sstevel@tonic-gate
94*0Sstevel@tonic-gate /*
95*0Sstevel@tonic-gate * Interpreter phandle to OS phandle
96*0Sstevel@tonic-gate */
97*0Sstevel@tonic-gate fstack_t
revert_phandle(fcode_env_t * env,device_t * d)98*0Sstevel@tonic-gate revert_phandle(fcode_env_t *env, device_t *d)
99*0Sstevel@tonic-gate {
100*0Sstevel@tonic-gate fc_resource_t *t;
101*0Sstevel@tonic-gate common_data_t *cdp = env->private;
102*0Sstevel@tonic-gate fstack_t r;
103*0Sstevel@tonic-gate
104*0Sstevel@tonic-gate if (use_os_handle) {
105*0Sstevel@tonic-gate my_nodeid_t nh;
106*0Sstevel@tonic-gate nh.node = d;
107*0Sstevel@tonic-gate t = find_resource(&cdp->nodeids, &nh, match_nodeid);
108*0Sstevel@tonic-gate if (t == NULL) {
109*0Sstevel@tonic-gate r = 0;
110*0Sstevel@tonic-gate } else {
111*0Sstevel@tonic-gate my_nodeid_t *p = (my_nodeid_t *) t->data;
112*0Sstevel@tonic-gate r = (fstack_t) p->my_handle;
113*0Sstevel@tonic-gate }
114*0Sstevel@tonic-gate } else
115*0Sstevel@tonic-gate r = (fstack_t) d;
116*0Sstevel@tonic-gate return (r);
117*0Sstevel@tonic-gate }
118*0Sstevel@tonic-gate
119*0Sstevel@tonic-gate void
add_my_handle(fcode_env_t * env,fc_phandle_t mh,device_t * d)120*0Sstevel@tonic-gate add_my_handle(fcode_env_t *env, fc_phandle_t mh, device_t *d)
121*0Sstevel@tonic-gate {
122*0Sstevel@tonic-gate my_nodeid_t *nh;
123*0Sstevel@tonic-gate common_data_t *cdp = env->private;
124*0Sstevel@tonic-gate
125*0Sstevel@tonic-gate nh = MALLOC(sizeof (my_nodeid_t));
126*0Sstevel@tonic-gate nh->my_handle = mh;
127*0Sstevel@tonic-gate nh->node = d;
128*0Sstevel@tonic-gate if (add_resource(&cdp->nodeids, nh, match_handle) == NULL) {
129*0Sstevel@tonic-gate log_message(MSG_ERROR, "add_my_handle: add_resource failed\n");
130*0Sstevel@tonic-gate }
131*0Sstevel@tonic-gate }
132*0Sstevel@tonic-gate
133*0Sstevel@tonic-gate void
allocate_phandle(fcode_env_t * env)134*0Sstevel@tonic-gate allocate_phandle(fcode_env_t *env)
135*0Sstevel@tonic-gate {
136*0Sstevel@tonic-gate private_data_t *pd;
137*0Sstevel@tonic-gate common_data_t *cdp;
138*0Sstevel@tonic-gate int error;
139*0Sstevel@tonic-gate char *service;
140*0Sstevel@tonic-gate device_t *current;
141*0Sstevel@tonic-gate fc_cell_t hcell;
142*0Sstevel@tonic-gate
143*0Sstevel@tonic-gate if ((cdp = env->private) == NULL) {
144*0Sstevel@tonic-gate log_message(MSG_ERROR, "allocate_phandle: NULL common\n");
145*0Sstevel@tonic-gate return;
146*0Sstevel@tonic-gate }
147*0Sstevel@tonic-gate
148*0Sstevel@tonic-gate if (!cdp->init_done)
149*0Sstevel@tonic-gate return;
150*0Sstevel@tonic-gate
151*0Sstevel@tonic-gate current = MYSELF->device;
152*0Sstevel@tonic-gate ASSERT(current);
153*0Sstevel@tonic-gate
154*0Sstevel@tonic-gate if (cdp->first_node) {
155*0Sstevel@tonic-gate service = FC_CONFIG_CHILD;
156*0Sstevel@tonic-gate cdp->first_node = 0;
157*0Sstevel@tonic-gate } else {
158*0Sstevel@tonic-gate service = FC_ALLOC_PHANDLE;
159*0Sstevel@tonic-gate }
160*0Sstevel@tonic-gate
161*0Sstevel@tonic-gate pd = MALLOC(sizeof (private_data_t));
162*0Sstevel@tonic-gate pd->common = cdp;
163*0Sstevel@tonic-gate pd->parent = (fc_phandle_t) revert_phandle(env, current->parent);
164*0Sstevel@tonic-gate pd->upload = (cdp->init_done == 1);
165*0Sstevel@tonic-gate current->private = pd;
166*0Sstevel@tonic-gate
167*0Sstevel@tonic-gate error = fc_run_priv(cdp, service, 0, 1, &hcell);
168*0Sstevel@tonic-gate
169*0Sstevel@tonic-gate pd->node = fc_cell2phandle(hcell);
170*0Sstevel@tonic-gate
171*0Sstevel@tonic-gate add_my_handle(env, pd->node, current);
172*0Sstevel@tonic-gate }
173*0Sstevel@tonic-gate
174*0Sstevel@tonic-gate fc_phandle_t
fc_get_ap(common_data_t * cdp)175*0Sstevel@tonic-gate fc_get_ap(common_data_t *cdp)
176*0Sstevel@tonic-gate {
177*0Sstevel@tonic-gate fc_cell_t hcell;
178*0Sstevel@tonic-gate int error;
179*0Sstevel@tonic-gate
180*0Sstevel@tonic-gate error = fc_run_priv(cdp, FC_AP_PHANDLE, 0, 1, &hcell);
181*0Sstevel@tonic-gate
182*0Sstevel@tonic-gate if (error)
183*0Sstevel@tonic-gate exit(1);
184*0Sstevel@tonic-gate
185*0Sstevel@tonic-gate return (fc_cell2phandle(hcell));
186*0Sstevel@tonic-gate }
187*0Sstevel@tonic-gate
188*0Sstevel@tonic-gate
189*0Sstevel@tonic-gate #pragma init(_init)
190*0Sstevel@tonic-gate
191*0Sstevel@tonic-gate static void
_init(void)192*0Sstevel@tonic-gate _init(void)
193*0Sstevel@tonic-gate {
194*0Sstevel@tonic-gate fcode_env_t *env = initial_env;
195*0Sstevel@tonic-gate
196*0Sstevel@tonic-gate ASSERT(env);
197*0Sstevel@tonic-gate NOTICE;
198*0Sstevel@tonic-gate
199*0Sstevel@tonic-gate env->convert_phandle = convert_phandle;
200*0Sstevel@tonic-gate env->revert_phandle = revert_phandle;
201*0Sstevel@tonic-gate env->allocate_phandle = allocate_phandle;
202*0Sstevel@tonic-gate FORTH(0, "use-os-handles", do_use_os_handles);
203*0Sstevel@tonic-gate FORTH(0, "use-fake-handles", do_use_fake_handles);
204*0Sstevel@tonic-gate }
205