2 * Copyright (c) 2005 Topspin Communications. 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
42 struct mlx4_db_page *prev, *next;
46 unsigned long free[0];
49 static const int db_size[] = {
50 [MLX4_DB_TYPE_CQ] = 8,
51 [MLX4_DB_TYPE_RQ] = 4,
54 static struct mlx4_db_page *__add_page(struct mlx4_context *context,
55 enum mlx4_db_type type)
57 struct mlx4_db_page *page;
58 int ps = to_mdev(context->ibv_ctx.device)->page_size;
62 pp = ps / db_size[type];
64 page = malloc(sizeof *page + pp / 8);
68 if (mlx4_alloc_buf(&page->buf, ps, ps)) {
75 for (i = 0; i < pp / (sizeof (long) * 8); ++i)
79 page->next = context->db_list[type];
80 context->db_list[type] = page;
82 page->next->prev = page;
87 uint32_t *mlx4_alloc_db(struct mlx4_context *context, enum mlx4_db_type type)
89 struct mlx4_db_page *page;
93 pthread_mutex_lock(&context->db_list_mutex);
95 for (page = context->db_list[type]; page; page = page->next)
96 if (page->use_cnt < page->num_db)
99 page = __add_page(context, type);
106 for (i = 0; !page->free[i]; ++i)
109 j = ffsl(page->free[i]);
110 page->free[i] &= ~(1UL << (j - 1));
111 db = page->buf.buf + (i * 8 * sizeof (long) + (j - 1)) * db_size[type];
114 pthread_mutex_unlock(&context->db_list_mutex);
119 void mlx4_free_db(struct mlx4_context *context, enum mlx4_db_type type, uint32_t *db)
121 struct mlx4_db_page *page;
122 uintptr_t ps = to_mdev(context->ibv_ctx.device)->page_size;
125 pthread_mutex_lock(&context->db_list_mutex);
127 for (page = context->db_list[type]; page; page = page->next)
128 if (((uintptr_t) db & ~(ps - 1)) == (uintptr_t) page->buf.buf)
134 i = ((void *) db - page->buf.buf) / db_size[type];
135 page->free[i / (8 * sizeof (long))] |= 1UL << (i % (8 * sizeof (long)));
137 if (!--page->use_cnt) {
139 page->prev->next = page->next;
141 context->db_list[type] = page->next;
143 page->next->prev = page->prev;
145 mlx4_free_buf(&page->buf);
150 pthread_mutex_unlock(&context->db_list_mutex);