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 2009 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /*
32  *
33  * MODULE: dapl_ep_free.c
34  *
35  * PURPOSE: Endpoint management
36  * Description: Interfaces in this file are completely described in
37  *		the DAPL 1.1 API, Chapter 6, section 5.4
38  *
39  * $Id: dapl_ep_free.c,v 1.21 2003/07/30 18:13:37 hobie16 Exp $
40  */
41 
42 #include "dapl.h"
43 #include "dapl_ia_util.h"
44 #include "dapl_ep_util.h"
45 #include "dapl_adapter_util.h"
46 #include "dapl_ring_buffer_util.h"
47 
48 /*
49  * dapl_ep_free
50  *
51  * DAPL Requirements Version xxx, 6.5.3
52  *
53  * Destroy an instance of the Endpoint
54  *
55  * Input:
56  *	ep_handle
57  *
58  * Output:
59  *	none
60  *
61  * Returns:
62  *	DAT_SUCCESS
63  *	DAT_INVALID_PARAMETER
64  *	DAT_INVALID_STATE
65  */
66 DAT_RETURN
dapl_ep_free(IN DAT_EP_HANDLE ep_handle)67 dapl_ep_free(
68 	IN DAT_EP_HANDLE ep_handle)
69 {
70 	DAPL_EP	*ep_ptr;
71 	DAPL_IA	*ia_ptr;
72 	DAT_EP_PARAM *param;
73 	DAT_RETURN dat_status = DAT_SUCCESS;
74 
75 	dapl_dbg_log(DAPL_DBG_TYPE_API, "dapl_ep_free (%p)\n", ep_handle);
76 
77 	ep_ptr = (DAPL_EP *) ep_handle;
78 	param = &ep_ptr->param;
79 
80 	/*
81 	 * Verify parameter & state
82 	 */
83 	if (DAPL_BAD_HANDLE(ep_ptr, DAPL_MAGIC_EP) &&
84 	    !(ep_ptr->header.magic == DAPL_MAGIC_EP_EXIT &&
85 	    ep_ptr->param.ep_state == DAT_EP_STATE_DISCONNECTED)) {
86 		dat_status = DAT_ERROR(DAT_INVALID_HANDLE,
87 		    DAT_INVALID_HANDLE_EP);
88 		goto bail;
89 	}
90 
91 	if (ep_ptr->param.ep_state == DAT_EP_STATE_RESERVED ||
92 	    ep_ptr->param.ep_state == DAT_EP_STATE_PASSIVE_CONNECTION_PENDING ||
93 	    ep_ptr->param.ep_state ==
94 	    DAT_EP_STATE_TENTATIVE_CONNECTION_PENDING) {
95 		dapl_dbg_log(DAPL_DBG_TYPE_WARN,
96 		    "--> dapl_ep_free: invalid state: %x, ep %p\n",
97 		    ep_ptr->param.ep_state, ep_ptr);
98 		dat_status = DAT_ERROR(DAT_INVALID_STATE,
99 		    dapls_ep_state_subtype(ep_ptr));
100 		goto bail;
101 	}
102 
103 	ia_ptr = ep_ptr->header.owner_ia;
104 
105 	/*
106 	 * If we are connected, issue a disconnect. If we are in the
107 	 * disconnect_pending state, disconnect with the ABRUPT flag
108 	 * set.
109 	 */
110 
111 	/*
112 	 * Do verification of parameters and the state change atomically.
113 	 */
114 	dapl_os_lock(&ep_ptr->header.lock);
115 
116 	if (ep_ptr->param.ep_state == DAT_EP_STATE_CONNECTED ||
117 	    ep_ptr->param.ep_state == DAT_EP_STATE_ACTIVE_CONNECTION_PENDING ||
118 	    ep_ptr->param.ep_state == DAT_EP_STATE_COMPLETION_PENDING ||
119 	    ep_ptr->param.ep_state == DAT_EP_STATE_DISCONNECT_PENDING) {
120 		/*
121 		 * Issue the disconnect and return. The DISCONNECT callback
122 		 * will invoke this routine and finish the job
123 		 */
124 		ep_ptr->param.ep_state = DAT_EP_STATE_DISCONNECT_PENDING;
125 		dapl_os_unlock(&ep_ptr->header.lock);
126 
127 		dapl_dbg_log(DAPL_DBG_TYPE_EP,
128 		    "--> dapl_ep_free: disconnecting EP: %x, ep %p\n",
129 		    ep_ptr->param.ep_state, ep_ptr);
130 
131 		dat_status = dapls_ib_disconnect(ep_ptr, DAT_CLOSE_ABRUPT_FLAG);
132 		ep_ptr->param.ep_state = DAT_EP_STATE_DISCONNECT_PENDING;
133 		ep_ptr->header.magic = DAPL_MAGIC_EP_EXIT;
134 	} else {
135 		dapl_os_unlock(&ep_ptr->header.lock);
136 	}
137 
138 	/*
139 	 * Release all reference counts and unlink this structure. If we
140 	 * got here from a callback, don't repeat this step
141 	 */
142 	if (!(ep_ptr->header.magic == DAPL_MAGIC_EP_EXIT &&
143 	    ep_ptr->param.ep_state == DAT_EP_STATE_DISCONNECTED)) {
144 		/* Remove link from the IA */
145 		dapl_ia_unlink_ep(ia_ptr, ep_ptr);
146 	}
147 
148 	/*
149 	 * If the EP is disconnected tear everything down.  Otherwise,
150 	 * disconnect the EP but leave the QP and basic EP structure
151 	 * intact; the callback code will finish the job.
152 	 */
153 	dapl_os_lock(&ep_ptr->header.lock);
154 	if (ep_ptr->param.ep_state == DAT_EP_STATE_DISCONNECTED ||
155 	    ep_ptr->param.ep_state == DAT_EP_STATE_UNCONNECTED) {
156 		/*
157 		 * Update ref counts. Note the user may have used ep_modify
158 		 * to set handles to NULL.
159 		 */
160 		if (param->pz_handle != NULL) {
161 			dapl_os_atomic_dec(&((DAPL_PZ *)
162 			    param->pz_handle)->pz_ref_count);
163 			param->pz_handle = NULL;
164 		}
165 		if (param->recv_evd_handle != NULL) {
166 			dapl_os_atomic_dec(&((DAPL_EVD *)
167 			    param->recv_evd_handle)->evd_ref_count);
168 			param->recv_evd_handle = NULL;
169 		}
170 		if (param->request_evd_handle != NULL) {
171 			dapl_os_atomic_dec(&((DAPL_EVD *)
172 			    param->request_evd_handle)->evd_ref_count);
173 			param->request_evd_handle = NULL;
174 		}
175 		if (param->connect_evd_handle != NULL) {
176 			dapl_os_atomic_dec(&((DAPL_EVD *)
177 			    param->connect_evd_handle)->evd_ref_count);
178 			param->connect_evd_handle = NULL;
179 		}
180 
181 		if (param->srq_handle != NULL) {
182 			dapl_os_atomic_dec(&((DAPL_SRQ *)
183 			    param->srq_handle)->srq_ref_count);
184 			param->srq_handle = NULL;
185 		}
186 
187 		dapl_dbg_log(DAPL_DBG_TYPE_EP,
188 		    "--> dapl_ep_free: Free EP: %x, ep %p\n",
189 		    ep_ptr->param.ep_state, ep_ptr);
190 		/*
191 		 * Free the QP. If the EP has never been used,
192 		 * the QP is invalid
193 		 */
194 		if (ep_ptr->qp_handle != IB_INVALID_HANDLE) {
195 			dat_status = dapls_ib_qp_free(ia_ptr, ep_ptr);
196 			/*
197 			 * This should always succeed, but report to the user if
198 			 * there is a problem
199 			 */
200 			if (dat_status != DAT_SUCCESS) {
201 				goto bail;
202 			}
203 			ep_ptr->qp_handle = IB_INVALID_HANDLE;
204 		}
205 		dapl_os_unlock(&ep_ptr->header.lock);
206 		/* Free the resource */
207 		dapl_ep_dealloc(ep_ptr);
208 	} else {
209 		dapl_os_unlock(&ep_ptr->header.lock);
210 	}
211 bail:
212 	return (dat_status);
213 
214 }
215