xref: /illumos-gate/usr/src/lib/udapl/udapl_tavor/common/dapl_evd_resize.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 2005 Sun Microsystems, Inc.  All rights reserved.
28  * Use is subject to license terms.
29  */
30 
31 /*
32  *
33  * MODULE: dapl_evd_resize.c
34  *
35  * PURPOSE: EVENT management
36  *
37  * Description: Interfaces in this file are completely defined in
38  *              the uDAPL 1.1 API, Chapter 6, section 3
39  *
40  * $Id: dapl_evd_resize.c,v 1.4 2003/06/16 17:53:32 sjs2 Exp $
41  */
42 
43 #include "dapl_evd_util.h"
44 #include "dapl_ring_buffer_util.h"
45 #include "dapl_adapter_util.h"
46 #include "dapl.h"
47 
48 #define	DAPL_MIN_RESZ_QLEN	4096
49 
50 /*
51  * dapl_evd_resize
52  *
53  * DAPL Requirements Version xxx, 6.3.2.5
54  *
55  * Modify the size fo the event queue of an Event Dispatcher
56  *
57  * Input:
58  * 	evd_handle
59  * 	evd_qlen
60  *
61  * Output:
62  * 	none
63  *
64  * Returns:
65  * 	DAT_SUCCESS
66  * 	DAT_INVALID_PARAMETER
67  * 	DAT_INSUFFICIENT_RESOURCES
68  * 	DAT_INVALID_STATE
69  */
70 
dapl_evd_resize(IN DAT_EVD_HANDLE evd_handle,IN DAT_COUNT req_evd_qlen)71 DAT_RETURN dapl_evd_resize(
72 	IN	DAT_EVD_HANDLE	   evd_handle,
73 	IN	DAT_COUNT	   req_evd_qlen)
74 {
75 	int			i;
76 	DAPL_EVD		*evd_ptr;
77 	DAT_EVENT		*event_ptr;
78 	DAT_EVENT		*eventp;
79 	DAT_EVENT		*event;
80 	DAT_EVENT		*new_event;
81 	DAPL_RING_BUFFER	free_event_queue;
82 	DAPL_RING_BUFFER	pending_event_queue;
83 	DAT_RETURN		dat_status;
84 	DAT_COUNT		max_evd_qlen;
85 	DAT_COUNT		evd_qlen;
86 
87 	evd_ptr = (DAPL_EVD *)evd_handle;
88 	dat_status = DAT_SUCCESS;
89 
90 	if (DAPL_BAD_HANDLE(evd_handle, DAPL_MAGIC_EVD)) {
91 		return (DAT_ERROR(DAT_INVALID_HANDLE, 0));
92 	}
93 
94 	if (req_evd_qlen < evd_ptr->qlen) {
95 		return (DAT_ERROR(DAT_INVALID_STATE, 0));
96 	}
97 
98 	if (req_evd_qlen == evd_ptr->qlen) {
99 		return (DAT_SUCCESS);
100 	}
101 
102 	max_evd_qlen = evd_ptr->header.owner_ia->hca_ptr->ia_attr.max_evd_qlen;
103 	if (req_evd_qlen > max_evd_qlen) {
104 		return (DAT_ERROR(DAT_INVALID_STATE, 0));
105 	}
106 
107 	evd_qlen = DAPL_MIN_RESZ_QLEN;
108 	while (req_evd_qlen > evd_qlen) {
109 		evd_qlen <<= 1;
110 		if (evd_qlen > max_evd_qlen)
111 			evd_qlen = max_evd_qlen;
112 	}
113 
114 	/* Allocate EVENTs */
115 	event_ptr = (DAT_EVENT *) dapl_os_alloc(evd_qlen * sizeof (DAT_EVENT));
116 	if (!event_ptr) {
117 		return (DAT_ERROR(DAT_INSUFFICIENT_RESOURCES,
118 		    DAT_RESOURCE_MEMORY));
119 	}
120 
121 	/* allocate free event queue */
122 	dat_status = dapls_rbuf_alloc(&free_event_queue, evd_qlen);
123 	if (dat_status != DAT_SUCCESS) {
124 		goto bail;
125 	}
126 
127 	/* allocate pending event queue */
128 	dat_status = dapls_rbuf_alloc(&pending_event_queue, evd_qlen);
129 	if (dat_status != DAT_SUCCESS) {
130 		goto bail;
131 	}
132 
133 	/* need to resize the cq only for DTO/BIND evds */
134 	if (0 != (evd_ptr->evd_flags & ~ (DAT_EVD_SOFTWARE_FLAG |
135 	    DAT_EVD_CONNECTION_FLAG | DAT_EVD_CR_FLAG))) {
136 		dat_status = dapls_ib_cq_resize(evd_ptr, evd_qlen);
137 		if (dat_status != DAT_SUCCESS)
138 			goto bail;
139 	}
140 
141 	/* add events to free event queue */
142 	for (i = 0, eventp = event_ptr; i < evd_qlen; i++) {
143 		(void) dapls_rbuf_add(&free_event_queue, (void *)eventp);
144 		eventp++;
145 	}
146 	/*
147 	 * copy pending events from evd to the new pending event queue
148 	 */
149 	while (event = (DAT_EVENT *)
150 	    dapls_rbuf_remove(&evd_ptr->pending_event_queue)) {
151 		new_event = (DAT_EVENT *) dapls_rbuf_remove(&free_event_queue);
152 		dapl_os_assert(new_event);
153 		(void) dapl_os_memcpy(new_event, event, sizeof (DAT_EVENT));
154 		dat_status = dapls_rbuf_add(&pending_event_queue, new_event);
155 		dapl_os_assert(dat_status == DAT_SUCCESS);
156 		dat_status = dapls_rbuf_add(&evd_ptr->free_event_queue, event);
157 		dapl_os_assert(dat_status == DAT_SUCCESS);
158 	}
159 
160 	dapls_rbuf_destroy(&evd_ptr->free_event_queue);
161 	dapls_rbuf_destroy(&evd_ptr->pending_event_queue);
162 	if (evd_ptr->events) {
163 		dapl_os_free(evd_ptr->events,
164 		    evd_ptr->qlen * sizeof (DAT_EVENT));
165 	}
166 	evd_ptr->events = event_ptr;
167 	evd_ptr->free_event_queue = free_event_queue;
168 	evd_ptr->pending_event_queue = pending_event_queue;
169 	evd_ptr->qlen = evd_qlen;
170 
171 	return (DAT_SUCCESS);
172 bail:
173 	/*
174 	 * If we are here means event_ptr was allocd but something else
175 	 * failed
176 	 */
177 	dapl_os_free(event_ptr, evd_qlen * sizeof (DAT_EVENT));
178 	dapls_rbuf_destroy(&free_event_queue);
179 	dapls_rbuf_destroy(&pending_event_queue);
180 
181 	return (dat_status);
182 }
183