xref: /illumos-gate/usr/src/lib/udapl/udapl_tavor/common/dapl_hca_util.c (revision 9e39c5ba00a55fa05777cc94b148296af305e135)
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_hca_util.c
34  *
35  * PURPOSE: Manage HCA structure
36  *
37  */
38 
39 #include "dapl.h"
40 #include "dapl_adapter_util.h"
41 #include "dapl_provider.h"
42 #include "dapl_hca_util.h"
43 #include "dapl_hash.h"
44 
45 
46 /*
47  * dapl_hca_alloc
48  *
49  * alloc and initialize an HCA struct
50  *
51  * Input:
52  * 	name
53  *      port
54  *
55  * Output:
56  * 	hca_ptr
57  *
58  * Returns:
59  * 	none
60  *
61  */
62 /* ARGSUSED */
63 DAPL_HCA *
dapl_hca_alloc(char * name,char * port)64 dapl_hca_alloc(char *name, char *port)
65 {
66 	DAPL_HCA *hca_ptr;
67 
68 	hca_ptr = dapl_os_alloc(sizeof (DAPL_HCA));
69 	if (NULL != hca_ptr) {
70 		(void) dapl_os_memzero(hca_ptr, sizeof (DAPL_HCA));
71 
72 		if (DAT_SUCCESS ==
73 		    dapls_hash_create(DAPL_HASH_TABLE_DEFAULT_CAPACITY,
74 		    DAT_TRUE, &hca_ptr->lmr_hash_table)) {
75 			dapl_os_lock_init(&hca_ptr->lock);
76 			dapl_llist_init_head(&hca_ptr->ia_list_head);
77 
78 			hca_ptr->name = dapl_os_strdup(name);
79 			hca_ptr->ib_hca_handle = IB_INVALID_HANDLE;
80 			hca_ptr->port_num = 0;
81 			hca_ptr->null_ib_cq_handle = IB_INVALID_HANDLE;
82 		} else {
83 			dapl_os_free(hca_ptr, sizeof (DAPL_HCA));
84 			hca_ptr = NULL;
85 		}
86 	}
87 	return (hca_ptr);
88 }
89 
90 /*
91  * dapl_hca_free
92  *
93  * free an IA INFO struct
94  *
95  * Input:
96  * 	hca_ptr
97  *
98  * Output:
99  * 	none
100  *
101  * Returns:
102  * 	none
103  *
104  */
105 void
dapl_hca_free(DAPL_HCA * hca_ptr)106 dapl_hca_free(DAPL_HCA *hca_ptr)
107 {
108 	unsigned int		len;
109 
110 	(void) dapls_hash_free(hca_ptr->lmr_hash_table);
111 	if (NULL != hca_ptr->name) {
112 		len = dapl_os_strlen(hca_ptr->name);
113 		/* pacify lint dapl_os_free macro doesn't use len */
114 		len = len;
115 		dapl_os_free(hca_ptr->name, len + 1);
116 	}
117 
118 	dapl_os_free(hca_ptr, sizeof (DAPL_HCA));
119 }
120 
121 /*
122  * dapl_hca_link_ia
123  *
124  * Add an ia to the HCA structure
125  *
126  * Input:
127  *	hca_ptr
128  *	ia_ptr
129  *
130  * Output:
131  * 	none
132  *
133  * Returns:
134  * 	none
135  *
136  */
137 void
dapl_hca_link_ia(IN DAPL_HCA * hca_ptr,IN DAPL_IA * ia_ptr)138 dapl_hca_link_ia(IN DAPL_HCA *hca_ptr, IN DAPL_IA *ia_ptr)
139 {
140 	dapl_os_lock(&hca_ptr->lock);
141 	dapl_llist_add_head(&hca_ptr->ia_list_head,
142 	    &ia_ptr->hca_ia_list_entry, ia_ptr);
143 	dapl_os_unlock(&hca_ptr->lock);
144 }
145 
146 /*
147  * dapl_hca_unlink_ia
148  *
149  * Remove an ia from the hca info structure
150  *
151  * Input:
152  *	hca_ptr
153  *	ia_ptr
154  *
155  * Output:
156  * 	none
157  *
158  * Returns:
159  * 	none
160  *
161  */
162 void
dapl_hca_unlink_ia(IN DAPL_HCA * hca_ptr,IN DAPL_IA * ia_ptr)163 dapl_hca_unlink_ia(IN DAPL_HCA *hca_ptr, IN DAPL_IA *ia_ptr)
164 {
165 	dapl_os_lock(&hca_ptr->lock);
166 	/*
167 	 * If an error occurred when we were opening the IA it
168 	 * will not be linked on the list; don't unlink an unlinked
169 	 * list!
170 	 */
171 	if (!dapl_llist_is_empty(&hca_ptr->ia_list_head)) {
172 		(void) dapl_llist_remove_entry(&hca_ptr->ia_list_head,
173 		    &ia_ptr->hca_ia_list_entry);
174 	}
175 	dapl_os_unlock(&hca_ptr->lock);
176 }
177