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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright (c) 2002-2003, Network Appliance, Inc. All rights reserved.
24  */
25 
26 /*
27  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /*
32  *
33  * MODULE: dapl_init.c
34  *
35  * PURPOSE: Interface Adapter management
36  * Description: Interfaces in this file are completely described in
37  *		the DAPL 1.1 API, Chapter 6, section 2
38  *
39  * $Id: dapl_init.c,v 1.42 2003/06/30 15:38:20 sjs2 Exp $
40  */
41 
42 #include "dapl.h"
43 #include "dapl_hca_util.h"
44 #include "dapl_init.h"
45 #include "dapl_provider.h"
46 #include "dapl_mr_util.h"
47 #include "dapl_osd.h"			/* needed for g_daplDebugLevel */
48 #include "dapl_adapter_util.h"
49 #include "dapl_name_service.h"
50 #include "dapl_vendor.h"
51 
52 static void dapl_init(void);
53 static void dapl_fini(void);
54 
55 #pragma init(dapl_init)
56 #pragma fini(dapl_fini)
57 
58 /*
59  * dapl_init
60  *
61  * initialize this provider
62  * includes initialization of all global variables
63  * as well as registering all supported IAs with the dat registry
64  *
65  * This function needs to be called once when the provider is loaded.
66  *
67  * Input:
68  *	none
69  *
70  * Output:
71  *	none
72  *
73  * Return Values:
74  */
75 static void
dapl_init(void)76 dapl_init(void)
77 {
78 	DAT_RETURN		dat_status;
79 
80 	dapl_dbg_log(DAPL_DBG_TYPE_UTIL, "DAPL: Started (dapl_init)\n");
81 
82 #if defined(DAPL_DBG)
83 	/* set up debug type */
84 	g_dapl_dbg_type = dapl_os_get_env_val("DAPL_DBG_TYPE",
85 	    DAPL_DBG_TYPE_ERR | DAPL_DBG_TYPE_WARN);
86 	/* set up debug level */
87 	g_dapl_dbg_dest = dapl_os_get_env_val("DAPL_DBG_DEST",
88 	    DAPL_DBG_DEST_STDOUT);
89 #endif /* DAPL_DBG */
90 
91 	/* See if the user is on a loopback setup */
92 	g_dapl_loopback_connection = dapl_os_get_env_bool("DAPL_LOOPBACK");
93 	dapl_dbg_log(DAPL_DBG_TYPE_UTIL, "DAPL: %s Setting Loopback\n",
94 	    g_dapl_loopback_connection ? "" : "NOT");
95 
96 	dapls_ib_state_init();
97 
98 	/* initialize the provider list */
99 	dat_status = dapl_provider_list_create();
100 	if (DAT_SUCCESS != dat_status) {
101 		dapl_dbg_log(DAPL_DBG_TYPE_ERR,
102 		    "dapl_provider_list_create failed %d\n", dat_status);
103 		goto bail;
104 	}
105 
106 	/* Set up name services */
107 	dat_status = dapls_ns_init();
108 
109 	if (DAT_SUCCESS != dat_status) {
110 		dapl_dbg_log(DAPL_DBG_TYPE_ERR, "dapls_ns_init failed %d\n",
111 		    dat_status);
112 		goto bail;
113 	}
114 
115 	return;
116 
117 bail:
118 	dapl_dbg_log(DAPL_DBG_TYPE_ERR, "ERROR: dapl_init failed\n");
119 	dapl_fini();
120 }
121 
122 /*
123  * dapl_fini
124  *
125  * finalize this provider
126  * includes freeing of all global variables
127  * as well as deregistering all supported IAs from the dat registry
128  *
129  * This function needs to be called once when the provider is loaded.
130  *
131  * Input:
132  *	none
133  *
134  * Output:
135  *	none
136  *
137  * Return Values:
138  */
139 static void
dapl_fini(void)140 dapl_fini(void)
141 {
142 	DAT_RETURN		dat_status;
143 
144 	dapl_dbg_log(DAPL_DBG_TYPE_UTIL, "DAPL: Stopped (dapl_fini)\n");
145 
146 	/*
147 	 * Free up hca related resources
148 	 */
149 	dapls_ib_state_fini();
150 
151 	dat_status = dapl_provider_list_destroy();
152 	if (DAT_SUCCESS != dat_status) {
153 		dapl_dbg_log(DAPL_DBG_TYPE_ERR,
154 		    "dapl_provider_list_destroy failed %d\n", dat_status);
155 	}
156 }
157 
158 /*
159  *
160  * This function is called by the registry to initialize a provider
161  *
162  * The instance data string is expected to have the following form:
163  *
164  * <hca name> <port number>
165  *
166  */
167 /* ARGSUSED */
168 void
dat_provider_init(IN const DAT_PROVIDER_INFO * provider_info,IN const char * instance_data)169 dat_provider_init(
170     IN const DAT_PROVIDER_INFO 	*provider_info,
171     IN const char 		*instance_data)
172 {
173 	DAT_PROVIDER		*provider;
174 	DAPL_HCA		*hca_ptr;
175 	DAT_RETURN		dat_status;
176 
177 	provider = NULL;
178 	hca_ptr = NULL;
179 
180 	dat_status = dapl_provider_list_insert(provider_info->ia_name,
181 	    &provider);
182 	if (DAT_SUCCESS != dat_status) {
183 		dapl_dbg_log(DAPL_DBG_TYPE_ERR,
184 		    "dat_provider_list_insert failed: %x\n", dat_status);
185 		goto bail;
186 	}
187 
188 	hca_ptr = dapl_hca_alloc((char *)provider_info->ia_name, 0);
189 	if (NULL == hca_ptr) {
190 		dat_status = DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
191 		    DAT_RESOURCE_MEMORY);
192 		goto bail;
193 	}
194 
195 	provider->extension = hca_ptr;
196 
197 	/* register providers with dat_registry */
198 	dat_status = dat_registry_add_provider(provider, provider_info);
199 	if (DAT_SUCCESS != dat_status) {
200 		dapl_dbg_log(DAPL_DBG_TYPE_ERR,
201 		    "dat_registry_add_provider failed: %x\n", dat_status);
202 		goto bail;
203 	}
204 
205 bail:
206 	if (DAT_SUCCESS != dat_status) {
207 		if (NULL != provider) {
208 			(void) dapl_provider_list_remove(
209 			    provider_info->ia_name);
210 		}
211 
212 		if (NULL != hca_ptr) {
213 			dapl_hca_free(hca_ptr);
214 		}
215 	}
216 }
217 
218 
219 /*
220  *
221  * This function is called by the registry to de-initialize a provider
222  *
223  */
224 void
dat_provider_fini(IN const DAT_PROVIDER_INFO * provider_info)225 dat_provider_fini(
226     IN const DAT_PROVIDER_INFO 	*provider_info)
227 {
228 	DAT_PROVIDER	*provider;
229 	DAT_RETURN	dat_status;
230 
231 	dat_status = dapl_provider_list_search(provider_info->ia_name,
232 	    &provider);
233 	if (DAT_SUCCESS != dat_status) {
234 		dapl_dbg_log(DAPL_DBG_TYPE_ERR,
235 		    "dat_registry_add_provider failed: %x\n", dat_status);
236 		return;
237 	}
238 
239 	dat_status = dat_registry_remove_provider(provider, provider_info);
240 	if (DAT_SUCCESS != dat_status) {
241 		dapl_dbg_log(DAPL_DBG_TYPE_ERR,
242 		    "dat_registry_add_provider failed: %x\n", dat_status);
243 	}
244 
245 	(void) dapl_provider_list_remove(provider_info->ia_name);
246 }
247 
248 
249 
250 /*
251  * Local variables:
252  *  c-indent-level: 4
253  *  c-basic-offset: 4
254  *  tab-width: 8
255  * End:
256  */
257