2 * Copyright (c) 2007 Cisco, Inc. All rights reserved.
4 * This software is available to you under a choice of one of two
5 * licenses. You may choose to be licensed under the terms of the GNU
6 * General Public License (GPL) Version 2, available from the file
7 * COPYING in the main directory of this source tree, or the
8 * OpenIB.org BSD license below:
10 * Redistribution and use in source and binary forms, with or
11 * without modification, are permitted provided that the following
14 * - Redistributions of source code must retain the above
15 * copyright notice, this list of conditions and the following
18 * - Redistributions in binary form must reproduce the above
19 * copyright notice, this list of conditions and the following
20 * disclaimer in the documentation and/or other materials
21 * provided with the distribution.
23 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
27 * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
28 * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
29 * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
35 #endif /* HAVE_CONFIG_H */
38 #include <netinet/in.h>
46 static void *get_wqe(struct mlx4_srq *srq, int n)
48 return srq->buf.buf + (n << srq->wqe_shift);
51 void mlx4_free_srq_wqe(struct mlx4_srq *srq, int ind)
53 struct mlx4_wqe_srq_next_seg *next;
55 pthread_spin_lock(&srq->lock);
57 next = get_wqe(srq, srq->tail);
58 next->next_wqe_index = htons(ind);
61 pthread_spin_unlock(&srq->lock);
64 int mlx4_post_srq_recv(struct ibv_srq *ibsrq,
65 struct ibv_recv_wr *wr,
66 struct ibv_recv_wr **bad_wr)
68 struct mlx4_srq *srq = to_msrq(ibsrq);
69 struct mlx4_wqe_srq_next_seg *next;
70 struct mlx4_wqe_data_seg *scat;
75 pthread_spin_lock(&srq->lock);
77 for (nreq = 0; wr; ++nreq, wr = wr->next) {
78 if (wr->num_sge > srq->max_gs) {
84 if (srq->head == srq->tail) {
91 srq->wrid[srq->head] = wr->wr_id;
93 next = get_wqe(srq, srq->head);
94 srq->head = ntohs(next->next_wqe_index);
95 scat = (struct mlx4_wqe_data_seg *) (next + 1);
97 for (i = 0; i < wr->num_sge; ++i) {
98 scat[i].byte_count = htonl(wr->sg_list[i].length);
99 scat[i].lkey = htonl(wr->sg_list[i].lkey);
100 scat[i].addr = htonll(wr->sg_list[i].addr);
103 if (i < srq->max_gs) {
104 scat[i].byte_count = 0;
105 scat[i].lkey = htonl(MLX4_INVALID_LKEY);
111 srq->counter += nreq;
114 * Make sure that descriptors are written before
115 * we write doorbell record.
119 *srq->db = htonl(srq->counter);
122 pthread_spin_unlock(&srq->lock);
127 int mlx4_alloc_srq_buf(struct ibv_pd *pd, struct ibv_srq_attr *attr,
128 struct mlx4_srq *srq)
130 struct mlx4_wqe_srq_next_seg *next;
131 struct mlx4_wqe_data_seg *scatter;
136 srq->wrid = malloc(srq->max * sizeof (uint64_t));
140 size = sizeof (struct mlx4_wqe_srq_next_seg) +
141 srq->max_gs * sizeof (struct mlx4_wqe_data_seg);
143 for (srq->wqe_shift = 5; 1 << srq->wqe_shift < size; ++srq->wqe_shift)
146 buf_size = srq->max << srq->wqe_shift;
148 if (mlx4_alloc_buf(&srq->buf, buf_size,
149 to_mdev(pd->context->device)->page_size)) {
154 memset(srq->buf.buf, 0, buf_size);
157 * Now initialize the SRQ buffer so that all of the WQEs are
158 * linked into the list of free WQEs.
161 for (i = 0; i < srq->max; ++i) {
162 next = get_wqe(srq, i);
163 next->next_wqe_index = htons((i + 1) & (srq->max - 1));
165 for (scatter = (void *) (next + 1);
166 (void *) scatter < (void *) next + (1 << srq->wqe_shift);
168 scatter->lkey = htonl(MLX4_INVALID_LKEY);
172 srq->tail = srq->max - 1;
177 struct mlx4_srq *mlx4_find_xrc_srq(struct mlx4_context *ctx, uint32_t xrc_srqn)
179 int tind = (xrc_srqn & (ctx->num_xrc_srqs - 1)) >> ctx->xrc_srq_table_shift;
181 if (ctx->xrc_srq_table[tind].refcnt)
182 return ctx->xrc_srq_table[tind].table[xrc_srqn & ctx->xrc_srq_table_mask];
187 int mlx4_store_xrc_srq(struct mlx4_context *ctx, uint32_t xrc_srqn,
188 struct mlx4_srq *srq)
190 int tind = (xrc_srqn & (ctx->num_xrc_srqs - 1)) >> ctx->xrc_srq_table_shift;
193 pthread_mutex_lock(&ctx->xrc_srq_table_mutex);
195 if (!ctx->xrc_srq_table[tind].refcnt) {
196 ctx->xrc_srq_table[tind].table = calloc(ctx->xrc_srq_table_mask + 1,
197 sizeof(struct mlx4_srq *));
198 if (!ctx->xrc_srq_table[tind].table) {
204 ++ctx->xrc_srq_table[tind].refcnt;
205 ctx->xrc_srq_table[tind].table[xrc_srqn & ctx->xrc_srq_table_mask] = srq;
208 pthread_mutex_unlock(&ctx->xrc_srq_table_mutex);
212 void mlx4_clear_xrc_srq(struct mlx4_context *ctx, uint32_t xrc_srqn)
214 int tind = (xrc_srqn & (ctx->num_xrc_srqs - 1)) >> ctx->xrc_srq_table_shift;
216 pthread_mutex_lock(&ctx->xrc_srq_table_mutex);
218 if (!--ctx->xrc_srq_table[tind].refcnt)
219 free(ctx->xrc_srq_table[tind].table);
221 ctx->xrc_srq_table[tind].table[xrc_srqn & ctx->xrc_srq_table_mask] = NULL;
223 pthread_mutex_unlock(&ctx->xrc_srq_table_mutex);