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  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
23  * Use is subject to license terms.
24  */
25 /*
26  * Copyright (c) 2005 SilverStorm Technologies, Inc. All rights reserved.
27  *
28  * This software is available to you under a choice of one of two
29  * licenses.  You may choose to be licensed under the terms of the GNU
30  * General Public License (GPL) Version 2, available from the file
31  * COPYING in the main directory of this source tree, or the
32  * OpenIB.org BSD license below:
33  *
34  *     Redistribution and use in source and binary forms, with or
35  *     without modification, are permitted provided that the following
36  *     conditions are met:
37  *
38  *	- Redistributions of source code must retain the above
39  *	  copyright notice, this list of conditions and the following
40  *	  disclaimer.
41  *
42  *	- Redistributions in binary form must reproduce the above
43  *	  copyright notice, this list of conditions and the following
44  *	  disclaimer in the documentation and/or other materials
45  *	  provided with the distribution.
46  *
47  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
48  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
49  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
50  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
51  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
52  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
53  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
54  * SOFTWARE.
55  *
56  */
57 /*
58  * Sun elects to include this software in Sun product
59  * under the OpenIB BSD license.
60  *
61  *
62  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
63  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
64  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
65  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
66  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
67  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
68  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
69  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
70  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
71  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
72  * POSSIBILITY OF SUCH DAMAGE.
73  */
74 
75 #ifndef _RDSIB_BUF_H
76 #define	_RDSIB_BUF_H
77 
78 #ifdef __cplusplus
79 extern "C" {
80 #endif
81 
82 typedef enum rds_sendbuf_state_s {
83 	RDS_SNDBUF_FREE			= 0,
84 	RDS_SNDBUF_PENDING		= 1,
85 	RDS_SNDBUF_ERROR		= 2
86 } rds_sendbuf_state_t;
87 
88 /* Receive buffer states */
89 typedef enum rds_recvbuf_state_s {
90 	RDS_RCVBUF_FREE		= 0,
91 	RDS_RCVBUF_POSTED	= 1,
92 	RDS_RCVBUF_ONSOCKQ	= 2
93 } rds_recvbuf_state_t;
94 
95 /*
96  * RDS Buffer
97  *
98  * nextp - Ptr to the next buffer
99  * ep - Endpoint that is using this buffer
100  * ds - Data segment for SGL
101  * state - rds_sendbuf_state for send buffers and rds_recvbuf_state for
102  *         receive buffers.
103  * frtn - Message freeing routine, for use by esballoc(9F), only used
104  *        by receive buffers
105  */
106 typedef struct rds_buf_s {
107 	struct rds_buf_s	*buf_nextp;
108 	struct rds_ep_s		*buf_ep;
109 	ibt_wr_ds_t		buf_ds;
110 	uint8_t			buf_state;
111 	frtn_t			buf_frtn;
112 } rds_buf_t;
113 
114 /*
115  * RDS Buffer pool
116  *
117  * lock - Synchronize access
118  * nbuffers - SQ depth for send buffer pool and RQ depth for receive buffer
119  *	pool
120  * nbusy - Number of buffers in the SQ or RQ
121  * nfree - Number of buffers in the pool(between headp and tailp).
122  * headp - First available buffer
123  * tailp - Last available buffer
124  * memp - pointer to the memory allocated for the buffer pool,
125  *        valid only for send pools.
126  * memsize - size of the memory allocated (valid for send pools only).
127  * cv - condition variable to wait for buffers
128  * cv_count - Number of buffers that are being waited on.
129  * sqpoll_pending - Flag to indicate that sendCQ handler is running.
130  *
131  * cv, cv_count and sqpoll_pending are only used when 'rds_no_interrupts'
132  * is set.
133  */
134 typedef struct rds_bufpool_s {
135 	kmutex_t		pool_lock;
136 	uint32_t		pool_nbuffers;
137 	uint32_t		pool_nbusy;
138 	uint32_t		pool_nfree;
139 	rds_buf_t		*pool_headp;
140 	rds_buf_t		*pool_tailp;
141 	uint8_t			*pool_memp;
142 	uint_t			pool_memsize;
143 	rds_buf_t		*pool_bufmemp;
144 	kcondvar_t		pool_cv;
145 	uint_t			pool_cv_count;
146 	boolean_t		pool_sqpoll_pending;
147 } rds_bufpool_t;
148 
149 /* Global pools of buffers */
150 extern rds_bufpool_t	rds_dpool; /* data pool */
151 extern rds_bufpool_t	rds_cpool; /* ctrl pool */
152 
153 /* defined in rds_buf.c */
154 int rds_init_recv_caches(rds_state_t *statep);
155 void rds_free_recv_caches(rds_state_t *statep);
156 int rds_init_send_pool(struct rds_ep_s *ep, ib_guid_t hca_guid);
157 int rds_reinit_send_pool(struct rds_ep_s *ep, ib_guid_t hca_guid);
158 void rds_free_send_pool(struct rds_ep_s *ep);
159 int rds_init_recv_pool(struct rds_ep_s *ep);
160 void rds_free_recv_pool(struct rds_ep_s *ep);
161 void rds_free_buf(rds_bufpool_t *pool, rds_buf_t *bp, uint_t nbuf);
162 rds_buf_t *rds_get_buf(rds_bufpool_t *pool, uint_t nbuf, uint_t *nret);
163 rds_buf_t *rds_get_send_buf(struct rds_ep_s *ep, uint_t nbufs);
164 void rds_free_send_buf(struct rds_ep_s *ep, rds_buf_t *headp,
165     rds_buf_t *tailp, uint_t nbuf, boolean_t lock);
166 void rds_free_recv_buf(rds_buf_t *bp, uint_t nbuf);
167 boolean_t rds_is_sendq_empty(struct rds_ep_s *ep, uint_t);
168 boolean_t rds_is_recvq_empty(struct rds_ep_s *ep, boolean_t);
169 
170 #ifdef __cplusplus
171 }
172 #endif
173 
174 #endif	/* _RDSIB_BUF_H */
175